andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "primpl.h" andre@0: andre@0: #include andre@0: andre@0: /* andre@0: * On Unix, the error code for gethostbyname() and gethostbyaddr() andre@0: * is returned in the global variable h_errno, instead of the usual andre@0: * errno. andre@0: */ andre@0: #if defined(XP_UNIX) andre@0: #if defined(_PR_NEED_H_ERRNO) andre@0: extern int h_errno; andre@0: #endif andre@0: #define _MD_GETHOST_ERRNO() h_errno andre@0: #else andre@0: #define _MD_GETHOST_ERRNO() _MD_ERRNO() andre@0: #endif andre@0: andre@0: /* andre@0: * The meaning of the macros related to gethostbyname, gethostbyaddr, andre@0: * and gethostbyname2 is defined below. andre@0: * - _PR_HAVE_THREADSAFE_GETHOST: the gethostbyXXX functions return andre@0: * the result in thread specific storage. For example, AIX, HP-UX, andre@0: * and OSF1. andre@0: * - _PR_HAVE_GETHOST_R: have the gethostbyXXX_r functions. See next andre@0: * two macros. andre@0: * - _PR_HAVE_GETHOST_R_INT: the gethostbyXXX_r functions return an andre@0: * int. For example, Linux glibc. andre@0: * - _PR_HAVE_GETHOST_R_POINTER: the gethostbyXXX_r functions return andre@0: * a struct hostent* pointer. For example, Solaris and IRIX. andre@0: */ andre@0: #if defined(_PR_NO_PREEMPT) || defined(_PR_HAVE_GETHOST_R) \ andre@0: || defined(_PR_HAVE_THREADSAFE_GETHOST) andre@0: #define _PR_NO_DNS_LOCK andre@0: #endif andre@0: andre@0: #if defined(_PR_NO_DNS_LOCK) andre@0: #define LOCK_DNS() andre@0: #define UNLOCK_DNS() andre@0: #else andre@0: PRLock *_pr_dnsLock = NULL; andre@0: #define LOCK_DNS() PR_Lock(_pr_dnsLock) andre@0: #define UNLOCK_DNS() PR_Unlock(_pr_dnsLock) andre@0: #endif /* defined(_PR_NO_DNS_LOCK) */ andre@0: andre@0: /* andre@0: * Some platforms have the reentrant getprotobyname_r() and andre@0: * getprotobynumber_r(). However, they come in three flavors. andre@0: * Some return a pointer to struct protoent, others return andre@0: * an int, and glibc's flavor takes five arguments. andre@0: */ andre@0: #if defined(XP_BEOS) && defined(BONE_VERSION) andre@0: #include /* pick up define for inet_addr */ andre@0: #include andre@0: #define _PR_HAVE_GETPROTO_R andre@0: #define _PR_HAVE_GETPROTO_R_POINTER andre@0: #endif andre@0: andre@0: #if defined(SOLARIS) || (defined(BSDI) && defined(_REENTRANT)) \ andre@0: || (defined(LINUX) && defined(_REENTRANT) \ andre@0: && !(defined(__GLIBC__) && __GLIBC__ >= 2) \ andre@0: && !defined(ANDROID)) andre@0: #define _PR_HAVE_GETPROTO_R andre@0: #define _PR_HAVE_GETPROTO_R_POINTER andre@0: #endif andre@0: andre@0: #if defined(OSF1) \ andre@0: || defined(AIX4_3_PLUS) || (defined(AIX) && defined(_THREAD_SAFE)) \ andre@0: || (defined(HPUX10_10) && defined(_REENTRANT)) \ andre@0: || (defined(HPUX10_20) && defined(_REENTRANT)) \ andre@0: || defined(OPENBSD) andre@0: #define _PR_HAVE_GETPROTO_R andre@0: #define _PR_HAVE_GETPROTO_R_INT andre@0: #endif andre@0: andre@0: #if __FreeBSD_version >= 602000 andre@0: #define _PR_HAVE_GETPROTO_R andre@0: #define _PR_HAVE_5_ARG_GETPROTO_R andre@0: #endif andre@0: andre@0: /* BeOS has glibc but not the glibc-style getprotobyxxx_r functions. */ andre@0: #if (defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(XP_BEOS)) andre@0: #define _PR_HAVE_GETPROTO_R andre@0: #define _PR_HAVE_5_ARG_GETPROTO_R andre@0: #endif andre@0: andre@0: #if !defined(_PR_HAVE_GETPROTO_R) andre@0: PRLock* _getproto_lock = NULL; andre@0: #endif andre@0: andre@0: #if defined(_PR_INET6_PROBE) andre@0: extern PRBool _pr_ipv6_is_present(void); andre@0: #endif andre@0: andre@0: #define _PR_IN6_IS_ADDR_UNSPECIFIED(a) \ andre@0: (((a)->pr_s6_addr32[0] == 0) && \ andre@0: ((a)->pr_s6_addr32[1] == 0) && \ andre@0: ((a)->pr_s6_addr32[2] == 0) && \ andre@0: ((a)->pr_s6_addr32[3] == 0)) andre@0: andre@0: #define _PR_IN6_IS_ADDR_LOOPBACK(a) \ andre@0: (((a)->pr_s6_addr32[0] == 0) && \ andre@0: ((a)->pr_s6_addr32[1] == 0) && \ andre@0: ((a)->pr_s6_addr32[2] == 0) && \ andre@0: ((a)->pr_s6_addr[12] == 0) && \ andre@0: ((a)->pr_s6_addr[13] == 0) && \ andre@0: ((a)->pr_s6_addr[14] == 0) && \ andre@0: ((a)->pr_s6_addr[15] == 0x1U)) andre@0: andre@0: const PRIPv6Addr _pr_in6addr_any = {{{ 0, 0, 0, 0, andre@0: 0, 0, 0, 0, andre@0: 0, 0, 0, 0, andre@0: 0, 0, 0, 0 }}}; andre@0: andre@0: const PRIPv6Addr _pr_in6addr_loopback = {{{ 0, 0, 0, 0, andre@0: 0, 0, 0, 0, andre@0: 0, 0, 0, 0, andre@0: 0, 0, 0, 0x1U }}}; andre@0: /* andre@0: * The values at bytes 10 and 11 are compared using pointers to andre@0: * 8-bit fields, and not 32-bit fields, to make the comparison work on andre@0: * both big-endian and little-endian systems andre@0: */ andre@0: andre@0: #define _PR_IN6_IS_ADDR_V4MAPPED(a) \ andre@0: (((a)->pr_s6_addr32[0] == 0) && \ andre@0: ((a)->pr_s6_addr32[1] == 0) && \ andre@0: ((a)->pr_s6_addr[8] == 0) && \ andre@0: ((a)->pr_s6_addr[9] == 0) && \ andre@0: ((a)->pr_s6_addr[10] == 0xff) && \ andre@0: ((a)->pr_s6_addr[11] == 0xff)) andre@0: andre@0: #define _PR_IN6_IS_ADDR_V4COMPAT(a) \ andre@0: (((a)->pr_s6_addr32[0] == 0) && \ andre@0: ((a)->pr_s6_addr32[1] == 0) && \ andre@0: ((a)->pr_s6_addr32[2] == 0)) andre@0: andre@0: #define _PR_IN6_V4MAPPED_TO_IPADDR(a) ((a)->pr_s6_addr32[3]) andre@0: andre@0: #if defined(_PR_INET6) && defined(_PR_HAVE_GETHOSTBYNAME2) andre@0: andre@0: /* andre@0: * The _pr_QueryNetIfs() function finds out if the system has andre@0: * IPv4 or IPv6 source addresses configured and sets _pr_have_inet_if andre@0: * and _pr_have_inet6_if accordingly. andre@0: * andre@0: * We have an implementation using SIOCGIFCONF ioctl and a andre@0: * default implementation that simply sets _pr_have_inet_if andre@0: * and _pr_have_inet6_if to true. A better implementation andre@0: * would be to use the routing sockets (see Chapter 17 of andre@0: * W. Richard Stevens' Unix Network Programming, Vol. 1, 2nd. Ed.) andre@0: */ andre@0: andre@0: static PRLock *_pr_query_ifs_lock = NULL; andre@0: static PRBool _pr_have_inet_if = PR_FALSE; andre@0: static PRBool _pr_have_inet6_if = PR_FALSE; andre@0: andre@0: #undef DEBUG_QUERY_IFS andre@0: andre@0: #if defined(AIX) \ andre@0: || (defined(DARWIN) && (!defined(HAVE_GETIFADDRS) \ andre@0: || (defined(XP_MACOSX) && (!defined(MAC_OS_X_VERSION_10_2) || \ andre@0: MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_2)))) andre@0: andre@0: /* andre@0: * Use SIOCGIFCONF ioctl on platforms that don't have routing andre@0: * sockets. Warning: whether SIOCGIFCONF ioctl returns AF_INET6 andre@0: * network interfaces is not portable. andre@0: * andre@0: * The _pr_QueryNetIfs() function is derived from the code in andre@0: * src/lib/libc/net/getifaddrs.c in BSD Unix and the code in andre@0: * Section 16.6 of W. Richard Stevens' Unix Network Programming, andre@0: * Vol. 1, 2nd. Ed. andre@0: */ andre@0: andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: andre@0: #ifdef DEBUG_QUERY_IFS andre@0: static void andre@0: _pr_PrintIfreq(struct ifreq *ifr) andre@0: { andre@0: PRNetAddr addr; andre@0: struct sockaddr *sa; andre@0: const char* family; andre@0: char addrstr[64]; andre@0: andre@0: sa = &ifr->ifr_addr; andre@0: if (sa->sa_family == AF_INET) { andre@0: struct sockaddr_in *sin = (struct sockaddr_in *)sa; andre@0: family = "inet"; andre@0: memcpy(&addr.inet.ip, &sin->sin_addr, sizeof(sin->sin_addr)); andre@0: } else if (sa->sa_family == AF_INET6) { andre@0: struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa; andre@0: family = "inet6"; andre@0: memcpy(&addr.ipv6.ip, &sin6->sin6_addr, sizeof(sin6->sin6_addr)); andre@0: } else { andre@0: return; /* skip if not AF_INET or AF_INET6 */ andre@0: } andre@0: addr.raw.family = sa->sa_family; andre@0: PR_NetAddrToString(&addr, addrstr, sizeof(addrstr)); andre@0: printf("%s: %s %s\n", ifr->ifr_name, family, addrstr); andre@0: } andre@0: #endif andre@0: andre@0: static void andre@0: _pr_QueryNetIfs(void) andre@0: { andre@0: int sock; andre@0: int rv; andre@0: struct ifconf ifc; andre@0: struct ifreq *ifr; andre@0: struct ifreq *lifr; andre@0: PRUint32 len, lastlen; andre@0: char *buf; andre@0: andre@0: if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { andre@0: return; andre@0: } andre@0: andre@0: /* Issue SIOCGIFCONF request in a loop. */ andre@0: lastlen = 0; andre@0: len = 100 * sizeof(struct ifreq); /* initial buffer size guess */ andre@0: for (;;) { andre@0: buf = (char *)PR_Malloc(len); andre@0: if (NULL == buf) { andre@0: close(sock); andre@0: return; andre@0: } andre@0: ifc.ifc_buf = buf; andre@0: ifc.ifc_len = len; andre@0: rv = ioctl(sock, SIOCGIFCONF, &ifc); andre@0: if (rv < 0) { andre@0: if (errno != EINVAL || lastlen != 0) { andre@0: close(sock); andre@0: PR_Free(buf); andre@0: return; andre@0: } andre@0: } else { andre@0: if (ifc.ifc_len == lastlen) andre@0: break; /* success, len has not changed */ andre@0: lastlen = ifc.ifc_len; andre@0: } andre@0: len += 10 * sizeof(struct ifreq); /* increment */ andre@0: PR_Free(buf); andre@0: } andre@0: close(sock); andre@0: andre@0: ifr = ifc.ifc_req; andre@0: lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len]; andre@0: andre@0: while (ifr < lifr) { andre@0: struct sockaddr *sa; andre@0: int sa_len; andre@0: andre@0: #ifdef DEBUG_QUERY_IFS andre@0: _pr_PrintIfreq(ifr); andre@0: #endif andre@0: sa = &ifr->ifr_addr; andre@0: if (sa->sa_family == AF_INET) { andre@0: struct sockaddr_in *sin = (struct sockaddr_in *) sa; andre@0: if (sin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) { andre@0: _pr_have_inet_if = PR_TRUE; andre@0: } andre@0: } else if (sa->sa_family == AF_INET6) { andre@0: struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa; andre@0: if (!IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) andre@0: && !IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { andre@0: _pr_have_inet6_if = PR_TRUE; andre@0: } andre@0: } andre@0: andre@0: #ifdef _PR_HAVE_SOCKADDR_LEN andre@0: sa_len = PR_MAX(sa->sa_len, sizeof(struct sockaddr)); andre@0: #else andre@0: switch (sa->sa_family) { andre@0: #ifdef AF_LINK andre@0: case AF_LINK: andre@0: sa_len = sizeof(struct sockaddr_dl); andre@0: break; andre@0: #endif andre@0: case AF_INET6: andre@0: sa_len = sizeof(struct sockaddr_in6); andre@0: break; andre@0: default: andre@0: sa_len = sizeof(struct sockaddr); andre@0: break; andre@0: } andre@0: #endif andre@0: ifr = (struct ifreq *)(((char *)sa) + sa_len); andre@0: } andre@0: PR_Free(buf); andre@0: } andre@0: andre@0: #elif (defined(DARWIN) && defined(HAVE_GETIFADDRS)) || defined(FREEBSD) \ andre@0: || defined(NETBSD) || defined(OPENBSD) andre@0: andre@0: /* andre@0: * Use the BSD getifaddrs function. andre@0: */ andre@0: andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: andre@0: #ifdef DEBUG_QUERY_IFS andre@0: static void andre@0: _pr_PrintIfaddrs(struct ifaddrs *ifa) andre@0: { andre@0: struct sockaddr *sa; andre@0: const char* family; andre@0: void *addrp; andre@0: char addrstr[64]; andre@0: andre@0: sa = ifa->ifa_addr; andre@0: if (sa->sa_family == AF_INET) { andre@0: struct sockaddr_in *sin = (struct sockaddr_in *)sa; andre@0: family = "inet"; andre@0: addrp = &sin->sin_addr; andre@0: } else if (sa->sa_family == AF_INET6) { andre@0: struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa; andre@0: family = "inet6"; andre@0: addrp = &sin6->sin6_addr; andre@0: } else { andre@0: return; /* skip if not AF_INET or AF_INET6 */ andre@0: } andre@0: inet_ntop(sa->sa_family, addrp, addrstr, sizeof(addrstr)); andre@0: printf("%s: %s %s\n", ifa->ifa_name, family, addrstr); andre@0: } andre@0: #endif andre@0: andre@0: static void andre@0: _pr_QueryNetIfs(void) andre@0: { andre@0: struct ifaddrs *ifp; andre@0: struct ifaddrs *ifa; andre@0: andre@0: if (getifaddrs(&ifp) == -1) { andre@0: return; andre@0: } andre@0: for (ifa = ifp; ifa; ifa = ifa->ifa_next) { andre@0: struct sockaddr *sa; andre@0: andre@0: #ifdef DEBUG_QUERY_IFS andre@0: _pr_PrintIfaddrs(ifa); andre@0: #endif andre@0: sa = ifa->ifa_addr; andre@0: if (sa->sa_family == AF_INET) { andre@0: struct sockaddr_in *sin = (struct sockaddr_in *) sa; andre@0: if (sin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) { andre@0: _pr_have_inet_if = 1; andre@0: } andre@0: } else if (sa->sa_family == AF_INET6) { andre@0: struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa; andre@0: if (!IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) andre@0: && !IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { andre@0: _pr_have_inet6_if = 1; andre@0: } andre@0: } andre@0: } andre@0: freeifaddrs(ifp); andre@0: } andre@0: andre@0: #else /* default */ andre@0: andre@0: /* andre@0: * Emulate the code in NSPR 4.2 or older. PR_GetIPNodeByName behaves andre@0: * as if the system had both IPv4 and IPv6 source addresses configured. andre@0: */ andre@0: static void andre@0: _pr_QueryNetIfs(void) andre@0: { andre@0: _pr_have_inet_if = PR_TRUE; andre@0: _pr_have_inet6_if = PR_TRUE; andre@0: } andre@0: andre@0: #endif andre@0: andre@0: #endif /* _PR_INET6 && _PR_HAVE_GETHOSTBYNAME2 */ andre@0: andre@0: void _PR_InitNet(void) andre@0: { andre@0: #if defined(XP_UNIX) andre@0: #ifdef HAVE_NETCONFIG andre@0: /* andre@0: * This one-liner prevents the endless re-open's and re-read's of andre@0: * /etc/netconfig on EACH and EVERY call to accept(), connect(), etc. andre@0: */ andre@0: (void)setnetconfig(); andre@0: #endif andre@0: #endif andre@0: #if !defined(_PR_NO_DNS_LOCK) andre@0: _pr_dnsLock = PR_NewLock(); andre@0: #endif andre@0: #if !defined(_PR_HAVE_GETPROTO_R) andre@0: _getproto_lock = PR_NewLock(); andre@0: #endif andre@0: #if defined(_PR_INET6) && defined(_PR_HAVE_GETHOSTBYNAME2) andre@0: _pr_query_ifs_lock = PR_NewLock(); andre@0: #endif andre@0: } andre@0: andre@0: void _PR_CleanupNet(void) andre@0: { andre@0: #if !defined(_PR_NO_DNS_LOCK) andre@0: if (_pr_dnsLock) { andre@0: PR_DestroyLock(_pr_dnsLock); andre@0: _pr_dnsLock = NULL; andre@0: } andre@0: #endif andre@0: #if !defined(_PR_HAVE_GETPROTO_R) andre@0: if (_getproto_lock) { andre@0: PR_DestroyLock(_getproto_lock); andre@0: _getproto_lock = NULL; andre@0: } andre@0: #endif andre@0: #if defined(_PR_INET6) && defined(_PR_HAVE_GETHOSTBYNAME2) andre@0: if (_pr_query_ifs_lock) { andre@0: PR_DestroyLock(_pr_query_ifs_lock); andre@0: _pr_query_ifs_lock = NULL; andre@0: } andre@0: #endif andre@0: } andre@0: andre@0: /* andre@0: ** Allocate space from the buffer, aligning it to "align" before doing andre@0: ** the allocation. "align" must be a power of 2. andre@0: */ andre@0: static char *Alloc(PRIntn amount, char **bufp, PRIntn *buflenp, PRIntn align) andre@0: { andre@0: char *buf = *bufp; andre@0: PRIntn buflen = *buflenp; andre@0: andre@0: if (align && ((long)buf & (align - 1))) { andre@0: PRIntn skip = align - ((ptrdiff_t)buf & (align - 1)); andre@0: if (buflen < skip) { andre@0: return 0; andre@0: } andre@0: buf += skip; andre@0: buflen -= skip; andre@0: } andre@0: if (buflen < amount) { andre@0: return 0; andre@0: } andre@0: *bufp = buf + amount; andre@0: *buflenp = buflen - amount; andre@0: return buf; andre@0: } andre@0: andre@0: typedef enum _PRIPAddrConversion { andre@0: _PRIPAddrNoConversion, andre@0: _PRIPAddrIPv4Mapped, andre@0: _PRIPAddrIPv4Compat andre@0: } _PRIPAddrConversion; andre@0: andre@0: /* andre@0: ** Convert an IPv4 address (v4) to an IPv4-mapped IPv6 address (v6). andre@0: */ andre@0: static void MakeIPv4MappedAddr(const char *v4, char *v6) andre@0: { andre@0: memset(v6, 0, 10); andre@0: memset(v6 + 10, 0xff, 2); andre@0: memcpy(v6 + 12, v4, 4); andre@0: } andre@0: andre@0: /* andre@0: ** Convert an IPv4 address (v4) to an IPv4-compatible IPv6 address (v6). andre@0: */ andre@0: static void MakeIPv4CompatAddr(const char *v4, char *v6) andre@0: { andre@0: memset(v6, 0, 12); andre@0: memcpy(v6 + 12, v4, 4); andre@0: } andre@0: andre@0: /* andre@0: ** Copy a hostent, and all of the memory that it refers to into andre@0: ** (hopefully) stacked buffers. andre@0: */ andre@0: static PRStatus CopyHostent( andre@0: struct hostent *from, andre@0: char **buf, andre@0: PRIntn *bufsize, andre@0: _PRIPAddrConversion conversion, andre@0: PRHostEnt *to) andre@0: { andre@0: PRIntn len, na; andre@0: char **ap; andre@0: andre@0: if (conversion != _PRIPAddrNoConversion andre@0: && from->h_addrtype == AF_INET) { andre@0: PR_ASSERT(from->h_length == 4); andre@0: to->h_addrtype = PR_AF_INET6; andre@0: to->h_length = 16; andre@0: } else { andre@0: #if defined(_PR_INET6) || defined(_PR_INET6_PROBE) andre@0: if (AF_INET6 == from->h_addrtype) andre@0: to->h_addrtype = PR_AF_INET6; andre@0: else andre@0: #endif andre@0: to->h_addrtype = from->h_addrtype; andre@0: to->h_length = from->h_length; andre@0: } andre@0: andre@0: /* Copy the official name */ andre@0: if (!from->h_name) return PR_FAILURE; andre@0: len = strlen(from->h_name) + 1; andre@0: to->h_name = Alloc(len, buf, bufsize, 0); andre@0: if (!to->h_name) return PR_FAILURE; andre@0: memcpy(to->h_name, from->h_name, len); andre@0: andre@0: /* Count the aliases, then allocate storage for the pointers */ andre@0: if (!from->h_aliases) { andre@0: na = 1; andre@0: } else { andre@0: for (na = 1, ap = from->h_aliases; *ap != 0; na++, ap++){;} /* nothing to execute */ andre@0: } andre@0: to->h_aliases = (char**)Alloc( andre@0: na * sizeof(char*), buf, bufsize, sizeof(char**)); andre@0: if (!to->h_aliases) return PR_FAILURE; andre@0: andre@0: /* Copy the aliases, one at a time */ andre@0: if (!from->h_aliases) { andre@0: to->h_aliases[0] = 0; andre@0: } else { andre@0: for (na = 0, ap = from->h_aliases; *ap != 0; na++, ap++) { andre@0: len = strlen(*ap) + 1; andre@0: to->h_aliases[na] = Alloc(len, buf, bufsize, 0); andre@0: if (!to->h_aliases[na]) return PR_FAILURE; andre@0: memcpy(to->h_aliases[na], *ap, len); andre@0: } andre@0: to->h_aliases[na] = 0; andre@0: } andre@0: andre@0: /* Count the addresses, then allocate storage for the pointers */ andre@0: for (na = 1, ap = from->h_addr_list; *ap != 0; na++, ap++){;} /* nothing to execute */ andre@0: to->h_addr_list = (char**)Alloc( andre@0: na * sizeof(char*), buf, bufsize, sizeof(char**)); andre@0: if (!to->h_addr_list) return PR_FAILURE; andre@0: andre@0: /* Copy the addresses, one at a time */ andre@0: for (na = 0, ap = from->h_addr_list; *ap != 0; na++, ap++) { andre@0: to->h_addr_list[na] = Alloc(to->h_length, buf, bufsize, 0); andre@0: if (!to->h_addr_list[na]) return PR_FAILURE; andre@0: if (conversion != _PRIPAddrNoConversion andre@0: && from->h_addrtype == AF_INET) { andre@0: if (conversion == _PRIPAddrIPv4Mapped) { andre@0: MakeIPv4MappedAddr(*ap, to->h_addr_list[na]); andre@0: } else { andre@0: PR_ASSERT(conversion == _PRIPAddrIPv4Compat); andre@0: MakeIPv4CompatAddr(*ap, to->h_addr_list[na]); andre@0: } andre@0: } else { andre@0: memcpy(to->h_addr_list[na], *ap, to->h_length); andre@0: } andre@0: } andre@0: to->h_addr_list[na] = 0; andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: #ifdef SYMBIAN andre@0: /* Set p_aliases by hand because Symbian's getprotobyname() returns NULL. */ andre@0: static void AssignAliases(struct protoent *Protoent, char** aliases) andre@0: { andre@0: if (NULL == Protoent->p_aliases) { andre@0: if (0 == strcmp(Protoent->p_name, "ip")) andre@0: aliases[0] = "IP"; andre@0: else if (0 == strcmp(Protoent->p_name, "tcp")) andre@0: aliases[0] = "TCP"; andre@0: else if (0 == strcmp(Protoent->p_name, "udp")) andre@0: aliases[0] = "UDP"; andre@0: else andre@0: aliases[0] = "UNKNOWN"; andre@0: aliases[1] = NULL; andre@0: Protoent->p_aliases = aliases; andre@0: } andre@0: } andre@0: #endif andre@0: andre@0: #if !defined(_PR_HAVE_GETPROTO_R) andre@0: /* andre@0: ** Copy a protoent, and all of the memory that it refers to into andre@0: ** (hopefully) stacked buffers. andre@0: */ andre@0: static PRStatus CopyProtoent( andre@0: struct protoent *from, char *buf, PRIntn bufsize, PRProtoEnt *to) andre@0: { andre@0: PRIntn len, na; andre@0: char **ap; andre@0: andre@0: /* Do the easy stuff */ andre@0: to->p_num = from->p_proto; andre@0: andre@0: /* Copy the official name */ andre@0: if (!from->p_name) return PR_FAILURE; andre@0: len = strlen(from->p_name) + 1; andre@0: to->p_name = Alloc(len, &buf, &bufsize, 0); andre@0: if (!to->p_name) return PR_FAILURE; andre@0: memcpy(to->p_name, from->p_name, len); andre@0: andre@0: /* Count the aliases, then allocate storage for the pointers */ andre@0: for (na = 1, ap = from->p_aliases; *ap != 0; na++, ap++){;} /* nothing to execute */ andre@0: to->p_aliases = (char**)Alloc( andre@0: na * sizeof(char*), &buf, &bufsize, sizeof(char**)); andre@0: if (!to->p_aliases) return PR_FAILURE; andre@0: andre@0: /* Copy the aliases, one at a time */ andre@0: for (na = 0, ap = from->p_aliases; *ap != 0; na++, ap++) { andre@0: len = strlen(*ap) + 1; andre@0: to->p_aliases[na] = Alloc(len, &buf, &bufsize, 0); andre@0: if (!to->p_aliases[na]) return PR_FAILURE; andre@0: memcpy(to->p_aliases[na], *ap, len); andre@0: } andre@0: to->p_aliases[na] = 0; andre@0: andre@0: return PR_SUCCESS; andre@0: } andre@0: #endif /* !defined(_PR_HAVE_GETPROTO_R) */ andre@0: andre@0: /* andre@0: * ################################################################# andre@0: * NOTE: tmphe, tmpbuf, bufsize, h, and h_err are local variables andre@0: * or arguments of PR_GetHostByName, PR_GetIPNodeByName, and andre@0: * PR_GetHostByAddr. DO NOT CHANGE THE NAMES OF THESE LOCAL andre@0: * VARIABLES OR ARGUMENTS. andre@0: * ################################################################# andre@0: */ andre@0: #if defined(_PR_HAVE_GETHOST_R_INT) andre@0: andre@0: #define GETHOSTBYNAME(name) \ andre@0: (gethostbyname_r(name, &tmphe, tmpbuf, bufsize, &h, &h_err), h) andre@0: #define GETHOSTBYNAME2(name, af) \ andre@0: (gethostbyname2_r(name, af, &tmphe, tmpbuf, bufsize, &h, &h_err), h) andre@0: #define GETHOSTBYADDR(addr, addrlen, af) \ andre@0: (gethostbyaddr_r(addr, addrlen, af, \ andre@0: &tmphe, tmpbuf, bufsize, &h, &h_err), h) andre@0: andre@0: #elif defined(_PR_HAVE_GETHOST_R_POINTER) andre@0: andre@0: #define GETHOSTBYNAME(name) \ andre@0: gethostbyname_r(name, &tmphe, tmpbuf, bufsize, &h_err) andre@0: #define GETHOSTBYNAME2(name, af) \ andre@0: gethostbyname2_r(name, af, &tmphe, tmpbuf, bufsize, &h_err) andre@0: #define GETHOSTBYADDR(addr, addrlen, af) \ andre@0: gethostbyaddr_r(addr, addrlen, af, &tmphe, tmpbuf, bufsize, &h_err) andre@0: andre@0: #else andre@0: andre@0: #define GETHOSTBYNAME(name) gethostbyname(name) andre@0: #define GETHOSTBYNAME2(name, af) gethostbyname2(name, af) andre@0: #define GETHOSTBYADDR(addr, addrlen, af) gethostbyaddr(addr, addrlen, af) andre@0: andre@0: #endif /* definition of GETHOSTBYXXX */ andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_GetHostByName( andre@0: const char *name, char *buf, PRIntn bufsize, PRHostEnt *hp) andre@0: { andre@0: struct hostent *h; andre@0: PRStatus rv = PR_FAILURE; andre@0: #if defined(_PR_HAVE_GETHOST_R) andre@0: char localbuf[PR_NETDB_BUF_SIZE]; andre@0: char *tmpbuf; andre@0: struct hostent tmphe; andre@0: int h_err; andre@0: #endif andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: #if defined(_PR_HAVE_GETHOST_R) andre@0: tmpbuf = localbuf; andre@0: if (bufsize > sizeof(localbuf)) andre@0: { andre@0: tmpbuf = (char *)PR_Malloc(bufsize); andre@0: if (NULL == tmpbuf) andre@0: { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: return rv; andre@0: } andre@0: } andre@0: #endif andre@0: andre@0: LOCK_DNS(); andre@0: andre@0: h = GETHOSTBYNAME(name); andre@0: andre@0: if (NULL == h) andre@0: { andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO()); andre@0: } andre@0: else andre@0: { andre@0: _PRIPAddrConversion conversion = _PRIPAddrNoConversion; andre@0: rv = CopyHostent(h, &buf, &bufsize, conversion, hp); andre@0: if (PR_SUCCESS != rv) andre@0: PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); andre@0: } andre@0: UNLOCK_DNS(); andre@0: #if defined(_PR_HAVE_GETHOST_R) andre@0: if (tmpbuf != localbuf) andre@0: PR_Free(tmpbuf); andre@0: #endif andre@0: return rv; andre@0: } andre@0: andre@0: #if !defined(_PR_INET6) && \ andre@0: defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME) andre@0: typedef struct hostent * (*_pr_getipnodebyname_t)(const char *, int, andre@0: int, int *); andre@0: typedef struct hostent * (*_pr_getipnodebyaddr_t)(const void *, size_t, andre@0: int, int *); andre@0: typedef void (*_pr_freehostent_t)(struct hostent *); andre@0: static void * _pr_getipnodebyname_fp; andre@0: static void * _pr_getipnodebyaddr_fp; andre@0: static void * _pr_freehostent_fp; andre@0: andre@0: /* andre@0: * Look up the addresses of getipnodebyname, getipnodebyaddr, andre@0: * and freehostent. andre@0: */ andre@0: PRStatus andre@0: _pr_find_getipnodebyname(void) andre@0: { andre@0: PRLibrary *lib; andre@0: PRStatus rv; andre@0: #define GETIPNODEBYNAME "getipnodebyname" andre@0: #define GETIPNODEBYADDR "getipnodebyaddr" andre@0: #define FREEHOSTENT "freehostent" andre@0: andre@0: _pr_getipnodebyname_fp = PR_FindSymbolAndLibrary(GETIPNODEBYNAME, &lib); andre@0: if (NULL != _pr_getipnodebyname_fp) { andre@0: _pr_freehostent_fp = PR_FindSymbol(lib, FREEHOSTENT); andre@0: if (NULL != _pr_freehostent_fp) { andre@0: _pr_getipnodebyaddr_fp = PR_FindSymbol(lib, GETIPNODEBYADDR); andre@0: if (NULL != _pr_getipnodebyaddr_fp) andre@0: rv = PR_SUCCESS; andre@0: else andre@0: rv = PR_FAILURE; andre@0: } else andre@0: rv = PR_FAILURE; andre@0: (void)PR_UnloadLibrary(lib); andre@0: } else andre@0: rv = PR_FAILURE; andre@0: return rv; andre@0: } andre@0: #endif andre@0: andre@0: #if defined(_PR_INET6) && defined(_PR_HAVE_GETHOSTBYNAME2) andre@0: /* andre@0: ** Append the V4 addresses to the end of the list andre@0: */ andre@0: static PRStatus AppendV4AddrsToHostent( andre@0: struct hostent *from, andre@0: char **buf, andre@0: PRIntn *bufsize, andre@0: PRHostEnt *to) andre@0: { andre@0: PRIntn na, na_old; andre@0: char **ap; andre@0: char **new_addr_list; andre@0: andre@0: /* Count the addresses, then grow storage for the pointers */ andre@0: for (na_old = 0, ap = to->h_addr_list; *ap != 0; na_old++, ap++) andre@0: {;} /* nothing to execute */ andre@0: for (na = na_old + 1, ap = from->h_addr_list; *ap != 0; na++, ap++) andre@0: {;} /* nothing to execute */ andre@0: new_addr_list = (char**)Alloc( andre@0: na * sizeof(char*), buf, bufsize, sizeof(char**)); andre@0: if (!new_addr_list) return PR_FAILURE; andre@0: andre@0: /* Copy the V6 addresses, one at a time */ andre@0: for (na = 0, ap = to->h_addr_list; *ap != 0; na++, ap++) { andre@0: new_addr_list[na] = to->h_addr_list[na]; andre@0: } andre@0: to->h_addr_list = new_addr_list; andre@0: andre@0: /* Copy the V4 addresses, one at a time */ andre@0: for (ap = from->h_addr_list; *ap != 0; na++, ap++) { andre@0: to->h_addr_list[na] = Alloc(to->h_length, buf, bufsize, 0); andre@0: if (!to->h_addr_list[na]) return PR_FAILURE; andre@0: MakeIPv4MappedAddr(*ap, to->h_addr_list[na]); andre@0: } andre@0: to->h_addr_list[na] = 0; andre@0: return PR_SUCCESS; andre@0: } andre@0: #endif andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_GetIPNodeByName( andre@0: const char *name, PRUint16 af, PRIntn flags, andre@0: char *buf, PRIntn bufsize, PRHostEnt *hp) andre@0: { andre@0: struct hostent *h = 0; andre@0: PRStatus rv = PR_FAILURE; andre@0: #if defined(_PR_HAVE_GETHOST_R) andre@0: char localbuf[PR_NETDB_BUF_SIZE]; andre@0: char *tmpbuf; andre@0: struct hostent tmphe; andre@0: int h_err; andre@0: #endif andre@0: #if defined(_PR_HAVE_GETIPNODEBYNAME) andre@0: PRUint16 md_af = af; andre@0: int error_num; andre@0: int tmp_flags = 0; andre@0: #endif andre@0: #if defined(_PR_HAVE_GETHOSTBYNAME2) andre@0: PRBool did_af_inet = PR_FALSE; andre@0: #endif andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: if (af != PR_AF_INET && af != PR_AF_INET6) { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: #if defined(_PR_INET6) && defined(_PR_HAVE_GETHOSTBYNAME2) andre@0: PR_Lock(_pr_query_ifs_lock); andre@0: /* andre@0: * Keep querying the presence of IPv4 and IPv6 interfaces until andre@0: * at least one is up. This allows us to detect the local andre@0: * machine going from offline to online. andre@0: */ andre@0: if (!_pr_have_inet_if && !_pr_have_inet6_if) { andre@0: _pr_QueryNetIfs(); andre@0: #ifdef DEBUG_QUERY_IFS andre@0: if (_pr_have_inet_if) andre@0: printf("Have IPv4 source address\n"); andre@0: if (_pr_have_inet6_if) andre@0: printf("Have IPv6 source address\n"); andre@0: #endif andre@0: } andre@0: PR_Unlock(_pr_query_ifs_lock); andre@0: #endif andre@0: andre@0: #if defined(_PR_HAVE_GETIPNODEBYNAME) andre@0: if (flags & PR_AI_V4MAPPED) andre@0: tmp_flags |= AI_V4MAPPED; andre@0: if (flags & PR_AI_ADDRCONFIG) andre@0: tmp_flags |= AI_ADDRCONFIG; andre@0: if (flags & PR_AI_ALL) andre@0: tmp_flags |= AI_ALL; andre@0: if (af == PR_AF_INET6) andre@0: md_af = AF_INET6; andre@0: else andre@0: md_af = af; andre@0: #endif andre@0: andre@0: #if defined(_PR_HAVE_GETHOST_R) andre@0: tmpbuf = localbuf; andre@0: if (bufsize > sizeof(localbuf)) andre@0: { andre@0: tmpbuf = (char *)PR_Malloc(bufsize); andre@0: if (NULL == tmpbuf) andre@0: { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: return rv; andre@0: } andre@0: } andre@0: #endif andre@0: andre@0: /* Do not need to lock the DNS lock if getipnodebyname() is called */ andre@0: #ifdef _PR_INET6 andre@0: #ifdef _PR_HAVE_GETHOSTBYNAME2 andre@0: LOCK_DNS(); andre@0: if (af == PR_AF_INET6) andre@0: { andre@0: if ((flags & PR_AI_ADDRCONFIG) == 0 || _pr_have_inet6_if) andre@0: { andre@0: #ifdef _PR_INET6_PROBE andre@0: if (_pr_ipv6_is_present()) andre@0: #endif andre@0: h = GETHOSTBYNAME2(name, AF_INET6); andre@0: } andre@0: if ((NULL == h) && (flags & PR_AI_V4MAPPED) andre@0: && ((flags & PR_AI_ADDRCONFIG) == 0 || _pr_have_inet_if)) andre@0: { andre@0: did_af_inet = PR_TRUE; andre@0: h = GETHOSTBYNAME2(name, AF_INET); andre@0: } andre@0: } andre@0: else andre@0: { andre@0: if ((flags & PR_AI_ADDRCONFIG) == 0 || _pr_have_inet_if) andre@0: { andre@0: did_af_inet = PR_TRUE; andre@0: h = GETHOSTBYNAME2(name, af); andre@0: } andre@0: } andre@0: #elif defined(_PR_HAVE_GETIPNODEBYNAME) andre@0: h = getipnodebyname(name, md_af, tmp_flags, &error_num); andre@0: #else andre@0: #error "Unknown name-to-address translation function" andre@0: #endif /* _PR_HAVE_GETHOSTBYNAME2 */ andre@0: #elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME) andre@0: if (_pr_ipv6_is_present()) andre@0: { andre@0: #ifdef PR_GETIPNODE_NOT_THREADSAFE andre@0: LOCK_DNS(); andre@0: #endif andre@0: h = (*((_pr_getipnodebyname_t)_pr_getipnodebyname_fp))(name, md_af, tmp_flags, &error_num); andre@0: } andre@0: else andre@0: { andre@0: LOCK_DNS(); andre@0: h = GETHOSTBYNAME(name); andre@0: } andre@0: #else /* _PR_INET6 */ andre@0: LOCK_DNS(); andre@0: h = GETHOSTBYNAME(name); andre@0: #endif /* _PR_INET6 */ andre@0: andre@0: if (NULL == h) andre@0: { andre@0: #if defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYNAME) andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, error_num); andre@0: #elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME) andre@0: if (_pr_ipv6_is_present()) andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, error_num); andre@0: else andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO()); andre@0: #else andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO()); andre@0: #endif andre@0: } andre@0: else andre@0: { andre@0: _PRIPAddrConversion conversion = _PRIPAddrNoConversion; andre@0: andre@0: if (af == PR_AF_INET6) conversion = _PRIPAddrIPv4Mapped; andre@0: rv = CopyHostent(h, &buf, &bufsize, conversion, hp); andre@0: if (PR_SUCCESS != rv) andre@0: PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); andre@0: #if defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYNAME) andre@0: freehostent(h); andre@0: #elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME) andre@0: if (_pr_ipv6_is_present()) andre@0: (*((_pr_freehostent_t)_pr_freehostent_fp))(h); andre@0: #endif andre@0: #if defined(_PR_INET6) && defined(_PR_HAVE_GETHOSTBYNAME2) andre@0: if ((PR_SUCCESS == rv) && (flags & PR_AI_V4MAPPED) andre@0: && ((flags & PR_AI_ALL) andre@0: || ((flags & PR_AI_ADDRCONFIG) && _pr_have_inet_if)) andre@0: && !did_af_inet && (h = GETHOSTBYNAME2(name, AF_INET)) != 0) { andre@0: rv = AppendV4AddrsToHostent(h, &buf, &bufsize, hp); andre@0: if (PR_SUCCESS != rv) andre@0: PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); andre@0: } andre@0: #endif andre@0: } andre@0: andre@0: /* Must match the convoluted logic above for LOCK_DNS() */ andre@0: #ifdef _PR_INET6 andre@0: #ifdef _PR_HAVE_GETHOSTBYNAME2 andre@0: UNLOCK_DNS(); andre@0: #endif /* _PR_HAVE_GETHOSTBYNAME2 */ andre@0: #elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME) andre@0: #ifdef PR_GETIPNODE_NOT_THREADSAFE andre@0: UNLOCK_DNS(); andre@0: #else andre@0: if (!_pr_ipv6_is_present()) andre@0: UNLOCK_DNS(); andre@0: #endif andre@0: #else /* _PR_INET6 */ andre@0: UNLOCK_DNS(); andre@0: #endif /* _PR_INET6 */ andre@0: andre@0: #if defined(_PR_HAVE_GETHOST_R) andre@0: if (tmpbuf != localbuf) andre@0: PR_Free(tmpbuf); andre@0: #endif andre@0: andre@0: return rv; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_GetHostByAddr( andre@0: const PRNetAddr *hostaddr, char *buf, PRIntn bufsize, PRHostEnt *hostentry) andre@0: { andre@0: struct hostent *h; andre@0: PRStatus rv = PR_FAILURE; andre@0: const void *addr; andre@0: PRUint32 tmp_ip; andre@0: int addrlen; andre@0: PRInt32 af; andre@0: #if defined(_PR_HAVE_GETHOST_R) andre@0: char localbuf[PR_NETDB_BUF_SIZE]; andre@0: char *tmpbuf; andre@0: struct hostent tmphe; andre@0: int h_err; andre@0: #endif andre@0: #if defined(_PR_HAVE_GETIPNODEBYADDR) andre@0: int error_num; andre@0: #endif andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: if (hostaddr->raw.family == PR_AF_INET6) andre@0: { andre@0: #if defined(_PR_INET6_PROBE) andre@0: af = _pr_ipv6_is_present() ? AF_INET6 : AF_INET; andre@0: #elif defined(_PR_INET6) andre@0: af = AF_INET6; andre@0: #else andre@0: af = AF_INET; andre@0: #endif andre@0: #if defined(_PR_GHBA_DISALLOW_V4MAPPED) andre@0: if (_PR_IN6_IS_ADDR_V4MAPPED(&hostaddr->ipv6.ip)) andre@0: af = AF_INET; andre@0: #endif andre@0: } andre@0: else andre@0: { andre@0: PR_ASSERT(hostaddr->raw.family == AF_INET); andre@0: af = AF_INET; andre@0: } andre@0: if (hostaddr->raw.family == PR_AF_INET6) { andre@0: #if defined(_PR_INET6) || defined(_PR_INET6_PROBE) andre@0: if (af == AF_INET6) { andre@0: addr = &hostaddr->ipv6.ip; andre@0: addrlen = sizeof(hostaddr->ipv6.ip); andre@0: } andre@0: else andre@0: #endif andre@0: { andre@0: PR_ASSERT(af == AF_INET); andre@0: if (!_PR_IN6_IS_ADDR_V4MAPPED(&hostaddr->ipv6.ip)) { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return rv; andre@0: } andre@0: tmp_ip = _PR_IN6_V4MAPPED_TO_IPADDR((PRIPv6Addr *) andre@0: &hostaddr->ipv6.ip); andre@0: addr = &tmp_ip; andre@0: addrlen = sizeof(tmp_ip); andre@0: } andre@0: } else { andre@0: PR_ASSERT(hostaddr->raw.family == AF_INET); andre@0: PR_ASSERT(af == AF_INET); andre@0: addr = &hostaddr->inet.ip; andre@0: addrlen = sizeof(hostaddr->inet.ip); andre@0: } andre@0: andre@0: #if defined(_PR_HAVE_GETHOST_R) andre@0: tmpbuf = localbuf; andre@0: if (bufsize > sizeof(localbuf)) andre@0: { andre@0: tmpbuf = (char *)PR_Malloc(bufsize); andre@0: if (NULL == tmpbuf) andre@0: { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: return rv; andre@0: } andre@0: } andre@0: #endif andre@0: andre@0: /* Do not need to lock the DNS lock if getipnodebyaddr() is called */ andre@0: #if defined(_PR_HAVE_GETIPNODEBYADDR) && defined(_PR_INET6) andre@0: h = getipnodebyaddr(addr, addrlen, af, &error_num); andre@0: #elif defined(_PR_HAVE_GETIPNODEBYADDR) && defined(_PR_INET6_PROBE) andre@0: if (_pr_ipv6_is_present()) andre@0: { andre@0: #ifdef PR_GETIPNODE_NOT_THREADSAFE andre@0: LOCK_DNS(); andre@0: #endif andre@0: h = (*((_pr_getipnodebyaddr_t)_pr_getipnodebyaddr_fp))(addr, addrlen, andre@0: af, &error_num); andre@0: } andre@0: else andre@0: { andre@0: LOCK_DNS(); andre@0: h = GETHOSTBYADDR(addr, addrlen, af); andre@0: } andre@0: #else /* _PR_HAVE_GETIPNODEBYADDR */ andre@0: LOCK_DNS(); andre@0: h = GETHOSTBYADDR(addr, addrlen, af); andre@0: #endif /* _PR_HAVE_GETIPNODEBYADDR */ andre@0: if (NULL == h) andre@0: { andre@0: #if defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYADDR) andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, error_num); andre@0: #elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYADDR) andre@0: if (_pr_ipv6_is_present()) andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, error_num); andre@0: else andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO()); andre@0: #else andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO()); andre@0: #endif andre@0: } andre@0: else andre@0: { andre@0: _PRIPAddrConversion conversion = _PRIPAddrNoConversion; andre@0: if (hostaddr->raw.family == PR_AF_INET6) { andre@0: if (af == AF_INET) { andre@0: if (_PR_IN6_IS_ADDR_V4MAPPED((PRIPv6Addr*) andre@0: &hostaddr->ipv6.ip)) { andre@0: conversion = _PRIPAddrIPv4Mapped; andre@0: } else if (_PR_IN6_IS_ADDR_V4COMPAT((PRIPv6Addr *) andre@0: &hostaddr->ipv6.ip)) { andre@0: conversion = _PRIPAddrIPv4Compat; andre@0: } andre@0: } andre@0: } andre@0: rv = CopyHostent(h, &buf, &bufsize, conversion, hostentry); andre@0: if (PR_SUCCESS != rv) { andre@0: PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); andre@0: } andre@0: #if defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYADDR) andre@0: freehostent(h); andre@0: #elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYADDR) andre@0: if (_pr_ipv6_is_present()) andre@0: (*((_pr_freehostent_t)_pr_freehostent_fp))(h); andre@0: #endif andre@0: } andre@0: andre@0: /* Must match the convoluted logic above for LOCK_DNS() */ andre@0: #if defined(_PR_HAVE_GETIPNODEBYADDR) && defined(_PR_INET6) andre@0: #elif defined(_PR_HAVE_GETIPNODEBYADDR) && defined(_PR_INET6_PROBE) andre@0: #ifdef PR_GETIPNODE_NOT_THREADSAFE andre@0: UNLOCK_DNS(); andre@0: #else andre@0: if (!_pr_ipv6_is_present()) andre@0: UNLOCK_DNS(); andre@0: #endif andre@0: #else /* _PR_HAVE_GETIPNODEBYADDR */ andre@0: UNLOCK_DNS(); andre@0: #endif /* _PR_HAVE_GETIPNODEBYADDR */ andre@0: andre@0: #if defined(_PR_HAVE_GETHOST_R) andre@0: if (tmpbuf != localbuf) andre@0: PR_Free(tmpbuf); andre@0: #endif andre@0: andre@0: return rv; andre@0: } andre@0: andre@0: /******************************************************************************/ andre@0: /* andre@0: * Some systems define a reentrant version of getprotobyname(). Too bad andre@0: * the signature isn't always the same. But hey, they tried. If there andre@0: * is such a definition, use it. Otherwise, grab a lock and do it here. andre@0: */ andre@0: /******************************************************************************/ andre@0: andre@0: #if !defined(_PR_HAVE_GETPROTO_R) andre@0: /* andre@0: * This may seem like a silly thing to do, but the compiler SHOULD andre@0: * complain if getprotobyname_r() is implemented on some system and andre@0: * we're not using it. For sure these signatures are different than andre@0: * any usable implementation. andre@0: */ andre@0: andre@0: #if defined(ANDROID) andre@0: /* Android's Bionic libc system includes prototypes for these in netdb.h, andre@0: * but doesn't actually include implementations. It uses the 5-arg form, andre@0: * so these functions end up not matching the prototype. So just rename andre@0: * them if not found. andre@0: */ andre@0: #define getprotobyname_r _pr_getprotobyname_r andre@0: #define getprotobynumber_r _pr_getprotobynumber_r andre@0: #endif andre@0: andre@0: static struct protoent *getprotobyname_r(const char* name) andre@0: { andre@0: return getprotobyname(name); andre@0: } /* getprotobyname_r */ andre@0: andre@0: static struct protoent *getprotobynumber_r(PRInt32 number) andre@0: { andre@0: return getprotobynumber(number); andre@0: } /* getprotobynumber_r */ andre@0: andre@0: #endif /* !defined(_PR_HAVE_GETPROTO_R) */ andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_GetProtoByName( andre@0: const char* name, char* buffer, PRInt32 buflen, PRProtoEnt* result) andre@0: { andre@0: PRStatus rv = PR_SUCCESS; andre@0: #if defined(_PR_HAVE_GETPROTO_R) andre@0: struct protoent* res = (struct protoent*)result; andre@0: #endif andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: #if defined(_PR_HAVE_GETPROTO_R_INT) andre@0: { andre@0: /* andre@0: ** The protoent_data has a pointer as the first field. andre@0: ** That implies the buffer better be aligned, and char* andre@0: ** doesn't promise much. andre@0: */ andre@0: PRUptrdiff aligned = (PRUptrdiff)buffer; andre@0: if (0 != (aligned & (sizeof(struct protoent_data*) - 1))) andre@0: { andre@0: aligned += sizeof(struct protoent_data*) - 1; andre@0: aligned &= ~(sizeof(struct protoent_data*) - 1); andre@0: buflen -= (aligned - (PRUptrdiff)buffer); andre@0: buffer = (char*)aligned; andre@0: } andre@0: } andre@0: #endif /* defined(_PR_HAVE_GETPROTO_R_INT) */ andre@0: andre@0: if (PR_NETDB_BUF_SIZE > buflen) andre@0: { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: #if defined(_PR_HAVE_GETPROTO_R_POINTER) andre@0: if (NULL == getprotobyname_r(name, res, buffer, buflen)) andre@0: { andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_ERRNO()); andre@0: return PR_FAILURE; andre@0: } andre@0: #elif defined(_PR_HAVE_GETPROTO_R_INT) andre@0: /* andre@0: ** The buffer needs to be zero'd, and it should be andre@0: ** at least the size of a struct protoent_data. andre@0: */ andre@0: memset(buffer, 0, buflen); andre@0: if (-1 == getprotobyname_r(name, res, (struct protoent_data*)buffer)) andre@0: { andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_ERRNO()); andre@0: return PR_FAILURE; andre@0: } andre@0: #elif defined(_PR_HAVE_5_ARG_GETPROTO_R) andre@0: /* The 5th argument for getprotobyname_r() cannot be NULL */ andre@0: if (-1 == getprotobyname_r(name, res, buffer, buflen, &res)) andre@0: { andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_ERRNO()); andre@0: return PR_FAILURE; andre@0: } andre@0: #else /* do it the hard way */ andre@0: { andre@0: struct protoent *staticBuf; andre@0: PR_Lock(_getproto_lock); andre@0: staticBuf = getprotobyname_r(name); andre@0: if (NULL == staticBuf) andre@0: { andre@0: rv = PR_FAILURE; andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_ERRNO()); andre@0: } andre@0: else andre@0: { andre@0: #if defined(SYMBIAN) andre@0: char* aliases[2]; andre@0: AssignAliases(staticBuf, aliases); andre@0: #endif andre@0: rv = CopyProtoent(staticBuf, buffer, buflen, result); andre@0: if (PR_FAILURE == rv) andre@0: PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); andre@0: } andre@0: PR_Unlock(_getproto_lock); andre@0: } andre@0: #endif /* all that */ andre@0: return rv; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_GetProtoByNumber( andre@0: PRInt32 number, char* buffer, PRInt32 buflen, PRProtoEnt* result) andre@0: { andre@0: PRStatus rv = PR_SUCCESS; andre@0: #if defined(_PR_HAVE_GETPROTO_R) andre@0: struct protoent* res = (struct protoent*)result; andre@0: #endif andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: #if defined(_PR_HAVE_GETPROTO_R_INT) andre@0: { andre@0: /* andre@0: ** The protoent_data has a pointer as the first field. andre@0: ** That implies the buffer better be aligned, and char* andre@0: ** doesn't promise much. andre@0: */ andre@0: PRUptrdiff aligned = (PRUptrdiff)buffer; andre@0: if (0 != (aligned & (sizeof(struct protoent_data*) - 1))) andre@0: { andre@0: aligned += sizeof(struct protoent_data*) - 1; andre@0: aligned &= ~(sizeof(struct protoent_data*) - 1); andre@0: buflen -= (aligned - (PRUptrdiff)buffer); andre@0: buffer = (char*)aligned; andre@0: } andre@0: } andre@0: #endif /* defined(_PR_HAVE_GETPROTO_R_INT) */ andre@0: andre@0: if (PR_NETDB_BUF_SIZE > buflen) andre@0: { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: #if defined(_PR_HAVE_GETPROTO_R_POINTER) andre@0: if (NULL == getprotobynumber_r(number, res, buffer, buflen)) andre@0: { andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_ERRNO()); andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: #elif defined(_PR_HAVE_GETPROTO_R_INT) andre@0: /* andre@0: ** The buffer needs to be zero'd for these OS's. andre@0: */ andre@0: memset(buffer, 0, buflen); andre@0: if (-1 == getprotobynumber_r(number, res, (struct protoent_data*)buffer)) andre@0: { andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_ERRNO()); andre@0: return PR_FAILURE; andre@0: } andre@0: #elif defined(_PR_HAVE_5_ARG_GETPROTO_R) andre@0: /* The 5th argument for getprotobynumber_r() cannot be NULL */ andre@0: if (-1 == getprotobynumber_r(number, res, buffer, buflen, &res)) andre@0: { andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_ERRNO()); andre@0: return PR_FAILURE; andre@0: } andre@0: #else /* do it the hard way */ andre@0: { andre@0: struct protoent *staticBuf; andre@0: PR_Lock(_getproto_lock); andre@0: staticBuf = getprotobynumber_r(number); andre@0: if (NULL == staticBuf) andre@0: { andre@0: rv = PR_FAILURE; andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_ERRNO()); andre@0: } andre@0: else andre@0: { andre@0: #if defined(SYMBIAN) andre@0: char* aliases[2]; andre@0: AssignAliases(staticBuf, aliases); andre@0: #endif andre@0: rv = CopyProtoent(staticBuf, buffer, buflen, result); andre@0: if (PR_FAILURE == rv) andre@0: PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); andre@0: } andre@0: PR_Unlock(_getproto_lock); andre@0: } andre@0: #endif /* all that crap */ andre@0: return rv; andre@0: andre@0: } andre@0: andre@0: PRUintn _PR_NetAddrSize(const PRNetAddr* addr) andre@0: { andre@0: PRUintn addrsize; andre@0: andre@0: /* andre@0: * RFC 2553 added a new field (sin6_scope_id) to andre@0: * struct sockaddr_in6. PRNetAddr's ipv6 member has a andre@0: * scope_id field to match the new field. In order to andre@0: * work with older implementations supporting RFC 2133, andre@0: * we take the size of struct sockaddr_in6 instead of andre@0: * addr->ipv6. andre@0: */ andre@0: if (AF_INET == addr->raw.family) andre@0: addrsize = sizeof(addr->inet); andre@0: else if (PR_AF_INET6 == addr->raw.family) andre@0: #if defined(_PR_INET6) andre@0: addrsize = sizeof(struct sockaddr_in6); andre@0: #else andre@0: addrsize = sizeof(addr->ipv6); andre@0: #endif andre@0: #if defined(XP_UNIX) || defined(XP_OS2) andre@0: else if (AF_UNIX == addr->raw.family) andre@0: addrsize = sizeof(addr->local); andre@0: #endif andre@0: else addrsize = 0; andre@0: andre@0: return addrsize; andre@0: } /* _PR_NetAddrSize */ andre@0: andre@0: PR_IMPLEMENT(PRIntn) PR_EnumerateHostEnt( andre@0: PRIntn enumIndex, const PRHostEnt *hostEnt, PRUint16 port, PRNetAddr *address) andre@0: { andre@0: void *addr = hostEnt->h_addr_list[enumIndex++]; andre@0: memset(address, 0, sizeof(PRNetAddr)); andre@0: if (NULL == addr) enumIndex = 0; andre@0: else andre@0: { andre@0: address->raw.family = hostEnt->h_addrtype; andre@0: if (PR_AF_INET6 == hostEnt->h_addrtype) andre@0: { andre@0: address->ipv6.port = htons(port); andre@0: address->ipv6.flowinfo = 0; andre@0: address->ipv6.scope_id = 0; andre@0: memcpy(&address->ipv6.ip, addr, hostEnt->h_length); andre@0: } andre@0: else andre@0: { andre@0: PR_ASSERT(AF_INET == hostEnt->h_addrtype); andre@0: address->inet.port = htons(port); andre@0: memcpy(&address->inet.ip, addr, hostEnt->h_length); andre@0: } andre@0: } andre@0: return enumIndex; andre@0: } /* PR_EnumerateHostEnt */ andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_InitializeNetAddr( andre@0: PRNetAddrValue val, PRUint16 port, PRNetAddr *addr) andre@0: { andre@0: PRStatus rv = PR_SUCCESS; andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: if (val != PR_IpAddrNull) memset(addr, 0, sizeof(addr->inet)); andre@0: addr->inet.family = AF_INET; andre@0: addr->inet.port = htons(port); andre@0: switch (val) andre@0: { andre@0: case PR_IpAddrNull: andre@0: break; /* don't overwrite the address */ andre@0: case PR_IpAddrAny: andre@0: addr->inet.ip = htonl(INADDR_ANY); andre@0: break; andre@0: case PR_IpAddrLoopback: andre@0: addr->inet.ip = htonl(INADDR_LOOPBACK); andre@0: break; andre@0: default: andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: rv = PR_FAILURE; andre@0: } andre@0: return rv; andre@0: } /* PR_InitializeNetAddr */ andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_SetNetAddr( andre@0: PRNetAddrValue val, PRUint16 af, PRUint16 port, PRNetAddr *addr) andre@0: { andre@0: PRStatus rv = PR_SUCCESS; andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: if (af == PR_AF_INET6) andre@0: { andre@0: if (val != PR_IpAddrNull) memset(addr, 0, sizeof(addr->ipv6)); andre@0: addr->ipv6.family = af; andre@0: addr->ipv6.port = htons(port); andre@0: addr->ipv6.flowinfo = 0; andre@0: addr->ipv6.scope_id = 0; andre@0: switch (val) andre@0: { andre@0: case PR_IpAddrNull: andre@0: break; /* don't overwrite the address */ andre@0: case PR_IpAddrAny: andre@0: addr->ipv6.ip = _pr_in6addr_any; andre@0: break; andre@0: case PR_IpAddrLoopback: andre@0: addr->ipv6.ip = _pr_in6addr_loopback; andre@0: break; andre@0: default: andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: rv = PR_FAILURE; andre@0: } andre@0: } andre@0: else andre@0: { andre@0: if (val != PR_IpAddrNull) memset(addr, 0, sizeof(addr->inet)); andre@0: addr->inet.family = af; andre@0: addr->inet.port = htons(port); andre@0: switch (val) andre@0: { andre@0: case PR_IpAddrNull: andre@0: break; /* don't overwrite the address */ andre@0: case PR_IpAddrAny: andre@0: addr->inet.ip = htonl(INADDR_ANY); andre@0: break; andre@0: case PR_IpAddrLoopback: andre@0: addr->inet.ip = htonl(INADDR_LOOPBACK); andre@0: break; andre@0: default: andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: rv = PR_FAILURE; andre@0: } andre@0: } andre@0: return rv; andre@0: } /* PR_SetNetAddr */ andre@0: andre@0: PR_IMPLEMENT(PRBool) andre@0: PR_IsNetAddrType(const PRNetAddr *addr, PRNetAddrValue val) andre@0: { andre@0: if (addr->raw.family == PR_AF_INET6) { andre@0: if (val == PR_IpAddrAny) { andre@0: if (_PR_IN6_IS_ADDR_UNSPECIFIED((PRIPv6Addr *)&addr->ipv6.ip)) { andre@0: return PR_TRUE; andre@0: } else if (_PR_IN6_IS_ADDR_V4MAPPED((PRIPv6Addr *)&addr->ipv6.ip) andre@0: && _PR_IN6_V4MAPPED_TO_IPADDR((PRIPv6Addr *)&addr->ipv6.ip) andre@0: == htonl(INADDR_ANY)) { andre@0: return PR_TRUE; andre@0: } andre@0: } else if (val == PR_IpAddrLoopback) { andre@0: if (_PR_IN6_IS_ADDR_LOOPBACK((PRIPv6Addr *)&addr->ipv6.ip)) { andre@0: return PR_TRUE; andre@0: } else if (_PR_IN6_IS_ADDR_V4MAPPED((PRIPv6Addr *)&addr->ipv6.ip) andre@0: && _PR_IN6_V4MAPPED_TO_IPADDR((PRIPv6Addr *)&addr->ipv6.ip) andre@0: == htonl(INADDR_LOOPBACK)) { andre@0: return PR_TRUE; andre@0: } andre@0: } else if (val == PR_IpAddrV4Mapped andre@0: && _PR_IN6_IS_ADDR_V4MAPPED((PRIPv6Addr *)&addr->ipv6.ip)) { andre@0: return PR_TRUE; andre@0: } andre@0: } else { andre@0: if (addr->raw.family == AF_INET) { andre@0: if (val == PR_IpAddrAny && addr->inet.ip == htonl(INADDR_ANY)) { andre@0: return PR_TRUE; andre@0: } else if (val == PR_IpAddrLoopback andre@0: && addr->inet.ip == htonl(INADDR_LOOPBACK)) { andre@0: return PR_TRUE; andre@0: } andre@0: } andre@0: } andre@0: return PR_FALSE; andre@0: } andre@0: andre@0: extern int pr_inet_aton(const char *cp, PRUint32 *addr); andre@0: andre@0: #define XX 127 andre@0: static const unsigned char index_hex[256] = { andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,XX,XX, XX,XX,XX,XX, andre@0: XX,10,11,12, 13,14,15,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,10,11,12, 13,14,15,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, andre@0: }; andre@0: andre@0: /* andre@0: * StringToV6Addr() returns 1 if the conversion succeeds, andre@0: * or 0 if the input is not a valid IPv6 address string. andre@0: * (Same as inet_pton(AF_INET6, string, addr).) andre@0: */ andre@0: static int StringToV6Addr(const char *string, PRIPv6Addr *addr) andre@0: { andre@0: const unsigned char *s = (const unsigned char *)string; andre@0: int section = 0; /* index of the current section (a 16-bit andre@0: * piece of the address */ andre@0: int double_colon = -1; /* index of the section after the first andre@0: * 16-bit group of zeros represented by andre@0: * the double colon */ andre@0: unsigned int val; andre@0: int len; andre@0: andre@0: /* Handle initial (double) colon */ andre@0: if (*s == ':') { andre@0: if (s[1] != ':') return 0; andre@0: s += 2; andre@0: addr->pr_s6_addr16[0] = 0; andre@0: section = double_colon = 1; andre@0: } andre@0: andre@0: while (*s) { andre@0: if (section == 8) return 0; /* too long */ andre@0: if (*s == ':') { andre@0: if (double_colon != -1) return 0; /* two double colons */ andre@0: addr->pr_s6_addr16[section++] = 0; andre@0: double_colon = section; andre@0: s++; andre@0: continue; andre@0: } andre@0: for (len = val = 0; len < 4 && index_hex[*s] != XX; len++) { andre@0: val = (val << 4) + index_hex[*s++]; andre@0: } andre@0: if (*s == '.') { andre@0: if (len == 0) return 0; /* nothing between : and . */ andre@0: break; andre@0: } andre@0: if (*s == ':') { andre@0: s++; andre@0: if (!*s) return 0; /* cannot end with single colon */ andre@0: } else if (*s) { andre@0: return 0; /* bad character */ andre@0: } andre@0: addr->pr_s6_addr16[section++] = htons((unsigned short)val); andre@0: } andre@0: andre@0: if (*s == '.') { andre@0: /* Have a trailing v4 format address */ andre@0: if (section > 6) return 0; /* not enough room */ andre@0: andre@0: /* andre@0: * The number before the '.' is decimal, but we parsed it andre@0: * as hex. That means it is in BCD. Check it for validity andre@0: * and convert it to binary. andre@0: */ andre@0: if (val > 0x0255 || (val & 0xf0) > 0x90 || (val & 0xf) > 9) return 0; andre@0: val = (val >> 8) * 100 + ((val >> 4) & 0xf) * 10 + (val & 0xf); andre@0: addr->pr_s6_addr[2 * section] = val; andre@0: andre@0: s++; andre@0: val = index_hex[*s++]; andre@0: if (val > 9) return 0; andre@0: while (*s >= '0' && *s <= '9') { andre@0: val = val * 10 + *s++ - '0'; andre@0: if (val > 255) return 0; andre@0: } andre@0: if (*s != '.') return 0; /* must have exactly 4 decimal numbers */ andre@0: addr->pr_s6_addr[2 * section + 1] = val; andre@0: section++; andre@0: andre@0: s++; andre@0: val = index_hex[*s++]; andre@0: if (val > 9) return 0; andre@0: while (*s >= '0' && *s <= '9') { andre@0: val = val * 10 + *s++ - '0'; andre@0: if (val > 255) return 0; andre@0: } andre@0: if (*s != '.') return 0; /* must have exactly 4 decimal numbers */ andre@0: addr->pr_s6_addr[2 * section] = val; andre@0: andre@0: s++; andre@0: val = index_hex[*s++]; andre@0: if (val > 9) return 0; andre@0: while (*s >= '0' && *s <= '9') { andre@0: val = val * 10 + *s++ - '0'; andre@0: if (val > 255) return 0; andre@0: } andre@0: if (*s) return 0; /* must have exactly 4 decimal numbers */ andre@0: addr->pr_s6_addr[2 * section + 1] = val; andre@0: section++; andre@0: } andre@0: andre@0: if (double_colon != -1) { andre@0: /* Stretch the double colon */ andre@0: int tosection; andre@0: int ncopy = section - double_colon; andre@0: for (tosection = 7; ncopy--; tosection--) { andre@0: addr->pr_s6_addr16[tosection] = andre@0: addr->pr_s6_addr16[double_colon + ncopy]; andre@0: } andre@0: while (tosection >= double_colon) { andre@0: addr->pr_s6_addr16[tosection--] = 0; andre@0: } andre@0: } else if (section != 8) { andre@0: return 0; /* too short */ andre@0: } andre@0: return 1; andre@0: } andre@0: #undef XX andre@0: andre@0: #ifndef _PR_HAVE_INET_NTOP andre@0: static const char *basis_hex = "0123456789abcdef"; andre@0: andre@0: /* andre@0: * V6AddrToString() returns a pointer to the buffer containing andre@0: * the text string if the conversion succeeds, and NULL otherwise. andre@0: * (Same as inet_ntop(AF_INET6, addr, buf, size), except that errno andre@0: * is not set on failure.) andre@0: */ andre@0: static const char *V6AddrToString( andre@0: const PRIPv6Addr *addr, char *buf, PRUint32 size) andre@0: { andre@0: #define STUFF(c) do { \ andre@0: if (!size--) return NULL; \ andre@0: *buf++ = (c); \ andre@0: } while (0) andre@0: andre@0: int double_colon = -1; /* index of the first 16-bit andre@0: * group of zeros represented andre@0: * by the double colon */ andre@0: int double_colon_length = 1; /* use double colon only if andre@0: * there are two or more 16-bit andre@0: * groups of zeros */ andre@0: int zero_length; andre@0: int section; andre@0: unsigned int val; andre@0: const char *bufcopy = buf; andre@0: andre@0: /* Scan to find the placement of the double colon */ andre@0: for (section = 0; section < 8; section++) { andre@0: if (addr->pr_s6_addr16[section] == 0) { andre@0: zero_length = 1; andre@0: section++; andre@0: while (section < 8 && addr->pr_s6_addr16[section] == 0) { andre@0: zero_length++; andre@0: section++; andre@0: } andre@0: /* Select the longest sequence of zeros */ andre@0: if (zero_length > double_colon_length) { andre@0: double_colon = section - zero_length; andre@0: double_colon_length = zero_length; andre@0: } andre@0: } andre@0: } andre@0: andre@0: /* Now start converting to a string */ andre@0: section = 0; andre@0: andre@0: if (double_colon == 0) { andre@0: if (double_colon_length == 6 || andre@0: (double_colon_length == 5 && addr->pr_s6_addr16[5] == 0xffff)) { andre@0: /* ipv4 format address */ andre@0: STUFF(':'); andre@0: STUFF(':'); andre@0: if (double_colon_length == 5) { andre@0: STUFF('f'); andre@0: STUFF('f'); andre@0: STUFF('f'); andre@0: STUFF('f'); andre@0: STUFF(':'); andre@0: } andre@0: if (addr->pr_s6_addr[12] > 99) STUFF(addr->pr_s6_addr[12]/100 + '0'); andre@0: if (addr->pr_s6_addr[12] > 9) STUFF((addr->pr_s6_addr[12]%100)/10 + '0'); andre@0: STUFF(addr->pr_s6_addr[12]%10 + '0'); andre@0: STUFF('.'); andre@0: if (addr->pr_s6_addr[13] > 99) STUFF(addr->pr_s6_addr[13]/100 + '0'); andre@0: if (addr->pr_s6_addr[13] > 9) STUFF((addr->pr_s6_addr[13]%100)/10 + '0'); andre@0: STUFF(addr->pr_s6_addr[13]%10 + '0'); andre@0: STUFF('.'); andre@0: if (addr->pr_s6_addr[14] > 99) STUFF(addr->pr_s6_addr[14]/100 + '0'); andre@0: if (addr->pr_s6_addr[14] > 9) STUFF((addr->pr_s6_addr[14]%100)/10 + '0'); andre@0: STUFF(addr->pr_s6_addr[14]%10 + '0'); andre@0: STUFF('.'); andre@0: if (addr->pr_s6_addr[15] > 99) STUFF(addr->pr_s6_addr[15]/100 + '0'); andre@0: if (addr->pr_s6_addr[15] > 9) STUFF((addr->pr_s6_addr[15]%100)/10 + '0'); andre@0: STUFF(addr->pr_s6_addr[15]%10 + '0'); andre@0: STUFF('\0'); andre@0: return bufcopy; andre@0: } andre@0: } andre@0: andre@0: while (section < 8) { andre@0: if (section == double_colon) { andre@0: STUFF(':'); andre@0: STUFF(':'); andre@0: section += double_colon_length; andre@0: continue; andre@0: } andre@0: val = ntohs(addr->pr_s6_addr16[section]); andre@0: if (val > 0xfff) { andre@0: STUFF(basis_hex[val >> 12]); andre@0: } andre@0: if (val > 0xff) { andre@0: STUFF(basis_hex[(val >> 8) & 0xf]); andre@0: } andre@0: if (val > 0xf) { andre@0: STUFF(basis_hex[(val >> 4) & 0xf]); andre@0: } andre@0: STUFF(basis_hex[val & 0xf]); andre@0: section++; andre@0: if (section < 8 && section != double_colon) STUFF(':'); andre@0: } andre@0: STUFF('\0'); andre@0: return bufcopy; andre@0: #undef STUFF andre@0: } andre@0: #endif /* !_PR_HAVE_INET_NTOP */ andre@0: andre@0: /* andre@0: * Convert an IPv4 addr to an (IPv4-mapped) IPv6 addr andre@0: */ andre@0: PR_IMPLEMENT(void) PR_ConvertIPv4AddrToIPv6(PRUint32 v4addr, PRIPv6Addr *v6addr) andre@0: { andre@0: PRUint8 *dstp; andre@0: dstp = v6addr->pr_s6_addr; andre@0: memset(dstp, 0, 10); andre@0: memset(dstp + 10, 0xff, 2); andre@0: memcpy(dstp + 12,(char *) &v4addr, 4); andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRUint16) PR_ntohs(PRUint16 n) { return ntohs(n); } andre@0: PR_IMPLEMENT(PRUint32) PR_ntohl(PRUint32 n) { return ntohl(n); } andre@0: PR_IMPLEMENT(PRUint16) PR_htons(PRUint16 n) { return htons(n); } andre@0: PR_IMPLEMENT(PRUint32) PR_htonl(PRUint32 n) { return htonl(n); } andre@0: PR_IMPLEMENT(PRUint64) PR_ntohll(PRUint64 n) andre@0: { andre@0: #ifdef IS_BIG_ENDIAN andre@0: return n; andre@0: #else andre@0: PRUint64 tmp; andre@0: PRUint32 hi, lo; andre@0: LL_L2UI(lo, n); andre@0: LL_SHR(tmp, n, 32); andre@0: LL_L2UI(hi, tmp); andre@0: hi = PR_ntohl(hi); andre@0: lo = PR_ntohl(lo); andre@0: LL_UI2L(n, lo); andre@0: LL_SHL(n, n, 32); andre@0: LL_UI2L(tmp, hi); andre@0: LL_ADD(n, n, tmp); andre@0: return n; andre@0: #endif andre@0: } /* ntohll */ andre@0: andre@0: PR_IMPLEMENT(PRUint64) PR_htonll(PRUint64 n) andre@0: { andre@0: #ifdef IS_BIG_ENDIAN andre@0: return n; andre@0: #else andre@0: PRUint64 tmp; andre@0: PRUint32 hi, lo; andre@0: LL_L2UI(lo, n); andre@0: LL_SHR(tmp, n, 32); andre@0: LL_L2UI(hi, tmp); andre@0: hi = htonl(hi); andre@0: lo = htonl(lo); andre@0: LL_UI2L(n, lo); andre@0: LL_SHL(n, n, 32); andre@0: LL_UI2L(tmp, hi); andre@0: LL_ADD(n, n, tmp); andre@0: return n; andre@0: #endif andre@0: } /* htonll */ andre@0: andre@0: andre@0: /* andre@0: * Implementation of PR_GetAddrInfoByName and friends andre@0: * andre@0: * Compile-time options: andre@0: * andre@0: * _PR_HAVE_GETADDRINFO Define this macro if the target system provides andre@0: * getaddrinfo. With this defined, NSPR will require andre@0: * getaddrinfo at run time. If this if not defined, andre@0: * then NSPR will attempt to dynamically resolve andre@0: * getaddrinfo, falling back to PR_GetHostByName if andre@0: * getaddrinfo does not exist on the target system. andre@0: * andre@0: * Since getaddrinfo is a relatively new system call on many systems, andre@0: * we are forced to dynamically resolve it at run time in most cases. andre@0: * The exception includes any system (such as Mac OS X) that is known to andre@0: * provide getaddrinfo in all versions that NSPR cares to support. andre@0: */ andre@0: andre@0: #if defined(_PR_HAVE_GETADDRINFO) andre@0: andre@0: #if defined(_PR_INET6) andre@0: andre@0: typedef struct addrinfo PRADDRINFO; andre@0: #define GETADDRINFO getaddrinfo andre@0: #define FREEADDRINFO freeaddrinfo andre@0: #define GETNAMEINFO getnameinfo andre@0: andre@0: #elif defined(_PR_INET6_PROBE) andre@0: andre@0: typedef struct addrinfo PRADDRINFO; andre@0: andre@0: /* getaddrinfo/freeaddrinfo/getnameinfo prototypes */ andre@0: #if defined(WIN32) andre@0: #define FUNC_MODIFIER __stdcall andre@0: #else andre@0: #define FUNC_MODIFIER andre@0: #endif andre@0: typedef int (FUNC_MODIFIER * FN_GETADDRINFO) andre@0: (const char *nodename, andre@0: const char *servname, andre@0: const PRADDRINFO *hints, andre@0: PRADDRINFO **res); andre@0: typedef int (FUNC_MODIFIER * FN_FREEADDRINFO) andre@0: (PRADDRINFO *ai); andre@0: typedef int (FUNC_MODIFIER * FN_GETNAMEINFO) andre@0: (const struct sockaddr *addr, int addrlen, andre@0: char *host, int hostlen, andre@0: char *serv, int servlen, int flags); andre@0: andre@0: /* global state */ andre@0: static FN_GETADDRINFO _pr_getaddrinfo = NULL; andre@0: static FN_FREEADDRINFO _pr_freeaddrinfo = NULL; andre@0: static FN_GETNAMEINFO _pr_getnameinfo = NULL; andre@0: andre@0: #define GETADDRINFO_SYMBOL "getaddrinfo" andre@0: #define FREEADDRINFO_SYMBOL "freeaddrinfo" andre@0: #define GETNAMEINFO_SYMBOL "getnameinfo" andre@0: andre@0: PRStatus andre@0: _pr_find_getaddrinfo(void) andre@0: { andre@0: PRLibrary *lib; andre@0: #ifdef WIN32 andre@0: /* andre@0: * On windows, we need to search ws2_32.dll or wship6.dll andre@0: * (Microsoft IPv6 Technology Preview for Windows 2000) for andre@0: * getaddrinfo and freeaddrinfo. These libraries might not andre@0: * be loaded yet. andre@0: */ andre@0: const char *libname[] = { "ws2_32.dll", "wship6.dll" }; andre@0: int i; andre@0: andre@0: for (i = 0; i < sizeof(libname)/sizeof(libname[0]); i++) { andre@0: lib = PR_LoadLibrary(libname[i]); andre@0: if (!lib) { andre@0: continue; andre@0: } andre@0: _pr_getaddrinfo = (FN_GETADDRINFO) andre@0: PR_FindFunctionSymbol(lib, GETADDRINFO_SYMBOL); andre@0: if (!_pr_getaddrinfo) { andre@0: PR_UnloadLibrary(lib); andre@0: continue; andre@0: } andre@0: _pr_freeaddrinfo = (FN_FREEADDRINFO) andre@0: PR_FindFunctionSymbol(lib, FREEADDRINFO_SYMBOL); andre@0: _pr_getnameinfo = (FN_GETNAMEINFO) andre@0: PR_FindFunctionSymbol(lib, GETNAMEINFO_SYMBOL); andre@0: if (!_pr_freeaddrinfo || !_pr_getnameinfo) { andre@0: PR_UnloadLibrary(lib); andre@0: continue; andre@0: } andre@0: /* Keep the library loaded. */ andre@0: return PR_SUCCESS; andre@0: } andre@0: return PR_FAILURE; andre@0: #else andre@0: /* andre@0: * Resolve getaddrinfo by searching all loaded libraries. Then andre@0: * search library containing getaddrinfo for freeaddrinfo. andre@0: */ andre@0: _pr_getaddrinfo = (FN_GETADDRINFO) andre@0: PR_FindFunctionSymbolAndLibrary(GETADDRINFO_SYMBOL, &lib); andre@0: if (!_pr_getaddrinfo) { andre@0: return PR_FAILURE; andre@0: } andre@0: _pr_freeaddrinfo = (FN_FREEADDRINFO) andre@0: PR_FindFunctionSymbol(lib, FREEADDRINFO_SYMBOL); andre@0: _pr_getnameinfo = (FN_GETNAMEINFO) andre@0: PR_FindFunctionSymbol(lib, GETNAMEINFO_SYMBOL); andre@0: PR_UnloadLibrary(lib); andre@0: if (!_pr_freeaddrinfo || !_pr_getnameinfo) { andre@0: return PR_FAILURE; andre@0: } andre@0: return PR_SUCCESS; andre@0: #endif andre@0: } andre@0: andre@0: #define GETADDRINFO (*_pr_getaddrinfo) andre@0: #define FREEADDRINFO (*_pr_freeaddrinfo) andre@0: #define GETNAMEINFO (*_pr_getnameinfo) andre@0: andre@0: #endif /* _PR_INET6 */ andre@0: andre@0: #endif /* _PR_HAVE_GETADDRINFO */ andre@0: andre@0: #if !defined(_PR_HAVE_GETADDRINFO) || defined(_PR_INET6_PROBE) andre@0: /* andre@0: * If getaddrinfo does not exist, then we will fall back on andre@0: * PR_GetHostByName, which requires that we allocate a buffer for the andre@0: * PRHostEnt data structure and its members. andre@0: */ andre@0: typedef struct PRAddrInfoFB { andre@0: char buf[PR_NETDB_BUF_SIZE]; andre@0: PRHostEnt hostent; andre@0: PRBool has_cname; andre@0: } PRAddrInfoFB; andre@0: andre@0: static PRAddrInfo * andre@0: pr_GetAddrInfoByNameFB(const char *hostname, andre@0: PRUint16 af, andre@0: PRIntn flags) andre@0: { andre@0: PRStatus rv; andre@0: PRAddrInfoFB *ai; andre@0: /* fallback on PR_GetHostByName */ andre@0: ai = PR_NEW(PRAddrInfoFB); andre@0: if (!ai) { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: return NULL; andre@0: } andre@0: rv = PR_GetHostByName(hostname, ai->buf, sizeof ai->buf, &ai->hostent); andre@0: if (rv == PR_FAILURE) { andre@0: PR_Free(ai); andre@0: return NULL; andre@0: } andre@0: ai->has_cname = !(flags & PR_AI_NOCANONNAME); andre@0: andre@0: return (PRAddrInfo *) ai; andre@0: } andre@0: #endif /* !_PR_HAVE_GETADDRINFO || _PR_INET6_PROBE */ andre@0: andre@0: PR_IMPLEMENT(PRAddrInfo *) PR_GetAddrInfoByName(const char *hostname, andre@0: PRUint16 af, andre@0: PRIntn flags) andre@0: { andre@0: /* restrict input to supported values */ andre@0: if ((af != PR_AF_INET && af != PR_AF_UNSPEC) || andre@0: (flags & ~ PR_AI_NOCANONNAME) != PR_AI_ADDRCONFIG) { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return NULL; andre@0: } andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: #if !defined(_PR_HAVE_GETADDRINFO) andre@0: return pr_GetAddrInfoByNameFB(hostname, af, flags); andre@0: #else andre@0: #if defined(_PR_INET6_PROBE) andre@0: if (!_pr_ipv6_is_present()) { andre@0: return pr_GetAddrInfoByNameFB(hostname, af, flags); andre@0: } andre@0: #endif andre@0: { andre@0: PRADDRINFO *res, hints; andre@0: int rv; andre@0: andre@0: /* andre@0: * we assume a RFC 2553 compliant getaddrinfo. this may at some andre@0: * point need to be customized as platforms begin to adopt the andre@0: * RFC 3493. andre@0: */ andre@0: andre@0: memset(&hints, 0, sizeof(hints)); andre@0: if (!(flags & PR_AI_NOCANONNAME)) andre@0: hints.ai_flags |= AI_CANONNAME; andre@0: #ifdef AI_ADDRCONFIG andre@0: /* andre@0: * Propagate AI_ADDRCONFIG to the GETADDRINFO call if PR_AI_ADDRCONFIG andre@0: * is set. andre@0: * andre@0: * Need a workaround for loopback host addresses: andre@0: * The problem is that in glibc and Windows, AI_ADDRCONFIG applies the andre@0: * existence of an outgoing network interface to IP addresses of the andre@0: * loopback interface, due to a strict interpretation of the andre@0: * specification. For example, if a computer does not have any andre@0: * outgoing IPv6 network interface, but its loopback network interface andre@0: * supports IPv6, a getaddrinfo call on "localhost" with AI_ADDRCONFIG andre@0: * won't return the IPv6 loopback address "::1", because getaddrinfo andre@0: * thinks the computer cannot connect to any IPv6 destination, andre@0: * ignoring the remote vs. local/loopback distinction. andre@0: */ andre@0: if ((flags & PR_AI_ADDRCONFIG) && andre@0: strcmp(hostname, "localhost") != 0 && andre@0: strcmp(hostname, "localhost.localdomain") != 0 && andre@0: strcmp(hostname, "localhost6") != 0 && andre@0: strcmp(hostname, "localhost6.localdomain6") != 0) { andre@0: hints.ai_flags |= AI_ADDRCONFIG; andre@0: } andre@0: #endif andre@0: hints.ai_family = (af == PR_AF_INET) ? AF_INET : AF_UNSPEC; andre@0: andre@0: /* andre@0: * it is important to select a socket type in the hints, otherwise we andre@0: * will get back repetitive entries: one for each socket type. since andre@0: * we do not expose ai_socktype through our API, it is okay to do this andre@0: * here. the application may still choose to create a socket of some andre@0: * other type. andre@0: */ andre@0: hints.ai_socktype = SOCK_STREAM; andre@0: andre@0: rv = GETADDRINFO(hostname, NULL, &hints, &res); andre@0: #ifdef AI_ADDRCONFIG andre@0: if (rv == EAI_BADFLAGS && (hints.ai_flags & AI_ADDRCONFIG)) { andre@0: hints.ai_flags &= ~AI_ADDRCONFIG; andre@0: rv = GETADDRINFO(hostname, NULL, &hints, &res); andre@0: } andre@0: #endif andre@0: if (rv == 0) andre@0: return (PRAddrInfo *) res; andre@0: andre@0: PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, rv); andre@0: } andre@0: return NULL; andre@0: #endif andre@0: } andre@0: andre@0: PR_IMPLEMENT(void) PR_FreeAddrInfo(PRAddrInfo *ai) andre@0: { andre@0: #if defined(_PR_HAVE_GETADDRINFO) andre@0: #if defined(_PR_INET6_PROBE) andre@0: if (!_pr_ipv6_is_present()) andre@0: PR_Free((PRAddrInfoFB *) ai); andre@0: else andre@0: #endif andre@0: FREEADDRINFO((PRADDRINFO *) ai); andre@0: #else andre@0: PR_Free((PRAddrInfoFB *) ai); andre@0: #endif andre@0: } andre@0: andre@0: PR_IMPLEMENT(void *) PR_EnumerateAddrInfo(void *iterPtr, andre@0: const PRAddrInfo *base, andre@0: PRUint16 port, andre@0: PRNetAddr *result) andre@0: { andre@0: #if defined(_PR_HAVE_GETADDRINFO) andre@0: PRADDRINFO *ai; andre@0: #if defined(_PR_INET6_PROBE) andre@0: if (!_pr_ipv6_is_present()) { andre@0: /* using PRAddrInfoFB */ andre@0: PRIntn iter = (PRIntn)(PRPtrdiff) iterPtr; andre@0: iter = PR_EnumerateHostEnt(iter, &((PRAddrInfoFB *) base)->hostent, port, result); andre@0: if (iter < 0) andre@0: iter = 0; andre@0: return (void *)(PRPtrdiff) iter; andre@0: } andre@0: #endif andre@0: andre@0: if (iterPtr) andre@0: ai = ((PRADDRINFO *) iterPtr)->ai_next; andre@0: else andre@0: ai = (PRADDRINFO *) base; andre@0: andre@0: while (ai && ai->ai_addrlen > sizeof(PRNetAddr)) andre@0: ai = ai->ai_next; andre@0: andre@0: if (ai) { andre@0: /* copy sockaddr to PRNetAddr */ andre@0: memcpy(result, ai->ai_addr, ai->ai_addrlen); andre@0: result->raw.family = ai->ai_addr->sa_family; andre@0: #ifdef _PR_INET6 andre@0: if (AF_INET6 == result->raw.family) andre@0: result->raw.family = PR_AF_INET6; andre@0: #endif andre@0: if (ai->ai_addrlen < sizeof(PRNetAddr)) andre@0: memset(((char*)result)+ai->ai_addrlen, 0, sizeof(PRNetAddr) - ai->ai_addrlen); andre@0: andre@0: if (result->raw.family == PR_AF_INET) andre@0: result->inet.port = htons(port); andre@0: else andre@0: result->ipv6.port = htons(port); andre@0: } andre@0: andre@0: return ai; andre@0: #else andre@0: /* using PRAddrInfoFB */ andre@0: PRIntn iter = (PRIntn) iterPtr; andre@0: iter = PR_EnumerateHostEnt(iter, &((PRAddrInfoFB *) base)->hostent, port, result); andre@0: if (iter < 0) andre@0: iter = 0; andre@0: return (void *) iter; andre@0: #endif andre@0: } andre@0: andre@0: PR_IMPLEMENT(const char *) PR_GetCanonNameFromAddrInfo(const PRAddrInfo *ai) andre@0: { andre@0: #if defined(_PR_HAVE_GETADDRINFO) andre@0: #if defined(_PR_INET6_PROBE) andre@0: if (!_pr_ipv6_is_present()) { andre@0: const PRAddrInfoFB *fb = (const PRAddrInfoFB *) ai; andre@0: return fb->has_cname ? fb->hostent.h_name : NULL; andre@0: } andre@0: #endif andre@0: return ((const PRADDRINFO *) ai)->ai_canonname; andre@0: #else andre@0: const PRAddrInfoFB *fb = (const PRAddrInfoFB *) ai; andre@0: return fb->has_cname ? fb->hostent.h_name : NULL; andre@0: #endif andre@0: } andre@0: andre@0: #if defined(_PR_HAVE_GETADDRINFO) andre@0: static PRStatus pr_StringToNetAddrGAI(const char *string, PRNetAddr *addr) andre@0: { andre@0: PRADDRINFO *res, hints; andre@0: int rv; /* 0 for success, or the error code EAI_xxx */ andre@0: PRNetAddr laddr; andre@0: PRStatus status = PR_SUCCESS; andre@0: andre@0: memset(&hints, 0, sizeof(hints)); andre@0: hints.ai_flags = AI_NUMERICHOST; andre@0: hints.ai_family = AF_UNSPEC; andre@0: hints.ai_socktype = SOCK_STREAM; andre@0: andre@0: rv = GETADDRINFO(string, NULL, &hints, &res); andre@0: if (rv != 0) andre@0: { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, rv); andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: /* pick up the first addr */ andre@0: memcpy(&laddr, res->ai_addr, res->ai_addrlen); andre@0: if (AF_INET6 == res->ai_addr->sa_family) andre@0: { andre@0: addr->ipv6.family = PR_AF_INET6; andre@0: addr->ipv6.ip = laddr.ipv6.ip; andre@0: addr->ipv6.scope_id = laddr.ipv6.scope_id; andre@0: } andre@0: else if (AF_INET == res->ai_addr->sa_family) andre@0: { andre@0: addr->inet.family = PR_AF_INET; andre@0: addr->inet.ip = laddr.inet.ip; andre@0: } andre@0: else andre@0: { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: status = PR_FAILURE; andre@0: } andre@0: andre@0: FREEADDRINFO(res); andre@0: return status; andre@0: } andre@0: #endif /* _PR_HAVE_GETADDRINFO */ andre@0: andre@0: static PRStatus pr_StringToNetAddrFB(const char *string, PRNetAddr *addr) andre@0: { andre@0: PRIntn rv; andre@0: andre@0: rv = pr_inet_aton(string, &addr->inet.ip); andre@0: if (1 == rv) andre@0: { andre@0: addr->raw.family = AF_INET; andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: PR_ASSERT(0 == rv); andre@0: /* clean up after the failed call */ andre@0: memset(&addr->inet.ip, 0, sizeof(addr->inet.ip)); andre@0: andre@0: rv = StringToV6Addr(string, &addr->ipv6.ip); andre@0: if (1 == rv) andre@0: { andre@0: addr->raw.family = PR_AF_INET6; andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: PR_ASSERT(0 == rv); andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_StringToNetAddr(const char *string, PRNetAddr *addr) andre@0: { andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: if (!addr || !string || !*string) andre@0: { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: #if !defined(_PR_HAVE_GETADDRINFO) andre@0: return pr_StringToNetAddrFB(string, addr); andre@0: #else andre@0: /* andre@0: * getaddrinfo with AI_NUMERICHOST is much slower than pr_inet_aton on some andre@0: * platforms, such as Mac OS X (bug 404399), Linux glibc 2.10 (bug 344809), andre@0: * and most likely others. So we only use it to convert literal IP addresses andre@0: * that contain IPv6 scope IDs, which pr_inet_aton cannot convert. andre@0: */ andre@0: if (!strchr(string, '%')) andre@0: return pr_StringToNetAddrFB(string, addr); andre@0: andre@0: #if defined(_PR_INET6_PROBE) andre@0: if (!_pr_ipv6_is_present()) andre@0: return pr_StringToNetAddrFB(string, addr); andre@0: #endif andre@0: andre@0: return pr_StringToNetAddrGAI(string, addr); andre@0: #endif andre@0: } andre@0: andre@0: #if defined(_PR_HAVE_GETADDRINFO) andre@0: static PRStatus pr_NetAddrToStringGNI( andre@0: const PRNetAddr *addr, char *string, PRUint32 size) andre@0: { andre@0: int addrlen; andre@0: const PRNetAddr *addrp = addr; andre@0: #if defined(_PR_HAVE_SOCKADDR_LEN) || defined(_PR_INET6) andre@0: PRUint16 md_af = addr->raw.family; andre@0: PRNetAddr addrcopy; andre@0: #endif andre@0: int rv; /* 0 for success, or the error code EAI_xxx */ andre@0: andre@0: #ifdef _PR_INET6 andre@0: if (addr->raw.family == PR_AF_INET6) andre@0: { andre@0: md_af = AF_INET6; andre@0: #ifndef _PR_HAVE_SOCKADDR_LEN andre@0: addrcopy = *addr; andre@0: addrcopy.raw.family = md_af; andre@0: addrp = &addrcopy; andre@0: #endif andre@0: } andre@0: #endif andre@0: andre@0: addrlen = PR_NETADDR_SIZE(addr); andre@0: #ifdef _PR_HAVE_SOCKADDR_LEN andre@0: addrcopy = *addr; andre@0: ((struct sockaddr*)&addrcopy)->sa_len = addrlen; andre@0: ((struct sockaddr*)&addrcopy)->sa_family = md_af; andre@0: addrp = &addrcopy; andre@0: #endif andre@0: rv = GETNAMEINFO((const struct sockaddr *)addrp, addrlen, andre@0: string, size, NULL, 0, NI_NUMERICHOST); andre@0: if (rv != 0) andre@0: { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, rv); andre@0: return PR_FAILURE; andre@0: } andre@0: return PR_SUCCESS; andre@0: } andre@0: #endif /* _PR_HAVE_GETADDRINFO */ andre@0: andre@0: #if !defined(_PR_HAVE_GETADDRINFO) || defined(_PR_INET6_PROBE) andre@0: static PRStatus pr_NetAddrToStringFB( andre@0: const PRNetAddr *addr, char *string, PRUint32 size) andre@0: { andre@0: if (PR_AF_INET6 == addr->raw.family) andre@0: { andre@0: #if defined(_PR_HAVE_INET_NTOP) andre@0: if (NULL == inet_ntop(AF_INET6, &addr->ipv6.ip, string, size)) andre@0: #else andre@0: if (NULL == V6AddrToString(&addr->ipv6.ip, string, size)) andre@0: #endif andre@0: { andre@0: /* the size of the result buffer is inadequate */ andre@0: PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: } andre@0: else andre@0: { andre@0: if (size < 16) goto failed; andre@0: if (AF_INET != addr->raw.family) goto failed; andre@0: else andre@0: { andre@0: unsigned char *byte = (unsigned char*)&addr->inet.ip; andre@0: PR_snprintf(string, size, "%u.%u.%u.%u", andre@0: byte[0], byte[1], byte[2], byte[3]); andre@0: } andre@0: } andre@0: andre@0: return PR_SUCCESS; andre@0: andre@0: failed: andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return PR_FAILURE; andre@0: andre@0: } /* pr_NetAddrToStringFB */ andre@0: #endif /* !_PR_HAVE_GETADDRINFO || _PR_INET6_PROBE */ andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_NetAddrToString( andre@0: const PRNetAddr *addr, char *string, PRUint32 size) andre@0: { andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: #if !defined(_PR_HAVE_GETADDRINFO) andre@0: return pr_NetAddrToStringFB(addr, string, size); andre@0: #else andre@0: #if defined(_PR_INET6_PROBE) andre@0: if (!_pr_ipv6_is_present()) andre@0: return pr_NetAddrToStringFB(addr, string, size); andre@0: #endif andre@0: return pr_NetAddrToStringGNI(addr, string, size); andre@0: #endif andre@0: } /* PR_NetAddrToString */