From Didier Gautheron:
[obnox/wireshark/wip.git] / epan / dissectors / packet-netsync.c
1 /* packet-netsync.c
2  * Routines for Monotone Netsync packet disassembly
3  *
4  * $Id$
5  *
6  * Copyright (c) 2005 by Erwin Rol <erwin@erwinrol.com>
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1999 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 /* Include files */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36 #include <time.h>
37 #include <string.h>
38 #include <epan/packet.h>
39 #include <epan/addr_resolv.h>
40 #include <epan/prefs.h>
41 #include <epan/strutil.h>
42 #include "packet-tcp.h"
43
44 /*
45  * See
46  *
47  *      http://www.venge.net/monotone/
48  */
49
50 /* Define TCP ports for Monotone netsync */
51
52 #define TCP_PORT_NETSYNC 5253
53
54 #define NETSYNC_ROLE_SOURCE     1
55 #define NETSYNC_ROLE_SINK       2
56 #define NETSYNC_ROLE_BOTH       3
57
58 static const value_string netsync_role_vals[] = {
59         { NETSYNC_ROLE_SOURCE,  "Source" },
60         { NETSYNC_ROLE_SINK,    "Sink" },
61         { NETSYNC_ROLE_BOTH,    "Both" },
62         { 0,                    NULL }
63 };
64
65
66 #define NETSYNC_CMD_ERROR       0
67 #define NETSYNC_CMD_BYE         1
68 #define NETSYNC_CMD_HELLO       2
69 #define NETSYNC_CMD_ANONYMOUS   3
70 #define NETSYNC_CMD_AUTH        4
71 #define NETSYNC_CMD_CONFIRM     5
72 #define NETSYNC_CMD_REFINE      6
73 #define NETSYNC_CMD_DONE        7
74 #define NETSYNC_CMD_SEND_DATA   8
75 #define NETSYNC_CMD_SEND_DELTA  9
76 #define NETSYNC_CMD_DATA        10
77 #define NETSYNC_CMD_DELTA       11
78 #define NETSYNC_CMD_NONEXISTENT 12
79
80 static const value_string netsync_cmd_vals[] = {
81         { NETSYNC_CMD_ERROR,            "Error" },
82         { NETSYNC_CMD_BYE,              "Bye" },
83         { NETSYNC_CMD_HELLO,            "Hello" },
84         { NETSYNC_CMD_ANONYMOUS,        "Anonymous" },
85         { NETSYNC_CMD_AUTH,             "Auth" },
86         { NETSYNC_CMD_CONFIRM,          "Confirm" },
87         { NETSYNC_CMD_REFINE,           "Refine" },
88         { NETSYNC_CMD_DONE,             "Done" },
89         { NETSYNC_CMD_SEND_DATA,        "Send Data" },
90         { NETSYNC_CMD_SEND_DELTA,       "Send Delta" },
91         { NETSYNC_CMD_DATA,             "Data" },
92         { NETSYNC_CMD_DELTA,            "Delta" },
93         { NETSYNC_CMD_NONEXISTENT,      "Nonexistent" },
94         { 0,                            NULL }
95 };
96
97 #define NETSNYC_MERKLE_HASH_LENGTH 20
98
99 void proto_reg_handoff_netsync(void);
100
101 /* Define the monotone netsync proto */
102 static int proto_netsync = -1;
103
104 static int hf_netsync_version = -1;
105 static int hf_netsync_command = -1;
106 static int hf_netsync_size = -1;
107 static int hf_netsync_data = -1;
108 static int hf_netsync_checksum = -1;
109
110 static int hf_netsync_cmd_done_level = -1;
111 static int hf_netsync_cmd_done_type = -1;
112
113 static int hf_netsync_cmd_hello_keyname = -1;
114 static int hf_netsync_cmd_hello_key = -1;
115 static int hf_netsync_cmd_nonce = -1;
116
117 static int hf_netsync_cmd_anonymous_role = -1;
118 static int hf_netsync_cmd_anonymous_collection = -1;
119
120 static int hf_netsync_cmd_send_data_type = -1;
121 static int hf_netsync_cmd_send_data_id = -1;
122
123 static int hf_netsync_cmd_error_msg = -1;
124
125
126 static int hf_netsync_cmd_confirm_sig = -1;
127
128 static int hf_netsync_cmd_auth_role = -1;
129 static int hf_netsync_cmd_auth_collection = -1;
130 static int hf_netsync_cmd_auth_id = -1;
131 static int hf_netsync_cmd_auth_nonce1 = -1;
132 static int hf_netsync_cmd_auth_nonce2 = -1;
133 static int hf_netsync_cmd_auth_sig = -1;
134
135 static int hf_netsync_cmd_data_type = -1;
136 static int hf_netsync_cmd_data_id = -1;
137 static int hf_netsync_cmd_data_compressed = -1;
138 static int hf_netsync_cmd_data_payload = -1;
139
140 static int hf_netsync_cmd_delta_type = -1;
141 static int hf_netsync_cmd_delta_base_id = -1;
142 static int hf_netsync_cmd_delta_ident_id = -1;
143 static int hf_netsync_cmd_delta_compressed = -1;
144 static int hf_netsync_cmd_delta_payload = -1;
145
146 static int hf_netsync_cmd_refine_tree_node = -1;
147
148 static int hf_netsync_cmd_send_delta_type = -1;
149 static int hf_netsync_cmd_send_delta_base_id = -1;
150 static int hf_netsync_cmd_send_delta_ident_id = -1;
151
152 static int hf_netsync_cmd_nonexistent_type = -1;
153 static int hf_netsync_cmd_nonexistent_id = -1;
154
155 /* Define the tree for netsync */
156 static int ett_netsync = -1;
157
158
159 /*
160  * Here are the global variables associated with the preferences
161  * for monotone netsync
162  */
163
164 static guint global_tcp_port_netsync = TCP_PORT_NETSYNC;
165 static gboolean netsync_desegment = TRUE;
166
167 static gint dissect_uleb128( tvbuff_t *tvb, gint offset, guint* size)
168 {
169         guint shift = 0;
170         guint8 tmp;
171         guint start_offset = offset;
172
173         *size = 0;
174
175         /* get size */
176         do {
177                 tmp = tvb_get_guint8(tvb, offset);
178                 offset += 1;
179
180                 *size |= (tmp & 0x7F) << shift;
181                 shift += 7;
182         } while (tmp & 0x80);
183
184
185         return offset - start_offset;
186 }
187
188 static gint dissect_netsync_cmd_error( tvbuff_t *tvb,  gint offset, proto_tree *tree, guint size _U_)
189 {
190         guint len = 0;
191
192         offset += dissect_uleb128( tvb, offset, &len );
193
194         proto_tree_add_item(tree, hf_netsync_cmd_error_msg, tvb,
195                                 offset, len, FALSE );
196         offset += len;
197
198         return offset;
199 }
200
201 static gint dissect_netsync_cmd_bye(tvbuff_t *tvb _U_,  gint offset, proto_tree *tree _U_, guint size _U_)
202 {
203         return offset;
204 }
205
206
207 static gint dissect_netsync_cmd_hello(tvbuff_t *tvb,  gint offset, proto_tree *tree, guint size _U_)
208 {
209         guint len = 0;
210
211         offset += dissect_uleb128( tvb, offset, &len );
212
213         proto_tree_add_item(tree, hf_netsync_cmd_hello_keyname, tvb,
214                                 offset, len, FALSE );
215         offset += len;
216
217
218         offset += dissect_uleb128( tvb, offset, &len );
219
220         proto_tree_add_item(tree, hf_netsync_cmd_hello_key, tvb,
221                                 offset, len, FALSE );
222         offset += len;
223
224         proto_tree_add_item(tree, hf_netsync_cmd_nonce, tvb,
225                                 offset, NETSNYC_MERKLE_HASH_LENGTH, FALSE );
226         offset += NETSNYC_MERKLE_HASH_LENGTH;
227
228         return offset;
229 }
230
231
232 static gint dissect_netsync_cmd_anonymous(tvbuff_t *tvb,  gint offset, proto_tree *tree, guint size _U_)
233 {
234         guint len = 0;
235
236         proto_tree_add_item(tree, hf_netsync_cmd_anonymous_role, tvb,
237                                 offset, 1, FALSE );
238         offset += 1;
239
240         offset += dissect_uleb128( tvb, offset, &len );
241
242         proto_tree_add_item(tree, hf_netsync_cmd_anonymous_collection, tvb,
243                                 offset, len, FALSE );
244         offset += len;
245
246         proto_tree_add_item(tree, hf_netsync_cmd_nonce, tvb,
247                                 offset, NETSNYC_MERKLE_HASH_LENGTH, FALSE );
248         offset += NETSNYC_MERKLE_HASH_LENGTH;
249
250         return offset;
251 }
252
253
254 static gint dissect_netsync_cmd_auth(tvbuff_t *tvb,  gint offset, proto_tree *tree, guint size _U_)
255 {
256         guint len = 0;
257
258         proto_tree_add_item(tree, hf_netsync_cmd_auth_role, tvb,
259                                 offset, 1, FALSE );
260         offset += 1;
261
262
263         offset += dissect_uleb128( tvb, offset, &len );
264
265         proto_tree_add_item(tree, hf_netsync_cmd_auth_collection, tvb,
266                                 offset, len, FALSE );
267         offset += len;
268
269         proto_tree_add_item(tree, hf_netsync_cmd_auth_id, tvb,
270                                 offset, NETSNYC_MERKLE_HASH_LENGTH, FALSE );
271         offset += NETSNYC_MERKLE_HASH_LENGTH;
272
273         offset += len;
274
275         proto_tree_add_item(tree, hf_netsync_cmd_auth_nonce1, tvb,
276                                 offset, NETSNYC_MERKLE_HASH_LENGTH, FALSE );
277         offset += NETSNYC_MERKLE_HASH_LENGTH;
278
279         offset += len;
280
281         proto_tree_add_item(tree, hf_netsync_cmd_auth_nonce2, tvb,
282                                 offset, NETSNYC_MERKLE_HASH_LENGTH, FALSE );
283         offset += NETSNYC_MERKLE_HASH_LENGTH;
284
285         offset += dissect_uleb128( tvb, offset, &len );
286
287         proto_tree_add_item(tree, hf_netsync_cmd_auth_sig, tvb,
288                                 offset, len, FALSE );
289         offset += len;
290
291         return offset;
292 }
293
294
295 static gint dissect_netsync_cmd_confirm(tvbuff_t *tvb,  gint offset, proto_tree *tree, guint size _U_)
296 {
297         guint len = 0;
298
299         offset += dissect_uleb128( tvb, offset, &len );
300
301         proto_tree_add_item(tree, hf_netsync_cmd_confirm_sig, tvb,
302                                 offset, len, FALSE );
303         offset += len;
304
305
306         return offset;
307 }
308
309
310 static gint dissect_netsync_cmd_refine(tvbuff_t *tvb,  gint offset, proto_tree *tree, guint size)
311 {
312         proto_tree_add_item(tree, hf_netsync_cmd_refine_tree_node, tvb,
313                                 offset, size, FALSE );
314         offset += size;
315
316         return offset;
317 }
318
319
320 static gint dissect_netsync_cmd_done(tvbuff_t *tvb,  gint offset, proto_tree *tree, guint size _U_)
321 {
322         guint len = 0;
323         guint bytes = 0;
324
325         bytes = dissect_uleb128( tvb, offset, &len );
326
327         proto_tree_add_uint(tree, hf_netsync_cmd_done_level, tvb,
328                                         offset, bytes, len );
329         offset += bytes;
330
331         proto_tree_add_item(tree, hf_netsync_cmd_done_type, tvb,
332                                 offset, 1, FALSE );
333         offset += 1;
334
335         return offset;
336 }
337
338
339 static gint dissect_netsync_cmd_send_data(tvbuff_t *tvb,  gint offset, proto_tree *tree, guint size _U_)
340 {
341         proto_tree_add_item(tree, hf_netsync_cmd_send_data_type, tvb,
342                                         offset, 1, FALSE );
343         offset += 1;
344
345         proto_tree_add_item(tree, hf_netsync_cmd_send_data_id, tvb,
346                                 offset, NETSNYC_MERKLE_HASH_LENGTH, FALSE );
347         offset += NETSNYC_MERKLE_HASH_LENGTH;
348
349         return offset;
350 }
351
352
353 static gint dissect_netsync_cmd_send_delta(tvbuff_t *tvb,  gint offset, proto_tree *tree, guint size _U_)
354 {
355         proto_tree_add_item(tree, hf_netsync_cmd_send_delta_type, tvb,
356                                         offset, 1, FALSE );
357         offset += 1;
358
359         proto_tree_add_item(tree, hf_netsync_cmd_send_delta_base_id, tvb,
360                                 offset, NETSNYC_MERKLE_HASH_LENGTH, FALSE );
361         offset += NETSNYC_MERKLE_HASH_LENGTH;
362
363
364         proto_tree_add_item(tree, hf_netsync_cmd_send_delta_ident_id, tvb,
365                                 offset, NETSNYC_MERKLE_HASH_LENGTH, FALSE );
366         offset += NETSNYC_MERKLE_HASH_LENGTH;
367
368         return offset;
369 }
370
371
372 static gint dissect_netsync_cmd_data(tvbuff_t *tvb,  gint offset, proto_tree *tree, guint size _U_)
373 {
374         guint len = 0;
375
376         proto_tree_add_item(tree, hf_netsync_cmd_data_type, tvb,
377                                 offset, 1, FALSE );
378         offset += 1;
379
380         proto_tree_add_item(tree, hf_netsync_cmd_data_id, tvb,
381                                 offset, NETSNYC_MERKLE_HASH_LENGTH, FALSE );
382         offset += NETSNYC_MERKLE_HASH_LENGTH;
383
384         proto_tree_add_item(tree, hf_netsync_cmd_data_compressed, tvb,
385                                 offset, 1, FALSE );
386         offset += 1;
387
388         offset += dissect_uleb128( tvb, offset, &len );
389
390         proto_tree_add_item(tree, hf_netsync_cmd_data_payload, tvb,
391                                 offset, len, FALSE );
392         offset += len;
393
394         return offset;
395 }
396
397
398 static gint dissect_netsync_cmd_delta(tvbuff_t *tvb,  gint offset, proto_tree *tree, guint size _U_)
399 {
400         guint len = 0;
401
402         proto_tree_add_item(tree, hf_netsync_cmd_delta_type, tvb,
403                                 offset, 1, FALSE );
404         offset += 1;
405
406         proto_tree_add_item(tree, hf_netsync_cmd_delta_base_id, tvb,
407                                 offset, NETSNYC_MERKLE_HASH_LENGTH, FALSE );
408         offset += NETSNYC_MERKLE_HASH_LENGTH;
409
410         proto_tree_add_item(tree, hf_netsync_cmd_delta_ident_id, tvb,
411                                 offset, NETSNYC_MERKLE_HASH_LENGTH, FALSE );
412         offset += NETSNYC_MERKLE_HASH_LENGTH;
413
414         proto_tree_add_item(tree, hf_netsync_cmd_delta_compressed, tvb,
415                                 offset, 1, FALSE );
416         offset += 1;
417
418         offset += dissect_uleb128( tvb, offset, &len );
419
420         proto_tree_add_item(tree, hf_netsync_cmd_delta_payload, tvb,
421                                 offset, len, FALSE );
422         offset += len;
423
424         return offset;
425 }
426
427
428 static gint dissect_netsync_cmd_nonexistent(tvbuff_t *tvb,  gint offset, proto_tree *tree, guint size _U_)
429 {
430         proto_tree_add_item(tree, hf_netsync_cmd_nonexistent_type, tvb,
431                                 offset, 1, FALSE );
432         offset += 1;
433
434         proto_tree_add_item(tree, hf_netsync_cmd_nonexistent_id, tvb,
435                                 offset, NETSNYC_MERKLE_HASH_LENGTH, FALSE );
436         offset += NETSNYC_MERKLE_HASH_LENGTH;
437
438         return offset;
439 }
440
441 static guint
442 get_netsync_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
443 {
444         guint size = 0, size_bytes;
445
446         /* skip version and command */
447         offset += 2;
448
449         size_bytes = dissect_uleb128( tvb, offset, &size );
450
451         /* the calculated size if for the data only, this doesn't
452          * include the version (1 byte), command (1 byte),
453          * length (size_bytes bytes) and checksum (4 bytes)
454          */
455
456         return 1 + 1 + size_bytes + size + 4;
457 }
458
459 static void
460 dissect_netsync_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
461 {
462         gint offset = 0;
463         guint8 tmp;
464         guint8 cmd, version;
465         guint32 size, size_bytes, shift;
466         proto_tree *ti,*netsync_tree=NULL;
467
468         /* Set the protocol column */
469         col_set_str(pinfo->cinfo, COL_PROTOCOL, "Netsync");
470
471
472         if (tree == NULL)
473                 return;
474
475         while (tvb_reported_length_remaining(tvb, offset)  > 0) {
476                 ti = proto_tree_add_item(tree, proto_netsync, tvb, offset, -1, FALSE);
477                 netsync_tree = proto_item_add_subtree(ti, ett_netsync);
478
479                 version = tvb_get_guint8(tvb, offset);
480                 proto_tree_add_item(netsync_tree, hf_netsync_version, tvb,
481                                         offset, 1, FALSE );
482                 offset += 1;
483
484                 cmd = tvb_get_guint8(tvb, offset);
485                 proto_tree_add_item(netsync_tree, hf_netsync_command, tvb,
486                                         offset, 1, FALSE );
487                 offset += 1;
488
489
490                 /* get size */
491                 size = 0;
492                 size_bytes = 0;
493                 shift = 0;
494                 do {
495                         tmp = tvb_get_guint8(tvb, offset + size_bytes);
496                         size_bytes += 1;
497
498                         size |= (tmp & 0x7F) << shift;
499                         shift += 7;
500                 } while (tmp & 0x80);
501
502
503                 proto_tree_add_uint(netsync_tree, hf_netsync_size, tvb,
504                                         offset, size_bytes, size );
505                 offset += size_bytes;
506
507                 switch (cmd) {
508                         case NETSYNC_CMD_DONE:
509                                 dissect_netsync_cmd_done( tvb, offset, netsync_tree, size );
510                                 break;
511
512                         case NETSYNC_CMD_ERROR:
513                                 dissect_netsync_cmd_error( tvb, offset, netsync_tree, size );
514                                 break;
515
516                         case NETSYNC_CMD_BYE:
517                                 dissect_netsync_cmd_bye( tvb, offset, netsync_tree, size );
518                                 break;
519
520                         case NETSYNC_CMD_HELLO:
521                                 dissect_netsync_cmd_hello( tvb, offset, netsync_tree, size );
522                                 break;
523
524                         case NETSYNC_CMD_ANONYMOUS:
525                                 dissect_netsync_cmd_anonymous( tvb, offset, netsync_tree, size );
526                                 break;
527
528                         case NETSYNC_CMD_AUTH:
529                                 dissect_netsync_cmd_auth( tvb, offset, netsync_tree, size );
530                                 break;
531
532                         case NETSYNC_CMD_CONFIRM:
533                                 dissect_netsync_cmd_confirm( tvb, offset, netsync_tree, size );
534                                 break;
535
536                         case NETSYNC_CMD_REFINE:
537                                 dissect_netsync_cmd_refine( tvb, offset, netsync_tree, size );
538                                 break;
539
540                         case NETSYNC_CMD_SEND_DATA:
541                                 dissect_netsync_cmd_send_data( tvb, offset, netsync_tree, size );
542                                 break;
543
544                         case NETSYNC_CMD_SEND_DELTA:
545                                 dissect_netsync_cmd_send_delta( tvb, offset, netsync_tree, size );
546                                 break;
547
548                         case NETSYNC_CMD_DATA:
549                                 dissect_netsync_cmd_data( tvb, offset, netsync_tree, size );
550                                 break;
551
552                         case NETSYNC_CMD_DELTA:
553                                 dissect_netsync_cmd_delta( tvb, offset, netsync_tree, size );
554                                 break;
555
556                         case NETSYNC_CMD_NONEXISTENT:
557                                 dissect_netsync_cmd_nonexistent( tvb, offset, netsync_tree, size );
558                                 break;
559
560                         default:
561                                 proto_tree_add_item(netsync_tree, hf_netsync_data, tvb,
562                                         offset, size, FALSE );
563                                 break;
564                 }
565
566                 offset += size;
567
568                 proto_tree_add_item(netsync_tree, hf_netsync_checksum, tvb,
569                                         offset, 4, FALSE );
570                 offset += 4;
571
572
573                 proto_item_append_text(netsync_tree, " V%d, Cmd: %s (%d), Size: %d",
574                                         version, val_to_str(cmd, netsync_cmd_vals, "(0x%x)"), cmd, size );
575
576                 proto_item_set_len(netsync_tree, 1+1+size_bytes+size+4);
577         }
578 }
579
580 static void
581 dissect_netsync(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
582 {
583         tcp_dissect_pdus(tvb, pinfo, tree, netsync_desegment, 7, get_netsync_pdu_len,
584                                         dissect_netsync_pdu);
585 }
586
587 void
588 proto_register_netsync(void)
589 {
590         static hf_register_info hf[] = {
591                 /* General */
592                 { &hf_netsync_version,
593                         { "Version", "netsync.version",
594                           FT_UINT8, BASE_DEC, NULL, 0x0,
595                           NULL, HFILL } },
596                 { &hf_netsync_command,
597                         { "Command", "netsync.command",
598                           FT_UINT8, BASE_HEX, VALS(netsync_cmd_vals), 0x0,
599                           NULL, HFILL } },
600                 { &hf_netsync_size,
601                         { "Size", "netsync.size",
602                           FT_UINT32, BASE_DEC, NULL, 0x0,
603                           NULL, HFILL } },
604                 { &hf_netsync_data,
605                         { "Data", "netsync.data",
606                           FT_BYTES, BASE_NONE, NULL, 0x0,
607                           NULL, HFILL } },
608                 { &hf_netsync_checksum,
609                         { "Checksum", "netsync.checksum",
610                           FT_UINT32, BASE_HEX, NULL, 0x0,
611                           NULL, HFILL } },
612                 { &hf_netsync_cmd_hello_keyname,
613                         { "Key Name", "netsync.cmd.hello.keyname",
614                           FT_STRING, BASE_NONE, NULL, 0x0,
615                           NULL, HFILL } },
616                 { &hf_netsync_cmd_hello_key,
617                         { "Key", "netsync.cmd.hello.key",
618                           FT_BYTES, BASE_NONE, NULL, 0x0,
619                           NULL, HFILL } },
620                 { &hf_netsync_cmd_nonce,
621                         { "Nonce", "netsync.cmd.nonce",
622                           FT_BYTES, BASE_NONE, NULL, 0x0,
623                           NULL, HFILL } },
624                 { &hf_netsync_cmd_anonymous_role,
625                         { "Role", "netsync.cmd.anonymous.role",
626                           FT_UINT8, BASE_DEC, VALS(netsync_role_vals), 0x0,
627                           NULL, HFILL } },
628                 { &hf_netsync_cmd_anonymous_collection,
629                         { "Collection", "netsync.cmd.anonymous.collection",
630                           FT_STRING, BASE_NONE, NULL, 0x0,
631                           NULL, HFILL } },
632                 { &hf_netsync_cmd_confirm_sig,
633                         { "Signature", "netsync.cmd.confirm.signature",
634                           FT_BYTES, BASE_NONE, NULL, 0x0,
635                           NULL, HFILL } },
636                 { &hf_netsync_cmd_send_data_type,
637                         { "Type", "netsync.cmd.send_data.type",
638                           FT_UINT8, BASE_DEC, NULL, 0x0,
639                           NULL, HFILL } },
640                 { &hf_netsync_cmd_send_data_id,
641                         { "ID", "netsync.cmd.send_data.id",
642                           FT_BYTES, BASE_NONE, NULL, 0x0,
643                           NULL, HFILL } },
644                 { &hf_netsync_cmd_error_msg,
645                         { "Message", "netsync.cmd.error.msg",
646                           FT_STRING, BASE_NONE, NULL, 0x0,
647                           NULL, HFILL } },
648
649                 { &hf_netsync_cmd_done_level,
650                         { "Level", "netsync.cmd.done.level",
651                           FT_UINT32, BASE_DEC, NULL, 0x0,
652                           NULL, HFILL } },
653                 { &hf_netsync_cmd_auth_role,
654                         { "Role", "netsync.cmd.auth.role",
655                           FT_UINT8, BASE_DEC, VALS(netsync_role_vals), 0x0,
656                           NULL, HFILL } },
657                 { &hf_netsync_cmd_auth_collection,
658                         { "Collection", "netsync.cmd.auth.collection",
659                           FT_STRING, BASE_NONE, NULL, 0x0,
660                           NULL, HFILL } },
661                 { &hf_netsync_cmd_auth_id,
662                         { "ID", "netsync.cmd.auth.id",
663                           FT_BYTES, BASE_NONE, NULL, 0x0,
664                           NULL, HFILL } },
665                 { &hf_netsync_cmd_auth_nonce1,
666                         { "Nonce 1", "netsync.cmd.auth.nonce1",
667                           FT_BYTES, BASE_NONE, NULL, 0x0,
668                           NULL, HFILL } },
669                 { &hf_netsync_cmd_auth_nonce2,
670                         { "Nonce 2", "netsync.cmd.auth.nonce2",
671                           FT_BYTES, BASE_NONE, NULL, 0x0,
672                           NULL, HFILL } },
673                 { &hf_netsync_cmd_auth_sig,
674                         { "Signature", "netsync.cmd.auth.sig",
675                           FT_BYTES, BASE_NONE, NULL, 0x0,
676                           NULL, HFILL } },
677                 { &hf_netsync_cmd_data_type,
678                         { "Type", "netsync.cmd.data.type",
679                           FT_UINT8, BASE_DEC, NULL, 0x0,
680                           NULL, HFILL } },
681                 { &hf_netsync_cmd_data_id,
682                         { "ID", "netsync.cmd.data.id",
683                           FT_BYTES, BASE_NONE, NULL, 0x0,
684                           NULL, HFILL } },
685                 { &hf_netsync_cmd_data_compressed,
686                         { "Compressed", "netsync.cmd.data.compressed",
687                           FT_UINT8, BASE_DEC, NULL, 0x0,
688                           NULL, HFILL } },
689                 { &hf_netsync_cmd_data_payload,
690                         { "Payload", "netsync.cmd.data.payload",
691                           FT_BYTES, BASE_NONE, NULL, 0x0,
692                           NULL, HFILL } },
693                 { &hf_netsync_cmd_delta_type,
694                         { "Type", "netsync.cmd.delta.type",
695                           FT_UINT8, BASE_DEC, NULL, 0x0,
696                           NULL, HFILL } },
697                 { &hf_netsync_cmd_delta_base_id,
698                         { "Base ID", "netsync.cmd.delta.base_id",
699                           FT_BYTES, BASE_NONE, NULL, 0x0,
700                           NULL, HFILL } },
701                 { &hf_netsync_cmd_delta_ident_id,
702                         { "Ident ID", "netsync.cmd.delta.ident_id",
703                           FT_BYTES, BASE_NONE, NULL, 0x0,
704                           NULL, HFILL } },
705                 { &hf_netsync_cmd_delta_compressed,
706                         { "Compressed", "netsync.cmd.delta.compressed",
707                           FT_UINT8, BASE_DEC, NULL, 0x0,
708                           NULL, HFILL } },
709                 { &hf_netsync_cmd_delta_payload,
710                         { "Payload", "netsync.cmd.delta.payload",
711                           FT_BYTES, BASE_NONE, NULL, 0x0,
712                           NULL, HFILL } },
713                 { &hf_netsync_cmd_refine_tree_node,
714                         { "Tree Node", "netsync.cmd.refine.tree_node",
715                           FT_BYTES, BASE_NONE, NULL, 0x0,
716                           NULL, HFILL } },
717                 { &hf_netsync_cmd_send_delta_type,
718                         { "Type", "netsync.cmd.send_delta.type",
719                           FT_UINT8, BASE_DEC, NULL, 0x0,
720                           NULL, HFILL } },
721                 { &hf_netsync_cmd_send_delta_base_id,
722                         { "Base ID", "netsync.cmd.send_delta.base_id",
723                           FT_BYTES, BASE_NONE, NULL, 0x0,
724                           NULL, HFILL } },
725                 { &hf_netsync_cmd_send_delta_ident_id,
726                         { "Ident ID", "netsync.cmd.send_delta.ident_id",
727                           FT_BYTES, BASE_NONE, NULL, 0x0,
728                           NULL, HFILL } },
729                 { &hf_netsync_cmd_nonexistent_id,
730                         { "ID", "netsync.cmd.nonexistent.id",
731                           FT_BYTES, BASE_NONE, NULL, 0x0,
732                           NULL, HFILL } },
733                 { &hf_netsync_cmd_nonexistent_type,
734                         { "Type", "netsync.cmd.nonexistent.type",
735                           FT_UINT8, BASE_DEC, NULL, 0x0,
736                           NULL, HFILL } },
737                 { &hf_netsync_cmd_done_type,
738                         { "Type", "netsync.cmd.done.type",
739                           FT_UINT8, BASE_DEC, NULL, 0x0,
740                           NULL, HFILL } }
741
742
743         };
744
745         static gint *ett[] = {
746                 &ett_netsync,
747         };
748
749         module_t *netsync_module;
750
751         proto_netsync = proto_register_protocol("Monotone Netsync", "Netsync", "netsync");
752         proto_register_field_array(proto_netsync, hf, array_length(hf));
753         proto_register_subtree_array(ett, array_length(ett));
754
755         netsync_module = prefs_register_protocol(proto_netsync,
756                                                 proto_reg_handoff_netsync);
757
758         prefs_register_uint_preference(netsync_module, "tcp_port",
759                                         "Monotone Netsync TCP Port",
760                                         "The TCP port on which Monotone Netsync packets will be sent",
761                                         10, &global_tcp_port_netsync);
762
763
764         prefs_register_bool_preference(netsync_module, "desegment_netsync_messages",
765                 "Reassemble Netsync messages spanning multiple TCP segments",
766                 "Whether the Netsync dissector should reassemble messages spanning multiple TCP segments."
767                 " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
768                 &netsync_desegment);
769
770 }
771
772 void
773 proto_reg_handoff_netsync(void)
774 {
775         static dissector_handle_t netsync_handle;
776         static guint tcp_port_netsync;
777         static gboolean initialized = FALSE;
778
779         if (!initialized) {
780                 netsync_handle = create_dissector_handle(dissect_netsync, proto_netsync);
781                 initialized = TRUE;
782         } else {
783                 dissector_delete("tcp.port", tcp_port_netsync, netsync_handle);
784         }
785
786         tcp_port_netsync = global_tcp_port_netsync;
787         dissector_add("tcp.port", global_tcp_port_netsync, netsync_handle);
788 }
789