libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

test_https_time_out.c (7032B)


      1 /*
      2  This file is part of libmicrohttpd
      3  Copyright (C) 2007 Christian Grothoff
      4  Copyright (C) 2014-2022 Evgeny Grin (Karlson2k)
      5 
      6  libmicrohttpd is free software; you can redistribute it and/or modify
      7  it under the terms of the GNU General Public License as published
      8  by the Free Software Foundation; either version 2, or (at your
      9  option) any later version.
     10 
     11  libmicrohttpd is distributed in the hope that it will be useful, but
     12  WITHOUT ANY WARRANTY; without even the implied warranty of
     13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  General Public License for more details.
     15 
     16  You should have received a copy of the GNU General Public License
     17  along with libmicrohttpd; see the file COPYING.  If not, write to the
     18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     19  Boston, MA 02110-1301, USA.
     20  */
     21 
     22 /**
     23  * @file test_https_time_out.c
     24  * @brief: daemon TLS timeout test
     25  *
     26  * @author Sagie Amir
     27  * @author Karlson2k (Evgeny Grin)
     28  */
     29 
     30 #include "platform.h"
     31 #include "microhttpd.h"
     32 #include "tls_test_common.h"
     33 #ifdef MHD_HTTPS_REQUIRE_GCRYPT
     34 #include <gcrypt.h>
     35 #endif /* MHD_HTTPS_REQUIRE_GCRYPT */
     36 #ifdef HAVE_SIGNAL_H
     37 #include <signal.h>
     38 #endif /* HAVE_SIGNAL_H */
     39 #ifdef HAVE_UNISTD_H
     40 #include <unistd.h>
     41 #endif /* HAVE_UNISTD_H */
     42 #ifdef HAVE_TIME_H
     43 #include <time.h>
     44 #endif /* HAVE_TIME_H */
     45 
     46 #include "mhd_sockets.h" /* only macros used */
     47 
     48 
     49 #ifdef _WIN32
     50 #ifndef WIN32_LEAN_AND_MEAN
     51 #define WIN32_LEAN_AND_MEAN 1
     52 #endif /* !WIN32_LEAN_AND_MEAN */
     53 #include <windows.h>
     54 #endif
     55 #include "tls_test_keys.h"
     56 
     57 static const unsigned int timeout_val = 2;
     58 
     59 static volatile unsigned int num_connects = 0;
     60 static volatile unsigned int num_disconnects = 0;
     61 
     62 
     63 /**
     64  * Pause execution for specified number of milliseconds.
     65  * @param ms the number of milliseconds to sleep
     66  */
     67 static void
     68 _MHD_sleep (uint32_t ms)
     69 {
     70 #if defined(_WIN32)
     71   Sleep (ms);
     72 #elif defined(HAVE_NANOSLEEP)
     73   struct timespec slp = {ms / 1000, (ms % 1000) * 1000000};
     74   struct timespec rmn;
     75   int num_retries = 0;
     76   while (0 != nanosleep (&slp, &rmn))
     77   {
     78     if (num_retries++ > 8)
     79       break;
     80     slp = rmn;
     81   }
     82 #elif defined(HAVE_USLEEP)
     83   uint64_t us = ms * 1000;
     84   do
     85   {
     86     uint64_t this_sleep;
     87     if (999999 < us)
     88       this_sleep = 999999;
     89     else
     90       this_sleep = us;
     91     /* Ignore return value as it could be void */
     92     usleep (this_sleep);
     93     us -= this_sleep;
     94   } while (us > 0);
     95 #else
     96   sleep ((ms + 999) / 1000);
     97 #endif
     98 }
     99 
    100 
    101 static void
    102 socket_cb (void *cls,
    103            struct MHD_Connection *c,
    104            void **socket_context,
    105            enum MHD_ConnectionNotificationCode toe)
    106 {
    107   if (NULL == socket_context)
    108     abort ();
    109   if (NULL == c)
    110     abort ();
    111   if (NULL != cls)
    112     abort ();
    113 
    114   if (MHD_CONNECTION_NOTIFY_STARTED == toe)
    115   {
    116     num_connects++;
    117 #ifdef _DEBUG
    118     fprintf (stderr, "MHD: Connection has started.\n");
    119 #endif /* _DEBUG */
    120   }
    121   else if (MHD_CONNECTION_NOTIFY_CLOSED == toe)
    122   {
    123     num_disconnects++;
    124 #ifdef _DEBUG
    125     fprintf (stderr, "MHD: Connection has closed.\n");
    126 #endif /* _DEBUG */
    127   }
    128   else
    129     abort ();
    130 }
    131 
    132 
    133 static unsigned int
    134 test_tls_session_time_out (gnutls_session_t session, uint16_t port)
    135 {
    136   int ret;
    137   MHD_socket sd;
    138   struct sockaddr_in sa;
    139 
    140   sd = socket (AF_INET, SOCK_STREAM, 0);
    141   if (sd == MHD_INVALID_SOCKET)
    142   {
    143     fprintf (stderr, "Failed to create socket: %s\n", strerror (errno));
    144     return 2;
    145   }
    146 
    147   memset (&sa, '\0', sizeof (struct sockaddr_in));
    148   sa.sin_family = AF_INET;
    149   sa.sin_port = htons (port);
    150   sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
    151 
    152   ret = connect (sd, (struct sockaddr *) &sa, sizeof (struct sockaddr_in));
    153 
    154   if (ret < 0)
    155   {
    156     fprintf (stderr, "Error: %s\n", MHD_E_FAILED_TO_CONNECT);
    157     MHD_socket_close_chk_ (sd);
    158     return 2;
    159   }
    160 
    161 #if (GNUTLS_VERSION_NUMBER + 0 >= 0x030109) && ! defined(_WIN64)
    162   gnutls_transport_set_int (session, (int) (sd));
    163 #else  /* GnuTLS before 3.1.9 or Win64 */
    164   gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) (intptr_t) (sd));
    165 #endif /* GnuTLS before 3.1.9 or Win64 */
    166 
    167   ret = gnutls_handshake (session);
    168   if (ret < 0)
    169   {
    170     fprintf (stderr, "Handshake failed\n");
    171     MHD_socket_close_chk_ (sd);
    172     return 2;
    173   }
    174 
    175   _MHD_sleep (timeout_val * 1000 + 1700);
    176 
    177   if (0 == num_connects)
    178   {
    179     fprintf (stderr, "MHD has not detected any connection attempt.\n");
    180     MHD_socket_close_chk_ (sd);
    181     return 4;
    182   }
    183   /* check that server has closed the connection */
    184   if (0 == num_disconnects)
    185   {
    186     fprintf (stderr, "MHD has not detected any disconnections.\n");
    187     MHD_socket_close_chk_ (sd);
    188     return 1;
    189   }
    190 
    191   MHD_socket_close_chk_ (sd);
    192   return 0;
    193 }
    194 
    195 
    196 int
    197 main (int argc, char *const *argv)
    198 {
    199   unsigned int errorCount = 0;
    200   struct MHD_Daemon *d;
    201   gnutls_session_t session;
    202   gnutls_certificate_credentials_t xcred;
    203   uint16_t port;
    204   (void) argc;   /* Unused. Silent compiler warning. */
    205 
    206   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    207     port = 0;
    208   else
    209     port = 3070;
    210 
    211 #ifdef MHD_SEND_SPIPE_SUPPRESS_NEEDED
    212 #if defined(HAVE_SIGNAL_H) && defined(SIGPIPE)
    213   if (SIG_ERR == signal (SIGPIPE, SIG_IGN))
    214   {
    215     fprintf (stderr, "Error suppressing SIGPIPE signal.\n");
    216     exit (99);
    217   }
    218 #else /* ! HAVE_SIGNAL_H || ! SIGPIPE */
    219   fprintf (stderr, "Cannot suppress SIGPIPE signal.\n");
    220   /* exit (77); */
    221 #endif
    222 #endif /* MHD_SEND_SPIPE_SUPPRESS_NEEDED */
    223 
    224 #ifdef MHD_HTTPS_REQUIRE_GCRYPT
    225   gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
    226 #ifdef GCRYCTL_INITIALIZATION_FINISHED
    227   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
    228 #endif
    229 #endif /* MHD_HTTPS_REQUIRE_GCRYPT */
    230   if (GNUTLS_E_SUCCESS != gnutls_global_init ())
    231   {
    232     fprintf (stderr, "Cannot initialize GnuTLS.\n");
    233     exit (99);
    234   }
    235   gnutls_global_set_log_level (11);
    236 
    237   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
    238                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS
    239                         | MHD_USE_ERROR_LOG, port,
    240                         NULL, NULL, &http_dummy_ahc, NULL,
    241                         MHD_OPTION_CONNECTION_TIMEOUT,
    242                         (unsigned int) timeout_val,
    243                         MHD_OPTION_NOTIFY_CONNECTION, &socket_cb, NULL,
    244                         MHD_OPTION_HTTPS_MEM_KEY, srv_self_signed_key_pem,
    245                         MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
    246                         MHD_OPTION_END);
    247 
    248   if (NULL == d)
    249   {
    250     fprintf (stderr, MHD_E_SERVER_INIT);
    251     return -1;
    252   }
    253   if (0 == port)
    254   {
    255     const union MHD_DaemonInfo *dinfo;
    256     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    257     if ((NULL == dinfo) || (0 == dinfo->port) )
    258     {
    259       MHD_stop_daemon (d);
    260       return 99;
    261     }
    262     port = dinfo->port;
    263   }
    264 
    265   if (0 != setup_session (&session, &xcred))
    266   {
    267     fprintf (stderr, "failed to setup session\n");
    268     return 1;
    269   }
    270   errorCount += test_tls_session_time_out (session, port);
    271   teardown_session (session, xcred);
    272 
    273   print_test_result (errorCount, argv[0]);
    274 
    275   MHD_stop_daemon (d);
    276   gnutls_global_deinit ();
    277 
    278   return errorCount != 0 ? 1 : 0;
    279 }