From b7fc2ba6d60def319c91ffd611400c3e37d05f56 Mon Sep 17 00:00:00 2001 From: hgn Date: Mon, 2 Sep 2024 19:54:04 +0100 Subject: [PATCH] blinding requirement --- servers2n.c | 77 +++++++++++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/servers2n.c b/servers2n.c index 28b5f58..5bdd910 100644 --- a/servers2n.c +++ b/servers2n.c @@ -52,6 +52,8 @@ struct client struct http_request request; struct s2n_connection *s2n; + + uint64_t blind_time; } _clients[ MAX_CLIENTS ]; int _ticks_without_action = 0; @@ -225,9 +227,10 @@ int _process_handshakes(void) if( s2n_error_get_type( s2n_errno ) != S2N_ERR_T_BLOCKED ) { write_s2n_error( stderr ); - s2n_connection_free( c->s2n ); - close( c->connfd ); - c->state = k_state_none; + c->blind_time = s2n_connection_get_delay( c->s2n ); + c->state = k_state_closing; + + printf( "Blinding for %lu\n", c->blind_time ); } else verifying ++; } @@ -238,7 +241,7 @@ int _process_handshakes(void) } /* returns number of shutting down */ -int _process_shutdowns(void) +int _process_shutdowns( uint64_t delta_ns ) { int closing = 0; for( int i=0; istate == k_state_closing ) { + if( c->blind_time ) + { + if( delta_ns < c->blind_time ) + { + c->blind_time -= delta_ns; + closing ++; + continue; + } + else + { + c->blind_time = 0; + } + } + s2n_blocked_status blocked; if( s2n_shutdown( c->s2n, &blocked ) == 0 ) { @@ -273,13 +290,13 @@ int _process_shutdowns(void) return closing; } -int s2nsend_busy( struct s2n_connection *conn, const void *buf, int len ) +int s2nsend_busy( struct client *client, const void *buf, int len ) { int written = 0; while( written < len ) { s2n_blocked_status blocked; - int w = s2n_send( conn, buf + written, len - written, &blocked); + int w = s2n_send( client->s2n, buf + written, len - written, &blocked); if( w >= 0 ) { written += w; @@ -289,7 +306,10 @@ int s2nsend_busy( struct s2n_connection *conn, const void *buf, int len ) } else { + client->blind_time = s2n_connection_get_delay( client->s2n ); + client->state = k_state_closing; write_s2n_error( stderr ); + printf( "Blinding for %lu\n", client->blind_time ); return -1; } @@ -330,18 +350,11 @@ int client_handle_requests( struct client *client ) ehttp_parse_state[ client->request.state ], client->request.line, client->request.col ); - if( s2nsend_busy( client->s2n, k_response_parsefail, - strlen( k_response_parsefail )) == -1 ) - { - s2n_connection_free( client->s2n ); - close( client->connfd ); - client->state = k_state_none; - } - else - { - client->state = k_state_closing; - } + s2nsend_busy( client, k_response_parsefail, + strlen( k_response_parsefail )); + client->state = k_state_closing; + memset( &client->request, 0, sizeof(struct http_request) ); return -1; } else @@ -356,33 +369,18 @@ int client_handle_requests( struct client *client ) client->request.resource_len ) ) { printf( "Gave website :D\n" ); - if( s2nsend_busy( client->s2n, WEBSITE, strlen(WEBSITE) ) - == -1 ) - { - s2n_connection_free( client->s2n ); - close( client->connfd ); - client->state = k_state_none; - return -1; - } + s2nsend_busy( client, WEBSITE, strlen(WEBSITE) ); } else { fprintf( stderr, "Responding #%d with 501\n", client->connfd ); - if( s2nsend_busy( client->s2n, - k_response_temp, strlen(k_response_temp) ) - == -1 ) - { - s2n_connection_free( client->s2n ); - close( client->connfd ); - client->state = k_state_none; - return -1; - } + s2nsend_busy( client, k_response_temp, + strlen(k_response_temp) ); } - printf( "Setting state to closing!\n" ); - /* reset parser */ - memset( &client->request, 0, sizeof(struct http_request) ); + printf( "Closing connection\n" ); client->state = k_state_closing; + memset( &client->request, 0, sizeof(struct http_request) ); } } } @@ -413,7 +411,6 @@ int client_handle_requests( struct client *client ) int _process_requests(void) { - int closing = 0; for( int i=0; i