Merge from vendor branch GROFF:
[dragonfly.git] / lib / libc / net / getnetnamadr.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/getnetnamadr.c,v 1.12.2.1 2001/03/05 10:47:08 obrien Exp $
26  * $DragonFly: src/lib/libc/net/getnetnamadr.c,v 1.3 2004/10/25 19:38:01 drhodus 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
38 #ifndef _PATH_NETCONF
39 #define _PATH_NETCONF   "/etc/host.conf"
40 #endif
41
42 enum service_type {
43   SERVICE_NONE = 0,
44   SERVICE_BIND,
45   SERVICE_TABLE,
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_TABLE },
54   { "/etc/hosts", SERVICE_TABLE },
55   { "hosttable", SERVICE_TABLE },
56   { "htable", SERVICE_TABLE },
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()
82 {
83         char *cp, *p, buf[BUFSIZ];
84         int cc = 0;
85         FILE *fd;
86
87         if ((fd = (FILE *)fopen(_PATH_NETCONF, "r")) == NULL) {
88                                 /* make some assumptions */
89                 service_order[0] = SERVICE_TABLE;
90                 service_order[1] = SERVICE_NONE;
91         } else {
92                 while (fgets(buf, BUFSIZ, fd) != NULL && cc < SERVICE_MAX) {
93                         if(buf[0] == '#')
94                                 continue;
95
96                         p = buf;
97                         while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0')
98                                 ;
99                         if (cp == NULL)
100                                 continue;
101                         do {
102                                 if (isalpha((unsigned char)cp[0])) {
103                                         service_order[cc] = get_service_name(cp);
104                                         if(service_order[cc] != SERVICE_NONE)
105                                                 cc++;
106                                 }
107                                 while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0')
108                                         ;
109                         } while(cp != NULL && cc < SERVICE_MAX);
110                 }
111                 service_order[cc] = SERVICE_NONE;
112                 fclose(fd);
113         }
114         service_done = 1;
115 }
116
117 struct netent *
118 getnetbyname(const char *name)
119 {
120         struct netent *hp = 0;
121         int nserv = 0;
122
123         if (!service_done)
124                 init_services();
125
126         while (!hp) {
127                 switch (service_order[nserv]) {
128                       case SERVICE_NONE:
129                         return NULL;
130                       case SERVICE_TABLE:
131                         hp = _getnetbyhtname(name);
132                         break;
133                       case SERVICE_BIND:
134                         hp = _getnetbydnsname(name);
135                         break;
136                       case SERVICE_NIS:
137                         hp = _getnetbynisname(name);
138                         break;
139                 }
140                 nserv++;
141         }
142         return hp;
143 }
144
145 struct netent *
146 getnetbyaddr(addr, af)
147         u_long addr;
148         int af;
149 {
150         struct netent *hp = 0;
151         int nserv = 0;
152
153         if (!service_done)
154                 init_services();
155
156         while (!hp) {
157                 switch (service_order[nserv]) {
158                       case SERVICE_NONE:
159                         return 0;
160                       case SERVICE_TABLE:
161                         hp = _getnetbyhtaddr(addr, af);
162                         break;
163                       case SERVICE_BIND:
164                         hp = _getnetbydnsaddr(addr, af);
165                         break;
166                       case SERVICE_NIS:
167                         hp = _getnetbynisaddr(addr, af);
168                         break;
169                 }
170                 nserv++;
171         }
172         return hp;
173 }
174
175 void
176 setnetent(stayopen)
177         int stayopen;
178 {
179         _setnethtent(stayopen);
180         _setnetdnsent(stayopen);
181 }
182
183 void
184 endnetent()
185 {
186         _endnethtent();
187         _endnetdnsent();
188 }