llmx-rtaco 0.0.1
RTNL-only netlink control-plane library for Linux (C++23).
Loading...
Searching...
No Matches
nl_protocol.hxx
Go to the documentation of this file.
1#pragma once
2
3#include <boost/asio/basic_raw_socket.hpp>
4#include <boost/asio/detail/socket_types.hpp>
5
6#include <cstddef>
7#include <cstdint>
8#include <cstring>
9
10#include <linux/netlink.h>
11#include <sys/socket.h>
12#include <unistd.h>
13
14namespace llmx {
15namespace rtaco {
16
22template<typename Protocol>
23class Endpoint {
24public:
26 using data_type = boost::asio::detail::socket_addr_type;
27
29 Endpoint() noexcept {
30 sockaddr_.nl_family = AF_NETLINK;
31 sockaddr_.nl_groups = AF_UNSPEC;
32 sockaddr_.nl_pid = AF_UNSPEC;
33 }
34
36 Endpoint(uint32_t groups, uint32_t pid = AF_UNSPEC) noexcept {
37 sockaddr_.nl_family = AF_NETLINK;
38 sockaddr_.nl_groups = groups;
39 sockaddr_.nl_pid = pid;
40 }
41
42 Endpoint(const Endpoint& other) noexcept = default;
43 Endpoint& operator=(const Endpoint& other) noexcept = default;
44
46 auto protocol() const noexcept -> protocol_type {
47 return protocol_type{};
48 }
49
51 auto data() noexcept -> data_type* {
52 return reinterpret_cast<data_type*>(&sockaddr_);
53 }
54
56 auto data() const noexcept -> const data_type* {
57 return reinterpret_cast<const data_type*>(&sockaddr_);
58 }
59
61 auto size() const noexcept -> size_t {
62 return sizeof(sockaddr_);
63 }
64
65 void resize(size_t) noexcept {}
66
68 auto capacity() const noexcept -> size_t {
69 return sizeof(sockaddr_);
70 }
71
72 friend bool operator==(const Endpoint& lhs, const Endpoint& rhs) noexcept {
73 return std::memcmp(&lhs.sockaddr_, &rhs.sockaddr_, sizeof(sockaddr_nl)) == 0;
74 }
75
76 friend bool operator!=(const Endpoint& lhs, const Endpoint& rhs) noexcept {
77 return !(lhs == rhs);
78 }
79
80 friend bool operator<(const Endpoint& lhs, const Endpoint& rhs) noexcept {
81 return std::memcmp(&lhs.sockaddr_, &rhs.sockaddr_, sizeof(sockaddr_nl)) < 0;
82 }
83
84 friend bool operator>(const Endpoint& lhs, const Endpoint& rhs) noexcept {
85 return rhs < lhs;
86 }
87
88 friend bool operator<=(const Endpoint& lhs, const Endpoint& rhs) noexcept {
89 return !(rhs < lhs);
90 }
91
92 friend bool operator>=(const Endpoint& lhs, const Endpoint& rhs) noexcept {
93 return !(lhs < rhs);
94 }
95
96private:
97 sockaddr_nl sockaddr_{};
98};
99
105class Protocol {
106public:
108 using socket = boost::asio::basic_raw_socket<Protocol>;
109
110 Protocol() = default;
111
113 explicit Protocol(int proto) noexcept
114 : proto_{proto} {}
115
117 auto type() const noexcept -> int {
118 return SOCK_RAW;
119 }
120
122 auto protocol() const noexcept -> int {
123 return proto_;
124 }
125
127 auto family() const noexcept -> int {
128 return AF_NETLINK;
129 }
130
131private:
132 int proto_{0};
133};
134
135} // namespace rtaco
136} // namespace llmx
Netlink-specific endpoint wrapper for Boost.Asio.
Definition nl_protocol.hxx:23
auto protocol() const noexcept -> protocol_type
Return the protocol type instance for this endpoint.
Definition nl_protocol.hxx:46
auto data() const noexcept -> const data_type *
Get const pointer to raw socket address storage.
Definition nl_protocol.hxx:56
boost::asio::detail::socket_addr_type data_type
Definition nl_protocol.hxx:26
Endpoint(const Endpoint &other) noexcept=default
sockaddr_nl sockaddr_
Definition nl_protocol.hxx:97
auto size() const noexcept -> size_t
Size of this endpoint's address structure.
Definition nl_protocol.hxx:61
Endpoint() noexcept
Default construct an Endpoint with unspecified pid/groups.
Definition nl_protocol.hxx:29
friend bool operator==(const Endpoint &lhs, const Endpoint &rhs) noexcept
Definition nl_protocol.hxx:72
friend bool operator<(const Endpoint &lhs, const Endpoint &rhs) noexcept
Definition nl_protocol.hxx:80
auto data() noexcept -> data_type *
Get mutable pointer to raw socket address storage.
Definition nl_protocol.hxx:51
auto capacity() const noexcept -> size_t
Capacity of the address storage.
Definition nl_protocol.hxx:68
void resize(size_t) noexcept
Definition nl_protocol.hxx:65
friend bool operator>(const Endpoint &lhs, const Endpoint &rhs) noexcept
Definition nl_protocol.hxx:84
friend bool operator<=(const Endpoint &lhs, const Endpoint &rhs) noexcept
Definition nl_protocol.hxx:88
Protocol protocol_type
Definition nl_protocol.hxx:25
Endpoint(uint32_t groups, uint32_t pid=AF_UNSPEC) noexcept
Construct an Endpoint for the given multicast groups and pid.
Definition nl_protocol.hxx:36
Endpoint & operator=(const Endpoint &other) noexcept=default
friend bool operator>=(const Endpoint &lhs, const Endpoint &rhs) noexcept
Definition nl_protocol.hxx:92
friend bool operator!=(const Endpoint &lhs, const Endpoint &rhs) noexcept
Definition nl_protocol.hxx:76
Protocol descriptor for netlink sockets.
Definition nl_protocol.hxx:105
Protocol(int proto) noexcept
Construct a Protocol wrapper for a given netlink protocol.
Definition nl_protocol.hxx:113
boost::asio::basic_raw_socket< Protocol > socket
Definition nl_protocol.hxx:108
auto type() const noexcept -> int
Socket type for this protocol (SOCK_RAW).
Definition nl_protocol.hxx:117
auto family() const noexcept -> int
Address family (AF_NETLINK).
Definition nl_protocol.hxx:127
Endpoint< Protocol > endpoint
Definition nl_protocol.hxx:107
int proto_
Definition nl_protocol.hxx:132
auto protocol() const noexcept -> int
Underlying protocol number used with socket(2).
Definition nl_protocol.hxx:122
Definition nl_common.hxx:21
Definition nl_common.hxx:20