diff options
-rw-r--r-- | src/vrrpv2.rs | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/vrrpv2.rs b/src/vrrpv2.rs index baf69cf..989a36b 100644 --- a/src/vrrpv2.rs +++ b/src/vrrpv2.rs @@ -133,7 +133,31 @@ fn checksum(bytes: &[u8]) -> u16 { } !(sum as u16) } - +/// Parse and validate a byte array to construct a VRRPv2 struct. +/// +/// # Examples +/// +/// ``` +/// use vrrpd::vrrpv2::VRRPv2; +/// use vrrpd::vrrpv2::VRRPv2AuthType; +/// use vrrpd::vrrpv2::from_bytes; +/// use std::net::Ipv4Addr; +/// +/// let bytes = [ +/// 0x21, 0x01, 0x64, 0x01, 0x00, 0x01, 0xba, 0x52, 0xc0, 0xa8, 0x00, 0x01, 0x00, 0x00, 0x00, +/// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +/// ]; +/// let expected = VRRPv2 { +/// virtual_router_id: 1, +/// priority: 100, +/// count_ip_addrs: 1, +/// auth_type: VRRPv2AuthType::VRRPv2AuthNoAuth, +/// checksum: 47698, +/// advertisement_interval: 1, +/// ip_addrs: vec![Ipv4Addr::from([192, 168, 0, 1])], +/// }; +/// assert_eq!(from_bytes(&bytes), Ok(expected)); +/// ``` pub fn from_bytes(bytes: &[u8]) -> Result<VRRPv2, VRRPv2Error> { let vrrpv2 = parse(bytes)?; if checksum(bytes) != 0 { |