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
|
/* packet-bzr.c
* Routines for Bazaar packet dissection
* Copyright 2011,2013 Jelmer Vernooij <jelmer@samba.org>
*
* http://doc.bazaar.canonical.com/developers/network-protocol.html
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* Copied from packet-pop.c
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <epan/packet.h>
#include <epan/prefs.h>
void proto_register_bzr(void);
void proto_reg_handoff_bzr(void);
static int proto_bzr;
static int ett_bzr;
static int ett_prefixed_bencode;
static int ett_prefixed_bytes;
static int hf_bzr_prefixed_bencode;
static int hf_bzr_prefixed_bencode_len;
static int hf_bzr_bytes;
static int hf_bzr_bytes_data;
static int hf_bzr_bytes_length;
static int hf_bzr_result;
static int hf_bzr_packet_protocol_version;
static int hf_bzr_packet_kind;
static dissector_handle_t bencode_handle;
static dissector_handle_t bzr_handle;
#define REQUEST_VERSION_TWO "bzr request 2\n"
#define RESPONSE_VERSION_TWO "bzr response 2\n"
#define MESSAGE_VERSION_THREE "bzr message 3 (bzr 1.6)\n"
#define TCP_PORT_BZR 4155
static const value_string message_part_kind[] = {
{'s', "Structure"},
{'b', "Bytes"},
{'o', "Single byte"},
{'e', "End"},
{0, NULL}
};
static const value_string message_results[] = {
{'S', "Success"},
{'E', "Error"},
{0, NULL}
};
/* desegmentation of Bazaar over TCP */
static bool bzr_desegment = true;
static unsigned get_bzr_prefixed_len(tvbuff_t *tvb, int offset)
{
unsigned header_len;
header_len = tvb_get_ntohl(tvb, offset);
return 4 + header_len;
}
static unsigned
get_bzr_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
{
int next_offset;
int len = 0, current_len;
int protocol_version_len;
uint8_t cmd = 0;
/* Protocol version */
protocol_version_len = tvb_find_line_end(tvb, offset, -1, &next_offset,
true);
if (protocol_version_len == -1) /* End of the packet not seen yet */
return -1;
len += protocol_version_len + 1;
/* Headers */
current_len = len;
len += get_bzr_prefixed_len(tvb, next_offset);
if (current_len > len) /* Make sure we're not going backwards */
return -1;
while (tvb_reported_length_remaining(tvb, offset + len) > 0) {
cmd = tvb_get_uint8(tvb, offset + len);
len += 1;
switch (cmd) {
case 's':
case 'b':
current_len = len;
len += get_bzr_prefixed_len(tvb, offset + len);
if (current_len > len) /* Make sure we're not going backwards */
return -1;
break;
case 'o':
len += 1;
break;
case 'e':
return len;
}
}
return -1;
}
static int
dissect_prefixed_bencode(tvbuff_t *tvb, int offset, packet_info *pinfo,
proto_tree *tree)
{
uint32_t plen;
proto_tree *prefixed_bencode_tree;
proto_item *ti;
tvbuff_t *subtvb;
plen = tvb_get_ntohl(tvb, offset);
ti = proto_tree_add_item(tree, hf_bzr_prefixed_bencode, tvb, offset, -1,
ENC_NA);
prefixed_bencode_tree = proto_item_add_subtree(ti, ett_prefixed_bencode);
proto_tree_add_item(prefixed_bencode_tree, hf_bzr_prefixed_bencode_len,
tvb, offset, 4, ENC_BIG_ENDIAN);
subtvb = tvb_new_subset_length(tvb, offset+4, plen);
call_dissector(bencode_handle, subtvb, pinfo, prefixed_bencode_tree);
proto_item_set_len(ti, 4 + plen);
return 4 + plen;
}
static int
dissect_prefixed_bytes(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
proto_tree *tree)
{
uint32_t plen;
proto_tree *prefixed_bytes_tree;
proto_item *ti;
plen = tvb_get_ntohl(tvb, offset);
ti = proto_tree_add_item(tree, hf_bzr_bytes, tvb, offset, -1, ENC_NA);
prefixed_bytes_tree = proto_item_add_subtree(ti, ett_prefixed_bytes);
proto_tree_add_item(prefixed_bytes_tree, hf_bzr_bytes_length,
tvb, offset, 4, ENC_BIG_ENDIAN);
proto_tree_add_item(prefixed_bytes_tree, hf_bzr_bytes_data,
tvb, offset+4, plen, ENC_NA);
proto_item_set_len(ti, 4 + plen);
return 4 + plen;
}
static int
dissect_body(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
{
uint8_t cmd = 0;
while (tvb_reported_length_remaining(tvb, offset) > 0) {
cmd = tvb_get_uint8(tvb, offset);
proto_tree_add_item(tree, hf_bzr_packet_kind, tvb, offset, 1, ENC_ASCII|ENC_NA);
offset += 1;
switch (cmd) {
case 's':
offset += dissect_prefixed_bencode(tvb, offset, pinfo, tree);
break;
case 'b':
offset += dissect_prefixed_bytes(tvb, offset, pinfo, tree);
break;
case 'o':
proto_tree_add_item(tree, hf_bzr_result, tvb, offset, 1,
ENC_ASCII|ENC_NA);
offset += 1;
break;
case 'e':
break;
}
}
return offset;
}
static void
dissect_bzr_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
proto_tree *bzr_tree;
proto_item *ti;
int offset = 0;
int protocol_version_len;
ti = proto_tree_add_item(tree, proto_bzr, tvb, offset, -1, ENC_NA);
bzr_tree = proto_item_add_subtree(ti, ett_bzr);
protocol_version_len = tvb_find_line_end(tvb, offset, -1, &offset, true);
if (protocol_version_len == -1) /* Invalid packet */
return;
if (bzr_tree)
{
proto_tree_add_item(bzr_tree, hf_bzr_packet_protocol_version, tvb, 0,
protocol_version_len+1, ENC_ASCII);
}
offset += dissect_prefixed_bencode(tvb, offset, pinfo, bzr_tree);
/*offset +=*/ dissect_body(tvb, offset, pinfo, bzr_tree);
}
static int
dissect_bzr(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
{
int offset = 0, pdu_len;
tvbuff_t *next_tvb;
col_set_str(pinfo->cinfo, COL_PROTOCOL, "BZR");
col_set_str(pinfo->cinfo, COL_INFO, "Bazaar Smart Protocol");
while (tvb_reported_length_remaining(tvb, offset) > 0) {
pdu_len = get_bzr_pdu_len(pinfo, tvb, offset);
if (pdu_len == -1) {
if (pinfo->can_desegment && bzr_desegment) {
pinfo->desegment_offset = offset;
pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
return tvb_captured_length(tvb);
} else {
pdu_len = tvb_reported_length_remaining(tvb, offset);
}
}
next_tvb = tvb_new_subset_length(tvb, offset, pdu_len);
dissect_bzr_pdu(next_tvb, pinfo, tree);
offset += pdu_len;
}
return tvb_captured_length(tvb);
}
void
proto_register_bzr(void)
{
static hf_register_info hf[] = {
{ &hf_bzr_packet_kind,
{ "Packet kind", "bzr.kind", FT_CHAR, BASE_HEX,
VALS(message_part_kind), 0x0, NULL, HFILL },
},
{ &hf_bzr_packet_protocol_version,
{ "Protocol version", "bzr.protocol_version", FT_STRING, BASE_NONE,
NULL, 0x0, NULL, HFILL },
},
{ &hf_bzr_prefixed_bencode,
{ "Bencode packet", "bzr.bencode", FT_NONE, BASE_NONE, NULL, 0x0,
"Serialized structure of integers, dictionaries, strings and "
"lists.", HFILL },
},
{ &hf_bzr_prefixed_bencode_len,
{ "Bencode packet length", "bzr.bencode.length", FT_UINT32,
BASE_HEX, NULL, 0x0, NULL, HFILL },
},
{ &hf_bzr_bytes,
{ "Prefixed bytes", "bzr.bytes", FT_NONE, BASE_NONE, NULL, 0x0,
"Bytes field with prefixed 32-bit length", HFILL },
},
{ &hf_bzr_bytes_data,
{ "Prefixed bytes data", "bzr.bytes.data", FT_BYTES, BASE_NONE,
NULL, 0x0, NULL, HFILL },
},
{ &hf_bzr_bytes_length,
{ "Prefixed bytes length", "bzr.bytes.length", FT_UINT32, BASE_HEX,
NULL, 0x0, NULL, HFILL },
},
{ &hf_bzr_result,
{ "Result", "bzr.result", FT_CHAR, BASE_HEX,
VALS(message_results), 0x0,
"Command result (success or failure with error message)", HFILL
},
},
};
static int *ett[] = {
&ett_bzr,
&ett_prefixed_bencode,
&ett_prefixed_bytes,
};
module_t *bzr_module;
proto_bzr = proto_register_protocol("Bazaar Smart Protocol", "Bazaar", "bzr");
bzr_handle = register_dissector("bzr", dissect_bzr, proto_bzr);
proto_register_field_array(proto_bzr, hf, array_length(hf));
proto_register_subtree_array(ett, array_length(ett));
bzr_module = prefs_register_protocol(proto_bzr, NULL);
prefs_register_bool_preference(bzr_module, "desegment",
"Reassemble Bazaar messages spanning multiple TCP segments",
"Whether the Bazaar dissector should reassemble messages spanning multiple TCP segments."
" To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\""
" in the TCP protocol settings.",
&bzr_desegment);
}
void
proto_reg_handoff_bzr(void)
{
bencode_handle = find_dissector_add_dependency("bencode", proto_bzr);
dissector_add_uint_with_preference("tcp.port", TCP_PORT_BZR, bzr_handle);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/
|