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

ffgraz-ddhcpd-batman-adv: fix shellcheck issues #61

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 37 additions & 28 deletions ffgraz-ddhcpd-batman-adv/files/usr/sbin/ddhcpd-gateway-update
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh
#!/bin/busybox sh
# shellcheck shell=dash

CACHE_TTL=600
CACHE_FILE=/tmp/ddhcp_gw_cache
Expand All @@ -7,20 +8,22 @@ GW_UPDATE_TRIES=5


verbose() {
[ -n "$VERBOSE" ] && ( >&2 echo $@ )
[ -n "$VERBOSE" ] && ( >&2 echo "$@" )
}

mac_to_ipv6_ll() {
IFS=':'; set $1; unset IFS
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'`"
local gwid
gwid="$(echo "$gwmac" | tr -d ':')"
gluon-neighbour-info -p 1001 -d "$(mac_to_ipv6_ll "$gwmac")" -i br-client -r gateway | \
while read -r resp; do
local node_id
node_id="$(echo "$resp" | jsonfilter -e '$.node_id')"
if [ "$node_id" = "$gwid" ]; then
echo "$resp" | jsonfilter -e '$.address.ipv4'
fi
Expand All @@ -40,49 +43,55 @@ update_gateway() {
/usr/sbin/ddhcpdctl -o "6:4:$1"
}

gwmac="`batctl gwl | sed 's/([[:space:]]*[0-9]\+)//g' | sed 's/[[:space:]]\+/ /g' | egrep '(^\*)|(=>)' | cut -d' ' -f2`"
[ -z "$gwmac" ] && {
gwmac="$(batctl gwl | sed 's/([[:space:]]*[0-9]\+)//g' | sed 's/[[:space:]]\+/ /g' | grep -E '(^\*)|(=>)' | cut -d' ' -f2)"
if [ -z "$gwmac" ]; then
verbose Failed to determine mac address of current gateway
gateway_lost
exit 1
}
fi

verbose Got gateway mac address: "$gwmac"

[ -f "$CACHE_FILE" ] && {
# disable checks for unknown variables (SC2154) because they are loaded from an unknown files (SC1090)
# shellcheck disable=SC1090,SC2154
if [ -f "$CACHE_FILE" ]; then
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
. "$CACHE_FILE"
now="$(date +'%s')"
if echo "$last_update_time" | grep -E -q '^[0-9]+$' && \
echo "$last_gwaddr" | grep -E -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
}
fi

for _ in `seq "$GW_UPDATE_TRIES"`; do
gwaddr="`get_gw_ipv4_address "$gwmac"`"
for _ in $(seq "$GW_UPDATE_TRIES"); do
gwaddr="$(get_gw_ipv4_address "$gwmac")"
[ -n "$gwaddr" ] && break
done
[ -z "$gwaddr" ] && [ ! "$gwmac" == "$last_gwmac" ] && {

if [ -z "$gwaddr" ] && [ ! "$gwmac" = "$last_gwmac" ]; then
verbose Failed to retrieve new gateway IPv4 address
gateway_lost
exit 2
}
[ ! -z "$gwaddr" ] && {
fi

if [ -z "$gwaddr" ]; then
verbose "Information gathering failed, done nothing"
exit 0
fi

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"
echo "last_update_time=$(date +'%s')" >> "$CACHE_FILE"

update_gateway "$gwaddr"
} || {
verbose Information gathering failed, done nothing
}