1 /* Get address information (partial implementation).
2 Copyright (C) 1997, 2001, 2002, 2004, 2005, 2006, 2007 Free Software
4 Contributed by Simon Josefsson <simon@josefsson.org>.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include "getaddrinfo.h"
25 # include <netinet/in.h>
31 /* Get memcpy, strdup. */
40 #define _(String) gettext (String)
41 #define N_(String) String
43 #include "inet_ntop.h"
45 /* BeOS has AF_INET, but not PF_INET. */
47 # define PF_INET AF_INET
49 /* BeOS also lacks PF_UNSPEC. */
54 #if defined _WIN32 || defined __WIN32__
59 typedef int (WSAAPI
*getaddrinfo_func
) (const char*, const char*,
60 const struct addrinfo
*,
62 typedef void (WSAAPI
*freeaddrinfo_func
) (struct addrinfo
*);
63 typedef int (WSAAPI
*getnameinfo_func
) (const struct sockaddr
*,
64 socklen_t
, char*, DWORD
,
67 static getaddrinfo_func getaddrinfo_ptr
= NULL
;
68 static freeaddrinfo_func freeaddrinfo_ptr
= NULL
;
69 static getnameinfo_func getnameinfo_ptr
= NULL
;
78 return getaddrinfo_ptr
? 1 : 0;
82 h
= GetModuleHandle ("ws2_32.dll");
86 getaddrinfo_ptr
= (getaddrinfo_func
) GetProcAddress (h
, "getaddrinfo");
87 freeaddrinfo_ptr
= (freeaddrinfo_func
) GetProcAddress (h
, "freeaddrinfo");
88 getnameinfo_ptr
= (getnameinfo_func
) GetProcAddress (h
, "getnameinfo");
91 /* If either is missing, something is odd. */
92 if (!getaddrinfo_ptr
|| !freeaddrinfo_ptr
|| !getnameinfo_ptr
)
94 getaddrinfo_ptr
= NULL
;
95 freeaddrinfo_ptr
= NULL
;
96 getnameinfo_ptr
= NULL
;
105 validate_family (int family
)
107 /* FIXME: Support more families. */
109 if (family
== PF_INET
)
113 if (family
== PF_INET6
)
116 if (family
== PF_UNSPEC
)
121 /* Translate name of a service location and/or a service name to set of
124 getaddrinfo (const char *restrict nodename
,
125 const char *restrict servname
,
126 const struct addrinfo
*restrict hints
,
127 struct addrinfo
**restrict res
)
129 struct addrinfo
*tmp
;
136 struct addrinfo addrinfo
;
137 struct sockaddr_in6 sockaddr_in6
;
142 struct addrinfo addrinfo
;
143 struct sockaddr_in sockaddr_in
;
149 return getaddrinfo_ptr (nodename
, servname
, hints
, res
);
152 if (hints
&& (hints
->ai_flags
& ~(AI_CANONNAME
|AI_PASSIVE
)))
153 /* FIXME: Support more flags. */
156 if (hints
&& !validate_family (hints
->ai_family
))
160 hints
->ai_socktype
!= SOCK_STREAM
&& hints
->ai_socktype
!= SOCK_DGRAM
)
161 /* FIXME: Support other socktype. */
162 return EAI_SOCKTYPE
; /* FIXME: Better return code? */
166 if (!(hints
->ai_flags
& AI_PASSIVE
))
170 nodename
= (hints
->ai_family
== AF_INET6
) ? "::" : "0.0.0.0";
172 nodename
= "0.0.0.0";
178 struct servent
*se
= NULL
;
180 (hints
&& hints
->ai_socktype
== SOCK_DGRAM
) ? "udp" : "tcp";
182 if (hints
== NULL
|| !(hints
->ai_flags
& AI_NUMERICSERV
))
183 /* FIXME: Use getservbyname_r if available. */
184 se
= getservbyname (servname
, proto
);
189 if (!(*servname
>= '0' && *servname
<= '9'))
191 port
= strtoul (servname
, &c
, 10);
192 if (*c
|| port
> 0xffff)
200 /* FIXME: Use gethostbyname_r if available. */
201 he
= gethostbyname (nodename
);
202 if (!he
|| he
->h_addr_list
[0] == NULL
)
205 switch (he
->h_addrtype
)
209 size
= sizeof (struct v6_pair
);
215 size
= sizeof (struct v4_pair
);
223 storage
= calloc (1, size
);
227 switch (he
->h_addrtype
)
232 struct v6_pair
*p
= storage
;
233 struct sockaddr_in6
*sinp
= &p
->sockaddr_in6
;
237 sinp
->sin6_port
= port
;
239 if (he
->h_length
!= sizeof (sinp
->sin6_addr
))
242 return EAI_SYSTEM
; /* FIXME: Better return code? Set errno? */
245 memcpy (&sinp
->sin6_addr
, he
->h_addr_list
[0], sizeof sinp
->sin6_addr
);
247 tmp
->ai_addr
= (struct sockaddr
*) sinp
;
248 tmp
->ai_addrlen
= sizeof *sinp
;
256 struct v4_pair
*p
= storage
;
257 struct sockaddr_in
*sinp
= &p
->sockaddr_in
;
261 sinp
->sin_port
= port
;
263 if (he
->h_length
!= sizeof (sinp
->sin_addr
))
266 return EAI_SYSTEM
; /* FIXME: Better return code? Set errno? */
269 memcpy (&sinp
->sin_addr
, he
->h_addr_list
[0], sizeof sinp
->sin_addr
);
271 tmp
->ai_addr
= (struct sockaddr
*) sinp
;
272 tmp
->ai_addrlen
= sizeof *sinp
;
282 if (hints
&& hints
->ai_flags
& AI_CANONNAME
)
290 tmp
->ai_canonname
= strdup (cn
);
291 if (!tmp
->ai_canonname
)
298 tmp
->ai_protocol
= (hints
) ? hints
->ai_protocol
: 0;
299 tmp
->ai_socktype
= (hints
) ? hints
->ai_socktype
: 0;
300 tmp
->ai_addr
->sa_family
= he
->h_addrtype
;
301 tmp
->ai_family
= he
->h_addrtype
;
303 /* FIXME: If more than one address, create linked list of addrinfo's. */
310 /* Free `addrinfo' structure AI including associated storage. */
312 freeaddrinfo (struct addrinfo
*ai
)
317 freeaddrinfo_ptr (ai
);
324 struct addrinfo
*cur
;
329 if (cur
->ai_canonname
) free (cur
->ai_canonname
);
334 int getnameinfo(const struct sockaddr
*restrict sa
, socklen_t salen
,
335 char *restrict node
, socklen_t nodelen
,
336 char *restrict service
, socklen_t servicelen
,
341 return getnameinfo_ptr (sa
, salen
, node
, nodelen
,
342 service
, servicelen
, flags
);
345 /* FIXME: Support other flags. */
346 if ((node
&& nodelen
> 0 && !(flags
& NI_NUMERICHOST
)) ||
347 (service
&& servicelen
> 0 && !(flags
& NI_NUMERICHOST
)) ||
348 (flags
& ~(NI_NUMERICHOST
|NI_NUMERICSERV
)))
351 if (sa
== NULL
|| salen
< sizeof (sa
->sa_family
))
354 switch (sa
->sa_family
)
358 if (salen
< sizeof (struct sockaddr_in
))
364 if (salen
< sizeof (struct sockaddr_in6
))
372 if (node
&& nodelen
> 0 && flags
& NI_NUMERICHOST
)
374 switch (sa
->sa_family
)
378 if (!inet_ntop (AF_INET
,
379 &(((const struct sockaddr_in
*) sa
)->sin_addr
),
387 if (!inet_ntop (AF_INET6
,
388 &(((const struct sockaddr_in6
*) sa
)->sin6_addr
),
399 if (service
&& servicelen
> 0 && flags
& NI_NUMERICSERV
)
400 switch (sa
->sa_family
)
409 unsigned short int port
410 = ntohs (((const struct sockaddr_in
*) sa
)->sin_port
);
411 if (servicelen
<= snprintf (service
, servicelen
, "%u", port
))