Sweep-fix comparing pointers with 0 (and assigning 0 to pointers).
[dragonfly.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  * @(#)getnetent.c      8.1 (Berkeley) 6/4/93
30  * From: Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp
31  * $FreeBSD: src/lib/libc/net/getnetbyht.c,v 1.19 2007/01/09 00:28:02 imp Exp $
32  */
33
34 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
35  *      Dep. Matematica Universidade de Coimbra, Portugal, Europe
36  *
37  * Permission to use, copy, modify, and distribute this software for any
38  * purpose with or without fee is hereby granted, provided that the above
39  * copyright notice and this permission notice appear in all copies.
40  *
41  * from getnetent.c     1.1 (Coimbra) 93/06/02
42  */
43
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <arpa/nameser.h>
49 #include <netdb.h>
50 #include <resolv.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <stdarg.h>
54 #include <nsswitch.h>
55 #include "netdb_private.h"
56
57 void
58 _setnethtent(int f, struct netent_data *ned)
59 {
60
61         if (ned->netf == NULL)
62                 ned->netf = fopen(_PATH_NETWORKS, "r");
63         else
64                 rewind(ned->netf);
65         ned->stayopen |= f;
66 }
67
68 void
69 _endnethtent(struct netent_data *ned)
70 {
71
72         if (ned->netf) {
73                 fclose(ned->netf);
74                 ned->netf = NULL;
75         }
76         ned->stayopen = 0;
77 }
78
79 static int
80 getnetent_p(struct netent *ne, struct netent_data *ned)
81 {
82         char *p, *bp, *ep;
83         char *cp, **q;
84         int len;
85         char line[BUFSIZ + 1];
86
87         if (ned->netf == NULL &&
88             (ned->netf = fopen(_PATH_NETWORKS, "r")) == NULL)
89                 return (-1);
90 again:
91         p = fgets(line, sizeof line, ned->netf);
92         if (p == NULL)
93                 return (-1);
94         if (*p == '#')
95                 goto again;
96         cp = strpbrk(p, "#\n");
97         if (cp != NULL)
98                 *cp = '\0';
99         bp = ned->netbuf;
100         ep = ned->netbuf + sizeof ned->netbuf;
101         ne->n_name = bp;
102         cp = strpbrk(p, " \t");
103         if (cp == NULL)
104                 goto again;
105         *cp++ = '\0';
106         len = strlen(p) + 1;
107         if (ep - bp < len) {
108                 RES_SET_H_ERRNO(__res_state(), NO_RECOVERY);
109                 return (-1);
110         }
111         strlcpy(bp, p, ep - bp);
112         bp += len;
113         while (*cp == ' ' || *cp == '\t')
114                 cp++;
115         p = strpbrk(cp, " \t");
116         if (p != NULL)
117                 *p++ = '\0';
118         ne->n_net = inet_network(cp);
119         ne->n_addrtype = AF_INET;
120         q = ne->n_aliases = ned->net_aliases;
121         if (p != NULL) {
122                 cp = p;
123                 while (cp && *cp) {
124                         if (*cp == ' ' || *cp == '\t') {
125                                 cp++;
126                                 continue;
127                         }
128                         if (q >= &ned->net_aliases[_MAXALIASES - 1])
129                                 break;
130                         p = strpbrk(cp, " \t");
131                         if (p != NULL)
132                                 *p++ = '\0';
133                         len = strlen(cp) + 1;
134                         if (ep - bp < len)
135                                 break;
136                         strlcpy(bp, cp, ep - bp);
137                         *q++ = bp;
138                         bp += len;
139                         cp = p;
140                 }
141         }
142         *q = NULL;
143         return (0);
144 }
145
146 int
147 getnetent_r(struct netent *nptr, char *buffer, size_t buflen,
148             struct netent **result, int *h_errnop)
149 {
150         struct netent_data *ned;
151         struct netent ne;
152         res_state statp;
153
154         statp = __res_state();
155         if ((ned = __netent_data_init()) == NULL) {
156                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
157                 *h_errnop = statp->res_h_errno;
158                 return (-1);
159         }
160         if (getnetent_p(&ne, ned) != 0)
161                 return (-1);
162         if (__copy_netent(&ne, nptr, buffer, buflen) != 0)
163                 return (-1);
164         *result = nptr;
165         return (0);
166 }
167
168 struct netent *
169 getnetent(void)
170 {
171         struct netdata *nd;
172         struct netent *rval;
173         int ret_h_errno;
174
175         if ((nd = __netdata_init()) == NULL)
176                 return (NULL);
177         if (getnetent_r(&nd->net, nd->data, sizeof(nd->data), &rval,
178             &ret_h_errno) != 0)
179                 return (NULL);
180         return (rval);
181 }
182
183 int
184 _ht_getnetbyname(void *rval, void *cb_data, va_list ap)
185 {
186         const char *name;
187         char *buffer;
188         size_t buflen;
189         int *errnop, *h_errnop;
190         struct netent *nptr, ne;
191         struct netent_data *ned;
192         char **cp;
193         res_state statp;
194         int error;
195
196         name = va_arg(ap, const char *);
197         nptr = va_arg(ap, struct netent *);
198         buffer = va_arg(ap, char *);
199         buflen = va_arg(ap, size_t);
200         errnop = va_arg(ap, int *);
201         h_errnop = va_arg(ap, int *);
202
203         statp = __res_state();
204         if ((ned = __netent_data_init()) == NULL) {
205                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
206                 *h_errnop = statp->res_h_errno;
207                 return (NS_UNAVAIL);
208         }
209
210         _setnethtent(ned->stayopen, ned);
211         while ((error = getnetent_p(&ne, ned)) == 0) {
212                 if (strcasecmp(ne.n_name, name) == 0)
213                         break;
214                 for (cp = ne.n_aliases; *cp != NULL; cp++)
215                         if (strcasecmp(*cp, name) == 0)
216                                 goto found;
217         }
218 found:
219         if (!ned->stayopen)
220                 _endnethtent(ned);
221         if (error != 0) {
222                 *h_errnop = statp->res_h_errno;
223                 return (NS_NOTFOUND);
224         }
225         if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
226                 *h_errnop = statp->res_h_errno;
227                 return (NS_NOTFOUND);
228         }
229         *((struct netent **)rval) = nptr;
230         return (NS_SUCCESS);
231 }
232
233 int
234 _ht_getnetbyaddr(void *rval, void *cb_data, va_list ap)
235 {
236         uint32_t net;
237         int type;
238         char *buffer;
239         size_t buflen;
240         int *errnop, *h_errnop;
241         struct netent *nptr, ne;
242         struct netent_data *ned;
243         res_state statp;
244         int error;
245
246         net = va_arg(ap, uint32_t);
247         type = va_arg(ap, int);
248         nptr = va_arg(ap, struct netent *);
249         buffer = va_arg(ap, char *);
250         buflen = va_arg(ap, size_t);
251         errnop = va_arg(ap, int *);
252         h_errnop = va_arg(ap, int *);
253
254         statp = __res_state();
255         if ((ned = __netent_data_init()) == NULL) {
256                 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
257                 *h_errnop = statp->res_h_errno;
258                 return (NS_UNAVAIL);
259         }
260
261         _setnethtent(ned->stayopen, ned);
262         while ((error = getnetent_p(&ne, ned)) == 0)
263                 if (ne.n_addrtype == type && ne.n_net == net)
264                         break;
265         if (!ned->stayopen)
266                 _endnethtent(ned);
267         if (error != 0) {
268                 *h_errnop = statp->res_h_errno;
269                 return (NS_NOTFOUND);
270         }
271         if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
272                 *h_errnop = statp->res_h_errno;
273                 return (NS_NOTFOUND);
274         }
275         *((struct netent **)rval) = nptr;
276         return (NS_SUCCESS);
277 }