add first batch of enrik's ar7 system code cleanups
[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 do_ifup() {
28 if_proto=$(nvram get ${2}_proto)
29 if=$(nvram get ${2}_ifname)
30 [ "${if%%[0-9]}" = "ppp" ] && if=$(nvram get ${if_proto}_ifname)
31
32 pidfile=/var/run/${if}.pid
33 [ -f $pidfile ] && $DEBUG kill $(cat $pidfile)
34
35 case "$1" in
36 static)
37 ip=$(nvram get ${2}_ipaddr)
38 netmask=$(nvram get ${2}_netmask)
39 gateway=$(nvram get ${2}_gateway)
40
41 $DEBUG ifconfig $if $ip ${netmask:+netmask $netmask} broadcast + up
42 ${gateway:+$DEBUG route add default gw $gateway}
43
44 [ -f /etc/resolv.conf ] && return
45
46 debug "# --- creating /etc/resolv.conf ---"
47 for dns in $(nvram get ${2}_dns); do
48 echo "nameserver $dns" >> /etc/resolv.conf
49 done
50 ;;
51 dhcp)
52 DHCP_IP=$(nvram get ${2}_ipaddr)
53 DHCP_NETMASK=$(nvram get ${2}_netmask)
54 $DEBUG ifconfig $if $ip ${netmask:+netmask $netmask} broadcast + up
55
56 DHCP_ARGS="-i $if ${DHCP_IP:+-r $DHCP_IP} -b -p $pidfile"
57 DHCP_HOSTNAME=$(nvram get ${2}_hostname)
58 DHCP_HOSTNAME=${DHCP_HOSTNAME%%.*}
59 [ -z $DHCP_HOSTNAME ] || DHCP_ARGS="$DHCP_ARGS -H $DHCP_HOSTNAME"
60 [ "$if_proto" = "pptp" ] && DHCP_ARGS="$DHCP_ARGS -n -q" || DHCP_ARGS="$DHCP_ARGS -R &"
61 oldpid=$(cat $pidfile)
62 ${DEBUG:-eval} "udhcpc $DHCP_ARGS"
63 pidof udhcpc | grep "$oldpid" >&- 2>&- && {
64 sleep 1
65 kill -9 $oldpid
66 }
67 ;;
68 none|"")
69 ;;
70 *)
71 [ -x "/sbin/ifup.$1" ] && { $DEBUG /sbin/ifup.$1 ${2}; exit; }
72 echo "### ifup ${2}: ignored ${2}_proto=\"$1\" (not supported)"
73 ;;
74 esac
75 }
76
77 bitcount () {
78 local c=$1
79 echo $((
80 c=((c>> 1)&0x55555555)+(c&0x55555555),
81 c=((c>> 2)&0x33333333)+(c&0x33333333),
82 c=((c>> 4)&0x0f0f0f0f)+(c&0x0f0f0f0f),
83 c=((c>> 8)&0x00ff00ff)+(c&0x00ff00ff),
84 c=((c>>16)&0x0000ffff)+(c&0x0000ffff)
85 ))
86 }
87
88 valid_netmask () {
89 return $((-($1)&~$1))
90 }
91
92 ip2int () (
93 set $(echo $1 | tr '\.' ' ')
94 echo $(($1<<24|$2<<16|$3<<8|$4))
95 )
96
97 int2ip () {
98 echo $(($1>>24&255)).$(($1>>16&255)).$(($1>>8&255)).$(($1&255))
99 }
This page took 0.04532 seconds and 5 git commands to generate.