2 #include <gio/gunixsocketaddress.h>
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;
17 gboolean unix_socket = FALSE;
18 const char *tls_cert_file = NULL;
20 static GOptionEntry cmd_entries[] = {
21 {"port", 'p', 0, G_OPTION_ARG_INT, &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,
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},
34 {"unix", 'U', 0, G_OPTION_ARG_NONE, &unix_socket,
35 "Use a unix socket instead of IP", NULL},
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"},
46 #include "socket-common.c"
52 GSocket *socket, *new_socket, *recv_socket;
53 GSocketAddress *src_address;
54 GSocketAddress *address;
55 GSocketType socket_type;
56 GSocketFamily socket_family;
58 GOptionContext *context;
59 GCancellable *cancellable;
61 GTlsCertificate *tlscert = NULL;
62 GIOStream *connection;
63 GInputStream *istream;
64 GOutputStream *ostream;
66 context = g_option_context_new (" - Test GSocket server stuff");
67 g_option_context_add_main_entries (context, cmd_entries, NULL);
68 if (!g_option_context_parse (context, &argc, &argv, &error))
70 g_printerr ("%s: %s\n", argv[0], error->message);
74 if (unix_socket && argc != 2)
76 g_printerr ("%s: %s\n", argv[0], "Need to specify unix socket name");
83 cancellable = g_cancellable_new ();
84 thread = g_thread_new ("cancel", cancel_thread, cancellable);
85 g_thread_unref (thread);
96 g_printerr ("DTLS (TLS over UDP) is not supported");
100 tlscert = g_tls_certificate_new_from_file (tls_cert_file, &error);
103 g_printerr ("Could not read server certificate '%s': %s\n",
104 tls_cert_file, error->message);
109 loop = g_main_loop_new (NULL, FALSE);
112 socket_type = G_SOCKET_TYPE_DATAGRAM;
114 socket_type = G_SOCKET_TYPE_STREAM;
117 socket_family = G_SOCKET_FAMILY_UNIX;
119 socket_family = G_SOCKET_FAMILY_IPV4;
121 socket = g_socket_new (socket_family, socket_type, 0, &error);
125 g_printerr ("%s: %s\n", argv[0], error->message);
130 g_socket_set_blocking (socket, FALSE);
134 src_address = socket_address_from_string (argv[1]);
135 if (src_address == NULL)
137 g_printerr ("%s: Could not parse '%s' as unix socket name\n", argv[0], argv[1]);
143 src_address = g_inet_socket_address_new (g_inet_address_new_any (G_SOCKET_FAMILY_IPV4), port);
146 if (!g_socket_bind (socket, src_address, !dont_reuse_address, &error))
148 g_printerr ("Can't bind socket: %s\n", error->message);
151 g_object_unref (src_address);
155 if (!g_socket_listen (socket, &error))
157 g_printerr ("Can't listen on socket: %s\n", error->message);
161 address = g_socket_get_local_address (socket, &error);
164 g_printerr ("Error getting local address: %s\n",
168 display_addr = socket_address_to_string (address);
169 g_print ("listening on %s...\n", display_addr);
170 g_free (display_addr);
172 ensure_socket_condition (socket, G_IO_IN, cancellable);
173 new_socket = g_socket_accept (socket, cancellable, &error);
176 g_printerr ("Error accepting socket: %s\n",
182 g_socket_set_blocking (new_socket, FALSE);
184 g_socket_set_timeout (new_socket, read_timeout);
186 address = g_socket_get_remote_address (new_socket, &error);
189 g_printerr ("Error getting remote address: %s\n",
194 display_addr = socket_address_to_string (address);
195 g_print ("got a new connection from %s\n", display_addr);
196 g_free(display_addr);
197 g_object_unref (address);
199 recv_socket = new_socket;
201 connection = G_IO_STREAM (g_socket_connection_factory_create_connection (recv_socket));
202 g_object_unref (new_socket);
206 recv_socket = socket;
214 tls_conn = g_tls_server_connection_new (connection, tlscert, &error);
217 g_printerr ("Could not create TLS connection: %s\n",
222 if (!g_tls_connection_handshake (G_TLS_CONNECTION (tls_conn),
223 cancellable, &error))
225 g_printerr ("Error during TLS handshake: %s\n",
230 g_object_unref (connection);
231 connection = tls_conn;
236 istream = g_io_stream_get_input_stream (connection);
237 ostream = g_io_stream_get_output_stream (connection);
254 ensure_socket_condition (recv_socket, G_IO_IN, cancellable);
255 size = g_socket_receive_from (recv_socket, &address,
256 buffer, sizeof buffer,
257 cancellable, &error);
261 ensure_connection_condition (connection, G_IO_IN, cancellable);
262 size = g_input_stream_read (istream,
263 buffer, sizeof buffer,
264 cancellable, &error);
269 g_printerr ("Error receiving from socket: %s\n",
277 g_print ("received %" G_GSSIZE_FORMAT " bytes of data", size);
279 g_print (" from %s", socket_address_to_string (address));
283 g_print ("-------------------------\n"
285 "-------------------------\n",
293 g_print ("delaying %d seconds before response\n", delay);
294 g_usleep (1000 * 1000 * delay);
301 ensure_socket_condition (recv_socket, G_IO_OUT, cancellable);
302 size = g_socket_send_to (recv_socket, address,
303 buffer, to_send, cancellable, &error);
307 ensure_connection_condition (connection, G_IO_OUT, cancellable);
308 size = g_output_stream_write (ostream,
310 cancellable, &error);
315 if (g_error_matches (error,
317 G_IO_ERROR_WOULD_BLOCK))
319 g_print ("socket send would block, handling\n");
320 g_error_free (error);
326 g_printerr ("Error sending to socket: %s\n",
332 g_print ("sent %" G_GSSIZE_FORMAT " bytes of data\n", size);
336 g_printerr ("Unexpected short write\n");
344 g_print ("connection closed\n");
348 if (!g_io_stream_close (connection, NULL, &error))
350 g_printerr ("Error closing connection stream: %s\n",
354 g_object_unref (connection);
357 if (!g_socket_close (socket, &error))
359 g_printerr ("Error closing master socket: %s\n",
363 g_object_unref (socket);