Changeset aa9562a3a70baf8f98e7db47060d32f9d5e0e177
- Timestamp:
- 09/08/08 11:32:15 (4 months ago)
- git-parent:
- Files:
-
- src/network/getaddrinfo.c (modified) (1 diff)
- src/network/io.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
src/network/getaddrinfo.c
rfe1fdaf raa9562a 689 689 freeaddrinfo (infos); 690 690 } 691 692 /** 693 * inet_pton() replacement 694 */ 695 int vlc_inet_pton (int af, const char *src, void *dst) 696 { 697 #ifndef HAVE_INET_PTON 698 /* Windows Vista has inet_pton(), but not XP. */ 699 /* We have a pretty good example of abstraction inversion here... */ 700 struct addrinfo hints = { 701 .ai_family = af, 702 .ai_socktype = SOCK_DGRAM, /* make sure we have... */ 703 .ai_protocol = IPPROTO_UDP, /* ...only one response */ 704 .ai_flags = AI_NUMERICHOST, 705 }, *res; 706 707 if (getaddrinfo (src, NULL, &hints, &res)) 708 return -1; 709 710 const void *data; 711 size_t len; 712 713 switch (af) 714 { 715 case AF_INET: 716 data = &((const struct sockaddr_in *)res->ai_addr)->sin_addr; 717 len = sizeof (struct in_addr); 718 break; 719 #ifdef AF_INET6 720 case AF_INET6: 721 data = &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 722 len = sizeof (struct in6_addr); 723 break; 724 #endif 725 default: 726 return -1; 727 } 728 memcpy (dst, data, len); 729 return 0; 730 #else /* HAVE_INET_PTON */ 731 return inet_pton( af, src, dst ); 732 #endif /* HAVE_INET_PTON */ 733 } 734 735 /** 736 * inet_ntop() replacement 737 */ 738 const char *vlc_inet_ntop (int af, const void *src, char *dst, socklen_t cnt) 739 { 740 #ifndef HAVE_INET_NTOP 741 int ret = EAI_FAMILY; 742 743 switch (af) 744 { 745 #ifdef AF_INET6 746 case AF_INET6: 747 { 748 struct sockaddr_in6 addr; 749 memset (&addr, 0, sizeof(addr)); 750 addr.sin6_family = AF_INET6; 751 addr.sin6_addr = *(struct in6_addr *)src; 752 ret = getnameinfo ((struct sockaddr *)&addr, sizeof (addr), 753 dst, cnt, NULL, 0, NI_NUMERICHOST); 754 } 755 756 #endif 757 case AF_INET: 758 { 759 struct sockaddr_in addr; 760 memset(&addr, 0, sizeof(addr)); 761 addr.sin_family = AF_INET; 762 addr.sin_addr = *(struct in_addr *)src; 763 ret = getnameinfo ((struct sockaddr *)&addr, sizeof (addr), 764 dst, cnt, NULL, 0, NI_NUMERICHOST); 765 } 766 } 767 return (ret == 0) ? dst : NULL; 768 #else /* HAVE_INET_NTOP */ 769 return inet_ntop( af, src, dst, cnt ); 770 #endif /* HAVE_INET_NTOP */ 771 } 772 src/network/io.c
r8cec5b2 raa9562a 33 33 #endif 34 34 35 #define _WIN32_WINNT 0x050136 35 #include <vlc_common.h> 37 36 … … 553 552 } 554 553 555 556 /*****************************************************************************557 * inet_pton replacement for obsolete and/or crap operating systems558 *****************************************************************************/559 int vlc_inet_pton(int af, const char *src, void *dst)560 {561 #ifndef HAVE_INET_PTON562 /* Windows Vista has inet_pton(), but not XP. */563 /* We have a pretty good example of abstraction inversion here... */564 struct addrinfo hints = {565 .ai_family = af,566 .ai_socktype = SOCK_DGRAM, /* make sure we have... */567 .ai_protocol = IPPROTO_UDP, /* ...only one response */568 .ai_flags = AI_NUMERICHOST,569 }, *res;570 571 if (getaddrinfo (src, NULL, &hints, &res))572 return -1;573 574 const void *data;575 size_t len;576 577 switch (af)578 {579 case AF_INET:580 data = &((const struct sockaddr_in *)res->ai_addr)->sin_addr;581 len = 4;582 break;583 #ifdef AF_INET6584 case AF_INET6:585 data = &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr;586 len = 16;587 break;588 #endif589 default:590 errno = EAFNOSUPPORT;591 return -1;592 }593 memcpy (dst, data, len);594 return 0;595 #else /* HAVE_INET_PTON */596 return inet_pton( af, src, dst );597 #endif /* HAVE_INET_PTON */598 }599 600 const char *vlc_inet_ntop(int af, const void * src,601 char * dst, socklen_t cnt)602 {603 #ifndef HAVE_INET_NTOP604 #ifdef WIN32605 switch( af )606 {607 #ifdef AF_INET6608 case AF_INET6:609 {610 struct sockaddr_in6 addr;611 memset(&addr, 0, sizeof(addr));612 addr.sin6_family = AF_INET6;613 addr.sin6_addr = *((struct in6_addr*)src);614 if( 0 == WSAAddressToStringA((LPSOCKADDR)&addr,615 sizeof(struct sockaddr_in6),616 NULL, dst, &cnt) )617 {618 dst[cnt] = '\0';619 return dst;620 }621 errno = WSAGetLastError();622 return NULL;623 624 }625 626 #endif627 case AF_INET:628 {629 struct sockaddr_in addr;630 memset(&addr, 0, sizeof(addr));631 addr.sin_family = AF_INET;632 addr.sin_addr = *((struct in_addr*)src);633 if( 0 == WSAAddressToStringA((LPSOCKADDR)&addr,634 sizeof(struct sockaddr_in),635 NULL, dst, &cnt) )636 {637 dst[cnt] = '\0';638 return dst;639 }640 errno = WSAGetLastError();641 return NULL;642 643 }644 }645 errno = EAFNOSUPPORT;646 return NULL;647 #else /* WIN32 */648 return NULL;649 #endif /* WIN32 */650 #else /* HAVE_INET_NTOP */651 return inet_ntop( af, src, dst, cnt );652 #endif /* HAVE_INET_NTOP */653 }654 655 554 #ifdef WIN32 656 555 /* vlc_sendmsg, vlc_recvmsg Defined in winsock.c */
