1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #if defined(LIBC_SCCS) && !defined(lint)
14 static char sccsid[] = "@(#)gethostnamadr.c	5.6 (Berkeley) 03/07/88";
15 #endif /* LIBC_SCCS and not lint */
16 
17 #include <stdio.h>
18 #include <netdb.h>
19 #include <sys/file.h>
20 #include <ndbm.h>
21 #include <ctype.h>
22 
23 #define	MAXALIASES	35
24 
25 static struct hostent host;
26 static char *host_aliases[MAXALIASES];
27 static char hostbuf[BUFSIZ+1];
28 static char *host_addrs[2];
29 
30 int h_errno;
31 
32 /*
33  * The following is shared with gethostent.c
34  */
35 extern	char *_host_file;
36 DBM	*_host_db = (DBM *)NULL;
37 int	_host_stayopen;	/* set by sethostent(), cleared by endhostent() */
38 
39 static struct hostent *
40 fetchhost(key)
41 	datum key;
42 {
43         register char *cp, *tp, **ap;
44 	int naliases;
45 
46         if (key.dptr == 0)
47                 return ((struct hostent *)NULL);
48 	key = dbm_fetch(_host_db, key);
49 	if (key.dptr == 0)
50                 return ((struct hostent *)NULL);
51         cp = key.dptr;
52 	tp = hostbuf;
53 	host.h_name = tp;
54 	while (*tp++ = *cp++)
55 		;
56 	bcopy(cp, (char *)&naliases, sizeof(int)); cp += sizeof (int);
57 	for (ap = host_aliases; naliases > 0; naliases--) {
58 		*ap++ = tp;
59 		while (*tp++ = *cp++)
60 			;
61 	}
62 	*ap = (char *)NULL;
63 	host.h_aliases = host_aliases;
64 	bcopy(cp, (char *)&host.h_addrtype, sizeof (int));
65 	cp += sizeof (int);
66 	bcopy(cp, (char *)&host.h_length, sizeof (int));
67 	cp += sizeof (int);
68 	host.h_addr_list = host_addrs;
69 	host.h_addr = tp;
70 	bcopy(cp, tp, host.h_length);
71         return (&host);
72 }
73 
74 struct hostent *
75 gethostbyname(nam)
76 	register char *nam;
77 {
78 	register struct hostent *hp;
79 	register char **cp;
80         datum key;
81 	char lowname[128];
82 	register char *lp = lowname;
83 
84 	while (*nam)
85 		if (isupper(*nam))
86 			*lp++ = tolower(*nam++);
87 		else
88 			*lp++ = *nam++;
89 	*lp = '\0';
90 
91 	if ((_host_db == (DBM *)NULL)
92 	  && ((_host_db = dbm_open(_host_file, O_RDONLY)) == (DBM *)NULL)) {
93 		sethostent(_host_stayopen);
94 		while (hp = gethostent()) {
95 			if (strcmp(hp->h_name, lowname) == 0)
96 				break;
97 			for (cp = hp->h_aliases; cp != 0 && *cp != 0; cp++)
98 				if (strcmp(*cp, lowname) == 0)
99 					goto found;
100 		}
101 	found:
102 		if (!_host_stayopen)
103 			endhostent();
104 		return (hp);
105 	}
106         key.dptr = lowname;
107         key.dsize = strlen(lowname);
108 	hp = fetchhost(key);
109 	if (!_host_stayopen) {
110 		dbm_close(_host_db);
111 		_host_db = (DBM *)NULL;
112 	}
113 	if ( hp == NULL)
114 		h_errno = HOST_NOT_FOUND;
115         return (hp);
116 }
117 
118 struct hostent *
119 gethostbyaddr(addr, length, type)
120 	char *addr;
121 	register int length;
122 	register int type;
123 {
124 	register struct hostent *hp;
125         datum key;
126 
127 	if ((_host_db == (DBM *)NULL)
128 	  && ((_host_db = dbm_open(_host_file, O_RDONLY)) == (DBM *)NULL)) {
129 		sethostent(_host_stayopen);
130 		while (hp = gethostent()) {
131 			if (hp->h_addrtype == type && hp->h_length == length
132 			    && bcmp(hp->h_addr, addr, length) == 0)
133 				break;
134 		}
135 		if (!_host_stayopen)
136 			endhostent();
137 		if ( hp == NULL)
138 			h_errno = HOST_NOT_FOUND;
139 		return (hp);
140 	}
141         key.dptr = addr;
142         key.dsize = length;
143 	hp = fetchhost(key);
144 	if (!_host_stayopen) {
145 		dbm_close(_host_db);
146 		_host_db = (DBM *)NULL;
147 	}
148 	if ( hp == NULL)
149 		h_errno = HOST_NOT_FOUND;
150         return (hp);
151 }
152