Skip to content

Commit

Permalink
Fix confusion with dhcpd subnet option
Browse files Browse the repository at this point in the history
Busybox calls the DHCP subnet mask option field "subnet" when
configuring it. This is reflected in VintageNet's configuration options
for it since they match one for one.

This cause some confusion on whether the subnet or subnet mask should be
passed. This fixes issues with the documentation and adds `:netmask` as
an alias so that users can make their own code more clear if they want.
  • Loading branch information
fhunleth committed May 12, 2023
1 parent 749904b commit 16b7895
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ involved. Here is a basic configuration:
end: "192.168.24.10",
options: %{
dns: ["1.1.1.1", "1.0.0.1"],
subnet: "192.168.24.255",
subnet: "255.255.255.0",
router: ["192.168.24.1"]
}
}
Expand Down
8 changes: 6 additions & 2 deletions lib/vintage_net/ip/dhcpd_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule VintageNet.IP.DhcpdConfig do
* `:router` - IP_LIST
* `:search` - STRING_LIST - [0x77] search domains
* `:serverid` - IP (defaults to the interface's IP address)
* `:subnet` - IP (as a subnet mask)
* `:subnet` or `:netmask` - IP as a subnet mask (`:netmask` is an alias for `:subnet`)
> #### :options {: .info}
> Options may also be passed in as integers. These are passed directly to the DHCP server
Expand All @@ -47,7 +47,7 @@ defmodule VintageNet.IP.DhcpdConfig do
end: "192.168.24.10",
options: %{
dns: ["1.1.1.1", "1.0.0.1"],
subnet: "192.168.24.255",
netmask: "255.255.255.0",
router: ["192.168.24.1"]
}
}
Expand Down Expand Up @@ -113,6 +113,10 @@ defmodule VintageNet.IP.DhcpdConfig do

defp normalize_options(dhcpd_config), do: dhcpd_config

# Support :netmask as an alias to :subnet in v0.13.2. This makes
# the configuration more consistent with `:ipv4` options.
defp normalize_option({:netmask, ip}), do: normalize_option({:subnet, ip})

defp normalize_option({ip_option, ip})
when ip_option in @ip_options do
{ip_option, IP.ip_to_tuple!(ip)}
Expand Down
24 changes: 24 additions & 0 deletions test/vintage_net/ip/dhcpd_config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ defmodule VintageNet.IP.DhcpdConfigTest do
:mtu => 9216,
:serverid => {192, 168, 1, 1},
:hostname => "marshmallow",
:subnet => "255.255.255.0",
0x08 => "01020304"
}
}
Expand All @@ -58,6 +59,7 @@ defmodule VintageNet.IP.DhcpdConfigTest do
:hostname => "marshmallow",
:mtu => 9216,
:serverid => {192, 168, 1, 1},
:subnet => {255, 255, 255, 0},
8 => "01020304"
}
}
Expand All @@ -66,6 +68,28 @@ defmodule VintageNet.IP.DhcpdConfigTest do
assert normalized_config == DhcpdConfig.normalize(config)
end

test "dhcpd netmask is an alias for subnet" do
config = %{
dhcpd: %{
start: "192.168.1.2",
end: "192.168.1.100",
options: %{
netmask: "255.255.255.0"
}
}
}

normalized_config = %{
dhcpd: %{
start: {192, 168, 1, 2},
end: {192, 168, 1, 100},
options: %{subnet: {255, 255, 255, 0}}
}
}

assert normalized_config == DhcpdConfig.normalize(config)
end

test "normalize fixes item passed instead of list" do
# Pass an IP address rather than a list for the DNS option
config = %{
Expand Down

0 comments on commit 16b7895

Please sign in to comment.