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