-
Notifications
You must be signed in to change notification settings - Fork 4
/
wireshark-glue.cc.template
executable file
·496 lines (423 loc) · 15.1 KB
/
wireshark-glue.cc.template
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include "|PROTO_H_FILE|"
#include "|GLUE_H_FILE|"
#include <iostream>
#include <google/protobuf/text_format.h>
#include <google/protobuf/wire_format.h>
#include <google/protobuf/wire_format_lite.h>
#include <google/protobuf/wire_format_lite_inl.h>
#include <google/protobuf/io/coded_stream.h>
using namespace std;
using namespace google;
using namespace protobuf;
using namespace internal;
extern "C" {
static HandleMap* handleMap = NULL;
static void * _tvb = NULL;
static const char ARRAY_FORMAT[] = "%s[%d]";
/**
* @param buf The message contents
* @param buf_size The length of message contents in buf
* @param tree_root The WireShark tree to which this message is to be added.
* @param item_id Internal wireshark id to refer to this FT_NONE datatype.
*/
int wireshark_pb_process_|PLUGIN_NAME|(proto_tree* tree_root, int item_id, tvbuff_t* tvb, void* buf, int buf_size) {
|PACKAGE||PLUGIN_NAME| msg;
if (!msg.ParseFromArray((char *) buf, buf_size)) {
DBG_ERR( "Failed to parse message." );
/*
for (int i=0; i < buf_size; i++) {
printf("%2x ", ((char *)buf)[i]);
}
*/
/*
printf("buf size=%d\n", buf_size);
printf("%s\n\n\n", buf);
*/
return -1;
}
// store tvb for subsequent calls
_tvb = tvb;
int offset = 0;
DissectorList dissectorList;
// we process the message tree breadth wise
dissectorList.push_back( |PLUGIN_NAME|_Dissector( tree_root, offset, &msg, NULL ) );
while ( !dissectorList.empty() )
{
|PLUGIN_NAME|_Dissector dissector = dissectorList.front();
dissectorList.pop_front();
// this can add further entries to message dissector list
dissector.dissect( dissectorList );
} // while( !dissectorList.empty() )
return 0;
}
void wireshark_pb_process_|PLUGIN_NAME|_register_subtree_( int proto,
const std::string& name,
const std::string& full_name )
{
Handles handles;
wireshark_pb_process_|PLUGIN_NAME|_register_subtree( proto,
name.c_str(),
&(handles.handle), &(handles.tree_handle) );
handleMap->insert( StringHandlePair( full_name, handles ) );
}
int wireshark_pb_process_|PLUGIN_NAME|_get_type( FieldDescriptor::CppType type )
{
struct ftmap_t
{
FieldDescriptor::CppType cpp_type;
ftenum fttype;
int length;
};
static ftmap_t ftmap[] = {
{ FieldDescriptor::CPPTYPE_UINT32, FT_UINT32 },
{ FieldDescriptor::CPPTYPE_INT32, FT_INT32 },
{ FieldDescriptor::CPPTYPE_UINT64, FT_UINT64 },
{ FieldDescriptor::CPPTYPE_INT64, FT_INT64 },
{ FieldDescriptor::CPPTYPE_DOUBLE, FT_DOUBLE },
{ FieldDescriptor::CPPTYPE_FLOAT, FT_FLOAT },
{ FieldDescriptor::CPPTYPE_BOOL, FT_BOOLEAN },
{ FieldDescriptor::CPPTYPE_ENUM, FT_INT32 },
{ FieldDescriptor::CPPTYPE_STRING, FT_STRING } };
for( int i =0; i < sizeof(ftmap)/sizeof(ftmap_t); i++ )
{
if( ftmap[i].cpp_type == type )
{
return ftmap[i].fttype;
}
}
DBG_ERR( "Couldnt find type for cpp type " << type );
return FT_NONE;
}
void wireshark_pb_process_|PLUGIN_NAME|_register_proto( int proto )
{
if( handleMap == NULL )
{
handleMap = new HandleMap();
}
DescriptorList messageDescriptorList;
FieldDescriptorList fieldDescriptorList;
// we process the message definition depth first
const Descriptor* desc = |PACKAGE||PLUGIN_NAME|::descriptor();
messageDescriptorList.push_back( desc );
while( !messageDescriptorList.empty() )
{
const Descriptor* messageDescriptor = messageDescriptorList.back();
messageDescriptorList.pop_back();
DBG( "Register message ( " << messageDescriptor->name() << " )" );
wireshark_pb_process_|PLUGIN_NAME|_register_subtree_( proto,
messageDescriptor->name(),
messageDescriptor->full_name() );
for( int i = 0; i < messageDescriptor->field_count(); i++ )
{
const FieldDescriptor* fieldDescriptor = messageDescriptor->field( i );
if( fieldDescriptor->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE )
{
messageDescriptorList.push_back( fieldDescriptor->message_type() );
}
else
{
fieldDescriptorList.push_back( fieldDescriptor );
}
}
}
// process all field descriptors at very end
while( !fieldDescriptorList.empty() )
{
const FieldDescriptor* fieldDescriptor = fieldDescriptorList.back();
fieldDescriptorList.pop_back();
Handles handles;
DBG( "Register field ( " << fieldDescriptor->name() << " : " << fieldDescriptor->full_name() << " )" );
wireshark_pb_process_|PLUGIN_NAME|_register_field( proto,
wireshark_pb_process_|PLUGIN_NAME|_get_type( fieldDescriptor->cpp_type() ),
fieldDescriptor->name().c_str(),
fieldDescriptor->name().c_str(),
&(handles.handle) );
handleMap->insert( StringHandlePair( fieldDescriptor->full_name(), handles ) );
}
}
|PLUGIN_NAME|_Dissector::|PLUGIN_NAME|_Dissector()
: _leaf( NULL ),
_len( -1 ),
_message( NULL ),
_offset( 0 ),
_prefix_len( -1 ),
_reflection( NULL )
{
}
|PLUGIN_NAME|_Dissector::|PLUGIN_NAME|_Dissector( proto_tree* tree, int offset,
const Message* message,
const FieldDescriptor* field,
int index )
: _leaf( NULL ),
_len( -1 ),
_message( message ),
_offset( offset ),
_prefix_len( 0 ),
_reflection( _message->GetReflection() )
{
bool is_root = ( field == NULL );
const std::string& label = ( is_root ) ? _message->GetDescriptor()->name() : field->name();
const std::string& fullname = _message->GetDescriptor()->full_name();
int data_size = _message->ByteSize();
int total_size = data_size;
// if not root field then prefix_len needs to be computed
if( !is_root )
{
_prefix_len = WireFormat::TagSize( _message->GetDescriptor()->index(),
FieldDescriptor::TYPE_MESSAGE );
_prefix_len+= io::CodedOutputStream::VarintSize32( data_size );
total_size+= _prefix_len;
}
_len = total_size;
// construct subtree here itself rather than in dissect method otherwise
// all repeated fields and messages are pushed to end of current tree
// regardless of their current position
HandleMap::iterator it = handleMap->find( fullname );
if( it == handleMap->end() )
{
DBG_ERR( "couldnt find handle for " << fullname );
return;
}
Handles handles = it->second;
// add a subtree for current message
proto_item* item;
if( index == -1 )
{
item = proto_tree_add_none_format( tree, handles.handle, _tvb,
_offset, _len,
label.c_str() );
}
else
{
item = proto_tree_add_none_format( tree, handles.handle, _tvb,
_offset, _len, ARRAY_FORMAT,
label.c_str(), index );
}
_leaf = proto_item_add_subtree( item, *(handles.tree_handle) );
}
void |PLUGIN_NAME|_Dissector::dissect( DissectorList& dissectorList )
{
//DBG( "Dissecting " << _message->GetDescriptor()->name()
// << " of length " << _len
// << " prefix len " << _prefix_len );
if( !_leaf )
{
DBG_ERR( "Couldnt find leaf node for current message" );
return;
}
_offset+= _prefix_len;
// now loop through all the field in current message
FieldDescriptorList fields ;
_reflection->ListFields( *_message, &fields );
for( FieldDescriptorList::iterator it = fields.begin(); it!=fields.end(); it++ )
{
const FieldDescriptor* field = *it;
bool is_message = ( field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE ) ;
if( field->is_repeated() )
{
if( field->options().packed() )
{
int data_size = WireFormat::FieldDataOnlyByteSize( field, *_message );
if (data_size > 0)
{
_offset += WireFormat::TagSize( field->number(), FieldDescriptor::TYPE_STRING );
_offset += io::CodedOutputStream::VarintSize32( data_size );
}
}
int size = _reflection->FieldSize( *_message, field );
for( int i = 0; i < size; i++ )
{
if( is_message )
{
const Message& subMessage = _reflection->GetRepeatedMessage( *_message,
field, i );
|PLUGIN_NAME|_Dissector dissector( _leaf, _offset,
&subMessage, field, i );
dissectorList.push_back( dissector );
_offset+= dissector.messageLength();
}
else
{
dissectRepeatedField( field, i );
}
}
}
else if( is_message )
{
const Message& subMessage = _reflection->GetMessage( *_message, field );
|PLUGIN_NAME|_Dissector dissector( _leaf, _offset,
&subMessage, field );
dissectorList.push_back( dissector );
_offset+= dissector.messageLength();
}
else // plain simple field. process it immediately
{
dissectField( field );
}
}
}
void |PLUGIN_NAME|_Dissector::dissectField( const FieldDescriptor* field )
{
int len = WireFormat::FieldByteSize( field, *_message );
//DBG( "Dissecting field " << field->name() << " " << len );
HandleMap::iterator it = handleMap->find( field->full_name() );
if( it == handleMap->end() )
{
DBG_ERR( "Couldnt find handle for " << field->full_name() );
return;
}
Handles handles = it->second;
const EnumValueDescriptor* enumDesc = NULL;
switch( field->cpp_type() )
{
case FieldDescriptor::CPPTYPE_UINT32:
proto_tree_add_uint( _leaf, handles.handle, _tvb, _offset, len,
_reflection->GetUInt32( *_message, field ) );
break;
case FieldDescriptor::CPPTYPE_INT32:
proto_tree_add_int( _leaf, handles.handle, _tvb, _offset, len,
_reflection->GetInt32( *_message, field ) );
break;
case FieldDescriptor::CPPTYPE_FLOAT:
proto_tree_add_float( _leaf, handles.handle, _tvb, _offset, len,
_reflection->GetFloat( *_message, field ) );
break;
case FieldDescriptor::CPPTYPE_UINT64:
proto_tree_add_uint64( _leaf, handles.handle, _tvb, _offset, len,
_reflection->GetUInt64( *_message, field ) );
break;
case FieldDescriptor::CPPTYPE_INT64:
proto_tree_add_int64( _leaf, handles.handle, _tvb, _offset, len,
_reflection->GetInt64( *_message, field ) );
break;
case FieldDescriptor::CPPTYPE_DOUBLE:
proto_tree_add_double( _leaf, handles.handle, _tvb, _offset, len,
_reflection->GetDouble( *_message, field ) );
break;
case FieldDescriptor::CPPTYPE_BOOL:
proto_tree_add_boolean( _leaf, handles.handle, _tvb, _offset, len,
_reflection->GetBool( *_message, field ) );
break;
case FieldDescriptor::CPPTYPE_ENUM:
enumDesc = _reflection->GetEnum( *_message, field );
proto_tree_add_int_format_value( _leaf, handles.handle, _tvb, _offset, len,
enumDesc->number(), "%d ( %s )", enumDesc->number(),
enumDesc->name().c_str() );
break;
case FieldDescriptor::CPPTYPE_STRING:
proto_tree_add_string( _leaf, handles.handle, _tvb, _offset, len,
_reflection->GetString( *_message, field ).c_str() );
break;
default:
proto_tree_add_item( _leaf, handles.handle, _tvb, _offset, len, true );
};
_offset+=len;
}
void |PLUGIN_NAME|_Dissector::dissectRepeatedField( const FieldDescriptor* field, int index )
{
int len = 0;
string scratch;
if( !field->options().packed() )
{
len+= WireFormat::TagSize( field->number(), field->type() );
}
//DBG( "Dissecting field " << field->name() << " " << len );
HandleMap::iterator it = handleMap->find( field->full_name() );
if( it == handleMap->end() )
{
DBG_ERR( "Couldnt find handle for " << field->full_name() );
return;
}
Handles handles = it->second;
const EnumValueDescriptor* enumDesc = NULL;
switch( field->cpp_type() )
{
case FieldDescriptor::CPPTYPE_UINT32:
if( field->type() == FieldDescriptor::TYPE_FIXED32 )
{
len+= WireFormatLite::kFixed32Size;
}
else
{
len+= WireFormatLite::UInt32Size( _reflection->GetRepeatedUInt32( *_message, field, index ) );
}
proto_tree_add_uint( _leaf, handles.handle, _tvb, _offset, len,
_reflection->GetRepeatedUInt32( *_message, field, index ) );
break;
case FieldDescriptor::CPPTYPE_INT32:
if( field->type() == FieldDescriptor::TYPE_SFIXED32 )
{
len+= WireFormatLite::kSFixed32Size;
}
else if( field->type() == FieldDescriptor::TYPE_SINT32 )
{
len+= WireFormatLite::SInt32Size( _reflection->GetRepeatedInt32( *_message, field, index ) );
}
else
{
len+= WireFormatLite::Int32Size( _reflection->GetRepeatedInt32( *_message, field, index ) );
}
proto_tree_add_int( _leaf, handles.handle, _tvb, _offset, len,
_reflection->GetRepeatedInt32( *_message, field, index ) );
break;
case FieldDescriptor::CPPTYPE_FLOAT:
len+= WireFormatLite::kFloatSize;
proto_tree_add_float( _leaf, handles.handle, _tvb, _offset, len,
_reflection->GetRepeatedFloat( *_message, field, index ) );
break;
case FieldDescriptor::CPPTYPE_UINT64:
if( field->type() == FieldDescriptor::TYPE_FIXED64 )
{
len+= WireFormatLite::kFixed64Size;
}
else
{
len+= WireFormatLite::UInt64Size( _reflection->GetRepeatedUInt64( *_message, field, index ) );
}
proto_tree_add_uint64( _leaf, handles.handle, _tvb, _offset, len,
_reflection->GetRepeatedUInt64( *_message, field, index ) );
break;
case FieldDescriptor::CPPTYPE_INT64:
if( field->type() == FieldDescriptor::TYPE_SFIXED64 )
{
len+= WireFormatLite::kSFixed64Size;
}
else if( field->type() == FieldDescriptor::TYPE_SINT64 )
{
len+= WireFormatLite::SInt64Size( _reflection->GetRepeatedInt64( *_message, field, index ) );
}
else
{
len+= WireFormatLite::Int64Size( _reflection->GetRepeatedInt64( *_message, field, index ) );
}
proto_tree_add_int64( _leaf, handles.handle, _tvb, _offset, len,
_reflection->GetRepeatedInt64( *_message, field, index ) );
break;
case FieldDescriptor::CPPTYPE_DOUBLE:
len+= WireFormatLite::kDoubleSize;
proto_tree_add_double( _leaf, handles.handle, _tvb, _offset, len,
_reflection->GetRepeatedDouble( *_message, field, index ) );
break;
case FieldDescriptor::CPPTYPE_BOOL:
len+= WireFormatLite::kBoolSize;
proto_tree_add_boolean( _leaf, handles.handle, _tvb, _offset, len,
_reflection->GetRepeatedBool( *_message, field, index ) );
break;
case FieldDescriptor::CPPTYPE_ENUM:
enumDesc = _reflection->GetRepeatedEnum( *_message, field, index );
len+= WireFormatLite::EnumSize( enumDesc->number() );
proto_tree_add_int_format_value( _leaf, handles.handle, _tvb, _offset, len,
enumDesc->number(), "%d ( %s )", enumDesc->number(),
enumDesc->name().c_str() );
break;
case FieldDescriptor::CPPTYPE_STRING:
len+= WireFormatLite::StringSize( _reflection->GetRepeatedStringReference( *_message, field, index, &scratch ) );
proto_tree_add_string( _leaf, handles.handle, _tvb, _offset, len,
_reflection->GetRepeatedString( *_message, field, index ).c_str() );
break;
default:
proto_tree_add_item( _leaf, handles.handle, _tvb, _offset, len, true );
};
_offset+=len;
}
}