#stun #message-parser #zero-copy #io #stun-parser

no-std stun-format

No-std parser for all RFCs STUN messages

2 stable releases

1.0.1 Nov 7, 2022

#18 in #stun

Download history 300/week @ 2024-11-21 248/week @ 2024-11-28 75/week @ 2024-12-05 152/week @ 2024-12-12 188/week @ 2024-12-19 26/week @ 2024-12-26 35/week @ 2025-01-02 22/week @ 2025-01-09 18/week @ 2025-01-16 19/week @ 2025-01-23 16/week @ 2025-01-30 638/week @ 2025-02-06 86/week @ 2025-02-13 353/week @ 2025-02-20 271/week @ 2025-02-27 318/week @ 2025-03-06

1,045 downloads per month
Used in 5 crates (2 directly)

MIT license

175KB
4K SLoC

stun-format

Github Crates.io Codacy grade Crates.io

No-std parser for all RFC STUN messages.

Highlights

  • All RFCs supported.
  • Support for each RFC can be enabled/disabled using feature flags.
  • No-std.
  • 100% code coverage.

Examples

Read STUN message

const MSG: [u8; 28] = [
    0x00, 0x01,                         // type: Binding Request
    0x00, 0x08,                         // length: 8 (header does not count)
    0x21, 0x12, 0xA4, 0x42,             // magic cookie
    0x00, 0x00, 0x00, 0x00,             
    0x00, 0x00, 0x00, 0x00,             
    0x00, 0x00, 0x00, 0x01,             // transaction id (16 bytes total incl. magic cookie)
    0x00, 0x03,                         // type: ChangeRequest
    0x00, 0x04,                         // length: 4 (only value bytes count)
    0x00, 0x00, 0x00, 0x40 | 0x20,      // change both ip and port
];

let msg = Msg::from(MSG.as_slice());

let typ = msg.typ()?;                   // MsgType::BindingRequest
let cookie = msg.cookie()?;             // 0x2112A442
let transaction_id = msg.tid()?;        // 0x01
let attr = msg.attrs_iter().next()?;    // Attr::ChangeRequest { change_ip: true, change_port: true }

Create STUN message

const MSG: [u8; 28] = [
    0x00, 0x01,                         // type: Binding Request
    0x00, 0x08,                         // length: 8 (header does not count)
    0x21, 0x12, 0xA4, 0x42,             // magic cookie
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x01,             // transaction id (16 bytes total incl. magic cookie)
    0x00, 0x03,                         // type: ChangeRequest
    0x00, 0x04,                         // length: 4 (only value bytes count)
    0x00, 0x00, 0x00, 0x40 | 0x20,      // change both ip and port
];

let mut buf = [0u8; MSG.len()];
let mut msg = MsgBuilder::from(buf.as_mut_slice());

msg.typ(MsgType::BindingRequest)?;
msg.tid(1)?;
msg.add_attr(Attr::ChangeRequest { change_ip: true, change_port: true })?;

assert_eq!(&MSG, msg.as_bytes());

Contribution guidelines

Pull requests are welcome. Please make sure your contribution adheres to the Principles section above.

Dependencies