Libinfo-78.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.2 1999/10/14 21:56:44 wsanchez 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 <netdb.h>
78 #include <resolv.h>
79 #include <ctype.h>
80 #include <errno.h>
81 #include <string.h>
82
83 extern int h_errno;
84
85 #if defined(mips) && defined(SYSTYPE_BSD43)
86 extern int errno;
87 #endif
88
89 struct netent *_getnetbyaddr __P((long net, int type));
90 struct netent *_getnetbyname __P((const char *name));
91
92 #define BYADDR 0
93 #define BYNAME 1
94 #define MAXALIASES 35
95
96 #if PACKETSZ > 1024
97 #define MAXPACKET PACKETSZ
98 #else
99 #define MAXPACKET 1024
100 #endif
101
102 typedef union {
103 HEADER hdr;
104 u_char buf[MAXPACKET];
105 } querybuf;
106
107 typedef union {
108 long al;
109 char ac;
110 } align;
111
112 static struct netent *
113 getnetanswer(answer, anslen, net_i)
114 querybuf *answer;
115 int anslen;
116 int net_i;
117 {
118
119 register HEADER *hp;
120 register u_char *cp;
121 register int n;
122 u_char *eom;
123 int type, class, buflen, ancount, qdcount, haveanswer, i, nchar;
124 char aux1[30], aux2[30], ans[30], *in, *st, *pauxt, *bp, **ap,
125 *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0;
126 static struct netent net_entry;
127 static char *net_aliases[MAXALIASES], netbuf[BUFSIZ+1];
128
129 /*
130 * find first satisfactory answer
131 *
132 * answer --> +------------+ ( MESSAGE )
133 * | Header |
134 * +------------+
135 * | Question | the question for the name server
136 * +------------+
137 * | Answer | RRs answering the question
138 * +------------+
139 * | Authority | RRs pointing toward an authority
140 * | Additional | RRs holding additional information
141 * +------------+
142 */
143 eom = answer->buf + anslen;
144 hp = &answer->hdr;
145 ancount = ntohs(hp->ancount); /* #/records in the answer section */
146 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
147 bp = netbuf;
148 buflen = sizeof(netbuf);
149 cp = answer->buf + HFIXEDSZ;
150 if (!qdcount) {
151 if (hp->aa)
152 h_errno = HOST_NOT_FOUND;
153 else
154 h_errno = TRY_AGAIN;
155 return (NULL);
156 }
157 while (qdcount-- > 0)
158 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
159 ap = net_aliases;
160 *ap = NULL;
161 net_entry.n_aliases = net_aliases;
162 haveanswer = 0;
163 while (--ancount >= 0 && cp < eom) {
164 n = dn_expand(answer->buf, eom, cp, bp, buflen);
165 if (n < 0)
166 break;
167 cp += n;
168 ans[0] = '\0';
169 (void)strcpy(&ans[0], bp);
170 GETSHORT(type, cp);
171 GETSHORT(class, cp);
172 cp += INT32SZ; /* TTL */
173 GETSHORT(n, cp);
174 if (class == C_IN && type == T_PTR) {
175 n = dn_expand(answer->buf, eom, cp, bp, buflen);
176 if (n < 0) {
177 cp += n;
178 return (NULL);
179 }
180 cp += n;
181 *ap++ = bp;
182 bp += strlen(bp) + 1;
183 net_entry.n_addrtype =
184 (class == C_IN) ? AF_INET : AF_UNSPEC;
185 haveanswer++;
186 }
187 }
188 if (haveanswer) {
189 *ap = NULL;
190 switch (net_i) {
191 case BYADDR:
192 net_entry.n_name = *net_entry.n_aliases;
193 net_entry.n_net = 0L;
194 break;
195 case BYNAME:
196 in = *net_entry.n_aliases;
197 net_entry.n_name = &ans[0];
198 aux2[0] = '\0';
199 for (i = 0; i < 4; i++) {
200 for (st = in, nchar = 0;
201 *st != '.';
202 st++, nchar++)
203 ;
204 if (nchar != 1 || *in != '0' || flag) {
205 flag = 1;
206 (void)strncpy(paux1,
207 (i==0) ? in : in-1,
208 (i==0) ?nchar : nchar+1);
209 paux1[(i==0) ? nchar : nchar+1] = '\0';
210 pauxt = paux2;
211 paux2 = strcat(paux1, paux2);
212 paux1 = pauxt;
213 }
214 in = ++st;
215 }
216 net_entry.n_net = inet_network(paux2);
217 break;
218 }
219 net_entry.n_aliases++;
220 return (&net_entry);
221 }
222 h_errno = TRY_AGAIN;
223 return (NULL);
224 }
225
226 struct netent *
227 getnetbyaddr(net, net_type)
228 register long net;
229 register int net_type;
230 {
231 unsigned int netbr[4];
232 int nn, anslen;
233 querybuf buf;
234 char qbuf[MAXDNAME];
235 unsigned long net2;
236 struct netent *net_entry;
237
238 if (net_type != AF_INET)
239 #if defined(__APPLE__)
240 return NULL;
241 #else
242 return (_getnetbyaddr(net, net_type));
243 #endif /* !NeXT */
244
245 for (nn = 4, net2 = net; net2; net2 >>= 8)
246 netbr[--nn] = net2 & 0xff;
247 switch (nn) {
248 case 3: /* Class A */
249 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
250 break;
251 case 2: /* Class B */
252 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
253 break;
254 case 1: /* Class C */
255 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
256 netbr[1]);
257 break;
258 case 0: /* Class D - E */
259 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
260 netbr[1], netbr[0]);
261 break;
262 }
263 anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));
264 if (anslen < 0) {
265 #ifdef DEBUG
266 if (_res.options & RES_DEBUG)
267 printf("res_query failed\n");
268 #endif
269 #if !defined(__APPLE__)
270 if (errno == ECONNREFUSED)
271 return (_getnetbyaddr(net, net_type));
272 #endif /* !NeXT */
273 return (NULL);
274 }
275 net_entry = getnetanswer(&buf, anslen, BYADDR);
276 if (net_entry) {
277 unsigned u_net = net; /* maybe net should be unsigned ? */
278
279 /* Strip trailing zeros */
280 while ((u_net & 0xff) == 0 && u_net != 0)
281 u_net >>= 8;
282 net_entry->n_net = u_net;
283 return (net_entry);
284 }
285 #if defined(__APPLE__)
286 return NULL;
287 #else
288 return (_getnetbyaddr(net, net_type));
289 #endif /* !NeXT */
290 }
291
292 struct netent *
293 getnetbyname(net)
294 register const char *net;
295 {
296 int anslen;
297 querybuf buf;
298 char qbuf[MAXDNAME];
299 struct netent *net_entry;
300
301 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
302 h_errno = NETDB_INTERNAL;
303 return (NULL);
304 }
305 strcpy(&qbuf[0], net);
306 anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));
307 if (anslen < 0) {
308 #ifdef DEBUG
309 if (_res.options & RES_DEBUG)
310 printf("res_query failed\n");
311 #endif
312 #if defined(__APPLE__)
313 return NULL;
314 #else
315 if (errno == ECONNREFUSED)
316 return (_getnetbyname(net));
317 return (_getnetbyname(net));
318 #endif /* !NeXT */
319 }
320 net_entry = getnetanswer(&buf, anslen, BYNAME);
321 if (net_entry)
322 return (net_entry);
323 #if defined(__APPLE__)
324 return NULL;
325 #else
326 return (_getnetbyname(net));
327 #endif /* !NeXT */
328 }