Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * UDP proxy: emulate an unreliable UDP connexion for DTLS testing |
| 3 | * |
| 4 | * Copyright (C) 2006-2014, Brainspark B.V. |
| 5 | * |
Manuel Pégourié-Gonnard | 3d2c4b7 | 2015-01-29 11:33:59 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #if !defined(POLARSSL_CONFIG_FILE) |
| 24 | #include "polarssl/config.h" |
| 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
| 28 | |
| 29 | #if !defined(POLARSSL_NET_C) |
| 30 | #include <stdio.h> |
| 31 | int main( void ) |
| 32 | { |
| 33 | printf( "POLARSSL_NET_C not defined.\n" ); |
| 34 | return( 0 ); |
| 35 | } |
| 36 | #else |
| 37 | |
| 38 | #include "polarssl/net.h" |
| 39 | #include "polarssl/error.h" |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 40 | #include "polarssl/ssl.h" |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 41 | |
| 42 | #include <stdio.h> |
| 43 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 44 | #include <time.h> |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 45 | |
| 46 | /* For select() */ |
| 47 | #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \ |
| 48 | !defined(EFI32) |
| 49 | #include <winsock2.h> |
| 50 | #include <windows.h> |
| 51 | #if defined(_MSC_VER) |
| 52 | #if defined(_WIN32_WCE) |
| 53 | #pragma comment( lib, "ws2.lib" ) |
| 54 | #else |
| 55 | #pragma comment( lib, "ws2_32.lib" ) |
| 56 | #endif |
| 57 | #endif /* _MSC_VER */ |
| 58 | #else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ |
| 59 | #include <sys/time.h> |
| 60 | #include <sys/types.h> |
| 61 | #include <unistd.h> |
| 62 | #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ |
| 63 | |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 64 | /* For gettimeofday() */ |
| 65 | #if !defined(_WIN32) |
| 66 | #include <sys/time.h> |
| 67 | #endif |
| 68 | |
Manuel Pégourié-Gonnard | b46780e | 2014-09-23 12:17:30 +0200 | [diff] [blame] | 69 | #define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 70 | |
| 71 | #define DFL_SERVER_ADDR "localhost" |
| 72 | #define DFL_SERVER_PORT 4433 |
| 73 | #define DFL_LISTEN_ADDR "localhost" |
| 74 | #define DFL_LISTEN_PORT 5556 |
| 75 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 76 | #define USAGE \ |
| 77 | "\n usage: udp_proxy param=<>...\n" \ |
| 78 | "\n acceptable parameters:\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 79 | " server_addr=%%s default: localhost\n" \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 80 | " server_port=%%d default: 4433\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 81 | " listen_addr=%%s default: localhost\n" \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 82 | " listen_port=%%d default: 4433\n" \ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 83 | "\n" \ |
| 84 | " duplicate=%%d default: 0 (no duplication)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 85 | " duplicate about 1:N packets randomly\n" \ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 86 | " delay=%%d default: 0 (no delayed packets)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 87 | " delay about 1:N packets randomly\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 88 | " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \ |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 89 | " drop=%%d default: 0 (no dropped packets)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 90 | " drop about 1:N packets randomly\n" \ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 91 | " mtu=%%d default: 0 (unlimited)\n" \ |
| 92 | " drop packets larger than N bytes\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 93 | " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \ |
| 94 | " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \ |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 95 | " protect_len=%%d default: (don't protect packets of this size)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 96 | "\n" \ |
| 97 | " seed=%%d default: (use current time)\n" \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 98 | "\n" |
| 99 | |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 100 | /* |
| 101 | * global options |
| 102 | */ |
| 103 | static struct options |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 104 | { |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 105 | const char *server_addr; /* address to forward packets to */ |
| 106 | int server_port; /* port to forward packets to */ |
| 107 | const char *listen_addr; /* address for accepting client connections */ |
| 108 | int listen_port; /* port for accepting client connections */ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 109 | |
| 110 | int duplicate; /* duplicate 1 in N packets (none if 0) */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 111 | int delay; /* delay 1 packet in N (none if 0) */ |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 112 | int delay_ccs; /* delay ChangeCipherSpec */ |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 113 | int drop; /* drop 1 packet in N (none if 0) */ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 114 | int mtu; /* drop packets larger than this */ |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 115 | int bad_ad; /* inject corrupted ApplicationData record */ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 116 | int protect_hvr; /* never drop or delay HelloVerifyRequest */ |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 117 | int protect_len; /* never drop/delay packet of the given size*/ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 118 | |
| 119 | unsigned int seed; /* seed for "random" events */ |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 120 | } opt; |
| 121 | |
| 122 | static void exit_usage( const char *name, const char *value ) |
| 123 | { |
| 124 | if( value == NULL ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 125 | printf( " unknown option or missing value: %s\n", name ); |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 126 | else |
| 127 | printf( " option %s: illegal value: %s\n", name, value ); |
| 128 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 129 | printf( USAGE ); |
| 130 | exit( 1 ); |
| 131 | } |
| 132 | |
| 133 | static void get_options( int argc, char *argv[] ) |
| 134 | { |
| 135 | int i; |
| 136 | char *p, *q; |
| 137 | |
| 138 | opt.server_addr = DFL_SERVER_ADDR; |
| 139 | opt.server_port = DFL_SERVER_PORT; |
| 140 | opt.listen_addr = DFL_LISTEN_ADDR; |
| 141 | opt.listen_port = DFL_LISTEN_PORT; |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 142 | /* Other members default to 0 */ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 143 | |
| 144 | for( i = 1; i < argc; i++ ) |
| 145 | { |
| 146 | p = argv[i]; |
| 147 | if( ( q = strchr( p, '=' ) ) == NULL ) |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 148 | exit_usage( p, NULL ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 149 | *q++ = '\0'; |
| 150 | |
| 151 | if( strcmp( p, "server_addr" ) == 0 ) |
| 152 | opt.server_addr = q; |
| 153 | else if( strcmp( p, "server_port" ) == 0 ) |
| 154 | { |
| 155 | opt.server_port = atoi( q ); |
| 156 | if( opt.server_port < 1 || opt.server_port > 65535 ) |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 157 | exit_usage( p, q ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 158 | } |
| 159 | else if( strcmp( p, "listen_addr" ) == 0 ) |
| 160 | opt.listen_addr = q; |
| 161 | else if( strcmp( p, "listen_port" ) == 0 ) |
| 162 | { |
| 163 | opt.listen_port = atoi( q ); |
| 164 | if( opt.listen_port < 1 || opt.listen_port > 65535 ) |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 165 | exit_usage( p, q ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 166 | } |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 167 | else if( strcmp( p, "duplicate" ) == 0 ) |
| 168 | { |
| 169 | opt.duplicate = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 170 | if( opt.duplicate < 0 || opt.duplicate > 20 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 171 | exit_usage( p, q ); |
| 172 | } |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 173 | else if( strcmp( p, "delay" ) == 0 ) |
| 174 | { |
| 175 | opt.delay = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 176 | if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 177 | exit_usage( p, q ); |
| 178 | } |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 179 | else if( strcmp( p, "delay_ccs" ) == 0 ) |
| 180 | { |
| 181 | opt.delay_ccs = atoi( q ); |
| 182 | if( opt.delay_ccs < 0 || opt.delay_ccs > 1 ) |
| 183 | exit_usage( p, q ); |
| 184 | } |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 185 | else if( strcmp( p, "drop" ) == 0 ) |
| 186 | { |
| 187 | opt.drop = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 188 | if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 ) |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 189 | exit_usage( p, q ); |
| 190 | } |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 191 | else if( strcmp( p, "mtu" ) == 0 ) |
| 192 | { |
| 193 | opt.mtu = atoi( q ); |
| 194 | if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE ) |
| 195 | exit_usage( p, q ); |
| 196 | } |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 197 | else if( strcmp( p, "bad_ad" ) == 0 ) |
| 198 | { |
| 199 | opt.bad_ad = atoi( q ); |
| 200 | if( opt.bad_ad < 0 || opt.bad_ad > 1 ) |
| 201 | exit_usage( p, q ); |
| 202 | } |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 203 | else if( strcmp( p, "protect_hvr" ) == 0 ) |
| 204 | { |
| 205 | opt.protect_hvr = atoi( q ); |
| 206 | if( opt.protect_hvr < 0 || opt.protect_hvr > 1 ) |
| 207 | exit_usage( p, q ); |
| 208 | } |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 209 | else if( strcmp( p, "protect_len" ) == 0 ) |
| 210 | { |
| 211 | opt.protect_len = atoi( q ); |
| 212 | if( opt.protect_len < 0 ) |
| 213 | exit_usage( p, q ); |
| 214 | } |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 215 | else if( strcmp( p, "seed" ) == 0 ) |
| 216 | { |
| 217 | opt.seed = atoi( q ); |
| 218 | if( opt.seed == 0 ) |
| 219 | exit_usage( p, q ); |
| 220 | } |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 221 | else |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 222 | exit_usage( p, NULL ); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | static const char *msg_type( unsigned char *msg, size_t len ) |
| 227 | { |
| 228 | if( len < 1 ) return( "Invalid" ); |
| 229 | switch( msg[0] ) |
| 230 | { |
| 231 | case SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" ); |
| 232 | case SSL_MSG_ALERT: return( "Alert" ); |
| 233 | case SSL_MSG_APPLICATION_DATA: return( "ApplicationData" ); |
| 234 | case SSL_MSG_HANDSHAKE: break; /* See below */ |
| 235 | default: return( "Unknown" ); |
| 236 | } |
| 237 | |
Manuel Pégourié-Gonnard | 8cc7e03 | 2014-09-25 12:59:05 +0200 | [diff] [blame] | 238 | if( len < 13 + 12 ) return( "Invalid handshake" ); |
| 239 | |
| 240 | /* |
| 241 | * Our handshake message are less than 2^16 bytes long, so they should |
| 242 | * have 0 as the first byte of length, frag_offset and frag_length. |
| 243 | * Otherwise, assume they are encrypted. |
| 244 | */ |
| 245 | if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" ); |
| 246 | |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 247 | switch( msg[13] ) |
| 248 | { |
| 249 | case SSL_HS_HELLO_REQUEST: return( "HelloRequest" ); |
| 250 | case SSL_HS_CLIENT_HELLO: return( "ClientHello" ); |
| 251 | case SSL_HS_SERVER_HELLO: return( "ServerHello" ); |
| 252 | case SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" ); |
| 253 | case SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" ); |
| 254 | case SSL_HS_CERTIFICATE: return( "Certificate" ); |
| 255 | case SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" ); |
| 256 | case SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" ); |
| 257 | case SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" ); |
| 258 | case SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" ); |
| 259 | case SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" ); |
| 260 | case SSL_HS_FINISHED: return( "Finished" ); |
Manuel Pégourié-Gonnard | bc010a0 | 2014-09-20 12:40:51 +0200 | [diff] [blame] | 261 | default: return( "Unknown handshake" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 265 | /* Return elapsed time in milliseconds since the first call */ |
| 266 | static unsigned long ellapsed_time( void ) |
| 267 | { |
| 268 | #if defined(_WIN32) |
| 269 | return( 0 ); |
| 270 | #else |
| 271 | static struct timeval ref = { 0, 0 }; |
| 272 | struct timeval now; |
| 273 | |
| 274 | if( ref.tv_sec == 0 && ref.tv_usec == 0 ) |
| 275 | { |
| 276 | gettimeofday( &ref, NULL ); |
| 277 | return( 0 ); |
| 278 | } |
| 279 | |
| 280 | gettimeofday( &now, NULL ); |
| 281 | return( 1000 * ( now.tv_sec - ref.tv_sec ) |
| 282 | + ( now.tv_usec - ref.tv_usec ) / 1000 ); |
| 283 | #endif |
| 284 | } |
| 285 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 286 | typedef struct |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 287 | { |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 288 | int dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 289 | const char *way; |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 290 | const char *type; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 291 | unsigned len; |
| 292 | unsigned char buf[MAX_MSG_SIZE]; |
| 293 | } packet; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 294 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 295 | /* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */ |
| 296 | void print_packet( const packet *p, const char *why ) |
| 297 | { |
| 298 | if( why == NULL ) |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 299 | printf( " %05lu %s %s (%u bytes)\n", |
| 300 | ellapsed_time(), p->way, p->type, p->len ); |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 301 | else |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 302 | printf( " %s %s (%u bytes): %s\n", |
| 303 | p->way, p->type, p->len, why ); |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 304 | fflush( stdout ); |
| 305 | } |
| 306 | |
| 307 | int send_packet( const packet *p, const char *why ) |
| 308 | { |
| 309 | int ret; |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 310 | int dst = p->dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 311 | |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 312 | /* insert corrupted ApplicationData record? */ |
| 313 | if( opt.bad_ad && |
| 314 | strcmp( p->type, "ApplicationData" ) == 0 ) |
| 315 | { |
| 316 | unsigned char buf[MAX_MSG_SIZE]; |
| 317 | memcpy( buf, p->buf, p->len ); |
| 318 | ++buf[p->len - 1]; |
| 319 | |
| 320 | print_packet( p, "corrupted" ); |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 321 | if( ( ret = net_send( &dst, buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 322 | { |
| 323 | printf( " ! net_send returned %d\n", ret ); |
| 324 | return( ret ); |
| 325 | } |
| 326 | } |
| 327 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 328 | print_packet( p, why ); |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 329 | if( ( ret = net_send( &dst, p->buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 330 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 331 | printf( " ! net_send returned %d\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 332 | return( ret ); |
| 333 | } |
| 334 | |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 335 | /* Don't duplicate Application Data, only handshake covered */ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 336 | if( opt.duplicate != 0 && |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 337 | strcmp( p->type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 338 | rand() % opt.duplicate == 0 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 339 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 340 | print_packet( p, "duplicated" ); |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 341 | |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 342 | if( ( ret = net_send( &dst, p->buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 343 | { |
| 344 | printf( " ! net_send returned %d\n", ret ); |
| 345 | return( ret ); |
| 346 | } |
| 347 | } |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 348 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 349 | return( 0 ); |
| 350 | } |
| 351 | |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 352 | static packet prev; |
| 353 | |
| 354 | void clear_pending( void ) |
| 355 | { |
| 356 | memset( &prev, 0, sizeof( packet ) ); |
| 357 | } |
| 358 | |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 359 | /* |
| 360 | * Avoid dropping or delaying a packet that was already dropped twice: this |
| 361 | * only results in uninteresting timeouts. We can't rely on type to identify |
| 362 | * packets, since during renegotiation they're all encrypted. So, rely on |
| 363 | * size mod 2048 (which is usually just size). |
| 364 | */ |
| 365 | static unsigned char dropped[2048] = { 0 }; |
| 366 | #define DROP_MAX 2 |
| 367 | |
| 368 | /* |
| 369 | * OpenSSL groups packets in a datagram the first time it sends them, but not |
| 370 | * when it resends them. Count every record as seen the first time. |
| 371 | */ |
| 372 | void update_dropped( const packet *p ) |
| 373 | { |
| 374 | size_t id = p->len % sizeof( dropped ); |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 375 | const unsigned char *end = p->buf + p->len; |
| 376 | const unsigned char *cur = p->buf; |
| 377 | size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13; |
| 378 | |
Manuel Pégourié-Gonnard | fa60f12 | 2014-09-26 16:07:29 +0200 | [diff] [blame] | 379 | ++dropped[id]; |
| 380 | |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 381 | /* Avoid counting single record twice */ |
| 382 | if( len == p->len ) |
| 383 | return; |
| 384 | |
| 385 | while( cur < end ) |
| 386 | { |
| 387 | size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13; |
| 388 | |
| 389 | id = len % sizeof( dropped ); |
| 390 | ++dropped[id]; |
| 391 | |
| 392 | cur += len; |
| 393 | } |
| 394 | } |
| 395 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 396 | int handle_message( const char *way, int dst, int src ) |
| 397 | { |
| 398 | int ret; |
| 399 | packet cur; |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 400 | size_t id; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 401 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 402 | /* receive packet */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 403 | if( ( ret = net_recv( &src, cur.buf, sizeof( cur.buf ) ) ) <= 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 404 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 405 | printf( " ! net_recv returned %d\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 406 | return( ret ); |
| 407 | } |
| 408 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 409 | cur.len = ret; |
| 410 | cur.type = msg_type( cur.buf, cur.len ); |
| 411 | cur.way = way; |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 412 | cur.dst = dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 413 | print_packet( &cur, NULL ); |
| 414 | |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 415 | id = cur.len % sizeof( dropped ); |
| 416 | |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 417 | /* do we want to drop, delay, or forward it? */ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 418 | if( ( opt.mtu != 0 && |
| 419 | cur.len > (unsigned) opt.mtu ) || |
| 420 | ( opt.drop != 0 && |
| 421 | strcmp( cur.type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 422 | ! ( opt.protect_hvr && |
| 423 | strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) && |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 424 | cur.len != (size_t) opt.protect_len && |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 425 | dropped[id] < DROP_MAX && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 426 | rand() % opt.drop == 0 ) ) |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 427 | { |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 428 | update_dropped( &cur ); |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 429 | } |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 430 | else if( ( opt.delay_ccs == 1 && |
| 431 | strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) || |
| 432 | ( opt.delay != 0 && |
| 433 | strcmp( cur.type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 434 | ! ( opt.protect_hvr && |
| 435 | strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) && |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 436 | prev.dst == 0 && |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 437 | cur.len != (size_t) opt.protect_len && |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 438 | dropped[id] < DROP_MAX && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 439 | rand() % opt.delay == 0 ) ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 440 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 441 | memcpy( &prev, &cur, sizeof( packet ) ); |
| 442 | } |
| 443 | else |
| 444 | { |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 445 | /* forward and possibly duplicate */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 446 | if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 ) |
| 447 | return( ret ); |
| 448 | |
| 449 | /* send previously delayed message if any */ |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 450 | if( prev.dst != 0 ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 451 | { |
| 452 | ret = send_packet( &prev, "delayed" ); |
| 453 | memset( &prev, 0, sizeof( packet ) ); |
| 454 | if( ret != 0 ) |
| 455 | return( ret ); |
| 456 | } |
| 457 | } |
| 458 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 459 | return( 0 ); |
| 460 | } |
| 461 | |
| 462 | int main( int argc, char *argv[] ) |
| 463 | { |
| 464 | int ret; |
| 465 | |
| 466 | int listen_fd = -1; |
| 467 | int client_fd = -1; |
| 468 | int server_fd = -1; |
| 469 | |
| 470 | int nb_fds; |
| 471 | fd_set read_fds; |
| 472 | |
| 473 | get_options( argc, argv ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 474 | |
| 475 | /* |
| 476 | * Decisions to drop/delay/duplicate packets are pseudo-random: dropping |
| 477 | * exactly 1 in N packets would lead to problems when a flight has exactly |
| 478 | * N packets: the same packet would be dropped on every resend. |
| 479 | * |
| 480 | * In order to be able to reproduce problems reliably, the seed may be |
| 481 | * specified explicitly. |
| 482 | */ |
| 483 | if( opt.seed == 0 ) |
| 484 | { |
| 485 | opt.seed = time( NULL ); |
| 486 | printf( " . Pseudo-random seed: %u\n", opt.seed ); |
| 487 | } |
| 488 | |
| 489 | srand( opt.seed ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 490 | |
| 491 | /* |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 492 | * 0. "Connect" to the server |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 493 | */ |
| 494 | printf( " . Connect to server on UDP/%s/%d ...", |
| 495 | opt.server_addr, opt.server_port ); |
| 496 | fflush( stdout ); |
| 497 | |
| 498 | if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port, |
| 499 | NET_PROTO_UDP ) ) != 0 ) |
| 500 | { |
| 501 | printf( " failed\n ! net_connect returned %d\n\n", ret ); |
| 502 | goto exit; |
| 503 | } |
| 504 | |
| 505 | printf( " ok\n" ); |
| 506 | |
| 507 | /* |
| 508 | * 1. Setup the "listening" UDP socket |
| 509 | */ |
| 510 | printf( " . Bind on UDP/%s/%d ...", |
| 511 | opt.listen_addr, opt.listen_port ); |
| 512 | fflush( stdout ); |
| 513 | |
| 514 | if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port, |
| 515 | NET_PROTO_UDP ) ) != 0 ) |
| 516 | { |
| 517 | printf( " failed\n ! net_bind returned %d\n\n", ret ); |
| 518 | goto exit; |
| 519 | } |
| 520 | |
| 521 | printf( " ok\n" ); |
| 522 | |
| 523 | /* |
| 524 | * 2. Wait until a client connects |
| 525 | */ |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 526 | accept: |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 527 | printf( " . Waiting for a remote connection ..." ); |
| 528 | fflush( stdout ); |
| 529 | |
| 530 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) |
| 531 | { |
| 532 | printf( " failed\n ! net_accept returned %d\n\n", ret ); |
| 533 | goto exit; |
| 534 | } |
| 535 | |
| 536 | printf( " ok\n" ); |
| 537 | fflush( stdout ); |
| 538 | |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 539 | printf( " . Re-bind on UDP/%s/%d ...", |
| 540 | opt.listen_addr, opt.listen_port ); |
| 541 | fflush( stdout ); |
| 542 | |
| 543 | if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port, |
| 544 | NET_PROTO_UDP ) ) != 0 ) |
| 545 | { |
| 546 | printf( " failed\n ! net_bind returned %d\n\n", ret ); |
| 547 | goto exit; |
| 548 | } |
| 549 | |
| 550 | printf( " ok\n" ); |
| 551 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 552 | /* |
| 553 | * 3. Forward packets forever (kill the process to terminate it) |
| 554 | */ |
Manuel Pégourié-Gonnard | ce8588c | 2014-10-01 00:56:03 +0200 | [diff] [blame] | 555 | clear_pending(); |
| 556 | memset( dropped, 0, sizeof( dropped ) ); |
| 557 | |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 558 | nb_fds = client_fd; |
| 559 | if( nb_fds < server_fd ) |
| 560 | nb_fds = server_fd; |
| 561 | if( nb_fds < listen_fd ) |
| 562 | nb_fds = listen_fd; |
| 563 | ++nb_fds; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 564 | |
| 565 | while( 1 ) |
| 566 | { |
| 567 | FD_ZERO( &read_fds ); |
| 568 | FD_SET( server_fd, &read_fds ); |
| 569 | FD_SET( client_fd, &read_fds ); |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 570 | FD_SET( listen_fd, &read_fds ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 571 | |
| 572 | if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 ) |
| 573 | { |
| 574 | perror( "select" ); |
| 575 | goto exit; |
| 576 | } |
| 577 | |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 578 | if( FD_ISSET( listen_fd, &read_fds ) ) |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 579 | goto accept; |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 580 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 581 | if( FD_ISSET( client_fd, &read_fds ) ) |
| 582 | { |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 583 | if( ( ret = handle_message( "S <- C", |
| 584 | server_fd, client_fd ) ) != 0 ) |
Manuel Pégourié-Gonnard | ce8588c | 2014-10-01 00:56:03 +0200 | [diff] [blame] | 585 | goto accept; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | if( FD_ISSET( server_fd, &read_fds ) ) |
| 589 | { |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 590 | if( ( ret = handle_message( "S -> C", |
| 591 | client_fd, server_fd ) ) != 0 ) |
Manuel Pégourié-Gonnard | ce8588c | 2014-10-01 00:56:03 +0200 | [diff] [blame] | 592 | goto accept; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
| 596 | exit: |
| 597 | |
| 598 | #ifdef POLARSSL_ERROR_C |
| 599 | if( ret != 0 ) |
| 600 | { |
| 601 | char error_buf[100]; |
| 602 | polarssl_strerror( ret, error_buf, 100 ); |
| 603 | printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf ); |
| 604 | fflush( stdout ); |
| 605 | } |
| 606 | #endif |
| 607 | |
| 608 | if( client_fd != -1 ) |
| 609 | net_close( client_fd ); |
| 610 | |
| 611 | if( listen_fd != -1 ) |
| 612 | net_close( listen_fd ); |
| 613 | |
| 614 | #if defined(_WIN32) |
| 615 | printf( " Press Enter to exit this program.\n" ); |
| 616 | fflush( stdout ); getchar(); |
| 617 | #endif |
| 618 | |
| 619 | return( ret != 0 ); |
| 620 | } |
| 621 | |
| 622 | #endif /* POLARSSL_NET_C */ |