svn commit: trunk/uClibc: extra/Configs include libc/inet

Ricard Wanderlof ricard.wanderlof at axis.com
Mon Jun 30 11:15:25 UTC 2008


On Fri, 27 Jun 2008, Bernhard Fischer wrote:

> In the end this addrconfig thing just tries to
> static int addrconfig (sa_family_t af)
> {
>        int tmp, ret, saved_errno = errno;
>
>        tmp = __check_pf();
> #ifdef __UCLIBC_HAS_IPV4__
>        if (af == AF_INET)
>                ret = tmp & SAW_IPv4;
>        else
> #endif
> #ifdef __UCLIBC_HAS_IPV6__
>        if (af == AF_INET6)
>                ret = tmp & SAW_IPv6;
>        else
> #endif
>        {
>                tmp = socket (af, SOCK_DGRAM, 0);
>                ret = 1; /* Assume PF_UNIX.  */
>                if (tmp < 0) {
>                        if (errno != EMFILE)
>                                ret = 0;
>                } else
>                        close (tmp);
>        }
>        __set_errno (saved_errno);
>        return ret;
> }
> so the __check_pf should either be manually inlined into it or you
> should try to inline it via keyword. I don't think it makes much sense
> to put __check_pf into a separate file, but that's just a cosmetic
> nitpick.

The only point about putting __check_pf in a separate file was that it 
was easiest to do that way as it was copied from glibc.

However, __check_pf is used in two places in the file, so I don't think 
inlining would be a good idea. However, I think it's a good point to put 
it in the only file where it is used, as a static function. If someone 
wants to extract it later that can be done then.

Here's a patch relative to svn 22531 (in addition to removing check_pf.c):

Index: Makefile.in
===================================================================
--- Makefile.in	(revision 22531)
+++ Makefile.in	(working copy)
@@ -14,7 +14,7 @@
  ifneq ($(UCLIBC_HAS_IPV4)$(UCLIBC_HAS_IPV6),)
  CSRC +=	getservice.c getproto.c hostid.c getnetent.c getnetbynm.c getnetbyad.c \
  	inet_net.c herror.c if_index.c gai_strerror.c getaddrinfo.c \
-	ether_addr.c ntohl.c ifaddrs.c ntop.c check_pf.c
+	ether_addr.c ntohl.c ifaddrs.c ntop.c
  endif
  ifeq ($(UCLIBC_HAS_IPV6),y)
  CSRC += in6_addr.c
Index: ifaddrs.h
===================================================================
--- ifaddrs.h	(revision 22562)
+++ ifaddrs.h	(working copy)
@@ -73,17 +73,4 @@

  __END_DECLS

-struct in6addrinfo
-{
-  enum {
-    in6ai_deprecated = 1,
-    in6ai_temporary = 2,
-    in6ai_homeaddress = 4
-  } flags;
-  uint32_t addr[4];
-};
-
-extern void __check_pf (bool *seen_ipv4, bool *seen_ipv6)
-  attribute_hidden;
-
  #endif /* ifaddrs.h */
Index: check_pf.c
===================================================================
--- check_pf.c	(revision 22533)
+++ check_pf.c	(working copy)
@@ -22,12 +22,11 @@
  #include <netdb.h>


-void
+unsigned
  attribute_hidden
-__check_pf (bool *seen_ipv4, bool *seen_ipv6)
+__check_pf (void)
  {
-  *seen_ipv4 = false;
-  *seen_ipv6 = false;
+  unsigned seen = 0;
  #if defined __UCLIBC_SUPPORT_AI_ADDRCONFIG__
    {
      /* Get the interface list via getifaddrs.  */
@@ -38,25 +37,25 @@
        /* We cannot determine what interfaces are available.  Be
        optimistic.  */
  #if defined __UCLIBC_HAS_IPV4__
-      *seen_ipv4 = true;
+      seen |= SEEN_IPV4;
  #endif /* __UCLIBC_HAS_IPV4__ */
  #if defined __UCLIBC_HAS_IPV6__
-      *seen_ipv6 = true;
+      seen |= SEEN_IPV6;
  #endif /* __UCLIBC_HAS_IPV6__ */
-      return;
+      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_ipv4 = true;
+        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_ipv6 = true;
+        seen |= SEEN_IPV4;
  #endif /* __UCLIBC_HAS_IPV6__ */

      (void) freeifaddrs (ifa);
@@ -64,11 +63,12 @@
  #else
    /* AI_ADDRCONFIG is disabled, assume both ipv4 and ipv6 available. */
  #if defined __UCLIBC_HAS_IPV4__
-  *seen_ipv4 = true;
+  seen |= SEEN_IPV4;
  #endif /* __UCLIBC_HAS_IPV4__ */
  #if defined __UCLIBC_HAS_IPV6__
-  *seen_ipv6 = true;
+  seen |= SEEN_IPV6;
  #endif /* __UCLIBC_HAS_IPV6__ */

  #endif /* __UCLIBC_SUPPORT_AI_ADDRCONFIG__ */
+  return seen;
  }
Index: getaddrinfo.c
===================================================================
--- getaddrinfo.c	(revision 22534)
+++ getaddrinfo.c	(working copy)
@@ -1,6 +1,8 @@
  /*
   * Copyright 1996 by Craig Metz
   * Copyright (C) 2000-2006 Erik Andersen <andersen at 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.
   */
@@ -157,29 +159,85 @@
  { 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_IPV4;
+#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;
-    bool seen_ipv4;
-    bool seen_ipv6;
+    unsigned seen;

-    __check_pf(&seen_ipv4, &seen_ipv6);
-    if (af == AF_INET)
-	ret = (int)seen_ipv4;
-    else if (af == AF_INET6)
-	ret = (int)seen_ipv6;
+    seen = __check_pf();
+#if defined __UCLIBC_HAS_IPV4__
+    if (af == AF_INET) 
+	ret = seen & SEEN_IPV4;
      else
+#endif
+#if defined __UCLIBC_HAS_IPV4__
+    if (af == AF_INET6)
+	ret = seen & SEEN_IPV6;
+    else
+#endif
      {
  	s = socket(af, SOCK_DGRAM, 0);
-	if (s < 0)
-	    ret = (errno == EMFILE) ? 1 : 0;
+	ret = 1; /* Assume PF_UNIX. */
+	if (s < 0) {
+	    if (errno != EMFILE)
+	        ret = 0;
+	}
  	else
-	{
  	    close(s);
-	    ret = 1;
-	}
      }
      __set_errno (saved_errno);
      return ret;
@@ -384,9 +442,7 @@
      int rc;
      int v4mapped = (req->ai_family == PF_UNSPEC || req->ai_family == PF_INET6) &&
  	(req->ai_flags & AI_V4MAPPED);
-    bool seen_ipv4;
-    bool seen_ipv6;
-    __check_pf(&seen_ipv4, &seen_ipv6);
+    unsigned seen = __check_pf();

      if (req->ai_protocol || req->ai_socktype)
      {
@@ -574,7 +630,7 @@

  #if defined __UCLIBC_HAS_IPV6__
  	    if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET6)
-		if (!(req->ai_flags & AI_ADDRCONFIG) || seen_ipv6)
+		if (!(req->ai_flags & AI_ADDRCONFIG) || (seen & SEEN_IPV6))
  		    gethosts (AF_INET6, struct in6_addr);
  #endif
  	    no_inet6_data = no_data;
@@ -582,7 +638,7 @@
  	    if (req->ai_family == AF_INET ||
  		(!v4mapped && req->ai_family == AF_UNSPEC) ||
  		(v4mapped && (no_inet6_data != 0 || (req->ai_flags & AI_ALL))))
-		if (!(req->ai_flags & AI_ADDRCONFIG) || seen_ipv4)
+		if (!(req->ai_flags & AI_ADDRCONFIG) || (seen & SEEN_IPV4))
  		    gethosts (AF_INET, struct in_addr);

  	    if (no_data != 0 && no_inet6_data != 0)
@@ -715,10 +771,10 @@
  	    for (st2 = st; st2 != NULL; st2 = st2->next)
  	    {
  		if (req->ai_flags & AI_ADDRCONFIG) {
-		    if (family == AF_INET && !seen_ipv4)
+		    if (family == AF_INET && !(seen & SEEN_IPV4))
  			break;
  #if defined __UCLIBC_HAS_IPV6__
-		    else if (family == AF_INET6 && !seen_ipv6)
+		    else if (family == AF_INET6 && !(seen & SEEN_IPV6))
  			break;
  #endif
  		}

--
Ricard Wolf Wanderlöf                           ricardw(at)axis.com
Axis Communications AB, Lund, Sweden            www.axis.com
Phone +46 46 272 2016                           Fax +46 46 13 61 30



More information about the uClibc mailing list