aboutsummaryrefslogtreecommitdiff
path: root/examples/var_service/dhcp_if/dhcp_handler
blob: 51740406b560d5daea54d278c356d9dd6e89520a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/sh
# executed by udhcpc
# parameters: $1 and environment
# $1 is:
#
# deconfig: udhcpc starts, or lease is lost.
# Environment example: interface=eth0
#
# bound: lease is obtained. Environment example:
# dhcptype=5
# serverid=172.16.42.102
# lease=97200
# interface=eth0
# ip=172.16.42.177
# subnet=255.255.255.0
# mask=24
# broadcast=172.16.22.255
# router=172.16.42.98
# dns=10.34.32.125 10.32.63.5 10.34.255.7 10.11.255.27
# domain=lab.example.com example.com
# ntpsrv=10.34.32.125 10.34.255.7
#
# renew: lease is renewed. Environment is similar to "bound".
# The IP address does not not change, however, the other DHCP paramaters,
# such as the default gateway, subnet mask, and dns server may change.
#
# nak: udhcpc received a NAK message.
# Environment example: interface=eth0
#
# leasefail: udhcpc cannot obtain a lease (DHCP server not responding, etc).
# Environment example: interface=eth0

# TODO: put $domain into /etc/resolv.conf (thru /var/service/fw)

service=${PWD##*/}
file_ipconf="$service.ipconf"
file_ntpconf="$service.ntpconf"
dir_ipconf="/var/run/service/fw"
dir_ntpconf="/var/run/service/ntp"

exec >/dev/null
#exec >>"$0.out"  #debug
exec 2>&1

echo "`date`: Params: $*"

if test x"$1" != x"bound" && test x"$1" != x"renew" ; then
	# Reconfigure network with this interface disabled
	echo "Deconfiguring"
	rm "$service.out"
	rm "$file_ipconf"
	rm "$file_ntpconf"
	rm "$dir_ipconf/$file_ipconf"
	rm "$dir_ntpconf/$file_ntpconf"
	sv u /var/service/fw
	exit
fi

# Bound: we've got the lease
#env >"$service.out"  # debug

./convert2ipconf "$file_ipconf"
# Reconfigure routing and firewall if needed
diff --brief "$file_ipconf" "$dir_ipconf/$file_ipconf" >/dev/null 2>&1
if test $? != 0; then
	echo "Reconfiguring fw"
	mkdir -p "$dir_ipconf" 2>/dev/null
	cp "$file_ipconf" "$dir_ipconf/$file_ipconf"
	sv u /var/service/fw
fi

if test -d /var/service/ntp; then
	./convert2ntpconf "$file_ntpconf"
	# Reconfigure ntp server addresses if needed
	diff --brief "$file_ntpconf" "$dir_ntpconf/$file_ntpconf" >/dev/null 2>&1
	if test $? != 0; then
		echo "Reconfiguring ntp"
		mkdir -p "$dir_ntpconf" 2>/dev/null
		cp "$file_ntpconf" "$dir_ntpconf/$file_ntpconf"
		sv t /var/service/ntp
		sv u /var/service/ntp
	fi
fi