2 #include <gio/gunixsocketaddress.h>
10 gboolean verbose = FALSE;
11 gboolean non_blocking = FALSE;
12 gboolean use_udp = FALSE;
13 int cancel_timeout = 0;
15 gboolean unix_socket = FALSE;
18 static GOptionEntry cmd_entries[] = {
19 {"cancel", 'c', 0, G_OPTION_ARG_INT, &cancel_timeout,
20 "Cancel any op after the specified amount of seconds", NULL},
21 {"udp", 'u', 0, G_OPTION_ARG_NONE, &use_udp,
22 "Use udp instead of tcp", NULL},
23 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
25 {"non-blocking", 'n', 0, G_OPTION_ARG_NONE, &non_blocking,
26 "Enable non-blocking i/o", NULL},
28 {"unix", 'U', 0, G_OPTION_ARG_NONE, &unix_socket,
29 "Use a unix socket instead of IP", NULL},
31 {"timeout", 't', 0, G_OPTION_ARG_INT, &read_timeout,
32 "Time out reads after the specified number of seconds", NULL},
33 {"tls", 'T', 0, G_OPTION_ARG_NONE, &tls,
34 "Use TLS (SSL)", NULL},
38 #include "socket-common.c"
41 accept_certificate (GTlsClientConnection *conn, GTlsCertificate *cert,
42 GTlsCertificateFlags errors, gpointer user_data)
44 g_print ("Certificate would have been rejected ( ");
45 if (errors & G_TLS_CERTIFICATE_UNKNOWN_CA)
46 g_print ("unknown-ca ");
47 if (errors & G_TLS_CERTIFICATE_BAD_IDENTITY)
48 g_print ("bad-identity ");
49 if (errors & G_TLS_CERTIFICATE_NOT_ACTIVATED)
50 g_print ("not-activated ");
51 if (errors & G_TLS_CERTIFICATE_EXPIRED)
53 if (errors & G_TLS_CERTIFICATE_REVOKED)
55 if (errors & G_TLS_CERTIFICATE_INSECURE)
56 g_print ("insecure ");
57 g_print (") but accepting anyway.\n");
67 GSocketAddress *src_address;
68 GSocketAddress *address;
69 GSocketType socket_type;
70 GSocketFamily socket_family;
72 GOptionContext *context;
73 GCancellable *cancellable;
74 GSocketAddressEnumerator *enumerator;
75 GSocketConnectable *connectable;
76 GIOStream *connection;
77 GInputStream *istream;
78 GOutputStream *ostream;
84 context = g_option_context_new (" <hostname>[:port] - Test GSocket client stuff");
85 g_option_context_add_main_entries (context, cmd_entries, NULL);
86 if (!g_option_context_parse (context, &argc, &argv, &error))
88 g_printerr ("%s: %s\n", argv[0], error->message);
94 g_printerr ("%s: %s\n", argv[0], "Need to specify hostname / unix socket name");
100 g_printerr ("DTLS (TLS over UDP) is not supported");
106 cancellable = g_cancellable_new ();
107 g_thread_create (cancel_thread, cancellable, FALSE, NULL);
114 loop = g_main_loop_new (NULL, FALSE);
117 socket_type = G_SOCKET_TYPE_DATAGRAM;
119 socket_type = G_SOCKET_TYPE_STREAM;
122 socket_family = G_SOCKET_FAMILY_UNIX;
124 socket_family = G_SOCKET_FAMILY_IPV4;
126 socket = g_socket_new (socket_family, socket_type, 0, &error);
129 g_printerr ("%s: %s\n", argv[0], error->message);
134 g_socket_set_timeout (socket, read_timeout);
138 GSocketAddress *addr;
140 addr = socket_address_from_string (argv[1]);
143 g_printerr ("%s: Could not parse '%s' as unix socket name\n", argv[0], argv[1]);
146 connectable = G_SOCKET_CONNECTABLE (addr);
150 connectable = g_network_address_parse (argv[1], 7777, &error);
151 if (connectable == NULL)
153 g_printerr ("%s: %s\n", argv[0], error->message);
158 enumerator = g_socket_connectable_enumerate (connectable);
161 address = g_socket_address_enumerator_next (enumerator, cancellable, &error);
165 g_printerr ("%s: No more addresses to try\n", argv[0]);
167 g_printerr ("%s: %s\n", argv[0], error->message);
171 if (g_socket_connect (socket, address, cancellable, &error))
173 g_printerr ("%s: Connection to %s failed: %s, trying next\n", argv[0], socket_address_to_string (address), error->message);
174 g_error_free (error);
177 g_object_unref (address);
179 g_object_unref (enumerator);
181 g_print ("Connected to %s\n",
182 socket_address_to_string (address));
184 src_address = g_socket_get_local_address (socket, &error);
187 g_printerr ("Error getting local address: %s\n",
191 g_print ("local address: %s\n",
192 socket_address_to_string (src_address));
193 g_object_unref (src_address);
202 connection = G_IO_STREAM (g_socket_connection_factory_create_connection (socket));
208 tls_conn = g_tls_client_connection_new (connection, connectable, &error);
211 g_printerr ("Could not create TLS connection: %s\n",
216 g_signal_connect (tls_conn, "accept-certificate",
217 G_CALLBACK (accept_certificate), NULL);
219 if (!g_tls_connection_handshake (G_TLS_CONNECTION (tls_conn),
220 cancellable, &error))
222 g_printerr ("Error during TLS handshake: %s\n",
227 g_object_unref (connection);
228 connection = G_IO_STREAM (tls_conn);
230 g_object_unref (connectable);
234 istream = g_io_stream_get_input_stream (connection);
235 ostream = g_io_stream_get_output_stream (connection);
238 /* TODO: Test non-blocking connect/handshake */
240 g_socket_set_blocking (socket, FALSE);
248 if (fgets (buffer, sizeof buffer, stdin) == NULL)
251 to_send = strlen (buffer);
256 ensure_socket_condition (socket, G_IO_OUT, cancellable);
257 size = g_socket_send_to (socket, address,
259 cancellable, &error);
263 ensure_connection_condition (connection, G_IO_OUT, cancellable);
264 size = g_output_stream_write (ostream,
266 cancellable, &error);
271 if (g_error_matches (error,
273 G_IO_ERROR_WOULD_BLOCK))
275 g_print ("socket send would block, handling\n");
276 g_error_free (error);
282 g_printerr ("Error sending to socket: %s\n",
288 g_print ("sent %" G_GSSIZE_FORMAT " bytes of data\n", size);
292 g_printerr ("Unexpected short write\n");
301 ensure_socket_condition (socket, G_IO_IN, cancellable);
302 size = g_socket_receive_from (socket, &src_address,
303 buffer, sizeof buffer,
304 cancellable, &error);
308 ensure_connection_condition (connection, G_IO_IN, cancellable);
309 size = g_input_stream_read (istream,
310 buffer, sizeof buffer,
311 cancellable, &error);
316 g_printerr ("Error receiving from socket: %s\n",
324 g_print ("received %" G_GSSIZE_FORMAT " bytes of data", size);
326 g_print (" from %s", socket_address_to_string (src_address));
330 g_print ("-------------------------\n"
332 "-------------------------\n",
337 g_print ("closing socket\n");
341 if (!g_io_stream_close (connection, cancellable, &error))
343 g_printerr ("Error closing connection: %s\n",
347 g_object_unref (connection);
351 if (!g_socket_close (socket, &error))
353 g_printerr ("Error closing master socket: %s\n",
359 g_object_unref (socket);
360 g_object_unref (address);