1 /* Get address information (partial implementation).
2 Copyright (C) 1997, 2001-2002, 2004-2013 Free Software Foundation, Inc.
3 Contributed by Simon Josefsson <simon@josefsson.org>.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
10 This program 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://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. */
45 #define _(String) gettext (String)
46 #define N_(String) String
48 /* BeOS has AF_INET, but not PF_INET. */
50 # define PF_INET AF_INET
52 /* BeOS also lacks PF_UNSPEC. */
57 #if defined _WIN32 || defined __WIN32__
58 # define WINDOWS_NATIVE
61 /* gl_sockets_startup */
65 typedef int (WSAAPI
*getaddrinfo_func
) (const char*, const char*,
66 const struct addrinfo
*,
68 typedef void (WSAAPI
*freeaddrinfo_func
) (struct addrinfo
*);
69 typedef int (WSAAPI
*getnameinfo_func
) (const struct sockaddr
*,
70 socklen_t
, char*, DWORD
,
73 static getaddrinfo_func getaddrinfo_ptr
= NULL
;
74 static freeaddrinfo_func freeaddrinfo_ptr
= NULL
;
75 static getnameinfo_func getnameinfo_ptr
= NULL
;
84 return getaddrinfo_ptr
? 1 : 0;
88 h
= GetModuleHandle ("ws2_32.dll");
92 getaddrinfo_ptr
= (getaddrinfo_func
) GetProcAddress (h
, "getaddrinfo");
93 freeaddrinfo_ptr
= (freeaddrinfo_func
) GetProcAddress (h
, "freeaddrinfo");
94 getnameinfo_ptr
= (getnameinfo_func
) GetProcAddress (h
, "getnameinfo");
97 /* If either is missing, something is odd. */
98 if (!getaddrinfo_ptr
|| !freeaddrinfo_ptr
|| !getnameinfo_ptr
)
100 getaddrinfo_ptr
= NULL
;
101 freeaddrinfo_ptr
= NULL
;
102 getnameinfo_ptr
= NULL
;
106 gl_sockets_startup (SOCKETS_1_1
);
113 validate_family (int family
)
115 /* FIXME: Support more families. */
117 if (family
== PF_INET
)
121 if (family
== PF_INET6
)
124 if (family
== PF_UNSPEC
)
129 /* Translate name of a service location and/or a service name to set of
132 getaddrinfo (const char *restrict nodename
,
133 const char *restrict servname
,
134 const struct addrinfo
*restrict hints
,
135 struct addrinfo
**restrict res
)
137 struct addrinfo
*tmp
;
144 struct addrinfo addrinfo
;
145 struct sockaddr_in6 sockaddr_in6
;
150 struct addrinfo addrinfo
;
151 struct sockaddr_in sockaddr_in
;
155 #ifdef WINDOWS_NATIVE
157 return getaddrinfo_ptr (nodename
, servname
, hints
, res
);
160 if (hints
&& (hints
->ai_flags
& ~(AI_CANONNAME
|AI_PASSIVE
)))
161 /* FIXME: Support more flags. */
164 if (hints
&& !validate_family (hints
->ai_family
))
168 hints
->ai_socktype
!= SOCK_STREAM
&& hints
->ai_socktype
!= SOCK_DGRAM
)
169 /* FIXME: Support other socktype. */
170 return EAI_SOCKTYPE
; /* FIXME: Better return code? */
174 if (!(hints
->ai_flags
& AI_PASSIVE
))
178 nodename
= (hints
->ai_family
== AF_INET6
) ? "::" : "0.0.0.0";
180 nodename
= "0.0.0.0";
186 struct servent
*se
= NULL
;
188 (hints
&& hints
->ai_socktype
== SOCK_DGRAM
) ? "udp" : "tcp";
190 if (hints
== NULL
|| !(hints
->ai_flags
& AI_NUMERICSERV
))
191 /* FIXME: Use getservbyname_r if available. */
192 se
= getservbyname (servname
, proto
);
197 if (!(*servname
>= '0' && *servname
<= '9'))
199 port
= strtoul (servname
, &c
, 10);
200 if (*c
|| port
> 0xffff)
208 /* FIXME: Use gethostbyname_r if available. */
209 he
= gethostbyname (nodename
);
210 if (!he
|| he
->h_addr_list
[0] == NULL
)
213 switch (he
->h_addrtype
)
217 size
= sizeof (struct v6_pair
);
223 size
= sizeof (struct v4_pair
);
231 storage
= calloc (1, size
);
235 switch (he
->h_addrtype
)
240 struct v6_pair
*p
= storage
;
241 struct sockaddr_in6
*sinp
= &p
->sockaddr_in6
;
245 sinp
->sin6_port
= port
;
247 if (he
->h_length
!= sizeof (sinp
->sin6_addr
))
250 return EAI_SYSTEM
; /* FIXME: Better return code? Set errno? */
253 memcpy (&sinp
->sin6_addr
, he
->h_addr_list
[0], sizeof sinp
->sin6_addr
);
255 tmp
->ai_addr
= (struct sockaddr
*) sinp
;
256 tmp
->ai_addrlen
= sizeof *sinp
;
264 struct v4_pair
*p
= storage
;
265 struct sockaddr_in
*sinp
= &p
->sockaddr_in
;
269 sinp
->sin_port
= port
;
271 if (he
->h_length
!= sizeof (sinp
->sin_addr
))
274 return EAI_SYSTEM
; /* FIXME: Better return code? Set errno? */
277 memcpy (&sinp
->sin_addr
, he
->h_addr_list
[0], sizeof sinp
->sin_addr
);
279 tmp
->ai_addr
= (struct sockaddr
*) sinp
;
280 tmp
->ai_addrlen
= sizeof *sinp
;
290 if (hints
&& hints
->ai_flags
& AI_CANONNAME
)
298 tmp
->ai_canonname
= strdup (cn
);
299 if (!tmp
->ai_canonname
)
306 tmp
->ai_protocol
= (hints
) ? hints
->ai_protocol
: 0;
307 tmp
->ai_socktype
= (hints
) ? hints
->ai_socktype
: 0;
308 tmp
->ai_addr
->sa_family
= he
->h_addrtype
;
309 tmp
->ai_family
= he
->h_addrtype
;
311 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
312 switch (he
->h_addrtype
)
316 tmp
->ai_addr
->sa_len
= sizeof (struct sockaddr_in
);
321 tmp
->ai_addr
->sa_len
= sizeof (struct sockaddr_in6
);
327 /* FIXME: If more than one address, create linked list of addrinfo's. */
334 /* Free 'addrinfo' structure AI including associated storage. */
336 freeaddrinfo (struct addrinfo
*ai
)
338 #ifdef WINDOWS_NATIVE
341 freeaddrinfo_ptr (ai
);
348 struct addrinfo
*cur
;
353 free (cur
->ai_canonname
);
359 getnameinfo (const struct sockaddr
*restrict sa
, socklen_t salen
,
360 char *restrict node
, socklen_t nodelen
,
361 char *restrict service
, socklen_t servicelen
,
364 #ifdef WINDOWS_NATIVE
366 return getnameinfo_ptr (sa
, salen
, node
, nodelen
,
367 service
, servicelen
, flags
);
370 /* FIXME: Support other flags. */
371 if ((node
&& nodelen
> 0 && !(flags
& NI_NUMERICHOST
)) ||
372 (service
&& servicelen
> 0 && !(flags
& NI_NUMERICHOST
)) ||
373 (flags
& ~(NI_NUMERICHOST
|NI_NUMERICSERV
)))
376 if (sa
== NULL
|| salen
< sizeof (sa
->sa_family
))
379 switch (sa
->sa_family
)
383 if (salen
< sizeof (struct sockaddr_in
))
389 if (salen
< sizeof (struct sockaddr_in6
))
397 if (node
&& nodelen
> 0 && flags
& NI_NUMERICHOST
)
399 switch (sa
->sa_family
)
403 if (!inet_ntop (AF_INET
,
404 &(((const struct sockaddr_in
*) sa
)->sin_addr
),
412 if (!inet_ntop (AF_INET6
,
413 &(((const struct sockaddr_in6
*) sa
)->sin6_addr
),
424 if (service
&& servicelen
> 0 && flags
& NI_NUMERICSERV
)
425 switch (sa
->sa_family
)
434 unsigned short int port
435 = ntohs (((const struct sockaddr_in
*) sa
)->sin_port
);
436 if (servicelen
<= snprintf (service
, servicelen
, "%u", port
))