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/macaddress.hpp>
30#include <join/ipaddress.hpp>
31#include <join/mutex.hpp>
32
33// C++.
34#include <string>
35#include <memory>
36#include <vector>
37
38// C.
39#include <cstdint>
40
41namespace join
42{
44 class InterfaceManager;
45
50 {
51 private:
57 explicit Interface (InterfaceManager* manager, uint32_t index);
58
59 public:
60 using Ptr = std::shared_ptr<Interface>;
61
65 struct Address
66 {
68 uint32_t prefix = 0;
70 };
71
72 using AddressList = std::vector<Address>;
73
77 Interface () = delete;
78
82 ~Interface () = default;
83
88 uint32_t index () const noexcept;
89
94 uint32_t master () const;
95
100 std::string name () const;
101
108 int mtu (uint32_t mtuBytes, bool sync = false) const;
109
114 uint32_t mtu () const;
115
120 std::string kind () const;
121
128 int mac (const MacAddress& macAddress, bool sync = false) const;
129
134 MacAddress mac () const;
135
144 int addAddress (const IpAddress& ipAddress, uint32_t prefix, const IpAddress& broadcast = {},
145 bool sync = false) const;
146
153 int addAddress (const Address& address, bool sync = false) const;
154
163 int removeAddress (const IpAddress& ipAddress, uint32_t prefix, const IpAddress& broadcast = {},
164 bool sync = false) const;
165
172 int removeAddress (const Address& address, bool sync = false) const;
173
178 AddressList addressList () const;
179
185 bool hasAddress (const IpAddress& ipAddress) const;
186
191 bool hasLocalAddress () const;
192
199 int addToBridge (uint32_t masterIndex, bool sync = false) const;
200
207 int addToBridge (const std::string& masterName, bool sync = false) const;
208
214 int removeFromBridge (bool sync = false) const;
215
220 uint32_t flags () const;
221
228 int enable (bool enabled = true, bool sync = false) const;
229
234 bool isEnabled () const;
235
240 bool isRunning () const;
241
246 bool isLoopback () const;
247
252 bool isPointToPoint () const;
253
258 bool isDummy () const;
259
264 bool isBridge () const;
265
270 bool isVlan () const;
271
276 bool isVeth () const;
277
282 bool isGre () const;
283
288 bool isTun () const;
289
294 bool supportsBroadcast () const;
295
300 bool supportsMulticast () const;
301
306 bool supportsIpv4 () const;
307
312 bool supportsIpv6 () const;
313
314 private:
316 InterfaceManager* _manager = nullptr;
317
319 uint32_t _index = 0;
320
322 uint32_t _master = 0;
323
325 std::string _name;
326
328 std::string _kind;
329
331 uint32_t _mtu = 0;
332
334 uint32_t _flags = 0;
335
337 MacAddress _mac;
338
340 AddressList _addresses;
341
343 mutable Mutex _mutex;
344
345 // friendship with equal operator.
346 friend bool operator== (const Interface::Ptr& lhs, const Interface::Ptr& rhs) noexcept;
347
348 // friendship with less operator.
349 friend bool operator< (const Interface::Ptr& lhs, const Interface::Ptr& rhs) noexcept;
350
351 // friendship with interface manager.
352 friend class InterfaceManager;
353 };
354
361 inline bool operator== (const Interface::Ptr& lhs, const Interface::Ptr& rhs) noexcept
362 {
363 if (!lhs && !rhs)
364 {
365 return true;
366 }
367 if (!lhs || !rhs)
368 {
369 return false;
370 }
371 return lhs->_index == rhs->_index;
372 }
373
380 inline bool operator< (const Interface::Ptr& lhs, const Interface::Ptr& rhs) noexcept
381 {
382 if (!lhs)
383 {
384 return true;
385 }
386 if (!rhs)
387 {
388 return false;
389 }
390 return lhs->_index < rhs->_index;
391 }
392
394 using InterfaceList = std::vector<Interface::Ptr>;
395}
396
397#endif
interface manager class.
Definition interfacemanager.hpp:154
interface class.
Definition interface.hpp:50
bool isLoopback() const
is interface a loopback interface.
Definition interface.cpp:288
bool supportsMulticast() const
is interface supporting multicast.
Definition interface.cpp:370
int enable(bool enabled=true, bool sync=false) const
enable interface.
Definition interface.cpp:261
uint32_t master() const
get master index if bridged.
Definition interface.cpp:65
friend bool operator==(const Interface::Ptr &lhs, const Interface::Ptr &rhs) noexcept
compare if two interfaces are equal.
Definition interface.hpp:361
bool isDummy() const
is interface a dummy interface.
Definition interface.cpp:306
bool supportsIpv6() const
is interface supporting IPv6.
Definition interface.cpp:398
bool isRunning() const
is interface running.
Definition interface.cpp:279
uint32_t index() const noexcept
get interface index.
Definition interface.cpp:56
bool supportsBroadcast() const
is interface supporting broadcast.
Definition interface.cpp:361
int removeAddress(const IpAddress &ipAddress, uint32_t prefix, const IpAddress &broadcast={}, bool sync=false) const
remove address from interface.
Definition interface.cpp:151
std::vector< Address > AddressList
Definition interface.hpp:72
MacAddress mac() const
get interface mac address.
Definition interface.cpp:123
~Interface()=default
destroy instance.
bool supportsIpv4() const
is interface supporting IPv4.
Definition interface.cpp:379
bool isEnabled() const
is interface enabled.
Definition interface.cpp:270
std::string kind() const
get interface kind.
Definition interface.cpp:104
bool hasLocalAddress() const
check if interface has link local address stored.
Definition interface.cpp:198
std::shared_ptr< Interface > Ptr
Definition interface.hpp:60
int removeFromBridge(bool sync=false) const
remove interface from bridge.
Definition interface.cpp:242
int addAddress(const IpAddress &ipAddress, uint32_t prefix, const IpAddress &broadcast={}, bool sync=false) const
add address to interface.
Definition interface.cpp:133
bool hasAddress(const IpAddress &ipAddress) const
check if interface has address stored.
Definition interface.cpp:179
uint32_t flags() const
get interface flags.
Definition interface.cpp:251
std::string name() const
get interface name.
Definition interface.cpp:75
bool isGre() const
is interface a gre interface.
Definition interface.cpp:342
bool isVlan() const
is interface a vlan interface.
Definition interface.cpp:324
AddressList addressList() const
get interface ip addresses.
Definition interface.cpp:169
int addToBridge(uint32_t masterIndex, bool sync=false) const
add interface to bridge.
Definition interface.cpp:217
bool isVeth() const
is interface a veth interface.
Definition interface.cpp:333
uint32_t mtu() const
get interface mtu.
Definition interface.cpp:94
friend bool operator<(const Interface::Ptr &lhs, const Interface::Ptr &rhs) noexcept
compare if interface is inferior.
Definition interface.hpp:380
friend class InterfaceManager
Definition interface.hpp:352
bool isBridge() const
is interface a bridge interface.
Definition interface.cpp:315
bool isTun() const
is interface a tun interface.
Definition interface.cpp:352
bool isPointToPoint() const
is interface a point to point interface.
Definition interface.cpp:297
Interface()=delete
create instance.
IPv6, IPv4 address class.
Definition ipaddress.hpp:51
MAC address class.
Definition macaddress.hpp:46
Definition acceptor.hpp:32
std::vector< Interface::Ptr > InterfaceList
list of interfaces.
Definition interface.hpp:394
bool operator<(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is lower.
Definition endpoint.hpp:207
bool operator==(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:183
Definition error.hpp:137
IP address entry associated with an interface.
Definition interface.hpp:66
uint32_t prefix
Definition interface.hpp:68
IpAddress broadcast
Definition interface.hpp:69
IpAddress ip
Definition interface.hpp:67
IpAddress address
Definition tcpacceptor_test.cpp:35