1 /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
2 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
5 * Copyright (c) 1983 Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)getnetnamadr.c 1.4 (Coimbra) 93/06/03";
39 static char rcsid[] = "$Id$";
40 #endif /* LIBC_SCCS and not lint */
42 #include <sys/param.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <arpa/nameser.h>
57 #if defined(mips) && defined(SYSTYPE_BSD43)
61 struct netent *_getnetbyaddr __P((long net, int type));
63 struct netent *_getnetbyname __P((char *name));
65 struct netent *_getnetbyname __P((const char *name));
73 #define MAXPACKET PACKETSZ
75 #define MAXPACKET 1024
80 u_char buf[MAXPACKET];
88 static struct netent *
89 getnetanswer(answer, anslen, net_i)
99 int type, class, buflen, ancount, qdcount,
100 haveanswer, i, nchar,
103 char aux1[30], aux2[30], ans[30],
104 *in, *st, *pauxt, *bp, **ap,
108 static struct netent net_entry;
109 static char *net_aliases[MAXALIASES],
113 * find first satisfactory answer
115 * answer --> +------------+ ( MESSAGE )
118 * | Question | the question for the name server
120 * | Answer | RRs answering the question
122 * | Authority | RRs pointing toward an authority
123 * | Additional | RRs holding additional information
126 eom = answer->buf + anslen;
128 ancount = ntohs(hp->ancount); /* #/records in the answer section */
129 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */
131 buflen = sizeof(netbuf);
132 cp = answer->buf + HFIXEDSZ;
135 h_errno = HOST_NOT_FOUND;
139 return ((struct netent *) NULL);
141 while (qdcount-- > 0){
142 cp += __dn_skipname(cp, eom) + QFIXEDSZ;
146 net_entry.n_aliases = net_aliases;
148 while (--ancount >= 0 && cp < eom) {
149 n = dn_expand(answer->buf, eom, cp, bp, buflen);
154 (void)strcpy(&ans[0], bp);
157 cp += INT32SZ; /* TTL */
159 if (class == C_IN && type == T_PTR) {
160 n = dn_expand(answer->buf, eom, cp, bp, buflen);
167 bp += (strlen(bp) + 1);
168 net_entry.n_addrtype = (class == C_IN)
178 net_entry.n_name = *net_entry.n_aliases;
179 net_entry.n_net = 0L;
182 in = *net_entry.n_aliases;
183 net_entry.n_name = &ans[0];
185 for (i = 0; i < 4; i++) {
186 for (st = in, nchar = 0;
190 if (nchar != 1 || *in != '0' || flag) {
194 (i==0) ?nchar :nchar+1);
195 paux1[(i==0) ?nchar :nchar+1] = '\0';
197 paux2 = strcat(paux1, paux2);
202 net_entry.n_net = inet_network(paux2);
204 net_entry.n_aliases++;
208 return ((struct netent *) NULL);
213 getnetbyaddr(net, net_type)
215 register int net_type;
217 unsigned int netbr[4];
222 struct netent *net_entry;
224 if (net_type != AF_INET)
225 return (_getnetbyaddr(net, net_type));
227 for (nn = 4, net2 = net; net2; net2 >>= 8) {
228 netbr[--nn] = net2 & 0xff;
231 case 3: /* Class A */
232 (void)sprintf(qbuf, "0.0.0.%u.in-addr.arpa",
235 case 2: /* Class B */
236 (void)sprintf(qbuf, "0.0.%u.%u.in-addr.arpa",
239 case 1: /* Class C */
240 (void)sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa",
241 netbr[3], netbr[2], netbr[1]);
243 case 0: /* Class D - E */
244 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa",
245 netbr[3], netbr[2], netbr[1], netbr[0]);
248 anslen = res_query(qbuf, C_IN, T_PTR, buf.buf, sizeof buf.buf);
251 if (_res.options & RES_DEBUG)
252 printf("res_query failed\n");
254 if (errno == ECONNREFUSED)
255 return (_getnetbyaddr(net, net_type));
256 return (_getnetbyaddr(net, net_type));
258 net_entry = getnetanswer(&buf, anslen, BYADDR);
260 unsigned u_net = net; /* maybe net should be unsigned ? */
262 /* Strip trailing zeros */
263 while ((u_net & 0xff) == 0 && u_net != 0) {
266 net_entry->n_net = u_net;
269 return (_getnetbyaddr(net, net_type));
278 register const char *net;
281 unsigned int netbr[4];
285 struct netent *net_entry;
287 (void)strcpy(&qbuf[0],net);
288 anslen = res_search(qbuf, C_IN, T_PTR, buf.buf, sizeof buf.buf);
291 if (_res.options & RES_DEBUG)
292 printf("res_query failed\n");
294 if (errno == ECONNREFUSED)
295 return (_getnetbyname(net));
296 return (_getnetbyname(net));
298 net_entry = getnetanswer(&buf, anslen, BYNAME);
302 return (_getnetbyname(net));