Skip to main content

sl4f_lib/netstack/
commands.rs

1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use 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::{Value, to_value};
11
12enum NetstackMethod<'a> {
13    DisableInterface,
14    EnableInterface,
15    GetIpv6Addresses,
16    GetLinkLocalIpv6Addresses,
17    ListInterfaces,
18    Undefined(&'a str),
19}
20
21impl NetstackMethod<'_> {
22    pub fn from_str(method: &str) -> NetstackMethod<'_> {
23        match method {
24            "DisableInterface" => NetstackMethod::DisableInterface,
25            "EnableInterface" => NetstackMethod::EnableInterface,
26            "GetIpv6Addresses" => NetstackMethod::GetIpv6Addresses,
27            "GetLinkLocalIpv6Addresses" => NetstackMethod::GetLinkLocalIpv6Addresses,
28            "ListInterfaces" => NetstackMethod::ListInterfaces,
29            method => NetstackMethod::Undefined(method),
30        }
31    }
32}
33
34#[async_trait(?Send)]
35impl Facade for NetstackFacade {
36    async fn handle_request(&self, method: String, args: Value) -> Result<Value, Error> {
37        match NetstackMethod::from_str(&method) {
38            NetstackMethod::ListInterfaces => {
39                let result = self.list_interfaces().await?;
40                to_value(result).map_err(Into::into)
41            }
42            NetstackMethod::GetIpv6Addresses => {
43                let result = self.get_ipv6_addresses().await?;
44                to_value(result).map_err(Into::into)
45            }
46            NetstackMethod::GetLinkLocalIpv6Addresses => {
47                let result = self.get_link_local_ipv6_addresses().await?;
48                to_value(result).map_err(Into::into)
49            }
50            NetstackMethod::EnableInterface => {
51                let identifier = parse_u64_identifier(args)?;
52                let result = self.enable_interface(identifier).await?;
53                to_value(result).map_err(Into::into)
54            }
55            NetstackMethod::DisableInterface => {
56                let identifier = parse_u64_identifier(args)?;
57                let result = self.disable_interface(identifier).await?;
58                to_value(result).map_err(Into::into)
59            }
60            NetstackMethod::Undefined(method) => {
61                Err(anyhow!("invalid Netstack method: {}", method))
62            }
63        }
64    }
65}