join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
interface.hpp
Go to the documentation of this file.
1
25#ifndef __JOIN_INTERFACE_HPP__
26#define __JOIN_INTERFACE_HPP__
27
28// libjoin.
29#include <join/macaddress.hpp>
30#include <join/ipaddress.hpp>
31
32// C++.
33#include <string>
34#include <memory>
35#include <vector>
36#include <tuple>
37#include <set>
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 using Address = std::tuple <IpAddress, uint32_t, IpAddress>;
63 using AddressList = std::vector <Address>;
64 using Route = std::tuple <IpAddress, uint32_t, IpAddress, uint32_t>;
65 using RouteList = std::vector <Route>;
66
70 Interface () = delete;
71
75 ~Interface () = default;
76
81 uint32_t index () const noexcept;
82
87 uint32_t master () const noexcept;
88
93 const std::string& name () const noexcept;
94
101 int mtu (uint32_t mtuBytes, bool sync = false);
102
107 uint32_t mtu () const noexcept;
108
113 const std::string& kind () const noexcept;
114
121 int mac (const MacAddress& macAddress, bool sync = false);
122
127 const MacAddress& mac () const noexcept;
128
137 int addAddress (const IpAddress& ipAddress, uint32_t prefix, const IpAddress& broadcast = {}, bool sync = false);
138
145 int addAddress (const Address& address, bool sync = false);
146
155 int removeAddress (const IpAddress& ipAddress, uint32_t prefix, const IpAddress& broadcast = {}, bool sync = false);
156
163 int removeAddress (const Address& address, bool sync = false);
164
169 const AddressList& addressList () const noexcept;
170
176 bool hasAddress (const IpAddress& ipAddress);
177
182 bool hasLocalAddress ();
183
193 int addRoute (const IpAddress& dest, uint32_t prefix, const IpAddress& gateway = {}, uint32_t metric = 0, bool sync = false);
194
201 int addRoute (const Route& route, bool sync = false);
202
212 int removeRoute (const IpAddress& dest, uint32_t prefix, const IpAddress& gateway = {}, uint32_t metric = 0, bool sync = false);
213
220 int removeRoute (const Route& route, bool sync = false);
221
226 const RouteList& routeList () const noexcept;
227
236 bool hasRoute (const IpAddress& dest, uint32_t prefix, const IpAddress& gateway, uint32_t metric);
237
243 bool hasRoute (const Route& route);
244
251 int addToBridge (uint32_t masterIndex, bool sync = false);
252
259 int addToBridge (const std::string& masterName, bool sync = false);
260
266 int removeFromBridge (bool sync = false);
267
272 uint32_t flags () const noexcept;
273
280 int enable (bool enabled = true, bool sync = false);
281
286 bool isEnabled () const noexcept;
287
292 bool isRunning () const noexcept;
293
298 bool isLoopback () const noexcept;
299
304 bool isPointToPoint () const noexcept;
305
310 bool isDummy () const noexcept;
311
316 bool isBridge () const noexcept;
317
322 bool isVlan () const noexcept;
323
328 bool isVeth () const noexcept;
329
334 bool isGre () const noexcept;
335
340 bool isTun () const noexcept;
341
346 bool supportsBroadcast () const noexcept;
347
352 bool supportsMulticast () const noexcept;
353
358 bool supportsIpv4 ();
359
364 bool supportsIpv6 ();
365
366 private:
368 InterfaceManager* _manager = nullptr;
369
371 uint32_t _index = 0;
372
374 uint32_t _master = 0;
375
377 std::string _name;
378
380 std::string _kind;
381
383 uint32_t _mtu = 0;
384
386 uint32_t _flags = 0;
387
389 MacAddress _mac;
390
392 AddressList _addresses;
393
395 RouteList _routes;
396
398 Mutex _mutex;
399
400 // friendship with equal operator.
401 friend bool operator== (const Interface::Ptr& lhs, const Interface::Ptr& rhs);
402
403 // friendship with not equal operator.
404 friend bool operator< (const Interface::Ptr& lhs, const Interface::Ptr& rhs);
405
406 // friendship with interface manager.
407 friend class InterfaceManager;
408 };
409
416 inline bool operator== (const Interface::Ptr& lhs, const Interface::Ptr& rhs)
417 {
418 if (!lhs && !rhs)
419 {
420 return true;
421 }
422 if (!lhs || !rhs)
423 {
424 return false;
425 }
426 return lhs->_index == rhs->_index;
427 }
428
435 inline bool operator< (const Interface::Ptr& lhs, const Interface::Ptr& rhs)
436 {
437 if (!lhs)
438 {
439 return true;
440 }
441 if (!rhs)
442 {
443 return false;
444 }
445 return lhs->_index < rhs->_index;
446 }
447
449 using InterfaceList = std::set <Interface::Ptr>;
450}
451
452#endif
interface manager class.
Definition interfacemanager.hpp:158
interface class.
Definition interface.hpp:51
const MacAddress & mac() const noexcept
get interface mac address.
Definition interface.cpp:121
bool isTun() const noexcept
is interface a tun interface.
Definition interface.cpp:422
const std::string & kind() const noexcept
get interface kind.
Definition interface.cpp:103
bool hasLocalAddress()
check if interface has link local address stored.
Definition interface.cpp:194
uint32_t mtu() const noexcept
get interface mtu.
Definition interface.cpp:94
std::vector< Route > RouteList
Definition interface.hpp:65
std::shared_ptr< Interface > Ptr
Definition interface.hpp:61
bool isGre() const noexcept
is interface a gre interface.
Definition interface.cpp:413
uint32_t master() const noexcept
get master index if bridged.
Definition interface.cpp:67
uint32_t index() const noexcept
get interface index.
Definition interface.cpp:58
bool supportsBroadcast() const noexcept
is interface supporting broadcast.
Definition interface.cpp:431
~Interface()=default
destroy instance.
const RouteList & routeList() const noexcept
get interface routes.
Definition interface.cpp:249
bool hasRoute(const IpAddress &dest, uint32_t prefix, const IpAddress &gateway, uint32_t metric)
check if interface has route stored.
Definition interface.cpp:258
bool isVlan() const noexcept
is interface a vlan interface.
Definition interface.cpp:395
const std::string & name() const noexcept
get interface name.
Definition interface.cpp:76
bool isVeth() const noexcept
is interface a veth interface.
Definition interface.cpp:404
const AddressList & addressList() const noexcept
get interface ip addresses.
Definition interface.cpp:166
std::tuple< IpAddress, uint32_t, IpAddress > Address
Definition interface.hpp:62
int addRoute(const IpAddress &dest, uint32_t prefix, const IpAddress &gateway={}, uint32_t metric=0, bool sync=false)
add route to interface.
Definition interface.cpp:213
bool isEnabled() const noexcept
is interface enabled.
Definition interface.cpp:341
int addToBridge(uint32_t masterIndex, bool sync=false)
add interface to bridge.
Definition interface.cpp:289
bool isBridge() const noexcept
is interface a bridge interface.
Definition interface.cpp:386
bool supportsMulticast() const noexcept
is interface supporting multicast.
Definition interface.cpp:440
bool isPointToPoint() const noexcept
is interface a point to point interface.
Definition interface.cpp:368
std::tuple< IpAddress, uint32_t, IpAddress, uint32_t > Route
Definition interface.hpp:64
uint32_t flags() const noexcept
get interface flags.
Definition interface.cpp:323
int removeAddress(const IpAddress &ipAddress, uint32_t prefix, const IpAddress &broadcast={}, bool sync=false)
remove address from interface.
Definition interface.cpp:148
bool isDummy() const noexcept
is interface a dummy interface.
Definition interface.cpp:377
bool isLoopback() const noexcept
is interface a loopback interface.
Definition interface.cpp:359
friend bool operator<(const Interface::Ptr &lhs, const Interface::Ptr &rhs)
compare if interface is inferior.
Definition interface.hpp:435
int addAddress(const IpAddress &ipAddress, uint32_t prefix, const IpAddress &broadcast={}, bool sync=false)
add address to interface.
Definition interface.cpp:130
friend class InterfaceManager
Definition interface.hpp:407
bool supportsIpv6()
is interface supporting IPv6.
Definition interface.cpp:468
int removeRoute(const IpAddress &dest, uint32_t prefix, const IpAddress &gateway={}, uint32_t metric=0, bool sync=false)
remove route from interface.
Definition interface.cpp:231
bool hasAddress(const IpAddress &ipAddress)
check if interface has address stored.
Definition interface.cpp:175
bool isRunning() const noexcept
is interface running.
Definition interface.cpp:350
std::vector< Address > AddressList
Definition interface.hpp:63
bool supportsIpv4()
is interface supporting IPv4.
Definition interface.cpp:449
int removeFromBridge(bool sync=false)
remove interface from bridge.
Definition interface.cpp:314
Interface()=delete
create instance.
int enable(bool enabled=true, bool sync=false)
enable interface.
Definition interface.cpp:332
IPv6, IPv4 address class.
Definition ipaddress.hpp:51
MAC address class.
Definition macaddress.hpp:46
Definition acceptor.hpp:32
std::set< Interface::Ptr > InterfaceList
list of interfaces.
Definition interface.hpp:449
Definition error.hpp:106
IpAddress address
Definition tcpacceptor_test.cpp:35