07ea4b8ffece3aadc19e4742e732ff089d728f43
[openwrt/svn-archive/archive.git] / net / ddns-scripts / files / usr / lib / ddns / dynamic_dns_functions.sh
1 # /usr/lib/dynamic_dns/dynamic_dns_functions.sh
2 #
3 # Written by Eric Paul Bishop, Janary 2008
4 # Distributed under the terms of the GNU General Public License (GPL) version 2.0
5 #
6 # This script is (loosely) based on the one posted by exobyte in the forums here:
7 # http://forum.openwrt.org/viewtopic.php?id=14040
8
9
10
11 . /etc/functions.sh
12 include /lib/network
13
14
15 #loads all options for a given package and section
16 #also, sets all_option_variables to a list of the variable names
17 load_all_config_options()
18 {
19 pkg_name="$1"
20 section_id="$2"
21
22 ALL_OPTION_VARIABLES=""
23 # this callback loads all the variables
24 # in the section_id section when we do
25 # config_load. We need to redefine
26 # the option_cb for different sections
27 # so that the active one isn't still active
28 # after we're done with it. For reference
29 # the $1 variable is the name of the option
30 # and $2 is the name of the section
31 config_cb()
32 {
33 if [ ."$2" = ."$section_id" ]; then
34 option_cb()
35 {
36 ALL_OPTION_VARIABLES="$ALL_OPTION_VARIABLES $1"
37 }
38 else
39 option_cb() { return 0; }
40 fi
41 }
42
43
44 config_load "$pkg_name"
45 for var in $ALL_OPTION_VARIABLES
46 do
47 config_get "$var" "$section_id" "$var"
48 done
49 }
50
51
52 get_current_ip()
53 {
54
55 #if ip source is not defined, assume we want to get ip from wan
56 if [ "$ip_source" != "interface" ] && [ "$ip_source" != "web" ]
57 then
58 ip_source="network"
59 ip_network="wan"
60 fi
61
62 if [ "$ip_source" = "network" ]
63 then
64 if [ -z "$ip_network" ]
65 then
66 ip_network="wan"
67 fi
68 scan_interfaces
69 config_load /var/state/network
70 config_get ip_interface $ip_network ifname
71 fi
72
73 current_ip='';
74 if [ "$ip_source" = "network" ] || [ "$ip_source" = "interface" ]
75 then
76 current_ip=$(ifconfig $ip_interface | grep -o 'inet addr:[0-9.]*' | grep -o "$ip_regex")
77 else
78 # get ip from web
79 # we check each url in order in ip_url variable, and if no ips are found we use dyndns ip checker
80 # ip is set to FIRST expression in page that matches the ip_regex regular expression
81 for addr in $ip_url
82 do
83 if [ -z "$current_ip" ]
84 then
85 current_ip=$(echo $( wget -O - $addr 2>/dev/null) | grep -o "$ip_regex")
86 fi
87 done
88
89 #here we hard-code the dyndns checkip url in case no url was specified
90 if [ -z "$current_ip" ]
91 then
92 current_ip=$(echo $( wget -O - http://checkip.dyndns.org 2>/dev/null) | grep -o "$ip_regex")
93 fi
94 fi
95
96 echo "$current_ip"
97 }
98
99
100 verbose_echo()
101 {
102 if [ "$verbose_mode" = 1 ]
103 then
104 echo $1
105 fi
106 }
107
108 start_daemon_for_all_ddns_sections()
109 {
110 SECTIONS=""
111 config_cb()
112 {
113 SECTIONS="$SECTIONS $2"
114 }
115 config_load "ddns"
116
117 for section in $SECTIONS
118 do
119 /usr/lib/ddns/dynamic_dns_updater.sh $section 0&
120 done
121 }