LCOV - code coverage report
Current view: top level - glib/gio/tests - socket-testserver.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 0 159 0.0 %
Date: 2024-04-23 05:16:05 Functions: 0 1 0.0 %
Branches: 0 80 0.0 %

           Branch data     Line data    Source code
       1                 :            : #include <gio/gio.h>
       2                 :            : #include <gio/gunixsocketaddress.h>
       3                 :            : #include <glib.h>
       4                 :            : #include <stdlib.h>
       5                 :            : #include <string.h>
       6                 :            : 
       7                 :            : GMainLoop *loop;
       8                 :            : 
       9                 :            : int default_port = 7777;
      10                 :            : gboolean verbose = FALSE;
      11                 :            : gboolean dont_reuse_address = FALSE;
      12                 :            : gboolean non_blocking = FALSE;
      13                 :            : gboolean use_udp = FALSE;
      14                 :            : int cancel_timeout = 0;
      15                 :            : int read_timeout = 0;
      16                 :            : int delay = 0;
      17                 :            : gboolean unix_socket = FALSE;
      18                 :            : const char *tls_cert_file = NULL;
      19                 :            : 
      20                 :            : static GOptionEntry cmd_entries[] = {
      21                 :            :   {"port", 'p', 0, G_OPTION_ARG_INT, &default_port,
      22                 :            :    "Local port to bind to", NULL},
      23                 :            :   {"cancel", 'c', 0, G_OPTION_ARG_INT, &cancel_timeout,
      24                 :            :    "Cancel any op after the specified amount of seconds", NULL},
      25                 :            :   {"udp", 'u', 0, G_OPTION_ARG_NONE, &use_udp,
      26                 :            :    "Use udp instead of tcp", NULL},
      27                 :            :   {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
      28                 :            :    "Be verbose", NULL},
      29                 :            :   {"no-reuse", 0, 0, G_OPTION_ARG_NONE, &dont_reuse_address,
      30                 :            :    "Don't SOADDRREUSE", NULL},
      31                 :            :   {"non-blocking", 'n', 0, G_OPTION_ARG_NONE, &non_blocking,
      32                 :            :    "Enable non-blocking i/o", NULL},
      33                 :            : #ifdef G_OS_UNIX
      34                 :            :   {"unix", 'U', 0, G_OPTION_ARG_NONE, &unix_socket,
      35                 :            :    "Use a unix socket instead of IP", NULL},
      36                 :            : #endif
      37                 :            :   {"delay", 'd', 0, G_OPTION_ARG_INT, &delay,
      38                 :            :    "Delay responses by the specified number of seconds", NULL},
      39                 :            :   {"timeout", 't', 0, G_OPTION_ARG_INT, &read_timeout,
      40                 :            :    "Time out reads after the specified number of seconds", NULL},
      41                 :            :   {"tls", 'T', 0, G_OPTION_ARG_STRING, &tls_cert_file,
      42                 :            :    "Use TLS (SSL) with indicated server certificate", "CERTFILE"},
      43                 :            :   G_OPTION_ENTRY_NULL
      44                 :            : };
      45                 :            : 
      46                 :            : #include "socket-common.c"
      47                 :            : 
      48                 :            : int
      49                 :          0 : main (int argc,
      50                 :            :       char *argv[])
      51                 :            : {
      52                 :            :   GSocket *socket, *new_socket, *recv_socket;
      53                 :            :   GSocketAddress *src_address;
      54                 :          0 :   GSocketAddress *address = NULL;
      55                 :            :   GSocketType socket_type;
      56                 :            :   GSocketFamily socket_family;
      57                 :          0 :   GError *error = NULL;
      58                 :            :   GOptionContext *context;
      59                 :            :   GCancellable *cancellable;
      60                 :            :   char *display_addr;
      61                 :          0 :   GTlsCertificate *tlscert = NULL;
      62                 :            :   GIOStream *connection;
      63                 :            :   GInputStream *istream;
      64                 :            :   GOutputStream *ostream;
      65                 :            : 
      66                 :          0 :   context = g_option_context_new (" - Test GSocket server stuff");
      67                 :          0 :   g_option_context_add_main_entries (context, cmd_entries, NULL);
      68         [ #  # ]:          0 :   if (!g_option_context_parse (context, &argc, &argv, &error))
      69                 :            :     {
      70                 :          0 :       g_printerr ("%s: %s\n", argv[0], error->message);
      71                 :          0 :       return 1;
      72                 :            :     }
      73                 :            : 
      74   [ #  #  #  # ]:          0 :   if (unix_socket && argc != 2)
      75                 :            :     {
      76                 :          0 :       g_printerr ("%s: %s\n", argv[0], "Need to specify unix socket name");
      77                 :          0 :       return 1;
      78                 :            :     }
      79                 :            : 
      80         [ #  # ]:          0 :   if (cancel_timeout)
      81                 :            :     {
      82                 :            :       GThread *thread;
      83                 :          0 :       cancellable = g_cancellable_new ();
      84                 :          0 :       thread = g_thread_new ("cancel", cancel_thread, cancellable);
      85                 :          0 :       g_thread_unref (thread);
      86                 :            :     }
      87                 :            :   else
      88                 :            :     {
      89                 :          0 :       cancellable = NULL;
      90                 :            :     }
      91                 :            : 
      92         [ #  # ]:          0 :   if (tls_cert_file)
      93                 :            :     {
      94         [ #  # ]:          0 :       if (use_udp)
      95                 :            :         {
      96                 :          0 :           g_printerr ("DTLS (TLS over UDP) is not supported");
      97                 :          0 :           return 1;
      98                 :            :         }
      99                 :            : 
     100                 :          0 :       tlscert = g_tls_certificate_new_from_file (tls_cert_file, &error);
     101         [ #  # ]:          0 :       if (!tlscert)
     102                 :            :         {
     103                 :          0 :           g_printerr ("Could not read server certificate '%s': %s\n",
     104                 :          0 :                       tls_cert_file, error->message);
     105                 :          0 :           return 1;
     106                 :            :         }
     107                 :            :     }
     108                 :            : 
     109                 :          0 :   loop = g_main_loop_new (NULL, FALSE);
     110                 :            : 
     111         [ #  # ]:          0 :   if (use_udp)
     112                 :          0 :     socket_type = G_SOCKET_TYPE_DATAGRAM;
     113                 :            :   else
     114                 :          0 :     socket_type = G_SOCKET_TYPE_STREAM;
     115                 :            : 
     116         [ #  # ]:          0 :   if (unix_socket)
     117                 :          0 :     socket_family = G_SOCKET_FAMILY_UNIX;
     118                 :            :   else
     119                 :          0 :     socket_family = G_SOCKET_FAMILY_IPV4;
     120                 :            : 
     121                 :          0 :   socket = g_socket_new (socket_family, socket_type, 0, &error);
     122                 :            : 
     123         [ #  # ]:          0 :   if (socket == NULL)
     124                 :            :     {
     125                 :          0 :       g_printerr ("%s: %s\n", argv[0], error->message);
     126                 :          0 :       return 1;
     127                 :            :     }
     128                 :            : 
     129         [ #  # ]:          0 :   if (non_blocking)
     130                 :          0 :     g_socket_set_blocking (socket, FALSE);
     131                 :            : 
     132         [ #  # ]:          0 :   if (unix_socket)
     133                 :            :     {
     134                 :          0 :       src_address = socket_address_from_string (argv[1]);
     135         [ #  # ]:          0 :       if (src_address == NULL)
     136                 :            :         {
     137                 :          0 :           g_printerr ("%s: Could not parse '%s' as unix socket name\n", argv[0], argv[1]);
     138                 :          0 :           return 1;
     139                 :            :         }
     140                 :            :     }
     141                 :            :   else
     142                 :            :     {
     143                 :          0 :       src_address = g_inet_socket_address_new (g_inet_address_new_any (G_SOCKET_FAMILY_IPV4), default_port);
     144                 :            :     }
     145                 :            : 
     146         [ #  # ]:          0 :   if (!g_socket_bind (socket, src_address, !dont_reuse_address, &error))
     147                 :            :     {
     148                 :          0 :       g_printerr ("Can't bind socket: %s\n", error->message);
     149                 :          0 :       return 1;
     150                 :            :     }
     151                 :          0 :   g_object_unref (src_address);
     152                 :            : 
     153         [ #  # ]:          0 :   if (!use_udp)
     154                 :            :     {
     155         [ #  # ]:          0 :       if (!g_socket_listen (socket, &error))
     156                 :            :         {
     157                 :          0 :           g_printerr ("Can't listen on socket: %s\n", error->message);
     158                 :          0 :           return 1;
     159                 :            :         }
     160                 :            : 
     161                 :          0 :       address = g_socket_get_local_address (socket, &error);
     162         [ #  # ]:          0 :       if (!address)
     163                 :            :         {
     164                 :          0 :           g_printerr ("Error getting local address: %s\n",
     165                 :          0 :                       error->message);
     166                 :          0 :           return 1;
     167                 :            :         }
     168                 :          0 :       display_addr = socket_address_to_string (address);
     169                 :          0 :       g_print ("listening on %s...\n", display_addr);
     170                 :          0 :       g_free (display_addr);
     171                 :            : 
     172                 :          0 :       ensure_socket_condition (socket, G_IO_IN, cancellable);
     173                 :          0 :       new_socket = g_socket_accept (socket, cancellable, &error);
     174         [ #  # ]:          0 :       if (!new_socket)
     175                 :            :         {
     176                 :          0 :           g_printerr ("Error accepting socket: %s\n",
     177                 :          0 :                       error->message);
     178                 :          0 :           return 1;
     179                 :            :         }
     180                 :            : 
     181         [ #  # ]:          0 :       if (non_blocking)
     182                 :          0 :         g_socket_set_blocking (new_socket, FALSE);
     183         [ #  # ]:          0 :       if (read_timeout)
     184                 :          0 :         g_socket_set_timeout (new_socket, read_timeout);
     185                 :            : 
     186                 :          0 :       address = g_socket_get_remote_address (new_socket, &error);
     187         [ #  # ]:          0 :       if (!address)
     188                 :            :         {
     189                 :          0 :           g_printerr ("Error getting remote address: %s\n",
     190                 :          0 :                       error->message);
     191                 :          0 :           return 1;
     192                 :            :         }
     193                 :            : 
     194                 :          0 :       display_addr = socket_address_to_string (address);
     195                 :          0 :       g_print ("got a new connection from %s\n", display_addr);
     196                 :          0 :       g_free(display_addr);
     197                 :          0 :       g_object_unref (address);
     198                 :            : 
     199                 :          0 :       recv_socket = new_socket;
     200                 :            : 
     201                 :          0 :       connection = G_IO_STREAM (g_socket_connection_factory_create_connection (recv_socket));
     202                 :          0 :       g_object_unref (new_socket);
     203                 :            :     }
     204                 :            :   else
     205                 :            :     {
     206                 :          0 :       recv_socket = socket;
     207                 :          0 :       connection = NULL;
     208                 :            :     }
     209                 :            : 
     210         [ #  # ]:          0 :   if (tlscert)
     211                 :            :     {
     212                 :            :       GIOStream *tls_conn;
     213                 :            : 
     214                 :          0 :       tls_conn = g_tls_server_connection_new (connection, tlscert, &error);
     215         [ #  # ]:          0 :       if (!tls_conn)
     216                 :            :         {
     217                 :          0 :           g_printerr ("Could not create TLS connection: %s\n",
     218                 :          0 :                       error->message);
     219                 :          0 :           return 1;
     220                 :            :         }
     221                 :            : 
     222         [ #  # ]:          0 :       if (!g_tls_connection_handshake (G_TLS_CONNECTION (tls_conn),
     223                 :            :                                        cancellable, &error))
     224                 :            :         {
     225                 :          0 :           g_printerr ("Error during TLS handshake: %s\n",
     226                 :          0 :                       error->message);
     227                 :          0 :           return 1;
     228                 :            :        }
     229                 :            : 
     230                 :          0 :       g_object_unref (connection);
     231                 :          0 :       connection = tls_conn;
     232                 :            :     }
     233                 :            : 
     234         [ #  # ]:          0 :   if (connection)
     235                 :            :     {
     236                 :          0 :       istream = g_io_stream_get_input_stream (connection);
     237                 :          0 :       ostream = g_io_stream_get_output_stream (connection);
     238                 :            :     }
     239                 :            :   else
     240                 :            :     {
     241                 :          0 :       g_assert (use_udp);
     242                 :          0 :       istream = NULL;
     243                 :          0 :       ostream = NULL;
     244                 :            :     }
     245                 :            : 
     246                 :            :   while (TRUE)
     247                 :          0 :     {
     248                 :            :       gchar buffer[4096];
     249                 :            :       gssize size;
     250                 :            :       gsize to_send;
     251                 :            : 
     252         [ #  # ]:          0 :       if (use_udp)
     253                 :            :         {
     254                 :          0 :           ensure_socket_condition (recv_socket, G_IO_IN, cancellable);
     255                 :          0 :           size = g_socket_receive_from (recv_socket, &address,
     256                 :            :                                         buffer, sizeof buffer,
     257                 :            :                                         cancellable, &error);
     258                 :            :         }
     259                 :            :       else
     260                 :            :         {
     261                 :          0 :           ensure_connection_condition (connection, G_IO_IN, cancellable);
     262                 :          0 :           size = g_input_stream_read (istream,
     263                 :            :                                       buffer, sizeof buffer,
     264                 :            :                                       cancellable, &error);
     265                 :            :         }
     266                 :            : 
     267         [ #  # ]:          0 :       if (size < 0)
     268                 :            :         {
     269                 :          0 :           g_printerr ("Error receiving from socket: %s\n",
     270                 :          0 :                       error->message);
     271                 :          0 :           return 1;
     272                 :            :         }
     273                 :            : 
     274         [ #  # ]:          0 :       if (size == 0)
     275                 :          0 :         break;
     276                 :            : 
     277                 :          0 :       g_print ("received %" G_GSSIZE_FORMAT " bytes of data", size);
     278         [ #  # ]:          0 :       if (use_udp)
     279                 :          0 :         g_print (" from %s", socket_address_to_string (address));
     280                 :          0 :       g_print ("\n");
     281                 :            : 
     282         [ #  # ]:          0 :       if (verbose)
     283                 :          0 :         g_print ("-------------------------\n"
     284                 :            :                  "%.*s\n"
     285                 :            :                  "-------------------------\n",
     286                 :            :                  (int)size, buffer);
     287                 :            : 
     288                 :          0 :       to_send = size;
     289                 :            : 
     290         [ #  # ]:          0 :       if (delay)
     291                 :            :         {
     292         [ #  # ]:          0 :           if (verbose)
     293                 :          0 :             g_print ("delaying %d seconds before response\n", delay);
     294                 :          0 :           g_usleep (1000 * 1000 * delay);
     295                 :            :         }
     296                 :            : 
     297         [ #  # ]:          0 :       while (to_send > 0)
     298                 :            :         {
     299         [ #  # ]:          0 :           if (use_udp)
     300                 :            :             {
     301                 :          0 :               ensure_socket_condition (recv_socket, G_IO_OUT, cancellable);
     302                 :          0 :               size = g_socket_send_to (recv_socket, address,
     303                 :            :                                        buffer, to_send, cancellable, &error);
     304                 :            :             }
     305                 :            :           else
     306                 :            :             {
     307                 :          0 :               ensure_connection_condition (connection, G_IO_OUT, cancellable);
     308                 :          0 :               size = g_output_stream_write (ostream,
     309                 :            :                                             buffer, to_send,
     310                 :            :                                             cancellable, &error);
     311                 :            :             }
     312                 :            : 
     313         [ #  # ]:          0 :           if (size < 0)
     314                 :            :             {
     315         [ #  # ]:          0 :               if (g_error_matches (error,
     316                 :            :                                    G_IO_ERROR,
     317                 :            :                                    G_IO_ERROR_WOULD_BLOCK))
     318                 :            :                 {
     319                 :          0 :                   g_print ("socket send would block, handling\n");
     320                 :          0 :                   g_error_free (error);
     321                 :          0 :                   error = NULL;
     322                 :          0 :                   continue;
     323                 :            :                 }
     324                 :            :               else
     325                 :            :                 {
     326                 :          0 :                   g_printerr ("Error sending to socket: %s\n",
     327                 :          0 :                               error->message);
     328                 :          0 :                   return 1;
     329                 :            :                 }
     330                 :            :             }
     331                 :            : 
     332                 :          0 :           g_print ("sent %" G_GSSIZE_FORMAT " bytes of data\n", size);
     333                 :            : 
     334         [ #  # ]:          0 :           if (size == 0)
     335                 :            :             {
     336                 :          0 :               g_printerr ("Unexpected short write\n");
     337                 :          0 :               return 1;
     338                 :            :             }
     339                 :            : 
     340                 :          0 :           to_send -= size;
     341                 :            :         }
     342                 :            :     }
     343                 :            : 
     344                 :          0 :   g_print ("connection closed\n");
     345                 :            : 
     346         [ #  # ]:          0 :   if (connection)
     347                 :            :     {
     348         [ #  # ]:          0 :       if (!g_io_stream_close (connection, NULL, &error))
     349                 :            :         {
     350                 :          0 :           g_printerr ("Error closing connection stream: %s\n",
     351                 :          0 :                       error->message);
     352                 :          0 :           return 1;
     353                 :            :         }
     354                 :          0 :       g_object_unref (connection);
     355                 :            :     }
     356                 :            : 
     357         [ #  # ]:          0 :   if (!g_socket_close (socket, &error))
     358                 :            :     {
     359                 :          0 :       g_printerr ("Error closing socket: %s\n",
     360                 :          0 :                   error->message);
     361                 :          0 :       return 1;
     362                 :            :     }
     363                 :          0 :   g_object_unref (socket);
     364                 :            : 
     365                 :          0 :   return 0;
     366                 :            : }

Generated by: LCOV version 1.14