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