diff options
Diffstat (limited to 'src/lib/libc/net/getnetnamadr.c')
-rw-r--r-- | src/lib/libc/net/getnetnamadr.c | 382 |
1 files changed, 382 insertions, 0 deletions
diff --git a/src/lib/libc/net/getnetnamadr.c b/src/lib/libc/net/getnetnamadr.c new file mode 100644 index 0000000000..de208bbac9 --- /dev/null +++ b/src/lib/libc/net/getnetnamadr.c | |||
@@ -0,0 +1,382 @@ | |||
1 | /* $OpenBSD: getnetnamadr.c,v 1.10 1997/12/02 01:34:05 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1997, Jason Downs. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. All advertising materials mentioning features or use of this software | ||
15 | * must display the following acknowledgement: | ||
16 | * This product includes software developed by Jason Downs for the | ||
17 | * OpenBSD system. | ||
18 | * 4. Neither the name(s) of the author(s) nor the name OpenBSD | ||
19 | * may be used to endorse or promote products derived from this software | ||
20 | * without specific prior written permission. | ||
21 | * | ||
22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS | ||
23 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, | ||
26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
29 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | * SUCH DAMAGE. | ||
33 | */ | ||
34 | /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro | ||
35 | * Dep. Matematica Universidade de Coimbra, Portugal, Europe | ||
36 | * | ||
37 | * Permission to use, copy, modify, and distribute this software for any | ||
38 | * purpose with or without fee is hereby granted, provided that the above | ||
39 | * copyright notice and this permission notice appear in all copies. | ||
40 | */ | ||
41 | /* | ||
42 | * Copyright (c) 1983, 1993 | ||
43 | * The Regents of the University of California. All rights reserved. | ||
44 | * | ||
45 | * Redistribution and use in source and binary forms, with or without | ||
46 | * modification, are permitted provided that the following conditions | ||
47 | * are met: | ||
48 | * 1. Redistributions of source code must retain the above copyright | ||
49 | * notice, this list of conditions and the following disclaimer. | ||
50 | * 2. Redistributions in binary form must reproduce the above copyright | ||
51 | * notice, this list of conditions and the following disclaimer in the | ||
52 | * documentation and/or other materials provided with the distribution. | ||
53 | * 3. All advertising materials mentioning features or use of this software | ||
54 | * must display the following acknowledgement: | ||
55 | * This product includes software developed by the University of | ||
56 | * California, Berkeley and its contributors. | ||
57 | * 4. Neither the name of the University nor the names of its contributors | ||
58 | * may be used to endorse or promote products derived from this software | ||
59 | * without specific prior written permission. | ||
60 | * | ||
61 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
62 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
63 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
64 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
65 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
66 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
67 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
68 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
69 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
70 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
71 | * SUCH DAMAGE. | ||
72 | */ | ||
73 | |||
74 | #if defined(LIBC_SCCS) && !defined(lint) | ||
75 | #if 0 | ||
76 | static char sccsid[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93"; | ||
77 | static char sccsid_[] = "from getnetnamadr.c 1.4 (Coimbra) 93/06/03"; | ||
78 | static char rcsid[] = "$From: getnetnamadr.c,v 8.7 1996/08/05 08:31:35 vixie Exp $"; | ||
79 | #else | ||
80 | static char rcsid[] = "$OpenBSD: getnetnamadr.c,v 1.10 1997/12/02 01:34:05 deraadt Exp $"; | ||
81 | #endif | ||
82 | #endif /* LIBC_SCCS and not lint */ | ||
83 | |||
84 | #include <sys/types.h> | ||
85 | #include <sys/param.h> | ||
86 | #include <sys/socket.h> | ||
87 | #include <netinet/in.h> | ||
88 | #include <arpa/inet.h> | ||
89 | #include <arpa/nameser.h> | ||
90 | |||
91 | #include <stdio.h> | ||
92 | #include <netdb.h> | ||
93 | #include <resolv.h> | ||
94 | #include <ctype.h> | ||
95 | #include <errno.h> | ||
96 | #include <string.h> | ||
97 | |||
98 | extern int h_errno; | ||
99 | |||
100 | struct netent *_getnetbyaddr __P((in_addr_t net, int type)); | ||
101 | struct netent *_getnetbyname __P((const char *name)); | ||
102 | |||
103 | int _hokchar __P((const char *)); | ||
104 | |||
105 | #define BYADDR 0 | ||
106 | #define BYNAME 1 | ||
107 | #define MAXALIASES 35 | ||
108 | |||
109 | #if PACKETSZ > 1024 | ||
110 | #define MAXPACKET PACKETSZ | ||
111 | #else | ||
112 | #define MAXPACKET 1024 | ||
113 | #endif | ||
114 | |||
115 | typedef union { | ||
116 | HEADER hdr; | ||
117 | u_char buf[MAXPACKET]; | ||
118 | } querybuf; | ||
119 | |||
120 | typedef union { | ||
121 | long al; | ||
122 | char ac; | ||
123 | } align; | ||
124 | |||
125 | static struct netent * | ||
126 | getnetanswer(answer, anslen, net_i) | ||
127 | querybuf *answer; | ||
128 | int anslen; | ||
129 | int net_i; | ||
130 | { | ||
131 | |||
132 | register HEADER *hp; | ||
133 | register u_char *cp; | ||
134 | register int n; | ||
135 | u_char *eom; | ||
136 | int type, class, buflen, ancount, qdcount, haveanswer, i, nchar; | ||
137 | char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN]; | ||
138 | char *in, *st, *pauxt, *bp, **ap; | ||
139 | char *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0; | ||
140 | static struct netent net_entry; | ||
141 | static char *net_aliases[MAXALIASES], netbuf[BUFSIZ+1]; | ||
142 | |||
143 | /* | ||
144 | * find first satisfactory answer | ||
145 | * | ||
146 | * answer --> +------------+ ( MESSAGE ) | ||
147 | * | Header | | ||
148 | * +------------+ | ||
149 | * | Question | the question for the name server | ||
150 | * +------------+ | ||
151 | * | Answer | RRs answering the question | ||
152 | * +------------+ | ||
153 | * | Authority | RRs pointing toward an authority | ||
154 | * | Additional | RRs holding additional information | ||
155 | * +------------+ | ||
156 | */ | ||
157 | eom = answer->buf + anslen; | ||
158 | hp = &answer->hdr; | ||
159 | ancount = ntohs(hp->ancount); /* #/records in the answer section */ | ||
160 | qdcount = ntohs(hp->qdcount); /* #/entries in the question section */ | ||
161 | bp = netbuf; | ||
162 | buflen = sizeof(netbuf); | ||
163 | cp = answer->buf + HFIXEDSZ; | ||
164 | if (!qdcount) { | ||
165 | if (hp->aa) | ||
166 | h_errno = HOST_NOT_FOUND; | ||
167 | else | ||
168 | h_errno = TRY_AGAIN; | ||
169 | return (NULL); | ||
170 | } | ||
171 | while (qdcount-- > 0) | ||
172 | cp += __dn_skipname(cp, eom) + QFIXEDSZ; | ||
173 | ap = net_aliases; | ||
174 | *ap = NULL; | ||
175 | net_entry.n_aliases = net_aliases; | ||
176 | haveanswer = 0; | ||
177 | while (--ancount >= 0 && cp < eom) { | ||
178 | n = dn_expand(answer->buf, eom, cp, bp, buflen); | ||
179 | #ifdef USE_RESOLV_NAME_OK | ||
180 | if ((n < 0) || !res_dnok(bp)) | ||
181 | #else | ||
182 | if ((n < 0) || !_hokchar(bp)) | ||
183 | #endif | ||
184 | break; | ||
185 | cp += n; | ||
186 | ans[0] = '\0'; | ||
187 | (void)strncpy(&ans[0], bp, sizeof ans-1); | ||
188 | ans[sizeof ans-1] = '\0'; | ||
189 | GETSHORT(type, cp); | ||
190 | GETSHORT(class, cp); | ||
191 | cp += INT32SZ; /* TTL */ | ||
192 | GETSHORT(n, cp); | ||
193 | if (class == C_IN && type == T_PTR) { | ||
194 | n = dn_expand(answer->buf, eom, cp, bp, buflen); | ||
195 | if ((n < 0) || !res_hnok(bp)) { | ||
196 | cp += n; | ||
197 | return (NULL); | ||
198 | } | ||
199 | cp += n; | ||
200 | *ap++ = bp; | ||
201 | bp += strlen(bp) + 1; | ||
202 | net_entry.n_addrtype = | ||
203 | (class == C_IN) ? AF_INET : AF_UNSPEC; | ||
204 | haveanswer++; | ||
205 | } | ||
206 | } | ||
207 | if (haveanswer) { | ||
208 | *ap = NULL; | ||
209 | switch (net_i) { | ||
210 | case BYADDR: | ||
211 | net_entry.n_name = *net_entry.n_aliases; | ||
212 | net_entry.n_net = 0L; | ||
213 | break; | ||
214 | case BYNAME: | ||
215 | in = *net_entry.n_aliases; | ||
216 | net_entry.n_name = &ans[0]; | ||
217 | aux2[0] = '\0'; | ||
218 | for (i = 0; i < 4; i++) { | ||
219 | for (st = in, nchar = 0; | ||
220 | *st != '.'; | ||
221 | st++, nchar++) | ||
222 | ; | ||
223 | if (nchar != 1 || *in != '0' || flag) { | ||
224 | flag = 1; | ||
225 | (void)strncpy(paux1, | ||
226 | (i==0) ? in : in-1, | ||
227 | (i==0) ?nchar : nchar+1); | ||
228 | paux1[(i==0) ? nchar : nchar+1] = '\0'; | ||
229 | pauxt = paux2; | ||
230 | paux2 = strcat(paux1, paux2); | ||
231 | paux1 = pauxt; | ||
232 | } | ||
233 | in = ++st; | ||
234 | } | ||
235 | net_entry.n_net = inet_network(paux2); | ||
236 | break; | ||
237 | } | ||
238 | net_entry.n_aliases++; | ||
239 | return (&net_entry); | ||
240 | } | ||
241 | h_errno = TRY_AGAIN; | ||
242 | return (NULL); | ||
243 | } | ||
244 | |||
245 | struct netent * | ||
246 | getnetbyaddr(net, net_type) | ||
247 | register in_addr_t net; | ||
248 | register int net_type; | ||
249 | { | ||
250 | unsigned int netbr[4]; | ||
251 | int nn, anslen; | ||
252 | querybuf buf; | ||
253 | char qbuf[MAXDNAME]; | ||
254 | in_addr_t net2; | ||
255 | struct netent *net_entry = NULL; | ||
256 | char lookups[MAXDNSLUS]; | ||
257 | int i; | ||
258 | |||
259 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) | ||
260 | return(_getnetbyaddr(net, net_type)); | ||
261 | |||
262 | bcopy(_res.lookups, lookups, sizeof lookups); | ||
263 | if (lookups[0] == '\0') | ||
264 | strncpy(lookups, "bf", sizeof lookups); | ||
265 | |||
266 | for (i = 0; i < MAXDNSLUS && lookups[i]; i++) { | ||
267 | switch (lookups[i]) { | ||
268 | #ifdef YP | ||
269 | case 'y': | ||
270 | /* There is no YP support. */ | ||
271 | break; | ||
272 | #endif /* YP */ | ||
273 | case 'b': | ||
274 | if (net_type != AF_INET) | ||
275 | break; /* DNS only supports AF_INET? */ | ||
276 | |||
277 | for (nn = 4, net2 = net; net2; net2 >>= 8) | ||
278 | netbr[--nn] = net2 & 0xff; | ||
279 | switch (nn) { | ||
280 | case 3: /* Class A */ | ||
281 | snprintf(qbuf, sizeof(qbuf), | ||
282 | "0.0.0.%u.in-addr.arpa", netbr[3]); | ||
283 | break; | ||
284 | case 2: /* Class B */ | ||
285 | snprintf(qbuf, sizeof(qbuf), | ||
286 | "0.0.%u.%u.in-addr.arpa", | ||
287 | netbr[3], netbr[2]); | ||
288 | break; | ||
289 | case 1: /* Class C */ | ||
290 | snprintf(qbuf, sizeof(qbuf), | ||
291 | "0.%u.%u.%u.in-addr.arpa", | ||
292 | netbr[3], netbr[2], netbr[1]); | ||
293 | break; | ||
294 | case 0: /* Class D - E */ | ||
295 | snprintf(qbuf, sizeof(qbuf), | ||
296 | "%u.%u.%u.%u.in-addr.arpa", | ||
297 | netbr[3], netbr[2], netbr[1], netbr[0]); | ||
298 | break; | ||
299 | } | ||
300 | anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf, | ||
301 | sizeof(buf)); | ||
302 | if (anslen < 0) { | ||
303 | #ifdef DEBUG | ||
304 | if (_res.options & RES_DEBUG) | ||
305 | printf("res_query failed\n"); | ||
306 | #endif | ||
307 | break; | ||
308 | } | ||
309 | net_entry = getnetanswer(&buf, anslen, BYADDR); | ||
310 | if (net_entry != NULL) { | ||
311 | unsigned u_net = net; /* maybe net should be unsigned ? */ | ||
312 | |||
313 | /* Strip trailing zeros */ | ||
314 | while ((u_net & 0xff) == 0 && u_net != 0) | ||
315 | u_net >>= 8; | ||
316 | net_entry->n_net = u_net; | ||
317 | return (net_entry); | ||
318 | } | ||
319 | break; | ||
320 | case 'f': | ||
321 | net_entry = _getnetbyaddr(net, net_type); | ||
322 | if (net_entry != NULL) | ||
323 | return (net_entry); | ||
324 | } | ||
325 | } | ||
326 | |||
327 | /* Nothing matched. */ | ||
328 | return (NULL); | ||
329 | } | ||
330 | |||
331 | struct netent * | ||
332 | getnetbyname(net) | ||
333 | register const char *net; | ||
334 | { | ||
335 | int anslen; | ||
336 | querybuf buf; | ||
337 | char qbuf[MAXDNAME]; | ||
338 | struct netent *net_entry = NULL; | ||
339 | char lookups[MAXDNSLUS]; | ||
340 | int i; | ||
341 | |||
342 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) | ||
343 | return (_getnetbyname(net)); | ||
344 | |||
345 | bcopy(_res.lookups, lookups, sizeof lookups); | ||
346 | if (lookups[0] == '\0') | ||
347 | strncpy(lookups, "bf", sizeof lookups); | ||
348 | |||
349 | for (i = 0; i < MAXDNSLUS && lookups[i]; i++) { | ||
350 | switch (lookups[i]) { | ||
351 | #ifdef YP | ||
352 | case 'y': | ||
353 | /* There is no YP support. */ | ||
354 | break; | ||
355 | #endif /* YP */ | ||
356 | case 'b': | ||
357 | strncpy(qbuf, net, sizeof qbuf-1); | ||
358 | qbuf[sizeof qbuf-1] = '\0'; | ||
359 | anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)&buf, | ||
360 | sizeof(buf)); | ||
361 | if (anslen < 0) { | ||
362 | #ifdef DEBUG | ||
363 | if (_res.options & RES_DEBUG) | ||
364 | printf("res_query failed\n"); | ||
365 | #endif | ||
366 | break; | ||
367 | } | ||
368 | net_entry = getnetanswer(&buf, anslen, BYNAME); | ||
369 | if (net_entry != NULL) | ||
370 | return (net_entry); | ||
371 | break; | ||
372 | case 'f': | ||
373 | net_entry = _getnetbyname(net); | ||
374 | if (net_entry != NULL) | ||
375 | return (net_entry); | ||
376 | break; | ||
377 | } | ||
378 | } | ||
379 | |||
380 | /* Nothing matched. */ | ||
381 | return (NULL); | ||
382 | } | ||