modify dnsmasq script to perform better calculations with new ipcalc script
authorMike Baker <mbm@openwrt.org>
Tue, 21 Mar 2006 11:10:03 +0000 (11:10 +0000)
committerMike Baker <mbm@openwrt.org>
Tue, 21 Mar 2006 11:10:03 +0000 (11:10 +0000)
SVN-Revision: 3426

openwrt/package/base-files/default/bin/ipcalc [new file with mode: 0755]
openwrt/package/base-files/default/etc/functions.sh
openwrt/package/base-files/default/usr/lib/common.awk
openwrt/package/busybox/config/networking/Config.in
openwrt/package/dnsmasq/files/S50dnsmasq

diff --git a/openwrt/package/base-files/default/bin/ipcalc b/openwrt/package/base-files/default/bin/ipcalc
new file mode 100755 (executable)
index 0000000..8d12545
--- /dev/null
@@ -0,0 +1,31 @@
+#!/bin/sh
+
+awk -f /usr/lib/common.awk -f - $* <<EOF
+BEGIN {
+       ipaddr=ip2int(ARGV[1])
+       netmask=ip2int(ARGV[2])
+       network=and(ipaddr,netmask)
+       broadcast=or(network,compl(netmask))
+       
+       start=or(network,and(ip2int(ARGV[3]),compl(netmask)))
+       limit=network+1
+       if (start<limit) start=limit
+       
+       end=or(start,and(ip2int(ARGV[4]),compl(netmask)))
+       limit=or(network,compl(netmask))-1
+       if (end>limit) end=limit
+
+       print "NETMASK="int2ip(netmask)
+       print "BROADCAST="int2ip(broadcast)
+       print "NETWORK="int2ip(network)
+       print "PREFIX="bitcount(int2ip(netmask))
+       
+       # range calculations:
+       # ipcalc <ip> <netmask> <start> <num>
+       
+       if (ARGC > 3) {
+               print "START="int2ip(start)
+               print "END="int2ip(end)
+       }
+}
+EOF
index 7c31539721fd275a7fe654ecf5aaa271c8aaf4b3..80b1792c864be005ebfbdc4f24aab30ec9263814 100755 (executable)
@@ -86,27 +86,3 @@ do_ifup() {
        ;;
        esac
 }
-
-bitcount () {
-  local c=$1
-  echo $((
-  c=((c>> 1)&0x55555555)+(c&0x55555555),
-  c=((c>> 2)&0x33333333)+(c&0x33333333),
-  c=((c>> 4)&0x0f0f0f0f)+(c&0x0f0f0f0f),
-  c=((c>> 8)&0x00ff00ff)+(c&0x00ff00ff),
-  c=((c>>16)&0x0000ffff)+(c&0x0000ffff)
-  ))
-}
-
-valid_netmask () {
-  return $((-($1)&~$1))
-}
-
-ip2int () (
-  set $(echo $1 | tr '\.' ' ')
-  echo $(($1<<24|$2<<16|$3<<8|$4))
-)
-
-int2ip () {
-  echo $(($1>>24&255)).$(($1>>16&255)).$(($1>>8&255)).$(($1&255))
-}
index 11454cec19edcb4b8b39cf657a17b94be4a50c3b..250a70f6416695efed9d660e3f383c7e28845e18 100644 (file)
@@ -39,3 +39,28 @@ function str2data(str) {
                if (_n2 == 2) _l[_c[1]] = _c[2]
        }
 }
+
+function bitcount(c) {
+       c=and(rshift(c, 1),0x55555555)+and(c,0x55555555)
+       c=and(rshift(c, 2),0x33333333)+and(c,0x33333333)
+       c=and(rshift(c, 4),0x0f0f0f0f)+and(c,0x0f0f0f0f)
+       c=and(rshift(c, 8),0x00ff00ff)+and(c,0x00ff00ff)
+       c=and(rshift(c,16),0x0000ffff)+and(c,0x0000ffff)
+       return c
+}
+
+function validate_netmask(nm) {
+       return and(-nm,compl(nm))
+}
+
+function ip2int(ip) {
+       for (ret=0,n=split(ip,a,"\."),x=1;x<=n;x++) ret=or(lshift(ret,8),a[x]) 
+       return ret
+}
+
+function int2ip(ip,ret) {
+       ret=and(ip,255)
+       ip=rshift(ip,8)
+       for(;ip;ret=and(ip,255)"."ret,ip=rshift(ip,8));
+       return ret
+}
index 996afb4473c5088b757eca06d6a532b0bbd9df29..d45948caa9a14e24160f1d5989f345f9fca795e9 100644 (file)
@@ -358,7 +358,7 @@ endif
 
 config BUSYBOX_CONFIG_IPCALC
        bool "ipcalc"
-       default y
+       default n
        help
          ipcalc takes an IP address and netmask and calculates the
          resulting broadcast, network, and host range.
index fc3837bd087153ab1fca916372f0b4a263da9ec8..a3f707d37b6d1ab877f1ef22f5221cd2fe577185 100755 (executable)
@@ -1,28 +1,33 @@
 #!/bin/sh
-. /etc/functions.sh
 
-# interface to use for DHCP
-iface=lan
+# The following is to automatically configure the DHCP settings
+# based on nvram settings. Feel free to replace all this crap
+# with a simple "dnsmasq" and manage everything via the
+# /etc/dnsmasq.conf config file
 
-ifname=$(nvram get ${iface}_ifname)
-ipaddr=$(nvram get ${iface}_ipaddr)
-netmask=$(nvram get ${iface}_netmask)
+# DHCP interface (lan, wan, wifi -- any ifup *)
+iface=lan
 
-# check for existing DHCP server
 udhcpc -n -q -R -s /bin/true -i $ifname >&- || {
+  # no existing DHCP server?
 
-  ipaddr=$(ip2int $ipaddr)
-  netmask=$(ip2int ${netmask:-255.255.255.0})
-  network=$((ipaddr&netmask))
-  
+  # calculate settings
+  ifname=$(nvram get ${iface}_ifname)
+  ipaddr=$(nvram get ${iface}_ipaddr)
+  netmask=$(nvram get ${iface}_netmask)
   start=$(nvram get dhcp_start)
   start=$((network+${start:-100}))
-  end=$(nvram get dhcp_num)
-  end=$((start+${end:-150}))
+  num=$(nvram get dhcp_num)
+  num=$((start+${num:-150}))
+  eval $(ipcalc $ipaddr $netmask $start $num)
   
-  wanproto=$(nvram get wan_proto)
-  [ -z "$wanproto" -o "$wanproto" = "none" ] || wanif=$(nvram get wan_ifname)
-  
-  args="-K -F $(int2ip $start),$(int2ip $end),$(int2ip $netmask),12h ${wanif:+-I ${wanif} }"
+  # and pass the args via the commandline
+  # (because trying to edit the config from here is crazy)
+  args="-K -F $START,$END,$NETMASK,12h"
 }
+
+# ignore requests from wan interface
+wanproto=$(nvram get wan_proto)
+[ -z "$wanproto" -o "$wanproto" = "none" ] || args="${args} -I $(nvram get wan_ifname)"
+
 dnsmasq ${args}