-
Notifications
You must be signed in to change notification settings - Fork 0
/
vintage.ex
78 lines (64 loc) · 2.2 KB
/
vintage.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
defmodule NervesBackdoor.Vintage do
use GenServer
def start_link(_opts \\ []) do
init_args = []
GenServer.start_link(__MODULE__, init_args, name: :nbd_vnet)
end
def stop() do
GenServer.stop(:nbd_vnet)
end
def all_interfaces() do
GenServer.call(:nbd_vnet, {:all_interfaces})
end
def configure(interface, params) do
GenServer.call(:nbd_vnet, {:configure, interface, params})
end
def get_configuration(interface) do
GenServer.call(:nbd_vnet, {:get_configuration, interface})
end
@impl true
def init(_args) do
{:ok, %{}}
end
@impl true
def handle_call(req, _from, state) do
res =
case req do
{:all_interfaces} ->
:erlang.apply(VintageNet, :all_interfaces, [])
{:configure, interface, params} ->
method = Map.fetch!(params, "method")
config =
case method do
"dhcp" ->
%{type: VintageNetEthernet, ipv4: %{method: :dhcp}}
"static" ->
address = Map.fetch!(params, "address")
prefix_length = Map.fetch!(params, "prefix_length")
gateway = Map.fetch!(params, "gateway")
name_servers = Map.fetch!(params, "name_servers")
%{
type: VintageNetEthernet,
ipv4: %{
method: :static,
# string
address: address,
# integer
prefix_length: prefix_length,
# string
gateway: gateway,
# list of strings
name_servers: name_servers
}
}
end
:erlang.apply(VintageNet, :configure, [interface, config])
{:get_configuration, interface} ->
state = :erlang.apply(VintageNet, :get, [["interface", interface, "state"]])
connection = :erlang.apply(VintageNet, :get, [["interface", interface, "connection"]])
config = :erlang.apply(VintageNet, :get_configuration, [interface])
{:ok, %{interface: interface, state: state, connection: connection, config: config}}
end
{:reply, res, state}
end
end