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