1 /* Get address information (partial implementation).
2 Copyright (C) 1997, 2001, 2002, 2004 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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))
77 if (hints && hints->ai_socktype)
78 /* FIXME: Support more socket types. */
82 hints->ai_protocol != SOCK_STREAM && hints->ai_protocol != SOCK_DGRAM)
83 /* FIXME: Support other protocols. */
84 return EAI_SERVICE; /* FIXME: Better return code? */
87 /* FIXME: Support server bind mode. */
93 (hints && hints->ai_protocol == SOCK_DGRAM) ? "udp" : "tcp";
95 /* FIXME: Use getservbyname_r if available. */
96 se = getservbyname (servname, proto);
102 /* FIXME: Use gethostbyname_r if available. */
103 he = gethostbyname (nodename);
104 if (!he || he->h_addr_list[0] == NULL)
107 switch (he->h_addrtype)
111 sinlen = sizeof (struct sockaddr_in6);
117 sinlen = sizeof (struct sockaddr_in);
125 tmp = calloc (1, sizeof (*tmp) + sinlen);
129 switch (he->h_addrtype)
134 struct sockaddr_in6 *sinp = (void *) tmp + sizeof (*tmp);
137 sinp->sin6_port = se->s_port;
139 if (he->h_length != sizeof (sinp->sin6_addr))
140 return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
142 memcpy (&sinp->sin6_addr, he->h_addr_list[0], he->h_length);
144 tmp->ai_addr = (struct sockaddr *) sinp;
145 tmp->ai_addrlen = sinlen;
153 struct sockaddr_in *sinp = (void *) tmp + sizeof (*tmp);
156 sinp->sin_port = se->s_port;
158 if (he->h_length != sizeof (sinp->sin_addr))
159 return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
161 memcpy (&sinp->sin_addr, he->h_addr_list[0], he->h_length);
163 tmp->ai_addr = (struct sockaddr *) sinp;
164 tmp->ai_addrlen = sinlen;
174 tmp->ai_addr->sa_family = he->h_addrtype;
176 /* FIXME: If more than one address, create linked list of addrinfo's. */
183 /* Free `addrinfo' structure AI including associated storage. */
185 freeaddrinfo (struct addrinfo *ai)
189 struct addrinfo *cur;