From cb9876940cb5eb91f7fb9a102ebf8d1f0a325aec Mon Sep 17 00:00:00 2001 From: moznion Date: Wed, 25 Nov 2020 01:13:31 +0900 Subject: [PATCH] Support octets type --- Makefile | 3 ++ src/bin/code_gen.rs | 40 ++++++++++++++++---- src/rfc2865.rs | 90 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 02abed5..758497d 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ +build: + cargo build + gen: cat /dev/null > $(PWD)/src/rfc2865.rs cargo run --bin code_gen $(PWD)/dicts/dictionary.rfc2865 $(PWD)/src/rfc2865.rs diff --git a/src/bin/code_gen.rs b/src/bin/code_gen.rs index 7617149..1a0ecd2 100644 --- a/src/bin/code_gen.rs +++ b/src/bin/code_gen.rs @@ -123,13 +123,16 @@ fn generate_attribute_code(w: &mut BufWriter, attr: &RadiusAttribute) { match attr.value_type { RadiusAttributeValueType::STRING => { generate_common_attribute_code(w, attr); - generate_string_attribute_code(w, attr) + generate_string_attribute_code(w, attr); } RadiusAttributeValueType::USER_PASSWORD => { generate_common_attribute_code(w, attr); - generate_user_password_attribute_code(w, attr) + generate_user_password_attribute_code(w, attr); + } + RadiusAttributeValueType::OCTETS => { + generate_common_attribute_code(w, attr); + generate_octets_attribute_code(w, attr); } - // RadiusAttributeValueType::OCTETS => generate_octets_attribute_code(w, attr), // RadiusAttributeValueType::IPADDR => generate_ipaddr_attribute_code(w, attr), // RadiusAttributeValueType::INTEGER => generate_integer_attribute_code(w, attr), // RadiusAttributeValueType::VSA => generate_vsa_attribute_code(w, attr), @@ -204,13 +207,36 @@ fn generate_user_password_attribute_code(w: &mut BufWriter, attr: &RadiusA w.write_all(code.as_bytes()).unwrap(); } -fn generate_octets_attribute_code(w: &mut BufWriter, attr: &RadiusAttribute) {} +fn generate_octets_attribute_code(w: &mut BufWriter, attr: &RadiusAttribute) { + let attr_name = attr.name.clone(); -fn generate_ipaddr_attribute_code(w: &mut BufWriter, attr: &RadiusAttribute) {} + let type_identifier = format!("{}_TYPE", attr_name.to_screaming_snake_case()); + let type_calling = format!("Self::{}", type_identifier); -fn generate_integer_attribute_code(w: &mut BufWriter, attr: &RadiusAttribute) {} + let code = format!( + "pub fn add_{method_identifier}(packet: &mut Packet, value: &[u8]) {{ + let attr = Attribute::from_bytes(value); + packet.add({type_calling}, &attr); +}} +", + method_identifier = attr_name.to_snake_case(), + type_calling = type_calling, + ); -fn generate_vsa_attribute_code(w: &mut BufWriter, attr: &RadiusAttribute) {} + w.write_all(code.as_bytes()).unwrap(); +} + +fn generate_ipaddr_attribute_code(w: &mut BufWriter, attr: &RadiusAttribute) { + unimplemented!() +} + +fn generate_integer_attribute_code(w: &mut BufWriter, attr: &RadiusAttribute) { + unimplemented!() +} + +fn generate_vsa_attribute_code(w: &mut BufWriter, attr: &RadiusAttribute) { + unimplemented!() +} type DictParsed = (Vec, HashMap>); diff --git a/src/rfc2865.rs b/src/rfc2865.rs index a56a034..6ab6afe 100644 --- a/src/rfc2865.rs +++ b/src/rfc2865.rs @@ -38,6 +38,21 @@ impl RFC2865 { Ok(()) } + pub const CHAP_PASSWORD_TYPE: AVPType = 3; + pub fn delete_chap_password(packet: &mut Packet) { + packet.delete(Self::CHAP_PASSWORD_TYPE); + } + pub fn lookup_chap_password(packet: &Packet) -> Option<&Attribute> { + packet.lookup(Self::CHAP_PASSWORD_TYPE) + } + pub fn lookup_all_chap_password(packet: &Packet) -> Vec<&Attribute> { + packet.lookup_all(Self::CHAP_PASSWORD_TYPE) + } + pub fn add_chap_password(packet: &mut Packet, value: &[u8]) { + let attr = Attribute::from_bytes(value); + packet.add(Self::CHAP_PASSWORD_TYPE, &attr); + } + pub const FILTER_ID_TYPE: AVPType = 11; pub fn delete_filter_id(packet: &mut Packet) { packet.delete(Self::FILTER_ID_TYPE); @@ -113,6 +128,36 @@ impl RFC2865 { packet.add(Self::FRAMED_ROUTE_TYPE, &attr); } + pub const STATE_TYPE: AVPType = 24; + pub fn delete_state(packet: &mut Packet) { + packet.delete(Self::STATE_TYPE); + } + pub fn lookup_state(packet: &Packet) -> Option<&Attribute> { + packet.lookup(Self::STATE_TYPE) + } + pub fn lookup_all_state(packet: &Packet) -> Vec<&Attribute> { + packet.lookup_all(Self::STATE_TYPE) + } + pub fn add_state(packet: &mut Packet, value: &[u8]) { + let attr = Attribute::from_bytes(value); + packet.add(Self::STATE_TYPE, &attr); + } + + pub const CLASS_TYPE: AVPType = 25; + pub fn delete_class(packet: &mut Packet) { + packet.delete(Self::CLASS_TYPE); + } + pub fn lookup_class(packet: &Packet) -> Option<&Attribute> { + packet.lookup(Self::CLASS_TYPE) + } + pub fn lookup_all_class(packet: &Packet) -> Vec<&Attribute> { + packet.lookup_all(Self::CLASS_TYPE) + } + pub fn add_class(packet: &mut Packet, value: &[u8]) { + let attr = Attribute::from_bytes(value); + packet.add(Self::CLASS_TYPE, &attr); + } + pub const CALLED_STATION_ID_TYPE: AVPType = 30; pub fn delete_called_station_id(packet: &mut Packet) { packet.delete(Self::CALLED_STATION_ID_TYPE); @@ -158,6 +203,21 @@ impl RFC2865 { packet.add(Self::NAS_IDENTIFIER_TYPE, &attr); } + pub const PROXY_STATE_TYPE: AVPType = 33; + pub fn delete_proxy_state(packet: &mut Packet) { + packet.delete(Self::PROXY_STATE_TYPE); + } + pub fn lookup_proxy_state(packet: &Packet) -> Option<&Attribute> { + packet.lookup(Self::PROXY_STATE_TYPE) + } + pub fn lookup_all_proxy_state(packet: &Packet) -> Vec<&Attribute> { + packet.lookup_all(Self::PROXY_STATE_TYPE) + } + pub fn add_proxy_state(packet: &mut Packet, value: &[u8]) { + let attr = Attribute::from_bytes(value); + packet.add(Self::PROXY_STATE_TYPE, &attr); + } + pub const LOGIN_LAT_SERVICE_TYPE: AVPType = 34; pub fn delete_login_lat_service(packet: &mut Packet) { packet.delete(Self::LOGIN_LAT_SERVICE_TYPE); @@ -188,6 +248,21 @@ impl RFC2865 { packet.add(Self::LOGIN_LAT_NODE_TYPE, &attr); } + pub const LOGIN_LAT_GROUP_TYPE: AVPType = 36; + pub fn delete_login_lat_group(packet: &mut Packet) { + packet.delete(Self::LOGIN_LAT_GROUP_TYPE); + } + pub fn lookup_login_lat_group(packet: &Packet) -> Option<&Attribute> { + packet.lookup(Self::LOGIN_LAT_GROUP_TYPE) + } + pub fn lookup_all_login_lat_group(packet: &Packet) -> Vec<&Attribute> { + packet.lookup_all(Self::LOGIN_LAT_GROUP_TYPE) + } + pub fn add_login_lat_group(packet: &mut Packet, value: &[u8]) { + let attr = Attribute::from_bytes(value); + packet.add(Self::LOGIN_LAT_GROUP_TYPE, &attr); + } + pub const FRAMED_APPLE_TALK_ZONE_TYPE: AVPType = 39; pub fn delete_framed_apple_talk_zone(packet: &mut Packet) { packet.delete(Self::FRAMED_APPLE_TALK_ZONE_TYPE); @@ -203,6 +278,21 @@ impl RFC2865 { packet.add(Self::FRAMED_APPLE_TALK_ZONE_TYPE, &attr); } + pub const CHAP_CHALLENGE_TYPE: AVPType = 60; + pub fn delete_chap_challenge(packet: &mut Packet) { + packet.delete(Self::CHAP_CHALLENGE_TYPE); + } + pub fn lookup_chap_challenge(packet: &Packet) -> Option<&Attribute> { + packet.lookup(Self::CHAP_CHALLENGE_TYPE) + } + pub fn lookup_all_chap_challenge(packet: &Packet) -> Vec<&Attribute> { + packet.lookup_all(Self::CHAP_CHALLENGE_TYPE) + } + pub fn add_chap_challenge(packet: &mut Packet, value: &[u8]) { + let attr = Attribute::from_bytes(value); + packet.add(Self::CHAP_CHALLENGE_TYPE, &attr); + } + pub const LOGIN_LAT_PORT_TYPE: AVPType = 63; pub fn delete_login_lat_port(packet: &mut Packet) { packet.delete(Self::LOGIN_LAT_PORT_TYPE);