forked from sargon/gluon-sargon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathddhcpd-gateway-update
executable file
·85 lines (70 loc) · 1.95 KB
/
ddhcpd-gateway-update
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
84
85
#!/bin/sh
CACHE_TTL=600
CACHE_FILE=/tmp/ddhcp_gw_cache
GW_UPDATE_TRIES=5
verbose() {
[ -n "$VERBOSE" ] && ( >&2 echo $@ )
}
mac_to_ipv6_ll() {
IFS=':'; set $1; unset IFS
echo "fe80::$(printf %02x $((0x$1 ^ 2)))$2:${3}ff:fe$4:$5$6"
}
get_gw_ipv4_address() {
local gwmac="$1"
local gwid="`echo "$gwmac" | tr -d ':'`"
gluon-neighbour-info -p 1001 -d "`mac_to_ipv6_ll "$gwmac"`" -i br-client -r gateway | \
while read resp; do
local node_id="`echo "$resp" | jsonfilter -e '$.node_id'`"
if [ "$node_id" = "$gwid" ]; then
echo "$resp" | jsonfilter -e '$.address.ipv4'
fi
done
}
gateway_lost() {
verbose Gateway lost, removing dhcp options
rm -f "$CACHE_FILE"
/usr/sbin/ddhcpdctl -r 3
/usr/sbin/ddhcpdctl -r 6
}
update_gateway() {
verbose Updating dhcp options
/usr/sbin/ddhcpdctl -o "3:4:$1"
/usr/sbin/ddhcpdctl -o "6:4:$1"
}
gwmac="`batctl gwl | grep '=>' | cut -d' ' -f2`"
[ -z "$gwmac" ] && {
verbose Failed to determine mac address of current gateway
gateway_lost
exit 1
}
verbose Got gateway mac address: "$gwmac"
[ -f "$CACHE_FILE" ] && {
verbose Retrieving cache
source "$CACHE_FILE"
now="`date +'%s'`"
if echo "$last_update_time" | egrep -q '^[0-9]+$' && \
echo "$last_gwaddr" | egrep -q '^(([0-9]){1,3}\.){3}[0-9]{1,3}$'; then
verbose Gateway mac address cache found, age $(($now - $last_update_time))
if [ "$gwmac" = "$last_gwmac" ] && \
[ $(($now - $last_update_time)) -lt "$CACHE_TTL" ]; then
verbose Cache is up to date
update_gateway "$last_gwaddr"
exit 0
fi
fi
verbose Updating cache
}
for _ in seq "$GW_UPDATE_TRIES"; do
gwaddr="`get_gw_ipv4_address "$gwmac"`"
[ -n "$gwaddr" ] && break
done
[ -z "$gwaddr" ] && {
verbose Failed to retrieve gateway IPv4 address
gateway_lost
exit 2
}
verbose Got gateway IPv4 address: "$gwaddr"
echo "last_gwmac=\"$gwmac\"" > "$CACHE_FILE"
echo "last_gwaddr=\"$gwaddr\"" >> "$CACHE_FILE"
echo "last_update_time=`date +'%s'`" >> "$CACHE_FILE"
update_gateway "$gwaddr"