fix dhcp calculations to now exclusively use binary representation.
This commit is contained in:
137
deploy.sh
137
deploy.sh
@@ -615,13 +615,12 @@ 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 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")
|
||||
# Function to add a number to an IP address (returns IP string)
|
||||
add_to_ip() {
|
||||
local ip=$1
|
||||
local num=$2
|
||||
local bin_ip=$(ip_to_bin "$ip")
|
||||
local bin_add=$(printf "%08d" "$(echo "obase=2; $num" | bc)")
|
||||
|
||||
# Split the IP into 4 octets (8 bits each)
|
||||
local octet1=${bin_ip:0:8}
|
||||
@@ -629,7 +628,7 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t
|
||||
local octet3=${bin_ip:16:8}
|
||||
local octet4=${bin_ip:24:8}
|
||||
|
||||
# Add the binary value to the last octet
|
||||
# Add the number to the last octet
|
||||
local new_octet4=$((2#$octet4 + 2#$bin_add))
|
||||
|
||||
# Handle overflow (if new_octet4 > 255)
|
||||
@@ -642,20 +641,83 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t
|
||||
# Update the last octet
|
||||
octet4=$(printf "%08d" "$(echo "obase=2; $new_octet4" | bc)")
|
||||
|
||||
# Reconstruct the binary IP string
|
||||
echo "${octet1}${octet2}${octet3}${octet4}"
|
||||
# Reconstruct the binary IP string and convert to IP
|
||||
bin_to_ip "${octet1}${octet2}${octet3}${octet4}"
|
||||
}
|
||||
|
||||
# Function to subtract 1 from a binary IP string
|
||||
subtract_one_from_ip() {
|
||||
local bin_ip=$1
|
||||
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}))
|
||||
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)"
|
||||
# Function to subtract a number from an IP address (returns IP string)
|
||||
subtract_from_ip() {
|
||||
local ip=$1
|
||||
local num=$2
|
||||
local bin_ip=$(ip_to_bin "$ip")
|
||||
local bin_sub=$(printf "%08d" "$(echo "obase=2; $num" | bc)")
|
||||
|
||||
# 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}
|
||||
|
||||
# Subtract the number from the last octet
|
||||
local new_octet4=$((2#$octet4 - 2#$bin_sub))
|
||||
|
||||
# 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)") # 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 and convert to IP
|
||||
bin_to_ip "${octet1}${octet2}${octet3}${octet4}"
|
||||
}
|
||||
|
||||
# Function to compare two IP addresses (returns 0 if ip1 <= ip2, 1 if ip1 > ip2)
|
||||
compare_ips() {
|
||||
local ip1=$1
|
||||
local ip2=$2
|
||||
local bin1=$(ip_to_bin "$ip1")
|
||||
local bin2=$(ip_to_bin "$ip2")
|
||||
|
||||
if [ "$bin1" = "$bin2" ]; then
|
||||
return 0 # equal
|
||||
fi
|
||||
|
||||
# Compare as 32-bit binary numbers
|
||||
for i in {0..31}; do
|
||||
local bit1=${bin1:$i:1}
|
||||
local bit2=${bin2:$i:1}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
# Function to check if an IP is within a range
|
||||
is_ip_in_range() {
|
||||
local ip=$1
|
||||
local start=$2
|
||||
local end=$3
|
||||
|
||||
compare_ips "$ip" "$start"
|
||||
local ip_ge_start=$?
|
||||
|
||||
compare_ips "$ip" "$end"
|
||||
local ip_le_end=$?
|
||||
|
||||
if [ "$ip_ge_start" -eq 0 ] && [ "$ip_le_end" -eq 0 ]; then
|
||||
return 0 # in range
|
||||
else
|
||||
return 1 # out of range
|
||||
fi
|
||||
}
|
||||
|
||||
# Calculate network and broadcast addresses using binary operations
|
||||
@@ -689,40 +751,33 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t
|
||||
echo " Broadcast Address: $broadcast_address"
|
||||
|
||||
# Calculate DHCP start: network_address + 10
|
||||
dhcp_start_bin=$(add_binary_to_ip "$network_bin" "1010") # 10 in binary
|
||||
dhcp_start=$(add_to_ip "$network_address" 10)
|
||||
|
||||
# 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
|
||||
network_plus_1=$(add_to_ip "$network_address" 1)
|
||||
compare_ips "$dhcp_start" "$network_plus_1"
|
||||
if [ $? -eq 1 ]; then # if dhcp_start < network_plus_1
|
||||
dhcp_start="$network_plus_1"
|
||||
fi
|
||||
|
||||
# Calculate DHCP end: dhcp_start + 100
|
||||
dhcp_end_bin=$(add_binary_to_ip "$dhcp_start_bin" "1100100") # 100 in binary
|
||||
dhcp_end=$(add_to_ip "$dhcp_start" 100)
|
||||
|
||||
# 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"
|
||||
broadcast_minus_1=$(subtract_from_ip "$broadcast_address" 1)
|
||||
compare_ips "$dhcp_end" "$broadcast_minus_1"
|
||||
if [ $? -eq 1 ]; then # if dhcp_end > broadcast_minus_1
|
||||
dhcp_end="$broadcast_minus_1"
|
||||
fi
|
||||
|
||||
# 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
|
||||
is_ip_in_range "$localip" "$dhcp_start" "$dhcp_end"
|
||||
if [ $? -eq 0 ]; then
|
||||
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_binary_to_ip "$localip_bin" "1") # Move start above local IP
|
||||
else
|
||||
dhcp_end_bin=$(subtract_one_from_ip "$localip_bin") # Move end below local IP
|
||||
fi
|
||||
# For simplicity, move the start up by 1
|
||||
dhcp_start=$(add_to_ip "$localip" 1)
|
||||
fi
|
||||
|
||||
# Convert to IP format
|
||||
dhcp_start=$(bin_to_ip "$dhcp_start_bin")
|
||||
dhcp_end=$(bin_to_ip "$dhcp_end_bin")
|
||||
|
||||
# Validate DHCP range
|
||||
if [ -z "$dhcp_start" ] || [ -z "$dhcp_end" ]; then
|
||||
echo "Error: Invalid DHCP range calculated. DHCP start or end is empty. Aborting."
|
||||
|
||||
Reference in New Issue
Block a user