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