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