Back to Site
Loading...
Searching...
No Matches
stun.h File Reference

STUN (Session Traversal Utilities for NAT) Protocol Implementation. More...

#include "socket.h"
#include <array>
#include <vector>
#include <string>
#include <optional>
#include <cstdint>
#include <memory>
#include <chrono>
#include <random>
Include dependency graph for stun.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  librats::StunMappedAddress
 STUN mapped address (result of binding request) More...
 
struct  librats::StunError
 STUN error information. More...
 
struct  librats::StunAttribute
 STUN attribute base class. More...
 
struct  librats::StunMessage
 STUN message structure. More...
 
struct  librats::StunClientConfig
 STUN client configuration. More...
 
struct  librats::StunResult
 STUN transaction result. More...
 
class  librats::StunClient
 STUN Client for NAT traversal. More...
 

Namespaces

namespace  librats
 

Enumerations

enum class  librats::StunMessageClass : uint8_t { librats::Request = 0x00 , librats::Indication = 0x01 , librats::SuccessResponse = 0x02 , librats::ErrorResponse = 0x03 }
 STUN message class (2 bits) More...
 
enum class  librats::StunMethod : uint16_t {
  librats::Binding = 0x001 , librats::Allocate = 0x003 , librats::Refresh = 0x004 , librats::Send = 0x006 ,
  librats::Data = 0x007 , librats::CreatePermission = 0x008 , librats::ChannelBind = 0x009
}
 STUN method (12 bits, only Binding is defined in RFC 5389) More...
 
enum class  librats::StunMessageType : uint16_t {
  librats::BindingRequest = 0x0001 , librats::BindingIndication = 0x0011 , librats::BindingSuccessResponse = 0x0101 , librats::BindingErrorResponse = 0x0111 ,
  librats::AllocateRequest = 0x0003 , librats::AllocateSuccessResponse = 0x0103 , librats::AllocateErrorResponse = 0x0113 , librats::RefreshRequest = 0x0004 ,
  librats::RefreshSuccessResponse = 0x0104 , librats::RefreshErrorResponse = 0x0114 , librats::SendIndication = 0x0016 , librats::DataIndication = 0x0017 ,
  librats::CreatePermissionRequest = 0x0008 , librats::CreatePermissionSuccessResponse = 0x0108 , librats::CreatePermissionErrorResponse = 0x0118 , librats::ChannelBindRequest = 0x0009 ,
  librats::ChannelBindSuccessResponse = 0x0109 , librats::ChannelBindErrorResponse = 0x0119
}
 Combined STUN message type (method + class) More...
 
enum class  librats::StunAttributeType : uint16_t {
  librats::MappedAddress = 0x0001 , librats::Username = 0x0006 , librats::MessageIntegrity = 0x0008 , librats::ErrorCode = 0x0009 ,
  librats::UnknownAttributes = 0x000A , librats::Realm = 0x0014 , librats::Nonce = 0x0015 , librats::XorMappedAddress = 0x0020 ,
  librats::ChannelNumber = 0x000C , librats::Lifetime = 0x000D , librats::XorPeerAddress = 0x0012 , librats::Data = 0x0013 ,
  librats::XorRelayedAddress = 0x0016 , librats::RequestedTransport = 0x0019 , librats::DontFragment = 0x001A , librats::Software = 0x8022 ,
  librats::AlternateServer = 0x8023 , librats::Fingerprint = 0x8028
}
 
enum class  librats::StunAddressFamily : uint8_t { librats::IPv4 = 0x01 , librats::IPv6 = 0x02 }
 
enum class  librats::StunErrorCode : uint16_t {
  librats::TryAlternate = 300 , librats::BadRequest = 400 , librats::Unauthorized = 401 , librats::Forbidden = 403 ,
  librats::UnknownAttribute = 420 , librats::StaleNonce = 438 , librats::ServerError = 500 , librats::AllocationMismatch = 437 ,
  librats::WrongCredentials = 441 , librats::UnsupportedTransportProtocol = 442 , librats::AllocationQuotaReached = 486 , librats::InsufficientCapacity = 508
}
 

Functions

uint32_t librats::stun_crc32 (const uint8_t *data, size_t length)
 Compute CRC32 for STUN FINGERPRINT attribute Uses CRC-32 as defined in RFC 5389 (ISO 3309)
 
std::array< uint8_t, 20 > librats::stun_hmac_sha1 (const std::vector< uint8_t > &key, const std::vector< uint8_t > &data)
 Compute HMAC-SHA1 for MESSAGE-INTEGRITY attribute.
 
std::vector< uint8_t > librats::stun_compute_long_term_key (const std::string &username, const std::string &realm, const std::string &password)
 Compute long-term credential key: MD5(username:realm:password)
 
StunMappedAddress librats::stun_xor_address (const StunMappedAddress &addr, const std::array< uint8_t, STUN_TRANSACTION_ID_SIZE > &transaction_id)
 XOR an address with the magic cookie and transaction ID Used for XOR-MAPPED-ADDRESS encoding/decoding.
 
std::vector< std::pair< std::string, uint16_t > > librats::get_public_stun_servers ()
 Get a list of well-known public STUN servers.
 

Variables

constexpr uint32_t librats::STUN_MAGIC_COOKIE = 0x2112A442
 STUN Magic Cookie (fixed value per RFC 5389)
 
constexpr size_t librats::STUN_HEADER_SIZE = 20
 STUN header size in bytes.
 
constexpr size_t librats::STUN_TRANSACTION_ID_SIZE = 12
 STUN transaction ID size in bytes.
 
constexpr uint16_t librats::STUN_DEFAULT_PORT = 3478
 Default STUN port.
 
constexpr uint16_t librats::STUNS_DEFAULT_PORT = 5349
 STUN over TLS default port.
 
constexpr size_t librats::STUN_MAX_MESSAGE_SIZE = 1500
 Maximum STUN message size (RFC 5389 recommends path MTU, typically ~1500)
 
constexpr int librats::STUN_DEFAULT_RTO_MS = 500
 Default retransmission timeout (ms)
 
constexpr int librats::STUN_MAX_RETRANSMISSIONS = 7
 Maximum retransmissions.
 

Detailed Description

STUN (Session Traversal Utilities for NAT) Protocol Implementation.

Implements RFC 5389 - STUN protocol for NAT traversal. Provides functionality to discover public IP address and port mappings.

Definition in file stun.h.