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 $
26 * $DragonFly: src/lib/libc/net/getnetbynis.c,v 1.4 2005/11/13 02:04:47 swildner Exp $
29 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
42 #include <arpa/nameser.h>
45 #include <rpcsvc/yp_prot.h>
46 #include <rpcsvc/ypclnt.h>
48 #include "netdb_private.h"
52 _getnetbynis(const char *name
, char *map
, int af
, struct netent
*ne
,
53 struct netent_data
*ned
)
59 char ypbuf
[YPMAXRECORD
+ 2];
70 if (ned
->yp_domain
== NULL
)
71 if (yp_get_default_domain (&ned
->yp_domain
))
74 if (yp_match(ned
->yp_domain
, map
, name
, strlen(name
), &result
,
78 bcopy((char *)result
, (char *)&ypbuf
, resultlen
);
79 ypbuf
[resultlen
] = '\0';
81 result
= (char *)&ypbuf
;
83 if ((cp
= index(result
, '\n')))
86 cp
= strpbrk(result
, " \t");
89 ep
= ned
->netbuf
+ sizeof ned
->netbuf
;
90 len
= strlen(result
) + 1;
92 RES_SET_H_ERRNO(__res_state(), NO_RECOVERY
);
95 strlcpy(bp
, result
, ep
- bp
);
99 while (*cp
== ' ' || *cp
== '\t')
102 ne
->n_net
= inet_network(cp
);
103 ne
->n_addrtype
= AF_INET
;
105 q
= ne
->n_aliases
= ned
->net_aliases
;
106 cp
= strpbrk(cp
, " \t");
110 if (*cp
== ' ' || *cp
== '\t') {
114 if (q
> &ned
->net_aliases
[_MAXALIASES
- 1])
116 p
= strpbrk(cp
, " \t");
119 len
= strlen(cp
) + 1;
122 strlcpy(bp
, cp
, ep
- bp
);
133 _nis_getnetbyname(void *rval
, void *cb_data
, va_list ap
)
139 int *errnop
, *h_errnop
;
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
, va_list ap
)
182 int *errnop
, *h_errnop
;
183 struct netent
*nptr
, ne
;
184 struct netent_data
*ned
;
188 unsigned int netbr
[4];
192 addr
= va_arg(ap
, uint32_t);
193 af
= va_arg(ap
, int);
194 nptr
= va_arg(ap
, struct netent
*);
195 buffer
= va_arg(ap
, char *);
196 buflen
= va_arg(ap
, size_t);
197 errnop
= va_arg(ap
, int *);
198 h_errnop
= va_arg(ap
, int *);
200 statp
= __res_state();
201 if ((ned
= __netent_data_init()) == NULL
) {
202 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
203 *h_errnop
= statp
->res_h_errno
;
208 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
209 *h_errnop
= statp
->res_h_errno
;
210 errno
= EAFNOSUPPORT
;
214 for (nn
= 4, net2
= addr
; net2
; net2
>>= 8) {
215 netbr
[--nn
] = net2
& 0xff;
219 case 3: /* Class A */
220 sprintf(buf
, "%u", netbr
[3]);
222 case 2: /* Class B */
223 sprintf(buf
, "%u.%u", netbr
[2], netbr
[3]);
225 case 1: /* Class C */
226 sprintf(buf
, "%u.%u.%u", netbr
[1], netbr
[2], netbr
[3]);
228 case 0: /* Class D - E */
229 sprintf(buf
, "%u.%u.%u.%u", netbr
[0], netbr
[1],
235 cp
= str
+ (strlen(str
) - 2);
237 while(!strcmp(cp
, ".0")) {
239 cp
= str
+ (strlen(str
) - 2);
242 if (_getnetbynis(str
, "networks.byaddr", af
, &ne
, ned
) != 0) {
243 *h_errnop
= statp
->res_h_errno
;
244 return (NS_NOTFOUND
);
246 if (__copy_netent(&ne
, nptr
, buffer
, buflen
) != 0) {
247 *h_errnop
= statp
->res_h_errno
;
248 return (NS_NOTFOUND
);
250 *((struct netent
**)rval
) = nptr
;