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