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