diff options
Diffstat (limited to 'nsprpub/pr/src/misc/prnetdb.c')
-rw-r--r-- | nsprpub/pr/src/misc/prnetdb.c | 1301 |
1 files changed, 719 insertions, 582 deletions
diff --git a/nsprpub/pr/src/misc/prnetdb.c b/nsprpub/pr/src/misc/prnetdb.c index c482e8e4a..6ad0242d6 100644 --- a/nsprpub/pr/src/misc/prnetdb.c +++ b/nsprpub/pr/src/misc/prnetdb.c @@ -29,14 +29,13 @@ extern int h_errno; * The meaning of the macros related to gethostbyname, gethostbyaddr, * and gethostbyname2 is defined below. * - _PR_HAVE_THREADSAFE_GETHOST: the gethostbyXXX functions return - * the result in thread specific storage. For example, AIX, HP-UX, - * and OSF1. + * the result in thread specific storage. For example, AIX, HP-UX. * - _PR_HAVE_GETHOST_R: have the gethostbyXXX_r functions. See next * two macros. * - _PR_HAVE_GETHOST_R_INT: the gethostbyXXX_r functions return an * int. For example, Linux glibc. * - _PR_HAVE_GETHOST_R_POINTER: the gethostbyXXX_r functions return - * a struct hostent* pointer. For example, Solaris and IRIX. + * a struct hostent* pointer. For example, Solaris. */ #if defined(_PR_NO_PREEMPT) || defined(_PR_HAVE_GETHOST_R) \ || defined(_PR_HAVE_THREADSAFE_GETHOST) @@ -58,23 +57,16 @@ PRLock *_pr_dnsLock = NULL; * Some return a pointer to struct protoent, others return * an int, and glibc's flavor takes five arguments. */ -#if defined(XP_BEOS) && defined(BONE_VERSION) -#include <arpa/inet.h> /* pick up define for inet_addr */ -#include <sys/socket.h> -#define _PR_HAVE_GETPROTO_R -#define _PR_HAVE_GETPROTO_R_POINTER -#endif #if defined(SOLARIS) || (defined(BSDI) && defined(_REENTRANT)) \ - || (defined(LINUX) && defined(_REENTRANT) \ + || (defined(LINUX) && defined(_REENTRANT) \ && defined(__GLIBC__) && __GLIBC__ < 2) #define _PR_HAVE_GETPROTO_R #define _PR_HAVE_GETPROTO_R_POINTER #endif -#if defined(OSF1) \ - || defined(AIX4_3_PLUS) || (defined(AIX) && defined(_THREAD_SAFE)) \ - || (defined(HPUX10_10) && defined(_REENTRANT)) \ +#if defined(AIX4_3_PLUS) || (defined(AIX) && defined(_THREAD_SAFE)) \ + || (defined(HPUX10_10) && defined(_REENTRANT)) \ || (defined(HPUX10_20) && defined(_REENTRANT)) \ || defined(OPENBSD) #define _PR_HAVE_GETPROTO_R @@ -87,7 +79,7 @@ PRLock *_pr_dnsLock = NULL; #endif /* BeOS has glibc but not the glibc-style getprotobyxxx_r functions. */ -#if (defined(__GLIBC__) && __GLIBC__ >= 2 && !defined(XP_BEOS)) +#if (defined(__GLIBC__) && __GLIBC__ >= 2) #define _PR_HAVE_GETPROTO_R #define _PR_HAVE_5_ARG_GETPROTO_R #endif @@ -100,48 +92,56 @@ PRLock* _getproto_lock = NULL; extern PRBool _pr_ipv6_is_present(void); #endif -#define _PR_IN6_IS_ADDR_UNSPECIFIED(a) \ - (((a)->pr_s6_addr32[0] == 0) && \ - ((a)->pr_s6_addr32[1] == 0) && \ - ((a)->pr_s6_addr32[2] == 0) && \ - ((a)->pr_s6_addr32[3] == 0)) - -#define _PR_IN6_IS_ADDR_LOOPBACK(a) \ - (((a)->pr_s6_addr32[0] == 0) && \ - ((a)->pr_s6_addr32[1] == 0) && \ - ((a)->pr_s6_addr32[2] == 0) && \ - ((a)->pr_s6_addr[12] == 0) && \ - ((a)->pr_s6_addr[13] == 0) && \ - ((a)->pr_s6_addr[14] == 0) && \ +#define _PR_IN6_IS_ADDR_UNSPECIFIED(a) \ + (((a)->pr_s6_addr32[0] == 0) && \ + ((a)->pr_s6_addr32[1] == 0) && \ + ((a)->pr_s6_addr32[2] == 0) && \ + ((a)->pr_s6_addr32[3] == 0)) + +#define _PR_IN6_IS_ADDR_LOOPBACK(a) \ + (((a)->pr_s6_addr32[0] == 0) && \ + ((a)->pr_s6_addr32[1] == 0) && \ + ((a)->pr_s6_addr32[2] == 0) && \ + ((a)->pr_s6_addr[12] == 0) && \ + ((a)->pr_s6_addr[13] == 0) && \ + ((a)->pr_s6_addr[14] == 0) && \ ((a)->pr_s6_addr[15] == 0x1U)) - -const PRIPv6Addr _pr_in6addr_any = {{{ 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0 }}}; - -const PRIPv6Addr _pr_in6addr_loopback = {{{ 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0x1U }}}; + +const PRIPv6Addr _pr_in6addr_any = {{{ + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0 + } + } +}; + +const PRIPv6Addr _pr_in6addr_loopback = {{{ + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0x1U + } + } +}; /* * The values at bytes 10 and 11 are compared using pointers to * 8-bit fields, and not 32-bit fields, to make the comparison work on * both big-endian and little-endian systems */ -#define _PR_IN6_IS_ADDR_V4MAPPED(a) \ - (((a)->pr_s6_addr32[0] == 0) && \ - ((a)->pr_s6_addr32[1] == 0) && \ - ((a)->pr_s6_addr[8] == 0) && \ - ((a)->pr_s6_addr[9] == 0) && \ - ((a)->pr_s6_addr[10] == 0xff) && \ - ((a)->pr_s6_addr[11] == 0xff)) +#define _PR_IN6_IS_ADDR_V4MAPPED(a) \ + (((a)->pr_s6_addr32[0] == 0) && \ + ((a)->pr_s6_addr32[1] == 0) && \ + ((a)->pr_s6_addr[8] == 0) && \ + ((a)->pr_s6_addr[9] == 0) && \ + ((a)->pr_s6_addr[10] == 0xff) && \ + ((a)->pr_s6_addr[11] == 0xff)) -#define _PR_IN6_IS_ADDR_V4COMPAT(a) \ - (((a)->pr_s6_addr32[0] == 0) && \ - ((a)->pr_s6_addr32[1] == 0) && \ - ((a)->pr_s6_addr32[2] == 0)) +#define _PR_IN6_IS_ADDR_V4COMPAT(a) \ + (((a)->pr_s6_addr32[0] == 0) && \ + ((a)->pr_s6_addr32[1] == 0) && \ + ((a)->pr_s6_addr32[2] == 0)) #define _PR_IN6_V4MAPPED_TO_IPADDR(a) ((a)->pr_s6_addr32[3]) @@ -247,8 +247,9 @@ _pr_QueryNetIfs(void) return; } } else { - if (ifc.ifc_len == lastlen) - break; /* success, len has not changed */ + if (ifc.ifc_len == lastlen) { + break; /* success, len has not changed */ + } lastlen = ifc.ifc_len; } len += 10 * sizeof(struct ifreq); /* increment */ @@ -271,13 +272,13 @@ _pr_QueryNetIfs(void) struct sockaddr_in *sin = (struct sockaddr_in *) sa; if (sin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) { _pr_have_inet_if = PR_TRUE; - } + } } else if (sa->sa_family == AF_INET6) { struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa; if (!IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) - && !IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { + && !IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { _pr_have_inet6_if = PR_TRUE; - } + } } #ifdef _PR_HAVE_SOCKADDR_LEN @@ -285,16 +286,16 @@ _pr_QueryNetIfs(void) #else switch (sa->sa_family) { #ifdef AF_LINK - case AF_LINK: - sa_len = sizeof(struct sockaddr_dl); - break; -#endif - case AF_INET6: - sa_len = sizeof(struct sockaddr_in6); - break; - default: - sa_len = sizeof(struct sockaddr); - break; + case AF_LINK: + sa_len = sizeof(struct sockaddr_dl); + break; +#endif + case AF_INET6: + sa_len = sizeof(struct sockaddr_in6); + break; + default: + sa_len = sizeof(struct sockaddr); + break; } #endif ifr = (struct ifreq *)(((char *)sa) + sa_len); @@ -360,15 +361,15 @@ _pr_QueryNetIfs(void) struct sockaddr_in *sin = (struct sockaddr_in *) sa; if (sin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) { _pr_have_inet_if = 1; - } + } } else if (sa->sa_family == AF_INET6) { struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa; if (!IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) - && !IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { + && !IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { _pr_have_inet6_if = 1; - } + } } - } + } freeifaddrs(ifp); } @@ -393,21 +394,21 @@ void _PR_InitNet(void) { #if defined(XP_UNIX) #ifdef HAVE_NETCONFIG - /* - * This one-liner prevents the endless re-open's and re-read's of - * /etc/netconfig on EACH and EVERY call to accept(), connect(), etc. - */ - (void)setnetconfig(); + /* + * This one-liner prevents the endless re-open's and re-read's of + * /etc/netconfig on EACH and EVERY call to accept(), connect(), etc. + */ + (void)setnetconfig(); #endif #endif #if !defined(_PR_NO_DNS_LOCK) - _pr_dnsLock = PR_NewLock(); + _pr_dnsLock = PR_NewLock(); #endif #if !defined(_PR_HAVE_GETPROTO_R) - _getproto_lock = PR_NewLock(); + _getproto_lock = PR_NewLock(); #endif #if defined(_PR_INET6) && defined(_PR_HAVE_GETHOSTBYNAME2) - _pr_query_ifs_lock = PR_NewLock(); + _pr_query_ifs_lock = PR_NewLock(); #endif } @@ -439,23 +440,23 @@ void _PR_CleanupNet(void) */ static char *Alloc(PRIntn amount, char **bufp, PRIntn *buflenp, PRIntn align) { - char *buf = *bufp; - PRIntn buflen = *buflenp; - - if (align && ((long)buf & (align - 1))) { - PRIntn skip = align - ((ptrdiff_t)buf & (align - 1)); - if (buflen < skip) { - return 0; - } - buf += skip; - buflen -= skip; - } - if (buflen < amount) { - return 0; - } - *bufp = buf + amount; - *buflenp = buflen - amount; - return buf; + char *buf = *bufp; + PRIntn buflen = *buflenp; + + if (align && ((long)buf & (align - 1))) { + PRIntn skip = align - ((ptrdiff_t)buf & (align - 1)); + if (buflen < skip) { + return 0; + } + buf += skip; + buflen -= skip; + } + if (buflen < amount) { + return 0; + } + *bufp = buf + amount; + *buflenp = buflen - amount; + return buf; } typedef enum _PRIPAddrConversion { @@ -494,98 +495,92 @@ static PRStatus CopyHostent( _PRIPAddrConversion conversion, PRHostEnt *to) { - PRIntn len, na; - char **ap; - - if (conversion != _PRIPAddrNoConversion - && from->h_addrtype == AF_INET) { - PR_ASSERT(from->h_length == 4); - to->h_addrtype = PR_AF_INET6; - to->h_length = 16; - } else { -#if defined(_PR_INET6) || defined(_PR_INET6_PROBE) - if (AF_INET6 == from->h_addrtype) - to->h_addrtype = PR_AF_INET6; - else -#endif - to->h_addrtype = from->h_addrtype; - to->h_length = from->h_length; - } - - /* Copy the official name */ - if (!from->h_name) return PR_FAILURE; - len = strlen(from->h_name) + 1; - to->h_name = Alloc(len, buf, bufsize, 0); - if (!to->h_name) return PR_FAILURE; - memcpy(to->h_name, from->h_name, len); - - /* Count the aliases, then allocate storage for the pointers */ - if (!from->h_aliases) { - na = 1; - } else { - for (na = 1, ap = from->h_aliases; *ap != 0; na++, ap++){;} /* nothing to execute */ - } - to->h_aliases = (char**)Alloc( - na * sizeof(char*), buf, bufsize, sizeof(char**)); - if (!to->h_aliases) return PR_FAILURE; - - /* Copy the aliases, one at a time */ - if (!from->h_aliases) { - to->h_aliases[0] = 0; - } else { - for (na = 0, ap = from->h_aliases; *ap != 0; na++, ap++) { - len = strlen(*ap) + 1; - to->h_aliases[na] = Alloc(len, buf, bufsize, 0); - if (!to->h_aliases[na]) return PR_FAILURE; - memcpy(to->h_aliases[na], *ap, len); - } - to->h_aliases[na] = 0; - } - - /* Count the addresses, then allocate storage for the pointers */ - for (na = 1, ap = from->h_addr_list; *ap != 0; na++, ap++){;} /* nothing to execute */ - to->h_addr_list = (char**)Alloc( - na * sizeof(char*), buf, bufsize, sizeof(char**)); - if (!to->h_addr_list) return PR_FAILURE; - - /* Copy the addresses, one at a time */ - for (na = 0, ap = from->h_addr_list; *ap != 0; na++, ap++) { - to->h_addr_list[na] = Alloc(to->h_length, buf, bufsize, 0); - if (!to->h_addr_list[na]) return PR_FAILURE; - if (conversion != _PRIPAddrNoConversion - && from->h_addrtype == AF_INET) { - if (conversion == _PRIPAddrIPv4Mapped) { - MakeIPv4MappedAddr(*ap, to->h_addr_list[na]); - } else { - PR_ASSERT(conversion == _PRIPAddrIPv4Compat); - MakeIPv4CompatAddr(*ap, to->h_addr_list[na]); - } - } else { - memcpy(to->h_addr_list[na], *ap, to->h_length); - } - } - to->h_addr_list[na] = 0; - return PR_SUCCESS; -} + PRIntn len, na; + char **ap; -#ifdef SYMBIAN -/* Set p_aliases by hand because Symbian's getprotobyname() returns NULL. */ -static void AssignAliases(struct protoent *Protoent, char** aliases) -{ - if (NULL == Protoent->p_aliases) { - if (0 == strcmp(Protoent->p_name, "ip")) - aliases[0] = "IP"; - else if (0 == strcmp(Protoent->p_name, "tcp")) - aliases[0] = "TCP"; - else if (0 == strcmp(Protoent->p_name, "udp")) - aliases[0] = "UDP"; + if (conversion != _PRIPAddrNoConversion + && from->h_addrtype == AF_INET) { + PR_ASSERT(from->h_length == 4); + to->h_addrtype = PR_AF_INET6; + to->h_length = 16; + } else { +#if defined(_PR_INET6) || defined(_PR_INET6_PROBE) + if (AF_INET6 == from->h_addrtype) { + to->h_addrtype = PR_AF_INET6; + } else - aliases[0] = "UNKNOWN"; - aliases[1] = NULL; - Protoent->p_aliases = aliases; +#endif + to->h_addrtype = from->h_addrtype; + to->h_length = from->h_length; } + + /* Copy the official name */ + if (!from->h_name) { + return PR_FAILURE; + } + len = strlen(from->h_name) + 1; + to->h_name = Alloc(len, buf, bufsize, 0); + if (!to->h_name) { + return PR_FAILURE; + } + memcpy(to->h_name, from->h_name, len); + + /* Count the aliases, then allocate storage for the pointers */ + if (!from->h_aliases) { + na = 1; + } else { + for (na = 1, ap = from->h_aliases; *ap != 0; na++, ap++) {;} /* nothing to execute */ + } + to->h_aliases = (char**)Alloc( + na * sizeof(char*), buf, bufsize, sizeof(char**)); + if (!to->h_aliases) { + return PR_FAILURE; + } + + /* Copy the aliases, one at a time */ + if (!from->h_aliases) { + to->h_aliases[0] = 0; + } else { + for (na = 0, ap = from->h_aliases; *ap != 0; na++, ap++) { + len = strlen(*ap) + 1; + to->h_aliases[na] = Alloc(len, buf, bufsize, 0); + if (!to->h_aliases[na]) { + return PR_FAILURE; + } + memcpy(to->h_aliases[na], *ap, len); + } + to->h_aliases[na] = 0; + } + + /* Count the addresses, then allocate storage for the pointers */ + for (na = 1, ap = from->h_addr_list; *ap != 0; na++, ap++) {;} /* nothing to execute */ + to->h_addr_list = (char**)Alloc( + na * sizeof(char*), buf, bufsize, sizeof(char**)); + if (!to->h_addr_list) { + return PR_FAILURE; + } + + /* Copy the addresses, one at a time */ + for (na = 0, ap = from->h_addr_list; *ap != 0; na++, ap++) { + to->h_addr_list[na] = Alloc(to->h_length, buf, bufsize, 0); + if (!to->h_addr_list[na]) { + return PR_FAILURE; + } + if (conversion != _PRIPAddrNoConversion + && from->h_addrtype == AF_INET) { + if (conversion == _PRIPAddrIPv4Mapped) { + MakeIPv4MappedAddr(*ap, to->h_addr_list[na]); + } else { + PR_ASSERT(conversion == _PRIPAddrIPv4Compat); + MakeIPv4CompatAddr(*ap, to->h_addr_list[na]); + } + } else { + memcpy(to->h_addr_list[na], *ap, to->h_length); + } + } + to->h_addr_list[na] = 0; + return PR_SUCCESS; } -#endif #if !defined(_PR_HAVE_GETPROTO_R) /* @@ -595,35 +590,43 @@ static void AssignAliases(struct protoent *Protoent, char** aliases) static PRStatus CopyProtoent( struct protoent *from, char *buf, PRIntn bufsize, PRProtoEnt *to) { - PRIntn len, na; - char **ap; - - /* Do the easy stuff */ - to->p_num = from->p_proto; - - /* Copy the official name */ - if (!from->p_name) return PR_FAILURE; - len = strlen(from->p_name) + 1; - to->p_name = Alloc(len, &buf, &bufsize, 0); - if (!to->p_name) return PR_FAILURE; - memcpy(to->p_name, from->p_name, len); - - /* Count the aliases, then allocate storage for the pointers */ - for (na = 1, ap = from->p_aliases; *ap != 0; na++, ap++){;} /* nothing to execute */ - to->p_aliases = (char**)Alloc( - na * sizeof(char*), &buf, &bufsize, sizeof(char**)); - if (!to->p_aliases) return PR_FAILURE; - - /* Copy the aliases, one at a time */ - for (na = 0, ap = from->p_aliases; *ap != 0; na++, ap++) { - len = strlen(*ap) + 1; - to->p_aliases[na] = Alloc(len, &buf, &bufsize, 0); - if (!to->p_aliases[na]) return PR_FAILURE; - memcpy(to->p_aliases[na], *ap, len); - } - to->p_aliases[na] = 0; - - return PR_SUCCESS; + PRIntn len, na; + char **ap; + + /* Do the easy stuff */ + to->p_num = from->p_proto; + + /* Copy the official name */ + if (!from->p_name) { + return PR_FAILURE; + } + len = strlen(from->p_name) + 1; + to->p_name = Alloc(len, &buf, &bufsize, 0); + if (!to->p_name) { + return PR_FAILURE; + } + memcpy(to->p_name, from->p_name, len); + + /* Count the aliases, then allocate storage for the pointers */ + for (na = 1, ap = from->p_aliases; *ap != 0; na++, ap++) {;} /* nothing to execute */ + to->p_aliases = (char**)Alloc( + na * sizeof(char*), &buf, &bufsize, sizeof(char**)); + if (!to->p_aliases) { + return PR_FAILURE; + } + + /* Copy the aliases, one at a time */ + for (na = 0, ap = from->p_aliases; *ap != 0; na++, ap++) { + len = strlen(*ap) + 1; + to->p_aliases[na] = Alloc(len, &buf, &bufsize, 0); + if (!to->p_aliases[na]) { + return PR_FAILURE; + } + memcpy(to->p_aliases[na], *ap, len); + } + to->p_aliases[na] = 0; + + return PR_SUCCESS; } #endif /* !defined(_PR_HAVE_GETPROTO_R) */ @@ -631,7 +634,7 @@ static PRStatus CopyProtoent( * ################################################################# * NOTE: tmphe, tmpbuf, bufsize, h, and h_err are local variables * or arguments of PR_GetHostByName, PR_GetIPNodeByName, and - * PR_GetHostByAddr. DO NOT CHANGE THE NAMES OF THESE LOCAL + * PR_GetHostByAddr. DO NOT CHANGE THE NAMES OF THESE LOCAL * VARIABLES OR ARGUMENTS. * ################################################################# */ @@ -665,8 +668,8 @@ static PRStatus CopyProtoent( PR_IMPLEMENT(PRStatus) PR_GetHostByName( const char *name, char *buf, PRIntn bufsize, PRHostEnt *hp) { - struct hostent *h; - PRStatus rv = PR_FAILURE; + struct hostent *h; + PRStatus rv = PR_FAILURE; #if defined(_PR_HAVE_GETHOST_R) char localbuf[PR_NETDB_BUF_SIZE]; char *tmpbuf; @@ -674,7 +677,9 @@ PR_IMPLEMENT(PRStatus) PR_GetHostByName( int h_err; #endif - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } #if defined(_PR_HAVE_GETHOST_R) tmpbuf = localbuf; @@ -689,35 +694,37 @@ PR_IMPLEMENT(PRStatus) PR_GetHostByName( } #endif - LOCK_DNS(); + LOCK_DNS(); - h = GETHOSTBYNAME(name); - - if (NULL == h) - { - PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO()); - } - else - { - _PRIPAddrConversion conversion = _PRIPAddrNoConversion; - rv = CopyHostent(h, &buf, &bufsize, conversion, hp); - if (PR_SUCCESS != rv) - PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); - } - UNLOCK_DNS(); + h = GETHOSTBYNAME(name); + + if (NULL == h) + { + PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO()); + } + else + { + _PRIPAddrConversion conversion = _PRIPAddrNoConversion; + rv = CopyHostent(h, &buf, &bufsize, conversion, hp); + if (PR_SUCCESS != rv) { + PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); + } + } + UNLOCK_DNS(); #if defined(_PR_HAVE_GETHOST_R) - if (tmpbuf != localbuf) + if (tmpbuf != localbuf) { PR_Free(tmpbuf); + } #endif - return rv; + return rv; } #if !defined(_PR_INET6) && \ defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME) typedef struct hostent * (*_pr_getipnodebyname_t)(const char *, int, - int, int *); + int, int *); typedef struct hostent * (*_pr_getipnodebyaddr_t)(const void *, size_t, - int, int *); + int, int *); typedef void (*_pr_freehostent_t)(struct hostent *); static void * _pr_getipnodebyname_fp; static void * _pr_getipnodebyaddr_fp; @@ -730,7 +737,7 @@ static void * _pr_freehostent_fp; PRStatus _pr_find_getipnodebyname(void) { - PRLibrary *lib; + PRLibrary *lib; PRStatus rv; #define GETIPNODEBYNAME "getipnodebyname" #define GETIPNODEBYADDR "getipnodebyaddr" @@ -741,15 +748,19 @@ _pr_find_getipnodebyname(void) _pr_freehostent_fp = PR_FindSymbol(lib, FREEHOSTENT); if (NULL != _pr_freehostent_fp) { _pr_getipnodebyaddr_fp = PR_FindSymbol(lib, GETIPNODEBYADDR); - if (NULL != _pr_getipnodebyaddr_fp) + if (NULL != _pr_getipnodebyaddr_fp) { rv = PR_SUCCESS; - else + } + else { rv = PR_FAILURE; - } else + } + } else { rv = PR_FAILURE; + } (void)PR_UnloadLibrary(lib); - } else + } else { rv = PR_FAILURE; + } return rv; } #endif @@ -767,15 +778,17 @@ static PRStatus AppendV4AddrsToHostent( PRIntn na, na_old; char **ap; char **new_addr_list; - + /* Count the addresses, then grow storage for the pointers */ for (na_old = 0, ap = to->h_addr_list; *ap != 0; na_old++, ap++) - {;} /* nothing to execute */ + {;} /* nothing to execute */ for (na = na_old + 1, ap = from->h_addr_list; *ap != 0; na++, ap++) - {;} /* nothing to execute */ + {;} /* nothing to execute */ new_addr_list = (char**)Alloc( - na * sizeof(char*), buf, bufsize, sizeof(char**)); - if (!new_addr_list) return PR_FAILURE; + na * sizeof(char*), buf, bufsize, sizeof(char**)); + if (!new_addr_list) { + return PR_FAILURE; + } /* Copy the V6 addresses, one at a time */ for (na = 0, ap = to->h_addr_list; *ap != 0; na++, ap++) { @@ -786,7 +799,9 @@ static PRStatus AppendV4AddrsToHostent( /* Copy the V4 addresses, one at a time */ for (ap = from->h_addr_list; *ap != 0; na++, ap++) { to->h_addr_list[na] = Alloc(to->h_length, buf, bufsize, 0); - if (!to->h_addr_list[na]) return PR_FAILURE; + if (!to->h_addr_list[na]) { + return PR_FAILURE; + } MakeIPv4MappedAddr(*ap, to->h_addr_list[na]); } to->h_addr_list[na] = 0; @@ -798,8 +813,8 @@ PR_IMPLEMENT(PRStatus) PR_GetIPNodeByName( const char *name, PRUint16 af, PRIntn flags, char *buf, PRIntn bufsize, PRHostEnt *hp) { - struct hostent *h = 0; - PRStatus rv = PR_FAILURE; + struct hostent *h = 0; + PRStatus rv = PR_FAILURE; #if defined(_PR_HAVE_GETHOST_R) char localbuf[PR_NETDB_BUF_SIZE]; char *tmpbuf; @@ -807,15 +822,17 @@ PR_IMPLEMENT(PRStatus) PR_GetIPNodeByName( int h_err; #endif #if defined(_PR_HAVE_GETIPNODEBYNAME) - PRUint16 md_af = af; - int error_num; - int tmp_flags = 0; + PRUint16 md_af = af; + int error_num; + int tmp_flags = 0; #endif #if defined(_PR_HAVE_GETHOSTBYNAME2) PRBool did_af_inet = PR_FALSE; #endif - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } if (af != PR_AF_INET && af != PR_AF_INET6) { PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); @@ -830,28 +847,35 @@ PR_IMPLEMENT(PRStatus) PR_GetIPNodeByName( * machine going from offline to online. */ if (!_pr_have_inet_if && !_pr_have_inet6_if) { - _pr_QueryNetIfs(); + _pr_QueryNetIfs(); #ifdef DEBUG_QUERY_IFS - if (_pr_have_inet_if) - printf("Have IPv4 source address\n"); - if (_pr_have_inet6_if) - printf("Have IPv6 source address\n"); + if (_pr_have_inet_if) { + printf("Have IPv4 source address\n"); + } + if (_pr_have_inet6_if) { + printf("Have IPv6 source address\n"); + } #endif } PR_Unlock(_pr_query_ifs_lock); #endif #if defined(_PR_HAVE_GETIPNODEBYNAME) - if (flags & PR_AI_V4MAPPED) - tmp_flags |= AI_V4MAPPED; - if (flags & PR_AI_ADDRCONFIG) - tmp_flags |= AI_ADDRCONFIG; - if (flags & PR_AI_ALL) - tmp_flags |= AI_ALL; - if (af == PR_AF_INET6) - md_af = AF_INET6; - else - md_af = af; + if (flags & PR_AI_V4MAPPED) { + tmp_flags |= AI_V4MAPPED; + } + if (flags & PR_AI_ADDRCONFIG) { + tmp_flags |= AI_ADDRCONFIG; + } + if (flags & PR_AI_ALL) { + tmp_flags |= AI_ALL; + } + if (af == PR_AF_INET6) { + md_af = AF_INET6; + } + else { + md_af = af; + } #endif #if defined(_PR_HAVE_GETHOST_R) @@ -876,12 +900,12 @@ PR_IMPLEMENT(PRStatus) PR_GetIPNodeByName( if ((flags & PR_AI_ADDRCONFIG) == 0 || _pr_have_inet6_if) { #ifdef _PR_INET6_PROBE - if (_pr_ipv6_is_present()) + if (_pr_ipv6_is_present()) #endif - h = GETHOSTBYNAME2(name, AF_INET6); + h = GETHOSTBYNAME2(name, AF_INET6); } if ((NULL == h) && (flags & PR_AI_V4MAPPED) - && ((flags & PR_AI_ADDRCONFIG) == 0 || _pr_have_inet_if)) + && ((flags & PR_AI_ADDRCONFIG) == 0 || _pr_have_inet_if)) { did_af_inet = PR_TRUE; h = GETHOSTBYNAME2(name, AF_INET); @@ -899,97 +923,106 @@ PR_IMPLEMENT(PRStatus) PR_GetIPNodeByName( h = getipnodebyname(name, md_af, tmp_flags, &error_num); #else #error "Unknown name-to-address translation function" -#endif /* _PR_HAVE_GETHOSTBYNAME2 */ +#endif /* _PR_HAVE_GETHOSTBYNAME2 */ #elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME) if (_pr_ipv6_is_present()) { #ifdef PR_GETIPNODE_NOT_THREADSAFE LOCK_DNS(); #endif - h = (*((_pr_getipnodebyname_t)_pr_getipnodebyname_fp))(name, md_af, tmp_flags, &error_num); + h = (*((_pr_getipnodebyname_t)_pr_getipnodebyname_fp))(name, md_af, tmp_flags, &error_num); } else { LOCK_DNS(); - h = GETHOSTBYNAME(name); + h = GETHOSTBYNAME(name); } #else /* _PR_INET6 */ LOCK_DNS(); h = GETHOSTBYNAME(name); #endif /* _PR_INET6 */ - - if (NULL == h) - { + + if (NULL == h) + { #if defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYNAME) - PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, error_num); + PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, error_num); #elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME) - if (_pr_ipv6_is_present()) - PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, error_num); - else - PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO()); + if (_pr_ipv6_is_present()) { + PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, error_num); + } + else { + PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO()); + } #else - PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO()); + PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO()); #endif - } - else - { - _PRIPAddrConversion conversion = _PRIPAddrNoConversion; + } + else + { + _PRIPAddrConversion conversion = _PRIPAddrNoConversion; - if (af == PR_AF_INET6) conversion = _PRIPAddrIPv4Mapped; - rv = CopyHostent(h, &buf, &bufsize, conversion, hp); - if (PR_SUCCESS != rv) - PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); + if (af == PR_AF_INET6) { + conversion = _PRIPAddrIPv4Mapped; + } + rv = CopyHostent(h, &buf, &bufsize, conversion, hp); + if (PR_SUCCESS != rv) { + PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); + } #if defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYNAME) - freehostent(h); + freehostent(h); #elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME) - if (_pr_ipv6_is_present()) - (*((_pr_freehostent_t)_pr_freehostent_fp))(h); + if (_pr_ipv6_is_present()) { + (*((_pr_freehostent_t)_pr_freehostent_fp))(h); + } #endif #if defined(_PR_INET6) && defined(_PR_HAVE_GETHOSTBYNAME2) - if ((PR_SUCCESS == rv) && (flags & PR_AI_V4MAPPED) - && ((flags & PR_AI_ALL) - || ((flags & PR_AI_ADDRCONFIG) && _pr_have_inet_if)) - && !did_af_inet && (h = GETHOSTBYNAME2(name, AF_INET)) != 0) { - rv = AppendV4AddrsToHostent(h, &buf, &bufsize, hp); - if (PR_SUCCESS != rv) - PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); - } + if ((PR_SUCCESS == rv) && (flags & PR_AI_V4MAPPED) + && ((flags & PR_AI_ALL) + || ((flags & PR_AI_ADDRCONFIG) && _pr_have_inet_if)) + && !did_af_inet && (h = GETHOSTBYNAME2(name, AF_INET)) != 0) { + rv = AppendV4AddrsToHostent(h, &buf, &bufsize, hp); + if (PR_SUCCESS != rv) { + PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); + } + } #endif - } + } /* Must match the convoluted logic above for LOCK_DNS() */ #ifdef _PR_INET6 #ifdef _PR_HAVE_GETHOSTBYNAME2 UNLOCK_DNS(); -#endif /* _PR_HAVE_GETHOSTBYNAME2 */ +#endif /* _PR_HAVE_GETHOSTBYNAME2 */ #elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYNAME) #ifdef PR_GETIPNODE_NOT_THREADSAFE UNLOCK_DNS(); #else - if (!_pr_ipv6_is_present()) + if (!_pr_ipv6_is_present()) { UNLOCK_DNS(); + } #endif #else /* _PR_INET6 */ UNLOCK_DNS(); #endif /* _PR_INET6 */ #if defined(_PR_HAVE_GETHOST_R) - if (tmpbuf != localbuf) + if (tmpbuf != localbuf) { PR_Free(tmpbuf); + } #endif - return rv; + return rv; } PR_IMPLEMENT(PRStatus) PR_GetHostByAddr( const PRNetAddr *hostaddr, char *buf, PRIntn bufsize, PRHostEnt *hostentry) { - struct hostent *h; - PRStatus rv = PR_FAILURE; - const void *addr; - PRUint32 tmp_ip; - int addrlen; - PRInt32 af; + struct hostent *h; + PRStatus rv = PR_FAILURE; + const void *addr; + PRUint32 tmp_ip; + int addrlen; + PRInt32 af; #if defined(_PR_HAVE_GETHOST_R) char localbuf[PR_NETDB_BUF_SIZE]; char *tmpbuf; @@ -997,55 +1030,58 @@ PR_IMPLEMENT(PRStatus) PR_GetHostByAddr( int h_err; #endif #if defined(_PR_HAVE_GETIPNODEBYADDR) - int error_num; + int error_num; #endif - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } - if (hostaddr->raw.family == PR_AF_INET6) - { + if (hostaddr->raw.family == PR_AF_INET6) + { #if defined(_PR_INET6_PROBE) - af = _pr_ipv6_is_present() ? AF_INET6 : AF_INET; + af = _pr_ipv6_is_present() ? AF_INET6 : AF_INET; #elif defined(_PR_INET6) - af = AF_INET6; + af = AF_INET6; #else - af = AF_INET; + af = AF_INET; #endif #if defined(_PR_GHBA_DISALLOW_V4MAPPED) - if (_PR_IN6_IS_ADDR_V4MAPPED(&hostaddr->ipv6.ip)) - af = AF_INET; -#endif - } - else - { - PR_ASSERT(hostaddr->raw.family == AF_INET); - af = AF_INET; - } - if (hostaddr->raw.family == PR_AF_INET6) { + if (_PR_IN6_IS_ADDR_V4MAPPED(&hostaddr->ipv6.ip)) { + af = AF_INET; + } +#endif + } + else + { + PR_ASSERT(hostaddr->raw.family == AF_INET); + af = AF_INET; + } + if (hostaddr->raw.family == PR_AF_INET6) { #if defined(_PR_INET6) || defined(_PR_INET6_PROBE) - if (af == AF_INET6) { - addr = &hostaddr->ipv6.ip; - addrlen = sizeof(hostaddr->ipv6.ip); - } - else -#endif - { - PR_ASSERT(af == AF_INET); - if (!_PR_IN6_IS_ADDR_V4MAPPED(&hostaddr->ipv6.ip)) { - PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); - return rv; - } - tmp_ip = _PR_IN6_V4MAPPED_TO_IPADDR((PRIPv6Addr *) - &hostaddr->ipv6.ip); - addr = &tmp_ip; - addrlen = sizeof(tmp_ip); - } - } else { - PR_ASSERT(hostaddr->raw.family == AF_INET); - PR_ASSERT(af == AF_INET); - addr = &hostaddr->inet.ip; - addrlen = sizeof(hostaddr->inet.ip); - } + if (af == AF_INET6) { + addr = &hostaddr->ipv6.ip; + addrlen = sizeof(hostaddr->ipv6.ip); + } + else +#endif + { + PR_ASSERT(af == AF_INET); + if (!_PR_IN6_IS_ADDR_V4MAPPED(&hostaddr->ipv6.ip)) { + PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); + return rv; + } + tmp_ip = _PR_IN6_V4MAPPED_TO_IPADDR((PRIPv6Addr *) + &hostaddr->ipv6.ip); + addr = &tmp_ip; + addrlen = sizeof(tmp_ip); + } + } else { + PR_ASSERT(hostaddr->raw.family == AF_INET); + PR_ASSERT(af == AF_INET); + addr = &hostaddr->inet.ip; + addrlen = sizeof(hostaddr->inet.ip); + } #if defined(_PR_HAVE_GETHOST_R) tmpbuf = localbuf; @@ -1062,63 +1098,66 @@ PR_IMPLEMENT(PRStatus) PR_GetHostByAddr( /* Do not need to lock the DNS lock if getipnodebyaddr() is called */ #if defined(_PR_HAVE_GETIPNODEBYADDR) && defined(_PR_INET6) - h = getipnodebyaddr(addr, addrlen, af, &error_num); + h = getipnodebyaddr(addr, addrlen, af, &error_num); #elif defined(_PR_HAVE_GETIPNODEBYADDR) && defined(_PR_INET6_PROBE) if (_pr_ipv6_is_present()) { #ifdef PR_GETIPNODE_NOT_THREADSAFE LOCK_DNS(); #endif - h = (*((_pr_getipnodebyaddr_t)_pr_getipnodebyaddr_fp))(addr, addrlen, - af, &error_num); + h = (*((_pr_getipnodebyaddr_t)_pr_getipnodebyaddr_fp))(addr, addrlen, + af, &error_num); } - else + else { LOCK_DNS(); - h = GETHOSTBYADDR(addr, addrlen, af); + h = GETHOSTBYADDR(addr, addrlen, af); } -#else /* _PR_HAVE_GETIPNODEBYADDR */ +#else /* _PR_HAVE_GETIPNODEBYADDR */ LOCK_DNS(); - h = GETHOSTBYADDR(addr, addrlen, af); + h = GETHOSTBYADDR(addr, addrlen, af); #endif /* _PR_HAVE_GETIPNODEBYADDR */ - if (NULL == h) - { + if (NULL == h) + { #if defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYADDR) - PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, error_num); + PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, error_num); #elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYADDR) - if (_pr_ipv6_is_present()) - PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, error_num); - else - PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO()); + if (_pr_ipv6_is_present()) { + PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, error_num); + } + else { + PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO()); + } #else - PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO()); -#endif - } - else - { - _PRIPAddrConversion conversion = _PRIPAddrNoConversion; - if (hostaddr->raw.family == PR_AF_INET6) { - if (af == AF_INET) { - if (_PR_IN6_IS_ADDR_V4MAPPED((PRIPv6Addr*) - &hostaddr->ipv6.ip)) { - conversion = _PRIPAddrIPv4Mapped; - } else if (_PR_IN6_IS_ADDR_V4COMPAT((PRIPv6Addr *) - &hostaddr->ipv6.ip)) { - conversion = _PRIPAddrIPv4Compat; - } - } - } - rv = CopyHostent(h, &buf, &bufsize, conversion, hostentry); - if (PR_SUCCESS != rv) { - PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); - } + PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_GETHOST_ERRNO()); +#endif + } + else + { + _PRIPAddrConversion conversion = _PRIPAddrNoConversion; + if (hostaddr->raw.family == PR_AF_INET6) { + if (af == AF_INET) { + if (_PR_IN6_IS_ADDR_V4MAPPED((PRIPv6Addr*) + &hostaddr->ipv6.ip)) { + conversion = _PRIPAddrIPv4Mapped; + } else if (_PR_IN6_IS_ADDR_V4COMPAT((PRIPv6Addr *) + &hostaddr->ipv6.ip)) { + conversion = _PRIPAddrIPv4Compat; + } + } + } + rv = CopyHostent(h, &buf, &bufsize, conversion, hostentry); + if (PR_SUCCESS != rv) { + PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); + } #if defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYADDR) - freehostent(h); + freehostent(h); #elif defined(_PR_INET6_PROBE) && defined(_PR_HAVE_GETIPNODEBYADDR) - if (_pr_ipv6_is_present()) - (*((_pr_freehostent_t)_pr_freehostent_fp))(h); + if (_pr_ipv6_is_present()) { + (*((_pr_freehostent_t)_pr_freehostent_fp))(h); + } #endif - } + } /* Must match the convoluted logic above for LOCK_DNS() */ #if defined(_PR_HAVE_GETIPNODEBYADDR) && defined(_PR_INET6) @@ -1126,19 +1165,21 @@ PR_IMPLEMENT(PRStatus) PR_GetHostByAddr( #ifdef PR_GETIPNODE_NOT_THREADSAFE UNLOCK_DNS(); #else - if (!_pr_ipv6_is_present()) + if (!_pr_ipv6_is_present()) { UNLOCK_DNS(); + } #endif -#else /* _PR_HAVE_GETIPNODEBYADDR */ +#else /* _PR_HAVE_GETIPNODEBYADDR */ UNLOCK_DNS(); #endif /* _PR_HAVE_GETIPNODEBYADDR */ #if defined(_PR_HAVE_GETHOST_R) - if (tmpbuf != localbuf) + if (tmpbuf != localbuf) { PR_Free(tmpbuf); + } #endif - return rv; + return rv; } /******************************************************************************/ @@ -1169,12 +1210,12 @@ PR_IMPLEMENT(PRStatus) PR_GetHostByAddr( static struct protoent *getprotobyname_r(const char* name) { - return getprotobyname(name); + return getprotobyname(name); } /* getprotobyname_r */ static struct protoent *getprotobynumber_r(PRInt32 number) { - return getprotobynumber(number); + return getprotobynumber(number); } /* getprotobynumber_r */ #endif /* !defined(_PR_HAVE_GETPROTO_R) */ @@ -1182,12 +1223,14 @@ static struct protoent *getprotobynumber_r(PRInt32 number) PR_IMPLEMENT(PRStatus) PR_GetProtoByName( const char* name, char* buffer, PRInt32 buflen, PRProtoEnt* result) { - PRStatus rv = PR_SUCCESS; + PRStatus rv = PR_SUCCESS; #if defined(_PR_HAVE_GETPROTO_R) - struct protoent* res = (struct protoent*)result; + struct protoent* res = (struct protoent*)result; #endif - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } #if defined(_PR_HAVE_GETPROTO_R_INT) { @@ -1225,7 +1268,7 @@ PR_IMPLEMENT(PRStatus) PR_GetProtoByName( ** at least the size of a struct protoent_data. */ memset(buffer, 0, buflen); - if (-1 == getprotobyname_r(name, res, (struct protoent_data*)buffer)) + if (-1 == getprotobyname_r(name, res, (struct protoent_data*)buffer)) { PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_ERRNO()); return PR_FAILURE; @@ -1238,27 +1281,24 @@ PR_IMPLEMENT(PRStatus) PR_GetProtoByName( return PR_FAILURE; } #else /* do it the hard way */ - { - struct protoent *staticBuf; - PR_Lock(_getproto_lock); - staticBuf = getprotobyname_r(name); - if (NULL == staticBuf) - { - rv = PR_FAILURE; - PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_ERRNO()); - } - else - { -#if defined(SYMBIAN) - char* aliases[2]; - AssignAliases(staticBuf, aliases); -#endif - rv = CopyProtoent(staticBuf, buffer, buflen, result); - if (PR_FAILURE == rv) - PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); - } - PR_Unlock(_getproto_lock); - } + { + struct protoent *staticBuf; + PR_Lock(_getproto_lock); + staticBuf = getprotobyname_r(name); + if (NULL == staticBuf) + { + rv = PR_FAILURE; + PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_ERRNO()); + } + else + { + rv = CopyProtoent(staticBuf, buffer, buflen, result); + if (PR_FAILURE == rv) { + PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); + } + } + PR_Unlock(_getproto_lock); + } #endif /* all that */ return rv; } @@ -1266,12 +1306,14 @@ PR_IMPLEMENT(PRStatus) PR_GetProtoByName( PR_IMPLEMENT(PRStatus) PR_GetProtoByNumber( PRInt32 number, char* buffer, PRInt32 buflen, PRProtoEnt* result) { - PRStatus rv = PR_SUCCESS; + PRStatus rv = PR_SUCCESS; #if defined(_PR_HAVE_GETPROTO_R) - struct protoent* res = (struct protoent*)result; + struct protoent* res = (struct protoent*)result; #endif - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } #if defined(_PR_HAVE_GETPROTO_R_INT) { @@ -1309,7 +1351,7 @@ PR_IMPLEMENT(PRStatus) PR_GetProtoByNumber( ** The buffer needs to be zero'd for these OS's. */ memset(buffer, 0, buflen); - if (-1 == getprotobynumber_r(number, res, (struct protoent_data*)buffer)) + if (-1 == getprotobynumber_r(number, res, (struct protoent_data*)buffer)) { PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_ERRNO()); return PR_FAILURE; @@ -1322,27 +1364,24 @@ PR_IMPLEMENT(PRStatus) PR_GetProtoByNumber( return PR_FAILURE; } #else /* do it the hard way */ - { - struct protoent *staticBuf; - PR_Lock(_getproto_lock); - staticBuf = getprotobynumber_r(number); - if (NULL == staticBuf) - { - rv = PR_FAILURE; - PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_ERRNO()); - } - else - { -#if defined(SYMBIAN) - char* aliases[2]; - AssignAliases(staticBuf, aliases); -#endif - rv = CopyProtoent(staticBuf, buffer, buflen, result); - if (PR_FAILURE == rv) - PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); - } - PR_Unlock(_getproto_lock); - } + { + struct protoent *staticBuf; + PR_Lock(_getproto_lock); + staticBuf = getprotobynumber_r(number); + if (NULL == staticBuf) + { + rv = PR_FAILURE; + PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, _MD_ERRNO()); + } + else + { + rv = CopyProtoent(staticBuf, buffer, buflen, result); + if (PR_FAILURE == rv) { + PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0); + } + } + PR_Unlock(_getproto_lock); + } #endif /* all that crap */ return rv; @@ -1360,8 +1399,9 @@ PRUintn _PR_NetAddrSize(const PRNetAddr* addr) * we take the size of struct sockaddr_in6 instead of * addr->ipv6. */ - if (AF_INET == addr->raw.family) + if (AF_INET == addr->raw.family) { addrsize = sizeof(addr->inet); + } else if (PR_AF_INET6 == addr->raw.family) #if defined(_PR_INET6) addrsize = sizeof(struct sockaddr_in6); @@ -1382,7 +1422,9 @@ PRUintn _PR_NetAddrSize(const PRNetAddr* addr) addrsize = sizeof(addr->local); } #endif - else addrsize = 0; + else { + addrsize = 0; + } return addrsize; } /* _PR_NetAddrSize */ @@ -1392,15 +1434,17 @@ PR_IMPLEMENT(PRIntn) PR_EnumerateHostEnt( { void *addr = hostEnt->h_addr_list[enumIndex++]; memset(address, 0, sizeof(PRNetAddr)); - if (NULL == addr) enumIndex = 0; + if (NULL == addr) { + enumIndex = 0; + } else { address->raw.family = hostEnt->h_addrtype; if (PR_AF_INET6 == hostEnt->h_addrtype) { address->ipv6.port = htons(port); - address->ipv6.flowinfo = 0; - address->ipv6.scope_id = 0; + address->ipv6.flowinfo = 0; + address->ipv6.scope_id = 0; memcpy(&address->ipv6.ip, addr, hostEnt->h_length); } else @@ -1417,25 +1461,29 @@ PR_IMPLEMENT(PRStatus) PR_InitializeNetAddr( PRNetAddrValue val, PRUint16 port, PRNetAddr *addr) { PRStatus rv = PR_SUCCESS; - if (!_pr_initialized) _PR_ImplicitInitialization(); - - if (val != PR_IpAddrNull) memset(addr, 0, sizeof(*addr)); - addr->inet.family = AF_INET; - addr->inet.port = htons(port); - switch (val) - { - case PR_IpAddrNull: - break; /* don't overwrite the address */ - case PR_IpAddrAny: - addr->inet.ip = htonl(INADDR_ANY); - break; - case PR_IpAddrLoopback: - addr->inet.ip = htonl(INADDR_LOOPBACK); - break; - default: - PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); - rv = PR_FAILURE; - } + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } + + if (val != PR_IpAddrNull) { + memset(addr, 0, sizeof(*addr)); + } + addr->inet.family = AF_INET; + addr->inet.port = htons(port); + switch (val) + { + case PR_IpAddrNull: + break; /* don't overwrite the address */ + case PR_IpAddrAny: + addr->inet.ip = htonl(INADDR_ANY); + break; + case PR_IpAddrLoopback: + addr->inet.ip = htonl(INADDR_LOOPBACK); + break; + default: + PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); + rv = PR_FAILURE; + } return rv; } /* PR_InitializeNetAddr */ @@ -1443,48 +1491,54 @@ PR_IMPLEMENT(PRStatus) PR_SetNetAddr( PRNetAddrValue val, PRUint16 af, PRUint16 port, PRNetAddr *addr) { PRStatus rv = PR_SUCCESS; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } if (af == PR_AF_INET6) { - if (val != PR_IpAddrNull) memset(addr, 0, sizeof(addr->ipv6)); + if (val != PR_IpAddrNull) { + memset(addr, 0, sizeof(addr->ipv6)); + } addr->ipv6.family = af; addr->ipv6.port = htons(port); addr->ipv6.flowinfo = 0; addr->ipv6.scope_id = 0; switch (val) { - case PR_IpAddrNull: - break; /* don't overwrite the address */ - case PR_IpAddrAny: - addr->ipv6.ip = _pr_in6addr_any; - break; - case PR_IpAddrLoopback: - addr->ipv6.ip = _pr_in6addr_loopback; - break; - default: - PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); - rv = PR_FAILURE; + case PR_IpAddrNull: + break; /* don't overwrite the address */ + case PR_IpAddrAny: + addr->ipv6.ip = _pr_in6addr_any; + break; + case PR_IpAddrLoopback: + addr->ipv6.ip = _pr_in6addr_loopback; + break; + default: + PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); + rv = PR_FAILURE; } } else { - if (val != PR_IpAddrNull) memset(addr, 0, sizeof(addr->inet)); + if (val != PR_IpAddrNull) { + memset(addr, 0, sizeof(addr->inet)); + } addr->inet.family = af; addr->inet.port = htons(port); switch (val) { - case PR_IpAddrNull: - break; /* don't overwrite the address */ - case PR_IpAddrAny: - addr->inet.ip = htonl(INADDR_ANY); - break; - case PR_IpAddrLoopback: - addr->inet.ip = htonl(INADDR_LOOPBACK); - break; - default: - PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); - rv = PR_FAILURE; + case PR_IpAddrNull: + break; /* don't overwrite the address */ + case PR_IpAddrAny: + addr->inet.ip = htonl(INADDR_ANY); + break; + case PR_IpAddrLoopback: + addr->inet.ip = htonl(INADDR_LOOPBACK); + break; + default: + PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); + rv = PR_FAILURE; } } return rv; @@ -1495,25 +1549,25 @@ PR_IsNetAddrType(const PRNetAddr *addr, PRNetAddrValue val) { if (addr->raw.family == PR_AF_INET6) { if (val == PR_IpAddrAny) { - if (_PR_IN6_IS_ADDR_UNSPECIFIED((PRIPv6Addr *)&addr->ipv6.ip)) { - return PR_TRUE; - } + if (_PR_IN6_IS_ADDR_UNSPECIFIED((PRIPv6Addr *)&addr->ipv6.ip)) { + return PR_TRUE; + } if (_PR_IN6_IS_ADDR_V4MAPPED((PRIPv6Addr *)&addr->ipv6.ip) && _PR_IN6_V4MAPPED_TO_IPADDR((PRIPv6Addr *)&addr->ipv6.ip) == htonl(INADDR_ANY)) { return PR_TRUE; - } + } } else if (val == PR_IpAddrLoopback) { if (_PR_IN6_IS_ADDR_LOOPBACK((PRIPv6Addr *)&addr->ipv6.ip)) { - return PR_TRUE; - } + return PR_TRUE; + } if (_PR_IN6_IS_ADDR_V4MAPPED((PRIPv6Addr *)&addr->ipv6.ip) && _PR_IN6_V4MAPPED_TO_IPADDR((PRIPv6Addr *)&addr->ipv6.ip) == htonl(INADDR_LOOPBACK)) { return PR_TRUE; - } + } } else if (val == PR_IpAddrV4Mapped - && _PR_IN6_IS_ADDR_V4MAPPED((PRIPv6Addr *)&addr->ipv6.ip)) { + && _PR_IN6_IS_ADDR_V4MAPPED((PRIPv6Addr *)&addr->ipv6.ip)) { return PR_TRUE; } } else { @@ -1537,7 +1591,7 @@ static const unsigned char index_hex[256] = { XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,XX,XX, XX,XX,XX,XX, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,XX,XX, XX,XX,XX,XX, XX,10,11,12, 13,14,15,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,10,11,12, 13,14,15,XX, XX,XX,XX,XX, XX,XX,XX,XX, @@ -1570,16 +1624,22 @@ static int StringToV6Addr(const char *string, PRIPv6Addr *addr) /* Handle initial (double) colon */ if (*s == ':') { - if (s[1] != ':') return 0; + if (s[1] != ':') { + return 0; + } s += 2; addr->pr_s6_addr16[0] = 0; section = double_colon = 1; } while (*s) { - if (section == 8) return 0; /* too long */ + if (section == 8) { + return 0; /* too long */ + } if (*s == ':') { - if (double_colon != -1) return 0; /* two double colons */ + if (double_colon != -1) { + return 0; /* two double colons */ + } addr->pr_s6_addr16[section++] = 0; double_colon = section; s++; @@ -1589,70 +1649,96 @@ static int StringToV6Addr(const char *string, PRIPv6Addr *addr) val = (val << 4) + index_hex[*s++]; } if (*s == '.') { - if (len == 0) return 0; /* nothing between : and . */ + if (len == 0) { + return 0; /* nothing between : and . */ + } break; } if (*s == ':') { s++; - if (!*s) return 0; /* cannot end with single colon */ + if (!*s) { + return 0; /* cannot end with single colon */ + } } else if (*s) { return 0; /* bad character */ } addr->pr_s6_addr16[section++] = htons((unsigned short)val); } - + if (*s == '.') { /* Have a trailing v4 format address */ - if (section > 6) return 0; /* not enough room */ + if (section > 6) { + return 0; /* not enough room */ + } /* * The number before the '.' is decimal, but we parsed it * as hex. That means it is in BCD. Check it for validity * and convert it to binary. */ - if (val > 0x0255 || (val & 0xf0) > 0x90 || (val & 0xf) > 9) return 0; + if (val > 0x0255 || (val & 0xf0) > 0x90 || (val & 0xf) > 9) { + return 0; + } val = (val >> 8) * 100 + ((val >> 4) & 0xf) * 10 + (val & 0xf); addr->pr_s6_addr[2 * section] = val; s++; val = index_hex[*s++]; - if (val > 9) return 0; + if (val > 9) { + return 0; + } while (*s >= '0' && *s <= '9') { val = val * 10 + *s++ - '0'; - if (val > 255) return 0; + if (val > 255) { + return 0; + } + } + if (*s != '.') { + return 0; /* must have exactly 4 decimal numbers */ } - if (*s != '.') return 0; /* must have exactly 4 decimal numbers */ addr->pr_s6_addr[2 * section + 1] = val; section++; s++; val = index_hex[*s++]; - if (val > 9) return 0; + if (val > 9) { + return 0; + } while (*s >= '0' && *s <= '9') { val = val * 10 + *s++ - '0'; - if (val > 255) return 0; + if (val > 255) { + return 0; + } + } + if (*s != '.') { + return 0; /* must have exactly 4 decimal numbers */ } - if (*s != '.') return 0; /* must have exactly 4 decimal numbers */ addr->pr_s6_addr[2 * section] = val; s++; val = index_hex[*s++]; - if (val > 9) return 0; + if (val > 9) { + return 0; + } while (*s >= '0' && *s <= '9') { val = val * 10 + *s++ - '0'; - if (val > 255) return 0; + if (val > 255) { + return 0; + } + } + if (*s) { + return 0; /* must have exactly 4 decimal numbers */ } - if (*s) return 0; /* must have exactly 4 decimal numbers */ addr->pr_s6_addr[2 * section + 1] = val; section++; } - + if (double_colon != -1) { /* Stretch the double colon */ int tosection; int ncopy = section - double_colon; for (tosection = 7; ncopy--; tosection--) { - addr->pr_s6_addr16[tosection] = + addr->pr_s6_addr16[tosection] = addr->pr_s6_addr16[double_colon + ncopy]; } while (tosection >= double_colon) { @@ -1726,20 +1812,36 @@ static const char *V6AddrToString( STUFF('f'); STUFF(':'); } - if (addr->pr_s6_addr[12] > 99) STUFF(addr->pr_s6_addr[12]/100 + '0'); - if (addr->pr_s6_addr[12] > 9) STUFF((addr->pr_s6_addr[12]%100)/10 + '0'); + if (addr->pr_s6_addr[12] > 99) { + STUFF(addr->pr_s6_addr[12]/100 + '0'); + } + if (addr->pr_s6_addr[12] > 9) { + STUFF((addr->pr_s6_addr[12]%100)/10 + '0'); + } STUFF(addr->pr_s6_addr[12]%10 + '0'); STUFF('.'); - if (addr->pr_s6_addr[13] > 99) STUFF(addr->pr_s6_addr[13]/100 + '0'); - if (addr->pr_s6_addr[13] > 9) STUFF((addr->pr_s6_addr[13]%100)/10 + '0'); + if (addr->pr_s6_addr[13] > 99) { + STUFF(addr->pr_s6_addr[13]/100 + '0'); + } + if (addr->pr_s6_addr[13] > 9) { + STUFF((addr->pr_s6_addr[13]%100)/10 + '0'); + } STUFF(addr->pr_s6_addr[13]%10 + '0'); STUFF('.'); - if (addr->pr_s6_addr[14] > 99) STUFF(addr->pr_s6_addr[14]/100 + '0'); - if (addr->pr_s6_addr[14] > 9) STUFF((addr->pr_s6_addr[14]%100)/10 + '0'); + if (addr->pr_s6_addr[14] > 99) { + STUFF(addr->pr_s6_addr[14]/100 + '0'); + } + if (addr->pr_s6_addr[14] > 9) { + STUFF((addr->pr_s6_addr[14]%100)/10 + '0'); + } STUFF(addr->pr_s6_addr[14]%10 + '0'); STUFF('.'); - if (addr->pr_s6_addr[15] > 99) STUFF(addr->pr_s6_addr[15]/100 + '0'); - if (addr->pr_s6_addr[15] > 9) STUFF((addr->pr_s6_addr[15]%100)/10 + '0'); + if (addr->pr_s6_addr[15] > 99) { + STUFF(addr->pr_s6_addr[15]/100 + '0'); + } + if (addr->pr_s6_addr[15] > 9) { + STUFF((addr->pr_s6_addr[15]%100)/10 + '0'); + } STUFF(addr->pr_s6_addr[15]%10 + '0'); STUFF('\0'); return bufcopy; @@ -1765,11 +1867,13 @@ static const char *V6AddrToString( } STUFF(basis_hex[val & 0xf]); section++; - if (section < 8 && section != double_colon) STUFF(':'); + if (section < 8 && section != double_colon) { + STUFF(':'); + } } STUFF('\0'); return bufcopy; -#undef STUFF +#undef STUFF } #endif /* !_PR_HAVE_INET_NTOP */ @@ -1785,10 +1889,18 @@ PR_IMPLEMENT(void) PR_ConvertIPv4AddrToIPv6(PRUint32 v4addr, PRIPv6Addr *v6addr) memcpy(dstp + 12,(char *) &v4addr, 4); } -PR_IMPLEMENT(PRUint16) PR_ntohs(PRUint16 n) { return ntohs(n); } -PR_IMPLEMENT(PRUint32) PR_ntohl(PRUint32 n) { return ntohl(n); } -PR_IMPLEMENT(PRUint16) PR_htons(PRUint16 n) { return htons(n); } -PR_IMPLEMENT(PRUint32) PR_htonl(PRUint32 n) { return htonl(n); } +PR_IMPLEMENT(PRUint16) PR_ntohs(PRUint16 n) { + return ntohs(n); +} +PR_IMPLEMENT(PRUint32) PR_ntohl(PRUint32 n) { + return ntohl(n); +} +PR_IMPLEMENT(PRUint16) PR_htons(PRUint16 n) { + return htons(n); +} +PR_IMPLEMENT(PRUint32) PR_htonl(PRUint32 n) { + return htonl(n); +} PR_IMPLEMENT(PRUint64) PR_ntohll(PRUint64 n) { #ifdef IS_BIG_ENDIAN @@ -1849,23 +1961,23 @@ typedef struct addrinfo PRADDRINFO; typedef struct addrinfo PRADDRINFO; -/* getaddrinfo/freeaddrinfo/getnameinfo prototypes */ +/* getaddrinfo/freeaddrinfo/getnameinfo prototypes */ #if defined(WIN32) #define FUNC_MODIFIER __stdcall #else #define FUNC_MODIFIER #endif typedef int (FUNC_MODIFIER * FN_GETADDRINFO) - (const char *nodename, - const char *servname, - const PRADDRINFO *hints, - PRADDRINFO **res); +(const char *nodename, + const char *servname, + const PRADDRINFO *hints, + PRADDRINFO **res); typedef int (FUNC_MODIFIER * FN_FREEADDRINFO) - (PRADDRINFO *ai); +(PRADDRINFO *ai); typedef int (FUNC_MODIFIER * FN_GETNAMEINFO) - (const struct sockaddr *addr, int addrlen, - char *host, int hostlen, - char *serv, int servlen, int flags); +(const struct sockaddr *addr, int addrlen, + char *host, int hostlen, + char *serv, int servlen, int flags); /* global state */ static FN_GETADDRINFO _pr_getaddrinfo = NULL; @@ -1896,15 +2008,15 @@ _pr_find_getaddrinfo(void) continue; } _pr_getaddrinfo = (FN_GETADDRINFO) - PR_FindFunctionSymbol(lib, GETADDRINFO_SYMBOL); + PR_FindFunctionSymbol(lib, GETADDRINFO_SYMBOL); if (!_pr_getaddrinfo) { PR_UnloadLibrary(lib); continue; } _pr_freeaddrinfo = (FN_FREEADDRINFO) - PR_FindFunctionSymbol(lib, FREEADDRINFO_SYMBOL); + PR_FindFunctionSymbol(lib, FREEADDRINFO_SYMBOL); _pr_getnameinfo = (FN_GETNAMEINFO) - PR_FindFunctionSymbol(lib, GETNAMEINFO_SYMBOL); + PR_FindFunctionSymbol(lib, GETNAMEINFO_SYMBOL); if (!_pr_freeaddrinfo || !_pr_getnameinfo) { PR_UnloadLibrary(lib); continue; @@ -1919,14 +2031,14 @@ _pr_find_getaddrinfo(void) * search library containing getaddrinfo for freeaddrinfo. */ _pr_getaddrinfo = (FN_GETADDRINFO) - PR_FindFunctionSymbolAndLibrary(GETADDRINFO_SYMBOL, &lib); + PR_FindFunctionSymbolAndLibrary(GETADDRINFO_SYMBOL, &lib); if (!_pr_getaddrinfo) { return PR_FAILURE; } _pr_freeaddrinfo = (FN_FREEADDRINFO) - PR_FindFunctionSymbol(lib, FREEADDRINFO_SYMBOL); + PR_FindFunctionSymbol(lib, FREEADDRINFO_SYMBOL); _pr_getnameinfo = (FN_GETNAMEINFO) - PR_FindFunctionSymbol(lib, GETNAMEINFO_SYMBOL); + PR_FindFunctionSymbol(lib, GETNAMEINFO_SYMBOL); PR_UnloadLibrary(lib); if (!_pr_freeaddrinfo || !_pr_getnameinfo) { return PR_FAILURE; @@ -1946,7 +2058,7 @@ _pr_find_getaddrinfo(void) #if !defined(_PR_HAVE_GETADDRINFO) || defined(_PR_INET6_PROBE) /* * If getaddrinfo does not exist, then we will fall back on - * PR_GetHostByName, which requires that we allocate a buffer for the + * PR_GetHostByName, which requires that we allocate a buffer for the * PRHostEnt data structure and its members. */ typedef struct PRAddrInfoFB { @@ -1980,8 +2092,8 @@ pr_GetAddrInfoByNameFB(const char *hostname, #endif /* !_PR_HAVE_GETADDRINFO || _PR_INET6_PROBE */ PR_IMPLEMENT(PRAddrInfo *) PR_GetAddrInfoByName(const char *hostname, - PRUint16 af, - PRIntn flags) + PRUint16 af, + PRIntn flags) { /* restrict input to supported values */ if ((af != PR_AF_INET && af != PR_AF_UNSPEC) || @@ -1990,7 +2102,9 @@ PR_IMPLEMENT(PRAddrInfo *) PR_GetAddrInfoByName(const char *hostname, return NULL; } - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } #if !defined(_PR_HAVE_GETADDRINFO) return pr_GetAddrInfoByNameFB(hostname, af, flags); @@ -2011,14 +2125,15 @@ PR_IMPLEMENT(PRAddrInfo *) PR_GetAddrInfoByName(const char *hostname, */ memset(&hints, 0, sizeof(hints)); - if (!(flags & PR_AI_NOCANONNAME)) + if (!(flags & PR_AI_NOCANONNAME)) { hints.ai_flags |= AI_CANONNAME; + } #ifdef AI_ADDRCONFIG - /* + /* * Propagate AI_ADDRCONFIG to the GETADDRINFO call if PR_AI_ADDRCONFIG * is set. - * - * Need a workaround for loopback host addresses: + * + * Need a workaround for loopback host addresses: * The problem is that in glibc and Windows, AI_ADDRCONFIG applies the * existence of an outgoing network interface to IP addresses of the * loopback interface, due to a strict interpretation of the @@ -2055,8 +2170,9 @@ PR_IMPLEMENT(PRAddrInfo *) PR_GetAddrInfoByName(const char *hostname, rv = GETADDRINFO(hostname, NULL, &hints, &res); } #endif - if (rv == 0) + if (rv == 0) { return (PRAddrInfo *) res; + } PR_SetError(PR_DIRECTORY_LOOKUP_ERROR, rv); } @@ -2068,8 +2184,9 @@ PR_IMPLEMENT(void) PR_FreeAddrInfo(PRAddrInfo *ai) { #if defined(_PR_HAVE_GETADDRINFO) #if defined(_PR_INET6_PROBE) - if (!_pr_ipv6_is_present()) + if (!_pr_ipv6_is_present()) { PR_Free((PRAddrInfoFB *) ai); + } else #endif FREEADDRINFO((PRADDRINFO *) ai); @@ -2079,9 +2196,9 @@ PR_IMPLEMENT(void) PR_FreeAddrInfo(PRAddrInfo *ai) } PR_IMPLEMENT(void *) PR_EnumerateAddrInfo(void *iterPtr, - const PRAddrInfo *base, - PRUint16 port, - PRNetAddr *result) + const PRAddrInfo *base, + PRUint16 port, + PRNetAddr *result) { #if defined(_PR_HAVE_GETADDRINFO) PRADDRINFO *ai; @@ -2090,35 +2207,43 @@ PR_IMPLEMENT(void *) PR_EnumerateAddrInfo(void *iterPtr, /* using PRAddrInfoFB */ PRIntn iter = (PRIntn)(PRPtrdiff) iterPtr; iter = PR_EnumerateHostEnt(iter, &((PRAddrInfoFB *) base)->hostent, port, result); - if (iter < 0) + if (iter < 0) { iter = 0; + } return (void *)(PRPtrdiff) iter; } #endif - if (iterPtr) + if (iterPtr) { ai = ((PRADDRINFO *) iterPtr)->ai_next; - else + } + else { ai = (PRADDRINFO *) base; + } - while (ai && ai->ai_addrlen > sizeof(PRNetAddr)) + while (ai && ai->ai_addrlen > sizeof(PRNetAddr)) { ai = ai->ai_next; + } if (ai) { /* copy sockaddr to PRNetAddr */ memcpy(result, ai->ai_addr, ai->ai_addrlen); result->raw.family = ai->ai_addr->sa_family; #ifdef _PR_INET6 - if (AF_INET6 == result->raw.family) + if (AF_INET6 == result->raw.family) { result->raw.family = PR_AF_INET6; + } #endif - if (ai->ai_addrlen < sizeof(PRNetAddr)) + if (ai->ai_addrlen < sizeof(PRNetAddr)) { memset(((char*)result)+ai->ai_addrlen, 0, sizeof(PRNetAddr) - ai->ai_addrlen); + } - if (result->raw.family == PR_AF_INET) + if (result->raw.family == PR_AF_INET) { result->inet.port = htons(port); - else + } + else { result->ipv6.port = htons(port); + } } return ai; @@ -2126,8 +2251,9 @@ PR_IMPLEMENT(void *) PR_EnumerateAddrInfo(void *iterPtr, /* using PRAddrInfoFB */ PRIntn iter = (PRIntn) iterPtr; iter = PR_EnumerateHostEnt(iter, &((PRAddrInfoFB *) base)->hostent, port, result); - if (iter < 0) + if (iter < 0) { iter = 0; + } return (void *) iter; #endif } @@ -2139,7 +2265,7 @@ PR_IMPLEMENT(const char *) PR_GetCanonNameFromAddrInfo(const PRAddrInfo *ai) if (!_pr_ipv6_is_present()) { const PRAddrInfoFB *fb = (const PRAddrInfoFB *) ai; return fb->has_cname ? fb->hostent.h_name : NULL; - } + } #endif return ((const PRADDRINFO *) ai)->ai_canonname; #else @@ -2221,7 +2347,9 @@ static PRStatus pr_StringToNetAddrFB(const char *string, PRNetAddr *addr) PR_IMPLEMENT(PRStatus) PR_StringToNetAddr(const char *string, PRNetAddr *addr) { - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } if (!addr || !string || !*string) { @@ -2238,12 +2366,14 @@ PR_IMPLEMENT(PRStatus) PR_StringToNetAddr(const char *string, PRNetAddr *addr) * and most likely others. So we only use it to convert literal IP addresses * that contain IPv6 scope IDs, which pr_inet_aton cannot convert. */ - if (!strchr(string, '%')) + if (!strchr(string, '%')) { return pr_StringToNetAddrFB(string, addr); + } #if defined(_PR_INET6_PROBE) - if (!_pr_ipv6_is_present()) + if (!_pr_ipv6_is_present()) { return pr_StringToNetAddrFB(string, addr); + } #endif return pr_StringToNetAddrGAI(string, addr); @@ -2282,7 +2412,7 @@ static PRStatus pr_NetAddrToStringGNI( addrp = &addrcopy; #endif rv = GETNAMEINFO((const struct sockaddr *)addrp, addrlen, - string, size, NULL, 0, NI_NUMERICHOST); + string, size, NULL, 0, NI_NUMERICHOST); if (rv != 0) { PR_SetError(PR_INVALID_ARGUMENT_ERROR, rv); @@ -2311,13 +2441,17 @@ static PRStatus pr_NetAddrToStringFB( } else { - if (size < 16) goto failed; - if (AF_INET != addr->raw.family) goto failed; + if (size < 16) { + goto failed; + } + if (AF_INET != addr->raw.family) { + goto failed; + } else { unsigned char *byte = (unsigned char*)&addr->inet.ip; PR_snprintf(string, size, "%u.%u.%u.%u", - byte[0], byte[1], byte[2], byte[3]); + byte[0], byte[1], byte[2], byte[3]); } } @@ -2333,14 +2467,17 @@ failed: PR_IMPLEMENT(PRStatus) PR_NetAddrToString( const PRNetAddr *addr, char *string, PRUint32 size) { - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } #if !defined(_PR_HAVE_GETADDRINFO) return pr_NetAddrToStringFB(addr, string, size); #else #if defined(_PR_INET6_PROBE) - if (!_pr_ipv6_is_present()) + if (!_pr_ipv6_is_present()) { return pr_NetAddrToStringFB(addr, string, size); + } #endif return pr_NetAddrToStringGNI(addr, string, size); #endif |