diff options
| author | Carmelo Amoroso <carmelo.amoroso@st.com> | 2008-07-09 16:47:01 +0000 |
|---|---|---|
| committer | Carmelo Amoroso <carmelo.amoroso@st.com> | 2008-07-09 16:47:01 +0000 |
| commit | 62a21af8006ab04282fdc354c5b4dc765f56d058 (patch) | |
| tree | 568761d58289238aa14cced3f0010809d4d28c00 /libc/inet | |
| parent | ef250238dc1572caf859c2b64652f9cdfb0d9e42 (diff) | |
| download | uClibc-alpine-62a21af8006ab04282fdc354c5b4dc765f56d058.tar.bz2 uClibc-alpine-62a21af8006ab04282fdc354c5b4dc765f56d058.tar.xz | |
BIG BIG commit: all left files merged from trunk [rev 22714]. Currenntly NPTL sh4 port build and work fine. All committed to allow Khem Ray working on a working branch to integrate the ARM nptl port. MIPS nptl port not tested but should still building and working fine. There are some other part non yet merged with trunk (misc/internals and some headers file that need some more work). Signed-off-by: Carmelo Amoroso <carmelo.amoroso@st.com>
Diffstat (limited to 'libc/inet')
46 files changed, 1180 insertions, 1002 deletions
diff --git a/libc/inet/addr.c b/libc/inet/addr.c index ba2a022d6..2a0bb84a8 100644 --- a/libc/inet/addr.c +++ b/libc/inet/addr.c @@ -45,7 +45,7 @@ */ #ifdef __UCLIBC_HAS_XLOCALE__ libc_hidden_proto(__ctype_b_loc) -#elif __UCLIBC_HAS_CTYPE_TABLES__ +#elif defined __UCLIBC_HAS_CTYPE_TABLES__ libc_hidden_proto(__ctype_b) #endif libc_hidden_proto(inet_aton) @@ -55,6 +55,10 @@ int inet_aton(const char *cp, struct in_addr *addrptr) int value; int part; + if (cp == NULL) { + return 0; + } + addr = 0; for (part = 1; part <= 4; part++) { @@ -75,7 +79,7 @@ int inet_aton(const char *cp, struct in_addr *addrptr) } else { char c = *cp++; if (c != '\0' && !isspace(c)) - return 0; + return 0; } addr <<= 8; @@ -118,7 +122,6 @@ libc_hidden_def(inet_addr) #define INET_NTOA_MAX_LEN 16 /* max 12 digits + 3 '.'s + 1 nul */ -extern char *inet_ntoa_r(struct in_addr in, char buf[INET_NTOA_MAX_LEN]); libc_hidden_proto(inet_ntoa_r) char *inet_ntoa_r(struct in_addr in, char buf[INET_NTOA_MAX_LEN]) { @@ -128,7 +131,7 @@ char *inet_ntoa_r(struct in_addr in, char buf[INET_NTOA_MAX_LEN]) q = 0; p = buf + INET_NTOA_MAX_LEN - 1; /* cannot use sizeof(buf) here */ - for (i=0 ; i < 4 ; i++ ) { + for (i = 0; i < 4; i++ ) { p = _int10tostr(p, addr & 0xff) - 1; addr >>= 8; if (q) { @@ -145,7 +148,7 @@ libc_hidden_proto(inet_ntoa) char *inet_ntoa(struct in_addr in) { static char buf[INET_NTOA_MAX_LEN]; - return(inet_ntoa_r(in, buf)); + return inet_ntoa_r(in, buf); } libc_hidden_def(inet_ntoa) #endif @@ -153,7 +156,7 @@ libc_hidden_def(inet_ntoa) #ifdef L_inet_makeaddr /* for some reason it does not remove the jump relocation */ -libc_hidden_proto(memmove) +/* Experimentally off - libc_hidden_proto(memmove) */ /* * Formulate an Internet address from network + host. Used in @@ -173,7 +176,7 @@ struct in_addr inet_makeaddr(in_addr_t net, in_addr_t host) else addr = net | host; addr = htonl(addr); - return (*(struct in_addr *)&addr); + return *(struct in_addr *)&addr; } libc_hidden_def(inet_makeaddr) #endif @@ -189,11 +192,11 @@ in_addr_t inet_lnaof(struct in_addr in) in_addr_t i = ntohl(in.s_addr); if (IN_CLASSA(i)) - return ((i)&IN_CLASSA_HOST); + return (i & IN_CLASSA_HOST); else if (IN_CLASSB(i)) - return ((i)&IN_CLASSB_HOST); + return (i & IN_CLASSB_HOST); else - return ((i)&IN_CLASSC_HOST); + return (i & IN_CLASSC_HOST); } #endif @@ -210,11 +213,11 @@ inet_netof(struct in_addr in) in_addr_t i = ntohl(in.s_addr); if (IN_CLASSA(i)) - return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); + return ((i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); else if (IN_CLASSB(i)) - return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT); + return ((i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT); else - return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT); + return ((i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT); } libc_hidden_def(inet_netof) #endif diff --git a/libc/inet/ether_addr.c b/libc/inet/ether_addr.c index e896e707d..bcea9ba46 100644 --- a/libc/inet/ether_addr.c +++ b/libc/inet/ether_addr.c @@ -37,7 +37,7 @@ libc_hidden_proto(sprintf) #ifdef __UCLIBC_HAS_XLOCALE__ libc_hidden_proto(__ctype_b_loc) libc_hidden_proto(__ctype_tolower_loc) -#elif __UCLIBC_HAS_CTYPE_TABLES__ +#elif defined __UCLIBC_HAS_CTYPE_TABLES__ libc_hidden_proto(__ctype_b) libc_hidden_proto(__ctype_tolower) #endif diff --git a/libc/inet/gai_strerror.c b/libc/inet/gai_strerror.c index c1c7b767f..61688bad9 100644 --- a/libc/inet/gai_strerror.c +++ b/libc/inet/gai_strerror.c @@ -24,7 +24,7 @@ #define N_(x) x #define _(x) x -static struct +static const struct { int code; const char *msg; diff --git a/libc/inet/getaddrinfo.c b/libc/inet/getaddrinfo.c index d0d172643..d81888f9a 100644 --- a/libc/inet/getaddrinfo.c +++ b/libc/inet/getaddrinfo.c @@ -1,6 +1,8 @@ /* * Copyright 1996 by Craig Metz * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org> + * Portions from the GNU C library, + * Copyright (C) 2003, 2006 Free Software Foundation, Inc. * * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. */ @@ -55,8 +57,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include <errno.h> #include <netdb.h> #include <stdio.h> -#include <resolv.h> #include <stdlib.h> +#include <resolv.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> @@ -66,14 +68,15 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include <sys/un.h> #include <sys/utsname.h> #include <net/if.h> +#include "ifaddrs.h" -libc_hidden_proto(memcpy) -libc_hidden_proto(memset) +/* Experimentally off - libc_hidden_proto(memcpy) */ +/* Experimentally off - libc_hidden_proto(memset) */ /* libc_hidden_proto(strcmp) */ /* libc_hidden_proto(stpcpy) */ -libc_hidden_proto(strchr) -libc_hidden_proto(strcpy) -libc_hidden_proto(strlen) +/* Experimentally off - libc_hidden_proto(strchr) */ +/* Experimentally off - libc_hidden_proto(strcpy) */ +/* Experimentally off - libc_hidden_proto(strlen) */ libc_hidden_proto(socket) libc_hidden_proto(close) libc_hidden_proto(getservbyname_r) @@ -156,19 +159,85 @@ static const struct addrinfo default_hints = { 0, PF_UNSPEC, 0, 0, 0, NULL, NULL, NULL }; #endif +#define SEEN_IPV4 1 +#define SEEN_IPV6 2 + +static unsigned __check_pf (void) +{ + unsigned seen = 0; +#if defined __UCLIBC_SUPPORT_AI_ADDRCONFIG__ + { + /* Get the interface list via getifaddrs. */ + struct ifaddrs *ifa = NULL; + struct ifaddrs *runp; + if (getifaddrs (&ifa) != 0) + { + /* We cannot determine what interfaces are available. Be + optimistic. */ +#if defined __UCLIBC_HAS_IPV4__ + seen |= SEEN_IPV4; +#endif /* __UCLIBC_HAS_IPV4__ */ +#if defined __UCLIBC_HAS_IPV6__ + seen |= SEEN_IPV6; +#endif /* __UCLIBC_HAS_IPV6__ */ + return seen; + } + + for (runp = ifa; runp != NULL; runp = runp->ifa_next) +#if defined __UCLIBC_HAS_IPV4__ + if (runp->ifa_addr->sa_family == PF_INET) + seen |= SEEN_IPV4; +#endif /* __UCLIBC_HAS_IPV4__ */ +#if defined __UCLIBC_HAS_IPV4__ && defined __UCLIBC_HAS_IPV6__ + else /* can't be both at once */ +#endif /* __UCLIBC_HAS_IPV4__ && defined __UCLIBC_HAS_IPV6__ */ +#if defined __UCLIBC_HAS_IPV6__ + if (runp->ifa_addr->sa_family == PF_INET6) + seen |= SEEN_IPV6; +#endif /* __UCLIBC_HAS_IPV6__ */ + + (void) freeifaddrs (ifa); + } +#else + /* AI_ADDRCONFIG is disabled, assume both ipv4 and ipv6 available. */ +#if defined __UCLIBC_HAS_IPV4__ + seen |= SEEN_IPV4; +#endif /* __UCLIBC_HAS_IPV4__ */ +#if defined __UCLIBC_HAS_IPV6__ + seen |= SEEN_IPV6; +#endif /* __UCLIBC_HAS_IPV6__ */ + +#endif /* __UCLIBC_SUPPORT_AI_ADDRCONFIG__ */ + return seen; +} static int addrconfig (sa_family_t af) { int s; int ret; int saved_errno = errno; - s = socket(af, SOCK_DGRAM, 0); - if (s < 0) - ret = (errno == EMFILE) ? 1 : 0; + unsigned seen; + + seen = __check_pf(); +#if defined __UCLIBC_HAS_IPV4__ + if (af == AF_INET) + ret = seen & SEEN_IPV4; else +#endif +#if defined __UCLIBC_HAS_IPV6__ + if (af == AF_INET6) + ret = seen & SEEN_IPV6; + else +#endif { - close(s); - ret = 1; + s = socket(af, SOCK_DGRAM, 0); + ret = 1; /* Assume PF_UNIX. */ + if (s < 0) { + if (errno != EMFILE) + ret = 0; + } + else + close(s); } __set_errno (saved_errno); return ret; @@ -373,6 +442,7 @@ gaih_inet (const char *name, const struct gaih_service *service, int rc; int v4mapped = (req->ai_family == PF_UNSPEC || req->ai_family == PF_INET6) && (req->ai_flags & AI_V4MAPPED); + unsigned seen = __check_pf(); if (req->ai_protocol || req->ai_socktype) { @@ -501,7 +571,7 @@ gaih_inet (const char *name, const struct gaih_service *service, return -EAI_FAMILY; } -#if __UCLIBC_HAS_IPV6__ +#if defined __UCLIBC_HAS_IPV6__ if (at->family == AF_UNSPEC) { char *namebuf = strdupa (name); @@ -558,16 +628,18 @@ gaih_inet (const char *name, const struct gaih_service *service, * IPv6 addresses. */ -#if __UCLIBC_HAS_IPV6__ +#if defined __UCLIBC_HAS_IPV6__ if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET6) - gethosts (AF_INET6, struct in6_addr); + if (!(req->ai_flags & AI_ADDRCONFIG) || (seen & SEEN_IPV6)) + gethosts (AF_INET6, struct in6_addr); #endif no_inet6_data = no_data; if (req->ai_family == AF_INET || (!v4mapped && req->ai_family == AF_UNSPEC) || (v4mapped && (no_inet6_data != 0 || (req->ai_flags & AI_ALL)))) - gethosts (AF_INET, struct in_addr); + if (!(req->ai_flags & AI_ADDRCONFIG) || (seen & SEEN_IPV4)) + gethosts (AF_INET, struct in_addr); if (no_data != 0 && no_inet6_data != 0) { @@ -598,7 +670,7 @@ gaih_inet (const char *name, const struct gaih_service *service, memset (at->next, '\0', sizeof (struct gaih_addrtuple)); } -#if __UCLIBC_HAS_IPV6__ +#if defined __UCLIBC_HAS_IPV6__ if (req->ai_family == 0 || req->ai_family == AF_INET6) { at->family = AF_INET6; @@ -680,21 +752,32 @@ gaih_inet (const char *name, const struct gaih_service *service, else namelen = 0; -#if __UCLIBC_HAS_IPV6__ +#if defined __UCLIBC_HAS_IPV6__ if (at2->family == AF_INET6 || v4mapped) { family = AF_INET6; socklen = sizeof (struct sockaddr_in6); } +#endif +#if defined __UCLIBC_HAS_IPV4__ && defined __UCLIBC_HAS_IPV6__ else #endif +#if defined __UCLIBC_HAS_IPV4__ { family = AF_INET; socklen = sizeof (struct sockaddr_in); } - +#endif for (st2 = st; st2 != NULL; st2 = st2->next) { + if (req->ai_flags & AI_ADDRCONFIG) { + if (family == AF_INET && !(seen & SEEN_IPV4)) + break; +#if defined __UCLIBC_HAS_IPV6__ + else if (family == AF_INET6 && !(seen & SEEN_IPV6)) + break; +#endif + } *pai = malloc (sizeof (struct addrinfo) + socklen + namelen); if (*pai == NULL) return -EAI_MEMORY; @@ -710,7 +793,7 @@ gaih_inet (const char *name, const struct gaih_service *service, #endif /* SALEN */ (*pai)->ai_addr->sa_family = family; -#if __UCLIBC_HAS_IPV6__ +#if defined __UCLIBC_HAS_IPV6__ if (family == AF_INET6) { struct sockaddr_in6 *sin6p = @@ -733,8 +816,11 @@ gaih_inet (const char *name, const struct gaih_service *service, sin6p->sin6_port = st2->port; sin6p->sin6_scope_id = at2->scopeid; } +#endif +#if defined __UCLIBC_HAS_IPV4__ && defined __UCLIBC_HAS_IPV6__ else #endif +#if defined __UCLIBC_HAS_IPV4__ { struct sockaddr_in *sinp = (struct sockaddr_in *) (*pai)->ai_addr; @@ -744,7 +830,7 @@ gaih_inet (const char *name, const struct gaih_service *service, sinp->sin_port = st2->port; memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero)); } - +#endif if (c) { (*pai)->ai_canonname = ((void *) (*pai) + @@ -766,7 +852,7 @@ gaih_inet (const char *name, const struct gaih_service *service, static struct gaih gaih[] = { -#if __UCLIBC_HAS_IPV6__ +#if defined __UCLIBC_HAS_IPV6__ { PF_INET6, gaih_inet }, #endif { PF_INET, gaih_inet }, @@ -853,7 +939,10 @@ getaddrinfo (const char *name, const char *service, if (hints->ai_family == g->family || hints->ai_family == AF_UNSPEC) { if ((hints->ai_flags & AI_ADDRCONFIG) && !addrconfig(g->family)) + { + ++g; continue; + } j++; if (pg == NULL || pg->gaih != g->gaih) { diff --git a/libc/inet/getnetbyad.c b/libc/inet/getnetbyad.c index e353f245b..c916a18cf 100644 --- a/libc/inet/getnetbyad.c +++ b/libc/inet/getnetbyad.c @@ -18,12 +18,13 @@ #define __FORCE_GLIBC #include <features.h> #include <netdb.h> +#include <unistd.h> libc_hidden_proto(setnetent) libc_hidden_proto(getnetent) libc_hidden_proto(endnetent) -extern int _net_stayopen attribute_hidden; +extern smallint _net_stayopen attribute_hidden; struct netent *getnetbyaddr (uint32_t net, int type) { diff --git a/libc/inet/getnetbynm.c b/libc/inet/getnetbynm.c index 97d5cb85f..9f3655121 100644 --- a/libc/inet/getnetbynm.c +++ b/libc/inet/getnetbynm.c @@ -19,13 +19,14 @@ #include <features.h> #include <netdb.h> #include <string.h> +#include <unistd.h> -libc_hidden_proto(strcmp) +/* Experimentally off - libc_hidden_proto(strcmp) */ libc_hidden_proto(setnetent) libc_hidden_proto(getnetent) libc_hidden_proto(endnetent) -extern int _net_stayopen attribute_hidden; +extern smallint _net_stayopen attribute_hidden; struct netent * getnetbyname(const char *name) diff --git a/libc/inet/getnetent.c b/libc/inet/getnetent.c index d3fdb988a..863266d48 100644 --- a/libc/inet/getnetent.c +++ b/libc/inet/getnetent.c @@ -21,6 +21,7 @@ #include <stdlib.h> #include <netdb.h> #include <arpa/inet.h> +#include <unistd.h> libc_hidden_proto(fopen) libc_hidden_proto(fclose) @@ -29,12 +30,8 @@ libc_hidden_proto(rewind) libc_hidden_proto(fgets) libc_hidden_proto(abort) -#ifdef __UCLIBC_HAS_THREADS__ -# include <pthread.h> -static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; -#endif -#define LOCK __pthread_mutex_lock(&mylock) -#define UNLOCK __pthread_mutex_unlock(&mylock) +#include <bits/uClibc_mutex.h> +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER); @@ -45,18 +42,18 @@ static char *line = NULL; static struct netent net; static char *net_aliases[MAXALIASES]; -int _net_stayopen attribute_hidden; +smallint _net_stayopen attribute_hidden; libc_hidden_proto(setnetent) void setnetent(int f) { - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (netf == NULL) netf = fopen(NETDB, "r" ); else rewind(netf); - _net_stayopen |= f; - UNLOCK; + if (f) _net_stayopen = 1; + __UCLIBC_MUTEX_UNLOCK(mylock); return; } libc_hidden_def(setnetent) @@ -64,13 +61,13 @@ libc_hidden_def(setnetent) libc_hidden_proto(endnetent) void endnetent(void) { - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (netf) { fclose(netf); netf = NULL; } _net_stayopen = 0; - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); } libc_hidden_def(endnetent) @@ -92,11 +89,11 @@ struct netent *getnetent(void) { char *p; register char *cp, **q; + struct netent *rv = NULL; - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) { - UNLOCK; - return (NULL); + goto DONE; } again: @@ -108,8 +105,7 @@ again: p = fgets(line, BUFSIZ, netf); if (p == NULL) { - UNLOCK; - return (NULL); + goto DONE; } if (*p == '#') goto again; @@ -144,7 +140,9 @@ again: *cp++ = '\0'; } *q = NULL; - UNLOCK; - return (&net); + rv = &net; +DONE: + __UCLIBC_MUTEX_UNLOCK(mylock); + return rv; } libc_hidden_def(getnetent) diff --git a/libc/inet/getproto.c b/libc/inet/getproto.c index a54532ede..1e3e843f5 100644 --- a/libc/inet/getproto.c +++ b/libc/inet/getproto.c @@ -60,22 +60,19 @@ #include <stdlib.h> #include <string.h> #include <errno.h> +#include <unistd.h> libc_hidden_proto(fopen) -libc_hidden_proto(strcmp) -libc_hidden_proto(strpbrk) +/* Experimentally off - libc_hidden_proto(strcmp) */ +/* Experimentally off - libc_hidden_proto(strpbrk) */ libc_hidden_proto(atoi) libc_hidden_proto(rewind) libc_hidden_proto(fgets) libc_hidden_proto(fclose) libc_hidden_proto(abort) -#ifdef __UCLIBC_HAS_THREADS__ -# include <pthread.h> -static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; -#endif -#define LOCK __pthread_mutex_lock(&mylock) -#define UNLOCK __pthread_mutex_unlock(&mylock) +#include <bits/uClibc_mutex.h> +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP); @@ -85,7 +82,7 @@ static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; static FILE *protof = NULL; static struct protoent proto; static char *static_aliases = NULL; -static int proto_stayopen; +static smallint proto_stayopen; static void __initbuf(void) { @@ -99,26 +96,26 @@ static void __initbuf(void) libc_hidden_proto(setprotoent) void setprotoent(int f) { - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (protof == NULL) protof = fopen(_PATH_PROTOCOLS, "r" ); else rewind(protof); - proto_stayopen |= f; - UNLOCK; + if (f) proto_stayopen = 1; + __UCLIBC_MUTEX_UNLOCK(mylock); } libc_hidden_def(setprotoent) libc_hidden_proto(endprotoent) void endprotoent(void) { - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (protof) { fclose(protof); protof = NULL; } proto_stayopen = 0; - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); } libc_hidden_def(endprotoent) @@ -131,6 +128,7 @@ int getprotoent_r(struct protoent *result_buf, register char *cp, **q; char **proto_aliases; char *line; + int rv; *result = NULL; @@ -138,28 +136,27 @@ int getprotoent_r(struct protoent *result_buf, errno=ERANGE; return errno; } - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); proto_aliases=(char **)buf; buf+=sizeof(*proto_aliases)*MAXALIASES; buflen-=sizeof(*proto_aliases)*MAXALIASES; if (buflen < BUFSIZ+1) { - UNLOCK; - errno=ERANGE; - return errno; + errno=rv=ERANGE; + goto DONE; } line=buf; buf+=BUFSIZ+1; buflen-=BUFSIZ+1; if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) { - UNLOCK; - return errno; + rv=errno; + goto DONE; } again: if ((p = fgets(line, BUFSIZ, protof)) == NULL) { - UNLOCK; - return TRY_AGAIN; + rv=TRY_AGAIN; + goto DONE; } if (*p == '#') @@ -196,8 +193,10 @@ again: } *q = NULL; *result=result_buf; - UNLOCK; - return 0; + rv = 0; +DONE: + __UCLIBC_MUTEX_UNLOCK(mylock); + return rv; } libc_hidden_def(getprotoent_r) @@ -220,7 +219,7 @@ int getprotobyname_r(const char *name, register char **cp; int ret; - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); setprotoent(proto_stayopen); while (!(ret=getprotoent_r(result_buf, buf, buflen, result))) { if (strcmp(result_buf->p_name, name) == 0) @@ -232,7 +231,7 @@ int getprotobyname_r(const char *name, found: if (!proto_stayopen) endprotoent(); - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); return *result?0:ret; } libc_hidden_def(getprotobyname_r) @@ -256,14 +255,14 @@ int getprotobynumber_r (int proto_num, { int ret; - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); setprotoent(proto_stayopen); while (!(ret=getprotoent_r(result_buf, buf, buflen, result))) if (result_buf->p_proto == proto_num) break; if (!proto_stayopen) endprotoent(); - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); return *result?0:ret; } libc_hidden_def(getprotobynumber_r) diff --git a/libc/inet/getservice.c b/libc/inet/getservice.c index 7f4939bbd..bb1d6d2e4 100644 --- a/libc/inet/getservice.c +++ b/libc/inet/getservice.c @@ -62,9 +62,10 @@ #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h> +#include <unistd.h> -libc_hidden_proto(strcmp) -libc_hidden_proto(strpbrk) +/* Experimentally off - libc_hidden_proto(strcmp) */ +/* Experimentally off - libc_hidden_proto(strpbrk) */ libc_hidden_proto(fopen) libc_hidden_proto(fclose) libc_hidden_proto(atoi) @@ -72,12 +73,8 @@ libc_hidden_proto(rewind) libc_hidden_proto(fgets) libc_hidden_proto(abort) -#ifdef __UCLIBC_HAS_THREADS__ -# include <pthread.h> -static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; -#endif -#define LOCK __pthread_mutex_lock(&mylock) -#define UNLOCK __pthread_mutex_unlock(&mylock) +#include <bits/uClibc_mutex.h> +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP); @@ -88,7 +85,7 @@ static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; static FILE *servf = NULL; static struct servent serv; static char *servbuf = NULL; -static int serv_stayopen; +static smallint serv_stayopen; static void __initbuf(void) { @@ -102,26 +99,26 @@ static void __initbuf(void) libc_hidden_proto(setservent) void setservent(int f) { - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (servf == NULL) servf = fopen(_PATH_SERVICES, "r" ); else rewind(servf); - serv_stayopen |= f; - UNLOCK; + if (f) serv_stayopen = 1; + __UCLIBC_MUTEX_UNLOCK(mylock); } libc_hidden_def(setservent) libc_hidden_proto(endservent) void endservent(void) { - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (servf) { fclose(servf); servf = NULL; } serv_stayopen = 0; - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); } libc_hidden_def(endservent) @@ -134,6 +131,7 @@ int getservent_r(struct servent * result_buf, register char *cp, **q; char **serv_aliases; char *line; + int rv; *result=NULL; @@ -141,30 +139,27 @@ int getservent_r(struct servent * result_buf, errno=ERANGE; return errno; } - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); serv_aliases=(char **)buf; buf+=sizeof(*serv_aliases)*MAXALIASES; buflen-=sizeof(*serv_aliases)*MAXALIASES; if (buflen < BUFSIZ+1) { - UNLOCK; - errno=ERANGE; - return errno; + errno=rv=ERANGE; + goto DONE; } line=buf; buf+=BUFSIZ+1; buflen-=BUFSIZ+1; if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL) { - UNLOCK; - errno=EIO; - return errno; + errno=rv=EIO; + goto DONE; } again: if ((p = fgets(line, BUFSIZ, servf)) == NULL) { - UNLOCK; - errno=EIO; - return errno; + errno=rv=EIO; + goto DONE; } if (*p == '#') goto again; @@ -202,8 +197,10 @@ again: } *q = NULL; *result=result_buf; - UNLOCK; - return 0; + rv = 0; +DONE: + __UCLIBC_MUTEX_UNLOCK(mylock); + return rv; } libc_hidden_def(getservent_r) @@ -224,7 +221,7 @@ int getservbyname_r(const char *name, const char *proto, register char **cp; int ret; - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); setservent(serv_stayopen); while (!(ret=getservent_r(result_buf, buf, buflen, result))) { if (strcmp(name, result_buf->s_name) == 0) @@ -239,7 +236,7 @@ gotname: } if (!serv_stayopen) endservent(); - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); return *result?0:ret; } libc_hidden_def(getservbyname_r) @@ -261,7 +258,7 @@ int getservbyport_r(int port, const char *proto, { int ret; - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); setservent(serv_stayopen); while (!(ret=getservent_r(result_buf, buf, buflen, result))) { if (result_buf->s_port != port) @@ -271,7 +268,7 @@ int getservbyport_r(int port, const char *proto, } if (!serv_stayopen) endservent(); - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); return *result?0:ret; } libc_hidden_def(getservbyport_r) diff --git a/libc/inet/hostid.c b/libc/inet/hostid.c index bd9f5b52b..33668a2fe 100644 --- a/libc/inet/hostid.c +++ b/libc/inet/hostid.c @@ -18,7 +18,7 @@ #include <not-cancel.h> #endif -libc_hidden_proto(memcpy) +/* Experimentally off - libc_hidden_proto(memcpy) */ libc_hidden_proto(open) libc_hidden_proto(close) libc_hidden_proto(read) @@ -57,7 +57,7 @@ long int gethostid(void) char host[MAXHOSTNAMELEN + 1]; int fd, id; - /* If hostid was already set the we can return that value. + /* If hostid was already set then we can return that value. * It is not an error if we cannot read this file. It is not even an * error if we cannot read all the bytes, we just carry on trying... */ @@ -92,8 +92,19 @@ long int gethostid(void) if (gethostname(host,MAXHOSTNAMELEN)>=0 && *host) { struct hostent *hp; struct in_addr in; + struct hostent ghbn_h; + char ghbn_buf[sizeof(struct in_addr) + + sizeof(struct in_addr *)*2 + + sizeof(char *)*((2 + 5/*MAX_ALIASES*/ + + 1)/*ALIAS_DIM*/) + + 256/*namebuffer*/ + 32/* margin */]; + int ghbn_errno; - if ((hp = gethostbyname(host)) == (struct hostent *)NULL) + /* replace gethostbyname() with gethostbyname_r() - ron@zing.net */ + /*if ((hp = gethostbyname(host)) == (struct hostent *)NULL)*/ + gethostbyname_r(host, &ghbn_h, ghbn_buf, sizeof(ghbn_buf), &hp, &ghbn_errno); + + if (hp == (struct hostent *)NULL) /* This is not a error if we get here, as all it means is that * this host is not on a network and/or they have not diff --git a/libc/inet/if_index.c b/libc/inet/if_index.c index b89051c79..05b7b2669 100644 --- a/libc/inet/if_index.c +++ b/libc/inet/if_index.c @@ -38,12 +38,12 @@ #include "netlinkaccess.h" -libc_hidden_proto(strncpy) -libc_hidden_proto(strdup) +/* Experimentally off - libc_hidden_proto(strncpy) */ +/* Experimentally off - libc_hidden_proto(strdup) */ libc_hidden_proto(ioctl) libc_hidden_proto(close) #if __ASSUME_NETLINK_SUPPORT -libc_hidden_proto(strndup) +/* Experimentally off - libc_hidden_proto(strndup) */ #endif extern int __opensock(void) attribute_hidden; @@ -314,14 +314,6 @@ if_nameindex (void) #endif libc_hidden_def(if_nameindex) -#if 0 -struct if_nameindex * -if_nameindex (void) -{ - return (if_nameindex_netlink () != NULL ? : if_nameindex_ioctl ()); -} -#endif - char * if_indextoname (unsigned int ifindex, char *ifname) { diff --git a/libc/inet/ifaddrs.c b/libc/inet/ifaddrs.c index b9f3cbd0e..a96b19a54 100644 --- a/libc/inet/ifaddrs.c +++ b/libc/inet/ifaddrs.c @@ -22,7 +22,7 @@ #include <alloca.h> #include <assert.h> #include <errno.h> -/*#include <ifaddrs.h>*/ +#include "ifaddrs.h" #include <net/if.h> #include <netinet/in.h> #include <netpacket/packet.h> @@ -41,12 +41,12 @@ libc_hidden_proto(socket) libc_hidden_proto(close) -libc_hidden_proto(time) +/* Experimentally off - libc_hidden_proto(time) */ libc_hidden_proto(sendto) libc_hidden_proto(recvmsg) libc_hidden_proto(bind) -libc_hidden_proto(memset) -libc_hidden_proto(mempcpy) +/* Experimentally off - libc_hidden_proto(memset) */ +/* Experimentally off - libc_hidden_proto(mempcpy) */ libc_hidden_proto(getsockname) libc_hidden_proto(fclose) libc_hidden_proto(abort) @@ -57,7 +57,7 @@ libc_hidden_proto(abort) #if __ASSUME_NETLINK_SUPPORT -#if 0 /* unused code */ +#ifdef __UCLIBC_SUPPORT_AI_ADDRCONFIG__ /* struct to hold the data for one ifaddrs entry, so we can allocate everything at once. */ struct ifaddrs_storage @@ -74,7 +74,7 @@ struct ifaddrs_storage } addr, netmask, broadaddr; char name[IF_NAMESIZE + 1]; }; -#endif /* unused code */ +#endif /* __UCLIBC_SUPPORT_AI_ADDRCONFIG__ */ void @@ -324,7 +324,7 @@ __netlink_open (struct netlink_handle *h) } -#if 0 /* unused code */ +#ifdef __UCLIBC_SUPPORT_AI_ADDRCONFIG__ /* We know the number of RTM_NEWLINK entries, so we reserve the first # of entries for this type. All RTM_NEWADDR entries have an index pointer to the RTM_NEWLINK entry. To find the entry, create @@ -562,7 +562,7 @@ getifaddrs (struct ifaddrs **ifap) if ((rta_payload + 1) <= sizeof (ifas[ifa_index].name)) { ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name; - *(char *) __mempcpy (ifas[ifa_index].name, rta_data, + *(char *) mempcpy (ifas[ifa_index].name, rta_data, rta_payload) = '\0'; } break; @@ -761,7 +761,7 @@ getifaddrs (struct ifaddrs **ifap) if (rta_payload + 1 <= sizeof (ifas[ifa_index].name)) { ifas[ifa_index].ifa.ifa_name = ifas[ifa_index].name; - *(char *) __mempcpy (ifas[ifa_index].name, rta_data, + *(char *) mempcpy (ifas[ifa_index].name, rta_data, rta_payload) = '\0'; } else @@ -872,6 +872,6 @@ freeifaddrs (struct ifaddrs *ifa) } #endif -#endif /* unused code */ +#endif /* __UCLIBC_SUPPORT_AI_ADDRCONFIG__ */ #endif /* __ASSUME_NETLINK_SUPPORT */ diff --git a/libc/inet/inet_net.c b/libc/inet/inet_net.c index 71ed8dc88..e46c9a561 100644 --- a/libc/inet/inet_net.c +++ b/libc/inet/inet_net.c @@ -37,9 +37,10 @@ #include <ctype.h> #include <netinet/in.h> #include <arpa/inet.h> + #ifdef __UCLIBC_HAS_XLOCALE__ libc_hidden_proto(__ctype_b_loc) -#elif __UCLIBC_HAS_CTYPE_TABLES__ +#elif defined __UCLIBC_HAS_CTYPE_TABLES__ libc_hidden_proto(__ctype_b) #endif @@ -50,55 +51,56 @@ libc_hidden_proto(__ctype_b) */ libc_hidden_proto(inet_network) in_addr_t -inet_network(cp) - register const char *cp; +inet_network(const char *cp) { - register u_int32_t val, base, n, i; - register char c; - u_int32_t parts[4], *pp = parts; - int digit; + u_char c; + int got_data; + u_int base, dots; + in_addr_t res, val; -again: - val = 0; base = 10; digit = 0; - if (*cp == '0') - digit = 1, base = 8, cp++; - if (*cp == 'x' || *cp == 'X') - base = 16, cp++; - while ((c = *cp) != 0) { - if (isdigit(c)) { - if (base == 8 && (c == '8' || c == '9')) - return (INADDR_NONE); - val = (val * base) + (c - '0'); - cp++; - digit = 1; - continue; - } - if (base == 16 && isxdigit(c)) { - val = (val << 4) + (tolower (c) + 10 - 'a'); + res = 0; + dots = 0; + again: + val = 0; + got_data = 0; + if (*cp == '0') { + cp++; + if (*cp == 'x' || *cp == 'X') { cp++; - digit = 1; - continue; + base = 16; + } else { + base = 8; + got_data = 1; } - break; + } else + base = 10; + while ((c = *cp) != '\0') { + if (isdigit(c)) { + if (base == 8 && c > '7') + return (INADDR_NONE); + val = val * base + c - '0'; + } else if (base == 16 && isxdigit(c)) + val = (val << 4) + 10 - (islower(c) ? 'a' : 'A'); + else + break; + if (val > 0xff) + return (INADDR_NONE); + cp++; + got_data = 1; } - if (!digit) - return (INADDR_NONE); - if (pp >= parts + 4 || val > 0xff) + if (!got_data) return (INADDR_NONE); - if (*cp == '.') { - *pp++ = val, cp++; + if (dots != 0) + res <<= 8; + res |= val; + if (c == '.') { + if (++dots == 4) + return (INADDR_NONE); + cp++; goto again; } - if (*cp && !isspace(*cp)) + if (c != '\0') return (INADDR_NONE); - if (pp >= parts + 4 || val > 0xff) - return (INADDR_NONE); - *pp++ = val; - n = pp - parts; - for (val = 0, i = 0; i < n; i++) { - val <<= 8; - val |= parts[i] & 0xff; - } - return (val); + return (res); } libc_hidden_def(inet_network) diff --git a/libc/inet/netlinkaccess.h b/libc/inet/netlinkaccess.h index a47d8901c..acadcb544 100644 --- a/libc/inet/netlinkaccess.h +++ b/libc/inet/netlinkaccess.h @@ -21,6 +21,7 @@ #include <features.h> #include <stdint.h> +#include <unistd.h> #include <sys/types.h> #define _LINUX_TYPES_H @@ -33,11 +34,13 @@ typedef int32_t __s32; #include <linux/netlink.h> /* Should prob be a configure option or something */ +#ifndef __ASSUME_NETLINK_SUPPORT #ifdef __UCLIBC_USE_NETLINK__ # define __ASSUME_NETLINK_SUPPORT 1 #else # define __ASSUME_NETLINK_SUPPORT 0 #endif +#endif struct netlink_res @@ -59,13 +62,13 @@ struct netlink_handle }; -#if 0 /* unused code */ +#ifdef __UCLIBC_SUPPORT_AI_ADDRCONFIG__ #if __ASSUME_NETLINK_SUPPORT == 0 -extern int __no_netlink_support attribute_hidden; +extern smallint __no_netlink_support attribute_hidden; #else # define __no_netlink_support 0 #endif -#endif /* unused code */ +#endif /* __UCLIBC_SUPPORT_AI_ADDRCONFIG__ */ extern int __netlink_open (struct netlink_handle *h) attribute_hidden; diff --git a/libc/inet/ntop.c b/libc/inet/ntop.c index 88ff6a150..c1f3fc83c 100644 --- a/libc/inet/ntop.c +++ b/libc/inet/ntop.c @@ -30,11 +30,11 @@ #include <string.h> #include <ctype.h> -libc_hidden_proto(memcpy) -libc_hidden_proto(memset) -libc_hidden_proto(strchr) -libc_hidden_proto(strcpy) -libc_hidden_proto(strlen) +/* Experimentally off - libc_hidden_proto(memcpy) */ +/* Experimentally off - libc_hidden_proto(memset) */ +/* Experimentally off - libc_hidden_proto(strchr) */ +/* Experimentally off - libc_hidden_proto(strcpy) */ +/* Experimentally off - libc_hidden_proto(strlen) */ libc_hidden_proto(sprintf) libc_hidden_proto(tolower) @@ -126,6 +126,8 @@ inet_ntop6(const u_char *src, char *dst, size_t size) words[i / 2] = (src[i] << 8) | src[i + 1]; best.base = -1; cur.base = -1; + best.len = best.len; /* shutting up compiler warning */ + cur.len = cur.len; /* shutting up compiler warning */ for (i = 0; i < 8; i++) { if (words[i] == 0) { if (cur.base == -1) diff --git a/libc/inet/resolv.c b/libc/inet/resolv.c index 6d6d18a96..6ddbd5c31 100644 --- a/libc/inet/resolv.c +++ b/libc/inet/resolv.c @@ -7,7 +7,7 @@ * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. -*/ + */ /* * Portions Copyright (c) 1985, 1993 @@ -139,6 +139,7 @@ #include <stdio.h> #include <signal.h> #include <errno.h> +#include <sys/poll.h> #include <sys/socket.h> #include <sys/types.h> #include <sys/time.h> @@ -149,36 +150,48 @@ #include <resolv.h> #include <netdb.h> #include <ctype.h> +#include <stdbool.h> #include <arpa/nameser.h> #include <sys/utsname.h> #include <sys/un.h> +#include <bits/uClibc_mutex.h> + +/* poll() is not supported in kernel <= 2.0, therefore if __NR_poll is + * not available, we assume an old Linux kernel is in use and we will + * use select() instead. */ +#include <sys/syscall.h> +#ifndef __NR_poll +# define USE_SELECT +#endif -libc_hidden_proto(memcpy) -libc_hidden_proto(memset) -libc_hidden_proto(memmove) -libc_hidden_proto(strchr) -libc_hidden_proto(strcmp) -libc_hidden_proto(strcpy) -libc_hidden_proto(strdup) -libc_hidden_proto(strlen) -libc_hidden_proto(strncat) -libc_hidden_proto(strncpy) +__UCLIBC_MUTEX_EXTERN(__resolv_lock); + +/* Experimentally off - libc_hidden_proto(memcpy) */ +/* Experimentally off - libc_hidden_proto(memset) */ +/* Experimentally off - libc_hidden_proto(memmove) */ +/* Experimentally off - libc_hidden_proto(strchr) */ +/* Experimentally off - libc_hidden_proto(strcmp) */ +/* Experimentally off - libc_hidden_proto(strcpy) */ +/* Experimentally off - libc_hidden_proto(strdup) */ +/* Experimentally off - libc_hidden_proto(strlen) */ +/* Experimentally off - libc_hidden_proto(strncat) */ +/* Experimentally off - libc_hidden_proto(strncpy) */ /* libc_hidden_proto(strnlen) */ -libc_hidden_proto(strstr) -libc_hidden_proto(strcasecmp) +/* Experimentally off - libc_hidden_proto(strstr) */ +/* Experimentally off - libc_hidden_proto(strcasecmp) */ libc_hidden_proto(socket) libc_hidden_proto(close) libc_hidden_proto(fopen) libc_hidden_proto(fclose) libc_hidden_proto(random) libc_hidden_proto(getservbyport) -libc_hidden_proto(getdomainname) libc_hidden_proto(uname) libc_hidden_proto(inet_addr) libc_hidden_proto(inet_aton) libc_hidden_proto(inet_pton) libc_hidden_proto(inet_ntop) libc_hidden_proto(connect) +libc_hidden_proto(poll) libc_hidden_proto(select) libc_hidden_proto(recv) libc_hidden_proto(send) @@ -202,9 +215,12 @@ libc_hidden_proto(fprintf) libc_hidden_proto(__h_errno_location) #ifdef __UCLIBC_HAS_XLOCALE__ libc_hidden_proto(__ctype_b_loc) -#elif __UCLIBC_HAS_CTYPE_TABLES__ +#elif defined __UCLIBC_HAS_CTYPE_TABLES__ libc_hidden_proto(__ctype_b) #endif +int __libc_getdomainname(char *name, size_t len); +libc_hidden_proto(__libc_getdomainname) + #define MAX_RECURSE 5 #define REPLY_TIMEOUT 10 @@ -215,7 +231,7 @@ libc_hidden_proto(__ctype_b) #define MAX_ALIASES 5 /* 1:ip + 1:full + MAX_ALIASES:aliases + 1:NULL */ -#define ALIAS_DIM (2 + MAX_ALIASES + 1) +#define ALIAS_DIM (2 + MAX_ALIASES + 1) #undef DEBUG /* #define DEBUG */ @@ -226,6 +242,14 @@ libc_hidden_proto(__ctype_b) #define DPRINTF(X,args...) #endif /* DEBUG */ +/* Make sure the incoming char * buffer is aligned enough to handle our random + * structures. This define is the same as we use for malloc alignment (which + * has same requirements). The offset is the number of bytes we need to adjust + * in order to attain desired alignment. + */ +#define ALIGN_ATTR __alignof__(double __attribute_aligned__ (sizeof(size_t))) +#define ALIGN_BUFFER_OFFSET(buf) ((ALIGN_ATTR - ((size_t)buf % ALIGN_ATTR)) % ALIGN_ATTR) + /* Global stuff (stuff needing to be locked to be thread safe)... */ extern int __nameservers attribute_hidden; @@ -234,14 +258,6 @@ extern int __searchdomains attribute_hidden; extern char * __searchdomain[MAX_SEARCH] attribute_hidden; -#ifdef __UCLIBC_HAS_THREADS__ -# include <pthread.h> -extern pthread_mutex_t __resolv_lock; -#endif -#define BIGLOCK __pthread_mutex_lock(&__resolv_lock) -#define BIGUNLOCK __pthread_mutex_unlock(&__resolv_lock) - - /* Structs */ struct resolv_header { @@ -265,7 +281,7 @@ struct resolv_answer { int aclass; int ttl; int rdlength; - unsigned char * rdata; + const unsigned char * rdata; int rdoffset; char* buf; size_t buflen; @@ -273,9 +289,9 @@ struct resolv_answer { }; enum etc_hosts_action { - GET_HOSTS_BYNAME = 0, - GETHOSTENT, - GET_HOSTS_BYADDR, + GET_HOSTS_BYNAME = 0, + GETHOSTENT, + GET_HOSTS_BYADDR, }; /* function prototypes */ @@ -289,7 +305,7 @@ extern int __get_hosts_byaddr_r(const char * addr, int len, int type, char * buf, size_t buflen, struct hostent ** result, int * h_errnop) attribute_hidden; -extern void __open_etc_hosts(FILE **fp) attribute_hidden; +extern FILE * __open_etc_hosts(void) attribute_hidden; extern int __read_etc_hosts_r(FILE *fp, const char * name, int type, enum etc_hosts_action action, struct hostent * result_buf, @@ -300,21 +316,21 @@ extern int __dns_lookup(const char * name, int type, int nscount, char ** nsip, unsigned char ** outpacket, struct resolv_answer * a) attribute_hidden; extern int __encode_dotted(const char * dotted, unsigned char * dest, int maxlen) attribute_hidden; -extern int __decode_dotted(const unsigned char * message, int offset, +extern int __decode_dotted(const unsigned char * const message, int offset, char * dest, int maxlen) attribute_hidden; -extern int __length_dotted(const unsigned char * message, int offset) attribute_hidden; +extern int __length_dotted(const unsigned char * const message, int offset) attribute_hidden; extern int __encode_header(struct resolv_header * h, unsigned char * dest, int maxlen) attribute_hidden; extern int __decode_header(unsigned char * data, struct resolv_header * h) attribute_hidden; -extern int __encode_question(struct resolv_question * q, +extern int __encode_question(const struct resolv_question * const q, unsigned char * dest, int maxlen) attribute_hidden; -extern int __decode_question(unsigned char * message, int offset, +extern int __decode_question(const unsigned char * const message, int offset, struct resolv_question * q) attribute_hidden; extern int __encode_answer(struct resolv_answer * a, unsigned char * dest, int maxlen) attribute_hidden; -extern int __decode_answer(unsigned char * message, int offset, +extern int __decode_answer(const unsigned char * message, int offset, struct resolv_answer * a) attribute_hidden; -extern int __length_question(unsigned char * message, int offset) attribute_hidden; -extern int __open_nameservers(void) attribute_hidden; +extern int __length_question(const unsigned char * const message, int offset) attribute_hidden; +extern void __open_nameservers(void) attribute_hidden; extern void __close_nameservers(void) attribute_hidden; extern int __dn_expand(const u_char *, const u_char *, const u_char *, char *, int); @@ -373,12 +389,16 @@ int attribute_hidden __decode_header(unsigned char *data, struct resolv_header * int attribute_hidden __encode_dotted(const char *dotted, unsigned char *dest, int maxlen) { - int used = 0; + unsigned used = 0; while (dotted && *dotted) { char *c = strchr(dotted, '.'); int l = c ? c - dotted : strlen(dotted); + /* two consecutive dots are not valid */ + if (l == 0) + return -1; + if (l >= (maxlen - used - 1)) return -1; @@ -405,20 +425,20 @@ int attribute_hidden __encode_dotted(const char *dotted, unsigned char *dest, in /* Decode a dotted string from nameserver transport-level encoding. This routine understands compressed data. */ -int attribute_hidden __decode_dotted(const unsigned char *data, int offset, +int attribute_hidden __decode_dotted(const unsigned char * const data, int offset, char *dest, int maxlen) { int l; - int measure = 1; - int total = 0; - int used = 0; + bool measure = 1; + unsigned total = 0; + unsigned used = 0; if (!data) return -1; - while ((l=data[offset++])) { + while ((l = data[offset++])) { if (measure) - total++; + total++; if ((l & 0xc0) == (0xc0)) { if (measure) total++; @@ -444,9 +464,8 @@ int attribute_hidden __decode_dotted(const unsigned char *data, int offset, } /* The null byte must be counted too */ - if (measure) { - total++; - } + if (measure) + total++; DPRINTF("Total decode len = %d\n", total); @@ -455,7 +474,7 @@ int attribute_hidden __decode_dotted(const unsigned char *data, int offset, #endif #ifdef L_lengthd -int attribute_hidden __length_dotted(const unsigned char *data, int offset) +int attribute_hidden __length_dotted(const unsigned char * const data, int offset) { int orig_offset = offset; int l; @@ -478,7 +497,7 @@ int attribute_hidden __length_dotted(const unsigned char *data, int offset) #endif #ifdef L_encodeq -int attribute_hidden __encode_question(struct resolv_question *q, +int attribute_hidden __encode_question(const struct resolv_question * const q, unsigned char *dest, int maxlen) { int i; @@ -503,7 +522,7 @@ int attribute_hidden __encode_question(struct resolv_question *q, #endif #ifdef L_decodeq -int attribute_hidden __decode_question(unsigned char *message, int offset, +int attribute_hidden __decode_question(const unsigned char * const message, int offset, struct resolv_question *q) { char temp[256]; @@ -515,7 +534,7 @@ int attribute_hidden __decode_question(unsigned char *message, int offset, offset += i; - q->dotted = strdup(temp); + q->dotted = strdup(temp); /* TODO: what if this fails? */ q->qtype = (message[offset + 0] << 8) | message[offset + 1]; q->qclass = (message[offset + 2] << 8) | message[offset + 3]; @@ -524,7 +543,7 @@ int attribute_hidden __decode_question(unsigned char *message, int offset, #endif #ifdef L_lengthq -int attribute_hidden __length_question(unsigned char *message, int offset) +int attribute_hidden __length_question(const unsigned char * const message, int offset) { int i; @@ -548,7 +567,7 @@ int attribute_hidden __encode_answer(struct resolv_answer *a, unsigned char *des dest += i; maxlen -= i; - if (maxlen < (RRFIXEDSZ+a->rdlength)) + if (maxlen < (RRFIXEDSZ + a->rdlength)) return -1; *dest++ = (a->atype & 0xff00) >> 8; @@ -568,7 +587,7 @@ int attribute_hidden __encode_answer(struct resolv_answer *a, unsigned char *des #endif #ifdef L_decodea -int attribute_hidden __decode_answer(unsigned char *message, int offset, +int attribute_hidden __decode_answer(const unsigned char *message, int offset, struct resolv_answer *a) { char temp[256]; @@ -580,7 +599,7 @@ int attribute_hidden __decode_answer(unsigned char *message, int offset, message += offset + i; - a->dotted = strdup(temp); + a->dotted = strdup(temp); /* TODO: what if this fails? */ a->atype = (message[0] << 8) | message[1]; message += 2; a->aclass = (message[0] << 8) | message[1]; @@ -607,14 +626,14 @@ int __encode_packet(struct resolv_header *h, struct resolv_answer **ar, unsigned char *dest, int maxlen) attribute_hidden; int __encode_packet(struct resolv_header *h, - struct resolv_question **q, - struct resolv_answer **an, - struct resolv_answer **ns, - struct resolv_answer **ar, - unsigned char *dest, int maxlen) + struct resolv_question **q, + struct resolv_answer **an, + struct resolv_answer **ns, + struct resolv_answer **ar, + unsigned char *dest, int maxlen) { int i, total = 0; - int j; + unsigned j; i = __encode_header(h, dest, maxlen); if (i < 0) @@ -673,7 +692,7 @@ int __decode_packet(unsigned char *data, struct resolv_header *h) #ifdef L_formquery int __form_query(int id, const char *name, int type, unsigned char *packet, int maxlen); int __form_query(int id, const char *name, int type, unsigned char *packet, - int maxlen) + int maxlen) { struct resolv_header h; struct resolv_question q; @@ -699,56 +718,60 @@ int __form_query(int id, const char *name, int type, unsigned char *packet, } #endif -#if defined(L_dnslookup) || defined(L_gethostent) -#ifdef __UCLIBC_HAS_THREADS__ -# include <pthread.h> -static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; -#endif -#define LOCK __pthread_mutex_lock(&mylock) -#define UNLOCK __pthread_mutex_unlock(&mylock) -#endif - #ifdef L_dnslookup +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER); /* Just for the record, having to lock __dns_lookup() just for these two globals * is pretty lame. I think these two variables can probably be de-global-ized, * which should eliminate the need for doing locking here... Needs a closer * look anyways. */ -static int ns=0, id=1; +static int static_ns = 0; +/* uint16: minimizing rw data size, even if code grows a tiny bit. + * rw data costs more. */ +static uint16_t static_id = 1; int attribute_hidden __dns_lookup(const char *name, int type, int nscount, char **nsip, unsigned char **outpacket, struct resolv_answer *a) { int i, j, len, fd, pos, rc; +#ifdef USE_SELECT struct timeval tv; fd_set fds; +#else + struct pollfd fds; +#endif struct resolv_header h; struct resolv_question q; struct resolv_answer ma; - int first_answer = 1; - int retries = 0; + bool first_answer = 1; + unsigned retries = 0; unsigned char * packet = malloc(PACKETSZ); char *dns, *lookup = malloc(MAXDNAME); - int variant = -1; - struct sockaddr_in sa; + int variant = -1; /* search domain to append, -1 - none */ int local_ns = -1, local_id = -1; + bool ends_with_dot; #ifdef __UCLIBC_HAS_IPV6__ - int v6; + bool v6; struct sockaddr_in6 sa6; #endif +#ifdef __UCLIBC_HAS_IPV4__ + struct sockaddr_in sa; +#endif fd = -1; - if (!packet || !lookup || !nscount) - goto fail; + if (!packet || !lookup || !nscount || !name[0]) + goto fail; DPRINTF("Looking up type %d answer for '%s'\n", type, name); + ends_with_dot = (name[strlen(name) - 1] == '.'); + /* Mess with globals while under lock */ - LOCK; - local_ns = ns % nscount; - local_id = id; - UNLOCK; + __UCLIBC_MUTEX_LOCK(mylock); + local_ns = static_ns % nscount; + local_id = static_id; + __UCLIBC_MUTEX_UNLOCK(mylock); while (retries < MAX_RETRIES) { if (fd != -1) @@ -761,8 +784,6 @@ int attribute_hidden __dns_lookup(const char *name, int type, int nscount, char ++local_id; local_id &= 0xffff; h.id = local_id; - dns = nsip[local_ns]; - h.qdcount = 1; h.rd = 1; @@ -772,15 +793,24 @@ int attribute_hidden __dns_lookup(const char *name, int type, int nscount, char if (i < 0) goto fail; - strncpy(lookup,name,MAXDNAME); + strncpy(lookup, name, MAXDNAME); + __UCLIBC_MUTEX_LOCK(__resolv_lock); + /* nsip is really __nameserver[] which is a global that + needs to hold __resolv_lock before access!! */ + dns = nsip[local_ns]; +/* TODO: all future accesses to 'dns' are guarded by __resolv_lock too. + * Why? We already fetched nsip[local_ns] here, + * future changes to nsip[] by other threads cannot affect us. + * We can use 'dns' without locking. If I'm wrong, + * please explain in comments why locking is needed. */ if (variant >= 0) { - BIGLOCK; - if (variant < __searchdomains) { - strncat(lookup,".", MAXDNAME); - strncat(lookup,__searchdomain[variant], MAXDNAME); - } - BIGUNLOCK; - } + if (variant < __searchdomains) { + strncat(lookup, ".", MAXDNAME); + strncat(lookup, __searchdomain[variant], MAXDNAME); + } + } + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); + DPRINTF("lookup name: %s\n", lookup); q.dotted = (char *)lookup; q.qtype = type; @@ -796,39 +826,49 @@ int attribute_hidden __dns_lookup(const char *name, int type, int nscount, char retries+1, NAMESERVER_PORT, dns); #ifdef __UCLIBC_HAS_IPV6__ + __UCLIBC_MUTEX_LOCK(__resolv_lock); + /* 'dns' is really __nameserver[] which is a global that + needs to hold __resolv_lock before access!! */ v6 = inet_pton(AF_INET6, dns, &sa6.sin6_addr) > 0; + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); fd = socket(v6 ? AF_INET6 : AF_INET, SOCK_DGRAM, IPPROTO_UDP); #else fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); #endif if (fd < 0) { - retries++; - continue; + retries++; + continue; } /* Connect to the UDP socket so that asyncronous errors are returned */ #ifdef __UCLIBC_HAS_IPV6__ if (v6) { - sa6.sin6_family = AF_INET6; - sa6.sin6_port = htons(NAMESERVER_PORT); - /* sa6.sin6_addr is already here */ - rc = connect(fd, (struct sockaddr *) &sa6, sizeof(sa6)); + sa6.sin6_family = AF_INET6; + sa6.sin6_port = htons(NAMESERVER_PORT); + /* sa6.sin6_addr is already here */ + rc = connect(fd, (struct sockaddr *) &sa6, sizeof(sa6)); } else { #endif - sa.sin_family = AF_INET; - sa.sin_port = htons(NAMESERVER_PORT); - sa.sin_addr.s_addr = inet_addr(dns); - rc = connect(fd, (struct sockaddr *) &sa, sizeof(sa)); +#ifdef __UCLIBC_HAS_IPV4__ + sa.sin_family = AF_INET; + sa.sin_port = htons(NAMESERVER_PORT); + __UCLIBC_MUTEX_LOCK(__resolv_lock); + /* 'dns' is really __nameserver[] which is a global that + needs to hold __resolv_lock before access!! */ + sa.sin_addr.s_addr = inet_addr(dns); + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); + rc = connect(fd, (struct sockaddr *) &sa, sizeof(sa)); +#endif #ifdef __UCLIBC_HAS_IPV6__ } #endif if (rc < 0) { - if (errno == ENETUNREACH) { - /* routing error, presume not transient */ - goto tryall; - } else + if (errno == ENETUNREACH) { + /* routing error, presume not transient */ + goto tryall; + } /* retry */ - retries++; + retries++; continue; } @@ -837,19 +877,31 @@ int attribute_hidden __dns_lookup(const char *name, int type, int nscount, char send(fd, packet, len, 0); +#ifdef USE_SELECT FD_ZERO(&fds); FD_SET(fd, &fds); tv.tv_sec = REPLY_TIMEOUT; tv.tv_usec = 0; if (select(fd + 1, &fds, NULL, NULL, &tv) <= 0) { - DPRINTF("Timeout\n"); + DPRINTF("Timeout\n"); /* timed out, so retry send and receive, * to next nameserver on queue */ goto tryall; } +#else + fds.fd = fd; + fds.events = POLLIN; + if (poll(&fds, 1, REPLY_TIMEOUT * 1000) <= 0) { + DPRINTF("Timeout\n"); - len = recv(fd, packet, 512, 0); + /* timed out, so retry send and receive, + * to next nameserver on queue */ + goto tryall; + } +#endif + + len = recv(fd, packet, PACKETSZ, 0); if (len < HFIXEDSZ) { /* too short ! */ goto again; @@ -864,7 +916,6 @@ int attribute_hidden __dns_lookup(const char *name, int type, int nscount, char goto again; } - DPRINTF("Got response %s\n", "(i think)!"); DPRINTF("qrcount=%d,ancount=%d,nscount=%d,arcount=%d\n", h.qdcount, h.ancount, h.nscount, h.arcount); @@ -889,55 +940,43 @@ int attribute_hidden __dns_lookup(const char *name, int type, int nscount, char DPRINTF("Decoding answer at pos %d\n", pos); first_answer = 1; - for (j=0;j<h.ancount;j++,pos += i) - { - i = __decode_answer(packet, pos, &ma); + for (j = 0; j < h.ancount; j++, pos += i) { + i = __decode_answer(packet, pos, &ma); - if (i<0) { - DPRINTF("failed decode %d\n", i); - goto again; - } - - if ( first_answer ) - { - ma.buf = a->buf; - ma.buflen = a->buflen; - ma.add_count = a->add_count; - memcpy(a, &ma, sizeof(ma)); - if (a->atype != T_SIG && (0 == a->buf || (type != T_A && type != T_AAAA))) - { - break; - } - if (a->atype != type) - { - free(a->dotted); - continue; - } - a->add_count = h.ancount - j - 1; - if ((a->rdlength + sizeof(struct in_addr*)) * a->add_count > a->buflen) - { - break; - } - a->add_count = 0; - first_answer = 0; - } - else - { - free(ma.dotted); - if (ma.atype != type) - { - continue; + if (i < 0) { + DPRINTF("failed decode %d\n", i); + goto again; } - if (a->rdlength != ma.rdlength) - { - free(a->dotted); - DPRINTF("Answer address len(%u) differs from original(%u)\n", - ma.rdlength, a->rdlength); - goto again; + + if (first_answer) { + ma.buf = a->buf; + ma.buflen = a->buflen; + ma.add_count = a->add_count; + memcpy(a, &ma, sizeof(ma)); + if (a->atype != T_SIG && (0 == a->buf || (type != T_A && type != T_AAAA))) + break; + if (a->atype != type) { + free(a->dotted); + continue; + } + a->add_count = h.ancount - j - 1; + if ((a->rdlength + sizeof(struct in_addr*)) * a->add_count > a->buflen) + break; + a->add_count = 0; + first_answer = 0; + } else { + free(ma.dotted); + if (ma.atype != type) + continue; + if (a->rdlength != ma.rdlength) { + free(a->dotted); + DPRINTF("Answer address len(%u) differs from original(%u)\n", + ma.rdlength, a->rdlength); + goto again; + } + memcpy(a->buf + (a->add_count * ma.rdlength), ma.rdata, ma.rdlength); + ++a->add_count; } - memcpy(a->buf + (a->add_count * ma.rdlength), ma.rdata, ma.rdlength); - ++a->add_count; - } } DPRINTF("Answer name = |%s|\n", a->dotted); @@ -952,61 +991,57 @@ int attribute_hidden __dns_lookup(const char *name, int type, int nscount, char free(lookup); /* Mess with globals while under lock */ - LOCK; - ns = local_ns; - id = local_id; - UNLOCK; + __UCLIBC_MUTEX_LOCK(mylock); + static_ns = local_ns; + static_id = local_id; + __UCLIBC_MUTEX_UNLOCK(mylock); - return (len); /* success! */ + return len; /* success! */ - tryall: + tryall: /* if there are other nameservers, give them a go, otherwise return with error */ - { - variant = -1; - local_ns = (local_ns + 1) % nscount; - if (local_ns == 0) - retries++; + variant = -1; + local_ns = (local_ns + 1) % nscount; + if (local_ns == 0) + retries++; - continue; - } + continue; - again: + again: /* if there are searchdomains, try them or fallback as passed */ - { - int sdomains; - BIGLOCK; - sdomains=__searchdomains; - BIGUNLOCK; - - if (variant < sdomains - 1) { - /* next search */ - variant++; - } else { - /* next server, first search */ - local_ns = (local_ns + 1) % nscount; - if (local_ns == 0) - retries++; - - variant = -1; - } + if (!ends_with_dot) { + int sdomains; + + __UCLIBC_MUTEX_LOCK(__resolv_lock); + sdomains = __searchdomains; + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); + + if (variant < sdomains - 1) { + /* next search */ + variant++; + continue; + } } + /* next server, first search */ + local_ns = (local_ns + 1) % nscount; + if (local_ns == 0) + retries++; + variant = -1; } -fail: + fail: if (fd != -1) - close(fd); - if (lookup) - free(lookup); - if (packet) - free(packet); + close(fd); + free(lookup); + free(packet); h_errno = NETDB_INTERNAL; /* Mess with globals while under lock */ if (local_ns != -1) { - LOCK; - ns = local_ns; - id = local_id; - UNLOCK; + __UCLIBC_MUTEX_LOCK(mylock); + static_ns = local_ns; + static_id = local_id; + __UCLIBC_MUTEX_UNLOCK(mylock); } return -1; } @@ -1014,21 +1049,21 @@ fail: #ifdef L_opennameservers +/* We use __resolv_lock to guard access to the + * '__nameservers' and __searchdomains globals */ int __nameservers; char * __nameserver[MAX_SERVERS]; int __searchdomains; char * __searchdomain[MAX_SEARCH]; -#ifdef __UCLIBC_HAS_THREADS__ -# include <pthread.h> -pthread_mutex_t __resolv_lock = PTHREAD_MUTEX_INITIALIZER; -#endif + +__UCLIBC_MUTEX_INIT(__resolv_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP); /* * we currently read formats not quite the same as that on normal * unix systems, we can have a list of nameservers after the keyword. */ -int attribute_hidden __open_nameservers() +void attribute_hidden __open_nameservers(void) { FILE *fp; int i; @@ -1036,18 +1071,14 @@ int attribute_hidden __open_nameservers() char szBuffer[128], *p, *argv[RESOLV_ARGS]; int argc; - BIGLOCK; - if (__nameservers > 0) { - BIGUNLOCK; - return 0; - } + __UCLIBC_MUTEX_LOCK(__resolv_lock); + if (__nameservers > 0) + goto DONE; if ((fp = fopen("/etc/resolv.conf", "r")) || - (fp = fopen("/etc/config/resolv.conf", "r"))) + (fp = fopen("/etc/config/resolv.conf", "r"))) { - while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) { - for (p = szBuffer; *p && isspace(*p); p++) /* skip white space */; if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */ @@ -1063,32 +1094,35 @@ int attribute_hidden __open_nameservers() if (strcmp(argv[0], "nameserver") == 0) { for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) { - __nameserver[__nameservers++] = strdup(argv[i]); + __nameserver[__nameservers++] = strdup(argv[i]); /* TODO: what if this fails? */ DPRINTF("adding nameserver %s\n", argv[i]); } } /* domain and search are mutually exclusive, the last one wins */ - if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) { + if (strcmp(argv[0],"domain") == 0 || strcmp(argv[0],"search") == 0) { while (__searchdomains > 0) { free(__searchdomain[--__searchdomains]); __searchdomain[__searchdomains] = NULL; } - for (i=1; i < argc && __searchdomains < MAX_SEARCH; i++) { - __searchdomain[__searchdomains++] = strdup(argv[i]); + for (i = 1; i < argc && __searchdomains < MAX_SEARCH; i++) { + __searchdomain[__searchdomains++] = strdup(argv[i]); /* TODO: what if this fails? */ DPRINTF("adding search %s\n", argv[i]); } } } fclose(fp); DPRINTF("nameservers = %d\n", __nameservers); - BIGUNLOCK; - return 0; + goto DONE; } DPRINTF("failed to open %s\n", "resolv.conf"); h_errno = NO_RECOVERY; - BIGUNLOCK; - return -1; + + /* rv = -1; */ + + DONE: + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); + /* return rv; */ } #endif @@ -1097,7 +1131,7 @@ int attribute_hidden __open_nameservers() void attribute_hidden __close_nameservers(void) { - BIGLOCK; + __UCLIBC_MUTEX_LOCK(__resolv_lock); while (__nameservers > 0) { free(__nameserver[--__nameservers]); __nameserver[__nameservers] = NULL; @@ -1106,7 +1140,7 @@ void attribute_hidden __close_nameservers(void) free(__searchdomain[--__searchdomains]); __searchdomain[__searchdomains] = NULL; } - BIGUNLOCK; + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); } #endif @@ -1117,7 +1151,7 @@ struct hostent *gethostbyname(const char *name) static struct hostent h; static char buf[sizeof(struct in_addr) + sizeof(struct in_addr *)*2 + - sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */]; + sizeof(char *)*ALIAS_DIM + 384/*namebuffer*/ + 32/* margin */]; struct hostent *hp; gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno); @@ -1137,7 +1171,7 @@ struct hostent *gethostbyname2(const char *name, int family) static struct hostent h; static char buf[sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 + - sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */]; + sizeof(char *)*ALIAS_DIM + 384/*namebuffer*/ + 32/* margin */]; struct hostent *hp; gethostbyname2_r(name, family, &h, buf, sizeof(buf), &hp, &h_errno); @@ -1150,57 +1184,65 @@ struct hostent *gethostbyname2(const char *name, int family) #ifdef L_res_init +/* We use __resolv_lock to guard access to global '_res' */ +#ifdef USE___THREAD && USE___THREAD +__thread +#endif +struct __res_state _res; + int res_init(void) { + struct __res_state *rp = &(_res); + + __UCLIBC_MUTEX_LOCK(__resolv_lock); /* must be a recursive lock! */ __close_nameservers(); __open_nameservers(); - - BIGLOCK; - _res.retrans = RES_TIMEOUT; - _res.retry = 4; - _res.options = RES_INIT; - _res.id = (u_int) random(); - _res.nsaddr.sin_addr.s_addr = INADDR_ANY; - _res.nsaddr.sin_family = AF_INET; - _res.nsaddr.sin_port = htons(NAMESERVER_PORT); - _res.ndots = 1; - /** _res.pfcode = 0; **/ - _res._vcsock = -1; - /** _res._flags = 0; **/ - /** _res.qhook = NULL; **/ - /** _res.rhook = NULL; **/ - /** _res._u._ext.nsinit = 0; **/ - - if(__searchdomains) { + rp->retrans = RES_TIMEOUT; + rp->retry = 4; + rp->options = RES_INIT; + rp->id = (u_int) random(); + rp->nsaddr.sin_addr.s_addr = INADDR_ANY; + rp->nsaddr.sin_family = AF_INET; + rp->nsaddr.sin_port = htons(NAMESERVER_PORT); + rp->ndots = 1; + /** rp->pfcode = 0; **/ + rp->_vcsock = -1; + /** rp->_flags = 0; **/ + /** rp->qhook = NULL; **/ + /** rp->rhook = NULL; **/ + /** rp->_u._ext.nsinit = 0; **/ + + if (__searchdomains) { int i; - for(i=0; i<__searchdomains; i++) { - _res.dnsrch[i] = __searchdomain[i]; - } + for (i = 0; i < __searchdomains; i++) + rp->dnsrch[i] = __searchdomain[i]; } - if(__nameservers) { + if (__nameservers) { int i; struct in_addr a; - for(i=0; i<__nameservers; i++) { + for (i = 0; i < __nameservers; i++) { if (inet_aton(__nameserver[i], &a)) { - _res.nsaddr_list[i].sin_addr = a; - _res.nsaddr_list[i].sin_family = AF_INET; - _res.nsaddr_list[i].sin_port = htons(NAMESERVER_PORT); + rp->nsaddr_list[i].sin_addr = a; + rp->nsaddr_list[i].sin_family = AF_INET; + rp->nsaddr_list[i].sin_port = htons(NAMESERVER_PORT); } } } - _res.nscount = __nameservers; - BIGUNLOCK; + rp->nscount = __nameservers; + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); - return(0); + return 0; } libc_hidden_def(res_init) -void res_close( void ) +#ifdef __UCLIBC_HAS_BSD_RES_CLOSE__ +void res_close(void) { - return; + __close_nameservers(); + memset(&_res, 0, sizeof(_res)); } - +#endif /* This needs to be after the use of _res in res_init, above. */ #undef _res @@ -1271,33 +1313,29 @@ int res_query(const char *dname, int class, int type, __open_nameservers(); if (!dname || class != 1 /* CLASS_IN */) { h_errno = NO_RECOVERY; - return(-1); + return -1; } - memset((char *) &a, '\0', sizeof(a)); + memset(&a, '\0', sizeof(a)); - BIGLOCK; - __nameserversXX=__nameservers; - __nameserverXX=__nameserver; - BIGUNLOCK; + __UCLIBC_MUTEX_LOCK(__resolv_lock); + __nameserversXX = __nameservers; + __nameserverXX = __nameserver; + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); i = __dns_lookup(dname, type, __nameserversXX, __nameserverXX, &packet, &a); if (i < 0) { h_errno = TRY_AGAIN; - return(-1); + return -1; } free(a.dotted); - if (a.atype == type) { /* CNAME*/ - int len = MIN(anslen, i); - memcpy(answer, packet, len); - if (packet) - free(packet); - return(len); + if (a.atype == type) { /* CNAME */ + i = MIN(anslen, i); + memcpy(answer, packet, i); } - if (packet) - free(packet); + free(packet); return i; } libc_hidden_def(res_query) @@ -1308,21 +1346,28 @@ libc_hidden_def(res_query) * If enabled, implement search rules until answer or unrecoverable failure * is detected. Error code, if any, is left in h_errno. */ -int res_search(name, class, type, answer, anslen) - const char *name; /* domain name */ - int class, type; /* class and type of query */ - u_char *answer; /* buffer to put answer */ - int anslen; /* size of answer */ +#define __TRAILING_DOT (1<<0) +#define __GOT_NODATA (1<<1) +#define __GOT_SERVFAIL (1<<2) +#define __TRIED_AS_IS (1<<3) +int res_search(const char *name, int class, int type, u_char *answer, + int anslen) { const char *cp, * const *domain; HEADER *hp = (HEADER *)(void *)answer; u_int dots; - int trailing_dot, ret, saved_herrno; - int got_nodata = 0, got_servfail = 0, tried_as_is = 0; - - if ((!name || !answer) || ((_res.options & RES_INIT) == 0 && res_init() == -1)) { + unsigned _state = 0; + int ret, saved_herrno; + u_long _res_options; + unsigned _res_ndots; + char **_res_dnsrch; + + __UCLIBC_MUTEX_LOCK(__resolv_lock); + _res_options = _res.options; + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); + if ((!name || !answer) || ((_res_options & RES_INIT) == 0 && res_init() == -1)) { h_errno = NETDB_INTERNAL; - return (-1); + return -1; } errno = 0; @@ -1330,21 +1375,24 @@ int res_search(name, class, type, answer, anslen) dots = 0; for (cp = name; *cp; cp++) dots += (*cp == '.'); - trailing_dot = 0; + if (cp > name && *--cp == '.') - trailing_dot++; + _state |= __TRAILING_DOT; /* * If there are dots in the name already, let's just give it a try * 'as is'. The threshold can be set with the "ndots" option. */ saved_herrno = -1; - if (dots >= _res.ndots) { + __UCLIBC_MUTEX_LOCK(__resolv_lock); + _res_ndots = _res.ndots; + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); + if (dots >= _res_ndots) { ret = res_querydomain(name, NULL, class, type, answer, anslen); if (ret > 0) - return (ret); + return ret; saved_herrno = h_errno; - tried_as_is++; + _state |= __TRIED_AS_IS; } /* @@ -1353,18 +1401,22 @@ int res_search(name, class, type, answer, anslen) * - there is at least one dot, there is no trailing dot, * and RES_DNSRCH is set. */ - if ((!dots && (_res.options & RES_DEFNAMES)) || - (dots && !trailing_dot && (_res.options & RES_DNSRCH))) { - int done = 0; - - for (domain = (const char * const *)_res.dnsrch; - *domain && !done; - domain++) { + __UCLIBC_MUTEX_LOCK(__resolv_lock); + _res_options = _res.options; + _res_dnsrch = _res.dnsrch; + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); + if ((!dots && (_res_options & RES_DEFNAMES)) || + (dots && !(_state & __TRAILING_DOT) && (_res_options & RES_DNSRCH))) { + bool done = 0; + + for (domain = (const char * const *)_res_dnsrch; + *domain && !done; + domain++) { ret = res_querydomain(name, *domain, class, type, - answer, anslen); + answer, anslen); if (ret > 0) - return (ret); + return ret; /* * If no server present, give up. @@ -1381,33 +1433,36 @@ int res_search(name, class, type, answer, anslen) */ if (errno == ECONNREFUSED) { h_errno = TRY_AGAIN; - return (-1); + return -1; } switch (h_errno) { - case NO_DATA: - got_nodata++; - /* FALLTHROUGH */ - case HOST_NOT_FOUND: - /* keep trying */ - break; - case TRY_AGAIN: - if (hp->rcode == SERVFAIL) { - /* try next search element, if any */ - got_servfail++; + case NO_DATA: + _state |= __GOT_NODATA; + /* FALLTHROUGH */ + case HOST_NOT_FOUND: + /* keep trying */ break; - } - /* FALLTHROUGH */ - default: - /* anything else implies that we're done */ - done++; + case TRY_AGAIN: + if (hp->rcode == SERVFAIL) { + /* try next search element, if any */ + _state |= __GOT_SERVFAIL; + break; + } + /* FALLTHROUGH */ + default: + /* anything else implies that we're done */ + done = 1; } /* * if we got here for some reason other than DNSRCH, * we only wanted one iteration of the loop, so stop. */ - if (!(_res.options & RES_DNSRCH)) - done++; + __UCLIBC_MUTEX_LOCK(__resolv_lock); + _res_options = _res.options; + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); + if (!(_res_options & RES_DNSRCH)) + done = 1; } } @@ -1416,10 +1471,10 @@ int res_search(name, class, type, answer, anslen) * note that we do this regardless of how many dots were in the * name or whether it ends with a dot. */ - if (!tried_as_is) { + if (!(_state & __TRIED_AS_IS)) { ret = res_querydomain(name, NULL, class, type, answer, anslen); if (ret > 0) - return (ret); + return ret; } /* @@ -1432,36 +1487,43 @@ int res_search(name, class, type, answer, anslen) */ if (saved_herrno != -1) h_errno = saved_herrno; - else if (got_nodata) + else if (_state & __GOT_NODATA) h_errno = NO_DATA; - else if (got_servfail) + else if (_state & __GOT_SERVFAIL) h_errno = TRY_AGAIN; - return (-1); + return -1; } - +#undef __TRAILING_DOT +#undef __GOT_NODATA +#undef __GOT_SERVFAIL +#undef __TRIED_AS_IS /* * Perform a call on res_query on the concatenation of name and domain, * removing a trailing dot from name if domain is NULL. */ -int res_querydomain(name, domain, class, type, answer, anslen) - const char *name, *domain; - int class, type; /* class and type of query */ - u_char *answer; /* buffer to put answer */ - int anslen; /* size of answer */ +int res_querydomain(const char *name, const char *domain, int class, int type, + u_char * answer, int anslen) { char nbuf[MAXDNAME]; const char *longname = nbuf; size_t n, d; + u_long _res_options; - if ((!name || !answer) || ((_res.options & RES_INIT) == 0 && res_init() == -1)) { + __UCLIBC_MUTEX_LOCK(__resolv_lock); + _res_options = _res.options; + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); + if ((!name || !answer) || ((_res_options & RES_INIT) == 0 && res_init() == -1)) { h_errno = NETDB_INTERNAL; - return (-1); + return -1; } #ifdef DEBUG - if (_res.options & RES_DEBUG) + __UCLIBC_MUTEX_LOCK(__resolv_lock); + _res_options = _res.options; + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); + if (_res_options & RES_DEBUG) printf(";; res_querydomain(%s, %s, %d, %d)\n", - name, domain?domain:"<Nil>", class, type); + name, (domain ? domain : "<Nil>"), class, type); #endif if (domain == NULL) { /* @@ -1471,7 +1533,7 @@ int res_querydomain(name, domain, class, type, answer, anslen) n = strlen(name); if (n + 1 > sizeof(nbuf)) { h_errno = NO_RECOVERY; - return (-1); + return -1; } if (n > 0 && name[--n] == '.') { strncpy(nbuf, name, n); @@ -1483,11 +1545,11 @@ int res_querydomain(name, domain, class, type, answer, anslen) d = strlen(domain); if (n + 1 + d + 1 > sizeof(nbuf)) { h_errno = NO_RECOVERY; - return (-1); + return -1; } snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); } - return (res_query(longname, class, type, answer, anslen)); + return res_query(longname, class, type, answer, anslen); } libc_hidden_def(res_querydomain) @@ -1503,11 +1565,11 @@ struct hostent *gethostbyaddr (const void *addr, socklen_t len, int type) static struct hostent h; static char buf[ #ifndef __UCLIBC_HAS_IPV6__ - sizeof(struct in_addr) + sizeof(struct in_addr *)*2 + + sizeof(struct in_addr) + sizeof(struct in_addr *)*2 + #else - sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 + + sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 + #endif /* __UCLIBC_HAS_IPV6__ */ - sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */]; + sizeof(char *)*ALIAS_DIM + 384 /*namebuffer*/ + 32 /* margin */]; struct hostent *hp; gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno); @@ -1520,12 +1582,13 @@ libc_hidden_def(gethostbyaddr) #ifdef L_read_etc_hosts_r -void attribute_hidden __open_etc_hosts(FILE **fp) +FILE * __open_etc_hosts(void) { - if ((*fp = fopen("/etc/hosts", "r")) == NULL) { - *fp = fopen("/etc/config/hosts", "r"); + FILE * fp; + if ((fp = fopen("/etc/hosts", "r")) == NULL) { + fp = fopen("/etc/config/hosts", "r"); } - return; + return fp; } int attribute_hidden __read_etc_hosts_r(FILE * fp, const char * name, int type, @@ -1535,69 +1598,78 @@ int attribute_hidden __read_etc_hosts_r(FILE * fp, const char * name, int type, struct hostent ** result, int * h_errnop) { - struct in_addr *in=NULL; - struct in_addr **addr_list=NULL; + struct in_addr *in = NULL; + struct in_addr **addr_list = NULL; #ifdef __UCLIBC_HAS_IPV6__ - struct in6_addr *in6=NULL; - struct in6_addr **addr_list6=NULL; + struct in6_addr *in6 = NULL; + struct in6_addr **addr_list6 =NULL; #endif /* __UCLIBC_HAS_IPV6__ */ char *cp, **alias; - int aliases, i, ret=HOST_NOT_FOUND; + int aliases, i, ret = HOST_NOT_FOUND; - if (buflen < sizeof(char *)*(ALIAS_DIM)) + /* make sure user char * is aligned */ + i = ALIGN_BUFFER_OFFSET(buf); + if (unlikely(i)) { + if (buflen < i) + return ERANGE; + buf += i; + buflen -= i; + } + + if (buflen < sizeof(char *)*ALIAS_DIM) return ERANGE; - alias=(char **)buf; - buf+=sizeof(char **)*(ALIAS_DIM); - buflen-=sizeof(char **)*(ALIAS_DIM); + alias = (char **)buf; + buf += sizeof(char **)*ALIAS_DIM; + buflen -= sizeof(char **)*ALIAS_DIM; - if (action!=GETHOSTENT) { + if (action != GETHOSTENT) { #ifdef __UCLIBC_HAS_IPV6__ - char *p=buf; - size_t len=buflen; + char *p = buf; + size_t len = buflen; #endif /* __UCLIBC_HAS_IPV6__ */ - *h_errnop=NETDB_INTERNAL; + *h_errnop = NETDB_INTERNAL; if (buflen < sizeof(*in)) return ERANGE; - in=(struct in_addr*)buf; - buf+=sizeof(*in); - buflen-=sizeof(*in); + in = (struct in_addr*)buf; + buf += sizeof(*in); + buflen -= sizeof(*in); if (buflen < sizeof(*addr_list)*2) return ERANGE; - addr_list=(struct in_addr **)buf; - buf+=sizeof(*addr_list)*2; - buflen-=sizeof(*addr_list)*2; + addr_list = (struct in_addr **)buf; + buf += sizeof(*addr_list)*2; + buflen -= sizeof(*addr_list)*2; #ifdef __UCLIBC_HAS_IPV6__ if (len < sizeof(*in6)) return ERANGE; - in6=(struct in6_addr*)p; - p+=sizeof(*in6); - len-=sizeof(*in6); + in6 = (struct in6_addr*)p; + p += sizeof(*in6); + len -= sizeof(*in6); if (len < sizeof(*addr_list6)*2) return ERANGE; - addr_list6=(struct in6_addr**)p; - p+=sizeof(*addr_list6)*2; - len-=sizeof(*addr_list6)*2; + addr_list6 = (struct in6_addr**)p; + p += sizeof(*addr_list6)*2; + len -= sizeof(*addr_list6)*2; if (len < buflen) { - buflen=len; - buf=p; + buflen = len; + buf = p; } #endif /* __UCLIBC_HAS_IPV6__ */ if (buflen < 80) return ERANGE; - __open_etc_hosts(&fp); + fp = __open_etc_hosts(); if (fp == NULL) { - result=NULL; + result = NULL; return errno; } } - *h_errnop=HOST_NOT_FOUND; + *h_errnop = HOST_NOT_FOUND; while (fgets(buf, buflen, fp)) { if ((cp = strchr(buf, '#'))) *cp = '\0'; @@ -1620,10 +1692,10 @@ int attribute_hidden __read_etc_hosts_r(FILE * fp, const char * name, int type, if (aliases < 2) continue; /* syntax error really */ - if (action==GETHOSTENT) { + if (action == GETHOSTENT) { /* Return whatever the next entry happens to be. */ break; - } else if (action==GET_HOSTS_BYADDR) { + } else if (action == GET_HOSTS_BYADDR) { if (strcmp(name, alias[0]) != 0) continue; } else { @@ -1644,10 +1716,10 @@ int attribute_hidden __read_etc_hosts_r(FILE * fp, const char * name, int type, result_buf->h_length = sizeof(*in); result_buf->h_addr_list = (char**) addr_list; result_buf->h_aliases = alias + 2; - *result=result_buf; - ret=NETDB_SUCCESS; + *result = result_buf; + ret = NETDB_SUCCESS; #ifdef __UCLIBC_HAS_IPV6__ - } else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], in6) > 0) { + } else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], in6) > 0) { DPRINTF("Found INET6\n"); addr_list6[0] = in6; addr_list6[1] = 0; @@ -1656,92 +1728,100 @@ int attribute_hidden __read_etc_hosts_r(FILE * fp, const char * name, int type, result_buf->h_length = sizeof(*in6); result_buf->h_addr_list = (char**) addr_list6; result_buf->h_aliases = alias + 2; - *result=result_buf; - ret=NETDB_SUCCESS; + *result = result_buf; + ret = NETDB_SUCCESS; #endif /* __UCLIBC_HAS_IPV6__ */ } else { - DPRINTF("Error\n"); - ret=TRY_AGAIN; - break; /* bad ip address */ - } + /* continue parsing in the hope the user has multiple + * host types listed in the database like so: + * <ipv4 addr> host + * <ipv6 addr> host + * If looking for an IPv6 addr, don't bail when we got the IPv4 + */ + DPRINTF("Error: Found host but diff network type\n"); + ret = TRY_AGAIN; + continue; + } - if (action!=GETHOSTENT) { + if (action != GETHOSTENT) fclose(fp); - } return ret; } - if (action!=GETHOSTENT) { + if (action != GETHOSTENT) fclose(fp); - } return ret; } #endif #ifdef L_gethostent +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER); -static int __stay_open; +static smallint __stay_open; static FILE * __gethostent_fp; -void endhostent (void) +void endhostent(void) { - LOCK; - __stay_open = 0; - if (__gethostent_fp) { - fclose(__gethostent_fp); - } - UNLOCK; + __UCLIBC_MUTEX_LOCK(mylock); + __stay_open = 0; + if (__gethostent_fp) { + fclose(__gethostent_fp); + __gethostent_fp = NULL; + } + __UCLIBC_MUTEX_UNLOCK(mylock); } -void sethostent (int stay_open) +void sethostent(int stay_open) { - LOCK; - __stay_open = stay_open; - UNLOCK; + __UCLIBC_MUTEX_LOCK(mylock); + __stay_open = (stay_open != 0); + __UCLIBC_MUTEX_UNLOCK(mylock); } int gethostent_r(struct hostent *result_buf, char *buf, size_t buflen, struct hostent **result, int *h_errnop) { - int ret; + int ret; - LOCK; - if (__gethostent_fp == NULL) { - __open_etc_hosts(&__gethostent_fp); + __UCLIBC_MUTEX_LOCK(mylock); if (__gethostent_fp == NULL) { - UNLOCK; - *result=NULL; - return 0; + __gethostent_fp = __open_etc_hosts(); + if (__gethostent_fp == NULL) { + *result = NULL; + ret = TRY_AGAIN; + goto DONE; + } } - } - ret = __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT, + ret = __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT, result_buf, buf, buflen, result, h_errnop); - if (__stay_open==0) { - fclose(__gethostent_fp); - } - UNLOCK; - return(ret); + if (__stay_open == 0) { + fclose(__gethostent_fp); + __gethostent_fp = NULL; + } +DONE: + __UCLIBC_MUTEX_UNLOCK(mylock); + return ret; } libc_hidden_def(gethostent_r) -struct hostent *gethostent (void) +struct hostent *gethostent(void) { - static struct hostent h; - static char buf[ + static struct hostent h; + static char buf[ #ifndef __UCLIBC_HAS_IPV6__ - sizeof(struct in_addr) + sizeof(struct in_addr *)*2 + + sizeof(struct in_addr) + sizeof(struct in_addr *)*2 + #else - sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 + + sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 + #endif /* __UCLIBC_HAS_IPV6__ */ - sizeof(char *)*(ALIAS_DIM) + - 80/*namebuffer*/ + 2/* margin */]; - struct hostent *host; - - LOCK; - gethostent_r(&h, buf, sizeof(buf), &host, &h_errno); - UNLOCK; - return(host); + sizeof(char *)*ALIAS_DIM + + 80 /*namebuffer*/ + 2 /* margin */]; + struct hostent *host; + + __UCLIBC_MUTEX_LOCK(mylock); + gethostent_r(&h, buf, sizeof(buf), &host, &h_errno); + __UCLIBC_MUTEX_UNLOCK(mylock); + return host; } #endif @@ -1753,8 +1833,8 @@ int attribute_hidden __get_hosts_byname_r(const char * name, int type, struct hostent ** result, int * h_errnop) { - return(__read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME, - result_buf, buf, buflen, result, h_errnop)); + return __read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME, + result_buf, buf, buflen, result, h_errnop); } #endif @@ -1772,25 +1852,25 @@ int attribute_hidden __get_hosts_byaddr_r(const char * addr, int len, int type, char ipaddr[INET6_ADDRSTRLEN]; #endif /* __UCLIBC_HAS_IPV6__ */ - switch (type) { - case AF_INET: - if (len != sizeof(struct in_addr)) - return 0; - break; + switch (type) { + case AF_INET: + if (len != sizeof(struct in_addr)) + return 0; + break; #ifdef __UCLIBC_HAS_IPV6__ - case AF_INET6: - if (len != sizeof(struct in6_addr)) - return 0; - break; + case AF_INET6: + if (len != sizeof(struct in6_addr)) + return 0; + break; #endif /* __UCLIBC_HAS_IPV6__ */ - default: - return 0; + default: + return 0; } inet_ntop(type, addr, ipaddr, sizeof(ipaddr)); - return(__read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR, - result_buf, buf, buflen, result, h_errnop)); + return __read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR, + result_buf, buf, buflen, result, h_errnop); } #endif @@ -1801,12 +1881,12 @@ int attribute_hidden __get_hosts_byaddr_r(const char * addr, int len, int type, #endif /* min */ libc_hidden_proto(getnameinfo) -int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host, - socklen_t hostlen, char *serv, socklen_t servlen, - unsigned int flags) +int getnameinfo(const struct sockaddr *sa, socklen_t addrlen, char *host, + socklen_t hostlen, char *serv, socklen_t servlen, + unsigned int flags) { int serrno = errno; - int ok = 0; + unsigned ok; struct hostent *h = NULL; char domain[256]; @@ -1814,25 +1894,27 @@ int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host, return EAI_BADFLAGS; if (sa == NULL || addrlen < sizeof (sa_family_t)) - return EAI_FAMILY; + goto BAD_FAM; - switch (sa->sa_family) { - case AF_LOCAL: - break; - case AF_INET: + ok = sa->sa_family; + if (ok == AF_LOCAL) /* valid */; +#ifdef __UCLIBC_HAS_IPV4__ + else if (ok == AF_INET) { if (addrlen < sizeof (struct sockaddr_in)) - return EAI_FAMILY; - break; + goto BAD_FAM; + } +#endif #ifdef __UCLIBC_HAS_IPV6__ - case AF_INET6: + else if (ok == AF_INET6) { if (addrlen < sizeof (struct sockaddr_in6)) - return EAI_FAMILY; - break; + goto BAD_FAM; + } #endif /* __UCLIBC_HAS_IPV6__ */ - default: + else +BAD_FAM: return EAI_FAMILY; - } + ok = 0; if (host != NULL && hostlen > 0) switch (sa->sa_family) { case AF_INET: @@ -1845,15 +1927,19 @@ int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host, h = gethostbyaddr ((const void *) &(((const struct sockaddr_in6 *) sa)->sin6_addr), sizeof(struct in6_addr), AF_INET6); - else #endif /* __UCLIBC_HAS_IPV6__ */ - h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr), +#if defined __UCLIBC_HAS_IPV6__ && defined __UCLIBC_HAS_IPV4__ + else +#endif +#ifdef __UCLIBC_HAS_IPV4__ + h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr), sizeof(struct in_addr), AF_INET); +#endif /* __UCLIBC_HAS_IPV4__ */ if (h) { char *c; if ((flags & NI_NOFQDN) - && (getdomainname (domain, sizeof(domain)) == 0) + && (__libc_getdomainname (domain, sizeof(domain)) == 0) && (c = strstr (h->h_name, domain)) && (c != h->h_name) && (*(--c) == '.')) { strncpy (host, h->h_name, @@ -1919,11 +2005,16 @@ int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host, memcpy (host + real_hostlen, scopebuf, scopelen + 1); } #endif - } else + } #endif /* __UCLIBC_HAS_IPV6__ */ +#if defined __UCLIBC_HAS_IPV6__ && defined __UCLIBC_HAS_IPV4__ + else +#endif /* __UCLIBC_HAS_IPV6__ && defined __UCLIBC_HAS_IPV4__ */ +#if defined __UCLIBC_HAS_IPV4__ c = inet_ntop (AF_INET, (const void *) &(((const struct sockaddr_in *) sa)->sin_addr), host, hostlen); +#endif /* __UCLIBC_HAS_IPV4__ */ if (c == NULL) { errno = serrno; @@ -1952,34 +2043,29 @@ int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host, strncpy (host, "localhost", hostlen); break; - default: +/*Already checked above default: return EAI_FAMILY; +*/ } if (serv && (servlen > 0)) { - switch (sa->sa_family) { - case AF_INET: -#ifdef __UCLIBC_HAS_IPV6__ - case AF_INET6: -#endif /* __UCLIBC_HAS_IPV6__ */ + if (sa->sa_family == AF_LOCAL) { + strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen); + } else { /* AF_INET || AF_INET6 */ if (!(flags & NI_NUMERICSERV)) { struct servent *s; s = getservbyport (((const struct sockaddr_in *) sa)->sin_port, ((flags & NI_DGRAM) ? "udp" : "tcp")); if (s) { strncpy (serv, s->s_name, servlen); - break; + goto DONE; } } snprintf (serv, servlen, "%d", ntohs (((const struct sockaddr_in *) sa)->sin_port)); - break; - - case AF_LOCAL: - strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen); - break; } } +DONE: if (host && (hostlen > 0)) host[hostlen-1] = 0; if (serv && (servlen > 0)) @@ -1994,10 +2080,10 @@ libc_hidden_def(getnameinfo) #ifdef L_gethostbyname_r int gethostbyname_r(const char * name, - struct hostent * result_buf, - char * buf, size_t buflen, - struct hostent ** result, - int * h_errnop) + struct hostent * result_buf, + char * buf, size_t buflen, + struct hostent ** result, + int * h_errnop) { struct in_addr *in; struct in_addr **addr_list; @@ -2009,7 +2095,7 @@ int gethostbyname_r(const char * name, char ** __nameserverXX; __open_nameservers(); - *result=NULL; + *result = NULL; if (!name) return EINVAL; @@ -2018,8 +2104,8 @@ int gethostbyname_r(const char * name, int old_errno = errno; /* Save the old errno and reset errno */ __set_errno(0); /* to check for missing /etc/hosts. */ - if ((i=__get_hosts_byname_r(name, AF_INET, result_buf, - buf, buflen, result, h_errnop))==0) + if ((i = __get_hosts_byname_r(name, AF_INET, result_buf, + buf, buflen, result, h_errnop)) == 0) return i; switch (*h_errnop) { case HOST_NOT_FOUND: @@ -2038,29 +2124,38 @@ int gethostbyname_r(const char * name, DPRINTF("Nothing found in /etc/hosts\n"); + /* make sure user char * is aligned */ + i = ALIGN_BUFFER_OFFSET(buf); + if (unlikely(i)) { + if (buflen < i) + return ERANGE; + buf += i; + buflen -= i; + } + *h_errnop = NETDB_INTERNAL; if (buflen < sizeof(*in)) return ERANGE; - in=(struct in_addr*)buf; - buf+=sizeof(*in); - buflen-=sizeof(*in); + in = (struct in_addr*)buf; + buf += sizeof(*in); + buflen -= sizeof(*in); if (buflen < sizeof(*addr_list)*2) return ERANGE; - addr_list=(struct in_addr**)buf; - buf+=sizeof(*addr_list)*2; - buflen-=sizeof(*addr_list)*2; + addr_list = (struct in_addr**)buf; + buf += sizeof(*addr_list)*2; + buflen -= sizeof(*addr_list)*2; addr_list[0] = in; addr_list[1] = 0; - if (buflen < sizeof(char *)*(ALIAS_DIM)) + if (buflen < sizeof(char *)*ALIAS_DIM) return ERANGE; - alias=(char **)buf; - buf+=sizeof(char **)*(ALIAS_DIM); - buflen-=sizeof(char **)*(ALIAS_DIM); + alias = (char **)buf; + buf += sizeof(char **)*ALIAS_DIM; + buflen -= sizeof(char **)*ALIAS_DIM; - if (buflen<256) + if (buflen < 256) return ERANGE; strncpy(buf, name, buflen); @@ -2069,76 +2164,72 @@ int gethostbyname_r(const char * name, /* First check if this is already an address */ if (inet_aton(name, in)) { - result_buf->h_name = buf; - result_buf->h_addrtype = AF_INET; - result_buf->h_length = sizeof(*in); - result_buf->h_addr_list = (char **) addr_list; - result_buf->h_aliases = alias; - *result=result_buf; - *h_errnop = NETDB_SUCCESS; - return NETDB_SUCCESS; + result_buf->h_name = buf; + result_buf->h_addrtype = AF_INET; + result_buf->h_length = sizeof(*in); + result_buf->h_addr_list = (char **) addr_list; + result_buf->h_aliases = alias; + *result = result_buf; + *h_errnop = NETDB_SUCCESS; + return NETDB_SUCCESS; } for (;;) { + __UCLIBC_MUTEX_LOCK(__resolv_lock); + __nameserversXX = __nameservers; + __nameserverXX = __nameserver; + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); + a.buf = buf; + a.buflen = buflen; + a.add_count = 0; + i = __dns_lookup(name, T_A, __nameserversXX, __nameserverXX, &packet, &a); - BIGLOCK; - __nameserversXX=__nameservers; - __nameserverXX=__nameserver; - BIGUNLOCK; - a.buf = buf; - a.buflen = buflen; - a.add_count = 0; - i = __dns_lookup(name, T_A, __nameserversXX, __nameserverXX, &packet, &a); - - if (i < 0) { - *h_errnop = HOST_NOT_FOUND; - DPRINTF("__dns_lookup\n"); - return TRY_AGAIN; - } - - if ((a.rdlength + sizeof(struct in_addr*)) * a.add_count + 256 > buflen) - { + if (i < 0) { + *h_errnop = HOST_NOT_FOUND; + DPRINTF("__dns_lookup\n"); + return TRY_AGAIN; + } + + if ((a.rdlength + sizeof(struct in_addr*)) * a.add_count + 256 > buflen) { + free(a.dotted); + free(packet); + *h_errnop = NETDB_INTERNAL; + DPRINTF("buffer too small for all addresses\n"); + return ERANGE; + } else if (a.add_count > 0) { + memmove(buf - sizeof(struct in_addr*)*2, buf, a.add_count * a.rdlength); + addr_list = (struct in_addr**)(buf + a.add_count * a.rdlength); + addr_list[0] = in; + for (i = a.add_count - 1; i >= 0; --i) + addr_list[i+1] = (struct in_addr*)(buf - sizeof(struct in_addr*)*2 + a.rdlength * i); + addr_list[a.add_count + 1] = 0; + buflen -= (((char*)&(addr_list[a.add_count + 2])) - buf); + buf = (char*)&addr_list[a.add_count + 2]; + } + + strncpy(buf, a.dotted, buflen); free(a.dotted); - free(packet); - *h_errnop = NETDB_INTERNAL; - DPRINTF("buffer too small for all addresses\n"); - return ERANGE; - } - else if(a.add_count > 0) - { - memmove(buf - sizeof(struct in_addr*)*2, buf, a.add_count * a.rdlength); - addr_list = (struct in_addr**)(buf + a.add_count * a.rdlength); - addr_list[0] = in; - for (i = a.add_count-1; i>=0; --i) - addr_list[i+1] = (struct in_addr*)(buf - sizeof(struct in_addr*)*2 + a.rdlength * i); - addr_list[a.add_count + 1] = 0; - buflen -= (((char*)&(addr_list[a.add_count + 2])) - buf); - buf = (char*)&addr_list[a.add_count + 2]; - } - - strncpy(buf, a.dotted, buflen); - free(a.dotted); - - if (a.atype == T_A) { /* ADDRESS */ - memcpy(in, a.rdata, sizeof(*in)); - result_buf->h_name = buf; - result_buf->h_addrtype = AF_INET; - result_buf->h_length = sizeof(*in); - result_buf->h_addr_list = (char **) addr_list; + + if (a.atype == T_A) { /* ADDRESS */ + memcpy(in, a.rdata, sizeof(*in)); + result_buf->h_name = buf; + result_buf->h_addrtype = AF_INET; + result_buf->h_length = sizeof(*in); + result_buf->h_addr_list = (char **) addr_list; #ifdef __UCLIBC_MJN3_ONLY__ #warning TODO -- generate the full list #endif - result_buf->h_aliases = alias; /* TODO: generate the full list */ - free(packet); - break; - } else { - free(packet); - *h_errnop=HOST_NOT_FOUND; - return TRY_AGAIN; - } + result_buf->h_aliases = alias; /* TODO: generate the full list */ + free(packet); + break; + } else { + free(packet); + *h_errnop = HOST_NOT_FOUND; + return TRY_AGAIN; + } } - *result=result_buf; + *result = result_buf; *h_errnop = NETDB_SUCCESS; return NETDB_SUCCESS; } @@ -2148,14 +2239,15 @@ libc_hidden_def(gethostbyname_r) #ifdef L_gethostbyname2_r int gethostbyname2_r(const char *name, int family, - struct hostent * result_buf, - char * buf, size_t buflen, - struct hostent ** result, - int * h_errnop) + struct hostent * result_buf, + char * buf, size_t buflen, + struct hostent ** result, + int * h_errnop) { #ifndef __UCLIBC_HAS_IPV6__ - return family == (AF_INET)? gethostbyname_r(name, result_buf, - buf, buflen, result, h_errnop) : HOST_NOT_FOUND; + return family == (AF_INET) + ? gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop) + : HOST_NOT_FOUND; #else /* __UCLIBC_HAS_IPV6__ */ struct in6_addr *in; struct in6_addr **addr_list; @@ -2173,7 +2265,7 @@ int gethostbyname2_r(const char *name, int family, return EINVAL; __open_nameservers(); - *result=NULL; + *result = NULL; if (!name) return EINVAL; @@ -2182,8 +2274,8 @@ int gethostbyname2_r(const char *name, int family, int old_errno = errno; /* Save the old errno and reset errno */ __set_errno(0); /* to check for missing /etc/hosts. */ - if ((i=__get_hosts_byname_r(name, AF_INET, result_buf, - buf, buflen, result, h_errnop))==0) + if ((i = __get_hosts_byname_r(name, family, result_buf, + buf, buflen, result, h_errnop)) == 0) return i; switch (*h_errnop) { case HOST_NOT_FOUND: @@ -2205,41 +2297,41 @@ int gethostbyname2_r(const char *name, int family, *h_errnop = NETDB_INTERNAL; if (buflen < sizeof(*in)) return ERANGE; - in=(struct in6_addr*)buf; - buf+=sizeof(*in); - buflen-=sizeof(*in); + in = (struct in6_addr*)buf; + buf += sizeof(*in); + buflen -= sizeof(*in); if (buflen < sizeof(*addr_list)*2) return ERANGE; - addr_list=(struct in6_addr**)buf; - buf+=sizeof(*addr_list)*2; - buflen-=sizeof(*addr_list)*2; + addr_list = (struct in6_addr**)buf; + buf += sizeof(*addr_list)*2; + buflen -= sizeof(*addr_list)*2; addr_list[0] = in; addr_list[1] = 0; - if (buflen<256) + if (buflen < 256) return ERANGE; strncpy(buf, name, buflen); /* First check if this is already an address */ if (inet_pton(AF_INET6, name, in)) { - result_buf->h_name = buf; - result_buf->h_addrtype = AF_INET6; - result_buf->h_length = sizeof(*in); - result_buf->h_addr_list = (char **) addr_list; - *result=result_buf; - *h_errnop = NETDB_SUCCESS; - return NETDB_SUCCESS; + result_buf->h_name = buf; + result_buf->h_addrtype = AF_INET6; + result_buf->h_length = sizeof(*in); + result_buf->h_addr_list = (char **) addr_list; + *result = result_buf; + *h_errnop = NETDB_SUCCESS; + return NETDB_SUCCESS; } memset((char *) &a, '\0', sizeof(a)); for (;;) { - BIGLOCK; - __nameserversXX=__nameservers; - __nameserverXX=__nameserver; - BIGUNLOCK; + __UCLIBC_MUTEX_LOCK(__resolv_lock); + __nameserversXX = __nameservers; + __nameserverXX = __nameserver; + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); i = __dns_lookup(buf, T_AAAA, __nameserversXX, __nameserverXX, &packet, &a); @@ -2275,12 +2367,12 @@ int gethostbyname2_r(const char *name, int family, break; } else { free(packet); - *h_errnop=HOST_NOT_FOUND; + *h_errnop = HOST_NOT_FOUND; return TRY_AGAIN; } } - *result=result_buf; + *result = result_buf; *h_errnop = NETDB_SUCCESS; return NETDB_SUCCESS; #endif /* __UCLIBC_HAS_IPV6__ */ @@ -2289,11 +2381,11 @@ libc_hidden_def(gethostbyname2_r) #endif #ifdef L_gethostbyaddr_r -int gethostbyaddr_r (const void *addr, socklen_t len, int type, - struct hostent * result_buf, - char * buf, size_t buflen, - struct hostent ** result, - int * h_errnop) +int gethostbyaddr_r(const void *addr, socklen_t len, int type, + struct hostent * result_buf, + char * buf, size_t buflen, + struct hostent ** result, + int * h_errnop) { struct in_addr *in; @@ -2312,7 +2404,7 @@ int gethostbyaddr_r (const void *addr, socklen_t len, int type, int __nameserversXX; char ** __nameserverXX; - *result=NULL; + *result = NULL; if (!addr) return EINVAL; @@ -2334,8 +2426,8 @@ int gethostbyaddr_r (const void *addr, socklen_t len, int type, } /* do /etc/hosts first */ - if ((i=__get_hosts_byaddr_r(addr, len, type, result_buf, - buf, buflen, result, h_errnop))==0) + if ((i = __get_hosts_byaddr_r(addr, len, type, result_buf, + buf, buflen, result, h_errnop)) == 0) return i; switch (*h_errnop) { case HOST_NOT_FOUND: @@ -2348,52 +2440,52 @@ int gethostbyaddr_r (const void *addr, socklen_t len, int type, __open_nameservers(); #ifdef __UCLIBC_HAS_IPV6__ - qp=buf; - plen=buflen; + qp = buf; + plen = buflen; #endif /* __UCLIBC_HAS_IPV6__ */ *h_errnop = NETDB_INTERNAL; if (buflen < sizeof(*in)) return ERANGE; - in=(struct in_addr*)buf; - buf+=sizeof(*in); - buflen-=sizeof(*in); + in = (struct in_addr*)buf; + buf += sizeof(*in); + buflen -= sizeof(*in); if (buflen < sizeof(*addr_list)*2) return ERANGE; - addr_list=(struct in_addr**)buf; - buf+=sizeof(*addr_list)*2; - buflen-=sizeof(*addr_list)*2; + addr_list = (struct in_addr**)buf; + buf += sizeof(*addr_list)*2; + buflen -= sizeof(*addr_list)*2; - if (buflen < sizeof(char *)*(ALIAS_DIM)) + if (buflen < sizeof(char *)*ALIAS_DIM) return ERANGE; - alias=(char **)buf; - buf+=sizeof(*alias)*(ALIAS_DIM); - buflen-=sizeof(*alias)*(ALIAS_DIM); + alias = (char **)buf; + buf += sizeof(*alias)*ALIAS_DIM; + buflen -= sizeof(*alias)*ALIAS_DIM; #ifdef __UCLIBC_HAS_IPV6__ if (plen < sizeof(*in6)) return ERANGE; - in6=(struct in6_addr*)qp; - qp+=sizeof(*in6); - plen-=sizeof(*in6); + in6 = (struct in6_addr*)qp; + qp += sizeof(*in6); + plen -= sizeof(*in6); if (plen < sizeof(*addr_list6)*2) return ERANGE; - addr_list6=(struct in6_addr**)qp; - qp+=sizeof(*addr_list6)*2; - plen-=sizeof(*addr_list6)*2; + addr_list6 = (struct in6_addr**)qp; + qp += sizeof(*addr_list6)*2; + plen -= sizeof(*addr_list6)*2; if (plen < buflen) { - buflen=plen; - buf=qp; + buflen = plen; + buf = qp; } #endif /* __UCLIBC_HAS_IPV6__ */ - if (buflen<256) + if (buflen < 256) return ERANGE; - if(type == AF_INET) { + if (type == AF_INET) { unsigned char *tmp_addr = (unsigned char *)addr; memcpy(&in->s_addr, addr, len); @@ -2401,7 +2493,7 @@ int gethostbyaddr_r (const void *addr, socklen_t len, int type, addr_list[0] = in; sprintf(buf, "%u.%u.%u.%u.in-addr.arpa", - tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]); + tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]); #ifdef __UCLIBC_HAS_IPV6__ } else { memcpy(in6->s6_addr, addr, len); @@ -2411,9 +2503,9 @@ int gethostbyaddr_r (const void *addr, socklen_t len, int type, for (i = len - 1; i >= 0; i--) { qp += sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf, - (in6->s6_addr[i] >> 4) & 0xf); - } - strcpy(qp, "ip6.int"); + (in6->s6_addr[i] >> 4) & 0xf); + } + strcpy(qp, "ip6.arpa"); #endif /* __UCLIBC_HAS_IPV6__ */ } @@ -2423,11 +2515,10 @@ int gethostbyaddr_r (const void *addr, socklen_t len, int type, alias[1] = 0; for (;;) { - - BIGLOCK; - __nameserversXX=__nameservers; - __nameserverXX=__nameserver; - BIGUNLOCK; + __UCLIBC_MUTEX_LOCK(__resolv_lock); + __nameserversXX = __nameservers; + __nameserverXX = __nameserver; + __UCLIBC_MUTEX_UNLOCK(__resolv_lock); i = __dns_lookup(buf, T_PTR, __nameserversXX, __nameserverXX, &packet, &a); if (i < 0) { @@ -2459,13 +2550,13 @@ int gethostbyaddr_r (const void *addr, socklen_t len, int type, result_buf->h_name = buf; result_buf->h_addrtype = type; - if(type == AF_INET) { + if (type == AF_INET) { result_buf->h_length = sizeof(*in); #ifdef __UCLIBC_HAS_IPV6__ } else { result_buf->h_length = sizeof(*in6); #endif /* __UCLIBC_HAS_IPV6__ */ - } + } result_buf->h_addr_list = (char **) addr_list; result_buf->h_aliases = alias; @@ -2477,7 +2568,7 @@ int gethostbyaddr_r (const void *addr, socklen_t len, int type, } } - *result=result_buf; + *result = result_buf; *h_errnop = NETDB_SUCCESS; return NETDB_SUCCESS; } @@ -2493,13 +2584,13 @@ libc_hidden_def(gethostbyaddr_r) * Return size of compressed name or -1 if there was an error. */ int __dn_expand(const u_char *msg, const u_char *eom, const u_char *src, - char *dst, int dstsiz) + char *dst, int dstsiz) { int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz); if (n > 0 && dst[0] == '.') dst[0] = '\0'; - return (n); + return n; } #endif /* L_res_comp */ @@ -2513,7 +2604,7 @@ int __dn_expand(const u_char *msg, const u_char *eom, const u_char *src, */ static int printable(int ch) { - return (ch > 0x20 && ch < 0x7f); + return (ch > 0x20 && ch < 0x7f); } /* @@ -2525,18 +2616,18 @@ static int printable(int ch) */ static int special(int ch) { - switch (ch) { - case 0x22: /* '"' */ - case 0x2E: /* '.' */ - case 0x3B: /* ';' */ - case 0x5C: /* '\\' */ - /* Special modifiers in zone files. */ - case 0x40: /* '@' */ - case 0x24: /* '$' */ - return (1); - default: - return (0); - } + switch (ch) { + case 0x22: /* '"' */ + case 0x2E: /* '.' */ + case 0x3B: /* ';' */ + case 0x5C: /* '\\' */ + /* Special modifiers in zone files. */ + case 0x40: /* '@' */ + case 0x24: /* '$' */ + return 1; + default: + return 0; + } } /* @@ -2554,10 +2645,10 @@ int ns_name_uncompress(const u_char *msg, const u_char *eom, int n; if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1) - return (-1); + return -1; if (ns_name_ntop(tmp, dst, dstsiz) == -1) - return (-1); - return (n); + return -1; + return n; } libc_hidden_def(ns_name_uncompress) @@ -2585,33 +2676,33 @@ int ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) { while ((n = *cp++) != 0) { if ((n & NS_CMPRSFLGS) != 0) { /* Some kind of compression pointer. */ - __set_errno (EMSGSIZE); - return (-1); + __set_errno(EMSGSIZE); + return -1; } if (dn != dst) { if (dn >= eom) { - __set_errno (EMSGSIZE); - return (-1); + __set_errno(EMSGSIZE); + return -1; } *dn++ = '.'; } if (dn + n >= eom) { - __set_errno (EMSGSIZE); - return (-1); + __set_errno(EMSGSIZE); + return -1; } for ((void)NULL; n > 0; n--) { c = *cp++; if (special(c)) { if (dn + 1 >= eom) { - __set_errno (EMSGSIZE); - return (-1); + __set_errno(EMSGSIZE); + return -1; } *dn++ = '\\'; *dn++ = (char)c; } else if (!printable(c)) { if (dn + 3 >= eom) { - __set_errno (EMSGSIZE); - return (-1); + __set_errno(EMSGSIZE); + return -1; } *dn++ = '\\'; *dn++ = digits[c / 100]; @@ -2619,8 +2710,8 @@ int ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) { *dn++ = digits[c % 10]; } else { if (dn >= eom) { - __set_errno (EMSGSIZE); - return (-1); + __set_errno(EMSGSIZE); + return -1; } *dn++ = (char)c; } @@ -2628,17 +2719,17 @@ int ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) { } if (dn == dst) { if (dn >= eom) { - __set_errno (EMSGSIZE); - return (-1); + __set_errno(EMSGSIZE); + return -1; } *dn++ = '.'; } if (dn >= eom) { - __set_errno (EMSGSIZE); - return (-1); + __set_errno(EMSGSIZE); + return -1; } *dn++ = '\0'; - return (dn - dst); + return (dn - dst); } libc_hidden_def(ns_name_ntop) @@ -2661,59 +2752,60 @@ int ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src, srcp = src; dstlim = dst + dstsiz; if (srcp < msg || srcp >= eom) { - __set_errno (EMSGSIZE); - return (-1); + __set_errno(EMSGSIZE); + return -1; } /* Fetch next label in domain name. */ while ((n = *srcp++) != 0) { /* Check for indirection. */ switch (n & NS_CMPRSFLGS) { - case 0: - /* Limit checks. */ - if (dstp + n + 1 >= dstlim || srcp + n >= eom) { - __set_errno (EMSGSIZE); - return (-1); - } - checked += n + 1; - *dstp++ = n; - memcpy(dstp, srcp, n); - dstp += n; - srcp += n; - break; + case 0: + /* Limit checks. */ + if (dstp + n + 1 >= dstlim || srcp + n >= eom) { + __set_errno(EMSGSIZE); + return -1; + } + checked += n + 1; + *dstp++ = n; + memcpy(dstp, srcp, n); + dstp += n; + srcp += n; + break; - case NS_CMPRSFLGS: - if (srcp >= eom) { - __set_errno (EMSGSIZE); - return (-1); - } - if (len < 0) - len = srcp - src + 1; - srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff)); - if (srcp < msg || srcp >= eom) { /* Out of range. */ - __set_errno (EMSGSIZE); - return (-1); - } - checked += 2; - /* - * Check for loops in the compressed name; - * if we've looked at the whole message, - * there must be a loop. - */ - if (checked >= eom - msg) { - __set_errno (EMSGSIZE); - return (-1); - } - break; + case NS_CMPRSFLGS: + if (srcp >= eom) { + __set_errno(EMSGSIZE); + return -1; + } + if (len < 0) + len = srcp - src + 1; + srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff)); + if (srcp < msg || srcp >= eom) { /* Out of range. */ + __set_errno(EMSGSIZE); + return -1; + } + checked += 2; + /* + * Check for loops in the compressed name; + * if we've looked at the whole message, + * there must be a loop. + */ + if (checked >= eom - msg) { + __set_errno(EMSGSIZE); + return -1; + } + break; - default: - __set_errno (EMSGSIZE); - return (-1); /* flag error */ + default: + __set_errno(EMSGSIZE); + return -1; /* flag error */ } } *dstp = '\0'; if (len < 0) len = srcp - src; - return (len); + return len; } libc_hidden_def(ns_name_unpack) #endif /* L_ns_name */ +/* vi: set sw=4 ts=4: */ diff --git a/libc/inet/rpc/Makefile.in b/libc/inet/rpc/Makefile.in index af047c0df..2f1246b20 100644 --- a/libc/inet/rpc/Makefile.in +++ b/libc/inet/rpc/Makefile.in @@ -5,6 +5,8 @@ # Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. # +CFLAGS-rpc := -fno-strict-aliasing + ifneq ($(UCLIBC_HAS_FULL_RPC),y) # For now, only compile the stuff needed to do an NFS mount.... CSRC:= authunix_prot.c auth_none.c auth_unix.c bindresvport.c \ diff --git a/libc/inet/rpc/auth_unix.c b/libc/inet/rpc/auth_unix.c index 80b60b56b..0654b51c1 100644 --- a/libc/inet/rpc/auth_unix.c +++ b/libc/inet/rpc/auth_unix.c @@ -56,7 +56,7 @@ # include <wchar.h> #endif -libc_hidden_proto(memcpy) +/* Experimentally off - libc_hidden_proto(memcpy) */ libc_hidden_proto(sysconf) libc_hidden_proto(getegid) libc_hidden_proto(geteuid) @@ -212,8 +212,7 @@ authunix_create_default (void) list of groups to NGRPS members since the code in authuxprot.c transforms a fixed array. Grrr. */ ret_auth = authunix_create (machname, uid, gid, MIN (NGRPS, len), gids); - if (gids) - free (gids); + free (gids); return ret_auth; } libc_hidden_def(authunix_create_default) diff --git a/libc/inet/rpc/bindresvport.c b/libc/inet/rpc/bindresvport.c index 32055cdf6..3f266e6cc 100644 --- a/libc/inet/rpc/bindresvport.c +++ b/libc/inet/rpc/bindresvport.c @@ -40,7 +40,7 @@ #include <sys/socket.h> #include <netinet/in.h> -libc_hidden_proto(memset) +/* Experimentally off - libc_hidden_proto(memset) */ libc_hidden_proto(bind) libc_hidden_proto(getpid) diff --git a/libc/inet/rpc/clnt_generic.c b/libc/inet/rpc/clnt_generic.c index 899a9dd0e..54bd0e914 100644 --- a/libc/inet/rpc/clnt_generic.c +++ b/libc/inet/rpc/clnt_generic.c @@ -40,10 +40,10 @@ #include <sys/socket.h> #include <netdb.h> -libc_hidden_proto(memcpy) -libc_hidden_proto(memset) -libc_hidden_proto(strcmp) -libc_hidden_proto(strcpy) +/* Experimentally off - libc_hidden_proto(memcpy) */ +/* Experimentally off - libc_hidden_proto(memset) */ +/* Experimentally off - libc_hidden_proto(strcmp) */ +/* Experimentally off - libc_hidden_proto(strcpy) */ libc_hidden_proto(clnttcp_create) libc_hidden_proto(clntudp_create) libc_hidden_proto(clntunix_create) diff --git a/libc/inet/rpc/clnt_perror.c b/libc/inet/rpc/clnt_perror.c index ef3ed2d30..89a31a2d6 100644 --- a/libc/inet/rpc/clnt_perror.c +++ b/libc/inet/rpc/clnt_perror.c @@ -51,8 +51,8 @@ static char sccsid[] = "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro"; # define fputs(s, f) _IO_fputs (s, f) #endif -libc_hidden_proto(strcpy) -libc_hidden_proto(strlen) +/* Experimentally off - libc_hidden_proto(strcpy) */ +/* Experimentally off - libc_hidden_proto(strlen) */ libc_hidden_proto(sprintf) libc_hidden_proto(__glibc_strerror_r) libc_hidden_proto(fputs) diff --git a/libc/inet/rpc/clnt_simple.c b/libc/inet/rpc/clnt_simple.c index f9f18f3d4..745a50213 100644 --- a/libc/inet/rpc/clnt_simple.c +++ b/libc/inet/rpc/clnt_simple.c @@ -50,9 +50,9 @@ static char sccsid[] = "@(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro"; #include <netdb.h> #include <string.h> -libc_hidden_proto(memcpy) -libc_hidden_proto(strcmp) -libc_hidden_proto(strncpy) +/* Experimentally off - libc_hidden_proto(memcpy) */ +/* Experimentally off - libc_hidden_proto(strcmp) */ +/* Experimentally off - libc_hidden_proto(strncpy) */ libc_hidden_proto(close) libc_hidden_proto(clntudp_create) libc_hidden_proto(gethostbyname_r) diff --git a/libc/inet/rpc/clnt_udp.c b/libc/inet/rpc/clnt_udp.c index 9e3444069..1d7cb4863 100644 --- a/libc/inet/rpc/clnt_udp.c +++ b/libc/inet/rpc/clnt_udp.c @@ -62,7 +62,7 @@ libc_hidden_proto(fwprintf) #include <sys/uio.h> #endif -libc_hidden_proto(memcmp) +/* Experimentally off - libc_hidden_proto(memcmp) */ libc_hidden_proto(ioctl) libc_hidden_proto(socket) libc_hidden_proto(close) diff --git a/libc/inet/rpc/clnt_unix.c b/libc/inet/rpc/clnt_unix.c index 82f994e51..73be74288 100644 --- a/libc/inet/rpc/clnt_unix.c +++ b/libc/inet/rpc/clnt_unix.c @@ -63,8 +63,8 @@ libc_hidden_proto(fwprintf) #endif -libc_hidden_proto(strlen) -libc_hidden_proto(memcpy) +/* Experimentally off - libc_hidden_proto(strlen) */ +/* Experimentally off - libc_hidden_proto(memcpy) */ libc_hidden_proto(socket) libc_hidden_proto(close) libc_hidden_proto(getpid) diff --git a/libc/inet/rpc/create_xid.c b/libc/inet/rpc/create_xid.c index 3b90d7a48..e3ee4799b 100644 --- a/libc/inet/rpc/create_xid.c +++ b/libc/inet/rpc/create_xid.c @@ -31,14 +31,11 @@ libc_hidden_proto(gettimeofday) /* The RPC code is not threadsafe, but new code should be threadsafe. */ -#ifdef __UCLIBC_HAS_THREADS__ -# include <pthread.h> -static pthread_mutex_t createxid_lock = PTHREAD_MUTEX_INITIALIZER; -#endif -#define LOCK __pthread_mutex_lock(&createxid_lock) -#define UNLOCK __pthread_mutex_unlock(&createxid_lock) - -static int is_initialized; +#include <bits/uClibc_mutex.h> +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER); + + +static smallint is_initialized; static struct drand48_data __rpc_lrand48_data; u_long _create_xid (void) attribute_hidden; @@ -46,7 +43,7 @@ u_long _create_xid (void) { unsigned long res; - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); if (!is_initialized) { @@ -59,7 +56,7 @@ u_long _create_xid (void) lrand48_r (&__rpc_lrand48_data, &res); - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); return res; } diff --git a/libc/inet/rpc/getrpcent.c b/libc/inet/rpc/getrpcent.c index 428fbcc31..186bd130f 100644 --- a/libc/inet/rpc/getrpcent.c +++ b/libc/inet/rpc/getrpcent.c @@ -7,23 +7,23 @@ * may copy or modify Sun RPC without charge, but are not authorized * to license or distribute it to anyone else except as part of a product or * program developed by the user. - * + * * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. - * + * * Sun RPC is provided with no support and without any obligation on the * part of Sun Microsystems, Inc. to assist in its use, correction, * modification or enhancement. - * + * * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC * OR ANY PART THEREOF. - * + * * In no event will Sun Microsystems, Inc. be liable for any lost revenue * or profits or other special, indirect and consequential damages, even if * Sun has been advised of the possibility of such damages. - * + * * Sun Microsystems, Inc. * 2550 Garcia Avenue * Mountain View, California 94043 @@ -44,11 +44,11 @@ #include <arpa/inet.h> #include <errno.h> -libc_hidden_proto(memcpy) -libc_hidden_proto(memset) -libc_hidden_proto(strchr) -libc_hidden_proto(strcmp) -libc_hidden_proto(strlen) +/* Experimentally off - libc_hidden_proto(memcpy) */ +/* Experimentally off - libc_hidden_proto(memset) */ +/* Experimentally off - libc_hidden_proto(strchr) */ +/* Experimentally off - libc_hidden_proto(strcmp) */ +/* Experimentally off - libc_hidden_proto(strlen) */ libc_hidden_proto(fopen) libc_hidden_proto(fclose) libc_hidden_proto(atoi) @@ -93,10 +93,8 @@ void endrpcent(void) return; if (d->stayopen) return; - if (d->current) { - free(d->current); - d->current = NULL; - } + free(d->current); + d->current = NULL; if (d->rpcf) { fclose(d->rpcf); d->rpcf = NULL; @@ -115,8 +113,7 @@ void setrpcent(int f) d->rpcf = fopen(RPCDB, "r"); else rewind(d->rpcf); - if (d->current) - free(d->current); + free(d->current); d->current = NULL; d->stayopen |= f; } @@ -275,14 +272,11 @@ static struct rpcent *interpret(register struct rpcdata *d) #if defined(__UCLIBC_HAS_REENTRANT_RPC__) -#if defined(__UCLIBC_HAS_THREADS__) -# include <pthread.h> -static pthread_mutex_t rpcdata_lock = PTHREAD_MUTEX_INITIALIZER; -#endif -#define LOCK __pthread_mutex_lock(&rpcdata_lock) -#define UNLOCK __pthread_mutex_unlock(&rpcdata_lock) +#include <bits/uClibc_mutex.h> +__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER); + -static int __copy_rpcent(struct rpcent *r, struct rpcent *result_buf, char *buffer, +static int __copy_rpcent(struct rpcent *r, struct rpcent *result_buf, char *buffer, size_t buflen, struct rpcent **result) { size_t i, s; @@ -298,7 +292,7 @@ static int __copy_rpcent(struct rpcent *r, struct rpcent *result_buf, char *buff result_buf->r_number = r->r_number; - /* copy the aliases ... need to not only copy the alias strings, + /* copy the aliases ... need to not only copy the alias strings, * but the array of pointers to the alias strings */ i = 0; while (r->r_aliases[i++]) ; @@ -339,9 +333,9 @@ int getrpcbynumber_r(int number, struct rpcent *result_buf, char *buffer, size_t buflen, struct rpcent **result) { int ret; - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); ret = __copy_rpcent(getrpcbynumber(number), result_buf, buffer, buflen, result); - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); return ret; } @@ -349,19 +343,19 @@ int getrpcbyname_r(const char *name, struct rpcent *result_buf, char *buffer, size_t buflen, struct rpcent **result) { int ret; - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); ret = __copy_rpcent(getrpcbyname(name), result_buf, buffer, buflen, result); - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); return ret; } -int getrpcent_r(struct rpcent *result_buf, char *buffer, +int getrpcent_r(struct rpcent *result_buf, char *buffer, size_t buflen, struct rpcent **result) { int ret; - LOCK; + __UCLIBC_MUTEX_LOCK(mylock); ret = __copy_rpcent(getrpcent(), result_buf, buffer, buflen, result); - UNLOCK; + __UCLIBC_MUTEX_UNLOCK(mylock); return ret; } diff --git a/libc/inet/rpc/getrpcport.c b/libc/inet/rpc/getrpcport.c index 82fa8d17a..0a57d1da9 100644 --- a/libc/inet/rpc/getrpcport.c +++ b/libc/inet/rpc/getrpcport.c @@ -48,7 +48,7 @@ static char sccsid[] = "@(#)getrpcport.c 1.3 87/08/11 SMI"; #include <rpc/pmap_clnt.h> #include <sys/socket.h> -libc_hidden_proto(memcpy) +/* Experimentally off - libc_hidden_proto(memcpy) */ libc_hidden_proto(pmap_getport) libc_hidden_proto(gethostbyname_r) diff --git a/libc/inet/rpc/pmap_rmt.c b/libc/inet/rpc/pmap_rmt.c index a0aa22985..759534aa7 100644 --- a/libc/inet/rpc/pmap_rmt.c +++ b/libc/inet/rpc/pmap_rmt.c @@ -59,7 +59,7 @@ static char sccsid[] = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro"; #include <arpa/inet.h> #define MAX_BROADCAST_SIZE 1400 -libc_hidden_proto(memset) +/* Experimentally off - libc_hidden_proto(memset) */ libc_hidden_proto(ioctl) libc_hidden_proto(perror) libc_hidden_proto(socket) diff --git a/libc/inet/rpc/rcmd.c b/libc/inet/rpc/rcmd.c index ccab7d60a..fe00d6a78 100644 --- a/libc/inet/rpc/rcmd.c +++ b/libc/inet/rpc/rcmd.c @@ -87,14 +87,14 @@ static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; #endif #include <sys/uio.h> -libc_hidden_proto(memcmp) -libc_hidden_proto(strcat) -libc_hidden_proto(strchr) -libc_hidden_proto(strcmp) -libc_hidden_proto(strcpy) -libc_hidden_proto(strlen) -libc_hidden_proto(strncmp) -libc_hidden_proto(memmove) +/* Experimentally off - libc_hidden_proto(memcmp) */ +/* Experimentally off - libc_hidden_proto(strcat) */ +/* Experimentally off - libc_hidden_proto(strchr) */ +/* Experimentally off - libc_hidden_proto(strcmp) */ +/* Experimentally off - libc_hidden_proto(strcpy) */ +/* Experimentally off - libc_hidden_proto(strlen) */ +/* Experimentally off - libc_hidden_proto(strncmp) */ +/* Experimentally off - libc_hidden_proto(memmove) */ libc_hidden_proto(getpid) libc_hidden_proto(socket) libc_hidden_proto(close) @@ -134,7 +134,7 @@ libc_hidden_proto(__h_errno_location) #ifdef __UCLIBC_HAS_XLOCALE__ libc_hidden_proto(__ctype_b_loc) libc_hidden_proto(__ctype_tolower_loc) -#elif __UCLIBC_HAS_CTYPE_TABLES__ +#elif defined __UCLIBC_HAS_CTYPE_TABLES__ libc_hidden_proto(__ctype_b) libc_hidden_proto(__ctype_tolower) #endif @@ -197,9 +197,7 @@ int rcmd(ahost, rport, locuser, remuser, cmd, fd2p) #ifdef __ARCH_USE_MMU__ tmphstbuf = alloca (hstbuflen); #else - if (tmphstbuf) { - free(tmphstbuf); - } + free(tmphstbuf); tmphstbuf = malloc (hstbuflen); #endif } @@ -411,9 +409,7 @@ int ruserok(rhost, superuser, ruser, luser) #ifdef __ARCH_USE_MMU__ buffer = alloca (buflen); #else - if (buffer) { - free(buffer); - } + free(buffer); buffer = malloc (buflen); #endif } @@ -780,8 +776,7 @@ __ivaliduser2(hostf, raddr, luser, ruser, rhost) } } - if (buf != NULL) - free (buf); + free (buf); return retval; } diff --git a/libc/inet/rpc/rexec.c b/libc/inet/rpc/rexec.c index bbbaac645..2eb567ed2 100644 --- a/libc/inet/rpc/rexec.c +++ b/libc/inet/rpc/rexec.c @@ -42,9 +42,9 @@ #include <string.h> #include <unistd.h> -libc_hidden_proto(memset) -libc_hidden_proto(strlen) -libc_hidden_proto(strncpy) +/* Experimentally off - libc_hidden_proto(memset) */ +/* Experimentally off - libc_hidden_proto(strlen) */ +/* Experimentally off - libc_hidden_proto(strncpy) */ libc_hidden_proto(read) libc_hidden_proto(write) libc_hidden_proto(close) @@ -61,14 +61,13 @@ libc_hidden_proto(atoi) libc_hidden_proto(connect) libc_hidden_proto(accept) libc_hidden_proto(listen) +libc_hidden_proto(ruserpass) #define SA_LEN(_x) __libc_sa_len((_x)->sa_family) extern int __libc_sa_len (sa_family_t __af) __THROW attribute_hidden; int rexecoptions; char ahostbuf[NI_MAXHOST] attribute_hidden; -extern int ruserpass(const char *host, const char **aname, const char **apass) attribute_hidden; -libc_hidden_proto(ruserpass) libc_hidden_proto(rexec_af) int @@ -87,7 +86,7 @@ rexec_af(char **ahost, int rport, const char *name, const char *pass, const char snprintf(servbuff, sizeof(servbuff), "%d", ntohs(rport)); servbuff[sizeof(servbuff) - 1] = '\0'; - memset(&hints, 0, sizeof(hints)); + memset(&hints, '\0', sizeof(hints)); hints.ai_family = af; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_CANONNAME; @@ -104,6 +103,8 @@ rexec_af(char **ahost, int rport, const char *name, const char *pass, const char } else{ *ahost = NULL; + __set_errno (ENOENT); + return -1; } ruserpass(res0->ai_canonname, &name, &pass); retry: @@ -127,7 +128,8 @@ retry: port = 0; } else { char num[32]; - int s2, sa2len; + int s2; + socklen_t sa2len; s2 = socket(res0->ai_family, res0->ai_socktype, 0); if (s2 < 0) { @@ -153,7 +155,8 @@ retry: (void) sprintf(num, "%u", port); (void) write(s, num, strlen(num)+1); { socklen_t len = sizeof (from); - s3 = accept(s2, (struct sockaddr *)&from, &len); + s3 = TEMP_FAILURE_RETRY (accept(s2, (struct sockaddr *)&from, + &len)); close(s2); if (s3 < 0) { perror("accept"); diff --git a/libc/inet/rpc/rpc_cmsg.c b/libc/inet/rpc/rpc_cmsg.c index b47a67c71..3206aae55 100644 --- a/libc/inet/rpc/rpc_cmsg.c +++ b/libc/inet/rpc/rpc_cmsg.c @@ -45,7 +45,7 @@ static char sccsid[] = "@(#)rpc_callmsg.c 1.4 87/08/11 Copyr 1984 Sun Micro"; #include <sys/param.h> #include <rpc/rpc.h> -libc_hidden_proto(memcpy) +/* Experimentally off - libc_hidden_proto(memcpy) */ libc_hidden_proto(xdr_enum) libc_hidden_proto(xdr_opaque) libc_hidden_proto(xdr_u_int) diff --git a/libc/inet/rpc/rpc_prot.c b/libc/inet/rpc/rpc_prot.c index ebe6c3128..74658e654 100644 --- a/libc/inet/rpc/rpc_prot.c +++ b/libc/inet/rpc/rpc_prot.c @@ -89,7 +89,6 @@ xdr_des_block (XDR *xdrs, des_block *blkp) /* * XDR the MSG_ACCEPTED part of a reply message union */ -extern bool_t xdr_accepted_reply (XDR *xdrs, struct accepted_reply *ar); libc_hidden_proto(xdr_accepted_reply) bool_t xdr_accepted_reply (XDR *xdrs, struct accepted_reply *ar) @@ -117,7 +116,6 @@ libc_hidden_def(xdr_accepted_reply) /* * XDR the MSG_DENIED part of a reply message union */ -extern bool_t xdr_rejected_reply (XDR *xdrs, struct rejected_reply *rr); libc_hidden_proto(xdr_rejected_reply) bool_t xdr_rejected_reply (XDR *xdrs, struct rejected_reply *rr) diff --git a/libc/inet/rpc/rpc_thread.c b/libc/inet/rpc/rpc_thread.c index b42fab6e0..2c7b8c1ea 100644 --- a/libc/inet/rpc/rpc_thread.c +++ b/libc/inet/rpc/rpc_thread.c @@ -18,7 +18,6 @@ libc_hidden_proto(__rpc_thread_svc_max_pollfd) #ifdef __UCLIBC_HAS_THREADS__ #include <bits/libc-tsd.h> -#include <bits/libc-lock.h> /* Variable used in non-threaded applications or for the first thread. */ static struct rpc_thread_variables __libc_tsd_RPC_VARS_mem; @@ -72,7 +71,7 @@ __rpc_thread_variables (void) if (tvp != NULL) __libc_tsd_set (RPC_VARS, tvp); else - tvp = __libc_tsd_RPC_VARS; + tvp = __libc_tsd_get (RPC_VARS); } } return tvp; diff --git a/libc/inet/rpc/ruserpass.c b/libc/inet/rpc/ruserpass.c index 97f1284b3..ad6e703c4 100644 --- a/libc/inet/rpc/ruserpass.c +++ b/libc/inet/rpc/ruserpass.c @@ -42,13 +42,13 @@ #include <string.h> #include <unistd.h> -libc_hidden_proto(strcat) -libc_hidden_proto(strchr) -libc_hidden_proto(strcmp) -libc_hidden_proto(strcpy) -libc_hidden_proto(strlen) -libc_hidden_proto(strcasecmp) -libc_hidden_proto(strncasecmp) +/* Experimentally off - libc_hidden_proto(strcat) */ +/* Experimentally off - libc_hidden_proto(strchr) */ +/* Experimentally off - libc_hidden_proto(strcmp) */ +/* Experimentally off - libc_hidden_proto(strcpy) */ +/* Experimentally off - libc_hidden_proto(strlen) */ +/* Experimentally off - libc_hidden_proto(strcasecmp) */ +/* Experimentally off - libc_hidden_proto(strncasecmp) */ libc_hidden_proto(getenv) libc_hidden_proto(printf) libc_hidden_proto(fstat) @@ -112,8 +112,8 @@ static const struct toktab { }; - -extern int ruserpass(const char *host, const char **aname, const char **apass); +/* ruserpass - remote password check. + This function also exists in glibc but is undocumented */ libc_hidden_proto(ruserpass) int ruserpass(const char *host, const char **aname, const char **apass) { diff --git a/libc/inet/rpc/svc.c b/libc/inet/rpc/svc.c index d41d7644c..253c06379 100644 --- a/libc/inet/rpc/svc.c +++ b/libc/inet/rpc/svc.c @@ -46,7 +46,7 @@ #include <rpc/pmap_clnt.h> #include <sys/poll.h> -libc_hidden_proto(ffs) +/* Experimentally off - libc_hidden_proto(ffs) */ libc_hidden_proto(pmap_set) libc_hidden_proto(pmap_unset) libc_hidden_proto(_authenticate) diff --git a/libc/inet/rpc/svc_authux.c b/libc/inet/rpc/svc_authux.c index ef8476784..64c911f93 100644 --- a/libc/inet/rpc/svc_authux.c +++ b/libc/inet/rpc/svc_authux.c @@ -46,7 +46,7 @@ #include <rpc/rpc.h> #include <rpc/svc.h> -libc_hidden_proto(memcpy) +/* Experimentally off - libc_hidden_proto(memcpy) */ libc_hidden_proto(printf) libc_hidden_proto(xdrmem_create) libc_hidden_proto(xdr_authunix_parms) diff --git a/libc/inet/rpc/svc_simple.c b/libc/inet/rpc/svc_simple.c index 075b930b1..72c272aa7 100644 --- a/libc/inet/rpc/svc_simple.c +++ b/libc/inet/rpc/svc_simple.c @@ -55,8 +55,8 @@ static char sccsid[] = "@(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro"; # define fputs(s, f) _IO_fputs (s, f) #endif -libc_hidden_proto(strdup) -libc_hidden_proto(memset) +/* Experimentally off - libc_hidden_proto(strdup) */ +/* Experimentally off - libc_hidden_proto(memset) */ libc_hidden_proto(asprintf) libc_hidden_proto(fputs) libc_hidden_proto(write) diff --git a/libc/inet/rpc/svc_tcp.c b/libc/inet/rpc/svc_tcp.c index 035119d7e..5e4667940 100644 --- a/libc/inet/rpc/svc_tcp.c +++ b/libc/inet/rpc/svc_tcp.c @@ -59,8 +59,8 @@ static char sccsid[] = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro"; # define fputs(s, f) _IO_fputs (s, f) #endif -libc_hidden_proto(memset) -libc_hidden_proto(memcpy) +/* Experimentally off - libc_hidden_proto(memset) */ +/* Experimentally off - libc_hidden_proto(memcpy) */ libc_hidden_proto(socket) libc_hidden_proto(close) libc_hidden_proto(read) diff --git a/libc/inet/rpc/svc_udp.c b/libc/inet/rpc/svc_udp.c index d2ed68d1b..e2c97bdc5 100644 --- a/libc/inet/rpc/svc_udp.c +++ b/libc/inet/rpc/svc_udp.c @@ -60,9 +60,9 @@ static char sccsid[] = "@(#)svc_udp.c 1.24 87/08/11 Copyr 1984 Sun Micro"; libc_hidden_proto(fwprintf) #endif -libc_hidden_proto(memcmp) -libc_hidden_proto(memcpy) -libc_hidden_proto(memset) +/* Experimentally off - libc_hidden_proto(memcmp) */ +/* Experimentally off - libc_hidden_proto(memcpy) */ +/* Experimentally off - libc_hidden_proto(memset) */ libc_hidden_proto(perror) libc_hidden_proto(socket) libc_hidden_proto(close) diff --git a/libc/inet/rpc/svc_unix.c b/libc/inet/rpc/svc_unix.c index 1683bd3ba..2d7790450 100644 --- a/libc/inet/rpc/svc_unix.c +++ b/libc/inet/rpc/svc_unix.c @@ -55,9 +55,9 @@ # include <wchar.h> #endif -libc_hidden_proto(memcpy) -libc_hidden_proto(memset) -libc_hidden_proto(strlen) +/* Experimentally off - libc_hidden_proto(memcpy) */ +/* Experimentally off - libc_hidden_proto(memset) */ +/* Experimentally off - libc_hidden_proto(strlen) */ libc_hidden_proto(socket) libc_hidden_proto(close) libc_hidden_proto(perror) diff --git a/libc/inet/rpc/xdr.c b/libc/inet/rpc/xdr.c index e8009910f..54e716176 100644 --- a/libc/inet/rpc/xdr.c +++ b/libc/inet/rpc/xdr.c @@ -56,7 +56,7 @@ static char sccsid[] = "@(#)xdr.c 1.35 87/08/12"; libc_hidden_proto(fwprintf) #endif -libc_hidden_proto(strlen) +/* Experimentally off - libc_hidden_proto(strlen) */ libc_hidden_proto(fputs) /* diff --git a/libc/inet/rpc/xdr_array.c b/libc/inet/rpc/xdr_array.c index 5f21fb29e..a2299b683 100644 --- a/libc/inet/rpc/xdr_array.c +++ b/libc/inet/rpc/xdr_array.c @@ -54,7 +54,7 @@ static char sccsid[] = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro"; libc_hidden_proto(fwprintf) #endif -libc_hidden_proto(memset) +/* Experimentally off - libc_hidden_proto(memset) */ libc_hidden_proto(fputs) libc_hidden_proto(xdr_u_int) diff --git a/libc/inet/rpc/xdr_mem.c b/libc/inet/rpc/xdr_mem.c index 7613ffc2f..b7410c404 100644 --- a/libc/inet/rpc/xdr_mem.c +++ b/libc/inet/rpc/xdr_mem.c @@ -43,7 +43,7 @@ #include <limits.h> #include <rpc/rpc.h> -libc_hidden_proto(memcpy) +/* Experimentally off - libc_hidden_proto(memcpy) */ static bool_t xdrmem_getlong (XDR *, long *); static bool_t xdrmem_putlong (XDR *, const long *); diff --git a/libc/inet/rpc/xdr_rec.c b/libc/inet/rpc/xdr_rec.c index 5f9e2b1fe..ac331c4d3 100644 --- a/libc/inet/rpc/xdr_rec.c +++ b/libc/inet/rpc/xdr_rec.c @@ -60,7 +60,7 @@ libc_hidden_proto(fwprintf) #endif -libc_hidden_proto(memcpy) +/* Experimentally off - libc_hidden_proto(memcpy) */ libc_hidden_proto(fputs) libc_hidden_proto(lseek) diff --git a/libc/inet/rpc/xdr_reference.c b/libc/inet/rpc/xdr_reference.c index 7201def7a..6282f9cb1 100644 --- a/libc/inet/rpc/xdr_reference.c +++ b/libc/inet/rpc/xdr_reference.c @@ -55,7 +55,7 @@ static char sccsid[] = "@(#)xdr_reference.c 1.11 87/08/11 SMI"; libc_hidden_proto(fwprintf) #endif -libc_hidden_proto(memset) +/* Experimentally off - libc_hidden_proto(memset) */ libc_hidden_proto(xdr_bool) libc_hidden_proto(fputs) diff --git a/libc/inet/socketcalls.c b/libc/inet/socketcalls.c index d684a3aaf..9211b01ce 100644 --- a/libc/inet/socketcalls.c +++ b/libc/inet/socketcalls.c @@ -193,7 +193,8 @@ libc_hidden_def(listen) extern __typeof(recv) __libc_recv; #ifdef __NR_recv #define __NR___libc_recv __NR_recv -_syscall4(ssize_t, __libc_recv, int, sockfd, __ptr_t, buffer, size_t, len, int, flags); +_syscall4(ssize_t, __libc_recv, int, sockfd, __ptr_t, buffer, size_t, len, + int, flags); #elif defined(__NR_socketcall) /* recv, recvfrom added by bir7@leland.stanford.edu */ ssize_t __libc_recv(int sockfd, __ptr_t buffer, size_t len, int flags) @@ -231,8 +232,8 @@ libc_hidden_weak(recv) extern __typeof(recvfrom) __libc_recvfrom; #ifdef __NR_recvfrom #define __NR___libc_recvfrom __NR_recvfrom -_syscall6(ssize_t, __libc_recvfrom, int, sockfd, __ptr_t, buffer, size_t, len, int, flags, - struct sockaddr *, to, socklen_t *, tolen); +_syscall6(ssize_t, __libc_recvfrom, int, sockfd, __ptr_t, buffer, size_t, len, + int, flags, struct sockaddr *, to, socklen_t *, tolen); #elif defined(__NR_socketcall) /* recv, recvfrom added by bir7@leland.stanford.edu */ ssize_t __libc_recvfrom(int sockfd, __ptr_t buffer, size_t len, int flags, @@ -365,8 +366,8 @@ libc_hidden_weak(sendmsg) extern __typeof(sendto) __libc_sendto; #ifdef __NR_sendto #define __NR___libc_sendto __NR_sendto -_syscall6(ssize_t, __libc_sendto, int, sockfd, const void *, buffer, size_t, len, - int, flags, const struct sockaddr *, to, socklen_t, tolen); +_syscall6(ssize_t, __libc_sendto, int, sockfd, const void *, buffer, + size_t, len, int, flags, const struct sockaddr *, to, socklen_t, tolen); #elif defined(__NR_socketcall) /* send, sendto added by bir7@leland.stanford.edu */ ssize_t __libc_sendto(int sockfd, const void *buffer, size_t len, int flags, |
