Source-Changes-HG archive

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

[src/trunk]: src/lib/libc/net Add not advertised reentrant functions: {get, se...



details:   https://anonhg.NetBSD.org/src/rev/dd740f7ef811
branches:  trunk
changeset: 789359:dd740f7ef811
user:      christos <christos%NetBSD.org@localhost>
date:      Fri Aug 16 15:27:12 2013 +0000

description:
Add not advertised reentrant functions: {get,set,end}hostent_r,
gethostbyname{,2}_r, gethostbyaddr_r. Make getnameinfo(3) use
gethostbyaddr_r(3) so it is re-entrant (ahem __ypdomain). These
are not being advertised because there is a bunch of different
implementation of them that have a variety of type signatures.

If people want to follow someone's implementation, it is now easy.

diffstat:

 lib/libc/net/gethnamaddr.c |  933 +++++++++++++++++++++++++-------------------
 lib/libc/net/getnameinfo.c |   11 +-
 lib/libc/net/hostent.h     |   77 +++
 lib/libc/net/sethostent.c  |   12 +-
 4 files changed, 614 insertions(+), 419 deletions(-)

diffs (truncated from 1817 to 300 lines):

diff -r abc8c1175704 -r dd740f7ef811 lib/libc/net/gethnamaddr.c
--- a/lib/libc/net/gethnamaddr.c        Fri Aug 16 13:39:47 2013 +0000
+++ b/lib/libc/net/gethnamaddr.c        Fri Aug 16 15:27:12 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gethnamaddr.c,v 1.79 2012/09/09 16:42:23 christos Exp $        */
+/*     $NetBSD: gethnamaddr.c,v 1.80 2013/08/16 15:27:12 christos Exp $        */
 
 /*
  * ++Copyright++ 1985, 1988, 1993
@@ -57,7 +57,7 @@
 static char sccsid[] = "@(#)gethostnamadr.c    8.1 (Berkeley) 6/4/93";
 static char rcsid[] = "Id: gethnamaddr.c,v 8.21 1997/06/01 20:34:37 vixie Exp ";
 #else
-__RCSID("$NetBSD: gethnamaddr.c,v 1.79 2012/09/09 16:42:23 christos Exp $");
+__RCSID("$NetBSD: gethnamaddr.c,v 1.80 2013/08/16 15:27:12 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -95,6 +95,8 @@
 #include <rpcsvc/ypclnt.h>
 #endif
 
+#include "hostent.h"
+
 #if defined(_LIBC) && defined(__weak_alias)
 __weak_alias(gethostbyaddr,_gethostbyaddr)
 __weak_alias(gethostbyname,_gethostbyname)
@@ -111,31 +113,23 @@
 #define        MAXADDRS        35
 
 static const char AskedForGot[] =
-                         "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
+    "gethostby*.getanswer: asked for \"%s\", got \"%s\"";
 
-static char *h_addr_ptrs[MAXADDRS + 1];
 
 #ifdef YP
 static char *__ypdomain;
 #endif
 
-static struct hostent host;
-static char *host_aliases[MAXALIASES];
-static char hostbuf[8*1024];
-static u_int32_t host_addr[16 / sizeof(u_int32_t)];    /* IPv4 or IPv6 */
-static FILE *hostf = NULL;
-static int stayopen = 0;
-
 #define        MAXPACKET       (64*1024)
 
 typedef union {
-    HEADER hdr;
-    u_char buf[MAXPACKET];
+       HEADER hdr;
+       u_char buf[MAXPACKET];
 } querybuf;
 
 typedef union {
-    int32_t al;
-    char ac;
+       int32_t al;
+       char ac;
 } align;
 
 #ifdef DEBUG
@@ -143,33 +137,23 @@
        __attribute__((__format__(__printf__, 1, 3)));
 #endif
 static struct hostent *getanswer(const querybuf *, int, const char *, int,
-    res_state);
+    res_state, struct hostent *, char *, size_t, int *);
 static void map_v4v6_address(const char *, char *);
 static void map_v4v6_hostent(struct hostent *, char **, char *);
 static void addrsort(char **, int, res_state);
 
-void _sethtent(int);
-void _endhtent(void);
-struct hostent *_gethtent(void);
-void ht_sethostent(int);
-void ht_endhostent(void);
-struct hostent *ht_gethostbyname(char *);
-struct hostent *ht_gethostbyaddr(const char *, int, int);
 void dns_service(void);
 #undef dn_skipname
 int dn_skipname(const u_char *, const u_char *);
-int _gethtbyaddr(void *, void *, va_list);
-int _gethtbyname(void *, void *, va_list);
-struct hostent *_gethtbyname2(const char *, int);
-int _dns_gethtbyaddr(void *, void *, va_list);
-int _dns_gethtbyname(void *, void *, va_list);
+
+static struct hostent *_hf_gethtbyname2(const char *, int, struct getnamaddr *);
+
 #ifdef YP
-struct hostent *_yphostent(char *, int);
-int _yp_gethtbyaddr(void *, void *, va_list);
-int _yp_gethtbyname(void *, void *, va_list);
+static struct hostent *_yp_hostent(char *, int, struct getnamaddr *);
 #endif
 
-static struct hostent *gethostbyname_internal(const char *, int, res_state);
+static struct hostent *gethostbyname_internal(const char *, int, res_state,
+    struct hostent *, char *, size_t, int *);
 
 static const ns_src default_dns_files[] = {
        { NSSRC_FILES,  NS_SUCCESS },
@@ -178,6 +162,33 @@
 };
 
 
+#define ARRAY(dst, anum, ptr, len) \
+       do { \
+               size_t _len = (anum + 1) * sizeof(*dst); \
+               if (_len > len) \
+                       goto nospc; \
+               dst = (void *)ptr; \
+               ptr += _len; \
+               len -= _len; \
+       } while (/*CONSTCOND*/0)
+
+#define COPY(dst, src, slen, ptr, len) \
+       do { \
+               if ((size_t)slen > len) \
+                       goto nospc; \
+               memcpy(ptr, src, (size_t)slen); \
+               dst = ptr; \
+               ptr += slen; \
+               len -= slen; \
+       } while (/* CONSTCOND */0)
+
+#define SCOPY(dst, src, ptr, len) \
+       do { \
+               size_t _len = strlen(src) + 1; \
+               COPY(dst, src, _len, ptr, len); \
+       } while (/* CONSTCOND */0)
+
+
 #ifdef DEBUG
 static void
 debugprintf(const char *msg, res_state res, ...)
@@ -218,17 +229,20 @@
 
 static struct hostent *
 getanswer(const querybuf *answer, int anslen, const char *qname, int qtype,
-    res_state res)
+    res_state res, struct hostent *hent, char *buf, size_t buflen, int *he)
 {
        const HEADER *hp;
        const u_char *cp;
        int n;
+       size_t qlen;
        const u_char *eom, *erdata;
        char *bp, **ap, **hap, *ep;
        int type, class, ancount, qdcount;
        int haveanswer, had_error;
        int toobig = 0;
        char tbuf[MAXDNAME];
+       char *aliases[MAXALIASES];
+       char *addr_ptrs[MAXADDRS + 1];
        const char *tname;
        int (*name_ok)(const char *);
 
@@ -236,7 +250,7 @@
        _DIAGASSERT(qname != NULL);
 
        tname = qname;
-       host.h_name = NULL;
+       hent->h_name = NULL;
        eom = answer->buf + anslen;
        switch (qtype) {
        case T_A:
@@ -255,19 +269,17 @@
        hp = &answer->hdr;
        ancount = ntohs(hp->ancount);
        qdcount = ntohs(hp->qdcount);
-       bp = hostbuf;
-       ep = hostbuf + sizeof hostbuf;
+       bp = buf;
+       ep = buf + buflen;
        cp = answer->buf;
        BOUNDED_INCR(HFIXEDSZ);
-       if (qdcount != 1) {
-               h_errno = NO_RECOVERY;
-               return NULL;
-       }
+       if (qdcount != 1)
+               goto no_recovery;
+
        n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp));
-       if ((n < 0) || !maybe_ok(res, bp, name_ok)) {
-               h_errno = NO_RECOVERY;
-               return NULL;
-       }
+       if ((n < 0) || !maybe_ok(res, bp, name_ok))
+               goto no_recovery;
+
        BOUNDED_INCR(n + QFIXEDSZ);
        if (qtype == T_A || qtype == T_AAAA) {
                /* res_send() has already verified that the query name is the
@@ -275,21 +287,17 @@
                 * (i.e., with the succeeding search-domain tacked on).
                 */
                n = (int)strlen(bp) + 1;                /* for the \0 */
-               if (n >= MAXHOSTNAMELEN) {
-                       h_errno = NO_RECOVERY;
-                       return NULL;
-               }
-               host.h_name = bp;
+               if (n >= MAXHOSTNAMELEN)
+                       goto no_recovery;
+               hent->h_name = bp;
                bp += n;
                /* The qname can be abbreviated, but h_name is now absolute. */
-               qname = host.h_name;
+               qname = hent->h_name;
        }
-       ap = host_aliases;
+       hent->h_aliases = ap = aliases;
+       hent->h_addr_list = hap = addr_ptrs;
        *ap = NULL;
-       host.h_aliases = host_aliases;
-       hap = h_addr_ptrs;
        *hap = NULL;
-       host.h_addr_list = h_addr_ptrs;
        haveanswer = 0;
        had_error = 0;
        while (ancount-- > 0 && cp < eom && !had_error) {
@@ -314,18 +322,17 @@
                        continue;               /* XXX - had_error++ ? */
                }
                if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) {
-                       if (ap >= &host_aliases[MAXALIASES-1])
+                       if (ap >= &aliases[MAXALIASES-1])
                                continue;
-                       n = dn_expand(answer->buf, eom, cp, tbuf, (int)sizeof tbuf);
+                       n = dn_expand(answer->buf, eom, cp, tbuf,
+                           (int)sizeof tbuf);
                        if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) {
                                had_error++;
                                continue;
                        }
                        cp += n;
-                       if (cp != erdata) {
-                               h_errno = NO_RECOVERY;
-                               return NULL;
-                       }
+                       if (cp != erdata)
+                               goto no_recovery;
                        /* Store alias. */
                        *ap++ = bp;
                        n = (int)strlen(bp) + 1;        /* for the \0 */
@@ -341,21 +348,20 @@
                                continue;
                        }
                        strlcpy(bp, tbuf, (size_t)(ep - bp));
-                       host.h_name = bp;
+                       hent->h_name = bp;
                        bp += n;
                        continue;
                }
                if (qtype == T_PTR && type == T_CNAME) {
-                       n = dn_expand(answer->buf, eom, cp, tbuf, (int)sizeof tbuf);
+                       n = dn_expand(answer->buf, eom, cp, tbuf,
+                           (int)sizeof tbuf);
                        if (n < 0 || !maybe_dnok(res, tbuf)) {
                                had_error++;
                                continue;
                        }
                        cp += n;
-                       if (cp != erdata) {
-                               h_errno = NO_RECOVERY;
-                               return NULL;
-                       }
+                       if (cp != erdata)
+                               goto no_recovery;
                        /* Get canonical name. */
                        n = (int)strlen(tbuf) + 1;      /* for the \0 */
                        if (n > ep - bp || n >= MAXHOSTNAMELEN) {
@@ -391,13 +397,11 @@
                        }
 #if MULTI_PTRS_ARE_ALIASES
                        cp += n;
-                       if (cp != erdata) {
-                               h_errno = NO_RECOVERY;
-                               return NULL;
-                       }
+                       if (cp != erdata)
+                               goto no_recovery;
                        if (!haveanswer)
-                               host.h_name = bp;
-                       else if (ap < &host_aliases[MAXALIASES-1])
+                               hent->h_name = bp;
+                       else if (ap < &aliases[MAXALIASES-1])
                                *ap++ = bp;
                        else
                                n = -1;
@@ -411,7 +415,7 @@
                        }
                        break;
 #else
-                       host.h_name = bp;



Home | Main Index | Thread Index | Old Index