49667a815e1caa1a9aaf5b91b4eb8cbedb91880f
[openwrt-10.03/.git] / package / 6in4 / files / 6in4.hotplug
1 #!/bin/sh
2
3 if [ "$ACTION" = ifup ]; then
4         . /etc/functions.sh
5
6         include /lib/network
7         scan_interfaces
8
9         update_tunnel() {
10                 local cfg="$1"
11
12                 local proto
13                 config_get proto "$cfg" proto
14                 [ "$proto" = 6in4 ] || return 0
15
16                 local wandev
17                 config_get wandev "$cfg" wan_device "$(find_6in4_wanif)"
18                 [ "$wandev" = "$DEVICE" ] || return 0
19
20                 local wanip=$(find_6in4_wanip "$wandev")
21
22                 [ -n "$wanip" ] && {
23                         local tunnelid
24                         config_get tunnelid "$cfg" tunnelid
25
26                         local username
27                         config_get username "$cfg" username
28
29                         local password
30                         config_get password "$cfg" password
31
32                         uci_set_state network "$cfg" ipaddr "$wanip"
33
34                         [ -n "$tunnelid" ] && [ -n "$username" ] && [ -n "$password" ] && {
35                                 [ "${#password}" == 32 -a -z "${password//[a-f0-9]/}" ] || {
36                                         password="$(echo -n "$password" | md5sum)"; password="${password%% *}"
37                                 }
38
39                                 (
40                                         local url="http://ipv4.tunnelbroker.net/ipv4_end.php?ipv4b=AUTO&user_id=$username&pass=$password&tunnel_id=$tunnelid"
41                                         local try=0
42                                         local max=3
43
44                                         while [ $((++try)) -le $max ]; do
45                                                 wget -qO/dev/null "$url" 2>/dev/null && {
46                                                         logger -t 6in4-update "Updated tunnel #$tunnelid endpoint to $wanip"
47                                                         ifup "$cfg"
48                                                         break
49                                                 } || {
50                                                         logger -t 6in4-update "Try $try/$max failed, retrying"
51                                                         sleep 1
52                                                 }
53                                         done
54                                 )&
55                         } || {
56                                 logger -t 6in4-update "Re-establishing tunnel due to change on $INTERFACE ($DEVICE)"
57                                 ifup "$cfg" &
58                         }
59                 }
60         }
61
62         config_foreach update_tunnel interface
63 fi