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