1 /* Get address information (partial implementation).
2 Copyright (C) 1997, 2001-2002, 2004-2011 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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
22 optimizes away the sa == NULL test below. */
23 #define _GL_ARG_NONNULL(params)
28 # include <netinet/in.h>
32 #include <arpa/inet.h>
37 /* Get memcpy, strdup. */
46 #define _(String) gettext (String)
47 #define N_(String) String
49 /* BeOS has AF_INET, but not PF_INET. */
51 # define PF_INET AF_INET
53 /* BeOS also lacks PF_UNSPEC. */
58 #if defined _WIN32 || defined __WIN32__
62 /* gl_sockets_startup */
66 typedef int (WSAAPI
*getaddrinfo_func
) (const char*, const char*,
67 const struct addrinfo
*,
69 typedef void (WSAAPI
*freeaddrinfo_func
) (struct addrinfo
*);
70 typedef int (WSAAPI
*getnameinfo_func
) (const struct sockaddr
*,
71 socklen_t
, char*, DWORD
,
74 static getaddrinfo_func getaddrinfo_ptr
= NULL
;
75 static freeaddrinfo_func freeaddrinfo_ptr
= NULL
;
76 static getnameinfo_func getnameinfo_ptr
= NULL
;
85 return getaddrinfo_ptr
? 1 : 0;
89 h
= GetModuleHandle ("ws2_32.dll");
93 getaddrinfo_ptr
= (getaddrinfo_func
) GetProcAddress (h
, "getaddrinfo");
94 freeaddrinfo_ptr
= (freeaddrinfo_func
) GetProcAddress (h
, "freeaddrinfo");
95 getnameinfo_ptr
= (getnameinfo_func
) GetProcAddress (h
, "getnameinfo");
98 /* If either is missing, something is odd. */
99 if (!getaddrinfo_ptr
|| !freeaddrinfo_ptr
|| !getnameinfo_ptr
)
101 getaddrinfo_ptr
= NULL
;
102 freeaddrinfo_ptr
= NULL
;
103 getnameinfo_ptr
= NULL
;
107 gl_sockets_startup (SOCKETS_1_1
);
114 validate_family (int family
)
116 /* FIXME: Support more families. */
118 if (family
== PF_INET
)
122 if (family
== PF_INET6
)
125 if (family
== PF_UNSPEC
)
130 /* Translate name of a service location and/or a service name to set of
133 getaddrinfo (const char *restrict nodename
,
134 const char *restrict servname
,
135 const struct addrinfo
*restrict hints
,
136 struct addrinfo
**restrict res
)
138 struct addrinfo
*tmp
;
145 struct addrinfo addrinfo
;
146 struct sockaddr_in6 sockaddr_in6
;
151 struct addrinfo addrinfo
;
152 struct sockaddr_in sockaddr_in
;
158 return getaddrinfo_ptr (nodename
, servname
, hints
, res
);
161 if (hints
&& (hints
->ai_flags
& ~(AI_CANONNAME
|AI_PASSIVE
)))
162 /* FIXME: Support more flags. */
165 if (hints
&& !validate_family (hints
->ai_family
))
169 hints
->ai_socktype
!= SOCK_STREAM
&& hints
->ai_socktype
!= SOCK_DGRAM
)
170 /* FIXME: Support other socktype. */
171 return EAI_SOCKTYPE
; /* FIXME: Better return code? */
175 if (!(hints
->ai_flags
& AI_PASSIVE
))
179 nodename
= (hints
->ai_family
== AF_INET6
) ? "::" : "0.0.0.0";
181 nodename
= "0.0.0.0";
187 struct servent
*se
= NULL
;
189 (hints
&& hints
->ai_socktype
== SOCK_DGRAM
) ? "udp" : "tcp";
191 if (hints
== NULL
|| !(hints
->ai_flags
& AI_NUMERICSERV
))
192 /* FIXME: Use getservbyname_r if available. */
193 se
= getservbyname (servname
, proto
);
198 if (!(*servname
>= '0' && *servname
<= '9'))
200 port
= strtoul (servname
, &c
, 10);
201 if (*c
|| port
> 0xffff)
209 /* FIXME: Use gethostbyname_r if available. */
210 he
= gethostbyname (nodename
);
211 if (!he
|| he
->h_addr_list
[0] == NULL
)
214 switch (he
->h_addrtype
)
218 size
= sizeof (struct v6_pair
);
224 size
= sizeof (struct v4_pair
);
232 storage
= calloc (1, size
);
236 switch (he
->h_addrtype
)
241 struct v6_pair
*p
= storage
;
242 struct sockaddr_in6
*sinp
= &p
->sockaddr_in6
;
246 sinp
->sin6_port
= port
;
248 if (he
->h_length
!= sizeof (sinp
->sin6_addr
))
251 return EAI_SYSTEM
; /* FIXME: Better return code? Set errno? */
254 memcpy (&sinp
->sin6_addr
, he
->h_addr_list
[0], sizeof sinp
->sin6_addr
);
256 tmp
->ai_addr
= (struct sockaddr
*) sinp
;
257 tmp
->ai_addrlen
= sizeof *sinp
;
265 struct v4_pair
*p
= storage
;
266 struct sockaddr_in
*sinp
= &p
->sockaddr_in
;
270 sinp
->sin_port
= port
;
272 if (he
->h_length
!= sizeof (sinp
->sin_addr
))
275 return EAI_SYSTEM
; /* FIXME: Better return code? Set errno? */
278 memcpy (&sinp
->sin_addr
, he
->h_addr_list
[0], sizeof sinp
->sin_addr
);
280 tmp
->ai_addr
= (struct sockaddr
*) sinp
;
281 tmp
->ai_addrlen
= sizeof *sinp
;
291 if (hints
&& hints
->ai_flags
& AI_CANONNAME
)
299 tmp
->ai_canonname
= strdup (cn
);
300 if (!tmp
->ai_canonname
)
307 tmp
->ai_protocol
= (hints
) ? hints
->ai_protocol
: 0;
308 tmp
->ai_socktype
= (hints
) ? hints
->ai_socktype
: 0;
309 tmp
->ai_addr
->sa_family
= he
->h_addrtype
;
310 tmp
->ai_family
= he
->h_addrtype
;
312 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
313 switch (he
->h_addrtype
)
317 tmp
->ai_addr
->sa_len
= sizeof (struct sockaddr_in
);
322 tmp
->ai_addr
->sa_len
= sizeof (struct sockaddr_in6
);
328 /* FIXME: If more than one address, create linked list of addrinfo's. */
335 /* Free `addrinfo' structure AI including associated storage. */
337 freeaddrinfo (struct addrinfo
*ai
)
342 freeaddrinfo_ptr (ai
);
349 struct addrinfo
*cur
;
354 free (cur
->ai_canonname
);
360 getnameinfo (const struct sockaddr
*restrict sa
, socklen_t salen
,
361 char *restrict node
, socklen_t nodelen
,
362 char *restrict service
, socklen_t servicelen
,
367 return getnameinfo_ptr (sa
, salen
, node
, nodelen
,
368 service
, servicelen
, flags
);
371 /* FIXME: Support other flags. */
372 if ((node
&& nodelen
> 0 && !(flags
& NI_NUMERICHOST
)) ||
373 (service
&& servicelen
> 0 && !(flags
& NI_NUMERICHOST
)) ||
374 (flags
& ~(NI_NUMERICHOST
|NI_NUMERICSERV
)))
377 if (sa
== NULL
|| salen
< sizeof (sa
->sa_family
))
380 switch (sa
->sa_family
)
384 if (salen
< sizeof (struct sockaddr_in
))
390 if (salen
< sizeof (struct sockaddr_in6
))
398 if (node
&& nodelen
> 0 && flags
& NI_NUMERICHOST
)
400 switch (sa
->sa_family
)
404 if (!inet_ntop (AF_INET
,
405 &(((const struct sockaddr_in
*) sa
)->sin_addr
),
413 if (!inet_ntop (AF_INET6
,
414 &(((const struct sockaddr_in6
*) sa
)->sin6_addr
),
425 if (service
&& servicelen
> 0 && flags
& NI_NUMERICSERV
)
426 switch (sa
->sa_family
)
435 unsigned short int port
436 = ntohs (((const struct sockaddr_in
*) sa
)->sin_port
);
437 if (servicelen
<= snprintf (service
, servicelen
, "%u", port
))