1 /* Get address information (partial implementation).
2 Copyright (C) 1997, 2001-2002, 2004-2023 Free Software Foundation, Inc.
3 Contributed by Simon Josefsson <simon@josefsson.org>.
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
19 optimizes away the sa == NULL test below. */
20 #define _GL_ARG_NONNULL(params)
27 # include <netinet/in.h>
31 #include <arpa/inet.h>
36 /* Get memcpy, strdup. */
43 #define _(String) gettext (String)
44 #define N_(String) String
46 /* BeOS has AF_INET, but not PF_INET. */
48 # define PF_INET AF_INET
50 /* BeOS also lacks PF_UNSPEC. */
57 /* Override with cdecl calling convention. */
60 getaddrinfo (const char *restrict nodename
,
61 const char *restrict servname
,
62 const struct addrinfo
*restrict hints
,
63 struct addrinfo
**restrict res
)
66 return getaddrinfo (nodename
, servname
, hints
, res
);
70 freeaddrinfo (struct addrinfo
*ai
)
78 # if defined _WIN32 && !defined __CYGWIN__
79 # define WINDOWS_NATIVE
82 /* gl_sockets_startup */
85 # ifdef WINDOWS_NATIVE
87 /* Don't assume that UNICODE is not defined. */
88 # undef GetModuleHandle
89 # define GetModuleHandle GetModuleHandleA
91 # if !(_WIN32_WINNT >= _WIN32_WINNT_WINXP)
93 /* Avoid warnings from gcc -Wcast-function-type. */
94 # define GetProcAddress \
95 (void *) GetProcAddress
97 typedef int (WSAAPI
*getaddrinfo_func
) (const char*, const char*,
98 const struct addrinfo
*,
100 typedef void (WSAAPI
*freeaddrinfo_func
) (struct addrinfo
*);
101 typedef int (WSAAPI
*getnameinfo_func
) (const struct sockaddr
*,
102 socklen_t
, char*, DWORD
,
105 static getaddrinfo_func getaddrinfo_ptr
= NULL
;
106 static freeaddrinfo_func freeaddrinfo_ptr
= NULL
;
107 static getnameinfo_func getnameinfo_ptr
= NULL
;
116 return getaddrinfo_ptr
? 1 : 0;
120 h
= GetModuleHandle ("ws2_32.dll");
124 getaddrinfo_ptr
= (getaddrinfo_func
) GetProcAddress (h
, "getaddrinfo");
125 freeaddrinfo_ptr
= (freeaddrinfo_func
) GetProcAddress (h
, "freeaddrinfo");
126 getnameinfo_ptr
= (getnameinfo_func
) GetProcAddress (h
, "getnameinfo");
129 /* If either is missing, something is odd. */
130 if (!getaddrinfo_ptr
|| !freeaddrinfo_ptr
|| !getnameinfo_ptr
)
132 getaddrinfo_ptr
= NULL
;
133 freeaddrinfo_ptr
= NULL
;
134 getnameinfo_ptr
= NULL
;
138 gl_sockets_startup (SOCKETS_1_1
);
154 gl_sockets_startup (SOCKETS_1_1
);
160 # define getaddrinfo_ptr getaddrinfo
161 # define freeaddrinfo_ptr freeaddrinfo
162 # define getnameinfo_ptr getnameinfo
168 validate_family (int family
)
170 /* FIXME: Support more families. */
172 if (family
== PF_INET
)
176 if (family
== PF_INET6
)
179 if (family
== PF_UNSPEC
)
184 /* Translate name of a service location and/or a service name to set of
187 getaddrinfo (const char *restrict nodename
,
188 const char *restrict servname
,
189 const struct addrinfo
*restrict hints
,
190 struct addrinfo
**restrict res
)
193 struct addrinfo
*tmp
;
200 struct addrinfo addrinfo
;
201 struct sockaddr_in6 sockaddr_in6
;
206 struct addrinfo addrinfo
;
207 struct sockaddr_in sockaddr_in
;
211 # ifdef WINDOWS_NATIVE
213 return getaddrinfo_ptr (nodename
, servname
, hints
, res
);
216 if (hints
&& (hints
->ai_flags
& ~(AI_CANONNAME
|AI_PASSIVE
)))
217 /* FIXME: Support more flags. */
220 if (hints
&& !validate_family (hints
->ai_family
))
224 hints
->ai_socktype
!= SOCK_STREAM
&& hints
->ai_socktype
!= SOCK_DGRAM
)
225 /* FIXME: Support other socktype. */
226 return EAI_SOCKTYPE
; /* FIXME: Better return code? */
230 if (!(hints
->ai_flags
& AI_PASSIVE
))
234 nodename
= (hints
->ai_family
== AF_INET6
) ? "::" : "0.0.0.0";
236 nodename
= "0.0.0.0";
242 struct servent
*se
= NULL
;
244 (hints
&& hints
->ai_socktype
== SOCK_DGRAM
) ? "udp" : "tcp";
246 if (hints
== NULL
|| !(hints
->ai_flags
& AI_NUMERICSERV
))
247 /* FIXME: Use getservbyname_r if available. */
248 se
= getservbyname (servname
, proto
);
253 if (!(*servname
>= '0' && *servname
<= '9'))
255 port
= strtoul (servname
, &c
, 10);
256 if (*c
|| port
> 0xffff)
264 /* FIXME: Use gethostbyname_r if available. */
265 he
= gethostbyname (nodename
);
266 if (!he
|| he
->h_addr_list
[0] == NULL
)
269 switch (he
->h_addrtype
)
273 size
= sizeof (struct v6_pair
);
279 size
= sizeof (struct v4_pair
);
287 storage
= calloc (1, size
);
291 switch (he
->h_addrtype
)
296 struct v6_pair
*p
= storage
;
297 struct sockaddr_in6
*sinp
= &p
->sockaddr_in6
;
301 sinp
->sin6_port
= port
;
303 if (he
->h_length
!= sizeof (sinp
->sin6_addr
))
306 return EAI_SYSTEM
; /* FIXME: Better return code? Set errno? */
309 memcpy (&sinp
->sin6_addr
, he
->h_addr_list
[0], sizeof sinp
->sin6_addr
);
311 tmp
->ai_addr
= (struct sockaddr
*) sinp
;
312 tmp
->ai_addrlen
= sizeof *sinp
;
320 struct v4_pair
*p
= storage
;
321 struct sockaddr_in
*sinp
= &p
->sockaddr_in
;
325 sinp
->sin_port
= port
;
327 if (he
->h_length
!= sizeof (sinp
->sin_addr
))
330 return EAI_SYSTEM
; /* FIXME: Better return code? Set errno? */
333 memcpy (&sinp
->sin_addr
, he
->h_addr_list
[0], sizeof sinp
->sin_addr
);
335 tmp
->ai_addr
= (struct sockaddr
*) sinp
;
336 tmp
->ai_addrlen
= sizeof *sinp
;
346 if (hints
&& hints
->ai_flags
& AI_CANONNAME
)
354 tmp
->ai_canonname
= strdup (cn
);
355 if (!tmp
->ai_canonname
)
362 tmp
->ai_protocol
= (hints
) ? hints
->ai_protocol
: 0;
363 tmp
->ai_socktype
= (hints
) ? hints
->ai_socktype
: 0;
364 tmp
->ai_addr
->sa_family
= he
->h_addrtype
;
365 tmp
->ai_family
= he
->h_addrtype
;
367 # ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
368 switch (he
->h_addrtype
)
372 tmp
->ai_addr
->sa_len
= sizeof (struct sockaddr_in
);
377 tmp
->ai_addr
->sa_len
= sizeof (struct sockaddr_in6
);
383 /* FIXME: If more than one address, create linked list of addrinfo's. */
390 /* Free 'addrinfo' structure AI including associated storage. */
392 freeaddrinfo (struct addrinfo
*ai
)
395 # ifdef WINDOWS_NATIVE
398 freeaddrinfo_ptr (ai
);
405 struct addrinfo
*cur
;
410 free (cur
->ai_canonname
);
416 getnameinfo (const struct sockaddr
*restrict sa
, socklen_t salen
,
417 char *restrict node
, socklen_t nodelen
,
418 char *restrict service
, socklen_t servicelen
,
422 # ifdef WINDOWS_NATIVE
424 return getnameinfo_ptr (sa
, salen
, node
, nodelen
,
425 service
, servicelen
, flags
);
428 /* FIXME: Support other flags. */
429 if ((node
&& nodelen
> 0 && !(flags
& NI_NUMERICHOST
)) ||
430 (service
&& servicelen
> 0 && !(flags
& NI_NUMERICHOST
)) ||
431 (flags
& ~(NI_NUMERICHOST
|NI_NUMERICSERV
)))
434 if (sa
== NULL
|| salen
< sizeof (sa
->sa_family
))
437 switch (sa
->sa_family
)
441 if (salen
< sizeof (struct sockaddr_in
))
447 if (salen
< sizeof (struct sockaddr_in6
))
455 if (node
&& nodelen
> 0 && flags
& NI_NUMERICHOST
)
457 switch (sa
->sa_family
)
461 if (!inet_ntop (AF_INET
,
462 &(((const struct sockaddr_in
*) sa
)->sin_addr
),
470 if (!inet_ntop (AF_INET6
,
471 &(((const struct sockaddr_in6
*) sa
)->sin6_addr
),
482 if (service
&& servicelen
> 0 && flags
& NI_NUMERICSERV
)
483 switch (sa
->sa_family
)
492 unsigned short int port
493 = ntohs (((const struct sockaddr_in
*) sa
)->sin_port
);
494 if (servicelen
<= snprintf (service
, servicelen
, "%u", port
))