big svn cleanup
[anytun.git] / src / openvpn / gremlin.c
blob0a3cf19d3cc34e63f761ba0dee0a15c7ab9772cf
1 /*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
8 * Copyright (C) 2002-2005 OpenVPN Solutions LLC <info@openvpn.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * Test protocol robustness by simulating dropped packets and
27 * network outages when the --gremlin option is used.
30 #ifdef WIN32
31 #include "config-win32.h"
32 #else
33 #include "config.h"
34 #endif
36 #include "syshead.h"
38 #ifdef ENABLE_DEBUG
40 #include "error.h"
41 #include "common.h"
42 #include "misc.h"
43 #include "otime.h"
44 #include "gremlin.h"
46 #include "memdbg.h"
49 * Parameters for packet corruption and droppage.
50 * Each parameter has 4 possible levels, 0 = disabled,
51 * while 1, 2, and 3 are enumerated in the below arrays.
52 * The parameter is a 2-bit field within the --gremlin
53 * parameter.
57 * Probability that we will drop a packet is 1 / n
59 static const int drop_freq[] = { 500, 100, 50 };
62 * Probability that we will corrupt a packet is 1 / n
64 static const int corrupt_freq[] = { 500, 100, 50 };
67 * When network goes up, it will be up for between
68 * UP_LOW and UP_HIGH seconds.
70 static const int up_low[] = { 60, 10, 5 };
71 static const int up_high[] = { 600, 60, 10 };
74 * When network goes down, it will be down for between
75 * DOWN_LOW and DOWN_HIGH seconds.
77 static const int down_low[] = { 5, 10, 10 };
78 static const int down_high[] = { 10, 60, 120 };
81 * Packet flood levels:
82 * { number of packets, packet size }
84 static const struct packet_flood_parms packet_flood_data[] =
85 {{10, 100}, {10, 1500}, {100, 1500}};
87 struct packet_flood_parms
88 get_packet_flood_parms (int level)
90 ASSERT (level > 0 && level < 4);
91 return packet_flood_data [level - 1];
95 * Return true with probability 1/n
97 static bool flip(int n) {
98 return (get_random() % n) == 0;
102 * Return uniformly distributed random number between
103 * low and high.
105 static int roll(int low, int high) {
106 int ret;
107 ASSERT (low <= high);
108 ret = low + (get_random() % (high - low + 1));
109 ASSERT (ret >= low && ret <= high);
110 return ret;
113 static bool initialized; /* GLOBAL */
114 static bool up; /* GLOBAL */
115 static time_t next; /* GLOBAL */
118 * Return false if we should drop a packet.
120 bool
121 ask_gremlin (int flags)
123 const int up_down_level = GREMLIN_UP_DOWN_LEVEL (flags);
124 const int drop_level = GREMLIN_DROP_LEVEL (flags);
126 if (!initialized)
128 initialized = true;
130 if (up_down_level)
131 up = false;
132 else
133 up = true;
135 next = now;
138 if (up_down_level) /* change up/down state? */
140 if (now >= next)
142 int delta;
143 if (up)
145 delta = roll (down_low[up_down_level-1], down_high[up_down_level-1]);
146 up = false;
148 else
150 delta = roll (up_low[up_down_level-1], up_high[up_down_level-1]);
151 up = true;
154 msg (D_GREMLIN,
155 "GREMLIN: CONNECTION GOING %s FOR %d SECONDS",
156 (up ? "UP" : "DOWN"),
157 delta);
158 next = now + delta;
162 if (drop_level)
164 if (up && flip (drop_freq[drop_level-1]))
166 dmsg (D_GREMLIN_VERBOSE, "GREMLIN: Random packet drop");
167 return false;
171 return up;
175 * Possibly corrupt a packet.
177 void corrupt_gremlin (struct buffer *buf, int flags) {
178 const int corrupt_level = GREMLIN_CORRUPT_LEVEL (flags);
179 if (corrupt_level)
181 if (flip (corrupt_freq[corrupt_level-1]))
185 if (buf->len > 0)
187 uint8_t r = roll (0, 255);
188 int method = roll (0, 5);
190 switch (method) {
191 case 0: /* corrupt the first byte */
192 *BPTR (buf) = r;
193 break;
194 case 1: /* corrupt the last byte */
195 *(BPTR (buf) + buf->len - 1) = r;
196 break;
197 case 2: /* corrupt a random byte */
198 *(BPTR(buf) + roll (0, buf->len - 1)) = r;
199 break;
200 case 3: /* append a random byte */
201 buf_write (buf, &r, 1);
202 break;
203 case 4: /* reduce length by 1 */
204 --buf->len;
205 break;
206 case 5: /* reduce length by a random amount */
207 buf->len -= roll (0, buf->len - 1);
208 break;
210 dmsg (D_GREMLIN_VERBOSE, "GREMLIN: Packet Corruption, method=%d", method);
212 else
213 break;
214 } while (flip (2)); /* a 50% chance we will corrupt again */
219 #else
220 static void dummy(void) {}
221 #endif