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" \ |
| 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 | ae8d239 | 2014-09-23 10:01:06 +0200 | [diff] [blame] | 91 | " delay_ccs=%%d 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 | 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 | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 267 | int dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 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; |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 289 | int dst = p->dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 290 | |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 291 | /* insert corrupted ApplicationData record? */ |
| 292 | if( opt.bad_ad && |
| 293 | strcmp( p->type, "ApplicationData" ) == 0 ) |
| 294 | { |
| 295 | unsigned char buf[MAX_MSG_SIZE]; |
| 296 | memcpy( buf, p->buf, p->len ); |
| 297 | ++buf[p->len - 1]; |
| 298 | |
| 299 | print_packet( p, "corrupted" ); |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 300 | if( ( ret = net_send( &dst, buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 301 | { |
| 302 | printf( " ! net_send returned %d\n", ret ); |
| 303 | return( ret ); |
| 304 | } |
| 305 | } |
| 306 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 307 | print_packet( p, why ); |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 308 | if( ( ret = net_send( &dst, p->buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 309 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 310 | printf( " ! net_send returned %d\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 311 | return( ret ); |
| 312 | } |
| 313 | |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 314 | /* Don't duplicate Application Data, only handshake covered */ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 315 | if( opt.duplicate != 0 && |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 316 | strcmp( p->type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 317 | rand() % opt.duplicate == 0 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 318 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 319 | print_packet( p, "duplicated" ); |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 320 | |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 321 | if( ( ret = net_send( &dst, p->buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 322 | { |
| 323 | printf( " ! net_send returned %d\n", ret ); |
| 324 | return( ret ); |
| 325 | } |
| 326 | } |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 327 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 328 | return( 0 ); |
| 329 | } |
| 330 | |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 331 | static packet prev; |
| 332 | |
| 333 | void clear_pending( void ) |
| 334 | { |
| 335 | memset( &prev, 0, sizeof( packet ) ); |
| 336 | } |
| 337 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 338 | int handle_message( const char *way, int dst, int src ) |
| 339 | { |
| 340 | int ret; |
| 341 | packet cur; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 342 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 343 | /* receive packet */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 344 | 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] | 345 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 346 | printf( " ! net_recv returned %d\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 347 | return( ret ); |
| 348 | } |
| 349 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 350 | cur.len = ret; |
| 351 | cur.type = msg_type( cur.buf, cur.len ); |
| 352 | cur.way = way; |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 353 | cur.dst = dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 354 | print_packet( &cur, NULL ); |
| 355 | |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 356 | /* do we want to drop, delay, or forward it? */ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 357 | if( ( opt.mtu != 0 && |
| 358 | cur.len > (unsigned) opt.mtu ) || |
| 359 | ( opt.drop != 0 && |
| 360 | strcmp( cur.type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 361 | rand() % opt.drop == 0 ) ) |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 362 | { |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 363 | ; /* Nothing to do */ |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 364 | } |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 365 | else if( ( opt.delay_ccs == 1 && |
| 366 | strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) || |
| 367 | ( opt.delay != 0 && |
| 368 | strcmp( cur.type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 369 | prev.dst == 0 && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 370 | rand() % opt.delay == 0 ) ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 371 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 372 | memcpy( &prev, &cur, sizeof( packet ) ); |
| 373 | } |
| 374 | else |
| 375 | { |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 376 | /* forward and possibly duplicate */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 377 | if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 ) |
| 378 | return( ret ); |
| 379 | |
| 380 | /* send previously delayed message if any */ |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 381 | if( prev.dst != 0 ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 382 | { |
| 383 | ret = send_packet( &prev, "delayed" ); |
| 384 | memset( &prev, 0, sizeof( packet ) ); |
| 385 | if( ret != 0 ) |
| 386 | return( ret ); |
| 387 | } |
| 388 | } |
| 389 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 390 | return( 0 ); |
| 391 | } |
| 392 | |
| 393 | int main( int argc, char *argv[] ) |
| 394 | { |
| 395 | int ret; |
| 396 | |
| 397 | int listen_fd = -1; |
| 398 | int client_fd = -1; |
| 399 | int server_fd = -1; |
| 400 | |
| 401 | int nb_fds; |
| 402 | fd_set read_fds; |
| 403 | |
| 404 | get_options( argc, argv ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 405 | |
| 406 | /* |
| 407 | * Decisions to drop/delay/duplicate packets are pseudo-random: dropping |
| 408 | * exactly 1 in N packets would lead to problems when a flight has exactly |
| 409 | * N packets: the same packet would be dropped on every resend. |
| 410 | * |
| 411 | * In order to be able to reproduce problems reliably, the seed may be |
| 412 | * specified explicitly. |
| 413 | */ |
| 414 | if( opt.seed == 0 ) |
| 415 | { |
| 416 | opt.seed = time( NULL ); |
| 417 | printf( " . Pseudo-random seed: %u\n", opt.seed ); |
| 418 | } |
| 419 | |
| 420 | srand( opt.seed ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 421 | |
| 422 | /* |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 423 | * 0. "Connect" to the server |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 424 | */ |
| 425 | printf( " . Connect to server on UDP/%s/%d ...", |
| 426 | opt.server_addr, opt.server_port ); |
| 427 | fflush( stdout ); |
| 428 | |
| 429 | if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port, |
| 430 | NET_PROTO_UDP ) ) != 0 ) |
| 431 | { |
| 432 | printf( " failed\n ! net_connect returned %d\n\n", ret ); |
| 433 | goto exit; |
| 434 | } |
| 435 | |
| 436 | printf( " ok\n" ); |
| 437 | |
| 438 | /* |
| 439 | * 1. Setup the "listening" UDP socket |
| 440 | */ |
| 441 | printf( " . Bind on UDP/%s/%d ...", |
| 442 | opt.listen_addr, opt.listen_port ); |
| 443 | fflush( stdout ); |
| 444 | |
| 445 | if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port, |
| 446 | NET_PROTO_UDP ) ) != 0 ) |
| 447 | { |
| 448 | printf( " failed\n ! net_bind returned %d\n\n", ret ); |
| 449 | goto exit; |
| 450 | } |
| 451 | |
| 452 | printf( " ok\n" ); |
| 453 | |
| 454 | /* |
| 455 | * 2. Wait until a client connects |
| 456 | */ |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 457 | accept: |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 458 | printf( " . Waiting for a remote connection ..." ); |
| 459 | fflush( stdout ); |
| 460 | |
| 461 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) |
| 462 | { |
| 463 | printf( " failed\n ! net_accept returned %d\n\n", ret ); |
| 464 | goto exit; |
| 465 | } |
| 466 | |
| 467 | printf( " ok\n" ); |
| 468 | fflush( stdout ); |
| 469 | |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 470 | printf( " . Re-bind on UDP/%s/%d ...", |
| 471 | opt.listen_addr, opt.listen_port ); |
| 472 | fflush( stdout ); |
| 473 | |
| 474 | if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port, |
| 475 | NET_PROTO_UDP ) ) != 0 ) |
| 476 | { |
| 477 | printf( " failed\n ! net_bind returned %d\n\n", ret ); |
| 478 | goto exit; |
| 479 | } |
| 480 | |
| 481 | printf( " ok\n" ); |
| 482 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 483 | /* |
| 484 | * 3. Forward packets forever (kill the process to terminate it) |
| 485 | */ |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 486 | nb_fds = client_fd; |
| 487 | if( nb_fds < server_fd ) |
| 488 | nb_fds = server_fd; |
| 489 | if( nb_fds < listen_fd ) |
| 490 | nb_fds = listen_fd; |
| 491 | ++nb_fds; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 492 | |
| 493 | while( 1 ) |
| 494 | { |
| 495 | FD_ZERO( &read_fds ); |
| 496 | FD_SET( server_fd, &read_fds ); |
| 497 | FD_SET( client_fd, &read_fds ); |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 498 | FD_SET( listen_fd, &read_fds ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 499 | |
| 500 | if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 ) |
| 501 | { |
| 502 | perror( "select" ); |
| 503 | goto exit; |
| 504 | } |
| 505 | |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 506 | if( FD_ISSET( listen_fd, &read_fds ) ) |
| 507 | { |
| 508 | clear_pending(); |
| 509 | goto accept; |
| 510 | } |
| 511 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 512 | if( FD_ISSET( client_fd, &read_fds ) ) |
| 513 | { |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 514 | if( ( ret = handle_message( "S <- C", |
| 515 | server_fd, client_fd ) ) != 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 516 | goto exit; |
| 517 | } |
| 518 | |
| 519 | if( FD_ISSET( server_fd, &read_fds ) ) |
| 520 | { |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 521 | if( ( ret = handle_message( "S -> C", |
| 522 | client_fd, server_fd ) ) != 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 523 | goto exit; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | exit: |
| 528 | |
| 529 | #ifdef POLARSSL_ERROR_C |
| 530 | if( ret != 0 ) |
| 531 | { |
| 532 | char error_buf[100]; |
| 533 | polarssl_strerror( ret, error_buf, 100 ); |
| 534 | printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf ); |
| 535 | fflush( stdout ); |
| 536 | } |
| 537 | #endif |
| 538 | |
| 539 | if( client_fd != -1 ) |
| 540 | net_close( client_fd ); |
| 541 | |
| 542 | if( listen_fd != -1 ) |
| 543 | net_close( listen_fd ); |
| 544 | |
| 545 | #if defined(_WIN32) |
| 546 | printf( " Press Enter to exit this program.\n" ); |
| 547 | fflush( stdout ); getchar(); |
| 548 | #endif |
| 549 | |
| 550 | return( ret != 0 ); |
| 551 | } |
| 552 | |
| 553 | #endif /* POLARSSL_NET_C */ |