1 /* Get address information (partial implementation).
2 Copyright (C) 1997, 2001, 2002, 2004, 2005, 2006 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 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 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. */
23 #include "getaddrinfo.h"
26 # include <netinet/in.h>
38 #define _(String) gettext (String)
39 #define N_(String) String
41 #include "inet_ntop.h"
45 #if defined _WIN32 || defined __WIN32__
50 typedef int (WSAAPI
*getaddrinfo_func
) (const char*, const char*,
51 const struct addrinfo
*,
53 typedef void (WSAAPI
*freeaddrinfo_func
) (struct addrinfo
*);
54 typedef int (WSAAPI
*getnameinfo_func
) (const struct sockaddr
*,
55 socklen_t
, char*, DWORD
,
58 static getaddrinfo_func getaddrinfo_ptr
= NULL
;
59 static freeaddrinfo_func freeaddrinfo_ptr
= NULL
;
60 static getnameinfo_func getnameinfo_ptr
= NULL
;
69 return getaddrinfo_ptr
? 1 : 0;
73 h
= GetModuleHandle ("ws2_32.dll");
77 getaddrinfo_ptr
= (getaddrinfo_func
) GetProcAddress (h
, "getaddrinfo");
78 freeaddrinfo_ptr
= (freeaddrinfo_func
) GetProcAddress (h
, "freeaddrinfo");
79 getnameinfo_ptr
= (getnameinfo_func
) GetProcAddress (h
, "getnameinfo");
82 /* If either is missing, something is odd. */
83 if (!getaddrinfo_ptr
|| !freeaddrinfo_ptr
|| !getnameinfo_ptr
)
85 getaddrinfo_ptr
= NULL
;
86 freeaddrinfo_ptr
= NULL
;
87 getnameinfo_ptr
= NULL
;
96 validate_family (int family
)
98 /* FIXME: Support more families. */
100 if (family
== PF_INET
)
104 if (family
== PF_INET6
)
107 if (family
== PF_UNSPEC
)
112 /* Translate name of a service location and/or a service name to set of
115 getaddrinfo (const char *restrict nodename
,
116 const char *restrict servname
,
117 const struct addrinfo
*restrict hints
,
118 struct addrinfo
**restrict res
)
120 struct addrinfo
*tmp
;
127 struct addrinfo addrinfo
;
128 struct sockaddr_in6 sockaddr_in6
;
133 struct addrinfo addrinfo
;
134 struct sockaddr_in sockaddr_in
;
140 return getaddrinfo_ptr (nodename
, servname
, hints
, res
);
143 if (hints
&& (hints
->ai_flags
& ~(AI_CANONNAME
|AI_PASSIVE
)))
144 /* FIXME: Support more flags. */
147 if (hints
&& !validate_family (hints
->ai_family
))
151 hints
->ai_socktype
!= SOCK_STREAM
&& hints
->ai_socktype
!= SOCK_DGRAM
)
152 /* FIXME: Support other socktype. */
153 return EAI_SOCKTYPE
; /* FIXME: Better return code? */
157 if (!(hints
->ai_flags
& AI_PASSIVE
))
161 nodename
= (hint
->ai_family
== AF_INET6
) ? "::" : "0.0.0.0";
163 nodename
= "0.0.0.0";
169 struct servent
*se
= NULL
;
171 (hints
&& hints
->ai_socktype
== SOCK_DGRAM
) ? "udp" : "tcp";
173 if (!(hints
->ai_flags
& AI_NUMERICSERV
))
174 /* FIXME: Use getservbyname_r if available. */
175 se
= getservbyname (servname
, proto
);
180 if (!(*servname
>= '0' && *servname
<= '9'))
182 port
= strtoul (servname
, &c
, 10);
183 if (*c
|| port
> 0xffff)
191 /* FIXME: Use gethostbyname_r if available. */
192 he
= gethostbyname (nodename
);
193 if (!he
|| he
->h_addr_list
[0] == NULL
)
196 switch (he
->h_addrtype
)
200 size
= sizeof (struct v6_pair
);
206 size
= sizeof (struct v4_pair
);
214 storage
= calloc (1, size
);
218 switch (he
->h_addrtype
)
223 struct v6_pair
*p
= storage
;
224 struct sockaddr_in6
*sinp
= &p
->sockaddr_in6
;
228 sinp
->sin6_port
= port
;
230 if (he
->h_length
!= sizeof (sinp
->sin6_addr
))
233 return EAI_SYSTEM
; /* FIXME: Better return code? Set errno? */
236 memcpy (&sinp
->sin6_addr
, he
->h_addr_list
[0], sizeof sinp
->sin6_addr
);
238 tmp
->ai_addr
= (struct sockaddr
*) sinp
;
239 tmp
->ai_addrlen
= sizeof *sinp
;
247 struct v4_pair
*p
= storage
;
248 struct sockaddr_in
*sinp
= &p
->sockaddr_in
;
252 sinp
->sin_port
= port
;
254 if (he
->h_length
!= sizeof (sinp
->sin_addr
))
257 return EAI_SYSTEM
; /* FIXME: Better return code? Set errno? */
260 memcpy (&sinp
->sin_addr
, he
->h_addr_list
[0], sizeof sinp
->sin_addr
);
262 tmp
->ai_addr
= (struct sockaddr
*) sinp
;
263 tmp
->ai_addrlen
= sizeof *sinp
;
273 if (hints
&& hints
->ai_flags
& AI_CANONNAME
)
281 tmp
->ai_canonname
= strdup (cn
);
282 if (!tmp
->ai_canonname
)
289 tmp
->ai_protocol
= (hints
) ? hints
->ai_protocol
: 0;
290 tmp
->ai_socktype
= (hints
) ? hints
->ai_socktype
: 0;
291 tmp
->ai_addr
->sa_family
= he
->h_addrtype
;
292 tmp
->ai_family
= he
->h_addrtype
;
294 /* FIXME: If more than one address, create linked list of addrinfo's. */
301 /* Free `addrinfo' structure AI including associated storage. */
303 freeaddrinfo (struct addrinfo
*ai
)
308 freeaddrinfo_ptr (ai
);
315 struct addrinfo
*cur
;
320 if (cur
->ai_canonname
) free (cur
->ai_canonname
);
325 int getnameinfo(const struct sockaddr
*restrict sa
, socklen_t salen
,
326 char *restrict node
, socklen_t nodelen
,
327 char *restrict service
, socklen_t servicelen
,
332 return getnameinfo_ptr (sa
, salen
, node
, nodelen
,
333 service
, servicelen
, flags
);
336 /* FIXME: Support other flags. */
337 if ((node
&& nodelen
> 0 && !(flags
& NI_NUMERICHOST
)) ||
338 (service
&& servicelen
> 0 && !(flags
& NI_NUMERICHOST
)) ||
339 (flags
& ~(NI_NUMERICHOST
|NI_NUMERICSERV
)))
342 if (sa
== NULL
|| salen
< sizeof (sa
->sa_family
))
345 switch (sa
->sa_family
)
349 if (salen
< sizeof (struct sockaddr_in
))
355 if (salen
< sizeof (struct sockaddr_in6
))
363 if (node
&& nodelen
> 0 && flags
& NI_NUMERICHOST
)
365 switch (sa
->sa_family
)
369 if (!inet_ntop (AF_INET
,
370 &(((const struct sockaddr_in
*) sa
)->sin_addr
),
378 if (!inet_ntop (AF_INET6
,
379 &(((const struct sockaddr_in6
*) sa
)->sin6_addr
),
390 if (service
&& servicelen
> 0 && flags
& NI_NUMERICSERV
)
391 switch (sa
->sa_family
)
399 if (snprintf (service
, servicelen
, "%d",
400 ntohs (((const struct sockaddr_in
*) sa
)->sin_port
))