Skip to content

Commit

Permalink
Merge pull request #37 from ivanovuri/vsa
Browse files Browse the repository at this point in the history
Handling of Vendor Specific Attributes (VSA)
  • Loading branch information
moznion authored Jan 15, 2024
2 parents a9ec2ac + e0dea38 commit b547a5f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions radius/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ pub mod rfc6911;
pub mod rfc7055;
pub mod rfc7155;
pub mod tag;
pub mod vsa;
7 changes: 7 additions & 0 deletions radius/src/core/rfc2865.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -357,6 +358,12 @@ pub fn lookup_all_framed_ip_address(packet: &Packet) -> Result<Vec<Ipv4Addr>, 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) {
Expand Down
35 changes: 35 additions & 0 deletions radius/src/core/vsa.rs
Original file line number Diff line number Diff line change
@@ -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<u8>,
type_id: u8,
length: u8,
tag: u8,
value: Vec<u8>,
}

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,

Check warning on line 19 in radius/src/core/vsa.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, stable)

redundant field names in struct initialization

Check warning on line 19 in radius/src/core/vsa.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, nightly)

redundant field names in struct initialization

Check warning on line 19 in radius/src/core/vsa.rs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, stable)

redundant field names in struct initialization

Check warning on line 19 in radius/src/core/vsa.rs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, nightly)

redundant field names in struct initialization

Check warning on line 19 in radius/src/core/vsa.rs

View workflow job for this annotation

GitHub Actions / build (windows-latest, stable)

redundant field names in struct initialization

Check warning on line 19 in radius/src/core/vsa.rs

View workflow job for this annotation

GitHub Actions / build (windows-latest, nightly)

redundant field names in struct initialization
value: value.as_bytes().to_vec(),
}
}

pub fn len(&self) -> usize {

Check warning on line 24 in radius/src/core/vsa.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, stable)

struct `VSA` has a public `len` method, but no `is_empty` method

Check warning on line 24 in radius/src/core/vsa.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, nightly)

struct `VSA` has a public `len` method, but no `is_empty` method

Check warning on line 24 in radius/src/core/vsa.rs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, stable)

struct `VSA` has a public `len` method, but no `is_empty` method

Check warning on line 24 in radius/src/core/vsa.rs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, nightly)

struct `VSA` has a public `len` method, but no `is_empty` method

Check warning on line 24 in radius/src/core/vsa.rs

View workflow job for this annotation

GitHub Actions / build (windows-latest, stable)

struct `VSA` has a public `len` method, but no `is_empty` method

Check warning on line 24 in radius/src/core/vsa.rs

View workflow job for this annotation

GitHub Actions / build (windows-latest, nightly)

struct `VSA` has a public `len` method, but no `is_empty` method
self.length as usize
}

pub fn message(&self) -> Vec<u8> {
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
}
}

0 comments on commit b547a5f

Please sign in to comment.