scclib
Stable Cloud Computing C++ Library
net_if.cc
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <net/net_if.h>
32 #include <gtest/gtest.h>
33 #include <iostream>
34 #include <algorithm>
35 #include <string>
36 #include <net/socket.h>
37 
44 using std::cout;
45 using std::endl;
46 using std::string;
47 using std::vector;
48 using scc::net::NetIf;
50 using scc::net::InetAddr;
53 
54 TEST(interfaces_test, Find_lo)
55 {
56  auto ifs = NetIf::all_interfaces();
57  auto i = std::find_if(ifs.begin(), ifs.end(), [](const auto& x) -> bool
58  {
59  return x.name() == "lo";
60  });
61  ASSERT_NE(i, ifs.end());
62  ASSERT_EQ(i->index(), 1);
63  ASSERT_EQ(i->hw_addr(), "00:00:00:00:00:00");
64  cout << *i << endl;
65 }
66 
67 TEST(interfaces_test, Find_lo_ipv4)
68 {
69  auto ifs = NetIf::all_interfaces();
70  auto i = std::find_if(ifs.begin(), ifs.end(), [](const auto& x) -> bool
71  {
72  return x.name() == "lo";
73  });
74  ASSERT_NE(i, ifs.end());
75  auto sa = std::find_if(i->addrs().begin(), i->addrs().end(), [](const NetIfAddr& ad) -> bool
76  {
77  return ad.test_flags(InetAddrFlag::ipv4|InetAddrFlag::loopback);
78  });
79  ASSERT_NE(sa, i->addrs().end());
80  cout << *i << endl;
81 }
82 
83 TEST(interfaces_test, find_lo_ipv6)
84 {
85  auto ifs = NetIf::all_interfaces();
86  auto i = std::find_if(ifs.begin(), ifs.end(), [](const auto& x) -> bool
87  {
88  return x.name() == "lo";
89  });
90  ASSERT_NE(i, ifs.end());
91  auto sa = std::find_if(i->addrs().begin(), i->addrs().end(), [](const NetIfAddr& ad) -> bool
92  {
93  return ad.test_flags(InetAddrFlag::ipv6|InetAddrFlag::loopback);
94  });
95  ASSERT_NE(sa, i->addrs().end());
96  cout << *i << endl;
97 }
98 
99 TEST(interfaces_test, find_unicast_ipv4)
100 {
101  bool found = false;
102  for (auto& i : NetIf::all_interfaces())
103  {
104  for (auto& j : i.addrs())
105  {
106  if ((j.test_flags(InetAddrFlag::ipv4|InetAddrFlag::unicast)))
107  {
108  found = true;
109  cout << j << endl;
110  }
111  }
112  }
113  EXPECT_TRUE(found);
114 }
115 
116 TEST(interfaces_test, find_unicast_ipv6)
117 {
118  bool found = false;
119  for (auto& i : NetIf::all_interfaces())
120  {
121  for (auto& j : i.addrs())
122  {
123  if ((j.test_flags(InetAddrFlag::ipv6|InetAddrFlag::unicast)))
124  {
125  found = true;
126  cout << j << endl;
127  }
128  }
129  }
130  EXPECT_TRUE(found);
131 }
132 
133 TEST(interfaces_test, iterate_addrs_test_local)
134 {
135  auto ifs = NetIf::all_interfaces();
136  ASSERT_TRUE(ifs.size() > 0); // there is always a lo
137  for (auto& i : ifs)
138  {
139  cout << i << endl;
140 
141  for (auto& j : i.addrs())
142  {
143  if ((j.test_flags(InetAddrFlag::link_local)))
144  {
145  cout << "*found link local address: " << j << endl;
146  ASSERT_EQ(j.scope_id(), i.index());
147  }
148  }
149  }
150 }
151 
152 TEST(host_addrs, local_addrs)
153 {
154  vector<string> hosts({
155  "localhost",
156  "127.0.0.1",
157  "::1",
158  "::"
159  });
160 
161  for (auto& h : hosts)
162  {
163  auto v = NetIf::host_addrs(h);
164  cout << "host " << h << " for all returned " << v.size() << " entries" << endl;
165  for (auto& i : v)
166  {
167  cout << i << endl;
168  }
169  ASSERT_GT(v.size(), 0);
170  v = NetIf::host_addrs(h, NetIf::SocketType::tcp_stream);
171  cout << "host " << h << " for tcp returned " << v.size() << " entries" << endl;
172  for (auto& i : v)
173  {
174  cout << i << endl;
175  }
176  ASSERT_GT(v.size(), 0);
177  v = NetIf::host_addrs(h, NetIf::SocketType::udp_datagram);
178  cout << "host " << h << " for udp returned " << v.size() << " entries" << endl;
179  for (auto& i : v)
180  {
181  cout << i << endl;
182  }
183  ASSERT_GT(v.size(), 0);
184  }
185  auto ifs = NetIf::all_interfaces();
186  for (auto& i : ifs)
187  {
188  if (i.test_flags(NetIfFlag::if_up|NetIfFlag::if_running))
189  {
190  cout << i << endl;
191  for (auto& a : i.addrs())
192  {
193  auto v = NetIf::host_addrs(a.host());
194  ASSERT_GT(v.size(), 0);
195  }
196  }
197  }
198 }
199 
Ipv6 internet address.
Definition: inet.h:120
Named address within an interface.
Definition: net_if.h:57
A network interface.
Definition: net_if.h:90
NetIfFlag
Interface flags.
Definition: net_if.h:71
@ if_running
Resources allocated.
Definition: net_if.h:76
@ if_up
Interface is running.
Definition: net_if.h:72
InetAddrFlag
Internet address flags.
Definition: inet.h:74
@ unicast
Unicast address.
Definition: inet.h:82
@ link_local
Traffic is restricted to the local link.
Definition: inet.h:87
@ ipv4
IPv4.
Definition: inet.h:76
@ ipv6
IPv6.
Definition: inet.h:77
Internet network interface utility.
Low-level tcp and udp sockets.
TEST(inet_example, client_server_stream_test)
[Inet client server]
Definition: inet.cc:521