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 | |
| 69 | #define MAX_MSG_SIZE 18445 /* 2^14 + 2048 + 13 */ |
| 70 | |
| 71 | #define DFL_SERVER_ADDR "localhost" |
| 72 | #define DFL_SERVER_PORT 4433 |
| 73 | #define DFL_LISTEN_ADDR "localhost" |
| 74 | #define DFL_LISTEN_PORT 5556 |
| 75 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 76 | #define USAGE \ |
| 77 | "\n usage: udp_proxy param=<>...\n" \ |
| 78 | "\n acceptable parameters:\n" \ |
| 79 | " server_addr=%%d default: localhost\n" \ |
| 80 | " server_port=%%d default: 4433\n" \ |
| 81 | " listen_addr=%%d default: localhost\n" \ |
| 82 | " listen_port=%%d default: 4433\n" \ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame^] | 83 | "\n" \ |
| 84 | " duplicate=%%d default: 0 (no duplication)\n" \ |
| 85 | " duplicate 1 packet every N packet\n" \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 86 | "\n" |
| 87 | |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 88 | /* |
| 89 | * global options |
| 90 | */ |
| 91 | static struct options |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 92 | { |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 93 | const char *server_addr; /* address to forward packets to */ |
| 94 | int server_port; /* port to forward packets to */ |
| 95 | const char *listen_addr; /* address for accepting client connections */ |
| 96 | int listen_port; /* port for accepting client connections */ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame^] | 97 | |
| 98 | int duplicate; /* duplicate 1 in N packets (none if 0) */ |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 99 | } opt; |
| 100 | |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame^] | 101 | /* |
| 102 | * global variables |
| 103 | */ |
| 104 | static int dupl_cnt; |
| 105 | |
| 106 | /* Do not always start with the same state */ |
| 107 | static void randomize_counters( void ) |
| 108 | { |
| 109 | #if defined(POLARSSL_HAVE_TIME) |
| 110 | srand( time( NULL ) ); |
| 111 | #endif |
| 112 | |
| 113 | if( opt.duplicate != 0 ) |
| 114 | dupl_cnt = rand() % opt.duplicate; |
| 115 | } |
| 116 | |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 117 | static void exit_usage( const char *name, const char *value ) |
| 118 | { |
| 119 | if( value == NULL ) |
| 120 | printf( " unknown option: %s\n", name ); |
| 121 | else |
| 122 | printf( " option %s: illegal value: %s\n", name, value ); |
| 123 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 124 | printf( USAGE ); |
| 125 | exit( 1 ); |
| 126 | } |
| 127 | |
| 128 | static void get_options( int argc, char *argv[] ) |
| 129 | { |
| 130 | int i; |
| 131 | char *p, *q; |
| 132 | |
| 133 | opt.server_addr = DFL_SERVER_ADDR; |
| 134 | opt.server_port = DFL_SERVER_PORT; |
| 135 | opt.listen_addr = DFL_LISTEN_ADDR; |
| 136 | opt.listen_port = DFL_LISTEN_PORT; |
| 137 | |
| 138 | for( i = 1; i < argc; i++ ) |
| 139 | { |
| 140 | p = argv[i]; |
| 141 | if( ( q = strchr( p, '=' ) ) == NULL ) |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 142 | exit_usage( p, NULL ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 143 | *q++ = '\0'; |
| 144 | |
| 145 | if( strcmp( p, "server_addr" ) == 0 ) |
| 146 | opt.server_addr = q; |
| 147 | else if( strcmp( p, "server_port" ) == 0 ) |
| 148 | { |
| 149 | opt.server_port = atoi( q ); |
| 150 | if( opt.server_port < 1 || opt.server_port > 65535 ) |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 151 | exit_usage( p, q ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 152 | } |
| 153 | else if( strcmp( p, "listen_addr" ) == 0 ) |
| 154 | opt.listen_addr = q; |
| 155 | else if( strcmp( p, "listen_port" ) == 0 ) |
| 156 | { |
| 157 | opt.listen_port = atoi( q ); |
| 158 | if( opt.listen_port < 1 || opt.listen_port > 65535 ) |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 159 | exit_usage( p, q ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 160 | } |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame^] | 161 | else if( strcmp( p, "duplicate" ) == 0 ) |
| 162 | { |
| 163 | opt.duplicate = atoi( q ); |
| 164 | if( opt.duplicate < 0 || opt.duplicate > 10 ) |
| 165 | exit_usage( p, q ); |
| 166 | } |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 167 | else |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 168 | exit_usage( p, NULL ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | static const char *msg_type( unsigned char *msg, size_t len ) |
| 173 | { |
| 174 | if( len < 1 ) return( "Invalid" ); |
| 175 | switch( msg[0] ) |
| 176 | { |
| 177 | case SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" ); |
| 178 | case SSL_MSG_ALERT: return( "Alert" ); |
| 179 | case SSL_MSG_APPLICATION_DATA: return( "ApplicationData" ); |
| 180 | case SSL_MSG_HANDSHAKE: break; /* See below */ |
| 181 | default: return( "Unknown" ); |
| 182 | } |
| 183 | |
| 184 | if( len < 13 ) return( "Invalid handshake" ); |
| 185 | switch( msg[13] ) |
| 186 | { |
| 187 | case SSL_HS_HELLO_REQUEST: return( "HelloRequest" ); |
| 188 | case SSL_HS_CLIENT_HELLO: return( "ClientHello" ); |
| 189 | case SSL_HS_SERVER_HELLO: return( "ServerHello" ); |
| 190 | case SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" ); |
| 191 | case SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" ); |
| 192 | case SSL_HS_CERTIFICATE: return( "Certificate" ); |
| 193 | case SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" ); |
| 194 | case SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" ); |
| 195 | case SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" ); |
| 196 | case SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" ); |
| 197 | case SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" ); |
| 198 | case SSL_HS_FINISHED: return( "Finished" ); |
| 199 | default: return( "Unkown handshake" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | int handle_message( const char *way, int dst, int src ) |
| 204 | { |
| 205 | unsigned char buf[MAX_MSG_SIZE] = { 0 }; |
| 206 | int ret; |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame^] | 207 | unsigned len; |
| 208 | const char *type; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 209 | |
| 210 | if( ( ret = net_recv( &src, buf, sizeof( buf ) ) ) <= 0 ) |
| 211 | { |
| 212 | printf( " ! net_recv returned %d\n", ret ); |
| 213 | return( ret ); |
| 214 | } |
| 215 | |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 216 | len = ret; |
| 217 | type = msg_type( buf, len ); |
| 218 | printf( " > %s: %s (%u bytes)\n", way, type, len ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 219 | |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame^] | 220 | /* Don't duplicate Application Data, only handshake covered */ |
| 221 | // Don't duplicate CSS for now (TODO later) |
| 222 | if( opt.duplicate != 0 && |
| 223 | strcmp( type, "ChangeCipherSpec" ) != 0 && |
| 224 | strcmp( type, "ApplicationData" ) != 0 && |
| 225 | ++dupl_cnt == opt.duplicate ) |
| 226 | { |
| 227 | dupl_cnt = 0; |
| 228 | printf( " < %s: %s (%u bytes): duplicate\n", way, type, len ); |
| 229 | |
| 230 | if( ( ret = net_send( &dst, buf, len ) ) <= 0 ) |
| 231 | { |
| 232 | printf( " ! net_send returned %d\n", ret ); |
| 233 | return( ret ); |
| 234 | } |
| 235 | } |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 236 | |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 237 | printf( " < %s: %s (%u bytes): forwarded\n", way, type, len ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 238 | if( ( ret = net_send( &dst, buf, len ) ) <= 0 ) |
| 239 | { |
| 240 | printf( " ! net_send returned %d\n", ret ); |
| 241 | return( ret ); |
| 242 | } |
| 243 | |
| 244 | fflush( stdout ); |
| 245 | return( 0 ); |
| 246 | } |
| 247 | |
| 248 | int main( int argc, char *argv[] ) |
| 249 | { |
| 250 | int ret; |
| 251 | |
| 252 | int listen_fd = -1; |
| 253 | int client_fd = -1; |
| 254 | int server_fd = -1; |
| 255 | |
| 256 | int nb_fds; |
| 257 | fd_set read_fds; |
| 258 | |
| 259 | get_options( argc, argv ); |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame^] | 260 | randomize_counters(); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 261 | |
| 262 | /* |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 263 | * 0. "Connect" to the server |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 264 | */ |
| 265 | printf( " . Connect to server on UDP/%s/%d ...", |
| 266 | opt.server_addr, opt.server_port ); |
| 267 | fflush( stdout ); |
| 268 | |
| 269 | if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port, |
| 270 | NET_PROTO_UDP ) ) != 0 ) |
| 271 | { |
| 272 | printf( " failed\n ! net_connect returned %d\n\n", ret ); |
| 273 | goto exit; |
| 274 | } |
| 275 | |
| 276 | printf( " ok\n" ); |
| 277 | |
| 278 | /* |
| 279 | * 1. Setup the "listening" UDP socket |
| 280 | */ |
| 281 | printf( " . Bind on UDP/%s/%d ...", |
| 282 | opt.listen_addr, opt.listen_port ); |
| 283 | fflush( stdout ); |
| 284 | |
| 285 | if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port, |
| 286 | NET_PROTO_UDP ) ) != 0 ) |
| 287 | { |
| 288 | printf( " failed\n ! net_bind returned %d\n\n", ret ); |
| 289 | goto exit; |
| 290 | } |
| 291 | |
| 292 | printf( " ok\n" ); |
| 293 | |
| 294 | /* |
| 295 | * 2. Wait until a client connects |
| 296 | */ |
| 297 | printf( " . Waiting for a remote connection ..." ); |
| 298 | fflush( stdout ); |
| 299 | |
| 300 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) |
| 301 | { |
| 302 | printf( " failed\n ! net_accept returned %d\n\n", ret ); |
| 303 | goto exit; |
| 304 | } |
| 305 | |
| 306 | printf( " ok\n" ); |
| 307 | fflush( stdout ); |
| 308 | |
| 309 | /* |
| 310 | * 3. Forward packets forever (kill the process to terminate it) |
| 311 | */ |
| 312 | nb_fds = ( client_fd > server_fd ? client_fd : server_fd ) + 1; |
| 313 | |
| 314 | while( 1 ) |
| 315 | { |
| 316 | FD_ZERO( &read_fds ); |
| 317 | FD_SET( server_fd, &read_fds ); |
| 318 | FD_SET( client_fd, &read_fds ); |
| 319 | |
| 320 | if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 ) |
| 321 | { |
| 322 | perror( "select" ); |
| 323 | goto exit; |
| 324 | } |
| 325 | |
| 326 | if( FD_ISSET( client_fd, &read_fds ) ) |
| 327 | { |
| 328 | if( ( ret = handle_message( "c2s", server_fd, client_fd ) ) != 0 ) |
| 329 | goto exit; |
| 330 | } |
| 331 | |
| 332 | if( FD_ISSET( server_fd, &read_fds ) ) |
| 333 | { |
| 334 | if( ( ret = handle_message( "s2c", client_fd, server_fd ) ) != 0 ) |
| 335 | goto exit; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | exit: |
| 340 | |
| 341 | #ifdef POLARSSL_ERROR_C |
| 342 | if( ret != 0 ) |
| 343 | { |
| 344 | char error_buf[100]; |
| 345 | polarssl_strerror( ret, error_buf, 100 ); |
| 346 | printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf ); |
| 347 | fflush( stdout ); |
| 348 | } |
| 349 | #endif |
| 350 | |
| 351 | if( client_fd != -1 ) |
| 352 | net_close( client_fd ); |
| 353 | |
| 354 | if( listen_fd != -1 ) |
| 355 | net_close( listen_fd ); |
| 356 | |
| 357 | #if defined(_WIN32) |
| 358 | printf( " Press Enter to exit this program.\n" ); |
| 359 | fflush( stdout ); getchar(); |
| 360 | #endif |
| 361 | |
| 362 | return( ret != 0 ); |
| 363 | } |
| 364 | |
| 365 | #endif /* POLARSSL_NET_C */ |