Back to Site
Loading...
Searching...
No Matches
librats::StunClient Class Reference

STUN Client for NAT traversal. More...

#include <stun.h>

Public Member Functions

 StunClient ()
 
 StunClient (const StunClientConfig &config)
 
 ~StunClient ()
 
 StunClient (const StunClient &)=delete
 
StunClientoperator= (const StunClient &)=delete
 
 StunClient (StunClient &&) noexcept
 
StunClientoperator= (StunClient &&) noexcept
 
StunResult binding_request (const std::string &server, uint16_t port=STUN_DEFAULT_PORT, int timeout_ms=0)
 Send STUN Binding Request to discover public address.
 
StunResult binding_request_with_socket (socket_t socket, const std::string &server, uint16_t port, int timeout_ms=0)
 Send STUN Binding Request using existing socket Useful when you need to discover the mapped address for a specific local socket.
 
std::optional< StunMessagesend_request (socket_t socket, const StunMessage &request, const std::string &server, uint16_t port, int timeout_ms)
 Send a raw STUN message and wait for response.
 
const StunClientConfigconfig () const
 Get the configuration.
 
void set_config (const StunClientConfig &config)
 Set configuration.
 

Detailed Description

STUN Client for NAT traversal.

Example usage:

StunClient client;
auto result = client.binding_request("stun.l.google.com", 19302);
if (result.success && result.mapped_address) {
std::cout << "Public address: " << result.mapped_address->to_string() << std::endl;
}
STUN Client for NAT traversal.
Definition stun.h:383
StunResult binding_request(const std::string &server, uint16_t port=STUN_DEFAULT_PORT, int timeout_ms=0)
Send STUN Binding Request to discover public address.
std::optional< StunMappedAddress > mapped_address
Definition stun.h:364

Definition at line 383 of file stun.h.

Constructor & Destructor Documentation

◆ StunClient() [1/4]

librats::StunClient::StunClient ( )

◆ StunClient() [2/4]

librats::StunClient::StunClient ( const StunClientConfig config)
explicit

◆ ~StunClient()

librats::StunClient::~StunClient ( )

◆ StunClient() [3/4]

librats::StunClient::StunClient ( const StunClient )
delete

◆ StunClient() [4/4]

librats::StunClient::StunClient ( StunClient &&  )
noexcept

Member Function Documentation

◆ binding_request()

StunResult librats::StunClient::binding_request ( const std::string &  server,
uint16_t  port = STUN_DEFAULT_PORT,
int  timeout_ms = 0 
)

Send STUN Binding Request to discover public address.

Parameters
serverSTUN server hostname or IP
portSTUN server port (default: 3478)
timeout_msTotal timeout in milliseconds (0 for config default)
Returns
Result containing mapped address or error

◆ binding_request_with_socket()

StunResult librats::StunClient::binding_request_with_socket ( socket_t  socket,
const std::string &  server,
uint16_t  port,
int  timeout_ms = 0 
)

Send STUN Binding Request using existing socket Useful when you need to discover the mapped address for a specific local socket.

Parameters
socketUDP socket to use (must be bound)
serverSTUN server hostname or IP
portSTUN server port
timeout_msTotal timeout in milliseconds
Returns
Result containing mapped address or error

◆ config()

const StunClientConfig & librats::StunClient::config ( ) const
inline

Get the configuration.

Definition at line 438 of file stun.h.

Referenced by set_config().

◆ operator=() [1/2]

StunClient & librats::StunClient::operator= ( const StunClient )
delete

◆ operator=() [2/2]

StunClient & librats::StunClient::operator= ( StunClient &&  )
noexcept

◆ send_request()

std::optional< StunMessage > librats::StunClient::send_request ( socket_t  socket,
const StunMessage request,
const std::string &  server,
uint16_t  port,
int  timeout_ms 
)

Send a raw STUN message and wait for response.

Parameters
socketUDP socket to use
requestSTUN message to send
serverDestination server
portDestination port
timeout_msTotal timeout
Returns
Response message or empty if timeout/error

◆ set_config()

void librats::StunClient::set_config ( const StunClientConfig config)
inline

Set configuration.

Definition at line 443 of file stun.h.

References config().


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