blinding requirement master
authorhgn <hgodden00@gmail.com>
Mon, 2 Sep 2024 18:54:04 +0000 (19:54 +0100)
committerhgn <hgodden00@gmail.com>
Mon, 2 Sep 2024 18:54:04 +0000 (19:54 +0100)
servers2n.c

index 28b5f58ee1b94de9b8fbee8104b2fb0b688195f7..5bdd910a844a0cbf78034dac28513bdd8fbbaf71 100644 (file)
@@ -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; i<MAX_CLIENTS; i ++ )
@@ -246,6 +249,20 @@ int _process_shutdowns(void)
       struct client *c = &_clients[ i ];
       if( c->state == 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<MAX_CLIENTS; i ++ )
    {
       struct client *c = &_clients[ i ];
@@ -479,7 +476,7 @@ int main(int argc, char *argv[])
    {
       _accept_any_new_client( listenfd, s2n_conf );
       _process_handshakes();
-      _process_shutdowns();
+      _process_shutdowns( 5000000 );
       _process_requests();
 
       struct timespec spec;
@@ -491,7 +488,7 @@ int main(int argc, char *argv[])
    printf( "Closing active connections\n" );
    while(1)
    {
-      if( _process_shutdowns() )
+      if( _process_shutdowns( 100000000 ) )
       {
          struct timespec spec;
          spec.tv_sec = 0;