Skip to content

Commit

Permalink
Move IP settings into shared module
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorRigby committed May 10, 2019
1 parent 619f5c6 commit 03e975a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 79 deletions.
62 changes: 62 additions & 0 deletions lib/vintage_net/ip/config_to_interfaces.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
defmodule VintageNet.IP.ConfigToInterfaces do
@moduledoc """
Common config for ifup ip configs
"""

def config_to_interfaces_contents(ifname, %{ipv4: %{method: :dhcp} = ipv4} = config) do
hostname = config[:hostname] || get_hostname()
"iface #{ifname} inet dhcp" <> dhcp_options(ipv4, hostname)
end

def config_to_interfaces_contents(ifname, %{ipv4: %{method: :static} = ipv4} = config) do
hostname = config[:hostname] || get_hostname()
"iface #{ifname} inet static" <> static_options(ipv4, hostname)
end

# Default to DHCP
def config_to_interfaces_contents(ifname, config) do
hostname = config[:hostname] || get_hostname()
"iface #{ifname} inet dhcp" <> dhcp_options(config, hostname)
end

defp dhcp_options(_ipv4, hostname) do
"""
script #{udhcpc_handler_path()}
hostname #{hostname}
"""
end

defp static_options(ipv4, hostname) do
contents =
ipv4
|> Map.take([
:address,
:netmask,
:broadcast,
:metric,
:gateway,
:pointopoint,
:hwaddress,
:mtu,
:scope
])
|> Enum.map(fn {option, value} -> "#{option} #{value}" end)
|> Enum.join("\n ")

"""
#{contents}
hostname #{hostname}
"""
end

defp get_hostname do
{:ok, hostname} = :inet.gethostname()
to_string(hostname)
end

defp udhcpc_handler_path() do
Application.app_dir(:vintage_net, ["priv", "udhcpc_handler"])
end
end
23 changes: 3 additions & 20 deletions lib/vintage_net/technology/ethernet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule VintageNet.Technology.Ethernet do
@behaviour VintageNet.Technology

alias VintageNet.Interface.RawConfig
alias VintageNet.IP.ConfigToInterfaces

@impl true
def to_raw_config(ifname, %{type: __MODULE__} = config, opts) do
Expand All @@ -11,15 +12,14 @@ defmodule VintageNet.Technology.Ethernet do

network_interfaces_path = Path.join(tmpdir, "network_interfaces.#{ifname}")

hostname = config[:hostname] || get_hostname()

{:ok,
%RawConfig{
ifname: ifname,
type: __MODULE__,
source_config: config,
files: [
{network_interfaces_path, "iface #{ifname} inet dhcp" <> dhcp_options(hostname)}
{network_interfaces_path,
ConfigToInterfaces.config_to_interfaces_contents(ifname, config)}
],
child_specs: [{VintageNet.Interface.ConnectivityChecker, ifname}],
# ifup hangs forever until Ethernet is plugged in
Expand All @@ -34,14 +34,6 @@ defmodule VintageNet.Technology.Ethernet do
{:error, :bad_configuration}
end

defp dhcp_options(hostname) do
"""
script #{udhcpc_handler_path()}
hostname #{hostname}
"""
end

@impl true
def ioctl(_ifname, _command, _args) do
{:error, :unsupported}
Expand All @@ -62,13 +54,4 @@ defmodule VintageNet.Technology.Ethernet do
{:error, "Can't find #{path}"}
end
end

defp udhcpc_handler_path() do
Application.app_dir(:vintage_net, ["priv", "udhcpc_handler"])
end

defp get_hostname do
{:ok, hostname} = :inet.gethostname()
to_string(hostname)
end
end
61 changes: 2 additions & 59 deletions lib/vintage_net/technology/wifi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule VintageNet.Technology.WiFi do

alias VintageNet.WiFi.{Scan, WPA2}
alias VintageNet.Interface.RawConfig
alias VintageNet.IP.ConfigToInterfaces

@impl true
def to_raw_config(ifname, %{type: __MODULE__, wifi: wifi_config} = config, opts) do
Expand All @@ -18,7 +19,7 @@ defmodule VintageNet.Technology.WiFi do
control_interface_path = Path.join(tmpdir, "wpa_supplicant")

files = [
{network_interfaces_path, config_to_interfaces_contents(ifname, config)},
{network_interfaces_path, ConfigToInterfaces.config_to_interfaces_contents(ifname, config)},
{wpa_supplicant_conf_path,
wifi_to_supplicant_contents(wifi_config, control_interface_path, regulatory_domain)}
]
Expand Down Expand Up @@ -343,64 +344,6 @@ defmodule VintageNet.Technology.WiFi do
"bssid_whitelist=#{value}"
end

defp config_to_interfaces_contents(ifname, %{ipv4: %{method: :dhcp} = ipv4} = config) do
hostname = config[:hostname] || get_hostname()
"iface #{ifname} inet dhcp" <> dhcp_options(ipv4, hostname)
end

defp config_to_interfaces_contents(ifname, %{ipv4: %{method: :static} = ipv4} = config) do
hostname = config[:hostname] || get_hostname()
"iface #{ifname} inet static" <> static_options(ipv4, hostname)
end

# Default to DHCP
defp config_to_interfaces_contents(ifname, config) do
hostname = config[:hostname] || get_hostname()
"iface #{ifname} inet dhcp" <> dhcp_options(config, hostname)
end

# TODO: Remove duplication with ethernet!!
defp dhcp_options(_ipv4, hostname) do
"""
script #{udhcpc_handler_path()}
hostname #{hostname}
"""
end

defp static_options(ipv4, hostname) do
contents =
ipv4
|> Map.take([
:address,
:netmask,
:broadcast,
:metric,
:gateway,
:pointopoint,
:hwaddress,
:mtu,
:scope
])
|> Enum.map(fn {option, value} -> "#{option} #{value}" end)
|> Enum.join("\n ")

"""
#{contents}
hostname #{hostname}
"""
end

defp udhcpc_handler_path() do
Application.app_dir(:vintage_net, ["priv", "udhcpc_handler"])
end

defp get_hostname do
{:ok, hostname} = :inet.gethostname()
to_string(hostname)
end

defp network_config(config) do
config =
Enum.map(config, fn
Expand Down

0 comments on commit 03e975a

Please sign in to comment.