1/*
2 * Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <config.h>
35
36#include "roken.h"
37
38/*
39 * uses hints->ai_socktype and hints->ai_protocol
40 */
41
42static int
43get_port_protocol_socktype (const char *servname,
44			    const struct addrinfo *hints,
45			    int *port,
46			    int *protocol,
47			    int *socktype)
48{
49    struct servent *se;
50    const char *proto_str = NULL;
51
52    *socktype = 0;
53
54    if (hints != NULL && hints->ai_protocol != 0) {
55	struct protoent *protoent = getprotobynumber (hints->ai_protocol);
56
57	if (protoent == NULL)
58	    return EAI_SOCKTYPE; /* XXX */
59
60	proto_str = protoent->p_name;
61	*protocol = protoent->p_proto;
62    }
63
64    if (hints != NULL)
65	*socktype = hints->ai_socktype;
66
67    if (*socktype == SOCK_STREAM) {
68	se = getservbyname (servname, proto_str ? proto_str : "tcp");
69	if (proto_str == NULL)
70	    *protocol = IPPROTO_TCP;
71    } else if (*socktype == SOCK_DGRAM) {
72	se = getservbyname (servname, proto_str ? proto_str : "udp");
73	if (proto_str == NULL)
74	    *protocol = IPPROTO_UDP;
75    } else if (*socktype == 0) {
76	if (proto_str != NULL) {
77	    se = getservbyname (servname, proto_str);
78	} else {
79	    se = getservbyname (servname, "tcp");
80	    *protocol = IPPROTO_TCP;
81	    *socktype = SOCK_STREAM;
82	    if (se == NULL) {
83		se = getservbyname (servname, "udp");
84		*protocol = IPPROTO_UDP;
85		*socktype = SOCK_DGRAM;
86	    }
87	}
88    } else
89	return EAI_SOCKTYPE;
90
91    if (se == NULL) {
92	char *endstr;
93
94	*port = htons(strtol (servname, &endstr, 10));
95	if (servname == endstr)
96	    return EAI_NONAME;
97    } else {
98	*port = se->s_port;
99    }
100    return 0;
101}
102
103static int
104add_one (int port, int protocol, int socktype,
105	 struct addrinfo ***ptr,
106	 int (*func)(struct addrinfo *, void *data, int port),
107	 void *data,
108	 char *canonname)
109{
110    struct addrinfo *a;
111    int ret;
112
113    a = malloc (sizeof (*a));
114    if (a == NULL)
115	return EAI_MEMORY;
116    memset (a, 0, sizeof(*a));
117    a->ai_flags     = 0;
118    a->ai_next      = NULL;
119    a->ai_protocol  = protocol;
120    a->ai_socktype  = socktype;
121    a->ai_canonname = canonname;
122    ret = (*func)(a, data, port);
123    if (ret) {
124	free (a);
125	return ret;
126    }
127    **ptr = a;
128    *ptr = &a->ai_next;
129    return 0;
130}
131
132static int
133const_v4 (struct addrinfo *a, void *data, int port)
134{
135    struct sockaddr_in *sin4;
136    struct in_addr *addr = (struct in_addr *)data;
137
138    a->ai_family  = PF_INET;
139    a->ai_addrlen = sizeof(*sin4);
140    a->ai_addr    = malloc (sizeof(*sin4));
141    if (a->ai_addr == NULL)
142	return EAI_MEMORY;
143    sin4 = (struct sockaddr_in *)a->ai_addr;
144    memset (sin4, 0, sizeof(*sin4));
145    sin4->sin_family = AF_INET;
146    sin4->sin_port   = port;
147    sin4->sin_addr   = *addr;
148    return 0;
149}
150
151#ifdef HAVE_IPV6
152static int
153const_v6 (struct addrinfo *a, void *data, int port)
154{
155    struct sockaddr_in6 *sin6;
156    struct in6_addr *addr = (struct in6_addr *)data;
157
158    a->ai_family  = PF_INET6;
159    a->ai_addrlen = sizeof(*sin6);
160    a->ai_addr    = malloc (sizeof(*sin6));
161    if (a->ai_addr == NULL)
162	return EAI_MEMORY;
163    sin6 = (struct sockaddr_in6 *)a->ai_addr;
164    memset (sin6, 0, sizeof(*sin6));
165    sin6->sin6_family = AF_INET6;
166    sin6->sin6_port   = port;
167    sin6->sin6_addr   = *addr;
168    return 0;
169}
170#endif
171
172/* this is mostly a hack for some versions of AIX that has a prototype
173   for in6addr_loopback but no actual symbol in libc */
174#if defined(HAVE_IPV6) && !defined(HAVE_IN6ADDR_LOOPBACK) && defined(IN6ADDR_LOOPBACK_INIT)
175#define in6addr_loopback _roken_in6addr_loopback
176struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
177#endif
178
179static int
180get_null (const struct addrinfo *hints,
181	  int port, int protocol, int socktype,
182	  struct addrinfo **res)
183{
184    struct in_addr v4_addr;
185#ifdef HAVE_IPV6
186    struct in6_addr v6_addr;
187#endif
188    struct addrinfo *first = NULL;
189    struct addrinfo **current = &first;
190    int family = PF_UNSPEC;
191    int ret;
192
193    if (hints != NULL)
194	family = hints->ai_family;
195
196    if (hints && hints->ai_flags & AI_PASSIVE) {
197	v4_addr.s_addr = INADDR_ANY;
198#ifdef HAVE_IPV6
199	v6_addr        = in6addr_any;
200#endif
201    } else {
202	v4_addr.s_addr = htonl(INADDR_LOOPBACK);
203#ifdef HAVE_IPV6
204	v6_addr        = in6addr_loopback;
205#endif
206    }
207
208#ifdef HAVE_IPV6
209    if (family == PF_INET6 || family == PF_UNSPEC) {
210	ret = add_one (port, protocol, socktype,
211		       &current, const_v6, &v6_addr, NULL);
212    }
213#endif
214    if (family == PF_INET || family == PF_UNSPEC) {
215	ret = add_one (port, protocol, socktype,
216		       &current, const_v4, &v4_addr, NULL);
217    }
218    *res = first;
219    return 0;
220}
221
222static int
223add_hostent (int port, int protocol, int socktype,
224	     struct addrinfo ***current,
225	     int (*func)(struct addrinfo *, void *data, int port),
226	     struct hostent *he, int *flags)
227{
228    int ret;
229    char *canonname = NULL;
230    char **h;
231
232    if (*flags & AI_CANONNAME) {
233	struct hostent *he2 = NULL;
234	const char *tmp_canon;
235
236	tmp_canon = hostent_find_fqdn (he);
237	if (strchr (tmp_canon, '.') == NULL) {
238	    int error;
239
240	    he2 = getipnodebyaddr (he->h_addr_list[0], he->h_length,
241				   he->h_addrtype, &error);
242	    if (he2 != NULL) {
243		const char *tmp = hostent_find_fqdn (he2);
244
245		if (strchr (tmp, '.') != NULL)
246		    tmp_canon = tmp;
247	    }
248	}
249
250	canonname = strdup (tmp_canon);
251	if (he2 != NULL)
252	    freehostent (he2);
253	if (canonname == NULL)
254	    return EAI_MEMORY;
255    }
256
257    for (h = he->h_addr_list; *h != NULL; ++h) {
258	ret = add_one (port, protocol, socktype,
259		       current, func, *h, canonname);
260	if (ret)
261	    return ret;
262	if (*flags & AI_CANONNAME) {
263	    *flags &= ~AI_CANONNAME;
264	    canonname = NULL;
265	}
266    }
267    return 0;
268}
269
270static int
271get_number (const char *nodename,
272	    const struct addrinfo *hints,
273	    int port, int protocol, int socktype,
274	    struct addrinfo **res)
275{
276    struct addrinfo *first = NULL;
277    struct addrinfo **current = &first;
278    int family = PF_UNSPEC;
279    int ret;
280
281    if (hints != NULL) {
282	family = hints->ai_family;
283    }
284
285#ifdef HAVE_IPV6
286    if (family == PF_INET6 || family == PF_UNSPEC) {
287	struct in6_addr v6_addr;
288
289	if (inet_pton (PF_INET6, nodename, &v6_addr) == 1) {
290	    ret = add_one (port, protocol, socktype,
291			   &current, const_v6, &v6_addr, NULL);
292	    *res = first;
293	    return ret;
294	}
295    }
296#endif
297    if (family == PF_INET || family == PF_UNSPEC) {
298	struct in_addr v4_addr;
299
300	if (inet_pton (PF_INET, nodename, &v4_addr) == 1) {
301	    ret = add_one (port, protocol, socktype,
302			   &current, const_v4, &v4_addr, NULL);
303	    *res = first;
304	    return ret;
305	}
306    }
307    return EAI_NONAME;
308}
309
310static int
311get_nodes (const char *nodename,
312	   const struct addrinfo *hints,
313	   int port, int protocol, int socktype,
314	   struct addrinfo **res)
315{
316    struct addrinfo *first = NULL;
317    struct addrinfo **current = &first;
318    int family = PF_UNSPEC;
319    int flags  = 0;
320    int ret = EAI_NONAME;
321    int error;
322
323    if (hints != NULL) {
324	family = hints->ai_family;
325	flags  = hints->ai_flags;
326    }
327
328#ifdef HAVE_IPV6
329    if (family == PF_INET6 || family == PF_UNSPEC) {
330	struct hostent *he;
331
332	he = getipnodebyname (nodename, PF_INET6, 0, &error);
333
334	if (he != NULL) {
335	    ret = add_hostent (port, protocol, socktype,
336			       &current, const_v6, he, &flags);
337	    freehostent (he);
338	}
339    }
340#endif
341    if (family == PF_INET || family == PF_UNSPEC) {
342	struct hostent *he;
343
344	he = getipnodebyname (nodename, PF_INET, 0, &error);
345
346	if (he != NULL) {
347	    ret = add_hostent (port, protocol, socktype,
348			       &current, const_v4, he, &flags);
349	    freehostent (he);
350	}
351    }
352    *res = first;
353    return ret;
354}
355
356/*
357 * hints:
358 *
359 * struct addrinfo {
360 *     int    ai_flags;
361 *     int    ai_family;
362 *     int    ai_socktype;
363 *     int    ai_protocol;
364 * ...
365 * };
366 */
367
368ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
369getaddrinfo(const char *nodename,
370	    const char *servname,
371	    const struct addrinfo *hints,
372	    struct addrinfo **res)
373{
374    int ret;
375    int port     = 0;
376    int protocol = 0;
377    int socktype = 0;
378
379    *res = NULL;
380
381    if (servname == NULL && nodename == NULL)
382	return EAI_NONAME;
383
384    if (hints != NULL
385	&& hints->ai_family != PF_UNSPEC
386	&& hints->ai_family != PF_INET
387#ifdef HAVE_IPV6
388	&& hints->ai_family != PF_INET6
389#endif
390	)
391	return EAI_FAMILY;
392
393    if (servname != NULL) {
394	ret = get_port_protocol_socktype (servname, hints,
395					  &port, &protocol, &socktype);
396	if (ret)
397	    return ret;
398    }
399    if (nodename != NULL) {
400	ret = get_number (nodename, hints, port, protocol, socktype, res);
401	if (ret) {
402	    if(hints && hints->ai_flags & AI_NUMERICHOST)
403		ret = EAI_NONAME;
404	    else
405		ret = get_nodes (nodename, hints, port, protocol, socktype,
406				 res);
407	}
408    } else {
409	ret = get_null (hints, port, protocol, socktype, res);
410    }
411    if (ret)
412	freeaddrinfo (*res);
413    return ret;
414}
415