1 /* 2 * Copyright (c) 1983 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 #ifndef lint 8 static char sccsid[] = "@(#)gethostnamadr.c 5.3 (Berkeley) 12/10/85"; 9 #endif not lint 10 11 #include <stdio.h> 12 #include <netdb.h> 13 #include <sys/file.h> 14 #include <ndbm.h> 15 #include <ctype.h> 16 17 #define MAXALIASES 35 18 19 static struct hostent host; 20 static char *host_aliases[MAXALIASES]; 21 static char hostbuf[BUFSIZ+1]; 22 static char *host_addrs[2]; 23 24 /* 25 * The following is shared with gethostent.c 26 */ 27 extern char *_host_file; 28 DBM *_host_db = (DBM *)NULL; 29 int _host_stayopen; /* set by sethostent(), cleared by endhostent() */ 30 31 static struct hostent * 32 fetchhost(key) 33 datum key; 34 { 35 register char *cp, *tp, **ap; 36 int naliases; 37 38 if (key.dptr == 0) 39 return ((struct hostent *)NULL); 40 key = dbm_fetch(_host_db, key); 41 if (key.dptr == 0) 42 return ((struct hostent *)NULL); 43 cp = key.dptr; 44 tp = hostbuf; 45 host.h_name = tp; 46 while (*tp++ = *cp++) 47 ; 48 bcopy(cp, (char *)&naliases, sizeof(int)); cp += sizeof (int); 49 for (ap = host_aliases; naliases > 0; naliases--) { 50 *ap++ = tp; 51 while (*tp++ = *cp++) 52 ; 53 } 54 *ap = (char *)NULL; 55 host.h_aliases = host_aliases; 56 bcopy(cp, (char *)&host.h_addrtype, sizeof (int)); 57 cp += sizeof (int); 58 bcopy(cp, (char *)&host.h_length, sizeof (int)); 59 cp += sizeof (int); 60 host.h_addr_list = host_addrs; 61 host.h_addr = tp; 62 bcopy(cp, tp, host.h_length); 63 return (&host); 64 } 65 66 struct hostent * 67 gethostbyname(nam) 68 register char *nam; 69 { 70 register struct hostent *hp; 71 register char **cp; 72 datum key; 73 char lowname[128]; 74 register char *lp = lowname; 75 76 while (*nam) 77 if (isupper(*nam)) 78 *lp++ = tolower(*nam++); 79 else 80 *lp++ = *nam++; 81 *lp = '\0'; 82 83 if ((_host_db == (DBM *)NULL) 84 && ((_host_db = dbm_open(_host_file, O_RDONLY)) == (DBM *)NULL)) { 85 sethostent(_host_stayopen); 86 while (hp = gethostent()) { 87 if (strcmp(hp->h_name, lowname) == 0) 88 break; 89 for (cp = hp->h_aliases; cp != 0 && *cp != 0; cp++) 90 if (strcmp(*cp, lowname) == 0) 91 goto found; 92 } 93 found: 94 if (!_host_stayopen) 95 endhostent(); 96 return (hp); 97 } 98 key.dptr = lowname; 99 key.dsize = strlen(lowname); 100 hp = fetchhost(key); 101 if (!_host_stayopen) { 102 dbm_close(_host_db); 103 _host_db = (DBM *)NULL; 104 } 105 return (hp); 106 } 107 108 struct hostent * 109 gethostbyaddr(addr, length, type) 110 char *addr; 111 register int length; 112 register int type; 113 { 114 register struct hostent *hp; 115 datum key; 116 117 if ((_host_db == (DBM *)NULL) 118 && ((_host_db = dbm_open(_host_file, O_RDONLY)) == (DBM *)NULL)) { 119 sethostent(_host_stayopen); 120 while (hp = gethostent()) { 121 if (hp->h_addrtype == type && hp->h_length == length 122 && bcmp(hp->h_addr, addr, length) == 0) 123 break; 124 } 125 if (!_host_stayopen) 126 endhostent(); 127 return (hp); 128 } 129 key.dptr = addr; 130 key.dsize = length; 131 hp = fetchhost(key); 132 if (!_host_stayopen) { 133 dbm_close(_host_db); 134 _host_db = (DBM *)NULL; 135 } 136 return (hp); 137 } 138