From ff27d2a474a49e1df68b4e3f835e8e3f69c970e1 Mon Sep 17 00:00:00 2001 From: Bill Paul Date: Sat, 23 Mar 1996 22:16:22 +0000 Subject: [PATCH] Fix other half of problem reported in PR #1079: _getnetbynisaddr() is broken. The translation from network number to ASCII string was not working correctly (you would sometimes get things like 0.244.0.0 instead of 244.0.0). Also copied results of yp_match() to a static buffer for consistency with gethostbynis.c. Note: _getnetbynisaddr() chops off trailing .0's, i.e. 244.0.0 is truncated to 244. By contrast, getnetbyht.c code (for local /etc/networks lookups) leaves the traling .0's in place. This means that the NIS and local file lookups will match different things when looking up the same network number. I'm not sure which is the correct behavior. (I think the DNS lookup code tries all combinations -- should the NIS and local host lookup routines do that too?) --- lib/libc/net/getnetbynis.c | 53 +++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/lib/libc/net/getnetbynis.c b/lib/libc/net/getnetbynis.c index d87ca0f29c5..ac11aa38f45 100644 --- a/lib/libc/net/getnetbynis.c +++ b/lib/libc/net/getnetbynis.c @@ -24,8 +24,8 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)$Id: getnetbynis.c,v 1.3 1995/05/30 05:40:47 rgrimes Exp $"; -static char rcsid[] = "$Id: getnetbynis.c,v 1.3 1995/05/30 05:40:47 rgrimes Exp $"; +static char sccsid[] = "@(#)$Id: getnetbynis.c,v 1.4 1995/10/22 14:39:06 phk Exp $"; +static char rcsid[] = "$Id: getnetbynis.c,v 1.4 1995/10/22 14:39:06 phk Exp $"; #endif /* LIBC_SCCS and not lint */ #include @@ -38,6 +38,12 @@ static char rcsid[] = "$Id: getnetbynis.c,v 1.3 1995/05/30 05:40:47 rgrimes Exp #include #include #include +#include +#ifdef YP +#include +#include +#include +#endif #define MAXALIASES 35 #define MAXADDRS 35 @@ -56,19 +62,19 @@ _getnetbynis(name, map) int resultlen; static struct netent h; static char *domain = (char *)NULL; + static char ypbuf[YPMAXRECORD]; if (domain == (char *)NULL) if (yp_get_default_domain (&domain)) return (NULL); - if (result) { - free(result); - result = 0; - } - if (yp_match(domain, map, name, strlen(name), &result, &resultlen)) return (NULL); + bcopy((char *)result, (char *)&ypbuf, resultlen); + free(result); + result = (char *)&ypbuf; + if ((cp = index(result, '\n'))) *cp = '\0'; @@ -116,18 +122,41 @@ _getnetbynisaddr(addr, type) long addr; int type; { - struct in_addr in; char *str, *cp; + unsigned long net2; + int nn; + unsigned int netbr[4]; + char buf[MAXDNAME]; if (type != AF_INET) return (NULL); - in.s_addr = addr; - str = inet_ntoa(in); - cp = str + strlen(str) - 2; + for (nn = 4, net2 = addr; net2; net2 >>= 8) { + netbr[--nn] = net2 & 0xff; + } + + switch (nn) { + case 3: /* Class A */ + sprintf(buf, "%u", netbr[3]); + break; + case 2: /* Class B */ + sprintf(buf, "%u.%u", netbr[2], netbr[3]); + break; + case 1: /* Class C */ + sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]); + break; + case 0: /* Class D - E */ + sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1], + netbr[2], netbr[3]); + break; + } + + str = (char *)&buf; + cp = str + (strlen(str) - 2); + while(!strcmp(cp, ".0")) { *cp = '\0'; - cp = str + strlen(str) - 2; + cp = str + (strlen(str) - 2); } return _getnetbynis(str, "networks.byaddr");