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