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