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