Skip to content

Commit 5911395

Browse files
committed
Support changing the subnet from 192.168.0.0
The default 192.168.0.0 network works well for the wizard's IP addresses and is easy to remember. There's confusion, though, when devices have WiFi adapters that can run in both AP and station mode at the same time. During the test step of connecting to an AP, if the network being tested is also on the 192.168.0.0 subnet, then there will be two 192.168.0.0 networks. To minimize the chance of this, it's now possible to set the subnet to something very unlikely to see. This isn't the default since changing the default would invalidate configuration instructions for people using the wizard in their projects.
1 parent 398ae0d commit 5911395

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,13 @@ Each configuration option is optional and will fall back to
111111
### DNS in AP-mode
112112

113113
When the wizard is running, users go to either `http://192.168.0.1/` or
114-
`http://wifi.config/` to access the web user interface. The latter can be
114+
`http://wifi.config/` to access the web user interface. Both can be
115115
changed via the `config.exs`:
116116

117117
```elixir
118118
config :vintage_net_wizard,
119-
dns_name: "my-wifi-config.com"
119+
dns_name: "my-wifi-config.com",
120+
subnet: "192.168.0.0"
120121
```
121122

122123
### Port

lib/vintage_net_wizard/ap_mode.ex

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@ defmodule VintageNetWizard.APMode do
44
"""
55

66
@default_hostname "vintage_net_wizard"
7+
@default_dns_name "wifi.config"
8+
@default_subnet {192, 168, 0, 0}
79

810
@doc """
911
Change the WiFi module into access point mode
1012
"""
1113
@spec into_ap_mode(VintageNet.ifname()) :: :ok | {:error, any()}
1214
def into_ap_mode(ifname) do
1315
hostname = get_hostname()
14-
our_name = Application.get_env(:vintage_net_wizard, :dns_name, "wifi.config")
16+
our_name = Application.get_env(:vintage_net_wizard, :dns_name, @default_dns_name)
1517

16-
config = ap_mode_configuration(hostname, our_name)
18+
subnet =
19+
Application.get_env(:vintage_net_wizard, :subnet, @default_subnet)
20+
|> VintageNet.IP.ip_to_tuple!()
21+
22+
config = ap_mode_configuration(hostname, our_name, subnet)
1723
VintageNet.configure(ifname, config, persist: false)
1824
end
1925

@@ -34,13 +40,26 @@ defmodule VintageNetWizard.APMode do
3440
VintageNet.configure(ifname, no_ap_mode)
3541
end
3642

43+
defp create_ip_24(subnet, host) when host > 0 and host < 255 do
44+
{a, b, c, _} = subnet
45+
{a, b, c, host}
46+
end
47+
3748
@doc """
3849
Return a configuration to put VintageNet into AP mode
50+
51+
* `hostname` - the device's hostname which will be modified to be the AP's SSID
52+
* `our_name` - the name for clients to use when connecting to the wizard if
53+
not using the IP address.
54+
* `class_c_subnet` - A class C subnet to use for all of the IP addresses on
55+
this network.
3956
"""
40-
@spec ap_mode_configuration(String.t(), String.t()) :: map()
41-
def ap_mode_configuration(hostname, our_name) do
57+
@spec ap_mode_configuration(String.t(), String.t(), :inet.ip4_address()) :: map()
58+
def ap_mode_configuration(hostname, our_name, class_c_subnet) do
4259
ssid = sanitize_hostname_for_ssid(hostname)
43-
our_ip_address = {192, 168, 0, 1}
60+
61+
subnet_mask = VintageNet.IP.prefix_length_to_subnet_mask(:inet, 24)
62+
our_ip_address = create_ip_24(class_c_subnet, 1)
4463

4564
%{
4665
type: VintageNetWiFi,
@@ -59,13 +78,12 @@ defmodule VintageNetWizard.APMode do
5978
prefix_length: 24
6079
},
6180
dhcpd: %{
62-
# These are defaults and are reproduced here as documentation
63-
start: {192, 168, 0, 20},
64-
end: {192, 168, 0, 254},
81+
start: create_ip_24(class_c_subnet, 20),
82+
end: create_ip_24(class_c_subnet, 254),
6583
max_leases: 235,
6684
options: %{
6785
dns: [our_ip_address],
68-
subnet: {255, 255, 255, 0},
86+
subnet: subnet_mask,
6987
router: [our_ip_address],
7088
domain: our_name,
7189
search: [our_name]

test/vintage_net_wizard/ap_mode_test.exs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule VintageNetWizard.APModeTest do
44
alias VintageNetWizard.APMode
55

66
test "AP mode configuration has expected content" do
7-
config = APMode.ap_mode_configuration("hostname", "our_name")
7+
config = APMode.ap_mode_configuration("hostname", "our_name", {192, 168, 0, 0})
88

99
expected = %{
1010
type: VintageNetWiFi,
@@ -29,18 +29,48 @@ defmodule VintageNetWizard.APModeTest do
2929
end
3030

3131
test "empty hostname gets changed to a valid ssid" do
32-
config = APMode.ap_mode_configuration("", "our_name")
32+
config = APMode.ap_mode_configuration("", "our_name", {192, 168, 0, 0})
3333

3434
%{vintage_net_wifi: %{networks: [%{ssid: ssid}]}} = config
3535

3636
assert ssid == "vintage_net_wizard"
3737
end
3838

3939
test "long hostname gets trimmed" do
40-
config = APMode.ap_mode_configuration("1234567890123456789012345678901234567890", "our_name")
40+
config =
41+
APMode.ap_mode_configuration(
42+
"1234567890123456789012345678901234567890",
43+
"our_name",
44+
{192, 168, 0, 0}
45+
)
4146

4247
%{vintage_net_wifi: %{networks: [%{ssid: ssid}]}} = config
4348

4449
assert byte_size(ssid) <= 32
4550
end
51+
52+
test "AP mode configuration on custom subnet" do
53+
config = APMode.ap_mode_configuration("hostname", "our_name", {10, 20, 30, 0})
54+
55+
expected = %{
56+
type: VintageNetWiFi,
57+
ipv4: %{address: {10, 20, 30, 1}, method: :static, prefix_length: 24},
58+
vintage_net_wifi: %{networks: [%{key_mgmt: :none, mode: :ap, ssid: "hostname"}]},
59+
dhcpd: %{
60+
end: {10, 20, 30, 254},
61+
max_leases: 235,
62+
options: %{
63+
dns: [{10, 20, 30, 1}],
64+
domain: "our_name",
65+
router: [{10, 20, 30, 1}],
66+
search: ["our_name"],
67+
subnet: {255, 255, 255, 0}
68+
},
69+
start: {10, 20, 30, 20}
70+
},
71+
dnsd: %{records: [{"our_name", {10, 20, 30, 1}}]}
72+
}
73+
74+
assert expected == config
75+
end
4676
end

0 commit comments

Comments
 (0)