Deleted Added
full compact
getnetbydns.c (145626) getnetbydns.c (146244)
1/*-
2 * Copyright (c) 1985, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 * -
33 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * --Copyright--
52 */
53/* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
54 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
55 *
56 * Permission to use, copy, modify, and distribute this software for any
57 * purpose with or without fee is hereby granted, provided that the above
58 * copyright notice and this permission notice appear in all copies.
59 */
60
61#if defined(LIBC_SCCS) && !defined(lint)
62static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
63#endif /* LIBC_SCCS and not lint */
64#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1985, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 * -
33 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * --Copyright--
52 */
53/* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
54 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
55 *
56 * Permission to use, copy, modify, and distribute this software for any
57 * purpose with or without fee is hereby granted, provided that the above
58 * copyright notice and this permission notice appear in all copies.
59 */
60
61#if defined(LIBC_SCCS) && !defined(lint)
62static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
63#endif /* LIBC_SCCS and not lint */
64#include <sys/cdefs.h>
65__FBSDID("$FreeBSD: head/lib/libc/net/getnetbydns.c 145626 2005-04-28 15:32:55Z ume $");
65__FBSDID("$FreeBSD: head/lib/libc/net/getnetbydns.c 146244 2005-05-15 20:15:15Z ume $");
66
67#include <sys/param.h>
68#include <sys/socket.h>
69#include <netinet/in.h>
70#include <arpa/inet.h>
71#include <arpa/nameser.h>
72
73#include <errno.h>
74#include <stdio.h>
75#include <stdlib.h>
76#include <netdb.h>
77#include <resolv.h>
78#include <ctype.h>
79#include <string.h>
80#include <unistd.h>
81#include <syslog.h>
82#include <stdarg.h>
83#include <nsswitch.h>
84
85#include "netdb_private.h"
86#include "res_config.h"
87
88extern int h_errno;
89
90#define BYADDR 0
91#define BYNAME 1
92
93#define MAXPACKET (64*1024)
94
95typedef union {
96 HEADER hdr;
97 u_char buf[MAXPACKET];
98} querybuf;
99
100typedef union {
101 long al;
102 char ac;
103} align;
104
105/*
106 * Reverse the order of first four dotted entries of in.
107 * Out must contain space for at least strlen(in) characters.
108 * The result does not include any leading 0s of in.
109 */
110static void
111ipreverse(char *in, char *out)
112{
113 char *pos[4];
114 int len[4];
115 char *p, *start;
116 int i = 0;
117 int leading = 1;
118
119 /* Fill-in element positions and lengths: pos[], len[]. */
120 start = p = in;
121 for (;;) {
122 if (*p == '.' || *p == '\0') {
123 /* Leading 0? */
124 if (leading && p - start == 1 && *start == '0')
125 len[i] = 0;
126 else {
127 len[i] = p - start;
128 leading = 0;
129 }
130 pos[i] = start;
131 start = p + 1;
132 i++;
133 }
134 if (i == 4)
135 break;
136 if (*p == 0) {
137 for (; i < 4; i++) {
138 pos[i] = p;
139 len[i] = 0;
140 }
141 break;
142 }
143 p++;
144 }
145
146 /* Copy the entries in reverse order */
147 p = out;
148 leading = 1;
149 for (i = 3; i >= 0; i--) {
150 memcpy(p, pos[i], len[i]);
151 if (len[i])
152 leading = 0;
153 p += len[i];
154 /* Need a . separator? */
155 if (!leading && i > 0 && len[i - 1])
156 *p++ = '.';
157 }
158 *p = '\0';
159}
160
161static int
162getnetanswer(querybuf *answer, int anslen, int net_i, struct netent *ne,
163 struct netent_data *ned)
164{
165
166 HEADER *hp;
167 u_char *cp;
168 int n;
169 u_char *eom;
170 int type, class, ancount, qdcount, haveanswer;
171 char aux[MAXHOSTNAMELEN];
172 char ans[MAXHOSTNAMELEN];
173 char *in, *bp, *ep, **ap;
174
175 /*
176 * find first satisfactory answer
177 *
178 * answer --> +------------+ ( MESSAGE )
179 * | Header |
180 * +------------+
181 * | Question | the question for the name server
182 * +------------+
183 * | Answer | RRs answering the question
184 * +------------+
185 * | Authority | RRs pointing toward an authority
186 * | Additional | RRs holding additional information
187 * +------------+
188 */
189 eom = answer->buf + anslen;
190 hp = &answer->hdr;
191 ancount = ntohs(hp->ancount); /* #/records in the answer section */
192 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
193 bp = ned->netbuf;
194 ep = ned->netbuf + sizeof(ned->netbuf);
195 cp = answer->buf + HFIXEDSZ;
196 if (!qdcount) {
197 if (hp->aa)
198 h_errno = HOST_NOT_FOUND;
199 else
200 h_errno = TRY_AGAIN;
201 return -1;
202 }
203 while (qdcount-- > 0)
204 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
205 ap = ned->net_aliases;
206 *ap = NULL;
207 ne->n_aliases = ned->net_aliases;
208 haveanswer = 0;
209 while (--ancount >= 0 && cp < eom) {
210 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
211 if ((n < 0) || !res_dnok(bp))
212 break;
213 cp += n;
214 ans[0] = '\0';
215 (void)strncpy(&ans[0], bp, sizeof(ans) - 1);
216 ans[sizeof(ans) - 1] = '\0';
217 GETSHORT(type, cp);
218 GETSHORT(class, cp);
219 cp += INT32SZ; /* TTL */
220 GETSHORT(n, cp);
221 if (class == C_IN && type == T_PTR) {
222 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
223 if ((n < 0) || !res_hnok(bp)) {
224 cp += n;
225 return -1;
226 }
227 cp += n;
228 *ap++ = bp;
229 n = strlen(bp) + 1;
230 bp += n;
231 ne->n_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
232 haveanswer++;
233 }
234 }
235 if (haveanswer) {
236 *ap = NULL;
237 switch (net_i) {
238 case BYADDR:
239 ne->n_name = *ne->n_aliases;
240 ne->n_net = 0L;
241 break;
242 case BYNAME:
243 in = *ne->n_aliases;
244 n = strlen(ans) + 1;
245 if (ep - bp < n) {
246 h_errno = NETDB_INTERNAL;
247 errno = ENOBUFS;
248 return -1;
249 }
250 strlcpy(bp, ans, ep - bp);
251 ne->n_name = bp;
252 if (strlen(in) + 1 > sizeof(aux)) {
253 h_errno = NETDB_INTERNAL;
254 errno = ENOBUFS;
255 return -1;
256 }
257 ipreverse(in, aux);
258 ne->n_net = inet_network(aux);
259 break;
260 }
261 ne->n_aliases++;
66
67#include <sys/param.h>
68#include <sys/socket.h>
69#include <netinet/in.h>
70#include <arpa/inet.h>
71#include <arpa/nameser.h>
72
73#include <errno.h>
74#include <stdio.h>
75#include <stdlib.h>
76#include <netdb.h>
77#include <resolv.h>
78#include <ctype.h>
79#include <string.h>
80#include <unistd.h>
81#include <syslog.h>
82#include <stdarg.h>
83#include <nsswitch.h>
84
85#include "netdb_private.h"
86#include "res_config.h"
87
88extern int h_errno;
89
90#define BYADDR 0
91#define BYNAME 1
92
93#define MAXPACKET (64*1024)
94
95typedef union {
96 HEADER hdr;
97 u_char buf[MAXPACKET];
98} querybuf;
99
100typedef union {
101 long al;
102 char ac;
103} align;
104
105/*
106 * Reverse the order of first four dotted entries of in.
107 * Out must contain space for at least strlen(in) characters.
108 * The result does not include any leading 0s of in.
109 */
110static void
111ipreverse(char *in, char *out)
112{
113 char *pos[4];
114 int len[4];
115 char *p, *start;
116 int i = 0;
117 int leading = 1;
118
119 /* Fill-in element positions and lengths: pos[], len[]. */
120 start = p = in;
121 for (;;) {
122 if (*p == '.' || *p == '\0') {
123 /* Leading 0? */
124 if (leading && p - start == 1 && *start == '0')
125 len[i] = 0;
126 else {
127 len[i] = p - start;
128 leading = 0;
129 }
130 pos[i] = start;
131 start = p + 1;
132 i++;
133 }
134 if (i == 4)
135 break;
136 if (*p == 0) {
137 for (; i < 4; i++) {
138 pos[i] = p;
139 len[i] = 0;
140 }
141 break;
142 }
143 p++;
144 }
145
146 /* Copy the entries in reverse order */
147 p = out;
148 leading = 1;
149 for (i = 3; i >= 0; i--) {
150 memcpy(p, pos[i], len[i]);
151 if (len[i])
152 leading = 0;
153 p += len[i];
154 /* Need a . separator? */
155 if (!leading && i > 0 && len[i - 1])
156 *p++ = '.';
157 }
158 *p = '\0';
159}
160
161static int
162getnetanswer(querybuf *answer, int anslen, int net_i, struct netent *ne,
163 struct netent_data *ned)
164{
165
166 HEADER *hp;
167 u_char *cp;
168 int n;
169 u_char *eom;
170 int type, class, ancount, qdcount, haveanswer;
171 char aux[MAXHOSTNAMELEN];
172 char ans[MAXHOSTNAMELEN];
173 char *in, *bp, *ep, **ap;
174
175 /*
176 * find first satisfactory answer
177 *
178 * answer --> +------------+ ( MESSAGE )
179 * | Header |
180 * +------------+
181 * | Question | the question for the name server
182 * +------------+
183 * | Answer | RRs answering the question
184 * +------------+
185 * | Authority | RRs pointing toward an authority
186 * | Additional | RRs holding additional information
187 * +------------+
188 */
189 eom = answer->buf + anslen;
190 hp = &answer->hdr;
191 ancount = ntohs(hp->ancount); /* #/records in the answer section */
192 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
193 bp = ned->netbuf;
194 ep = ned->netbuf + sizeof(ned->netbuf);
195 cp = answer->buf + HFIXEDSZ;
196 if (!qdcount) {
197 if (hp->aa)
198 h_errno = HOST_NOT_FOUND;
199 else
200 h_errno = TRY_AGAIN;
201 return -1;
202 }
203 while (qdcount-- > 0)
204 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
205 ap = ned->net_aliases;
206 *ap = NULL;
207 ne->n_aliases = ned->net_aliases;
208 haveanswer = 0;
209 while (--ancount >= 0 && cp < eom) {
210 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
211 if ((n < 0) || !res_dnok(bp))
212 break;
213 cp += n;
214 ans[0] = '\0';
215 (void)strncpy(&ans[0], bp, sizeof(ans) - 1);
216 ans[sizeof(ans) - 1] = '\0';
217 GETSHORT(type, cp);
218 GETSHORT(class, cp);
219 cp += INT32SZ; /* TTL */
220 GETSHORT(n, cp);
221 if (class == C_IN && type == T_PTR) {
222 n = dn_expand(answer->buf, eom, cp, bp, ep - bp);
223 if ((n < 0) || !res_hnok(bp)) {
224 cp += n;
225 return -1;
226 }
227 cp += n;
228 *ap++ = bp;
229 n = strlen(bp) + 1;
230 bp += n;
231 ne->n_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC;
232 haveanswer++;
233 }
234 }
235 if (haveanswer) {
236 *ap = NULL;
237 switch (net_i) {
238 case BYADDR:
239 ne->n_name = *ne->n_aliases;
240 ne->n_net = 0L;
241 break;
242 case BYNAME:
243 in = *ne->n_aliases;
244 n = strlen(ans) + 1;
245 if (ep - bp < n) {
246 h_errno = NETDB_INTERNAL;
247 errno = ENOBUFS;
248 return -1;
249 }
250 strlcpy(bp, ans, ep - bp);
251 ne->n_name = bp;
252 if (strlen(in) + 1 > sizeof(aux)) {
253 h_errno = NETDB_INTERNAL;
254 errno = ENOBUFS;
255 return -1;
256 }
257 ipreverse(in, aux);
258 ne->n_net = inet_network(aux);
259 break;
260 }
261 ne->n_aliases++;
262#if __LONG_BIT == 64
263 ne->__n_pad0 = 0; /* ABI compatibility */
264#endif
262 return 0;
263 }
264 h_errno = TRY_AGAIN;
265 return -1;
266}
267
268int
269_dns_getnetbyaddr(void *rval, void *cb_data, va_list ap)
270{
265 return 0;
266 }
267 h_errno = TRY_AGAIN;
268 return -1;
269}
270
271int
272_dns_getnetbyaddr(void *rval, void *cb_data, va_list ap)
273{
271 unsigned long net;
274 uint32_t net;
272 int net_type;
273 struct netent *ne;
274 struct netent_data *ned;
275 unsigned int netbr[4];
276 int nn, anslen, error;
277 querybuf *buf;
278 char qbuf[MAXDNAME];
275 int net_type;
276 struct netent *ne;
277 struct netent_data *ned;
278 unsigned int netbr[4];
279 int nn, anslen, error;
280 querybuf *buf;
281 char qbuf[MAXDNAME];
279 unsigned long net2;
282 uint32_t net2;
280
283
281 net = va_arg(ap, unsigned long);
284 net = va_arg(ap, uint32_t);
282 net_type = va_arg(ap, int);
283 ne = va_arg(ap, struct netent *);
284 ned = va_arg(ap, struct netent_data *);
285
286 if (net_type != AF_INET)
287 return NS_UNAVAIL;
288
289 for (nn = 4, net2 = net; net2; net2 >>= 8)
290 netbr[--nn] = net2 & 0xff;
291 switch (nn) {
292 case 3: /* Class A */
293 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
294 break;
295 case 2: /* Class B */
296 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
297 break;
298 case 1: /* Class C */
299 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
300 netbr[1]);
301 break;
302 case 0: /* Class D - E */
303 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
304 netbr[1], netbr[0]);
305 break;
306 }
307 if ((buf = malloc(sizeof(*buf))) == NULL) {
308 h_errno = NETDB_INTERNAL;
309 return NS_NOTFOUND;
310 }
311 anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)buf, sizeof(*buf));
312 if (anslen < 0) {
313 free(buf);
314#ifdef DEBUG
315 if (_res.options & RES_DEBUG)
316 printf("res_search failed\n");
317#endif
318 return NS_UNAVAIL;
319 } else if (anslen > sizeof(*buf)) {
320 free(buf);
321#ifdef DEBUG
322 if (_res.options & RES_DEBUG)
323 printf("res_search static buffer too small\n");
324#endif
325 return NS_UNAVAIL;
326 }
327 error = getnetanswer(buf, anslen, BYADDR, ne, ned);
328 free(buf);
329 if (error == 0) {
285 net_type = va_arg(ap, int);
286 ne = va_arg(ap, struct netent *);
287 ned = va_arg(ap, struct netent_data *);
288
289 if (net_type != AF_INET)
290 return NS_UNAVAIL;
291
292 for (nn = 4, net2 = net; net2; net2 >>= 8)
293 netbr[--nn] = net2 & 0xff;
294 switch (nn) {
295 case 3: /* Class A */
296 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
297 break;
298 case 2: /* Class B */
299 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
300 break;
301 case 1: /* Class C */
302 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
303 netbr[1]);
304 break;
305 case 0: /* Class D - E */
306 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
307 netbr[1], netbr[0]);
308 break;
309 }
310 if ((buf = malloc(sizeof(*buf))) == NULL) {
311 h_errno = NETDB_INTERNAL;
312 return NS_NOTFOUND;
313 }
314 anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)buf, sizeof(*buf));
315 if (anslen < 0) {
316 free(buf);
317#ifdef DEBUG
318 if (_res.options & RES_DEBUG)
319 printf("res_search failed\n");
320#endif
321 return NS_UNAVAIL;
322 } else if (anslen > sizeof(*buf)) {
323 free(buf);
324#ifdef DEBUG
325 if (_res.options & RES_DEBUG)
326 printf("res_search static buffer too small\n");
327#endif
328 return NS_UNAVAIL;
329 }
330 error = getnetanswer(buf, anslen, BYADDR, ne, ned);
331 free(buf);
332 if (error == 0) {
330 unsigned u_net = net; /* maybe net should be unsigned ? */
331
332 /* Strip trailing zeros */
333 /* Strip trailing zeros */
333 while ((u_net & 0xff) == 0 && u_net != 0)
334 u_net >>= 8;
335 ne->n_net = u_net;
334 while ((net & 0xff) == 0 && net != 0)
335 net >>= 8;
336 ne->n_net = net;
337#if __LONG_BIT == 64
338 ne->__n_pad0 = 0; /* ABI compatibility */
339#endif
336 return NS_SUCCESS;
337 }
338 return NS_NOTFOUND;
339}
340
341int
342_dns_getnetbyname(void *rval, void *cb_data, va_list ap)
343{
344 const char *net;
345 struct netent *ne;
346 struct netent_data *ned;
347 int anslen, error;
348 querybuf *buf;
349 char qbuf[MAXDNAME];
350
351 net = va_arg(ap, const char *);
352 ne = va_arg(ap, struct netent *);
353 ned = va_arg(ap, struct netent_data *);
354
355 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
356 h_errno = NETDB_INTERNAL;
357 return NS_UNAVAIL;
358 }
359 if ((buf = malloc(sizeof(*buf))) == NULL) {
360 h_errno = NETDB_INTERNAL;
361 return NS_NOTFOUND;
362 }
363 strncpy(qbuf, net, sizeof(qbuf) - 1);
364 qbuf[sizeof(qbuf) - 1] = '\0';
365 anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)buf, sizeof(*buf));
366 if (anslen < 0) {
367 free(buf);
368#ifdef DEBUG
369 if (_res.options & RES_DEBUG)
370 printf("res_search failed\n");
371#endif
372 return NS_UNAVAIL;
373 } else if (anslen > sizeof(*buf)) {
374 free(buf);
375#ifdef DEBUG
376 if (_res.options & RES_DEBUG)
377 printf("res_search static buffer too small\n");
378#endif
379 return NS_UNAVAIL;
380 }
381 error = getnetanswer(buf, anslen, BYNAME, ne, ned);
382 free(buf);
383 return (error == 0) ? NS_SUCCESS : NS_NOTFOUND;
384}
385
386void
387_setnetdnsent(stayopen)
388 int stayopen;
389{
390 if (stayopen)
391 _res.options |= RES_STAYOPEN | RES_USEVC;
392}
393
394void
395_endnetdnsent()
396{
397 _res.options &= ~(RES_STAYOPEN | RES_USEVC);
398 res_close();
399}
340 return NS_SUCCESS;
341 }
342 return NS_NOTFOUND;
343}
344
345int
346_dns_getnetbyname(void *rval, void *cb_data, va_list ap)
347{
348 const char *net;
349 struct netent *ne;
350 struct netent_data *ned;
351 int anslen, error;
352 querybuf *buf;
353 char qbuf[MAXDNAME];
354
355 net = va_arg(ap, const char *);
356 ne = va_arg(ap, struct netent *);
357 ned = va_arg(ap, struct netent_data *);
358
359 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
360 h_errno = NETDB_INTERNAL;
361 return NS_UNAVAIL;
362 }
363 if ((buf = malloc(sizeof(*buf))) == NULL) {
364 h_errno = NETDB_INTERNAL;
365 return NS_NOTFOUND;
366 }
367 strncpy(qbuf, net, sizeof(qbuf) - 1);
368 qbuf[sizeof(qbuf) - 1] = '\0';
369 anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)buf, sizeof(*buf));
370 if (anslen < 0) {
371 free(buf);
372#ifdef DEBUG
373 if (_res.options & RES_DEBUG)
374 printf("res_search failed\n");
375#endif
376 return NS_UNAVAIL;
377 } else if (anslen > sizeof(*buf)) {
378 free(buf);
379#ifdef DEBUG
380 if (_res.options & RES_DEBUG)
381 printf("res_search static buffer too small\n");
382#endif
383 return NS_UNAVAIL;
384 }
385 error = getnetanswer(buf, anslen, BYNAME, ne, ned);
386 free(buf);
387 return (error == 0) ? NS_SUCCESS : NS_NOTFOUND;
388}
389
390void
391_setnetdnsent(stayopen)
392 int stayopen;
393{
394 if (stayopen)
395 _res.options |= RES_STAYOPEN | RES_USEVC;
396}
397
398void
399_endnetdnsent()
400{
401 _res.options &= ~(RES_STAYOPEN | RES_USEVC);
402 res_close();
403}