1 /* 2 * Copyright (c) 1985 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7 #if defined(LIBC_SCCS) && !defined(lint) 8 static char sccsid[] = "@(#)gethostnamadr.c 6.18 (Berkeley) 05/12/87"; 9 #endif LIBC_SCCS and not lint 10 11 #include <sys/param.h> 12 #include <sys/socket.h> 13 #include <netinet/in.h> 14 #include <ctype.h> 15 #include <netdb.h> 16 #include <stdio.h> 17 #include <errno.h> 18 #include <arpa/inet.h> 19 #include <arpa/nameser.h> 20 #include <resolv.h> 21 22 #define MAXALIASES 35 23 #define MAXADDRS 35 24 25 static char *h_addr_ptrs[MAXADDRS + 1]; 26 27 static struct hostent host; 28 static char *host_aliases[MAXALIASES]; 29 static char hostbuf[BUFSIZ+1]; 30 static struct in_addr host_addr; 31 static char HOSTDB[] = "/etc/hosts"; 32 static FILE *hostf = NULL; 33 static char line[BUFSIZ+1]; 34 static char hostaddr[MAXADDRS]; 35 static char *host_addrs[2]; 36 static int stayopen = 0; 37 static char *any(); 38 39 typedef union { 40 HEADER qb1; 41 char qb2[PACKETSZ]; 42 } querybuf; 43 44 static union { 45 long al; 46 char ac; 47 } align; 48 49 50 int h_errno; 51 extern errno; 52 53 static struct hostent * 54 getanswer(msg, msglen, iquery) 55 char *msg; 56 int msglen, iquery; 57 { 58 register HEADER *hp; 59 register char *cp; 60 register int n; 61 querybuf answer; 62 char *eom, *bp, **ap; 63 int type, class, buflen, ancount, qdcount; 64 int haveanswer, getclass = C_ANY; 65 char **hap; 66 67 n = res_send(msg, msglen, (char *)&answer, sizeof(answer)); 68 if (n < 0) { 69 #ifdef DEBUG 70 int terrno; 71 terrno = errno; 72 if (_res.options & RES_DEBUG) 73 printf("res_send failed\n"); 74 errno = terrno; 75 #endif 76 h_errno = TRY_AGAIN; 77 return (NULL); 78 } 79 eom = (char *)&answer + n; 80 /* 81 * find first satisfactory answer 82 */ 83 hp = (HEADER *) &answer; 84 ancount = ntohs(hp->ancount); 85 qdcount = ntohs(hp->qdcount); 86 if (hp->rcode != NOERROR || ancount == 0) { 87 #ifdef DEBUG 88 if (_res.options & RES_DEBUG) 89 printf("rcode = %d, ancount=%d\n", hp->rcode, ancount); 90 #endif 91 switch (hp->rcode) { 92 case NXDOMAIN: 93 /* Check if it's an authoritive answer */ 94 if (hp->aa) 95 h_errno = HOST_NOT_FOUND; 96 else 97 h_errno = TRY_AGAIN; 98 break; 99 case SERVFAIL: 100 h_errno = TRY_AGAIN; 101 break; 102 case NOERROR: 103 h_errno = NO_ADDRESS; 104 break; 105 case FORMERR: 106 case NOTIMP: 107 case REFUSED: 108 h_errno = NO_RECOVERY; 109 } 110 return (NULL); 111 } 112 bp = hostbuf; 113 buflen = sizeof(hostbuf); 114 cp = (char *)&answer + sizeof(HEADER); 115 if (qdcount) { 116 if (iquery) { 117 if ((n = dn_expand((char *)&answer, eom, 118 cp, bp, buflen)) < 0) { 119 h_errno = NO_RECOVERY; 120 return (NULL); 121 } 122 cp += n + QFIXEDSZ; 123 host.h_name = bp; 124 n = strlen(bp) + 1; 125 bp += n; 126 buflen -= n; 127 } else 128 cp += dn_skip(cp) + QFIXEDSZ; 129 while (--qdcount > 0) 130 cp += dn_skip(cp) + QFIXEDSZ; 131 } else if (iquery) { 132 if (hp->aa) 133 h_errno = HOST_NOT_FOUND; 134 else 135 h_errno = TRY_AGAIN; 136 return (NULL); 137 } 138 ap = host_aliases; 139 host.h_aliases = host_aliases; 140 hap = h_addr_ptrs; 141 #if BSD >= 43 142 host.h_addr_list = h_addr_ptrs; 143 #else 144 host.h_addr = h_addr_ptrs[0]; 145 #endif 146 haveanswer = 0; 147 while (--ancount >= 0 && cp < eom) { 148 if ((n = dn_expand((char *)&answer, eom, cp, bp, buflen)) < 0) 149 break; 150 cp += n; 151 type = _getshort(cp); 152 cp += sizeof(u_short); 153 class = _getshort(cp); 154 cp += sizeof(u_short) + sizeof(u_long); 155 n = _getshort(cp); 156 cp += sizeof(u_short); 157 if (type == T_CNAME) { 158 cp += n; 159 if (ap >= &host_aliases[MAXALIASES-1]) 160 continue; 161 *ap++ = bp; 162 n = strlen(bp) + 1; 163 bp += n; 164 buflen -= n; 165 continue; 166 } 167 if (type == T_PTR) { 168 if ((n = dn_expand((char *)&answer, eom, 169 cp, bp, buflen)) < 0) { 170 cp += n; 171 continue; 172 } 173 cp += n; 174 host.h_name = bp; 175 return(&host); 176 } 177 if (type != T_A) { 178 #ifdef DEBUG 179 if (_res.options & RES_DEBUG) 180 printf("unexpected answer type %d, size %d\n", 181 type, n); 182 #endif 183 cp += n; 184 continue; 185 } 186 if (haveanswer) { 187 if (n != host.h_length) { 188 cp += n; 189 continue; 190 } 191 if (class != getclass) { 192 cp += n; 193 continue; 194 } 195 } else { 196 host.h_length = n; 197 getclass = class; 198 host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC; 199 if (!iquery) { 200 host.h_name = bp; 201 bp += strlen(bp) + 1; 202 } 203 } 204 205 bp += (sizeof(align) - ((u_long)bp % sizeof(align))) &~ 206 sizeof(align); 207 208 if (bp + n >= &hostbuf[sizeof(hostbuf)]) { 209 #ifdef DEBUG 210 if (_res.options & RES_DEBUG) 211 printf("size (%d) too big\n", n); 212 #endif 213 break; 214 } 215 bcopy(cp, *hap++ = bp, n); 216 bp +=n; 217 cp += n; 218 haveanswer++; 219 } 220 if (haveanswer) { 221 *ap = NULL; 222 *hap = NULL; 223 return (&host); 224 } else { 225 h_errno = TRY_AGAIN; 226 return (NULL); 227 } 228 } 229 230 struct hostent * 231 gethostbyname(name) 232 char *name; 233 { 234 register char *cp, **domain; 235 int n; 236 struct hostent *hp, *gethostdomain(); 237 char *hostalias(); 238 extern struct hostent *_gethtbyname(); 239 240 if (!(_res.options & RES_INIT) && res_init() == -1) 241 return (NULL); 242 errno = 0; 243 for (cp = name, n = 0; *cp; cp++) 244 if (*cp == '.') 245 n++; 246 if ((n && cp[-1] == '.') || (_res.options & RES_DEFNAMES) == 0) { 247 cp[-1] = 0; 248 hp = gethostdomain(name, (char *)NULL); 249 cp[-1] = '.'; 250 return (hp); 251 } 252 if (n == 0 && (cp = hostalias(name))) 253 return (gethostdomain(cp, (char *)NULL)); 254 for (domain = _res.dnsrch; *domain; domain++) { 255 hp = gethostdomain(name, *domain); 256 if (hp) 257 return (hp); 258 if (errno == ECONNREFUSED) 259 return (_gethtbyname(name)); 260 if (h_errno != HOST_NOT_FOUND || 261 (_res.options & RES_DNSRCH) == 0) 262 return (NULL); 263 } 264 return (gethostdomain(name, (char *)NULL)); 265 } 266 267 static struct hostent * 268 gethostdomain(name, domain) 269 char *name, *domain; 270 { 271 querybuf buf; 272 char nbuf[2*MAXDNAME+2]; 273 int n; 274 275 sprintf(nbuf, "%.*s.%.*s", MAXDNAME, name, MAXDNAME, domain); 276 n = res_mkquery(QUERY, nbuf, C_IN, T_A, (char *)NULL, 0, NULL, 277 (char *)&buf, sizeof(buf)); 278 if (n < 0) { 279 #ifdef DEBUG 280 if (_res.options & RES_DEBUG) 281 printf("res_mkquery failed\n"); 282 #endif 283 return (NULL); 284 } 285 return (getanswer((char *)&buf, n, 0)); 286 } 287 288 struct hostent * 289 gethostbyaddr(addr, len, type) 290 char *addr; 291 int len, type; 292 { 293 int n; 294 querybuf buf; 295 register struct hostent *hp; 296 char qbuf[MAXDNAME]; 297 extern struct hostent *_gethtbyaddr(); 298 299 if (type != AF_INET) 300 return (NULL); 301 (void)sprintf(qbuf, "%d.%d.%d.%d.in-addr.arpa", 302 ((unsigned)addr[3] & 0xff), 303 ((unsigned)addr[2] & 0xff), 304 ((unsigned)addr[1] & 0xff), 305 ((unsigned)addr[0] & 0xff)); 306 n = res_mkquery(QUERY, qbuf, C_IN, T_PTR, (char *)NULL, 0, NULL, 307 (char *)&buf, sizeof(buf)); 308 if (n < 0) { 309 #ifdef DEBUG 310 if (_res.options & RES_DEBUG) 311 printf("res_mkquery failed\n"); 312 #endif 313 return (NULL); 314 } 315 hp = getanswer((char *)&buf, n, 1); 316 if (hp == NULL && errno == ECONNREFUSED) 317 hp = _gethtbyaddr(addr, len, type); 318 if (hp == NULL) 319 return(NULL); 320 hp->h_addrtype = type; 321 hp->h_length = len; 322 h_addr_ptrs[0] = (char *)&host_addr; 323 h_addr_ptrs[1] = (char *)0; 324 host_addr = *(struct in_addr *)addr; 325 return(hp); 326 } 327 328 char * 329 hostalias(name) 330 register char *name; 331 { 332 FILE *fp; 333 char *file, *getenv(); 334 static char abuf[MAXDNAME]; 335 char nbuf[MAXDNAME]; 336 int n; 337 338 file = getenv("HOSTALIASES"); 339 if (file == NULL || (fp = fopen(file, "r")) == NULL) 340 return (NULL); 341 while ((n = fscanf(fp, "%s %s\n", nbuf, abuf)) != EOF) 342 if (strcmp(nbuf, name) == 0) { 343 fclose(fp); 344 return (abuf); 345 } 346 fclose(fp); 347 return (NULL); 348 } 349 350 _sethtent(f) 351 int f; 352 { 353 if (hostf == NULL) 354 hostf = fopen(HOSTDB, "r" ); 355 else 356 rewind(hostf); 357 stayopen |= f; 358 } 359 360 _endhtent() 361 { 362 if (hostf && !stayopen) { 363 (void) fclose(hostf); 364 hostf = NULL; 365 } 366 } 367 368 struct hostent * 369 _gethtent() 370 { 371 char *p; 372 register char *cp, **q; 373 374 if (hostf == NULL && (hostf = fopen(HOSTDB, "r" )) == NULL) 375 return (NULL); 376 again: 377 if ((p = fgets(line, BUFSIZ, hostf)) == NULL) 378 return (NULL); 379 if (*p == '#') 380 goto again; 381 cp = any(p, "#\n"); 382 if (cp == NULL) 383 goto again; 384 *cp = '\0'; 385 cp = any(p, " \t"); 386 if (cp == NULL) 387 goto again; 388 *cp++ = '\0'; 389 /* THIS STUFF IS INTERNET SPECIFIC */ 390 #if BSD >= 43 391 host.h_addr_list = host_addrs; 392 #endif 393 host.h_addr = hostaddr; 394 *((u_long *)host.h_addr) = inet_addr(p); 395 host.h_length = sizeof (u_long); 396 host.h_addrtype = AF_INET; 397 while (*cp == ' ' || *cp == '\t') 398 cp++; 399 host.h_name = cp; 400 q = host.h_aliases = host_aliases; 401 cp = any(cp, " \t"); 402 if (cp != NULL) 403 *cp++ = '\0'; 404 while (cp && *cp) { 405 if (*cp == ' ' || *cp == '\t') { 406 cp++; 407 continue; 408 } 409 if (q < &host_aliases[MAXALIASES - 1]) 410 *q++ = cp; 411 cp = any(cp, " \t"); 412 if (cp != NULL) 413 *cp++ = '\0'; 414 } 415 *q = NULL; 416 return (&host); 417 } 418 419 static char * 420 any(cp, match) 421 register char *cp; 422 char *match; 423 { 424 register char *mp, c; 425 426 while (c = *cp) { 427 for (mp = match; *mp; mp++) 428 if (*mp == c) 429 return (cp); 430 cp++; 431 } 432 return ((char *)0); 433 } 434 435 struct hostent * 436 _gethtbyname(name) 437 char *name; 438 { 439 register struct hostent *p; 440 register char **cp; 441 char lowname[128]; 442 register char *lp = lowname; 443 444 while (*name) 445 if (isupper(*name)) 446 *lp++ = tolower(*name++); 447 else 448 *lp++ = *name++; 449 *lp = '\0'; 450 451 _sethtent(0); 452 while (p = _gethtent()) { 453 if (strcmp(p->h_name, lowname) == 0) 454 break; 455 for (cp = p->h_aliases; *cp != 0; cp++) 456 if (strcmp(*cp, lowname) == 0) 457 goto found; 458 } 459 found: 460 _endhtent(); 461 return (p); 462 } 463 464 struct hostent * 465 _gethtbyaddr(addr, len, type) 466 char *addr; 467 int len, type; 468 { 469 register struct hostent *p; 470 471 _sethtent(0); 472 while (p = _gethtent()) 473 if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len)) 474 break; 475 _endhtent(); 476 return (p); 477 } 478