--- /dev/null
+#!/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
;;
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))
-}
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
+}
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.
#!/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}