]> git.saurik.com Git - apple/libinfo.git/blob - dns.subproj/getnetnamadr.c
Libinfo-129.4.tar.gz
[apple/libinfo.git] / dns.subproj / getnetnamadr.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
25 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
26 *
27 * Permission to use, copy, modify, and distribute this software for any
28 * purpose with or without fee is hereby granted, provided that the above
29 * copyright notice and this permission notice appear in all copies.
30 */
31 /*
32 * Copyright (c) 1983, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 */
63
64 #if defined(LIBC_SCCS) && !defined(lint)
65 static char sccsid[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93";
66 static char sccsid_[] = "from getnetnamadr.c 1.4 (Coimbra) 93/06/03";
67 static char rcsid[] = "$Id: getnetnamadr.c,v 1.3.44.1 2002/11/19 01:20:51 bbraun Exp $";
68 #endif /* LIBC_SCCS and not lint */
69
70 #include <sys/param.h>
71 #include <sys/socket.h>
72 #include <netinet/in.h>
73 #include <arpa/inet.h>
74 #include <arpa/nameser.h>
75
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <netdb.h>
79 #include <resolv.h>
80 #include <ctype.h>
81 #include <errno.h>
82 #include <string.h>
83
84 extern int h_errno;
85
86 #if defined(mips) && defined(SYSTYPE_BSD43)
87 extern int errno;
88 #endif
89
90 struct netent *_getnetbyaddr __P((long net, int type));
91 struct netent *_getnetbyname __P((const char *name));
92
93 #define BYADDR 0
94 #define BYNAME 1
95 #define MAXALIASES 35
96
97 #if PACKETSZ > 1024
98 #define MAXPACKET PACKETSZ
99 #else
100 #define MAXPACKET 1024
101 #endif
102
103 typedef union {
104 HEADER hdr;
105 u_char buf[MAXPACKET];
106 } querybuf;
107
108 typedef union {
109 long al;
110 char ac;
111 } align;
112
113 static struct netent *
114 getnetanswer(answer, anslen, net_i)
115 querybuf *answer;
116 int anslen;
117 int net_i;
118 {
119
120 register HEADER *hp;
121 register u_char *cp;
122 register int n;
123 u_char *eom;
124 int type, class, buflen, ancount, qdcount, haveanswer, i, nchar;
125 char aux1[30], aux2[30], *in, *st, *pauxt, *bp, **ap,
126 *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0;
127 static struct netent net_entry;
128 static char *net_aliases[MAXALIASES], *netbuf = NULL;
129 static char ans[MAXDNAME];
130
131 if (netbuf == NULL) {
132 netbuf = malloc(BUFSIZ+1);
133 if (netbuf == NULL)
134 return (NULL);
135 }
136 buflen = BUFSIZ+1;
137
138 /*
139 * find first satisfactory answer
140 *
141 * answer --> +------------+ ( MESSAGE )
142 * | Header |
143 * +------------+
144 * | Question | the question for the name server
145 * +------------+
146 * | Answer | RRs answering the question
147 * +------------+
148 * | Authority | RRs pointing toward an authority
149 * | Additional | RRs holding additional information
150 * +------------+
151 */
152 eom = answer->buf + anslen;
153 hp = &answer->hdr;
154 ancount = ntohs(hp->ancount); /* #/records in the answer section */
155 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
156 bp = netbuf;
157 cp = answer->buf + HFIXEDSZ;
158 if (!qdcount) {
159 if (hp->aa)
160 h_errno = HOST_NOT_FOUND;
161 else
162 h_errno = TRY_AGAIN;
163 return (NULL);
164 }
165 while (qdcount-- > 0) {
166 n = __dn_skipname(cp, eom);
167 if (n < 0 || (cp + n + QFIXEDSZ) > eom) {
168 h_errno = NO_RECOVERY;
169 return(NULL);
170 }
171 cp += n + QFIXEDSZ;
172 }
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 if (n < 0)
180 break;
181 cp += n;
182 ans[0] = '\0';
183 (void)strcpy(ans, bp);
184 GETSHORT(type, cp);
185 GETSHORT(class, cp);
186 cp += INT32SZ; /* TTL */
187 GETSHORT(n, cp);
188 if (class == C_IN && type == T_PTR) {
189 n = dn_expand(answer->buf, eom, cp, bp, buflen);
190 if (n < 0) {
191 cp += n;
192 return (NULL);
193 }
194 cp += n;
195 if ((ap + 2) < &net_aliases[MAXALIASES]) {
196 *ap++ = bp;
197 bp += strlen(bp) + 1;
198 net_entry.n_addrtype =
199 (class == C_IN) ? AF_INET : AF_UNSPEC;
200 haveanswer++;
201 }
202 }
203 }
204 if (haveanswer) {
205 *ap = NULL;
206 switch (net_i) {
207 case BYADDR:
208 net_entry.n_name = *net_entry.n_aliases;
209 net_entry.n_net = 0L;
210 break;
211 case BYNAME:
212 ap = net_entry.n_aliases;
213 next_alias:
214 in = *ap++;
215 if (in == NULL) {
216 h_errno = HOST_NOT_FOUND;
217 return (NULL);
218 }
219 net_entry.n_name = ans;
220 aux2[0] = '\0';
221 for (i = 0; i < 4; i++) {
222 for (st = in, nchar = 0;
223 isdigit((unsigned char)*st);
224 st++, nchar++)
225 ;
226
227 if (*st != '.' || nchar == 0 || nchar > 3)
228 goto next_alias;
229 if (i != 0)
230 nchar++;
231 (void)strncpy(paux1, in, nchar);
232 paux1[nchar] = '\0';
233 pauxt = paux2;
234 paux2 = strcat(paux1, paux2);
235 paux1 = pauxt;
236 in = ++st;
237 }
238
239 if (strcasecmp(in, "IN-ADDR.ARPA") != 0)
240 goto next_alias;
241 net_entry.n_net = inet_network(paux2);
242 break;
243 }
244 net_entry.n_aliases++;
245 return (&net_entry);
246 }
247 h_errno = TRY_AGAIN;
248 return (NULL);
249 }
250
251 struct netent *
252 getnetbyaddr(net, net_type)
253 register long net;
254 register int net_type;
255 {
256 unsigned int netbr[4];
257 int nn, anslen;
258 querybuf buf;
259 char qbuf[MAXDNAME];
260 unsigned long net2;
261 struct netent *net_entry;
262
263 if (net_type != AF_INET)
264 #if defined(__APPLE__)
265 return NULL;
266 #else
267 return (_getnetbyaddr(net, net_type));
268 #endif /* !NeXT */
269
270 for (nn = 4, net2 = net; net2; net2 >>= 8)
271 netbr[--nn] = net2 & 0xff;
272 switch (nn) {
273 case 3: /* Class A */
274 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
275 break;
276 case 2: /* Class B */
277 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
278 break;
279 case 1: /* Class C */
280 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
281 netbr[1]);
282 break;
283 case 0: /* Class D - E */
284 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
285 netbr[1], netbr[0]);
286 break;
287 }
288 anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));
289 if (anslen < 0) {
290 #ifdef DEBUG
291 if (_res.options & RES_DEBUG)
292 printf("res_query failed\n");
293 #endif
294 #if !defined(__APPLE__)
295 if (errno == ECONNREFUSED)
296 return (_getnetbyaddr(net, net_type));
297 #endif /* !NeXT */
298 return (NULL);
299 }
300 net_entry = getnetanswer(&buf, anslen, BYADDR);
301 if (net_entry) {
302 unsigned u_net = net; /* maybe net should be unsigned ? */
303
304 /* Strip trailing zeros */
305 while ((u_net & 0xff) == 0 && u_net != 0)
306 u_net >>= 8;
307 net_entry->n_net = u_net;
308 return (net_entry);
309 }
310 #if defined(__APPLE__)
311 return NULL;
312 #else
313 return (_getnetbyaddr(net, net_type));
314 #endif /* !NeXT */
315 }
316
317 struct netent *
318 getnetbyname(net)
319 register const char *net;
320 {
321 int anslen;
322 querybuf buf;
323 char qbuf[MAXDNAME];
324 struct netent *net_entry;
325
326 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
327 h_errno = NETDB_INTERNAL;
328 return (NULL);
329 }
330 strcpy(&qbuf[0], net);
331 anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));
332 if (anslen < 0) {
333 #ifdef DEBUG
334 if (_res.options & RES_DEBUG)
335 printf("res_query failed\n");
336 #endif
337 #if defined(__APPLE__)
338 return NULL;
339 #else
340 if (errno == ECONNREFUSED)
341 return (_getnetbyname(net));
342 return (_getnetbyname(net));
343 #endif /* !NeXT */
344 }
345 net_entry = getnetanswer(&buf, anslen, BYNAME);
346 if (net_entry)
347 return (net_entry);
348 #if defined(__APPLE__)
349 return NULL;
350 #else
351 return (_getnetbyname(net));
352 #endif /* !NeXT */
353 }