Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve IPv4 address of gateway via respondd #13

Merged
merged 1 commit into from Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions ddhcpd/Makefile
Expand Up @@ -2,6 +2,7 @@ include $(TOPDIR)/rules.mk

PKG_NAME:=ddhcpd
PKG_VERSION:=2018-06-28
PKG_RELEASE:=1

PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://github.com/sargon/ddhcpd
Expand Down
88 changes: 79 additions & 9 deletions ddhcpd/files/usr/sbin/ddhcpd-gateway-update
@@ -1,15 +1,85 @@
#!/bin/sh

GW="$(batctl gwl | grep '=>' | cut -f 2 -d ' ')"
GW_MAC="$(batctl tg | grep "$GW" | cut -d ' ' -f 3)"
CACHE_TTL=600
CACHE_FILE=/tmp/ddhcp_gw_cache

COUNT=0
for gw_mac in "$GW_MAC" ; do
GW_ADDR=$(batctl dc | grep "$gw_mac" | sed -e 's/^[^0-9]*\([0-9]\+\)/\1/' | cut -d ' ' -f 1)
COUNT=$(expr $COUNT + 1)
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"

[[ "$COUNT" -eq 1 ]] || exit 1
echo "last_gwmac=\"$gwmac\"" > "$CACHE_FILE"
echo "last_gwaddr=\"$gwaddr\"" >> "$CACHE_FILE"
echo "last_update_time=`date +'%s'`" >> "$CACHE_FILE"

/usr/sbin/ddhcpdctl -o "3:4:${GW_ADDR}"
/usr/sbin/ddhcpdctl -o "6:4:${GW_ADDR}"
update_gateway "$gwaddr"