From c67a8f88dd35f38de4992e80f100598d66c3b42e Mon Sep 17 00:00:00 2001 From: moznion Date: Wed, 25 Nov 2020 22:28:57 +0900 Subject: [PATCH] Fix code generator to ensure the order of the generated values --- scripts/generate_code.sh | 14 ++++---- src/bin/code_gen.rs | 8 ++--- src/rfc2865.rs | 78 ++++++++++++++++++++-------------------- 3 files changed, 51 insertions(+), 49 deletions(-) diff --git a/scripts/generate_code.sh b/scripts/generate_code.sh index 53952d9..afc100d 100755 --- a/scripts/generate_code.sh +++ b/scripts/generate_code.sh @@ -3,14 +3,16 @@ set -eu REPO_ROOT="$(cd ./"$(git rev-parse --show-cdup)" || exit; pwd)" +DICTS_DIR="${REPO_ROOT}/dicts" +SRC_DIR="${REPO_ROOT}/src" -DICT_NAMES=( - "rfc2865" -) +DICTS=$(ls "$DICTS_DIR") -for DICT_NAME in "${DICT_NAMES[@]}"; do - cat /dev/null > "${REPO_ROOT}/src/${DICT_NAME}.rs" - cargo run --bin code_gen "${REPO_ROOT}/dicts/dictionary.${DICT_NAME}" "${REPO_ROOT}/src/${DICT_NAME}.rs" +# shellcheck disable=SC2068 +for DICT in ${DICTS[@]}; do + DICT_NAME="${DICT##*.}" + cat /dev/null > "${SRC_DIR}/${DICT_NAME}.rs" + cargo run --bin code_gen "${DICTS_DIR}/dictionary.${DICT_NAME}" "${SRC_DIR}/${DICT_NAME}.rs" done cargo fmt diff --git a/src/bin/code_gen.rs b/src/bin/code_gen.rs index 68d40c7..af54557 100644 --- a/src/bin/code_gen.rs +++ b/src/bin/code_gen.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::{BTreeMap, HashSet}; use std::fs::File; use std::io::{BufRead, BufWriter, Write}; use std::path::Path; @@ -110,7 +110,7 @@ use crate::packet::Packet; fn generate_values_code( w: &mut BufWriter, - attr_to_values_map: &HashMap>, + attr_to_values_map: &BTreeMap>, ) { for (attr, values) in attr_to_values_map { generate_values_for_attribute_code(w, attr, values); @@ -333,14 +333,14 @@ fn generate_vsa_attribute_code() { // NOP } -type DictParsed = (Vec, HashMap>); +type DictParsed = (Vec, BTreeMap>); fn parse_dict_file(dict_file_path: &Path) -> Result { let line_filter_re = Regex::new(r"^(?:#.*|)$").unwrap(); let tabs_re = Regex::new(r"\t+").unwrap(); let mut radius_attributes: Vec = Vec::new(); - let mut radius_attribute_to_values: HashMap> = HashMap::new(); + let mut radius_attribute_to_values: BTreeMap> = BTreeMap::new(); let lines = read_lines(dict_file_path).unwrap(); for line_result in lines { diff --git a/src/rfc2865.rs b/src/rfc2865.rs index 764e3bd..cd9ad07 100644 --- a/src/rfc2865.rs +++ b/src/rfc2865.rs @@ -6,6 +6,41 @@ use crate::attribute::Attribute; use crate::attributes::AVPType; use crate::packet::Packet; +pub type FramedCompression = u32; +pub const FRAMED_COMPRESSION_NONE: FramedCompression = 0; +pub const FRAMED_COMPRESSION_VAN_JACOBSON_TCP_IP: FramedCompression = 1; +pub const FRAMED_COMPRESSION_IPX_HEADER_COMPRESSION: FramedCompression = 2; +pub const FRAMED_COMPRESSION_STAC_LZS: FramedCompression = 3; + +pub type FramedProtocol = u32; +pub const FRAMED_PROTOCOL_PPP: FramedProtocol = 1; +pub const FRAMED_PROTOCOL_SLIP: FramedProtocol = 2; +pub const FRAMED_PROTOCOL_ARAP: FramedProtocol = 3; +pub const FRAMED_PROTOCOL_GANDALF_SLML: FramedProtocol = 4; +pub const FRAMED_PROTOCOL_XYLOGICS_IPX_SLIP: FramedProtocol = 5; +pub const FRAMED_PROTOCOL_X_75_SYNCHRONOUS: FramedProtocol = 6; + +pub type FramedRouting = u32; +pub const FRAMED_ROUTING_NONE: FramedRouting = 0; +pub const FRAMED_ROUTING_BROADCAST: FramedRouting = 1; +pub const FRAMED_ROUTING_LISTEN: FramedRouting = 2; +pub const FRAMED_ROUTING_BROADCAST_LISTEN: FramedRouting = 3; + +pub type LoginService = u32; +pub const LOGIN_SERVICE_TELNET: LoginService = 0; +pub const LOGIN_SERVICE_RLOGIN: LoginService = 1; +pub const LOGIN_SERVICE_TCP_CLEAR: LoginService = 2; +pub const LOGIN_SERVICE_PORT_MASTER: LoginService = 3; +pub const LOGIN_SERVICE_LAT: LoginService = 4; +pub const LOGIN_SERVICE_X25_PAD: LoginService = 5; +pub const LOGIN_SERVICE_X25_T3POS: LoginService = 6; +pub const LOGIN_SERVICE_TCP_CLEAR_QUIET: LoginService = 8; + +pub type LoginTCPPort = u32; +pub const LOGIN_TCP_PORT_TELNET: LoginTCPPort = 23; +pub const LOGIN_TCP_PORT_RLOGIN: LoginTCPPort = 513; +pub const LOGIN_TCP_PORT_RSH: LoginTCPPort = 514; + pub type NasPortType = u32; pub const NAS_PORT_TYPE_ASYNC: NasPortType = 0; pub const NAS_PORT_TYPE_SYNC: NasPortType = 1; @@ -28,45 +63,6 @@ pub const NAS_PORT_TYPE_CABLE: NasPortType = 17; pub const NAS_PORT_TYPE_WIRELESS_OTHER: NasPortType = 18; pub const NAS_PORT_TYPE_WIRELESS_802_11: NasPortType = 19; -pub type LoginTCPPort = u32; -pub const LOGIN_TCP_PORT_TELNET: LoginTCPPort = 23; -pub const LOGIN_TCP_PORT_RLOGIN: LoginTCPPort = 513; -pub const LOGIN_TCP_PORT_RSH: LoginTCPPort = 514; - -pub type FramedCompression = u32; -pub const FRAMED_COMPRESSION_NONE: FramedCompression = 0; -pub const FRAMED_COMPRESSION_VAN_JACOBSON_TCP_IP: FramedCompression = 1; -pub const FRAMED_COMPRESSION_IPX_HEADER_COMPRESSION: FramedCompression = 2; -pub const FRAMED_COMPRESSION_STAC_LZS: FramedCompression = 3; - -pub type TerminationAction = u32; -pub const TERMINATION_ACTION_DEFAULT: TerminationAction = 0; -pub const TERMINATION_ACTION_RADIUS_REQUEST: TerminationAction = 1; - -pub type FramedRouting = u32; -pub const FRAMED_ROUTING_NONE: FramedRouting = 0; -pub const FRAMED_ROUTING_BROADCAST: FramedRouting = 1; -pub const FRAMED_ROUTING_LISTEN: FramedRouting = 2; -pub const FRAMED_ROUTING_BROADCAST_LISTEN: FramedRouting = 3; - -pub type LoginService = u32; -pub const LOGIN_SERVICE_TELNET: LoginService = 0; -pub const LOGIN_SERVICE_RLOGIN: LoginService = 1; -pub const LOGIN_SERVICE_TCP_CLEAR: LoginService = 2; -pub const LOGIN_SERVICE_PORT_MASTER: LoginService = 3; -pub const LOGIN_SERVICE_LAT: LoginService = 4; -pub const LOGIN_SERVICE_X25_PAD: LoginService = 5; -pub const LOGIN_SERVICE_X25_T3POS: LoginService = 6; -pub const LOGIN_SERVICE_TCP_CLEAR_QUIET: LoginService = 8; - -pub type FramedProtocol = u32; -pub const FRAMED_PROTOCOL_PPP: FramedProtocol = 1; -pub const FRAMED_PROTOCOL_SLIP: FramedProtocol = 2; -pub const FRAMED_PROTOCOL_ARAP: FramedProtocol = 3; -pub const FRAMED_PROTOCOL_GANDALF_SLML: FramedProtocol = 4; -pub const FRAMED_PROTOCOL_XYLOGICS_IPX_SLIP: FramedProtocol = 5; -pub const FRAMED_PROTOCOL_X_75_SYNCHRONOUS: FramedProtocol = 6; - pub type ServiceType = u32; pub const SERVICE_TYPE_LOGIN_USER: ServiceType = 1; pub const SERVICE_TYPE_FRAMED_USER: ServiceType = 2; @@ -80,6 +76,10 @@ pub const SERVICE_TYPE_CALLBACK_NAS_PROMPT: ServiceType = 9; pub const SERVICE_TYPE_CALL_CHECK: ServiceType = 10; pub const SERVICE_TYPE_CALLBACK_ADMINISTRATIVE: ServiceType = 11; +pub type TerminationAction = u32; +pub const TERMINATION_ACTION_DEFAULT: TerminationAction = 0; +pub const TERMINATION_ACTION_RADIUS_REQUEST: TerminationAction = 1; + pub const USER_NAME_TYPE: AVPType = 1; pub fn delete_user_name(packet: &mut Packet) { packet.delete(USER_NAME_TYPE);