1 /* gethostnamadr.c 4.3 83/12/21 */ 2 3 #include <stdio.h> 4 #include <netdb.h> 5 #include <sys/file.h> 6 #include <ndbm.h> 7 8 #define MAXALIASES 35 9 10 static char HOSTDB[] = "/etc/hosts"; 11 DBM *_host_db = (DBM *)NULL; 12 static datum curkey; 13 static struct hostent host; 14 static char *host_aliases[MAXALIASES]; 15 int _host_stayopen; /* set by sethostent(), cleared by endhostent() */ 16 17 static struct hostent * 18 fetchhost(key) 19 datum key; 20 { 21 register char *cp, **ap; 22 register int naliases; 23 24 curkey = key; 25 if (curkey.dptr == 0) 26 return ((struct hostent *)NULL); 27 key = dbmfetch(_host_db, curkey); 28 if (key.dptr == 0) 29 return ((struct hostent *)NULL); 30 cp = key.dptr; 31 host.h_name = cp; 32 while (*cp++) 33 ; 34 naliases = *(int *)cp; cp += sizeof (int); 35 for (ap = host_aliases; naliases > 0; naliases--) { 36 *ap++ = cp; 37 while (*cp++) 38 ; 39 } 40 *ap = (char *)NULL; 41 host.h_aliases = host_aliases; 42 host.h_addrtype = *(int *)cp; cp += sizeof (int); 43 host.h_length = *(int *)cp; cp += sizeof (int); 44 host.h_addr = cp; 45 return (&host); 46 } 47 48 struct hostent * 49 gethostbyname(nam) 50 char *nam; 51 { 52 datum key; 53 register struct hostent *hp; 54 55 if ((_host_db == (DBM *)NULL) 56 && ((_host_db = ndbmopen(HOSTDB, O_RDONLY)) == (DBM *)NULL)) 57 return ((struct hostent *)NULL); 58 key.dptr = nam; 59 key.dsize = strlen(nam); 60 hp = fetchhost(key); 61 if (!_host_stayopen) { 62 ndbmclose(_host_db); 63 _host_db = (DBM *)NULL; 64 } 65 return (hp); 66 } 67 68 struct hostent * 69 gethostbyaddr(addr, length) 70 char *addr; 71 int length; 72 { 73 datum key; 74 register struct hostent *hp; 75 76 if ((_host_db == (DBM *)NULL) 77 && ((_host_db = ndbmopen(HOSTDB, O_RDONLY)) == (DBM *)NULL)) 78 return ((struct hostent *)NULL); 79 key.dptr = addr; 80 key.dsize = length; 81 hp = fetchhost(key); 82 if (!_host_stayopen) { 83 ndbmclose(_host_db); 84 _host_db = (DBM *)NULL; 85 } 86 return (hp); 87 } 88