2 #include <gio/gunixsocketaddress.h>
8 #include "gtlsconsoleinteraction.h"
12 gboolean verbose = FALSE;
13 gboolean non_blocking = FALSE;
14 gboolean use_udp = FALSE;
15 int cancel_timeout = 0;
17 gboolean unix_socket = FALSE;
20 static GOptionEntry cmd_entries[] = {
21 {"cancel", 'c', 0, G_OPTION_ARG_INT, &cancel_timeout,
22 "Cancel any op after the specified amount of seconds", NULL},
23 {"udp", 'u', 0, G_OPTION_ARG_NONE, &use_udp,
24 "Use udp instead of tcp", NULL},
25 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
27 {"non-blocking", 'n', 0, G_OPTION_ARG_NONE, &non_blocking,
28 "Enable non-blocking i/o", NULL},
30 {"unix", 'U', 0, G_OPTION_ARG_NONE, &unix_socket,
31 "Use a unix socket instead of IP", NULL},
33 {"timeout", 't', 0, G_OPTION_ARG_INT, &read_timeout,
34 "Time out reads after the specified number of seconds", NULL},
35 {"tls", 'T', 0, G_OPTION_ARG_NONE, &tls,
36 "Use TLS (SSL)", NULL},
40 #include "socket-common.c"
43 accept_certificate (GTlsClientConnection *conn, GTlsCertificate *cert,
44 GTlsCertificateFlags errors, gpointer user_data)
46 g_print ("Certificate would have been rejected ( ");
47 if (errors & G_TLS_CERTIFICATE_UNKNOWN_CA)
48 g_print ("unknown-ca ");
49 if (errors & G_TLS_CERTIFICATE_BAD_IDENTITY)
50 g_print ("bad-identity ");
51 if (errors & G_TLS_CERTIFICATE_NOT_ACTIVATED)
52 g_print ("not-activated ");
53 if (errors & G_TLS_CERTIFICATE_EXPIRED)
55 if (errors & G_TLS_CERTIFICATE_REVOKED)
57 if (errors & G_TLS_CERTIFICATE_INSECURE)
58 g_print ("insecure ");
59 g_print (") but accepting anyway.\n");
64 static GTlsCertificate *
65 lookup_client_certificate (GTlsClientConnection *conn, GError **error)
68 GList *c, *certificates;
69 GTlsDatabase *database;
70 GTlsCertificate *certificate = NULL;
73 accepted = g_tls_client_connection_get_accepted_cas (conn);
74 for (l = accepted; l != NULL; l = g_list_next (l))
76 base = G_TLS_CONNECTION (conn);
77 database = g_tls_connection_get_database (base);
78 certificates = g_tls_database_lookup_certificates_issued_by (database, l->data,
79 g_tls_connection_get_interaction (base),
80 G_TLS_DATABASE_LOOKUP_KEYPAIR,
86 certificate = g_object_ref (certificates->data);
88 for (c = certificates; c != NULL; c = g_list_next (c))
89 g_object_unref (c->data);
90 g_list_free (certificates);
93 for (l = accepted; l != NULL; l = g_list_next (l))
94 g_byte_array_unref (l->data);
95 g_list_free (accepted);
97 if (certificate == NULL && error && !*error)
98 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_CERTIFICATE_REQUIRED,
99 "Server requested a certificate, but could not find relevant certificate in database.");
104 make_connection (const char *argument, GTlsCertificate *certificate, GCancellable *cancellable,
105 GSocket **socket, GSocketAddress **address, GIOStream **connection,
106 GInputStream **istream, GOutputStream **ostream, GError **error)
108 GSocketType socket_type;
109 GSocketFamily socket_family;
110 GSocketAddressEnumerator *enumerator;
111 GSocketConnectable *connectable;
112 GSocketAddress *src_address;
113 GTlsInteraction *interaction;
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);
131 g_socket_set_timeout (*socket, read_timeout);
135 GSocketAddress *addr;
137 addr = socket_address_from_string (argument);
140 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
141 "Could not parse '%s' as unix socket name", argument);
144 connectable = G_SOCKET_CONNECTABLE (addr);
148 connectable = g_network_address_parse (argument, 7777, error);
149 if (connectable == NULL)
153 enumerator = g_socket_connectable_enumerate (connectable);
156 *address = g_socket_address_enumerator_next (enumerator, cancellable, error);
157 if (*address == NULL)
160 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
161 "No more addresses to try");
165 if (g_socket_connect (*socket, *address, cancellable, &err))
167 g_message ("Connection to %s failed: %s, trying next\n", socket_address_to_string (*address), err->message);
168 g_clear_error (&err);
170 g_object_unref (*address);
172 g_object_unref (enumerator);
174 g_print ("Connected to %s\n",
175 socket_address_to_string (*address));
177 src_address = g_socket_get_local_address (*socket, error);
180 g_prefix_error (error, "Error getting local address: ");
184 g_print ("local address: %s\n",
185 socket_address_to_string (src_address));
186 g_object_unref (src_address);
195 *connection = G_IO_STREAM (g_socket_connection_factory_create_connection (*socket));
201 tls_conn = g_tls_client_connection_new (*connection, connectable, error);
204 g_prefix_error (error, "Could not create TLS connection: ");
208 g_signal_connect (tls_conn, "accept-certificate",
209 G_CALLBACK (accept_certificate), NULL);
211 interaction = g_tls_console_interaction_new ();
212 g_tls_connection_set_interaction (G_TLS_CONNECTION (tls_conn), interaction);
213 g_object_unref (interaction);
216 g_tls_connection_set_certificate (G_TLS_CONNECTION (tls_conn), certificate);
218 g_object_unref (*connection);
219 *connection = G_IO_STREAM (tls_conn);
221 if (!g_tls_connection_handshake (G_TLS_CONNECTION (tls_conn),
224 g_prefix_error (error, "Error during TLS handshake: ");
228 g_object_unref (connectable);
232 *istream = g_io_stream_get_input_stream (*connection);
233 *ostream = g_io_stream_get_output_stream (*connection);
244 GSocketAddress *address;
245 GError *error = NULL;
246 GOptionContext *context;
247 GCancellable *cancellable;
248 GIOStream *connection;
249 GInputStream *istream;
250 GOutputStream *ostream;
251 GSocketAddress *src_address;
252 GTlsCertificate *certificate = NULL;
255 g_thread_init (NULL);
262 context = g_option_context_new (" <hostname>[:port] - Test GSocket client stuff");
263 g_option_context_add_main_entries (context, cmd_entries, NULL);
264 if (!g_option_context_parse (context, &argc, &argv, &error))
266 g_printerr ("%s: %s\n", argv[0], error->message);
272 g_printerr ("%s: %s\n", argv[0], "Need to specify hostname / unix socket name");
278 g_printerr ("DTLS (TLS over UDP) is not supported");
284 cancellable = g_cancellable_new ();
285 g_thread_create (cancel_thread, cancellable, FALSE, NULL);
292 loop = g_main_loop_new (NULL, FALSE);
294 for (i = 0; i < 2; i++)
296 if (make_connection (argv[1], certificate, cancellable, &socket, &address,
297 &connection, &istream, &ostream, &error))
300 if (g_error_matches (error, G_TLS_ERROR, G_TLS_ERROR_CERTIFICATE_REQUIRED))
302 g_clear_error (&error);
303 certificate = lookup_client_certificate (G_TLS_CLIENT_CONNECTION (connection), &error);
304 if (certificate != NULL)
308 g_printerr ("%s: %s", argv[0], error->message);
312 /* TODO: Test non-blocking connect/handshake */
314 g_socket_set_blocking (socket, FALSE);
322 if (fgets (buffer, sizeof buffer, stdin) == NULL)
325 to_send = strlen (buffer);
330 ensure_socket_condition (socket, G_IO_OUT, cancellable);
331 size = g_socket_send_to (socket, address,
333 cancellable, &error);
337 ensure_connection_condition (connection, G_IO_OUT, cancellable);
338 size = g_output_stream_write (ostream,
340 cancellable, &error);
345 if (g_error_matches (error,
347 G_IO_ERROR_WOULD_BLOCK))
349 g_print ("socket send would block, handling\n");
350 g_error_free (error);
356 g_printerr ("Error sending to socket: %s\n",
362 g_print ("sent %" G_GSSIZE_FORMAT " bytes of data\n", size);
366 g_printerr ("Unexpected short write\n");
375 ensure_socket_condition (socket, G_IO_IN, cancellable);
376 size = g_socket_receive_from (socket, &src_address,
377 buffer, sizeof buffer,
378 cancellable, &error);
382 ensure_connection_condition (connection, G_IO_IN, cancellable);
383 size = g_input_stream_read (istream,
384 buffer, sizeof buffer,
385 cancellable, &error);
390 g_printerr ("Error receiving from socket: %s\n",
398 g_print ("received %" G_GSSIZE_FORMAT " bytes of data", size);
400 g_print (" from %s", socket_address_to_string (src_address));
404 g_print ("-------------------------\n"
406 "-------------------------\n",
411 g_print ("closing socket\n");
415 if (!g_io_stream_close (connection, cancellable, &error))
417 g_printerr ("Error closing connection: %s\n",
421 g_object_unref (connection);
425 if (!g_socket_close (socket, &error))
427 g_printerr ("Error closing master socket: %s\n",
433 g_object_unref (socket);
434 g_object_unref (address);