aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-rtps-utils.c
blob: c785a2686f8aaf4f531785ba17dfb802b14d9387 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/* packet-rtps-utils.c
 * ~~~~~~~~~~~~~
 *
 * The following file contains helper routines for the RTPS packet dissector
 *
 * (c) 2005-2020 Copyright, Real-Time Innovations, Inc.
 * Real-Time Innovations, Inc.
 * 232 East Java Drive
 * Sunnyvale, CA 94089
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 *                  -------------------------------------
 *
 * The following file is part of the RTPS packet dissector for Wireshark.
 *
 * RTPS protocol was developed by Real-Time Innovations, Inc. as wire
 * protocol for Data Distribution System.
 * Additional information at:
 *
 *   OMG DDS standards: http://portals.omg.org/dds/omg-dds-standard/
 *
 *   Older OMG DDS specification:
 *                             http://www.omg.org/cgi-bin/doc?ptc/2003-07-07
 *
 *   NDDS and RTPS information: http://www.rti.com/resources.html
 *
 */

#include "config.h"

#include <epan/packet.h>
#include "packet-rtps.h"

static wmem_map_t * dissection_infos = NULL;
static wmem_map_t * union_member_mappings = NULL;
static wmem_map_t * mutable_member_mappings = NULL;

#define DISSECTION_INFO_MAX_ELEMENTS    (100)
#define MAX_MEMBER_NAME                 (256)
#define HASHMAP_DISCRIMINATOR_CONSTANT  (-2)

typedef struct _union_member_mapping {
    guint64 union_type_id;
    guint64 member_type_id;
    gint32  discriminator;
    gchar member_name[MAX_MEMBER_NAME];
} union_member_mapping;

typedef struct _mutable_member_mapping {
    gint64 key;
    guint64 struct_type_id;
    guint64 member_type_id;
    guint32 member_id;
    gchar member_name[MAX_MEMBER_NAME];
} mutable_member_mapping;

typedef struct _dissection_element {
    guint64 type_id;
    guint16 flags;
    guint32 member_id;
    gchar member_name[MAX_MEMBER_NAME];
} dissection_element;

typedef struct _dissection_info {
  guint64 type_id;
  gint member_kind;
  guint64 base_type_id;
  guint32 member_length;
  gchar member_name[MAX_MEMBER_NAME];

  RTICdrTypeObjectExtensibility extensibility;

  gint32 bound;
  gint32 num_elements;
  dissection_element elements[DISSECTION_INFO_MAX_ELEMENTS];

} dissection_info;

gint dissect_user_defined(proto_tree *tree, tvbuff_t * tvb, gint offset, guint encoding,
        dissection_info * _info, guint64 type_id, gchar * name,
        RTICdrTypeObjectExtensibility extensibility, gint offset_zero,
        guint16 flags, guint32 element_member_id);

gint dissect_mutable_member(proto_tree *tree , tvbuff_t * tvb, gint offset, guint encoding,
        dissection_info * info, gboolean * is_end) {

    proto_tree * member;
    guint32 member_id, member_length;
    mutable_member_mapping * mapping;
    gint64 key;

    rtps_util_dissect_parameter_header(tvb, &offset, encoding, &member_id, &member_length);
    if ((member_id & PID_LIST_END) == PID_LIST_END){
    /* If this is the end of the list, don't add a tree.
    * If we add more logic here in the future, take into account that
    * offset is incremented by 4 */
        offset += 0;
        *is_end = TRUE;
        return offset;
    }
    if (member_length == 0){
        return offset;
    }
    member = proto_tree_add_subtree_format(tree, tvb, offset, member_length, ett_rtps_dissection_tree,
        NULL, "ID: %d, Length: %d", member_id, member_length);

    {
        if (info->base_type_id > 0) {
            key = (info->base_type_id + info->base_type_id * member_id);
            mapping = (mutable_member_mapping *) wmem_map_lookup(mutable_member_mappings, &(key));
            if (mapping) { /* the library knows how to dissect this */
                proto_item_append_text(member, "(base found 0x%016" G_GINT64_MODIFIER "x)", key);
                dissect_user_defined(tree, tvb, offset, encoding, NULL, mapping->member_type_id,
                    mapping->member_name, EXTENSIBILITY_INVALID, offset, 0, mapping->member_id);
                PROTO_ITEM_SET_HIDDEN(member);
                return offset + member_length;
            } else
                proto_item_append_text(member, "(base not found 0x%016" G_GINT64_MODIFIER "x from 0x%016" G_GINT64_MODIFIER "x)",
                  key, info->base_type_id);
        }
    }

    key = (info->type_id + info->type_id * member_id);
    mapping = (mutable_member_mapping *) wmem_map_lookup(mutable_member_mappings, &(key));
    if (mapping) { /* the library knows how to dissect this */
        proto_item_append_text(member, "(found 0x%016" G_GINT64_MODIFIER "x)", key);
        dissect_user_defined(tree, tvb, offset, encoding, NULL, mapping->member_type_id,
            mapping->member_name, EXTENSIBILITY_INVALID, offset, 0, mapping->member_id);

    } else
        proto_item_append_text(member, "(not found 0x%016" G_GINT64_MODIFIER "x from 0x%016" G_GINT64_MODIFIER "x)",
                  key, info->type_id);
    PROTO_ITEM_SET_HIDDEN(member);
    return offset + member_length;
}

/* this is a recursive function. _info may or may not be NULL depending on the use iteration */
gint dissect_user_defined(proto_tree *tree, tvbuff_t * tvb, gint offset, guint encoding,
        dissection_info * _info, guint64 type_id, gchar * name,
        RTICdrTypeObjectExtensibility extensibility, gint offset_zero,
        guint16 flags, guint32 element_member_id) {

    guint64 member_kind;
    dissection_info * info = NULL;
    guint32 member_id, member_length;

    if (_info)  { /* first call enters here */
      info = _info;
      member_kind = info->member_kind;
    } else {
      info = (dissection_info *) wmem_map_lookup(dissection_infos, &(type_id));
      if (info != NULL) {
        member_kind = info->member_kind;
      } else {
        member_kind = type_id;
      }
    }
    if (info && (flags & MEMBER_OPTIONAL) == MEMBER_OPTIONAL) {
        gint offset_before = offset;
        rtps_util_dissect_parameter_header(tvb, &offset, encoding, &member_id, &member_length);
        offset = offset_before;
        if (element_member_id != 0 && member_id != element_member_id)
            return offset;
    }
    if (extensibility == EXTENSIBILITY_MUTABLE) {
      rtps_util_dissect_parameter_header(tvb, &offset, encoding, &member_id, &member_length);
      offset_zero = offset;
      if ((member_id & PID_LIST_END) == PID_LIST_END){
       /* If this is the end of the list, don't add a tree.
       * If we add more logic here in the future, take into account that
       * offset is incremented by 4 */
          offset += 0;
          return offset;
      }
      if (member_length == 0){
          return offset;
      }
    }
    //proto_item_append_text(tree, "(Before Switch 0x%016" G_GINT64_MODIFIER "x)", type_id);

    switch (member_kind) {
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_BOOLEAN_TYPE: {
            gint length = 1;
            ALIGN_ZERO(offset, length, offset_zero);
            gint16 value =  tvb_get_gint8(tvb, offset);
            proto_tree_add_boolean_format(tree, hf_rtps_dissection_boolean, tvb, offset, length, value,
                "%s: %d", name, value);
            offset += length;
            break;
        }
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_CHAR_8_TYPE:
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_BYTE_TYPE: {
            gint length = 1;
            ALIGN_ZERO(offset, length, offset_zero);
            gint16 value =  tvb_get_gint8(tvb, offset);
            proto_tree_add_uint_format(tree, hf_rtps_dissection_byte, tvb, offset, length, value,
                "%s: %d", name, value);
            offset += length;
            break;
        }
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_INT_16_TYPE: {
            gint length = 2;
            ALIGN_ZERO(offset, length, offset_zero);
            gint16 value =  tvb_get_gint16(tvb, offset, encoding);
            proto_tree_add_int_format(tree, hf_rtps_dissection_int16, tvb, offset, length, value,
                "%s: %d", name, value);
            offset += length;
            break;
        }
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_UINT_16_TYPE: {
            gint length = 2;
            ALIGN_ZERO(offset, length, offset_zero);
            guint16 value =  tvb_get_guint16(tvb, offset, encoding);
            proto_tree_add_uint_format(tree, hf_rtps_dissection_uint16, tvb, offset, length, value,
                "%s: %u", name, value);
            offset += length;
            break;
        }
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_ENUMERATION_TYPE:
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_INT_32_TYPE: {
            gint length = 4;
            ALIGN_ZERO(offset, length, offset_zero);
            gint value =  tvb_get_gint32(tvb, offset, encoding);
            proto_tree_add_int_format(tree, hf_rtps_dissection_int32, tvb, offset, length, value,
                "%s: %d", name, value);
            offset += length;
            break;
        }
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_UINT_32_TYPE: {
            gint length = 4;
            ALIGN_ZERO(offset, length, offset_zero);
            guint value =  tvb_get_guint32(tvb, offset, encoding);
            proto_tree_add_uint_format(tree, hf_rtps_dissection_uint32, tvb, offset, length, value,
                "%s: %u", name, value);
            offset += length;
            break;
        }
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_INT_64_TYPE: {
            gint length = 8;
            ALIGN_ZERO(offset, length, offset_zero);
            gint64 value =  tvb_get_gint64(tvb, offset, encoding);
            proto_tree_add_int64_format(tree, hf_rtps_dissection_int64, tvb, offset, length, value,
                "%s: %"G_GINT64_MODIFIER"d", name, value);
            offset += length;
            break;
        }
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_UINT_64_TYPE: {
            gint length = 8;
            ALIGN_ZERO(offset, length, offset_zero);
            guint64 value =  tvb_get_guint64(tvb, offset, encoding);
            proto_tree_add_uint64_format(tree, hf_rtps_dissection_uint64, tvb, offset, length, value,
                "%s: %"G_GINT64_MODIFIER"u", name, value);
            offset += length;
            break;
        }
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_FLOAT_32_TYPE: {
            gint length = 4;
            ALIGN_ZERO(offset, length, offset_zero);
            gfloat value =  tvb_get_ieee_float(tvb, offset, encoding);
            proto_tree_add_float_format(tree, hf_rtps_dissection_float, tvb, offset, length, value,
                "%s: %.6f", name, value);
            offset += length;
            break;
        }
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_FLOAT_64_TYPE: {
            gint length = 8;
            ALIGN_ZERO(offset, length, offset_zero);
            gdouble value =  tvb_get_ieee_double(tvb, offset, encoding);
            proto_tree_add_double_format(tree, hf_rtps_dissection_double, tvb, offset, length, value,
                "%s: %.6f", name, value);
            offset += length;
            break;
        }
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_FLOAT_128_TYPE: {
            gint length = 16;
            ALIGN_ZERO(offset, length, offset_zero);
            proto_tree_add_item(tree, hf_rtps_dissection_int128, tvb, offset, length, encoding);
            offset += length;
            break;
        }
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_ARRAY_TYPE: {
            gint i;
            proto_tree * aux_tree;
            gint base_offset = offset;

            aux_tree = proto_tree_add_subtree(tree, tvb, offset, -1, ett_rtps_dissection_tree,
                  NULL, name);
            for (i = 0; i < info->bound; i++) {
                gchar temp_buff[MAX_MEMBER_NAME];
                g_snprintf(temp_buff, MAX_MEMBER_NAME, "%s[%u]", name, i);
                offset = dissect_user_defined(aux_tree, tvb, offset, encoding, NULL,
                        info->base_type_id, temp_buff, EXTENSIBILITY_INVALID, offset_zero, 0, 0);
            }
            proto_item_set_len(aux_tree, offset - base_offset);
            break;
        }
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_SEQUENCE_TYPE: {
            guint i;
            proto_tree * aux_tree;
            gint base_offset = offset;

            gint length = 4;
            ALIGN_ZERO(offset, length, offset_zero);
            guint seq_size =  tvb_get_guint32(tvb, offset, encoding);
            aux_tree = proto_tree_add_subtree_format(tree, tvb, offset, -1, ett_rtps_dissection_tree,
                  NULL, "%s (%u elements)", name, seq_size);
            offset += 4;

            for (i = 0; i < seq_size; i++) {
                gchar temp_buff[MAX_MEMBER_NAME];
                g_snprintf(temp_buff, MAX_MEMBER_NAME, "%s[%u]", name, i);
                if (info->base_type_id > 0)
                    offset = dissect_user_defined(aux_tree, tvb, offset, encoding, NULL,
                         info->base_type_id, temp_buff, EXTENSIBILITY_INVALID, offset_zero, 0, 0);
            }
            proto_item_set_len(aux_tree, offset - base_offset);
            break;
        }
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_STRING_TYPE: {
            gchar * string_value = NULL;
            gint length = 4;
            ALIGN_ZERO(offset, length, offset_zero);

            guint string_size =  tvb_get_guint32(tvb, offset, encoding);
            offset += 4;
            //proto_item_append_text(tree, "(String length: %u)", string_size);

            string_value = tvb_get_string_enc(wmem_packet_scope(), tvb, offset, string_size, ENC_ASCII);
            proto_tree_add_string_format(tree, hf_rtps_dissection_string, tvb, offset, string_size,
                string_value, "%s: %s", name, string_value);

            offset += string_size;
            break;
        }
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_ALIAS_TYPE: {
            offset = dissect_user_defined(tree, tvb, offset, encoding, NULL,
                         info->base_type_id, name, EXTENSIBILITY_INVALID, offset_zero, 0, 0);
            break;
        }
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_UNION_TYPE: {
            guint64 key = type_id - 1;
            union_member_mapping * result = (union_member_mapping *)wmem_map_lookup(union_member_mappings, &(key));

            if (result != NULL) {
                switch (result->member_type_id) {
                    case RTI_CDR_TYPE_OBJECT_TYPE_KIND_ENUMERATION_TYPE:
                    case RTI_CDR_TYPE_OBJECT_TYPE_KIND_INT_32_TYPE: {
                        gint value =  tvb_get_gint32(tvb, offset, encoding);
                        offset += 4;
                        key = type_id + value;
                        result = (union_member_mapping *)wmem_map_lookup(union_member_mappings, &(key));
                        if (result != NULL) {
                          proto_item_append_text(tree, " (discriminator = %d, type_id = 0x%016" G_GINT64_MODIFIER "x)",
                               value, result->member_type_id);
                          offset = dissect_user_defined(tree, tvb, offset, encoding, NULL,
                             result->member_type_id, result->member_name, EXTENSIBILITY_INVALID, offset, 0, 0);
                        } else {
                            /* the hashmap uses the type_id to index the objects. substracting -2 here to lookup the discriminator
                               related to the type_id that identifies an union */
                            key = type_id + HASHMAP_DISCRIMINATOR_CONSTANT;
                            result = (union_member_mapping *)wmem_map_lookup(union_member_mappings, &(key));
                            if (result != NULL) {
                            proto_item_append_text(tree, " (discriminator = %d, type_id = 0x%016" G_GINT64_MODIFIER "x)",
                                value, result->member_type_id);
                            offset = dissect_user_defined(tree, tvb, offset, encoding, NULL,
                                result->member_type_id, result->member_name, EXTENSIBILITY_INVALID, offset, 0, 0);
                            }
                        }
                        break;
                    }
                    default:
                        break;
                }
            } else {
              proto_item_append_text(tree, "(NULL 0x%016" G_GINT64_MODIFIER "x)", type_id);
            }
            break;
        }
        case RTI_CDR_TYPE_OBJECT_TYPE_KIND_STRUCTURE_TYPE: {
            gint i;
            proto_tree * aux_tree;

            offset_zero = offset;
            aux_tree = proto_tree_add_subtree(tree, tvb, offset, -1, ett_rtps_dissection_tree,
                  NULL, name);

            if (info->extensibility == EXTENSIBILITY_MUTABLE) {
                gboolean is_end = FALSE;
                while(!is_end)
                    offset = dissect_mutable_member(aux_tree, tvb, offset, encoding, info, &is_end);
                } else {
                if (info->base_type_id > 0) {
                    proto_item_append_text(tree, "(BaseId: 0x%016" G_GINT64_MODIFIER "x)", info->base_type_id);
                    offset = dissect_user_defined(aux_tree, tvb, offset, encoding, NULL,
                            info->base_type_id, info->member_name, EXTENSIBILITY_INVALID,
                            offset, 0, 0);
                }

                for (i = 0; i < info->num_elements && i < DISSECTION_INFO_MAX_ELEMENTS; i++) {
                    if (info->elements[i].type_id > 0)
                            offset = dissect_user_defined(aux_tree, tvb, offset, encoding, NULL,
                                info->elements[i].type_id, info->elements[i].member_name, info->extensibility,
                                offset_zero, info->elements[i].flags, info->elements[i].member_id);
                }
            }
            break;
        }
        default:{
            /* undefined behavior. this should not happen. the following line helps to debug if it happened */
            proto_item_append_text(tree, "(unknown 0x%016" G_GINT64_MODIFIER "x)", member_kind);
            break;
        }
    }

    if (extensibility == EXTENSIBILITY_MUTABLE) {
        offset_zero += member_length;
        return offset_zero;
    } else {
        return offset;
    }
}

/*
 * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */