mirror of
https://github.com/cubixle/radius-rs.git
synced 2026-04-30 15:28:43 +01:00
Refactor the code generator
This commit is contained in:
+73
-85
@@ -28,24 +28,23 @@ struct RadiusValue {
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum RadiusAttributeValueType {
|
||||
STRING,
|
||||
USER_PASSWORD,
|
||||
OCTETS,
|
||||
IPADDR,
|
||||
INTEGER,
|
||||
String,
|
||||
UserPassword,
|
||||
Octets,
|
||||
IpAddr,
|
||||
Integer,
|
||||
VSA,
|
||||
}
|
||||
|
||||
impl FromStr for RadiusAttributeValueType {
|
||||
type Err = ();
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.to_uppercase().as_str() {
|
||||
"STRING" => Ok(RadiusAttributeValueType::STRING),
|
||||
"USER_PASSWORD" => Ok(RadiusAttributeValueType::USER_PASSWORD),
|
||||
"OCTETS" => Ok(RadiusAttributeValueType::OCTETS),
|
||||
"IPADDR" => Ok(RadiusAttributeValueType::IPADDR),
|
||||
"INTEGER" => Ok(RadiusAttributeValueType::INTEGER),
|
||||
"VSA" => Ok(RadiusAttributeValueType::VSA),
|
||||
match s {
|
||||
"string" => Ok(RadiusAttributeValueType::String),
|
||||
"octets" => Ok(RadiusAttributeValueType::Octets),
|
||||
"ipaddr" => Ok(RadiusAttributeValueType::IpAddr),
|
||||
"integer" => Ok(RadiusAttributeValueType::Integer),
|
||||
"vsa" => Ok(RadiusAttributeValueType::VSA),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
@@ -91,7 +90,7 @@ fn main() {
|
||||
generate_footer(&mut buf_writer);
|
||||
}
|
||||
|
||||
fn generate_header(w: &mut BufWriter<File>, struct_name: &String) {
|
||||
fn generate_header(w: &mut BufWriter<File>, struct_name: &str) {
|
||||
let code = format!(
|
||||
"// Code generated by machine generator; DO NOT EDIT.
|
||||
|
||||
@@ -122,38 +121,40 @@ fn generate_attributes_code(w: &mut BufWriter<File>, attrs: &[RadiusAttribute])
|
||||
}
|
||||
|
||||
fn generate_attribute_code(w: &mut BufWriter<File>, attr: &RadiusAttribute) {
|
||||
let attr_name = attr.name.clone();
|
||||
let type_identifier = format!("{}_TYPE", attr_name.to_screaming_snake_case());
|
||||
let type_calling = format!("Self::{}", type_identifier);
|
||||
let type_value = attr.typ;
|
||||
let method_identifier = attr_name.to_snake_case();
|
||||
|
||||
generate_common_attribute_code(w, &attr_name, &type_identifier, &type_calling, type_value);
|
||||
match attr.value_type {
|
||||
RadiusAttributeValueType::STRING => {
|
||||
generate_common_attribute_code(w, attr);
|
||||
generate_string_attribute_code(w, attr);
|
||||
RadiusAttributeValueType::String => {
|
||||
generate_string_attribute_code(w, &method_identifier, &type_calling)
|
||||
}
|
||||
RadiusAttributeValueType::USER_PASSWORD => {
|
||||
generate_common_attribute_code(w, attr);
|
||||
generate_user_password_attribute_code(w, attr);
|
||||
RadiusAttributeValueType::UserPassword => {
|
||||
generate_user_password_attribute_code(w, &method_identifier, &type_calling)
|
||||
}
|
||||
RadiusAttributeValueType::OCTETS => {
|
||||
generate_common_attribute_code(w, attr);
|
||||
generate_octets_attribute_code(w, attr);
|
||||
RadiusAttributeValueType::Octets => {
|
||||
generate_octets_attribute_code(w, &method_identifier, &type_calling)
|
||||
}
|
||||
RadiusAttributeValueType::IPADDR => {
|
||||
generate_common_attribute_code(w, attr);
|
||||
generate_ipaddr_attribute_code(w, attr);
|
||||
RadiusAttributeValueType::IpAddr => {
|
||||
generate_ipaddr_attribute_code(w, &method_identifier, &type_calling)
|
||||
}
|
||||
RadiusAttributeValueType::INTEGER => {
|
||||
generate_common_attribute_code(w, attr);
|
||||
generate_integer_attribute_code(w, attr);
|
||||
RadiusAttributeValueType::Integer => {
|
||||
generate_integer_attribute_code(w, &method_identifier, &type_calling)
|
||||
}
|
||||
// RadiusAttributeValueType::VSA => generate_vsa_attribute_code(w, attr),
|
||||
_ => {}
|
||||
RadiusAttributeValueType::VSA => generate_vsa_attribute_code(),
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_common_attribute_code(w: &mut BufWriter<File>, attr: &RadiusAttribute) {
|
||||
let attr_name = attr.name.clone();
|
||||
|
||||
let type_identifier = format!("{}_TYPE", attr_name.to_screaming_snake_case());
|
||||
let type_calling = format!("Self::{}", type_identifier);
|
||||
|
||||
fn generate_common_attribute_code(
|
||||
w: &mut BufWriter<File>,
|
||||
attr_name: &str,
|
||||
type_identifier: &str,
|
||||
type_calling: &str,
|
||||
type_value: u8,
|
||||
) {
|
||||
let code = format!(
|
||||
"
|
||||
pub const {type_identifier}: AVPType = {type_value};
|
||||
@@ -170,37 +171,33 @@ pub fn lookup_all_{method_identifier}(packet: &Packet) -> Vec<&Attribute> {{
|
||||
method_identifier = attr_name.to_snake_case(),
|
||||
type_identifier = type_identifier,
|
||||
type_calling = type_calling,
|
||||
type_value = attr.typ,
|
||||
type_value = type_value,
|
||||
);
|
||||
|
||||
w.write_all(code.as_bytes()).unwrap();
|
||||
}
|
||||
|
||||
fn generate_string_attribute_code(w: &mut BufWriter<File>, attr: &RadiusAttribute) {
|
||||
let attr_name = attr.name.clone();
|
||||
|
||||
let type_identifier = format!("{}_TYPE", attr_name.to_screaming_snake_case());
|
||||
let type_calling = format!("Self::{}", type_identifier);
|
||||
|
||||
fn generate_string_attribute_code(
|
||||
w: &mut BufWriter<File>,
|
||||
method_identifier: &str,
|
||||
type_calling: &str,
|
||||
) {
|
||||
let code = format!(
|
||||
"pub fn add_{method_identifier}(packet: &mut Packet, value: &str) {{
|
||||
let attr = Attribute::from_string(value);
|
||||
packet.add({type_calling}, &attr);
|
||||
}}
|
||||
",
|
||||
method_identifier = attr_name.to_snake_case(),
|
||||
method_identifier = method_identifier,
|
||||
type_calling = type_calling,
|
||||
);
|
||||
|
||||
w.write_all(code.as_bytes()).unwrap();
|
||||
}
|
||||
|
||||
fn generate_user_password_attribute_code(w: &mut BufWriter<File>, attr: &RadiusAttribute) {
|
||||
let attr_name = attr.name.clone();
|
||||
|
||||
let type_identifier = format!("{}_TYPE", attr_name.to_screaming_snake_case());
|
||||
let type_calling = format!("Self::{}", type_identifier);
|
||||
|
||||
fn generate_user_password_attribute_code(
|
||||
w: &mut BufWriter<File>,
|
||||
method_identifier: &str,
|
||||
type_calling: &str,
|
||||
) {
|
||||
let code = format!(
|
||||
"pub fn add_{method_identifier}(packet: &mut Packet, value: &[u8]) -> Result<(), String> {{
|
||||
let attr = Attribute::from_user_password(value, packet.get_secret(), packet.get_authenticator())?;
|
||||
@@ -208,72 +205,65 @@ fn generate_user_password_attribute_code(w: &mut BufWriter<File>, attr: &RadiusA
|
||||
Ok(())
|
||||
}}
|
||||
",
|
||||
method_identifier = attr_name.to_snake_case(),
|
||||
method_identifier = method_identifier,
|
||||
type_calling = type_calling,
|
||||
);
|
||||
|
||||
w.write_all(code.as_bytes()).unwrap();
|
||||
}
|
||||
|
||||
fn generate_octets_attribute_code(w: &mut BufWriter<File>, attr: &RadiusAttribute) {
|
||||
let attr_name = attr.name.clone();
|
||||
|
||||
let type_identifier = format!("{}_TYPE", attr_name.to_screaming_snake_case());
|
||||
let type_calling = format!("Self::{}", type_identifier);
|
||||
|
||||
fn generate_octets_attribute_code(
|
||||
w: &mut BufWriter<File>,
|
||||
method_identifier: &str,
|
||||
type_calling: &str,
|
||||
) {
|
||||
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(),
|
||||
method_identifier = method_identifier,
|
||||
type_calling = type_calling,
|
||||
);
|
||||
|
||||
w.write_all(code.as_bytes()).unwrap();
|
||||
}
|
||||
|
||||
fn generate_ipaddr_attribute_code(w: &mut BufWriter<File>, attr: &RadiusAttribute) {
|
||||
let attr_name = attr.name.clone();
|
||||
|
||||
let type_identifier = format!("{}_TYPE", attr_name.to_screaming_snake_case());
|
||||
let type_calling = format!("Self::{}", type_identifier);
|
||||
|
||||
fn generate_ipaddr_attribute_code(
|
||||
w: &mut BufWriter<File>,
|
||||
method_identifier: &str,
|
||||
type_calling: &str,
|
||||
) {
|
||||
let code = format!(
|
||||
"pub fn add_{method_identifier}(packet: &mut Packet, value: &Ipv4Addr) {{
|
||||
let attr = Attribute::from_ipv4(value);
|
||||
packet.add({type_calling}, &attr);
|
||||
}}
|
||||
",
|
||||
method_identifier = attr_name.to_snake_case(),
|
||||
method_identifier = method_identifier,
|
||||
type_calling = type_calling,
|
||||
);
|
||||
|
||||
w.write_all(code.as_bytes()).unwrap();
|
||||
}
|
||||
|
||||
fn generate_integer_attribute_code(w: &mut BufWriter<File>, attr: &RadiusAttribute) {
|
||||
let attr_name = attr.name.clone();
|
||||
|
||||
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<File>,
|
||||
method_identifier: &str,
|
||||
type_calling: &str,
|
||||
) {
|
||||
let code = format!(
|
||||
"pub fn add_{method_identifier}(packet: &mut Packet, value: u32) {{
|
||||
let attr = Attribute::from_u32(value);
|
||||
packet.add({type_calling}, &attr);
|
||||
}}
|
||||
",
|
||||
method_identifier = attr_name.to_snake_case(),
|
||||
method_identifier = method_identifier,
|
||||
type_calling = type_calling,
|
||||
);
|
||||
|
||||
w.write_all(code.as_bytes()).unwrap();
|
||||
}
|
||||
|
||||
fn generate_vsa_attribute_code(w: &mut BufWriter<File>, attr: &RadiusAttribute) {
|
||||
unimplemented!()
|
||||
fn generate_vsa_attribute_code() {
|
||||
// NOP
|
||||
}
|
||||
|
||||
type DictParsed = (Vec<RadiusAttribute>, HashMap<String, Vec<RadiusValue>>);
|
||||
@@ -312,16 +302,14 @@ fn parse_dict_file(dict_file_path: &Path) -> Result<DictParsed, String> {
|
||||
|
||||
let typ = match RadiusAttributeValueType::from_str(type_descriptions[0]) {
|
||||
Ok(t) => {
|
||||
if t == RadiusAttributeValueType::STRING && is_encrypt {
|
||||
RadiusAttributeValueType::USER_PASSWORD
|
||||
if t == RadiusAttributeValueType::String && is_encrypt {
|
||||
RadiusAttributeValueType::UserPassword
|
||||
} else {
|
||||
t
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
return Err(
|
||||
format!("invalid type has come => {}", type_descriptions[0]).to_owned()
|
||||
);
|
||||
return Err(format!("invalid type has come => {}", type_descriptions[0]));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -355,6 +355,17 @@ impl RFC2865 {
|
||||
packet.add(Self::CLASS_TYPE, &attr);
|
||||
}
|
||||
|
||||
pub const VENDOR_SPECIFIC_TYPE: AVPType = 26;
|
||||
pub fn delete_vendor_specific(packet: &mut Packet) {
|
||||
packet.delete(Self::VENDOR_SPECIFIC_TYPE);
|
||||
}
|
||||
pub fn lookup_vendor_specific(packet: &Packet) -> Option<&Attribute> {
|
||||
packet.lookup(Self::VENDOR_SPECIFIC_TYPE)
|
||||
}
|
||||
pub fn lookup_all_vendor_specific(packet: &Packet) -> Vec<&Attribute> {
|
||||
packet.lookup_all(Self::VENDOR_SPECIFIC_TYPE)
|
||||
}
|
||||
|
||||
pub const SESSION_TIMEOUT_TYPE: AVPType = 27;
|
||||
pub fn delete_session_timeout(packet: &mut Packet) {
|
||||
packet.delete(Self::SESSION_TIMEOUT_TYPE);
|
||||
|
||||
Reference in New Issue
Block a user