]> johannes.truschnigg.info Git - sqm_lagthrottle/commitdiff
Improve parameter validation in wrapper
authorJohannes Truschnigg <johannes@truschnigg.info>
Sat, 12 Mar 2022 15:56:18 +0000 (16:56 +0100)
committerJohannes Truschnigg <johannes@truschnigg.info>
Sat, 12 Mar 2022 15:56:18 +0000 (16:56 +0100)
lagdetect.sh

index f37fe731b38b359cad9a051cbc9e236e3fadc201..9b7f3cd203ee6b61ea6a90c695c996df767afe80 100755 (executable)
@@ -14,21 +14,85 @@ PING_INTERVAL=500
 # DO NOT EDIT BELOW #
 #####################
 
+__is_num() {
+case "${1}" in
+  '')
+    return 1
+  ;;
+  *.*.*)
+    return 1
+  ;;
+  *[!0123456789.]*)
+    return 1
+  ;;
+  [0123456789]*)
+    return 0
+  ;;
+  *)
+    return 1
+  ;;
+esac
+}
+
+
+__is_int() {
+if __is_num "${1}"
+then
+  case "${1}" in
+    *.*)
+      return 1
+    ;;
+    *)
+      return 0
+    ;;
+  esac
+fi
+return 1
+}
+
+
 for p in traceroute fping awk
 do
   type "${p}" >/dev/null || { printf 'FATAL: Required program "%s" not installed.\n' "${p}" >&2; exit 1; }
 done
 
-if [ x"${PING_GW_IPV6}" = xyes ]
+
+if [ x"${PING_GW_IPV6:-no}" = xyes ]
 then
   PING_PEERS="$(traceroute -6 -n -m 2001:4860:4860::8888 | awk '{if("1" == $1){print $2}}') ${PING_PEERS}"
 fi
 
-if [ x"${PING_GW_IPV4}" = xyes ]
+
+if [ x"${PING_GW_IPV4:-no}" = xyes ]
+then
+  PING_PEERS="$(traceroute -4 -n -m 1 8.8.8.8 | awk '{if("1" == $1){print $2}}') ${PING_PEERS}"
+fi
+
+if ! __is_num "${THRESHOLD}"
 then
-  PING_PEERS="$(traceroute -4 -n -m 1 1.1.1.1 | awk '{if("1" == $1){print $2}}') ${PING_PEERS}"
+  printf 'FATAL: Invalid value for THRESHOLD\n'
+  exit 1
 fi
 
+if ! __is_num "${FUDGE_MS}"
+then
+  printf 'FATAL: Invalid value for FUDGE_MS\n'
+  exit 1
+fi
+
+if ! __is_int "${PING_SLOTS_PER_PEER}"
+then
+  printf 'FATAL: Invalid value for PING_SLOTS_PER_PEER\n'
+  exit 1
+fi
+
+if ! __is_int "${PING_INTERVAL}"
+then
+  printf 'FATAL: Invalid value for PING_INTERVAL\n'
+  exit 1
+fi
+
+
 set -u
 
 fping --loop --retry=1 --size=1280 --period="${PING_INTERVAL}" --random ${PING_PEERS} 2>/dev/null \