idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
idfxx::net::raw_socket Class Reference

A raw IP socket. More...

Inheritance diagram for idfxx::net::raw_socket:

Classes

struct  config
 Raw socket configuration. More...
 

Public Member Functions

 raw_socket (ip_protocol protocol)
 Creates a new raw IPv4 socket for the given IP protocol.
 
 raw_socket (ip_protocol protocol, address_family fam)
 Creates a new raw socket for the given protocol and address family.
 
 raw_socket (const config &cfg)
 Creates a new raw socket with the given configuration.
 
size_t send (std::span< const std::byte > buf)
 Sends a raw packet on a connected socket.
 
result< size_ttry_send (std::span< const std::byte > buf)
 Sends a raw packet on a connected socket.
 

Static Public Member Functions

static result< raw_socketmake (ip_protocol protocol)
 Creates a new raw IPv4 socket for the given IP protocol.
 
static result< raw_socketmake (ip_protocol protocol, address_family fam)
 Creates a new raw socket for the given protocol and address family.
 
static result< raw_socketmake (const config &cfg)
 Creates a new raw socket with the given configuration.
 

Detailed Description

A raw IP socket.

The protocol (e.g. ip_protocol::icmp) selects the IP-layer protocol for which the socket is opened. Use send_to / recv_from for connectionless addressing, or connect() to set a default peer.

On IPv4 a received packet includes the 20-byte IP header that the stack prepends; the header is not part of an outgoing send and is added by the stack. As with datagram_socket, a send larger than the link can carry returns errc::message_too_long, and a receive into too small a buffer returns errc::message_too_long rather than truncating.

Ownership is exclusive: the socket is non-copyable and move-only. A moved-from socket may only be destroyed, move-assigned, or queried via is_open() (→ false) and idf_handle() (→ -1); calling any other method on it is undefined behavior. The destructor closes the socket if open.

Definition at line 50 of file raw_socket.hpp.

Constructor & Destructor Documentation

◆ raw_socket() [1/3]

idfxx::net::raw_socket::raw_socket ( ip_protocol  protocol)
explicit

Creates a new raw IPv4 socket for the given IP protocol.

Parameters
protocolIP-layer protocol (e.g. ip_protocol::icmp).
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

◆ raw_socket() [2/3]

idfxx::net::raw_socket::raw_socket ( ip_protocol  protocol,
address_family  fam 
)

Creates a new raw socket for the given protocol and address family.

Parameters
protocolIP-layer protocol.
famAddress family.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

◆ raw_socket() [3/3]

idfxx::net::raw_socket::raw_socket ( const config cfg)
explicit

Creates a new raw socket with the given configuration.

Parameters
cfgSocket configuration.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.

Member Function Documentation

◆ make() [1/3]

static result< raw_socket > idfxx::net::raw_socket::make ( const config cfg)
static

Creates a new raw socket with the given configuration.

Parameters
cfgSocket configuration.
Returns
The new socket, or an error.

◆ make() [2/3]

static result< raw_socket > idfxx::net::raw_socket::make ( ip_protocol  protocol)
static

Creates a new raw IPv4 socket for the given IP protocol.

Parameters
protocolIP-layer protocol (e.g. ip_protocol::icmp).
Returns
The new socket, or an error.

◆ make() [3/3]

static result< raw_socket > idfxx::net::raw_socket::make ( ip_protocol  protocol,
address_family  fam 
)
static

Creates a new raw socket for the given protocol and address family.

Parameters
protocolIP-layer protocol.
famAddress family.
Returns
The new socket, or an error.

◆ send()

size_t idfxx::net::raw_socket::send ( std::span< const std::byte >  buf)
inline

Sends a raw packet on a connected socket.

Parameters
bufData to send.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
Exceptions
std::system_erroron failure.
Returns
The number of bytes sent — always buf.size() on success, since a raw packet is sent in full or not at all.

Definition at line 158 of file raw_socket.hpp.

References idfxx::unwrap().

◆ try_send()

result< size_t > idfxx::net::raw_socket::try_send ( std::span< const std::byte >  buf)

Sends a raw packet on a connected socket.

Parameters
bufData to send.
Returns
The number of bytes sent — always buf.size() on success, since a raw packet is sent in full or not at all — or an error.

The documentation for this class was generated from the following file: