diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2009-11-09 09:39:50 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-11-09 09:39:50 +0100 |
commit | 391dd92ce218dea9a015e6b06f95403772876ac9 (patch) | |
tree | 073292e8e65ab85a0d19aea2b0f07671b8bfdd03 | |
parent | c3896850f748c4cff3e2a950f6b31f17c25ca141 (diff) | |
download | busybox-w32-391dd92ce218dea9a015e6b06f95403772876ac9.tar.gz busybox-w32-391dd92ce218dea9a015e6b06f95403772876ac9.tar.bz2 busybox-w32-391dd92ce218dea9a015e6b06f95403772876ac9.zip |
tweak dhcp service example - add ntp configuration
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rwxr-xr-x | examples/var_service/dhcp_if/convert2ipconf | 22 | ||||
-rwxr-xr-x | examples/var_service/dhcp_if/convert2ntpconf | 34 | ||||
-rwxr-xr-x | examples/var_service/dhcp_if/dhcp_handler | 81 |
3 files changed, 84 insertions, 53 deletions
diff --git a/examples/var_service/dhcp_if/convert2ipconf b/examples/var_service/dhcp_if/convert2ipconf index cee085463..114ab0555 100755 --- a/examples/var_service/dhcp_if/convert2ipconf +++ b/examples/var_service/dhcp_if/convert2ipconf | |||
@@ -19,24 +19,12 @@ | |||
19 | #let cfg=cfg+1 | 19 | #let cfg=cfg+1 |
20 | #if[$cfg]=...; ip[$cfg]=...; ipmask[$cfg]=.../...; gw[$cfg]=...; net[$cfg]=... dns[$cfg]=... | 20 | #if[$cfg]=...; ip[$cfg]=...; ipmask[$cfg]=.../...; gw[$cfg]=...; net[$cfg]=... dns[$cfg]=... |
21 | 21 | ||
22 | exec >"$0.out" 2>&1 | 22 | exec >/dev/null |
23 | #exec >"$0.out" # debug | ||
24 | exec 2>&1 | ||
23 | 25 | ||
24 | test "$interface" || exit 1 | 26 | test "$interface" || exit 1 |
25 | test -f "$1" || exit 1 | 27 | test "$ip" || exit 1 |
26 | |||
27 | # Unsafe, and does not handle values with spaces: | ||
28 | #. "./$1" || exit 1 | ||
29 | # Safe(r) parsing: | ||
30 | sq="'" | ||
31 | while read line; do | ||
32 | #echo "line: $line" | ||
33 | # Skip empty lines and lines with single quotes | ||
34 | test "${line##*$sq*}" || continue | ||
35 | var="${line%%=*}" | ||
36 | val="${line#*=}" | ||
37 | #echo "var:$var val:'$val'" | ||
38 | eval "$var='$val'" | ||
39 | done <"$1" | ||
40 | 28 | ||
41 | { | 29 | { |
42 | echo "let cfg=cfg+1" | 30 | echo "let cfg=cfg+1" |
@@ -50,4 +38,4 @@ test "$dns" && echo "dns[\$cfg]='$dns'" | |||
50 | # which subnet(s) is/are available thru advertised router | 38 | # which subnet(s) is/are available thru advertised router |
51 | # Assume 0/0 | 39 | # Assume 0/0 |
52 | echo "net[\$cfg]='0/0'" | 40 | echo "net[\$cfg]='0/0'" |
53 | } >"$2" | 41 | } >"$1" |
diff --git a/examples/var_service/dhcp_if/convert2ntpconf b/examples/var_service/dhcp_if/convert2ntpconf new file mode 100755 index 000000000..debf1ebfe --- /dev/null +++ b/examples/var_service/dhcp_if/convert2ntpconf | |||
@@ -0,0 +1,34 @@ | |||
1 | #!/bin/sh | ||
2 | # convert: | ||
3 | |||
4 | # dhcptype=5 | ||
5 | # serverid=172.16.42.102 | ||
6 | # lease=97200 | ||
7 | # interface=eth0 | ||
8 | # ip=172.16.42.177 | ||
9 | # subnet=255.255.255.0 | ||
10 | # mask=24 | ||
11 | # broadcast=172.16.22.255 | ||
12 | # router=172.16.42.98 | ||
13 | # dns=10.34.32.125 10.32.63.5 10.34.255.7 10.11.255.27 | ||
14 | # domain=lab.example.com example.com | ||
15 | # ntpsrv=10.34.32.125 10.34.255.7 | ||
16 | |||
17 | # into: | ||
18 | |||
19 | #let cfg=cfg+1 | ||
20 | #ntpip[$cfg]=... | ||
21 | |||
22 | exec >/dev/null | ||
23 | #exec >"$0.out" # debug | ||
24 | exec 2>&1 | ||
25 | |||
26 | test "$interface" || exit 1 | ||
27 | test "$ip" || exit 1 | ||
28 | |||
29 | { | ||
30 | for n in $ntpsrv; do | ||
31 | echo "let cfg=cfg+1" | ||
32 | echo "ntpip[\$cfg]='$n'"; | ||
33 | done | ||
34 | } >"$1" | ||
diff --git a/examples/var_service/dhcp_if/dhcp_handler b/examples/var_service/dhcp_if/dhcp_handler index 9ed3e7a3f..a119b4db1 100755 --- a/examples/var_service/dhcp_if/dhcp_handler +++ b/examples/var_service/dhcp_if/dhcp_handler | |||
@@ -3,56 +3,54 @@ | |||
3 | # parameters: $1 and environment | 3 | # parameters: $1 and environment |
4 | # | 4 | # |
5 | # $1 is: | 5 | # $1 is: |
6 | # | ||
7 | # deconfig: This argument is used when udhcpc starts, and | 6 | # deconfig: This argument is used when udhcpc starts, and |
8 | # when a lease is lost. The script should put the interface in an | 7 | # when a leases is lost. The script should put the interface in an |
9 | # up, but deconfigured state, ie: ifconfig $interface 0.0.0.0. | 8 | # up, but deconfigured state, ie: ifconfig $interface 0.0.0.0. |
10 | # Environment: interface=ethN | ||
11 | # | 9 | # |
12 | # bound: This argument is used when udhcpc moves from an | 10 | # bound: This argument is used when udhcpc moves from an |
13 | # unbound, to a bound state. All of the paramaters are set in | 11 | # unbound, to a bound state. All of the paramaters are set in |
14 | # enviromental variables, The script should configure the interface, | 12 | # enviromental variables, The script should configure the interface, |
15 | # and set any other relavent parameters (default gateway, dns server, etc). | 13 | # and set any other relavent parameters (default gateway, dns server, |
16 | # Environment: | 14 | # etc). |
17 | # dhcptype=5 | ||
18 | # serverid=172.16.42.102 | ||
19 | # lease=97200 | ||
20 | # interface=eth0 | ||
21 | # ip=172.16.42.177 | ||
22 | # subnet=255.255.255.0 | ||
23 | # mask=24 | ||
24 | # broadcast=172.16.22.255 | ||
25 | # router=172.16.42.98 | ||
26 | # dns=10.34.32.125 10.32.63.5 10.34.255.7 10.11.255.27 | ||
27 | # domain=lab.example.com example.com | ||
28 | # ntpsrv=10.34.32.125 10.34.255.7 | ||
29 | # | 15 | # |
30 | # renew: This argument is used when a DHCP lease is renewed. All of | 16 | # renew: This argument is used when a DHCP lease is renewed. All of |
31 | # the paramaters are set in enviromental variables. This argument is | 17 | # the paramaters are set in enviromental variables. This argument is |
32 | # used when the interface is already configured, so the IP address, | 18 | # used when the interface is already configured, so the IP address, |
33 | # will not change, however, the other DHCP paramaters, such as the | 19 | # will not change, however, the other DHCP paramaters, such as the |
34 | # default gateway, subnet mask, and dns server may change. | 20 | # default gateway, subnet mask, and dns server may change. |
35 | # Environment: same as for "bound". | ||
36 | # | 21 | # |
37 | # nak: This argument is used with udhcpc receives a NAK message. | 22 | # nak: This argument is used with udhcpc receives a NAK message. |
38 | # The script with the deconfig argument will be called directly | 23 | # The script with the deconfig argument will be called directly |
39 | # afterwards, so no changes to the network interface are neccessary. | 24 | # afterwards, so no changes to the network interface are neccessary. |
40 | # This hook is provided for purely informational purposes (the | 25 | # This hook is provided for purely informational purposes (the |
41 | # message option may contain a reason for the NAK). | 26 | # message option may contain a reason for the NAK). |
42 | # Environment: interface=ethN, serverid=IP_ADDR | ||
43 | # | 27 | # |
44 | # leasefail: called when lease cannot be obtained | 28 | # env shows something like: |
45 | # (for example, when DHCP server is down). | 29 | # dhcptype=5 |
46 | # Environment: interface=ethN | 30 | # serverid=172.16.42.102 |
31 | # lease=97200 | ||
32 | # interface=eth0 | ||
33 | # ip=172.16.42.177 | ||
34 | # subnet=255.255.255.0 | ||
35 | # mask=24 | ||
36 | # broadcast=172.16.22.255 | ||
37 | # router=172.16.42.98 | ||
38 | # dns=10.34.32.125 10.32.63.5 10.34.255.7 10.11.255.27 | ||
39 | # domain=lab.example.com example.com | ||
40 | # ntpsrv=10.34.32.125 10.34.255.7 | ||
47 | 41 | ||
48 | # TODO: put domain into /etc/resolv.conf (thru /var/service/fw) | 42 | # TODO: put domain into /etc/resolv.conf (thru /var/service/fw) |
49 | # TODO: feed ntp IPs to /var/service/ntp | 43 | # TODO: feed ntp IPs to /var/service/ntp |
50 | 44 | ||
51 | service=`basename $PWD` | 45 | service=${PWD##*/} |
52 | outfile="$service.ipconf" | 46 | file_ipconf="$service.ipconf" |
53 | dir="/var/run/service/fw" | 47 | file_ntpconf="$service.ntpconf" |
48 | dir_ipconf="/var/run/service/fw" | ||
49 | dir_ntpconf="/var/run/service/ntp" | ||
54 | 50 | ||
55 | exec >>"$0.out" 2>&1 | 51 | exec >/dev/null |
52 | #exec >>"$0.out" #debug | ||
53 | exec 2>&1 | ||
56 | 54 | ||
57 | echo "`date`: Params: $*" | 55 | echo "`date`: Params: $*" |
58 | 56 | ||
@@ -60,23 +58,34 @@ if test x"$1" != x"bound" && test x"$1" != x"renew" ; then | |||
60 | # Reconfigure network with this interface disabled | 58 | # Reconfigure network with this interface disabled |
61 | echo "Deconfiguring" | 59 | echo "Deconfiguring" |
62 | rm "$service.out" | 60 | rm "$service.out" |
63 | rm "$outfile" | 61 | rm "$file_ipconf" |
64 | rm "$dir/$outfile" | 62 | rm "$file_ntpconf" |
63 | rm "$dir_ipconf/$file_ipconf" | ||
64 | rm "$dir_ntpconf/$file_ntpconf" | ||
65 | sv u /var/service/fw | 65 | sv u /var/service/fw |
66 | exit | 66 | exit |
67 | fi | 67 | fi |
68 | 68 | ||
69 | # Bound: we've got the lease | 69 | # Bound: we've got the lease |
70 | #env >"$service.out" # debug | ||
70 | 71 | ||
71 | # Process params | 72 | ./convert2ipconf "$file_ipconf" |
72 | env >"$service.out" | ||
73 | ./convert2ipconf "$service.out" "$outfile" | ||
74 | |||
75 | # Reconfigure routing and firewall if needed | 73 | # Reconfigure routing and firewall if needed |
76 | diff --brief "$outfile" "$dir/$outfile" >/dev/null 2>&1 | 74 | diff --brief "$file_ipconf" "$dir_ipconf/$file_ipconf" >/dev/null 2>&1 |
77 | if test "$?" != "0"; then | 75 | if test "$?" != "0"; then |
78 | echo "Reconfiguring" | 76 | echo "Reconfiguring fw" |
79 | mkdir -p "$dir" 2>/dev/null | 77 | mkdir -p "$dir_ipconf" 2>/dev/null |
80 | cp "$outfile" "$dir/$outfile" | 78 | cp "$file_ipconf" "$dir_ipconf/$file_ipconf" |
81 | sv u /var/service/fw | 79 | sv u /var/service/fw |
82 | fi | 80 | fi |
81 | |||
82 | ./convert2ntpconf "$file_ntpconf" | ||
83 | # Reconfigure ntp server addresses if needed | ||
84 | diff --brief "$file_ntpconf" "$dir_ntpconf/$file_ntpconf" >/dev/null 2>&1 | ||
85 | if test "$?" != "0"; then | ||
86 | echo "Reconfiguring ntp" | ||
87 | mkdir -p "$dir_ntpconf" 2>/dev/null | ||
88 | cp "$file_ntpconf" "$dir_ntpconf/$file_ntpconf" | ||
89 | sv t /var/service/ntp | ||
90 | sv u /var/service/ntp | ||
91 | fi | ||