diff --git a/packages/mtu-check/default.nix b/packages/mtu-check/default.nix index e12aea4d..1f001542 100644 --- a/packages/mtu-check/default.nix +++ b/packages/mtu-check/default.nix @@ -1,17 +1,29 @@ -{ pkgs, stdenv, ... }: -let - mtu-check-skript = pkgs.writeShellScriptBin "mtu-check" '' - # start with a known good MTU - MTU=1380 - IP=1.1.1.1 +# 2. Check 5 times before giving up +{ writeShellScriptBin, iputils, ... }: +writeShellScriptBin "mtu-check" '' + # start with a known good MTU + MTU=1380 + + IP=1.1.1.1 + + # if argument is given, use it as IP + if [ -n "$1" ]; then + IP=$1 + fi - # check maximum transmission unit (MTU) of a network - # loop until we get an error - while true; do - # try to ping with the current MTU - echo "Trying MTU of $MTU" - ping -c 1 -M do -s $MTU $IP >/dev/null 2>&1 - if [ $? -ne 0 ]; then + # counter for failed pings in a row + counter=0 + + # check maximum transmission unit (MTU) of a network + # loop until we get an error + while true; do + # try to ping with the current MTU + echo "Trying MTU of $MTU" + ${iputils}/bin/ping -c 1 -M do -s $MTU $IP >/dev/null 2>&1 + if [ $? -ne 0 ]; then + counter=$(($counter + 1)) + # if we failed 5 times in a row, we assume the MTU is too big + if [ $counter -eq 5 ]; then # ping failed, so the MTU is too big echo "Ping with MTU size $MTU failed..." echo "Adding 28 Bytes for ICMP and IP header to the previous value..." @@ -20,25 +32,14 @@ let MTU=$(($MTU + 28 - 1)) echo "MTU of network is $MTU Bytes" exit 0 - else - # ping succeeded, so the MTU is too small - # increase it by 1 and try again - MTU=$(($MTU + 1)) - fi - done - ''; -in -stdenv.mkDerivation { - - pname = "mtu-check"; - version = "0.1.0"; - - # Needed if no src is used. Alternatively place script in - # separate file and include it as src - dontUnpack = true; - - installPhase = '' - cp -r ${mtu-check-skript} $out - ''; -} + fi + else + # ping succeeded, so the MTU is too small + # increase it by 1 and try again + MTU=$(($MTU + 1)) + # reset counter if we had a successful ping + counter=0 + fi + done +''