1 /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
2 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
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.
9 * Copyright (c) 1983, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
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 */
47 #include <sys/types.h>
48 #include <sys/param.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 #include <arpa/nameser.h>
65 #if defined(mips) && defined(SYSTYPE_BSD43) && !defined(errno)
69 struct netent *_getnetbyaddr __P((long net, int type));
70 struct netent *_getnetbyname __P((const char *name));
77 #define MAXPACKET PACKETSZ
79 #define MAXPACKET 1024
84 u_char buf[MAXPACKET];
92 static struct netent *
93 getnetanswer(answer, anslen, net_i)
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;
106 static struct netent net_entry;
107 static char *net_aliases[MAXALIASES], netbuf[BUFSIZ+1];
110 * find first satisfactory answer
112 * answer --> +------------+ ( MESSAGE )
115 * | Question | the question for the name server
117 * | Answer | RRs answering the question
119 * | Authority | RRs pointing toward an authority
120 * | Additional | RRs holding additional information
123 eom = answer->buf + anslen;
125 ancount = ntohs(hp->ancount); /* #/records in the answer section */
126 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
128 buflen = sizeof(netbuf);
129 cp = answer->buf + HFIXEDSZ;
132 __set_h_errno (HOST_NOT_FOUND);
134 __set_h_errno (TRY_AGAIN);
137 while (qdcount-- > 0)
138 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
141 net_entry.n_aliases = net_aliases;
143 while (--ancount >= 0 && cp < eom) {
144 n = dn_expand(answer->buf, eom, cp, bp, buflen);
145 if ((n < 0) || !res_dnok(bp))
149 (void)strcpy(&ans[0], bp);
152 cp += INT32SZ; /* TTL */
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)) {
162 bp += strlen(bp) + 1;
163 net_entry.n_addrtype =
164 (class == C_IN) ? AF_INET : AF_UNSPEC;
172 net_entry.n_name = *net_entry.n_aliases;
173 net_entry.n_net = 0L;
176 in = *net_entry.n_aliases;
177 net_entry.n_name = &ans[0];
179 for (i = 0; i < 4; i++) {
180 for (st = in, nchar = 0;
184 if (nchar != 1 || *in != '0' || flag) {
188 (i==0) ?nchar : nchar+1);
189 paux1[(i==0) ? nchar : nchar+1] = '\0';
191 paux2 = strcat(paux1, paux2);
196 net_entry.n_net = inet_network(paux2);
199 net_entry.n_aliases++;
202 __set_h_errno (TRY_AGAIN);
207 getnetbyaddr(net, net_type)
209 register int net_type;
211 unsigned int netbr[4];
215 u_int32_t net2; /* Changed from unsigned long --roland */
216 struct netent *net_entry;
218 if (net_type != AF_INET)
219 return (_getnetbyaddr(net, net_type));
221 for (nn = 4, net2 = net; net2; net2 >>= 8)
222 netbr[--nn] = net2 & 0xff;
224 case 3: /* Class A */
225 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]);
227 case 2: /* Class B */
228 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]);
230 case 1: /* Class C */
231 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
234 case 0: /* Class D - E */
235 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2],
239 anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));
242 if (_res.options & RES_DEBUG)
243 printf("res_query failed\n");
245 if (errno == ECONNREFUSED)
246 return (_getnetbyaddr(net, net_type));
249 net_entry = getnetanswer(&buf, anslen, BYADDR);
251 unsigned u_net = net; /* maybe net should be unsigned ? */
253 /* Strip trailing zeros */
254 while ((u_net & 0xff) == 0 && u_net != 0)
256 net_entry->n_net = u_net;
259 return (_getnetbyaddr(net, net_type));
264 register const char *net;
269 struct netent *net_entry;
271 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
272 __set_h_errno (NETDB_INTERNAL);
275 strcpy(&qbuf[0], net);
276 anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf));
279 if (_res.options & RES_DEBUG)
280 printf("res_query failed\n");
282 if (errno == ECONNREFUSED)
283 return (_getnetbyname(net));
284 return (_getnetbyname(net));
286 net_entry = getnetanswer(&buf, anslen, BYNAME);
289 return (_getnetbyname(net));