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