1 /* Get address information (partial implementation).
2 Copyright (C) 1997, 2001, 2002, 2004, 2005 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"
31 /* Get struct hostent. */
37 #define _(String) gettext (String)
38 #define N_(String) String
43 validate_family (int family)
45 /* FIXME: Support more families. */
47 if (family == PF_INET)
51 if (family == PF_INET6)
54 if (family == PF_UNSPEC)
59 /* Translate name of a service location and/or a service name to set of
62 getaddrinfo (const char *restrict nodename,
63 const char *restrict servname,
64 const struct addrinfo *restrict hints,
65 struct addrinfo **restrict res)
72 if (hints && (hints->ai_flags & ~AI_CANONNAME))
73 /* FIXME: Support more flags. */
76 if (hints && !validate_family (hints->ai_family))
80 hints->ai_socktype != SOCK_STREAM && hints->ai_socktype != SOCK_DGRAM)
81 /* FIXME: Support other socktype. */
82 return EAI_SOCKTYPE; /* FIXME: Better return code? */
85 /* FIXME: Support server bind mode. */
91 (hints && hints->ai_socktype == SOCK_DGRAM) ? "udp" : "tcp";
93 /* FIXME: Use getservbyname_r if available. */
94 se = getservbyname (servname, proto);
100 /* FIXME: Use gethostbyname_r if available. */
101 he = gethostbyname (nodename);
102 if (!he || he->h_addr_list[0] == NULL)
105 switch (he->h_addrtype)
109 sinlen = sizeof (struct sockaddr_in6);
115 sinlen = sizeof (struct sockaddr_in);
123 tmp = calloc (1, sizeof (*tmp) + sinlen);
127 switch (he->h_addrtype)
132 struct sockaddr_in6 *sinp = (char *) tmp + sizeof (*tmp);
135 sinp->sin6_port = se->s_port;
137 if (he->h_length != sizeof (sinp->sin6_addr))
138 return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
140 memcpy (&sinp->sin6_addr, he->h_addr_list[0], he->h_length);
142 tmp->ai_addr = (struct sockaddr *) sinp;
143 tmp->ai_addrlen = sinlen;
151 struct sockaddr_in *sinp = (char *) tmp + sizeof (*tmp);
154 sinp->sin_port = se->s_port;
156 if (he->h_length != sizeof (sinp->sin_addr))
157 return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
159 memcpy (&sinp->sin_addr, he->h_addr_list[0], he->h_length);
161 tmp->ai_addr = (struct sockaddr *) sinp;
162 tmp->ai_addrlen = sinlen;
172 if (hints && hints->ai_flags & AI_CANONNAME)
180 tmp->ai_canonname = strdup (cn);
181 if (!tmp->ai_canonname)
188 tmp->ai_protocol = (hints) ? hints->ai_protocol : 0;
189 tmp->ai_socktype = (hints) ? hints->ai_socktype : 0;
190 tmp->ai_addr->sa_family = he->h_addrtype;
192 /* FIXME: If more than one address, create linked list of addrinfo's. */
199 /* Free `addrinfo' structure AI including associated storage. */
201 freeaddrinfo (struct addrinfo *ai)
205 struct addrinfo *cur;
210 if (cur->ai_canonname) free (cur->ai_canonname);