MFC r1.28:
[dragonfly.git] / lib / libc / net / getnetbydns.c
blob7ba9405b34781e1914a40eae2be6d3ab3875b849
1 /*-
2 * Copyright (c) 1985, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 * -
29 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
31 * Permission to use, copy, modify, and distribute this software for any
32 * purpose with or without fee is hereby granted, provided that the above
33 * copyright notice and this permission notice appear in all copies, and that
34 * the name of Digital Equipment Corporation not be used in advertising or
35 * publicity pertaining to distribution of the document or software without
36 * specific, written prior permission.
38 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
39 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
40 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
41 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
42 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
43 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
44 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45 * SOFTWARE.
46 * -
47 * --Copyright--
49 * @(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93
50 * $FreeBSD: src/lib/libc/net/getnetbydns.c,v 1.13.2.4 2002/10/11 11:07:13 ume Exp $
51 * $DragonFly: src/lib/libc/net/getnetbydns.c,v 1.5 2005/11/13 02:04:47 swildner Exp $
53 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
54 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
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.
61 #include <sys/param.h>
62 #include <sys/socket.h>
63 #include <netinet/in.h>
64 #include <arpa/inet.h>
65 #include <arpa/nameser.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <netdb.h>
70 #include <resolv.h>
71 #include <ctype.h>
72 #include <string.h>
73 #include <unistd.h>
74 #include <syslog.h>
76 #include "res_config.h"
78 extern int h_errno;
80 #define BYADDR 0
81 #define BYNAME 1
82 #define MAXALIASES 35
84 #define MAXPACKET (64*1024)
86 typedef union {
87 HEADER hdr;
88 u_char buf[MAXPACKET];
89 } querybuf;
91 typedef union {
92 long al;
93 char ac;
94 } align;
96 static struct netent *
97 getnetanswer(querybuf *answer, int anslen, int net_i)
100 HEADER *hp;
101 u_char *cp;
102 int n;
103 u_char *eom;
104 int type, class, buflen, ancount, qdcount, haveanswer, i, nchar;
105 char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN];
106 char *in, *st, *pauxt, *bp, **ap;
107 char *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0;
108 static struct netent net_entry;
109 static char *net_aliases[MAXALIASES], netbuf[PACKETSZ];
112 * find first satisfactory answer
114 * answer --> +------------+ ( MESSAGE )
115 * | Header |
116 * +------------+
117 * | Question | the question for the name server
118 * +------------+
119 * | Answer | RRs answering the question
120 * +------------+
121 * | Authority | RRs pointing toward an authority
122 * | Additional | RRs holding additional information
123 * +------------+
125 eom = answer->buf + anslen;
126 hp = &answer->hdr;
127 ancount = ntohs(hp->ancount); /* #/records in the answer section */
128 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
129 bp = netbuf;
130 buflen = sizeof(netbuf);
131 cp = answer->buf + HFIXEDSZ;
132 if (!qdcount) {
133 if (hp->aa)
134 h_errno = HOST_NOT_FOUND;
135 else
136 h_errno = TRY_AGAIN;
137 return (NULL);
139 while (qdcount-- > 0)
140 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
141 ap = net_aliases;
142 *ap = NULL;
143 net_entry.n_aliases = net_aliases;
144 haveanswer = 0;
145 while (--ancount >= 0 && cp < eom) {
146 n = dn_expand(answer->buf, eom, cp, bp, buflen);
147 if ((n < 0) || !res_dnok(bp))
148 break;
149 cp += n;
150 ans[0] = '\0';
151 strncpy(&ans[0], bp, sizeof(ans) - 1);
152 ans[sizeof(ans) - 1] = '\0';
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) || !res_hnok(bp)) {
160 cp += n;
161 return (NULL);
163 cp += n;
164 *ap++ = bp;
165 n = strlen(bp) + 1;
166 bp += n;
167 buflen -= n;
168 net_entry.n_addrtype =
169 (class == C_IN) ? AF_INET : AF_UNSPEC;
170 haveanswer++;
173 if (haveanswer) {
174 *ap = NULL;
175 switch (net_i) {
176 case BYADDR:
177 net_entry.n_name = *net_entry.n_aliases;
178 net_entry.n_net = 0L;
179 break;
180 case BYNAME:
181 in = *net_entry.n_aliases;
182 net_entry.n_name = &ans[0];
183 aux2[0] = '\0';
184 for (i = 0; i < 4; i++) {
185 for (st = in, nchar = 0;
186 *st != '.';
187 st++, nchar++)
189 if (nchar != 1 || *in != '0' || flag) {
190 flag = 1;
191 strncpy(paux1, (i==0) ? in : in-1,
192 (i==0) ? nchar : nchar+1);
193 paux1[(i==0) ? nchar : nchar+1] = '\0';
194 pauxt = paux2;
195 paux2 = strcat(paux1, paux2);
196 paux1 = pauxt;
198 in = ++st;
200 net_entry.n_net = inet_network(paux2);
201 break;
203 net_entry.n_aliases++;
204 return (&net_entry);
206 h_errno = TRY_AGAIN;
207 return (NULL);
210 struct netent *
211 _getnetbydnsaddr(unsigned long net, int net_type)
213 unsigned int netbr[4];
214 int nn, anslen;
215 querybuf *buf;
216 char qbuf[MAXDNAME];
217 unsigned long net2;
218 struct netent *net_entry;
220 if (net_type != AF_INET)
221 return (NULL);
223 for (nn = 4, net2 = net; net2; net2 >>= 8)
224 netbr[--nn] = net2 & 0xff;
225 switch (nn) {
226 case 3: /* Class A */
227 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
228 break;
229 case 2: /* Class B */
230 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
231 break;
232 case 1: /* Class C */
233 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
234 netbr[1]);
235 break;
236 case 0: /* Class D - E */
237 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
238 netbr[1], netbr[0]);
239 break;
241 if ((buf = malloc(sizeof(*buf))) == NULL) {
242 h_errno = NETDB_INTERNAL;
243 return (NULL);
245 anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)buf, sizeof(*buf));
246 if (anslen < 0) {
247 free(buf);
248 #ifdef DEBUG
249 if (_res.options & RES_DEBUG)
250 printf("res_search failed\n");
251 #endif
252 return (NULL);
253 } else if (anslen > sizeof(*buf)) {
254 free(buf);
255 #ifdef DEBUG
256 if (_res.options & RES_DEBUG)
257 printf("res_search static buffer too small\n");
258 #endif
259 return (NULL);
261 net_entry = getnetanswer(buf, anslen, BYADDR);
262 free(buf);
263 if (net_entry) {
264 unsigned u_net = net; /* maybe net should be unsigned ? */
266 /* Strip trailing zeros */
267 while ((u_net & 0xff) == 0 && u_net != 0)
268 u_net >>= 8;
269 net_entry->n_net = u_net;
270 return (net_entry);
272 return (NULL);
275 struct netent *
276 _getnetbydnsname(const char *net)
278 int anslen;
279 querybuf *buf;
280 char qbuf[MAXDNAME];
281 struct netent *net_entry;
283 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
284 h_errno = NETDB_INTERNAL;
285 return (NULL);
287 if ((buf = malloc(sizeof(*buf))) == NULL) {
288 h_errno = NETDB_INTERNAL;
289 return (NULL);
291 strncpy(qbuf, net, sizeof(qbuf) - 1);
292 qbuf[sizeof(qbuf) - 1] = '\0';
293 anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)buf, sizeof(*buf));
294 if (anslen < 0) {
295 free(buf);
296 #ifdef DEBUG
297 if (_res.options & RES_DEBUG)
298 printf("res_search failed\n");
299 #endif
300 return (NULL);
301 } else if (anslen > sizeof(*buf)) {
302 free(buf);
303 #ifdef DEBUG
304 if (_res.options & RES_DEBUG)
305 printf("res_search static buffer too small\n");
306 #endif
307 return (NULL);
309 net_entry = getnetanswer(buf, anslen, BYNAME);
310 free(buf);
311 return net_entry;
314 void
315 _setnetdnsent(int stayopen)
317 if (stayopen)
318 _res.options |= RES_STAYOPEN | RES_USEVC;
321 void
322 _endnetdnsent(void)
324 _res.options &= ~(RES_STAYOPEN | RES_USEVC);
325 res_close();