1 /* Get address information (partial implementation).
2 Copyright (C) 1997, 2001-2002, 2004-2010 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2, 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 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, 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__
63 typedef int (WSAAPI
*getaddrinfo_func
) (const char*, const char*,
64 const struct addrinfo
*,
66 typedef void (WSAAPI
*freeaddrinfo_func
) (struct addrinfo
*);
67 typedef int (WSAAPI
*getnameinfo_func
) (const struct sockaddr
*,
68 socklen_t
, char*, DWORD
,
71 static getaddrinfo_func getaddrinfo_ptr
= NULL
;
72 static freeaddrinfo_func freeaddrinfo_ptr
= NULL
;
73 static getnameinfo_func getnameinfo_ptr
= NULL
;
82 return getaddrinfo_ptr
? 1 : 0;
86 h
= GetModuleHandle ("ws2_32.dll");
90 getaddrinfo_ptr
= (getaddrinfo_func
) GetProcAddress (h
, "getaddrinfo");
91 freeaddrinfo_ptr
= (freeaddrinfo_func
) GetProcAddress (h
, "freeaddrinfo");
92 getnameinfo_ptr
= (getnameinfo_func
) GetProcAddress (h
, "getnameinfo");
95 /* If either is missing, something is odd. */
96 if (!getaddrinfo_ptr
|| !freeaddrinfo_ptr
|| !getnameinfo_ptr
)
98 getaddrinfo_ptr
= NULL
;
99 freeaddrinfo_ptr
= NULL
;
100 getnameinfo_ptr
= NULL
;
109 validate_family (int family
)
111 /* FIXME: Support more families. */
113 if (family
== PF_INET
)
117 if (family
== PF_INET6
)
120 if (family
== PF_UNSPEC
)
125 /* Translate name of a service location and/or a service name to set of
128 getaddrinfo (const char *restrict nodename
,
129 const char *restrict servname
,
130 const struct addrinfo
*restrict hints
,
131 struct addrinfo
**restrict res
)
133 struct addrinfo
*tmp
;
140 struct addrinfo addrinfo
;
141 struct sockaddr_in6 sockaddr_in6
;
146 struct addrinfo addrinfo
;
147 struct sockaddr_in sockaddr_in
;
153 return getaddrinfo_ptr (nodename
, servname
, hints
, res
);
156 if (hints
&& (hints
->ai_flags
& ~(AI_CANONNAME
|AI_PASSIVE
)))
157 /* FIXME: Support more flags. */
160 if (hints
&& !validate_family (hints
->ai_family
))
164 hints
->ai_socktype
!= SOCK_STREAM
&& hints
->ai_socktype
!= SOCK_DGRAM
)
165 /* FIXME: Support other socktype. */
166 return EAI_SOCKTYPE
; /* FIXME: Better return code? */
170 if (!(hints
->ai_flags
& AI_PASSIVE
))
174 nodename
= (hints
->ai_family
== AF_INET6
) ? "::" : "0.0.0.0";
176 nodename
= "0.0.0.0";
182 struct servent
*se
= NULL
;
184 (hints
&& hints
->ai_socktype
== SOCK_DGRAM
) ? "udp" : "tcp";
186 if (hints
== NULL
|| !(hints
->ai_flags
& AI_NUMERICSERV
))
187 /* FIXME: Use getservbyname_r if available. */
188 se
= getservbyname (servname
, proto
);
193 if (!(*servname
>= '0' && *servname
<= '9'))
195 port
= strtoul (servname
, &c
, 10);
196 if (*c
|| port
> 0xffff)
204 /* FIXME: Use gethostbyname_r if available. */
205 he
= gethostbyname (nodename
);
206 if (!he
|| he
->h_addr_list
[0] == NULL
)
209 switch (he
->h_addrtype
)
213 size
= sizeof (struct v6_pair
);
219 size
= sizeof (struct v4_pair
);
227 storage
= calloc (1, size
);
231 switch (he
->h_addrtype
)
236 struct v6_pair
*p
= storage
;
237 struct sockaddr_in6
*sinp
= &p
->sockaddr_in6
;
241 sinp
->sin6_port
= port
;
243 if (he
->h_length
!= sizeof (sinp
->sin6_addr
))
246 return EAI_SYSTEM
; /* FIXME: Better return code? Set errno? */
249 memcpy (&sinp
->sin6_addr
, he
->h_addr_list
[0], sizeof sinp
->sin6_addr
);
251 tmp
->ai_addr
= (struct sockaddr
*) sinp
;
252 tmp
->ai_addrlen
= sizeof *sinp
;
260 struct v4_pair
*p
= storage
;
261 struct sockaddr_in
*sinp
= &p
->sockaddr_in
;
265 sinp
->sin_port
= port
;
267 if (he
->h_length
!= sizeof (sinp
->sin_addr
))
270 return EAI_SYSTEM
; /* FIXME: Better return code? Set errno? */
273 memcpy (&sinp
->sin_addr
, he
->h_addr_list
[0], sizeof sinp
->sin_addr
);
275 tmp
->ai_addr
= (struct sockaddr
*) sinp
;
276 tmp
->ai_addrlen
= sizeof *sinp
;
286 if (hints
&& hints
->ai_flags
& AI_CANONNAME
)
294 tmp
->ai_canonname
= strdup (cn
);
295 if (!tmp
->ai_canonname
)
302 tmp
->ai_protocol
= (hints
) ? hints
->ai_protocol
: 0;
303 tmp
->ai_socktype
= (hints
) ? hints
->ai_socktype
: 0;
304 tmp
->ai_addr
->sa_family
= he
->h_addrtype
;
305 tmp
->ai_family
= he
->h_addrtype
;
307 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
308 switch (he
->h_addrtype
)
312 tmp
->ai_addr
->sa_len
= sizeof (struct sockaddr_in
);
317 tmp
->ai_addr
->sa_len
= sizeof (struct sockaddr_in6
);
323 /* FIXME: If more than one address, create linked list of addrinfo's. */
330 /* Free `addrinfo' structure AI including associated storage. */
332 freeaddrinfo (struct addrinfo
*ai
)
337 freeaddrinfo_ptr (ai
);
344 struct addrinfo
*cur
;
349 free (cur
->ai_canonname
);
355 getnameinfo (const struct sockaddr
*restrict sa
, socklen_t salen
,
356 char *restrict node
, socklen_t nodelen
,
357 char *restrict service
, socklen_t servicelen
,
362 return getnameinfo_ptr (sa
, salen
, node
, nodelen
,
363 service
, servicelen
, flags
);
366 /* FIXME: Support other flags. */
367 if ((node
&& nodelen
> 0 && !(flags
& NI_NUMERICHOST
)) ||
368 (service
&& servicelen
> 0 && !(flags
& NI_NUMERICHOST
)) ||
369 (flags
& ~(NI_NUMERICHOST
|NI_NUMERICSERV
)))
372 if (sa
== NULL
|| salen
< sizeof (sa
->sa_family
))
375 switch (sa
->sa_family
)
379 if (salen
< sizeof (struct sockaddr_in
))
385 if (salen
< sizeof (struct sockaddr_in6
))
393 if (node
&& nodelen
> 0 && flags
& NI_NUMERICHOST
)
395 switch (sa
->sa_family
)
399 if (!inet_ntop (AF_INET
,
400 &(((const struct sockaddr_in
*) sa
)->sin_addr
),
408 if (!inet_ntop (AF_INET6
,
409 &(((const struct sockaddr_in6
*) sa
)->sin6_addr
),
420 if (service
&& servicelen
> 0 && flags
& NI_NUMERICSERV
)
421 switch (sa
->sa_family
)
430 unsigned short int port
431 = ntohs (((const struct sockaddr_in
*) sa
)->sin_port
);
432 if (servicelen
<= snprintf (service
, servicelen
, "%u", port
))