1#!/bin/bash
2
3# Copyright (c) 2013, 2021, Oracle and/or its affiliates.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License, version 2.0,
7# as published by the Free Software Foundation.
8#
9# This program is also distributed with certain software (including
10# but not limited to OpenSSL) that is licensed under separate terms,
11# as designated in a particular file or component or in included license
12# documentation.  The authors of MySQL hereby grant you an additional
13# permission to link the program and your derivative works with the
14# separately licensed software that they have included with MySQL.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19# GNU General Public License, version 2.0, for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24
25# simulate network latency on Mac OS X, see man ipfw(8)
26
27#set -x
28
29if [ $# -lt 1 ] ; then
30  echo "usage: `basename $0` <delay-ms> [<port> ...]"
31  echo "                 delay < 0 deletes the delay rule set"
32  echo "                       = 0 disables the delay rule set"
33  echo "                       > 0 enables the delay rule set"
34  echo "                 port... adds the ports to the delay rule set"
35  exit 1
36fi
37
38delay=$1
39shift
40ports=$*
41#echo "simulate a delay of ($delay + $delay) ms on ports $ports"
42echo "simulate a delay of $delay ms on destination ports $ports"
43pipe=4711 # 1..64k
44rset=21   # 0..30
45
46if [ $delay -lt 0 ] ; then
47    echo "removing rules (as admin)..."
48    sudo ipfw delete set $rset
49    sudo ipfw pipe delete $pipe > /dev/null 2>&1
50elif [ $delay -eq 0 ] ; then
51    echo "disabling rules (as admin)..."
52    sudo ipfw set disable $rset
53else
54    echo "adding rules (as admin)..."
55    # not strictly necessary but better change atomically
56    sudo ipfw set disable $rset
57    sudo ipfw pipe $pipe config delay $delay
58    sudo ipfw set enable $rset
59    # rules not visible if set disabled
60    for p in $ports ; do
61        sudo ipfw list $p > /dev/null
62        if [ $? -ne 0 ] ; then
63            # not strictly necessary but better change atomically
64            sudo ipfw set disable $rset
65#            sudo ipfw add $p set $rset pipe $pipe src-port $p > /dev/null 2>&1
66            sudo ipfw add $p set $rset pipe $pipe dst-port $p > /dev/null 2>&1
67            sudo ipfw set enable $rset
68        fi
69    done
70fi
71echo "current rules:"
72sudo ipfw list | grep $pipe
73#sudo ipfw show
74echo "done."
75
76#set +x
77