|
Packit Service |
09cdfc |
/*
|
|
Packit Service |
09cdfc |
* resolving DNS hostname routine
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* Copyright (C) 2010 Jeff Layton (jlayton@samba.org)
|
|
Packit Service |
09cdfc |
* Copyright (C) 2010 Igor Druzhinin (jaxbrigs@gmail.com)
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* This program is free software; you can redistribute it and/or modify
|
|
Packit Service |
09cdfc |
* it under the terms of the GNU General Public License as published by
|
|
Packit Service |
09cdfc |
* the Free Software Foundation; either version 3 of the License, or
|
|
Packit Service |
09cdfc |
* (at your option) any later version.
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
09cdfc |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
09cdfc |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
09cdfc |
* GNU General Public License for more details.
|
|
Packit Service |
09cdfc |
*
|
|
Packit Service |
09cdfc |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
09cdfc |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
#ifdef HAVE_CONFIG_H
|
|
Packit Service |
09cdfc |
#include "config.h"
|
|
Packit Service |
09cdfc |
#endif /* HAVE_CONFIG_H */
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
#include <stdio.h>
|
|
Packit Service |
09cdfc |
#include <string.h>
|
|
Packit Service |
09cdfc |
#include <sys/socket.h>
|
|
Packit Service |
09cdfc |
#include <arpa/inet.h>
|
|
Packit Service |
09cdfc |
#include <netdb.h>
|
|
Packit Service |
09cdfc |
#include "mount.h"
|
|
Packit Service |
09cdfc |
#include "util.h"
|
|
Packit Service |
09cdfc |
#include "resolve_host.h"
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
/*
|
|
Packit Service |
09cdfc |
* resolve hostname to comma-separated list of address(es)
|
|
Packit Service |
09cdfc |
*/
|
|
Packit Service |
09cdfc |
int resolve_host(const char *host, char *addrstr)
|
|
Packit Service |
09cdfc |
{
|
|
Packit Service |
09cdfc |
int rc;
|
|
Packit Service |
09cdfc |
/* 10 for max width of decimal scopeid */
|
|
Packit Service |
09cdfc |
char tmpbuf[NI_MAXHOST + 1 + 10 + 1];
|
|
Packit Service |
09cdfc |
const char *ipaddr;
|
|
Packit Service |
09cdfc |
size_t len;
|
|
Packit Service |
09cdfc |
struct addrinfo *addrlist, *addr;
|
|
Packit Service |
09cdfc |
struct sockaddr_in *sin;
|
|
Packit Service |
09cdfc |
struct sockaddr_in6 *sin6;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
rc = getaddrinfo(host, NULL, NULL, &addrlist);
|
|
Packit Service |
09cdfc |
if (rc != 0)
|
|
Packit Service |
09cdfc |
return EX_USAGE;
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
addr = addrlist;
|
|
Packit Service |
09cdfc |
while (addr) {
|
|
Packit Service |
09cdfc |
/* skip non-TCP entries */
|
|
Packit Service |
09cdfc |
if (addr->ai_socktype != SOCK_STREAM ||
|
|
Packit Service |
09cdfc |
addr->ai_protocol != IPPROTO_TCP) {
|
|
Packit Service |
09cdfc |
addr = addr->ai_next;
|
|
Packit Service |
09cdfc |
continue;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
switch (addr->ai_addr->sa_family) {
|
|
Packit Service |
09cdfc |
case AF_INET6:
|
|
Packit Service |
09cdfc |
sin6 = (struct sockaddr_in6 *)addr->ai_addr;
|
|
Packit Service |
09cdfc |
ipaddr = inet_ntop(AF_INET6, &sin6->sin6_addr, tmpbuf,
|
|
Packit Service |
09cdfc |
sizeof(tmpbuf));
|
|
Packit Service |
09cdfc |
if (!ipaddr) {
|
|
Packit Service |
09cdfc |
rc = EX_SYSERR;
|
|
Packit Service |
09cdfc |
goto resolve_host_out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (sin6->sin6_scope_id) {
|
|
Packit Service |
09cdfc |
len = strnlen(tmpbuf, sizeof(tmpbuf));
|
|
Packit Service |
09cdfc |
snprintf(tmpbuf + len, sizeof(tmpbuf) - len, "%%%u",
|
|
Packit Service |
09cdfc |
sin6->sin6_scope_id);
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
case AF_INET:
|
|
Packit Service |
09cdfc |
sin = (struct sockaddr_in *)addr->ai_addr;
|
|
Packit Service |
09cdfc |
ipaddr = inet_ntop(AF_INET, &sin->sin_addr, tmpbuf,
|
|
Packit Service |
09cdfc |
sizeof(tmpbuf));
|
|
Packit Service |
09cdfc |
if (!ipaddr) {
|
|
Packit Service |
09cdfc |
rc = EX_SYSERR;
|
|
Packit Service |
09cdfc |
goto resolve_host_out;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
break;
|
|
Packit Service |
09cdfc |
default:
|
|
Packit Service |
09cdfc |
addr = addr->ai_next;
|
|
Packit Service |
09cdfc |
continue;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
if (addr == addrlist)
|
|
Packit Service |
09cdfc |
*addrstr = '\0';
|
|
Packit Service |
09cdfc |
else
|
|
Packit Service |
09cdfc |
strlcat(addrstr, ",", MAX_ADDR_LIST_LEN);
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
strlcat(addrstr, tmpbuf, MAX_ADDR_LIST_LEN);
|
|
Packit Service |
09cdfc |
addr = addr->ai_next;
|
|
Packit Service |
09cdfc |
}
|
|
Packit Service |
09cdfc |
|
|
Packit Service |
09cdfc |
resolve_host_out:
|
|
Packit Service |
09cdfc |
freeaddrinfo(addrlist);
|
|
Packit Service |
09cdfc |
return rc;
|
|
Packit Service |
09cdfc |
}
|