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 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid
[] = "$Id: gethostent_r.c,v 1.9 2005/09/03 12:41:37 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
22 #include <port_before.h>
23 #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
24 static int gethostent_r_not_required
= 0;
29 #include <sys/types.h>
30 #include <netinet/in.h>
32 #include <sys/param.h>
33 #include <port_after.h>
38 copy_hostent(struct hostent
*, struct hostent
*, HOST_R_COPY_ARGS
);
41 gethostbyname_r(const char *name
, struct hostent
*hptr
, HOST_R_ARGS
) {
42 struct hostent
*he
= gethostbyname(name
);
43 #ifdef HOST_R_SETANSWER
51 #ifdef HOST_R_SETANSWER
52 if (he
== NULL
|| (n
= copy_hostent(he
, hptr
, HOST_R_COPY
)) != 0)
62 return (copy_hostent(he
, hptr
, HOST_R_COPY
));
67 gethostbyaddr_r(const char *addr
, int len
, int type
,
68 struct hostent
*hptr
, HOST_R_ARGS
) {
69 struct hostent
*he
= gethostbyaddr(addr
, len
, type
);
70 #ifdef HOST_R_SETANSWER
78 #ifdef HOST_R_SETANSWER
79 if (he
== NULL
|| (n
= copy_hostent(he
, hptr
, HOST_R_COPY
)) != 0)
89 return (copy_hostent(he
, hptr
, HOST_R_COPY
));
94 * These assume a single context is in operation per thread.
95 * If this is not the case we will need to call irs directly
96 * rather than through the base functions.
100 gethostent_r(struct hostent
*hptr
, HOST_R_ARGS
) {
101 struct hostent
*he
= gethostent();
102 #ifdef HOST_R_SETANSWER
110 #ifdef HOST_R_SETANSWER
111 if (he
== NULL
|| (n
= copy_hostent(he
, hptr
, HOST_R_COPY
)) != 0)
121 return (copy_hostent(he
, hptr
, HOST_R_COPY
));
126 #ifdef HOST_R_ENT_ARGS
127 sethostent_r(int stay_open
, HOST_R_ENT_ARGS
)
129 sethostent_r(int stay_open
)
132 #ifdef HOST_R_ENT_ARGS
135 sethostent(stay_open
);
136 #ifdef HOST_R_SET_RESULT
137 return (HOST_R_SET_RESULT
);
142 #ifdef HOST_R_ENT_ARGS
143 endhostent_r(HOST_R_ENT_ARGS
)
148 #ifdef HOST_R_ENT_ARGS
152 HOST_R_END_RESULT(HOST_R_OK
);
159 copy_hostent(struct hostent
*he
, struct hostent
*hptr
, HOST_R_COPY_ARGS
) {
165 /* Find out the amount of space required to store the answer. */
166 nptr
= 2; /*%< NULL ptrs */
167 len
= (char *)ALIGN(buf
) - buf
;
168 for (i
= 0; he
->h_addr_list
[i
]; i
++, nptr
++) {
171 for (i
= 0; he
->h_aliases
[i
]; i
++, nptr
++) {
172 len
+= strlen(he
->h_aliases
[i
]) + 1;
174 len
+= strlen(he
->h_name
) + 1;
175 len
+= nptr
* sizeof(char*);
182 /* copy address size and type */
183 hptr
->h_addrtype
= he
->h_addrtype
;
184 n
= hptr
->h_length
= he
->h_length
;
186 ptr
= (char **)ALIGN(buf
);
187 cp
= (char *)ALIGN(buf
) + nptr
* sizeof(char *);
189 /* copy address list */
190 hptr
->h_addr_list
= ptr
;
191 for (i
= 0; he
->h_addr_list
[i
]; i
++ , ptr
++) {
192 memcpy(cp
, he
->h_addr_list
[i
], n
);
193 hptr
->h_addr_list
[i
] = cp
;
196 hptr
->h_addr_list
[i
] = NULL
;
199 /* copy official name */
200 n
= strlen(he
->h_name
) + 1;
201 strcpy(cp
, he
->h_name
);
206 hptr
->h_aliases
= ptr
;
207 for (i
= 0 ; he
->h_aliases
[i
]; i
++) {
208 n
= strlen(he
->h_aliases
[i
]) + 1;
209 strcpy(cp
, he
->h_aliases
[i
]);
210 hptr
->h_aliases
[i
] = cp
;
213 hptr
->h_aliases
[i
] = NULL
;
217 #else /* !HOSTENT_DATA */
219 copy_hostent(struct hostent
*he
, struct hostent
*hptr
, HOST_R_COPY_ARGS
) {
223 /* copy address size and type */
224 hptr
->h_addrtype
= he
->h_addrtype
;
225 n
= hptr
->h_length
= he
->h_length
;
227 /* copy up to first 35 addresses */
230 eob
= hdptr
->hostbuf
+ sizeof(hdptr
->hostbuf
);
231 hptr
->h_addr_list
= hdptr
->h_addr_ptrs
;
232 while (he
->h_addr_list
[i
] && i
< (_MAXADDRS
)) {
233 if (n
< (eob
- cp
)) {
234 memcpy(cp
, he
->h_addr_list
[i
], n
);
235 hptr
->h_addr_list
[i
] = cp
;
242 hptr
->h_addr_list
[i
] = NULL
;
244 /* copy official name */
245 if ((n
= strlen(he
->h_name
) + 1) < (eob
- cp
)) {
246 strcpy(cp
, he
->h_name
);
255 hptr
->h_aliases
= hdptr
->host_aliases
;
256 while (he
->h_aliases
[i
] && i
< (_MAXALIASES
-1)) {
257 if ((n
= strlen(he
->h_aliases
[i
]) + 1) < (eob
- cp
)) {
258 strcpy(cp
, he
->h_aliases
[i
]);
259 hptr
->h_aliases
[i
] = cp
;
266 hptr
->h_aliases
[i
] = NULL
;
270 #endif /* !HOSTENT_DATA */
271 #else /* HOST_R_RETURN */
272 static int gethostent_r_unknown_system
= 0;
273 #endif /* HOST_R_RETURN */
274 #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */