diff --git a/deploy.sh b/deploy.sh index 2b0e8e5..abd5e14 100644 --- a/deploy.sh +++ b/deploy.sh @@ -587,13 +587,11 @@ fi if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; then echo "Configuring DHCP on PiHole..." echo "Gathering network information..." - # Get network information gateway=$(ip route | awk '/default/ {print $3}') interface=$(ip route | awk '/default/ {print $5}') netmask=$(ifconfig $interface | awk '/netmask/ {print $4}') localip=$(hostname -I | awk '{print $1}') - echo " Gateway: $gateway" echo " Interface: $interface" echo " Netmask: $netmask" @@ -617,21 +615,47 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t echo "$(echo "ibase=2; $octet1" | bc).$(echo "ibase=2; $octet2" | bc).$(echo "ibase=2; $octet3" | bc).$(echo "ibase=2; $octet4" | bc)" } - # Function to add a number to a binary IP string - add_to_bin_ip() { + # Function to add a binary value to a binary IP string + add_binary_to_ip() { + local bin_ip=$1 + local bin_add=$2 # e.g., "1010" for 10, "1100100" for 100 + + # Pad the binary addition to 8 bits (e.g., "1010" → "00001010") + bin_add=$(printf "%08d" "$bin_add") + + # Split the IP into 4 octets (8 bits each) + local octet1=${bin_ip:0:8} + local octet2=${bin_ip:8:8} + local octet3=${bin_ip:16:8} + local octet4=${bin_ip:24:8} + + # Add the binary value to the last octet + local new_octet4=$((2#$octet4 + 2#$bin_add)) + + # Handle overflow (if new_octet4 > 255) + if [ "$new_octet4" -gt 255 ]; then + new_octet4=$((new_octet4 - 256)) + octet3=$((2#$octet3 + 1)) # Carry over to the third octet + octet3=$(printf "%08d" "$(echo "obase=2; $octet3" | bc)") # Convert back to 8-bit binary + fi + + # Update the last octet + octet4=$(printf "%08d" "$(echo "obase=2; $new_octet4" | bc)") + + # Reconstruct the binary IP string + echo "${octet1}${octet2}${octet3}${octet4}" + } + + # Function to subtract 1 from a binary IP string + subtract_one_from_ip() { local bin_ip=$1 - local num=$2 - # Convert binary IP to decimal local dec_ip=$((2#${bin_ip:0:8} * 256**3 + 2#${bin_ip:8:8} * 256**2 + 2#${bin_ip:16:8} * 256 + 2#${bin_ip:24:8})) - # Add the number - dec_ip=$((dec_ip + num)) - # Convert back to binary IP - local new_bin_ip=$(printf "%08d%08d%08d%08d" \ + dec_ip=$((dec_ip - 1)) + printf "%08d%08d%08d%08d" \ "$(echo "obase=2; ($dec_ip >> 24) & 255" | bc)" \ "$(echo "obase=2; ($dec_ip >> 16) & 255" | bc)" \ "$(echo "obase=2; ($dec_ip >> 8) & 255" | bc)" \ - "$(echo "obase=2; $dec_ip & 255" | bc)") - echo "$new_bin_ip" + "$(echo "obase=2; $dec_ip & 255" | bc)" } # Calculate network and broadcast addresses using binary operations @@ -661,44 +685,41 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t network_address=$(bin_to_ip "$network_bin") broadcast_address=$(bin_to_ip "$broadcast_bin") - echo " Network Address: $network_address" echo " Broadcast Address: $broadcast_address" - # Calculate DHCP range using binary operations - # Start DHCP 10 IPs after the gateway (but not before network+1) - dhcp_start_bin=$(add_to_bin_ip "$gateway_bin" 10) - network_dec=$((2#${network_bin:0:8} * 256**3 + 2#${network_bin:8:8} * 256**2 + 2#${network_bin:16:8} * 256 + 2#${network_bin:24:8})) - dhcp_start_dec=$((2#${dhcp_start_bin:0:8} * 256**3 + 2#${dhcp_start_bin:8:8} * 256**2 + 2#${dhcp_start_bin:16:8} * 256 + 2#${dhcp_start_bin:24:8})) + # Calculate DHCP start: network_address + 10 + dhcp_start_bin=$(add_binary_to_ip "$network_bin" "1010") # 10 in binary - if [ "$dhcp_start_dec" -le "$network_dec" ]; then - dhcp_start_bin=$(add_to_bin_ip "$network_bin" 1) + # Ensure DHCP start is not before network+1 + if [ "$(bin_to_ip "$dhcp_start_bin")" = "$(bin_to_ip "$network_bin")" ]; then + dhcp_start_bin=$(add_binary_to_ip "$network_bin" "1") # network + 1 fi - # End DHCP 100 IPs after start (but not after broadcast-1) - dhcp_end_bin=$(add_to_bin_ip "$dhcp_start_bin" 100) - broadcast_dec=$((2#${broadcast_bin:0:8} * 256**3 + 2#${broadcast_bin:8:8} * 256**2 + 2#${broadcast_bin:16:8} * 256 + 2#${broadcast_bin:24:8})) - dhcp_end_dec=$((2#${dhcp_end_bin:0:8} * 256**3 + 2#${dhcp_end_bin:8:8} * 256**2 + 2#${dhcp_end_bin:16:8} * 256 + 2#${dhcp_end_bin:24:8})) + # Calculate DHCP end: dhcp_start + 100 + dhcp_end_bin=$(add_binary_to_ip "$dhcp_start_bin" "1100100") # 100 in binary - if [ "$dhcp_end_dec" -ge "$broadcast_dec" ]; then - dhcp_end_bin=$(add_to_bin_ip "$broadcast_bin" -1) + # Ensure DHCP end is not after broadcast-1 + broadcast_minus_1_bin=$(subtract_one_from_ip "$broadcast_bin") + if [ "$(printf "%d" "$(bin_to_ip "$dhcp_end_bin" | tr '.' ' ')")" -ge "$(printf "%d" "$(bin_to_ip "$broadcast_bin" | tr '.' ' ')")" ]; then + dhcp_end_bin="$broadcast_minus_1_bin" fi - # Ensure we don't include the local IP in the DHCP range - localip_dec=$((2#${localip_bin:0:8} * 256**3 + 2#${localip_bin:8:8} * 256**2 + 2#${localip_bin:16:8} * 256 + 2#${localip_bin:24:8})) - dhcp_start_dec=$((2#${dhcp_start_bin:0:8} * 256**3 + 2#${dhcp_start_bin:8:8} * 256**2 + 2#${dhcp_start_bin:16:8} * 256 + 2#${dhcp_start_bin:24:8})) - dhcp_end_dec=$((2#${dhcp_end_bin:0:8} * 256**3 + 2#${dhcp_end_bin:8:8} * 256**2 + 2#${dhcp_end_bin:16:8} * 256 + 2#${dhcp_end_bin:24:8})) + # Exclude local IP from range + localip_dec=$(printf "%d" "$(bin_to_ip "$localip_bin" | tr '.' ' ')") + dhcp_start_dec=$(printf "%d" "$(bin_to_ip "$dhcp_start_bin" | tr '.' ' ')") + dhcp_end_dec=$(printf "%d" "$(bin_to_ip "$dhcp_end_bin" | tr '.' ' ')") if [ "$localip_dec" -ge "$dhcp_start_dec" ] && [ "$localip_dec" -le "$dhcp_end_dec" ]; then - # If local IP is in the range, adjust the range + echo "Local IP is in DHCP range. Adjusting..." if [ $((localip_dec - dhcp_start_dec)) -lt $((dhcp_end_dec - localip_dec)) ]; then - dhcp_start_bin=$(add_to_bin_ip "$localip_bin" 1) + dhcp_start_bin=$(add_binary_to_ip "$localip_bin" "1") # Move start above local IP else - dhcp_end_bin=$(add_to_bin_ip "$localip_bin" -1) + dhcp_end_bin=$(subtract_one_from_ip "$localip_bin") # Move end below local IP fi fi - # Convert back to IP format + # Convert to IP format dhcp_start=$(bin_to_ip "$dhcp_start_bin") dhcp_end=$(bin_to_ip "$dhcp_end_bin") @@ -725,7 +746,6 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t # Restart PiHole to apply changes echo "Restarting PiHole to apply changes..." docker restart pihole - echo "DHCP configuration applied successfully." fi