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

           Branch data     Line data    Source code
       1                 :            : /* GLib testing framework examples and tests
       2                 :            :  *
       3                 :            :  * Copyright (C) 2010 Collabora, Ltd.
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :            :  *
       7                 :            :  * This library is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU Lesser General Public
       9                 :            :  * License as published by the Free Software Foundation; either
      10                 :            :  * version 2.1 of the License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This library is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :            :  * Lesser General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU Lesser General
      18                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :            :  *
      20                 :            :  * Authors: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
      21                 :            :  */
      22                 :            : 
      23                 :            : #include "config.h"
      24                 :            : 
      25                 :            : #include <stdio.h>
      26                 :            : #include <stdlib.h>
      27                 :            : #include <string.h>
      28                 :            : 
      29                 :            : #include <gio/gio.h>
      30                 :            : #include <glib.h>
      31                 :            : 
      32                 :            : #include "glibintl.h"
      33                 :            : 
      34                 :            : #ifdef G_OS_UNIX
      35                 :            : #include "gio/gunixsocketaddress.h"
      36                 :            : #endif
      37                 :            : 
      38                 :            : static const gchar *info = NULL;
      39                 :            : static GCancellable *cancellable = NULL;
      40                 :            : static gint return_value = 0;
      41                 :            : 
      42                 :            : static G_NORETURN void
      43                 :          0 : usage (void)
      44                 :            : {
      45                 :          0 :   fprintf (stderr, "Usage: proxy [-s] (uri|host:port|ip:port|path|srv/protocol/domain)\n");
      46                 :          0 :   fprintf (stderr, "       Use -t to enable threading.\n");
      47                 :          0 :   fprintf (stderr, "       Use -s to do synchronous lookups.\n");
      48                 :          0 :   fprintf (stderr, "       Use -c to cancel operation.\n");
      49                 :          0 :   fprintf (stderr, "       Use -e to use enumerator.\n");
      50                 :          0 :   fprintf (stderr, "       Use -inet to use GInetSocketAddress enumerator (ip:port).\n");
      51                 :            : #ifdef G_OS_UNIX
      52                 :          0 :   fprintf (stderr, "       Use -unix to use GUnixSocketAddress enumerator (path).\n");
      53                 :            : #endif
      54                 :          0 :   fprintf (stderr, "       Use -proxyaddr tp use GProxyAddress enumerator "
      55                 :            :                    "(ip:port:protocol:dest_host:dest_port[:username[:password]]).\n");
      56                 :          0 :   fprintf (stderr, "       Use -netaddr to use GNetworkAddress enumerator (host:port).\n");
      57                 :          0 :   fprintf (stderr, "       Use -neturi to use GNetworkAddress enumerator (uri).\n");
      58                 :          0 :   fprintf (stderr, "       Use -netsrv to use GNetworkService enumerator (srv/protocol/domain).\n");
      59                 :          0 :   fprintf (stderr, "       Use -connect to create a connection using GSocketClient object (uri).\n");
      60                 :          0 :   exit (1);
      61                 :            : }
      62                 :            : 
      63                 :            : static void
      64                 :          0 : print_and_free_error (GError *error)
      65                 :            : {
      66                 :          0 :   fprintf (stderr, "Failed to obtain proxies: %s\n", error->message);
      67                 :          0 :   g_error_free (error);
      68                 :          0 :   return_value = 1;
      69                 :          0 : }
      70                 :            : 
      71                 :            : static void
      72                 :          0 : print_proxies (const gchar *local_info, gchar **proxies)
      73                 :            : {
      74                 :          0 :   printf ("Proxies for URI '%s' are:\n", local_info);
      75                 :            : 
      76   [ #  #  #  # ]:          0 :   if (proxies == NULL || proxies[0] == NULL)
      77                 :          0 :     printf ("\tnone\n");
      78                 :            :   else
      79         [ #  # ]:          0 :     for (; proxies[0]; proxies++)
      80                 :          0 :       printf ("\t%s\n", proxies[0]);
      81                 :          0 : }
      82                 :            : 
      83                 :            : static void
      84                 :          0 : _proxy_lookup_cb (GObject *source_object,
      85                 :            :                   GAsyncResult *result,
      86                 :            :                   gpointer user_data)
      87                 :            : {
      88                 :          0 :   GError *error = NULL;
      89                 :            :   gchar **proxies;
      90                 :          0 :   GMainLoop *loop = user_data;
      91                 :            : 
      92                 :          0 :   proxies = g_proxy_resolver_lookup_finish (G_PROXY_RESOLVER (source_object),
      93                 :            :                                             result,
      94                 :            :                                             &error);
      95         [ #  # ]:          0 :   if (error)
      96                 :            :     {
      97                 :          0 :       print_and_free_error (error);
      98                 :            :     }
      99                 :            :   else
     100                 :            :     {
     101                 :          0 :       print_proxies (info, proxies);
     102                 :          0 :       g_strfreev (proxies);
     103                 :            :     }
     104                 :            : 
     105                 :          0 :   g_main_loop_quit (loop);
     106                 :          0 : }
     107                 :            : 
     108                 :            : static void
     109                 :          0 : use_resolver (gboolean synchronous)
     110                 :            : {
     111                 :            :   GProxyResolver *resolver;
     112                 :            : 
     113                 :          0 :   resolver = g_proxy_resolver_get_default ();
     114                 :            : 
     115         [ #  # ]:          0 :   if (synchronous)
     116                 :            :     {
     117                 :          0 :       GError *error = NULL;
     118                 :            :       gchar **proxies;
     119                 :            : 
     120                 :          0 :       proxies = g_proxy_resolver_lookup (resolver, info, cancellable, &error);
     121                 :            : 
     122         [ #  # ]:          0 :       if (error)
     123                 :          0 :           print_and_free_error (error);
     124                 :            :       else
     125                 :          0 :           print_proxies (info, proxies);
     126                 :            : 
     127                 :          0 :       g_strfreev (proxies);
     128                 :            :     }
     129                 :            :   else
     130                 :            :     {
     131                 :          0 :       GMainLoop *loop = g_main_loop_new (NULL, FALSE);
     132                 :            : 
     133                 :          0 :       g_proxy_resolver_lookup_async (resolver,
     134                 :            :                                      info,
     135                 :            :                                      cancellable,
     136                 :            :                                      _proxy_lookup_cb,
     137                 :            :                                      loop);
     138                 :            : 
     139                 :          0 :       g_main_loop_run (loop);
     140                 :          0 :       g_main_loop_unref (loop);
     141                 :            :     }
     142                 :          0 : }
     143                 :            : 
     144                 :            : static void
     145                 :          0 : print_proxy_address (GSocketAddress *sockaddr)
     146                 :            : {
     147                 :          0 :   GProxyAddress *proxy = NULL;
     148                 :            : 
     149         [ #  # ]:          0 :   if (sockaddr == NULL)
     150                 :            :     {
     151                 :          0 :       printf ("\tdirect://\n");
     152                 :          0 :       return;
     153                 :            :     }
     154                 :            : 
     155   [ #  #  #  #  :          0 :   if (G_IS_PROXY_ADDRESS (sockaddr))
             #  #  #  # ]
     156                 :            :     {
     157                 :          0 :       proxy = G_PROXY_ADDRESS (sockaddr);
     158                 :          0 :       printf ("\t%s://", g_proxy_address_get_protocol(proxy));
     159                 :            :     }
     160                 :            :   else
     161                 :            :     {
     162                 :          0 :       printf ("\tdirect://");
     163                 :            :     }
     164                 :            : 
     165   [ #  #  #  #  :          0 :   if (G_IS_INET_SOCKET_ADDRESS (sockaddr))
             #  #  #  # ]
     166                 :            :     {
     167                 :            :       GInetAddress *inetaddr;
     168                 :            :       guint port;
     169                 :            :       gchar *addr;
     170                 :            : 
     171                 :          0 :       g_object_get (sockaddr,
     172                 :            :                     "address", &inetaddr,
     173                 :            :                     "port", &port,
     174                 :            :                     NULL);
     175                 :            : 
     176                 :          0 :       addr = g_inet_address_to_string (inetaddr);
     177                 :            : 
     178                 :          0 :       printf ("%s:%u", addr, port);
     179                 :            : 
     180                 :          0 :       g_free (addr);
     181                 :            :     }
     182                 :            : 
     183         [ #  # ]:          0 :   if (proxy)
     184                 :            :     {
     185         [ #  # ]:          0 :       if (g_proxy_address_get_username(proxy))
     186                 :          0 :         printf (" (Username: %s  Password: %s)",
     187                 :            :                 g_proxy_address_get_username(proxy),
     188                 :            :                 g_proxy_address_get_password(proxy));
     189                 :          0 :       printf (" (Hostname: %s, Port: %i)",
     190                 :            :               g_proxy_address_get_destination_hostname (proxy),
     191                 :          0 :               g_proxy_address_get_destination_port (proxy));
     192                 :            :     }
     193                 :            : 
     194                 :          0 :   printf ("\n");
     195                 :            : }
     196                 :            : 
     197                 :            : static void
     198                 :          0 : _proxy_enumerate_cb (GObject *object,
     199                 :            :                      GAsyncResult *result,
     200                 :            :                      gpointer user_data)
     201                 :            : {
     202                 :          0 :   GError *error = NULL;
     203                 :          0 :   GMainLoop *loop = user_data;
     204                 :          0 :   GSocketAddressEnumerator *enumerator = G_SOCKET_ADDRESS_ENUMERATOR (object);
     205                 :            :   GSocketAddress *sockaddr;
     206                 :            : 
     207                 :          0 :   sockaddr = g_socket_address_enumerator_next_finish (enumerator,
     208                 :            :                                                       result,
     209                 :            :                                                       &error);
     210         [ #  # ]:          0 :   if (sockaddr)
     211                 :            :     {
     212                 :          0 :       print_proxy_address (sockaddr);
     213                 :          0 :       g_socket_address_enumerator_next_async (enumerator,
     214                 :            :                                               cancellable,
     215                 :            :                                               _proxy_enumerate_cb,
     216                 :            :                                               loop);
     217                 :          0 :       g_object_unref (sockaddr);
     218                 :            :     }
     219                 :            :   else
     220                 :            :     {
     221         [ #  # ]:          0 :       if (error)
     222                 :          0 :         print_and_free_error (error);
     223                 :            : 
     224                 :          0 :       g_main_loop_quit (loop);
     225                 :            :     }
     226                 :          0 : }
     227                 :            : 
     228                 :            : static void
     229                 :          0 : run_with_enumerator (gboolean synchronous, GSocketAddressEnumerator *enumerator)
     230                 :            : {
     231                 :          0 :   GError *error = NULL;
     232                 :            : 
     233         [ #  # ]:          0 :   if (synchronous)
     234                 :            :     {
     235                 :            :       GSocketAddress *sockaddr;
     236                 :            : 
     237         [ #  # ]:          0 :       while ((sockaddr = g_socket_address_enumerator_next (enumerator,
     238                 :            :                                                            cancellable,
     239                 :            :                                                            &error)))
     240                 :            :         {
     241                 :          0 :           print_proxy_address (sockaddr);
     242                 :          0 :           g_object_unref (sockaddr);
     243                 :            :         }
     244                 :            : 
     245         [ #  # ]:          0 :       if (error)
     246                 :          0 :         print_and_free_error (error);
     247                 :            :     }
     248                 :            :   else
     249                 :            :     {
     250                 :          0 :       GMainLoop *loop = g_main_loop_new (NULL, FALSE);
     251                 :            : 
     252                 :          0 :       g_socket_address_enumerator_next_async (enumerator,
     253                 :            :                                               cancellable,
     254                 :            :                                               _proxy_enumerate_cb,
     255                 :            :                                               loop);
     256                 :          0 :       g_main_loop_run (loop);
     257                 :          0 :       g_main_loop_unref (loop);
     258                 :            :     }
     259                 :          0 : }
     260                 :            : 
     261                 :            : static void
     262                 :          0 : use_enumerator (gboolean synchronous)
     263                 :            : {
     264                 :            :   GSocketAddressEnumerator *enumerator;
     265                 :            : 
     266                 :          0 :   enumerator = g_object_new (G_TYPE_PROXY_ADDRESS_ENUMERATOR,
     267                 :            :                              "uri", info,
     268                 :            :                              NULL);
     269                 :            : 
     270                 :          0 :   printf ("Proxies for URI '%s' are:\n", info);
     271                 :          0 :   run_with_enumerator (synchronous, enumerator);
     272                 :            : 
     273                 :          0 :   g_object_unref (enumerator);
     274                 :          0 : }
     275                 :            : 
     276                 :            : static void
     277                 :          0 : use_inet_address (gboolean synchronous)
     278                 :            : {
     279                 :            :   GSocketAddressEnumerator *enumerator;
     280                 :            :   GSocketAddress *sockaddr;
     281                 :          0 :   GInetAddress *addr = NULL;
     282                 :          0 :   guint port = 0;
     283                 :            :   gchar **ip_and_port;
     284                 :            : 
     285                 :          0 :   ip_and_port = g_strsplit (info, ":", 2);
     286                 :            : 
     287         [ #  # ]:          0 :   if (ip_and_port[0])
     288                 :            :     {
     289                 :          0 :       addr = g_inet_address_new_from_string (ip_and_port[0]);
     290         [ #  # ]:          0 :       if (ip_and_port [1])
     291                 :          0 :         port = strtoul (ip_and_port [1], NULL, 10);
     292                 :            :     }
     293                 :            : 
     294                 :          0 :   g_strfreev (ip_and_port);
     295                 :            : 
     296   [ #  #  #  #  :          0 :   if (addr == NULL || port <= 0 || port >= 65535)
                   #  # ]
     297                 :            :     {
     298                 :          0 :       fprintf (stderr, "Bad 'ip:port' parameter '%s'\n", info);
     299         [ #  # ]:          0 :       if (addr)
     300                 :          0 :         g_object_unref (addr);
     301                 :          0 :       return_value = 1;
     302                 :          0 :       return;
     303                 :            :     }
     304                 :            : 
     305                 :          0 :   sockaddr = g_inet_socket_address_new (addr, port);
     306                 :          0 :   g_object_unref (addr);
     307                 :            : 
     308                 :            :   enumerator =
     309                 :          0 :     g_socket_connectable_proxy_enumerate (G_SOCKET_CONNECTABLE (sockaddr));
     310                 :          0 :   g_object_unref (sockaddr);
     311                 :            : 
     312                 :          0 :   printf ("Proxies for ip and port '%s' are:\n", info);
     313                 :          0 :   run_with_enumerator (synchronous, enumerator);
     314                 :            : 
     315                 :          0 :   g_object_unref (enumerator);
     316                 :            : }
     317                 :            : 
     318                 :            : #ifdef G_OS_UNIX
     319                 :            : static void
     320                 :          0 : use_unix_address (gboolean synchronous)
     321                 :            : {
     322                 :            :   GSocketAddressEnumerator *enumerator;
     323                 :            :   GSocketAddress *sockaddr;
     324                 :            : 
     325                 :          0 :   sockaddr = g_unix_socket_address_new_with_type (info, -1, G_UNIX_SOCKET_ADDRESS_ABSTRACT);
     326                 :            : 
     327         [ #  # ]:          0 :   if (sockaddr == NULL)
     328                 :            :     {
     329                 :          0 :       fprintf (stderr, "Failed to create unix socket with name '%s'\n", info);
     330                 :          0 :       return_value = 1;
     331                 :          0 :       return;
     332                 :            :     }
     333                 :            : 
     334                 :            :   enumerator =
     335                 :          0 :     g_socket_connectable_proxy_enumerate (G_SOCKET_CONNECTABLE (sockaddr));
     336                 :          0 :   g_object_unref (sockaddr);
     337                 :            : 
     338                 :          0 :   printf ("Proxies for path '%s' are:\n", info);
     339                 :          0 :   run_with_enumerator (synchronous, enumerator);
     340                 :            : 
     341                 :          0 :   g_object_unref (enumerator);
     342                 :            : }
     343                 :            : #endif
     344                 :            : 
     345                 :            : static void
     346                 :          0 : use_proxy_address (gboolean synchronous)
     347                 :            : {
     348                 :            :   GSocketAddressEnumerator *enumerator;
     349                 :            :   GSocketAddress *sockaddr;
     350                 :            :   GInetAddress *addr;
     351                 :          0 :   guint port = 0;
     352                 :            :   gchar *protocol;
     353                 :            :   gchar *dest_host;
     354                 :            :   guint dest_port;
     355                 :          0 :   gchar *username = NULL;
     356                 :          0 :   gchar *password = NULL;
     357                 :            :   gchar **split_info;
     358                 :            : 
     359                 :          0 :   split_info = g_strsplit (info, ":", 7);
     360                 :            : 
     361         [ #  # ]:          0 :   if (!split_info[0]
     362         [ #  # ]:          0 :       || !split_info[1]
     363         [ #  # ]:          0 :       || !split_info[2]
     364         [ #  # ]:          0 :       || !split_info[3]
     365         [ #  # ]:          0 :       || !split_info[4])
     366                 :            :     {
     367                 :          0 :       fprintf (stderr, "Bad 'ip:port:protocol:dest_host:dest_port' parameter '%s'\n", info);
     368                 :          0 :       return_value = 1;
     369                 :          0 :       return;
     370                 :            :     }
     371                 :            : 
     372                 :          0 :   addr = g_inet_address_new_from_string (split_info[0]);
     373                 :          0 :   port = strtoul (split_info [1], NULL, 10);
     374                 :          0 :   protocol = g_strdup (split_info[2]);
     375                 :          0 :   dest_host = g_strdup (split_info[3]);
     376                 :          0 :   dest_port = strtoul (split_info[4], NULL, 10);
     377                 :            :  
     378         [ #  # ]:          0 :   if (split_info[5])
     379                 :            :     {
     380                 :          0 :       username = g_strdup (split_info[5]);
     381         [ #  # ]:          0 :       if (split_info[6])
     382                 :          0 :         password = g_strdup (split_info[6]);
     383                 :            :     }
     384                 :            : 
     385                 :          0 :   g_strfreev (split_info);
     386                 :            : 
     387                 :          0 :   sockaddr = g_proxy_address_new (addr, port,
     388                 :            :                                   protocol, dest_host, dest_port,
     389                 :            :                                   username, password);
     390                 :            :   
     391                 :          0 :   g_object_unref (addr);
     392                 :          0 :   g_free (protocol);
     393                 :          0 :   g_free (dest_host);
     394                 :          0 :   g_free (username);
     395                 :          0 :   g_free (password);
     396                 :            : 
     397                 :            :   enumerator =
     398                 :          0 :     g_socket_connectable_proxy_enumerate (G_SOCKET_CONNECTABLE (sockaddr));
     399                 :          0 :   g_object_unref (sockaddr);
     400                 :            : 
     401                 :          0 :   printf ("Proxies for ip and port '%s' are:\n", info);
     402                 :          0 :   run_with_enumerator (synchronous, enumerator);
     403                 :            : 
     404                 :          0 :   g_object_unref (enumerator);
     405                 :            : }
     406                 :            : 
     407                 :            : static void
     408                 :          0 : use_network_address (gboolean synchronous)
     409                 :            : {
     410                 :          0 :   GError *error = NULL;
     411                 :            :   GSocketAddressEnumerator *enumerator;
     412                 :            :   GSocketConnectable *connectable;
     413                 :            : 
     414                 :          0 :   connectable = g_network_address_parse (info, -1, &error);
     415                 :            : 
     416         [ #  # ]:          0 :   if (error)
     417                 :            :     {
     418                 :          0 :       print_and_free_error (error);
     419                 :          0 :       return;
     420                 :            :     }
     421                 :            : 
     422                 :          0 :   enumerator = g_socket_connectable_proxy_enumerate (connectable);
     423                 :          0 :   g_object_unref (connectable);
     424                 :            : 
     425                 :          0 :   printf ("Proxies for hostname and port '%s' are:\n", info);
     426                 :          0 :   run_with_enumerator (synchronous, enumerator);
     427                 :            : 
     428                 :          0 :   g_object_unref (enumerator);
     429                 :            : }
     430                 :            : 
     431                 :            : static void
     432                 :          0 : use_network_uri (gboolean synchronous)
     433                 :            : {
     434                 :          0 :   GError *error = NULL;
     435                 :            :   GSocketAddressEnumerator *enumerator;
     436                 :            :   GSocketConnectable *connectable;
     437                 :            : 
     438                 :          0 :   connectable = g_network_address_parse_uri (info, 0, &error);
     439                 :            : 
     440         [ #  # ]:          0 :   if (error)
     441                 :            :     {
     442                 :          0 :       print_and_free_error (error);
     443                 :          0 :       return;
     444                 :            :     }
     445                 :            : 
     446                 :          0 :   enumerator = g_socket_connectable_proxy_enumerate (connectable);
     447                 :          0 :   g_object_unref (connectable);
     448                 :            : 
     449                 :          0 :   printf ("Proxies for URI '%s' are:\n", info);
     450                 :          0 :   run_with_enumerator (synchronous, enumerator);
     451                 :            : 
     452                 :          0 :   g_object_unref (enumerator);
     453                 :            : }
     454                 :            : 
     455                 :            : static void
     456                 :          0 : use_network_service (gboolean synchronous)
     457                 :            : {
     458                 :            :   GSocketAddressEnumerator *enumerator;
     459                 :          0 :   GSocketConnectable *connectable = NULL;
     460                 :            :   gchar **split;
     461                 :            : 
     462                 :          0 :   split = g_strsplit (info, "/", 3);
     463                 :            : 
     464   [ #  #  #  #  :          0 :   if (split[0] && split[1] && split[2])
                   #  # ]
     465                 :          0 :     connectable = g_network_service_new (split[0], split[1], split[2]);
     466                 :            : 
     467                 :          0 :   g_strfreev (split);
     468                 :            : 
     469         [ #  # ]:          0 :   if (connectable == NULL)
     470                 :            :     {
     471                 :          0 :        fprintf (stderr, "Bad 'srv/protocol/domain' parameter '%s'\n", info);
     472                 :          0 :        return_value = 1;
     473                 :          0 :        return;
     474                 :            :     }
     475                 :            : 
     476                 :          0 :   enumerator = g_socket_connectable_proxy_enumerate (connectable);
     477                 :          0 :   g_object_unref (connectable);
     478                 :            : 
     479                 :          0 :   printf ("Proxies for hostname and port '%s' are:\n", info);
     480                 :          0 :   run_with_enumerator (synchronous, enumerator);
     481                 :            : 
     482                 :          0 :   g_object_unref (enumerator);
     483                 :            : }
     484                 :            : 
     485                 :            : static void
     486                 :          0 : _socket_connect_cb (GObject *object,
     487                 :            :                     GAsyncResult *result,
     488                 :            :                     gpointer user_data)
     489                 :            : {
     490                 :          0 :   GError *error = NULL;
     491                 :          0 :   GMainLoop *loop = user_data;
     492                 :          0 :   GSocketClient *client = G_SOCKET_CLIENT (object);
     493                 :            :   GSocketConnection *connection;
     494                 :            : 
     495                 :          0 :   connection = g_socket_client_connect_to_uri_finish (client,
     496                 :            :                                                       result,
     497                 :            :                                                       &error);
     498         [ #  # ]:          0 :   if (connection)
     499                 :            :     {
     500                 :            :       GSocketAddress *proxy_addr;
     501                 :          0 :       proxy_addr = g_socket_connection_get_remote_address (connection, NULL);
     502                 :          0 :       print_proxy_address (proxy_addr);
     503                 :            :     }
     504                 :            :   else
     505                 :            :     {
     506                 :          0 :       print_and_free_error (error);
     507                 :            :     }
     508                 :            : 
     509                 :          0 :   g_main_loop_quit (loop);
     510                 :          0 : }
     511                 :            : 
     512                 :            : static void
     513                 :          0 : use_socket_client (gboolean synchronous)
     514                 :            : {
     515                 :          0 :   GError *error = NULL;
     516                 :            :   GSocketClient *client;
     517                 :            : 
     518                 :          0 :   client = g_socket_client_new ();
     519                 :            : 
     520                 :          0 :   printf ("Proxies for URI '%s' are:\n", info);
     521                 :            : 
     522         [ #  # ]:          0 :   if (synchronous)
     523                 :            :     {
     524                 :            :       GSocketConnection *connection;
     525                 :            :       GSocketAddress *proxy_addr;
     526                 :            : 
     527                 :          0 :       connection = g_socket_client_connect_to_uri (client,
     528                 :            :                                                    info,
     529                 :            :                                                    0,
     530                 :            :                                                    cancellable,
     531                 :            :                                                    &error);
     532                 :            : 
     533         [ #  # ]:          0 :       if (connection)
     534                 :            :         {
     535                 :          0 :           proxy_addr = g_socket_connection_get_remote_address (connection, NULL);
     536                 :          0 :           print_proxy_address (proxy_addr);
     537                 :            :         }
     538                 :            :       else
     539                 :            :         {
     540                 :          0 :           print_and_free_error (error);
     541                 :            :         }
     542                 :            :     }
     543                 :            :   else
     544                 :            :     {
     545                 :          0 :       GMainLoop *loop = g_main_loop_new (NULL, FALSE);
     546                 :            : 
     547                 :          0 :       g_socket_client_connect_to_uri_async (client,
     548                 :            :                                             info,
     549                 :            :                                             0,
     550                 :            :                                             cancellable,
     551                 :            :                                             _socket_connect_cb,
     552                 :            :                                             loop);
     553                 :            : 
     554                 :          0 :       g_main_loop_run (loop);
     555                 :          0 :       g_main_loop_unref (loop);
     556                 :            :     }
     557                 :            : 
     558                 :          0 :   g_object_unref (client);
     559                 :          0 : }
     560                 :            : 
     561                 :            : typedef enum
     562                 :            : {
     563                 :            :   USE_RESOLVER,
     564                 :            :   USE_ENUMERATOR,
     565                 :            : #ifdef G_OS_UNIX
     566                 :            :   USE_UNIX_SOCKET_ADDRESS,
     567                 :            : #endif
     568                 :            :   USE_INET_SOCKET_ADDRESS,
     569                 :            :   USE_PROXY_ADDRESS,
     570                 :            :   USE_NETWORK_ADDRESS,
     571                 :            :   USE_NETWORK_URI,
     572                 :            :   USE_NETWORK_SERVICE,
     573                 :            :   USE_SOCKET_CLIENT,
     574                 :            : } ProxyTestType;
     575                 :            : 
     576                 :            : gint
     577                 :          0 : main (gint argc, gchar **argv)
     578                 :            : {
     579                 :          0 :   gboolean synchronous = FALSE;
     580                 :          0 :   gboolean cancel = FALSE;
     581                 :          0 :   ProxyTestType type = USE_RESOLVER;
     582                 :            : 
     583   [ #  #  #  # ]:          0 :   while (argc >= 2 && argv[1][0] == '-')
     584                 :            :     {
     585         [ #  # ]:          0 :       if (!strcmp (argv[1], "-s"))
     586                 :          0 :         synchronous = TRUE;
     587         [ #  # ]:          0 :       else if (!strcmp (argv[1], "-c"))
     588                 :          0 :         cancel = TRUE;
     589         [ #  # ]:          0 :       else if (!strcmp (argv[1], "-e"))
     590                 :          0 :         type = USE_ENUMERATOR;
     591         [ #  # ]:          0 :       else if (!strcmp (argv[1], "-inet"))
     592                 :          0 :         type = USE_INET_SOCKET_ADDRESS;
     593                 :            : #ifdef G_OS_UNIX
     594         [ #  # ]:          0 :       else if (!strcmp (argv[1], "-unix"))
     595                 :          0 :         type = USE_UNIX_SOCKET_ADDRESS;
     596                 :            : #endif
     597         [ #  # ]:          0 :       else if (!strcmp (argv[1], "-proxyaddr"))
     598                 :          0 :         type = USE_PROXY_ADDRESS;
     599         [ #  # ]:          0 :       else if (!strcmp (argv[1], "-netaddr"))
     600                 :          0 :         type = USE_NETWORK_ADDRESS;
     601         [ #  # ]:          0 :       else if (!strcmp (argv[1], "-neturi"))
     602                 :          0 :         type = USE_NETWORK_URI;
     603         [ #  # ]:          0 :       else if (!strcmp (argv[1], "-netsrv"))
     604                 :          0 :         type = USE_NETWORK_SERVICE;
     605         [ #  # ]:          0 :       else if (!strcmp (argv[1], "-connect"))
     606                 :          0 :         type = USE_SOCKET_CLIENT;
     607                 :            :       else
     608                 :          0 :         usage ();
     609                 :            : 
     610                 :          0 :       argv++;
     611                 :          0 :       argc--;
     612                 :            :     }
     613                 :            : 
     614         [ #  # ]:          0 :   if (argc != 2)
     615                 :          0 :     usage ();
     616                 :            : 
     617                 :            :   /* Save URI for asynchronous callback */
     618                 :          0 :   info = argv[1];
     619                 :            : 
     620         [ #  # ]:          0 :   if (cancel)
     621                 :            :     {
     622                 :          0 :       cancellable = g_cancellable_new ();
     623                 :          0 :       g_cancellable_cancel (cancellable);
     624                 :            :     }
     625                 :            : 
     626   [ #  #  #  #  :          0 :   switch (type)
          #  #  #  #  #  
                      # ]
     627                 :            :     {
     628                 :          0 :     case USE_RESOLVER:
     629                 :          0 :       use_resolver (synchronous);
     630                 :          0 :       break;
     631                 :          0 :     case USE_ENUMERATOR:
     632                 :          0 :       use_enumerator (synchronous);
     633                 :          0 :       break;
     634                 :          0 :     case USE_INET_SOCKET_ADDRESS:
     635                 :          0 :       use_inet_address (synchronous);
     636                 :          0 :       break;
     637                 :            : #ifdef G_OS_UNIX
     638                 :          0 :     case USE_UNIX_SOCKET_ADDRESS:
     639                 :          0 :       use_unix_address (synchronous);
     640                 :          0 :       break;
     641                 :            : #endif
     642                 :          0 :     case USE_PROXY_ADDRESS:
     643                 :          0 :       use_proxy_address (synchronous);
     644                 :          0 :       break;
     645                 :          0 :     case USE_NETWORK_ADDRESS:
     646                 :          0 :       use_network_address (synchronous);
     647                 :          0 :       break;
     648                 :          0 :     case USE_NETWORK_URI:
     649                 :          0 :       use_network_uri (synchronous);
     650                 :          0 :       break;
     651                 :          0 :     case USE_NETWORK_SERVICE:
     652                 :          0 :       use_network_service (synchronous);
     653                 :          0 :       break;
     654                 :          0 :     case USE_SOCKET_CLIENT:
     655                 :          0 :       use_socket_client (synchronous);
     656                 :          0 :       break;
     657                 :            :     }
     658                 :            : 
     659                 :          0 :   return return_value;
     660                 :            : }

Generated by: LCOV version 1.14