/src/glib/gio/gdbusmessage.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* GDBus - GLib D-Bus Library |
2 | | * |
3 | | * Copyright (C) 2008-2010 Red Hat, Inc. |
4 | | * |
5 | | * This library is free software; you can redistribute it and/or |
6 | | * modify it under the terms of the GNU Lesser General Public |
7 | | * License as published by the Free Software Foundation; either |
8 | | * version 2.1 of the License, or (at your option) any later version. |
9 | | * |
10 | | * This library is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | | * Lesser General Public License for more details. |
14 | | * |
15 | | * You should have received a copy of the GNU Lesser General |
16 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
17 | | * |
18 | | * Author: David Zeuthen <davidz@redhat.com> |
19 | | */ |
20 | | |
21 | | /* Uncomment to debug serializer code */ |
22 | | /* #define DEBUG_SERIALIZER */ |
23 | | |
24 | | #include "config.h" |
25 | | |
26 | | #include <string.h> |
27 | | #include <errno.h> |
28 | | #include <sys/types.h> |
29 | | #include <sys/stat.h> |
30 | | |
31 | | #if MAJOR_IN_MKDEV |
32 | | #include <sys/mkdev.h> |
33 | | #elif MAJOR_IN_SYSMACROS |
34 | | #include <sys/sysmacros.h> |
35 | | #elif MAJOR_IN_TYPES |
36 | | #include <sys/types.h> |
37 | | #else |
38 | | #define MAJOR_MINOR_NOT_FOUND 1 |
39 | | #endif |
40 | | |
41 | | #include "gdbusutils.h" |
42 | | #include "gdbusmessage.h" |
43 | | #include "gdbuserror.h" |
44 | | #include "gioenumtypes.h" |
45 | | #include "ginputstream.h" |
46 | | #include "gdatainputstream.h" |
47 | | #include "gmemoryinputstream.h" |
48 | | #include "goutputstream.h" |
49 | | #include "gdataoutputstream.h" |
50 | | #include "gmemoryoutputstream.h" |
51 | | #include "gseekable.h" |
52 | | #include "gioerror.h" |
53 | | #include "gdbusprivate.h" |
54 | | |
55 | | #ifdef G_OS_UNIX |
56 | | #include "gunixfdlist.h" |
57 | | #endif |
58 | | |
59 | | #include "glibintl.h" |
60 | | |
61 | | /* See https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-marshaling-signature |
62 | | * This is 64 containers plus 1 value within them. */ |
63 | 0 | #define G_DBUS_MAX_TYPE_DEPTH (64 + 1) |
64 | | |
65 | | typedef struct _GMemoryBuffer GMemoryBuffer; |
66 | | struct _GMemoryBuffer |
67 | | { |
68 | | gsize len; |
69 | | gsize valid_len; |
70 | | gsize pos; |
71 | | gchar *data; |
72 | | GDataStreamByteOrder byte_order; |
73 | | }; |
74 | | |
75 | | static gboolean |
76 | | g_memory_buffer_is_byteswapped (GMemoryBuffer *mbuf) |
77 | 0 | { |
78 | 0 | #if G_BYTE_ORDER == G_LITTLE_ENDIAN |
79 | 0 | return mbuf->byte_order == G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN; |
80 | | #else |
81 | | return mbuf->byte_order == G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN; |
82 | | #endif |
83 | 0 | } |
84 | | |
85 | | static guchar |
86 | | g_memory_buffer_read_byte (GMemoryBuffer *mbuf) |
87 | 0 | { |
88 | 0 | if (mbuf->pos >= mbuf->valid_len) |
89 | 0 | return 0; |
90 | 0 | return mbuf->data [mbuf->pos++]; |
91 | 0 | } |
92 | | |
93 | | static gint16 |
94 | | g_memory_buffer_read_int16 (GMemoryBuffer *mbuf) |
95 | 0 | { |
96 | 0 | gint16 v; |
97 | | |
98 | 0 | if (mbuf->pos > mbuf->valid_len - 2) |
99 | 0 | { |
100 | 0 | mbuf->pos = mbuf->valid_len; |
101 | 0 | return 0; |
102 | 0 | } |
103 | | |
104 | 0 | memcpy (&v, mbuf->data + mbuf->pos, 2); |
105 | 0 | mbuf->pos += 2; |
106 | |
|
107 | 0 | if (g_memory_buffer_is_byteswapped (mbuf)) |
108 | 0 | v = GUINT16_SWAP_LE_BE (v); |
109 | |
|
110 | 0 | return v; |
111 | 0 | } |
112 | | |
113 | | static guint16 |
114 | | g_memory_buffer_read_uint16 (GMemoryBuffer *mbuf) |
115 | 0 | { |
116 | 0 | guint16 v; |
117 | | |
118 | 0 | if (mbuf->pos > mbuf->valid_len - 2) |
119 | 0 | { |
120 | 0 | mbuf->pos = mbuf->valid_len; |
121 | 0 | return 0; |
122 | 0 | } |
123 | | |
124 | 0 | memcpy (&v, mbuf->data + mbuf->pos, 2); |
125 | 0 | mbuf->pos += 2; |
126 | |
|
127 | 0 | if (g_memory_buffer_is_byteswapped (mbuf)) |
128 | 0 | v = GUINT16_SWAP_LE_BE (v); |
129 | |
|
130 | 0 | return v; |
131 | 0 | } |
132 | | |
133 | | static gint32 |
134 | | g_memory_buffer_read_int32 (GMemoryBuffer *mbuf) |
135 | 0 | { |
136 | 0 | gint32 v; |
137 | | |
138 | 0 | if (mbuf->pos > mbuf->valid_len - 4) |
139 | 0 | { |
140 | 0 | mbuf->pos = mbuf->valid_len; |
141 | 0 | return 0; |
142 | 0 | } |
143 | | |
144 | 0 | memcpy (&v, mbuf->data + mbuf->pos, 4); |
145 | 0 | mbuf->pos += 4; |
146 | |
|
147 | 0 | if (g_memory_buffer_is_byteswapped (mbuf)) |
148 | 0 | v = GUINT32_SWAP_LE_BE (v); |
149 | |
|
150 | 0 | return v; |
151 | 0 | } |
152 | | |
153 | | static guint32 |
154 | | g_memory_buffer_read_uint32 (GMemoryBuffer *mbuf) |
155 | 0 | { |
156 | 0 | guint32 v; |
157 | | |
158 | 0 | if (mbuf->pos > mbuf->valid_len - 4) |
159 | 0 | { |
160 | 0 | mbuf->pos = mbuf->valid_len; |
161 | 0 | return 0; |
162 | 0 | } |
163 | | |
164 | 0 | memcpy (&v, mbuf->data + mbuf->pos, 4); |
165 | 0 | mbuf->pos += 4; |
166 | |
|
167 | 0 | if (g_memory_buffer_is_byteswapped (mbuf)) |
168 | 0 | v = GUINT32_SWAP_LE_BE (v); |
169 | |
|
170 | 0 | return v; |
171 | 0 | } |
172 | | |
173 | | static gint64 |
174 | | g_memory_buffer_read_int64 (GMemoryBuffer *mbuf) |
175 | 0 | { |
176 | 0 | gint64 v; |
177 | | |
178 | 0 | if (mbuf->pos > mbuf->valid_len - 8) |
179 | 0 | { |
180 | 0 | mbuf->pos = mbuf->valid_len; |
181 | 0 | return 0; |
182 | 0 | } |
183 | | |
184 | 0 | memcpy (&v, mbuf->data + mbuf->pos, 8); |
185 | 0 | mbuf->pos += 8; |
186 | |
|
187 | 0 | if (g_memory_buffer_is_byteswapped (mbuf)) |
188 | 0 | v = GUINT64_SWAP_LE_BE (v); |
189 | |
|
190 | 0 | return v; |
191 | 0 | } |
192 | | |
193 | | static guint64 |
194 | | g_memory_buffer_read_uint64 (GMemoryBuffer *mbuf) |
195 | 0 | { |
196 | 0 | guint64 v; |
197 | | |
198 | 0 | if (mbuf->pos > mbuf->valid_len - 8) |
199 | 0 | { |
200 | 0 | mbuf->pos = mbuf->valid_len; |
201 | 0 | return 0; |
202 | 0 | } |
203 | | |
204 | 0 | memcpy (&v, mbuf->data + mbuf->pos, 8); |
205 | 0 | mbuf->pos += 8; |
206 | |
|
207 | 0 | if (g_memory_buffer_is_byteswapped (mbuf)) |
208 | 0 | v = GUINT64_SWAP_LE_BE (v); |
209 | |
|
210 | 0 | return v; |
211 | 0 | } |
212 | | |
213 | 0 | #define MIN_ARRAY_SIZE 128 |
214 | | |
215 | | static gsize |
216 | | g_nearest_pow (gsize num) |
217 | 0 | { |
218 | 0 | gsize n = 1; |
219 | |
|
220 | 0 | while (n < num && n > 0) |
221 | 0 | n <<= 1; |
222 | |
|
223 | 0 | return n; |
224 | 0 | } |
225 | | |
226 | | static void |
227 | | array_resize (GMemoryBuffer *mbuf, |
228 | | gsize size) |
229 | 0 | { |
230 | 0 | gpointer data; |
231 | 0 | gsize len; |
232 | |
|
233 | 0 | if (mbuf->len == size) |
234 | 0 | return; |
235 | | |
236 | 0 | len = mbuf->len; |
237 | 0 | data = g_realloc (mbuf->data, size); |
238 | |
|
239 | 0 | if (size > len) |
240 | 0 | memset ((guint8 *)data + len, 0, size - len); |
241 | |
|
242 | 0 | mbuf->data = data; |
243 | 0 | mbuf->len = size; |
244 | |
|
245 | 0 | if (mbuf->len < mbuf->valid_len) |
246 | 0 | mbuf->valid_len = mbuf->len; |
247 | 0 | } |
248 | | |
249 | | static gboolean |
250 | | g_memory_buffer_write (GMemoryBuffer *mbuf, |
251 | | const void *buffer, |
252 | | gsize count) |
253 | 0 | { |
254 | 0 | guint8 *dest; |
255 | 0 | gsize new_size; |
256 | |
|
257 | 0 | if (count == 0) |
258 | 0 | return TRUE; |
259 | | |
260 | | /* Check for address space overflow, but only if the buffer is resizable. |
261 | | Otherwise we just do a short write and don't worry. */ |
262 | 0 | if (mbuf->pos + count < mbuf->pos) |
263 | 0 | return FALSE; |
264 | | |
265 | 0 | if (mbuf->pos + count > mbuf->len) |
266 | 0 | { |
267 | | /* At least enough to fit the write, rounded up |
268 | | for greater than linear growth. |
269 | | TODO: This wastes a lot of memory at large buffer sizes. |
270 | | Figure out a more rational allocation strategy. */ |
271 | 0 | new_size = g_nearest_pow (mbuf->pos + count); |
272 | | /* Check for overflow again. We have checked if |
273 | | pos + count > G_MAXSIZE, but now check if g_nearest_pow () has |
274 | | overflowed */ |
275 | 0 | if (new_size == 0) |
276 | 0 | return FALSE; |
277 | | |
278 | 0 | new_size = MAX (new_size, MIN_ARRAY_SIZE); |
279 | 0 | array_resize (mbuf, new_size); |
280 | 0 | } |
281 | | |
282 | 0 | dest = (guint8 *)mbuf->data + mbuf->pos; |
283 | 0 | memcpy (dest, buffer, count); |
284 | 0 | mbuf->pos += count; |
285 | |
|
286 | 0 | if (mbuf->pos > mbuf->valid_len) |
287 | 0 | mbuf->valid_len = mbuf->pos; |
288 | |
|
289 | 0 | return TRUE; |
290 | 0 | } |
291 | | |
292 | | static gboolean |
293 | | g_memory_buffer_put_byte (GMemoryBuffer *mbuf, |
294 | | guchar data) |
295 | 0 | { |
296 | 0 | return g_memory_buffer_write (mbuf, &data, 1); |
297 | 0 | } |
298 | | |
299 | | static gboolean |
300 | | g_memory_buffer_put_int16 (GMemoryBuffer *mbuf, |
301 | | gint16 data) |
302 | 0 | { |
303 | 0 | switch (mbuf->byte_order) |
304 | 0 | { |
305 | 0 | case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN: |
306 | 0 | data = GINT16_TO_BE (data); |
307 | 0 | break; |
308 | 0 | case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN: |
309 | 0 | data = GINT16_TO_LE (data); |
310 | 0 | break; |
311 | 0 | case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN: |
312 | 0 | default: |
313 | 0 | break; |
314 | 0 | } |
315 | | |
316 | 0 | return g_memory_buffer_write (mbuf, &data, 2); |
317 | 0 | } |
318 | | |
319 | | static gboolean |
320 | | g_memory_buffer_put_uint16 (GMemoryBuffer *mbuf, |
321 | | guint16 data) |
322 | 0 | { |
323 | 0 | switch (mbuf->byte_order) |
324 | 0 | { |
325 | 0 | case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN: |
326 | 0 | data = GUINT16_TO_BE (data); |
327 | 0 | break; |
328 | 0 | case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN: |
329 | 0 | data = GUINT16_TO_LE (data); |
330 | 0 | break; |
331 | 0 | case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN: |
332 | 0 | default: |
333 | 0 | break; |
334 | 0 | } |
335 | | |
336 | 0 | return g_memory_buffer_write (mbuf, &data, 2); |
337 | 0 | } |
338 | | |
339 | | static gboolean |
340 | | g_memory_buffer_put_int32 (GMemoryBuffer *mbuf, |
341 | | gint32 data) |
342 | 0 | { |
343 | 0 | switch (mbuf->byte_order) |
344 | 0 | { |
345 | 0 | case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN: |
346 | 0 | data = GINT32_TO_BE (data); |
347 | 0 | break; |
348 | 0 | case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN: |
349 | 0 | data = GINT32_TO_LE (data); |
350 | 0 | break; |
351 | 0 | case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN: |
352 | 0 | default: |
353 | 0 | break; |
354 | 0 | } |
355 | | |
356 | 0 | return g_memory_buffer_write (mbuf, &data, 4); |
357 | 0 | } |
358 | | |
359 | | static gboolean |
360 | | g_memory_buffer_put_uint32 (GMemoryBuffer *mbuf, |
361 | | guint32 data) |
362 | 0 | { |
363 | 0 | switch (mbuf->byte_order) |
364 | 0 | { |
365 | 0 | case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN: |
366 | 0 | data = GUINT32_TO_BE (data); |
367 | 0 | break; |
368 | 0 | case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN: |
369 | 0 | data = GUINT32_TO_LE (data); |
370 | 0 | break; |
371 | 0 | case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN: |
372 | 0 | default: |
373 | 0 | break; |
374 | 0 | } |
375 | | |
376 | 0 | return g_memory_buffer_write (mbuf, &data, 4); |
377 | 0 | } |
378 | | |
379 | | static gboolean |
380 | | g_memory_buffer_put_int64 (GMemoryBuffer *mbuf, |
381 | | gint64 data) |
382 | 0 | { |
383 | 0 | switch (mbuf->byte_order) |
384 | 0 | { |
385 | 0 | case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN: |
386 | 0 | data = GINT64_TO_BE (data); |
387 | 0 | break; |
388 | 0 | case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN: |
389 | 0 | data = GINT64_TO_LE (data); |
390 | 0 | break; |
391 | 0 | case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN: |
392 | 0 | default: |
393 | 0 | break; |
394 | 0 | } |
395 | | |
396 | 0 | return g_memory_buffer_write (mbuf, &data, 8); |
397 | 0 | } |
398 | | |
399 | | static gboolean |
400 | | g_memory_buffer_put_uint64 (GMemoryBuffer *mbuf, |
401 | | guint64 data) |
402 | 0 | { |
403 | 0 | switch (mbuf->byte_order) |
404 | 0 | { |
405 | 0 | case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN: |
406 | 0 | data = GUINT64_TO_BE (data); |
407 | 0 | break; |
408 | 0 | case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN: |
409 | 0 | data = GUINT64_TO_LE (data); |
410 | 0 | break; |
411 | 0 | case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN: |
412 | 0 | default: |
413 | 0 | break; |
414 | 0 | } |
415 | | |
416 | 0 | return g_memory_buffer_write (mbuf, &data, 8); |
417 | 0 | } |
418 | | |
419 | | static gboolean |
420 | | g_memory_buffer_put_string (GMemoryBuffer *mbuf, |
421 | | const char *str) |
422 | 0 | { |
423 | 0 | g_return_val_if_fail (str != NULL, FALSE); |
424 | | |
425 | 0 | return g_memory_buffer_write (mbuf, str, strlen (str)); |
426 | 0 | } |
427 | | |
428 | | |
429 | | /** |
430 | | * SECTION:gdbusmessage |
431 | | * @short_description: D-Bus Message |
432 | | * @include: gio/gio.h |
433 | | * |
434 | | * A type for representing D-Bus messages that can be sent or received |
435 | | * on a #GDBusConnection. |
436 | | */ |
437 | | |
438 | | typedef struct _GDBusMessageClass GDBusMessageClass; |
439 | | |
440 | | /** |
441 | | * GDBusMessageClass: |
442 | | * |
443 | | * Class structure for #GDBusMessage. |
444 | | * |
445 | | * Since: 2.26 |
446 | | */ |
447 | | struct _GDBusMessageClass |
448 | | { |
449 | | /*< private >*/ |
450 | | GObjectClass parent_class; |
451 | | }; |
452 | | |
453 | | /** |
454 | | * GDBusMessage: |
455 | | * |
456 | | * The #GDBusMessage structure contains only private data and should |
457 | | * only be accessed using the provided API. |
458 | | * |
459 | | * Since: 2.26 |
460 | | */ |
461 | | struct _GDBusMessage |
462 | | { |
463 | | /*< private >*/ |
464 | | GObject parent_instance; |
465 | | |
466 | | GDBusMessageType type; |
467 | | GDBusMessageFlags flags; |
468 | | gboolean locked; |
469 | | GDBusMessageByteOrder byte_order; |
470 | | guchar major_protocol_version; |
471 | | guint32 serial; |
472 | | GHashTable *headers; |
473 | | GVariant *body; |
474 | | #ifdef G_OS_UNIX |
475 | | GUnixFDList *fd_list; |
476 | | #endif |
477 | | }; |
478 | | |
479 | | enum |
480 | | { |
481 | | PROP_0, |
482 | | PROP_LOCKED |
483 | | }; |
484 | | |
485 | | G_DEFINE_TYPE (GDBusMessage, g_dbus_message, G_TYPE_OBJECT) |
486 | | |
487 | | static void |
488 | | g_dbus_message_finalize (GObject *object) |
489 | 0 | { |
490 | 0 | GDBusMessage *message = G_DBUS_MESSAGE (object); |
491 | |
|
492 | 0 | if (message->headers != NULL) |
493 | 0 | g_hash_table_unref (message->headers); |
494 | 0 | if (message->body != NULL) |
495 | 0 | g_variant_unref (message->body); |
496 | 0 | #ifdef G_OS_UNIX |
497 | 0 | if (message->fd_list != NULL) |
498 | 0 | g_object_unref (message->fd_list); |
499 | 0 | #endif |
500 | |
|
501 | 0 | if (G_OBJECT_CLASS (g_dbus_message_parent_class)->finalize != NULL) |
502 | 0 | G_OBJECT_CLASS (g_dbus_message_parent_class)->finalize (object); |
503 | 0 | } |
504 | | |
505 | | static void |
506 | | g_dbus_message_get_property (GObject *object, |
507 | | guint prop_id, |
508 | | GValue *value, |
509 | | GParamSpec *pspec) |
510 | 0 | { |
511 | 0 | GDBusMessage *message = G_DBUS_MESSAGE (object); |
512 | |
|
513 | 0 | switch (prop_id) |
514 | 0 | { |
515 | 0 | case PROP_LOCKED: |
516 | 0 | g_value_set_boolean (value, g_dbus_message_get_locked (message)); |
517 | 0 | break; |
518 | | |
519 | 0 | default: |
520 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
521 | 0 | break; |
522 | 0 | } |
523 | 0 | } |
524 | | |
525 | | static void |
526 | | g_dbus_message_class_init (GDBusMessageClass *klass) |
527 | 0 | { |
528 | 0 | GObjectClass *gobject_class; |
529 | |
|
530 | 0 | gobject_class = G_OBJECT_CLASS (klass); |
531 | 0 | gobject_class->finalize = g_dbus_message_finalize; |
532 | 0 | gobject_class->get_property = g_dbus_message_get_property; |
533 | | |
534 | | /** |
535 | | * GDBusConnection:locked: |
536 | | * |
537 | | * A boolean specifying whether the message is locked. |
538 | | * |
539 | | * Since: 2.26 |
540 | | */ |
541 | 0 | g_object_class_install_property (gobject_class, |
542 | 0 | PROP_LOCKED, |
543 | 0 | g_param_spec_boolean ("locked", |
544 | 0 | P_("Locked"), |
545 | 0 | P_("Whether the message is locked"), |
546 | 0 | FALSE, |
547 | 0 | G_PARAM_READABLE | |
548 | 0 | G_PARAM_STATIC_NAME | |
549 | 0 | G_PARAM_STATIC_BLURB | |
550 | 0 | G_PARAM_STATIC_NICK)); |
551 | 0 | } |
552 | | |
553 | | static void |
554 | | g_dbus_message_init (GDBusMessage *message) |
555 | 0 | { |
556 | | /* Any D-Bus implementation is supposed to handle both Big and |
557 | | * Little Endian encodings and the Endianness is part of the D-Bus |
558 | | * message - we prefer to use Big Endian (since it's Network Byte |
559 | | * Order and just easier to read for humans) but if the machine is |
560 | | * Little Endian we use that for performance reasons. |
561 | | */ |
562 | 0 | #if G_BYTE_ORDER == G_LITTLE_ENDIAN |
563 | 0 | message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN; |
564 | | #else |
565 | | /* this could also be G_PDP_ENDIAN */ |
566 | | message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN; |
567 | | #endif |
568 | 0 | message->headers = g_hash_table_new_full (g_direct_hash, |
569 | 0 | g_direct_equal, |
570 | 0 | NULL, |
571 | 0 | (GDestroyNotify) g_variant_unref); |
572 | 0 | } |
573 | | |
574 | | /** |
575 | | * g_dbus_message_new: |
576 | | * |
577 | | * Creates a new empty #GDBusMessage. |
578 | | * |
579 | | * Returns: A #GDBusMessage. Free with g_object_unref(). |
580 | | * |
581 | | * Since: 2.26 |
582 | | */ |
583 | | GDBusMessage * |
584 | | g_dbus_message_new (void) |
585 | 0 | { |
586 | 0 | return g_object_new (G_TYPE_DBUS_MESSAGE, NULL); |
587 | 0 | } |
588 | | |
589 | | /** |
590 | | * g_dbus_message_new_method_call: |
591 | | * @name: (nullable): A valid D-Bus name or %NULL. |
592 | | * @path: A valid object path. |
593 | | * @interface_: (nullable): A valid D-Bus interface name or %NULL. |
594 | | * @method: A valid method name. |
595 | | * |
596 | | * Creates a new #GDBusMessage for a method call. |
597 | | * |
598 | | * Returns: A #GDBusMessage. Free with g_object_unref(). |
599 | | * |
600 | | * Since: 2.26 |
601 | | */ |
602 | | GDBusMessage * |
603 | | g_dbus_message_new_method_call (const gchar *name, |
604 | | const gchar *path, |
605 | | const gchar *interface_, |
606 | | const gchar *method) |
607 | 0 | { |
608 | 0 | GDBusMessage *message; |
609 | |
|
610 | 0 | g_return_val_if_fail (name == NULL || g_dbus_is_name (name), NULL); |
611 | 0 | g_return_val_if_fail (g_variant_is_object_path (path), NULL); |
612 | 0 | g_return_val_if_fail (g_dbus_is_member_name (method), NULL); |
613 | 0 | g_return_val_if_fail (interface_ == NULL || g_dbus_is_interface_name (interface_), NULL); |
614 | | |
615 | 0 | message = g_dbus_message_new (); |
616 | 0 | message->type = G_DBUS_MESSAGE_TYPE_METHOD_CALL; |
617 | |
|
618 | 0 | if (name != NULL) |
619 | 0 | g_dbus_message_set_destination (message, name); |
620 | 0 | g_dbus_message_set_path (message, path); |
621 | 0 | g_dbus_message_set_member (message, method); |
622 | 0 | if (interface_ != NULL) |
623 | 0 | g_dbus_message_set_interface (message, interface_); |
624 | |
|
625 | 0 | return message; |
626 | 0 | } |
627 | | |
628 | | /** |
629 | | * g_dbus_message_new_signal: |
630 | | * @path: A valid object path. |
631 | | * @interface_: A valid D-Bus interface name. |
632 | | * @signal: A valid signal name. |
633 | | * |
634 | | * Creates a new #GDBusMessage for a signal emission. |
635 | | * |
636 | | * Returns: A #GDBusMessage. Free with g_object_unref(). |
637 | | * |
638 | | * Since: 2.26 |
639 | | */ |
640 | | GDBusMessage * |
641 | | g_dbus_message_new_signal (const gchar *path, |
642 | | const gchar *interface_, |
643 | | const gchar *signal) |
644 | 0 | { |
645 | 0 | GDBusMessage *message; |
646 | |
|
647 | 0 | g_return_val_if_fail (g_variant_is_object_path (path), NULL); |
648 | 0 | g_return_val_if_fail (g_dbus_is_member_name (signal), NULL); |
649 | 0 | g_return_val_if_fail (g_dbus_is_interface_name (interface_), NULL); |
650 | | |
651 | 0 | message = g_dbus_message_new (); |
652 | 0 | message->type = G_DBUS_MESSAGE_TYPE_SIGNAL; |
653 | 0 | message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED; |
654 | |
|
655 | 0 | g_dbus_message_set_path (message, path); |
656 | 0 | g_dbus_message_set_member (message, signal); |
657 | 0 | g_dbus_message_set_interface (message, interface_); |
658 | |
|
659 | 0 | return message; |
660 | 0 | } |
661 | | |
662 | | |
663 | | /** |
664 | | * g_dbus_message_new_method_reply: |
665 | | * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to |
666 | | * create a reply message to. |
667 | | * |
668 | | * Creates a new #GDBusMessage that is a reply to @method_call_message. |
669 | | * |
670 | | * Returns: (transfer full): #GDBusMessage. Free with g_object_unref(). |
671 | | * |
672 | | * Since: 2.26 |
673 | | */ |
674 | | GDBusMessage * |
675 | | g_dbus_message_new_method_reply (GDBusMessage *method_call_message) |
676 | 0 | { |
677 | 0 | GDBusMessage *message; |
678 | 0 | const gchar *sender; |
679 | |
|
680 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message), NULL); |
681 | 0 | g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL, NULL); |
682 | 0 | g_return_val_if_fail (g_dbus_message_get_serial (method_call_message) != 0, NULL); |
683 | | |
684 | 0 | message = g_dbus_message_new (); |
685 | 0 | message->type = G_DBUS_MESSAGE_TYPE_METHOD_RETURN; |
686 | 0 | message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED; |
687 | | /* reply with same endianness */ |
688 | 0 | message->byte_order = method_call_message->byte_order; |
689 | |
|
690 | 0 | g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message)); |
691 | 0 | sender = g_dbus_message_get_sender (method_call_message); |
692 | 0 | if (sender != NULL) |
693 | 0 | g_dbus_message_set_destination (message, sender); |
694 | |
|
695 | 0 | return message; |
696 | 0 | } |
697 | | |
698 | | /** |
699 | | * g_dbus_message_new_method_error: |
700 | | * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to |
701 | | * create a reply message to. |
702 | | * @error_name: A valid D-Bus error name. |
703 | | * @error_message_format: The D-Bus error message in a printf() format. |
704 | | * @...: Arguments for @error_message_format. |
705 | | * |
706 | | * Creates a new #GDBusMessage that is an error reply to @method_call_message. |
707 | | * |
708 | | * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref(). |
709 | | * |
710 | | * Since: 2.26 |
711 | | */ |
712 | | GDBusMessage * |
713 | | g_dbus_message_new_method_error (GDBusMessage *method_call_message, |
714 | | const gchar *error_name, |
715 | | const gchar *error_message_format, |
716 | | ...) |
717 | 0 | { |
718 | 0 | GDBusMessage *ret; |
719 | 0 | va_list var_args; |
720 | |
|
721 | 0 | va_start (var_args, error_message_format); |
722 | 0 | ret = g_dbus_message_new_method_error_valist (method_call_message, |
723 | 0 | error_name, |
724 | 0 | error_message_format, |
725 | 0 | var_args); |
726 | 0 | va_end (var_args); |
727 | |
|
728 | 0 | return ret; |
729 | 0 | } |
730 | | |
731 | | /** |
732 | | * g_dbus_message_new_method_error_literal: |
733 | | * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to |
734 | | * create a reply message to. |
735 | | * @error_name: A valid D-Bus error name. |
736 | | * @error_message: The D-Bus error message. |
737 | | * |
738 | | * Creates a new #GDBusMessage that is an error reply to @method_call_message. |
739 | | * |
740 | | * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref(). |
741 | | * |
742 | | * Since: 2.26 |
743 | | */ |
744 | | GDBusMessage * |
745 | | g_dbus_message_new_method_error_literal (GDBusMessage *method_call_message, |
746 | | const gchar *error_name, |
747 | | const gchar *error_message) |
748 | 0 | { |
749 | 0 | GDBusMessage *message; |
750 | 0 | const gchar *sender; |
751 | |
|
752 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (method_call_message), NULL); |
753 | 0 | g_return_val_if_fail (g_dbus_message_get_message_type (method_call_message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL, NULL); |
754 | 0 | g_return_val_if_fail (g_dbus_message_get_serial (method_call_message) != 0, NULL); |
755 | 0 | g_return_val_if_fail (g_dbus_is_name (error_name), NULL); |
756 | 0 | g_return_val_if_fail (error_message != NULL, NULL); |
757 | | |
758 | 0 | message = g_dbus_message_new (); |
759 | 0 | message->type = G_DBUS_MESSAGE_TYPE_ERROR; |
760 | 0 | message->flags = G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED; |
761 | | /* reply with same endianness */ |
762 | 0 | message->byte_order = method_call_message->byte_order; |
763 | |
|
764 | 0 | g_dbus_message_set_reply_serial (message, g_dbus_message_get_serial (method_call_message)); |
765 | 0 | g_dbus_message_set_error_name (message, error_name); |
766 | 0 | g_dbus_message_set_body (message, g_variant_new ("(s)", error_message)); |
767 | |
|
768 | 0 | sender = g_dbus_message_get_sender (method_call_message); |
769 | 0 | if (sender != NULL) |
770 | 0 | g_dbus_message_set_destination (message, sender); |
771 | |
|
772 | 0 | return message; |
773 | 0 | } |
774 | | |
775 | | /** |
776 | | * g_dbus_message_new_method_error_valist: |
777 | | * @method_call_message: A message of type %G_DBUS_MESSAGE_TYPE_METHOD_CALL to |
778 | | * create a reply message to. |
779 | | * @error_name: A valid D-Bus error name. |
780 | | * @error_message_format: The D-Bus error message in a printf() format. |
781 | | * @var_args: Arguments for @error_message_format. |
782 | | * |
783 | | * Like g_dbus_message_new_method_error() but intended for language bindings. |
784 | | * |
785 | | * Returns: (transfer full): A #GDBusMessage. Free with g_object_unref(). |
786 | | * |
787 | | * Since: 2.26 |
788 | | */ |
789 | | G_GNUC_PRINTF(3, 0) |
790 | | GDBusMessage * |
791 | | g_dbus_message_new_method_error_valist (GDBusMessage *method_call_message, |
792 | | const gchar *error_name, |
793 | | const gchar *error_message_format, |
794 | | va_list var_args) |
795 | 0 | { |
796 | 0 | GDBusMessage *ret; |
797 | 0 | gchar *error_message; |
798 | 0 | error_message = g_strdup_vprintf (error_message_format, var_args); |
799 | 0 | ret = g_dbus_message_new_method_error_literal (method_call_message, |
800 | 0 | error_name, |
801 | 0 | error_message); |
802 | 0 | g_free (error_message); |
803 | 0 | return ret; |
804 | 0 | } |
805 | | |
806 | | /* ---------------------------------------------------------------------------------------------------- */ |
807 | | |
808 | | /** |
809 | | * g_dbus_message_get_byte_order: |
810 | | * @message: A #GDBusMessage. |
811 | | * |
812 | | * Gets the byte order of @message. |
813 | | * |
814 | | * Returns: The byte order. |
815 | | */ |
816 | | GDBusMessageByteOrder |
817 | | g_dbus_message_get_byte_order (GDBusMessage *message) |
818 | 0 | { |
819 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), (GDBusMessageByteOrder) 0); |
820 | 0 | return message->byte_order; |
821 | 0 | } |
822 | | |
823 | | /** |
824 | | * g_dbus_message_set_byte_order: |
825 | | * @message: A #GDBusMessage. |
826 | | * @byte_order: The byte order. |
827 | | * |
828 | | * Sets the byte order of @message. |
829 | | */ |
830 | | void |
831 | | g_dbus_message_set_byte_order (GDBusMessage *message, |
832 | | GDBusMessageByteOrder byte_order) |
833 | 0 | { |
834 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
835 | | |
836 | 0 | if (message->locked) |
837 | 0 | { |
838 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
839 | 0 | return; |
840 | 0 | } |
841 | | |
842 | 0 | message->byte_order = byte_order; |
843 | 0 | } |
844 | | |
845 | | /* ---------------------------------------------------------------------------------------------------- */ |
846 | | |
847 | | /* TODO: need GI annotations to specify that any guchar value goes for the type */ |
848 | | |
849 | | /** |
850 | | * g_dbus_message_get_message_type: |
851 | | * @message: A #GDBusMessage. |
852 | | * |
853 | | * Gets the type of @message. |
854 | | * |
855 | | * Returns: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration). |
856 | | * |
857 | | * Since: 2.26 |
858 | | */ |
859 | | GDBusMessageType |
860 | | g_dbus_message_get_message_type (GDBusMessage *message) |
861 | 0 | { |
862 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), G_DBUS_MESSAGE_TYPE_INVALID); |
863 | 0 | return message->type; |
864 | 0 | } |
865 | | |
866 | | /** |
867 | | * g_dbus_message_set_message_type: |
868 | | * @message: A #GDBusMessage. |
869 | | * @type: A 8-bit unsigned integer (typically a value from the #GDBusMessageType enumeration). |
870 | | * |
871 | | * Sets @message to be of @type. |
872 | | * |
873 | | * Since: 2.26 |
874 | | */ |
875 | | void |
876 | | g_dbus_message_set_message_type (GDBusMessage *message, |
877 | | GDBusMessageType type) |
878 | 0 | { |
879 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
880 | 0 | g_return_if_fail ((guint) type < 256); |
881 | | |
882 | 0 | if (message->locked) |
883 | 0 | { |
884 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
885 | 0 | return; |
886 | 0 | } |
887 | | |
888 | 0 | message->type = type; |
889 | 0 | } |
890 | | |
891 | | /* ---------------------------------------------------------------------------------------------------- */ |
892 | | |
893 | | /* TODO: need GI annotations to specify that any guchar value goes for flags */ |
894 | | |
895 | | /** |
896 | | * g_dbus_message_get_flags: |
897 | | * @message: A #GDBusMessage. |
898 | | * |
899 | | * Gets the flags for @message. |
900 | | * |
901 | | * Returns: Flags that are set (typically values from the #GDBusMessageFlags enumeration bitwise ORed together). |
902 | | * |
903 | | * Since: 2.26 |
904 | | */ |
905 | | GDBusMessageFlags |
906 | | g_dbus_message_get_flags (GDBusMessage *message) |
907 | 0 | { |
908 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), G_DBUS_MESSAGE_FLAGS_NONE); |
909 | 0 | return message->flags; |
910 | 0 | } |
911 | | |
912 | | /** |
913 | | * g_dbus_message_set_flags: |
914 | | * @message: A #GDBusMessage. |
915 | | * @flags: Flags for @message that are set (typically values from the #GDBusMessageFlags |
916 | | * enumeration bitwise ORed together). |
917 | | * |
918 | | * Sets the flags to set on @message. |
919 | | * |
920 | | * Since: 2.26 |
921 | | */ |
922 | | void |
923 | | g_dbus_message_set_flags (GDBusMessage *message, |
924 | | GDBusMessageFlags flags) |
925 | 0 | { |
926 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
927 | 0 | g_return_if_fail ((guint) flags < 256); |
928 | | |
929 | 0 | if (message->locked) |
930 | 0 | { |
931 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
932 | 0 | return; |
933 | 0 | } |
934 | | |
935 | 0 | message->flags = flags; |
936 | 0 | } |
937 | | |
938 | | /* ---------------------------------------------------------------------------------------------------- */ |
939 | | |
940 | | /** |
941 | | * g_dbus_message_get_serial: |
942 | | * @message: A #GDBusMessage. |
943 | | * |
944 | | * Gets the serial for @message. |
945 | | * |
946 | | * Returns: A #guint32. |
947 | | * |
948 | | * Since: 2.26 |
949 | | */ |
950 | | guint32 |
951 | | g_dbus_message_get_serial (GDBusMessage *message) |
952 | 0 | { |
953 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0); |
954 | 0 | return message->serial; |
955 | 0 | } |
956 | | |
957 | | /** |
958 | | * g_dbus_message_set_serial: |
959 | | * @message: A #GDBusMessage. |
960 | | * @serial: A #guint32. |
961 | | * |
962 | | * Sets the serial for @message. |
963 | | * |
964 | | * Since: 2.26 |
965 | | */ |
966 | | void |
967 | | g_dbus_message_set_serial (GDBusMessage *message, |
968 | | guint32 serial) |
969 | 0 | { |
970 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
971 | | |
972 | 0 | if (message->locked) |
973 | 0 | { |
974 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
975 | 0 | return; |
976 | 0 | } |
977 | | |
978 | 0 | message->serial = serial; |
979 | 0 | } |
980 | | |
981 | | /* ---------------------------------------------------------------------------------------------------- */ |
982 | | |
983 | | /* TODO: need GI annotations to specify that any guchar value goes for header_field */ |
984 | | |
985 | | /** |
986 | | * g_dbus_message_get_header: |
987 | | * @message: A #GDBusMessage. |
988 | | * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration) |
989 | | * |
990 | | * Gets a header field on @message. |
991 | | * |
992 | | * The caller is responsible for checking the type of the returned #GVariant |
993 | | * matches what is expected. |
994 | | * |
995 | | * Returns: (transfer none) (nullable): A #GVariant with the value if the header was found, %NULL |
996 | | * otherwise. Do not free, it is owned by @message. |
997 | | * |
998 | | * Since: 2.26 |
999 | | */ |
1000 | | GVariant * |
1001 | | g_dbus_message_get_header (GDBusMessage *message, |
1002 | | GDBusMessageHeaderField header_field) |
1003 | 0 | { |
1004 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
1005 | 0 | g_return_val_if_fail ((guint) header_field < 256, NULL); |
1006 | 0 | return g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field)); |
1007 | 0 | } |
1008 | | |
1009 | | /** |
1010 | | * g_dbus_message_set_header: |
1011 | | * @message: A #GDBusMessage. |
1012 | | * @header_field: A 8-bit unsigned integer (typically a value from the #GDBusMessageHeaderField enumeration) |
1013 | | * @value: (nullable): A #GVariant to set the header field or %NULL to clear the header field. |
1014 | | * |
1015 | | * Sets a header field on @message. |
1016 | | * |
1017 | | * If @value is floating, @message assumes ownership of @value. |
1018 | | * |
1019 | | * Since: 2.26 |
1020 | | */ |
1021 | | void |
1022 | | g_dbus_message_set_header (GDBusMessage *message, |
1023 | | GDBusMessageHeaderField header_field, |
1024 | | GVariant *value) |
1025 | 0 | { |
1026 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
1027 | 0 | g_return_if_fail ((guint) header_field < 256); |
1028 | | |
1029 | 0 | if (message->locked) |
1030 | 0 | { |
1031 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
1032 | 0 | return; |
1033 | 0 | } |
1034 | | |
1035 | 0 | if (value == NULL) |
1036 | 0 | { |
1037 | 0 | g_hash_table_remove (message->headers, GUINT_TO_POINTER (header_field)); |
1038 | 0 | } |
1039 | 0 | else |
1040 | 0 | { |
1041 | 0 | g_hash_table_insert (message->headers, GUINT_TO_POINTER (header_field), g_variant_ref_sink (value)); |
1042 | 0 | } |
1043 | 0 | } |
1044 | | |
1045 | | /** |
1046 | | * g_dbus_message_get_header_fields: |
1047 | | * @message: A #GDBusMessage. |
1048 | | * |
1049 | | * Gets an array of all header fields on @message that are set. |
1050 | | * |
1051 | | * Returns: (array zero-terminated=1): An array of header fields |
1052 | | * terminated by %G_DBUS_MESSAGE_HEADER_FIELD_INVALID. Each element |
1053 | | * is a #guchar. Free with g_free(). |
1054 | | * |
1055 | | * Since: 2.26 |
1056 | | */ |
1057 | | guchar * |
1058 | | g_dbus_message_get_header_fields (GDBusMessage *message) |
1059 | 0 | { |
1060 | 0 | GList *keys; |
1061 | 0 | guchar *ret; |
1062 | 0 | guint num_keys; |
1063 | 0 | GList *l; |
1064 | 0 | guint n; |
1065 | |
|
1066 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
1067 | | |
1068 | 0 | keys = g_hash_table_get_keys (message->headers); |
1069 | 0 | num_keys = g_list_length (keys); |
1070 | 0 | ret = g_new (guchar, num_keys + 1); |
1071 | 0 | for (l = keys, n = 0; l != NULL; l = l->next, n++) |
1072 | 0 | ret[n] = GPOINTER_TO_UINT (l->data); |
1073 | 0 | g_assert (n == num_keys); |
1074 | 0 | ret[n] = G_DBUS_MESSAGE_HEADER_FIELD_INVALID; |
1075 | 0 | g_list_free (keys); |
1076 | |
|
1077 | 0 | return ret; |
1078 | 0 | } |
1079 | | |
1080 | | /* ---------------------------------------------------------------------------------------------------- */ |
1081 | | |
1082 | | /** |
1083 | | * g_dbus_message_get_body: |
1084 | | * @message: A #GDBusMessage. |
1085 | | * |
1086 | | * Gets the body of a message. |
1087 | | * |
1088 | | * Returns: (nullable) (transfer none): A #GVariant or %NULL if the body is |
1089 | | * empty. Do not free, it is owned by @message. |
1090 | | * |
1091 | | * Since: 2.26 |
1092 | | */ |
1093 | | GVariant * |
1094 | | g_dbus_message_get_body (GDBusMessage *message) |
1095 | 0 | { |
1096 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
1097 | 0 | return message->body; |
1098 | 0 | } |
1099 | | |
1100 | | /** |
1101 | | * g_dbus_message_set_body: |
1102 | | * @message: A #GDBusMessage. |
1103 | | * @body: Either %NULL or a #GVariant that is a tuple. |
1104 | | * |
1105 | | * Sets the body @message. As a side-effect the |
1106 | | * %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field is set to the |
1107 | | * type string of @body (or cleared if @body is %NULL). |
1108 | | * |
1109 | | * If @body is floating, @message assumes ownership of @body. |
1110 | | * |
1111 | | * Since: 2.26 |
1112 | | */ |
1113 | | void |
1114 | | g_dbus_message_set_body (GDBusMessage *message, |
1115 | | GVariant *body) |
1116 | 0 | { |
1117 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
1118 | 0 | g_return_if_fail ((body == NULL) || g_variant_is_of_type (body, G_VARIANT_TYPE_TUPLE)); |
1119 | | |
1120 | 0 | if (message->locked) |
1121 | 0 | { |
1122 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
1123 | 0 | return; |
1124 | 0 | } |
1125 | | |
1126 | 0 | if (message->body != NULL) |
1127 | 0 | g_variant_unref (message->body); |
1128 | 0 | if (body == NULL) |
1129 | 0 | { |
1130 | 0 | message->body = NULL; |
1131 | 0 | g_dbus_message_set_signature (message, NULL); |
1132 | 0 | } |
1133 | 0 | else |
1134 | 0 | { |
1135 | 0 | const gchar *type_string; |
1136 | 0 | gsize type_string_len; |
1137 | 0 | gchar *signature; |
1138 | |
|
1139 | 0 | message->body = g_variant_ref_sink (body); |
1140 | |
|
1141 | 0 | type_string = g_variant_get_type_string (body); |
1142 | 0 | type_string_len = strlen (type_string); |
1143 | 0 | g_assert (type_string_len >= 2); |
1144 | 0 | signature = g_strndup (type_string + 1, type_string_len - 2); |
1145 | 0 | g_dbus_message_set_signature (message, signature); |
1146 | 0 | g_free (signature); |
1147 | 0 | } |
1148 | 0 | } |
1149 | | |
1150 | | /* ---------------------------------------------------------------------------------------------------- */ |
1151 | | |
1152 | | #ifdef G_OS_UNIX |
1153 | | /** |
1154 | | * g_dbus_message_get_unix_fd_list: |
1155 | | * @message: A #GDBusMessage. |
1156 | | * |
1157 | | * Gets the UNIX file descriptors associated with @message, if any. |
1158 | | * |
1159 | | * This method is only available on UNIX. |
1160 | | * |
1161 | | * The file descriptors normally correspond to %G_VARIANT_TYPE_HANDLE |
1162 | | * values in the body of the message. For example, |
1163 | | * if g_variant_get_handle() returns 5, that is intended to be a reference |
1164 | | * to the file descriptor that can be accessed by |
1165 | | * `g_unix_fd_list_get (list, 5, ...)`. |
1166 | | * |
1167 | | * Returns: (nullable) (transfer none): A #GUnixFDList or %NULL if no file descriptors are |
1168 | | * associated. Do not free, this object is owned by @message. |
1169 | | * |
1170 | | * Since: 2.26 |
1171 | | */ |
1172 | | GUnixFDList * |
1173 | | g_dbus_message_get_unix_fd_list (GDBusMessage *message) |
1174 | 0 | { |
1175 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
1176 | 0 | return message->fd_list; |
1177 | 0 | } |
1178 | | |
1179 | | /** |
1180 | | * g_dbus_message_set_unix_fd_list: |
1181 | | * @message: A #GDBusMessage. |
1182 | | * @fd_list: (nullable): A #GUnixFDList or %NULL. |
1183 | | * |
1184 | | * Sets the UNIX file descriptors associated with @message. As a |
1185 | | * side-effect the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header |
1186 | | * field is set to the number of fds in @fd_list (or cleared if |
1187 | | * @fd_list is %NULL). |
1188 | | * |
1189 | | * This method is only available on UNIX. |
1190 | | * |
1191 | | * When designing D-Bus APIs that are intended to be interoperable, |
1192 | | * please note that non-GDBus implementations of D-Bus can usually only |
1193 | | * access file descriptors if they are referenced by a value of type |
1194 | | * %G_VARIANT_TYPE_HANDLE in the body of the message. |
1195 | | * |
1196 | | * Since: 2.26 |
1197 | | */ |
1198 | | void |
1199 | | g_dbus_message_set_unix_fd_list (GDBusMessage *message, |
1200 | | GUnixFDList *fd_list) |
1201 | 0 | { |
1202 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
1203 | 0 | g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list)); |
1204 | | |
1205 | 0 | if (message->locked) |
1206 | 0 | { |
1207 | 0 | g_warning ("%s: Attempted to modify a locked message", G_STRFUNC); |
1208 | 0 | return; |
1209 | 0 | } |
1210 | | |
1211 | 0 | if (message->fd_list != NULL) |
1212 | 0 | g_object_unref (message->fd_list); |
1213 | 0 | if (fd_list != NULL) |
1214 | 0 | { |
1215 | 0 | message->fd_list = g_object_ref (fd_list); |
1216 | 0 | g_dbus_message_set_num_unix_fds (message, g_unix_fd_list_get_length (fd_list)); |
1217 | 0 | } |
1218 | 0 | else |
1219 | 0 | { |
1220 | 0 | message->fd_list = NULL; |
1221 | 0 | g_dbus_message_set_num_unix_fds (message, 0); |
1222 | 0 | } |
1223 | 0 | } |
1224 | | #endif |
1225 | | |
1226 | | /* ---------------------------------------------------------------------------------------------------- */ |
1227 | | |
1228 | | static guint |
1229 | | get_type_fixed_size (const GVariantType *type) |
1230 | 0 | { |
1231 | | /* NB: we do not treat 'b' as fixed-size here because GVariant and |
1232 | | * D-Bus disagree about the size. |
1233 | | */ |
1234 | 0 | switch (*g_variant_type_peek_string (type)) |
1235 | 0 | { |
1236 | 0 | case 'y': |
1237 | 0 | return 1; |
1238 | 0 | case 'n': case 'q': |
1239 | 0 | return 2; |
1240 | 0 | case 'i': case 'u': case 'h': |
1241 | 0 | return 4; |
1242 | 0 | case 'x': case 't': case 'd': |
1243 | 0 | return 8; |
1244 | 0 | default: |
1245 | 0 | return 0; |
1246 | 0 | } |
1247 | 0 | } |
1248 | | |
1249 | | static gboolean |
1250 | | validate_headers (GDBusMessage *message, |
1251 | | GError **error) |
1252 | 0 | { |
1253 | 0 | gboolean ret; |
1254 | |
|
1255 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE); |
1256 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
1257 | | |
1258 | 0 | ret = FALSE; |
1259 | |
|
1260 | 0 | switch (message->type) |
1261 | 0 | { |
1262 | 0 | case G_DBUS_MESSAGE_TYPE_INVALID: |
1263 | 0 | g_set_error_literal (error, |
1264 | 0 | G_IO_ERROR, |
1265 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1266 | 0 | _("type is INVALID")); |
1267 | 0 | goto out; |
1268 | 0 | break; |
1269 | | |
1270 | 0 | case G_DBUS_MESSAGE_TYPE_METHOD_CALL: |
1271 | 0 | if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH) == NULL || |
1272 | 0 | g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER) == NULL) |
1273 | 0 | { |
1274 | 0 | g_set_error_literal (error, |
1275 | 0 | G_IO_ERROR, |
1276 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1277 | 0 | _("METHOD_CALL message: PATH or MEMBER header field is missing")); |
1278 | 0 | goto out; |
1279 | 0 | } |
1280 | 0 | break; |
1281 | | |
1282 | 0 | case G_DBUS_MESSAGE_TYPE_METHOD_RETURN: |
1283 | 0 | if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL) == NULL) |
1284 | 0 | { |
1285 | 0 | g_set_error_literal (error, |
1286 | 0 | G_IO_ERROR, |
1287 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1288 | 0 | _("METHOD_RETURN message: REPLY_SERIAL header field is missing")); |
1289 | 0 | goto out; |
1290 | 0 | } |
1291 | 0 | break; |
1292 | | |
1293 | 0 | case G_DBUS_MESSAGE_TYPE_ERROR: |
1294 | 0 | if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME) == NULL || |
1295 | 0 | g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL) == NULL) |
1296 | 0 | { |
1297 | 0 | g_set_error_literal (error, |
1298 | 0 | G_IO_ERROR, |
1299 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1300 | 0 | _("ERROR message: REPLY_SERIAL or ERROR_NAME header field is missing")); |
1301 | 0 | goto out; |
1302 | 0 | } |
1303 | 0 | break; |
1304 | | |
1305 | 0 | case G_DBUS_MESSAGE_TYPE_SIGNAL: |
1306 | 0 | if (g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH) == NULL || |
1307 | 0 | g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE) == NULL || |
1308 | 0 | g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER) == NULL) |
1309 | 0 | { |
1310 | 0 | g_set_error_literal (error, |
1311 | 0 | G_IO_ERROR, |
1312 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1313 | 0 | _("SIGNAL message: PATH, INTERFACE or MEMBER header field is missing")); |
1314 | 0 | goto out; |
1315 | 0 | } |
1316 | 0 | if (g_strcmp0 (g_dbus_message_get_path (message), "/org/freedesktop/DBus/Local") == 0) |
1317 | 0 | { |
1318 | 0 | g_set_error_literal (error, |
1319 | 0 | G_IO_ERROR, |
1320 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1321 | 0 | _("SIGNAL message: The PATH header field is using the reserved value /org/freedesktop/DBus/Local")); |
1322 | 0 | goto out; |
1323 | 0 | } |
1324 | 0 | if (g_strcmp0 (g_dbus_message_get_interface (message), "org.freedesktop.DBus.Local") == 0) |
1325 | 0 | { |
1326 | 0 | g_set_error_literal (error, |
1327 | 0 | G_IO_ERROR, |
1328 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1329 | 0 | _("SIGNAL message: The INTERFACE header field is using the reserved value org.freedesktop.DBus.Local")); |
1330 | 0 | goto out; |
1331 | 0 | } |
1332 | 0 | break; |
1333 | | |
1334 | 0 | default: |
1335 | | /* hitherto unknown type - nothing to check */ |
1336 | 0 | break; |
1337 | 0 | } |
1338 | | |
1339 | 0 | ret = TRUE; |
1340 | |
|
1341 | 0 | out: |
1342 | 0 | g_assert (ret || (error == NULL || *error != NULL)); |
1343 | 0 | return ret; |
1344 | 0 | } |
1345 | | |
1346 | | /* ---------------------------------------------------------------------------------------------------- */ |
1347 | | |
1348 | | static gboolean |
1349 | | ensure_input_padding (GMemoryBuffer *buf, |
1350 | | gsize padding_size) |
1351 | 0 | { |
1352 | 0 | gsize offset; |
1353 | 0 | gsize wanted_offset; |
1354 | |
|
1355 | 0 | offset = buf->pos; |
1356 | 0 | wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size; |
1357 | 0 | buf->pos = wanted_offset; |
1358 | 0 | return TRUE; |
1359 | 0 | } |
1360 | | |
1361 | | static const gchar * |
1362 | | read_string (GMemoryBuffer *mbuf, |
1363 | | gsize len, |
1364 | | GError **error) |
1365 | 0 | { |
1366 | 0 | gchar *str; |
1367 | 0 | const gchar *end_valid; |
1368 | |
|
1369 | 0 | if G_UNLIKELY (mbuf->pos + len >= mbuf->valid_len || mbuf->pos + len < mbuf->pos) |
1370 | 0 | { |
1371 | 0 | mbuf->pos = mbuf->valid_len; |
1372 | | /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */ |
1373 | 0 | g_set_error (error, |
1374 | 0 | G_IO_ERROR, |
1375 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1376 | 0 | g_dngettext (GETTEXT_PACKAGE, |
1377 | 0 | "Wanted to read %lu byte but only got %lu", |
1378 | 0 | "Wanted to read %lu bytes but only got %lu", |
1379 | 0 | (gulong)len), |
1380 | 0 | (gulong)len, |
1381 | 0 | (gulong)(mbuf->valid_len - mbuf->pos)); |
1382 | 0 | return NULL; |
1383 | 0 | } |
1384 | | |
1385 | 0 | if G_UNLIKELY (mbuf->data[mbuf->pos + len] != '\0') |
1386 | 0 | { |
1387 | 0 | str = g_strndup (mbuf->data + mbuf->pos, len); |
1388 | 0 | g_set_error (error, |
1389 | 0 | G_IO_ERROR, |
1390 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1391 | 0 | _("Expected NUL byte after the string “%s” but found byte %d"), |
1392 | 0 | str, mbuf->data[mbuf->pos + len]); |
1393 | 0 | g_free (str); |
1394 | 0 | mbuf->pos += len + 1; |
1395 | 0 | return NULL; |
1396 | 0 | } |
1397 | | |
1398 | 0 | str = mbuf->data + mbuf->pos; |
1399 | 0 | mbuf->pos += len + 1; |
1400 | |
|
1401 | 0 | if G_UNLIKELY (!g_utf8_validate (str, -1, &end_valid)) |
1402 | 0 | { |
1403 | 0 | gint offset; |
1404 | 0 | gchar *valid_str; |
1405 | 0 | offset = (gint) (end_valid - str); |
1406 | 0 | valid_str = g_strndup (str, offset); |
1407 | 0 | g_set_error (error, |
1408 | 0 | G_IO_ERROR, |
1409 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1410 | 0 | _("Expected valid UTF-8 string but found invalid bytes at byte offset %d (length of string is %d). " |
1411 | 0 | "The valid UTF-8 string up until that point was “%s”"), |
1412 | 0 | offset, |
1413 | 0 | (gint) len, |
1414 | 0 | valid_str); |
1415 | 0 | g_free (valid_str); |
1416 | 0 | return NULL; |
1417 | 0 | } |
1418 | | |
1419 | 0 | return str; |
1420 | 0 | } |
1421 | | |
1422 | | static gconstpointer |
1423 | | read_bytes (GMemoryBuffer *mbuf, |
1424 | | gsize len, |
1425 | | GError **error) |
1426 | 0 | { |
1427 | 0 | gconstpointer result; |
1428 | |
|
1429 | 0 | if G_UNLIKELY (mbuf->pos + len > mbuf->valid_len || mbuf->pos + len < mbuf->pos) |
1430 | 0 | { |
1431 | 0 | mbuf->pos = mbuf->valid_len; |
1432 | | /* G_GSIZE_FORMAT doesn't work with gettext, so we use %lu */ |
1433 | 0 | g_set_error (error, |
1434 | 0 | G_IO_ERROR, |
1435 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1436 | 0 | g_dngettext (GETTEXT_PACKAGE, |
1437 | 0 | "Wanted to read %lu byte but only got %lu", |
1438 | 0 | "Wanted to read %lu bytes but only got %lu", |
1439 | 0 | (gulong)len), |
1440 | 0 | (gulong)len, |
1441 | 0 | (gulong)(mbuf->valid_len - mbuf->pos)); |
1442 | 0 | return NULL; |
1443 | 0 | } |
1444 | | |
1445 | 0 | result = mbuf->data + mbuf->pos; |
1446 | 0 | mbuf->pos += len; |
1447 | |
|
1448 | 0 | return result; |
1449 | 0 | } |
1450 | | |
1451 | | /* if just_align==TRUE, don't read a value, just align the input stream wrt padding */ |
1452 | | |
1453 | | /* returns a non-floating GVariant! */ |
1454 | | static GVariant * |
1455 | | parse_value_from_blob (GMemoryBuffer *buf, |
1456 | | const GVariantType *type, |
1457 | | guint max_depth, |
1458 | | gboolean just_align, |
1459 | | guint indent, |
1460 | | GError **error) |
1461 | 0 | { |
1462 | 0 | GVariant *ret = NULL; |
1463 | 0 | GError *local_error = NULL; |
1464 | | #ifdef DEBUG_SERIALIZER |
1465 | | gboolean is_leaf; |
1466 | | #endif /* DEBUG_SERIALIZER */ |
1467 | 0 | const gchar *type_string; |
1468 | |
|
1469 | 0 | if (max_depth == 0) |
1470 | 0 | { |
1471 | 0 | g_set_error_literal (&local_error, |
1472 | 0 | G_IO_ERROR, |
1473 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1474 | 0 | _("Value nested too deeply")); |
1475 | 0 | goto fail; |
1476 | 0 | } |
1477 | | |
1478 | 0 | type_string = g_variant_type_peek_string (type); |
1479 | |
|
1480 | | #ifdef DEBUG_SERIALIZER |
1481 | | { |
1482 | | gchar *s; |
1483 | | s = g_variant_type_dup_string (type); |
1484 | | g_print ("%*s%s type %s from offset 0x%04x", |
1485 | | indent, "", |
1486 | | just_align ? "Aligning" : "Reading", |
1487 | | s, |
1488 | | (gint) buf->pos); |
1489 | | g_free (s); |
1490 | | } |
1491 | | #endif /* DEBUG_SERIALIZER */ |
1492 | |
|
1493 | | #ifdef DEBUG_SERIALIZER |
1494 | | is_leaf = TRUE; |
1495 | | #endif /* DEBUG_SERIALIZER */ |
1496 | 0 | switch (type_string[0]) |
1497 | 0 | { |
1498 | 0 | case 'b': /* G_VARIANT_TYPE_BOOLEAN */ |
1499 | 0 | ensure_input_padding (buf, 4); |
1500 | 0 | if (!just_align) |
1501 | 0 | { |
1502 | 0 | gboolean v; |
1503 | 0 | v = g_memory_buffer_read_uint32 (buf); |
1504 | 0 | ret = g_variant_new_boolean (v); |
1505 | 0 | } |
1506 | 0 | break; |
1507 | | |
1508 | 0 | case 'y': /* G_VARIANT_TYPE_BYTE */ |
1509 | 0 | if (!just_align) |
1510 | 0 | { |
1511 | 0 | guchar v; |
1512 | 0 | v = g_memory_buffer_read_byte (buf); |
1513 | 0 | ret = g_variant_new_byte (v); |
1514 | 0 | } |
1515 | 0 | break; |
1516 | | |
1517 | 0 | case 'n': /* G_VARIANT_TYPE_INT16 */ |
1518 | 0 | ensure_input_padding (buf, 2); |
1519 | 0 | if (!just_align) |
1520 | 0 | { |
1521 | 0 | gint16 v; |
1522 | 0 | v = g_memory_buffer_read_int16 (buf); |
1523 | 0 | ret = g_variant_new_int16 (v); |
1524 | 0 | } |
1525 | 0 | break; |
1526 | | |
1527 | 0 | case 'q': /* G_VARIANT_TYPE_UINT16 */ |
1528 | 0 | ensure_input_padding (buf, 2); |
1529 | 0 | if (!just_align) |
1530 | 0 | { |
1531 | 0 | guint16 v; |
1532 | 0 | v = g_memory_buffer_read_uint16 (buf); |
1533 | 0 | ret = g_variant_new_uint16 (v); |
1534 | 0 | } |
1535 | 0 | break; |
1536 | | |
1537 | 0 | case 'i': /* G_VARIANT_TYPE_INT32 */ |
1538 | 0 | ensure_input_padding (buf, 4); |
1539 | 0 | if (!just_align) |
1540 | 0 | { |
1541 | 0 | gint32 v; |
1542 | 0 | v = g_memory_buffer_read_int32 (buf); |
1543 | 0 | ret = g_variant_new_int32 (v); |
1544 | 0 | } |
1545 | 0 | break; |
1546 | | |
1547 | 0 | case 'u': /* G_VARIANT_TYPE_UINT32 */ |
1548 | 0 | ensure_input_padding (buf, 4); |
1549 | 0 | if (!just_align) |
1550 | 0 | { |
1551 | 0 | guint32 v; |
1552 | 0 | v = g_memory_buffer_read_uint32 (buf); |
1553 | 0 | ret = g_variant_new_uint32 (v); |
1554 | 0 | } |
1555 | 0 | break; |
1556 | | |
1557 | 0 | case 'x': /* G_VARIANT_TYPE_INT64 */ |
1558 | 0 | ensure_input_padding (buf, 8); |
1559 | 0 | if (!just_align) |
1560 | 0 | { |
1561 | 0 | gint64 v; |
1562 | 0 | v = g_memory_buffer_read_int64 (buf); |
1563 | 0 | ret = g_variant_new_int64 (v); |
1564 | 0 | } |
1565 | 0 | break; |
1566 | | |
1567 | 0 | case 't': /* G_VARIANT_TYPE_UINT64 */ |
1568 | 0 | ensure_input_padding (buf, 8); |
1569 | 0 | if (!just_align) |
1570 | 0 | { |
1571 | 0 | guint64 v; |
1572 | 0 | v = g_memory_buffer_read_uint64 (buf); |
1573 | 0 | ret = g_variant_new_uint64 (v); |
1574 | 0 | } |
1575 | 0 | break; |
1576 | | |
1577 | 0 | case 'd': /* G_VARIANT_TYPE_DOUBLE */ |
1578 | 0 | ensure_input_padding (buf, 8); |
1579 | 0 | if (!just_align) |
1580 | 0 | { |
1581 | 0 | union { |
1582 | 0 | guint64 v_uint64; |
1583 | 0 | gdouble v_double; |
1584 | 0 | } u; |
1585 | 0 | G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64)); |
1586 | 0 | u.v_uint64 = g_memory_buffer_read_uint64 (buf); |
1587 | 0 | ret = g_variant_new_double (u.v_double); |
1588 | 0 | } |
1589 | 0 | break; |
1590 | | |
1591 | 0 | case 's': /* G_VARIANT_TYPE_STRING */ |
1592 | 0 | ensure_input_padding (buf, 4); |
1593 | 0 | if (!just_align) |
1594 | 0 | { |
1595 | 0 | guint32 len; |
1596 | 0 | const gchar *v; |
1597 | 0 | len = g_memory_buffer_read_uint32 (buf); |
1598 | 0 | v = read_string (buf, (gsize) len, &local_error); |
1599 | 0 | if (v == NULL) |
1600 | 0 | goto fail; |
1601 | 0 | ret = g_variant_new_string (v); |
1602 | 0 | } |
1603 | 0 | break; |
1604 | | |
1605 | 0 | case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */ |
1606 | 0 | ensure_input_padding (buf, 4); |
1607 | 0 | if (!just_align) |
1608 | 0 | { |
1609 | 0 | guint32 len; |
1610 | 0 | const gchar *v; |
1611 | 0 | len = g_memory_buffer_read_uint32 (buf); |
1612 | 0 | v = read_string (buf, (gsize) len, &local_error); |
1613 | 0 | if (v == NULL) |
1614 | 0 | goto fail; |
1615 | 0 | if (!g_variant_is_object_path (v)) |
1616 | 0 | { |
1617 | 0 | g_set_error (&local_error, |
1618 | 0 | G_IO_ERROR, |
1619 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1620 | 0 | _("Parsed value “%s” is not a valid D-Bus object path"), |
1621 | 0 | v); |
1622 | 0 | goto fail; |
1623 | 0 | } |
1624 | 0 | ret = g_variant_new_object_path (v); |
1625 | 0 | } |
1626 | 0 | break; |
1627 | | |
1628 | 0 | case 'g': /* G_VARIANT_TYPE_SIGNATURE */ |
1629 | 0 | if (!just_align) |
1630 | 0 | { |
1631 | 0 | guchar len; |
1632 | 0 | const gchar *v; |
1633 | 0 | len = g_memory_buffer_read_byte (buf); |
1634 | 0 | v = read_string (buf, (gsize) len, &local_error); |
1635 | 0 | if (v == NULL) |
1636 | 0 | goto fail; |
1637 | 0 | if (!g_variant_is_signature (v)) |
1638 | 0 | { |
1639 | 0 | g_set_error (&local_error, |
1640 | 0 | G_IO_ERROR, |
1641 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1642 | 0 | _("Parsed value “%s” is not a valid D-Bus signature"), |
1643 | 0 | v); |
1644 | 0 | goto fail; |
1645 | 0 | } |
1646 | 0 | ret = g_variant_new_signature (v); |
1647 | 0 | } |
1648 | 0 | break; |
1649 | | |
1650 | 0 | case 'h': /* G_VARIANT_TYPE_HANDLE */ |
1651 | 0 | ensure_input_padding (buf, 4); |
1652 | 0 | if (!just_align) |
1653 | 0 | { |
1654 | 0 | gint32 v; |
1655 | 0 | v = g_memory_buffer_read_int32 (buf); |
1656 | 0 | ret = g_variant_new_handle (v); |
1657 | 0 | } |
1658 | 0 | break; |
1659 | | |
1660 | 0 | case 'a': /* G_VARIANT_TYPE_ARRAY */ |
1661 | 0 | ensure_input_padding (buf, 4); |
1662 | | |
1663 | | /* If we are only aligning for this array type, it is the child type of |
1664 | | * another array, which is empty. So, we do not need to add padding for |
1665 | | * this nonexistent array's elements: we only need to align for this |
1666 | | * array itself (4 bytes). See |
1667 | | * <https://bugzilla.gnome.org/show_bug.cgi?id=673612>. |
1668 | | */ |
1669 | 0 | if (!just_align) |
1670 | 0 | { |
1671 | 0 | guint32 array_len; |
1672 | 0 | const GVariantType *element_type; |
1673 | 0 | guint fixed_size; |
1674 | |
|
1675 | 0 | array_len = g_memory_buffer_read_uint32 (buf); |
1676 | |
|
1677 | | #ifdef DEBUG_SERIALIZER |
1678 | | is_leaf = FALSE; |
1679 | | g_print (": array spans 0x%04x bytes\n", array_len); |
1680 | | #endif /* DEBUG_SERIALIZER */ |
1681 | |
|
1682 | 0 | if (array_len > (2<<26)) |
1683 | 0 | { |
1684 | | /* G_GUINT32_FORMAT doesn't work with gettext, so use u */ |
1685 | 0 | g_set_error (&local_error, |
1686 | 0 | G_IO_ERROR, |
1687 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1688 | 0 | g_dngettext (GETTEXT_PACKAGE, |
1689 | 0 | "Encountered array of length %u byte. Maximum length is 2<<26 bytes (64 MiB).", |
1690 | 0 | "Encountered array of length %u bytes. Maximum length is 2<<26 bytes (64 MiB).", |
1691 | 0 | array_len), |
1692 | 0 | array_len); |
1693 | 0 | goto fail; |
1694 | 0 | } |
1695 | | |
1696 | 0 | element_type = g_variant_type_element (type); |
1697 | 0 | fixed_size = get_type_fixed_size (element_type); |
1698 | | |
1699 | | /* Fast-path the cases like 'ay', etc. */ |
1700 | 0 | if (fixed_size != 0) |
1701 | 0 | { |
1702 | 0 | gconstpointer array_data; |
1703 | |
|
1704 | 0 | if (array_len % fixed_size != 0) |
1705 | 0 | { |
1706 | 0 | g_set_error (&local_error, |
1707 | 0 | G_IO_ERROR, |
1708 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1709 | 0 | _("Encountered array of type “a%c”, expected to have a length a multiple " |
1710 | 0 | "of %u bytes, but found to be %u bytes in length"), |
1711 | 0 | g_variant_type_peek_string (element_type)[0], fixed_size, array_len); |
1712 | 0 | goto fail; |
1713 | 0 | } |
1714 | | |
1715 | 0 | if (max_depth == 1) |
1716 | 0 | { |
1717 | | /* If we had recursed into parse_value_from_blob() again to |
1718 | | * parse the array values, this would have been emitted. */ |
1719 | 0 | g_set_error_literal (&local_error, |
1720 | 0 | G_IO_ERROR, |
1721 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1722 | 0 | _("Value nested too deeply")); |
1723 | 0 | goto fail; |
1724 | 0 | } |
1725 | | |
1726 | 0 | ensure_input_padding (buf, fixed_size); |
1727 | 0 | array_data = read_bytes (buf, array_len, &local_error); |
1728 | 0 | if (array_data == NULL) |
1729 | 0 | goto fail; |
1730 | | |
1731 | 0 | ret = g_variant_new_fixed_array (element_type, array_data, array_len / fixed_size, fixed_size); |
1732 | |
|
1733 | 0 | if (g_memory_buffer_is_byteswapped (buf)) |
1734 | 0 | { |
1735 | 0 | GVariant *tmp = g_variant_ref_sink (ret); |
1736 | 0 | ret = g_variant_byteswap (tmp); |
1737 | 0 | g_variant_unref (tmp); |
1738 | 0 | } |
1739 | 0 | } |
1740 | 0 | else |
1741 | 0 | { |
1742 | 0 | GVariantBuilder builder; |
1743 | 0 | goffset offset; |
1744 | 0 | goffset target; |
1745 | |
|
1746 | 0 | g_variant_builder_init (&builder, type); |
1747 | |
|
1748 | 0 | if (array_len == 0) |
1749 | 0 | { |
1750 | 0 | GVariant *item G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */; |
1751 | 0 | item = parse_value_from_blob (buf, |
1752 | 0 | element_type, |
1753 | 0 | max_depth - 1, |
1754 | 0 | TRUE, |
1755 | 0 | indent + 2, |
1756 | 0 | NULL); |
1757 | 0 | g_assert (item == NULL); |
1758 | 0 | } |
1759 | 0 | else |
1760 | 0 | { |
1761 | 0 | offset = buf->pos; |
1762 | 0 | target = offset + array_len; |
1763 | 0 | while (offset < target) |
1764 | 0 | { |
1765 | 0 | GVariant *item; |
1766 | 0 | item = parse_value_from_blob (buf, |
1767 | 0 | element_type, |
1768 | 0 | max_depth - 1, |
1769 | 0 | FALSE, |
1770 | 0 | indent + 2, |
1771 | 0 | &local_error); |
1772 | 0 | if (item == NULL) |
1773 | 0 | { |
1774 | 0 | g_variant_builder_clear (&builder); |
1775 | 0 | goto fail; |
1776 | 0 | } |
1777 | 0 | g_variant_builder_add_value (&builder, item); |
1778 | 0 | g_variant_unref (item); |
1779 | 0 | offset = buf->pos; |
1780 | 0 | } |
1781 | 0 | } |
1782 | | |
1783 | 0 | ret = g_variant_builder_end (&builder); |
1784 | 0 | } |
1785 | 0 | } |
1786 | 0 | break; |
1787 | | |
1788 | 0 | default: |
1789 | 0 | if (g_variant_type_is_dict_entry (type)) |
1790 | 0 | { |
1791 | 0 | const GVariantType *key_type; |
1792 | 0 | const GVariantType *value_type; |
1793 | 0 | GVariant *key; |
1794 | 0 | GVariant *value; |
1795 | |
|
1796 | 0 | ensure_input_padding (buf, 8); |
1797 | |
|
1798 | | #ifdef DEBUG_SERIALIZER |
1799 | | is_leaf = FALSE; |
1800 | | g_print ("\n"); |
1801 | | #endif /* DEBUG_SERIALIZER */ |
1802 | |
|
1803 | 0 | if (!just_align) |
1804 | 0 | { |
1805 | 0 | key_type = g_variant_type_key (type); |
1806 | 0 | key = parse_value_from_blob (buf, |
1807 | 0 | key_type, |
1808 | 0 | max_depth - 1, |
1809 | 0 | FALSE, |
1810 | 0 | indent + 2, |
1811 | 0 | &local_error); |
1812 | 0 | if (key == NULL) |
1813 | 0 | goto fail; |
1814 | 0 | value_type = g_variant_type_value (type); |
1815 | 0 | value = parse_value_from_blob (buf, |
1816 | 0 | value_type, |
1817 | 0 | max_depth - 1, |
1818 | 0 | FALSE, |
1819 | 0 | indent + 2, |
1820 | 0 | &local_error); |
1821 | 0 | if (value == NULL) |
1822 | 0 | { |
1823 | 0 | g_variant_unref (key); |
1824 | 0 | goto fail; |
1825 | 0 | } |
1826 | 0 | ret = g_variant_new_dict_entry (key, value); |
1827 | 0 | g_variant_unref (key); |
1828 | 0 | g_variant_unref (value); |
1829 | 0 | } |
1830 | 0 | } |
1831 | 0 | else if (g_variant_type_is_tuple (type)) |
1832 | 0 | { |
1833 | 0 | ensure_input_padding (buf, 8); |
1834 | |
|
1835 | | #ifdef DEBUG_SERIALIZER |
1836 | | is_leaf = FALSE; |
1837 | | g_print ("\n"); |
1838 | | #endif /* DEBUG_SERIALIZER */ |
1839 | |
|
1840 | 0 | if (!just_align) |
1841 | 0 | { |
1842 | 0 | const GVariantType *element_type; |
1843 | 0 | GVariantBuilder builder; |
1844 | |
|
1845 | 0 | g_variant_builder_init (&builder, type); |
1846 | 0 | element_type = g_variant_type_first (type); |
1847 | 0 | while (element_type != NULL) |
1848 | 0 | { |
1849 | 0 | GVariant *item; |
1850 | 0 | item = parse_value_from_blob (buf, |
1851 | 0 | element_type, |
1852 | 0 | max_depth - 1, |
1853 | 0 | FALSE, |
1854 | 0 | indent + 2, |
1855 | 0 | &local_error); |
1856 | 0 | if (item == NULL) |
1857 | 0 | { |
1858 | 0 | g_variant_builder_clear (&builder); |
1859 | 0 | goto fail; |
1860 | 0 | } |
1861 | 0 | g_variant_builder_add_value (&builder, item); |
1862 | 0 | g_variant_unref (item); |
1863 | |
|
1864 | 0 | element_type = g_variant_type_next (element_type); |
1865 | 0 | } |
1866 | 0 | ret = g_variant_builder_end (&builder); |
1867 | 0 | } |
1868 | 0 | } |
1869 | 0 | else if (g_variant_type_is_variant (type)) |
1870 | 0 | { |
1871 | | #ifdef DEBUG_SERIALIZER |
1872 | | is_leaf = FALSE; |
1873 | | g_print ("\n"); |
1874 | | #endif /* DEBUG_SERIALIZER */ |
1875 | |
|
1876 | 0 | if (!just_align) |
1877 | 0 | { |
1878 | 0 | guchar siglen; |
1879 | 0 | const gchar *sig; |
1880 | 0 | GVariantType *variant_type; |
1881 | 0 | GVariant *value; |
1882 | |
|
1883 | 0 | siglen = g_memory_buffer_read_byte (buf); |
1884 | 0 | sig = read_string (buf, (gsize) siglen, &local_error); |
1885 | 0 | if (sig == NULL) |
1886 | 0 | goto fail; |
1887 | 0 | if (!g_variant_is_signature (sig) || |
1888 | 0 | !g_variant_type_string_is_valid (sig)) |
1889 | 0 | { |
1890 | | /* A D-Bus signature can contain zero or more complete types, |
1891 | | * but a GVariant has to be exactly one complete type. */ |
1892 | 0 | g_set_error (&local_error, |
1893 | 0 | G_IO_ERROR, |
1894 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1895 | 0 | _("Parsed value “%s” for variant is not a valid D-Bus signature"), |
1896 | 0 | sig); |
1897 | 0 | goto fail; |
1898 | 0 | } |
1899 | | |
1900 | 0 | if (max_depth <= g_variant_type_string_get_depth_ (sig)) |
1901 | 0 | { |
1902 | | /* Catch the type nesting being too deep without having to |
1903 | | * parse the data. We don’t have to check this for static |
1904 | | * container types (like arrays and tuples, above) because |
1905 | | * the g_variant_type_string_is_valid() check performed before |
1906 | | * the initial parse_value_from_blob() call should check the |
1907 | | * static type nesting. */ |
1908 | 0 | g_set_error_literal (&local_error, |
1909 | 0 | G_IO_ERROR, |
1910 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1911 | 0 | _("Value nested too deeply")); |
1912 | 0 | goto fail; |
1913 | 0 | } |
1914 | | |
1915 | 0 | variant_type = g_variant_type_new (sig); |
1916 | 0 | value = parse_value_from_blob (buf, |
1917 | 0 | variant_type, |
1918 | 0 | max_depth - 1, |
1919 | 0 | FALSE, |
1920 | 0 | indent + 2, |
1921 | 0 | &local_error); |
1922 | 0 | g_variant_type_free (variant_type); |
1923 | 0 | if (value == NULL) |
1924 | 0 | goto fail; |
1925 | 0 | ret = g_variant_new_variant (value); |
1926 | 0 | g_variant_unref (value); |
1927 | 0 | } |
1928 | 0 | } |
1929 | 0 | else |
1930 | 0 | { |
1931 | 0 | gchar *s; |
1932 | 0 | s = g_variant_type_dup_string (type); |
1933 | 0 | g_set_error (&local_error, |
1934 | 0 | G_IO_ERROR, |
1935 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
1936 | 0 | _("Error deserializing GVariant with type string “%s” from the D-Bus wire format"), |
1937 | 0 | s); |
1938 | 0 | g_free (s); |
1939 | 0 | goto fail; |
1940 | 0 | } |
1941 | 0 | break; |
1942 | 0 | } |
1943 | | |
1944 | 0 | g_assert ((just_align && ret == NULL) || (!just_align && ret != NULL)); |
1945 | | |
1946 | | #ifdef DEBUG_SERIALIZER |
1947 | | if (ret != NULL) |
1948 | | { |
1949 | | if (is_leaf) |
1950 | | { |
1951 | | gchar *s; |
1952 | | if (g_variant_type_equal (type, G_VARIANT_TYPE_BYTE)) |
1953 | | { |
1954 | | s = g_strdup_printf ("0x%02x '%c'", g_variant_get_byte (ret), g_variant_get_byte (ret)); |
1955 | | } |
1956 | | else |
1957 | | { |
1958 | | s = g_variant_print (ret, FALSE); |
1959 | | } |
1960 | | g_print (": %s\n", s); |
1961 | | g_free (s); |
1962 | | } |
1963 | | } |
1964 | | #endif /* DEBUG_SERIALIZER */ |
1965 | | |
1966 | | /* sink the reference, if floating */ |
1967 | 0 | if (ret != NULL) |
1968 | 0 | g_variant_take_ref (ret); |
1969 | 0 | return ret; |
1970 | | |
1971 | 0 | fail: |
1972 | | #ifdef DEBUG_SERIALIZER |
1973 | | g_print ("\n" |
1974 | | "%*sFAILURE: %s (%s, %d)\n", |
1975 | | indent, "", |
1976 | | local_error->message, |
1977 | | g_quark_to_string (local_error->domain), |
1978 | | local_error->code); |
1979 | | #endif /* DEBUG_SERIALIZER */ |
1980 | 0 | g_propagate_error (error, local_error); |
1981 | 0 | return NULL; |
1982 | 0 | } |
1983 | | |
1984 | | /* ---------------------------------------------------------------------------------------------------- */ |
1985 | | |
1986 | | /* message_header must be at least 16 bytes */ |
1987 | | |
1988 | | /** |
1989 | | * g_dbus_message_bytes_needed: |
1990 | | * @blob: (array length=blob_len) (element-type guint8): A blob representing a binary D-Bus message. |
1991 | | * @blob_len: The length of @blob (must be at least 16). |
1992 | | * @error: Return location for error or %NULL. |
1993 | | * |
1994 | | * Utility function to calculate how many bytes are needed to |
1995 | | * completely deserialize the D-Bus message stored at @blob. |
1996 | | * |
1997 | | * Returns: Number of bytes needed or -1 if @error is set (e.g. if |
1998 | | * @blob contains invalid data or not enough data is available to |
1999 | | * determine the size). |
2000 | | * |
2001 | | * Since: 2.26 |
2002 | | */ |
2003 | | gssize |
2004 | | g_dbus_message_bytes_needed (guchar *blob, |
2005 | | gsize blob_len, |
2006 | | GError **error) |
2007 | 0 | { |
2008 | 0 | gssize ret; |
2009 | |
|
2010 | 0 | ret = -1; |
2011 | |
|
2012 | 0 | g_return_val_if_fail (blob != NULL, -1); |
2013 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, -1); |
2014 | 0 | g_return_val_if_fail (blob_len >= 16, -1); |
2015 | | |
2016 | 0 | if (blob[0] == 'l') |
2017 | 0 | { |
2018 | | /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */ |
2019 | 0 | ret = 12 + 4 + GUINT32_FROM_LE (((guint32 *) blob)[3]); |
2020 | | /* round up so it's a multiple of 8 */ |
2021 | 0 | ret = 8 * ((ret + 7)/8); |
2022 | | /* finally add the body size */ |
2023 | 0 | ret += GUINT32_FROM_LE (((guint32 *) blob)[1]); |
2024 | 0 | } |
2025 | 0 | else if (blob[0] == 'B') |
2026 | 0 | { |
2027 | | /* core header (12 bytes) + ARRAY of STRUCT of (BYTE,VARIANT) */ |
2028 | 0 | ret = 12 + 4 + GUINT32_FROM_BE (((guint32 *) blob)[3]); |
2029 | | /* round up so it's a multiple of 8 */ |
2030 | 0 | ret = 8 * ((ret + 7)/8); |
2031 | | /* finally add the body size */ |
2032 | 0 | ret += GUINT32_FROM_BE (((guint32 *) blob)[1]); |
2033 | 0 | } |
2034 | 0 | else |
2035 | 0 | { |
2036 | 0 | g_set_error (error, |
2037 | 0 | G_IO_ERROR, |
2038 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2039 | 0 | "Unable to determine message blob length - given blob is malformed"); |
2040 | 0 | } |
2041 | |
|
2042 | 0 | if (ret > (1<<27)) |
2043 | 0 | { |
2044 | 0 | g_set_error (error, |
2045 | 0 | G_IO_ERROR, |
2046 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2047 | 0 | "Blob indicates that message exceeds maximum message length (128MiB)"); |
2048 | 0 | ret = -1; |
2049 | 0 | } |
2050 | |
|
2051 | 0 | return ret; |
2052 | 0 | } |
2053 | | |
2054 | | /* ---------------------------------------------------------------------------------------------------- */ |
2055 | | |
2056 | | /** |
2057 | | * g_dbus_message_new_from_blob: |
2058 | | * @blob: (array length=blob_len) (element-type guint8): A blob representing a binary D-Bus message. |
2059 | | * @blob_len: The length of @blob. |
2060 | | * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported. |
2061 | | * @error: Return location for error or %NULL. |
2062 | | * |
2063 | | * Creates a new #GDBusMessage from the data stored at @blob. The byte |
2064 | | * order that the message was in can be retrieved using |
2065 | | * g_dbus_message_get_byte_order(). |
2066 | | * |
2067 | | * If the @blob cannot be parsed, contains invalid fields, or contains invalid |
2068 | | * headers, %G_IO_ERROR_INVALID_ARGUMENT will be returned. |
2069 | | * |
2070 | | * Returns: A new #GDBusMessage or %NULL if @error is set. Free with |
2071 | | * g_object_unref(). |
2072 | | * |
2073 | | * Since: 2.26 |
2074 | | */ |
2075 | | GDBusMessage * |
2076 | | g_dbus_message_new_from_blob (guchar *blob, |
2077 | | gsize blob_len, |
2078 | | GDBusCapabilityFlags capabilities, |
2079 | | GError **error) |
2080 | 0 | { |
2081 | 0 | gboolean ret; |
2082 | 0 | GMemoryBuffer mbuf; |
2083 | 0 | GDBusMessage *message; |
2084 | 0 | guchar endianness; |
2085 | 0 | guchar major_protocol_version; |
2086 | 0 | guint32 message_body_len; |
2087 | 0 | GVariant *headers; |
2088 | 0 | GVariant *item; |
2089 | 0 | GVariantIter iter; |
2090 | 0 | GVariant *signature; |
2091 | | |
2092 | | /* TODO: check against @capabilities */ |
2093 | |
|
2094 | 0 | ret = FALSE; |
2095 | |
|
2096 | 0 | g_return_val_if_fail (blob != NULL, NULL); |
2097 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
2098 | 0 | g_return_val_if_fail (blob_len >= 12, NULL); |
2099 | | |
2100 | 0 | message = g_dbus_message_new (); |
2101 | |
|
2102 | 0 | memset (&mbuf, 0, sizeof (mbuf)); |
2103 | 0 | mbuf.data = (gchar *)blob; |
2104 | 0 | mbuf.len = mbuf.valid_len = blob_len; |
2105 | |
|
2106 | 0 | endianness = g_memory_buffer_read_byte (&mbuf); |
2107 | 0 | switch (endianness) |
2108 | 0 | { |
2109 | 0 | case 'l': |
2110 | 0 | mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN; |
2111 | 0 | message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN; |
2112 | 0 | break; |
2113 | 0 | case 'B': |
2114 | 0 | mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN; |
2115 | 0 | message->byte_order = G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN; |
2116 | 0 | break; |
2117 | 0 | default: |
2118 | 0 | g_set_error (error, |
2119 | 0 | G_IO_ERROR, |
2120 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2121 | 0 | _("Invalid endianness value. Expected 0x6c (“l”) or 0x42 (“B”) but found value 0x%02x"), |
2122 | 0 | endianness); |
2123 | 0 | goto out; |
2124 | 0 | } |
2125 | | |
2126 | 0 | message->type = g_memory_buffer_read_byte (&mbuf); |
2127 | 0 | message->flags = g_memory_buffer_read_byte (&mbuf); |
2128 | 0 | major_protocol_version = g_memory_buffer_read_byte (&mbuf); |
2129 | 0 | if (major_protocol_version != 1) |
2130 | 0 | { |
2131 | 0 | g_set_error (error, |
2132 | 0 | G_IO_ERROR, |
2133 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2134 | 0 | _("Invalid major protocol version. Expected 1 but found %d"), |
2135 | 0 | major_protocol_version); |
2136 | 0 | goto out; |
2137 | 0 | } |
2138 | 0 | message_body_len = g_memory_buffer_read_uint32 (&mbuf); |
2139 | 0 | message->serial = g_memory_buffer_read_uint32 (&mbuf); |
2140 | |
|
2141 | | #ifdef DEBUG_SERIALIZER |
2142 | | g_print ("Parsing blob (blob_len = 0x%04x bytes)\n", (gint) blob_len); |
2143 | | { |
2144 | | gchar *s; |
2145 | | s = _g_dbus_hexdump ((const gchar *) blob, blob_len, 2); |
2146 | | g_print ("%s\n", s); |
2147 | | g_free (s); |
2148 | | } |
2149 | | #endif /* DEBUG_SERIALIZER */ |
2150 | |
|
2151 | | #ifdef DEBUG_SERIALIZER |
2152 | | g_print ("Parsing headers (blob_len = 0x%04x bytes)\n", (gint) blob_len); |
2153 | | #endif /* DEBUG_SERIALIZER */ |
2154 | 0 | headers = parse_value_from_blob (&mbuf, |
2155 | 0 | G_VARIANT_TYPE ("a{yv}"), |
2156 | 0 | G_DBUS_MAX_TYPE_DEPTH + 2 /* for the a{yv} */, |
2157 | 0 | FALSE, |
2158 | 0 | 2, |
2159 | 0 | error); |
2160 | 0 | if (headers == NULL) |
2161 | 0 | goto out; |
2162 | 0 | g_variant_iter_init (&iter, headers); |
2163 | 0 | while ((item = g_variant_iter_next_value (&iter)) != NULL) |
2164 | 0 | { |
2165 | 0 | guchar header_field; |
2166 | 0 | GVariant *value; |
2167 | 0 | g_variant_get (item, |
2168 | 0 | "{yv}", |
2169 | 0 | &header_field, |
2170 | 0 | &value); |
2171 | 0 | g_dbus_message_set_header (message, header_field, value); |
2172 | 0 | g_variant_unref (value); |
2173 | 0 | g_variant_unref (item); |
2174 | 0 | } |
2175 | 0 | g_variant_unref (headers); |
2176 | |
|
2177 | 0 | signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE); |
2178 | 0 | if (signature != NULL) |
2179 | 0 | { |
2180 | 0 | const gchar *signature_str; |
2181 | 0 | gsize signature_str_len; |
2182 | |
|
2183 | 0 | if (!g_variant_is_of_type (signature, G_VARIANT_TYPE_SIGNATURE)) |
2184 | 0 | { |
2185 | 0 | g_set_error_literal (error, |
2186 | 0 | G_IO_ERROR, |
2187 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2188 | 0 | _("Signature header found but is not of type signature")); |
2189 | 0 | goto out; |
2190 | 0 | } |
2191 | | |
2192 | 0 | signature_str = g_variant_get_string (signature, &signature_str_len); |
2193 | | |
2194 | | /* signature but no body */ |
2195 | 0 | if (message_body_len == 0 && signature_str_len > 0) |
2196 | 0 | { |
2197 | 0 | g_set_error (error, |
2198 | 0 | G_IO_ERROR, |
2199 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2200 | 0 | _("Signature header with signature “%s” found but message body is empty"), |
2201 | 0 | signature_str); |
2202 | 0 | goto out; |
2203 | 0 | } |
2204 | 0 | else if (signature_str_len > 0) |
2205 | 0 | { |
2206 | 0 | GVariantType *variant_type; |
2207 | 0 | gchar *tupled_signature_str = g_strdup_printf ("(%s)", signature_str); |
2208 | |
|
2209 | 0 | if (!g_variant_is_signature (signature_str) || |
2210 | 0 | !g_variant_type_string_is_valid (tupled_signature_str)) |
2211 | 0 | { |
2212 | 0 | g_set_error (error, |
2213 | 0 | G_IO_ERROR, |
2214 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2215 | 0 | _("Parsed value “%s” is not a valid D-Bus signature (for body)"), |
2216 | 0 | signature_str); |
2217 | 0 | g_free (tupled_signature_str); |
2218 | 0 | goto out; |
2219 | 0 | } |
2220 | | |
2221 | 0 | variant_type = g_variant_type_new (tupled_signature_str); |
2222 | 0 | g_free (tupled_signature_str); |
2223 | | #ifdef DEBUG_SERIALIZER |
2224 | | g_print ("Parsing body (blob_len = 0x%04x bytes)\n", (gint) blob_len); |
2225 | | #endif /* DEBUG_SERIALIZER */ |
2226 | 0 | message->body = parse_value_from_blob (&mbuf, |
2227 | 0 | variant_type, |
2228 | 0 | G_DBUS_MAX_TYPE_DEPTH + 1 /* for the surrounding tuple */, |
2229 | 0 | FALSE, |
2230 | 0 | 2, |
2231 | 0 | error); |
2232 | 0 | g_variant_type_free (variant_type); |
2233 | 0 | if (message->body == NULL) |
2234 | 0 | goto out; |
2235 | 0 | } |
2236 | 0 | } |
2237 | 0 | else |
2238 | 0 | { |
2239 | | /* no signature, this is only OK if the body is empty */ |
2240 | 0 | if (message_body_len != 0) |
2241 | 0 | { |
2242 | | /* G_GUINT32_FORMAT doesn't work with gettext, just use %u */ |
2243 | 0 | g_set_error (error, |
2244 | 0 | G_IO_ERROR, |
2245 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2246 | 0 | g_dngettext (GETTEXT_PACKAGE, |
2247 | 0 | "No signature header in message but the message body is %u byte", |
2248 | 0 | "No signature header in message but the message body is %u bytes", |
2249 | 0 | message_body_len), |
2250 | 0 | message_body_len); |
2251 | 0 | goto out; |
2252 | 0 | } |
2253 | 0 | } |
2254 | | |
2255 | 0 | if (!validate_headers (message, error)) |
2256 | 0 | { |
2257 | 0 | g_prefix_error (error, _("Cannot deserialize message: ")); |
2258 | 0 | goto out; |
2259 | 0 | } |
2260 | | |
2261 | 0 | ret = TRUE; |
2262 | |
|
2263 | 0 | out: |
2264 | 0 | if (ret) |
2265 | 0 | { |
2266 | 0 | return message; |
2267 | 0 | } |
2268 | 0 | else |
2269 | 0 | { |
2270 | 0 | if (message != NULL) |
2271 | 0 | g_object_unref (message); |
2272 | 0 | return NULL; |
2273 | 0 | } |
2274 | 0 | } |
2275 | | |
2276 | | /* ---------------------------------------------------------------------------------------------------- */ |
2277 | | |
2278 | | static gsize |
2279 | | ensure_output_padding (GMemoryBuffer *mbuf, |
2280 | | gsize padding_size) |
2281 | 0 | { |
2282 | 0 | gsize offset; |
2283 | 0 | gsize wanted_offset; |
2284 | 0 | gsize padding_needed; |
2285 | 0 | guint n; |
2286 | |
|
2287 | 0 | offset = mbuf->pos; |
2288 | 0 | wanted_offset = ((offset + padding_size - 1) / padding_size) * padding_size; |
2289 | 0 | padding_needed = wanted_offset - offset; |
2290 | |
|
2291 | 0 | for (n = 0; n < padding_needed; n++) |
2292 | 0 | g_memory_buffer_put_byte (mbuf, '\0'); |
2293 | |
|
2294 | 0 | return padding_needed; |
2295 | 0 | } |
2296 | | |
2297 | | /* note that value can be NULL for e.g. empty arrays - type is never NULL */ |
2298 | | static gboolean |
2299 | | append_value_to_blob (GVariant *value, |
2300 | | const GVariantType *type, |
2301 | | GMemoryBuffer *mbuf, |
2302 | | gsize *out_padding_added, |
2303 | | GError **error) |
2304 | 0 | { |
2305 | 0 | gsize padding_added; |
2306 | 0 | const gchar *type_string; |
2307 | |
|
2308 | 0 | type_string = g_variant_type_peek_string (type); |
2309 | |
|
2310 | 0 | padding_added = 0; |
2311 | |
|
2312 | 0 | switch (type_string[0]) |
2313 | 0 | { |
2314 | 0 | case 'b': /* G_VARIANT_TYPE_BOOLEAN */ |
2315 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2316 | 0 | if (value != NULL) |
2317 | 0 | { |
2318 | 0 | gboolean v = g_variant_get_boolean (value); |
2319 | 0 | g_memory_buffer_put_uint32 (mbuf, v); |
2320 | 0 | } |
2321 | 0 | break; |
2322 | | |
2323 | 0 | case 'y': /* G_VARIANT_TYPE_BYTE */ |
2324 | 0 | if (value != NULL) |
2325 | 0 | { |
2326 | 0 | guint8 v = g_variant_get_byte (value); |
2327 | 0 | g_memory_buffer_put_byte (mbuf, v); |
2328 | 0 | } |
2329 | 0 | break; |
2330 | | |
2331 | 0 | case 'n': /* G_VARIANT_TYPE_INT16 */ |
2332 | 0 | padding_added = ensure_output_padding (mbuf, 2); |
2333 | 0 | if (value != NULL) |
2334 | 0 | { |
2335 | 0 | gint16 v = g_variant_get_int16 (value); |
2336 | 0 | g_memory_buffer_put_int16 (mbuf, v); |
2337 | 0 | } |
2338 | 0 | break; |
2339 | | |
2340 | 0 | case 'q': /* G_VARIANT_TYPE_UINT16 */ |
2341 | 0 | padding_added = ensure_output_padding (mbuf, 2); |
2342 | 0 | if (value != NULL) |
2343 | 0 | { |
2344 | 0 | guint16 v = g_variant_get_uint16 (value); |
2345 | 0 | g_memory_buffer_put_uint16 (mbuf, v); |
2346 | 0 | } |
2347 | 0 | break; |
2348 | | |
2349 | 0 | case 'i': /* G_VARIANT_TYPE_INT32 */ |
2350 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2351 | 0 | if (value != NULL) |
2352 | 0 | { |
2353 | 0 | gint32 v = g_variant_get_int32 (value); |
2354 | 0 | g_memory_buffer_put_int32 (mbuf, v); |
2355 | 0 | } |
2356 | 0 | break; |
2357 | | |
2358 | 0 | case 'u': /* G_VARIANT_TYPE_UINT32 */ |
2359 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2360 | 0 | if (value != NULL) |
2361 | 0 | { |
2362 | 0 | guint32 v = g_variant_get_uint32 (value); |
2363 | 0 | g_memory_buffer_put_uint32 (mbuf, v); |
2364 | 0 | } |
2365 | 0 | break; |
2366 | | |
2367 | 0 | case 'x': /* G_VARIANT_TYPE_INT64 */ |
2368 | 0 | padding_added = ensure_output_padding (mbuf, 8); |
2369 | 0 | if (value != NULL) |
2370 | 0 | { |
2371 | 0 | gint64 v = g_variant_get_int64 (value); |
2372 | 0 | g_memory_buffer_put_int64 (mbuf, v); |
2373 | 0 | } |
2374 | 0 | break; |
2375 | | |
2376 | 0 | case 't': /* G_VARIANT_TYPE_UINT64 */ |
2377 | 0 | padding_added = ensure_output_padding (mbuf, 8); |
2378 | 0 | if (value != NULL) |
2379 | 0 | { |
2380 | 0 | guint64 v = g_variant_get_uint64 (value); |
2381 | 0 | g_memory_buffer_put_uint64 (mbuf, v); |
2382 | 0 | } |
2383 | 0 | break; |
2384 | | |
2385 | 0 | case 'd': /* G_VARIANT_TYPE_DOUBLE */ |
2386 | 0 | padding_added = ensure_output_padding (mbuf, 8); |
2387 | 0 | if (value != NULL) |
2388 | 0 | { |
2389 | 0 | union { |
2390 | 0 | guint64 v_uint64; |
2391 | 0 | gdouble v_double; |
2392 | 0 | } u; |
2393 | 0 | G_STATIC_ASSERT (sizeof (gdouble) == sizeof (guint64)); |
2394 | 0 | u.v_double = g_variant_get_double (value); |
2395 | 0 | g_memory_buffer_put_uint64 (mbuf, u.v_uint64); |
2396 | 0 | } |
2397 | 0 | break; |
2398 | | |
2399 | 0 | case 's': /* G_VARIANT_TYPE_STRING */ |
2400 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2401 | 0 | if (value != NULL) |
2402 | 0 | { |
2403 | 0 | gsize len; |
2404 | 0 | const gchar *v; |
2405 | 0 | #ifndef G_DISABLE_ASSERT |
2406 | 0 | const gchar *end; |
2407 | 0 | #endif |
2408 | |
|
2409 | 0 | v = g_variant_get_string (value, &len); |
2410 | 0 | g_assert (g_utf8_validate (v, -1, &end) && (end == v + len)); |
2411 | 0 | g_memory_buffer_put_uint32 (mbuf, len); |
2412 | 0 | g_memory_buffer_put_string (mbuf, v); |
2413 | 0 | g_memory_buffer_put_byte (mbuf, '\0'); |
2414 | 0 | } |
2415 | 0 | break; |
2416 | | |
2417 | 0 | case 'o': /* G_VARIANT_TYPE_OBJECT_PATH */ |
2418 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2419 | 0 | if (value != NULL) |
2420 | 0 | { |
2421 | 0 | gsize len; |
2422 | 0 | const gchar *v = g_variant_get_string (value, &len); |
2423 | 0 | g_assert (g_variant_is_object_path (v)); |
2424 | 0 | g_memory_buffer_put_uint32 (mbuf, len); |
2425 | 0 | g_memory_buffer_put_string (mbuf, v); |
2426 | 0 | g_memory_buffer_put_byte (mbuf, '\0'); |
2427 | 0 | } |
2428 | 0 | break; |
2429 | | |
2430 | 0 | case 'g': /* G_VARIANT_TYPE_SIGNATURE */ |
2431 | 0 | if (value != NULL) |
2432 | 0 | { |
2433 | 0 | gsize len; |
2434 | 0 | const gchar *v = g_variant_get_string (value, &len); |
2435 | 0 | g_assert (g_variant_is_signature (v)); |
2436 | 0 | g_memory_buffer_put_byte (mbuf, len); |
2437 | 0 | g_memory_buffer_put_string (mbuf, v); |
2438 | 0 | g_memory_buffer_put_byte (mbuf, '\0'); |
2439 | 0 | } |
2440 | 0 | break; |
2441 | | |
2442 | 0 | case 'h': /* G_VARIANT_TYPE_HANDLE */ |
2443 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2444 | 0 | if (value != NULL) |
2445 | 0 | { |
2446 | 0 | gint32 v = g_variant_get_handle (value); |
2447 | 0 | g_memory_buffer_put_int32 (mbuf, v); |
2448 | 0 | } |
2449 | 0 | break; |
2450 | | |
2451 | 0 | case 'a': /* G_VARIANT_TYPE_ARRAY */ |
2452 | 0 | { |
2453 | 0 | const GVariantType *element_type; |
2454 | 0 | GVariant *item; |
2455 | 0 | GVariantIter iter; |
2456 | 0 | goffset array_len_offset; |
2457 | 0 | goffset array_payload_begin_offset; |
2458 | 0 | goffset cur_offset; |
2459 | 0 | gsize array_len; |
2460 | 0 | guint fixed_size; |
2461 | |
|
2462 | 0 | padding_added = ensure_output_padding (mbuf, 4); |
2463 | 0 | if (value != NULL) |
2464 | 0 | { |
2465 | | /* array length - will be filled in later */ |
2466 | 0 | array_len_offset = mbuf->valid_len; |
2467 | 0 | g_memory_buffer_put_uint32 (mbuf, 0xF00DFACE); |
2468 | | |
2469 | | /* From the D-Bus spec: |
2470 | | * |
2471 | | * "A UINT32 giving the length of the array data in bytes, |
2472 | | * followed by alignment padding to the alignment boundary of |
2473 | | * the array element type, followed by each array element. The |
2474 | | * array length is from the end of the alignment padding to |
2475 | | * the end of the last element, i.e. it does not include the |
2476 | | * padding after the length, or any padding after the last |
2477 | | * element." |
2478 | | * |
2479 | | * Thus, we need to count how much padding the first element |
2480 | | * contributes and subtract that from the array length. |
2481 | | */ |
2482 | 0 | array_payload_begin_offset = mbuf->valid_len; |
2483 | |
|
2484 | 0 | element_type = g_variant_type_element (type); |
2485 | 0 | fixed_size = get_type_fixed_size (element_type); |
2486 | |
|
2487 | 0 | if (g_variant_n_children (value) == 0) |
2488 | 0 | { |
2489 | 0 | gsize padding_added_for_item; |
2490 | 0 | if (!append_value_to_blob (NULL, |
2491 | 0 | element_type, |
2492 | 0 | mbuf, |
2493 | 0 | &padding_added_for_item, |
2494 | 0 | error)) |
2495 | 0 | goto fail; |
2496 | 0 | array_payload_begin_offset += padding_added_for_item; |
2497 | 0 | } |
2498 | 0 | else if (fixed_size != 0) |
2499 | 0 | { |
2500 | 0 | GVariant *use_value; |
2501 | |
|
2502 | 0 | if (g_memory_buffer_is_byteswapped (mbuf)) |
2503 | 0 | use_value = g_variant_byteswap (value); |
2504 | 0 | else |
2505 | 0 | use_value = g_variant_ref (value); |
2506 | |
|
2507 | 0 | array_payload_begin_offset += ensure_output_padding (mbuf, fixed_size); |
2508 | |
|
2509 | 0 | array_len = g_variant_get_size (use_value); |
2510 | 0 | g_memory_buffer_write (mbuf, g_variant_get_data (use_value), array_len); |
2511 | 0 | g_variant_unref (use_value); |
2512 | 0 | } |
2513 | 0 | else |
2514 | 0 | { |
2515 | 0 | guint n; |
2516 | 0 | n = 0; |
2517 | 0 | g_variant_iter_init (&iter, value); |
2518 | 0 | while ((item = g_variant_iter_next_value (&iter)) != NULL) |
2519 | 0 | { |
2520 | 0 | gsize padding_added_for_item; |
2521 | 0 | if (!append_value_to_blob (item, |
2522 | 0 | g_variant_get_type (item), |
2523 | 0 | mbuf, |
2524 | 0 | &padding_added_for_item, |
2525 | 0 | error)) |
2526 | 0 | { |
2527 | 0 | g_variant_unref (item); |
2528 | 0 | goto fail; |
2529 | 0 | } |
2530 | 0 | g_variant_unref (item); |
2531 | 0 | if (n == 0) |
2532 | 0 | { |
2533 | 0 | array_payload_begin_offset += padding_added_for_item; |
2534 | 0 | } |
2535 | 0 | n++; |
2536 | 0 | } |
2537 | 0 | } |
2538 | | |
2539 | 0 | cur_offset = mbuf->valid_len; |
2540 | 0 | array_len = cur_offset - array_payload_begin_offset; |
2541 | 0 | mbuf->pos = array_len_offset; |
2542 | |
|
2543 | 0 | g_memory_buffer_put_uint32 (mbuf, array_len); |
2544 | 0 | mbuf->pos = cur_offset; |
2545 | 0 | } |
2546 | 0 | } |
2547 | 0 | break; |
2548 | | |
2549 | 0 | default: |
2550 | 0 | if (g_variant_type_is_dict_entry (type) || g_variant_type_is_tuple (type)) |
2551 | 0 | { |
2552 | 0 | padding_added = ensure_output_padding (mbuf, 8); |
2553 | 0 | if (value != NULL) |
2554 | 0 | { |
2555 | 0 | GVariant *item; |
2556 | 0 | GVariantIter iter; |
2557 | 0 | g_variant_iter_init (&iter, value); |
2558 | 0 | while ((item = g_variant_iter_next_value (&iter)) != NULL) |
2559 | 0 | { |
2560 | 0 | if (!append_value_to_blob (item, |
2561 | 0 | g_variant_get_type (item), |
2562 | 0 | mbuf, |
2563 | 0 | NULL, |
2564 | 0 | error)) |
2565 | 0 | { |
2566 | 0 | g_variant_unref (item); |
2567 | 0 | goto fail; |
2568 | 0 | } |
2569 | 0 | g_variant_unref (item); |
2570 | 0 | } |
2571 | 0 | } |
2572 | 0 | } |
2573 | 0 | else if (g_variant_type_is_variant (type)) |
2574 | 0 | { |
2575 | 0 | if (value != NULL) |
2576 | 0 | { |
2577 | 0 | GVariant *child; |
2578 | 0 | const gchar *signature; |
2579 | 0 | child = g_variant_get_child_value (value, 0); |
2580 | 0 | signature = g_variant_get_type_string (child); |
2581 | 0 | g_memory_buffer_put_byte (mbuf, strlen (signature)); |
2582 | 0 | g_memory_buffer_put_string (mbuf, signature); |
2583 | 0 | g_memory_buffer_put_byte (mbuf, '\0'); |
2584 | 0 | if (!append_value_to_blob (child, |
2585 | 0 | g_variant_get_type (child), |
2586 | 0 | mbuf, |
2587 | 0 | NULL, |
2588 | 0 | error)) |
2589 | 0 | { |
2590 | 0 | g_variant_unref (child); |
2591 | 0 | goto fail; |
2592 | 0 | } |
2593 | 0 | g_variant_unref (child); |
2594 | 0 | } |
2595 | 0 | } |
2596 | 0 | else |
2597 | 0 | { |
2598 | 0 | g_set_error (error, |
2599 | 0 | G_IO_ERROR, |
2600 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2601 | 0 | _("Error serializing GVariant with type string “%s” to the D-Bus wire format"), |
2602 | 0 | g_variant_get_type_string (value)); |
2603 | 0 | goto fail; |
2604 | 0 | } |
2605 | 0 | break; |
2606 | 0 | } |
2607 | | |
2608 | 0 | if (out_padding_added != NULL) |
2609 | 0 | *out_padding_added = padding_added; |
2610 | |
|
2611 | 0 | return TRUE; |
2612 | | |
2613 | 0 | fail: |
2614 | 0 | return FALSE; |
2615 | 0 | } |
2616 | | |
2617 | | static gboolean |
2618 | | append_body_to_blob (GVariant *value, |
2619 | | GMemoryBuffer *mbuf, |
2620 | | GError **error) |
2621 | 0 | { |
2622 | 0 | GVariant *item; |
2623 | 0 | GVariantIter iter; |
2624 | |
|
2625 | 0 | if (!g_variant_is_of_type (value, G_VARIANT_TYPE_TUPLE)) |
2626 | 0 | { |
2627 | 0 | g_set_error (error, |
2628 | 0 | G_IO_ERROR, |
2629 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2630 | 0 | "Expected a tuple for the body of the GDBusMessage."); |
2631 | 0 | goto fail; |
2632 | 0 | } |
2633 | | |
2634 | 0 | g_variant_iter_init (&iter, value); |
2635 | 0 | while ((item = g_variant_iter_next_value (&iter)) != NULL) |
2636 | 0 | { |
2637 | 0 | if (!append_value_to_blob (item, |
2638 | 0 | g_variant_get_type (item), |
2639 | 0 | mbuf, |
2640 | 0 | NULL, |
2641 | 0 | error)) |
2642 | 0 | { |
2643 | 0 | g_variant_unref (item); |
2644 | 0 | goto fail; |
2645 | 0 | } |
2646 | 0 | g_variant_unref (item); |
2647 | 0 | } |
2648 | 0 | return TRUE; |
2649 | | |
2650 | 0 | fail: |
2651 | 0 | return FALSE; |
2652 | 0 | } |
2653 | | |
2654 | | /* ---------------------------------------------------------------------------------------------------- */ |
2655 | | |
2656 | | /** |
2657 | | * g_dbus_message_to_blob: |
2658 | | * @message: A #GDBusMessage. |
2659 | | * @out_size: Return location for size of generated blob. |
2660 | | * @capabilities: A #GDBusCapabilityFlags describing what protocol features are supported. |
2661 | | * @error: Return location for error. |
2662 | | * |
2663 | | * Serializes @message to a blob. The byte order returned by |
2664 | | * g_dbus_message_get_byte_order() will be used. |
2665 | | * |
2666 | | * Returns: (array length=out_size) (transfer full): A pointer to a |
2667 | | * valid binary D-Bus message of @out_size bytes generated by @message |
2668 | | * or %NULL if @error is set. Free with g_free(). |
2669 | | * |
2670 | | * Since: 2.26 |
2671 | | */ |
2672 | | guchar * |
2673 | | g_dbus_message_to_blob (GDBusMessage *message, |
2674 | | gsize *out_size, |
2675 | | GDBusCapabilityFlags capabilities, |
2676 | | GError **error) |
2677 | 0 | { |
2678 | 0 | GMemoryBuffer mbuf; |
2679 | 0 | guchar *ret; |
2680 | 0 | gsize size; |
2681 | 0 | goffset body_len_offset; |
2682 | 0 | goffset body_start_offset; |
2683 | 0 | gsize body_size; |
2684 | 0 | GVariant *header_fields; |
2685 | 0 | GVariantBuilder builder; |
2686 | 0 | GHashTableIter hash_iter; |
2687 | 0 | gpointer key; |
2688 | 0 | GVariant *header_value; |
2689 | 0 | GVariant *signature; |
2690 | 0 | const gchar *signature_str; |
2691 | 0 | gint num_fds_in_message; |
2692 | 0 | gint num_fds_according_to_header; |
2693 | | |
2694 | | /* TODO: check against @capabilities */ |
2695 | |
|
2696 | 0 | ret = NULL; |
2697 | |
|
2698 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
2699 | 0 | g_return_val_if_fail (out_size != NULL, NULL); |
2700 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
2701 | | |
2702 | 0 | memset (&mbuf, 0, sizeof (mbuf)); |
2703 | 0 | mbuf.len = MIN_ARRAY_SIZE; |
2704 | 0 | mbuf.data = g_malloc (mbuf.len); |
2705 | |
|
2706 | 0 | mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN; |
2707 | 0 | switch (message->byte_order) |
2708 | 0 | { |
2709 | 0 | case G_DBUS_MESSAGE_BYTE_ORDER_BIG_ENDIAN: |
2710 | 0 | mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN; |
2711 | 0 | break; |
2712 | 0 | case G_DBUS_MESSAGE_BYTE_ORDER_LITTLE_ENDIAN: |
2713 | 0 | mbuf.byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN; |
2714 | 0 | break; |
2715 | 0 | } |
2716 | | |
2717 | | /* Core header */ |
2718 | 0 | g_memory_buffer_put_byte (&mbuf, (guchar) message->byte_order); |
2719 | 0 | g_memory_buffer_put_byte (&mbuf, message->type); |
2720 | 0 | g_memory_buffer_put_byte (&mbuf, message->flags); |
2721 | 0 | g_memory_buffer_put_byte (&mbuf, 1); /* major protocol version */ |
2722 | 0 | body_len_offset = mbuf.valid_len; |
2723 | | /* body length - will be filled in later */ |
2724 | 0 | g_memory_buffer_put_uint32 (&mbuf, 0xF00DFACE); |
2725 | 0 | g_memory_buffer_put_uint32 (&mbuf, message->serial); |
2726 | |
|
2727 | 0 | num_fds_in_message = 0; |
2728 | 0 | #ifdef G_OS_UNIX |
2729 | 0 | if (message->fd_list != NULL) |
2730 | 0 | num_fds_in_message = g_unix_fd_list_get_length (message->fd_list); |
2731 | 0 | #endif |
2732 | 0 | num_fds_according_to_header = g_dbus_message_get_num_unix_fds (message); |
2733 | 0 | if (num_fds_in_message != num_fds_according_to_header) |
2734 | 0 | { |
2735 | 0 | g_set_error (error, |
2736 | 0 | G_IO_ERROR, |
2737 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2738 | 0 | _("Number of file descriptors in message (%d) differs from header field (%d)"), |
2739 | 0 | num_fds_in_message, |
2740 | 0 | num_fds_according_to_header); |
2741 | 0 | goto out; |
2742 | 0 | } |
2743 | | |
2744 | 0 | if (!validate_headers (message, error)) |
2745 | 0 | { |
2746 | 0 | g_prefix_error (error, _("Cannot serialize message: ")); |
2747 | 0 | goto out; |
2748 | 0 | } |
2749 | | |
2750 | 0 | g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{yv}")); |
2751 | 0 | g_hash_table_iter_init (&hash_iter, message->headers); |
2752 | 0 | while (g_hash_table_iter_next (&hash_iter, &key, (gpointer) &header_value)) |
2753 | 0 | { |
2754 | 0 | g_variant_builder_add (&builder, |
2755 | 0 | "{yv}", |
2756 | 0 | (guchar) GPOINTER_TO_UINT (key), |
2757 | 0 | header_value); |
2758 | 0 | } |
2759 | 0 | header_fields = g_variant_builder_end (&builder); |
2760 | |
|
2761 | 0 | if (!append_value_to_blob (header_fields, |
2762 | 0 | g_variant_get_type (header_fields), |
2763 | 0 | &mbuf, |
2764 | 0 | NULL, |
2765 | 0 | error)) |
2766 | 0 | { |
2767 | 0 | g_variant_unref (header_fields); |
2768 | 0 | goto out; |
2769 | 0 | } |
2770 | 0 | g_variant_unref (header_fields); |
2771 | | |
2772 | | /* header size must be a multiple of 8 */ |
2773 | 0 | ensure_output_padding (&mbuf, 8); |
2774 | |
|
2775 | 0 | body_start_offset = mbuf.valid_len; |
2776 | |
|
2777 | 0 | signature = g_dbus_message_get_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE); |
2778 | |
|
2779 | 0 | if (signature != NULL && !g_variant_is_of_type (signature, G_VARIANT_TYPE_SIGNATURE)) |
2780 | 0 | { |
2781 | 0 | g_set_error_literal (error, |
2782 | 0 | G_IO_ERROR, |
2783 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2784 | 0 | _("Signature header found but is not of type signature")); |
2785 | 0 | goto out; |
2786 | 0 | } |
2787 | | |
2788 | 0 | signature_str = NULL; |
2789 | 0 | if (signature != NULL) |
2790 | 0 | signature_str = g_variant_get_string (signature, NULL); |
2791 | 0 | if (message->body != NULL) |
2792 | 0 | { |
2793 | 0 | gchar *tupled_signature_str; |
2794 | 0 | if (signature == NULL) |
2795 | 0 | { |
2796 | 0 | g_set_error (error, |
2797 | 0 | G_IO_ERROR, |
2798 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2799 | 0 | _("Message body has signature “%s” but there is no signature header"), |
2800 | 0 | g_variant_get_type_string (message->body)); |
2801 | 0 | goto out; |
2802 | 0 | } |
2803 | 0 | tupled_signature_str = g_strdup_printf ("(%s)", signature_str); |
2804 | 0 | if (g_strcmp0 (tupled_signature_str, g_variant_get_type_string (message->body)) != 0) |
2805 | 0 | { |
2806 | 0 | g_set_error (error, |
2807 | 0 | G_IO_ERROR, |
2808 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2809 | 0 | _("Message body has type signature “%s” but signature in the header field is “%s”"), |
2810 | 0 | g_variant_get_type_string (message->body), tupled_signature_str); |
2811 | 0 | g_free (tupled_signature_str); |
2812 | 0 | goto out; |
2813 | 0 | } |
2814 | 0 | g_free (tupled_signature_str); |
2815 | 0 | if (!append_body_to_blob (message->body, &mbuf, error)) |
2816 | 0 | goto out; |
2817 | 0 | } |
2818 | 0 | else |
2819 | 0 | { |
2820 | 0 | if (signature != NULL && strlen (signature_str) > 0) |
2821 | 0 | { |
2822 | 0 | g_set_error (error, |
2823 | 0 | G_IO_ERROR, |
2824 | 0 | G_IO_ERROR_INVALID_ARGUMENT, |
2825 | 0 | _("Message body is empty but signature in the header field is “(%s)”"), |
2826 | 0 | signature_str); |
2827 | 0 | goto out; |
2828 | 0 | } |
2829 | 0 | } |
2830 | | |
2831 | | /* OK, we're done writing the message - set the body length */ |
2832 | 0 | size = mbuf.valid_len; |
2833 | 0 | body_size = size - body_start_offset; |
2834 | |
|
2835 | 0 | mbuf.pos = body_len_offset; |
2836 | |
|
2837 | 0 | g_memory_buffer_put_uint32 (&mbuf, body_size); |
2838 | |
|
2839 | 0 | *out_size = size; |
2840 | 0 | ret = (guchar *)mbuf.data; |
2841 | |
|
2842 | 0 | out: |
2843 | 0 | if (ret == NULL) |
2844 | 0 | g_free (mbuf.data); |
2845 | |
|
2846 | 0 | return ret; |
2847 | 0 | } |
2848 | | |
2849 | | /* ---------------------------------------------------------------------------------------------------- */ |
2850 | | |
2851 | | static guint32 |
2852 | | get_uint32_header (GDBusMessage *message, |
2853 | | GDBusMessageHeaderField header_field) |
2854 | 0 | { |
2855 | 0 | GVariant *value; |
2856 | 0 | guint32 ret; |
2857 | |
|
2858 | 0 | ret = 0; |
2859 | 0 | value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field)); |
2860 | 0 | if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32)) |
2861 | 0 | ret = g_variant_get_uint32 (value); |
2862 | |
|
2863 | 0 | return ret; |
2864 | 0 | } |
2865 | | |
2866 | | static const gchar * |
2867 | | get_string_header (GDBusMessage *message, |
2868 | | GDBusMessageHeaderField header_field) |
2869 | 0 | { |
2870 | 0 | GVariant *value; |
2871 | 0 | const gchar *ret; |
2872 | |
|
2873 | 0 | ret = NULL; |
2874 | 0 | value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field)); |
2875 | 0 | if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_STRING)) |
2876 | 0 | ret = g_variant_get_string (value, NULL); |
2877 | |
|
2878 | 0 | return ret; |
2879 | 0 | } |
2880 | | |
2881 | | static const gchar * |
2882 | | get_object_path_header (GDBusMessage *message, |
2883 | | GDBusMessageHeaderField header_field) |
2884 | 0 | { |
2885 | 0 | GVariant *value; |
2886 | 0 | const gchar *ret; |
2887 | |
|
2888 | 0 | ret = NULL; |
2889 | 0 | value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field)); |
2890 | 0 | if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_OBJECT_PATH)) |
2891 | 0 | ret = g_variant_get_string (value, NULL); |
2892 | |
|
2893 | 0 | return ret; |
2894 | 0 | } |
2895 | | |
2896 | | static const gchar * |
2897 | | get_signature_header (GDBusMessage *message, |
2898 | | GDBusMessageHeaderField header_field) |
2899 | 0 | { |
2900 | 0 | GVariant *value; |
2901 | 0 | const gchar *ret; |
2902 | |
|
2903 | 0 | ret = NULL; |
2904 | 0 | value = g_hash_table_lookup (message->headers, GUINT_TO_POINTER (header_field)); |
2905 | 0 | if (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_SIGNATURE)) |
2906 | 0 | ret = g_variant_get_string (value, NULL); |
2907 | |
|
2908 | 0 | return ret; |
2909 | 0 | } |
2910 | | |
2911 | | /* ---------------------------------------------------------------------------------------------------- */ |
2912 | | |
2913 | | static void |
2914 | | set_uint32_header (GDBusMessage *message, |
2915 | | GDBusMessageHeaderField header_field, |
2916 | | guint32 value) |
2917 | 0 | { |
2918 | 0 | g_dbus_message_set_header (message, |
2919 | 0 | header_field, |
2920 | 0 | g_variant_new_uint32 (value)); |
2921 | 0 | } |
2922 | | |
2923 | | static void |
2924 | | set_string_header (GDBusMessage *message, |
2925 | | GDBusMessageHeaderField header_field, |
2926 | | const gchar *value) |
2927 | 0 | { |
2928 | 0 | g_dbus_message_set_header (message, |
2929 | 0 | header_field, |
2930 | 0 | value == NULL ? NULL : g_variant_new_string (value)); |
2931 | 0 | } |
2932 | | |
2933 | | static void |
2934 | | set_object_path_header (GDBusMessage *message, |
2935 | | GDBusMessageHeaderField header_field, |
2936 | | const gchar *value) |
2937 | 0 | { |
2938 | 0 | g_dbus_message_set_header (message, |
2939 | 0 | header_field, |
2940 | 0 | value == NULL ? NULL : g_variant_new_object_path (value)); |
2941 | 0 | } |
2942 | | |
2943 | | static void |
2944 | | set_signature_header (GDBusMessage *message, |
2945 | | GDBusMessageHeaderField header_field, |
2946 | | const gchar *value) |
2947 | 0 | { |
2948 | 0 | g_dbus_message_set_header (message, |
2949 | 0 | header_field, |
2950 | 0 | value == NULL ? NULL : g_variant_new_signature (value)); |
2951 | 0 | } |
2952 | | |
2953 | | /* ---------------------------------------------------------------------------------------------------- */ |
2954 | | |
2955 | | /** |
2956 | | * g_dbus_message_get_reply_serial: |
2957 | | * @message: A #GDBusMessage. |
2958 | | * |
2959 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field. |
2960 | | * |
2961 | | * Returns: The value. |
2962 | | * |
2963 | | * Since: 2.26 |
2964 | | */ |
2965 | | guint32 |
2966 | | g_dbus_message_get_reply_serial (GDBusMessage *message) |
2967 | 0 | { |
2968 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0); |
2969 | 0 | return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL); |
2970 | 0 | } |
2971 | | |
2972 | | /** |
2973 | | * g_dbus_message_set_reply_serial: |
2974 | | * @message: A #GDBusMessage. |
2975 | | * @value: The value to set. |
2976 | | * |
2977 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL header field. |
2978 | | * |
2979 | | * Since: 2.26 |
2980 | | */ |
2981 | | void |
2982 | | g_dbus_message_set_reply_serial (GDBusMessage *message, |
2983 | | guint32 value) |
2984 | 0 | { |
2985 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
2986 | 0 | set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_REPLY_SERIAL, value); |
2987 | 0 | } |
2988 | | |
2989 | | /* ---------------------------------------------------------------------------------------------------- */ |
2990 | | |
2991 | | /** |
2992 | | * g_dbus_message_get_interface: |
2993 | | * @message: A #GDBusMessage. |
2994 | | * |
2995 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field. |
2996 | | * |
2997 | | * Returns: (nullable): The value. |
2998 | | * |
2999 | | * Since: 2.26 |
3000 | | */ |
3001 | | const gchar * |
3002 | | g_dbus_message_get_interface (GDBusMessage *message) |
3003 | 0 | { |
3004 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3005 | 0 | return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE); |
3006 | 0 | } |
3007 | | |
3008 | | /** |
3009 | | * g_dbus_message_set_interface: |
3010 | | * @message: A #GDBusMessage. |
3011 | | * @value: (nullable): The value to set. |
3012 | | * |
3013 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE header field. |
3014 | | * |
3015 | | * Since: 2.26 |
3016 | | */ |
3017 | | void |
3018 | | g_dbus_message_set_interface (GDBusMessage *message, |
3019 | | const gchar *value) |
3020 | 0 | { |
3021 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3022 | 0 | g_return_if_fail (value == NULL || g_dbus_is_interface_name (value)); |
3023 | 0 | set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_INTERFACE, value); |
3024 | 0 | } |
3025 | | |
3026 | | /* ---------------------------------------------------------------------------------------------------- */ |
3027 | | |
3028 | | /** |
3029 | | * g_dbus_message_get_member: |
3030 | | * @message: A #GDBusMessage. |
3031 | | * |
3032 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field. |
3033 | | * |
3034 | | * Returns: (nullable): The value. |
3035 | | * |
3036 | | * Since: 2.26 |
3037 | | */ |
3038 | | const gchar * |
3039 | | g_dbus_message_get_member (GDBusMessage *message) |
3040 | 0 | { |
3041 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3042 | 0 | return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER); |
3043 | 0 | } |
3044 | | |
3045 | | /** |
3046 | | * g_dbus_message_set_member: |
3047 | | * @message: A #GDBusMessage. |
3048 | | * @value: (nullable): The value to set. |
3049 | | * |
3050 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_MEMBER header field. |
3051 | | * |
3052 | | * Since: 2.26 |
3053 | | */ |
3054 | | void |
3055 | | g_dbus_message_set_member (GDBusMessage *message, |
3056 | | const gchar *value) |
3057 | 0 | { |
3058 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3059 | 0 | g_return_if_fail (value == NULL || g_dbus_is_member_name (value)); |
3060 | 0 | set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_MEMBER, value); |
3061 | 0 | } |
3062 | | |
3063 | | /* ---------------------------------------------------------------------------------------------------- */ |
3064 | | |
3065 | | /** |
3066 | | * g_dbus_message_get_path: |
3067 | | * @message: A #GDBusMessage. |
3068 | | * |
3069 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field. |
3070 | | * |
3071 | | * Returns: (nullable): The value. |
3072 | | * |
3073 | | * Since: 2.26 |
3074 | | */ |
3075 | | const gchar * |
3076 | | g_dbus_message_get_path (GDBusMessage *message) |
3077 | 0 | { |
3078 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3079 | 0 | return get_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH); |
3080 | 0 | } |
3081 | | |
3082 | | /** |
3083 | | * g_dbus_message_set_path: |
3084 | | * @message: A #GDBusMessage. |
3085 | | * @value: (nullable): The value to set. |
3086 | | * |
3087 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_PATH header field. |
3088 | | * |
3089 | | * Since: 2.26 |
3090 | | */ |
3091 | | void |
3092 | | g_dbus_message_set_path (GDBusMessage *message, |
3093 | | const gchar *value) |
3094 | 0 | { |
3095 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3096 | 0 | g_return_if_fail (value == NULL || g_variant_is_object_path (value)); |
3097 | 0 | set_object_path_header (message, G_DBUS_MESSAGE_HEADER_FIELD_PATH, value); |
3098 | 0 | } |
3099 | | |
3100 | | /* ---------------------------------------------------------------------------------------------------- */ |
3101 | | |
3102 | | /** |
3103 | | * g_dbus_message_get_sender: |
3104 | | * @message: A #GDBusMessage. |
3105 | | * |
3106 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field. |
3107 | | * |
3108 | | * Returns: (nullable): The value. |
3109 | | * |
3110 | | * Since: 2.26 |
3111 | | */ |
3112 | | const gchar * |
3113 | | g_dbus_message_get_sender (GDBusMessage *message) |
3114 | 0 | { |
3115 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3116 | 0 | return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER); |
3117 | 0 | } |
3118 | | |
3119 | | /** |
3120 | | * g_dbus_message_set_sender: |
3121 | | * @message: A #GDBusMessage. |
3122 | | * @value: (nullable): The value to set. |
3123 | | * |
3124 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SENDER header field. |
3125 | | * |
3126 | | * Since: 2.26 |
3127 | | */ |
3128 | | void |
3129 | | g_dbus_message_set_sender (GDBusMessage *message, |
3130 | | const gchar *value) |
3131 | 0 | { |
3132 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3133 | 0 | g_return_if_fail (value == NULL || g_dbus_is_name (value)); |
3134 | 0 | set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SENDER, value); |
3135 | 0 | } |
3136 | | |
3137 | | /* ---------------------------------------------------------------------------------------------------- */ |
3138 | | |
3139 | | /** |
3140 | | * g_dbus_message_get_destination: |
3141 | | * @message: A #GDBusMessage. |
3142 | | * |
3143 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field. |
3144 | | * |
3145 | | * Returns: (nullable): The value. |
3146 | | * |
3147 | | * Since: 2.26 |
3148 | | */ |
3149 | | const gchar * |
3150 | | g_dbus_message_get_destination (GDBusMessage *message) |
3151 | 0 | { |
3152 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3153 | 0 | return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION); |
3154 | 0 | } |
3155 | | |
3156 | | /** |
3157 | | * g_dbus_message_set_destination: |
3158 | | * @message: A #GDBusMessage. |
3159 | | * @value: (nullable): The value to set. |
3160 | | * |
3161 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION header field. |
3162 | | * |
3163 | | * Since: 2.26 |
3164 | | */ |
3165 | | void |
3166 | | g_dbus_message_set_destination (GDBusMessage *message, |
3167 | | const gchar *value) |
3168 | 0 | { |
3169 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3170 | 0 | g_return_if_fail (value == NULL || g_dbus_is_name (value)); |
3171 | 0 | set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION, value); |
3172 | 0 | } |
3173 | | |
3174 | | /* ---------------------------------------------------------------------------------------------------- */ |
3175 | | |
3176 | | /** |
3177 | | * g_dbus_message_get_error_name: |
3178 | | * @message: A #GDBusMessage. |
3179 | | * |
3180 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field. |
3181 | | * |
3182 | | * Returns: (nullable): The value. |
3183 | | * |
3184 | | * Since: 2.26 |
3185 | | */ |
3186 | | const gchar * |
3187 | | g_dbus_message_get_error_name (GDBusMessage *message) |
3188 | 0 | { |
3189 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3190 | 0 | return get_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME); |
3191 | 0 | } |
3192 | | |
3193 | | /** |
3194 | | * g_dbus_message_set_error_name: |
3195 | | * @message: (nullable): A #GDBusMessage. |
3196 | | * @value: The value to set. |
3197 | | * |
3198 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field. |
3199 | | * |
3200 | | * Since: 2.26 |
3201 | | */ |
3202 | | void |
3203 | | g_dbus_message_set_error_name (GDBusMessage *message, |
3204 | | const gchar *value) |
3205 | 0 | { |
3206 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3207 | 0 | g_return_if_fail (value == NULL || g_dbus_is_interface_name (value)); |
3208 | 0 | set_string_header (message, G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME, value); |
3209 | 0 | } |
3210 | | |
3211 | | /* ---------------------------------------------------------------------------------------------------- */ |
3212 | | |
3213 | | /** |
3214 | | * g_dbus_message_get_signature: |
3215 | | * @message: A #GDBusMessage. |
3216 | | * |
3217 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field. |
3218 | | * |
3219 | | * Returns: The value. |
3220 | | * |
3221 | | * Since: 2.26 |
3222 | | */ |
3223 | | const gchar * |
3224 | | g_dbus_message_get_signature (GDBusMessage *message) |
3225 | 0 | { |
3226 | 0 | const gchar *ret; |
3227 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3228 | 0 | ret = get_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE); |
3229 | 0 | if (ret == NULL) |
3230 | 0 | ret = ""; |
3231 | 0 | return ret; |
3232 | 0 | } |
3233 | | |
3234 | | /** |
3235 | | * g_dbus_message_set_signature: |
3236 | | * @message: A #GDBusMessage. |
3237 | | * @value: (nullable): The value to set. |
3238 | | * |
3239 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE header field. |
3240 | | * |
3241 | | * Since: 2.26 |
3242 | | */ |
3243 | | void |
3244 | | g_dbus_message_set_signature (GDBusMessage *message, |
3245 | | const gchar *value) |
3246 | 0 | { |
3247 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3248 | 0 | g_return_if_fail (value == NULL || g_variant_is_signature (value)); |
3249 | 0 | set_signature_header (message, G_DBUS_MESSAGE_HEADER_FIELD_SIGNATURE, value); |
3250 | 0 | } |
3251 | | |
3252 | | /* ---------------------------------------------------------------------------------------------------- */ |
3253 | | |
3254 | | /** |
3255 | | * g_dbus_message_get_arg0: |
3256 | | * @message: A #GDBusMessage. |
3257 | | * |
3258 | | * Convenience to get the first item in the body of @message. |
3259 | | * |
3260 | | * Returns: (nullable): The string item or %NULL if the first item in the body of |
3261 | | * @message is not a string. |
3262 | | * |
3263 | | * Since: 2.26 |
3264 | | */ |
3265 | | const gchar * |
3266 | | g_dbus_message_get_arg0 (GDBusMessage *message) |
3267 | 0 | { |
3268 | 0 | const gchar *ret; |
3269 | |
|
3270 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3271 | | |
3272 | 0 | ret = NULL; |
3273 | |
|
3274 | 0 | if (message->body != NULL && g_variant_is_of_type (message->body, G_VARIANT_TYPE_TUPLE)) |
3275 | 0 | { |
3276 | 0 | GVariant *item; |
3277 | 0 | item = g_variant_get_child_value (message->body, 0); |
3278 | 0 | if (g_variant_is_of_type (item, G_VARIANT_TYPE_STRING)) |
3279 | 0 | ret = g_variant_get_string (item, NULL); |
3280 | 0 | g_variant_unref (item); |
3281 | 0 | } |
3282 | |
|
3283 | 0 | return ret; |
3284 | 0 | } |
3285 | | |
3286 | | /* ---------------------------------------------------------------------------------------------------- */ |
3287 | | |
3288 | | /** |
3289 | | * g_dbus_message_get_num_unix_fds: |
3290 | | * @message: A #GDBusMessage. |
3291 | | * |
3292 | | * Convenience getter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field. |
3293 | | * |
3294 | | * Returns: The value. |
3295 | | * |
3296 | | * Since: 2.26 |
3297 | | */ |
3298 | | guint32 |
3299 | | g_dbus_message_get_num_unix_fds (GDBusMessage *message) |
3300 | 0 | { |
3301 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), 0); |
3302 | 0 | return get_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS); |
3303 | 0 | } |
3304 | | |
3305 | | /** |
3306 | | * g_dbus_message_set_num_unix_fds: |
3307 | | * @message: A #GDBusMessage. |
3308 | | * @value: The value to set. |
3309 | | * |
3310 | | * Convenience setter for the %G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS header field. |
3311 | | * |
3312 | | * Since: 2.26 |
3313 | | */ |
3314 | | void |
3315 | | g_dbus_message_set_num_unix_fds (GDBusMessage *message, |
3316 | | guint32 value) |
3317 | 0 | { |
3318 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3319 | 0 | set_uint32_header (message, G_DBUS_MESSAGE_HEADER_FIELD_NUM_UNIX_FDS, value); |
3320 | 0 | } |
3321 | | |
3322 | | /* ---------------------------------------------------------------------------------------------------- */ |
3323 | | |
3324 | | /** |
3325 | | * g_dbus_message_to_gerror: |
3326 | | * @message: A #GDBusMessage. |
3327 | | * @error: The #GError to set. |
3328 | | * |
3329 | | * If @message is not of type %G_DBUS_MESSAGE_TYPE_ERROR does |
3330 | | * nothing and returns %FALSE. |
3331 | | * |
3332 | | * Otherwise this method encodes the error in @message as a #GError |
3333 | | * using g_dbus_error_set_dbus_error() using the information in the |
3334 | | * %G_DBUS_MESSAGE_HEADER_FIELD_ERROR_NAME header field of @message as |
3335 | | * well as the first string item in @message's body. |
3336 | | * |
3337 | | * Returns: %TRUE if @error was set, %FALSE otherwise. |
3338 | | * |
3339 | | * Since: 2.26 |
3340 | | */ |
3341 | | gboolean |
3342 | | g_dbus_message_to_gerror (GDBusMessage *message, |
3343 | | GError **error) |
3344 | 0 | { |
3345 | 0 | gboolean ret; |
3346 | 0 | const gchar *error_name; |
3347 | |
|
3348 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE); |
3349 | | |
3350 | 0 | ret = FALSE; |
3351 | 0 | if (message->type != G_DBUS_MESSAGE_TYPE_ERROR) |
3352 | 0 | goto out; |
3353 | | |
3354 | 0 | error_name = g_dbus_message_get_error_name (message); |
3355 | 0 | if (error_name != NULL) |
3356 | 0 | { |
3357 | 0 | GVariant *body; |
3358 | |
|
3359 | 0 | body = g_dbus_message_get_body (message); |
3360 | |
|
3361 | 0 | if (body != NULL && g_variant_is_of_type (body, G_VARIANT_TYPE ("(s)"))) |
3362 | 0 | { |
3363 | 0 | const gchar *error_message; |
3364 | 0 | g_variant_get (body, "(&s)", &error_message); |
3365 | 0 | g_dbus_error_set_dbus_error (error, |
3366 | 0 | error_name, |
3367 | 0 | error_message, |
3368 | 0 | NULL); |
3369 | 0 | } |
3370 | 0 | else |
3371 | 0 | { |
3372 | | /* these two situations are valid, yet pretty rare */ |
3373 | 0 | if (body != NULL) |
3374 | 0 | { |
3375 | 0 | g_dbus_error_set_dbus_error (error, |
3376 | 0 | error_name, |
3377 | 0 | "", |
3378 | 0 | _("Error return with body of type “%s”"), |
3379 | 0 | g_variant_get_type_string (body)); |
3380 | 0 | } |
3381 | 0 | else |
3382 | 0 | { |
3383 | 0 | g_dbus_error_set_dbus_error (error, |
3384 | 0 | error_name, |
3385 | 0 | "", |
3386 | 0 | _("Error return with empty body")); |
3387 | 0 | } |
3388 | 0 | } |
3389 | 0 | } |
3390 | 0 | else |
3391 | 0 | { |
3392 | | /* TODO: this shouldn't happen - should check this at message serialization |
3393 | | * time and disconnect the peer. |
3394 | | */ |
3395 | 0 | g_set_error (error, |
3396 | 0 | G_IO_ERROR, |
3397 | 0 | G_IO_ERROR_FAILED, |
3398 | 0 | "Error return without error-name header!"); |
3399 | 0 | } |
3400 | |
|
3401 | 0 | ret = TRUE; |
3402 | |
|
3403 | 0 | out: |
3404 | 0 | return ret; |
3405 | 0 | } |
3406 | | |
3407 | | /* ---------------------------------------------------------------------------------------------------- */ |
3408 | | |
3409 | | static gchar * |
3410 | | flags_to_string (GType flags_type, guint value) |
3411 | 0 | { |
3412 | 0 | GString *s; |
3413 | 0 | GFlagsClass *klass; |
3414 | 0 | guint n; |
3415 | |
|
3416 | 0 | klass = g_type_class_ref (flags_type); |
3417 | 0 | s = g_string_new (NULL); |
3418 | 0 | for (n = 0; n < 32; n++) |
3419 | 0 | { |
3420 | 0 | if ((value & (1<<n)) != 0) |
3421 | 0 | { |
3422 | 0 | GFlagsValue *flags_value; |
3423 | 0 | flags_value = g_flags_get_first_value (klass, (1<<n)); |
3424 | 0 | if (s->len > 0) |
3425 | 0 | g_string_append_c (s, ','); |
3426 | 0 | if (flags_value != NULL) |
3427 | 0 | g_string_append (s, flags_value->value_nick); |
3428 | 0 | else |
3429 | 0 | g_string_append_printf (s, "unknown (bit %d)", n); |
3430 | 0 | } |
3431 | 0 | } |
3432 | 0 | if (s->len == 0) |
3433 | 0 | g_string_append (s, "none"); |
3434 | 0 | g_type_class_unref (klass); |
3435 | 0 | return g_string_free (s, FALSE); |
3436 | 0 | } |
3437 | | |
3438 | | static gint |
3439 | | _sort_keys_func (gconstpointer a, |
3440 | | gconstpointer b) |
3441 | 0 | { |
3442 | 0 | gint ia; |
3443 | 0 | gint ib; |
3444 | |
|
3445 | 0 | ia = GPOINTER_TO_INT (a); |
3446 | 0 | ib = GPOINTER_TO_INT (b); |
3447 | |
|
3448 | 0 | return ia - ib; |
3449 | 0 | } |
3450 | | |
3451 | | /** |
3452 | | * g_dbus_message_print: |
3453 | | * @message: A #GDBusMessage. |
3454 | | * @indent: Indentation level. |
3455 | | * |
3456 | | * Produces a human-readable multi-line description of @message. |
3457 | | * |
3458 | | * The contents of the description has no ABI guarantees, the contents |
3459 | | * and formatting is subject to change at any time. Typical output |
3460 | | * looks something like this: |
3461 | | * |[ |
3462 | | * Type: method-call |
3463 | | * Flags: none |
3464 | | * Version: 0 |
3465 | | * Serial: 4 |
3466 | | * Headers: |
3467 | | * path -> objectpath '/org/gtk/GDBus/TestObject' |
3468 | | * interface -> 'org.gtk.GDBus.TestInterface' |
3469 | | * member -> 'GimmeStdout' |
3470 | | * destination -> ':1.146' |
3471 | | * Body: () |
3472 | | * UNIX File Descriptors: |
3473 | | * (none) |
3474 | | * ]| |
3475 | | * or |
3476 | | * |[ |
3477 | | * Type: method-return |
3478 | | * Flags: no-reply-expected |
3479 | | * Version: 0 |
3480 | | * Serial: 477 |
3481 | | * Headers: |
3482 | | * reply-serial -> uint32 4 |
3483 | | * destination -> ':1.159' |
3484 | | * sender -> ':1.146' |
3485 | | * num-unix-fds -> uint32 1 |
3486 | | * Body: () |
3487 | | * UNIX File Descriptors: |
3488 | | * fd 12: dev=0:10,mode=020620,ino=5,uid=500,gid=5,rdev=136:2,size=0,atime=1273085037,mtime=1273085851,ctime=1272982635 |
3489 | | * ]| |
3490 | | * |
3491 | | * Returns: A string that should be freed with g_free(). |
3492 | | * |
3493 | | * Since: 2.26 |
3494 | | */ |
3495 | | gchar * |
3496 | | g_dbus_message_print (GDBusMessage *message, |
3497 | | guint indent) |
3498 | 0 | { |
3499 | 0 | GString *str; |
3500 | 0 | gchar *s; |
3501 | 0 | GList *keys; |
3502 | 0 | GList *l; |
3503 | |
|
3504 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3505 | | |
3506 | 0 | str = g_string_new (NULL); |
3507 | |
|
3508 | 0 | s = _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_TYPE, message->type); |
3509 | 0 | g_string_append_printf (str, "%*sType: %s\n", indent, "", s); |
3510 | 0 | g_free (s); |
3511 | 0 | s = flags_to_string (G_TYPE_DBUS_MESSAGE_FLAGS, message->flags); |
3512 | 0 | g_string_append_printf (str, "%*sFlags: %s\n", indent, "", s); |
3513 | 0 | g_free (s); |
3514 | 0 | g_string_append_printf (str, "%*sVersion: %d\n", indent, "", message->major_protocol_version); |
3515 | 0 | g_string_append_printf (str, "%*sSerial: %d\n", indent, "", message->serial); |
3516 | |
|
3517 | 0 | g_string_append_printf (str, "%*sHeaders:\n", indent, ""); |
3518 | 0 | keys = g_hash_table_get_keys (message->headers); |
3519 | 0 | keys = g_list_sort (keys, _sort_keys_func); |
3520 | 0 | if (keys != NULL) |
3521 | 0 | { |
3522 | 0 | for (l = keys; l != NULL; l = l->next) |
3523 | 0 | { |
3524 | 0 | gint key = GPOINTER_TO_INT (l->data); |
3525 | 0 | GVariant *value; |
3526 | 0 | gchar *value_str; |
3527 | |
|
3528 | 0 | value = g_hash_table_lookup (message->headers, l->data); |
3529 | 0 | g_assert (value != NULL); |
3530 | | |
3531 | 0 | s = _g_dbus_enum_to_string (G_TYPE_DBUS_MESSAGE_HEADER_FIELD, key); |
3532 | 0 | value_str = g_variant_print (value, TRUE); |
3533 | 0 | g_string_append_printf (str, "%*s %s -> %s\n", indent, "", s, value_str); |
3534 | 0 | g_free (s); |
3535 | 0 | g_free (value_str); |
3536 | 0 | } |
3537 | 0 | } |
3538 | 0 | else |
3539 | 0 | { |
3540 | 0 | g_string_append_printf (str, "%*s (none)\n", indent, ""); |
3541 | 0 | } |
3542 | 0 | g_list_free (keys); |
3543 | 0 | g_string_append_printf (str, "%*sBody: ", indent, ""); |
3544 | 0 | if (message->body != NULL) |
3545 | 0 | { |
3546 | 0 | g_variant_print_string (message->body, |
3547 | 0 | str, |
3548 | 0 | TRUE); |
3549 | 0 | } |
3550 | 0 | else |
3551 | 0 | { |
3552 | 0 | g_string_append (str, "()"); |
3553 | 0 | } |
3554 | 0 | g_string_append (str, "\n"); |
3555 | 0 | #ifdef G_OS_UNIX |
3556 | 0 | g_string_append_printf (str, "%*sUNIX File Descriptors:\n", indent, ""); |
3557 | 0 | if (message->fd_list != NULL) |
3558 | 0 | { |
3559 | 0 | gint num_fds; |
3560 | 0 | const gint *fds; |
3561 | 0 | gint n; |
3562 | |
|
3563 | 0 | fds = g_unix_fd_list_peek_fds (message->fd_list, &num_fds); |
3564 | 0 | if (num_fds > 0) |
3565 | 0 | { |
3566 | 0 | for (n = 0; n < num_fds; n++) |
3567 | 0 | { |
3568 | 0 | GString *fs; |
3569 | 0 | struct stat statbuf; |
3570 | 0 | fs = g_string_new (NULL); |
3571 | 0 | if (fstat (fds[n], &statbuf) == 0) |
3572 | 0 | { |
3573 | 0 | #ifndef MAJOR_MINOR_NOT_FOUND |
3574 | 0 | g_string_append_printf (fs, "%s" "dev=%d:%d", fs->len > 0 ? "," : "", |
3575 | 0 | (gint) major (statbuf.st_dev), (gint) minor (statbuf.st_dev)); |
3576 | 0 | #endif |
3577 | 0 | g_string_append_printf (fs, "%s" "mode=0%o", fs->len > 0 ? "," : "", |
3578 | 0 | (guint) statbuf.st_mode); |
3579 | 0 | g_string_append_printf (fs, "%s" "ino=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "", |
3580 | 0 | (guint64) statbuf.st_ino); |
3581 | 0 | g_string_append_printf (fs, "%s" "uid=%u", fs->len > 0 ? "," : "", |
3582 | 0 | (guint) statbuf.st_uid); |
3583 | 0 | g_string_append_printf (fs, "%s" "gid=%u", fs->len > 0 ? "," : "", |
3584 | 0 | (guint) statbuf.st_gid); |
3585 | 0 | #ifndef MAJOR_MINOR_NOT_FOUND |
3586 | 0 | g_string_append_printf (fs, "%s" "rdev=%d:%d", fs->len > 0 ? "," : "", |
3587 | 0 | (gint) major (statbuf.st_rdev), (gint) minor (statbuf.st_rdev)); |
3588 | 0 | #endif |
3589 | 0 | g_string_append_printf (fs, "%s" "size=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "", |
3590 | 0 | (guint64) statbuf.st_size); |
3591 | 0 | g_string_append_printf (fs, "%s" "atime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "", |
3592 | 0 | (guint64) statbuf.st_atime); |
3593 | 0 | g_string_append_printf (fs, "%s" "mtime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "", |
3594 | 0 | (guint64) statbuf.st_mtime); |
3595 | 0 | g_string_append_printf (fs, "%s" "ctime=%" G_GUINT64_FORMAT, fs->len > 0 ? "," : "", |
3596 | 0 | (guint64) statbuf.st_ctime); |
3597 | 0 | } |
3598 | 0 | else |
3599 | 0 | { |
3600 | 0 | int errsv = errno; |
3601 | 0 | g_string_append_printf (fs, "(fstat failed: %s)", g_strerror (errsv)); |
3602 | 0 | } |
3603 | 0 | g_string_append_printf (str, "%*s fd %d: %s\n", indent, "", fds[n], fs->str); |
3604 | 0 | g_string_free (fs, TRUE); |
3605 | 0 | } |
3606 | 0 | } |
3607 | 0 | else |
3608 | 0 | { |
3609 | 0 | g_string_append_printf (str, "%*s (empty)\n", indent, ""); |
3610 | 0 | } |
3611 | 0 | } |
3612 | 0 | else |
3613 | 0 | { |
3614 | 0 | g_string_append_printf (str, "%*s (none)\n", indent, ""); |
3615 | 0 | } |
3616 | 0 | #endif |
3617 | |
|
3618 | 0 | return g_string_free (str, FALSE); |
3619 | 0 | } |
3620 | | |
3621 | | /** |
3622 | | * g_dbus_message_get_locked: |
3623 | | * @message: A #GDBusMessage. |
3624 | | * |
3625 | | * Checks whether @message is locked. To monitor changes to this |
3626 | | * value, conncet to the #GObject::notify signal to listen for changes |
3627 | | * on the #GDBusMessage:locked property. |
3628 | | * |
3629 | | * Returns: %TRUE if @message is locked, %FALSE otherwise. |
3630 | | * |
3631 | | * Since: 2.26 |
3632 | | */ |
3633 | | gboolean |
3634 | | g_dbus_message_get_locked (GDBusMessage *message) |
3635 | 0 | { |
3636 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE); |
3637 | 0 | return message->locked; |
3638 | 0 | } |
3639 | | |
3640 | | /** |
3641 | | * g_dbus_message_lock: |
3642 | | * @message: A #GDBusMessage. |
3643 | | * |
3644 | | * If @message is locked, does nothing. Otherwise locks the message. |
3645 | | * |
3646 | | * Since: 2.26 |
3647 | | */ |
3648 | | void |
3649 | | g_dbus_message_lock (GDBusMessage *message) |
3650 | 0 | { |
3651 | 0 | g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
3652 | | |
3653 | 0 | if (message->locked) |
3654 | 0 | goto out; |
3655 | | |
3656 | 0 | message->locked = TRUE; |
3657 | 0 | g_object_notify (G_OBJECT (message), "locked"); |
3658 | |
|
3659 | 0 | out: |
3660 | 0 | ; |
3661 | 0 | } |
3662 | | |
3663 | | /** |
3664 | | * g_dbus_message_copy: |
3665 | | * @message: A #GDBusMessage. |
3666 | | * @error: Return location for error or %NULL. |
3667 | | * |
3668 | | * Copies @message. The copy is a deep copy and the returned |
3669 | | * #GDBusMessage is completely identical except that it is guaranteed |
3670 | | * to not be locked. |
3671 | | * |
3672 | | * This operation can fail if e.g. @message contains file descriptors |
3673 | | * and the per-process or system-wide open files limit is reached. |
3674 | | * |
3675 | | * Returns: (transfer full): A new #GDBusMessage or %NULL if @error is set. |
3676 | | * Free with g_object_unref(). |
3677 | | * |
3678 | | * Since: 2.26 |
3679 | | */ |
3680 | | GDBusMessage * |
3681 | | g_dbus_message_copy (GDBusMessage *message, |
3682 | | GError **error) |
3683 | 0 | { |
3684 | 0 | GDBusMessage *ret; |
3685 | 0 | GHashTableIter iter; |
3686 | 0 | gpointer header_key; |
3687 | 0 | GVariant *header_value; |
3688 | |
|
3689 | 0 | g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
3690 | 0 | g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
3691 | | |
3692 | 0 | ret = g_dbus_message_new (); |
3693 | 0 | ret->type = message->type; |
3694 | 0 | ret->flags = message->flags; |
3695 | 0 | ret->byte_order = message->byte_order; |
3696 | 0 | ret->major_protocol_version = message->major_protocol_version; |
3697 | 0 | ret->serial = message->serial; |
3698 | |
|
3699 | 0 | #ifdef G_OS_UNIX |
3700 | 0 | if (message->fd_list != NULL) |
3701 | 0 | { |
3702 | 0 | gint n; |
3703 | 0 | gint num_fds; |
3704 | 0 | const gint *fds; |
3705 | |
|
3706 | 0 | ret->fd_list = g_unix_fd_list_new (); |
3707 | 0 | fds = g_unix_fd_list_peek_fds (message->fd_list, &num_fds); |
3708 | 0 | for (n = 0; n < num_fds; n++) |
3709 | 0 | { |
3710 | 0 | if (g_unix_fd_list_append (ret->fd_list, |
3711 | 0 | fds[n], |
3712 | 0 | error) == -1) |
3713 | 0 | { |
3714 | 0 | g_object_unref (ret); |
3715 | 0 | ret = NULL; |
3716 | 0 | goto out; |
3717 | 0 | } |
3718 | 0 | } |
3719 | 0 | } |
3720 | 0 | #endif |
3721 | | |
3722 | | /* see https://bugzilla.gnome.org/show_bug.cgi?id=624546#c8 for why it's fine |
3723 | | * to just ref (as opposed to deep-copying) the GVariant instances |
3724 | | */ |
3725 | 0 | ret->body = message->body != NULL ? g_variant_ref (message->body) : NULL; |
3726 | 0 | g_hash_table_iter_init (&iter, message->headers); |
3727 | 0 | while (g_hash_table_iter_next (&iter, &header_key, (gpointer) &header_value)) |
3728 | 0 | g_hash_table_insert (ret->headers, header_key, g_variant_ref (header_value)); |
3729 | |
|
3730 | 0 | #ifdef G_OS_UNIX |
3731 | 0 | out: |
3732 | 0 | #endif |
3733 | 0 | return ret; |
3734 | 0 | } |