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