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/gethostbynis.c,v 1.28 2006/05/12 15:37:23 ume Exp $
28 #include <sys/param.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <arpa/nameser.h>
41 #include <resolv.h> /* XXX */
44 #include <rpcsvc/yp_prot.h>
45 #include <rpcsvc/ypclnt.h>
47 #include "netdb_private.h"
51 _gethostbynis(const char *name
, char *map
, int af
, struct hostent
*he
,
52 struct hostent_data
*hed
)
57 int resultlen
, size
, addrok
= 0;
58 char ypbuf
[YPMAXRECORD
+ 2];
61 statp
= __res_state();
71 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
75 if (hed
->yp_domain
== NULL
)
76 if (yp_get_default_domain (&hed
->yp_domain
)) {
77 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
81 if (yp_match(hed
->yp_domain
, map
, name
, strlen(name
), &result
,
83 RES_SET_H_ERRNO(statp
, HOST_NOT_FOUND
);
87 /* avoid potential memory leak */
88 bcopy((char *)result
, (char *)&ypbuf
, resultlen
);
89 ypbuf
[resultlen
] = '\0';
91 result
= (char *)&ypbuf
;
93 if ((cp
= index(result
, '\n')))
96 cp
= strpbrk(result
, " \t");
98 he
->h_addr_list
= hed
->h_addr_ptrs
;
99 he
->h_addr
= (char *)hed
->host_addr
;
102 addrok
= inet_aton(result
, (struct in_addr
*)hed
->host_addr
);
105 if (statp
->options
& RES_USE_INET6
) {
106 _map_v4v6_address((char *)hed
->host_addr
,
107 (char *)hed
->host_addr
);
113 addrok
= inet_pton(af
, result
, hed
->host_addr
);
117 RES_SET_H_ERRNO(statp
, HOST_NOT_FOUND
);
120 he
->h_addr_list
[1] = NULL
;
123 while (*cp
== ' ' || *cp
== '\t')
126 ep
= hed
->hostbuf
+ sizeof hed
->hostbuf
;
128 q
= he
->h_aliases
= hed
->host_aliases
;
129 p
= strpbrk(cp
, " \t");
132 size
= strlen(cp
) + 1;
133 if (ep
- bp
< size
) {
134 RES_SET_H_ERRNO(statp
, NO_RECOVERY
);
137 strlcpy(bp
, cp
, ep
- bp
);
141 if (*cp
== ' ' || *cp
== '\t') {
145 if (q
>= &hed
->host_aliases
[_MAXALIASES
- 1])
147 p
= strpbrk(cp
, " \t");
150 size
= strlen(cp
) + 1;
153 strlcpy(bp
, cp
, ep
- bp
);
163 _gethostbynisname_r(const char *name
, int af
, struct hostent
*he
,
164 struct hostent_data
*hed
)
170 map
= "hosts.byname";
173 map
= "ipnodes.byname";
176 return (_gethostbynis(name
, map
, af
, he
, hed
));
180 _gethostbynisaddr_r(const void *addr
, socklen_t len __unused
, int af
,
181 struct hostent
*he
, struct hostent_data
*hed
)
188 map
= "hosts.byaddr";
191 map
= "ipnodes.byaddr";
194 if (inet_ntop(af
, addr
, numaddr
, sizeof(numaddr
)) == NULL
)
196 return (_gethostbynis(numaddr
, map
, af
, he
, hed
));
200 /* XXX _gethostbynisname/_gethostbynisaddr only used by getipnodeby*() */
202 _gethostbynisname(const char *name
, int af
)
206 struct hostent_data
*hed
;
211 statp
= __res_state();
212 if ((he
= __hostent_init()) == NULL
||
213 (hed
= __hostent_data_init()) == NULL
) {
214 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
218 oresopt
= statp
->options
;
219 statp
->options
&= ~RES_USE_INET6
;
220 error
= _gethostbynisname_r(name
, af
, he
, hed
);
221 statp
->options
= oresopt
;
222 return (error
== 0) ? he
: NULL
;
229 _gethostbynisaddr(const void *addr
, socklen_t len
, int af
)
233 struct hostent_data
*hed
;
238 statp
= __res_state();
239 if ((he
= __hostent_init()) == NULL
||
240 (hed
= __hostent_data_init()) == NULL
) {
241 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
245 oresopt
= statp
->options
;
246 statp
->options
&= ~RES_USE_INET6
;
247 error
= _gethostbynisaddr_r(addr
, len
, af
, he
, hed
);
248 statp
->options
= oresopt
;
249 return (error
== 0) ? he
: NULL
;
256 _nis_gethostbyname(void *rval
, void *cb_data __unused
, va_list ap
)
263 int *errnop __unused
;
265 struct hostent
*hptr
, he
;
266 struct hostent_data
*hed
;
269 name
= va_arg(ap
, const char *);
270 af
= va_arg(ap
, int);
271 hptr
= va_arg(ap
, struct hostent
*);
272 buffer
= va_arg(ap
, char *);
273 buflen
= va_arg(ap
, size_t);
274 errnop
= va_arg(ap
, int *);
275 h_errnop
= va_arg(ap
, int *);
277 *((struct hostent
**)rval
) = NULL
;
279 statp
= __res_state();
280 if ((hed
= __hostent_data_init()) == NULL
) {
281 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
282 *h_errnop
= statp
->res_h_errno
;
283 return (NS_NOTFOUND
);
286 if (_gethostbynisname_r(name
, af
, &he
, hed
) != 0) {
287 *h_errnop
= statp
->res_h_errno
;
288 return (NS_NOTFOUND
);
290 if (__copy_hostent(&he
, hptr
, buffer
, buflen
) != 0) {
291 *h_errnop
= statp
->res_h_errno
;
292 return (NS_NOTFOUND
);
294 *((struct hostent
**)rval
) = hptr
;
297 *((struct hostent
**)rval
) = NULL
;
303 _nis_gethostbyaddr(void *rval
, void *cb_data __unused
, va_list ap
)
311 int *errnop __unused
;
313 struct hostent
*hptr
, he
;
314 struct hostent_data
*hed
;
317 addr
= va_arg(ap
, const void *);
318 len
= va_arg(ap
, socklen_t
);
319 af
= va_arg(ap
, int);
320 hptr
= va_arg(ap
, struct hostent
*);
321 buffer
= va_arg(ap
, char *);
322 buflen
= va_arg(ap
, size_t);
323 errnop
= va_arg(ap
, int *);
324 h_errnop
= va_arg(ap
, int *);
326 *((struct hostent
**)rval
) = NULL
;
328 statp
= __res_state();
329 if ((hed
= __hostent_data_init()) == NULL
) {
330 RES_SET_H_ERRNO(statp
, NETDB_INTERNAL
);
331 *h_errnop
= statp
->res_h_errno
;
332 return (NS_NOTFOUND
);
335 if (_gethostbynisaddr_r(addr
, len
, af
, &he
, hed
) != 0) {
336 *h_errnop
= statp
->res_h_errno
;
337 return (NS_NOTFOUND
);
339 if (__copy_hostent(&he
, hptr
, buffer
, buflen
) != 0) {
340 *h_errnop
= statp
->res_h_errno
;
341 return (NS_NOTFOUND
);
343 *((struct hostent
**)rval
) = hptr
;
346 *((struct hostent
**)rval
) = NULL
;