rtci-stun
STUN messages, attributes and a sans-I/O client (RFC 8489), for WebRTC. 22 public modules, 196 items at 0.5.0.
cargo add [email protected] · crates.io · docs.rs · README on GitHub· MSRV 1.89
README
STUN messages and attributes, message integrity (HMAC-SHA1 and HMAC-SHA256), fingerprints, long-term credentials with the password algorithms and USERHASH, a validator that exposes only the attributes the integrity check covers, and a client transaction layer with the RFC 8489 retransmission timer.
Specifications
- RFC 8489, Session Traversal Utilities for NAT (STUN)
- RFC 5780, NAT behaviour discovery attributes
- RFC 8656 and RFC 6062, the attributes TURN adds
- RFC 7983, multiplexing STUN with DTLS, RTP and others
Clause-by-clause status, with the test that proves each clause: rfc8489, rfc5780, rfc7983.
Use
message::Message builds, marshals and parses messages; validated::Validator checks integrity and fingerprint before an attribute is read; client::Client runs transactions. rtci-ice and rtci-turn are built on it.
Sans-I/O
The client is a state machine driven by the caller's datagrams and Instants. The crate does no I/O and reads no clock: every call that depends on time takes an Instant from the caller. Integrity uses rtci-crypto's provider.
Licence
Licensed under either of the MIT licence or the Apache License, Version 2.0, at your option.
This crate is part of the rtci workspace, which descends from webrtc-rs/rtc and is developed independently; FORK.md lists what it carries over upstream. Upstream's licence and copyright notices are retained.
rtci_stun
STUN for the Sans-I/O WebRTC stack.
- mod
addrSocket-address helpers shared by the address attributes. - mod
agentTransaction tracking: which requests are outstanding and when they time out. - mod
alternateThe client side of 300 (Try Alternate):ALTERNATE-SERVERand the redirection-loop rule. - mod
attributesSTUN attribute types. - mod
checksValidation helpers for received messages and attributes. - mod
clientA Sans-I/O STUN client for talking to a STUN server. - mod
error_codeTheERROR-CODEattribute and the codes defined by STUN, TURN and ICE. - mod
fingerprintTheFINGERPRINTattribute, a CRC-32 over the message. - mod
integrityTheMESSAGE-INTEGRITYandMESSAGE-INTEGRITY-SHA256attributes, HMACs over the message. - mod
long_termThe client side of the long-term credential mechanism. - mod
messageThe STUN message itself: header, attributes, and encoding. - mod
nonce_cookieThe nonce cookie and its STUN Security Feature bits. - mod
opaqueThe OpaqueString profile (RFC 8265) for user names, realms and passwords. - mod
password_algorithmThePASSWORD-ALGORITHMandPASSWORD-ALGORITHMSattributes. - mod
textattrsText-valued attributes such asUSERNAME,REALMandSOFTWARE. - mod
timerThe retransmission timer of RFC 8489 section 6.2, shared by every transaction layer. - mod
uattrsTheUNKNOWN-ATTRIBUTESattribute, listing attributes a server could not process. - mod
uriParsingstun:andstuns:URIs. - mod
userhashTheUSERHASHattribute. - mod
validatedChecking a received message and reading only what its integrity covers. - mod
xoraddrTheXOR-MAPPED-ADDRESSattribute, whose value is masked with the magic cookie. - const
DEFAULT_PORTIANA assigned ports for "stun" protocol. - const
DEFAULT_TLS_PORTThe default port forstuns:(STUN over TLS/DTLS). - use
crypto
rtci_stun::addr
Socket-address helpers shared by the address attributes.
- struct
MappedAddressMappedAddress represents MAPPED-ADDRESS attribute.
rtci_stun::agent
Transaction tracking: which requests are outstanding and when they time out.
- struct
AgentAgent is low-level abstraction over transaction list that handles concurrency and time outs (via Collect call). - struct
EventEvent is passed to Handler describing the transaction event. Do not reuse outside Handler. - enum
ClientAgentClientAgent is Agent implementation that is used by Client to process transactions. - enum
StunEventWhat became of a STUN transaction. - enum
TransactionFailureWhy a transaction failed although the server answered.
rtci_stun::alternate
The client side of 300 (Try Alternate): ALTERNATE-SERVER and the redirection-loop rule.
- struct
RedirectGuardRemembers which servers a request went to, so that a redirection back to one of them within five minutes is refused (RFC 8489 section 10: the client "MUST ignore the redirection and consider the transaction to have failed"). - struct
TryAlternateWhat a 300 (Try Alternate) error response says. - fn
is_usable_alternateWhetherserver, read from an ALTERNATE-SERVER, can name "a different STUN server that the STUN client should try" (RFC 8489 section 14.15) for a client that reached the redirecting server atoriginal. - const
REDIRECT_LOOP_WINDOWHow long a server counts as already tried (RFC 8489 section 10: "within the last five minutes").
rtci_stun::attributes
STUN attribute types.
- struct
AttributesAttributes is list of message attributes. - struct
AttrTypeAttrType is attribute type. - struct
RawAttributeRawAttribute is a Type-Length-Value (TLV) object that can be added to a STUN message. Attributes are divided into two types: comprehension-required and comprehension-optional. STUN agents can safely ignore comprehension-optional attributes they don't understand, but cannot successfully process a message if it contains comprehension-required attributes that are not understood. - const
ATTR_ALTERNATE_DOMAINALTERNATE-DOMAIN. - const
ATTR_ALTERNATE_SERVERALTERNATE-SERVER. - const
ATTR_CACHE_TIMEOUTCACHE-TIMEOUT. - const
ATTR_CHANGE_REQUESTAttributes from RFC 5780 NAT Behavior Discovery CHANGE-REQUEST. - const
ATTR_CHANGED_ADDRESSCHANGED-ADDRESS. - const
ATTR_CHANNEL_NUMBERAttributes from RFC 5766 TURN. CHANNEL-NUMBER. - const
ATTR_CONNECTION_IDAttributes from RFC 6062 TURN Extensions for TCP Allocations. CONNECTION-ID. - const
ATTR_DATADATA. - const
ATTR_DONT_FRAGMENTDONT-FRAGMENT. - const
ATTR_ERROR_CODEERROR-CODE. - const
ATTR_EVEN_PORTEVEN-PORT. - const
ATTR_FINGERPRINTFINGERPRINT. - const
ATTR_ICE_CONTROLLEDICE-CONTROLLED. - const
ATTR_ICE_CONTROLLINGICE-CONTROLLING. - const
ATTR_LIFETIMELIFETIME. - const
ATTR_MAPPED_ADDRESSAttributes from comprehension-required range (0x0000-0x7FFF). MAPPED-ADDRESS. - const
ATTR_MESSAGE_INTEGRITYMESSAGE-INTEGRITY. - const
ATTR_MESSAGE_INTEGRITY_SHA256Attributes from RFC 8489 STUN. MESSAGE-INTEGRITY-SHA256. - const
ATTR_NETWORK_COSTNETWORK-COST. - const
ATTR_NONCENONCE. - const
ATTR_ORIGINAttributes from An Origin Attribute for the STUN Protocol. - const
ATTR_OTHER_ADDRESSOTHER-ADDRESS. - const
ATTR_PADDINGPADDING. - const
ATTR_PASSWORD_ALGORITHMPASSWORD-ALGORITHM. - const
ATTR_PASSWORD_ALGORITHMSPASSWORD-ALGORITHMS. - const
ATTR_PRIORITYAttributes from RFC 5245 ICE. PRIORITY. - const
ATTR_REALMREALM. - const
ATTR_REQUESTED_ADDRESS_FAMILYAttributes from RFC 6156 TURN IPv6. REQUESTED-ADDRESS-FAMILY. - const
ATTR_REQUESTED_TRANSPORTREQUESTED-TRANSPORT. - const
ATTR_RESERVATION_TOKENRESERVATION-TOKEN. - const
ATTR_RESPONSE_ORIGINRESPONSE-ORIGIN. - const
ATTR_RESPONSE_PORTRESPONSE-PORT. - const
ATTR_SOFTWAREAttributes from comprehension-optional range (0x8000-0xFFFF). SOFTWARE. - const
ATTR_SOURCE_ADDRESSAttributes from RFC 3489, removed by RFC 5389, but still used by RFC5389-implementing software like Vovida.org, reTURNServer, etc. SOURCE-ADDRESS. - const
ATTR_UNKNOWN_ATTRIBUTESUNKNOWN-ATTRIBUTES. - const
ATTR_USE_CANDIDATEUSE-CANDIDATE. - const
ATTR_USER_HASHUSER-HASH. - const
ATTR_USERNAMEUSERNAME. - const
ATTR_XOR_PEER_ADDRESSXOR-PEER-ADDRESS. - const
ATTR_XOR_RELAYED_ADDRESSXOR-RELAYED-ADDRESS. - const
ATTR_XORMAPPED_ADDRESSXOR-MAPPED-ADDRESS. - const
STUN_ATTRIBUTESThe comprehension-required attributes RFC 8489 itself defines (section 18.3), which every STUN agent understands.
rtci_stun::checks
Validation helpers for received messages and attributes.
- fn
check_overflowCheck_overflow returns ErrAttributeSizeOverflow if got is bigger that max. - fn
check_sizeCheck_size returns ErrAttrSizeInvalid if got is not equal to expected. - fn
is_attr_size_invalidIs_attr_size_invalid returns true if error means that attribute size is invalid. - fn
is_attr_size_overflowIs_attr_size_overflow returns true if error means that attribute size is too big.
rtci_stun::client
A Sans-I/O STUN client for talking to a STUN server.
- struct
ClientClient simulates "connection" to STUN server. - struct
ClientBuilderBuilds aClientwith a chosen transaction timeout, retransmission schedule and handler. - struct
ClientTransactionClientTransaction represents transaction in progress. If transaction is succeed or failed, f will be called provided by event. Concurrent access is invalid. - struct
TaggedMessageAMessagetogether with the instant the caller is sending it at.
rtci_stun::error_code
The ERROR-CODE attribute and the codes defined by STUN, TURN and ICE.
- struct
ErrorCodeA STUN error code, as carried inERROR-CODE. - struct
ErrorCodeAttributeTheERROR-CODEattribute: a numeric code and a human-readable reason. - const
CODE_ADDR_FAMILY_NOT_SUPPORTEDError codes from RFC 6156. - const
CODE_ALLOC_MISMATCHAllocation Mismatch. - const
CODE_ALLOC_QUOTA_REACHEDAllocation Quota Reached. - const
CODE_BAD_REQUEST400 Bad Request: the request was malformed. - const
CODE_CONN_ALREADY_EXISTSError codes from RFC 6062. - const
CODE_CONN_TIMEOUT_OR_FAILURE447 Connection Timeout or Failure: the TURN TCP connection to the peer failed. - const
CODE_FORBIDDENError codes from RFC 5766. - const
CODE_INSUFFICIENT_CAPACITYInsufficient Capacity. - const
CODE_PEER_ADDR_FAMILY_MISMATCHPeer Address Family Mismatch. - const
CODE_ROLE_CONFLICT487 Role Conflict: both ICE agents claimed the same role. - const
CODE_SERVER_ERROR500 Server Error: a temporary failure on the server. - const
CODE_STALE_NONCE438 Stale Nonce: the nonce expired; retry with the one in this response. - const
CODE_TRY_ALTERNATEPossible error codes. - const
CODE_UNAUTHENTICATED401 Unauthenticated, under the name RFC 8489 gives it. - const
CODE_UNAUTHORISEDDEPRECATED constants. DEPRECATED, use CODE_UNAUTHORIZED. - const
CODE_UNAUTHORIZED401 Unauthenticated (RFC 8489; "Unauthorized" in RFC 5389): authentication is required, or the credentials were wrong. - const
CODE_UNKNOWN_ATTRIBUTE420 Unknown Attribute: the request carried a comprehension-required attribute the server does not understand. - const
CODE_UNSUPPORTED_TRANS_PROTOUnsupported Transport Protocol. - const
CODE_WRONG_CREDENTIALSWrong Credentials.
rtci_stun::fingerprint
The FINGERPRINT attribute, a CRC-32 over the message.
- struct
FingerprintAttrFINGERPRINT attribute. - fn
fingerprint_valueComputes theFINGERPRINTvalue overb: CRC-32 XORed withFINGERPRINT_XOR_VALUE. - const
FINGERPRINTShorthand for FingerprintAttr. - const
FINGERPRINT_SIZEThe attribute's value length in bytes. - const
FINGERPRINT_XOR_VALUEThe value the CRC-32 is XORed with,0x5354554e— ASCIISTUN.
rtci_stun::integrity
The MESSAGE-INTEGRITY and MESSAGE-INTEGRITY-SHA256 attributes, HMACs over the message.
- struct
MessageIntegrityTheMESSAGE-INTEGRITYkey: an HMAC-SHA1 is computed over the message with it. - struct
MessageIntegritySha256TheMESSAGE-INTEGRITY-SHA256key: an HMAC-SHA256 is computed over the message with it (RFC 8489 section 14.6). - enum
IntegrityAlgorithmWhich of the two integrity attributes protects a message. - fn
long_term_keyThe key of the long-term credential mechanism for a password algorithm of the STUN Password Algorithms registry (RFC 8489 sections 9.2.2 and 18.5.1): - const
MESSAGE_INTEGRITY_SHA256_MIN_SIZEThe shortestMESSAGE-INTEGRITY-SHA256value RFC 8489 section 14.6 allows, in bytes. - const
MESSAGE_INTEGRITY_SHA256_SIZEThe length of an untruncatedMESSAGE-INTEGRITY-SHA256value in bytes. - const
MESSAGE_INTEGRITY_SIZEThe length of aMESSAGE-INTEGRITYvalue in bytes.
rtci_stun::long_term
The client side of the long-term credential mechanism.
- struct
LongTermAuthenticationThe authentication attributes of a request, as aSetter: seeLongTermCredentials::setter. - struct
LongTermCredentialsA client's long-term credentials for one server, and what the server's challenges added to them. See the module documentation. - enum
ChallengeErrorWhy a 401 or 438 error response does not lead to a retry. - enum
LongTermResponseWhat a response to a request of this client is, once checked.
rtci_stun::message
The STUN message itself: header, attributes, and encoding.
- struct
MessageA STUN message: type, transaction id, and attributes, plus the encoded bytes. - struct
MessageClassA STUN message class: request, indication, success response or error response. - struct
MessageTypeA message's class and method together, as encoded in the first two header bytes. - struct
MethodA STUN method, such as Binding or one of TURN's. - struct
TransactionIdA 96-bit transaction id, which pairs a response with its request. - trait
CheckerChecker checks *Message attribute. - trait
GetterGetter parses attribute from *Message. - trait
SetterInterfaces that are implemented by message attributes, shorthands for them, or helpers for message fields as type or transaction id. - fn
is_stun_messageIs_stun_message returns true if b looks like STUN message: at least a header, the first two bits zero and the magic cookie in place (RFC 8489 section 6.3). Useful for multiplexing. is_stun_message does not guarantee that decoding will be successful. - const
ATTRIBUTE_HEADER_SIZEBytes of type and length preceding each attribute value. - const
BINDING_ERRORBinding error response message type. - const
BINDING_REQUESTCommon STUN message types. Binding request message type. - const
BINDING_SUCCESSBinding success response message type. - const
CLASS_ERROR_RESPONSE0b11. - const
CLASS_INDICATION0b01. - const
CLASS_REQUESTPossible values for message class in STUN Message Type. 0b00. - const
CLASS_SUCCESS_RESPONSE0b10. - const
MAGIC_COOKIEFixed value that aids in distinguishing STUN packets from packets of other protocols when STUN is multiplexed with those other protocols on the same Port. - const
MESSAGE_HEADER_SIZEBytes in the STUN message header. - const
METHOD_ALLOCATETURN Allocate: asks a relay for a public address. - const
METHOD_BINDINGPossible methods for STUN Message. - const
METHOD_CHANNEL_BINDTURN ChannelBind: binds a channel number to a peer for compact framing. - const
METHOD_CONNECTMethods from RFC 6062. - const
METHOD_CONNECTION_ATTEMPTTURN-TCP ConnectionAttempt: notifies the client of an inbound TCP connection. - const
METHOD_CONNECTION_BINDTURN-TCP ConnectionBind: associates a new TCP connection with an allocation. - const
METHOD_CREATE_PERMISSIONTURN CreatePermission: authorizes traffic to and from a peer address. - const
METHOD_DATATURN Data: an indication carrying data from a peer through the relay. - const
METHOD_REFRESHTURN Refresh: extends or releases an allocation. - const
METHOD_SENDTURN Send: an indication carrying data to a peer through the relay. - const
TRANSACTION_ID_SIZELength of transaction id array (in bytes). 96 bit.
rtci_stun::nonce_cookie
The nonce cookie and its STUN Security Feature bits.
- struct
SecurityFeaturesThe 24 STUN Security Feature bits of a nonce cookie. - const
NONCE_COOKIE_LENThe length of the nonce cookie: the prefix and four base64 characters. - const
NONCE_COOKIE_PREFIXWhat a nonce that carries the cookie starts with.
rtci_stun::opaque
The OpaqueString profile (RFC 8265) for user names, realms and passwords.
- fn
opaque_stringApplies the OpaqueString profile tos.
rtci_stun::password_algorithm
The PASSWORD-ALGORITHM and PASSWORD-ALGORITHMS attributes.
- struct
PasswordAlgorithmOne entry of the STUN Password Algorithms registry with its parameters: the value ofPASSWORD-ALGORITHM, and an element ofPASSWORD-ALGORITHMS. - struct
PasswordAlgorithmsThePASSWORD-ALGORITHMSattribute: the algorithms a server can derive the long-term key with, in its order of preference. - const
PASSWORD_ALGORITHM_MD5MD5, the key of RFC 5389 (RFC 8489 section 18.5.1.1): for legacy systems only. - const
PASSWORD_ALGORITHM_SHA256SHA-256 (RFC 8489 section 18.5.1.2).
rtci_stun::textattrs
Text-valued attributes such as USERNAME, REALM and SOFTWARE.
- struct
TextAttributeA text-valued attribute:USERNAME,REALM,NONCE,SOFTWAREorALTERNATE-DOMAIN. - type
AlternateDomainALTERNATE-DOMAIN attribute. - type
NonceNONCE attribute. - type
RealmREALM attribute. - type
SoftwareSOFTWARE attribute. - type
UsernameUSERNAME attribute. - const
MAX_ALTERNATE_DOMAIN_BThe longest ALTERNATE-DOMAIN, in bytes (RFC 8489 section 14.16). - const
MAX_TEXT_BThe longest REALM, NONCE, SOFTWARE or reason phrase that is sent, in bytes (RFC 8489 sections 14.8 to 14.10 and 14.14). - const
MAX_TEXT_CHARSREALM, NONCE, SOFTWARE and the reason phrase are fewer than this many characters. - const
MAX_USERNAME_BThe longest USERNAME that is sent: "fewer than 509 bytes" (RFC 8489 section 14.3).
rtci_stun::timer
The retransmission timer of RFC 8489 section 6.2, shared by every transaction layer.
- struct
RtoEstimatorThe retransmission timeout of RFC 6298, as RFC 8489 section 6.2.1 uses it: kept per server, to 1 ms, never below the configured initial value, and forgotten after ten minutes without a transaction. - struct
ScheduleThe parameters of the retransmission schedule (RFC 8489 sections 6.2.1 and 6.2.2). The default is the RFC's: Rc 7, Rm 16, Ti 39.5 s. - fn
afternow + d, without the panicInstantreserves for an overflow. - fn
is_server_errorWhethercodeis one of the server errors (500 through 599) after which RFC 8489 section 6.3.4 lets a client resend its request. - fn
server_error_resend_delayHow long a client waits, after the server-error response, before theresend-th resend of a request (1 for the first): the interval its retransmission of the same number would wait (RFC 8489 section 6.2.1: RTO, then twice that, and so on), so that four resends are spread over RTO, 2 RTO, 4 RTO and 8 RTO (7.5 s at 500 ms) and a failing server is not asked again at the rate it answers. Each … - const
DEFAULT_MAX_OUTSTANDINGRFC 8489 section 6.2: outstanding transactions to one server. - const
DEFAULT_RCRFC 8489 section 6.2.1: Rc, the number of times a request is sent. - const
DEFAULT_RMRFC 8489 section 6.2.1: Rm, the last wait in units of RTO. - const
DEFAULT_RTORFC 8489 section 6.2.1: the initial RTO "SHOULD be greater than or equal to 500 ms". - const
DEFAULT_SERVER_ERROR_RESENDSRFC 8489 section 6.3.4: after an error response of 500 through 599 "the client MAY resend the request; clients that do so MUST limit the number of times they do this. Unless a specific error code specifies a different value, the number of retransmissions SHOULD be limited to 4." The engine's clients resend that many times by default. - const
DEFAULT_TIRFC 8489 section 6.2.2: Ti, how long a request over a reliable transport waits. - const
RTO_CACHE_LIFETIMERFC 8489 section 6.2.1: a cached RTO is stale after ten minutes without a transaction.
rtci_stun::uattrs
The UNKNOWN-ATTRIBUTES attribute, listing attributes a server could not process.
- struct
UnknownAttributesUNKNOWN-ATTRIBUTES attribute.
rtci_stun::uri
Parsing stun: and stuns: URIs.
- struct
UriA parsedstun:orstuns:URI. - const
SCHEMEThestun:URI scheme. - const
SCHEME_SECUREThestuns:URI scheme, for STUN over TLS or DTLS.
rtci_stun::userhash
The USERHASH attribute.
- struct
UserhashTheUSERHASHattribute:SHA-256(OpaqueString(username) ":" OpaqueString(realm)). - const
USERHASH_SIZEThe length of a USERHASH value in bytes.
rtci_stun::validated
Checking a received message and reading only what its integrity covers.
- struct
ValidatedMessageA received message that passed aValidator, reduced to what its checks cover. - struct
ValidatorThe checks a usage applies to every message it receives. - enum
RejectionWhy a received message was not accepted. - fn
unknown_attributes_responseBuilds the answer to a request that failed withRejection::UnknownAttributes: an error response of the request's method and transaction id with ERROR-CODE 420 (Unknown Attribute) and the attributes in UNKNOWN-ATTRIBUTES (RFC 8489 sections 6.3.1 and 14.8).
rtci_stun::xoraddr
The XOR-MAPPED-ADDRESS attribute, whose value is masked with the magic cookie.
- struct
XorMappedAddressXORMappedAddress implements XOR-MAPPED-ADDRESS attribute. - fn
xor_bytesxor_bytes xors the bytes in a and b. The destination is assumed to have enough space. Returns the number of bytes xor'd.
Generated from the sources of rtci-stun 0.5.0 at its release tag. Items made by macros and methods are on docs.rs.