xref: /openbsd-src/lib/libc/asr/getaddrinfo.c (revision 9936a0e983044822375a0d5cdebec20c3623866c)
1*9936a0e9Seric /*	$OpenBSD: getaddrinfo.c,v 1.9 2015/10/08 14:08:44 eric Exp $	*/
28082e013Seric /*
38082e013Seric  * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
48082e013Seric  *
58082e013Seric  * Permission to use, copy, modify, and distribute this software for any
68082e013Seric  * purpose with or without fee is hereby granted, provided that the above
78082e013Seric  * copyright notice and this permission notice appear in all copies.
88082e013Seric  *
98082e013Seric  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
108082e013Seric  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
118082e013Seric  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
128082e013Seric  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
138082e013Seric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
148082e013Seric  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
158082e013Seric  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
168082e013Seric  */
178082e013Seric 
188082e013Seric #include <sys/types.h>
19d216d6b1Seric #include <sys/socket.h>
208082e013Seric #include <netinet/in.h>
21d216d6b1Seric #include <netdb.h>
228082e013Seric 
23d216d6b1Seric #include <asr.h>
248082e013Seric #include <errno.h>
258082e013Seric #include <resolv.h>
268082e013Seric 
278082e013Seric int
getaddrinfo(const char * hostname,const char * servname,const struct addrinfo * hints,struct addrinfo ** res)288082e013Seric getaddrinfo(const char *hostname, const char *servname,
298082e013Seric     const struct addrinfo *hints, struct addrinfo **res)
308082e013Seric {
315be03f8fSeric 	struct asr_query *as;
325be03f8fSeric 	struct asr_result ar;
338082e013Seric 	int		 saved_errno = errno;
348082e013Seric 
35*9936a0e9Seric 	if (hints == NULL || (hints->ai_flags & AI_NUMERICHOST) == 0)
361ed934d0Seric 		res_init();
371ed934d0Seric 
388082e013Seric 	as = getaddrinfo_async(hostname, servname, hints, NULL);
398082e013Seric 	if (as == NULL) {
408082e013Seric 		if (errno == ENOMEM) {
418082e013Seric 			errno = saved_errno;
428082e013Seric 			return (EAI_MEMORY);
438082e013Seric 		}
448082e013Seric 		return (EAI_SYSTEM);
458082e013Seric 	}
468082e013Seric 
475be03f8fSeric 	asr_run_sync(as, &ar);
488082e013Seric 
498082e013Seric 	*res = ar.ar_addrinfo;
508082e013Seric 	if (ar.ar_gai_errno == EAI_SYSTEM)
518082e013Seric 		errno = ar.ar_errno;
528082e013Seric 
538082e013Seric 	return (ar.ar_gai_errno);
548082e013Seric }
5506201dd4Sguenther DEF_WEAK(getaddrinfo);
56