From 41c02bca59078ed16fab24ba5e10d47426be8051 Mon Sep 17 00:00:00 2001 From: SDGDen Date: Mon, 9 Mar 2026 11:22:40 +0100 Subject: [PATCH] fix dhcp config again. --- deploy.sh | 196 +++++++++++++++++++++++++++--------------------------- 1 file changed, 97 insertions(+), 99 deletions(-) diff --git a/deploy.sh b/deploy.sh index 4851dba..2a40fa1 100644 --- a/deploy.sh +++ b/deploy.sh @@ -618,18 +618,12 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t IFS=. read -r a b c d <<< "$ip" # Convert each octet to binary with error checking - local bin_a=$(echo "obase=2; $a" | bc 2>/dev/null || echo "ERROR") - local bin_b=$(echo "obase=2; $b" | bc 2>/dev/null || echo "ERROR") - local bin_c=$(echo "obase=2; $c" | bc 2>/dev/null || echo "ERROR") - local bin_d=$(echo "obase=2; $d" | bc 2>/dev/null || echo "ERROR") + local bin_a=$(printf "%08d" "$(echo "obase=2; $a" | bc)") + local bin_b=$(printf "%08d" "$(echo "obase=2; $b" | bc)") + local bin_c=$(printf "%08d" "$(echo "obase=2; $c" | bc)") + local bin_d=$(printf "%08d" "$(echo "obase=2; $d" | bc)") - if [[ "$bin_a" == "ERROR" || "$bin_b" == "ERROR" || "$bin_c" == "ERROR" || "$bin_d" == "ERROR" ]]; then - echo "Error: Failed to convert IP '$ip' to binary" >&2 - return 1 - fi - - # Pad each octet to 8 bits - printf "%08d%08d%08d%08d" "$bin_a" "$bin_b" "$bin_c" "$bin_d" + echo "${bin_a}${bin_b}${bin_c}${bin_d}" } # Function to convert binary string to IP with error checking @@ -648,15 +642,10 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t local octet4=${bin:24:8} # Convert each octet with error checking - local ip1=$(echo "ibase=2; $octet1" | bc 2>/dev/null || echo "ERROR") - local ip2=$(echo "ibase=2; $octet2" | bc 2>/dev/null || echo "ERROR") - local ip3=$(echo "ibase=2; $octet3" | bc 2>/dev/null || echo "ERROR") - local ip4=$(echo "ibase=2; $octet4" | bc 2>/dev/null || echo "ERROR") - - if [[ "$ip1" == "ERROR" || "$ip2" == "ERROR" || "$ip3" == "ERROR" || "$ip4" == "ERROR" ]]; then - echo "Error: Failed to convert binary '$bin' to IP" >&2 - return 1 - fi + local ip1=$(echo "ibase=2; $octet1" | bc) + local ip2=$(echo "ibase=2; $octet2" | bc) + local ip3=$(echo "ibase=2; $octet3" | bc) + local ip4=$(echo "ibase=2; $octet4" | bc) echo "$ip1.$ip2.$ip3.$ip4" } @@ -668,12 +657,6 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t # Convert IP to binary local bin_ip=$(ip_to_bin "$ip" || return 1) - local bin_add=$(printf "%08d" "$(echo "obase=2; $num" | bc 2>/dev/null || echo "ERROR")") - - if [ "$bin_add" = "ERROR" ]; then - echo "Error: Failed to convert number $num to binary" >&2 - return 1 - fi # Split the IP into 4 octets (8 bits each) local octet1=${bin_ip:0:8} @@ -681,31 +664,41 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t local octet3=${bin_ip:16:8} local octet4=${bin_ip:24:8} + # Convert octets to decimal + local dec1=$((2#$octet1)) + local dec2=$((2#$octet2)) + local dec3=$((2#$octet3)) + local dec4=$((2#$octet4)) + # Add the number to the last octet - local new_octet4=$((2#$octet4 + 2#$bin_add)) + dec4=$((dec4 + num)) - # 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 2>/dev/null || echo "ERROR")") - - if [ "$octet3" = "ERROR" ]; then - echo "Error: Failed to process octet3 after carry" >&2 - return 1 - fi + # Handle overflow + if [ $dec4 -gt 255 ]; then + dec4=$((dec4 - 256)) + dec3=$((dec3 + 1)) fi - # Update the last octet - octet4=$(printf "%08d" "$(echo "obase=2; $new_octet4" | bc 2>/dev/null || echo "ERROR")") - - if [ "$octet4" = "ERROR" ]; then - echo "Error: Failed to process octet4 after addition" >&2 - return 1 + # Handle overflow in third octet + if [ $dec3 -gt 255 ]; then + dec3=$((dec3 - 256)) + dec2=$((dec2 + 1)) fi - # Reconstruct the binary IP string and convert to IP - bin_to_ip "${octet1}${octet2}${octet3}${octet4}" || return 1 + # Handle overflow in second octet (unlikely in our use case) + if [ $dec2 -gt 255 ]; then + dec2=$((dec2 - 256)) + dec1=$((dec1 + 1)) + fi + + # Convert back to binary + octet1=$(printf "%08d" "$(echo "obase=2; $dec1" | bc)") + octet2=$(printf "%08d" "$(echo "obase=2; $dec2" | bc)") + octet3=$(printf "%08d" "$(echo "obase=2; $dec3" | bc)") + octet4=$(printf "%08d" "$(echo "obase=2; $dec4" | bc)") + + # Convert back to IP + bin_to_ip "${octet1}${octet2}${octet3}${octet4}" } # Function to subtract a number from an IP address (returns IP string) @@ -715,12 +708,6 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t # Convert IP to binary local bin_ip=$(ip_to_bin "$ip" || return 1) - local bin_sub=$(printf "%08d" "$(echo "obase=2; $num" | bc 2>/dev/null || echo "ERROR")") - - if [ "$bin_sub" = "ERROR" ]; then - echo "Error: Failed to convert number $num to binary" >&2 - return 1 - fi # Split the IP into 4 octets (8 bits each) local octet1=${bin_ip:0:8} @@ -728,31 +715,41 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t local octet3=${bin_ip:16:8} local octet4=${bin_ip:24:8} + # Convert octets to decimal + local dec1=$((2#$octet1)) + local dec2=$((2#$octet2)) + local dec3=$((2#$octet3)) + local dec4=$((2#$octet4)) + # Subtract the number from the last octet - local new_octet4=$((2#$octet4 - 2#$bin_sub)) + dec4=$((dec4 - num)) - # Handle underflow (if new_octet4 < 0) - if [ "$new_octet4" -lt 0 ]; then - new_octet4=$((new_octet4 + 256)) - octet3=$((2#$octet3 - 1)) # Borrow from the third octet - octet3=$(printf "%08d" "$(echo "obase=2; $octet3" | bc 2>/dev/null || echo "ERROR")") - - if [ "$octet3" = "ERROR" ]; then - echo "Error: Failed to process octet3 after borrow" >&2 - return 1 - fi + # Handle underflow + if [ $dec4 -lt 0 ]; then + dec4=$((dec4 + 256)) + dec3=$((dec3 - 1)) fi - # Update the last octet - octet4=$(printf "%08d" "$(echo "obase=2; $new_octet4" | bc 2>/dev/null || echo "ERROR")") - - if [ "$octet4" = "ERROR" ]; then - echo "Error: Failed to process octet4 after subtraction" >&2 - return 1 + # Handle underflow in third octet + if [ $dec3 -lt 0 ]; then + dec3=$((dec3 + 256)) + dec2=$((dec2 - 1)) fi - # Reconstruct the binary IP string and convert to IP - bin_to_ip "${octet1}${octet2}${octet3}${octet4}" || return 1 + # Handle underflow in second octet (unlikely in our use case) + if [ $dec2 -lt 0 ]; then + dec2=$((dec2 + 256)) + dec1=$((dec1 - 1)) + fi + + # Convert back to binary + octet1=$(printf "%08d" "$(echo "obase=2; $dec1" | bc)") + octet2=$(printf "%08d" "$(echo "obase=2; $dec2" | bc)") + octet3=$(printf "%08d" "$(echo "obase=2; $dec3" | bc)") + octet4=$(printf "%08d" "$(echo "obase=2; $dec4" | bc)") + + # Convert back to IP + bin_to_ip "${octet1}${octet2}${octet3}${octet4}" } # Function to compare two IP addresses (returns 0 if ip1 <= ip2, 1 if ip1 > ip2) @@ -760,32 +757,22 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t local ip1=$1 local ip2=$2 - # Convert IPs to binary - local bin1=$(ip_to_bin "$ip1" 2>/dev/null || echo "ERROR") - local bin2=$(ip_to_bin "$ip2" 2>/dev/null || echo "ERROR") + # Convert IPs to their components + IFS=. read -r a1 b1 c1 d1 <<< "$ip1" + IFS=. read -r a2 b2 c2 d2 <<< "$ip2" - if [[ "$bin1" == "ERROR" || "$bin2" == "ERROR" ]]; then - echo "Error: Failed to convert IPs to binary for comparison" >&2 - return 1 - fi + # Compare each octet in order + if [ "$a1" -lt "$a2" ]; then return 0; fi + if [ "$a1" -gt "$a2" ]; then return 1; fi - if [ "$bin1" = "$bin2" ]; then - return 0 # equal - fi + if [ "$b1" -lt "$b2" ]; then return 0; fi + if [ "$b1" -gt "$b2" ]; then return 1; fi - # Compare as 32-bit binary numbers - for i in {0..31}; do - local bit1=${bin1:$i:1} - local bit2=${bin2:$i:1} + if [ "$c1" -lt "$c2" ]; then return 0; fi + if [ "$c1" -gt "$c2" ]; then return 1; fi - if [ "$bit1" -lt "$bit2" ]; then - return 0 # ip1 < ip2 - elif [ "$bit1" -gt "$bit2" ]; then - return 1 # ip1 > ip2 - fi - done - - return 0 # equal (shouldn't reach here) + if [ "$d1" -le "$d2" ]; then return 0; fi + return 1 } # Function to check if an IP is within a range @@ -845,8 +832,10 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t # Ensure DHCP start is not before network+1 network_plus_1=$(add_to_ip "$network_address" 1 || exit 1) - compare_ips "$dhcp_start" "$network_plus_1" - if [ $? -eq 1 ]; then # if dhcp_start < network_plus_1 + echo " Network+1: $network_plus_1" + + if compare_ips "$dhcp_start" "$network_plus_1"; then + # dhcp_start <= network_plus_1, so we need to adjust echo " Adjusting DHCP start to network+1: $network_plus_1" dhcp_start="$network_plus_1" fi @@ -857,23 +846,32 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t # Ensure DHCP end is not after broadcast-1 broadcast_minus_1=$(subtract_from_ip "$broadcast_address" 1 || exit 1) - compare_ips "$dhcp_end" "$broadcast_minus_1" - if [ $? -eq 1 ]; then # if dhcp_end > broadcast_minus_1 + echo " Broadcast-1: $broadcast_minus_1" + + if ! compare_ips "$dhcp_end" "$broadcast_minus_1"; then + # dhcp_end > broadcast_minus_1, so we need to adjust echo " Adjusting DHCP end to broadcast-1: $broadcast_minus_1" dhcp_end="$broadcast_minus_1" fi # Exclude local IP from range - is_ip_in_range "$localip" "$dhcp_start" "$dhcp_end" - if [ $? -eq 0 ]; then + if is_ip_in_range "$localip" "$dhcp_start" "$dhcp_end"; then echo "Local IP $localip is in DHCP range. Adjusting..." # For simplicity, move the start up by 1 new_dhcp_start=$(add_to_ip "$localip" 1 || exit 1) - if compare_ips "$new_dhcp_start" "$dhcp_end" && [ $? -eq 0 ]; then + if compare_ips "$new_dhcp_start" "$dhcp_end"; then echo " Adjusting DHCP start to $new_dhcp_start" dhcp_start="$new_dhcp_start" else echo " Cannot adjust DHCP start without making it exceed end" + # Try moving the end down by 1 instead + new_dhcp_end=$(subtract_from_ip "$localip" 1 || exit 1) + if ! compare_ips "$dhcp_start" "$new_dhcp_end"; then + echo " Cannot adjust DHCP range without excluding valid IPs" + else + echo " Adjusting DHCP end to $new_dhcp_end" + dhcp_end="$new_dhcp_end" + fi fi fi