1*5bbd2a12Schristos /* $NetBSD: getnetent_r.c,v 1.1.1.2 2012/09/09 16:07:57 christos Exp $ */
2b5677b36Schristos
3b5677b36Schristos /*
4b5677b36Schristos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5b5677b36Schristos * Copyright (c) 1998-1999 by Internet Software Consortium.
6b5677b36Schristos *
7b5677b36Schristos * Permission to use, copy, modify, and distribute this software for any
8b5677b36Schristos * purpose with or without fee is hereby granted, provided that the above
9b5677b36Schristos * copyright notice and this permission notice appear in all copies.
10b5677b36Schristos *
11b5677b36Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12b5677b36Schristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13b5677b36Schristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14b5677b36Schristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15b5677b36Schristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16b5677b36Schristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17b5677b36Schristos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18b5677b36Schristos */
19b5677b36Schristos
20b5677b36Schristos #if defined(LIBC_SCCS) && !defined(lint)
21b5677b36Schristos static const char rcsid[] = "Id: getnetent_r.c,v 1.6 2005/09/03 12:41:38 marka Exp ";
22b5677b36Schristos #endif /* LIBC_SCCS and not lint */
23b5677b36Schristos
24b5677b36Schristos #include <port_before.h>
25b5677b36Schristos #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
26b5677b36Schristos static int getnetent_r_not_required = 0;
27b5677b36Schristos #else
28b5677b36Schristos #include <errno.h>
29b5677b36Schristos #include <string.h>
30b5677b36Schristos #include <stdio.h>
31b5677b36Schristos #include <sys/types.h>
32b5677b36Schristos #include <netinet/in.h>
33b5677b36Schristos #include <netdb.h>
34b5677b36Schristos #include <sys/param.h>
35b5677b36Schristos #include <port_after.h>
36b5677b36Schristos
37b5677b36Schristos #ifdef NET_R_RETURN
38b5677b36Schristos
39b5677b36Schristos static NET_R_RETURN
40b5677b36Schristos copy_netent(struct netent *, struct netent *, NET_R_COPY_ARGS);
41b5677b36Schristos
42b5677b36Schristos NET_R_RETURN
getnetbyname_r(const char * name,struct netent * nptr,NET_R_ARGS)43b5677b36Schristos getnetbyname_r(const char *name, struct netent *nptr, NET_R_ARGS) {
44b5677b36Schristos struct netent *ne = getnetbyname(name);
45b5677b36Schristos #ifdef NET_R_SETANSWER
46b5677b36Schristos int n = 0;
47b5677b36Schristos
48b5677b36Schristos if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0)
49b5677b36Schristos *answerp = NULL;
50b5677b36Schristos else
51b5677b36Schristos *answerp = ne;
52b5677b36Schristos if (ne == NULL)
53b5677b36Schristos *h_errnop = h_errno;
54b5677b36Schristos return (n);
55b5677b36Schristos #else
56b5677b36Schristos if (ne == NULL)
57b5677b36Schristos return (NET_R_BAD);
58b5677b36Schristos
59b5677b36Schristos return (copy_netent(ne, nptr, NET_R_COPY));
60b5677b36Schristos #endif
61b5677b36Schristos }
62b5677b36Schristos
63b5677b36Schristos #ifndef GETNETBYADDR_ADDR_T
64b5677b36Schristos #define GETNETBYADDR_ADDR_T long
65b5677b36Schristos #endif
66b5677b36Schristos NET_R_RETURN
getnetbyaddr_r(GETNETBYADDR_ADDR_T addr,int type,struct netent * nptr,NET_R_ARGS)67b5677b36Schristos getnetbyaddr_r(GETNETBYADDR_ADDR_T addr, int type, struct netent *nptr, NET_R_ARGS) {
68b5677b36Schristos struct netent *ne = getnetbyaddr(addr, type);
69b5677b36Schristos #ifdef NET_R_SETANSWER
70b5677b36Schristos int n = 0;
71b5677b36Schristos
72b5677b36Schristos if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0)
73b5677b36Schristos *answerp = NULL;
74b5677b36Schristos else
75b5677b36Schristos *answerp = ne;
76b5677b36Schristos if (ne == NULL)
77b5677b36Schristos *h_errnop = h_errno;
78b5677b36Schristos return (n);
79b5677b36Schristos #else
80b5677b36Schristos
81b5677b36Schristos if (ne == NULL)
82b5677b36Schristos return (NET_R_BAD);
83b5677b36Schristos
84b5677b36Schristos return (copy_netent(ne, nptr, NET_R_COPY));
85b5677b36Schristos #endif
86b5677b36Schristos }
87b5677b36Schristos
88b5677b36Schristos /*%
89b5677b36Schristos * These assume a single context is in operation per thread.
90b5677b36Schristos * If this is not the case we will need to call irs directly
91b5677b36Schristos * rather than through the base functions.
92b5677b36Schristos */
93b5677b36Schristos
94b5677b36Schristos NET_R_RETURN
getnetent_r(struct netent * nptr,NET_R_ARGS)95b5677b36Schristos getnetent_r(struct netent *nptr, NET_R_ARGS) {
96b5677b36Schristos struct netent *ne = getnetent();
97b5677b36Schristos #ifdef NET_R_SETANSWER
98b5677b36Schristos int n = 0;
99b5677b36Schristos
100b5677b36Schristos if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0)
101b5677b36Schristos *answerp = NULL;
102b5677b36Schristos else
103b5677b36Schristos *answerp = ne;
104b5677b36Schristos if (ne == NULL)
105b5677b36Schristos *h_errnop = h_errno;
106b5677b36Schristos return (n);
107b5677b36Schristos #else
108b5677b36Schristos
109b5677b36Schristos if (ne == NULL)
110b5677b36Schristos return (NET_R_BAD);
111b5677b36Schristos
112b5677b36Schristos return (copy_netent(ne, nptr, NET_R_COPY));
113b5677b36Schristos #endif
114b5677b36Schristos }
115b5677b36Schristos
116b5677b36Schristos NET_R_SET_RETURN
117b5677b36Schristos #ifdef NET_R_ENT_ARGS
setnetent_r(int stay_open,NET_R_ENT_ARGS)118b5677b36Schristos setnetent_r(int stay_open, NET_R_ENT_ARGS)
119b5677b36Schristos #else
120b5677b36Schristos setnetent_r(int stay_open)
121b5677b36Schristos #endif
122b5677b36Schristos {
123b5677b36Schristos #ifdef NET_R_ENT_ARGS
124b5677b36Schristos UNUSED(ndptr);
125b5677b36Schristos #endif
126b5677b36Schristos setnetent(stay_open);
127b5677b36Schristos #ifdef NET_R_SET_RESULT
128b5677b36Schristos return (NET_R_SET_RESULT);
129b5677b36Schristos #endif
130b5677b36Schristos }
131b5677b36Schristos
132b5677b36Schristos NET_R_END_RETURN
133b5677b36Schristos #ifdef NET_R_ENT_ARGS
endnetent_r(NET_R_ENT_ARGS)134b5677b36Schristos endnetent_r(NET_R_ENT_ARGS)
135b5677b36Schristos #else
136b5677b36Schristos endnetent_r()
137b5677b36Schristos #endif
138b5677b36Schristos {
139b5677b36Schristos #ifdef NET_R_ENT_ARGS
140b5677b36Schristos UNUSED(ndptr);
141b5677b36Schristos #endif
142b5677b36Schristos endnetent();
143b5677b36Schristos NET_R_END_RESULT(NET_R_OK);
144b5677b36Schristos }
145b5677b36Schristos
146b5677b36Schristos /* Private */
147b5677b36Schristos
148b5677b36Schristos #ifndef NETENT_DATA
149b5677b36Schristos static NET_R_RETURN
copy_netent(struct netent * ne,struct netent * nptr,NET_R_COPY_ARGS)150b5677b36Schristos copy_netent(struct netent *ne, struct netent *nptr, NET_R_COPY_ARGS) {
151b5677b36Schristos char *cp;
152b5677b36Schristos int i, n;
153b5677b36Schristos int numptr, len;
154b5677b36Schristos
155b5677b36Schristos /* Find out the amount of space required to store the answer. */
156b5677b36Schristos numptr = 1; /*%< NULL ptr */
157b5677b36Schristos len = (char *)ALIGN(buf) - buf;
158b5677b36Schristos for (i = 0; ne->n_aliases[i]; i++, numptr++) {
159b5677b36Schristos len += strlen(ne->n_aliases[i]) + 1;
160b5677b36Schristos }
161b5677b36Schristos len += strlen(ne->n_name) + 1;
162b5677b36Schristos len += numptr * sizeof(char*);
163b5677b36Schristos
164b5677b36Schristos if (len > (int)buflen) {
165b5677b36Schristos errno = ERANGE;
166b5677b36Schristos return (NET_R_BAD);
167b5677b36Schristos }
168b5677b36Schristos
169b5677b36Schristos /* copy net value and type */
170b5677b36Schristos nptr->n_addrtype = ne->n_addrtype;
171b5677b36Schristos nptr->n_net = ne->n_net;
172b5677b36Schristos
173b5677b36Schristos cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
174b5677b36Schristos
175b5677b36Schristos /* copy official name */
176b5677b36Schristos n = strlen(ne->n_name) + 1;
177b5677b36Schristos strcpy(cp, ne->n_name);
178b5677b36Schristos nptr->n_name = cp;
179b5677b36Schristos cp += n;
180b5677b36Schristos
181b5677b36Schristos /* copy aliases */
182b5677b36Schristos nptr->n_aliases = (char **)ALIGN(buf);
183b5677b36Schristos for (i = 0 ; ne->n_aliases[i]; i++) {
184b5677b36Schristos n = strlen(ne->n_aliases[i]) + 1;
185b5677b36Schristos strcpy(cp, ne->n_aliases[i]);
186b5677b36Schristos nptr->n_aliases[i] = cp;
187b5677b36Schristos cp += n;
188b5677b36Schristos }
189b5677b36Schristos nptr->n_aliases[i] = NULL;
190b5677b36Schristos
191b5677b36Schristos return (NET_R_OK);
192b5677b36Schristos }
193b5677b36Schristos #else /* !NETENT_DATA */
194b5677b36Schristos static int
copy_netent(struct netent * ne,struct netent * nptr,NET_R_COPY_ARGS)195b5677b36Schristos copy_netent(struct netent *ne, struct netent *nptr, NET_R_COPY_ARGS) {
196b5677b36Schristos char *cp, *eob;
197b5677b36Schristos int i, n;
198b5677b36Schristos
199b5677b36Schristos /* copy net value and type */
200b5677b36Schristos nptr->n_addrtype = ne->n_addrtype;
201b5677b36Schristos nptr->n_net = ne->n_net;
202b5677b36Schristos
203b5677b36Schristos /* copy official name */
204b5677b36Schristos cp = ndptr->line;
205b5677b36Schristos eob = ndptr->line + sizeof(ndptr->line);
206b5677b36Schristos if ((n = strlen(ne->n_name) + 1) < (eob - cp)) {
207b5677b36Schristos strcpy(cp, ne->n_name);
208b5677b36Schristos nptr->n_name = cp;
209b5677b36Schristos cp += n;
210b5677b36Schristos } else {
211b5677b36Schristos return (-1);
212b5677b36Schristos }
213b5677b36Schristos
214b5677b36Schristos /* copy aliases */
215b5677b36Schristos i = 0;
216b5677b36Schristos nptr->n_aliases = ndptr->net_aliases;
217b5677b36Schristos while (ne->n_aliases[i] && i < (_MAXALIASES-1)) {
218b5677b36Schristos if ((n = strlen(ne->n_aliases[i]) + 1) < (eob - cp)) {
219b5677b36Schristos strcpy(cp, ne->n_aliases[i]);
220b5677b36Schristos nptr->n_aliases[i] = cp;
221b5677b36Schristos cp += n;
222b5677b36Schristos } else {
223b5677b36Schristos break;
224b5677b36Schristos }
225b5677b36Schristos i++;
226b5677b36Schristos }
227b5677b36Schristos nptr->n_aliases[i] = NULL;
228b5677b36Schristos
229b5677b36Schristos return (NET_R_OK);
230b5677b36Schristos }
231b5677b36Schristos #endif /* !NETENT_DATA */
232b5677b36Schristos #else /* NET_R_RETURN */
233b5677b36Schristos static int getnetent_r_unknown_system = 0;
234b5677b36Schristos #endif /* NET_R_RETURN */
235b5677b36Schristos #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */
236b5677b36Schristos /*! \file */
237