summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSunil Nimmagadda <sunil@nimmagadda.net>2025-07-01 23:44:02 +0530
committerSunil Nimmagadda <sunil@nimmagadda.net>2025-07-01 23:44:02 +0530
commit2c440c9f176da6e8e80ced0bf68279f14f5d2e0f (patch)
treeb69c8373e3221bc9d645420fafc099e2ced62251
parent0c7dd54139470b2c458da5fed40c3aa7d19d87e8 (diff)
Check for invalid IP count.
-rw-r--r--src/vrrpv2.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/vrrpv2.rs b/src/vrrpv2.rs
index bcb2315..4e53b20 100644
--- a/src/vrrpv2.rs
+++ b/src/vrrpv2.rs
@@ -44,6 +44,7 @@ pub struct VRRPv2 {
pub enum VRRPv2Error {
InvalidAuthType,
InvalidChecksum,
+ InvalidIPCount,
InvalidTTL,
InvalidType,
InvalidVersion,
@@ -55,6 +56,7 @@ impl std::fmt::Display for VRRPv2Error {
match self {
Self::InvalidAuthType => write!(f, "Invalid Auth Type"),
Self::InvalidChecksum => write!(f, "Invalid Checksum"),
+ Self::InvalidIPCount => write!(f, "Invalid IP Count"),
Self::InvalidTTL => write!(f, "Invalid TTL"),
Self::InvalidType => write!(f, "Invalid Type"),
Self::InvalidVersion => write!(f, "Invalid Version"),
@@ -144,6 +146,9 @@ fn parse(bytes: &[u8]) -> Result<VRRPv2, VRRPv2Error> {
let virtual_router_id = rdr.read_u8()?;
let priority = rdr.read_u8()?;
let count_ip_addrs = rdr.read_u8()?;
+ if count_ip_addrs == 0 {
+ return Err(VRRPv2Error::InvalidIPCount);
+ }
let auth_type = rdr.read_u8()?;
let auth_type = match auth_type {
0 => AuthType::NoAuth,
@@ -259,6 +264,15 @@ fn test_invalid_checksum() {
}
#[test]
+fn test_invalid_ipcount() {
+ let bytes = [
+ 0x21, 0x2a, 0x64, 0x0, 0x0, 0x1, 0xaa, 0x29, 0xc0, 0xa8, 0x0, 0x1, 0x0,
+ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
+ ];
+ assert_eq!(from_bytes(&bytes), Err(VRRPv2Error::InvalidIPCount));
+}
+
+#[test]
fn test_checksum() {
let bytes = [0x00, 0x01, 0xf2, 0x03, 0xf4, 0xf5, 0xf6, 0xf7];
assert_eq!(checksum(&bytes), 0x220d);