Bug#516806: Should replicate gateway check from netcfg
* Martin Michlmayr <tbm@cyrius.com> [2009-02-23 20:08]:
> Michael Hahn reported that the installer didn't boot because netcfg
> showed a "Unreachable gateway" error message. oldsys-preseed should
> implement a similar check and use DHCP if the network configuration is
> invalid.
I've implemented this now based on some code Stephen Gran kindly
wrote.
Stephen, are you happy to release your code under GPL 2 or later?
The only thing that is not done that netcfg does is to make up a
netmask based on the IP address if no netmask is given.
Index: functions
===================================================================
--- functions (revision 57551)
+++ functions (working copy)
@@ -1,4 +1,5 @@
-# Copyright (C) 2005, 2006, 2008 Martin Michlmayr <tbm@cyrius.com>
+# Copyright (C) 2005, 2006, 2008, 2009 Martin Michlmayr <tbm@cyrius.com>
+# Copyright (C) 2009 Stephen Gran <sgran@debian.org>
# This code is covered by the GNU General Public License (version 2
# or higher)
@@ -246,11 +247,42 @@
return 0
}
-# Sanity check for static IP configuration: netcfg will prompt for an IP
-# address and nameserver if they are not set, so use DHCP in that case.
+# Convert an IP address (like 127.0.0.1) to bits.
+make_bit_address () {
+ addr=$1
+
+ first=$(( $(echo $addr | cut -d. -f1) << 24))
+ second=$(( $(echo $addr | cut -d. -f2) << 16))
+ third=$(( $(echo $addr | cut -d. -f3) << 8))
+ fourth=$(echo $addr | cut -d. -f4)
+ echo $(( $first + $second + $third + $fourth ))
+}
+
+# Check if two IP addresses are on the same network.
+address_is_in () {
+ addr=$1
+ addr2=$2
+ netmask=$3
+
+ bitaddr=$(make_bit_address $addr)
+ bitaddr2=$(make_bit_address $addr2)
+ bitmask=$(make_bit_address $netmask)
+
+ [ "$(( $bitaddr & $bitmask ))" = "$(( $bitaddr2 & $bitmask))" ]
+}
+
+# Sanity checks for static IP configuration
sanity_check_static_config() {
+ # netcfg will prompt for an IP address and nameserver if they are
+ # not set, so use DHCP in that case.
if [ "$NONINTERACTIVE" = "yes" ] && [ -z "$IPADDRESS" -o -z "$NAMESERVERS" ]; then
NET_CONFIG=dhcp
fi
+ # Replicate the "gateway is unreachable" check from netcfg.
+ if [ -n "$IPADDRESS" -a -n "$GATEWAY" -a -n "$NETMASK " ]; then
+ if ! address_is_in "$IPADDRESS" "$GATEWAY" "$NETMASK"; then
+ NET_CONFIG=dhcp
+ fi
+ fi
}
Index: tests/misc/gateway.test
===================================================================
--- tests/misc/gateway.test (revision 0)
+++ tests/misc/gateway.test (revision 0)
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+tmp="$1"
+
+check_gateway() {
+ if ! address_is_in "$1" "$2" "$3" ; then
+ echo "Invalid gateway $2" >> $tmp
+ else
+ echo "Valid gateway $2" >> $tmp
+ fi
+}
+
+check_gateway 192.168.178.10 192.168.178.1 255.255.255.0
+check_gateway 192.168.178.10 192.168.1.1 255.255.255.0
+
Property changes on: tests/misc/gateway.test
___________________________________________________________________
Added: svn:executable
+ *
Index: tests/misc/gateway.preseed
===================================================================
--- tests/misc/gateway.preseed (revision 0)
+++ tests/misc/gateway.preseed (revision 0)
@@ -0,0 +1,2 @@
+Valid gateway 192.168.178.1
+Invalid gateway 192.168.1.1
--
Martin Michlmayr
http://www.cyrius.com/
Reply to: