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

无需 std stun-format

所有 RFC STUN 消息的 no-std 解析器

2 个稳定版本

1.0.1 2022 年 11 月 7 日

#16 in #stun

Download history 204/week @ 2024-03-15 233/week @ 2024-03-22 187/week @ 2024-03-29 216/week @ 2024-04-05 161/week @ 2024-04-12 209/week @ 2024-04-19 166/week @ 2024-04-26 217/week @ 2024-05-03 238/week @ 2024-05-10 253/week @ 2024-05-17 240/week @ 2024-05-24 186/week @ 2024-05-31 207/week @ 2024-06-07 262/week @ 2024-06-14 214/week @ 2024-06-21 194/week @ 2024-06-28

902 每月下载量

MIT 许可证

175KB
4K SLoC

stun-format

Github Crates.io Codacy grade Crates.io

所有 RFC 的 no-std 解析器。

亮点

  • 所有 RFC 支持。
  • 可以通过功能标志启用/禁用每个 RFC 的支持。
  • 无需 std。
  • 100% 代码覆盖率。

示例

读取 STUN 消息

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 }

创建 STUN 消息

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());

贡献指南

欢迎提交拉取请求。请确保您的贡献遵守上面的 原则 部分。

依赖项