idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
raw_socket.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Chris Leishman
3
4#pragma once
5
17#include "sdkconfig.h"
18
20#include <idfxx/net/endpoint>
21#include <idfxx/net/error>
22
23#include <chrono>
24#include <cstddef>
25#include <optional>
26#include <span>
27#include <string_view>
28
29namespace idfxx::net {
30
50class [[nodiscard]] raw_socket : public detail::connectionless_socket_base {
51public:
58 struct config {
59 ip_protocol protocol = {};
60 address_family family = address_family::ipv4;
63 bool non_blocking = false;
66 std::optional<std::chrono::milliseconds> recv_timeout = std::nullopt;
69 std::optional<std::chrono::milliseconds> send_timeout = std::nullopt;
70#ifdef CONFIG_LWIP_IPV6
73 bool ipv6_only = false;
74#endif
77 std::optional<std::string_view> bind_to_device = std::nullopt;
78 };
79
80 // ----- construction -----
81
82#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
91 explicit raw_socket(ip_protocol protocol);
92#endif
93
101
102#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
113#endif
114
123
124#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
133 explicit raw_socket(const config& cfg);
134#endif
135
143
144 // ----- I/O -----
145
146#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
158 size_t send(std::span<const std::byte> buf) { return idfxx::unwrap(try_send(buf)); }
159#endif
160
168 [[nodiscard]] result<size_t> try_send(std::span<const std::byte> buf);
169
170private:
171 struct from_fd_t {
172 explicit from_fd_t() = default;
173 };
174
175 raw_socket(from_fd_t, int fd, address_family fam) noexcept
176 : connectionless_socket_base(fd, fam) {}
177};
178
179} // namespace idfxx::net
180
// end of idfxx_net_raw_socket
A raw IP socket.
raw_socket(const config &cfg)
Creates a new raw socket with the given configuration.
static result< raw_socket > make(ip_protocol protocol, address_family fam)
Creates a new raw socket for the given protocol and address family.
result< size_t > try_send(std::span< const std::byte > buf)
Sends a raw packet on a connected socket.
static result< raw_socket > make(const config &cfg)
Creates a new raw socket with the given configuration.
raw_socket(ip_protocol protocol, address_family fam)
Creates a new raw socket for the given protocol and address family.
static result< raw_socket > make(ip_protocol protocol)
Creates a new raw IPv4 socket for the given IP protocol.
raw_socket(ip_protocol protocol)
Creates a new raw IPv4 socket for the given IP protocol.
size_t send(std::span< const std::byte > buf)
Sends a raw packet on a connected socket.
address_family
Address family.
Definition endpoint.hpp:37
ip_protocol
IP-layer protocol number, per IANA assigned protocol numbers.
Definition endpoint.hpp:50
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
Definition error.hpp:307
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120
Raw socket configuration.