cifs-utils: bump version to 7.0
[cifs-utils.git] / resolve_host.c
1 /*
2  * resolving DNS hostname routine
3  *
4  * Copyright (C) 2010 Jeff Layton (jlayton@samba.org)
5  * Copyright (C) 2010 Igor Druzhinin (jaxbrigs@gmail.com)
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif /* HAVE_CONFIG_H */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <netdb.h>
30 #include "mount.h"
31 #include "util.h"
32 #include "resolve_host.h"
33
34 /*
35  * resolve hostname to comma-separated list of address(es)
36  */
37 int resolve_host(const char *host, char *addrstr)
38 {
39         int rc;
40         /* 10 for max width of decimal scopeid */
41         char tmpbuf[NI_MAXHOST + 1 + 10 + 1];
42         const char *ipaddr;
43         size_t len;
44         struct addrinfo *addrlist, *addr;
45         struct sockaddr_in *sin;
46         struct sockaddr_in6 *sin6;
47
48         rc = getaddrinfo(host, NULL, NULL, &addrlist);
49         if (rc != 0)
50                 return EX_USAGE;
51
52         addr = addrlist;
53         while (addr) {
54                 /* skip non-TCP entries */
55                 if (addr->ai_socktype != SOCK_STREAM ||
56                     addr->ai_protocol != IPPROTO_TCP) {
57                         addr = addr->ai_next;
58                         continue;
59                 }
60
61                 switch (addr->ai_addr->sa_family) {
62                 case AF_INET6:
63                         sin6 = (struct sockaddr_in6 *)addr->ai_addr;
64                         ipaddr = inet_ntop(AF_INET6, &sin6->sin6_addr, tmpbuf,
65                                            sizeof(tmpbuf));
66                         if (!ipaddr) {
67                                 rc = EX_SYSERR;
68                                 goto resolve_host_out;
69                         }
70
71                         if (sin6->sin6_scope_id) {
72                                 len = strnlen(tmpbuf, sizeof(tmpbuf));
73                                 snprintf(tmpbuf + len, sizeof(tmpbuf) - len, "%%%u",
74                                          sin6->sin6_scope_id);
75                         }
76                         break;
77                 case AF_INET:
78                         sin = (struct sockaddr_in *)addr->ai_addr;
79                         ipaddr = inet_ntop(AF_INET, &sin->sin_addr, tmpbuf,
80                                            sizeof(tmpbuf));
81                         if (!ipaddr) {
82                                 rc = EX_SYSERR;
83                                 goto resolve_host_out;
84                         }
85
86                         break;
87                 default:
88                         addr = addr->ai_next;
89                         continue;
90                 }
91
92                 if (addr == addrlist)
93                         *addrstr = '\0';
94                 else
95                         strlcat(addrstr, ",", MAX_ADDR_LIST_LEN);
96
97                 strlcat(addrstr, tmpbuf, MAX_ADDR_LIST_LEN);
98                 addr = addr->ai_next;
99         }
100
101 resolve_host_out:
102         freeaddrinfo(addrlist);
103         return rc;
104 }