blob: c3f0273199c95025386da1593a3b7d376cc1a2ba [file] [log] [blame]
Elliott Hughes9cddb482016-01-04 20:38:05 +00001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <ifaddrs.h>
20
Yi Kong64b481c2016-01-13 11:28:14 +000021#include <dirent.h>
Yi Kongfdb29632015-12-22 17:07:23 +000022#include <linux/if_packet.h>
Elliott Hughes5d843732016-01-11 21:30:07 -080023#include <net/ethernet.h>
24#include <net/if.h>
25#include <netdb.h>
Yi Kongfdb29632015-12-22 17:07:23 +000026#include <netinet/in.h>
Yi Kong64b481c2016-01-13 11:28:14 +000027#include <sys/ioctl.h>
28
29#include <algorithm>
30#include <map>
31#include <vector>
Yi Kongfdb29632015-12-22 17:07:23 +000032
Elliott Hughes9cddb482016-01-04 20:38:05 +000033TEST(ifaddrs, freeifaddrs_null) {
34 freeifaddrs(nullptr);
35}
36
Yi Kong64b481c2016-01-13 11:28:14 +000037// We can't statically say much about what network interfaces are available, but we can be pretty
38// sure there's a loopback interface, and that it has IPv4, IPv6, and AF_PACKET entries.
39TEST(ifaddrs, getifaddrs_lo) {
Elliott Hughes9cddb482016-01-04 20:38:05 +000040 ifaddrs* addrs = nullptr;
41
42 ASSERT_EQ(0, getifaddrs(&addrs));
43 ASSERT_TRUE(addrs != nullptr);
44
Yi Kongfdb29632015-12-22 17:07:23 +000045 ifaddrs* lo_inet4 = nullptr;
46 ifaddrs* lo_inet6 = nullptr;
47 ifaddrs* lo_packet = nullptr;
Elliott Hughes9cddb482016-01-04 20:38:05 +000048 for (ifaddrs* addr = addrs; addr != nullptr; addr = addr->ifa_next) {
Yi Kongfdb29632015-12-22 17:07:23 +000049 if (addr->ifa_name && strcmp(addr->ifa_name, "lo") == 0) {
50 if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_INET) lo_inet4 = addr;
51 else if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_INET6) lo_inet6 = addr;
52 else if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_PACKET) lo_packet = addr;
53 }
Elliott Hughes9cddb482016-01-04 20:38:05 +000054 }
Yi Kongfdb29632015-12-22 17:07:23 +000055
56 // Does the IPv4 entry look right?
57 ASSERT_TRUE(lo_inet4 != nullptr);
58 const sockaddr_in* sa_inet4 = reinterpret_cast<const sockaddr_in*>(lo_inet4->ifa_addr);
59 ASSERT_TRUE(ntohl(sa_inet4->sin_addr.s_addr) == INADDR_LOOPBACK);
60
61 // Does the IPv6 entry look right?
62 ASSERT_TRUE(lo_inet6 != nullptr);
63 const sockaddr_in6* sa_inet6 = reinterpret_cast<const sockaddr_in6*>(lo_inet6->ifa_addr);
64 ASSERT_TRUE(IN6_IS_ADDR_LOOPBACK(&sa_inet6->sin6_addr));
65
66 // Does the AF_PACKET entry look right?
67 ASSERT_TRUE(lo_packet != nullptr);
68 const sockaddr_ll* sa_ll = reinterpret_cast<const sockaddr_ll*>(lo_packet->ifa_addr);
69 ASSERT_EQ(6, sa_ll->sll_halen);
Elliott Hughes9cddb482016-01-04 20:38:05 +000070
71 freeifaddrs(addrs);
72}
Elliott Hughes5d843732016-01-11 21:30:07 -080073
Yi Kong64b481c2016-01-13 11:28:14 +000074// Check that getifaddrs sees the same list of interfaces as /sys/class/net.
75TEST(ifaddrs, getifaddrs_interfaces) {
76 std::vector<std::string> ifaddrs_socks;
77 {
78 ifaddrs* addrs;
79 ASSERT_EQ(0, getifaddrs(&addrs));
80
81 for (ifaddrs* addr = addrs; addr != nullptr; addr = addr->ifa_next) {
82 int family = addr->ifa_addr ? addr->ifa_addr->sa_family :
83 addr->ifa_broadaddr ? addr->ifa_broadaddr->sa_family :
84 AF_UNSPEC;
85
86 if (family == AF_PACKET || family == AF_UNSPEC) {
87 ifaddrs_socks.push_back(std::string(addr->ifa_name));
88 }
89 }
90
91 freeifaddrs(addrs);
92 }
93
94 std::vector<std::string> sys_class_net;
95 {
Elliott Hughes7dac8b82016-02-17 14:19:48 -080096 std::unique_ptr<DIR, decltype(&closedir)> d(opendir("/sys/class/net"), closedir);
Yi Kong64b481c2016-01-13 11:28:14 +000097 ASSERT_TRUE(d != nullptr);
98 dirent* dir;
99 while ((dir = readdir(d.get())) != nullptr) {
100 if (dir->d_type == DT_LNK) {
101 sys_class_net.push_back(std::string(dir->d_name));
102 }
103 }
104 }
105
106 ASSERT_TRUE(std::is_permutation(ifaddrs_socks.begin(), ifaddrs_socks.end(),
107 sys_class_net.begin()));
108}
109
110TEST(ifaddrs, getifaddrs_INET) {
Elliott Hughes2d5e21f2016-02-03 07:42:33 -0800111 std::multimap<std::string, in_addr_t> inetaddrs;
112 std::multimap<std::string, in_addr_t> broadinetaddrs;
Yi Kong64b481c2016-01-13 11:28:14 +0000113
Elliott Hughes2d5e21f2016-02-03 07:42:33 -0800114 // Collect the IPv4 addresses for each interface.
115 ifaddrs* addrs;
116 ASSERT_EQ(0, getifaddrs(&addrs));
117 for (ifaddrs* addr = addrs; addr != nullptr; addr = addr->ifa_next) {
118 if (addr->ifa_name && addr->ifa_addr && addr->ifa_addr->sa_family == AF_INET) {
119 auto sock = reinterpret_cast<sockaddr_in*>(addr->ifa_addr);
120 inetaddrs.emplace(std::string(addr->ifa_name), sock->sin_addr.s_addr);
Yi Kong64b481c2016-01-13 11:28:14 +0000121 }
Elliott Hughes2d5e21f2016-02-03 07:42:33 -0800122 if (addr->ifa_name && addr->ifa_broadaddr && addr->ifa_broadaddr->sa_family == AF_INET) {
123 auto sock = reinterpret_cast<sockaddr_in*>(addr->ifa_broadaddr);
124 broadinetaddrs.emplace(std::string(addr->ifa_name), sock->sin_addr.s_addr);
125 }
Yi Kong64b481c2016-01-13 11:28:14 +0000126 }
Elliott Hughes2d5e21f2016-02-03 07:42:33 -0800127 freeifaddrs(addrs);
Yi Kong64b481c2016-01-13 11:28:14 +0000128
Elliott Hughes2d5e21f2016-02-03 07:42:33 -0800129 // Check that the addresses returned by the SIOCGIFADDR and SIOCGIFBRDADDR ioctls
130 // are in our collections.
131 auto check_inet_agrees = [&](std::multimap<std::string, in_addr_t> addrs, int request)->void {
132 for (auto it = addrs.begin(); it != addrs.end(); ) {
133 std::string if_name(it->first);
Yi Kong64b481c2016-01-13 11:28:14 +0000134
Elliott Hughes2d5e21f2016-02-03 07:42:33 -0800135 ifreq ifr;
136 memset(&ifr, 0, sizeof(ifr));
137 ifr.ifr_addr.sa_family = AF_INET;
138 if_name.copy(ifr.ifr_name, IFNAMSIZ - 1);
Yi Kong64b481c2016-01-13 11:28:14 +0000139
Elliott Hughes2d5e21f2016-02-03 07:42:33 -0800140 int fd = socket(AF_INET, SOCK_DGRAM, 0);
141 ASSERT_TRUE(fd != -1);
142 ASSERT_EQ(0, ioctl(fd, request, &ifr)) << if_name << ' ' << strerror(errno);
143 close(fd);
Yi Kong64b481c2016-01-13 11:28:14 +0000144
Elliott Hughes2d5e21f2016-02-03 07:42:33 -0800145 sockaddr_in* sock = reinterpret_cast<sockaddr_in*>(&ifr.ifr_addr);
146 in_addr_t addr = sock->sin_addr.s_addr;
147
148 bool found = false;
149 for (auto ub = addrs.upper_bound(it->first); it != ub; ++it) {
150 if (it->second == addr) {
151 found = true;
Elliott Hughes7dac8b82016-02-17 14:19:48 -0800152 break;
Yi Kong64b481c2016-01-13 11:28:14 +0000153 }
Yi Kong64b481c2016-01-13 11:28:14 +0000154 }
Elliott Hughes2d5e21f2016-02-03 07:42:33 -0800155 EXPECT_TRUE(found) << if_name;
156 }
157 };
Yi Kong64b481c2016-01-13 11:28:14 +0000158
Elliott Hughes2d5e21f2016-02-03 07:42:33 -0800159 check_inet_agrees(inetaddrs, SIOCGIFADDR);
160 check_inet_agrees(broadinetaddrs, SIOCGIFBRDADDR);
Yi Kong64b481c2016-01-13 11:28:14 +0000161}
162
Elliott Hughes5d843732016-01-11 21:30:07 -0800163static void print_sockaddr_ll(const char* what, const sockaddr* p) {
164 const sockaddr_ll* s = reinterpret_cast<const sockaddr_ll*>(p);
Elliott Hughesed57b982016-01-15 21:02:56 -0800165 printf("\t\t%s\t", what);
Elliott Hughes5d843732016-01-11 21:30:07 -0800166 for (int i = 0; i < s->sll_halen; ++i) {
167 if (i > 0) printf(":");
168 printf("%02X", s->sll_addr[i]);
169 }
170 printf(" (%d bytes)\n", s->sll_halen);
171}
172
173static void print_sockaddr_inet(const char* what, const sockaddr* addr) {
174 char host[NI_MAXHOST];
175 int family = addr->sa_family;
176 int error = getnameinfo(addr,
177 (family == AF_INET) ? sizeof(sockaddr_in) : sizeof(sockaddr_in6),
178 host, NI_MAXHOST, nullptr, 0, NI_NUMERICHOST);
179 if (error != 0) {
180 printf("%d getnameinfo() failed: %s\n", family, gai_strerror(error));
181 strcpy(host, "???");
182 }
Elliott Hughesed57b982016-01-15 21:02:56 -0800183 printf("\t\t%s: <%s>\n", what, host);
Elliott Hughes5d843732016-01-11 21:30:07 -0800184}
185
186static const char* family_to_name(int family) {
187 if (family == AF_INET) return "AF_INET";
188 if (family == AF_INET6) return "AF_INET6";
189 if (family == AF_PACKET) return "AF_PACKET";
190 if (family == AF_UNSPEC) return "AF_UNSPEC";
191 return "?";
192}
193
194// Not really a test, but a useful debugging tool.
195TEST(ifaddrs, dump) {
196 ifaddrs* addrs;
197 ASSERT_EQ(0, getifaddrs(&addrs));
198
199 for (ifaddrs* ifa = addrs; ifa != nullptr; ifa = ifa->ifa_next) {
200 int family = ifa->ifa_addr ? ifa->ifa_addr->sa_family :
201 ifa->ifa_broadaddr ? ifa->ifa_broadaddr->sa_family : AF_UNSPEC;
202
Elliott Hughesed57b982016-01-15 21:02:56 -0800203 printf("\t%s\n"
204 "\t\t%s (%d) flags=%#x\n",
Elliott Hughes5d843732016-01-11 21:30:07 -0800205 ifa->ifa_name, family_to_name(family), family, ifa->ifa_flags);
206
207 if (family == AF_PACKET) {
208 if (ifa->ifa_addr) print_sockaddr_ll("hwaddr", ifa->ifa_addr);
209 if (ifa->ifa_broadaddr) print_sockaddr_ll("hwbroad", ifa->ifa_addr);
210 } else if (family == AF_INET || family == AF_INET6) {
211 if (ifa->ifa_addr) print_sockaddr_inet("address", ifa->ifa_addr);
212 if (ifa->ifa_broadaddr && (ifa->ifa_flags & (IFF_BROADCAST | IFF_POINTOPOINT)) != 0) {
213 print_sockaddr_inet((ifa->ifa_flags & IFF_BROADCAST) ? "broadcast" : "destination",
214 ifa->ifa_broadaddr);
215 }
216 }
217
218 fflush(stdout);
219 }
220
221 freeifaddrs(addrs);
222}
Elliott Hughes7dac8b82016-02-17 14:19:48 -0800223
224TEST(ifaddrs, inet6_scope_ids) {
225 ifaddrs* addrs;
226 ASSERT_EQ(0, getifaddrs(&addrs));
227
228 for (ifaddrs* ifa = addrs; ifa != nullptr; ifa = ifa->ifa_next) {
229 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
230 sockaddr_in6* sa6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr);
231 // Any link-local IPv6 address should have a scope id. (http://b/27219454.)
232 // 0 isn't a valid interface index, so that would mean the scope id wasn't set.
233 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) || IN6_IS_ADDR_MC_LINKLOCAL(&sa6->sin6_addr)) {
234 ASSERT_NE(sa6->sin6_scope_id, 0U);
235 }
236 }
237 }
238
239 freeifaddrs(addrs);
240}