llmx-rtaco 0.0.1
RTNL-only netlink control-plane library for Linux (C++23).
Loading...
Searching...
No Matches
nl_socket.hxx
Go to the documentation of this file.
1#pragma once
2
10
11#include <cstddef>
12#include <expected>
13#include <string>
14#include <string_view>
15#include <system_error>
16#include <utility>
17
18#include <linux/netlink.h>
19#include <stdint.h>
20#include <sys/socket.h>
21
22#include <boost/asio/detail/socket_option.hpp>
23
25
26namespace boost {
27namespace asio {
28class io_context;
29}
30} // namespace boost
31namespace boost {
32namespace system {
33class error_code;
34}
35} // namespace boost
36
37namespace llmx {
38namespace rtaco {
39
46class Socket {
47public:
50 using native_t = typename socket_t::native_handle_type;
51
53 explicit Socket(boost::asio::io_context& io, std::string_view label) noexcept;
55 ~Socket() noexcept;
56
57 Socket(const Socket&) = delete;
58 Socket& operator=(const Socket&) = delete;
59
60 Socket(Socket&&) noexcept = default;
61 Socket& operator=(Socket&&) noexcept = default;
62
65 auto is_open() const noexcept -> bool;
72 auto close() -> std::expected<void, std::error_code>;
79 auto cancel() -> std::expected<void, std::error_code>;
80
92 auto open(int proto, uint32_t groups) -> std::expected<void, std::error_code>;
93
94 template<typename Option>
95 void set_option(const Option& option, boost::system::error_code& ec) {
96 socket_.set_option(option, ec);
97 }
98
99 template<typename Endpoint>
100 void bind(const Endpoint& endpoint, boost::system::error_code& ec) {
101 socket_.bind(endpoint, ec);
102 }
103
104 template<typename Endpoint>
105 void connect(const Endpoint& endpoint, boost::system::error_code& ec) {
106 socket_.connect(endpoint, ec);
107 }
108
109 template<typename MutableBufferSequence, typename CompletionToken>
110 auto async_receive(const MutableBufferSequence& buffers, CompletionToken&& token)
111 -> decltype(std::declval<socket_t>().async_receive(buffers,
112 std::forward<CompletionToken>(token))) {
113 return socket_.async_receive(buffers, std::forward<CompletionToken>(token));
114 }
115
116 template<typename MutableBufferSequence>
117 auto receive(const MutableBufferSequence& buffers, boost::system::error_code& ec)
118 -> size_t {
119 return socket_.receive(buffers, 0, ec);
120 }
121
122 template<typename ConstBufferSequence, typename CompletionToken>
123 auto async_send(const ConstBufferSequence& buffers, CompletionToken&& token)
124 -> decltype(std::declval<socket_t>()
125 .async_send(buffers, std::forward<CompletionToken>(token))) {
126 return socket_.async_send(buffers, std::forward<CompletionToken>(token));
127 }
128
129 template<typename ConstBufferSequence>
130 auto send(const ConstBufferSequence& buffers, boost::system::error_code& ec)
131 -> size_t {
132 return socket_.send(buffers, 0, ec);
133 }
134
140 auto native_handle() -> native_t;
141
142private:
144 boost::asio::detail::socket_option::integer<SOL_NETLINK, NETLINK_EXT_ACK>;
145
146 using strict_chk_option = boost::asio::detail::socket_option::integer<SOL_NETLINK,
147 NETLINK_GET_STRICT_CHK>;
148
150 boost::asio::detail::socket_option::integer<SOL_SOCKET, SO_RCVBUF>;
151
153 boost::asio::detail::socket_option::integer<SOL_NETLINK, NETLINK_NO_ENOBUFS>;
154
156 std::string label_;
157};
158
159} // namespace rtaco
160} // namespace llmx
Netlink-specific endpoint wrapper for Boost.Asio.
Definition nl_protocol.hxx:23
boost::asio::basic_raw_socket< Protocol > socket
Definition nl_protocol.hxx:108
Endpoint< Protocol > endpoint
Definition nl_protocol.hxx:107
std::string label_
Definition nl_socket.hxx:156
~Socket() noexcept
Destroy the Socket object and ensure underlying socket is closed.
Definition nl_socket.cxx:25
auto async_send(const ConstBufferSequence &buffers, CompletionToken &&token) -> decltype(std::declval< socket_t >() .async_send(buffers, std::forward< CompletionToken >(token)))
Definition nl_socket.hxx:123
boost::asio::detail::socket_option::integer< SOL_SOCKET, SO_RCVBUF > recv_buf_option
Definition nl_socket.hxx:149
auto close() -> std::expected< void, std::error_code >
Close the underlying socket.
Definition nl_socket.cxx:35
boost::asio::detail::socket_option::integer< SOL_NETLINK, NETLINK_NO_ENOBUFS > no_enobufs_option
Definition nl_socket.hxx:152
boost::asio::detail::socket_option::integer< SOL_NETLINK, NETLINK_GET_STRICT_CHK > strict_chk_option
Definition nl_socket.hxx:146
auto open(int proto, uint32_t groups) -> std::expected< void, std::error_code >
Open and configure the netlink socket.
Definition nl_socket.cxx:55
boost::asio::detail::socket_option::integer< SOL_NETLINK, NETLINK_EXT_ACK > ext_ack_option
Definition nl_socket.hxx:143
void bind(const Endpoint &endpoint, boost::system::error_code &ec)
Definition nl_socket.hxx:100
socket_t socket_
Definition nl_socket.hxx:155
auto receive(const MutableBufferSequence &buffers, boost::system::error_code &ec) -> size_t
Definition nl_socket.hxx:117
auto is_open() const noexcept -> bool
Check whether the socket is currently open.
Definition nl_socket.cxx:31
auto async_receive(const MutableBufferSequence &buffers, CompletionToken &&token) -> decltype(std::declval< socket_t >().async_receive(buffers, std::forward< CompletionToken >(token)))
Definition nl_socket.hxx:110
typename socket_t::native_handle_type native_t
Definition nl_socket.hxx:50
Socket(boost::asio::io_context &io, std::string_view label) noexcept
Construct a netlink Socket with an io_context and label.
Definition nl_socket.cxx:21
Protocol::endpoint endpoint_t
Definition nl_socket.hxx:49
void connect(const Endpoint &endpoint, boost::system::error_code &ec)
Definition nl_socket.hxx:105
auto send(const ConstBufferSequence &buffers, boost::system::error_code &ec) -> size_t
Definition nl_socket.hxx:130
void set_option(const Option &option, boost::system::error_code &ec)
Definition nl_socket.hxx:95
auto native_handle() -> native_t
Get the native socket handle.
Definition nl_socket.cxx:113
Protocol::socket socket_t
Definition nl_socket.hxx:48
auto cancel() -> std::expected< void, std::error_code >
Cancel any asynchronous operations on the socket.
Definition nl_socket.cxx:45
Definition nl_socket.hxx:27
Definition nl_socket.hxx:32
Definition nl_socket.hxx:26
Definition nl_common.hxx:20