From 42878664e8bebc8e8cf03c7aacbff281f811199d Mon Sep 17 00:00:00 2001 From: Mike Baker Date: Tue, 21 Mar 2006 11:10:03 +0000 Subject: [PATCH] modify dnsmasq script to perform better calculations with new ipcalc script SVN-Revision: 3426 --- openwrt/package/base-files/default/bin/ipcalc | 31 +++++++++++++++ .../base-files/default/etc/functions.sh | 24 ------------ .../base-files/default/usr/lib/common.awk | 25 ++++++++++++ .../busybox/config/networking/Config.in | 2 +- openwrt/package/dnsmasq/files/S50dnsmasq | 39 +++++++++++-------- 5 files changed, 79 insertions(+), 42 deletions(-) create mode 100755 openwrt/package/base-files/default/bin/ipcalc diff --git a/openwrt/package/base-files/default/bin/ipcalc b/openwrt/package/base-files/default/bin/ipcalc new file mode 100755 index 0000000000..8d12545b65 --- /dev/null +++ b/openwrt/package/base-files/default/bin/ipcalc @@ -0,0 +1,31 @@ +#!/bin/sh + +awk -f /usr/lib/common.awk -f - $* <limit) end=limit + + print "NETMASK="int2ip(netmask) + print "BROADCAST="int2ip(broadcast) + print "NETWORK="int2ip(network) + print "PREFIX="bitcount(int2ip(netmask)) + + # range calculations: + # ipcalc + + if (ARGC > 3) { + print "START="int2ip(start) + print "END="int2ip(end) + } +} +EOF diff --git a/openwrt/package/base-files/default/etc/functions.sh b/openwrt/package/base-files/default/etc/functions.sh index 7c31539721..80b1792c86 100755 --- a/openwrt/package/base-files/default/etc/functions.sh +++ b/openwrt/package/base-files/default/etc/functions.sh @@ -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)) -} diff --git a/openwrt/package/base-files/default/usr/lib/common.awk b/openwrt/package/base-files/default/usr/lib/common.awk index 11454cec19..250a70f641 100644 --- a/openwrt/package/base-files/default/usr/lib/common.awk +++ b/openwrt/package/base-files/default/usr/lib/common.awk @@ -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 +} diff --git a/openwrt/package/busybox/config/networking/Config.in b/openwrt/package/busybox/config/networking/Config.in index 996afb4473..d45948caa9 100644 --- a/openwrt/package/busybox/config/networking/Config.in +++ b/openwrt/package/busybox/config/networking/Config.in @@ -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. diff --git a/openwrt/package/dnsmasq/files/S50dnsmasq b/openwrt/package/dnsmasq/files/S50dnsmasq index fc3837bd08..a3f707d37b 100755 --- a/openwrt/package/dnsmasq/files/S50dnsmasq +++ b/openwrt/package/dnsmasq/files/S50dnsmasq @@ -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} -- 2.30.2