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