Fix remaining printfs in programs

This commit is contained in:
Manuel Pégourié-Gonnard 2015-01-29 13:29:20 +00:00
parent 226d37ac6f
commit f224678864
5 changed files with 111 additions and 91 deletions

View file

@ -26,6 +26,13 @@
#include POLARSSL_CONFIG_FILE
#endif
#if defined(POLARSSL_PLATFORM_C)
#include "polarssl/platform.h"
#else
#define polarssl_printf printf
#define polarssl_fprintf fprintf
#endif
#if !defined(POLARSSL_SSL_CLI_C) || !defined(POLARSSL_SSL_PROTO_DTLS) || \
!defined(POLARSSL_NET_C) || \
!defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
@ -38,7 +45,7 @@ int main( int argc, char *argv[] )
((void) argc);
((void) argv);
printf( "POLARSSL_SSL_CLI_C and/or POLARSSL_SSL_PROTO_DTLS and/or "
polarssl_printf( "POLARSSL_SSL_CLI_C and/or POLARSSL_SSL_PROTO_DTLS and/or "
"POLARSSL_NET_C and/or "
"POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C and/or "
"POLARSSL_X509_CRT_PARSE_C and/or POLARSSL_RSA_C and/or "
@ -72,7 +79,7 @@ static void my_debug( void *ctx, int level, const char *str )
{
((void) level);
fprintf( (FILE *) ctx, "%s", str );
polarssl_fprintf( (FILE *) ctx, "%s", str );
fflush( (FILE *) ctx );
}
@ -101,7 +108,7 @@ int main( int argc, char *argv[] )
memset( &ssl, 0, sizeof( ssl_context ) );
x509_crt_init( &cacert );
printf( "\n . Seeding the random number generator..." );
polarssl_printf( "\n . Seeding the random number generator..." );
fflush( stdout );
entropy_init( &entropy );
@ -109,16 +116,16 @@ int main( int argc, char *argv[] )
(const unsigned char *) pers,
strlen( pers ) ) ) != 0 )
{
printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
goto exit;
}
printf( " ok\n" );
polarssl_printf( " ok\n" );
/*
* 0. Initialize certificates
*/
printf( " . Loading the CA root certificate ..." );
polarssl_printf( " . Loading the CA root certificate ..." );
fflush( stdout );
#if defined(POLARSSL_CERTS_C)
@ -126,46 +133,46 @@ int main( int argc, char *argv[] )
strlen( test_ca_list ) );
#else
ret = 1;
printf("POLARSSL_CERTS_C not defined.");
polarssl_printf("POLARSSL_CERTS_C not defined.");
#endif
if( ret < 0 )
{
printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
polarssl_printf( " failed\n ! x509_crt_parse returned -0x%x\n\n", -ret );
goto exit;
}
printf( " ok (%d skipped)\n", ret );
polarssl_printf( " ok (%d skipped)\n", ret );
/*
* 1. Start the connection
*/
printf( " . Connecting to udp/%s/%4d...", SERVER_NAME,
polarssl_printf( " . Connecting to udp/%s/%4d...", SERVER_NAME,
SERVER_PORT );
fflush( stdout );
if( ( ret = net_connect( &server_fd, SERVER_ADDR,
SERVER_PORT, NET_PROTO_UDP ) ) != 0 )
{
printf( " failed\n ! net_connect returned %d\n\n", ret );
polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret );
goto exit;
}
printf( " ok\n" );
polarssl_printf( " ok\n" );
/*
* 2. Setup stuff
*/
printf( " . Setting up the DTLS structure..." );
polarssl_printf( " . Setting up the DTLS structure..." );
fflush( stdout );
if( ( ret = ssl_init( &ssl ) ) != 0 )
{
printf( " failed\n ! ssl_init returned %d\n\n", ret );
polarssl_printf( " failed\n ! ssl_init returned %d\n\n", ret );
goto exit;
}
printf( " ok\n" );
polarssl_printf( " ok\n" );
ssl_set_endpoint( &ssl, SSL_IS_CLIENT );
ssl_set_transport( &ssl, SSL_TRANSPORT_DATAGRAM );
@ -186,7 +193,7 @@ int main( int argc, char *argv[] )
/*
* 4. Handshake
*/
printf( " . Performing the SSL/TLS handshake..." );
polarssl_printf( " . Performing the SSL/TLS handshake..." );
fflush( stdout );
do ret = ssl_handshake( &ssl );
@ -195,46 +202,46 @@ int main( int argc, char *argv[] )
if( ret != 0 )
{
printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
polarssl_printf( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
goto exit;
}
printf( " ok\n" );
polarssl_printf( " ok\n" );
/*
* 5. Verify the server certificate
*/
printf( " . Verifying peer X.509 certificate..." );
polarssl_printf( " . Verifying peer X.509 certificate..." );
/* In real life, we would have used SSL_VERIFY_REQUIRED so that the
* handshake would not succeed if the peer's cert is bad. Even if we used
* SSL_VERIFY_OPTIONAL, we would bail out here if ret != 0 */
if( ( ret = ssl_get_verify_result( &ssl ) ) != 0 )
{
printf( " failed\n" );
polarssl_printf( " failed\n" );
if( ( ret & BADCERT_EXPIRED ) != 0 )
printf( " ! server certificate has expired\n" );
polarssl_printf( " ! server certificate has expired\n" );
if( ( ret & BADCERT_REVOKED ) != 0 )
printf( " ! server certificate has been revoked\n" );
polarssl_printf( " ! server certificate has been revoked\n" );
if( ( ret & BADCERT_CN_MISMATCH ) != 0 )
printf( " ! CN mismatch (expected CN=%s)\n", SERVER_NAME );
polarssl_printf( " ! CN mismatch (expected CN=%s)\n", SERVER_NAME );
if( ( ret & BADCERT_NOT_TRUSTED ) != 0 )
printf( " ! self-signed or not signed by a trusted CA\n" );
polarssl_printf( " ! self-signed or not signed by a trusted CA\n" );
printf( "\n" );
polarssl_printf( "\n" );
}
else
printf( " ok\n" );
polarssl_printf( " ok\n" );
/*
* 6. Write the echo request
*/
send_request:
printf( " > Write to server:" );
polarssl_printf( " > Write to server:" );
fflush( stdout );
len = sizeof( MESSAGE ) - 1;
@ -245,17 +252,17 @@ send_request:
if( ret < 0 )
{
printf( " failed\n ! ssl_write returned %d\n\n", ret );
polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
goto exit;
}
len = ret;
printf( " %d bytes written\n\n%s\n\n", len, MESSAGE );
polarssl_printf( " %d bytes written\n\n%s\n\n", len, MESSAGE );
/*
* 7. Read the echo response
*/
printf( " < Read from server:" );
polarssl_printf( " < Read from server:" );
fflush( stdout );
len = sizeof( buf ) - 1;
@ -270,37 +277,37 @@ send_request:
switch( ret )
{
case POLARSSL_ERR_NET_TIMEOUT:
printf( " timeout\n\n" );
polarssl_printf( " timeout\n\n" );
if( retry_left-- > 0 )
goto send_request;
goto exit;
case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
printf( " connection was closed gracefully\n" );
polarssl_printf( " connection was closed gracefully\n" );
ret = 0;
goto close_notify;
default:
printf( " ssl_read returned -0x%x\n\n", -ret );
polarssl_printf( " ssl_read returned -0x%x\n\n", -ret );
goto exit;
}
}
len = ret;
printf( " %d bytes read\n\n%s\n\n", len, buf );
polarssl_printf( " %d bytes read\n\n%s\n\n", len, buf );
/*
* 8. Done, cleanly close the connection
*/
close_notify:
printf( " . Closing the connection..." );
polarssl_printf( " . Closing the connection..." );
/* No error checking, the connection might be closed already */
do ret = ssl_close_notify( &ssl );
while( ret == POLARSSL_ERR_NET_WANT_WRITE );
ret = 0;
printf( " done\n" );
polarssl_printf( " done\n" );
/*
* 9. Final clean-ups and exit
@ -312,7 +319,7 @@ exit:
{
char error_buf[100];
polarssl_strerror( ret, error_buf, 100 );
printf( "Last error was: %d - %s\n\n", ret, error_buf );
polarssl_printf( "Last error was: %d - %s\n\n", ret, error_buf );
}
#endif
@ -325,7 +332,7 @@ exit:
entropy_free( &entropy );
#if defined(_WIN32)
printf( " + Press Enter to exit this program.\n" );
polarssl_printf( " + Press Enter to exit this program.\n" );
fflush( stdout ); getchar();
#endif

View file

@ -26,6 +26,13 @@
#include POLARSSL_CONFIG_FILE
#endif
#if defined(POLARSSL_PLATFORM_C)
#include "polarssl/platform.h"
#else
#define polarssl_printf printf
#define polarssl_fprintf fprintf
#endif
#if !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_SSL_PROTO_DTLS) || \
!defined(POLARSSL_SSL_COOKIE_C) || !defined(POLARSSL_NET_C) || \
!defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) || \
@ -73,7 +80,7 @@ static void my_debug( void *ctx, int level, const char *str )
{
((void) level);
fprintf( (FILE *) ctx, "%s", str );
polarssl_fprintf( (FILE *) ctx, "%s", str );
fflush( (FILE *) ctx );
}

View file

@ -1058,7 +1058,7 @@ int main( int argc, char *argv[] )
#if defined(POLARSSL_SSL_PROTO_DTLS)
if( ( ret = ssl_set_transport( &ssl, opt.transport ) ) != 0 )
{
printf( " failed\n ! selected transport is not available\n" );
polarssl_printf( " failed\n ! selected transport is not available\n" );
goto exit;
}
@ -1180,7 +1180,7 @@ int main( int argc, char *argv[] )
ret = ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
if( ret != 0 && opt.min_version != DFL_MIN_VERSION )
{
printf( " failed\n ! selected min_version is not available\n" );
polarssl_printf( " failed\n ! selected min_version is not available\n" );
goto exit;
}
}
@ -1190,7 +1190,7 @@ int main( int argc, char *argv[] )
ret = ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
if( ret != 0 )
{
printf( " failed\n ! selected max_version is not available\n" );
polarssl_printf( " failed\n ! selected max_version is not available\n" );
goto exit;
}
}
@ -1230,9 +1230,9 @@ int main( int argc, char *argv[] )
ssl_get_version( &ssl ), ssl_get_ciphersuite( &ssl ) );
if( ( ret = ssl_get_record_expansion( &ssl ) ) >= 0 )
printf( " [ Record expansion is %d ]\n", ret );
polarssl_printf( " [ Record expansion is %d ]\n", ret );
else
printf( " [ Record expansion is unknown (compression) ]\n" );
polarssl_printf( " [ Record expansion is unknown (compression) ]\n" );
#if defined(POLARSSL_SSL_ALPN)
if( opt.alpn_string != NULL )
@ -1373,7 +1373,7 @@ send_request:
if( ret < 0 )
{
printf( " failed\n ! ssl_write returned %d\n\n", ret );
polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
goto exit;
}
@ -1410,25 +1410,25 @@ send_request:
switch( ret )
{
case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
printf( " connection was closed gracefully\n" );
polarssl_printf( " connection was closed gracefully\n" );
ret = 0;
goto close_notify;
case 0:
case POLARSSL_ERR_NET_CONN_RESET:
printf( " connection was reset by peer\n" );
polarssl_printf( " connection was reset by peer\n" );
ret = 0;
goto reconnect;
default:
printf( " ssl_read returned -0x%x\n", -ret );
polarssl_printf( " ssl_read returned -0x%x\n", -ret );
goto exit;
}
}
len = ret;
buf[len] = '\0';
printf( " %d bytes read\n\n%s", len, (char *) buf );
polarssl_printf( " %d bytes read\n\n%s", len, (char *) buf );
/* End of message should be detected according to the syntax of the
* application protocol (eg HTTP), just use a dummy test here. */
@ -1539,7 +1539,7 @@ reconnect:
ret = net_set_block( server_fd );
if( ret != 0 )
{
printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
polarssl_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
-ret );
goto exit;
}

View file

@ -1491,7 +1491,7 @@ int main( int argc, char *argv[] )
#if defined(POLARSSL_SSL_PROTO_DTLS)
if( ( ret = ssl_set_transport( &ssl, opt.transport ) ) != 0 )
{
printf( " failed\n ! selected transport is not available\n" );
polarssl_printf( " failed\n ! selected transport is not available\n" );
goto exit;
}
@ -1565,7 +1565,7 @@ int main( int argc, char *argv[] )
if( ( ret = ssl_cookie_setup( &cookie_ctx,
ctr_drbg_random, &ctr_drbg ) ) != 0 )
{
printf( " failed\n ! ssl_cookie_setup returned %d\n\n", ret );
polarssl_printf( " failed\n ! ssl_cookie_setup returned %d\n\n", ret );
goto exit;
}
@ -1699,7 +1699,7 @@ int main( int argc, char *argv[] )
ret = ssl_set_min_version( &ssl, SSL_MAJOR_VERSION_3, opt.min_version );
if( ret != 0 && opt.min_version != DFL_MIN_VERSION )
{
printf( " failed\n ! selected min_version is not available\n" );
polarssl_printf( " failed\n ! selected min_version is not available\n" );
goto exit;
}
}
@ -1709,7 +1709,7 @@ int main( int argc, char *argv[] )
ret = ssl_set_max_version( &ssl, SSL_MAJOR_VERSION_3, opt.max_version );
if( ret != 0 )
{
printf( " failed\n ! selected max_version is not available\n" );
polarssl_printf( " failed\n ! selected max_version is not available\n" );
goto exit;
}
}
@ -1790,7 +1790,7 @@ reset:
if( ( ret = ssl_set_client_transport_id( &ssl, client_ip,
sizeof( client_ip ) ) ) != 0 )
{
printf( " failed\n ! "
polarssl_printf( " failed\n ! "
"ssl_set_client_tranport_id() returned -0x%x\n\n", -ret );
goto exit;
}
@ -1805,7 +1805,7 @@ reset:
#if defined(POLARSSL_SSL_PROTO_DTLS)
if( opt.transport == SSL_TRANSPORT_DATAGRAM )
{
printf( " . Re-bind on udp://%s:%-4d/ ...",
polarssl_printf( " . Re-bind on udp://%s:%-4d/ ...",
opt.server_addr ? opt.server_addr : "*",
opt.server_port );
fflush( stdout );
@ -1813,11 +1813,11 @@ reset:
if( ( ret = net_bind( &listen_fd, opt.server_addr,
opt.server_port, NET_PROTO_UDP ) ) != 0 )
{
printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
polarssl_printf( " failed\n ! net_bind returned -0x%x\n\n", -ret );
goto exit;
}
printf( " ok\n" );
polarssl_printf( " ok\n" );
}
#endif /* POLARSSL_SSL_PROTO_DTLS */
@ -1949,7 +1949,7 @@ data_exchange:
{
len = ret;
buf[len] = '\0';
printf( " %d bytes read\n\n%s\n", len, (char *) buf );
polarssl_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
/* End of message should be detected according to the syntax of the
* application protocol (eg HTTP), just use a dummy test here. */
@ -1967,7 +1967,7 @@ data_exchange:
larger_buf = polarssl_malloc( ori_len + extra_len + 1 );
if( larger_buf == NULL )
{
printf( " ! memory allocation failed\n" );
polarssl_printf( " ! memory allocation failed\n" );
ret = 1;
goto reset;
}
@ -1980,13 +1980,13 @@ data_exchange:
if( ret != extra_len ||
ssl_get_bytes_avail( &ssl ) != 0 )
{
printf( " ! ssl_read failed on cached data\n" );
polarssl_printf( " ! ssl_read failed on cached data\n" );
ret = 1;
goto reset;
}
larger_buf[ori_len + extra_len] = '\0';
printf( " %u bytes read (%u + %u)\n\n%s\n",
polarssl_printf( " %u bytes read (%u + %u)\n\n%s\n",
ori_len + extra_len, ori_len, extra_len,
(char *) larger_buf );
@ -2020,19 +2020,19 @@ data_exchange:
switch( ret )
{
case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
printf( " connection was closed gracefully\n" );
polarssl_printf( " connection was closed gracefully\n" );
ret = 0;
goto close_notify;
default:
printf( " ssl_read returned -0x%x\n", -ret );
polarssl_printf( " ssl_read returned -0x%x\n", -ret );
goto reset;
}
}
len = ret;
buf[len] = '\0';
printf( " %d bytes read\n\n%s", len, (char *) buf );
polarssl_printf( " %d bytes read\n\n%s", len, (char *) buf );
ret = 0;
}
@ -2099,7 +2099,7 @@ data_exchange:
if( ret < 0 )
{
printf( " failed\n ! ssl_write returned %d\n\n", ret );
polarssl_printf( " failed\n ! ssl_write returned %d\n\n", ret );
goto reset;
}

View file

@ -26,11 +26,17 @@
#include POLARSSL_CONFIG_FILE
#endif
#if defined(POLARSSL_PLATFORM_C)
#include "polarssl/platform.h"
#else
#define polarssl_printf printf
#endif
#if !defined(POLARSSL_NET_C)
#include <stdio.h>
int main( void )
{
printf( "POLARSSL_NET_C not defined.\n" );
polarssl_printf( "POLARSSL_NET_C not defined.\n" );
return( 0 );
}
#else
@ -122,11 +128,11 @@ static struct options
static void exit_usage( const char *name, const char *value )
{
if( value == NULL )
printf( " unknown option or missing value: %s\n", name );
polarssl_printf( " unknown option or missing value: %s\n", name );
else
printf( " option %s: illegal value: %s\n", name, value );
polarssl_printf( " option %s: illegal value: %s\n", name, value );
printf( USAGE );
polarssl_printf( USAGE );
exit( 1 );
}
@ -296,10 +302,10 @@ typedef struct
void print_packet( const packet *p, const char *why )
{
if( why == NULL )
printf( " %05lu %s %s (%u bytes)\n",
polarssl_printf( " %05lu %s %s (%u bytes)\n",
ellapsed_time(), p->way, p->type, p->len );
else
printf( " %s %s (%u bytes): %s\n",
polarssl_printf( " %s %s (%u bytes): %s\n",
p->way, p->type, p->len, why );
fflush( stdout );
}
@ -320,7 +326,7 @@ int send_packet( const packet *p, const char *why )
print_packet( p, "corrupted" );
if( ( ret = net_send( &dst, buf, p->len ) ) <= 0 )
{
printf( " ! net_send returned %d\n", ret );
polarssl_printf( " ! net_send returned %d\n", ret );
return( ret );
}
}
@ -328,7 +334,7 @@ int send_packet( const packet *p, const char *why )
print_packet( p, why );
if( ( ret = net_send( &dst, p->buf, p->len ) ) <= 0 )
{
printf( " ! net_send returned %d\n", ret );
polarssl_printf( " ! net_send returned %d\n", ret );
return( ret );
}
@ -341,7 +347,7 @@ int send_packet( const packet *p, const char *why )
if( ( ret = net_send( &dst, p->buf, p->len ) ) <= 0 )
{
printf( " ! net_send returned %d\n", ret );
polarssl_printf( " ! net_send returned %d\n", ret );
return( ret );
}
}
@ -402,7 +408,7 @@ int handle_message( const char *way, int dst, int src )
/* receive packet */
if( ( ret = net_recv( &src, cur.buf, sizeof( cur.buf ) ) ) <= 0 )
{
printf( " ! net_recv returned %d\n", ret );
polarssl_printf( " ! net_recv returned %d\n", ret );
return( ret );
}
@ -483,7 +489,7 @@ int main( int argc, char *argv[] )
if( opt.seed == 0 )
{
opt.seed = time( NULL );
printf( " . Pseudo-random seed: %u\n", opt.seed );
polarssl_printf( " . Pseudo-random seed: %u\n", opt.seed );
}
srand( opt.seed );
@ -491,63 +497,63 @@ int main( int argc, char *argv[] )
/*
* 0. "Connect" to the server
*/
printf( " . Connect to server on UDP/%s/%d ...",
polarssl_printf( " . Connect to server on UDP/%s/%d ...",
opt.server_addr, opt.server_port );
fflush( stdout );
if( ( ret = net_connect( &server_fd, opt.server_addr, opt.server_port,
NET_PROTO_UDP ) ) != 0 )
{
printf( " failed\n ! net_connect returned %d\n\n", ret );
polarssl_printf( " failed\n ! net_connect returned %d\n\n", ret );
goto exit;
}
printf( " ok\n" );
polarssl_printf( " ok\n" );
/*
* 1. Setup the "listening" UDP socket
*/
printf( " . Bind on UDP/%s/%d ...",
polarssl_printf( " . Bind on UDP/%s/%d ...",
opt.listen_addr, opt.listen_port );
fflush( stdout );
if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
NET_PROTO_UDP ) ) != 0 )
{
printf( " failed\n ! net_bind returned %d\n\n", ret );
polarssl_printf( " failed\n ! net_bind returned %d\n\n", ret );
goto exit;
}
printf( " ok\n" );
polarssl_printf( " ok\n" );
/*
* 2. Wait until a client connects
*/
accept:
printf( " . Waiting for a remote connection ..." );
polarssl_printf( " . Waiting for a remote connection ..." );
fflush( stdout );
if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
{
printf( " failed\n ! net_accept returned %d\n\n", ret );
polarssl_printf( " failed\n ! net_accept returned %d\n\n", ret );
goto exit;
}
printf( " ok\n" );
polarssl_printf( " ok\n" );
fflush( stdout );
printf( " . Re-bind on UDP/%s/%d ...",
polarssl_printf( " . Re-bind on UDP/%s/%d ...",
opt.listen_addr, opt.listen_port );
fflush( stdout );
if( ( ret = net_bind( &listen_fd, opt.listen_addr, opt.listen_port,
NET_PROTO_UDP ) ) != 0 )
{
printf( " failed\n ! net_bind returned %d\n\n", ret );
polarssl_printf( " failed\n ! net_bind returned %d\n\n", ret );
goto exit;
}
printf( " ok\n" );
polarssl_printf( " ok\n" );
/*
* 3. Forward packets forever (kill the process to terminate it)
@ -600,7 +606,7 @@ exit:
{
char error_buf[100];
polarssl_strerror( ret, error_buf, 100 );
printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
polarssl_printf( "Last error was: -0x%04X - %s\n\n", - ret, error_buf );
fflush( stdout );
}
#endif
@ -612,7 +618,7 @@ exit:
net_close( listen_fd );
#if defined(_WIN32)
printf( " Press Enter to exit this program.\n" );
polarssl_printf( " Press Enter to exit this program.\n" );
fflush( stdout ); getchar();
#endif