]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libc/net/getnetbyht.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libc / net / getnetbyht.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
31  *      Dep. Matematica Universidade de Coimbra, Portugal, Europe
32  *
33  * Permission to use, copy, modify, and distribute this software for any
34  * purpose with or without fee is hereby granted, provided that the above
35  * copyright notice and this permission notice appear in all copies.
36  *
37  * from getnetent.c     1.1 (Coimbra) 93/06/02
38  */
39
40 #if defined(LIBC_SCCS) && !defined(lint)
41 static char sccsid[] = "@(#)getnetent.c 8.1 (Berkeley) 6/4/93";
42 static char orig_rcsid[] = "From: Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp";
43 #endif /* LIBC_SCCS and not lint */
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
51 #include <arpa/nameser.h>
52 #include <netdb.h>
53 #include <resolv.h>
54 #include <stdio.h>
55 #include <string.h>
56 #include <stdarg.h>
57 #include <nsswitch.h>
58 #include "netdb_private.h"
59
60 void
61 _setnethtent(int f, struct netent_data *ned)
62 {
63
64         if (ned->netf == NULL)
65                 ned->netf = fopen(_PATH_NETWORKS, "r");
66         else
67                 rewind(ned->netf);
68         ned->stayopen |= f;
69 }
70
71 void
72 _endnethtent(struct netent_data *ned)
73 {
74
75         if (ned->netf) {
76                 fclose(ned->netf);
77                 ned->netf = NULL;
78         }
79         ned->stayopen = 0;
80 }
81
82 static int
83 getnetent_p(struct netent *ne, struct netent_data *ned)
84 {
85         char *p, *bp, *ep;
86         char *cp, **q;
87         int len;
88         char line[BUFSIZ + 1];
89
90         if (ned->netf == NULL &&
91             (ned->netf = fopen(_PATH_NETWORKS, "r")) == NULL)
92                 return (-1);
93 again:
94         p = fgets(line, sizeof line, ned->netf);
95         if (p == NULL)
96                 return (-1);
97         if (*p == '#')
98                 goto again;
99         cp = strpbrk(p, "#\n");
100         if (cp != NULL)
101                 *cp = '\0';
102         bp = ned->netbuf;
103         ep = ned->netbuf + sizeof ned->netbuf;
104         ne->n_name = bp;
105         cp = strpbrk(p, " \t");
106         if (cp == NULL)
107                 goto again;
108         *cp++ = '\0';
109         len = strlen(p) + 1;
110         if (ep - bp < len) {
111                 RES_SET_H_ERRNO(__res_state(), NO_RECOVERY);
112                 return (-1);
113         }
114         strlcpy(bp, p, ep - bp);
115         bp += len;
116         while (*cp == ' ' || *cp == '\t')
117                 cp++;
118         p = strpbrk(cp, " \t");
119         if (p != NULL)
120                 *p++ = '\0';
121         ne->n_net = inet_network(cp);
122         ne->n_addrtype = AF_INET;
123         q = ne->n_aliases = ned->net_aliases;
124         if (p != NULL) {
125                 cp = p;
126                 while (cp && *cp) {
127                         if (*cp == ' ' || *cp == '\t') {
128                                 cp++;
129                                 continue;
130                         }
131                         if (q >= &ned->net_aliases[_MAXALIASES - 1])
132                                 break;
133                         p = strpbrk(cp, " \t");
134                         if (p != NULL)
135                                 *p++ = '\0';
136                         len = strlen(cp) + 1;
137                         if (ep - bp < len)
138                                 break;
139                         strlcpy(bp, cp, ep - bp);
140                         *q++ = bp;
141                         bp += len;
142                         cp = p;
143                 }
144         }
145         *q = NULL;
146         return (0);
147 }
148
149 int
150 getnetent_r(struct netent *nptr, char *buffer, size_t buflen,
151     struct netent **result, int *h_errnop)
152 {
153         struct netent_data *ned;
154         struct netent ne;
155         res_state statp;
156
157         statp = __res_state();
158         if ((ned = __netent_data_init()) == NULL) {
159                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
160                 *h_errnop = statp->res_h_errno;
161                 return (-1);
162         }
163         if (getnetent_p(&ne, ned) != 0)
164                 return (-1);
165         if (__copy_netent(&ne, nptr, buffer, buflen) != 0)
166                 return (-1);
167         *result = nptr;
168         return (0);
169 }
170
171 struct netent *
172 getnetent(void)
173 {
174         struct netdata *nd;
175         struct netent *rval;
176         int ret_h_errno;
177
178         if ((nd = __netdata_init()) == NULL)
179                 return (NULL);
180         if (getnetent_r(&nd->net, nd->data, sizeof(nd->data), &rval,
181             &ret_h_errno) != 0)
182                 return (NULL);
183         return (rval);
184 }
185
186 int
187 _ht_getnetbyname(void *rval, void *cb_data, va_list ap)
188 {
189         const char *name;
190         char *buffer;
191         size_t buflen;
192         int *errnop, *h_errnop;
193         struct netent *nptr, ne;
194         struct netent_data *ned;
195         char **cp;
196         res_state statp;
197         int error;
198
199         name = va_arg(ap, const char *);
200         nptr = va_arg(ap, struct netent *);
201         buffer = va_arg(ap, char *);
202         buflen = va_arg(ap, size_t);
203         errnop = va_arg(ap, int *);
204         h_errnop = va_arg(ap, int *);
205
206         statp = __res_state();
207         if ((ned = __netent_data_init()) == NULL) {
208                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
209                 *h_errnop = statp->res_h_errno;
210                 return (NS_UNAVAIL);
211         }
212
213         _setnethtent(ned->stayopen, ned);
214         while ((error = getnetent_p(&ne, ned)) == 0) {
215                 if (strcasecmp(ne.n_name, name) == 0)
216                         break;
217                 for (cp = ne.n_aliases; *cp != 0; cp++)
218                         if (strcasecmp(*cp, name) == 0)
219                                 goto found;
220         }
221 found:
222         if (!ned->stayopen)
223                 _endnethtent(ned);
224         if (error != 0) {
225                 *h_errnop = statp->res_h_errno;
226                 return (NS_NOTFOUND);
227         }
228         if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
229                 *h_errnop = statp->res_h_errno;
230                 return (NS_NOTFOUND);
231         }
232         *((struct netent **)rval) = nptr;
233         return (NS_SUCCESS);
234 }
235
236 int
237 _ht_getnetbyaddr(void *rval, void *cb_data, va_list ap)
238 {
239         uint32_t net;
240         int type;
241         char *buffer;
242         size_t buflen;
243         int *errnop, *h_errnop;
244         struct netent *nptr, ne;
245         struct netent_data *ned;
246         res_state statp;
247         int error;
248
249         net = va_arg(ap, uint32_t);
250         type = va_arg(ap, int);
251         nptr = va_arg(ap, struct netent *);
252         buffer = va_arg(ap, char *);
253         buflen = va_arg(ap, size_t);
254         errnop = va_arg(ap, int *);
255         h_errnop = va_arg(ap, int *);
256
257         statp = __res_state();
258         if ((ned = __netent_data_init()) == NULL) {
259                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
260                 *h_errnop = statp->res_h_errno;
261                 return (NS_UNAVAIL);
262         }
263
264         _setnethtent(ned->stayopen, ned);
265         while ((error = getnetent_p(&ne, ned)) == 0)
266                 if (ne.n_addrtype == type && ne.n_net == net)
267                         break;
268         if (!ned->stayopen)
269                 _endnethtent(ned);
270         if (error != 0) {
271                 *h_errnop = statp->res_h_errno;
272                 return (NS_NOTFOUND);
273         }
274         if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
275                 *h_errnop = statp->res_h_errno;
276                 return (NS_NOTFOUND);
277         }
278         *((struct netent **)rval) = nptr;
279         return (NS_SUCCESS);
280 }