sl4f_lib/netstack/
commands.rs1use super::facade::NetstackFacade;
6use crate::common_utils::common::parse_u64_identifier;
7use crate::server::Facade;
8use anyhow::Error;
9use async_trait::async_trait;
10use serde_json::{to_value, Value};
11
12enum NetstackMethod<'a> {
13 DisableInterface,
14 EnableInterface,
15 GetIpv6Addresses,
16 GetLinkLocalIpv6Addresses,
17 ListInterfaces,
18 GetNetstackVersion,
19 SetUserNetstackVersion,
20 Undefined(&'a str),
21}
22
23impl NetstackMethod<'_> {
24 pub fn from_str(method: &str) -> NetstackMethod<'_> {
25 match method {
26 "DisableInterface" => NetstackMethod::DisableInterface,
27 "EnableInterface" => NetstackMethod::EnableInterface,
28 "GetIpv6Addresses" => NetstackMethod::GetIpv6Addresses,
29 "GetLinkLocalIpv6Addresses" => NetstackMethod::GetLinkLocalIpv6Addresses,
30 "ListInterfaces" => NetstackMethod::ListInterfaces,
31 "GetNetstackVersion" => NetstackMethod::GetNetstackVersion,
32 "SetUserNetstackVersion" => NetstackMethod::SetUserNetstackVersion,
33 method => NetstackMethod::Undefined(method),
34 }
35 }
36}
37
38#[async_trait(?Send)]
39impl Facade for NetstackFacade {
40 async fn handle_request(&self, method: String, args: Value) -> Result<Value, Error> {
41 match NetstackMethod::from_str(&method) {
42 NetstackMethod::ListInterfaces => {
43 let result = self.list_interfaces().await?;
44 to_value(result).map_err(Into::into)
45 }
46 NetstackMethod::GetIpv6Addresses => {
47 let result = self.get_ipv6_addresses().await?;
48 to_value(result).map_err(Into::into)
49 }
50 NetstackMethod::GetLinkLocalIpv6Addresses => {
51 let result = self.get_link_local_ipv6_addresses().await?;
52 to_value(result).map_err(Into::into)
53 }
54 NetstackMethod::EnableInterface => {
55 let identifier = parse_u64_identifier(args)?;
56 let result = self.enable_interface(identifier).await?;
57 to_value(result).map_err(Into::into)
58 }
59 NetstackMethod::DisableInterface => {
60 let identifier = parse_u64_identifier(args)?;
61 let result = self.disable_interface(identifier).await?;
62 to_value(result).map_err(Into::into)
63 }
64 NetstackMethod::GetNetstackVersion => {
65 let result = self.get_netstack_version().await?;
66 to_value(result).map_err(Into::into)
67 }
68 NetstackMethod::SetUserNetstackVersion => {
69 let version = args.try_into()?;
70 let result = self.set_user_netstack_version(version).await?;
71 to_value(result).map_err(Into::into)
72 }
73 NetstackMethod::Undefined(method) => {
74 Err(anyhow!("invalid Netstack method: {}", method))
75 }
76 }
77 }
78}