Skip to content

Commit

Permalink
improve on MTU-check
Browse files Browse the repository at this point in the history
  • Loading branch information
MayNiklas committed Feb 3, 2024
1 parent c7eed55 commit ab9d3c5
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions packages/mtu-check/default.nix
Original file line number Diff line number Diff line change
@@ -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..."
Expand All @@ -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
''

0 comments on commit ab9d3c5

Please sign in to comment.