| Wireshark 4.7.3 The Wireshark network protocol analyzer

Loading...
Searching...
No Matches
protobuf-helper.h
Go to the documentation of this file.
1
12
13/* The packet-protobuf dissector needs get information from *.proto files for dissecting
14 * protobuf packet correctly. The information includes:
15 * - The names of MESSAGE, ENUM, FIELD, ENUM_VALUE;
16 * - The data type of FIELD which assuring the value of protobuf field of packet can be dissected correctly.
17 *
18 * At present, we use C Protocol Buffers Language Parser which generated by protobuf_lang_parser.lemon and protobuf_lang_scanner.l.
19 * Because wireshark is mainly implemented in plain ANSI C but the official protobuf library is implemented in C++ language.
20 *
21 * One day, if C++ library is allowed, we can create a protobuf-helper.cpp file, that invoking official protobuf C++ library directly,
22 * to replace protobuf-helper.c. The packet-protobuf.c can keep unchanged.
23 */
24#pragma once
25#include <wsutil/value_string.h>
26
27#ifdef __cplusplus
28 extern "C" {
29#endif /* __cplusplus */
30
31/* Protobuf field type. Must be kept in sync with FieldType of protobuf wire_format_lite.h */
32#define protobuf_field_type_VALUE_STRING_LIST(XXX) \
33 XXX(PROTOBUF_TYPE_NONE, 0, "") \
34 XXX(PROTOBUF_TYPE_DOUBLE, 1, "double") \
35 XXX(PROTOBUF_TYPE_FLOAT, 2, "float") \
36 XXX(PROTOBUF_TYPE_INT64, 3, "int64") \
37 XXX(PROTOBUF_TYPE_UINT64, 4, "uint64") \
38 XXX(PROTOBUF_TYPE_INT32, 5, "int32") \
39 XXX(PROTOBUF_TYPE_FIXED64, 6, "fixed64") \
40 XXX(PROTOBUF_TYPE_FIXED32, 7, "fixed32") \
41 XXX(PROTOBUF_TYPE_BOOL, 8, "bool") \
42 XXX(PROTOBUF_TYPE_STRING, 9, "string") \
43 XXX(PROTOBUF_TYPE_GROUP, 10, "group") \
44 XXX(PROTOBUF_TYPE_MESSAGE, 11, "message") \
45 XXX(PROTOBUF_TYPE_BYTES, 12, "bytes") \
46 XXX(PROTOBUF_TYPE_UINT32, 13, "uint32") \
47 XXX(PROTOBUF_TYPE_ENUM, 14, "enum") \
48 XXX(PROTOBUF_TYPE_SFIXED32, 15, "sfixed32") \
49 XXX(PROTOBUF_TYPE_SFIXED64, 16, "sfixed64") \
50 XXX(PROTOBUF_TYPE_SINT32, 17, "sint32") \
51 XXX(PROTOBUF_TYPE_SINT64, 18, "sint64")
52
53#define PROTOBUF_MAX_FIELD_TYPE 18
54
56 VALUE_STRING_ENUM(protobuf_field_type);
57
59 VALUE_STRING_ARRAY_GLOBAL_DCL(protobuf_field_type);
60
61/* like google::protobuf::DescriptorPool of protobuf cpp library */
62 typedef struct PbwDescriptorPool PbwDescriptorPool;
63/* like google::protobuf::MethodDescriptor of protobuf cpp library */
64 typedef struct PbwMethodDescriptor PbwMethodDescriptor;
65/* like google::protobuf::Descriptor of protobuf cpp library */
66 typedef struct PbwDescriptor PbwDescriptor;
67/* like google::protobuf::FieldDescriptor of protobuf cpp library */
68 typedef struct PbwFieldDescriptor PbwFieldDescriptor;
69/* like google::protobuf::EnumDescriptor of protobuf cpp library */
70 typedef struct PbwEnumDescriptor PbwEnumDescriptor;
71/* like google::protobuf::EnumValueDescriptor of protobuf cpp library */
72 typedef struct PbwEnumValueDescriptor PbwEnumValueDescriptor;
73
74 typedef void(*pbw_report_error_cb_t)(const char *msg_format, ...);
75
81 void
82 pbw_reinit_DescriptorPool(PbwDescriptorPool** pool, const char** directories, pbw_report_error_cb_t error_cb);
83
84/* load a proto file, return 0 if success */
92 int
93 pbw_load_proto_file(PbwDescriptorPool* pool, const char* filename);
94
95/* like DescriptorPool::FindMethodByName */
103 const PbwMethodDescriptor*
104 pbw_DescriptorPool_FindMethodByName(const PbwDescriptorPool* pool, const char* name);
105
106/* like MethodDescriptor::name() */
113 const char*
114 pbw_MethodDescriptor_name(const PbwMethodDescriptor* method);
115
116/* like MethodDescriptor::full_name() */
123 const char*
124 pbw_MethodDescriptor_full_name(const PbwMethodDescriptor* method);
125
126/* like MethodDescriptor::input_type() */
133 const PbwDescriptor*
134 pbw_MethodDescriptor_input_type(const PbwMethodDescriptor* method);
135
136/* like MethodDescriptor::output_type() */
143 const PbwDescriptor*
144 pbw_MethodDescriptor_output_type(const PbwMethodDescriptor* method);
145
146/* like DescriptorPool::FindMessageTypeByName() */
154 const PbwDescriptor*
155 pbw_DescriptorPool_FindMessageTypeByName(const PbwDescriptorPool* pool, const char* name);
156
157/* like Descriptor::name() */
164 const char*
165 pbw_Descriptor_name(const PbwDescriptor* message);
166
167/* like Descriptor::full_name() */
174 const char*
175 pbw_Descriptor_full_name(const PbwDescriptor* message);
176
177/* like Descriptor::field_count() */
184 int
185 pbw_Descriptor_field_count(const PbwDescriptor* message);
186
187/* like Descriptor::field() */
195 const PbwFieldDescriptor*
196 pbw_Descriptor_field(const PbwDescriptor* message, int field_index);
197
198/* like Descriptor::FindFieldByNumber() */
206 const PbwFieldDescriptor*
207 pbw_Descriptor_FindFieldByNumber(const PbwDescriptor* message, int number);
208
209/* like Descriptor::FindFieldByName() */
217 const PbwFieldDescriptor*
218 pbw_Descriptor_FindFieldByName(const PbwDescriptor* message, const char* name);
219
220/* like FieldDescriptor::full_name() */
227 const char*
228 pbw_FieldDescriptor_full_name(const PbwFieldDescriptor* field);
229
230/* like FieldDescriptor::name() */
237 const char*
238 pbw_FieldDescriptor_name(const PbwFieldDescriptor* field);
239
240/* like FieldDescriptor::number() */
247 int
248 pbw_FieldDescriptor_number(const PbwFieldDescriptor* field);
249
250/* like FieldDescriptor::type() */
257 int
258 pbw_FieldDescriptor_type(const PbwFieldDescriptor* field);
259
260/* like FieldDescriptor::is_repeated() */
267 int
268 pbw_FieldDescriptor_is_repeated(const PbwFieldDescriptor* field);
269
270/* like FieldDescriptor::is_packed() */
277 int
278 pbw_FieldDescriptor_is_packed(const PbwFieldDescriptor* field);
279
280/* like FieldDescriptor::typeName() */
288 const char*
289 pbw_FieldDescriptor_typeName(wmem_allocator_t* scope, int field_type);
290
291/* like FieldDescriptor::message_type() */
298 const PbwDescriptor*
299 pbw_FieldDescriptor_message_type(const PbwFieldDescriptor* field);
300
301/* like FieldDescriptor::enum_type() */
308 const PbwEnumDescriptor*
309 pbw_FieldDescriptor_enum_type(const PbwFieldDescriptor* field);
310
311/* like FieldDescriptor::is_required() */
318 bool
319 pbw_FieldDescriptor_is_required(const PbwFieldDescriptor* field);
320
321/* like FieldDescriptor::has_default_value().
322 * Does this field have an explicitly-declared default value? */
329 bool
330 pbw_FieldDescriptor_has_default_value(const PbwFieldDescriptor* field);
331
332/* like FieldDescriptor::default_value_int32() */
339int32_t
340 pbw_FieldDescriptor_default_value_int32(const PbwFieldDescriptor* field);
341
342/* like FieldDescriptor::default_value_int64() */
349int64_t
350 pbw_FieldDescriptor_default_value_int64(const PbwFieldDescriptor* field);
351
352/* like FieldDescriptor::default_value_uint32() */
359uint32_t
360 pbw_FieldDescriptor_default_value_uint32(const PbwFieldDescriptor* field);
361
362/* like FieldDescriptor::default_value_uint64() */
369uint64_t
370 pbw_FieldDescriptor_default_value_uint64(const PbwFieldDescriptor* field);
371
372/* like FieldDescriptor::default_value_float() */
379 float
380 pbw_FieldDescriptor_default_value_float(const PbwFieldDescriptor* field);
381
382/* like FieldDescriptor::default_value_double() */
389 double
390 pbw_FieldDescriptor_default_value_double(const PbwFieldDescriptor* field);
391
392/* like FieldDescriptor::default_value_bool() */
399 bool
400 pbw_FieldDescriptor_default_value_bool(const PbwFieldDescriptor* field);
401
402/* like FieldDescriptor::default_value_string() */
410 const char*
411 pbw_FieldDescriptor_default_value_string(const PbwFieldDescriptor* field, int* size);
412
413/* like FieldDescriptor::default_value_enum() */
420 const PbwEnumValueDescriptor*
421 pbw_FieldDescriptor_default_value_enum(const PbwFieldDescriptor* field);
422
423/* like EnumDescriptor::name() */
430 const char*
431 pbw_EnumDescriptor_name(const PbwEnumDescriptor* anEnum);
432
433/* like EnumDescriptor::full_name() */
440 const char*
441 pbw_EnumDescriptor_full_name(const PbwEnumDescriptor* anEnum);
442
443/* like EnumDescriptor::value_count() */
450 int
451 pbw_EnumDescriptor_value_count(const PbwEnumDescriptor* anEnum);
452
453/* like EnumDescriptor::value() */
461 const PbwEnumValueDescriptor*
462 pbw_EnumDescriptor_value(const PbwEnumDescriptor* anEnum, int value_index);
463
464/* like EnumDescriptor::FindValueByNumber() */
472 const PbwEnumValueDescriptor*
473 pbw_EnumDescriptor_FindValueByNumber(const PbwEnumDescriptor* anEnum, int number);
474
475/* like EnumDescriptor::FindValueByName() */
483 const PbwEnumValueDescriptor*
484 pbw_EnumDescriptor_FindValueByName(const PbwEnumDescriptor* anEnum, const char* name);
485
486/* like EnumValueDescriptor::name() */
493 const char*
494 pbw_EnumValueDescriptor_name(const PbwEnumValueDescriptor* enumValue);
495
496/* like EnumValueDescriptor::full_name() */
503 const char*
504 pbw_EnumValueDescriptor_full_name(const PbwEnumValueDescriptor* enumValue);
505
506/* like EnumValueDescriptor::number() */
513 int
514 pbw_EnumValueDescriptor_number(const PbwEnumValueDescriptor* enumValue);
515
516/* visit all messages of this pool */
524 void
525 pbw_foreach_message(const PbwDescriptorPool* pool, void (*cb)(const PbwDescriptor* message, void* userdata), void* userdata);
526
527#ifdef __cplusplus
528}
529#endif /* __cplusplus */
530
531/*
532 * Editor modelines - https://www.wireshark.org/tools/modelines.html
533 *
534 * Local variables:
535 * c-basic-offset: 4
536 * tab-width: 8
537 * indent-tabs-mode: nil
538 * End:
539 *
540 * vi: set shiftwidth=4 tabstop=8 expandtab:
541 * :indentSize=4:tabSize=8:noTabs=true:
542 */
wmem_allocator_t
struct _wmem_allocator_t wmem_allocator_t
Definition wmem_core.h:44
pbw_EnumValueDescriptor_full_name
const char * pbw_EnumValueDescriptor_full_name(const PbwEnumValueDescriptor *enumValue)
Get the full name of an enum value descriptor.
Definition protobuf-helper.c:284
pbw_FieldDescriptor_number
int pbw_FieldDescriptor_number(const PbwFieldDescriptor *field)
Retrieves the number associated with a Protocol Buffers field descriptor.
Definition protobuf-helper.c:134
pbw_Descriptor_field_count
int pbw_Descriptor_field_count(const PbwDescriptor *message)
Get the count of fields in a Protocol Buffers descriptor.
Definition protobuf-helper.c:98
pbw_EnumDescriptor_name
const char * pbw_EnumDescriptor_name(const PbwEnumDescriptor *anEnum)
Get the name of an enum descriptor.
Definition protobuf-helper.c:242
pbw_DescriptorPool_FindMethodByName
const PbwMethodDescriptor * pbw_DescriptorPool_FindMethodByName(const PbwDescriptorPool *pool, const char *name)
Finds a method descriptor by name in a descriptor pool.
Definition protobuf-helper.c:50
pbw_FieldDescriptor_full_name
const char * pbw_FieldDescriptor_full_name(const PbwFieldDescriptor *field)
Get the full name of a field descriptor.
Definition protobuf-helper.c:122
pbw_FieldDescriptor_default_value_bool
bool pbw_FieldDescriptor_default_value_bool(const PbwFieldDescriptor *field)
Get the default value of a boolean field descriptor.
Definition protobuf-helper.c:224
pbw_FieldDescriptor_default_value_double
double pbw_FieldDescriptor_default_value_double(const PbwFieldDescriptor *field)
Retrieves the default value of a double field descriptor.
Definition protobuf-helper.c:218
pbw_EnumDescriptor_full_name
const char * pbw_EnumDescriptor_full_name(const PbwEnumDescriptor *anEnum)
Get the full name of an enum descriptor.
Definition protobuf-helper.c:248
pbw_EnumValueDescriptor_name
const char * pbw_EnumValueDescriptor_name(const PbwEnumValueDescriptor *enumValue)
Get the name of an EnumValueDescriptor.
Definition protobuf-helper.c:278
pbw_Descriptor_FindFieldByNumber
const PbwFieldDescriptor * pbw_Descriptor_FindFieldByNumber(const PbwDescriptor *message, int number)
Finds a field descriptor by its number in a message descriptor.
Definition protobuf-helper.c:110
pbw_MethodDescriptor_input_type
const PbwDescriptor * pbw_MethodDescriptor_input_type(const PbwMethodDescriptor *method)
Retrieves the input type descriptor for a method.
Definition protobuf-helper.c:68
VALUE_STRING_ENUM
VALUE_STRING_ENUM(protobuf_field_type)
Value string array for protobuf field types.
pbw_Descriptor_name
const char * pbw_Descriptor_name(const PbwDescriptor *message)
Retrieves the name of a Protocol Buffers descriptor.
Definition protobuf-helper.c:92
pbw_FieldDescriptor_has_default_value
bool pbw_FieldDescriptor_has_default_value(const PbwFieldDescriptor *field)
Check if a field descriptor has a default value.
Definition protobuf-helper.c:182
pbw_FieldDescriptor_default_value_string
const char * pbw_FieldDescriptor_default_value_string(const PbwFieldDescriptor *field, int *size)
Get the default value string of a protobuf field descriptor.
Definition protobuf-helper.c:230
pbw_FieldDescriptor_default_value_int32
int32_t pbw_FieldDescriptor_default_value_int32(const PbwFieldDescriptor *field)
Retrieves the default value for an int32 field descriptor.
Definition protobuf-helper.c:188
pbw_MethodDescriptor_name
const char * pbw_MethodDescriptor_name(const PbwMethodDescriptor *method)
Get the name of a Protocol Buffers method descriptor.
Definition protobuf-helper.c:56
pbw_EnumDescriptor_FindValueByNumber
const PbwEnumValueDescriptor * pbw_EnumDescriptor_FindValueByNumber(const PbwEnumDescriptor *anEnum, int number)
Finds an enum value descriptor by its number.
Definition protobuf-helper.c:266
pbw_FieldDescriptor_is_repeated
int pbw_FieldDescriptor_is_repeated(const PbwFieldDescriptor *field)
Check if a field descriptor is repeated.
Definition protobuf-helper.c:146
pbw_FieldDescriptor_is_required
bool pbw_FieldDescriptor_is_required(const PbwFieldDescriptor *field)
Check if a field descriptor is required.
Definition protobuf-helper.c:176
pbw_MethodDescriptor_output_type
const PbwDescriptor * pbw_MethodDescriptor_output_type(const PbwMethodDescriptor *method)
Retrieves the output type of a method descriptor.
Definition protobuf-helper.c:74
pbw_FieldDescriptor_default_value_int64
int64_t pbw_FieldDescriptor_default_value_int64(const PbwFieldDescriptor *field)
Retrieves the default value for a field descriptor as an int64.
Definition protobuf-helper.c:194
pbw_FieldDescriptor_default_value_float
float pbw_FieldDescriptor_default_value_float(const PbwFieldDescriptor *field)
Retrieves the default value of a float field descriptor.
Definition protobuf-helper.c:212
pbw_foreach_message
void pbw_foreach_message(const PbwDescriptorPool *pool, void(*cb)(const PbwDescriptor *message, void *userdata), void *userdata)
Iterates over all messages in a descriptor pool and applies a callback function to each.
Definition protobuf-helper.c:296
pbw_FieldDescriptor_name
const char * pbw_FieldDescriptor_name(const PbwFieldDescriptor *field)
Retrieves the name of a Protocol Buffers field descriptor.
Definition protobuf-helper.c:128
pbw_FieldDescriptor_type
int pbw_FieldDescriptor_type(const PbwFieldDescriptor *field)
Get the type of a Protocol Buffers field descriptor.
Definition protobuf-helper.c:140
pbw_load_proto_file
int pbw_load_proto_file(PbwDescriptorPool *pool, const char *filename)
Loads a Protocol Buffers file into the descriptor pool.
Definition protobuf-helper.c:40
pbw_EnumDescriptor_value_count
int pbw_EnumDescriptor_value_count(const PbwEnumDescriptor *anEnum)
Get the count of values in an enum descriptor.
Definition protobuf-helper.c:254
pbw_FieldDescriptor_default_value_uint64
uint64_t pbw_FieldDescriptor_default_value_uint64(const PbwFieldDescriptor *field)
Get the default value for a uint64 field descriptor.
Definition protobuf-helper.c:206
pbw_EnumDescriptor_value
const PbwEnumValueDescriptor * pbw_EnumDescriptor_value(const PbwEnumDescriptor *anEnum, int value_index)
Retrieves a PbwEnumValueDescriptor by its index.
Definition protobuf-helper.c:260
VALUE_STRING_ARRAY_GLOBAL_DCL
VALUE_STRING_ARRAY_GLOBAL_DCL(protobuf_field_type)
Value string array for protobuf field types.
pbw_Descriptor_full_name
const char * pbw_Descriptor_full_name(const PbwDescriptor *message)
Get the full name of a Protocol Buffers descriptor.
Definition protobuf-helper.c:86
pbw_MethodDescriptor_full_name
const char * pbw_MethodDescriptor_full_name(const PbwMethodDescriptor *method)
Get the full name of a method descriptor.
Definition protobuf-helper.c:62
pbw_Descriptor_field
const PbwFieldDescriptor * pbw_Descriptor_field(const PbwDescriptor *message, int field_index)
Retrieves a field descriptor by index from a message descriptor.
Definition protobuf-helper.c:104
pbw_FieldDescriptor_default_value_uint32
uint32_t pbw_FieldDescriptor_default_value_uint32(const PbwFieldDescriptor *field)
Retrieves the default value for a uint32 field descriptor.
Definition protobuf-helper.c:200
pbw_FieldDescriptor_default_value_enum
const PbwEnumValueDescriptor * pbw_FieldDescriptor_default_value_enum(const PbwFieldDescriptor *field)
Get the default value of an enum field descriptor.
Definition protobuf-helper.c:236
pbw_FieldDescriptor_is_packed
int pbw_FieldDescriptor_is_packed(const PbwFieldDescriptor *field)
Checks if the given field descriptor is packed.
Definition protobuf-helper.c:152
pbw_FieldDescriptor_message_type
const PbwDescriptor * pbw_FieldDescriptor_message_type(const PbwFieldDescriptor *field)
Retrieves the message type associated with a Protocol Buffers field descriptor.
Definition protobuf-helper.c:164
pbw_EnumValueDescriptor_number
int pbw_EnumValueDescriptor_number(const PbwEnumValueDescriptor *enumValue)
Get the number associated with a Protocol Buffers enum value descriptor.
Definition protobuf-helper.c:290
pbw_FieldDescriptor_typeName
const char * pbw_FieldDescriptor_typeName(wmem_allocator_t *scope, int field_type)
Retrieves the type name for a given field type.
Definition protobuf-helper.c:158
pbw_reinit_DescriptorPool
void pbw_reinit_DescriptorPool(PbwDescriptorPool **pool, const char **directories, pbw_report_error_cb_t error_cb)
Definition protobuf-helper.c:33
pbw_DescriptorPool_FindMessageTypeByName
const PbwDescriptor * pbw_DescriptorPool_FindMessageTypeByName(const PbwDescriptorPool *pool, const char *name)
Finds a message type by its name in a descriptor pool.
Definition protobuf-helper.c:80
pbw_Descriptor_FindFieldByName
const PbwFieldDescriptor * pbw_Descriptor_FindFieldByName(const PbwDescriptor *message, const char *name)
Finds a field descriptor by name in a message descriptor.
Definition protobuf-helper.c:116
pbw_FieldDescriptor_enum_type
const PbwEnumDescriptor * pbw_FieldDescriptor_enum_type(const PbwFieldDescriptor *field)
Get the enum type of a field descriptor.
Definition protobuf-helper.c:170
pbw_EnumDescriptor_FindValueByName
const PbwEnumValueDescriptor * pbw_EnumDescriptor_FindValueByName(const PbwEnumDescriptor *anEnum, const char *name)
Finds a value descriptor by name in an enum descriptor.
Definition protobuf-helper.c:272
value_string.h