From 98c2b0ea7dd2f2303ea752276ec7df9e6514523f Mon Sep 17 00:00:00 2001 From: Janos Follath Date: Thu, 28 Apr 2016 23:37:16 +0100 Subject: [PATCH] Improves and makes pretty the ssl_fork_server output --- programs/ssl/ssl_fork_server.c | 75 +++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/programs/ssl/ssl_fork_server.c b/programs/ssl/ssl_fork_server.c index 4da96a66c..545e2fbf3 100644 --- a/programs/ssl/ssl_fork_server.c +++ b/programs/ssl/ssl_fork_server.c @@ -127,7 +127,7 @@ int main( void ) (const unsigned char *) pers, strlen( pers ) ) ) != 0 ) { - mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret ); + mbedtls_printf( " failed! mbedtls_ctr_drbg_seed returned %d\n\n", ret ); goto exit; } @@ -148,7 +148,7 @@ int main( void ) mbedtls_test_srv_crt_len ); if( ret != 0 ) { - mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret ); + mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret ); goto exit; } @@ -156,7 +156,7 @@ int main( void ) mbedtls_test_cas_pem_len ); if( ret != 0 ) { - mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned %d\n\n", ret ); + mbedtls_printf( " failed! mbedtls_x509_crt_parse returned %d\n\n", ret ); goto exit; } @@ -164,7 +164,7 @@ int main( void ) mbedtls_test_srv_key_len, NULL, 0 ); if( ret != 0 ) { - mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned %d\n\n", ret ); + mbedtls_printf( " failed! mbedtls_pk_parse_key returned %d\n\n", ret ); goto exit; } @@ -181,7 +181,7 @@ int main( void ) MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 ) { - mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret ); + mbedtls_printf( " failed! mbedtls_ssl_config_defaults returned %d\n\n", ret ); goto exit; } @@ -191,7 +191,7 @@ int main( void ) mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL ); if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 ) { - mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret ); + mbedtls_printf( " failed! mbedtls_ssl_conf_own_cert returned %d\n\n", ret ); goto exit; } @@ -205,7 +205,7 @@ int main( void ) if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 ) { - mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret ); + mbedtls_printf( " failed! mbedtls_net_bind returned %d\n\n", ret ); goto exit; } @@ -219,42 +219,40 @@ int main( void ) mbedtls_net_init( &client_fd ); mbedtls_ssl_init( &ssl ); - mbedtls_printf( " . Waiting for a remote connection ..." ); + mbedtls_printf( " . Waiting for a remote connection ...\n" ); fflush( stdout ); if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd, NULL, 0, NULL ) ) != 0 ) { - mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret ); + mbedtls_printf( " failed! mbedtls_net_accept returned %d\n\n", ret ); goto exit; } - mbedtls_printf( " ok\n" ); - /* * 3.5. Forking server thread */ - pid = fork(); - mbedtls_printf( " . Forking to handle connection ..." ); fflush( stdout ); + pid = fork(); + if( pid < 0 ) { - mbedtls_printf(" failed\n ! fork returned %d\n\n", pid ); + mbedtls_printf(" failed! fork returned %d\n\n", pid ); goto exit; } - mbedtls_printf( " ok\n" ); - if( pid != 0 ) { + mbedtls_printf( " ok\n" ); + if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg, (const unsigned char *) "parent", 6 ) ) != 0 ) { - mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_reseed returned %d\n", ret ); + mbedtls_printf( " failed! mbedtls_ctr_drbg_reseed returned %d\n\n", ret ); goto exit; } @@ -263,51 +261,59 @@ int main( void ) mbedtls_net_init( &listen_fd ); + pid = getpid(); + /* * 4. Setup stuff */ - mbedtls_printf( " . Setting up the SSL data...." ); + mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid ); fflush( stdout ); if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg, (const unsigned char *) "child", 5 ) ) != 0 ) { - mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_reseed returned %d\n", ret ); + mbedtls_printf( + "pid %d: SSL setup failed! mbedtls_ctr_drbg_reseed returned %d\n\n", + pid, ret ); goto exit; } if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 ) { - mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret ); + mbedtls_printf( + "pid %d: SSL setup failed! mbedtls_ssl_setup returned %d\n\n", + pid, ret ); goto exit; } mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL ); - mbedtls_printf( " ok\n" ); + mbedtls_printf( "pid %d: SSL setup ok\n", pid ); /* * 5. Handshake */ - mbedtls_printf( " . Performing the SSL/TLS handshake..." ); + mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid ); fflush( stdout ); while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 ) { if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) { - mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned %d\n\n", ret ); + mbedtls_printf( + "pid %d: SSL handshake failed! mbedtls_ssl_handshake returned %d\n\n", + pid, ret ); goto exit; } } - mbedtls_printf( " ok\n" ); + mbedtls_printf( "pid %d: SSL handshake ok\n", pid ); /* * 6. Read the HTTP Request */ - mbedtls_printf( " < Read from client:" ); + mbedtls_printf( "pid %d: Start reading from client.\n", pid ); fflush( stdout ); do @@ -324,15 +330,15 @@ int main( void ) switch( ret ) { case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: - mbedtls_printf( " connection was closed gracefully\n" ); + mbedtls_printf( "pid %d: connection was closed gracefully\n", pid ); break; case MBEDTLS_ERR_NET_CONN_RESET: - mbedtls_printf( " connection was reset by peer\n" ); + mbedtls_printf( "pid %d: connection was reset by peer\n", pid ); break; default: - mbedtls_printf( " mbedtls_ssl_read returned %d\n", ret ); + mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret ); break; } @@ -340,7 +346,7 @@ int main( void ) } len = ret; - mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf ); + mbedtls_printf( "pid %d: %d bytes read\n\n%s", pid, len, (char *) buf ); if( ret > 0 ) break; @@ -350,7 +356,7 @@ int main( void ) /* * 7. Write the 200 Response */ - mbedtls_printf( " > Write to client:" ); + mbedtls_printf( "pid %d: Start writing to client.\n", pid ); fflush( stdout ); len = sprintf( (char *) buf, HTTP_RESPONSE, @@ -362,18 +368,21 @@ int main( void ) { if( ret == MBEDTLS_ERR_NET_CONN_RESET ) { - mbedtls_printf( " failed\n ! peer closed the connection\n\n" ); + mbedtls_printf( + "pid %d: Write failed! peer closed the connection\n\n", pid ); goto exit; } if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) { - mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret ); + mbedtls_printf( + "pid %d: Write failed! mbedtls_ssl_write returned %d\n\n", + pid, ret ); goto exit; } } len = ret; - mbedtls_printf( " %d bytes written\n\n%s\n", len, (char *) buf ); + mbedtls_printf( "pid %d: %d bytes written\n\n%s\n", pid, len, (char *) buf ); mbedtls_net_usleep( 1000000 ); }