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.
25 #define ALIGN(p) (((uintptr_t)(p) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
28 struct hostent
*gethostbyname_r(const char *, struct hostent
*,
32 gethostbyaddr_r(const char *addr
, int len
, int type
,
33 struct hostent
*hptr
, char *buf
, int buflen
, int *h_errnop
);
36 static struct hostent
*
37 copy_hostent(struct hostent
*he
, struct hostent
*hptr
, char *buf
, int buflen
) {
43 /* Find out the amount of space required to store the answer. */
44 nptr
= 2; /*%< NULL ptrs */
45 len
= (char *)ALIGN(buf
) - buf
;
46 for (i
= 0; he
->h_addr_list
[i
]; i
++, nptr
++) {
49 for (i
= 0; he
->h_aliases
[i
]; i
++, nptr
++) {
50 len
+= strlen(he
->h_aliases
[i
]) + 1;
52 len
+= strlen(he
->h_name
) + 1;
53 len
+= nptr
* sizeof(char*);
60 /* copy address size and type */
61 hptr
->h_addrtype
= he
->h_addrtype
;
62 n
= hptr
->h_length
= he
->h_length
;
64 ptr
= (char **)ALIGN(buf
);
65 cp
= (char *)ALIGN(buf
) + nptr
* sizeof(char *);
67 /* copy address list */
68 hptr
->h_addr_list
= ptr
;
69 for (i
= 0; he
->h_addr_list
[i
]; i
++ , ptr
++) {
70 memcpy(cp
, he
->h_addr_list
[i
], n
);
71 hptr
->h_addr_list
[i
] = cp
;
74 hptr
->h_addr_list
[i
] = NULL
;
77 /* copy official name */
78 n
= strlen(he
->h_name
) + 1;
79 strcpy(cp
, he
->h_name
);
84 hptr
->h_aliases
= ptr
;
85 for (i
= 0 ; he
->h_aliases
[i
]; i
++) {
86 n
= strlen(he
->h_aliases
[i
]) + 1;
87 strcpy(cp
, he
->h_aliases
[i
]);
88 hptr
->h_aliases
[i
] = cp
;
91 hptr
->h_aliases
[i
] = NULL
;
98 gethostbyname_r(const char *name
, struct hostent
*hptr
,
99 char *buf
, int buflen
, int *h_errnop
)
101 struct hostent
*he
= gethostbyname(name
);
108 return copy_hostent(he
, hptr
, buf
, buflen
);
113 gethostbyaddr_r(const char *addr
, int len
, int type
,
114 struct hostent
*hptr
, char *buf
, int buflen
, int *h_errnop
)
116 struct hostent
*he
= gethostbyaddr(addr
, len
, type
);
123 return copy_hostent(he
, hptr
, buf
, buflen
);