Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/net Put back AF_LINK support. This time, all the n...



details:   https://anonhg.NetBSD.org/src/rev/cd242f0b7d70
branches:  trunk
changeset: 515861:cd242f0b7d70
user:      bjh21 <bjh21%NetBSD.org@localhost>
date:      Sat Oct 06 13:59:41 2001 +0000

description:
Put back AF_LINK support.  This time, all the new code passes lint(1), modulo
bin/14175.  It should pass lint on a native i386 build, but I can't test that.

diffstat:

 lib/libc/net/getnameinfo.c |  147 ++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 145 insertions(+), 2 deletions(-)

diffs (201 lines):

diff -r 1d437e53c33f -r cd242f0b7d70 lib/libc/net/getnameinfo.c
--- a/lib/libc/net/getnameinfo.c        Sat Oct 06 13:32:58 2001 +0000
+++ b/lib/libc/net/getnameinfo.c        Sat Oct 06 13:59:41 2001 +0000
@@ -1,7 +1,8 @@
-/*     $NetBSD: getnameinfo.c,v 1.31 2001/10/05 20:51:19 bjh21 Exp $   */
+/*     $NetBSD: getnameinfo.c,v 1.32 2001/10/06 13:59:41 bjh21 Exp $   */
 /*     $KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $    */
 
 /*
+ * Copyright (c) 2000 Ben Harris.
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  * All rights reserved.
  *
@@ -44,17 +45,21 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: getnameinfo.c,v 1.31 2001/10/05 20:51:19 bjh21 Exp $");
+__RCSID("$NetBSD: getnameinfo.c,v 1.32 2001/10/06 13:59:41 bjh21 Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include "namespace.h"
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <net/if.h>
+#include <net/if_dl.h>
+#include <net/if_ieee1394.h>
+#include <net/if_types.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <arpa/nameser.h>
 #include <assert.h>
+#include <limits.h>
 #include <netdb.h>
 #include <resolv.h>
 #include <stddef.h>
@@ -90,11 +95,16 @@
        u_short si_port;
 };
 
+static int getnameinfo_inet __P((const struct sockaddr *, socklen_t, char *,
+    size_t, char *, size_t, int));
 #ifdef INET6
 static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
                                 size_t, int));
 static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int));
 #endif
+static int getnameinfo_link __P((const struct sockaddr *, socklen_t, char *,
+    size_t, char *, size_t, int));
+static int hexname __P((const u_int8_t *, size_t, char *, size_t));
 
 /* 2553bis: use EAI_xx for getnameinfo */
 #define ENI_NOSOCKET   EAI_FAIL                /*XXX*/
@@ -105,10 +115,41 @@
 #define ENI_FAMILY     EAI_FAMILY
 #define ENI_SALEN      EAI_FAMILY
 
+/*
+ * Top-level getnameinfo() code.  Look at the address family, and pick an
+ * appropriate function to call.
+ */
 int
 getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
        const struct sockaddr *sa;
        socklen_t salen;
+       char *host, *serv;
+       size_t hostlen, servlen;
+       int flags;
+{
+
+       switch (sa->sa_family) {
+       case AF_INET:
+       case AF_INET6:
+               return getnameinfo_inet(sa, salen, host, hostlen,
+                   serv, servlen, flags);
+       case AF_LINK:
+               return getnameinfo_link(sa, salen, host, hostlen,
+                   serv, servlen, flags);
+       default:
+               return EAI_FAMILY;
+       }
+}      
+
+
+/*
+ * getnameinfo_inet():
+ * Format an IPv4 or IPv6 sockaddr into a printable string.
+ */
+static int
+getnameinfo_inet(sa, salen, host, hostlen, serv, servlen, flags)
+       const struct sockaddr *sa;
+       socklen_t salen;
        char *host;
        size_t hostlen;
        char *serv;
@@ -397,3 +438,105 @@
                return n;
 }
 #endif /* INET6 */
+
+
+/*
+ * getnameinfo_link():
+ * Format a link-layer address into a printable format, paying attention to
+ * the interface type.
+ */
+/* ARGSUSED */
+static int
+getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
+    char *host, size_t hostlen, char *serv, size_t servlen, int flags)
+{
+       const struct sockaddr_dl *sdl =
+           (const struct sockaddr_dl *)(const void *)sa;
+       const struct ieee1394_hwaddr *iha;
+       int n;
+
+       if (serv != NULL && servlen > 0)
+               *serv = '\0';
+
+       if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
+               n = snprintf(host, hostlen, "link#%d", sdl->sdl_index);
+               if (n > hostlen) {
+                       *host = '\0';
+                       return EAI_MEMORY;
+               }
+               return 0;
+       }
+
+       switch (sdl->sdl_type) {
+#ifdef IFT_ECONET
+       case IFT_ECONET:
+               if (sdl->sdl_alen < 2)
+                       return EAI_FAMILY;
+               if (LLADDR(sdl)[1] == 0)
+                       n = snprintf(host, hostlen, "%d", LLADDR(sdl)[0]);
+               else
+                       n = snprintf(host, hostlen, "%d.%d",
+                           LLADDR(sdl)[1], LLADDR(sdl)[0]);
+               if (n >= hostlen) {
+                       *host = '\0';
+                       return EAI_MEMORY;
+               } else
+                       return 0;
+#endif
+       case IFT_IEEE1394:
+               if (sdl->sdl_alen < sizeof(iha->iha_uid))
+                       return EAI_FAMILY;
+               iha =
+                   (const struct ieee1394_hwaddr *)(const void *)LLADDR(sdl);
+               return hexname(iha->iha_uid, sizeof(iha->iha_uid),
+                   host, hostlen);
+       /*
+        * The following have zero-length addresses.
+        * IFT_ATM      (net/if_atmsubr.c)
+        * IFT_FAITH    (net/if_faith.c)
+        * IFT_GIF      (net/if_gif.c)
+        * IFT_LOOP     (net/if_loop.c)
+        * IFT_PPP      (net/if_ppp.c, net/if_spppsubr.c)
+        * IFT_SLIP     (net/if_sl.c, net/if_strip.c)
+        * IFT_STF      (net/if_stf.c)
+        * IFT_L2VLAN   (net/if_vlan.c)
+        * IFT_PROPVIRTUAL (net/if_bridge.h>
+        */
+       /*
+        * The following use IPv4 addresses as link-layer addresses:
+        * IFT_OTHER    (net/if_gre.c)
+        * IFT_OTHER    (netinet/ip_ipip.c)
+        */
+       case IFT_ARCNET: /* default below is believed correct for all these. */
+       case IFT_ETHER:
+       case IFT_FDDI:
+       case IFT_HIPPI:
+       case IFT_ISO88025:
+       default:
+               return hexname((u_int8_t *)LLADDR(sdl), (size_t)sdl->sdl_alen,
+                   host, hostlen);
+       }
+}
+
+static int
+hexname(cp, len, host, hostlen)
+       const u_int8_t *cp;
+       char *host;
+       size_t len, hostlen;
+{
+       int i, n;
+       char *outp = host;
+
+       *outp = '\0';
+       for (i = 0; i < len; i++) {
+               n = snprintf(outp, hostlen, "%s%02x",
+                   i ? ":" : "", cp[i]);
+               if (n < 0 || n >= hostlen) {
+                       *host = '\0';
+                       return EAI_MEMORY;
+               }
+               outp += n;
+               hostlen -= n;
+       }
+       return 0;
+}



Home | Main Index | Thread Index | Old Index