Remove bogus non-reentrant "temporary" implementation of gethostbyaddr_r()
[dragonfly.git] / lib / libc / net / gethostnamadr.c
1 /*-
2  * Copyright (c) 1994, Garrett Wollman
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD: src/lib/libc/net/gethostnamadr.c,v 1.15.2.2 2001/03/05 10:40:42 obrien Exp $
26  * $DragonFly: src/lib/libc/net/gethostnamadr.c,v 1.6 2007/12/29 22:55:29 matthias Exp $
27  */
28
29 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <netdb.h>
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <string.h>
37 #include <arpa/nameser.h>               /* XXX hack for _res */
38 #include <resolv.h>                     /* XXX hack for _res */
39
40 #define _PATH_HOSTCONF  "/etc/host.conf"
41
42 enum service_type {
43   SERVICE_NONE = 0,
44   SERVICE_BIND,
45   SERVICE_HOSTS,
46   SERVICE_NIS };
47 #define SERVICE_MAX     SERVICE_NIS
48
49 static struct {
50   const char *name;
51   enum service_type type;
52 } service_names[] = {
53   { "hosts", SERVICE_HOSTS },
54   { "/etc/hosts", SERVICE_HOSTS },
55   { "hosttable", SERVICE_HOSTS },
56   { "htable", SERVICE_HOSTS },
57   { "bind", SERVICE_BIND },
58   { "dns", SERVICE_BIND },
59   { "domain", SERVICE_BIND },
60   { "yp", SERVICE_NIS },
61   { "yellowpages", SERVICE_NIS },
62   { "nis", SERVICE_NIS },
63   { 0, SERVICE_NONE }
64 };
65
66 static enum service_type service_order[SERVICE_MAX + 1];
67 static int service_done = 0;
68
69 static enum service_type
70 get_service_name(const char *name) {
71         int i;
72         for(i = 0; service_names[i].type != SERVICE_NONE; i++) {
73                 if(!strcasecmp(name, service_names[i].name)) {
74                         return service_names[i].type;
75                 }
76         }
77         return SERVICE_NONE;
78 }
79
80 static void
81 init_services(void)
82 {
83         char *cp, *p, buf[BUFSIZ];
84         int cc = 0;
85         FILE *fd;
86
87         if ((fd = (FILE *)fopen(_PATH_HOSTCONF, "r")) == NULL) {
88                                 /* make some assumptions */
89                 service_order[0] = SERVICE_BIND;
90                 service_order[1] = SERVICE_HOSTS;
91                 service_order[2] = SERVICE_NONE;
92         } else {
93                 while (fgets(buf, BUFSIZ, fd) != NULL && cc < SERVICE_MAX) {
94                         if(buf[0] == '#')
95                                 continue;
96
97                         p = buf;
98                         while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0')
99                                 ;
100                         if (cp == NULL)
101                                 continue;
102                         do {
103                                 if (isalpha((unsigned char)cp[0])) {
104                                         service_order[cc] = get_service_name(cp);
105                                         if(service_order[cc] != SERVICE_NONE)
106                                                 cc++;
107                                 }
108                                 while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0')
109                                         ;
110                         } while(cp != NULL && cc < SERVICE_MAX);
111                 }
112                 service_order[cc] = SERVICE_NONE;
113                 fclose(fd);
114         }
115         service_done = 1;
116 }
117
118 struct hostent *
119 gethostbyname(const char *name)
120 {
121         struct hostent *hp;
122
123         if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
124                 h_errno = NETDB_INTERNAL;
125                 return (NULL);
126         }
127         if (_res.options & RES_USE_INET6) {             /* XXX */
128                 hp = gethostbyname2(name, AF_INET6);    /* XXX */
129                 if (hp)                                 /* XXX */
130                         return (hp);                    /* XXX */
131         }                                               /* XXX */
132         return (gethostbyname2(name, AF_INET));
133 }
134
135 struct hostent *
136 gethostbyname2(const char *name, int type)
137 {
138         struct hostent *hp = 0;
139         int nserv = 0;
140
141         if (!service_done)
142                 init_services();
143
144         while (!hp) {
145                 switch (service_order[nserv]) {
146                       case SERVICE_NONE:
147                         return NULL;
148                       case SERVICE_HOSTS:
149                         hp = _gethostbyhtname(name, type);
150                         break;
151                       case SERVICE_BIND:
152                         hp = _gethostbydnsname(name, type);
153                         break;
154                       case SERVICE_NIS:
155                         hp = _gethostbynisname(name, type);
156                         break;
157                 }
158                 nserv++;
159         }
160         return hp;
161 }
162
163 struct hostent *
164 gethostbyaddr(const char *addr, int len, int type)
165 {
166         struct hostent *hp = 0;
167         int nserv = 0;
168
169         if (!service_done)
170                 init_services();
171
172         while (!hp) {
173                 switch (service_order[nserv]) {
174                       case SERVICE_NONE:
175                         return 0;
176                       case SERVICE_HOSTS:
177                         hp = _gethostbyhtaddr(addr, len, type);
178                         break;
179                       case SERVICE_BIND:
180                         hp = _gethostbydnsaddr(addr, len, type);
181                         break;
182                       case SERVICE_NIS:
183                         hp = _gethostbynisaddr(addr, len, type);
184                         break;
185                 }
186                 nserv++;
187         }
188         return hp;
189 }
190
191 void
192 sethostent(int stayopen)
193 {
194         _sethosthtent(stayopen);
195         _sethostdnsent(stayopen);
196 }
197
198 void
199 endhostent(void)
200 {
201         _endhosthtent();
202         _endhostdnsent();
203 }