2 * resolving DNS hostname routine
4 * Copyright (C) 2010 Jeff Layton (jlayton@samba.org)
5 * Copyright (C) 2010 Igor Druzhinin (jaxbrigs@gmail.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #endif /* HAVE_CONFIG_H */
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
32 #include "resolve_host.h"
35 * resolve hostname to comma-separated list of address(es)
37 int resolve_host(const char *host, char *addrstr)
40 /* 10 for max width of decimal scopeid */
41 char tmpbuf[NI_MAXHOST + 1 + 10 + 1];
44 struct addrinfo *addrlist, *addr;
45 struct sockaddr_in *sin;
46 struct sockaddr_in6 *sin6;
48 rc = getaddrinfo(host, NULL, NULL, &addrlist);
54 /* skip non-TCP entries */
55 if (addr->ai_socktype != SOCK_STREAM ||
56 addr->ai_protocol != IPPROTO_TCP) {
61 switch (addr->ai_addr->sa_family) {
63 sin6 = (struct sockaddr_in6 *)addr->ai_addr;
64 ipaddr = inet_ntop(AF_INET6, &sin6->sin6_addr, tmpbuf,
68 goto resolve_host_out;
71 if (sin6->sin6_scope_id) {
72 len = strnlen(tmpbuf, sizeof(tmpbuf));
73 snprintf(tmpbuf + len, sizeof(tmpbuf) - len, "%%%u",
78 sin = (struct sockaddr_in *)addr->ai_addr;
79 ipaddr = inet_ntop(AF_INET, &sin->sin_addr, tmpbuf,
83 goto resolve_host_out;
95 strlcat(addrstr, ",", MAX_ADDR_LIST_LEN);
97 strlcat(addrstr, tmpbuf, MAX_ADDR_LIST_LEN);
102 freeaddrinfo(addrlist);