1 /* $OpenBSD: gethostnamadr.c,v 1.13 2015/09/14 07:38:37 guenther Exp $ */
3 * Copyright (c) 2012,2013 Eric Faurot <eric@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/param.h> /* ALIGN */
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
31 static int _gethostbyname(const char *, int, struct hostent *, char *, size_t,
33 static int _fillhostent(const struct hostent *, struct hostent *, char *,
36 static struct hostent _hostent;
37 static char _entbuf[4096];
39 static char *_empty[] = { NULL, };
42 _fillhostent(const struct hostent *h, struct hostent *r, char *buf, size_t len)
44 char **ptr, *end, *pos;
50 r->h_aliases = _empty;
51 r->h_addr_list = _empty;
54 ptr = (char **)ALIGN(buf);
56 if ((char *)ptr >= end)
59 for (naliases = 0; h->h_aliases[naliases]; naliases++)
61 for (naddrs = 0; h->h_addr_list[naddrs]; naddrs++)
64 pos = (char *)(ptr + (naliases + 1) + (naddrs + 1));
69 r->h_addrtype = h->h_addrtype;
70 r->h_length = h->h_length;
72 r->h_addr_list = ptr + naliases + 1;
74 n = strlcpy(pos, h->h_name, end - pos);
80 for (i = 0; i < naliases; i++) {
81 n = strlcpy(pos, h->h_aliases[i], end - pos);
84 r->h_aliases[i] = pos;
88 pos = (char *)ALIGN(pos);
92 for (i = 0; i < naddrs; i++) {
93 if (r->h_length > end - pos)
95 memmove(pos, h->h_addr_list[i], r->h_length);
96 r->h_addr_list[i] = pos;
104 _gethostbyname(const char *name, int af, struct hostent *ret, char *buf,
105 size_t buflen, int *h_errnop)
107 struct asr_query *as;
108 struct asr_result ar;
112 as = gethostbyname_async(name, NULL);
114 as = gethostbyname2_async(name, af, NULL);
119 asr_run_sync(as, &ar);
122 *h_errnop = ar.ar_h_errno;
123 if (ar.ar_hostent == NULL)
126 r = _fillhostent(ar.ar_hostent, ret, buf, buflen);
133 gethostbyname(const char *name)
139 if (_res.options & RES_USE_INET6 &&
140 (h = gethostbyname2(name, AF_INET6)))
143 return gethostbyname2(name, AF_INET);
145 DEF_WEAK(gethostbyname);
148 gethostbyname2(const char *name, int af)
154 r = _gethostbyname(name, af, &_hostent, _entbuf, sizeof(_entbuf),
157 h_errno = NETDB_INTERNAL;
166 DEF_WEAK(gethostbyname2);
169 gethostbyaddr(const void *addr, socklen_t len, int af)
171 struct asr_query *as;
172 struct asr_result ar;
177 as = gethostbyaddr_async(addr, len, af, NULL);
179 h_errno = NETDB_INTERNAL;
183 asr_run_sync(as, &ar);
186 h_errno = ar.ar_h_errno;
187 if (ar.ar_hostent == NULL)
190 r = _fillhostent(ar.ar_hostent, &_hostent, _entbuf, sizeof(_entbuf));
194 h_errno = NETDB_INTERNAL;