-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for udhcpd in wifi technology
- Loading branch information
1 parent
e61d32d
commit f726935
Showing
6 changed files
with
244 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
defmodule VintageNet.IP.ConfigToUdhcpd do | ||
@moduledoc """ | ||
This is a helper module for VintageNet.Technology implementations that use | ||
the udhcpd server. | ||
""" | ||
|
||
@doc """ | ||
Convert a configuration to the contents of a /etc/udhcpd.conf file | ||
`start` - Start of the lease block | ||
`end` - End of the lease block | ||
`max_leases` - The maximum number of leases | ||
`decline_time` - The amount of time that an IP will be reserved (leased to nobody) | ||
`conflict_time` -The amount of time that an IP will be reserved | ||
`offer_time` - How long an offered address is reserved (seconds) | ||
`min_lease` - If client asks for lease below this value, it will be rounded up to this value (seconds) | ||
`auto_time` - The time period at which udhcpd will write out leases file. | ||
`static_leases` - list of `{macaddress, ipaddress}` | ||
""" | ||
@spec config_to_udhcpd_contents(VintageNet.ifname(), map(), Path.t()) :: String.t() | ||
def config_to_udhcpd_contents(ifname, %{dhcpd: dhcpd}, tmpdir) do | ||
pidfile = Path.join(tmpdir, "udhcpd.#{ifname}.pid") | ||
lease_file = Path.join(tmpdir, "udhcpd.#{ifname}.leases") | ||
|
||
initial = """ | ||
interface #{ifname} | ||
pidfile #{pidfile} | ||
lease_file #{lease_file} | ||
""" | ||
|
||
config = Enum.map(dhcpd, &to_udhcpd_string/1) | ||
IO.iodata_to_binary([initial, "\n", config, "\n"]) | ||
end | ||
|
||
defp to_udhcpd_string({:start, val}) do | ||
"start #{val}\n" | ||
end | ||
|
||
defp to_udhcpd_string({:end, val}) do | ||
"end #{val}\n" | ||
end | ||
|
||
defp to_udhcpd_string({:max_leases, val}) do | ||
"max_leases #{val}\n" | ||
end | ||
|
||
defp to_udhcpd_string({:decline_time, val}) do | ||
"decline_time #{val}\n" | ||
end | ||
|
||
defp to_udhcpd_string({:conflict_time, val}) do | ||
"conflict_time #{val}\n" | ||
end | ||
|
||
defp to_udhcpd_string({:offer_time, val}) do | ||
"offer_time #{val}\n" | ||
end | ||
|
||
defp to_udhcpd_string({:min_lease, val}) do | ||
"min_lease #{val}\n" | ||
end | ||
|
||
defp to_udhcpd_string({:auto_time, val}) do | ||
"auto_time #{val}\n" | ||
end | ||
|
||
defp to_udhcpd_string({:static_leases, leases}) do | ||
Enum.map(leases, fn {mac, ip} -> | ||
"static_lease #{mac} #{ip}\n" | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule VintageNet.IP.ConfigToUdhcpdTest do | ||
use ExUnit.Case | ||
alias VintageNet.IP.ConfigToUdhcpd | ||
|
||
test "dhcp server config" do | ||
tmp_dir = "test_tmp" | ||
|
||
input = %{ | ||
dhcpd: %{ | ||
start: "192.168.1.2", | ||
end: "192.168.1.100", | ||
max_leases: 98, | ||
decline_time: 100, | ||
conflict_time: 200, | ||
offer_time: 300, | ||
min_lease: 60, | ||
auto_time: 60, | ||
static_leases: [ | ||
{"00:60:08:11:CE:4E", "192.168.1.55"}, | ||
{"00:60:08:11:CE:3E", "192.168.1.56"} | ||
] | ||
} | ||
} | ||
|
||
output = ConfigToUdhcpd.config_to_udhcpd_contents("eth0", input, tmp_dir) | ||
|
||
assert output =~ """ | ||
auto_time 60 | ||
conflict_time 200 | ||
decline_time 100 | ||
end 192.168.1.100 | ||
max_leases 98 | ||
min_lease 60 | ||
offer_time 300 | ||
start 192.168.1.2 | ||
static_lease 00:60:08:11:CE:4E 192.168.1.55 | ||
static_lease 00:60:08:11:CE:3E 192.168.1.56 | ||
""" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters