1 /* Get address information (partial implementation).
2 Copyright (C) 1997, 2001, 2002, 2004, 2005, 2006, 2007, 2008 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. */
25 # include <netinet/in.h>
29 #include <arpa/inet.h>
34 /* Get memcpy, strdup. */
43 #define _(String) gettext (String)
44 #define N_(String) String
46 /* BeOS has AF_INET, but not PF_INET. */
48 # define PF_INET AF_INET
50 /* BeOS also lacks PF_UNSPEC. */
55 #if defined _WIN32 || defined __WIN32__
60 typedef int (WSAAPI
*getaddrinfo_func
) (const char*, const char*,
61 const struct addrinfo
*,
63 typedef void (WSAAPI
*freeaddrinfo_func
) (struct addrinfo
*);
64 typedef int (WSAAPI
*getnameinfo_func
) (const struct sockaddr
*,
65 socklen_t
, char*, DWORD
,
68 static getaddrinfo_func getaddrinfo_ptr
= NULL
;
69 static freeaddrinfo_func freeaddrinfo_ptr
= NULL
;
70 static getnameinfo_func getnameinfo_ptr
= NULL
;
79 return getaddrinfo_ptr
? 1 : 0;
83 h
= GetModuleHandle ("ws2_32.dll");
87 getaddrinfo_ptr
= (getaddrinfo_func
) GetProcAddress (h
, "getaddrinfo");
88 freeaddrinfo_ptr
= (freeaddrinfo_func
) GetProcAddress (h
, "freeaddrinfo");
89 getnameinfo_ptr
= (getnameinfo_func
) GetProcAddress (h
, "getnameinfo");
92 /* If either is missing, something is odd. */
93 if (!getaddrinfo_ptr
|| !freeaddrinfo_ptr
|| !getnameinfo_ptr
)
95 getaddrinfo_ptr
= NULL
;
96 freeaddrinfo_ptr
= NULL
;
97 getnameinfo_ptr
= NULL
;
106 validate_family (int family
)
108 /* FIXME: Support more families. */
110 if (family
== PF_INET
)
114 if (family
== PF_INET6
)
117 if (family
== PF_UNSPEC
)
122 /* Translate name of a service location and/or a service name to set of
125 getaddrinfo (const char *restrict nodename
,
126 const char *restrict servname
,
127 const struct addrinfo
*restrict hints
,
128 struct addrinfo
**restrict res
)
130 struct addrinfo
*tmp
;
137 struct addrinfo addrinfo
;
138 struct sockaddr_in6 sockaddr_in6
;
143 struct addrinfo addrinfo
;
144 struct sockaddr_in sockaddr_in
;
150 return getaddrinfo_ptr (nodename
, servname
, hints
, res
);
153 if (hints
&& (hints
->ai_flags
& ~(AI_CANONNAME
|AI_PASSIVE
)))
154 /* FIXME: Support more flags. */
157 if (hints
&& !validate_family (hints
->ai_family
))
161 hints
->ai_socktype
!= SOCK_STREAM
&& hints
->ai_socktype
!= SOCK_DGRAM
)
162 /* FIXME: Support other socktype. */
163 return EAI_SOCKTYPE
; /* FIXME: Better return code? */
167 if (!(hints
->ai_flags
& AI_PASSIVE
))
171 nodename
= (hints
->ai_family
== AF_INET6
) ? "::" : "0.0.0.0";
173 nodename
= "0.0.0.0";
179 struct servent
*se
= NULL
;
181 (hints
&& hints
->ai_socktype
== SOCK_DGRAM
) ? "udp" : "tcp";
183 if (hints
== NULL
|| !(hints
->ai_flags
& AI_NUMERICSERV
))
184 /* FIXME: Use getservbyname_r if available. */
185 se
= getservbyname (servname
, proto
);
190 if (!(*servname
>= '0' && *servname
<= '9'))
192 port
= strtoul (servname
, &c
, 10);
193 if (*c
|| port
> 0xffff)
201 /* FIXME: Use gethostbyname_r if available. */
202 he
= gethostbyname (nodename
);
203 if (!he
|| he
->h_addr_list
[0] == NULL
)
206 switch (he
->h_addrtype
)
210 size
= sizeof (struct v6_pair
);
216 size
= sizeof (struct v4_pair
);
224 storage
= calloc (1, size
);
228 switch (he
->h_addrtype
)
233 struct v6_pair
*p
= storage
;
234 struct sockaddr_in6
*sinp
= &p
->sockaddr_in6
;
238 sinp
->sin6_port
= port
;
240 if (he
->h_length
!= sizeof (sinp
->sin6_addr
))
243 return EAI_SYSTEM
; /* FIXME: Better return code? Set errno? */
246 memcpy (&sinp
->sin6_addr
, he
->h_addr_list
[0], sizeof sinp
->sin6_addr
);
248 tmp
->ai_addr
= (struct sockaddr
*) sinp
;
249 tmp
->ai_addrlen
= sizeof *sinp
;
257 struct v4_pair
*p
= storage
;
258 struct sockaddr_in
*sinp
= &p
->sockaddr_in
;
262 sinp
->sin_port
= port
;
264 if (he
->h_length
!= sizeof (sinp
->sin_addr
))
267 return EAI_SYSTEM
; /* FIXME: Better return code? Set errno? */
270 memcpy (&sinp
->sin_addr
, he
->h_addr_list
[0], sizeof sinp
->sin_addr
);
272 tmp
->ai_addr
= (struct sockaddr
*) sinp
;
273 tmp
->ai_addrlen
= sizeof *sinp
;
283 if (hints
&& hints
->ai_flags
& AI_CANONNAME
)
291 tmp
->ai_canonname
= strdup (cn
);
292 if (!tmp
->ai_canonname
)
299 tmp
->ai_protocol
= (hints
) ? hints
->ai_protocol
: 0;
300 tmp
->ai_socktype
= (hints
) ? hints
->ai_socktype
: 0;
301 tmp
->ai_addr
->sa_family
= he
->h_addrtype
;
302 tmp
->ai_family
= he
->h_addrtype
;
304 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
305 switch (he
->h_addrtype
)
309 tmp
->ai_addr
->sa_len
= sizeof (struct sockaddr_in
);
314 tmp
->ai_addr
->sa_len
= sizeof (struct sockaddr_in6
);
320 /* FIXME: If more than one address, create linked list of addrinfo's. */
327 /* Free `addrinfo' structure AI including associated storage. */
329 freeaddrinfo (struct addrinfo
*ai
)
334 freeaddrinfo_ptr (ai
);
341 struct addrinfo
*cur
;
346 free (cur
->ai_canonname
);
351 int getnameinfo(const struct sockaddr
*restrict sa
, socklen_t salen
,
352 char *restrict node
, socklen_t nodelen
,
353 char *restrict service
, socklen_t servicelen
,
358 return getnameinfo_ptr (sa
, salen
, node
, nodelen
,
359 service
, servicelen
, flags
);
362 /* FIXME: Support other flags. */
363 if ((node
&& nodelen
> 0 && !(flags
& NI_NUMERICHOST
)) ||
364 (service
&& servicelen
> 0 && !(flags
& NI_NUMERICHOST
)) ||
365 (flags
& ~(NI_NUMERICHOST
|NI_NUMERICSERV
)))
368 if (sa
== NULL
|| salen
< sizeof (sa
->sa_family
))
371 switch (sa
->sa_family
)
375 if (salen
< sizeof (struct sockaddr_in
))
381 if (salen
< sizeof (struct sockaddr_in6
))
389 if (node
&& nodelen
> 0 && flags
& NI_NUMERICHOST
)
391 switch (sa
->sa_family
)
395 if (!inet_ntop (AF_INET
,
396 &(((const struct sockaddr_in
*) sa
)->sin_addr
),
404 if (!inet_ntop (AF_INET6
,
405 &(((const struct sockaddr_in6
*) sa
)->sin6_addr
),
416 if (service
&& servicelen
> 0 && flags
& NI_NUMERICSERV
)
417 switch (sa
->sa_family
)
426 unsigned short int port
427 = ntohs (((const struct sockaddr_in
*) sa
)->sin_port
);
428 if (servicelen
<= snprintf (service
, servicelen
, "%u", port
))