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;
70 context = g_option_context_new (" - Test GSocket server stuff");
71 g_option_context_add_main_entries (context, cmd_entries, NULL);
72 if (!g_option_context_parse (context, &argc, &argv, &error))
74 g_printerr ("%s: %s\n", argv[0], error->message);
78 if (unix_socket && argc != 2)
80 g_printerr ("%s: %s\n", argv[0], "Need to specify unix socket name");
86 cancellable = g_cancellable_new ();
87 g_thread_create (cancel_thread, cancellable, FALSE, NULL);
98 g_printerr ("DTLS (TLS over UDP) is not supported");
102 tlscert = g_tls_certificate_new_from_file (tls_cert_file, &error);
105 g_printerr ("Could not read server certificate '%s': %s\n",
106 tls_cert_file, error->message);
111 loop = g_main_loop_new (NULL, FALSE);
114 socket_type = G_SOCKET_TYPE_DATAGRAM;
116 socket_type = G_SOCKET_TYPE_STREAM;
119 socket_family = G_SOCKET_FAMILY_UNIX;
121 socket_family = G_SOCKET_FAMILY_IPV4;
123 socket = g_socket_new (socket_family, socket_type, 0, &error);
127 g_printerr ("%s: %s\n", argv[0], error->message);
132 g_socket_set_blocking (socket, FALSE);
136 src_address = socket_address_from_string (argv[1]);
137 if (src_address == NULL)
139 g_printerr ("%s: Could not parse '%s' as unix socket name\n", argv[0], argv[1]);
145 src_address = g_inet_socket_address_new (g_inet_address_new_any (G_SOCKET_FAMILY_IPV4), port);
148 if (!g_socket_bind (socket, src_address, !dont_reuse_address, &error))
150 g_printerr ("Can't bind socket: %s\n", error->message);
153 g_object_unref (src_address);
157 if (!g_socket_listen (socket, &error))
159 g_printerr ("Can't listen on socket: %s\n", error->message);
163 address = g_socket_get_local_address (socket, &error);
166 g_printerr ("Error getting local address: %s\n",
170 display_addr = socket_address_to_string (address);
171 g_print ("listening on %s...\n", display_addr);
172 g_free (display_addr);
174 ensure_socket_condition (socket, G_IO_IN, cancellable);
175 new_socket = g_socket_accept (socket, cancellable, &error);
178 g_printerr ("Error accepting socket: %s\n",
184 g_socket_set_blocking (new_socket, FALSE);
186 g_socket_set_timeout (new_socket, read_timeout);
188 address = g_socket_get_remote_address (new_socket, &error);
191 g_printerr ("Error getting remote address: %s\n",
196 display_addr = socket_address_to_string (address);
197 g_print ("got a new connection from %s\n", display_addr);
198 g_free(display_addr);
199 g_object_unref (address);
201 recv_socket = new_socket;
203 connection = G_IO_STREAM (g_socket_connection_factory_create_connection (recv_socket));
204 g_object_unref (new_socket);
208 recv_socket = socket;
214 GTlsServerConnection *tls_conn;
216 tls_conn = g_tls_server_connection_new (connection, tlscert, &error);
219 g_printerr ("Could not create TLS connection: %s\n",
224 if (!g_tls_connection_handshake (G_TLS_CONNECTION (tls_conn),
225 cancellable, &error))
227 g_printerr ("Error during TLS handshake: %s\n",
232 g_object_unref (connection);
233 connection = G_IO_STREAM (tls_conn);
238 istream = g_io_stream_get_input_stream (connection);
239 ostream = g_io_stream_get_output_stream (connection);
256 ensure_socket_condition (recv_socket, G_IO_IN, cancellable);
257 size = g_socket_receive_from (recv_socket, &address,
258 buffer, sizeof buffer,
259 cancellable, &error);
263 ensure_connection_condition (connection, G_IO_IN, cancellable);
264 size = g_input_stream_read (istream,
265 buffer, sizeof buffer,
266 cancellable, &error);
271 g_printerr ("Error receiving from socket: %s\n",
279 g_print ("received %" G_GSSIZE_FORMAT " bytes of data", size);
281 g_print (" from %s", socket_address_to_string (address));
285 g_print ("-------------------------\n"
287 "-------------------------\n",
295 g_print ("delaying %d seconds before response\n", delay);
296 g_usleep (1000 * 1000 * delay);
303 ensure_socket_condition (recv_socket, G_IO_OUT, cancellable);
304 size = g_socket_send_to (recv_socket, address,
305 buffer, to_send, cancellable, &error);
309 ensure_connection_condition (connection, G_IO_OUT, cancellable);
310 size = g_output_stream_write (ostream,
312 cancellable, &error);
317 if (g_error_matches (error,
319 G_IO_ERROR_WOULD_BLOCK))
321 g_print ("socket send would block, handling\n");
322 g_error_free (error);
328 g_printerr ("Error sending to socket: %s\n",
334 g_print ("sent %" G_GSSIZE_FORMAT " bytes of data\n", size);
338 g_printerr ("Unexpected short write\n");
346 g_print ("connection closed\n");
350 if (!g_io_stream_close (connection, NULL, &error))
352 g_printerr ("Error closing connection stream: %s\n",
356 g_object_unref (connection);
359 if (!g_socket_close (socket, &error))
361 g_printerr ("Error closing master socket: %s\n",
365 g_object_unref (socket);