1个不稳定版本

0.1.13 2022年2月19日

编码 中排名 1545

Download history 6316/week @ 2024-03-07 5978/week @ 2024-03-14 5522/week @ 2024-03-21 3746/week @ 2024-03-28 7486/week @ 2024-04-04 5571/week @ 2024-04-11 7638/week @ 2024-04-18 5259/week @ 2024-04-25 5562/week @ 2024-05-02 7037/week @ 2024-05-09 7880/week @ 2024-05-16 6175/week @ 2024-05-23 6686/week @ 2024-05-30 6041/week @ 2024-06-06 7942/week @ 2024-06-13 4038/week @ 2024-06-20

每月下载量 25,696
用于 nat-detect

MIT 许可证

155KB
3.5K SLoC

源自 stun_codec


lib.rs:

为STUN (RFC 5389) 及其扩展提供编码和解码器。

示例

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

定义自己的属性枚举

通过使用 define_attribute_enums! 宏,您可以轻松定义包含任意属性的自定义枚举。

以下是从 rusturn 包中摘取的示例

#[macro_use] extern crate trackable;

use stun_codec::define_attribute_enums;
use stun_codec::rfc5389::attributes::*;
use stun_codec::rfc5766::attributes::*;

define_attribute_enums!(
    Attribute, AttributeDecoder, AttributeEncoder,
    [
        // RFC 5389
        MappedAddress, Username, MessageIntegrity, ErrorCode,
        UnknownAttributes, Realm, Nonce, XorMappedAddress,
        Software, AlternateServer, Fingerprint,

        // RFC 5766
        ChannelNumber, Lifetime, XorPeerAddress, Data,
        XorRelayAddress, EvenPort, RequestedTransport,
        DontFragment, ReservationToken
    ]
);

参考

依赖项

~2MB
~45K SLoC