Remove unneeded .Pp before .Rs in various manual pages.
[dragonfly.git] / lib / libc / net / getnetbyht.c
blob541e54c45a4fae9e524cceff71c5778cab1f4f2c
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
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 * $DragonFly: src/lib/libc/net/getnetbyht.c,v 1.5 2005/11/13 02:04:47 swildner Exp $
35 /* Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro
36 * Dep. Matematica Universidade de Coimbra, Portugal, Europe
38 * Permission to use, copy, modify, and distribute this software for any
39 * purpose with or without fee is hereby granted, provided that the above
40 * copyright notice and this permission notice appear in all copies.
42 * from getnetent.c 1.1 (Coimbra) 93/06/02
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <arpa/nameser.h>
50 #include <netdb.h>
51 #include <resolv.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <stdarg.h>
55 #include <nsswitch.h>
56 #include "netdb_private.h"
58 void
59 _setnethtent(int f, struct netent_data *ned)
62 if (ned->netf == NULL)
63 ned->netf = fopen(_PATH_NETWORKS, "r");
64 else
65 rewind(ned->netf);
66 ned->stayopen |= f;
69 void
70 _endnethtent(struct netent_data *ned)
73 if (ned->netf) {
74 fclose(ned->netf);
75 ned->netf = NULL;
77 ned->stayopen = 0;
80 static int
81 getnetent_p(struct netent *ne, struct netent_data *ned)
83 char *p, *bp, *ep;
84 char *cp, **q;
85 int len;
86 char line[BUFSIZ + 1];
88 if (ned->netf == NULL &&
89 (ned->netf = fopen(_PATH_NETWORKS, "r")) == NULL)
90 return (-1);
91 again:
92 p = fgets(line, sizeof line, ned->netf);
93 if (p == NULL)
94 return (-1);
95 if (*p == '#')
96 goto again;
97 cp = strpbrk(p, "#\n");
98 if (cp != NULL)
99 *cp = '\0';
100 bp = ned->netbuf;
101 ep = ned->netbuf + sizeof ned->netbuf;
102 ne->n_name = bp;
103 cp = strpbrk(p, " \t");
104 if (cp == NULL)
105 goto again;
106 *cp++ = '\0';
107 len = strlen(p) + 1;
108 if (ep - bp < len) {
109 RES_SET_H_ERRNO(__res_state(), NO_RECOVERY);
110 return (-1);
112 strlcpy(bp, p, ep - bp);
113 bp += len;
114 while (*cp == ' ' || *cp == '\t')
115 cp++;
116 p = strpbrk(cp, " \t");
117 if (p != NULL)
118 *p++ = '\0';
119 ne->n_net = inet_network(cp);
120 ne->n_addrtype = AF_INET;
121 q = ne->n_aliases = ned->net_aliases;
122 if (p != NULL) {
123 cp = p;
124 while (cp && *cp) {
125 if (*cp == ' ' || *cp == '\t') {
126 cp++;
127 continue;
129 if (q >= &ned->net_aliases[_MAXALIASES - 1])
130 break;
131 p = strpbrk(cp, " \t");
132 if (p != NULL)
133 *p++ = '\0';
134 len = strlen(cp) + 1;
135 if (ep - bp < len)
136 break;
137 strlcpy(bp, cp, ep - bp);
138 *q++ = bp;
139 bp += len;
140 cp = p;
143 *q = NULL;
144 return (0);
148 getnetent_r(struct netent *nptr, char *buffer, size_t buflen,
149 struct netent **result, int *h_errnop)
151 struct netent_data *ned;
152 struct netent ne;
153 res_state statp;
155 statp = __res_state();
156 if ((ned = __netent_data_init()) == NULL) {
157 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
158 *h_errnop = statp->res_h_errno;
159 return (-1);
161 if (getnetent_p(&ne, ned) != 0)
162 return (-1);
163 if (__copy_netent(&ne, nptr, buffer, buflen) != 0)
164 return (-1);
165 *result = nptr;
166 return (0);
169 struct netent *
170 getnetent(void)
172 struct netdata *nd;
173 struct netent *rval;
174 int ret_h_errno;
176 if ((nd = __netdata_init()) == NULL)
177 return (NULL);
178 if (getnetent_r(&nd->net, nd->data, sizeof(nd->data), &rval,
179 &ret_h_errno) != 0)
180 return (NULL);
181 return (rval);
185 _ht_getnetbyname(void *rval, void *cb_data, va_list ap)
187 const char *name;
188 char *buffer;
189 size_t buflen;
190 int *errnop, *h_errnop;
191 struct netent *nptr, ne;
192 struct netent_data *ned;
193 char **cp;
194 res_state statp;
195 int error;
197 name = va_arg(ap, const char *);
198 nptr = va_arg(ap, struct netent *);
199 buffer = va_arg(ap, char *);
200 buflen = va_arg(ap, size_t);
201 errnop = va_arg(ap, int *);
202 h_errnop = va_arg(ap, int *);
204 statp = __res_state();
205 if ((ned = __netent_data_init()) == NULL) {
206 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
207 *h_errnop = statp->res_h_errno;
208 return (NS_UNAVAIL);
211 _setnethtent(ned->stayopen, ned);
212 while ((error = getnetent_p(&ne, ned)) == 0) {
213 if (strcasecmp(ne.n_name, name) == 0)
214 break;
215 for (cp = ne.n_aliases; *cp != 0; cp++)
216 if (strcasecmp(*cp, name) == 0)
217 goto found;
219 found:
220 if (!ned->stayopen)
221 _endnethtent(ned);
222 if (error != 0) {
223 *h_errnop = statp->res_h_errno;
224 return (NS_NOTFOUND);
226 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
227 *h_errnop = statp->res_h_errno;
228 return (NS_NOTFOUND);
230 *((struct netent **)rval) = nptr;
231 return (NS_SUCCESS);
235 _ht_getnetbyaddr(void *rval, void *cb_data, va_list ap)
237 uint32_t net;
238 int type;
239 char *buffer;
240 size_t buflen;
241 int *errnop, *h_errnop;
242 struct netent *nptr, ne;
243 struct netent_data *ned;
244 res_state statp;
245 int error;
247 net = va_arg(ap, uint32_t);
248 type = va_arg(ap, int);
249 nptr = va_arg(ap, struct netent *);
250 buffer = va_arg(ap, char *);
251 buflen = va_arg(ap, size_t);
252 errnop = va_arg(ap, int *);
253 h_errnop = va_arg(ap, int *);
255 statp = __res_state();
256 if ((ned = __netent_data_init()) == NULL) {
257 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
258 *h_errnop = statp->res_h_errno;
259 return (NS_UNAVAIL);
262 _setnethtent(ned->stayopen, ned);
263 while ((error = getnetent_p(&ne, ned)) == 0)
264 if (ne.n_addrtype == type && ne.n_net == net)
265 break;
266 if (!ned->stayopen)
267 _endnethtent(ned);
268 if (error != 0) {
269 *h_errnop = statp->res_h_errno;
270 return (NS_NOTFOUND);
272 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) {
273 *h_errnop = statp->res_h_errno;
274 return (NS_NOTFOUND);
276 *((struct netent **)rval) = nptr;
277 return (NS_SUCCESS);