OSDN Git Service

Merge commit 'origin/master' into nptl
[uclinux-h8/uClibc.git] / libc / inet / getaddrinfo.c
index 41abbe2..8c7f7b9 100644 (file)
@@ -56,6 +56,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <assert.h>
 #include <errno.h>
 #include <netdb.h>
+#include <tls.h>
 #include <resolv.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -70,28 +71,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include <net/if.h>
 #include <ifaddrs.h>
 
-/* Experimentally off - libc_hidden_proto(memcpy) */
-/* Experimentally off - libc_hidden_proto(memset) */
-/* libc_hidden_proto(strcmp) */
-/* libc_hidden_proto(stpcpy) */
-/* 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) */
-/* libc_hidden_proto(gethostbyname2_r) */
-/* libc_hidden_proto(gethostbyaddr_r) */
-/* libc_hidden_proto(inet_pton) */
-/* libc_hidden_proto(inet_ntop) */
-/* libc_hidden_proto(strtoul) */
-/* libc_hidden_proto(if_nametoindex) */
-/* libc_hidden_proto(__h_errno_location) */
-/* libc_hidden_proto(uname) */
-#ifdef __UCLIBC_HAS_IPV6__
-/* libc_hidden_proto(in6addr_loopback) */
-#endif
-
 #define GAIH_OKIFUNSPEC 0x0100
 #define GAIH_EAI        ~(GAIH_OKIFUNSPEC)
 
@@ -169,45 +148,51 @@ struct gaih {
 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.  */
+
+       struct ifaddrs *ifa;
+       struct ifaddrs *runp;
+
+       /* Get the interface list via getifaddrs.  */
+       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__ */
+               seen |= SEEN_IPV4;
+#endif
 #if defined __UCLIBC_HAS_IPV6__
-                       seen |= SEEN_IPV6;
-#endif /* __UCLIBC_HAS_IPV6__ */
-                       return seen;
-               }
+               seen |= SEEN_IPV6;
+#endif
+               return seen;
+       }
 
-               for (runp = ifa; runp != NULL; runp = runp->ifa_next)
+       for (runp = ifa; runp != NULL; runp = runp->ifa_next) {
+               if (runp->ifa_addr == NULL)
+                       continue;
 #if defined __UCLIBC_HAS_IPV4__
-                       if (runp->ifa_addr->sa_family == PF_INET)
-                               seen |= SEEN_IPV4;
-#endif /* __UCLIBC_HAS_IPV4__ */
+               if (runp->ifa_addr->sa_family == PF_INET)
+                       seen |= SEEN_IPV4;
+#endif
 #if defined __UCLIBC_HAS_IPV6__
-                       if (runp->ifa_addr->sa_family == PF_INET6)
-                               seen |= SEEN_IPV6;
-#endif /* __UCLIBC_HAS_IPV6__ */
-
-               freeifaddrs(ifa);
+               if (runp->ifa_addr->sa_family == PF_INET6)
+                       seen |= SEEN_IPV6;
+#endif
        }
+       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__ */
+#endif
 #if defined __UCLIBC_HAS_IPV6__
        seen |= SEEN_IPV6;
-#endif /* __UCLIBC_HAS_IPV6__ */
+#endif
 
 #endif /* __UCLIBC_SUPPORT_AI_ADDRCONFIG__ */
+
        return seen;
 }
 
@@ -264,52 +249,51 @@ gaih_local(const char *name, const struct gaih_service *service,
                    strcmp(name, "unix") &&
                    strcmp(name, utsname.nodename))
                        return (GAIH_OKIFUNSPEC | -EAI_NONAME);
-               }
+       }
 
-               if (req->ai_protocol || req->ai_socktype) {
-                       const struct gaih_typeproto *tp = gaih_inet_typeproto + 1;
+       if (req->ai_protocol || req->ai_socktype) {
+               const struct gaih_typeproto *tp = gaih_inet_typeproto + 1;
 
-                       while (tp->name[0]
-                           && ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0
-                              || (req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
-                              || (req->ai_protocol != 0 && !(tp->protoflag & GAI_PROTO_PROTOANY) && req->ai_protocol != tp->protocol))
-                       ) {
-                               ++tp;
-                       }
-                       if (! tp->name[0]) {
-                               if (req->ai_socktype)
-                                       return (GAIH_OKIFUNSPEC | -EAI_SOCKTYPE);
-                               return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
-                       }
+               while (tp->name[0]
+                   && ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0
+                      || (req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
+                      || (req->ai_protocol != 0 && !(tp->protoflag & GAI_PROTO_PROTOANY) && req->ai_protocol != tp->protocol))
+               ) {
+                       ++tp;
+               }
+               if (! tp->name[0]) {
+                       if (req->ai_socktype)
+                               return (GAIH_OKIFUNSPEC | -EAI_SOCKTYPE);
+                       return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
                }
+       }
 
-               *pai = ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_un)
-                               + ((req->ai_flags & AI_CANONNAME)
-                               ? (strlen(utsname.nodename) + 1) : 0));
-               if (ai == NULL)
-                       return -EAI_MEMORY;
-
-               ai->ai_next = NULL;
-               ai->ai_flags = req->ai_flags;
-               ai->ai_family = AF_LOCAL;
-               ai->ai_socktype = req->ai_socktype ? req->ai_socktype : SOCK_STREAM;
-               ai->ai_protocol = req->ai_protocol;
-               ai->ai_addrlen = sizeof(struct sockaddr_un);
-               ai->ai_addr = (void *)ai + sizeof(struct addrinfo);
+       *pai = ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_un)
+                       + ((req->ai_flags & AI_CANONNAME)
+                       ? (strlen(utsname.nodename) + 1) : 0));
+       if (ai == NULL)
+               return -EAI_MEMORY;
+
+       ai->ai_next = NULL;
+       ai->ai_flags = req->ai_flags;
+       ai->ai_family = AF_LOCAL;
+       ai->ai_socktype = req->ai_socktype ? req->ai_socktype : SOCK_STREAM;
+       ai->ai_protocol = req->ai_protocol;
+       ai->ai_addrlen = sizeof(struct sockaddr_un);
+       ai->ai_addr = (void *)ai + sizeof(struct addrinfo);
 #if SALEN
-               ((struct sockaddr_un *)ai->ai_addr)->sun_len = sizeof(struct sockaddr_un);
+       ((struct sockaddr_un *)ai->ai_addr)->sun_len = sizeof(struct sockaddr_un);
 #endif /* SALEN */
 
-               ((struct sockaddr_un *)ai->ai_addr)->sun_family = AF_LOCAL;
-               memset(((struct sockaddr_un *)ai->ai_addr)->sun_path, 0, UNIX_PATH_MAX);
-
-               if (service) {
-                       struct sockaddr_un *sunp = (struct sockaddr_un *)ai->ai_addr;
+       ((struct sockaddr_un *)ai->ai_addr)->sun_family = AF_LOCAL;
+       memset(((struct sockaddr_un *)ai->ai_addr)->sun_path, 0, UNIX_PATH_MAX);
 
-                       if (strchr(service->name, '/') != NULL) {
-                               if (strlen(service->name) >= sizeof(sunp->sun_path))
-                                       return GAIH_OKIFUNSPEC | -EAI_SERVICE;
+       if (service) {
+               struct sockaddr_un *sunp = (struct sockaddr_un *)ai->ai_addr;
 
+               if (strchr(service->name, '/') != NULL) {
+                       if (strlen(service->name) >= sizeof(sunp->sun_path))
+                               return GAIH_OKIFUNSPEC | -EAI_SERVICE;
                        strcpy(sunp->sun_path, service->name);
                } else {
                        if (strlen(P_tmpdir "/") + 1 + strlen(service->name) >= sizeof(sunp->sun_path))
@@ -324,7 +308,7 @@ gaih_local(const char *name, const struct gaih_service *service,
                char *buf = ((struct sockaddr_un *)ai->ai_addr)->sun_path;
 
                if (__path_search(buf, L_tmpnam, NULL, NULL, 0) != 0
-                || __gen_tempname(buf, __GT_NOCREATE) != 0
+                || __gen_tempname(buf, __GT_NOCREATE, 0) != 0
                ) {
                        return -EAI_SYSTEM;
                }
@@ -539,7 +523,8 @@ gaih_inet(const char *name, const struct gaih_service *service,
 
                                if (scope_delim != NULL) {
                                        int try_numericscope = 0;
-                                       if (IN6_IS_ADDR_LINKLOCAL(at->addr) || IN6_IS_ADDR_MC_LINKLOCAL(at->addr)) {
+                                       uint32_t *a32 = (uint32_t*)at->addr;
+                                       if (IN6_IS_ADDR_LINKLOCAL(a32) || IN6_IS_ADDR_MC_LINKLOCAL(at->addr)) {
                                                at->scopeid = if_nametoindex(scope_delim + 1);
                                                if (at->scopeid == 0)
                                                        try_numericscope = 1;
@@ -617,8 +602,10 @@ gaih_inet(const char *name, const struct gaih_service *service,
 #endif
                if (req->ai_family == 0 || req->ai_family == AF_INET) {
                        atr->family = AF_INET;
-                       if ((req->ai_flags & AI_PASSIVE) == 0)
-                               *(uint32_t*)atr->addr = htonl(INADDR_LOOPBACK);
+                       if ((req->ai_flags & AI_PASSIVE) == 0) {
+                               uint32_t *a = (uint32_t*)atr->addr;
+                               *a = htonl(INADDR_LOOPBACK);
+                       }
                }
        }
 
@@ -649,8 +636,6 @@ gaih_inet(const char *name, const struct gaih_service *service,
                                do {
                                        tmpbuflen *= 2;
                                        tmpbuf = alloca(tmpbuflen);
-                                       //if (tmpbuf == NULL)
-                                       //      return -EAI_MEMORY;
                                        rc = gethostbyaddr_r(at2->addr,
                                                ((at2->family == AF_INET6)
                                                 ? sizeof(struct in6_addr)
@@ -711,9 +696,9 @@ gaih_inet(const char *name, const struct gaih_service *service,
                                (*pai)->ai_protocol = st2->protocol;
                                (*pai)->ai_addrlen = socklen;
                                (*pai)->ai_addr = (void *) (*pai) + sizeof(struct addrinfo);
-#if SALEN
+#if defined SALEN
                                (*pai)->ai_addr->sa_len = socklen;
-#endif /* SALEN */
+#endif
                                (*pai)->ai_addr->sa_family = family;
 
 #if defined __UCLIBC_HAS_IPV6__
@@ -775,7 +760,6 @@ static const struct gaih gaih[] = {
        { PF_UNSPEC, NULL }
 };
 
-/* libc_hidden_proto(freeaddrinfo) */
 void
 freeaddrinfo(struct addrinfo *ai)
 {
@@ -789,7 +773,6 @@ freeaddrinfo(struct addrinfo *ai)
 }
 libc_hidden_def(freeaddrinfo)
 
-/* libc_hidden_proto(getaddrinfo) */
 int
 getaddrinfo(const char *name, const char *service,
             const struct addrinfo *hints, struct addrinfo **pai)