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 | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 72 | #define MAX_MSG_SIZE 4096 /* Reasonable max size for our tests */ |
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" \ |
| 82 | " server_addr=%%d default: localhost\n" \ |
| 83 | " server_port=%%d default: 4433\n" \ |
| 84 | " listen_addr=%%d default: localhost\n" \ |
| 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 | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 91 | " delay_ccs=%%d default: 0 (don't delay ChangeCipherSuite)\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 | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 96 | " bad_ad=%%d default: 0 (don't add bad ApplicationData)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame^] | 97 | "\n" \ |
| 98 | " seed=%%d default: (use current time)\n" \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 99 | "\n" |
| 100 | |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 101 | /* |
| 102 | * global options |
| 103 | */ |
| 104 | static struct options |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 105 | { |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 106 | const char *server_addr; /* address to forward packets to */ |
| 107 | int server_port; /* port to forward packets to */ |
| 108 | const char *listen_addr; /* address for accepting client connections */ |
| 109 | int listen_port; /* port for accepting client connections */ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 110 | |
| 111 | int duplicate; /* duplicate 1 in N packets (none if 0) */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 112 | int delay; /* delay 1 packet in N (none if 0) */ |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 113 | int delay_ccs; /* delay ChangeCipherSpec */ |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 114 | int drop; /* drop 1 packet in N (none if 0) */ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 115 | int mtu; /* drop packets larger than this */ |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 116 | int bad_ad; /* inject corrupted ApplicationData record */ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame^] | 117 | |
| 118 | unsigned int seed; /* seed for "random" events */ |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 119 | } opt; |
| 120 | |
| 121 | static void exit_usage( const char *name, const char *value ) |
| 122 | { |
| 123 | if( value == NULL ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 124 | printf( " unknown option or missing value: %s\n", name ); |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 125 | else |
| 126 | printf( " option %s: illegal value: %s\n", name, value ); |
| 127 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 128 | printf( USAGE ); |
| 129 | exit( 1 ); |
| 130 | } |
| 131 | |
| 132 | static void get_options( int argc, char *argv[] ) |
| 133 | { |
| 134 | int i; |
| 135 | char *p, *q; |
| 136 | |
| 137 | opt.server_addr = DFL_SERVER_ADDR; |
| 138 | opt.server_port = DFL_SERVER_PORT; |
| 139 | opt.listen_addr = DFL_LISTEN_ADDR; |
| 140 | opt.listen_port = DFL_LISTEN_PORT; |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 141 | /* Other members default to 0 */ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 142 | |
| 143 | for( i = 1; i < argc; i++ ) |
| 144 | { |
| 145 | p = argv[i]; |
| 146 | if( ( q = strchr( p, '=' ) ) == NULL ) |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 147 | exit_usage( p, NULL ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 148 | *q++ = '\0'; |
| 149 | |
| 150 | if( strcmp( p, "server_addr" ) == 0 ) |
| 151 | opt.server_addr = q; |
| 152 | else if( strcmp( p, "server_port" ) == 0 ) |
| 153 | { |
| 154 | opt.server_port = atoi( q ); |
| 155 | if( opt.server_port < 1 || opt.server_port > 65535 ) |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 156 | exit_usage( p, q ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 157 | } |
| 158 | else if( strcmp( p, "listen_addr" ) == 0 ) |
| 159 | opt.listen_addr = q; |
| 160 | else if( strcmp( p, "listen_port" ) == 0 ) |
| 161 | { |
| 162 | opt.listen_port = atoi( q ); |
| 163 | if( opt.listen_port < 1 || opt.listen_port > 65535 ) |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 164 | exit_usage( p, q ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 165 | } |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 166 | else if( strcmp( p, "duplicate" ) == 0 ) |
| 167 | { |
| 168 | opt.duplicate = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame^] | 169 | if( opt.duplicate < 0 || opt.duplicate > 20 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 170 | exit_usage( p, q ); |
| 171 | } |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 172 | else if( strcmp( p, "delay" ) == 0 ) |
| 173 | { |
| 174 | opt.delay = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame^] | 175 | if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 176 | exit_usage( p, q ); |
| 177 | } |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 178 | else if( strcmp( p, "delay_ccs" ) == 0 ) |
| 179 | { |
| 180 | opt.delay_ccs = atoi( q ); |
| 181 | if( opt.delay_ccs < 0 || opt.delay_ccs > 1 ) |
| 182 | exit_usage( p, q ); |
| 183 | } |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 184 | else if( strcmp( p, "drop" ) == 0 ) |
| 185 | { |
| 186 | opt.drop = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame^] | 187 | if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 ) |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 188 | exit_usage( p, q ); |
| 189 | } |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 190 | else if( strcmp( p, "mtu" ) == 0 ) |
| 191 | { |
| 192 | opt.mtu = atoi( q ); |
| 193 | if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE ) |
| 194 | exit_usage( p, q ); |
| 195 | } |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 196 | else if( strcmp( p, "bad_ad" ) == 0 ) |
| 197 | { |
| 198 | opt.bad_ad = atoi( q ); |
| 199 | if( opt.bad_ad < 0 || opt.bad_ad > 1 ) |
| 200 | exit_usage( p, q ); |
| 201 | } |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame^] | 202 | else if( strcmp( p, "seed" ) == 0 ) |
| 203 | { |
| 204 | opt.seed = atoi( q ); |
| 205 | if( opt.seed == 0 ) |
| 206 | exit_usage( p, q ); |
| 207 | } |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 208 | else |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 209 | exit_usage( p, NULL ); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | static const char *msg_type( unsigned char *msg, size_t len ) |
| 214 | { |
| 215 | if( len < 1 ) return( "Invalid" ); |
| 216 | switch( msg[0] ) |
| 217 | { |
| 218 | case SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" ); |
| 219 | case SSL_MSG_ALERT: return( "Alert" ); |
| 220 | case SSL_MSG_APPLICATION_DATA: return( "ApplicationData" ); |
| 221 | case SSL_MSG_HANDSHAKE: break; /* See below */ |
| 222 | default: return( "Unknown" ); |
| 223 | } |
| 224 | |
| 225 | if( len < 13 ) return( "Invalid handshake" ); |
| 226 | switch( msg[13] ) |
| 227 | { |
| 228 | case SSL_HS_HELLO_REQUEST: return( "HelloRequest" ); |
| 229 | case SSL_HS_CLIENT_HELLO: return( "ClientHello" ); |
| 230 | case SSL_HS_SERVER_HELLO: return( "ServerHello" ); |
| 231 | case SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" ); |
| 232 | case SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" ); |
| 233 | case SSL_HS_CERTIFICATE: return( "Certificate" ); |
| 234 | case SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" ); |
| 235 | case SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" ); |
| 236 | case SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" ); |
| 237 | case SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" ); |
| 238 | case SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" ); |
| 239 | case SSL_HS_FINISHED: return( "Finished" ); |
Manuel Pégourié-Gonnard | bc010a0 | 2014-09-20 12:40:51 +0200 | [diff] [blame] | 240 | default: return( "Unknown handshake" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 244 | /* Return elapsed time in milliseconds since the first call */ |
| 245 | static unsigned long ellapsed_time( void ) |
| 246 | { |
| 247 | #if defined(_WIN32) |
| 248 | return( 0 ); |
| 249 | #else |
| 250 | static struct timeval ref = { 0, 0 }; |
| 251 | struct timeval now; |
| 252 | |
| 253 | if( ref.tv_sec == 0 && ref.tv_usec == 0 ) |
| 254 | { |
| 255 | gettimeofday( &ref, NULL ); |
| 256 | return( 0 ); |
| 257 | } |
| 258 | |
| 259 | gettimeofday( &now, NULL ); |
| 260 | return( 1000 * ( now.tv_sec - ref.tv_sec ) |
| 261 | + ( now.tv_usec - ref.tv_usec ) / 1000 ); |
| 262 | #endif |
| 263 | } |
| 264 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 265 | typedef struct |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 266 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 267 | void *dst; |
| 268 | const char *way; |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 269 | const char *type; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 270 | unsigned len; |
| 271 | unsigned char buf[MAX_MSG_SIZE]; |
| 272 | } packet; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 273 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 274 | /* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */ |
| 275 | void print_packet( const packet *p, const char *why ) |
| 276 | { |
| 277 | if( why == NULL ) |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 278 | printf( " %05lu %s %s (%u bytes)\n", |
| 279 | ellapsed_time(), p->way, p->type, p->len ); |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 280 | else |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 281 | printf( " %s %s (%u bytes): %s\n", |
| 282 | p->way, p->type, p->len, why ); |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 283 | fflush( stdout ); |
| 284 | } |
| 285 | |
| 286 | int send_packet( const packet *p, const char *why ) |
| 287 | { |
| 288 | int ret; |
| 289 | |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 290 | /* insert corrupted ApplicationData record? */ |
| 291 | if( opt.bad_ad && |
| 292 | strcmp( p->type, "ApplicationData" ) == 0 ) |
| 293 | { |
| 294 | unsigned char buf[MAX_MSG_SIZE]; |
| 295 | memcpy( buf, p->buf, p->len ); |
| 296 | ++buf[p->len - 1]; |
| 297 | |
| 298 | print_packet( p, "corrupted" ); |
| 299 | if( ( ret = net_send( p->dst, buf, p->len ) ) <= 0 ) |
| 300 | { |
| 301 | printf( " ! net_send returned %d\n", ret ); |
| 302 | return( ret ); |
| 303 | } |
| 304 | } |
| 305 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 306 | print_packet( p, why ); |
| 307 | if( ( ret = net_send( p->dst, p->buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 308 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 309 | printf( " ! net_send returned %d\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 310 | return( ret ); |
| 311 | } |
| 312 | |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 313 | /* Don't duplicate Application Data, only handshake covered */ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 314 | if( opt.duplicate != 0 && |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 315 | strcmp( p->type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame^] | 316 | rand() % opt.duplicate == 0 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 317 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 318 | print_packet( p, "duplicated" ); |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 319 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 320 | if( ( ret = net_send( p->dst, p->buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 321 | { |
| 322 | printf( " ! net_send returned %d\n", ret ); |
| 323 | return( ret ); |
| 324 | } |
| 325 | } |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 326 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 327 | return( 0 ); |
| 328 | } |
| 329 | |
| 330 | int handle_message( const char *way, int dst, int src ) |
| 331 | { |
| 332 | int ret; |
| 333 | packet cur; |
| 334 | static packet prev; |
| 335 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 336 | /* receive packet */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 337 | 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] | 338 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 339 | printf( " ! net_recv returned %d\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 340 | return( ret ); |
| 341 | } |
| 342 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 343 | cur.len = ret; |
| 344 | cur.type = msg_type( cur.buf, cur.len ); |
| 345 | cur.way = way; |
| 346 | cur.dst = &dst; |
| 347 | print_packet( &cur, NULL ); |
| 348 | |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 349 | /* do we want to drop, delay, or forward it? */ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 350 | if( ( opt.mtu != 0 && |
| 351 | cur.len > (unsigned) opt.mtu ) || |
| 352 | ( opt.drop != 0 && |
| 353 | strcmp( cur.type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | bc010a0 | 2014-09-20 12:40:51 +0200 | [diff] [blame] | 354 | strcmp( cur.type, "NewSessionTicket" ) != 0 && // temporary |
| 355 | strcmp( cur.type, "ChangeCipherSpec" ) != 0 && // temporary |
| 356 | strcmp( cur.type, "Unknown handshake" ) != 0 && // temporary |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame^] | 357 | rand() % opt.drop == 0 ) ) |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 358 | { |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame^] | 359 | ; /* Nothing to do */ |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 360 | } |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 361 | else if( ( opt.delay_ccs == 1 && |
| 362 | strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) || |
| 363 | ( opt.delay != 0 && |
| 364 | strcmp( cur.type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | bc010a0 | 2014-09-20 12:40:51 +0200 | [diff] [blame] | 365 | strcmp( cur.type, "NewSessionTicket" ) != 0 && // temporary |
| 366 | strcmp( cur.type, "ChangeCipherSpec" ) != 0 && // temporary |
| 367 | strcmp( cur.type, "Unknown handshake" ) != 0 && // temporary |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame^] | 368 | rand() % opt.delay == 0 ) ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 369 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 370 | memcpy( &prev, &cur, sizeof( packet ) ); |
| 371 | } |
| 372 | else |
| 373 | { |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 374 | /* forward and possibly duplicate */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 375 | if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 ) |
| 376 | return( ret ); |
| 377 | |
| 378 | /* send previously delayed message if any */ |
| 379 | if( prev.dst != NULL ) |
| 380 | { |
| 381 | ret = send_packet( &prev, "delayed" ); |
| 382 | memset( &prev, 0, sizeof( packet ) ); |
| 383 | if( ret != 0 ) |
| 384 | return( ret ); |
| 385 | } |
| 386 | } |
| 387 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 388 | return( 0 ); |
| 389 | } |
| 390 | |
| 391 | int main( int argc, char *argv[] ) |
| 392 | { |
| 393 | int ret; |
| 394 | |
| 395 | int listen_fd = -1; |
| 396 | int client_fd = -1; |
| 397 | int server_fd = -1; |
| 398 | |
| 399 | int nb_fds; |
| 400 | fd_set read_fds; |
| 401 | |
| 402 | get_options( argc, argv ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame^] | 403 | |
| 404 | /* |
| 405 | * Decisions to drop/delay/duplicate packets are pseudo-random: dropping |
| 406 | * exactly 1 in N packets would lead to problems when a flight has exactly |
| 407 | * N packets: the same packet would be dropped on every resend. |
| 408 | * |
| 409 | * In order to be able to reproduce problems reliably, the seed may be |
| 410 | * specified explicitly. |
| 411 | */ |
| 412 | if( opt.seed == 0 ) |
| 413 | { |
| 414 | opt.seed = time( NULL ); |
| 415 | printf( " . Pseudo-random seed: %u\n", opt.seed ); |
| 416 | } |
| 417 | |
| 418 | srand( opt.seed ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 419 | |
| 420 | /* |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 421 | * 0. "Connect" to the server |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 422 | */ |
| 423 | printf( " . Connect to server on UDP/%s/%d ...", |
| 424 | opt.server_addr, opt.server_port ); |
| 425 | fflush( stdout ); |
| 426 | |
| 427 | if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port, |
| 428 | NET_PROTO_UDP ) ) != 0 ) |
| 429 | { |
| 430 | printf( " failed\n ! net_connect returned %d\n\n", ret ); |
| 431 | goto exit; |
| 432 | } |
| 433 | |
| 434 | printf( " ok\n" ); |
| 435 | |
| 436 | /* |
| 437 | * 1. Setup the "listening" UDP socket |
| 438 | */ |
| 439 | printf( " . Bind on UDP/%s/%d ...", |
| 440 | opt.listen_addr, opt.listen_port ); |
| 441 | fflush( stdout ); |
| 442 | |
| 443 | if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port, |
| 444 | NET_PROTO_UDP ) ) != 0 ) |
| 445 | { |
| 446 | printf( " failed\n ! net_bind returned %d\n\n", ret ); |
| 447 | goto exit; |
| 448 | } |
| 449 | |
| 450 | printf( " ok\n" ); |
| 451 | |
| 452 | /* |
| 453 | * 2. Wait until a client connects |
| 454 | */ |
| 455 | printf( " . Waiting for a remote connection ..." ); |
| 456 | fflush( stdout ); |
| 457 | |
| 458 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) |
| 459 | { |
| 460 | printf( " failed\n ! net_accept returned %d\n\n", ret ); |
| 461 | goto exit; |
| 462 | } |
| 463 | |
| 464 | printf( " ok\n" ); |
| 465 | fflush( stdout ); |
| 466 | |
| 467 | /* |
| 468 | * 3. Forward packets forever (kill the process to terminate it) |
| 469 | */ |
| 470 | nb_fds = ( client_fd > server_fd ? client_fd : server_fd ) + 1; |
| 471 | |
| 472 | while( 1 ) |
| 473 | { |
| 474 | FD_ZERO( &read_fds ); |
| 475 | FD_SET( server_fd, &read_fds ); |
| 476 | FD_SET( client_fd, &read_fds ); |
| 477 | |
| 478 | if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 ) |
| 479 | { |
| 480 | perror( "select" ); |
| 481 | goto exit; |
| 482 | } |
| 483 | |
| 484 | if( FD_ISSET( client_fd, &read_fds ) ) |
| 485 | { |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 486 | if( ( ret = handle_message( "S <- C", |
| 487 | server_fd, client_fd ) ) != 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 488 | goto exit; |
| 489 | } |
| 490 | |
| 491 | if( FD_ISSET( server_fd, &read_fds ) ) |
| 492 | { |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 493 | if( ( ret = handle_message( "S -> C", |
| 494 | client_fd, server_fd ) ) != 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 495 | goto exit; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | exit: |
| 500 | |
| 501 | #ifdef POLARSSL_ERROR_C |
| 502 | if( ret != 0 ) |
| 503 | { |
| 504 | char error_buf[100]; |
| 505 | polarssl_strerror( ret, error_buf, 100 ); |
| 506 | printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf ); |
| 507 | fflush( stdout ); |
| 508 | } |
| 509 | #endif |
| 510 | |
| 511 | if( client_fd != -1 ) |
| 512 | net_close( client_fd ); |
| 513 | |
| 514 | if( listen_fd != -1 ) |
| 515 | net_close( listen_fd ); |
| 516 | |
| 517 | #if defined(_WIN32) |
| 518 | printf( " Press Enter to exit this program.\n" ); |
| 519 | fflush( stdout ); getchar(); |
| 520 | #endif |
| 521 | |
| 522 | return( ret != 0 ); |
| 523 | } |
| 524 | |
| 525 | #endif /* POLARSSL_NET_C */ |