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 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | e4d4890 | 2015-03-06 13:40:52 +0000 | [diff] [blame] | 19 | * 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] | 20 | */ |
| 21 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 22 | /* |
| 23 | * Warning: this is an internal utility program we use for tests. |
| 24 | * It does break some abstractions from the NET layer, and is thus NOT an |
| 25 | * example of good general usage. |
| 26 | */ |
| 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 30 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 32 | #endif |
| 33 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 35 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame] | 36 | #else |
Simon Butcher | d3138c3 | 2016-04-27 01:26:50 +0100 | [diff] [blame] | 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <time.h> |
| 40 | #define mbedtls_time time |
| 41 | #define mbedtls_time_t time_t |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | #define mbedtls_printf printf |
Manuel Pégourié-Gonnard | f224678 | 2015-01-29 13:29:20 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | #if !defined(MBEDTLS_NET_C) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 46 | int main( void ) |
| 47 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | mbedtls_printf( "MBEDTLS_NET_C not defined.\n" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 49 | return( 0 ); |
| 50 | } |
| 51 | #else |
| 52 | |
Andres AG | 788aa4a | 2016-09-14 14:32:09 +0100 | [diff] [blame] | 53 | #include "mbedtls/net_sockets.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 54 | #include "mbedtls/error.h" |
| 55 | #include "mbedtls/ssl.h" |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 56 | #include "mbedtls/timing.h" |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 57 | |
Manuel Pégourié-Gonnard | d901d17 | 2015-02-16 18:37:53 +0000 | [diff] [blame] | 58 | #include <string.h> |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 59 | |
| 60 | /* For select() */ |
| 61 | #if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \ |
| 62 | !defined(EFI32) |
| 63 | #include <winsock2.h> |
| 64 | #include <windows.h> |
| 65 | #if defined(_MSC_VER) |
| 66 | #if defined(_WIN32_WCE) |
| 67 | #pragma comment( lib, "ws2.lib" ) |
| 68 | #else |
| 69 | #pragma comment( lib, "ws2_32.lib" ) |
| 70 | #endif |
| 71 | #endif /* _MSC_VER */ |
| 72 | #else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ |
| 73 | #include <sys/time.h> |
| 74 | #include <sys/types.h> |
| 75 | #include <unistd.h> |
| 76 | #endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */ |
| 77 | |
Manuel Pégourié-Gonnard | b46780e | 2014-09-23 12:17:30 +0200 | [diff] [blame] | 78 | #define MAX_MSG_SIZE 16384 + 2048 /* max record/datagram size */ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 79 | |
| 80 | #define DFL_SERVER_ADDR "localhost" |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 81 | #define DFL_SERVER_PORT "4433" |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 82 | #define DFL_LISTEN_ADDR "localhost" |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 83 | #define DFL_LISTEN_PORT "5556" |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 84 | #define DFL_PACK 0 |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 85 | |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 86 | #if defined(MBEDTLS_TIMING_C) |
| 87 | #define USAGE_PACK \ |
| 88 | " pack=%%d default: 0 (don't pack)\n" \ |
| 89 | " options: t > 0 (pack for t milliseconds)\n" |
| 90 | #else |
| 91 | #define USAGE_PACK |
| 92 | #endif |
| 93 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 94 | #define USAGE \ |
| 95 | "\n usage: udp_proxy param=<>...\n" \ |
| 96 | "\n acceptable parameters:\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 97 | " server_addr=%%s default: localhost\n" \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 98 | " server_port=%%d default: 4433\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 99 | " listen_addr=%%s default: localhost\n" \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 100 | " listen_port=%%d default: 4433\n" \ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 101 | "\n" \ |
| 102 | " duplicate=%%d default: 0 (no duplication)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 103 | " duplicate about 1:N packets randomly\n" \ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 104 | " delay=%%d default: 0 (no delayed packets)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 105 | " delay about 1:N packets randomly\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 106 | " 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] | 107 | " drop=%%d default: 0 (no dropped packets)\n" \ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 108 | " drop about 1:N packets randomly\n" \ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 109 | " mtu=%%d default: 0 (unlimited)\n" \ |
| 110 | " drop packets larger than N bytes\n" \ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 111 | " bad_ad=0/1 default: 0 (don't add bad ApplicationData)\n" \ |
| 112 | " protect_hvr=0/1 default: 0 (don't protect HelloVerifyRequest)\n" \ |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 113 | " 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] | 114 | "\n" \ |
| 115 | " seed=%%d default: (use current time)\n" \ |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 116 | USAGE_PACK \ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 117 | "\n" |
| 118 | |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 119 | /* |
| 120 | * global options |
| 121 | */ |
| 122 | static struct options |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 123 | { |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 124 | const char *server_addr; /* address to forward packets to */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 125 | const char *server_port; /* port to forward packets to */ |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 126 | const char *listen_addr; /* address for accepting client connections */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 127 | const char *listen_port; /* port for accepting client connections */ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 128 | |
| 129 | int duplicate; /* duplicate 1 in N packets (none if 0) */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 130 | int delay; /* delay 1 packet in N (none if 0) */ |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 131 | int delay_ccs; /* delay ChangeCipherSpec */ |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 132 | int drop; /* drop 1 packet in N (none if 0) */ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 133 | int mtu; /* drop packets larger than this */ |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 134 | int bad_ad; /* inject corrupted ApplicationData record */ |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 135 | int protect_hvr; /* never drop or delay HelloVerifyRequest */ |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 136 | int protect_len; /* never drop/delay packet of the given size*/ |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 137 | unsigned pack; /* merge packets into single datagram for |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 138 | * at most \c merge milliseconds if > 0 */ |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 139 | unsigned int seed; /* seed for "random" events */ |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 140 | } opt; |
| 141 | |
| 142 | static void exit_usage( const char *name, const char *value ) |
| 143 | { |
| 144 | if( value == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | mbedtls_printf( " unknown option or missing value: %s\n", name ); |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 146 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | mbedtls_printf( " option %s: illegal value: %s\n", name, value ); |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 148 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | mbedtls_printf( USAGE ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 150 | exit( 1 ); |
| 151 | } |
| 152 | |
| 153 | static void get_options( int argc, char *argv[] ) |
| 154 | { |
| 155 | int i; |
| 156 | char *p, *q; |
| 157 | |
| 158 | opt.server_addr = DFL_SERVER_ADDR; |
| 159 | opt.server_port = DFL_SERVER_PORT; |
| 160 | opt.listen_addr = DFL_LISTEN_ADDR; |
| 161 | opt.listen_port = DFL_LISTEN_PORT; |
Hanno Becker | 211f44c | 2017-10-31 14:08:10 +0000 | [diff] [blame] | 162 | opt.pack = DFL_PACK; |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 163 | /* Other members default to 0 */ |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 164 | |
| 165 | for( i = 1; i < argc; i++ ) |
| 166 | { |
| 167 | p = argv[i]; |
| 168 | if( ( q = strchr( p, '=' ) ) == NULL ) |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 169 | exit_usage( p, NULL ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 170 | *q++ = '\0'; |
| 171 | |
| 172 | if( strcmp( p, "server_addr" ) == 0 ) |
| 173 | opt.server_addr = q; |
| 174 | else if( strcmp( p, "server_port" ) == 0 ) |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 175 | opt.server_port = q; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 176 | else if( strcmp( p, "listen_addr" ) == 0 ) |
| 177 | opt.listen_addr = q; |
| 178 | else if( strcmp( p, "listen_port" ) == 0 ) |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 179 | opt.listen_port = q; |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 180 | else if( strcmp( p, "duplicate" ) == 0 ) |
| 181 | { |
| 182 | opt.duplicate = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 183 | if( opt.duplicate < 0 || opt.duplicate > 20 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 184 | exit_usage( p, q ); |
| 185 | } |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 186 | else if( strcmp( p, "delay" ) == 0 ) |
| 187 | { |
| 188 | opt.delay = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 189 | if( opt.delay < 0 || opt.delay > 20 || opt.delay == 1 ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 190 | exit_usage( p, q ); |
| 191 | } |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 192 | else if( strcmp( p, "delay_ccs" ) == 0 ) |
| 193 | { |
| 194 | opt.delay_ccs = atoi( q ); |
| 195 | if( opt.delay_ccs < 0 || opt.delay_ccs > 1 ) |
| 196 | exit_usage( p, q ); |
| 197 | } |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 198 | else if( strcmp( p, "drop" ) == 0 ) |
| 199 | { |
| 200 | opt.drop = atoi( q ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 201 | if( opt.drop < 0 || opt.drop > 20 || opt.drop == 1 ) |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 202 | exit_usage( p, q ); |
| 203 | } |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 204 | else if( strcmp( p, "pack" ) == 0 ) |
| 205 | { |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 206 | #if defined(MBEDTLS_TIMING_C) |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 207 | opt.pack = (unsigned) atoi( q ); |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 208 | #else |
| 209 | mbedtls_printf( " option pack only defined if MBEDTLS_TIMING_C is enabled\n" ); |
| 210 | exit( 1 ); |
| 211 | #endif |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 212 | } |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 213 | else if( strcmp( p, "mtu" ) == 0 ) |
| 214 | { |
| 215 | opt.mtu = atoi( q ); |
| 216 | if( opt.mtu < 0 || opt.mtu > MAX_MSG_SIZE ) |
| 217 | exit_usage( p, q ); |
| 218 | } |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 219 | else if( strcmp( p, "bad_ad" ) == 0 ) |
| 220 | { |
| 221 | opt.bad_ad = atoi( q ); |
| 222 | if( opt.bad_ad < 0 || opt.bad_ad > 1 ) |
| 223 | exit_usage( p, q ); |
| 224 | } |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 225 | else if( strcmp( p, "protect_hvr" ) == 0 ) |
| 226 | { |
| 227 | opt.protect_hvr = atoi( q ); |
| 228 | if( opt.protect_hvr < 0 || opt.protect_hvr > 1 ) |
| 229 | exit_usage( p, q ); |
| 230 | } |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 231 | else if( strcmp( p, "protect_len" ) == 0 ) |
| 232 | { |
| 233 | opt.protect_len = atoi( q ); |
| 234 | if( opt.protect_len < 0 ) |
| 235 | exit_usage( p, q ); |
| 236 | } |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 237 | else if( strcmp( p, "seed" ) == 0 ) |
| 238 | { |
| 239 | opt.seed = atoi( q ); |
| 240 | if( opt.seed == 0 ) |
| 241 | exit_usage( p, q ); |
| 242 | } |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 243 | else |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 244 | exit_usage( p, NULL ); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | static const char *msg_type( unsigned char *msg, size_t len ) |
| 249 | { |
| 250 | if( len < 1 ) return( "Invalid" ); |
| 251 | switch( msg[0] ) |
| 252 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" ); |
| 254 | case MBEDTLS_SSL_MSG_ALERT: return( "Alert" ); |
| 255 | case MBEDTLS_SSL_MSG_APPLICATION_DATA: return( "ApplicationData" ); |
| 256 | case MBEDTLS_SSL_MSG_HANDSHAKE: break; /* See below */ |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 257 | default: return( "Unknown" ); |
| 258 | } |
| 259 | |
Manuel Pégourié-Gonnard | 8cc7e03 | 2014-09-25 12:59:05 +0200 | [diff] [blame] | 260 | if( len < 13 + 12 ) return( "Invalid handshake" ); |
| 261 | |
| 262 | /* |
| 263 | * Our handshake message are less than 2^16 bytes long, so they should |
| 264 | * have 0 as the first byte of length, frag_offset and frag_length. |
| 265 | * Otherwise, assume they are encrypted. |
| 266 | */ |
| 267 | if( msg[14] || msg[19] || msg[22] ) return( "Encrypted handshake" ); |
| 268 | |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 269 | switch( msg[13] ) |
| 270 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 271 | case MBEDTLS_SSL_HS_HELLO_REQUEST: return( "HelloRequest" ); |
| 272 | case MBEDTLS_SSL_HS_CLIENT_HELLO: return( "ClientHello" ); |
| 273 | case MBEDTLS_SSL_HS_SERVER_HELLO: return( "ServerHello" ); |
| 274 | case MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" ); |
| 275 | case MBEDTLS_SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" ); |
| 276 | case MBEDTLS_SSL_HS_CERTIFICATE: return( "Certificate" ); |
| 277 | case MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" ); |
| 278 | case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" ); |
| 279 | case MBEDTLS_SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" ); |
| 280 | case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" ); |
| 281 | case MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" ); |
| 282 | case MBEDTLS_SSL_HS_FINISHED: return( "Finished" ); |
Manuel Pégourié-Gonnard | bc010a0 | 2014-09-20 12:40:51 +0200 | [diff] [blame] | 283 | default: return( "Unknown handshake" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 287 | #if defined(MBEDTLS_TIMING_C) |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 288 | /* Return elapsed time in milliseconds since the first call */ |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 289 | static unsigned ellapsed_time( void ) |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 290 | { |
Hanno Becker | 92474da | 2017-10-31 14:09:30 +0000 | [diff] [blame] | 291 | static int initialized = 0; |
| 292 | static struct mbedtls_timing_hr_time hires; |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 293 | |
Hanno Becker | 92474da | 2017-10-31 14:09:30 +0000 | [diff] [blame] | 294 | if( initialized == 0 ) |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 295 | { |
Hanno Becker | 92474da | 2017-10-31 14:09:30 +0000 | [diff] [blame] | 296 | (void) mbedtls_timing_get_timer( &hires, 1 ); |
| 297 | initialized = 1; |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 298 | return( 0 ); |
| 299 | } |
| 300 | |
Hanno Becker | 92474da | 2017-10-31 14:09:30 +0000 | [diff] [blame] | 301 | return( mbedtls_timing_get_timer( &hires, 0 ) ); |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 304 | typedef struct |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 305 | { |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 306 | mbedtls_net_context *ctx; |
| 307 | |
| 308 | const char *description; |
| 309 | |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 310 | unsigned packet_lifetime; |
| 311 | unsigned num_datagrams; |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 312 | |
| 313 | unsigned char data[MAX_MSG_SIZE]; |
Hanno Becker | a5e6897 | 2017-12-06 08:35:02 +0000 | [diff] [blame] | 314 | size_t len; |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 315 | |
| 316 | } ctx_buffer; |
| 317 | |
| 318 | static ctx_buffer outbuf[2]; |
| 319 | |
| 320 | static int ctx_buffer_flush( ctx_buffer *buf ) |
| 321 | { |
| 322 | int ret; |
| 323 | |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 324 | mbedtls_printf( " %05u flush %s: %u bytes, %u datagrams, last %u ms\n", |
| 325 | ellapsed_time(), buf->description, |
Hanno Becker | a5e6897 | 2017-12-06 08:35:02 +0000 | [diff] [blame] | 326 | (unsigned) buf->len, buf->num_datagrams, |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 327 | ellapsed_time() - buf->packet_lifetime ); |
| 328 | |
| 329 | ret = mbedtls_net_send( buf->ctx, buf->data, buf->len ); |
| 330 | |
| 331 | buf->len = 0; |
| 332 | buf->num_datagrams = 0; |
| 333 | |
| 334 | return( ret ); |
| 335 | } |
| 336 | |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 337 | static unsigned ctx_buffer_time_remaining( ctx_buffer *buf ) |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 338 | { |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 339 | unsigned const cur_time = ellapsed_time(); |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 340 | |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 341 | if( buf->num_datagrams == 0 ) |
| 342 | return( (unsigned) -1 ); |
| 343 | |
| 344 | if( cur_time - buf->packet_lifetime >= opt.pack ) |
| 345 | return( 0 ); |
| 346 | |
| 347 | return( opt.pack - ( cur_time - buf->packet_lifetime ) ); |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | static int ctx_buffer_append( ctx_buffer *buf, |
| 351 | const unsigned char * data, |
| 352 | size_t len ) |
| 353 | { |
| 354 | int ret; |
| 355 | |
Hanno Becker | a5e6897 | 2017-12-06 08:35:02 +0000 | [diff] [blame] | 356 | if( len > (size_t) INT_MAX ) |
| 357 | return( -1 ); |
| 358 | |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 359 | if( len > sizeof( buf->data ) ) |
| 360 | { |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 361 | mbedtls_printf( " ! buffer size %u too large (max %u)\n", |
| 362 | (unsigned) len, (unsigned) sizeof( buf->data ) ); |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 363 | return( -1 ); |
| 364 | } |
| 365 | |
| 366 | if( sizeof( buf->data ) - buf->len < len ) |
| 367 | { |
| 368 | if( ( ret = ctx_buffer_flush( buf ) ) <= 0 ) |
| 369 | return( ret ); |
| 370 | } |
| 371 | |
| 372 | memcpy( buf->data + buf->len, data, len ); |
| 373 | |
| 374 | buf->len += len; |
| 375 | if( ++buf->num_datagrams == 1 ) |
| 376 | buf->packet_lifetime = ellapsed_time(); |
| 377 | |
Hanno Becker | a5e6897 | 2017-12-06 08:35:02 +0000 | [diff] [blame] | 378 | return( (int) len ); |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 379 | } |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 380 | #endif /* MBEDTLS_TIMING_C */ |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 381 | |
| 382 | static int dispatch_data( mbedtls_net_context *ctx, |
| 383 | const unsigned char * data, |
| 384 | size_t len ) |
| 385 | { |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 386 | #if defined(MBEDTLS_TIMING_C) |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 387 | ctx_buffer *buf = NULL; |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 388 | if( opt.pack > 0 ) |
| 389 | { |
| 390 | if( outbuf[0].ctx == ctx ) |
| 391 | buf = &outbuf[0]; |
| 392 | else if( outbuf[1].ctx == ctx ) |
| 393 | buf = &outbuf[1]; |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 394 | |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 395 | if( buf == NULL ) |
| 396 | return( -1 ); |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 397 | |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 398 | return( ctx_buffer_append( buf, data, len ) ); |
| 399 | } |
| 400 | #endif /* MBEDTLS_TIMING_C */ |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 401 | |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 402 | return( mbedtls_net_send( ctx, data, len ) ); |
| 403 | } |
| 404 | |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 405 | typedef struct |
| 406 | { |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 407 | mbedtls_net_context *dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 408 | const char *way; |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 409 | const char *type; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 410 | unsigned len; |
| 411 | unsigned char buf[MAX_MSG_SIZE]; |
| 412 | } packet; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 413 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 414 | /* Print packet. Outgoing packets come with a reason (forward, dupl, etc.) */ |
| 415 | void print_packet( const packet *p, const char *why ) |
| 416 | { |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 417 | #if defined(MBEDTLS_TIMING_C) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 418 | if( why == NULL ) |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 419 | mbedtls_printf( " %05u dispatch %s %s (%u bytes)\n", |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 420 | ellapsed_time(), p->way, p->type, p->len ); |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 421 | else |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 422 | mbedtls_printf( " %05u dispatch %s %s (%u bytes): %s\n", |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 423 | ellapsed_time(), p->way, p->type, p->len, why ); |
| 424 | #else |
| 425 | if( why == NULL ) |
| 426 | mbedtls_printf( " dispatch %s %s (%u bytes)\n", |
| 427 | p->way, p->type, p->len ); |
| 428 | else |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 429 | mbedtls_printf( " dispatch %s %s (%u bytes): %s\n", |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 430 | p->way, p->type, p->len, why ); |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 431 | #endif |
| 432 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 433 | fflush( stdout ); |
| 434 | } |
| 435 | |
| 436 | int send_packet( const packet *p, const char *why ) |
| 437 | { |
| 438 | int ret; |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 439 | mbedtls_net_context *dst = p->dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 440 | |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 441 | /* insert corrupted ApplicationData record? */ |
| 442 | if( opt.bad_ad && |
| 443 | strcmp( p->type, "ApplicationData" ) == 0 ) |
| 444 | { |
| 445 | unsigned char buf[MAX_MSG_SIZE]; |
| 446 | memcpy( buf, p->buf, p->len ); |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 447 | |
Hanno Becker | fbb0b70 | 2017-05-26 16:55:07 +0100 | [diff] [blame] | 448 | if( p->len <= 13 ) |
| 449 | { |
| 450 | mbedtls_printf( " ! can't corrupt empty AD record" ); |
| 451 | } |
| 452 | else |
| 453 | { |
| 454 | ++buf[13]; |
| 455 | print_packet( p, "corrupted" ); |
| 456 | } |
| 457 | |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 458 | if( ( ret = dispatch_data( dst, buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 459 | { |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 460 | mbedtls_printf( " ! dispatch returned %d\n", ret ); |
Manuel Pégourié-Gonnard | 6c18a39 | 2014-09-08 11:24:58 +0200 | [diff] [blame] | 461 | return( ret ); |
| 462 | } |
| 463 | } |
| 464 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 465 | print_packet( p, why ); |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 466 | if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 467 | { |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 468 | mbedtls_printf( " ! dispatch returned %d\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 469 | return( ret ); |
| 470 | } |
| 471 | |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 472 | /* Don't duplicate Application Data, only handshake covered */ |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 473 | if( opt.duplicate != 0 && |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 474 | strcmp( p->type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 475 | rand() % opt.duplicate == 0 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 476 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 477 | print_packet( p, "duplicated" ); |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 478 | |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 479 | if( ( ret = dispatch_data( dst, p->buf, p->len ) ) <= 0 ) |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 480 | { |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 481 | mbedtls_printf( " ! dispatch returned %d\n", ret ); |
Manuel Pégourié-Gonnard | 2c41bd8 | 2014-09-06 08:14:47 +0200 | [diff] [blame] | 482 | return( ret ); |
| 483 | } |
| 484 | } |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 485 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 486 | return( 0 ); |
| 487 | } |
| 488 | |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 489 | static packet prev; |
| 490 | |
| 491 | void clear_pending( void ) |
| 492 | { |
| 493 | memset( &prev, 0, sizeof( packet ) ); |
| 494 | } |
| 495 | |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 496 | /* |
| 497 | * Avoid dropping or delaying a packet that was already dropped twice: this |
| 498 | * only results in uninteresting timeouts. We can't rely on type to identify |
| 499 | * packets, since during renegotiation they're all encrypted. So, rely on |
| 500 | * size mod 2048 (which is usually just size). |
| 501 | */ |
| 502 | static unsigned char dropped[2048] = { 0 }; |
| 503 | #define DROP_MAX 2 |
| 504 | |
| 505 | /* |
| 506 | * OpenSSL groups packets in a datagram the first time it sends them, but not |
| 507 | * when it resends them. Count every record as seen the first time. |
| 508 | */ |
| 509 | void update_dropped( const packet *p ) |
| 510 | { |
| 511 | size_t id = p->len % sizeof( dropped ); |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 512 | const unsigned char *end = p->buf + p->len; |
| 513 | const unsigned char *cur = p->buf; |
| 514 | size_t len = ( ( cur[11] << 8 ) | cur[12] ) + 13; |
| 515 | |
Manuel Pégourié-Gonnard | fa60f12 | 2014-09-26 16:07:29 +0200 | [diff] [blame] | 516 | ++dropped[id]; |
| 517 | |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 518 | /* Avoid counting single record twice */ |
| 519 | if( len == p->len ) |
| 520 | return; |
| 521 | |
| 522 | while( cur < end ) |
| 523 | { |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 524 | len = ( ( cur[11] << 8 ) | cur[12] ) + 13; |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 525 | |
| 526 | id = len % sizeof( dropped ); |
| 527 | ++dropped[id]; |
| 528 | |
| 529 | cur += len; |
| 530 | } |
| 531 | } |
| 532 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 533 | int handle_message( const char *way, |
| 534 | mbedtls_net_context *dst, |
| 535 | mbedtls_net_context *src ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 536 | { |
| 537 | int ret; |
| 538 | packet cur; |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 539 | size_t id; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 540 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 541 | /* receive packet */ |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 542 | if( ( ret = mbedtls_net_recv( src, cur.buf, sizeof( cur.buf ) ) ) <= 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 543 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 544 | mbedtls_printf( " ! mbedtls_net_recv returned %d\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 545 | return( ret ); |
| 546 | } |
| 547 | |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 548 | cur.len = ret; |
| 549 | cur.type = msg_type( cur.buf, cur.len ); |
| 550 | cur.way = way; |
Manuel Pégourié-Gonnard | 6265d30 | 2014-09-24 17:42:09 +0200 | [diff] [blame] | 551 | cur.dst = dst; |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 552 | print_packet( &cur, NULL ); |
| 553 | |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 554 | id = cur.len % sizeof( dropped ); |
| 555 | |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 556 | /* do we want to drop, delay, or forward it? */ |
Manuel Pégourié-Gonnard | eb00bfd | 2014-09-08 11:11:42 +0200 | [diff] [blame] | 557 | if( ( opt.mtu != 0 && |
| 558 | cur.len > (unsigned) opt.mtu ) || |
| 559 | ( opt.drop != 0 && |
| 560 | strcmp( cur.type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 561 | ! ( opt.protect_hvr && |
| 562 | strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) && |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 563 | cur.len != (size_t) opt.protect_len && |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 564 | dropped[id] < DROP_MAX && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 565 | rand() % opt.drop == 0 ) ) |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 566 | { |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 567 | update_dropped( &cur ); |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 568 | } |
Manuel Pégourié-Gonnard | 81f2fe9 | 2014-09-08 10:44:57 +0200 | [diff] [blame] | 569 | else if( ( opt.delay_ccs == 1 && |
| 570 | strcmp( cur.type, "ChangeCipherSpec" ) == 0 ) || |
| 571 | ( opt.delay != 0 && |
| 572 | strcmp( cur.type, "ApplicationData" ) != 0 && |
Manuel Pégourié-Gonnard | d0fd1da | 2014-09-25 17:00:27 +0200 | [diff] [blame] | 573 | ! ( opt.protect_hvr && |
| 574 | strcmp( cur.type, "HelloVerifyRequest" ) == 0 ) && |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 575 | prev.dst == NULL && |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 576 | cur.len != (size_t) opt.protect_len && |
Manuel Pégourié-Gonnard | ae666c5 | 2014-09-26 12:08:36 +0200 | [diff] [blame] | 577 | dropped[id] < DROP_MAX && |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 578 | rand() % opt.delay == 0 ) ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 579 | { |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 580 | memcpy( &prev, &cur, sizeof( packet ) ); |
| 581 | } |
| 582 | else |
| 583 | { |
Manuel Pégourié-Gonnard | 60fdd7e | 2014-09-06 14:49:52 +0200 | [diff] [blame] | 584 | /* forward and possibly duplicate */ |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 585 | if( ( ret = send_packet( &cur, "forwarded" ) ) != 0 ) |
| 586 | return( ret ); |
| 587 | |
| 588 | /* send previously delayed message if any */ |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 589 | if( prev.dst != NULL ) |
Manuel Pégourié-Gonnard | 21398c3 | 2014-09-06 14:36:46 +0200 | [diff] [blame] | 590 | { |
| 591 | ret = send_packet( &prev, "delayed" ); |
| 592 | memset( &prev, 0, sizeof( packet ) ); |
| 593 | if( ret != 0 ) |
| 594 | return( ret ); |
| 595 | } |
| 596 | } |
| 597 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 598 | return( 0 ); |
| 599 | } |
| 600 | |
| 601 | int main( int argc, char *argv[] ) |
| 602 | { |
| 603 | int ret; |
| 604 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 605 | mbedtls_net_context listen_fd, client_fd, server_fd; |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 606 | |
| 607 | #if defined( MBEDTLS_TIMING_C ) |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 608 | struct timeval tm; |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 609 | #endif |
| 610 | |
| 611 | struct timeval *tm_ptr = NULL; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 612 | |
| 613 | int nb_fds; |
| 614 | fd_set read_fds; |
| 615 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 616 | mbedtls_net_init( &listen_fd ); |
| 617 | mbedtls_net_init( &client_fd ); |
| 618 | mbedtls_net_init( &server_fd ); |
| 619 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 620 | get_options( argc, argv ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 621 | |
| 622 | /* |
| 623 | * Decisions to drop/delay/duplicate packets are pseudo-random: dropping |
| 624 | * exactly 1 in N packets would lead to problems when a flight has exactly |
| 625 | * N packets: the same packet would be dropped on every resend. |
| 626 | * |
| 627 | * In order to be able to reproduce problems reliably, the seed may be |
| 628 | * specified explicitly. |
| 629 | */ |
| 630 | if( opt.seed == 0 ) |
| 631 | { |
Manuel Pégourié-Gonnard | 9de64f5 | 2015-07-01 15:51:43 +0200 | [diff] [blame] | 632 | opt.seed = (unsigned int) time( NULL ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 633 | mbedtls_printf( " . Pseudo-random seed: %u\n", opt.seed ); |
Manuel Pégourié-Gonnard | 992e136 | 2014-09-20 18:06:23 +0200 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | srand( opt.seed ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 637 | |
| 638 | /* |
Manuel Pégourié-Gonnard | 44d5e63 | 2014-09-06 08:07:45 +0200 | [diff] [blame] | 639 | * 0. "Connect" to the server |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 640 | */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 641 | mbedtls_printf( " . Connect to server on UDP/%s/%s ...", |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 642 | opt.server_addr, opt.server_port ); |
| 643 | fflush( stdout ); |
| 644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 645 | if( ( ret = mbedtls_net_connect( &server_fd, opt.server_addr, opt.server_port, |
| 646 | MBEDTLS_NET_PROTO_UDP ) ) != 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 647 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 648 | mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 649 | goto exit; |
| 650 | } |
| 651 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 652 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 653 | |
| 654 | /* |
| 655 | * 1. Setup the "listening" UDP socket |
| 656 | */ |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 657 | mbedtls_printf( " . Bind on UDP/%s/%s ...", |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 658 | opt.listen_addr, opt.listen_port ); |
| 659 | fflush( stdout ); |
| 660 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 661 | if( ( ret = mbedtls_net_bind( &listen_fd, opt.listen_addr, opt.listen_port, |
| 662 | MBEDTLS_NET_PROTO_UDP ) ) != 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 663 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 664 | mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 665 | goto exit; |
| 666 | } |
| 667 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 668 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 669 | |
| 670 | /* |
| 671 | * 2. Wait until a client connects |
| 672 | */ |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 673 | accept: |
Manuel Pégourié-Gonnard | abc729e | 2015-07-01 01:28:24 +0200 | [diff] [blame] | 674 | mbedtls_net_free( &client_fd ); |
| 675 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 676 | mbedtls_printf( " . Waiting for a remote connection ..." ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 677 | fflush( stdout ); |
| 678 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 679 | if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd, |
Manuel Pégourié-Gonnard | 0b104b0 | 2015-05-14 21:52:40 +0200 | [diff] [blame] | 680 | NULL, 0, NULL ) ) != 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 681 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 682 | mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 683 | goto exit; |
| 684 | } |
| 685 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 686 | mbedtls_printf( " ok\n" ); |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 687 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 688 | /* |
| 689 | * 3. Forward packets forever (kill the process to terminate it) |
| 690 | */ |
Manuel Pégourié-Gonnard | ce8588c | 2014-10-01 00:56:03 +0200 | [diff] [blame] | 691 | clear_pending(); |
| 692 | memset( dropped, 0, sizeof( dropped ) ); |
| 693 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 694 | nb_fds = client_fd.fd; |
| 695 | if( nb_fds < server_fd.fd ) |
| 696 | nb_fds = server_fd.fd; |
| 697 | if( nb_fds < listen_fd.fd ) |
| 698 | nb_fds = listen_fd.fd; |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 699 | ++nb_fds; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 700 | |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 701 | #if defined(MBEDTLS_TIMING_C) |
Hanno Becker | 211f44c | 2017-10-31 14:08:10 +0000 | [diff] [blame] | 702 | if( opt.pack > 0 ) |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 703 | { |
| 704 | outbuf[0].ctx = &server_fd; |
| 705 | outbuf[0].description = "S <- C"; |
| 706 | outbuf[0].num_datagrams = 0; |
| 707 | outbuf[0].len = 0; |
| 708 | |
| 709 | outbuf[1].ctx = &client_fd; |
| 710 | outbuf[1].description = "S -> C"; |
| 711 | outbuf[1].num_datagrams = 0; |
| 712 | outbuf[1].len = 0; |
| 713 | } |
Hanno Becker | 0cc7774 | 2017-10-31 14:10:07 +0000 | [diff] [blame] | 714 | #endif /* MBEDTLS_TIMING_C */ |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 715 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 716 | while( 1 ) |
| 717 | { |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 718 | #if defined(MBEDTLS_TIMING_C) |
| 719 | if( opt.pack > 0 ) |
| 720 | { |
| 721 | unsigned max_wait_server, max_wait_client, max_wait; |
| 722 | max_wait_server = ctx_buffer_time_remaining( &outbuf[0] ); |
| 723 | max_wait_client = ctx_buffer_time_remaining( &outbuf[1] ); |
| 724 | |
| 725 | max_wait = (unsigned) -1; |
| 726 | |
| 727 | if( max_wait_server == 0 ) |
| 728 | ctx_buffer_flush( &outbuf[0] ); |
| 729 | else |
| 730 | max_wait = max_wait_server; |
| 731 | |
| 732 | if( max_wait_client == 0 ) |
| 733 | ctx_buffer_flush( &outbuf[1] ); |
| 734 | else |
| 735 | { |
| 736 | if( max_wait_client < max_wait ) |
| 737 | max_wait = max_wait_client; |
| 738 | } |
| 739 | |
| 740 | if( max_wait != (unsigned) -1 ) |
| 741 | { |
| 742 | tm.tv_sec = max_wait / 1000; |
| 743 | tm.tv_usec = ( max_wait % 1000 ) * 1000; |
| 744 | |
| 745 | tm_ptr = &tm; |
| 746 | } |
| 747 | else |
| 748 | { |
| 749 | tm_ptr = NULL; |
| 750 | } |
| 751 | } |
| 752 | #endif /* MBEDTLS_TIMING_C */ |
| 753 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 754 | FD_ZERO( &read_fds ); |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 755 | FD_SET( server_fd.fd, &read_fds ); |
| 756 | FD_SET( client_fd.fd, &read_fds ); |
| 757 | FD_SET( listen_fd.fd, &read_fds ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 758 | |
Hanno Becker | 77abef5 | 2017-11-02 10:50:28 +0000 | [diff] [blame] | 759 | if( ( ret = select( nb_fds, &read_fds, NULL, NULL, tm_ptr ) ) < 0 ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 760 | { |
| 761 | perror( "select" ); |
| 762 | goto exit; |
| 763 | } |
| 764 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 765 | if( FD_ISSET( listen_fd.fd, &read_fds ) ) |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 766 | goto accept; |
Manuel Pégourié-Gonnard | 6312e0f | 2014-09-23 12:46:33 +0200 | [diff] [blame] | 767 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 768 | if( FD_ISSET( client_fd.fd, &read_fds ) ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 769 | { |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 770 | if( ( ret = handle_message( "S <- C", |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 771 | &server_fd, &client_fd ) ) != 0 ) |
Manuel Pégourié-Gonnard | ce8588c | 2014-10-01 00:56:03 +0200 | [diff] [blame] | 772 | goto accept; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 773 | } |
| 774 | |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 775 | if( FD_ISSET( server_fd.fd, &read_fds ) ) |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 776 | { |
Manuel Pégourié-Gonnard | 7cf3518 | 2014-09-20 09:43:48 +0200 | [diff] [blame] | 777 | if( ( ret = handle_message( "S -> C", |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 778 | &client_fd, &server_fd ) ) != 0 ) |
Manuel Pégourié-Gonnard | ce8588c | 2014-10-01 00:56:03 +0200 | [diff] [blame] | 779 | goto accept; |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 780 | } |
Hanno Becker | 1dd62ea | 2017-05-22 14:30:59 +0100 | [diff] [blame] | 781 | |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | exit: |
| 785 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 786 | #ifdef MBEDTLS_ERROR_C |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 787 | if( ret != 0 ) |
| 788 | { |
| 789 | char error_buf[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 790 | mbedtls_strerror( ret, error_buf, 100 ); |
| 791 | mbedtls_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] | 792 | fflush( stdout ); |
| 793 | } |
| 794 | #endif |
| 795 | |
Manuel Pégourié-Gonnard | 3d7d00a | 2015-06-30 15:55:03 +0200 | [diff] [blame] | 796 | mbedtls_net_free( &client_fd ); |
| 797 | mbedtls_net_free( &server_fd ); |
| 798 | mbedtls_net_free( &listen_fd ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 799 | |
| 800 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 801 | mbedtls_printf( " Press Enter to exit this program.\n" ); |
Manuel Pégourié-Gonnard | cb4137b | 2014-09-04 14:55:28 +0200 | [diff] [blame] | 802 | fflush( stdout ); getchar(); |
| 803 | #endif |
| 804 | |
| 805 | return( ret != 0 ); |
| 806 | } |
| 807 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 808 | #endif /* MBEDTLS_NET_C */ |