Source-Changes-HG archive

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

[src/trunk]: src/distrib/utils/libhack avoid copying most of libc (except get...



details:   https://anonhg.NetBSD.org/src/rev/ee280bcdfea5
branches:  trunk
changeset: 789615:ee280bcdfea5
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Aug 27 09:53:33 2013 +0000

description:
avoid copying most of libc (except gethostent_r, which unfortunately
adds a resolver dependency) by callling the internal nsswitch functions.

diffstat:

 distrib/utils/libhack/Makefile     |    5 +-
 distrib/utils/libhack/Makefile.inc |    3 +-
 distrib/utils/libhack/gethost.c    |  301 +++++++++++++++---------------------
 3 files changed, 133 insertions(+), 176 deletions(-)

diffs (truncated from 405 to 300 lines):

diff -r 77e80a1d7057 -r ee280bcdfea5 distrib/utils/libhack/Makefile
--- a/distrib/utils/libhack/Makefile    Tue Aug 27 06:41:05 2013 +0000
+++ b/distrib/utils/libhack/Makefile    Tue Aug 27 09:53:33 2013 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.23 2012/10/11 17:11:16 christos Exp $
+# $NetBSD: Makefile,v 1.24 2013/08/27 09:53:33 christos Exp $
 #
 # Stubs to kill off some things from libc:
 # This save space on a boot system.
@@ -13,11 +13,12 @@
 CPPFLAGS.runetable.c+= -I${HACKSRC}/../../../lib/libc/citrus \
                        -DALL_80_TO_FF_SW1
 CPPFLAGS.syslog.c+=    -I${HACKSRC}/../../../lib/libc/include
+CPPFLAGS.gethost.c+=   -I${HACKSRC}/../../../lib/libc/net
 
 LIB=           hack
 SRCS=          getcap.c getgrent.c getnet.c getnetgr.c getpwent.c \
                localeconv.c multibyte.c perror.c runetable.c setlocale.c \
-               strerror.c strsignal.c syslog.c utmp.c yplib.c
+               strerror.c strsignal.c syslog.c utmp.c yplib.c gethost.c
 
 WARNS=         1
 NOLINKLIB=     # defined
diff -r 77e80a1d7057 -r ee280bcdfea5 distrib/utils/libhack/Makefile.inc
--- a/distrib/utils/libhack/Makefile.inc        Tue Aug 27 06:41:05 2013 +0000
+++ b/distrib/utils/libhack/Makefile.inc        Tue Aug 27 09:53:33 2013 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile.inc,v 1.24 2012/10/11 17:11:16 christos Exp $
+# $NetBSD: Makefile.inc,v 1.25 2013/08/27 09:53:33 christos Exp $
 #
 # Include this fragment to build libhack.o
 # It is .o and not .a to make sure these are the
@@ -25,6 +25,7 @@
                        -DALL_80_TO_FF_SW1
 
 CPPFLAGS.syslog.c+= -I${HACKSRC}/../../../lib/libc/include
+CPPFLAGS.gethost.c+= -I${HACKSRC}/../../../lib/libc/net
 
 libhack.o: ${HACKOBJS}
        ${LD} -r -o $@ ${HACKOBJS}
diff -r 77e80a1d7057 -r ee280bcdfea5 distrib/utils/libhack/gethost.c
--- a/distrib/utils/libhack/gethost.c   Tue Aug 27 06:41:05 2013 +0000
+++ b/distrib/utils/libhack/gethost.c   Tue Aug 27 09:53:33 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gethost.c,v 1.10 2013/08/25 08:46:34 christos Exp $    */
+/*     $NetBSD: gethost.c,v 1.11 2013/08/27 09:53:33 christos Exp $    */
 
 /*-
  * Copyright (c) 1985, 1988, 1993
@@ -49,10 +49,7 @@
  * --Copyright--
  */
 
-/*
- * Copied from:  lib/libc/net/gethostnamadr.c
- * and then gutted, leaving only /etc/hosts support.
- */
+/* Provide just /etc/hosts lookup support */
 
 #include <sys/cdefs.h>
 
@@ -61,217 +58,175 @@
 #define gethostbyname          _gethostbyname
 #endif
 
-#include <sys/param.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#include <netdb.h>
+#include <string.h>
+#include <nsswitch.h>
+#include <errno.h>
 #include <arpa/nameser.h>
-#include <netdb.h>
-#include <resolv.h>
-#include <stdio.h>
-#include <ctype.h>
-#include <errno.h>
-#include <string.h>
+#include <arpa/inet.h>
+#include <sys/socket.h>
+
+#include "hostent.h"
 
 #ifdef __weak_alias
 __weak_alias(gethostbyaddr,_gethostbyaddr);
 __weak_alias(gethostbyname,_gethostbyname);
 #endif
 
-#define        MAXALIASES      35
-#define        MAXADDRS        35
-
-static char *h_addr_ptrs[MAXADDRS + 1];
-
-static struct hostent host;
-static char *host_aliases[MAXALIASES];
-static char hostbuf[BUFSIZ+1];
-static struct in_addr host_addr;
+extern int h_errno;
+FILE *_h_file;
+static struct hostent h_ent;
+static char h_buf[4096]; 
 
-FILE *_h_file = NULL;
-void sethostent_r(FILE **);
-void endhostent_r(FILE **);
-
-struct hostent *_gethtent(void);
-struct hostent *_gethtbyname(const char *);
-struct hostent *_gethtbyaddr(const void *, socklen_t, int);
+static struct hostent *
+getby(int (*f)(void *, void *, va_list), struct getnamaddr *info, ...)
+{
+        va_list ap;
+        int e;
+        
+        va_start(ap, info);
+        e = (*f)(info, NULL, ap);
+        va_end(ap); 
+        switch (e) {
+        case NS_SUCCESS: 
+                return info->hp;  
+        default:
+               return NULL;
+        }       
+}                       
 
+struct hostent *
+gethostbyname_r(const char *name, struct hostent *hp, char *buf, size_t bufsiz,
+    int *he)
+{
+       struct getnamaddr info;
+       info.hp = hp;
+       info.buf = buf;
+       info.buflen = bufsiz;
+       info.he = he;
+       return getby(_hf_gethtbyname, &info, name, 0, AF_INET);
+}
 
-#if PACKETSZ > 1024
-#define        MAXPACKET       PACKETSZ
-#else
-#define        MAXPACKET       1024
-#endif
-
-extern int h_errno;
 
 struct hostent *
 gethostbyname(const char *name)
 {
-       const char *cp;
+       return gethostbyname_r(name, &h_ent, h_buf, sizeof(h_buf), &h_errno);
+}
 
-       /*
-        * disallow names consisting only of digits/dots, unless
-        * they end in a dot.
-        */
-       if (isdigit((unsigned char)name[0]))
-               for (cp = name;; ++cp) {
-                       if (!*cp) {
-                               if (*--cp == '.')
-                                       break;
-                               /*
-                                * All-numeric, no dot at the end.
-                                * Fake up a hostent as if we'd actually
-                                * done a lookup.
-                                */
-                               if (!inet_aton(name, &host_addr)) {
-                                       h_errno = HOST_NOT_FOUND;
-                                       return((struct hostent *) NULL);
-                               }
-                               host.h_name = (char *)name;
-                               host.h_aliases = host_aliases;
-                               host_aliases[0] = NULL;
-                               host.h_addrtype = AF_INET;
-                               host.h_length = sizeof(u_int32_t);
-                               h_addr_ptrs[0] = (char *)&host_addr;
-                               h_addr_ptrs[1] = NULL;
-                               host.h_addr_list = h_addr_ptrs;
-                               return &host;
-                       }
-                       if (!isdigit((unsigned char)*cp) && *cp != '.') 
-                               break;
-               }
-
-       /* XXX - Force host table lookup. */
-       return _gethtbyname(name);
+struct hostent *
+gethostbyaddr_r(const void *addr, socklen_t len, int type, struct hostent *hp,
+    char *buf, size_t bufsiz, int *he)
+{
+       struct getnamaddr info;
+       info.hp = hp;
+       info.buf = buf;
+       info.buflen = bufsiz;
+       info.he = he;
+       return getby(_hf_gethtbyaddr, &info, addr, len, type);
 }
 
 struct hostent *
 gethostbyaddr(const void *addr, socklen_t len, int type)
 {
-#if 0
-       char qbuf[MAXDNAME];
-#endif
-
-       if (type != AF_INET)
-               return NULL;
-#if 0
-       (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa",
-           ((unsigned)addr[3] & 0xff), ((unsigned)addr[2] & 0xff),
-           ((unsigned)addr[1] & 0xff), ((unsigned)addr[0] & 0xff));
-#endif
-
-       /* XXX - Force host table lookup. */
-       return _gethtbyaddr(addr, len, type);
-}
-
-void
-sethostent_r(FILE **hf)
-{
-       if (*hf == NULL)
-               *hf = fopen(_PATH_HOSTS, "r");
-       else
-               rewind(*hf);
-}
-
-void
-endhostent_r(FILE **hf)
-{
-       if (*hf) {
-               (void)fclose(*hf);
-               *hf = NULL;
-       }
+       return gethostbyaddr_r(addr, len, type, &h_ent, h_buf, sizeof(h_buf),
+           &h_errno);
 }
 
 struct hostent *
-_gethtent(void)
+gethostent_r(FILE *hf, struct hostent *hent, char *buf, size_t buflen, int *he)
 {
-       char *p;
+       char *p, *name;
        char *cp, **q;
+       int af, len;
+       size_t llen, anum;
+       char *aliases[MAXALIASES];
+       struct in6_addr host_addr;
 
-       if (_h_file == NULL && (_h_file = fopen(_PATH_HOSTS, "r" )) == NULL)
+       if (hf == NULL) {
+               *he = NETDB_INTERNAL;
+               errno = EINVAL;
                return NULL;
-again:
-       if ((p = fgets(hostbuf, BUFSIZ, _h_file)) == NULL)
+       }
+ again:
+       if ((p = fgetln(hf, &llen)) == NULL) {
+               *he = HOST_NOT_FOUND;
                return NULL;
+       }
+       if (llen < 1)
+               goto again;
        if (*p == '#')
                goto again;
-       cp = strpbrk(p, "#\n");
-       if (cp == NULL)
+       p[llen] = '\0';
+       if (!(cp = strpbrk(p, "#\n")))
                goto again;
        *cp = '\0';
-       cp = strpbrk(p, " \t");
-       if (cp == NULL)
+       if (!(cp = strpbrk(p, " \t")))
                goto again;
        *cp++ = '\0';
-       /* THIS STUFF IS INTERNET SPECIFIC */
-       h_addr_ptrs[0] = (char *)&host_addr;
-       h_addr_ptrs[1] = NULL;
-       (void) inet_aton(p, &host_addr);
-       host.h_addr_list = h_addr_ptrs;
-       host.h_length = sizeof(u_int32_t);
-       host.h_addrtype = AF_INET;
+       if (inet_pton(AF_INET6, p, &host_addr) > 0) {
+               af = AF_INET6;
+               len = NS_IN6ADDRSZ;
+       } else if (inet_pton(AF_INET, p, &host_addr) > 0) {
+#if 0
+               res_state res = __res_get_state();
+               if (res == NULL)
+                       return NULL;
+               if (res->options & RES_USE_INET6) {
+                       map_v4v6_address(buf, buf);
+                       af = AF_INET6;
+                       len = NS_IN6ADDRSZ;
+               } else {



Home | Main Index | Thread Index | Old Index