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