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
25 * $FreeBSD: src/lib/libc/net/getnetbynis.c,v 1.21 2006/04/15 16:20:27 ume Exp $
28 #include <sys/param.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
41 #include <arpa/nameser.h>
44 #include <rpcsvc/yp_prot.h>
45 #include <rpcsvc/ypclnt.h>
47 #include "netdb_private.h"
51 _getnetbynis(const char *name, char *map, int af, struct netent *ne,
52 struct netent_data *ned)
58 char ypbuf[YPMAXRECORD + 2];
69 if (ned->yp_domain == NULL)
70 if (yp_get_default_domain (&ned->yp_domain))
73 if (yp_match(ned->yp_domain, map, name, strlen(name), &result,
77 bcopy((char *)result, (char *)&ypbuf, resultlen);
78 ypbuf[resultlen] = '\0';
80 result = (char *)&ypbuf;
82 if ((cp = index(result, '\n')))
85 cp = strpbrk(result, " \t");
88 ep = ned->netbuf + sizeof ned->netbuf;
89 len = strlen(result) + 1;
91 RES_SET_H_ERRNO(__res_state(), NO_RECOVERY);
94 strlcpy(bp, result, ep - bp);
98 while (*cp == ' ' || *cp == '\t')
101 ne->n_net = inet_network(cp);
102 ne->n_addrtype = AF_INET;
104 q = ne->n_aliases = ned->net_aliases;
105 cp = strpbrk(cp, " \t");
109 if (*cp == ' ' || *cp == '\t') {
113 if (q > &ned->net_aliases[_MAXALIASES - 1])
115 p = strpbrk(cp, " \t");
118 len = strlen(cp) + 1;
121 strlcpy(bp, cp, ep - bp);
132 _nis_getnetbyname(void *rval, void *cb_data __unused, va_list ap)
138 int *errnop __unused;
140 struct netent *nptr, ne;
141 struct netent_data *ned;
144 name = va_arg(ap, const char *);
145 nptr = va_arg(ap, struct netent *);
146 buffer = va_arg(ap, char *);
147 buflen = va_arg(ap, size_t);
148 errnop = va_arg(ap, int *);
149 h_errnop = va_arg(ap, int *);
151 statp = __res_state();
152 if ((ned = __netent_data_init()) == NULL) {
153 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
154 *h_errnop = statp->res_h_errno;
158 if (_getnetbynis(name, "networks.byname", AF_INET, &ne, ned) != 0) {
159 *h_errnop = statp->res_h_errno;
160 return (NS_NOTFOUND);
162 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
163 *h_errnop = statp->res_h_errno;
164 return (NS_NOTFOUND);
166 *((struct netent **)rval) = nptr;
175 _nis_getnetbyaddr(void *rval, void *cb_data __unused, va_list ap)
182 int *errnop __unused;
184 struct netent *nptr, ne;
185 struct netent_data *ned;
189 unsigned int netbr[4];
193 addr = va_arg(ap, uint32_t);
194 af = va_arg(ap, int);
195 nptr = va_arg(ap, struct netent *);
196 buffer = va_arg(ap, char *);
197 buflen = va_arg(ap, size_t);
198 errnop = va_arg(ap, int *);
199 h_errnop = va_arg(ap, int *);
201 statp = __res_state();
202 if ((ned = __netent_data_init()) == NULL) {
203 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
204 *h_errnop = statp->res_h_errno;
209 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
210 *h_errnop = statp->res_h_errno;
211 errno = EAFNOSUPPORT;
215 for (nn = 4, net2 = addr; net2; net2 >>= 8) {
216 netbr[--nn] = net2 & 0xff;
220 case 3: /* Class A */
221 sprintf(buf, "%u", netbr[3]);
223 case 2: /* Class B */
224 sprintf(buf, "%u.%u", netbr[2], netbr[3]);
226 case 1: /* Class C */
227 sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]);
229 case 0: /* Class D - E */
230 sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1],
236 cp = str + (strlen(str) - 2);
238 while(!strcmp(cp, ".0")) {
240 cp = str + (strlen(str) - 2);
243 if (_getnetbynis(str, "networks.byaddr", af, &ne, ned) != 0) {
244 *h_errnop = statp->res_h_errno;
245 return (NS_NOTFOUND);
247 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
248 *h_errnop = statp->res_h_errno;
249 return (NS_NOTFOUND);
251 *((struct netent **)rval) = nptr;