c813e39a6171b103c873c7ff8b90d6dd57c67c7b
[openwrt.git] / openwrt / package / base-files / default / etc / functions.sh
1 #!/bin/ash
2 . /etc/nvram.sh
3
4 alias debug=${DEBUG:-:}
5
6 # valid interface?
7 if_valid () (
8 ifconfig "$1" >&- 2>&- ||
9 [ "${1%%[0-9]}" = "br" ] ||
10 {
11 [ "${1%%[0-9]}" = "vlan" ] && (
12 i=${1#vlan}
13 hwname=$(nvram get vlan${i}hwname)
14 hwaddr=$(nvram get ${hwname}macaddr)
15 [ -z "$hwaddr" ] && return 1
16
17 vif=$(ifconfig -a | awk '/^eth.*'$hwaddr'/ {print $1; exit}' IGNORECASE=1)
18 debug "# vlan$i => $vif"
19
20 $DEBUG ifconfig $vif up
21 $DEBUG vconfig add $vif $i 2>&-
22 )
23 } ||
24 { debug "# missing interface '$1' ignored"; false; }
25 )
26
27 bitcount () {
28 local c=$1
29 echo $((
30 c=((c>> 1)&0x55555555)+(c&0x55555555),
31 c=((c>> 2)&0x33333333)+(c&0x33333333),
32 c=((c>> 4)&0x0f0f0f0f)+(c&0x0f0f0f0f),
33 c=((c>> 8)&0x00ff00ff)+(c&0x00ff00ff),
34 c=((c>>16)&0x0000ffff)+(c&0x0000ffff)
35 ))
36 }
37
38 valid_netmask () {
39 return $((-($1)&~$1))
40 }
41
42 ip2int () (
43 set $(echo $1 | tr '\.' ' ')
44 echo $(($1<<24|$2<<16|$3<<8|$4))
45 )
46
47 int2ip () {
48 echo $(($1>>24&255)).$(($1>>16&255)).$(($1>>8&255)).$(($1&255))
49 }
This page took 0.039642 seconds and 3 git commands to generate.