blob: cddc142472e1fcf343052b8c08d53ac86aaa7285 [file] [log] [blame]
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +02001/*
2 * UDP proxy: emulate an unreliable UDP connexion for DTLS testing
3 *
4 * Copyright (C) 2006-2014, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#if !defined(POLARSSL_CONFIG_FILE)
27#include "polarssl/config.h"
28#else
29#include POLARSSL_CONFIG_FILE
30#endif
31
32#if !defined(POLARSSL_NET_C)
33#include <stdio.h>
34int main( void )
35{
36 printf( "POLARSSL_NET_C not defined.\n" );
37 return( 0 );
38}
39#else
40
41#include "polarssl/net.h"
42#include "polarssl/error.h"
43
44#include <stdio.h>
45#include <stdlib.h>
46
47/* For select() */
48#if (defined(_WIN32) || defined(_WIN32_WCE)) && !defined(EFIX64) && \
49 !defined(EFI32)
50#include <winsock2.h>
51#include <windows.h>
52#if defined(_MSC_VER)
53#if defined(_WIN32_WCE)
54#pragma comment( lib, "ws2.lib" )
55#else
56#pragma comment( lib, "ws2_32.lib" )
57#endif
58#endif /* _MSC_VER */
59#else /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
60#include <sys/time.h>
61#include <sys/types.h>
62#include <unistd.h>
63#endif /* ( _WIN32 || _WIN32_WCE ) && !EFIX64 && !EFI32 */
64
65#define MAX_MSG_SIZE 18445 /* 2^14 + 2048 + 13 */
66
67#define DFL_SERVER_ADDR "localhost"
68#define DFL_SERVER_PORT 4433
69#define DFL_LISTEN_ADDR "localhost"
70#define DFL_LISTEN_PORT 5556
71
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020072#define USAGE \
73 "\n usage: udp_proxy param=<>...\n" \
74 "\n acceptable parameters:\n" \
75 " server_addr=%%d default: localhost\n" \
76 " server_port=%%d default: 4433\n" \
77 " listen_addr=%%d default: localhost\n" \
78 " listen_port=%%d default: 4433\n" \
79 "\n"
80
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +020081/*
82 * global options
83 */
84static struct options
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020085{
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +020086 const char *server_addr; /* address to forward packets to */
87 int server_port; /* port to forward packets to */
88 const char *listen_addr; /* address for accepting client connections */
89 int listen_port; /* port for accepting client connections */
90} opt;
91
92static void exit_usage( const char *name, const char *value )
93{
94 if( value == NULL )
95 printf( " unknown option: %s\n", name );
96 else
97 printf( " option %s: illegal value: %s\n", name, value );
98
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +020099 printf( USAGE );
100 exit( 1 );
101}
102
103static void get_options( int argc, char *argv[] )
104{
105 int i;
106 char *p, *q;
107
108 opt.server_addr = DFL_SERVER_ADDR;
109 opt.server_port = DFL_SERVER_PORT;
110 opt.listen_addr = DFL_LISTEN_ADDR;
111 opt.listen_port = DFL_LISTEN_PORT;
112
113 for( i = 1; i < argc; i++ )
114 {
115 p = argv[i];
116 if( ( q = strchr( p, '=' ) ) == NULL )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200117 exit_usage( p, NULL );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200118 *q++ = '\0';
119
120 if( strcmp( p, "server_addr" ) == 0 )
121 opt.server_addr = q;
122 else if( strcmp( p, "server_port" ) == 0 )
123 {
124 opt.server_port = atoi( q );
125 if( opt.server_port < 1 || opt.server_port > 65535 )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200126 exit_usage( p, q );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200127 }
128 else if( strcmp( p, "listen_addr" ) == 0 )
129 opt.listen_addr = q;
130 else if( strcmp( p, "listen_port" ) == 0 )
131 {
132 opt.listen_port = atoi( q );
133 if( opt.listen_port < 1 || opt.listen_port > 65535 )
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200134 exit_usage( p, q );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200135 }
136 else
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200137 exit_usage( p, NULL );
138 }
139}
140
141static const char *msg_type( unsigned char *msg, size_t len )
142{
143 if( len < 1 ) return( "Invalid" );
144 switch( msg[0] )
145 {
146 case SSL_MSG_CHANGE_CIPHER_SPEC: return( "ChangeCipherSpec" );
147 case SSL_MSG_ALERT: return( "Alert" );
148 case SSL_MSG_APPLICATION_DATA: return( "ApplicationData" );
149 case SSL_MSG_HANDSHAKE: break; /* See below */
150 default: return( "Unknown" );
151 }
152
153 if( len < 13 ) return( "Invalid handshake" );
154 switch( msg[13] )
155 {
156 case SSL_HS_HELLO_REQUEST: return( "HelloRequest" );
157 case SSL_HS_CLIENT_HELLO: return( "ClientHello" );
158 case SSL_HS_SERVER_HELLO: return( "ServerHello" );
159 case SSL_HS_HELLO_VERIFY_REQUEST: return( "HelloVerifyRequest" );
160 case SSL_HS_NEW_SESSION_TICKET: return( "NewSessionTicket" );
161 case SSL_HS_CERTIFICATE: return( "Certificate" );
162 case SSL_HS_SERVER_KEY_EXCHANGE: return( "ServerKeyExchange" );
163 case SSL_HS_CERTIFICATE_REQUEST: return( "CertificateRequest" );
164 case SSL_HS_SERVER_HELLO_DONE: return( "ServerHelloDone" );
165 case SSL_HS_CERTIFICATE_VERIFY: return( "CertificateVerify" );
166 case SSL_HS_CLIENT_KEY_EXCHANGE: return( "ClientKeyExchange" );
167 case SSL_HS_FINISHED: return( "Finished" );
168 default: return( "Unkown handshake" );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200169 }
170}
171
172int handle_message( const char *way, int dst, int src )
173{
174 unsigned char buf[MAX_MSG_SIZE] = { 0 };
175 int ret;
176 size_t len;
177
178 if( ( ret = net_recv( &src, buf, sizeof( buf ) ) ) <= 0 )
179 {
180 printf( " ! net_recv returned %d\n", ret );
181 return( ret );
182 }
183
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200184 len = ret;
185 type = msg_type( buf, len );
186 printf( " > %s: %s (%u bytes)\n", way, type, len );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200187
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200188
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200189 printf( " < %s: %s (%u bytes): forwarded\n", way, type, len );
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200190 if( ( ret = net_send( &dst, buf, len ) ) <= 0 )
191 {
192 printf( " ! net_send returned %d\n", ret );
193 return( ret );
194 }
195
196 fflush( stdout );
197 return( 0 );
198}
199
200int main( int argc, char *argv[] )
201{
202 int ret;
203
204 int listen_fd = -1;
205 int client_fd = -1;
206 int server_fd = -1;
207
208 int nb_fds;
209 fd_set read_fds;
210
211 get_options( argc, argv );
212
213 /*
Manuel Pégourié-Gonnard44d5e632014-09-06 08:07:45 +0200214 * 0. "Connect" to the server
Manuel Pégourié-Gonnardcb4137b2014-09-04 14:55:28 +0200215 */
216 printf( " . Connect to server on UDP/%s/%d ...",
217 opt.server_addr, opt.server_port );
218 fflush( stdout );
219
220 if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
221 NET_PROTO_UDP ) ) != 0 )
222 {
223 printf( " failed\n ! net_connect returned %d\n\n", ret );
224 goto exit;
225 }
226
227 printf( " ok\n" );
228
229 /*
230 * 1. Setup the "listening" UDP socket
231 */
232 printf( " . Bind on UDP/%s/%d ...",
233 opt.listen_addr, opt.listen_port );
234 fflush( stdout );
235
236 if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
237 NET_PROTO_UDP ) ) != 0 )
238 {
239 printf( " failed\n ! net_bind returned %d\n\n", ret );
240 goto exit;
241 }
242
243 printf( " ok\n" );
244
245 /*
246 * 2. Wait until a client connects
247 */
248 printf( " . Waiting for a remote connection ..." );
249 fflush( stdout );
250
251 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
252 {
253 printf( " failed\n ! net_accept returned %d\n\n", ret );
254 goto exit;
255 }
256
257 printf( " ok\n" );
258 fflush( stdout );
259
260 /*
261 * 3. Forward packets forever (kill the process to terminate it)
262 */
263 nb_fds = ( client_fd > server_fd ? client_fd : server_fd ) + 1;
264
265 while( 1 )
266 {
267 FD_ZERO( &read_fds );
268 FD_SET( server_fd, &read_fds );
269 FD_SET( client_fd, &read_fds );
270
271 if( ( ret = select( nb_fds, &read_fds, NULL, NULL, NULL ) ) <= 0 )
272 {
273 perror( "select" );
274 goto exit;
275 }
276
277 if( FD_ISSET( client_fd, &read_fds ) )
278 {
279 if( ( ret = handle_message( "c2s", server_fd, client_fd ) ) != 0 )
280 goto exit;
281 }
282
283 if( FD_ISSET( server_fd, &read_fds ) )
284 {
285 if( ( ret = handle_message( "s2c", client_fd, server_fd ) ) != 0 )
286 goto exit;
287 }
288 }
289
290exit:
291
292#ifdef POLARSSL_ERROR_C
293 if( ret != 0 )
294 {
295 char error_buf[100];
296 polarssl_strerror( ret, error_buf, 100 );
297 printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
298 fflush( stdout );
299 }
300#endif
301
302 if( client_fd != -1 )
303 net_close( client_fd );
304
305 if( listen_fd != -1 )
306 net_close( listen_fd );
307
308#if defined(_WIN32)
309 printf( " Press Enter to exit this program.\n" );
310 fflush( stdout ); getchar();
311#endif
312
313 return( ret != 0 );
314}
315
316#endif /* POLARSSL_NET_C */