#stun

stun_codec

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

15 releases

0.2.0 Mar 18, 2022
0.1.13 Feb 7, 2021
0.1.12 Mar 17, 2019
0.1.11 Feb 6, 2019
0.1.10 Sep 17, 2018

#335 in Encoding

Download history 7563/week @ 2022-11-28 7861/week @ 2022-12-05 7648/week @ 2022-12-12 7063/week @ 2022-12-19 6204/week @ 2022-12-26 7711/week @ 2023-01-02 7259/week @ 2023-01-09 6882/week @ 2023-01-16 6985/week @ 2023-01-23 6197/week @ 2023-01-30 6482/week @ 2023-02-06 5423/week @ 2023-02-13 4821/week @ 2023-02-20 4888/week @ 2023-02-27 5220/week @ 2023-03-06 5637/week @ 2023-03-13

21,021 downloads per month
Used in 7 crates

MIT license

155KB
3K 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

~1–1.5MB
~34K SLoC