diff --git a/radius/src/core/mod.rs b/radius/src/core/mod.rs index b137f92..d051329 100644 --- a/radius/src/core/mod.rs +++ b/radius/src/core/mod.rs @@ -30,3 +30,4 @@ pub mod rfc6911; pub mod rfc7055; pub mod rfc7155; pub mod tag; +pub mod vsa; diff --git a/radius/src/core/rfc2865.rs b/radius/src/core/rfc2865.rs index d79c8b8..907a26f 100644 --- a/radius/src/core/rfc2865.rs +++ b/radius/src/core/rfc2865.rs @@ -150,6 +150,7 @@ use std::net::Ipv4Addr; use crate::core::avp::{AVPError, AVPType, AVP}; use crate::core::packet::Packet; +use crate::core::vsa::VSA; pub const USER_NAME_TYPE: AVPType = 1; /// Delete all of `user_name` values from a packet. @@ -357,6 +358,12 @@ pub fn lookup_all_framed_ip_address(packet: &Packet) -> Result, AV Ok(vec) } +/// Add `vsa_attribute` vsa value to a packet. +pub fn add_vsa_attribute(packet: &mut Packet, value: &VSA) { + // packet.add(AVP::from_ipv4(FRAMED_IP_ADDRESS_TYPE, value)); + packet.add(AVP::from_bytes(VENDOR_SPECIFIC_TYPE, &value.message())); +} + pub const FRAMED_IP_NETMASK_TYPE: AVPType = 9; /// Delete all of `framed_ip_netmask` values from a packet. pub fn delete_framed_ip_netmask(packet: &mut Packet) { diff --git a/radius/src/core/vsa.rs b/radius/src/core/vsa.rs new file mode 100644 index 0000000..62ed77b --- /dev/null +++ b/radius/src/core/vsa.rs @@ -0,0 +1,35 @@ +const SINGLE_FIELDS_COUNT: usize = 3; + +/// This struct represents a attribute-value pair. +#[derive(Debug, Clone, PartialEq)] +pub struct VSA { + vendor_id: Vec, + type_id: u8, + length: u8, + tag: u8, + value: Vec, +} + +impl VSA { + pub fn new(vendor_id: i32, type_id: u8, tag: u8, value: &str) -> VSA { + VSA { + vendor_id: vendor_id.to_be_bytes().to_vec(), + type_id, + length: (SINGLE_FIELDS_COUNT + value.len()) as u8, + tag: tag, + value: value.as_bytes().to_vec(), + } + } + + pub fn len(&self) -> usize { + self.length as usize + } + + pub fn message(&self) -> Vec { + let mut msg = vec![self.type_id, self.length, self.tag]; + msg.splice(0..0, self.vendor_id.iter().cloned()); + msg.append(&mut self.value.clone()); + + msg + } +}