1 /* 2 * Copyright (c) 1985, 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #if defined(LIBC_SCCS) && !defined(lint) 9 static char sccsid[] = "@(#)gethostnamadr.c 6.48 (Berkeley) 01/10/93"; 10 #endif /* LIBC_SCCS and not lint */ 11 12 #include <sys/param.h> 13 #include <sys/socket.h> 14 #include <netinet/in.h> 15 #include <arpa/inet.h> 16 #include <arpa/nameser.h> 17 #include <netdb.h> 18 #include <resolv.h> 19 #include <stdio.h> 20 #include <ctype.h> 21 #include <errno.h> 22 #include <string.h> 23 24 #define MAXALIASES 35 25 #define MAXADDRS 35 26 27 static char *h_addr_ptrs[MAXADDRS + 1]; 28 29 static struct hostent host; 30 static char *host_aliases[MAXALIASES]; 31 static char hostbuf[BUFSIZ+1]; 32 static struct in_addr host_addr; 33 static FILE *hostf = NULL; 34 static char hostaddr[MAXADDRS]; 35 static char *host_addrs[2]; 36 static int stayopen = 0; 37 char *strpbrk(); 38 39 #if PACKETSZ > 1024 40 #define MAXPACKET PACKETSZ 41 #else 42 #define MAXPACKET 1024 43 #endif 44 45 typedef union { 46 HEADER hdr; 47 u_char buf[MAXPACKET]; 48 } querybuf; 49 50 typedef union { 51 long al; 52 char ac; 53 } align; 54 55 int h_errno; 56 57 static struct hostent * 58 getanswer(answer, anslen, iquery) 59 querybuf *answer; 60 int anslen; 61 int iquery; 62 { 63 register HEADER *hp; 64 register u_char *cp; 65 register int n; 66 u_char *eom; 67 char *bp, **ap; 68 int type, class, buflen, ancount, qdcount; 69 int haveanswer, getclass = C_ANY; 70 char **hap; 71 72 eom = answer->buf + anslen; 73 /* 74 * find first satisfactory answer 75 */ 76 hp = &answer->hdr; 77 ancount = ntohs(hp->ancount); 78 qdcount = ntohs(hp->qdcount); 79 bp = hostbuf; 80 buflen = sizeof(hostbuf); 81 cp = answer->buf + sizeof(HEADER); 82 if (qdcount) { 83 if (iquery) { 84 if ((n = dn_expand((u_char *)answer->buf, 85 (u_char *)eom, (u_char *)cp, (u_char *)bp, 86 buflen)) < 0) { 87 h_errno = NO_RECOVERY; 88 return ((struct hostent *) NULL); 89 } 90 cp += n + QFIXEDSZ; 91 host.h_name = bp; 92 n = strlen(bp) + 1; 93 bp += n; 94 buflen -= n; 95 } else 96 cp += __dn_skipname(cp, eom) + QFIXEDSZ; 97 while (--qdcount > 0) 98 cp += __dn_skipname(cp, eom) + QFIXEDSZ; 99 } else if (iquery) { 100 if (hp->aa) 101 h_errno = HOST_NOT_FOUND; 102 else 103 h_errno = TRY_AGAIN; 104 return ((struct hostent *) NULL); 105 } 106 ap = host_aliases; 107 *ap = NULL; 108 host.h_aliases = host_aliases; 109 hap = h_addr_ptrs; 110 *hap = NULL; 111 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */ 112 host.h_addr_list = h_addr_ptrs; 113 #endif 114 haveanswer = 0; 115 while (--ancount >= 0 && cp < eom) { 116 if ((n = dn_expand((u_char *)answer->buf, (u_char *)eom, 117 (u_char *)cp, (u_char *)bp, buflen)) < 0) 118 break; 119 cp += n; 120 type = _getshort(cp); 121 cp += sizeof(u_short); 122 class = _getshort(cp); 123 cp += sizeof(u_short) + sizeof(u_long); 124 n = _getshort(cp); 125 cp += sizeof(u_short); 126 if (type == T_CNAME) { 127 cp += n; 128 if (ap >= &host_aliases[MAXALIASES-1]) 129 continue; 130 *ap++ = bp; 131 n = strlen(bp) + 1; 132 bp += n; 133 buflen -= n; 134 continue; 135 } 136 if (iquery && type == T_PTR) { 137 if ((n = dn_expand((u_char *)answer->buf, 138 (u_char *)eom, (u_char *)cp, (u_char *)bp, 139 buflen)) < 0) 140 break; 141 cp += n; 142 host.h_name = bp; 143 return(&host); 144 } 145 if (iquery || type != T_A) { 146 #ifdef DEBUG 147 if (_res.options & RES_DEBUG) 148 printf("unexpected answer type %d, size %d\n", 149 type, n); 150 #endif 151 cp += n; 152 continue; 153 } 154 if (haveanswer) { 155 if (n != host.h_length) { 156 cp += n; 157 continue; 158 } 159 if (class != getclass) { 160 cp += n; 161 continue; 162 } 163 } else { 164 host.h_length = n; 165 getclass = class; 166 host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC; 167 if (!iquery) { 168 host.h_name = bp; 169 bp += strlen(bp) + 1; 170 } 171 } 172 173 bp += sizeof(align) - ((u_long)bp % sizeof(align)); 174 175 if (bp + n >= &hostbuf[sizeof(hostbuf)]) { 176 #ifdef DEBUG 177 if (_res.options & RES_DEBUG) 178 printf("size (%d) too big\n", n); 179 #endif 180 break; 181 } 182 bcopy(cp, *hap++ = bp, n); 183 bp +=n; 184 cp += n; 185 haveanswer++; 186 } 187 if (haveanswer) { 188 *ap = NULL; 189 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */ 190 *hap = NULL; 191 #else 192 host.h_addr = h_addr_ptrs[0]; 193 #endif 194 return (&host); 195 } else { 196 h_errno = TRY_AGAIN; 197 return ((struct hostent *) NULL); 198 } 199 } 200 201 struct hostent * 202 gethostbyname(name) 203 const char *name; 204 { 205 querybuf buf; 206 register const char *cp; 207 int n; 208 extern struct hostent *_gethtbyname(); 209 210 /* 211 * disallow names consisting only of digits/dots, unless 212 * they end in a dot. 213 */ 214 if (isdigit(name[0])) 215 for (cp = name;; ++cp) { 216 if (!*cp) { 217 if (*--cp == '.') 218 break; 219 /* 220 * All-numeric, no dot at the end. 221 * Fake up a hostent as if we'd actually 222 * done a lookup. 223 */ 224 if (!inet_aton(name, &host_addr)) { 225 h_errno = HOST_NOT_FOUND; 226 return((struct hostent *) NULL); 227 } 228 host.h_name = (char *)name; 229 host.h_aliases = host_aliases; 230 host_aliases[0] = NULL; 231 host.h_addrtype = AF_INET; 232 host.h_length = sizeof(u_long); 233 h_addr_ptrs[0] = (char *)&host_addr; 234 h_addr_ptrs[1] = (char *)0; 235 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */ 236 host.h_addr_list = h_addr_ptrs; 237 #else 238 host.h_addr = h_addr_ptrs[0]; 239 #endif 240 return (&host); 241 } 242 if (!isdigit(*cp) && *cp != '.') 243 break; 244 } 245 246 if ((n = res_search(name, C_IN, T_A, buf.buf, sizeof(buf))) < 0) { 247 #ifdef DEBUG 248 if (_res.options & RES_DEBUG) 249 printf("res_search failed\n"); 250 #endif 251 if (errno == ECONNREFUSED) 252 return (_gethtbyname(name)); 253 else 254 return ((struct hostent *) NULL); 255 } 256 return (getanswer(&buf, n, 0)); 257 } 258 259 struct hostent * 260 gethostbyaddr(addr, len, type) 261 const char *addr; 262 int len, type; 263 { 264 int n; 265 querybuf buf; 266 register struct hostent *hp; 267 char qbuf[MAXDNAME]; 268 extern struct hostent *_gethtbyaddr(); 269 270 if (type != AF_INET) 271 return ((struct hostent *) NULL); 272 (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", 273 ((unsigned)addr[3] & 0xff), 274 ((unsigned)addr[2] & 0xff), 275 ((unsigned)addr[1] & 0xff), 276 ((unsigned)addr[0] & 0xff)); 277 n = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof(buf)); 278 if (n < 0) { 279 #ifdef DEBUG 280 if (_res.options & RES_DEBUG) 281 printf("res_query failed\n"); 282 #endif 283 if (errno == ECONNREFUSED) 284 return (_gethtbyaddr(addr, len, type)); 285 return ((struct hostent *) NULL); 286 } 287 hp = getanswer(&buf, n, 1); 288 if (hp == NULL) 289 return ((struct hostent *) NULL); 290 hp->h_addrtype = type; 291 hp->h_length = len; 292 h_addr_ptrs[0] = (char *)&host_addr; 293 h_addr_ptrs[1] = (char *)0; 294 host_addr = *(struct in_addr *)addr; 295 #if BSD < 43 && !defined(h_addr) /* new-style hostent structure */ 296 hp->h_addr = h_addr_ptrs[0]; 297 #endif 298 return(hp); 299 } 300 301 _sethtent(f) 302 int f; 303 { 304 if (hostf == NULL) 305 hostf = fopen(_PATH_HOSTS, "r" ); 306 else 307 rewind(hostf); 308 stayopen |= f; 309 } 310 311 _endhtent() 312 { 313 if (hostf && !stayopen) { 314 (void) fclose(hostf); 315 hostf = NULL; 316 } 317 } 318 319 struct hostent * 320 _gethtent() 321 { 322 char *p; 323 register char *cp, **q; 324 325 if (hostf == NULL && (hostf = fopen(_PATH_HOSTS, "r" )) == NULL) 326 return (NULL); 327 again: 328 if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL) 329 return (NULL); 330 if (*p == '#') 331 goto again; 332 cp = strpbrk(p, "#\n"); 333 if (cp == NULL) 334 goto again; 335 *cp = '\0'; 336 cp = strpbrk(p, " \t"); 337 if (cp == NULL) 338 goto again; 339 *cp++ = '\0'; 340 /* THIS STUFF IS INTERNET SPECIFIC */ 341 #if BSD >= 43 || defined(h_addr) /* new-style hostent structure */ 342 host.h_addr_list = host_addrs; 343 #endif 344 host.h_addr = hostaddr; 345 *((u_long *)host.h_addr) = inet_addr(p); 346 host.h_length = sizeof (u_long); 347 host.h_addrtype = AF_INET; 348 while (*cp == ' ' || *cp == '\t') 349 cp++; 350 host.h_name = cp; 351 q = host.h_aliases = host_aliases; 352 cp = strpbrk(cp, " \t"); 353 if (cp != NULL) 354 *cp++ = '\0'; 355 while (cp && *cp) { 356 if (*cp == ' ' || *cp == '\t') { 357 cp++; 358 continue; 359 } 360 if (q < &host_aliases[MAXALIASES - 1]) 361 *q++ = cp; 362 cp = strpbrk(cp, " \t"); 363 if (cp != NULL) 364 *cp++ = '\0'; 365 } 366 *q = NULL; 367 return (&host); 368 } 369 370 struct hostent * 371 _gethtbyname(name) 372 char *name; 373 { 374 register struct hostent *p; 375 register char **cp; 376 377 _sethtent(0); 378 while (p = _gethtent()) { 379 if (strcasecmp(p->h_name, name) == 0) 380 break; 381 for (cp = p->h_aliases; *cp != 0; cp++) 382 if (strcasecmp(*cp, name) == 0) 383 goto found; 384 } 385 found: 386 _endhtent(); 387 return (p); 388 } 389 390 struct hostent * 391 _gethtbyaddr(addr, len, type) 392 const char *addr; 393 int len, type; 394 { 395 register struct hostent *p; 396 397 _sethtent(0); 398 while (p = _gethtent()) 399 if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len)) 400 break; 401 _endhtent(); 402 return (p); 403 } 404