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