xref: /aosp_15_r20/tools/netsim/rust/daemon/src/config_file.rs (revision cf78ab8cffb8fc9207af348f23af247fb04370a6)
1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 /// Configuration file for netsim
16 use netsim_proto::config::Config;
17 use protobuf_json_mapping::merge_from_str;
18 use std::fs;
19 use std::net::{Ipv4Addr, Ipv6Addr};
20 
21 #[allow(dead_code)]
new_from_file(filename: &str) -> Result<Config, String>22 pub fn new_from_file(filename: &str) -> Result<Config, String> {
23     let contents = fs::read_to_string(filename)
24         .map_err(|e| format!("Failed to read config file {}: {} ", filename, e))?;
25     from_str(&contents)
26 }
27 
from_str(contents: &str) -> Result<Config, String>28 pub fn from_str(contents: &str) -> Result<Config, String> {
29     let mut config = Config::new();
30     merge_from_str(&mut config, contents).map_err(|e| format!("Failed to parse config: {}", e))?;
31     validate_wifi(&config.wifi)?;
32     Ok(config)
33 }
34 
validate_wifi(wifi: &netsim_proto::config::WiFi) -> Result<(), String>35 fn validate_wifi(wifi: &netsim_proto::config::WiFi) -> Result<(), String> {
36     validate_ipv4(&wifi.slirp_options.vnet, "vnet")?;
37     validate_ipv4(&wifi.slirp_options.vhost, "vhost")?;
38     validate_ipv4(&wifi.slirp_options.vmask, "vmask")?;
39     validate_ipv6(&wifi.slirp_options.vprefix6, "vprefix6")?;
40     validate_ipv6(&wifi.slirp_options.vhost6, "vhost6")?;
41     Ok(())
42 }
43 
validate_ipv4(in_addr: &str, field: &str) -> Result<(), String>44 fn validate_ipv4(in_addr: &str, field: &str) -> Result<(), String> {
45     if !in_addr.is_empty() {
46         in_addr.parse::<Ipv4Addr>().map_err(|e| format!("Invalid {}: {}", field, e))?;
47     }
48     Ok(())
49 }
50 
validate_ipv6(in6_addr: &str, field: &str) -> Result<(), String>51 fn validate_ipv6(in6_addr: &str, field: &str) -> Result<(), String> {
52     if !in6_addr.is_empty() {
53         in6_addr.parse::<Ipv6Addr>().map_err(|e| format!("Invalid {}: {}", field, e))?;
54     }
55     Ok(())
56 }
57 
58 #[cfg(test)]
59 mod tests {
60     use super::*;
61 
62     #[test]
test_json()63     fn test_json() {
64         let config = from_str(
65             r#"
66         {
67           "wifi" : {
68              "slirp_options" : {
69                 "disabled" : true,
70                 "vnet" : "192.168.1.1"
71              }
72           }
73         }"#,
74         );
75         eprintln!("{:?}", config);
76         assert!(config.as_ref().ok().is_some());
77         let config = config.unwrap();
78         assert!(config.wifi.slirp_options.as_ref().unwrap().disabled);
79     }
80 }
81