ddns-scripts: replace IP type (name) "local" with "current"

Local suggests something related to the local network or available
locally only. All that code related to the "local" IP was actually
dealing with *current* device external IP address. Using name "current"
should make code a bit easier to understand.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
This commit is contained in:
Rafał Miłecki 2022-09-27 17:42:58 +02:00
parent e3292e4c97
commit a6247d81a9
3 changed files with 40 additions and 40 deletions

View file

@ -47,8 +47,8 @@ CURR_TIME=0 # holds the current uptime
NEXT_TIME=0 # calculated time for next FORCED update NEXT_TIME=0 # calculated time for next FORCED update
EPOCH_TIME=0 # seconds since 1.1.1970 00:00:00 EPOCH_TIME=0 # seconds since 1.1.1970 00:00:00
CURRENT_IP="" # holds the current IP read from the box
REGISTERED_IP="" # holds the IP read from DNS REGISTERED_IP="" # holds the IP read from DNS
LOCAL_IP="" # holds the local IP read from the box
URL_USER="" # url encoded $username from config file URL_USER="" # url encoded $username from config file
URL_PASS="" # url encoded $password from config file URL_PASS="" # url encoded $password from config file
@ -57,7 +57,7 @@ URL_PENC="" # url encoded $param_enc from config file
UPD_ANSWER="" # Answer given by service on success UPD_ANSWER="" # Answer given by service on success
ERR_LAST=0 # used to save $? return code of program and function calls ERR_LAST=0 # used to save $? return code of program and function calls
ERR_UPDATE=0 # error counter on different local and registered ip ERR_UPDATE=0 # error counter on different current and registered IPs
PID_SLEEP=0 # ProcessID of current background "sleep" PID_SLEEP=0 # ProcessID of current background "sleep"
@ -722,7 +722,7 @@ do_transfer() {
# set correct program to detect IP # set correct program to detect IP
[ $use_ipv6 -eq 0 ] && __RUNPROG="network_get_ipaddr" || __RUNPROG="network_get_ipaddr6" [ $use_ipv6 -eq 0 ] && __RUNPROG="network_get_ipaddr" || __RUNPROG="network_get_ipaddr6"
eval "$__RUNPROG __BINDIP $bind_network" || \ eval "$__RUNPROG __BINDIP $bind_network" || \
write_log 13 "Can not detect local IP using '$__RUNPROG $bind_network' - Error: '$?'" write_log 13 "Can not detect current IP using '$__RUNPROG $bind_network' - Error: '$?'"
write_log 7 "Force communication via IP '$__BINDIP'" write_log 7 "Force communication via IP '$__BINDIP'"
__PROG="$__PROG --bind-address=$__BINDIP" __PROG="$__PROG --bind-address=$__BINDIP"
fi fi
@ -923,13 +923,13 @@ send_update() {
fi fi
} }
get_local_ip () { get_current_ip () {
# $1 Name of Variable to store local IP (LOCAL_IP) # $1 Name of Variable to store current IP
local __CNT=0 # error counter local __CNT=0 # error counter
local __RUNPROG __DATA __URL __ERR local __RUNPROG __DATA __URL __ERR
[ $# -ne 1 ] && write_log 12 "Error calling 'get_local_ip()' - wrong number of parameters" [ $# -ne 1 ] && write_log 12 "Error calling 'get_current_ip()' - wrong number of parameters"
write_log 7 "Detect local IP on '$ip_source'" write_log 7 "Detect current IP on '$ip_source'"
while : ; do while : ; do
if [ -n "$ip_network" -a "$ip_source" = "network" ]; then if [ -n "$ip_network" -a "$ip_source" = "network" ]; then
@ -938,8 +938,8 @@ get_local_ip () {
[ $use_ipv6 -eq 0 ] && __RUNPROG="network_get_ipaddr" \ [ $use_ipv6 -eq 0 ] && __RUNPROG="network_get_ipaddr" \
|| __RUNPROG="network_get_ipaddr6" || __RUNPROG="network_get_ipaddr6"
eval "$__RUNPROG __DATA $ip_network" || \ eval "$__RUNPROG __DATA $ip_network" || \
write_log 13 "Can not detect local IP using $__RUNPROG '$ip_network' - Error: '$?'" write_log 13 "Can not detect current IP using $__RUNPROG '$ip_network' - Error: '$?'"
[ -n "$__DATA" ] && write_log 7 "Local IP '$__DATA' detected on network '$ip_network'" [ -n "$__DATA" ] && write_log 7 "Current IP '$__DATA' detected on network '$ip_network'"
elif [ -n "$ip_interface" -a "$ip_source" = "interface" ]; then elif [ -n "$ip_interface" -a "$ip_source" = "interface" ]; then
local __DATA4=""; local __DATA6="" local __DATA4=""; local __DATA6=""
if [ -n "$(command -v ip)" ]; then # ip program installed if [ -n "$(command -v ip)" ]; then # ip program installed
@ -1018,14 +1018,14 @@ get_local_ip () {
fi fi
fi fi
[ $use_ipv6 -eq 0 ] && __DATA="$__DATA4" || __DATA="$__DATA6" [ $use_ipv6 -eq 0 ] && __DATA="$__DATA4" || __DATA="$__DATA6"
[ -n "$__DATA" ] && write_log 7 "Local IP '$__DATA' detected on interface '$ip_interface'" [ -n "$__DATA" ] && write_log 7 "Current IP '$__DATA' detected on interface '$ip_interface'"
elif [ -n "$ip_script" -a "$ip_source" = "script" ]; then elif [ -n "$ip_script" -a "$ip_source" = "script" ]; then
write_log 7 "#> $ip_script >$DATFILE 2>$ERRFILE" write_log 7 "#> $ip_script >$DATFILE 2>$ERRFILE"
eval $ip_script >$DATFILE 2>$ERRFILE eval $ip_script >$DATFILE 2>$ERRFILE
__ERR=$? __ERR=$?
if [ $__ERR -eq 0 ]; then if [ $__ERR -eq 0 ]; then
__DATA=$(cat $DATFILE) __DATA=$(cat $DATFILE)
[ -n "$__DATA" ] && write_log 7 "Local IP '$__DATA' detected via script '$ip_script'" [ -n "$__DATA" ] && write_log 7 "Current IP '$__DATA' detected via script '$ip_script'"
else else
write_log 3 "$ip_script Error: '$__ERR'" write_log 3 "$ip_script Error: '$__ERR'"
write_log 7 "$(cat $ERRFILE)" # report error write_log 7 "$(cat $ERRFILE)" # report error
@ -1036,9 +1036,9 @@ get_local_ip () {
[ $use_ipv6 -eq 0 ] \ [ $use_ipv6 -eq 0 ] \
&& __DATA=$(grep -m 1 -o "$IPV4_REGEX" $DATFILE) \ && __DATA=$(grep -m 1 -o "$IPV4_REGEX" $DATFILE) \
|| __DATA=$(grep -m 1 -o "$IPV6_REGEX" $DATFILE) || __DATA=$(grep -m 1 -o "$IPV6_REGEX" $DATFILE)
[ -n "$__DATA" ] && write_log 7 "Local IP '$__DATA' detected on web at '$ip_url'" [ -n "$__DATA" ] && write_log 7 "Current IP '$__DATA' detected on web at '$ip_url'"
else else
write_log 12 "Error in 'get_local_ip()' - unhandled ip_source '$ip_source'" write_log 12 "Error in 'get_current_ip()' - unhandled ip_source '$ip_source'"
fi fi
# valid data found return here # valid data found return here
[ -n "$__DATA" ] && { [ -n "$__DATA" ] && {
@ -1053,22 +1053,22 @@ get_local_ip () {
[ $VERBOSE -gt 1 ] && { [ $VERBOSE -gt 1 ] && {
# VERBOSE > 1 then NO retry # VERBOSE > 1 then NO retry
write_log 4 "Get local IP via '$ip_source' failed - Verbose Mode: $VERBOSE - NO retry on error" write_log 4 "Get current IP via '$ip_source' failed - Verbose Mode: $VERBOSE - NO retry on error"
return 1 return 1
} }
__CNT=$(( $__CNT + 1 )) # increment error counter __CNT=$(( $__CNT + 1 )) # increment error counter
# if error count > retry_max_count leave here # if error count > retry_max_count leave here
[ $retry_max_count -gt 0 -a $__CNT -gt $retry_max_count ] && \ [ $retry_max_count -gt 0 -a $__CNT -gt $retry_max_count ] && \
write_log 14 "Get local IP via '$ip_source' failed after $retry_max_count retries" write_log 14 "Get current IP via '$ip_source' failed after $retry_max_count retries"
write_log 4 "Get local IP via '$ip_source' failed - retry $__CNT/$retry_max_count in $RETRY_SECONDS seconds" write_log 4 "Get current IP via '$ip_source' failed - retry $__CNT/$retry_max_count in $RETRY_SECONDS seconds"
sleep $RETRY_SECONDS & sleep $RETRY_SECONDS &
PID_SLEEP=$! PID_SLEEP=$!
wait $PID_SLEEP # enable trap-handler wait $PID_SLEEP # enable trap-handler
PID_SLEEP=0 PID_SLEEP=0
done done
# we should never come here there must be a programming error # we should never come here there must be a programming error
write_log 12 "Error in 'get_local_ip()' - program coding error" write_log 12 "Error in 'get_current_ip()' - program coding error"
} }
get_registered_ip() { get_registered_ip() {

View file

@ -137,11 +137,11 @@ case "$1" in
if [ "$ip_source" = "web" -o "$ip_source" = "script" ]; then if [ "$ip_source" = "web" -o "$ip_source" = "script" ]; then
# we wait only 3 seconds for an # we wait only 3 seconds for an
# answer from "web" or "script" # answer from "web" or "script"
write_log 7 "-----> timeout 3 -- get_local_ip IP" write_log 7 "-----> timeout 3 -- get_current_ip IP"
timeout 3 -- get_local_ip IP timeout 3 -- get_current_ip IP
else else
write_log 7 "-----> get_local_ip IP" write_log 7 "-----> get_current_ip IP"
get_local_ip IP get_current_ip IP
fi fi
__RET=$? __RET=$?
;; ;;

View file

@ -145,10 +145,10 @@ trap "trap_handler 15" 15 # SIGTERM Termination
# #
# use_syslog log activity to syslog # use_syslog log activity to syslog
# #
# ip_source source to detect current local IP ('network' or 'web' or 'script' or 'interface') # ip_source source to detect current IP ('network' or 'web' or 'script' or 'interface')
# ip_network local defined network to read IP from i.e. 'wan' or 'wan6' # ip_network local defined network to read IP from i.e. 'wan' or 'wan6'
# ip_url URL to read local address from i.e. http://checkip.dyndns.com/ or http://checkipv6.dyndns.com/ # ip_url URL to read current IP from i.e. http://checkip.dyndns.com/ or http://checkipv6.dyndns.com/
# ip_script full path and name of your script to detect local IP # ip_script full path and name of your script to detect current IP
# ip_interface physical interface to use for detecting # ip_interface physical interface to use for detecting
# #
# check_interval check for changes every !!! checks below 10 minutes make no sense because the Internet # check_interval check for changes every !!! checks below 10 minutes make no sense because the Internet
@ -165,7 +165,7 @@ trap "trap_handler 15" 15 # SIGTERM Termination
# force_ipversion force usage of IPv4 or IPv6 for the whole detection and update communication # force_ipversion force usage of IPv4 or IPv6 for the whole detection and update communication
# dns_server using a non default dns server to get Registered IP from Internet # dns_server using a non default dns server to get Registered IP from Internet
# force_dnstcp force communication with DNS server via TCP instead of default UDP # force_dnstcp force communication with DNS server via TCP instead of default UDP
# proxy using a proxy for communication !!! ALSO used to detect local IP via web => return proxy's IP !!! # proxy using a proxy for communication !!! ALSO used to detect current IP via web => return proxy's IP !!!
# use_logfile self-explanatory "/var/log/ddns/$SECTION_ID.log" # use_logfile self-explanatory "/var/log/ddns/$SECTION_ID.log"
# is_glue the record that should be updated is a glue record # is_glue the record that should be updated is a glue record
# #
@ -280,8 +280,8 @@ esac
# verify ip_source 'script' if script is configured and executable # verify ip_source 'script' if script is configured and executable
if [ "$ip_source" = "script" ]; then if [ "$ip_source" = "script" ]; then
set -- $ip_script #handling script with parameters, we need a trick set -- $ip_script #handling script with parameters, we need a trick
[ -z "$1" ] && write_log 14 "No script defined to detect local IP!" [ -z "$1" ] && write_log 14 "No script defined to detect current IP!"
[ -x "$1" ] || write_log 14 "Script to detect local IP not executable!" [ -x "$1" ] || write_log 14 "Script to detect current IP not executable!"
fi fi
# compute update interval in seconds # compute update interval in seconds
@ -347,8 +347,8 @@ ERR_LAST=$?
write_log 6 "Starting main loop at $(eval $DATE_PROG)" write_log 6 "Starting main loop at $(eval $DATE_PROG)"
while : ; do while : ; do
get_local_ip LOCAL_IP # read local IP get_current_ip CURRENT_IP # read current IP
[ $use_ipv6 -eq 1 ] && expand_ipv6 "$LOCAL_IP" LOCAL_IP # on IPv6 we use expanded version [ $use_ipv6 -eq 1 ] && expand_ipv6 "$CURRENT_IP" CURRENT_IP # on IPv6 we use expanded version
# prepare update # prepare update
# never updated or forced immediate then NEXT_TIME = 0 # never updated or forced immediate then NEXT_TIME = 0
@ -358,24 +358,24 @@ while : ; do
get_uptime CURR_TIME # get current uptime get_uptime CURR_TIME # get current uptime
# send update when current time > next time or local ip different from registered ip # send update when current time > next time or current ip different from registered ip
if [ $CURR_TIME -ge $NEXT_TIME -o "$LOCAL_IP" != "$REGISTERED_IP" ]; then if [ $CURR_TIME -ge $NEXT_TIME -o "$CURRENT_IP" != "$REGISTERED_IP" ]; then
if [ $VERBOSE -gt 2 ]; then if [ $VERBOSE -gt 2 ]; then
write_log 7 "Verbose Mode: $VERBOSE - NO UPDATE send" write_log 7 "Verbose Mode: $VERBOSE - NO UPDATE send"
elif [ "$LOCAL_IP" != "$REGISTERED_IP" ]; then elif [ "$CURRENT_IP" != "$REGISTERED_IP" ]; then
write_log 7 "Update needed - L: '$LOCAL_IP' <> R: '$REGISTERED_IP'" write_log 7 "Update needed - L: '$CURRENT_IP' <> R: '$REGISTERED_IP'"
else else
write_log 7 "Forced Update - L: '$LOCAL_IP' == R: '$REGISTERED_IP'" write_log 7 "Forced Update - L: '$CURRENT_IP' == R: '$REGISTERED_IP'"
fi fi
ERR_LAST=0 ERR_LAST=0
[ $VERBOSE -lt 3 ] && { [ $VERBOSE -lt 3 ] && {
# only send if VERBOSE < 3 # only send if VERBOSE < 3
send_update "$LOCAL_IP" send_update "$CURRENT_IP"
ERR_LAST=$? # save return value ERR_LAST=$? # save return value
} }
# error sending local IP to provider # error sending current IP to provider
# we have no communication error (handled inside send_update/do_transfer) # we have no communication error (handled inside send_update/do_transfer)
# but update was not recognized # but update was not recognized
# do NOT retry after RETRY_SECONDS, do retry after CHECK_SECONDS # do NOT retry after RETRY_SECONDS, do retry after CHECK_SECONDS
@ -384,9 +384,9 @@ while : ; do
if [ $ERR_LAST -eq 0 ]; then if [ $ERR_LAST -eq 0 ]; then
get_uptime LAST_TIME # we send update, so get_uptime LAST_TIME # we send update, so
echo $LAST_TIME > $UPDFILE # save LASTTIME to file echo $LAST_TIME > $UPDFILE # save LASTTIME to file
[ "$LOCAL_IP" != "$REGISTERED_IP" ] \ [ "$CURRENT_IP" != "$REGISTERED_IP" ] \
&& write_log 6 "Update successful - IP '$LOCAL_IP' send" \ && write_log 6 "Update successful - IP '$CURRENT_IP' send" \
|| write_log 6 "Forced update successful - IP: '$LOCAL_IP' send" || write_log 6 "Forced update successful - IP: '$CURRENT_IP' send"
elif [ $ERR_LAST -eq 127 ]; then elif [ $ERR_LAST -eq 127 ]; then
write_log 3 "No update send to DDNS Provider" write_log 3 "No update send to DDNS Provider"
else else
@ -409,7 +409,7 @@ while : ; do
[ $use_ipv6 -eq 1 ] && expand_ipv6 "$REGISTERED_IP" REGISTERED_IP # on IPv6 we use expanded version [ $use_ipv6 -eq 1 ] && expand_ipv6 "$REGISTERED_IP" REGISTERED_IP # on IPv6 we use expanded version
# IP's are still different # IP's are still different
if [ "$LOCAL_IP" != "$REGISTERED_IP" ]; then if [ "$CURRENT_IP" != "$REGISTERED_IP" ]; then
if [ $VERBOSE -le 1 ]; then # VERBOSE <=1 then retry if [ $VERBOSE -le 1 ]; then # VERBOSE <=1 then retry
ERR_UPDATE=$(( $ERR_UPDATE + 1 )) ERR_UPDATE=$(( $ERR_UPDATE + 1 ))
[ $retry_max_count -gt 0 -a $ERR_UPDATE -gt $retry_max_count ] && \ [ $retry_max_count -gt 0 -a $ERR_UPDATE -gt $retry_max_count ] && \