4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1998-1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid
[] = "Id: getnetent_r.c,v 1.6 2005/09/03 12:41:38 marka Exp";
22 #endif /* LIBC_SCCS and not lint */
24 #include <port_before.h>
25 #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
26 static int getnetent_r_not_required
= 0;
31 #include <sys/types.h>
32 #include <netinet/in.h>
34 #include <sys/param.h>
35 #include <port_after.h>
40 copy_netent(struct netent
*, struct netent
*, NET_R_COPY_ARGS
);
43 getnetbyname_r(const char *name
, struct netent
*nptr
, NET_R_ARGS
) {
44 struct netent
*ne
= getnetbyname(name
);
45 #ifdef NET_R_SETANSWER
48 if (ne
== NULL
|| (n
= copy_netent(ne
, nptr
, NET_R_COPY
)) != 0)
59 return (copy_netent(ne
, nptr
, NET_R_COPY
));
63 #ifndef GETNETBYADDR_ADDR_T
64 #define GETNETBYADDR_ADDR_T long
67 getnetbyaddr_r(GETNETBYADDR_ADDR_T addr
, int type
, struct netent
*nptr
, NET_R_ARGS
) {
68 struct netent
*ne
= getnetbyaddr(addr
, type
);
69 #ifdef NET_R_SETANSWER
72 if (ne
== NULL
|| (n
= copy_netent(ne
, nptr
, NET_R_COPY
)) != 0)
84 return (copy_netent(ne
, nptr
, NET_R_COPY
));
89 * These assume a single context is in operation per thread.
90 * If this is not the case we will need to call irs directly
91 * rather than through the base functions.
95 getnetent_r(struct netent
*nptr
, NET_R_ARGS
) {
96 struct netent
*ne
= getnetent();
97 #ifdef NET_R_SETANSWER
100 if (ne
== NULL
|| (n
= copy_netent(ne
, nptr
, NET_R_COPY
)) != 0)
112 return (copy_netent(ne
, nptr
, NET_R_COPY
));
117 #ifdef NET_R_ENT_ARGS
118 setnetent_r(int stay_open
, NET_R_ENT_ARGS
)
120 setnetent_r(int stay_open
)
123 #ifdef NET_R_ENT_ARGS
126 setnetent(stay_open
);
127 #ifdef NET_R_SET_RESULT
128 return (NET_R_SET_RESULT
);
133 #ifdef NET_R_ENT_ARGS
134 endnetent_r(NET_R_ENT_ARGS
)
139 #ifdef NET_R_ENT_ARGS
143 NET_R_END_RESULT(NET_R_OK
);
150 copy_netent(struct netent
*ne
, struct netent
*nptr
, NET_R_COPY_ARGS
) {
155 /* Find out the amount of space required to store the answer. */
156 numptr
= 1; /*%< NULL ptr */
157 len
= (char *)ALIGN(buf
) - buf
;
158 for (i
= 0; ne
->n_aliases
[i
]; i
++, numptr
++) {
159 len
+= strlen(ne
->n_aliases
[i
]) + 1;
161 len
+= strlen(ne
->n_name
) + 1;
162 len
+= numptr
* sizeof(char*);
164 if (len
> (int)buflen
) {
169 /* copy net value and type */
170 nptr
->n_addrtype
= ne
->n_addrtype
;
171 nptr
->n_net
= ne
->n_net
;
173 cp
= (char *)ALIGN(buf
) + numptr
* sizeof(char *);
175 /* copy official name */
176 n
= strlen(ne
->n_name
) + 1;
177 strcpy(cp
, ne
->n_name
);
182 nptr
->n_aliases
= (char **)ALIGN(buf
);
183 for (i
= 0 ; ne
->n_aliases
[i
]; i
++) {
184 n
= strlen(ne
->n_aliases
[i
]) + 1;
185 strcpy(cp
, ne
->n_aliases
[i
]);
186 nptr
->n_aliases
[i
] = cp
;
189 nptr
->n_aliases
[i
] = NULL
;
193 #else /* !NETENT_DATA */
195 copy_netent(struct netent
*ne
, struct netent
*nptr
, NET_R_COPY_ARGS
) {
199 /* copy net value and type */
200 nptr
->n_addrtype
= ne
->n_addrtype
;
201 nptr
->n_net
= ne
->n_net
;
203 /* copy official name */
205 eob
= ndptr
->line
+ sizeof(ndptr
->line
);
206 if ((n
= strlen(ne
->n_name
) + 1) < (eob
- cp
)) {
207 strcpy(cp
, ne
->n_name
);
216 nptr
->n_aliases
= ndptr
->net_aliases
;
217 while (ne
->n_aliases
[i
] && i
< (_MAXALIASES
-1)) {
218 if ((n
= strlen(ne
->n_aliases
[i
]) + 1) < (eob
- cp
)) {
219 strcpy(cp
, ne
->n_aliases
[i
]);
220 nptr
->n_aliases
[i
] = cp
;
227 nptr
->n_aliases
[i
] = NULL
;
231 #endif /* !NETENT_DATA */
232 #else /* NET_R_RETURN */
233 static int getnetent_r_unknown_system
= 0;
234 #endif /* NET_R_RETURN */
235 #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */