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"
26 # include <netinet/in.h>
38 #define _(String) gettext (String)
39 #define N_(String) String
44 validate_family (int family)
46 /* FIXME: Support more families. */
48 if (family == PF_INET)
52 if (family == PF_INET6)
55 if (family == PF_UNSPEC)
60 /* Translate name of a service location and/or a service name to set of
63 getaddrinfo (const char *restrict nodename,
64 const char *restrict servname,
65 const struct addrinfo *restrict hints,
66 struct addrinfo **restrict res)
69 struct servent *se = NULL;
75 struct addrinfo addrinfo;
76 struct sockaddr_in6 sockaddr_in6;
81 struct addrinfo addrinfo;
82 struct sockaddr_in sockaddr_in;
86 if (hints && (hints->ai_flags & ~AI_CANONNAME))
87 /* FIXME: Support more flags. */
90 if (hints && !validate_family (hints->ai_family))
94 hints->ai_socktype != SOCK_STREAM && hints->ai_socktype != SOCK_DGRAM)
95 /* FIXME: Support other socktype. */
96 return EAI_SOCKTYPE; /* FIXME: Better return code? */
99 /* FIXME: Support server bind mode. */
105 (hints && hints->ai_socktype == SOCK_DGRAM) ? "udp" : "tcp";
107 /* FIXME: Use getservbyname_r if available. */
108 se = getservbyname (servname, proto);
114 /* FIXME: Use gethostbyname_r if available. */
115 he = gethostbyname (nodename);
116 if (!he || he->h_addr_list[0] == NULL)
119 switch (he->h_addrtype)
123 size = sizeof (struct v6_pair);
129 size = sizeof (struct v4_pair);
137 storage = calloc (1, size);
141 switch (he->h_addrtype)
146 struct v6_pair *p = storage;
147 struct sockaddr_in6 *sinp = &p->sockaddr_in6;
151 sinp->sin6_port = se->s_port;
153 if (he->h_length != sizeof (sinp->sin6_addr))
156 return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
159 memcpy (&sinp->sin6_addr, he->h_addr_list[0], sizeof sinp->sin6_addr);
161 tmp->ai_addr = (struct sockaddr *) sinp;
162 tmp->ai_addrlen = sizeof *sinp;
170 struct v4_pair *p = storage;
171 struct sockaddr_in *sinp = &p->sockaddr_in;
175 sinp->sin_port = se->s_port;
177 if (he->h_length != sizeof (sinp->sin_addr))
180 return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
183 memcpy (&sinp->sin_addr, he->h_addr_list[0], sizeof sinp->sin_addr);
185 tmp->ai_addr = (struct sockaddr *) sinp;
186 tmp->ai_addrlen = sizeof *sinp;
196 if (hints && hints->ai_flags & AI_CANONNAME)
204 tmp->ai_canonname = strdup (cn);
205 if (!tmp->ai_canonname)
212 tmp->ai_protocol = (hints) ? hints->ai_protocol : 0;
213 tmp->ai_socktype = (hints) ? hints->ai_socktype : 0;
214 tmp->ai_addr->sa_family = he->h_addrtype;
215 tmp->ai_family = he->h_addrtype;
217 /* FIXME: If more than one address, create linked list of addrinfo's. */
224 /* Free `addrinfo' structure AI including associated storage. */
226 freeaddrinfo (struct addrinfo *ai)
230 struct addrinfo *cur;
235 if (cur->ai_canonname) free (cur->ai_canonname);