join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
interface.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_FABRIC_INTERFACE_HPP
26#define JOIN_FABRIC_INTERFACE_HPP
27
28// libjoin.
29#include <join/mac_address.hpp>
30#include <join/ip_address.hpp>
31#include <join/mutex.hpp>
32
33// C++.
34#include <compare>
35#include <string>
36#include <memory>
37#include <vector>
38
39// C.
40#include <cstdint>
41
42namespace join
43{
45 class InterfaceManager;
46
51 {
52 private:
58 explicit Interface (InterfaceManager* manager, uint32_t index);
59
60 public:
61 using Ptr = std::shared_ptr<Interface>;
62
66 struct Address
67 {
69 uint32_t prefix = 0;
71 };
72
73 using AddressList = std::vector<Address>;
74
78 Interface () = delete;
79
83 ~Interface () = default;
84
89 uint32_t index () const noexcept;
90
95 uint32_t master () const;
96
101 std::string name () const;
102
109 int mtu (uint32_t mtuBytes, bool sync = false) const;
110
115 uint32_t mtu () const;
116
121 std::string kind () const;
122
129 int mac (const MacAddress& macAddress, bool sync = false) const;
130
135 MacAddress mac () const;
136
145 int addAddress (const IpAddress& ipAddress, uint32_t prefix, const IpAddress& broadcast = {},
146 bool sync = false) const;
147
154 int addAddress (const Address& address, bool sync = false) const;
155
164 int removeAddress (const IpAddress& ipAddress, uint32_t prefix, const IpAddress& broadcast = {},
165 bool sync = false) const;
166
173 int removeAddress (const Address& address, bool sync = false) const;
174
179 AddressList addressList () const;
180
186 bool hasAddress (const IpAddress& ipAddress) const;
187
192 bool hasLocalAddress () const;
193
200 int addToBridge (uint32_t masterIndex, bool sync = false) const;
201
208 int addToBridge (const std::string& masterName, bool sync = false) const;
209
215 int removeFromBridge (bool sync = false) const;
216
221 uint32_t flags () const;
222
229 int enable (bool enabled = true, bool sync = false) const;
230
235 bool isEnabled () const;
236
241 bool isRunning () const;
242
247 bool isLoopback () const;
248
253 bool isPointToPoint () const;
254
259 bool isDummy () const;
260
265 bool isBridge () const;
266
271 bool isVlan () const;
272
277 bool isVeth () const;
278
283 bool isGre () const;
284
289 bool isTun () const;
290
295 bool supportsBroadcast () const;
296
301 bool supportsMulticast () const;
302
307 bool supportsIpv4 () const;
308
313 bool supportsIpv6 () const;
314
315 private:
317 InterfaceManager* _manager = nullptr;
318
320 uint32_t _index = 0;
321
323 uint32_t _master = 0;
324
326 std::string _name;
327
329 std::string _kind;
330
332 uint32_t _mtu = 0;
333
335 uint32_t _flags = 0;
336
338 MacAddress _mac;
339
341 AddressList _addresses;
342
344 mutable Mutex _mutex;
345
346 // friendship with equal operator.
347 friend bool operator== (const Interface::Ptr& lhs, const Interface::Ptr& rhs) noexcept;
348
349 // friendship with three-way comparison operator.
350 friend std::strong_ordering operator<=> (const Interface::Ptr& lhs, const Interface::Ptr& rhs) noexcept;
351
352 // friendship with interface manager.
353 friend class InterfaceManager;
354 };
355
362 inline bool operator== (const Interface::Ptr& lhs, const Interface::Ptr& rhs) noexcept
363 {
364 if (!lhs && !rhs)
365 {
366 return true;
367 }
368 if (!lhs || !rhs)
369 {
370 return false;
371 }
372 return lhs->_index == rhs->_index;
373 }
374
381 inline std::strong_ordering operator<=> (const Interface::Ptr& lhs, const Interface::Ptr& rhs) noexcept
382 {
383 if (!lhs || !rhs)
384 {
385 return bool (lhs) <=> bool (rhs);
386 }
387 return lhs->_index <=> rhs->_index;
388 }
389
391 using InterfaceList = std::vector<Interface::Ptr>;
392}
393
394#endif
interface manager class.
Definition interface_manager.hpp:154
interface class.
Definition interface.hpp:51
bool isLoopback() const
is interface a loopback interface.
Definition interface.cpp:285
bool supportsMulticast() const
is interface supporting multicast.
Definition interface.cpp:367
int enable(bool enabled=true, bool sync=false) const
enable interface.
Definition interface.cpp:258
uint32_t master() const
get master index if bridged.
Definition interface.cpp:62
friend bool operator==(const Interface::Ptr &lhs, const Interface::Ptr &rhs) noexcept
compare if two interfaces are equal.
Definition interface.hpp:362
bool isDummy() const
is interface a dummy interface.
Definition interface.cpp:303
bool supportsIpv6() const
is interface supporting IPv6.
Definition interface.cpp:395
bool isRunning() const
is interface running.
Definition interface.cpp:276
uint32_t index() const noexcept
get interface index.
Definition interface.cpp:53
bool supportsBroadcast() const
is interface supporting broadcast.
Definition interface.cpp:358
friend std::strong_ordering operator<=>(const Interface::Ptr &lhs, const Interface::Ptr &rhs) noexcept
three-way compare interfaces.
Definition interface.hpp:381
int removeAddress(const IpAddress &ipAddress, uint32_t prefix, const IpAddress &broadcast={}, bool sync=false) const
remove address from interface.
Definition interface.cpp:148
std::vector< Address > AddressList
Definition interface.hpp:73
MacAddress mac() const
get interface mac address.
Definition interface.cpp:120
~Interface()=default
destroy instance.
bool supportsIpv4() const
is interface supporting IPv4.
Definition interface.cpp:376
bool isEnabled() const
is interface enabled.
Definition interface.cpp:267
std::string kind() const
get interface kind.
Definition interface.cpp:101
bool hasLocalAddress() const
check if interface has link local address stored.
Definition interface.cpp:195
std::shared_ptr< Interface > Ptr
Definition interface.hpp:61
int removeFromBridge(bool sync=false) const
remove interface from bridge.
Definition interface.cpp:239
int addAddress(const IpAddress &ipAddress, uint32_t prefix, const IpAddress &broadcast={}, bool sync=false) const
add address to interface.
Definition interface.cpp:130
bool hasAddress(const IpAddress &ipAddress) const
check if interface has address stored.
Definition interface.cpp:176
uint32_t flags() const
get interface flags.
Definition interface.cpp:248
std::string name() const
get interface name.
Definition interface.cpp:72
bool isGre() const
is interface a gre interface.
Definition interface.cpp:339
bool isVlan() const
is interface a vlan interface.
Definition interface.cpp:321
AddressList addressList() const
get interface ip addresses.
Definition interface.cpp:166
int addToBridge(uint32_t masterIndex, bool sync=false) const
add interface to bridge.
Definition interface.cpp:214
bool isVeth() const
is interface a veth interface.
Definition interface.cpp:330
uint32_t mtu() const
get interface mtu.
Definition interface.cpp:91
friend class InterfaceManager
Definition interface.hpp:353
bool isBridge() const
is interface a bridge interface.
Definition interface.cpp:312
bool isTun() const
is interface a tun interface.
Definition interface.cpp:349
bool isPointToPoint() const
is interface a point to point interface.
Definition interface.cpp:294
Interface()=delete
create instance.
IPv6, IPv4 address class.
Definition ip_address.hpp:52
MAC address class.
Definition mac_address.hpp:47
Definition acceptor.hpp:32
std::vector< Interface::Ptr > InterfaceList
list of interfaces.
Definition interface.hpp:391
bool operator==(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:196
std::strong_ordering operator<=>(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
three-way compare endpoints.
Definition endpoint.hpp:208
Definition error.hpp:144
IP address entry associated with an interface.
Definition interface.hpp:67
uint32_t prefix
Definition interface.hpp:69
IpAddress broadcast
Definition interface.hpp:70
IpAddress ip
Definition interface.hpp:68
IpAddress address
Definition tcp_acceptor_test.cpp:35