Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI Support for Configuring DHCP Subnet Mask #2800

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions scripts/pi-hole/php/func.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ function validIP($address)
return !filter_var($address, FILTER_VALIDATE_IP) === false;
}

function validSubnetMask($subnetMask)
{
$octets = explode('.', $subnetMask);
if (count($octets) != 4) {
return false;
}

$subnetBinary = '';
foreach ($octets as $octet) {
if (!is_numeric($octet) || $octet < 0 || $octet > 255) {
return false;
}
$subnetBinary .= str_pad(decbin($octet), 8, '0', STR_PAD_LEFT);
}

if (strpos($subnetBinary, '01') !== false) {
return false;
}

return true;
}

function validCIDRIP($address)
{
// This validation strategy has been taken from ../js/groups-common.js
Expand Down
8 changes: 7 additions & 1 deletion scripts/pi-hole/php/savesettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,12 @@ function addStaticDHCPLease($mac, $ip, $hostname)
$error .= 'To IP ('.htmlspecialchars($to).') is invalid!<br>';
}

// Validate to Subnet Mask
$subnetMask = $_POST['subnetMask'];
if (!validSubnetMask($subnetMask)) {
$error .= 'Subnet Mask ('.htmlspecialchars($subnetMask).') is invalid!<br>';
}

// Validate router IP
$router = $_POST['router'];
if (!validIP($router)) {
Expand Down Expand Up @@ -531,7 +537,7 @@ function addStaticDHCPLease($mac, $ip, $hostname)
}

if (!strlen($error)) {
pihole_execute('-a enabledhcp '.$from.' '.$to.' '.$router.' '.$leasetime.' '.$domain.' '.$ipv6.' '.$rapidcommit);
pihole_execute('-a enabledhcp '.$from.' '.$to.' '.$subnetMask.' '.$router.' '.$leasetime.' '.$domain.' '.$ipv6.' '.$rapidcommit);
$success .= 'The DHCP server has been activated '.htmlspecialchars($type);
}
} else {
Expand Down
20 changes: 19 additions & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@
} else {
$DHCPend = '';
}
if (isset($setupVars['DHCP_SUBNET_MASK'])) {
$DHCPSubnetMask = $setupVars['DHCP_SUBNET_MASK'];
} else {
$DHCPSubnetMask = '255.255.255.0'; // Setting default value here
}
if (isset($setupVars['DHCP_ROUTER'])) {
$DHCProuter = $setupVars['DHCP_ROUTER'];
} else {
Expand Down Expand Up @@ -442,6 +447,7 @@
$DHCP = false;
$DHCPstart = '';
$DHCPend = '';
$DHCPsubnetMask = '255.255.255.0';
$DHCProuter = '';

// Try to guess initial settings
Expand Down Expand Up @@ -506,7 +512,19 @@
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="col-xs-12 col-sm-6 col-md-12 col-lg-6">
<label>Subnet Mask</label>
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">Subnet</div>
<input type="text" class="form-control DHCPgroup" name="subnetMask"
autocomplete="off" spellcheck="false" autocapitalize="none"
autocorrect="off" value="<?php echo $DHCPSubnetMask; ?>"
<?php if (!$DHCP) { ?>disabled<?php } ?>>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-12 col-lg-6">
<label>Router (gateway) IP address</label>
<div class="form-group">
<div class="input-group">
Expand Down