3 #include <sys/socket.h>
6 #include <netinet/in.h>
10 int gethostbyname2_r(const char *name
, int af
,
11 struct hostent
*h
, char *buf
, size_t buflen
,
12 struct hostent
**res
, int *err
)
14 struct addrinfo hint
= {
15 .ai_family
= af
==AF_INET6
? af
: AF_INET
,
16 .ai_flags
= AI_CANONNAME
18 struct addrinfo
*ai
, *p
;
26 i
= (uintptr_t)buf
& sizeof(char *)-1;
28 if (buflen
< sizeof(char *)-i
) return ERANGE
;
29 buf
+= sizeof(char *)-i
;
30 buflen
-= sizeof(char *)-i
;
33 switch (getaddrinfo(name
, 0, &hint
, &ai
)) {
35 *err
= HOST_NOT_FOUND
;
51 h
->h_length
= af
==AF_INET6
? 16 : 4;
53 canon
= ai
->ai_canonname
? ai
->ai_canonname
: name
;
54 need
= 4*sizeof(char *);
55 for (i
=0, p
=ai
; p
; i
++, p
=p
->ai_next
)
56 need
+= sizeof(char *) + h
->h_length
;
57 need
+= strlen(name
)+1;
58 need
+= strlen(canon
)+1;
65 h
->h_aliases
= (void *)buf
;
66 buf
+= 3*sizeof(char *);
67 h
->h_addr_list
= (void *)buf
;
68 buf
+= (i
+1)*sizeof(char *);
70 h
->h_name
= h
->h_aliases
[0] = buf
;
71 strcpy(h
->h_name
, canon
);
72 buf
+= strlen(h
->h_name
)+1;
74 if (strcmp(h
->h_name
, name
)) {
75 h
->h_aliases
[1] = buf
;
76 strcpy(h
->h_aliases
[1], name
);
77 buf
+= strlen(h
->h_aliases
[1])+1;
78 } else h
->h_aliases
[1] = 0;
82 for (i
=0, p
=ai
; p
; i
++, p
=p
->ai_next
) {
83 h
->h_addr_list
[i
] = (void *)buf
;
85 memcpy(h
->h_addr_list
[i
],
86 &((struct sockaddr_in
*)p
->ai_addr
)->sin_addr
,
89 h
->h_addr_list
[i
] = 0;