stun-bytes
A low-level base for STUN message protocol parsers.
Highlights
- Zero-copy
- No-std
- RFC-agnostic
STUN Message structure
Header
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Message Type (16 bits) | Message Length (16 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Magic Cookie (32 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| Transaction ID (96 bits) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Note: since RFC5389,
Transaction ID
field has been split into Magic Cookie
and Transaction ID
.
If you want to follow this split, enable the cookie
feature.
Attribute
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type (16 bits) | Length (16 bits) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Value (variable) ..
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Examples
Parse STUN message
const MSG: = ;
let msg = from_arr;
assert_eq!; // read type field
assert_eq!; // read length field
assert_eq!; // read cookie field (enable 'cookie' feature first)
assert_eq!; // read transaction id field
assert_eq!; // read all attribute bytes
let attr = msg.attrs_iter.next?; // iterate over attributes
assert_eq!; // read attribute type field
assert_eq!; // read attribute length field
assert_eq!; // read attribute value field
Create STUN message
const MSG: = ;
let mut buf = ;
let mut msg = from_arr_mut;
msg.typ?.copy_from; // write type field
// msg.len()?.copy_from(MSG.carve(2)?); // length field updates automatically
msg.cookie?.copy_from; // write cookie field
msg.tid?.copy_from; // write transaction id field
msg.add_attr; // write attribute (type, length, value)
assert_eq!;
Contribution guidelines
Pull requests are welcome. Please make sure your contribution adheres to the Principles section above.