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 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro 54 * Dep. Matematica Universidade de Coimbra, Portugal, Europe 55 * 56 * Permission to use, copy, modify, and distribute this software for any 57 * purpose with or without fee is hereby granted, provided that the above 58 * copyright notice and this permission notice appear in all copies. 59 */ 60 61 #if defined(LIBC_SCCS) && !defined(lint) 62 static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93"; 63 #endif /* LIBC_SCCS and not lint */ 64 #include <sys/cdefs.h> 65 __FBSDID("$FreeBSD$"); 66 67 #include <sys/param.h> 68 #include <sys/socket.h> 69 #include <netinet/in.h> 70 #include <arpa/inet.h> 71 #include <arpa/nameser.h> 72 73 #include <stdio.h> 74 #include <netdb.h> 75 #include <resolv.h> 76 #include <ctype.h> 77 #include <string.h> 78 #include <unistd.h> 79 #include <syslog.h> 80 #include <stdarg.h> 81 #include <nsswitch.h> 82 83 #include "res_config.h" 84 85 extern int h_errno; 86 87 #define BYADDR 0 88 #define BYNAME 1 89 #define MAXALIASES 35 90 91 #if PACKETSZ > 1024 92 #define MAXPACKET PACKETSZ 93 #else 94 #define MAXPACKET 1024 95 #endif 96 97 typedef union { 98 HEADER hdr; 99 u_char buf[MAXPACKET]; 100 } querybuf; 101 102 typedef union { 103 long al; 104 char ac; 105 } align; 106 107 static struct netent * 108 getnetanswer(answer, anslen, net_i) 109 querybuf *answer; 110 int anslen; 111 int net_i; 112 { 113 114 HEADER *hp; 115 u_char *cp; 116 int n; 117 u_char *eom; 118 int type, class, ancount, qdcount, haveanswer, i, nchar; 119 char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN]; 120 char *in, *st, *pauxt, *bp, *ep, **ap; 121 char *paux1 = &aux1[0], *paux2 = &aux2[0], flag = 0; 122 static struct netent net_entry; 123 static char *net_aliases[MAXALIASES], netbuf[PACKETSZ]; 124 125 /* 126 * find first satisfactory answer 127 * 128 * answer --> +------------+ ( MESSAGE ) 129 * | Header | 130 * +------------+ 131 * | Question | the question for the name server 132 * +------------+ 133 * | Answer | RRs answering the question 134 * +------------+ 135 * | Authority | RRs pointing toward an authority 136 * | Additional | RRs holding additional information 137 * +------------+ 138 */ 139 eom = answer->buf + anslen; 140 hp = &answer->hdr; 141 ancount = ntohs(hp->ancount); /* #/records in the answer section */ 142 qdcount = ntohs(hp->qdcount); /* #/entries in the question section */ 143 bp = netbuf; 144 ep = netbuf + sizeof(netbuf); 145 cp = answer->buf + HFIXEDSZ; 146 if (!qdcount) { 147 if (hp->aa) 148 h_errno = HOST_NOT_FOUND; 149 else 150 h_errno = TRY_AGAIN; 151 return (NULL); 152 } 153 while (qdcount-- > 0) 154 cp += __dn_skipname(cp, eom) + QFIXEDSZ; 155 ap = net_aliases; 156 *ap = NULL; 157 net_entry.n_aliases = net_aliases; 158 haveanswer = 0; 159 while (--ancount >= 0 && cp < eom) { 160 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 161 if ((n < 0) || !res_dnok(bp)) 162 break; 163 cp += n; 164 ans[0] = '\0'; 165 (void)strncpy(&ans[0], bp, sizeof(ans) - 1); 166 ans[sizeof(ans) - 1] = '\0'; 167 GETSHORT(type, cp); 168 GETSHORT(class, cp); 169 cp += INT32SZ; /* TTL */ 170 GETSHORT(n, cp); 171 if (class == C_IN && type == T_PTR) { 172 n = dn_expand(answer->buf, eom, cp, bp, ep - bp); 173 if ((n < 0) || !res_hnok(bp)) { 174 cp += n; 175 return (NULL); 176 } 177 cp += n; 178 *ap++ = bp; 179 n = strlen(bp) + 1; 180 bp += n; 181 net_entry.n_addrtype = 182 (class == C_IN) ? AF_INET : AF_UNSPEC; 183 haveanswer++; 184 } 185 } 186 if (haveanswer) { 187 *ap = NULL; 188 switch (net_i) { 189 case BYADDR: 190 net_entry.n_name = *net_entry.n_aliases; 191 net_entry.n_net = 0L; 192 break; 193 case BYNAME: 194 in = *net_entry.n_aliases; 195 net_entry.n_name = &ans[0]; 196 aux2[0] = '\0'; 197 for (i = 0; i < 4; i++) { 198 for (st = in, nchar = 0; 199 *st != '.'; 200 st++, nchar++) 201 ; 202 if (nchar != 1 || *in != '0' || flag) { 203 flag = 1; 204 (void)strncpy(paux1, 205 (i==0) ? in : in-1, 206 (i==0) ?nchar : nchar+1); 207 paux1[(i==0) ? nchar : nchar+1] = '\0'; 208 pauxt = paux2; 209 paux2 = strcat(paux1, paux2); 210 paux1 = pauxt; 211 } 212 in = ++st; 213 } 214 net_entry.n_net = inet_network(paux2); 215 break; 216 } 217 net_entry.n_aliases++; 218 return (&net_entry); 219 } 220 h_errno = TRY_AGAIN; 221 return (NULL); 222 } 223 224 int 225 _dns_getnetbyaddr(void *rval, void *cb_data, va_list ap) 226 { 227 unsigned long net; 228 int net_type; 229 unsigned int netbr[4]; 230 int nn, anslen; 231 querybuf buf; 232 char qbuf[MAXDNAME]; 233 unsigned long net2; 234 struct netent *net_entry; 235 236 net = va_arg(ap, unsigned long); 237 net_type = va_arg(ap, int); 238 239 *(struct netent **)rval = NULL; 240 241 if (net_type != AF_INET) 242 return NS_UNAVAIL; 243 244 for (nn = 4, net2 = net; net2; net2 >>= 8) 245 netbr[--nn] = net2 & 0xff; 246 switch (nn) { 247 case 3: /* Class A */ 248 sprintf(qbuf, "0.0.0.%u.in-addr.arpa", netbr[3]); 249 break; 250 case 2: /* Class B */ 251 sprintf(qbuf, "0.0.%u.%u.in-addr.arpa", netbr[3], netbr[2]); 252 break; 253 case 1: /* Class C */ 254 sprintf(qbuf, "0.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 255 netbr[1]); 256 break; 257 case 0: /* Class D - E */ 258 sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", netbr[3], netbr[2], 259 netbr[1], netbr[0]); 260 break; 261 } 262 anslen = res_query(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf)); 263 if (anslen < 0) { 264 #ifdef DEBUG 265 if (_res.options & RES_DEBUG) 266 printf("res_query failed\n"); 267 #endif 268 return NS_UNAVAIL; 269 } 270 net_entry = getnetanswer(&buf, anslen, BYADDR); 271 if (net_entry) { 272 unsigned u_net = net; /* maybe net should be unsigned ? */ 273 274 /* Strip trailing zeros */ 275 while ((u_net & 0xff) == 0 && u_net != 0) 276 u_net >>= 8; 277 net_entry->n_net = u_net; 278 *(struct netent **)rval = net_entry; 279 return NS_SUCCESS; 280 } 281 return NS_NOTFOUND; 282 } 283 284 int 285 _dns_getnetbyname(void *rval, void *cb_data, va_list ap) 286 { 287 const char *net; 288 int anslen; 289 querybuf buf; 290 char qbuf[MAXDNAME]; 291 292 net = va_arg(ap, const char *); 293 294 *(struct netent**)rval = NULL; 295 296 if ((_res.options & RES_INIT) == 0 && res_init() == -1) { 297 h_errno = NETDB_INTERNAL; 298 return NS_UNAVAIL; 299 } 300 strncpy(qbuf, net, sizeof(qbuf) - 1); 301 qbuf[sizeof(qbuf) - 1] = '\0'; 302 anslen = res_search(qbuf, C_IN, T_PTR, (u_char *)&buf, sizeof(buf)); 303 if (anslen < 0) { 304 #ifdef DEBUG 305 if (_res.options & RES_DEBUG) 306 printf("res_query failed\n"); 307 #endif 308 return NS_UNAVAIL; 309 } 310 *(struct netent**)rval = getnetanswer(&buf, anslen, BYNAME); 311 return (*(struct netent**)rval != NULL) ? NS_SUCCESS : NS_NOTFOUND; 312 } 313 314 void 315 _setnetdnsent(stayopen) 316 int stayopen; 317 { 318 if (stayopen) 319 _res.options |= RES_STAYOPEN | RES_USEVC; 320 } 321 322 void 323 _endnetdnsent() 324 { 325 _res.options &= ~(RES_STAYOPEN | RES_USEVC); 326 res_close(); 327 } 328