blob: 4e35b4dfebff19220bcf7f52615ed2c024696317 [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 Kongfdb29632015-12-22 17:07:23 +000021#include <linux/if_packet.h>
Elliott Hughes5d843732016-01-11 21:30:07 -080022#include <net/ethernet.h>
23#include <net/if.h>
24#include <netdb.h>
Yi Kongfdb29632015-12-22 17:07:23 +000025#include <netinet/in.h>
26
Elliott Hughes9cddb482016-01-04 20:38:05 +000027TEST(ifaddrs, freeifaddrs_null) {
28 freeifaddrs(nullptr);
29}
30
31TEST(ifaddrs, getifaddrs_smoke) {
32 ifaddrs* addrs = nullptr;
33
34 ASSERT_EQ(0, getifaddrs(&addrs));
35 ASSERT_TRUE(addrs != nullptr);
36
Yi Kongfdb29632015-12-22 17:07:23 +000037 // We can't 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.
39 ifaddrs* lo_inet4 = nullptr;
40 ifaddrs* lo_inet6 = nullptr;
41 ifaddrs* lo_packet = nullptr;
Elliott Hughes9cddb482016-01-04 20:38:05 +000042 for (ifaddrs* addr = addrs; addr != nullptr; addr = addr->ifa_next) {
Yi Kongfdb29632015-12-22 17:07:23 +000043 if (addr->ifa_name && strcmp(addr->ifa_name, "lo") == 0) {
44 if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_INET) lo_inet4 = addr;
45 else if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_INET6) lo_inet6 = addr;
46 else if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_PACKET) lo_packet = addr;
47 }
Elliott Hughes9cddb482016-01-04 20:38:05 +000048 }
Yi Kongfdb29632015-12-22 17:07:23 +000049
50 // Does the IPv4 entry look right?
51 ASSERT_TRUE(lo_inet4 != nullptr);
52 const sockaddr_in* sa_inet4 = reinterpret_cast<const sockaddr_in*>(lo_inet4->ifa_addr);
53 ASSERT_TRUE(ntohl(sa_inet4->sin_addr.s_addr) == INADDR_LOOPBACK);
54
55 // Does the IPv6 entry look right?
56 ASSERT_TRUE(lo_inet6 != nullptr);
57 const sockaddr_in6* sa_inet6 = reinterpret_cast<const sockaddr_in6*>(lo_inet6->ifa_addr);
58 ASSERT_TRUE(IN6_IS_ADDR_LOOPBACK(&sa_inet6->sin6_addr));
59
60 // Does the AF_PACKET entry look right?
61 ASSERT_TRUE(lo_packet != nullptr);
62 const sockaddr_ll* sa_ll = reinterpret_cast<const sockaddr_ll*>(lo_packet->ifa_addr);
63 ASSERT_EQ(6, sa_ll->sll_halen);
Elliott Hughes9cddb482016-01-04 20:38:05 +000064
65 freeifaddrs(addrs);
66}
Elliott Hughes5d843732016-01-11 21:30:07 -080067
68static void print_sockaddr_ll(const char* what, const sockaddr* p) {
69 const sockaddr_ll* s = reinterpret_cast<const sockaddr_ll*>(p);
Elliott Hughesed57b982016-01-15 21:02:56 -080070 printf("\t\t%s\t", what);
Elliott Hughes5d843732016-01-11 21:30:07 -080071 for (int i = 0; i < s->sll_halen; ++i) {
72 if (i > 0) printf(":");
73 printf("%02X", s->sll_addr[i]);
74 }
75 printf(" (%d bytes)\n", s->sll_halen);
76}
77
78static void print_sockaddr_inet(const char* what, const sockaddr* addr) {
79 char host[NI_MAXHOST];
80 int family = addr->sa_family;
81 int error = getnameinfo(addr,
82 (family == AF_INET) ? sizeof(sockaddr_in) : sizeof(sockaddr_in6),
83 host, NI_MAXHOST, nullptr, 0, NI_NUMERICHOST);
84 if (error != 0) {
85 printf("%d getnameinfo() failed: %s\n", family, gai_strerror(error));
86 strcpy(host, "???");
87 }
Elliott Hughesed57b982016-01-15 21:02:56 -080088 printf("\t\t%s: <%s>\n", what, host);
Elliott Hughes5d843732016-01-11 21:30:07 -080089}
90
91static const char* family_to_name(int family) {
92 if (family == AF_INET) return "AF_INET";
93 if (family == AF_INET6) return "AF_INET6";
94 if (family == AF_PACKET) return "AF_PACKET";
95 if (family == AF_UNSPEC) return "AF_UNSPEC";
96 return "?";
97}
98
99// Not really a test, but a useful debugging tool.
100TEST(ifaddrs, dump) {
101 ifaddrs* addrs;
102 ASSERT_EQ(0, getifaddrs(&addrs));
103
104 for (ifaddrs* ifa = addrs; ifa != nullptr; ifa = ifa->ifa_next) {
105 int family = ifa->ifa_addr ? ifa->ifa_addr->sa_family :
106 ifa->ifa_broadaddr ? ifa->ifa_broadaddr->sa_family : AF_UNSPEC;
107
Elliott Hughesed57b982016-01-15 21:02:56 -0800108 printf("\t%s\n"
109 "\t\t%s (%d) flags=%#x\n",
Elliott Hughes5d843732016-01-11 21:30:07 -0800110 ifa->ifa_name, family_to_name(family), family, ifa->ifa_flags);
111
112 if (family == AF_PACKET) {
113 if (ifa->ifa_addr) print_sockaddr_ll("hwaddr", ifa->ifa_addr);
114 if (ifa->ifa_broadaddr) print_sockaddr_ll("hwbroad", ifa->ifa_addr);
115 } else if (family == AF_INET || family == AF_INET6) {
116 if (ifa->ifa_addr) print_sockaddr_inet("address", ifa->ifa_addr);
117 if (ifa->ifa_broadaddr && (ifa->ifa_flags & (IFF_BROADCAST | IFF_POINTOPOINT)) != 0) {
118 print_sockaddr_inet((ifa->ifa_flags & IFF_BROADCAST) ? "broadcast" : "destination",
119 ifa->ifa_broadaddr);
120 }
121 }
122
123 fflush(stdout);
124 }
125
126 freeifaddrs(addrs);
127}