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