2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1998-1999 by Internet Software Consortium.
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.
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.
18 #include <port_before.h>
19 #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
20 static int gethostent_r_not_required
= 0;
25 #include <sys/types.h>
26 #include <netinet/in.h>
28 #include <sys/param.h>
29 #include <port_after.h>
34 copy_hostent(struct hostent
*, struct hostent
*, HOST_R_COPY_ARGS
);
37 gethostbyname_r(const char *name
, struct hostent
*hptr
, HOST_R_ARGS
) {
38 struct hostent
*he
= gethostbyname(name
);
39 #ifdef HOST_R_SETANSWER
47 #ifdef HOST_R_SETANSWER
48 if (he
== NULL
|| (n
= copy_hostent(he
, hptr
, HOST_R_COPY
)) != 0)
58 return (copy_hostent(he
, hptr
, HOST_R_COPY
));
63 gethostbyaddr_r(const char *addr
, int len
, int type
,
64 struct hostent
*hptr
, HOST_R_ARGS
) {
65 struct hostent
*he
= gethostbyaddr(addr
, len
, type
);
66 #ifdef HOST_R_SETANSWER
74 #ifdef HOST_R_SETANSWER
75 if (he
== NULL
|| (n
= copy_hostent(he
, hptr
, HOST_R_COPY
)) != 0)
85 return (copy_hostent(he
, hptr
, HOST_R_COPY
));
90 * These assume a single context is in operation per thread.
91 * If this is not the case we will need to call irs directly
92 * rather than through the base functions.
96 gethostent_r(struct hostent
*hptr
, HOST_R_ARGS
) {
97 struct hostent
*he
= gethostent();
98 #ifdef HOST_R_SETANSWER
106 #ifdef HOST_R_SETANSWER
107 if (he
== NULL
|| (n
= copy_hostent(he
, hptr
, HOST_R_COPY
)) != 0)
117 return (copy_hostent(he
, hptr
, HOST_R_COPY
));
122 #ifdef HOST_R_ENT_ARGS
123 sethostent_r(int stay_open
, HOST_R_ENT_ARGS
)
125 sethostent_r(int stay_open
)
128 #ifdef HOST_R_ENT_ARGS
131 sethostent(stay_open
);
132 #ifdef HOST_R_SET_RESULT
133 return (HOST_R_SET_RESULT
);
138 #ifdef HOST_R_ENT_ARGS
139 endhostent_r(HOST_R_ENT_ARGS
)
144 #ifdef HOST_R_ENT_ARGS
148 HOST_R_END_RESULT(HOST_R_OK
);
155 copy_hostent(struct hostent
*he
, struct hostent
*hptr
, HOST_R_COPY_ARGS
) {
161 /* Find out the amount of space required to store the answer. */
162 nptr
= 2; /*%< NULL ptrs */
163 len
= (char *)ALIGN(buf
) - buf
;
164 for (i
= 0; he
->h_addr_list
[i
]; i
++, nptr
++) {
167 for (i
= 0; he
->h_aliases
[i
]; i
++, nptr
++) {
168 len
+= strlen(he
->h_aliases
[i
]) + 1;
170 len
+= strlen(he
->h_name
) + 1;
171 len
+= nptr
* sizeof(char*);
178 /* copy address size and type */
179 hptr
->h_addrtype
= he
->h_addrtype
;
180 n
= hptr
->h_length
= he
->h_length
;
182 ptr
= (char **)ALIGN(buf
);
183 cp
= (char *)ALIGN(buf
) + nptr
* sizeof(char *);
185 /* copy address list */
186 hptr
->h_addr_list
= ptr
;
187 for (i
= 0; he
->h_addr_list
[i
]; i
++ , ptr
++) {
188 memcpy(cp
, he
->h_addr_list
[i
], n
);
189 hptr
->h_addr_list
[i
] = cp
;
192 hptr
->h_addr_list
[i
] = NULL
;
195 /* copy official name */
196 n
= strlen(he
->h_name
) + 1;
197 strcpy(cp
, he
->h_name
);
202 hptr
->h_aliases
= ptr
;
203 for (i
= 0 ; he
->h_aliases
[i
]; i
++) {
204 n
= strlen(he
->h_aliases
[i
]) + 1;
205 strcpy(cp
, he
->h_aliases
[i
]);
206 hptr
->h_aliases
[i
] = cp
;
209 hptr
->h_aliases
[i
] = NULL
;
213 #else /* !HOSTENT_DATA */
215 copy_hostent(struct hostent
*he
, struct hostent
*hptr
, HOST_R_COPY_ARGS
) {
219 /* copy address size and type */
220 hptr
->h_addrtype
= he
->h_addrtype
;
221 n
= hptr
->h_length
= he
->h_length
;
223 /* copy up to first 35 addresses */
226 eob
= hdptr
->hostbuf
+ sizeof(hdptr
->hostbuf
);
227 hptr
->h_addr_list
= hdptr
->h_addr_ptrs
;
228 while (he
->h_addr_list
[i
] && i
< (_MAXADDRS
)) {
229 if (n
< (eob
- cp
)) {
230 memcpy(cp
, he
->h_addr_list
[i
], n
);
231 hptr
->h_addr_list
[i
] = cp
;
238 hptr
->h_addr_list
[i
] = NULL
;
240 /* copy official name */
241 if ((n
= strlen(he
->h_name
) + 1) < (eob
- cp
)) {
242 strcpy(cp
, he
->h_name
);
251 hptr
->h_aliases
= hdptr
->host_aliases
;
252 while (he
->h_aliases
[i
] && i
< (_MAXALIASES
-1)) {
253 if ((n
= strlen(he
->h_aliases
[i
]) + 1) < (eob
- cp
)) {
254 strcpy(cp
, he
->h_aliases
[i
]);
255 hptr
->h_aliases
[i
] = cp
;
262 hptr
->h_aliases
[i
] = NULL
;
266 #endif /* !HOSTENT_DATA */
267 #else /* HOST_R_RETURN */
268 static int gethostent_r_unknown_system
= 0;
269 #endif /* HOST_R_RETURN */
270 #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */