#codec #stun #rfc #attributes #decoder #encoder #transaction-id

stun_codec

Encoders and decoders for STUN (RFC 5389) and its extensions

21 releases

0.3.5 Apr 3, 2024
0.3.4 Oct 23, 2023
0.3.3 Sep 19, 2023
0.3.2 Jun 5, 2023
0.1.10 Sep 17, 2018

#111 in Encoding

Download history 2498/week @ 2023-12-23 5728/week @ 2023-12-30 7498/week @ 2024-01-06 5924/week @ 2024-01-13 8649/week @ 2024-01-20 12913/week @ 2024-01-27 9351/week @ 2024-02-03 10432/week @ 2024-02-10 14420/week @ 2024-02-17 15850/week @ 2024-02-24 19185/week @ 2024-03-02 15645/week @ 2024-03-09 15256/week @ 2024-03-16 11079/week @ 2024-03-23 13570/week @ 2024-03-30 11860/week @ 2024-04-06

53,436 downloads per month
Used in 10 crates (8 directly)

MIT license

175KB
3.5K SLoC

stun_codec

stun_codec Documentation Actions Status Coverage Status License: MIT

Encoders and decoders for STUN (RFC 5389) and its extensions.

Documentation

Examples

use bytecodec::{DecodeExt, EncodeExt, Error};
use stun_codec::{Message, MessageClass, MessageDecoder, MessageEncoder, TransactionId};
use stun_codec::rfc5389::{attributes::Software, methods::BINDING, Attribute};

// Creates a message
let mut message = Message::new(MessageClass::Request, BINDING, TransactionId::new([3; 12]));
message.add_attribute(Attribute::Software(Software::new("foo".to_owned())?));

// Encodes the message
let mut encoder = MessageEncoder::new();
let bytes = encoder.encode_into_bytes(message.clone())?;
assert_eq!(
    bytes,
    [
        0, 1, 0, 8, 33, 18, 164, 66, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 128, 34, 0, 3,
        102, 111, 111, 0
    ]
);

// Decodes the message
let mut decoder = MessageDecoder::<Attribute>::new();
let decoded = decoder.decode_from_bytes(&bytes)?.map_err(Error::from)?;
assert_eq!(decoded.class(), message.class());
assert_eq!(decoded.method(), message.method());
assert_eq!(decoded.transaction_id(), message.transaction_id());
assert!(decoded.attributes().eq(message.attributes()));

References

Dependencies

~2.5MB
~52K SLoC