Skip to content

Instantly share code, notes, and snippets.

@rcook
Last active December 24, 2023 00:41
Show Gist options
  • Save rcook/5c151c7c0098244191fb to your computer and use it in GitHub Desktop.
Save rcook/5c151c7c0098244191fb to your computer and use it in GitHub Desktop.
Simulating packet loss with iptables/ipfw
The MIT License (MIT)
Copyright (c) 2010 Richard Cook
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#!/bin/bash
IPTABLES_SAVE_FILE_NAME=/tmp/udp-client-server-iptables.save
PLATFORM=`uname -s`
PACKET_LOSS_RATE=$1
# BEGIN: Platform-specific.
function check_preconditions {
case $PLATFORM in
Linux)
if [ -f $IPTABLES_SAVE_FILE_NAME ]
then
echo "iptables backup file already exists. Please clean up and try again."
return 1
fi
;;
Darwin)
;;
*)
echo "Unsupported platform \"$PLATFORM\"."
return 1
;;
esac
}
function set_up {
case $PLATFORM in
Linux)
iptables-save -c > $IPTABLES_SAVE_FILE_NAME
iptables -A INPUT -m statistic --mode random --probability $PACKET_LOSS_RATE -j DROP
;;
Darwin)
ipfw add pipe 1 in proto udp
ipfw pipe 1 config plr $PACKET_LOSS_RATE
;;
*)
echo "Unsupported platform \"$PLATFORM\"."
return 1
;;
esac
}
function tear_down {
case $PLATFORM in
Linux)
if [ -f $IPTABLES_SAVE_FILE_NAME ]
then
iptables-restore -c < $IPTABLES_SAVE_FILE_NAME
rm $IPTABLES_SAVE_FILE_NAME
fi
;;
Darwin)
ipfw -q flush
;;
*)
echo "Unsupported platform \"$PLATFORM\"."
return 1
;;
esac
}
function display_diagnostics {
echo "--------------------"
case $PLATFORM in
Linux)
iptables --list
;;
Darwin)
ipfw list
;;
*)
echo "Unsupported platform \"$PLATFORM\"."
return 1
;;
esac
echo "--------------------"
}
# END: Platform-specific.
if [[ `whoami` != "root" ]]
then
echo "Please run this script as root or using sudo."
exit 1
fi
if [[ "$PACKET_LOSS_RATE" == "" ]]
then
echo "Please specify packet loss rate (in range 0.0 to 1.0)."
exit 1
fi
function on_exit {
tear_down
exit $?
}
trap on_exit EXIT
check_preconditions
if [ $? != 0 ]
then
exit 1
fi
set_up
if [ $? != 0 ]
then
exit 1
fi
display_diagnostics
if [ $? != 0 ]
then
exit 1
fi
while [ true ]
do
echo -n "Press Q to quit and restore firewall settings >"
read text
if [[ "$text" == "Q" || "$text" == "q" ]]
then
break
fi
done
tear_down
if [ $? != 0 ]
then
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment