Skip to content

Commit 0bbf52e

Browse files
authored
Merge pull request #2073 from RaspAP/fix/dhcp-static
Fix: Save dhcpcd static IP regardless of dnsmasq config state
2 parents 5b9ac36 + e3451c1 commit 0bbf52e

2 files changed

Lines changed: 24 additions & 27 deletions

File tree

includes/dhcp.php

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,37 +110,33 @@ function saveDHCPConfig($status)
110110
$dnsmasq = new DnsmasqManager();
111111
$iface = $_POST['interface'];
112112

113-
// dhcp
114-
if (!isset($_POST['dhcp-iface']) && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) {
115-
// remove dhcp + dnsmasq configs for selected interface
116-
$return = $dhcpcd->remove($iface, $status);
117-
$return = $dnsmasq->remove($iface, $status);
113+
// Always save dhcpcd static IP config
114+
$errors = $dhcpcd->validate($_POST);
115+
if (empty($errors)) {
116+
$dhcp_cfg = $dhcpcd->buildConfigEx($iface, $_POST, $status);
117+
$dhcpcd->saveConfig($dhcp_cfg, $iface, $status);
118118
} else {
119-
$errors = $dhcpcd->validate($_POST);
119+
foreach ($errors as $error) {
120+
$status->addMessage($error, 'danger');
121+
}
122+
}
123+
124+
// dnsmasq: remove if DHCP was previously enabled but now disabled
125+
if (!isset($_POST['dhcp-iface']) && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) {
126+
$dnsmasq->remove($iface, $status);
127+
} elseif (($_POST['dhcp-iface'] ?? '') == "1" || isset($_POST['mac'])) {
128+
$errors = $dnsmasq->validate($_POST);
120129
if (empty($errors)) {
121-
$dhcp_cfg = $dhcpcd->buildConfigEx($iface, $_POST, $status);
122-
$dhcpcd->saveConfig($dhcp_cfg, $iface, $status);
130+
$config = $dnsmasq->buildConfigEx($iface, $_POST);
131+
$return = $dnsmasq->saveConfig($config, $iface);
132+
$config = $dnsmasq->buildDefault($_POST);
133+
$return = $dnsmasq->saveConfigDefault($config);
123134
} else {
124135
foreach ($errors as $error) {
125136
$status->addMessage($error, 'danger');
126137
}
127138
}
128-
129-
// dnsmasq
130-
if (($_POST['dhcp-iface'] == "1") || (isset($_POST['mac']))) {
131-
$errors = $dnsmasq->validate($_POST);
132-
if (empty($errors)) {
133-
$config = $dnsmasq->buildConfigEx($iface, $_POST);
134-
$return = $dnsmasq->saveConfig($config, $iface);
135-
$config = $dnsmasq->buildDefault($_POST);
136-
$return = $dnsmasq->saveConfigDefault($config);
137-
} else {
138-
foreach ($errors as $error) {
139-
$status->addMessage($error, 'danger');
140-
}
141-
}
142-
}
143-
return true;
144139
}
140+
return true;
145141
}
146142

src/RaspAP/Networking/Hotspot/DhcpcdManager.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ public function buildConfigEx(string $iface, array $post_data, StatusMessage $st
177177
if (isset($post_data['DefaultGateway']) && $post_data['DefaultGateway'] !== '') {
178178
$cfg[] = 'static routers='.$post_data['DefaultGateway'];
179179
}
180-
if ($post_data['DNS1'] !== '' || $post_data['DNS2'] !== '') {
181-
$cfg[] = 'static domain_name_servers='.$post_data['DNS1'].' '.$post_data['DNS2'];
180+
if (!empty($post_data['DNS1']) || !empty($post_data['DNS2'])) {
181+
$cfg[] = 'static domain_name_servers='.trim(($post_data['DNS1'] ?? '').' '.($post_data['DNS2'] ?? ''));
182182
}
183-
if ($post_data['Metric'] !== '') {
183+
if (!empty($post_data['Metric'])) {
184184
$cfg[] = 'metric '.$post_data['Metric'];
185185
}
186186
if (($post_data['Fallback'] ?? 0) == 1) {
@@ -305,6 +305,7 @@ public function remove(string $iface, StatusMessage $status): bool
305305
return false;
306306
}
307307
}
308+
return false;
308309
}
309310

310311
/**

0 commit comments

Comments
 (0)