Debian Codegraph @

src/ws/cockpittransport.c

raw
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 /* This gets logged as part of the (more verbose) protocol logging */
9 #ifdef G_LOG_DOMAIN
10 #undef G_LOG_DOMAIN
11 #endif
12 #define G_LOG_DOMAIN "cockpit-protocol"
13
14 #include "cockpittransport.h"
15
16 #include "cockpitjson.h"
17
18 #include <stdlib.h>
19 #include <string.h>
20
21 typedef struct {
22 gconstpointer channel;
23 JsonObject *control;
24 GBytes *data;
25 } FrozenMessage;
26
27 static void
28 frozen_message_free (gpointer data)
29 {
30 FrozenMessage *frozen = data;
31 if (frozen->data)
32 g_bytes_unref (frozen->data);
33 if (frozen->control)
34 json_object_unref (frozen->control);
35 g_slice_free (FrozenMessage, frozen);
36 }
37
38 enum {
39 RECV,
40 CONTROL,
41 CLOSED,
42 NUM_SIGNALS
43 };
44
45 static guint signals[NUM_SIGNALS];
46
47 typedef struct {
48 GHashTable *freeze;
49 GQueue *frozen;
50 } CockpitTransportPrivate;
51
52 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (CockpitTransport, cockpit_transport, G_TYPE_OBJECT,
53 G_ADD_PRIVATE (CockpitTransport));
54
55 static void
56 cockpit_transport_init (CockpitTransport *self)
57 {
58 }
59
60 static void
61 cockpit_transport_get_property (GObject *object,
62 guint property_id,
63 GValue *value,
64 GParamSpec *pspec)
65 {
66 /* Should be overridden by derived abstract classes */
67 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
68 }
69
70 static gboolean
71 maybe_freeze_message (CockpitTransport *self,
72 const gchar *channel,
73 JsonObject *control,
74 GBytes *data)
75 {
76 CockpitTransportPrivate *priv = cockpit_transport_get_instance_private (self);
77 FrozenMessage *frozen = NULL;
78
79 if (priv->freeze && channel)
80 {
81 /* Note that we dig out the real value for the channel */
82 channel = g_hash_table_lookup (priv->freeze, channel);
83 if (channel)
84 {
85 frozen = g_slice_new0 (FrozenMessage);
86 frozen->channel = channel; /* owned by hashtable */
87 frozen->data = g_bytes_ref (data);
88 if (control)
89 frozen->control = json_object_ref (control);
90 if (!priv->frozen)
91 priv->frozen = g_queue_new ();
92 g_queue_push_tail (priv->frozen, frozen);
93 return TRUE;
94 }
95 }
96
97 return FALSE;
98 }
99
100 static gboolean
101 cockpit_transport_default_recv (CockpitTransport *transport,
102 const gchar *channel,
103 GBytes *payload)
104 {
105 const gchar *inner_channel;
106 JsonObject *options;
107 const gchar *command = NULL;
108
109 /* Our default handler parses control channel and fires control signal */
110 if (channel)
111 return FALSE;
112
113 /* Read out the actual command and channel this message is about */
114 if (!cockpit_transport_parse_command (payload, &command, &inner_channel, &options))
115 {
116 /* Warning already logged */
117 cockpit_transport_close (transport, "protocol-error");
118 return TRUE;
119 }
120
121 cockpit_transport_emit_control (transport, command, inner_channel, options, payload);
122 json_object_unref (options);
123
124 return TRUE;
125 }
126
127 static gboolean
128 cockpit_transport_default_control (CockpitTransport *transport,
129 const gchar *command,
130 const gchar *channel,
131 JsonObject *options,
132 GBytes *payload)
133 {
134 GBytes *message;
135
136 if (channel != NULL)
137 return FALSE;
138
139 /* A single hop ping. Respond to it right here, immediately */
140 if (g_str_equal (command, "ping"))
141 {
142 json_object_set_string_member (options, "command", "pong");
143 message = cockpit_json_write_bytes (options);
144 cockpit_transport_send (transport, NULL, message);
145 g_bytes_unref (message);
146 return TRUE;
147 }
148 else if (g_str_equal (command, "pong"))
149 {
150 /* Ignore pong commands */
151 return TRUE;
152 }
153
154 /* Not handled */
155 return FALSE;
156 }
157
158 static void
159 cockpit_transport_finalize (GObject *object)
160 {
161 CockpitTransport *self = COCKPIT_TRANSPORT (object);
162 CockpitTransportPrivate *priv = cockpit_transport_get_instance_private (self);
163
164 if (priv->freeze)
165 g_hash_table_destroy (priv->freeze);
166 if (priv->frozen)
167 g_queue_free_full (priv->frozen, frozen_message_free);
168
169 G_OBJECT_CLASS (cockpit_transport_parent_class)->finalize (object);
170 }
171
172 static void
173 cockpit_transport_class_init (CockpitTransportClass *klass)
174 {
175 GObjectClass *object_class = G_OBJECT_CLASS (klass);
176
177 klass->recv = cockpit_transport_default_recv;
178 klass->control = cockpit_transport_default_control;
179
180 object_class->get_property = cockpit_transport_get_property;
181 object_class->finalize = cockpit_transport_finalize;
182
183 g_object_class_install_property (object_class, 1,
184 g_param_spec_string ("name", "name", "name", NULL,
185 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
186
187 signals[RECV] = g_signal_new ("recv", COCKPIT_TYPE_TRANSPORT, G_SIGNAL_RUN_LAST,
188 G_STRUCT_OFFSET (CockpitTransportClass, recv),
189 g_signal_accumulator_true_handled, NULL,
190 g_cclosure_marshal_generic,
191 G_TYPE_BOOLEAN, 2, G_TYPE_STRING, G_TYPE_BYTES);
192
193 signals[CONTROL] = g_signal_new ("control", COCKPIT_TYPE_TRANSPORT, G_SIGNAL_RUN_LAST,
194 G_STRUCT_OFFSET (CockpitTransportClass, control),
195 g_signal_accumulator_true_handled, NULL,
196 g_cclosure_marshal_generic,
197 G_TYPE_BOOLEAN, 4,
198 G_TYPE_STRING, G_TYPE_STRING, JSON_TYPE_OBJECT, G_TYPE_BYTES);
199
200 signals[CLOSED] = g_signal_new ("closed", COCKPIT_TYPE_TRANSPORT, G_SIGNAL_RUN_FIRST,
201 G_STRUCT_OFFSET (CockpitTransportClass, closed),
202 NULL, NULL, g_cclosure_marshal_generic,
203 G_TYPE_NONE, 1, G_TYPE_STRING);
204 }
205
206 void
207 cockpit_transport_send (CockpitTransport *transport,
208 const gchar *channel,
209 GBytes *data)
210 {
211 CockpitTransportClass *klass;
212
213 g_return_if_fail (COCKPIT_IS_TRANSPORT (transport));
214
215 klass = COCKPIT_TRANSPORT_GET_CLASS (transport);
216 g_return_if_fail (klass && klass->send);
217 klass->send (transport, channel, data);
218 }
219
220 void
221 cockpit_transport_close (CockpitTransport *transport,
222 const gchar *problem)
223 {
224 CockpitTransportClass *klass;
225
226 g_return_if_fail (COCKPIT_IS_TRANSPORT (transport));
227
228 klass = COCKPIT_TRANSPORT_GET_CLASS (transport);
229 g_return_if_fail (klass && klass->close);
230 klass->close (transport, problem);
231 }
232
233 void
234 cockpit_transport_emit_recv (CockpitTransport *transport,
235 const gchar *channel,
236 GBytes *data)
237 {
238 gboolean result = FALSE;
239
240 g_return_if_fail (COCKPIT_IS_TRANSPORT (transport));
241
242 if (maybe_freeze_message (transport, channel, NULL, data))
243 return;
244
245 g_signal_emit (transport, signals[RECV], 0, channel, data, &result);
246
247 if (!result)
248 g_debug ("no handler for received message in channel %s", channel);
249 }
250
251 void
252 cockpit_transport_emit_control (CockpitTransport *transport,
253 const gchar *command,
254 const gchar *channel,
255 JsonObject *options,
256 GBytes *data)
257 {
258 gboolean result = FALSE;
259
260 g_return_if_fail (COCKPIT_IS_TRANSPORT (transport));
261
262 if (maybe_freeze_message (transport, channel, options, data))
263 return;
264
265 g_signal_emit (transport, signals[CONTROL], 0, command, channel, options, data, &result);
266
267 if (!result)
268 g_debug ("received unknown control command: %s", command);
269 }
270
271 void
272 cockpit_transport_emit_closed (CockpitTransport *transport,
273 const gchar *problem)
274 {
275 g_return_if_fail (COCKPIT_IS_TRANSPORT (transport));
276 g_signal_emit (transport, signals[CLOSED], 0, problem);
277 }
278
279 void
280 cockpit_transport_freeze (CockpitTransport *self,
281 const gchar *channel)
282 {
283 CockpitTransportPrivate *priv = cockpit_transport_get_instance_private (self);
284
285 g_return_if_fail (COCKPIT_IS_TRANSPORT (self));
286 g_return_if_fail (channel != NULL);
287
288 if (!priv->freeze)
289 priv->freeze = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
290 g_hash_table_add (priv->freeze, g_strdup (channel));
291 }
292
293 void
294 cockpit_transport_thaw (CockpitTransport *self,
295 const gchar *channel)
296 {
297 CockpitTransportPrivate *priv = cockpit_transport_get_instance_private (self);
298 FrozenMessage *frozen;
299 const gchar *command;
300 gchar *stolen = NULL;
301 GList *l, *flush;
302
303 g_return_if_fail (COCKPIT_IS_TRANSPORT (self));
304 g_return_if_fail (channel != NULL);
305
306 if (priv->freeze)
307 stolen = g_hash_table_lookup (priv->freeze, channel);
308 if (stolen)
309 g_hash_table_steal (priv->freeze, channel);
310
311 for (l = priv->frozen ? priv->frozen->head : NULL; l != NULL; )
312 {
313 frozen = l->data;
314 flush = (stolen == frozen->channel) ? l : NULL;
315 l = g_list_next (l);
316
317 if (flush)
318 {
319 if (frozen->control)
320 {
321 command = NULL;
322 cockpit_json_get_string (frozen->control, "command", NULL, &command);
323 cockpit_transport_emit_control (self, command, stolen, frozen->control, frozen->data);
324 }
325 else
326 {
327 cockpit_transport_emit_recv (self, stolen, frozen->data);
328 }
329 g_queue_delete_link (priv->frozen, flush);
330 frozen_message_free (frozen);
331 }
332 }
333
334 g_free (stolen);
335 }
336
337 static GBytes *
338 parse_frame (GBytes *message,
339 gchar **channel)
340 {
341 const gchar *data;
342 gsize length;
343 const gchar *line;
344 gsize channel_len;
345
346 g_return_val_if_fail (message != NULL, NULL);
347
348 data = g_bytes_get_data (message, &length);
349 line = memchr (data, '\n', length);
350 if (!line)
351 {
352 g_message ("received invalid message without channel prefix");
353 return NULL;
354 }
355
356 channel_len = line - data;
357 if (memchr (data, '\0', channel_len) != NULL)
358 {
359 g_message ("received massage with invalid channel prefix");
360 return NULL;
361 }
362
363 if (channel_len)
364 *channel = g_strndup (data, channel_len);
365 else
366 *channel = NULL;
367
368 channel_len++;
369 return g_bytes_new_from_bytes (message, channel_len, length - channel_len);
370 }
371
372 /**
373 * cockpit_transport_parse_frame:
374 * @message: message to parse
375 * @channel: location to return the channel
376 *
377 * Parse a message into a channel and payload.
378 * @channel will be set to NULL if a control channel
379 * message. @channel must be freed.
380 *
381 * Will return NULL if invalid message.
382 *
383 * Returns: (transfer full): the payload or NULL.
384 */
385 GBytes *
386 cockpit_transport_parse_frame (GBytes *message,
387 gchar **channel)
388 {
389 return parse_frame (message, channel);
390 }
391
392 /**
393 * cockpit_transport_parse_command:
394 * @payload: command JSON payload to parse
395 * @command: a location to return the command
396 * @channel: location to return the channel
397 * @options: location to return the options
398 *
399 * Parse a command and return various values from the
400 * command. The @options value is transferred with ownership,
401 * so you should free it after done. @command and @channel are owned by
402 * @options. @channel will be NULL for a missing channel.
403 *
404 * On failure, message has already been printed.
405 *
406 * Returns: whether command parsed or not.
407 */
408 gboolean
409 cockpit_transport_parse_command (GBytes *payload,
410 const gchar **command,
411 const gchar **channel,
412 JsonObject **options)
413 {
414 GError *error = NULL;
415 gboolean ret = FALSE;
416 JsonObject *object;
417 gboolean valid;
418
419 object = cockpit_json_parse_bytes (payload, &error);
420 if (!object)
421 {
422 g_warning ("Received unparsable control message: %s", error->message);
423 g_error_free (error);
424 goto out;
425 }
426
427 /* Parse out the command */
428 if (command)
429 {
430 if (!cockpit_json_get_string (object, "command", NULL, command) ||
431 *command == NULL || g_str_equal (*command, ""))
432 {
433 g_warning ("Received invalid control message: invalid or missing command");
434 goto out;
435 }
436 }
437
438 /* Parse out the channel */
439 if (channel)
440 {
441 valid = cockpit_json_get_string (object, "channel", NULL, channel);
442 if (valid && *channel)
443 {
444 valid = (!g_str_equal ("", *channel) &&
445 strcspn (*channel, "\n") == strlen (*channel));
446 }
447 if (!valid)
448 {
449 g_warning ("Received invalid control message: invalid channel");
450 goto out;
451 }
452 }
453
454 *options = json_object_ref (object);
455 ret = TRUE;
456
457 out:
458 if (object)
459 json_object_unref (object);
460 return ret;
461 }
462
463 static JsonObject *
464 build_json_va (const gchar *name,
465 va_list va)
466 {
467 JsonObject *object;
468 const gchar *value;
469
470 object = json_object_new ();
471
472 while (name)
473 {
474 value = va_arg (va, const gchar *);
475 if (value)
476 json_object_set_string_member (object, name, value);
477 name = va_arg (va, const gchar *);
478 }
479
480 return object;
481 }
482
483 JsonObject *
484 cockpit_transport_build_json (const gchar *name,
485 ...)
486 {
487 JsonObject *object;
488 va_list va;
489
490 va_start (va, name);
491 object = build_json_va (name, va);
492 va_end (va);
493
494 return object;
495 }
496
497 GBytes *
498 cockpit_transport_build_control (const gchar *name,
499 ...)
500 {
501 JsonObject *object;
502 GBytes *message;
503 va_list va;
504
505 va_start (va, name);
506 object = build_json_va (name, va);
507 va_end (va);
508
509 message = cockpit_json_write_bytes (object);
510 json_object_unref (object);
511 return message;
512 }