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 | * |
Manuel Pégourié-Gonnard | 3d2c4b7 | 2015-01-29 11:33:59 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #if !defined(POLARSSL_CONFIG_FILE) |
| 24 | #include "polarssl/config.h" |
| 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
| 28 | |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 29 | #if defined(POLARSSL_PLATFORM_C) |
| 30 | #include "polarssl/platform.h" |
| 31 | #else |
| 32 | #define polarssl_printf printf |
| 33 | #endif |
| 34 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 35 | #if !defined(POLARSSL_NET_C) |
| 36 | #include <stdio.h> |
| 37 | int main( void ) |
| 38 | { |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 39 | polarssl_printf( "POLARSSL_NET_C not defined.\n" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 40 | return( 0 ); |
| 41 | } |
| 42 | #else |
| 43 | |
| 44 | #include "polarssl/net.h" |
| 45 | #include "polarssl/error.h" |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 46 | #include "polarssl/ssl.h" |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 47 | |
| 48 | #include <stdio.h> |
| 49 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 50 | #include <time.h> |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 51 | |
| 52 | /* For select() */ |
| 53 | #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \ |
| 54 | !defined(EFI32) |
| 55 | #include <winsock2.h> |
| 56 | #include <windows.h> |
| 57 | #if defined(_MSC_VER) |
| 58 | #if defined(_WIN32_WCE) |
| 59 | #pragma comment( lib, "ws2.lib" ) |
| 60 | #else |
| 61 | #pragma comment( lib, "ws2_32.lib" ) |
| 62 | #endif |
| 63 | #endif /* _MSC_VER */ |
| 64 | #else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ |
| 65 | #include <sys/time.h> |
| 66 | #include <sys/types.h> |
| 67 | #include <unistd.h> |
| 68 | #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ |
| 69 | |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 70 | /* For gettimeofday() */ |
| 71 | #if !defined(_WIN32) |
| 72 | #include <sys/time.h> |
| 73 | #endif |
| 74 | |
Manuel Pégourié-Gonnard | b46780e | 2014-09-23 12:17:30 +0200 | [diff] [blame] | 75 | #define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 76 | |
| 77 | #define DFL_SERVER_ADDR "localhost" |
| 78 | #define DFL_SERVER_PORT 4433 |
| 79 | #define DFL_LISTEN_ADDR "localhost" |
| 80 | #define DFL_LISTEN_PORT 5556 |
| 81 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 82 | #define USAGE \ |
| 83 | "\n usage: udp_proxy param=<>...\n" \ |
| 84 | "\n acceptable parameters:\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 85 | " server_addr=%%s default: localhost\n" \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 86 | " server_port=%%d default: 4433\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 87 | " listen_addr=%%s default: localhost\n" \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 88 | " listen_port=%%d default: 4433\n" \ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 89 | "\n" \ |
| 90 | " duplicate=%%d default: 0 (no duplication)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 91 | " duplicate about 1:N packets randomly\n" \ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 92 | " delay=%%d default: 0 (no delayed packets)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 93 | " delay about 1:N packets randomly\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 94 | " delay_ccs=0/1 default: 0 (don't delay ChangeCipherSpec)\n" \ |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 95 | " drop=%%d default: 0 (no dropped packets)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 96 | " drop about 1:N packets randomly\n" \ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 97 | " mtu=%%d default: 0 (unlimited)\n" \ |
| 98 | " drop packets larger than N bytes\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 99 | " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \ |
| 100 | " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \ |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 101 | " protect_len=%%d default: (don't protect packets of this size)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 102 | "\n" \ |
| 103 | " seed=%%d default: (use current time)\n" \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 104 | "\n" |
| 105 | |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 106 | /* |
| 107 | * global options |
| 108 | */ |
| 109 | static struct options |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 110 | { |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 111 | const char *server_addr; /* address to forward packets to */ |
| 112 | int server_port; /* port to forward packets to */ |
| 113 | const char *listen_addr; /* address for accepting client connections */ |
| 114 | int listen_port; /* port for accepting client connections */ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 115 | |
| 116 | int duplicate; /* duplicate 1 in N packets (none if 0) */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 117 | int delay; /* delay 1 packet in N (none if 0) */ |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 118 | int delay_ccs; /* delay ChangeCipherSpec */ |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 119 | int drop; /* drop 1 packet in N (none if 0) */ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 120 | int mtu; /* drop packets larger than this */ |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 121 | int bad_ad; /* inject corrupted ApplicationData record */ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 122 | int protect_hvr; /* never drop or delay HelloVerifyRequest */ |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 123 | int protect_len; /* never drop/delay packet of the given size*/ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 124 | |
| 125 | unsigned int seed; /* seed for "random" events */ |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 126 | } opt; |
| 127 | |
| 128 | static void exit_usage( const char *name, const char *value ) |
| 129 | { |
| 130 | if( value == NULL ) |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 131 | polarssl_printf( " unknown option or missing value: %s\n", name ); |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 132 | else |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 133 | polarssl_printf( " option %s: illegal value: %s\n", name, value ); |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 134 | |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 135 | polarssl_printf( USAGE ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 136 | exit( 1 ); |
| 137 | } |
| 138 | |
| 139 | static void get_options( int argc, char *argv[] ) |
| 140 | { |
| 141 | int i; |
| 142 | char *p, *q; |
| 143 | |
| 144 | opt.server_addr = DFL_SERVER_ADDR; |
| 145 | opt.server_port = DFL_SERVER_PORT; |
| 146 | opt.listen_addr = DFL_LISTEN_ADDR; |
| 147 | opt.listen_port = DFL_LISTEN_PORT; |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 148 | /* Other members default to 0 */ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 149 | |
| 150 | for( i = 1; i < argc; i++ ) |
| 151 | { |
| 152 | p = argv[i]; |
| 153 | if( ( q = strchr( p, '=' ) ) == NULL ) |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 154 | exit_usage( p, NULL ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 155 | *q++ = '\0'; |
| 156 | |
| 157 | if( strcmp( p, "server_addr" ) == 0 ) |
| 158 | opt.server_addr = q; |
| 159 | else if( strcmp( p, "server_port" ) == 0 ) |
| 160 | { |
| 161 | opt.server_port = atoi( q ); |
| 162 | if( opt.server_port < 1 || opt.server_port > 65535 ) |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 163 | exit_usage( p, q ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 164 | } |
| 165 | else if( strcmp( p, "listen_addr" ) == 0 ) |
| 166 | opt.listen_addr = q; |
| 167 | else if( strcmp( p, "listen_port" ) == 0 ) |
| 168 | { |
| 169 | opt.listen_port = atoi( q ); |
| 170 | if( opt.listen_port < 1 || opt.listen_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 | } |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 173 | else if( strcmp( p, "duplicate" ) == 0 ) |
| 174 | { |
| 175 | opt.duplicate = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 176 | if( opt.duplicate < 0 || opt.duplicate > 20 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 177 | exit_usage( p, q ); |
| 178 | } |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 179 | else if( strcmp( p, "delay" ) == 0 ) |
| 180 | { |
| 181 | opt.delay = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 182 | if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 183 | exit_usage( p, q ); |
| 184 | } |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 185 | else if( strcmp( p, "delay_ccs" ) == 0 ) |
| 186 | { |
| 187 | opt.delay_ccs = atoi( q ); |
| 188 | if( opt.delay_ccs < 0 || opt.delay_ccs > 1 ) |
| 189 | exit_usage( p, q ); |
| 190 | } |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 191 | else if( strcmp( p, "drop" ) == 0 ) |
| 192 | { |
| 193 | opt.drop = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 194 | if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 ) |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 195 | exit_usage( p, q ); |
| 196 | } |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 197 | else if( strcmp( p, "mtu" ) == 0 ) |
| 198 | { |
| 199 | opt.mtu = atoi( q ); |
| 200 | if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE ) |
| 201 | exit_usage( p, q ); |
| 202 | } |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 203 | else if( strcmp( p, "bad_ad" ) == 0 ) |
| 204 | { |
| 205 | opt.bad_ad = atoi( q ); |
| 206 | if( opt.bad_ad < 0 || opt.bad_ad > 1 ) |
| 207 | exit_usage( p, q ); |
| 208 | } |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 209 | else if( strcmp( p, "protect_hvr" ) == 0 ) |
| 210 | { |
| 211 | opt.protect_hvr = atoi( q ); |
| 212 | if( opt.protect_hvr < 0 || opt.protect_hvr > 1 ) |
| 213 | exit_usage( p, q ); |
| 214 | } |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 215 | else if( strcmp( p, "protect_len" ) == 0 ) |
| 216 | { |
| 217 | opt.protect_len = atoi( q ); |
| 218 | if( opt.protect_len < 0 ) |
| 219 | exit_usage( p, q ); |
| 220 | } |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 221 | else if( strcmp( p, "seed" ) == 0 ) |
| 222 | { |
| 223 | opt.seed = atoi( q ); |
| 224 | if( opt.seed == 0 ) |
| 225 | exit_usage( p, q ); |
| 226 | } |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 227 | else |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 228 | exit_usage( p, NULL ); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | static const char *msg_type( unsigned char *msg, size_t len ) |
| 233 | { |
| 234 | if( len < 1 ) return( "Invalid" ); |
| 235 | switch( msg[0] ) |
| 236 | { |
| 237 | case SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" ); |
| 238 | case SSL_MSG_ALERT: return( "Alert" ); |
| 239 | case SSL_MSG_APPLICATION_DATA: return( "ApplicationData" ); |
| 240 | case SSL_MSG_HANDSHAKE: break; /* See below */ |
| 241 | default: return( "Unknown" ); |
| 242 | } |
| 243 | |
Manuel Pégourié-Gonnard | 8cc7e03 | 2014-09-25 12:59:05 +0200 | [diff] [blame] | 244 | if( len < 13 + 12 ) return( "Invalid handshake" ); |
| 245 | |
| 246 | /* |
| 247 | * Our handshake message are less than 2^16 bytes long, so they should |
| 248 | * have 0 as the first byte of length, frag_offset and frag_length. |
| 249 | * Otherwise, assume they are encrypted. |
| 250 | */ |
| 251 | if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" ); |
| 252 | |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 253 | switch( msg[13] ) |
| 254 | { |
| 255 | case SSL_HS_HELLO_REQUEST: return( "HelloRequest" ); |
| 256 | case SSL_HS_CLIENT_HELLO: return( "ClientHello" ); |
| 257 | case SSL_HS_SERVER_HELLO: return( "ServerHello" ); |
| 258 | case SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" ); |
| 259 | case SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" ); |
| 260 | case SSL_HS_CERTIFICATE: return( "Certificate" ); |
| 261 | case SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" ); |
| 262 | case SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" ); |
| 263 | case SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" ); |
| 264 | case SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" ); |
| 265 | case SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" ); |
| 266 | case SSL_HS_FINISHED: return( "Finished" ); |
Manuel Pégourié-Gonnard | bc010a0 | 2014-09-20 12:40:51 +0200 | [diff] [blame] | 267 | default: return( "Unknown handshake" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 271 | /* Return elapsed time in milliseconds since the first call */ |
| 272 | static unsigned long ellapsed_time( void ) |
| 273 | { |
| 274 | #if defined(_WIN32) |
| 275 | return( 0 ); |
| 276 | #else |
| 277 | static struct timeval ref = { 0, 0 }; |
| 278 | struct timeval now; |
| 279 | |
| 280 | if( ref.tv_sec == 0 && ref.tv_usec == 0 ) |
| 281 | { |
| 282 | gettimeofday( &ref, NULL ); |
| 283 | return( 0 ); |
| 284 | } |
| 285 | |
| 286 | gettimeofday( &now, NULL ); |
| 287 | return( 1000 * ( now.tv_sec - ref.tv_sec ) |
| 288 | + ( now.tv_usec - ref.tv_usec ) / 1000 ); |
| 289 | #endif |
| 290 | } |
| 291 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 292 | typedef struct |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 293 | { |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 294 | int dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 295 | const char *way; |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 296 | const char *type; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 297 | unsigned len; |
| 298 | unsigned char buf[MAX_MSG_SIZE]; |
| 299 | } packet; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 300 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 301 | /* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */ |
| 302 | void print_packet( const packet *p, const char *why ) |
| 303 | { |
| 304 | if( why == NULL ) |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 305 | polarssl_printf( " %05lu %s %s (%u bytes)\n", |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 306 | ellapsed_time(), p->way, p->type, p->len ); |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 307 | else |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 308 | polarssl_printf( " %s %s (%u bytes): %s\n", |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 309 | p->way, p->type, p->len, why ); |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 310 | fflush( stdout ); |
| 311 | } |
| 312 | |
| 313 | int send_packet( const packet *p, const char *why ) |
| 314 | { |
| 315 | int ret; |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 316 | int dst = p->dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 317 | |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 318 | /* insert corrupted ApplicationData record? */ |
| 319 | if( opt.bad_ad && |
| 320 | strcmp( p->type, "ApplicationData" ) == 0 ) |
| 321 | { |
| 322 | unsigned char buf[MAX_MSG_SIZE]; |
| 323 | memcpy( buf, p->buf, p->len ); |
| 324 | ++buf[p->len - 1]; |
| 325 | |
| 326 | print_packet( p, "corrupted" ); |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 327 | if( ( ret = net_send( &dst, buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 328 | { |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 329 | polarssl_printf( " ! net_send returned %d\n", ret ); |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 330 | return( ret ); |
| 331 | } |
| 332 | } |
| 333 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 334 | print_packet( p, why ); |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 335 | if( ( ret = net_send( &dst, p->buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 336 | { |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 337 | polarssl_printf( " ! net_send returned %d\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 338 | return( ret ); |
| 339 | } |
| 340 | |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 341 | /* Don't duplicate Application Data, only handshake covered */ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 342 | if( opt.duplicate != 0 && |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 343 | strcmp( p->type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 344 | rand() % opt.duplicate == 0 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 345 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 346 | print_packet( p, "duplicated" ); |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 347 | |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 348 | if( ( ret = net_send( &dst, p->buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 349 | { |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 350 | polarssl_printf( " ! net_send returned %d\n", ret ); |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 351 | return( ret ); |
| 352 | } |
| 353 | } |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 354 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 355 | return( 0 ); |
| 356 | } |
| 357 | |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 358 | static packet prev; |
| 359 | |
| 360 | void clear_pending( void ) |
| 361 | { |
| 362 | memset( &prev, 0, sizeof( packet ) ); |
| 363 | } |
| 364 | |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 365 | /* |
| 366 | * Avoid dropping or delaying a packet that was already dropped twice: this |
| 367 | * only results in uninteresting timeouts. We can't rely on type to identify |
| 368 | * packets, since during renegotiation they're all encrypted. So, rely on |
| 369 | * size mod 2048 (which is usually just size). |
| 370 | */ |
| 371 | static unsigned char dropped[2048] = { 0 }; |
| 372 | #define DROP_MAX 2 |
| 373 | |
| 374 | /* |
| 375 | * OpenSSL groups packets in a datagram the first time it sends them, but not |
| 376 | * when it resends them. Count every record as seen the first time. |
| 377 | */ |
| 378 | void update_dropped( const packet *p ) |
| 379 | { |
| 380 | size_t id = p->len % sizeof( dropped ); |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 381 | const unsigned char *end = p->buf + p->len; |
| 382 | const unsigned char *cur = p->buf; |
| 383 | size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13; |
| 384 | |
Manuel Pégourié-Gonnard | fa60f12 | 2014-09-26 16:07:29 +0200 | [diff] [blame] | 385 | ++dropped[id]; |
| 386 | |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 387 | /* Avoid counting single record twice */ |
| 388 | if( len == p->len ) |
| 389 | return; |
| 390 | |
| 391 | while( cur < end ) |
| 392 | { |
| 393 | size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13; |
| 394 | |
| 395 | id = len % sizeof( dropped ); |
| 396 | ++dropped[id]; |
| 397 | |
| 398 | cur += len; |
| 399 | } |
| 400 | } |
| 401 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 402 | int handle_message( const char *way, int dst, int src ) |
| 403 | { |
| 404 | int ret; |
| 405 | packet cur; |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 406 | size_t id; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 407 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 408 | /* receive packet */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 409 | 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] | 410 | { |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 411 | polarssl_printf( " ! net_recv returned %d\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 412 | return( ret ); |
| 413 | } |
| 414 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 415 | cur.len = ret; |
| 416 | cur.type = msg_type( cur.buf, cur.len ); |
| 417 | cur.way = way; |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 418 | cur.dst = dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 419 | print_packet( &cur, NULL ); |
| 420 | |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 421 | id = cur.len % sizeof( dropped ); |
| 422 | |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 423 | /* do we want to drop, delay, or forward it? */ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 424 | if( ( opt.mtu != 0 && |
| 425 | cur.len > (unsigned) opt.mtu ) || |
| 426 | ( opt.drop != 0 && |
| 427 | strcmp( cur.type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 428 | ! ( opt.protect_hvr && |
| 429 | strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) && |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 430 | cur.len != (size_t) opt.protect_len && |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 431 | dropped[id] < DROP_MAX && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 432 | rand() % opt.drop == 0 ) ) |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 433 | { |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 434 | update_dropped( &cur ); |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 435 | } |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 436 | else if( ( opt.delay_ccs == 1 && |
| 437 | strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) || |
| 438 | ( opt.delay != 0 && |
| 439 | strcmp( cur.type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 440 | ! ( opt.protect_hvr && |
| 441 | strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) && |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 442 | prev.dst == 0 && |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 443 | cur.len != (size_t) opt.protect_len && |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 444 | dropped[id] < DROP_MAX && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 445 | rand() % opt.delay == 0 ) ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 446 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 447 | memcpy( &prev, &cur, sizeof( packet ) ); |
| 448 | } |
| 449 | else |
| 450 | { |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 451 | /* forward and possibly duplicate */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 452 | if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 ) |
| 453 | return( ret ); |
| 454 | |
| 455 | /* send previously delayed message if any */ |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 456 | if( prev.dst != 0 ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 457 | { |
| 458 | ret = send_packet( &prev, "delayed" ); |
| 459 | memset( &prev, 0, sizeof( packet ) ); |
| 460 | if( ret != 0 ) |
| 461 | return( ret ); |
| 462 | } |
| 463 | } |
| 464 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 465 | return( 0 ); |
| 466 | } |
| 467 | |
| 468 | int main( int argc, char *argv[] ) |
| 469 | { |
| 470 | int ret; |
| 471 | |
| 472 | int listen_fd = -1; |
| 473 | int client_fd = -1; |
| 474 | int server_fd = -1; |
| 475 | |
| 476 | int nb_fds; |
| 477 | fd_set read_fds; |
| 478 | |
| 479 | get_options( argc, argv ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | * Decisions to drop/delay/duplicate packets are pseudo-random: dropping |
| 483 | * exactly 1 in N packets would lead to problems when a flight has exactly |
| 484 | * N packets: the same packet would be dropped on every resend. |
| 485 | * |
| 486 | * In order to be able to reproduce problems reliably, the seed may be |
| 487 | * specified explicitly. |
| 488 | */ |
| 489 | if( opt.seed == 0 ) |
| 490 | { |
| 491 | opt.seed = time( NULL ); |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 492 | polarssl_printf( " . Pseudo-random seed: %u\n", opt.seed ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | srand( opt.seed ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 496 | |
| 497 | /* |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 498 | * 0. "Connect" to the server |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 499 | */ |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 500 | polarssl_printf( " . Connect to server on UDP/%s/%d ...", |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 501 | opt.server_addr, opt.server_port ); |
| 502 | fflush( stdout ); |
| 503 | |
| 504 | if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port, |
| 505 | NET_PROTO_UDP ) ) != 0 ) |
| 506 | { |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 507 | polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 508 | goto exit; |
| 509 | } |
| 510 | |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 511 | polarssl_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 512 | |
| 513 | /* |
| 514 | * 1. Setup the "listening" UDP socket |
| 515 | */ |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 516 | polarssl_printf( " . Bind on UDP/%s/%d ...", |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 517 | opt.listen_addr, opt.listen_port ); |
| 518 | fflush( stdout ); |
| 519 | |
| 520 | if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port, |
| 521 | NET_PROTO_UDP ) ) != 0 ) |
| 522 | { |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 523 | polarssl_printf( " failed\n ! net_bind returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 524 | goto exit; |
| 525 | } |
| 526 | |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 527 | polarssl_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 528 | |
| 529 | /* |
| 530 | * 2. Wait until a client connects |
| 531 | */ |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 532 | accept: |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 533 | polarssl_printf( " . Waiting for a remote connection ..." ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 534 | fflush( stdout ); |
| 535 | |
| 536 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) |
| 537 | { |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 538 | polarssl_printf( " failed\n ! net_accept returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 539 | goto exit; |
| 540 | } |
| 541 | |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 542 | polarssl_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 543 | fflush( stdout ); |
| 544 | |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 545 | polarssl_printf( " . Re-bind on UDP/%s/%d ...", |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 546 | opt.listen_addr, opt.listen_port ); |
| 547 | fflush( stdout ); |
| 548 | |
| 549 | if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port, |
| 550 | NET_PROTO_UDP ) ) != 0 ) |
| 551 | { |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 552 | polarssl_printf( " failed\n ! net_bind returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 553 | goto exit; |
| 554 | } |
| 555 | |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 556 | polarssl_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 557 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 558 | /* |
| 559 | * 3. Forward packets forever (kill the process to terminate it) |
| 560 | */ |
Manuel Pégourié-Gonnard | ce8588c | 2014-10-01 00:56:03 +0200 | [diff] [blame] | 561 | clear_pending(); |
| 562 | memset( dropped, 0, sizeof( dropped ) ); |
| 563 | |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 564 | nb_fds = client_fd; |
| 565 | if( nb_fds < server_fd ) |
| 566 | nb_fds = server_fd; |
| 567 | if( nb_fds < listen_fd ) |
| 568 | nb_fds = listen_fd; |
| 569 | ++nb_fds; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 570 | |
| 571 | while( 1 ) |
| 572 | { |
| 573 | FD_ZERO( &read_fds ); |
| 574 | FD_SET( server_fd, &read_fds ); |
| 575 | FD_SET( client_fd, &read_fds ); |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 576 | FD_SET( listen_fd, &read_fds ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 577 | |
| 578 | if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 ) |
| 579 | { |
| 580 | perror( "select" ); |
| 581 | goto exit; |
| 582 | } |
| 583 | |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 584 | if( FD_ISSET( listen_fd, &read_fds ) ) |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 585 | goto accept; |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 586 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 587 | if( FD_ISSET( client_fd, &read_fds ) ) |
| 588 | { |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 589 | if( ( ret = handle_message( "S <- C", |
| 590 | server_fd, client_fd ) ) != 0 ) |
Manuel Pégourié-Gonnard | ce8588c | 2014-10-01 00:56:03 +0200 | [diff] [blame] | 591 | goto accept; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | if( FD_ISSET( server_fd, &read_fds ) ) |
| 595 | { |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 596 | if( ( ret = handle_message( "S -> C", |
| 597 | client_fd, server_fd ) ) != 0 ) |
Manuel Pégourié-Gonnard | ce8588c | 2014-10-01 00:56:03 +0200 | [diff] [blame] | 598 | goto accept; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
| 602 | exit: |
| 603 | |
| 604 | #ifdef POLARSSL_ERROR_C |
| 605 | if( ret != 0 ) |
| 606 | { |
| 607 | char error_buf[100]; |
| 608 | polarssl_strerror( ret, error_buf, 100 ); |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 609 | polarssl_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 610 | fflush( stdout ); |
| 611 | } |
| 612 | #endif |
| 613 | |
| 614 | if( client_fd != -1 ) |
| 615 | net_close( client_fd ); |
| 616 | |
| 617 | if( listen_fd != -1 ) |
| 618 | net_close( listen_fd ); |
| 619 | |
| 620 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame^] | 621 | polarssl_printf( " Press Enter to exit this program.\n" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 622 | fflush( stdout ); getchar(); |
| 623 | #endif |
| 624 | |
| 625 | return( ret != 0 ); |
| 626 | } |
| 627 | |
| 628 | #endif /* POLARSSL_NET_C */ |