2 * Copyright (c) 1994, Garrett Wollman
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #if defined(LIBC_SCCS) && !defined(lint)
27 static char sccsid[] = "@(#)$FreeBSD: src/lib/libc/net/getnetbynis.c,v 1.11 1999/08/28 00:00:07 peter Exp $";
28 static char rcsid[] = "$FreeBSD: src/lib/libc/net/getnetbynis.c,v 1.11 1999/08/28 00:00:07 peter Exp $";
29 #endif /* LIBC_SCCS and not lint */
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
41 #include <arpa/nameser.h>
44 #include <rpcsvc/yp_prot.h>
45 #include <rpcsvc/ypclnt.h>
52 static char *host_aliases[MAXALIASES];
55 static struct netent *
56 _getnetbynis(name, map, af)
62 register char *cp, **q;
65 static struct netent h;
66 static char *domain = (char *)NULL;
67 static char ypbuf[YPMAXRECORD + 2];
78 if (domain == (char *)NULL)
79 if (yp_get_default_domain (&domain))
82 if (yp_match(domain, map, name, strlen(name), &result, &resultlen))
85 bcopy((char *)result, (char *)&ypbuf, resultlen);
86 ypbuf[resultlen] = '\0';
88 result = (char *)&ypbuf;
90 if ((cp = index(result, '\n')))
93 cp = strpbrk(result, " \t");
97 while (*cp == ' ' || *cp == '\t')
100 h.n_net = inet_network(cp);
101 h.n_addrtype = AF_INET;
103 q = h.n_aliases = host_aliases;
104 cp = strpbrk(cp, " \t");
108 if (*cp == ' ' || *cp == '\t') {
112 if (q < &host_aliases[MAXALIASES - 1])
114 cp = strpbrk(cp, " \t");
126 _getnetbynisname(name)
129 return _getnetbynis(name, "networks.byname", AF_INET);
133 _getnetbynisaddr(addr, af)
140 unsigned int netbr[4];
144 errno = EAFNOSUPPORT;
148 for (nn = 4, net2 = addr; net2; net2 >>= 8) {
149 netbr[--nn] = net2 & 0xff;
153 case 3: /* Class A */
154 sprintf(buf, "%u", netbr[3]);
156 case 2: /* Class B */
157 sprintf(buf, "%u.%u", netbr[2], netbr[3]);
159 case 1: /* Class C */
160 sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]);
162 case 0: /* Class D - E */
163 sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1],
169 cp = str + (strlen(str) - 2);
171 while(!strcmp(cp, ".0")) {
173 cp = str + (strlen(str) - 2);
176 return _getnetbynis(str, "networks.byaddr", af);