join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
netlink_endpoint.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_FABRIC_NETLINK_ENDPOINT_HPP
26#define JOIN_FABRIC_NETLINK_ENDPOINT_HPP
27
28// libjoin.
29#include <join/endpoint.hpp>
30
31// C.
32#include <linux/netfilter/nfnetlink.h>
33#include <linux/rtnetlink.h>
34#include <linux/netlink.h>
35
36namespace join
37{
41 template <class Protocol>
42 class BasicNetlinkEndpoint : public BasicEndpoint<Protocol>
43 {
44 public:
48 constexpr BasicNetlinkEndpoint () noexcept
49 : BasicEndpoint<Protocol> ()
50 , _protocol (Protocol ().protocol ())
51 {
52 }
53
59 BasicNetlinkEndpoint (const struct sockaddr* addr, socklen_t len) noexcept
61 , _protocol (Protocol ().protocol ())
62 {
63 }
64
71 BasicNetlinkEndpoint (const Protocol& protocol, uint32_t pid, uint32_t groups) noexcept
73 , _protocol (protocol.protocol ())
74 {
75 struct sockaddr_nl* nl = reinterpret_cast<struct sockaddr_nl*> (&this->_addr);
76 nl->nl_pid = pid;
77 nl->nl_groups = groups;
78 }
79
85 BasicNetlinkEndpoint (uint32_t pid, uint32_t groups) noexcept
86 : BasicNetlinkEndpoint (Protocol (), pid, groups)
87 {
88 }
89
95 BasicNetlinkEndpoint (const Protocol& protocol, uint32_t groups) noexcept
97 {
98 }
99
104 BasicNetlinkEndpoint (uint32_t groups) noexcept
105 : BasicNetlinkEndpoint (Protocol (), 0, groups)
106 {
107 }
108
113 Protocol protocol () const noexcept
114 {
115 if (_protocol == NETLINK_NETFILTER)
116 {
117 return Protocol::nf ();
118 }
119 return Protocol::rt ();
120 }
121
126 constexpr socklen_t length () const noexcept
127 {
128 return sizeof (struct sockaddr_nl);
129 }
130
135 void pid (uint32_t pid) noexcept
136 {
137 reinterpret_cast<struct sockaddr_nl*> (&this->_addr)->nl_pid = pid;
138 }
139
144 uint32_t pid () const noexcept
145 {
146 return reinterpret_cast<const struct sockaddr_nl*> (&this->_addr)->nl_pid;
147 }
148
153 void groups (uint32_t groups) noexcept
154 {
155 reinterpret_cast<struct sockaddr_nl*> (&this->_addr)->nl_groups = groups;
156 }
157
162 uint32_t groups () const noexcept
163 {
164 return reinterpret_cast<const struct sockaddr_nl*> (&this->_addr)->nl_groups;
165 }
166
171 std::string device () const
172 {
173 return std::string ();
174 }
175
176 protected:
179 };
180
187 template <class Protocol>
189 {
190 return a.pid () == b.pid () && a.groups () == b.groups ();
191 }
192
199 template <class Protocol>
201 {
202 return !(a == b);
203 }
204
211 template <class Protocol>
213 {
214 if (a.pid () != b.pid ())
215 {
216 return a.pid () < b.pid ();
217 }
218 return a.groups () < b.groups ();
219 }
220
227 template <class Protocol>
229 {
230 return b < a;
231 }
232
239 template <class Protocol>
241 {
242 return !(b < a);
243 }
244
251 template <class Protocol>
253 {
254 return !(a < b);
255 }
256
263 template <class Protocol>
264 std::ostream& operator<< (std::ostream& os, const BasicNetlinkEndpoint<Protocol>& endpoint)
265 {
266 os << "pid=" << endpoint.pid () << ",groups=" << endpoint.groups ();
267 return os;
268 }
269}
270
271#endif
basic endpoint class.
Definition endpoint.hpp:49
constexpr BasicEndpoint() noexcept
default constructor.
Definition endpoint.hpp:54
struct sockaddr * addr() noexcept
get socket address.
Definition endpoint.hpp:73
struct sockaddr_storage _addr
socket address storage.
Definition endpoint.hpp:89
basic netlink endpoint class.
Definition netlink_endpoint.hpp:43
uint32_t groups() const noexcept
get netlink groups.
Definition netlink_endpoint.hpp:162
uint32_t pid() const noexcept
get port id.
Definition netlink_endpoint.hpp:144
int _protocol
netlink protocol type.
Definition netlink_endpoint.hpp:178
void groups(uint32_t groups) noexcept
set netlink groups.
Definition netlink_endpoint.hpp:153
std::string device() const
get device name (not applicable for netlink).
Definition netlink_endpoint.hpp:171
constexpr BasicNetlinkEndpoint() noexcept
default constructor.
Definition netlink_endpoint.hpp:48
BasicNetlinkEndpoint(const struct sockaddr *addr, socklen_t len) noexcept
create instance using socket address.
Definition netlink_endpoint.hpp:59
BasicNetlinkEndpoint(uint32_t pid, uint32_t groups) noexcept
create instance using netlink groups.
Definition netlink_endpoint.hpp:85
BasicNetlinkEndpoint(const Protocol &protocol, uint32_t groups) noexcept
create instance using netlink groups.
Definition netlink_endpoint.hpp:95
Protocol protocol() const noexcept
get endpoint protocol.
Definition netlink_endpoint.hpp:113
constexpr socklen_t length() const noexcept
get socket address length.
Definition netlink_endpoint.hpp:126
BasicNetlinkEndpoint(const Protocol &protocol, uint32_t pid, uint32_t groups) noexcept
create instance using netlink groups.
Definition netlink_endpoint.hpp:71
void pid(uint32_t pid) noexcept
set port id.
Definition netlink_endpoint.hpp:135
BasicNetlinkEndpoint(uint32_t groups) noexcept
create instance using netlink groups.
Definition netlink_endpoint.hpp:104
Definition acceptor.hpp:32
bool operator<=(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoint is lower or equal.
Definition endpoint.hpp:234
bool operator==(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:186
bool operator!=(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoints are not equal.
Definition endpoint.hpp:198
bool operator>=(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoint is greater or equal.
Definition endpoint.hpp:246
std::ostream & operator<<(std::ostream &os, const BasicLinkLayerEndpoint< Protocol > &endpoint)
push endpoint representation into a stream.
Definition endpoint.hpp:258
bool operator<(const BasicDatagramSocket< Protocol > &a, const BasicDatagramSocket< Protocol > &b) noexcept
compare if socket handle is inferior.
Definition datagram_socket.hpp:394
bool operator>(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoint is greater.
Definition endpoint.hpp:222