1a7c91847Schristos /* Get address information (partial implementation).
2a7c91847Schristos Copyright (C) 1997, 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
3a7c91847Schristos Contributed by Simon Josefsson <simon@josefsson.org>.
4a7c91847Schristos
5a7c91847Schristos This program is free software; you can redistribute it and/or modify
6a7c91847Schristos it under the terms of the GNU General Public License as published by
7a7c91847Schristos the Free Software Foundation; either version 2, or (at your option)
8a7c91847Schristos any later version.
9a7c91847Schristos
10a7c91847Schristos This program is distributed in the hope that it will be useful,
11a7c91847Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
12a7c91847Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a7c91847Schristos GNU General Public License for more details.
14a7c91847Schristos
15a7c91847Schristos You should have received a copy of the GNU General Public License
16a7c91847Schristos along with this program; if not, write to the Free Software Foundation,
17a7c91847Schristos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18*5a6c14c8Schristos #include <sys/cdefs.h>
19*5a6c14c8Schristos __RCSID("$NetBSD: getaddrinfo.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
20*5a6c14c8Schristos
21a7c91847Schristos
22a7c91847Schristos #ifdef HAVE_CONFIG_H
23a7c91847Schristos # include <config.h>
24a7c91847Schristos #endif
25a7c91847Schristos
26a7c91847Schristos #include "getaddrinfo.h"
27a7c91847Schristos
28a7c91847Schristos /* Get calloc. */
29a7c91847Schristos #include <stdlib.h>
30a7c91847Schristos
31a7c91847Schristos /* Get memcpy. */
32a7c91847Schristos #include <string.h>
33a7c91847Schristos
34a7c91847Schristos #include <stdbool.h>
35a7c91847Schristos
36a7c91847Schristos #include "gettext.h"
37a7c91847Schristos #define _(String) gettext (String)
38a7c91847Schristos #define N_(String) String
39a7c91847Schristos
40a7c91847Schristos #include "strdup.h"
41a7c91847Schristos
42a7c91847Schristos static inline bool
validate_family(int family)43a7c91847Schristos validate_family (int family)
44a7c91847Schristos {
45a7c91847Schristos /* FIXME: Support more families. */
46a7c91847Schristos #if HAVE_IPV4
47a7c91847Schristos if (family == PF_INET)
48a7c91847Schristos return true;
49a7c91847Schristos #endif
50a7c91847Schristos #if HAVE_IPV6
51a7c91847Schristos if (family == PF_INET6)
52a7c91847Schristos return true;
53a7c91847Schristos #endif
54a7c91847Schristos if (family == PF_UNSPEC)
55a7c91847Schristos return true;
56a7c91847Schristos return false;
57a7c91847Schristos }
58a7c91847Schristos
59a7c91847Schristos /* Translate name of a service location and/or a service name to set of
60a7c91847Schristos socket addresses. */
61a7c91847Schristos int
getaddrinfo(const char * restrict nodename,const char * restrict servname,const struct addrinfo * restrict hints,struct addrinfo ** restrict res)62a7c91847Schristos getaddrinfo (const char *restrict nodename,
63a7c91847Schristos const char *restrict servname,
64a7c91847Schristos const struct addrinfo *restrict hints,
65a7c91847Schristos struct addrinfo **restrict res)
66a7c91847Schristos {
67a7c91847Schristos struct addrinfo *tmp;
68a7c91847Schristos struct servent *se;
69a7c91847Schristos struct hostent *he;
70a7c91847Schristos size_t sinlen;
71a7c91847Schristos
72a7c91847Schristos if (hints && (hints->ai_flags & ~AI_CANONNAME))
73a7c91847Schristos /* FIXME: Support more flags. */
74a7c91847Schristos return EAI_BADFLAGS;
75a7c91847Schristos
76a7c91847Schristos if (hints && !validate_family (hints->ai_family))
77a7c91847Schristos return EAI_FAMILY;
78a7c91847Schristos
79a7c91847Schristos if (hints &&
80a7c91847Schristos hints->ai_socktype != SOCK_STREAM && hints->ai_socktype != SOCK_DGRAM)
81a7c91847Schristos /* FIXME: Support other socktype. */
82a7c91847Schristos return EAI_SOCKTYPE; /* FIXME: Better return code? */
83a7c91847Schristos
84a7c91847Schristos if (!nodename)
85a7c91847Schristos /* FIXME: Support server bind mode. */
86a7c91847Schristos return EAI_NONAME;
87a7c91847Schristos
88a7c91847Schristos if (servname)
89a7c91847Schristos {
90a7c91847Schristos const char *proto =
91a7c91847Schristos (hints && hints->ai_socktype == SOCK_DGRAM) ? "udp" : "tcp";
92a7c91847Schristos
93a7c91847Schristos /* FIXME: Use getservbyname_r if available. */
94a7c91847Schristos se = getservbyname (servname, proto);
95a7c91847Schristos
96a7c91847Schristos if (!se)
97a7c91847Schristos return EAI_SERVICE;
98a7c91847Schristos }
99a7c91847Schristos
100a7c91847Schristos /* FIXME: Use gethostbyname_r if available. */
101a7c91847Schristos he = gethostbyname (nodename);
102a7c91847Schristos if (!he || he->h_addr_list[0] == NULL)
103a7c91847Schristos return EAI_NONAME;
104a7c91847Schristos
105a7c91847Schristos switch (he->h_addrtype)
106a7c91847Schristos {
107a7c91847Schristos #if HAVE_IPV6
108a7c91847Schristos case PF_INET6:
109a7c91847Schristos sinlen = sizeof (struct sockaddr_in6);
110a7c91847Schristos break;
111a7c91847Schristos #endif
112a7c91847Schristos
113a7c91847Schristos #if HAVE_IPV4
114a7c91847Schristos case PF_INET:
115a7c91847Schristos sinlen = sizeof (struct sockaddr_in);
116a7c91847Schristos break;
117a7c91847Schristos #endif
118a7c91847Schristos
119a7c91847Schristos default:
120a7c91847Schristos return EAI_NODATA;
121a7c91847Schristos }
122a7c91847Schristos
123a7c91847Schristos tmp = calloc (1, sizeof (*tmp) + sinlen);
124a7c91847Schristos if (!tmp)
125a7c91847Schristos return EAI_MEMORY;
126a7c91847Schristos
127a7c91847Schristos switch (he->h_addrtype)
128a7c91847Schristos {
129a7c91847Schristos #if HAVE_IPV6
130a7c91847Schristos case PF_INET6:
131a7c91847Schristos {
132a7c91847Schristos struct sockaddr_in6 *sinp = (char *) tmp + sizeof (*tmp);
133a7c91847Schristos
134a7c91847Schristos if (se)
135a7c91847Schristos sinp->sin6_port = se->s_port;
136a7c91847Schristos
137a7c91847Schristos if (he->h_length != sizeof (sinp->sin6_addr))
138a7c91847Schristos return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
139a7c91847Schristos
140a7c91847Schristos memcpy (&sinp->sin6_addr, he->h_addr_list[0], he->h_length);
141a7c91847Schristos
142a7c91847Schristos tmp->ai_addr = (struct sockaddr *) sinp;
143a7c91847Schristos tmp->ai_addrlen = sinlen;
144a7c91847Schristos }
145a7c91847Schristos break;
146a7c91847Schristos #endif
147a7c91847Schristos
148a7c91847Schristos #if HAVE_IPV4
149a7c91847Schristos case PF_INET:
150a7c91847Schristos {
151a7c91847Schristos struct sockaddr_in *sinp = (char *) tmp + sizeof (*tmp);
152a7c91847Schristos
153a7c91847Schristos if (se)
154a7c91847Schristos sinp->sin_port = se->s_port;
155a7c91847Schristos
156a7c91847Schristos if (he->h_length != sizeof (sinp->sin_addr))
157a7c91847Schristos return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
158a7c91847Schristos
159a7c91847Schristos memcpy (&sinp->sin_addr, he->h_addr_list[0], he->h_length);
160a7c91847Schristos
161a7c91847Schristos tmp->ai_addr = (struct sockaddr *) sinp;
162a7c91847Schristos tmp->ai_addrlen = sinlen;
163a7c91847Schristos }
164a7c91847Schristos break;
165a7c91847Schristos #endif
166a7c91847Schristos
167a7c91847Schristos default:
168a7c91847Schristos free (tmp);
169a7c91847Schristos return EAI_NODATA;
170a7c91847Schristos }
171a7c91847Schristos
172a7c91847Schristos if (hints && hints->ai_flags & AI_CANONNAME)
173a7c91847Schristos {
174a7c91847Schristos const char *cn;
175a7c91847Schristos if (he->h_name)
176a7c91847Schristos cn = he->h_name;
177a7c91847Schristos else
178a7c91847Schristos cn = nodename;
179a7c91847Schristos
180a7c91847Schristos tmp->ai_canonname = strdup (cn);
181a7c91847Schristos if (!tmp->ai_canonname)
182a7c91847Schristos {
183a7c91847Schristos free (tmp);
184a7c91847Schristos return EAI_MEMORY;
185a7c91847Schristos }
186a7c91847Schristos }
187a7c91847Schristos
188a7c91847Schristos tmp->ai_protocol = (hints) ? hints->ai_protocol : 0;
189a7c91847Schristos tmp->ai_socktype = (hints) ? hints->ai_socktype : 0;
190a7c91847Schristos tmp->ai_addr->sa_family = he->h_addrtype;
191a7c91847Schristos
192a7c91847Schristos /* FIXME: If more than one address, create linked list of addrinfo's. */
193a7c91847Schristos
194a7c91847Schristos *res = tmp;
195a7c91847Schristos
196a7c91847Schristos return 0;
197a7c91847Schristos }
198a7c91847Schristos
199a7c91847Schristos /* Free `addrinfo' structure AI including associated storage. */
200a7c91847Schristos void
freeaddrinfo(struct addrinfo * ai)201a7c91847Schristos freeaddrinfo (struct addrinfo *ai)
202a7c91847Schristos {
203a7c91847Schristos while (ai)
204a7c91847Schristos {
205a7c91847Schristos struct addrinfo *cur;
206a7c91847Schristos
207a7c91847Schristos cur = ai;
208a7c91847Schristos ai = ai->ai_next;
209a7c91847Schristos
210a7c91847Schristos if (cur->ai_canonname) free (cur->ai_canonname);
211a7c91847Schristos free (cur);
212a7c91847Schristos }
213a7c91847Schristos }
214