121374Sdist /* 225302Skjd * Copyright (c) 1985 Regents of the University of California. 321374Sdist * All rights reserved. The Berkeley software License Agreement 421374Sdist * specifies the terms and conditions for redistribution. 521374Sdist */ 615662Sralph 726630Sdonn #if defined(LIBC_SCCS) && !defined(lint) 8*31994Skarels static char sccsid[] = "@(#)gethostnamadr.c 6.23 (Berkeley) 08/03/87"; 926630Sdonn #endif LIBC_SCCS and not lint 1021374Sdist 1130468Skjd #include <sys/param.h> 1218505Sralph #include <sys/socket.h> 1318505Sralph #include <netinet/in.h> 1426887Skjd #include <ctype.h> 1518505Sralph #include <netdb.h> 1615662Sralph #include <stdio.h> 1726887Skjd #include <errno.h> 1827034Skjd #include <arpa/inet.h> 1924508Sbloom #include <arpa/nameser.h> 2026894Skjd #include <resolv.h> 2115662Sralph 2215662Sralph #define MAXALIASES 35 2324687Sbloom #define MAXADDRS 35 2415662Sralph 2524687Sbloom static char *h_addr_ptrs[MAXADDRS + 1]; 2624687Sbloom 2724687Sbloom static struct hostent host; 2815662Sralph static char *host_aliases[MAXALIASES]; 2915912Sralph static char hostbuf[BUFSIZ+1]; 3025153Sbloom static struct in_addr host_addr; 3126887Skjd static char HOSTDB[] = "/etc/hosts"; 3226887Skjd static FILE *hostf = NULL; 3326887Skjd static char line[BUFSIZ+1]; 3426887Skjd static char hostaddr[MAXADDRS]; 3526887Skjd static char *host_addrs[2]; 3626887Skjd static int stayopen = 0; 3726887Skjd static char *any(); 3815662Sralph 3925302Skjd typedef union { 4025302Skjd HEADER qb1; 4125302Skjd char qb2[PACKETSZ]; 4225302Skjd } querybuf; 4324509Sbloom 4425302Skjd static union { 4525302Skjd long al; 4625302Skjd char ac; 4725302Skjd } align; 4825302Skjd 4925302Skjd 5025484Skjd int h_errno; 5126887Skjd extern errno; 5225484Skjd 5315662Sralph static struct hostent * 5418505Sralph getanswer(msg, msglen, iquery) 5518505Sralph char *msg; 5618505Sralph int msglen, iquery; 5715662Sralph { 5818505Sralph register HEADER *hp; 5918505Sralph register char *cp; 6018505Sralph register int n; 6125302Skjd querybuf answer; 6218505Sralph char *eom, *bp, **ap; 6327034Skjd int type, class, buflen, ancount, qdcount; 6427034Skjd int haveanswer, getclass = C_ANY; 6524687Sbloom char **hap; 6615662Sralph 6725302Skjd n = res_send(msg, msglen, (char *)&answer, sizeof(answer)); 6818505Sralph if (n < 0) { 6924733Sbloom #ifdef DEBUG 7026887Skjd int terrno; 7126887Skjd terrno = errno; 7218505Sralph if (_res.options & RES_DEBUG) 7318531Sralph printf("res_send failed\n"); 7426887Skjd errno = terrno; 7524733Sbloom #endif 7625484Skjd h_errno = TRY_AGAIN; 7718505Sralph return (NULL); 7815662Sralph } 7925302Skjd eom = (char *)&answer + n; 8018505Sralph /* 8118505Sralph * find first satisfactory answer 8218505Sralph */ 8325302Skjd hp = (HEADER *) &answer; 8418505Sralph ancount = ntohs(hp->ancount); 8525153Sbloom qdcount = ntohs(hp->qdcount); 8618505Sralph if (hp->rcode != NOERROR || ancount == 0) { 8724733Sbloom #ifdef DEBUG 8818505Sralph if (_res.options & RES_DEBUG) 8918505Sralph printf("rcode = %d, ancount=%d\n", hp->rcode, ancount); 9024733Sbloom #endif 9125484Skjd switch (hp->rcode) { 9225484Skjd case NXDOMAIN: 9325484Skjd /* Check if it's an authoritive answer */ 9425484Skjd if (hp->aa) 9525484Skjd h_errno = HOST_NOT_FOUND; 9625484Skjd else 9725484Skjd h_errno = TRY_AGAIN; 9825484Skjd break; 9925484Skjd case SERVFAIL: 10025484Skjd h_errno = TRY_AGAIN; 10125484Skjd break; 10225484Skjd case NOERROR: 10325484Skjd h_errno = NO_ADDRESS; 10425484Skjd break; 10525484Skjd case FORMERR: 10625484Skjd case NOTIMP: 10725484Skjd case REFUSED: 10825484Skjd h_errno = NO_RECOVERY; 10925484Skjd } 11018505Sralph return (NULL); 11118505Sralph } 11218505Sralph bp = hostbuf; 11318505Sralph buflen = sizeof(hostbuf); 11425302Skjd cp = (char *)&answer + sizeof(HEADER); 11525153Sbloom if (qdcount) { 11618505Sralph if (iquery) { 11726066Skjd if ((n = dn_expand((char *)&answer, eom, 11826066Skjd cp, bp, buflen)) < 0) { 11925484Skjd h_errno = NO_RECOVERY; 12018505Sralph return (NULL); 12125484Skjd } 12218505Sralph cp += n + QFIXEDSZ; 12318505Sralph host.h_name = bp; 12418505Sralph n = strlen(bp) + 1; 12518505Sralph bp += n; 12618505Sralph buflen -= n; 12718505Sralph } else 12818505Sralph cp += dn_skip(cp) + QFIXEDSZ; 12925153Sbloom while (--qdcount > 0) 13025153Sbloom cp += dn_skip(cp) + QFIXEDSZ; 13125484Skjd } else if (iquery) { 13225484Skjd if (hp->aa) 13325484Skjd h_errno = HOST_NOT_FOUND; 13425484Skjd else 13525484Skjd h_errno = TRY_AGAIN; 13618505Sralph return (NULL); 13725484Skjd } 13824687Sbloom ap = host_aliases; 13924687Sbloom host.h_aliases = host_aliases; 14024687Sbloom hap = h_addr_ptrs; 14130468Skjd #if BSD >= 43 14224687Sbloom host.h_addr_list = h_addr_ptrs; 14330460Skjd #endif 14424687Sbloom haveanswer = 0; 14518505Sralph while (--ancount >= 0 && cp < eom) { 14626066Skjd if ((n = dn_expand((char *)&answer, eom, cp, bp, buflen)) < 0) 14724687Sbloom break; 14818505Sralph cp += n; 14930440Skjd type = _getshort(cp); 15018505Sralph cp += sizeof(u_short); 15130440Skjd class = _getshort(cp); 15218505Sralph cp += sizeof(u_short) + sizeof(u_long); 15330440Skjd n = _getshort(cp); 15418505Sralph cp += sizeof(u_short); 15518505Sralph if (type == T_CNAME) { 15618505Sralph cp += n; 15718505Sralph if (ap >= &host_aliases[MAXALIASES-1]) 15818505Sralph continue; 15918505Sralph *ap++ = bp; 16018505Sralph n = strlen(bp) + 1; 16118505Sralph bp += n; 16218505Sralph buflen -= n; 16318505Sralph continue; 16418505Sralph } 16525153Sbloom if (type == T_PTR) { 16627034Skjd if ((n = dn_expand((char *)&answer, eom, 16727034Skjd cp, bp, buflen)) < 0) { 16825153Sbloom cp += n; 16925153Sbloom continue; 17025153Sbloom } 17125153Sbloom cp += n; 17225153Sbloom host.h_name = bp; 17325153Sbloom return(&host); 17425153Sbloom } 17524687Sbloom if (type != T_A) { 17624733Sbloom #ifdef DEBUG 17718505Sralph if (_res.options & RES_DEBUG) 17818505Sralph printf("unexpected answer type %d, size %d\n", 17918505Sralph type, n); 18024733Sbloom #endif 18124687Sbloom cp += n; 18218505Sralph continue; 18318505Sralph } 18424687Sbloom if (haveanswer) { 18524687Sbloom if (n != host.h_length) { 18624687Sbloom cp += n; 18724687Sbloom continue; 18824687Sbloom } 18924687Sbloom if (class != getclass) { 19024687Sbloom cp += n; 19124687Sbloom continue; 19224687Sbloom } 19324687Sbloom } else { 19424687Sbloom host.h_length = n; 19524687Sbloom getclass = class; 19625386Skjd host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC; 19724687Sbloom if (!iquery) { 19824687Sbloom host.h_name = bp; 19924687Sbloom bp += strlen(bp) + 1; 20024687Sbloom } 20118505Sralph } 20225302Skjd 20331902Skarels bp += sizeof(align) - ((u_long)bp % sizeof(align)); 20425302Skjd 20518505Sralph if (bp + n >= &hostbuf[sizeof(hostbuf)]) { 20624733Sbloom #ifdef DEBUG 20718505Sralph if (_res.options & RES_DEBUG) 20818505Sralph printf("size (%d) too big\n", n); 20924733Sbloom #endif 21024687Sbloom break; 21118505Sralph } 21224687Sbloom bcopy(cp, *hap++ = bp, n); 21324687Sbloom bp +=n; 21424687Sbloom cp += n; 21524687Sbloom haveanswer++; 21624687Sbloom } 21724687Sbloom if (haveanswer) { 21824687Sbloom *ap = NULL; 21931902Skarels #if BSD >= 43 22024687Sbloom *hap = NULL; 22131902Skarels #else 22231902Skarels host.h_addr = h_addr_ptrs[0]; 22331902Skarels #endif 22418505Sralph return (&host); 22525484Skjd } else { 22625484Skjd h_errno = TRY_AGAIN; 22724687Sbloom return (NULL); 22825484Skjd } 22915662Sralph } 23015662Sralph 23115662Sralph struct hostent * 23218505Sralph gethostbyname(name) 23318505Sralph char *name; 23415662Sralph { 23531111Skarels register char *cp, **domain; 23618505Sralph int n; 23731111Skarels struct hostent *hp, *gethostdomain(); 23831111Skarels char *hostalias(); 23926887Skjd extern struct hostent *_gethtbyname(); 24015662Sralph 24131111Skarels if (!(_res.options & RES_INIT) && res_init() == -1) 24231111Skarels return (NULL); 243*31994Skarels if (isdigit(name[0])) { 244*31994Skarels h_errno = HOST_NOT_FOUND; 245*31994Skarels return (NULL); 246*31994Skarels 247*31994Skarels } 24829930Skjd errno = 0; 24931111Skarels for (cp = name, n = 0; *cp; cp++) 25031111Skarels if (*cp == '.') 25131111Skarels n++; 25231902Skarels if ((n && *--cp == '.') || (_res.options & RES_DEFNAMES) == 0) { 25331902Skarels if (n && *cp == '.') 25431902Skarels *cp = 0; 25531111Skarels hp = gethostdomain(name, (char *)NULL); 25631902Skarels if (n && cp == 0) 25731902Skarels *cp = '.'; 25831111Skarels return (hp); 25931111Skarels } 26031111Skarels if (n == 0 && (cp = hostalias(name))) 26131111Skarels return (gethostdomain(cp, (char *)NULL)); 26231111Skarels for (domain = _res.dnsrch; *domain; domain++) { 26331111Skarels hp = gethostdomain(name, *domain); 26431111Skarels if (hp) 26531111Skarels return (hp); 26631960Sbostic /* 26731960Sbostic * If no server present, use host table. 26831960Sbostic * If host isn't found in this domain, 26931960Sbostic * keep trying higher domains in the search list 27031960Sbostic * (if that's enabled). 27131960Sbostic * On a NO_ADDRESS error, keep trying, 27231960Sbostic * or a wildcard MX entry could keep us from finding 27331960Sbostic * host entries higher in the domain. 27431960Sbostic */ 27531111Skarels if (errno == ECONNREFUSED) 27631111Skarels return (_gethtbyname(name)); 27731960Sbostic if ((h_errno != HOST_NOT_FOUND && 27831960Sbostic h_errno != NO_ADDRESS) || 27931111Skarels (_res.options & RES_DNSRCH) == 0) 28031111Skarels return (NULL); 28131709Skarels h_errno = 0; 28231111Skarels } 28331111Skarels return (gethostdomain(name, (char *)NULL)); 28431111Skarels } 28531111Skarels 28631111Skarels static struct hostent * 28731111Skarels gethostdomain(name, domain) 28831111Skarels char *name, *domain; 28931111Skarels { 29031111Skarels querybuf buf; 29131111Skarels char nbuf[2*MAXDNAME+2]; 29231111Skarels int n; 29331111Skarels 29431902Skarels if (domain == NULL) 29531902Skarels sprintf(nbuf, "%.*s", MAXDNAME, name); 29631902Skarels else 29731902Skarels sprintf(nbuf, "%.*s.%.*s", MAXDNAME, name, MAXDNAME, domain); 29831111Skarels n = res_mkquery(QUERY, nbuf, C_IN, T_A, (char *)NULL, 0, NULL, 29925302Skjd (char *)&buf, sizeof(buf)); 30018505Sralph if (n < 0) { 30124733Sbloom #ifdef DEBUG 30218505Sralph if (_res.options & RES_DEBUG) 30318531Sralph printf("res_mkquery failed\n"); 30424733Sbloom #endif 30518505Sralph return (NULL); 30617761Sserge } 30731111Skarels return (getanswer((char *)&buf, n, 0)); 30815662Sralph } 30915662Sralph 31015662Sralph struct hostent * 31118505Sralph gethostbyaddr(addr, len, type) 31215662Sralph char *addr; 31318505Sralph int len, type; 31415662Sralph { 31518505Sralph int n; 31625302Skjd querybuf buf; 31725153Sbloom register struct hostent *hp; 31825153Sbloom char qbuf[MAXDNAME]; 31926887Skjd extern struct hostent *_gethtbyaddr(); 32026887Skjd 32118505Sralph if (type != AF_INET) 32218505Sralph return (NULL); 32325153Sbloom (void)sprintf(qbuf, "%d.%d.%d.%d.in-addr.arpa", 32425484Skjd ((unsigned)addr[3] & 0xff), 32525484Skjd ((unsigned)addr[2] & 0xff), 32625484Skjd ((unsigned)addr[1] & 0xff), 32725484Skjd ((unsigned)addr[0] & 0xff)); 32827034Skjd n = res_mkquery(QUERY, qbuf, C_IN, T_PTR, (char *)NULL, 0, NULL, 32925302Skjd (char *)&buf, sizeof(buf)); 33018505Sralph if (n < 0) { 33124733Sbloom #ifdef DEBUG 33218505Sralph if (_res.options & RES_DEBUG) 33318531Sralph printf("res_mkquery failed\n"); 33424733Sbloom #endif 33518505Sralph return (NULL); 33617761Sserge } 33726887Skjd hp = getanswer((char *)&buf, n, 1); 33826887Skjd if (hp == NULL && errno == ECONNREFUSED) 33926887Skjd hp = _gethtbyaddr(addr, len, type); 34026887Skjd if (hp == NULL) 34125153Sbloom return(NULL); 34225153Sbloom hp->h_addrtype = type; 34325153Sbloom hp->h_length = len; 34425282Sbloom h_addr_ptrs[0] = (char *)&host_addr; 34525153Sbloom h_addr_ptrs[1] = (char *)0; 34625153Sbloom host_addr = *(struct in_addr *)addr; 34725153Sbloom return(hp); 34815662Sralph } 34925302Skjd 35031111Skarels char * 35131111Skarels hostalias(name) 35231111Skarels register char *name; 35331111Skarels { 35431780Sbostic register char *C1, *C2; 35531111Skarels FILE *fp; 35631780Sbostic char *file, *getenv(), *strcpy(), *strncpy(); 35731780Sbostic char buf[BUFSIZ]; 35831111Skarels static char abuf[MAXDNAME]; 35926887Skjd 36031111Skarels file = getenv("HOSTALIASES"); 36131111Skarels if (file == NULL || (fp = fopen(file, "r")) == NULL) 36231111Skarels return (NULL); 36331780Sbostic buf[sizeof(buf) - 1] = '\0'; 36431780Sbostic while (fgets(buf, sizeof(buf), fp)) { 36531780Sbostic for (C1 = buf; *C1 && !isspace(*C1); ++C1); 36631780Sbostic if (!*C1 || *C1 == '\n') 36731780Sbostic break; 368*31994Skarels if (!strncasecmp(buf, name, C1 - buf)) { 36931780Sbostic while (isspace(*++C1)); 37031780Sbostic if (!*C1) 37131780Sbostic break; 37231780Sbostic for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2); 37331780Sbostic abuf[sizeof(abuf) - 1] = *C2 = '\0'; 37431780Sbostic (void)strncpy(abuf, C1, sizeof(abuf) - 1); 37531111Skarels fclose(fp); 37631111Skarels return (abuf); 37731111Skarels } 37831780Sbostic } 37931111Skarels fclose(fp); 38031111Skarels return (NULL); 38131111Skarels } 38231111Skarels 38326887Skjd _sethtent(f) 38426887Skjd int f; 38526887Skjd { 38626887Skjd if (hostf == NULL) 38726887Skjd hostf = fopen(HOSTDB, "r" ); 38826887Skjd else 38926887Skjd rewind(hostf); 39026887Skjd stayopen |= f; 39126887Skjd } 39226887Skjd 39326887Skjd _endhtent() 39426887Skjd { 39526887Skjd if (hostf && !stayopen) { 39627034Skjd (void) fclose(hostf); 39726887Skjd hostf = NULL; 39826887Skjd } 39926887Skjd } 40026887Skjd 40126887Skjd struct hostent * 40226887Skjd _gethtent() 40326887Skjd { 40426887Skjd char *p; 40526887Skjd register char *cp, **q; 40626887Skjd 40726887Skjd if (hostf == NULL && (hostf = fopen(HOSTDB, "r" )) == NULL) 40826887Skjd return (NULL); 40926887Skjd again: 41026887Skjd if ((p = fgets(line, BUFSIZ, hostf)) == NULL) 41126887Skjd return (NULL); 41226887Skjd if (*p == '#') 41326887Skjd goto again; 41426887Skjd cp = any(p, "#\n"); 41526887Skjd if (cp == NULL) 41626887Skjd goto again; 41726887Skjd *cp = '\0'; 41826887Skjd cp = any(p, " \t"); 41926887Skjd if (cp == NULL) 42026887Skjd goto again; 42126887Skjd *cp++ = '\0'; 42226887Skjd /* THIS STUFF IS INTERNET SPECIFIC */ 42330468Skjd #if BSD >= 43 42426887Skjd host.h_addr_list = host_addrs; 42530460Skjd #endif 42626887Skjd host.h_addr = hostaddr; 42726887Skjd *((u_long *)host.h_addr) = inet_addr(p); 42826887Skjd host.h_length = sizeof (u_long); 42926887Skjd host.h_addrtype = AF_INET; 43026887Skjd while (*cp == ' ' || *cp == '\t') 43126887Skjd cp++; 43226887Skjd host.h_name = cp; 43326887Skjd q = host.h_aliases = host_aliases; 43426887Skjd cp = any(cp, " \t"); 43526887Skjd if (cp != NULL) 43626887Skjd *cp++ = '\0'; 43726887Skjd while (cp && *cp) { 43826887Skjd if (*cp == ' ' || *cp == '\t') { 43926887Skjd cp++; 44026887Skjd continue; 44126887Skjd } 44226887Skjd if (q < &host_aliases[MAXALIASES - 1]) 44326887Skjd *q++ = cp; 44426887Skjd cp = any(cp, " \t"); 44526887Skjd if (cp != NULL) 44626887Skjd *cp++ = '\0'; 44726887Skjd } 44826887Skjd *q = NULL; 44926887Skjd return (&host); 45026887Skjd } 45126887Skjd 45226887Skjd static char * 45326887Skjd any(cp, match) 45426887Skjd register char *cp; 45526887Skjd char *match; 45626887Skjd { 45726887Skjd register char *mp, c; 45826887Skjd 45926887Skjd while (c = *cp) { 46026887Skjd for (mp = match; *mp; mp++) 46126887Skjd if (*mp == c) 46226887Skjd return (cp); 46326887Skjd cp++; 46426887Skjd } 46526887Skjd return ((char *)0); 46626887Skjd } 46726887Skjd 46826887Skjd struct hostent * 46926887Skjd _gethtbyname(name) 47026887Skjd char *name; 47126887Skjd { 47226887Skjd register struct hostent *p; 47326887Skjd register char **cp; 47428307Skarels 47526887Skjd _sethtent(0); 47626887Skjd while (p = _gethtent()) { 47731960Sbostic if (strcasecmp(p->h_name, name) == 0) 47826887Skjd break; 47926887Skjd for (cp = p->h_aliases; *cp != 0; cp++) 48031960Sbostic if (strcasecmp(*cp, name) == 0) 48126887Skjd goto found; 48226887Skjd } 48326887Skjd found: 48426887Skjd _endhtent(); 48526887Skjd return (p); 48626887Skjd } 48726887Skjd 48826887Skjd struct hostent * 48926887Skjd _gethtbyaddr(addr, len, type) 49026887Skjd char *addr; 49126887Skjd int len, type; 49226887Skjd { 49326887Skjd register struct hostent *p; 49426887Skjd 49526887Skjd _sethtent(0); 49626887Skjd while (p = _gethtent()) 49726909Skjd if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len)) 49826887Skjd break; 49926887Skjd _endhtent(); 50026887Skjd return (p); 50126887Skjd } 502