Improves and makes pretty the ssl_fork_server output

This commit is contained in:
Janos Follath 2016-04-28 23:37:16 +01:00 committed by Simon Butcher
parent e609a08c2f
commit 98c2b0ea7d

View File

@ -127,7 +127,7 @@ int main( void )
(const unsigned char *) pers, (const unsigned char *) pers,
strlen( pers ) ) ) != 0 ) 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; goto exit;
} }
@ -148,7 +148,7 @@ int main( void )
mbedtls_test_srv_crt_len ); mbedtls_test_srv_crt_len );
if( ret != 0 ) 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; goto exit;
} }
@ -156,7 +156,7 @@ int main( void )
mbedtls_test_cas_pem_len ); mbedtls_test_cas_pem_len );
if( ret != 0 ) 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; goto exit;
} }
@ -164,7 +164,7 @@ int main( void )
mbedtls_test_srv_key_len, NULL, 0 ); mbedtls_test_srv_key_len, NULL, 0 );
if( ret != 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; goto exit;
} }
@ -181,7 +181,7 @@ int main( void )
MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 ) 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; goto exit;
} }
@ -191,7 +191,7 @@ int main( void )
mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL ); mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 ) 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; goto exit;
} }
@ -205,7 +205,7 @@ int main( void )
if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 ) 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; goto exit;
} }
@ -219,42 +219,40 @@ int main( void )
mbedtls_net_init( &client_fd ); mbedtls_net_init( &client_fd );
mbedtls_ssl_init( &ssl ); mbedtls_ssl_init( &ssl );
mbedtls_printf( " . Waiting for a remote connection ..." ); mbedtls_printf( " . Waiting for a remote connection ...\n" );
fflush( stdout ); fflush( stdout );
if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd, if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
NULL, 0, NULL ) ) != 0 ) 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; goto exit;
} }
mbedtls_printf( " ok\n" );
/* /*
* 3.5. Forking server thread * 3.5. Forking server thread
*/ */
pid = fork();
mbedtls_printf( " . Forking to handle connection ..." ); mbedtls_printf( " . Forking to handle connection ..." );
fflush( stdout ); fflush( stdout );
pid = fork();
if( pid < 0 ) if( pid < 0 )
{ {
mbedtls_printf(" failed\n ! fork returned %d\n\n", pid ); mbedtls_printf(" failed! fork returned %d\n\n", pid );
goto exit; goto exit;
} }
mbedtls_printf( " ok\n" );
if( pid != 0 ) if( pid != 0 )
{ {
mbedtls_printf( " ok\n" );
if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg, if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
(const unsigned char *) "parent", (const unsigned char *) "parent",
6 ) ) != 0 ) 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; goto exit;
} }
@ -263,51 +261,59 @@ int main( void )
mbedtls_net_init( &listen_fd ); mbedtls_net_init( &listen_fd );
pid = getpid();
/* /*
* 4. Setup stuff * 4. Setup stuff
*/ */
mbedtls_printf( " . Setting up the SSL data...." ); mbedtls_printf( "pid %d: Setting up the SSL data.\n", pid );
fflush( stdout ); fflush( stdout );
if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg, if( ( ret = mbedtls_ctr_drbg_reseed( &ctr_drbg,
(const unsigned char *) "child", (const unsigned char *) "child",
5 ) ) != 0 ) 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; goto exit;
} }
if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 ) 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; goto exit;
} }
mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL ); 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 * 5. Handshake
*/ */
mbedtls_printf( " . Performing the SSL/TLS handshake..." ); mbedtls_printf( "pid %d: Performing the SSL/TLS handshake.\n", pid );
fflush( stdout ); fflush( stdout );
while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 ) while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
{ {
if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) 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; goto exit;
} }
} }
mbedtls_printf( " ok\n" ); mbedtls_printf( "pid %d: SSL handshake ok\n", pid );
/* /*
* 6. Read the HTTP Request * 6. Read the HTTP Request
*/ */
mbedtls_printf( " < Read from client:" ); mbedtls_printf( "pid %d: Start reading from client.\n", pid );
fflush( stdout ); fflush( stdout );
do do
@ -324,15 +330,15 @@ int main( void )
switch( ret ) switch( ret )
{ {
case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: 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; break;
case MBEDTLS_ERR_NET_CONN_RESET: 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; break;
default: default:
mbedtls_printf( " mbedtls_ssl_read returned %d\n", ret ); mbedtls_printf( "pid %d: mbedtls_ssl_read returned %d\n", pid, ret );
break; break;
} }
@ -340,7 +346,7 @@ int main( void )
} }
len = ret; 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 ) if( ret > 0 )
break; break;
@ -350,7 +356,7 @@ int main( void )
/* /*
* 7. Write the 200 Response * 7. Write the 200 Response
*/ */
mbedtls_printf( " > Write to client:" ); mbedtls_printf( "pid %d: Start writing to client.\n", pid );
fflush( stdout ); fflush( stdout );
len = sprintf( (char *) buf, HTTP_RESPONSE, len = sprintf( (char *) buf, HTTP_RESPONSE,
@ -362,18 +368,21 @@ int main( void )
{ {
if( ret == MBEDTLS_ERR_NET_CONN_RESET ) 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; goto exit;
} }
if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) 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; goto exit;
} }
} }
len = ret; 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 ); mbedtls_net_usleep( 1000000 );
} }