Add Friulian translation
[glib.git] / gio / tests / socket-server.c
blobdecf3ecd1c273c47628047571f3d883234125fc6
1 #include <gio/gio.h>
2 #include <gio/gunixsocketaddress.h>
3 #include <glib.h>
4 #include <stdlib.h>
5 #include <string.h>
7 GMainLoop *loop;
9 int port = 7777;
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;
15 int read_timeout = 0;
16 int delay = 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,
28 "Be verbose", NULL},
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},
33 #ifdef G_OS_UNIX
34 {"unix", 'U', 0, G_OPTION_ARG_NONE, &unix_socket,
35 "Use a unix socket instead of IP", NULL},
36 #endif
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"},
43 {NULL}
46 #include "socket-common.c"
48 int
49 main (int argc,
50 char *argv[])
52 GSocket *socket, *new_socket, *recv_socket;
53 GSocketAddress *src_address;
54 GSocketAddress *address;
55 GSocketType socket_type;
56 GSocketFamily socket_family;
57 GError *error = NULL;
58 GOptionContext *context;
59 GCancellable *cancellable;
60 char *display_addr;
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);
71 return 1;
74 if (unix_socket && argc != 2)
76 g_printerr ("%s: %s\n", argv[0], "Need to specify unix socket name");
77 return 1;
80 if (cancel_timeout)
82 GThread *thread;
83 cancellable = g_cancellable_new ();
84 thread = g_thread_new ("cancel", cancel_thread, cancellable);
85 g_thread_unref (thread);
87 else
89 cancellable = NULL;
92 if (tls_cert_file)
94 if (use_udp)
96 g_printerr ("DTLS (TLS over UDP) is not supported");
97 return 1;
100 tlscert = g_tls_certificate_new_from_file (tls_cert_file, &error);
101 if (!tlscert)
103 g_printerr ("Could not read server certificate '%s': %s\n",
104 tls_cert_file, error->message);
105 return 1;
109 loop = g_main_loop_new (NULL, FALSE);
111 if (use_udp)
112 socket_type = G_SOCKET_TYPE_DATAGRAM;
113 else
114 socket_type = G_SOCKET_TYPE_STREAM;
116 if (unix_socket)
117 socket_family = G_SOCKET_FAMILY_UNIX;
118 else
119 socket_family = G_SOCKET_FAMILY_IPV4;
121 socket = g_socket_new (socket_family, socket_type, 0, &error);
123 if (socket == NULL)
125 g_printerr ("%s: %s\n", argv[0], error->message);
126 return 1;
129 if (non_blocking)
130 g_socket_set_blocking (socket, FALSE);
132 if (unix_socket)
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]);
138 return 1;
141 else
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);
149 return 1;
151 g_object_unref (src_address);
153 if (!use_udp)
155 if (!g_socket_listen (socket, &error))
157 g_printerr ("Can't listen on socket: %s\n", error->message);
158 return 1;
161 address = g_socket_get_local_address (socket, &error);
162 if (!address)
164 g_printerr ("Error getting local address: %s\n",
165 error->message);
166 return 1;
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);
174 if (!new_socket)
176 g_printerr ("Error accepting socket: %s\n",
177 error->message);
178 return 1;
181 if (non_blocking)
182 g_socket_set_blocking (new_socket, FALSE);
183 if (read_timeout)
184 g_socket_set_timeout (new_socket, read_timeout);
186 address = g_socket_get_remote_address (new_socket, &error);
187 if (!address)
189 g_printerr ("Error getting remote address: %s\n",
190 error->message);
191 return 1;
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);
204 else
206 recv_socket = socket;
207 connection = NULL;
210 if (tlscert)
212 GIOStream *tls_conn;
214 tls_conn = g_tls_server_connection_new (connection, tlscert, &error);
215 if (!tls_conn)
217 g_printerr ("Could not create TLS connection: %s\n",
218 error->message);
219 return 1;
222 if (!g_tls_connection_handshake (G_TLS_CONNECTION (tls_conn),
223 cancellable, &error))
225 g_printerr ("Error during TLS handshake: %s\n",
226 error->message);
227 return 1;
230 g_object_unref (connection);
231 connection = tls_conn;
234 if (connection)
236 istream = g_io_stream_get_input_stream (connection);
237 ostream = g_io_stream_get_output_stream (connection);
239 else
241 g_assert (use_udp);
242 istream = NULL;
243 ostream = NULL;
246 while (TRUE)
248 gchar buffer[4096];
249 gssize size;
250 gsize to_send;
252 if (use_udp)
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);
259 else
261 ensure_connection_condition (connection, G_IO_IN, cancellable);
262 size = g_input_stream_read (istream,
263 buffer, sizeof buffer,
264 cancellable, &error);
267 if (size < 0)
269 g_printerr ("Error receiving from socket: %s\n",
270 error->message);
271 return 1;
274 if (size == 0)
275 break;
277 g_print ("received %" G_GSSIZE_FORMAT " bytes of data", size);
278 if (use_udp)
279 g_print (" from %s", socket_address_to_string (address));
280 g_print ("\n");
282 if (verbose)
283 g_print ("-------------------------\n"
284 "%.*s\n"
285 "-------------------------\n",
286 (int)size, buffer);
288 to_send = size;
290 if (delay)
292 if (verbose)
293 g_print ("delaying %d seconds before response\n", delay);
294 g_usleep (1000 * 1000 * delay);
297 while (to_send > 0)
299 if (use_udp)
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);
305 else
307 ensure_connection_condition (connection, G_IO_OUT, cancellable);
308 size = g_output_stream_write (ostream,
309 buffer, to_send,
310 cancellable, &error);
313 if (size < 0)
315 if (g_error_matches (error,
316 G_IO_ERROR,
317 G_IO_ERROR_WOULD_BLOCK))
319 g_print ("socket send would block, handling\n");
320 g_error_free (error);
321 error = NULL;
322 continue;
324 else
326 g_printerr ("Error sending to socket: %s\n",
327 error->message);
328 return 1;
332 g_print ("sent %" G_GSSIZE_FORMAT " bytes of data\n", size);
334 if (size == 0)
336 g_printerr ("Unexpected short write\n");
337 return 1;
340 to_send -= size;
344 g_print ("connection closed\n");
346 if (connection)
348 if (!g_io_stream_close (connection, NULL, &error))
350 g_printerr ("Error closing connection stream: %s\n",
351 error->message);
352 return 1;
354 g_object_unref (connection);
357 if (!g_socket_close (socket, &error))
359 g_printerr ("Error closing master socket: %s\n",
360 error->message);
361 return 1;
363 g_object_unref (socket);
365 return 0;