Rename dissector_add_handle() to dissector_add_for_decode_as().
[metze/wireshark/wip.git] / epan / dissectors / packet-dbus.c
1 /* packet-dbus.c
2  * Routines for D-Bus dissection
3  * Copyright 2012, Jakub Zawadzki <darkjames-ws@darkjames.pl>
4  *
5  * Protocol specification available at http://dbus.freedesktop.org/doc/dbus-specification.html
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #define NEW_PROTO_TREE_API
27
28 #include "config.h"
29
30 #include <epan/packet.h>
31 #include <wiretap/wtap.h>
32 #include <epan/expert.h>
33 #include <epan/dissectors/packet-tcp.h>
34
35 void proto_register_dbus(void);
36 void proto_reg_handoff_dbus(void);
37
38 static gboolean dbus_desegment = TRUE;
39
40 static dissector_handle_t dbus_handle;
41 static dissector_handle_t dbus_handle_tcp;
42
43 #define DBUS_MESSAGE_TYPE_INVALID 0
44 #define DBUS_MESSAGE_TYPE_METHOD_CALL 1
45 #define DBUS_MESSAGE_TYPE_METHOD_RETURN 2
46 #define DBUS_MESSAGE_TYPE_ERROR 3
47 #define DBUS_MESSAGE_TYPE_SIGNAL 4
48
49 static const value_string message_type_vals[] = {
50         { DBUS_MESSAGE_TYPE_INVALID, "Invalid" },
51         { DBUS_MESSAGE_TYPE_METHOD_CALL, "Method call" },
52         { DBUS_MESSAGE_TYPE_METHOD_RETURN, "Method reply" },
53         { DBUS_MESSAGE_TYPE_ERROR, "Error reply" },
54         { DBUS_MESSAGE_TYPE_SIGNAL, "Signal emission" },
55         { 0, NULL }
56 };
57
58 #define DBUS_HEADER_FIELD_INVALID        0
59 #define DBUS_HEADER_FIELD_PATH           1
60 #define DBUS_HEADER_FIELD_INTERFACE      2
61 #define DBUS_HEADER_FIELD_MEMBER         3
62 #define DBUS_HEADER_FIELD_ERROR_NAME     4
63 #define DBUS_HEADER_FIELD_REPLY_SERIAL   5
64 #define DBUS_HEADER_FIELD_DESTINATION    6
65 #define DBUS_HEADER_FIELD_SENDER         7
66 #define DBUS_HEADER_FIELD_SIGNATURE      8
67 #define DBUS_HEADER_FIELD_UNIX_FDS       9
68
69 static const value_string field_code_vals[] = {
70         { DBUS_HEADER_FIELD_INVALID, "INVALID" },
71         { DBUS_HEADER_FIELD_PATH, "PATH" },
72         { DBUS_HEADER_FIELD_INTERFACE, "INTERFACE" },
73         { DBUS_HEADER_FIELD_MEMBER, "MEMBER" },
74         { DBUS_HEADER_FIELD_ERROR_NAME, "ERROR_NAME" },
75         { DBUS_HEADER_FIELD_REPLY_SERIAL, "REPLY_SERIAL" },
76         { DBUS_HEADER_FIELD_DESTINATION, "DESTINATION" },
77         { DBUS_HEADER_FIELD_SENDER, "SENDER" },
78         { DBUS_HEADER_FIELD_SIGNATURE, "SIGNATURE" },
79         { DBUS_HEADER_FIELD_UNIX_FDS, "UNIX_FDS" },
80         { 0, NULL }
81 };
82
83 static header_field_info *hfi_dbus = NULL;
84
85 #define DBUS_HFI_INIT HFI_INIT(proto_dbus)
86
87 /* XXX, FT_NONE -> FT_BYTES? */
88
89 /* Header */
90 static header_field_info hfi_dbus_hdr DBUS_HFI_INIT =
91         { "Header", "dbus.header", FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL };
92
93 static header_field_info hfi_dbus_hdr_endianness DBUS_HFI_INIT =
94         { "Endianness Flag", "dbus.endianness", FT_STRING, BASE_NONE, NULL, 0x00, NULL, HFILL };
95
96 static header_field_info hfi_dbus_hdr_type DBUS_HFI_INIT =
97         { "Message Type", "dbus.type", FT_UINT8, BASE_DEC, VALS(message_type_vals), 0x00, NULL, HFILL };
98
99 static header_field_info hfi_dbus_hdr_flags DBUS_HFI_INIT =
100         { "Message Flags", "dbus.flags", FT_UINT8, BASE_HEX, NULL, 0x00, NULL, HFILL };
101
102 static header_field_info hfi_dbus_hdr_version DBUS_HFI_INIT =
103         { "Protocol Version", "dbus.version", FT_UINT8, BASE_DEC, NULL, 0x00, NULL, HFILL };
104
105 static header_field_info hfi_dbus_hdr_body_length DBUS_HFI_INIT =
106         { "Message body Length", "dbus.length", FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL };
107
108 static header_field_info hfi_dbus_hdr_serial DBUS_HFI_INIT =
109         { "Message Serial (cookie)", "dbus.serial", FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL };
110
111 static header_field_info hfi_dbus_hdr_fields_length DBUS_HFI_INIT =
112         { "Header fields Length", "dbus.fields_length", FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL };
113
114 /* Header field */
115 static header_field_info hfi_dbus_hdr_field DBUS_HFI_INIT =
116         { "Header Field", "dbus.field", FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL };
117
118 static header_field_info hfi_dbus_hdr_field_code DBUS_HFI_INIT =
119         { "Field code", "dbus.field.code", FT_UINT8, BASE_DEC, VALS(field_code_vals), 0x00, NULL, HFILL };
120
121 static header_field_info hfi_dbus_type_signature DBUS_HFI_INIT =
122         { "Type signature", "dbus.type_signature", FT_STRINGZ, BASE_NONE, NULL, 0x00, NULL, HFILL };
123
124 static header_field_info hfi_dbus_body DBUS_HFI_INIT =
125         { "Body", "dbus.body", FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL };
126
127 /* Values */
128 static header_field_info hfi_dbus_value_bool DBUS_HFI_INIT =
129         { "Value", "dbus.value.bool", FT_BOOLEAN, BASE_NONE, NULL, 0x00, NULL, HFILL };
130
131 static header_field_info hfi_dbus_value_int DBUS_HFI_INIT =
132         { "Value", "dbus.value.int", FT_INT32, BASE_DEC, NULL, 0x00, NULL, HFILL };
133
134 static header_field_info hfi_dbus_value_uint DBUS_HFI_INIT =
135         { "Value", "dbus.value.uint", FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL };
136
137 static header_field_info hfi_dbus_value_str DBUS_HFI_INIT =
138         { "Value", "dbus.value.str", FT_STRING, BASE_NONE, NULL, 0x00, NULL, HFILL };
139
140 static header_field_info hfi_dbus_value_double DBUS_HFI_INIT =
141         { "Value", "dbus.value.double", FT_DOUBLE, BASE_NONE, NULL, 0x00, NULL, HFILL };
142
143
144 static int ett_dbus = -1;
145 static int ett_dbus_hdr = -1;
146 static int ett_dbus_body = -1;
147 static int ett_dbus_field = -1;
148
149 static expert_field ei_dbus_value_bool_invalid = EI_INIT;
150 static expert_field ei_dbus_value_str_invalid = EI_INIT;
151 static expert_field ei_dbus_invalid_object_path = EI_INIT;
152 static expert_field ei_dbus_invalid_signature = EI_INIT;
153
154 typedef struct {
155         packet_info *pinfo;
156
157         guint16 (*get16)(tvbuff_t *, const gint);
158         guint32 (*get32)(tvbuff_t *, const gint);
159         gdouble (*getdouble)(tvbuff_t *, const gint);
160         int enc;
161
162         guint32 body_len;
163         guint32 fields_len;
164         const char *body_sig;
165 } dbus_info_t;
166
167 typedef union {
168         char *str;
169         guint uint;
170
171 } dbus_val_t;
172
173 static gboolean
174 dbus_validate_object_path(const char *path)
175 {
176         /* XXX check */
177         if (*path != '/')
178                 return FALSE;
179
180         do {
181                 path++;
182
183                 if (*path == '/')
184                         return FALSE;
185
186                 while ((*path >= 'A' && *path <= 'Z') || (*path >= 'a' && *path <= 'z') || (*path >= '0' && *path <= '9') || *path == '_')
187                         path++;
188
189                 if (*path == '\0')
190                         return TRUE;
191
192         } while (*path == '/');
193
194         return FALSE;
195 }
196
197 static gboolean
198 dbus_validate_signature(const char *sig _U_)
199 {
200         /* XXX implement */
201         return TRUE;
202 }
203
204 static int
205 dissect_dbus_sig(tvbuff_t *tvb, dbus_info_t *dinfo, proto_tree *tree, int offset, char sig, dbus_val_t *ret)
206 {
207         const int org_offset = offset;
208         proto_item *ti;
209
210         switch (sig) {
211                 case 'y':       /* BYTE */
212                 {
213                         guint8 val;
214
215                         val = tvb_get_guint8(tvb, offset);
216                         offset += 1;
217
218                         proto_tree_add_uint_format(tree, hfi_dbus_value_uint.id, tvb, org_offset, offset - org_offset, val, "BYTE: %u", val);
219                         ret->uint = val;
220                         return offset;
221                 }
222
223                 case 'b':       /* BOOLEAN */
224                 {
225                         guint32 val;
226
227                         val = dinfo->get32(tvb, offset);
228                         offset += 4;
229
230                         ti = proto_tree_add_boolean_format(tree, hfi_dbus_value_bool.id, tvb, org_offset, offset - org_offset, val, "BOOLEAN: %s", val ? "True" : "False");
231                         if (val != 0 && val != 1) {
232                                 expert_add_info_format(dinfo->pinfo, ti, &ei_dbus_value_bool_invalid, "Invalid boolean value (must be 0 or 1 is: %u)", val);
233                                 return -1;
234                         }
235                         ret->uint = val;
236                         return offset;
237                 }
238
239                 case 'n':       /* INT16 */
240                 {
241                         gint16 val;
242
243                         val = (gint16 )dinfo->get16(tvb, offset);
244                         offset += 2;
245
246                         proto_tree_add_uint_format(tree, hfi_dbus_value_int.id, tvb, org_offset, offset - org_offset, val, "INT16: %d", val);
247                         /* XXX ret */
248                         return offset;
249                 }
250
251                 case 'q':       /* UINT16 */
252                 {
253                         guint16 val;
254
255                         val = dinfo->get16(tvb, offset);
256                         offset += 2;
257
258                         proto_tree_add_uint_format(tree, hfi_dbus_value_uint.id, tvb, org_offset, offset - org_offset, val, "UINT16: %u", val);
259                         ret->uint = val;
260                         return offset;
261                 }
262
263                 case 'i':       /* INT32 */
264                 {
265                         gint32 val;
266
267                         val = (gint32) dinfo->get32(tvb, offset);
268                         offset += 4;
269
270                         proto_tree_add_int_format(tree, hfi_dbus_value_int.id, tvb, org_offset, offset - org_offset, val, "INT32: %d", val);
271                         /* XXX ret */
272                         return offset;
273                 }
274
275                 case 'u':       /* UINT32 */
276                 {
277                         guint32 val;
278
279                         val = dinfo->get32(tvb, offset);
280                         offset += 4;
281
282                         proto_tree_add_uint_format(tree, hfi_dbus_value_uint.id, tvb, org_offset, offset - org_offset, val, "UINT32: %u", val);
283                         ret->uint = val;
284                         return offset;
285                 }
286
287                 case 'x':       /* INT64 */
288                 case 't':       /* UINT64 */
289                         return -1;
290
291                 case 'd':       /* DOUBLE */
292                 {
293                         gdouble val;
294
295                         val = dinfo->getdouble(tvb, offset);
296                         offset += 8;
297
298                         proto_tree_add_double_format(tree, hfi_dbus_value_double.id, tvb, org_offset, offset - org_offset, val, "DOUBLE: %." G_STRINGIFY(DBL_DIG) "g", val);
299                         /* XXX ret */
300                         return offset;
301                 }
302
303                 case 's':       /* STRING */
304                 case 'o':       /* OBJECT_PATH */
305                 {
306                         guint32 len;
307                         char *val;
308
309                         len = dinfo->get32(tvb, offset);
310                         offset += 4;
311
312                         val = tvb_get_string_enc(wmem_packet_scope(), tvb, offset, len, ENC_ASCII);
313                         offset += (len + 1 /* NUL-byte */ + 3) & ~3;
314
315                         if (sig == 's') {
316                                 ti = proto_tree_add_string_format(tree, hfi_dbus_value_str.id, tvb, org_offset, offset - org_offset, val, "STRING: %s", val);
317                                 if (!g_utf8_validate(val, -1, NULL)) {
318                                         expert_add_info(dinfo->pinfo, ti, &ei_dbus_value_str_invalid);
319                                         return -1;
320                                 }
321                         } else {
322                                 ti = proto_tree_add_string_format(tree, hfi_dbus_value_str.id, tvb, org_offset, offset - org_offset, val, "OBJECT_PATH: %s", val);
323                                 if (!dbus_validate_object_path(val)) {
324                                         expert_add_info(dinfo->pinfo, ti, &ei_dbus_invalid_object_path);
325                                         return -1;
326                                 }
327                         }
328                         ret->str = val;
329                         return offset;
330                 }
331
332                 case 'g':       /* SIGNATURE */
333                 {
334                         guint8 len;
335                         char *val;
336
337                         len = tvb_get_guint8(tvb, offset);
338                         offset += 1;
339
340                         val = tvb_get_string_enc(wmem_packet_scope(), tvb, offset, len, ENC_ASCII);
341                         offset += (len + 1);
342
343                         ti = proto_tree_add_string_format(tree, hfi_dbus_value_str.id, tvb, org_offset, offset - org_offset, val, "SIGNATURE: %s", val);
344                         if (!dbus_validate_signature(val)) {
345                                 expert_add_info(dinfo->pinfo, ti, &ei_dbus_invalid_signature);
346                                 return -1;
347                         }
348                         ret->str = val;
349                         return offset;
350                 }
351
352                 /* ... */
353         }
354         return -1;
355 }
356
357 static int
358 dissect_dbus_field_signature(tvbuff_t *tvb, dbus_info_t *dinfo, proto_tree *tree, int offset, int field_code)
359 {
360         const int org_offset = offset;
361
362         proto_item *ti;
363         guint sig_len;
364         char *sig;
365
366         sig_len = tvb_get_guint8(tvb, offset);
367         offset += 1;
368
369         /* sig_len = tvb_strsize(tvb, offset); */
370
371         sig = tvb_get_string_enc(wmem_packet_scope(), tvb, offset, sig_len, ENC_ASCII);
372         offset += (sig_len + 1);
373
374         ti = proto_tree_add_string(tree, &hfi_dbus_type_signature, tvb, org_offset, offset - org_offset, sig);
375         if (!dbus_validate_signature(sig)) {
376                 expert_add_info(dinfo->pinfo, ti, &ei_dbus_invalid_signature);
377                 return -1;
378         }
379
380         switch (field_code) {
381                 case DBUS_HEADER_FIELD_REPLY_SERIAL:
382                         if (!strcmp(sig, "u")) {        /* UINT32 */
383                                 dbus_val_t serial_val;
384
385                                 offset = dissect_dbus_sig(tvb, dinfo, tree, offset, 'u', &serial_val);
386                                 if (offset != -1)
387                                         { /* XXX link with sending frame (serial_val.uint) */ }
388                                 return offset;
389                         }
390                         break;
391
392                 case DBUS_HEADER_FIELD_DESTINATION:
393                 case DBUS_HEADER_FIELD_SENDER:
394                         if (!strcmp(sig, "s")) {        /* STRING */
395                                 dbus_val_t addr_val;
396
397                                 offset = dissect_dbus_sig(tvb, dinfo, tree, offset, 's', &addr_val);
398                                 if (offset != -1)
399                                         SET_ADDRESS((field_code == DBUS_HEADER_FIELD_DESTINATION) ? &dinfo->pinfo->dst : &dinfo->pinfo->src,
400                                                     AT_STRINGZ, (int)strlen(addr_val.str)+1, addr_val.str);
401                                 return offset;
402                         }
403                         break;
404
405                 case DBUS_HEADER_FIELD_SIGNATURE:
406                         if (!strcmp(sig, "g")) {        /* SIGNATURE */
407                                 dbus_val_t sig_val;
408
409                                 offset = dissect_dbus_sig(tvb, dinfo, tree, offset, 'g', &sig_val);
410                                 if (offset != -1)
411                                         dinfo->body_sig = sig_val.str;
412                                 return offset;
413                         }
414                         break;
415         }
416
417         while (*sig) {
418                 dbus_val_t val;
419
420                 offset = dissect_dbus_sig(tvb, dinfo, tree, offset, *sig, &val);
421                 if (offset == -1)
422                         return -1;
423                 sig++;
424         }
425         return offset;
426 }
427
428 static int
429 dissect_dbus_hdr_fields(tvbuff_t *tvb, dbus_info_t *dinfo, proto_tree *tree, int offset)
430 {
431         int end_offset;
432
433         end_offset = offset + dinfo->fields_len;
434
435         while (offset < end_offset) {
436                 proto_tree *field_tree;
437                 proto_item *ti;
438
439                 guint8 field_code;
440
441                 ti = proto_tree_add_item(tree, &hfi_dbus_hdr_field, tvb, offset, 0, ENC_NA);
442                 field_tree = proto_item_add_subtree(ti, ett_dbus_field);
443
444                 field_code = tvb_get_guint8(tvb, offset);
445                 proto_tree_add_item(field_tree, &hfi_dbus_hdr_field_code, tvb, offset, 1, dinfo->enc);
446                 proto_item_append_text(ti, ": %s", val_to_str(field_code, field_code_vals, "Unknown: %d"));
447                 offset += 1;
448
449                 offset = dissect_dbus_field_signature(tvb, dinfo, field_tree, offset, field_code);
450                 if (offset == -1)
451                         break;
452
453                 offset = (offset + 7) & ~7;     /* XXX ? */
454
455                 proto_item_set_end(ti, tvb, offset);
456         }
457
458         /* XXX, verify if all required fields are preset */
459
460         if (offset >= end_offset) {
461                 /* XXX expert */
462         }
463
464         return end_offset;
465 }
466
467 static int
468 dissect_dbus_hdr(tvbuff_t *tvb, dbus_info_t *dinfo, proto_tree *tree, int offset)
469 {
470         proto_tree *hdr_tree;
471         proto_item *ti;
472
473         guint8 type;
474
475         ti = proto_tree_add_item(tree, &hfi_dbus_hdr, tvb, offset, 0, ENC_NA);
476         hdr_tree = proto_item_add_subtree(ti, ett_dbus_hdr);
477
478         proto_tree_add_item(hdr_tree, &hfi_dbus_hdr_endianness, tvb, offset, 1, ENC_ASCII | ENC_NA);
479         offset += 1;
480
481         type = tvb_get_guint8(tvb, offset);
482         col_set_str(dinfo->pinfo->cinfo, COL_INFO, val_to_str_const(type, message_type_vals, ""));
483         proto_tree_add_item(hdr_tree, &hfi_dbus_hdr_type, tvb, offset, 1, dinfo->enc);
484         offset += 1;
485
486         proto_tree_add_item(hdr_tree, &hfi_dbus_hdr_flags, tvb, offset, 1, dinfo->enc);
487         offset += 1;
488
489         proto_tree_add_item(hdr_tree, &hfi_dbus_hdr_version, tvb, offset, 1, dinfo->enc);
490         offset += 1;
491
492         dinfo->body_len = dinfo->get32(tvb, offset);
493         proto_tree_add_item(hdr_tree, &hfi_dbus_hdr_body_length, tvb, offset, 4, dinfo->enc);
494         offset += 4;
495
496         proto_tree_add_item(hdr_tree, &hfi_dbus_hdr_serial, tvb, offset, 4, dinfo->enc);
497         offset += 4;
498
499         dinfo->fields_len = dinfo->get32(tvb, offset);
500         proto_tree_add_item(hdr_tree, &hfi_dbus_hdr_fields_length, tvb, offset, 4, dinfo->enc);
501         offset += 4;
502
503         return offset;
504 }
505
506 static int
507 dissect_dbus_body(tvbuff_t *tvb, dbus_info_t *dinfo, proto_tree *tree, int offset)
508 {
509         proto_tree *body_tree;
510         proto_item *ti;
511
512         if (dinfo->body_len && dinfo->body_sig[0]) {
513                 const char *sig = dinfo->body_sig;
514
515                 ti = proto_tree_add_item(tree, &hfi_dbus_body, tvb, offset, 0, ENC_NA);
516                 body_tree = proto_item_add_subtree(ti, ett_dbus_body);
517
518                 while (*sig) {
519                         dbus_val_t val;
520
521                         offset = dissect_dbus_sig(tvb, dinfo, body_tree, offset, *sig, &val);
522                         if (offset == -1)
523                                 return -1;
524                         sig++;
525                 }
526
527                 proto_item_set_end(ti, tvb, offset);
528
529         } else if (dinfo->body_len || dinfo->body_sig[0]) {
530                 /* XXX smth wrong */
531         }
532         return offset;
533 }
534
535 static int
536 dissect_dbus(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
537 {
538         proto_tree *dbus_tree = NULL;
539         dbus_info_t dinfo;
540
541         int offset;
542
543         col_set_str(pinfo->cinfo, COL_PROTOCOL, "D-BUS");
544         col_clear(pinfo->cinfo, COL_INFO);
545
546         memset(&dinfo, 0, sizeof(dinfo));
547         dinfo.pinfo = pinfo;
548         switch (tvb_get_guint8(tvb, 0)) {
549                 case 'l':
550                         dinfo.enc   = ENC_LITTLE_ENDIAN;
551                         dinfo.get16 = tvb_get_letohs;
552                         dinfo.get32 = tvb_get_letohl;
553                         dinfo.getdouble = tvb_get_letohieee_double;
554                         break;
555                 case 'B':
556                         dinfo.enc   = ENC_BIG_ENDIAN;
557                         dinfo.get16 = tvb_get_ntohs;
558                         dinfo.get32 = tvb_get_ntohl;
559                         dinfo.getdouble = tvb_get_ntohieee_double;
560                         break;
561                 default:        /* same as BIG_ENDIAN */
562                         /* XXX we should probably return 0; */
563                         dinfo.enc   = ENC_NA;
564                         dinfo.get16 = tvb_get_ntohs;
565                         dinfo.get32 = tvb_get_ntohl;
566                         dinfo.getdouble = tvb_get_ntohieee_double;
567         }
568
569         if (tree) {
570                 proto_item *ti = proto_tree_add_item(tree, hfi_dbus, tvb, 0, -1, ENC_NA);
571                 dbus_tree = proto_item_add_subtree(ti, ett_dbus);
572         }
573
574         offset = 0;
575         offset = dissect_dbus_hdr(tvb, &dinfo, dbus_tree, offset);
576         offset = dissect_dbus_hdr_fields(tvb, &dinfo, dbus_tree, offset);
577         /* header aligned to 8B */
578         offset = (offset + 7) & ~7;
579
580         if (!dinfo.body_sig)
581                 dinfo.body_sig = "";
582
583         offset = dissect_dbus_body(tvb, &dinfo, dbus_tree, offset);
584
585         return offset;
586 }
587
588 #define DBUS_HEADER_LEN 16
589
590 static guint
591 get_dbus_message_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
592 {
593         guint32 (*get_guint32)(tvbuff_t *, const gint);
594
595         guint32 len_body, len_hdr;
596
597         switch (tvb_get_guint8(tvb, offset)) {
598                 case 'l':
599                         get_guint32 = tvb_get_letohl;
600                         break;
601                 case 'B':
602                 default:
603                         get_guint32 = tvb_get_ntohl;
604                         break;
605         }
606
607         len_hdr = DBUS_HEADER_LEN + get_guint32(tvb, offset + 12);
608         len_hdr = (len_hdr + 7) & ~7;
609         len_body = get_guint32(tvb, offset + 4);
610
611         return len_hdr + len_body;
612 }
613
614 static int
615 dissect_dbus_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
616 {
617         return dissect_dbus(tvb, pinfo, tree, data);
618 }
619
620 static int
621 dissect_dbus_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
622 {
623         tcp_dissect_pdus(tvb, pinfo, tree, dbus_desegment, DBUS_HEADER_LEN, get_dbus_message_len, dissect_dbus_pdu, data);
624         return tvb_length(tvb);
625 }
626
627 void
628 proto_register_dbus(void)
629 {
630 #ifndef HAVE_HFI_SECTION_INIT
631         static header_field_info *hfi[] = {
632         /* Header */
633                 &hfi_dbus_hdr,
634                 &hfi_dbus_hdr_endianness,
635                 &hfi_dbus_hdr_type,
636                 &hfi_dbus_hdr_flags,
637                 &hfi_dbus_hdr_version,
638                 &hfi_dbus_hdr_body_length,
639                 &hfi_dbus_hdr_serial,
640                 &hfi_dbus_hdr_fields_length,
641         /* Header field */
642                 &hfi_dbus_hdr_field,
643                 &hfi_dbus_hdr_field_code,
644                 &hfi_dbus_type_signature,
645                 &hfi_dbus_body,
646         /* Values */
647                 &hfi_dbus_value_bool,
648                 &hfi_dbus_value_int,
649                 &hfi_dbus_value_uint,
650                 &hfi_dbus_value_str,
651                 &hfi_dbus_value_double,
652         };
653 #endif
654
655         static gint *ett[] = {
656                 &ett_dbus,
657                 &ett_dbus_hdr,
658                 &ett_dbus_body,
659                 &ett_dbus_field
660         };
661
662         static ei_register_info ei[] = {
663                 { &ei_dbus_value_bool_invalid, { "dbus.value.bool.invalid", PI_PROTOCOL, PI_WARN, "Invalid boolean value", EXPFILL }},
664                 { &ei_dbus_value_str_invalid, { "dbus.value.str.invalid", PI_PROTOCOL, PI_WARN, "Invalid string (not UTF-8)", EXPFILL }},
665                 { &ei_dbus_invalid_object_path, { "dbus.invalid_object_path", PI_PROTOCOL, PI_WARN, "Invalid object_path", EXPFILL }},
666                 { &ei_dbus_invalid_signature, { "dbus.invalid_signature", PI_PROTOCOL, PI_WARN, "Invalid signature", EXPFILL }},
667         };
668
669         expert_module_t *expert_dbus;
670
671         int proto_dbus;
672
673         proto_dbus = proto_register_protocol("D-Bus", "D-BUS", "dbus");
674         hfi_dbus = proto_registrar_get_nth(proto_dbus);
675
676         proto_register_fields(proto_dbus, hfi, array_length(hfi));
677         proto_register_subtree_array(ett, array_length(ett));
678         expert_dbus = expert_register_protocol(proto_dbus);
679         expert_register_field_array(expert_dbus, ei, array_length(ei));
680
681         dbus_handle = new_create_dissector_handle(dissect_dbus, proto_dbus);
682         dbus_handle_tcp = new_create_dissector_handle(dissect_dbus_tcp, proto_dbus);
683 }
684
685 void
686 proto_reg_handoff_dbus(void)
687 {
688         dissector_add_uint("wtap_encap", WTAP_ENCAP_DBUS, dbus_handle);
689         dissector_add_for_decode_as("tcp.port", dbus_handle_tcp);
690 }
691