1 /* Get address information (partial implementation).
2 Copyright (C) 1997, 2001, 2002, 2004, 2005, 2006 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 2, 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>
37 #define _(String) gettext (String)
38 #define N_(String) String
40 #include "inet_ntop.h"
44 /* BeOS has AF_INET, but not PF_INET. */
46 # define PF_INET AF_INET
48 /* BeOS also lacks PF_UNSPEC. */
53 #if defined _WIN32 || defined __WIN32__
58 typedef int (WSAAPI *getaddrinfo_func) (const char*, const char*,
59 const struct addrinfo*,
61 typedef void (WSAAPI *freeaddrinfo_func) (struct addrinfo*);
62 typedef int (WSAAPI *getnameinfo_func) (const struct sockaddr*,
63 socklen_t, char*, DWORD,
66 static getaddrinfo_func getaddrinfo_ptr = NULL;
67 static freeaddrinfo_func freeaddrinfo_ptr = NULL;
68 static getnameinfo_func getnameinfo_ptr = NULL;
77 return getaddrinfo_ptr ? 1 : 0;
81 h = GetModuleHandle ("ws2_32.dll");
85 getaddrinfo_ptr = (getaddrinfo_func) GetProcAddress (h, "getaddrinfo");
86 freeaddrinfo_ptr = (freeaddrinfo_func) GetProcAddress (h, "freeaddrinfo");
87 getnameinfo_ptr = (getnameinfo_func) GetProcAddress (h, "getnameinfo");
90 /* If either is missing, something is odd. */
91 if (!getaddrinfo_ptr || !freeaddrinfo_ptr || !getnameinfo_ptr)
93 getaddrinfo_ptr = NULL;
94 freeaddrinfo_ptr = NULL;
95 getnameinfo_ptr = NULL;
104 validate_family (int family)
106 /* FIXME: Support more families. */
108 if (family == PF_INET)
112 if (family == PF_INET6)
115 if (family == PF_UNSPEC)
120 /* Translate name of a service location and/or a service name to set of
123 getaddrinfo (const char *restrict nodename,
124 const char *restrict servname,
125 const struct addrinfo *restrict hints,
126 struct addrinfo **restrict res)
128 struct addrinfo *tmp;
135 struct addrinfo addrinfo;
136 struct sockaddr_in6 sockaddr_in6;
141 struct addrinfo addrinfo;
142 struct sockaddr_in sockaddr_in;
148 return getaddrinfo_ptr (nodename, servname, hints, res);
151 if (hints && (hints->ai_flags & ~(AI_CANONNAME|AI_PASSIVE)))
152 /* FIXME: Support more flags. */
155 if (hints && !validate_family (hints->ai_family))
159 hints->ai_socktype != SOCK_STREAM && hints->ai_socktype != SOCK_DGRAM)
160 /* FIXME: Support other socktype. */
161 return EAI_SOCKTYPE; /* FIXME: Better return code? */
165 if (!(hints->ai_flags & AI_PASSIVE))
169 nodename = (hints->ai_family == AF_INET6) ? "::" : "0.0.0.0";
171 nodename = "0.0.0.0";
177 struct servent *se = NULL;
179 (hints && hints->ai_socktype == SOCK_DGRAM) ? "udp" : "tcp";
181 if (!(hints->ai_flags & AI_NUMERICSERV))
182 /* FIXME: Use getservbyname_r if available. */
183 se = getservbyname (servname, proto);
188 if (!(*servname >= '0' && *servname <= '9'))
190 port = strtoul (servname, &c, 10);
191 if (*c || port > 0xffff)
199 /* FIXME: Use gethostbyname_r if available. */
200 he = gethostbyname (nodename);
201 if (!he || he->h_addr_list[0] == NULL)
204 switch (he->h_addrtype)
208 size = sizeof (struct v6_pair);
214 size = sizeof (struct v4_pair);
222 storage = calloc (1, size);
226 switch (he->h_addrtype)
231 struct v6_pair *p = storage;
232 struct sockaddr_in6 *sinp = &p->sockaddr_in6;
236 sinp->sin6_port = port;
238 if (he->h_length != sizeof (sinp->sin6_addr))
241 return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
244 memcpy (&sinp->sin6_addr, he->h_addr_list[0], sizeof sinp->sin6_addr);
246 tmp->ai_addr = (struct sockaddr *) sinp;
247 tmp->ai_addrlen = sizeof *sinp;
255 struct v4_pair *p = storage;
256 struct sockaddr_in *sinp = &p->sockaddr_in;
260 sinp->sin_port = port;
262 if (he->h_length != sizeof (sinp->sin_addr))
265 return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
268 memcpy (&sinp->sin_addr, he->h_addr_list[0], sizeof sinp->sin_addr);
270 tmp->ai_addr = (struct sockaddr *) sinp;
271 tmp->ai_addrlen = sizeof *sinp;
281 if (hints && hints->ai_flags & AI_CANONNAME)
289 tmp->ai_canonname = strdup (cn);
290 if (!tmp->ai_canonname)
297 tmp->ai_protocol = (hints) ? hints->ai_protocol : 0;
298 tmp->ai_socktype = (hints) ? hints->ai_socktype : 0;
299 tmp->ai_addr->sa_family = he->h_addrtype;
300 tmp->ai_family = he->h_addrtype;
302 /* FIXME: If more than one address, create linked list of addrinfo's. */
309 /* Free `addrinfo' structure AI including associated storage. */
311 freeaddrinfo (struct addrinfo *ai)
316 freeaddrinfo_ptr (ai);
323 struct addrinfo *cur;
328 if (cur->ai_canonname) free (cur->ai_canonname);
333 int getnameinfo(const struct sockaddr *restrict sa, socklen_t salen,
334 char *restrict node, socklen_t nodelen,
335 char *restrict service, socklen_t servicelen,
340 return getnameinfo_ptr (sa, salen, node, nodelen,
341 service, servicelen, flags);
344 /* FIXME: Support other flags. */
345 if ((node && nodelen > 0 && !(flags & NI_NUMERICHOST)) ||
346 (service && servicelen > 0 && !(flags & NI_NUMERICHOST)) ||
347 (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV)))
350 if (sa == NULL || salen < sizeof (sa->sa_family))
353 switch (sa->sa_family)
357 if (salen < sizeof (struct sockaddr_in))
363 if (salen < sizeof (struct sockaddr_in6))
371 if (node && nodelen > 0 && flags & NI_NUMERICHOST)
373 switch (sa->sa_family)
377 if (!inet_ntop (AF_INET,
378 &(((const struct sockaddr_in *) sa)->sin_addr),
386 if (!inet_ntop (AF_INET6,
387 &(((const struct sockaddr_in6 *) sa)->sin6_addr),
398 if (service && servicelen > 0 && flags & NI_NUMERICSERV)
399 switch (sa->sa_family)
408 unsigned short int port
409 = ntohs (((const struct sockaddr_in *) sa)->sin_port);
410 if (servicelen <= snprintf (service, servicelen, "%u", port))