scclib
Stable Cloud Computing C++ Library
net_if.h
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 #ifndef _SCC_NET_IF_H
32 #define _SCC_NET_IF_H
33 
34 #include <vector>
35 #include <string>
36 #include <net/inet.h>
37 
38 namespace scc::net {
39 
56 class NetIfAddr : public InetAddr
57 {
58  std::string m_name;
59 public:
60  NetIfAddr(const std::string& name, const InetAddr& addr) : InetAddr(addr), m_name(name) {}
61 
63  const std::string& if_addr_name() const { return m_name; }
64 
65  std::string str() const;
66 };
67 
71 {
72  if_up= 0x001,
73  if_broadcast= 0x002,
74  if_loopback= 0x004,
75  if_pointtopoint= 0x008,
76  if_running= 0x010,
77  if_noarp= 0x020,
78  if_promisc= 0x040,
79  if_allmulti= 0x080,
80  if_multicast= 0x100,
81  if_dynamic= 0x200,
82  if_echo= 0x400
83 };
84 
89 class NetIf
90 {
91  std::string m_name;
92  std::string m_hwaddr;
93  int m_index;
94  size_t m_speed;
95  size_t m_mtu;
96  int m_flags;
97  std::vector<NetIfAddr> m_addrs;
98 
99  static int parse_flags(unsigned);
100  static int find_index(const std::string&);
101 
102  NetIf(const std::string&, const std::string&, int);
103  friend class NetIfIterator;
104 
105 public:
106  NetIf() {}
107 
110  static std::vector<NetIf> all_interfaces();
111 
112  enum class SocketType
113  {
114  any,
115  tcp_stream,
116  udp_datagram,
117  };
118 
129  static std::vector<InetAddr> host_addrs(const std::string& name, SocketType=SocketType::any);
130 
132  const std::string& name() const { return m_name; }
133 
135  const int index() const { return m_index; }
136 
138  const std::string& hw_addr() const { return m_hwaddr; }
139 
141  const size_t speed() const { return m_speed; }
142 
144  const size_t mtu() const { return m_mtu; }
145 
149  int flags() const { return m_flags; }
150  bool test_flags(int f) const { return (m_flags&f)==f; }
151 
153  const std::vector<NetIfAddr>& addrs() const { return m_addrs; }
154 
155  std::string str() const;
156 };
157 
161 } // namespace
162 
163 std::ostream& operator<<(std::ostream&, const scc::net::NetIf&);
164 std::ostream& operator<<(std::ostream&, const scc::net::NetIfAddr&);
165 
166 #endif
Ipv6 internet address.
Definition: inet.h:120
InetAddr()
ipv6 internet address, initialized with "any" address
Definition: inet.cc:60
Named address within an interface.
Definition: net_if.h:57
std::string str() const
String representation of a network interface address.
Definition: net_if.cc:93
const std::string & if_addr_name() const
The interface address name.
Definition: net_if.h:63
A network interface.
Definition: net_if.h:90
const std::string & hw_addr() const
Hardware address for the interface.
Definition: net_if.h:138
const int index() const
The interface index.
Definition: net_if.h:135
std::string str() const
String representation of network interface.
Definition: net_if.cc:237
const size_t speed() const
Link speed in bytes / second, 0 means loopback.
Definition: net_if.h:141
const size_t mtu() const
Maximum transmission unit.
Definition: net_if.h:144
static std::vector< NetIf > all_interfaces()
List network interfaces on the local system.
Definition: net_if.cc:105
static std::vector< InetAddr > host_addrs(const std::string &name, SocketType=SocketType::any)
List addresses for the specified host (do a name lookup).
Definition: net_if.cc:57
int flags() const
Flags for this interface.
Definition: net_if.h:149
const std::string & name() const
Name of the interface.
Definition: net_if.h:132
const std::vector< NetIfAddr > & addrs() const
Zero or more addresses associated with this interface.
Definition: net_if.h:153
NetIfFlag
Interface flags.
Definition: net_if.h:71
@ if_loopback
Interface is a loopback interface.
Definition: net_if.h:74
@ if_broadcast
Valid broadcast address set.
Definition: net_if.h:73
@ if_multicast
Supports multicast.
Definition: net_if.h:80
@ if_running
Resources allocated.
Definition: net_if.h:76
@ if_noarp
No arp protocol, L2 destination address not set.
Definition: net_if.h:77
@ if_dynamic
The addresses are lost when the interface goes down.
Definition: net_if.h:81
@ if_up
Interface is running.
Definition: net_if.h:72
@ if_pointtopoint
Interface is a point-to-point link.
Definition: net_if.h:75
@ if_allmulti
Receives all multicast packets.
Definition: net_if.h:79
@ if_promisc
Interface is in promiscuous mode.
Definition: net_if.h:78
@ if_echo
Echoes sent packets.
Definition: net_if.h:82
@ any
Any address.
Definition: inet.h:80
Internet tcp and udp networking.
std::ostream & operator<<(std::ostream &, const scc::net::InetAddr &)
Print the socket address details to an output stream.
Definition: inet.cc:320