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. */
29 /* Get struct hostent. */
35 #define _(String) gettext (String)
36 #define N_(String) String
38 #include "getaddrinfo.h"
41 validate_family (int family)
43 /* FIXME: Support more families. */
45 if (family == PF_INET)
49 if (family == PF_INET6)
52 if (family == PF_UNSPEC)
57 /* Translate name of a service location and/or a service name to set of
60 getaddrinfo (const char *restrict nodename,
61 const char *restrict servname,
62 const struct addrinfo *restrict hints,
63 struct addrinfo **restrict res)
70 if (hints && hints->ai_flags)
71 /* FIXME: Support more flags. */
74 if (hints && !validate_family (hints->ai_family))
78 hints->ai_socktype != SOCK_STREAM && hints->ai_socktype != SOCK_DGRAM)
79 /* FIXME: Support other socktype. */
80 return EAI_SOCKTYPE; /* FIXME: Better return code? */
83 /* FIXME: Support server bind mode. */
89 (hints && hints->ai_socktype == SOCK_DGRAM) ? "udp" : "tcp";
91 /* FIXME: Use getservbyname_r if available. */
92 se = getservbyname (servname, proto);
98 /* FIXME: Use gethostbyname_r if available. */
99 he = gethostbyname (nodename);
100 if (!he || he->h_addr_list[0] == NULL)
103 switch (he->h_addrtype)
107 sinlen = sizeof (struct sockaddr_in6);
113 sinlen = sizeof (struct sockaddr_in);
121 tmp = calloc (1, sizeof (*tmp) + sinlen);
125 switch (he->h_addrtype)
130 struct sockaddr_in6 *sinp = (void *) tmp + sizeof (*tmp);
133 sinp->sin6_port = se->s_port;
135 if (he->h_length != sizeof (sinp->sin6_addr))
136 return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
138 memcpy (&sinp->sin6_addr, he->h_addr_list[0], he->h_length);
140 tmp->ai_addr = (struct sockaddr *) sinp;
141 tmp->ai_addrlen = sinlen;
149 struct sockaddr_in *sinp = (void *) tmp + sizeof (*tmp);
152 sinp->sin_port = se->s_port;
154 if (he->h_length != sizeof (sinp->sin_addr))
155 return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
157 memcpy (&sinp->sin_addr, he->h_addr_list[0], he->h_length);
159 tmp->ai_addr = (struct sockaddr *) sinp;
160 tmp->ai_addrlen = sinlen;
170 tmp->ai_protocol = (hints) ? hints->ai_protocol : 0;
171 tmp->ai_socktype = (hints) ? hints->ai_socktype : 0;
172 tmp->ai_addr->sa_family = he->h_addrtype;
174 /* FIXME: If more than one address, create linked list of addrinfo's. */
181 /* Free `addrinfo' structure AI including associated storage. */
183 freeaddrinfo (struct addrinfo *ai)
187 struct addrinfo *cur;