Deleted Added
full compact
getnetbydns.c (8870) getnetbydns.c (11661)
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
54#if defined(LIBC_SCCS) && !defined(lint)
55static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
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
54#if defined(LIBC_SCCS) && !defined(lint)
55static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93";
56static char rcsid[] = "$Id: getnetbydns.c,v 1.2 1994/09/25 17:45:38 pst Exp $";
56static char rcsid[] = "$Id: getnetbydns.c,v 1.3 1995/05/30 05:40:46 rgrimes Exp $";
57#endif /* LIBC_SCCS and not lint */
58
59#include <sys/param.h>
60#include <sys/socket.h>
61#include <netinet/in.h>
62#include <arpa/inet.h>
63#include <arpa/nameser.h>
64#include <netdb.h>
65#include <resolv.h>
66#include <stdio.h>
57#endif /* LIBC_SCCS and not lint */
58
59#include <sys/param.h>
60#include <sys/socket.h>
61#include <netinet/in.h>
62#include <arpa/inet.h>
63#include <arpa/nameser.h>
64#include <netdb.h>
65#include <resolv.h>
66#include <stdio.h>
67#include <unistd.h>
67#include <ctype.h>
68#include <errno.h>
69#include <string.h>
70#include <syslog.h>
71
72#define BYNAME 0
73#define BYADDR 1
74
75#define MAXALIASES 35
76#define MAXADDRS 35
77
78#if PACKETSZ > 1024
79#define MAXPACKET PACKETSZ
80#else
81#define MAXPACKET 1024
82#endif
83
84typedef union {
85 HEADER hdr;
86 u_char buf[MAXPACKET];
87} querybuf;
88
89typedef union {
90 int32_t al;
91 char ac;
92} align;
93
94extern int h_errno;
95
96static struct netent *
97getnetanswer(answer, anslen, net_i)
98 querybuf *answer;
99 int anslen;
100 int net_i;
101{
102
103 register HEADER *hp;
104 register u_char *cp;
105 register int n;
106 u_char *eom;
107 int type, class, buflen, ancount, qdcount,
108 haveanswer, i, nchar;
109 char aux1[30], aux2[30], ans[30],
110 *in, *st, *pauxt, *bp, **ap,
111 *paux1 = &aux1[0],
112 *paux2 = &aux2[0],
113 flag = 0;
114static struct netent net_entry;
115static char *net_aliases[MAXALIASES],
116 netbuf[BUFSIZ+1];
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
145 return ((struct netent *) NULL);
146 }
147 while (qdcount-- > 0){
148 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
149 }
150 ap = net_aliases;
151 *ap = NULL;
152 net_entry.n_aliases = net_aliases;
153 haveanswer = 0;
154 while (--ancount >= 0 && cp < eom) {
155 n = dn_expand(answer->buf, eom, cp, bp, buflen);
156 if (n < 0)
157 break;
158 cp += n;
159 ans[0] = '\0';
160 (void)strcpy(&ans[0], bp);
161 GETSHORT(type, cp);
162 GETSHORT(class, cp);
163 cp += INT32SZ; /* TTL */
164 GETSHORT(n, cp);
165 if (class == C_IN && type == T_PTR) {
166 n = dn_expand(answer->buf, eom, cp, bp, buflen);
167 if (n < 0) {
168 cp += n;
169 return (NULL);
170 }
171 cp += n;
172 *ap++ = bp;
173 bp += (strlen(bp) + 1);
174 net_entry.n_addrtype = (class == C_IN)
175 ? AF_INET
176 : 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 }
210 net_entry.n_aliases++;
211 return (&net_entry);
212 } else {
213 h_errno = TRY_AGAIN;
214 return ((struct netent *) NULL);
215 }
216}
217
218struct netent *
219_getnetbydnsaddr(net, net_type)
220 register 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 }
236 switch (nn) {
237 case 3: /* Class A */
238 (void)sprintf(qbuf, "0.0.0.%u.in-addr.arpa",
239 netbr[3]);
240 break;
241 case 2: /* Class B */
242 (void)sprintf(qbuf, "0.0.%u.%u.in-addr.arpa",
243 netbr[3], netbr[2]);
244 break;
245 case 1: /* Class C */
246 (void)sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa",
247 netbr[3], netbr[2], netbr[1]);
248 break;
249 case 0: /* Class D - E */
250 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
251 netbr[3], netbr[2], netbr[1], netbr[0]);
252 break;
253 }
254 anslen = res_query(qbuf, C_IN, T_PTR, buf.buf, sizeof buf.buf);
255 if (anslen < 0) {
256 if (_res.options & RES_DEBUG)
257 printf("res_query failed\n");
258 return (NULL);
259 }
260 net_entry = getnetanswer(&buf, anslen, BYADDR);
261 if (net_entry) {
262 unsigned u_net = net; /* maybe net should be unsigned ? */
263
264 /* Strip trailing zeros */
265 while ((u_net & 0xff) == 0 && u_net != 0) {
266 u_net >>= 8;
267 }
268 net_entry->n_net = u_net;
269 }
270 return (net_entry);
271}
272
273struct netent *
274_getnetbydnsname(net)
275 register const char *net;
276{
277 int anslen;
278 querybuf buf;
279 char qbuf[MAXDNAME];
280
281 (void)strcpy(&qbuf[0],net);
282 anslen = res_search(qbuf, C_IN, T_PTR, buf.buf, sizeof buf.buf);
283 if (anslen < 0) {
284 if (_res.options & RES_DEBUG)
285 printf("res_query failed\n");
286 return NULL;
287 }
288 return getnetanswer(&buf, anslen, BYNAME);
289}
290
291void
292_setnetdnsent(stayopen)
293 int stayopen;
294{
295 if (stayopen)
296 _res.options |= RES_STAYOPEN | RES_USEVC;
297}
298
299void
300_endnetdnsent()
301{
302 _res.options &= ~(RES_STAYOPEN | RES_USEVC);
303 _res_close();
304}
68#include <ctype.h>
69#include <errno.h>
70#include <string.h>
71#include <syslog.h>
72
73#define BYNAME 0
74#define BYADDR 1
75
76#define MAXALIASES 35
77#define MAXADDRS 35
78
79#if PACKETSZ > 1024
80#define MAXPACKET PACKETSZ
81#else
82#define MAXPACKET 1024
83#endif
84
85typedef union {
86 HEADER hdr;
87 u_char buf[MAXPACKET];
88} querybuf;
89
90typedef union {
91 int32_t al;
92 char ac;
93} align;
94
95extern int h_errno;
96
97static struct netent *
98getnetanswer(answer, anslen, net_i)
99 querybuf *answer;
100 int anslen;
101 int net_i;
102{
103
104 register HEADER *hp;
105 register u_char *cp;
106 register int n;
107 u_char *eom;
108 int type, class, buflen, ancount, qdcount,
109 haveanswer, i, nchar;
110 char aux1[30], aux2[30], ans[30],
111 *in, *st, *pauxt, *bp, **ap,
112 *paux1 = &aux1[0],
113 *paux2 = &aux2[0],
114 flag = 0;
115static struct netent net_entry;
116static char *net_aliases[MAXALIASES],
117 netbuf[BUFSIZ+1];
118
119 /*
120 * find first satisfactory answer
121 *
122 * answer --> +------------+ ( MESSAGE )
123 * | Header |
124 * +------------+
125 * | Question | the question for the name server
126 * +------------+
127 * | Answer | RRs answering the question
128 * +------------+
129 * | Authority | RRs pointing toward an authority
130 * | Additional | RRs holding additional information
131 * +------------+
132 */
133 eom = answer->buf + anslen;
134 hp = &answer->hdr;
135 ancount = ntohs(hp->ancount); /* #/records in the answer section */
136 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
137 bp = netbuf;
138 buflen = sizeof(netbuf);
139 cp = answer->buf + HFIXEDSZ;
140 if (!qdcount) {
141 if (hp->aa)
142 h_errno = HOST_NOT_FOUND;
143 else
144 h_errno = TRY_AGAIN;
145
146 return ((struct netent *) NULL);
147 }
148 while (qdcount-- > 0){
149 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
150 }
151 ap = net_aliases;
152 *ap = NULL;
153 net_entry.n_aliases = net_aliases;
154 haveanswer = 0;
155 while (--ancount >= 0 && cp < eom) {
156 n = dn_expand(answer->buf, eom, cp, bp, buflen);
157 if (n < 0)
158 break;
159 cp += n;
160 ans[0] = '\0';
161 (void)strcpy(&ans[0], bp);
162 GETSHORT(type, cp);
163 GETSHORT(class, cp);
164 cp += INT32SZ; /* TTL */
165 GETSHORT(n, cp);
166 if (class == C_IN && type == T_PTR) {
167 n = dn_expand(answer->buf, eom, cp, bp, buflen);
168 if (n < 0) {
169 cp += n;
170 return (NULL);
171 }
172 cp += n;
173 *ap++ = bp;
174 bp += (strlen(bp) + 1);
175 net_entry.n_addrtype = (class == C_IN)
176 ? AF_INET
177 : AF_UNSPEC;
178 haveanswer++;
179 }
180 }
181 if (haveanswer) {
182 *ap = NULL;
183 switch (net_i) {
184 case BYADDR :
185 net_entry.n_name = *net_entry.n_aliases;
186 net_entry.n_net = 0L;
187 break;
188 case BYNAME :
189 in = *net_entry.n_aliases;
190 net_entry.n_name = &ans[0];
191 aux2[0] = '\0';
192 for (i = 0; i < 4; i++) {
193 for (st = in, nchar = 0;
194 *st != '.';
195 st++, nchar++)
196 ;
197 if (nchar != 1 || *in != '0' || flag) {
198 flag = 1;
199 (void)strncpy(paux1,
200 (i==0) ?in :in-1,
201 (i==0) ?nchar :nchar+1);
202 paux1[(i==0) ?nchar :nchar+1] = '\0';
203 pauxt = paux2;
204 paux2 = strcat(paux1, paux2);
205 paux1 = pauxt;
206 }
207 in = ++st;
208 }
209 net_entry.n_net = inet_network(paux2);
210 }
211 net_entry.n_aliases++;
212 return (&net_entry);
213 } else {
214 h_errno = TRY_AGAIN;
215 return ((struct netent *) NULL);
216 }
217}
218
219struct netent *
220_getnetbydnsaddr(net, net_type)
221 register long net;
222 register int net_type;
223{
224 unsigned int netbr[4];
225 int nn, anslen;
226 querybuf buf;
227 char qbuf[MAXDNAME];
228 unsigned long net2;
229 struct netent *net_entry;
230
231 if (net_type != AF_INET)
232 return (NULL);
233
234 for (nn = 4, net2 = net; net2; net2 >>= 8) {
235 netbr[--nn] = net2 & 0xff;
236 }
237 switch (nn) {
238 case 3: /* Class A */
239 (void)sprintf(qbuf, "0.0.0.%u.in-addr.arpa",
240 netbr[3]);
241 break;
242 case 2: /* Class B */
243 (void)sprintf(qbuf, "0.0.%u.%u.in-addr.arpa",
244 netbr[3], netbr[2]);
245 break;
246 case 1: /* Class C */
247 (void)sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa",
248 netbr[3], netbr[2], netbr[1]);
249 break;
250 case 0: /* Class D - E */
251 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
252 netbr[3], netbr[2], netbr[1], netbr[0]);
253 break;
254 }
255 anslen = res_query(qbuf, C_IN, T_PTR, buf.buf, sizeof buf.buf);
256 if (anslen < 0) {
257 if (_res.options & RES_DEBUG)
258 printf("res_query failed\n");
259 return (NULL);
260 }
261 net_entry = getnetanswer(&buf, anslen, BYADDR);
262 if (net_entry) {
263 unsigned u_net = net; /* maybe net should be unsigned ? */
264
265 /* Strip trailing zeros */
266 while ((u_net & 0xff) == 0 && u_net != 0) {
267 u_net >>= 8;
268 }
269 net_entry->n_net = u_net;
270 }
271 return (net_entry);
272}
273
274struct netent *
275_getnetbydnsname(net)
276 register const char *net;
277{
278 int anslen;
279 querybuf buf;
280 char qbuf[MAXDNAME];
281
282 (void)strcpy(&qbuf[0],net);
283 anslen = res_search(qbuf, C_IN, T_PTR, buf.buf, sizeof buf.buf);
284 if (anslen < 0) {
285 if (_res.options & RES_DEBUG)
286 printf("res_query failed\n");
287 return NULL;
288 }
289 return getnetanswer(&buf, anslen, BYNAME);
290}
291
292void
293_setnetdnsent(stayopen)
294 int stayopen;
295{
296 if (stayopen)
297 _res.options |= RES_STAYOPEN | RES_USEVC;
298}
299
300void
301_endnetdnsent()
302{
303 _res.options &= ~(RES_STAYOPEN | RES_USEVC);
304 _res_close();
305}