Add initial TLS (SSL) support to gio
[platform/upstream/glib.git] / gio / tests / socket-server.c
1 #include <gio/gio.h>
2 #include <gio/gunixsocketaddress.h>
3 #include <glib.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 GMainLoop *loop;
8
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;
19
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}
44 };
45
46 #include "socket-common.c"
47
48 int
49 main (int argc,
50       char *argv[])
51 {
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;
65
66   g_thread_init (NULL);
67
68   g_type_init ();
69
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))
73     {
74       g_printerr ("%s: %s\n", argv[0], error->message);
75       return 1;
76     }
77
78   if (unix_socket && argc != 2)
79     {
80       g_printerr ("%s: %s\n", argv[0], "Need to specify unix socket name");
81       return 1;
82     }
83
84   if (cancel_timeout)
85     {
86       cancellable = g_cancellable_new ();
87       g_thread_create (cancel_thread, cancellable, FALSE, NULL);
88     }
89   else
90     {
91       cancellable = NULL;
92     }
93
94   if (tls_cert_file)
95     {
96       if (use_udp)
97         {
98           g_printerr ("DTLS (TLS over UDP) is not supported");
99           return 1;
100         }
101
102       tlscert = g_tls_certificate_new_from_file (tls_cert_file, &error);
103       if (!tlscert)
104         {
105           g_printerr ("Could not read server certificate '%s': %s\n",
106                       tls_cert_file, error->message);
107           return 1;
108         }
109     }
110
111   loop = g_main_loop_new (NULL, FALSE);
112
113   if (use_udp)
114     socket_type = G_SOCKET_TYPE_DATAGRAM;
115   else
116     socket_type = G_SOCKET_TYPE_STREAM;
117
118   if (unix_socket)
119     socket_family = G_SOCKET_FAMILY_UNIX;
120   else
121     socket_family = G_SOCKET_FAMILY_IPV4;
122
123   socket = g_socket_new (socket_family, socket_type, 0, &error);
124
125   if (socket == NULL)
126     {
127       g_printerr ("%s: %s\n", argv[0], error->message);
128       return 1;
129     }
130
131   if (non_blocking)
132     g_socket_set_blocking (socket, FALSE);
133
134   if (unix_socket)
135     {
136       src_address = socket_address_from_string (argv[1]);
137       if (src_address == NULL)
138         {
139           g_printerr ("%s: Could not parse '%s' as unix socket name\n", argv[0], argv[1]);
140           return 1;
141         }
142     }
143   else
144     {
145       src_address = g_inet_socket_address_new (g_inet_address_new_any (G_SOCKET_FAMILY_IPV4), port);
146     }
147
148   if (!g_socket_bind (socket, src_address, !dont_reuse_address, &error))
149     {
150       g_printerr ("Can't bind socket: %s\n", error->message);
151       return 1;
152     }
153   g_object_unref (src_address);
154
155   if (!use_udp)
156     {
157       if (!g_socket_listen (socket, &error))
158         {
159           g_printerr ("Can't listen on socket: %s\n", error->message);
160           return 1;
161         }
162
163       address = g_socket_get_local_address (socket, &error);
164       if (!address)
165         {
166           g_printerr ("Error getting local address: %s\n",
167                       error->message);
168           return 1;
169         }
170       display_addr = socket_address_to_string (address);
171       g_print ("listening on %s...\n", display_addr);
172       g_free (display_addr);
173
174       ensure_socket_condition (socket, G_IO_IN, cancellable);
175       new_socket = g_socket_accept (socket, cancellable, &error);
176       if (!new_socket)
177         {
178           g_printerr ("Error accepting socket: %s\n",
179                       error->message);
180           return 1;
181         }
182
183       if (non_blocking)
184         g_socket_set_blocking (new_socket, FALSE);
185       if (read_timeout)
186         g_socket_set_timeout (new_socket, read_timeout);
187
188       address = g_socket_get_remote_address (new_socket, &error);
189       if (!address)
190         {
191           g_printerr ("Error getting remote address: %s\n",
192                       error->message);
193           return 1;
194         }
195
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);
200
201       recv_socket = new_socket;
202
203       connection = G_IO_STREAM (g_socket_connection_factory_create_connection (recv_socket));
204       g_object_unref (new_socket);
205     }
206   else
207     {
208       recv_socket = socket;
209       connection = NULL;
210     }
211
212   if (tlscert)
213     {
214       GTlsServerConnection *tls_conn;
215
216       tls_conn = g_tls_server_connection_new (connection, tlscert, &error);
217       if (!tls_conn)
218         {
219           g_printerr ("Could not create TLS connection: %s\n",
220                       error->message);
221           return 1;
222         }
223
224       if (!g_tls_connection_handshake (G_TLS_CONNECTION (tls_conn),
225                                        cancellable, &error))
226         {
227           g_printerr ("Error during TLS handshake: %s\n",
228                       error->message);
229           return 1;
230        }
231
232       g_object_unref (connection);
233       connection = G_IO_STREAM (tls_conn);
234     }
235
236   if (connection)
237     {
238       istream = g_io_stream_get_input_stream (connection);
239       ostream = g_io_stream_get_output_stream (connection);
240     }
241
242   while (TRUE)
243     {
244       gchar buffer[4096];
245       gssize size;
246       gsize to_send;
247
248       if (use_udp)
249         {
250           ensure_socket_condition (recv_socket, G_IO_IN, cancellable);
251           size = g_socket_receive_from (recv_socket, &address,
252                                         buffer, sizeof buffer,
253                                         cancellable, &error);
254         }
255       else
256         {
257           ensure_connection_condition (connection, G_IO_IN, cancellable);
258           size = g_input_stream_read (istream,
259                                       buffer, sizeof buffer,
260                                       cancellable, &error);
261         }
262
263       if (size < 0)
264         {
265           g_printerr ("Error receiving from socket: %s\n",
266                       error->message);
267           return 1;
268         }
269
270       if (size == 0)
271         break;
272
273       g_print ("received %" G_GSSIZE_FORMAT " bytes of data", size);
274       if (use_udp)
275         g_print (" from %s", socket_address_to_string (address));
276       g_print ("\n");
277
278       if (verbose)
279         g_print ("-------------------------\n"
280                  "%.*s\n"
281                  "-------------------------\n",
282                  (int)size, buffer);
283
284       to_send = size;
285
286       if (delay)
287         {
288           if (verbose)
289             g_print ("delaying %d seconds before response\n", delay);
290           g_usleep (1000 * 1000 * delay);
291         }
292
293       while (to_send > 0)
294         {
295           if (use_udp)
296             {
297               ensure_socket_condition (recv_socket, G_IO_OUT, cancellable);
298               size = g_socket_send_to (recv_socket, address,
299                                        buffer, to_send, cancellable, &error);
300             }
301           else
302             {
303               ensure_connection_condition (connection, G_IO_OUT, cancellable);
304               size = g_output_stream_write (ostream,
305                                             buffer, to_send,
306                                             cancellable, &error);
307             }
308
309           if (size < 0)
310             {
311               if (g_error_matches (error,
312                                    G_IO_ERROR,
313                                    G_IO_ERROR_WOULD_BLOCK))
314                 {
315                   g_print ("socket send would block, handling\n");
316                   g_error_free (error);
317                   error = NULL;
318                   continue;
319                 }
320               else
321                 {
322                   g_printerr ("Error sending to socket: %s\n",
323                               error->message);
324                   return 1;
325                 }
326             }
327
328           g_print ("sent %" G_GSSIZE_FORMAT " bytes of data\n", size);
329
330           if (size == 0)
331             {
332               g_printerr ("Unexpected short write\n");
333               return 1;
334             }
335
336           to_send -= size;
337         }
338     }
339
340   g_print ("connection closed\n");
341
342   if (connection)
343     {
344       if (!g_io_stream_close (connection, NULL, &error))
345         {
346           g_printerr ("Error closing connection stream: %s\n",
347                       error->message);
348           return 1;
349         }
350       g_object_unref (connection);
351     }
352
353   if (!g_socket_close (socket, &error))
354     {
355       g_printerr ("Error closing master socket: %s\n",
356                   error->message);
357       return 1;
358     }
359   g_object_unref (socket);
360
361   return 0;
362 }