Removed some "initializer does not fit or is out of range" warnings.
[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         if (check_col(pinfo->cinfo,COL_PROTOCOL)) {
470                 col_set_str(pinfo->cinfo,COL_PROTOCOL,"Netsync");
471         }
472
473
474         if (tree == NULL)
475                 return;
476
477         while (tvb_reported_length_remaining(tvb, offset)  > 0) {
478                 ti = proto_tree_add_item(tree, proto_netsync, tvb, offset, -1, FALSE);
479                 netsync_tree = proto_item_add_subtree(ti, ett_netsync);
480
481                 version = tvb_get_guint8(tvb, offset);
482                 proto_tree_add_item(netsync_tree, hf_netsync_version, tvb,
483                                         offset, 1, FALSE );
484                 offset += 1;
485
486                 cmd = tvb_get_guint8(tvb, offset);
487                 proto_tree_add_item(netsync_tree, hf_netsync_command, tvb,
488                                         offset, 1, FALSE );
489                 offset += 1;
490
491
492                 /* get size */
493                 size = 0;
494                 size_bytes = 0;
495                 shift = 0;
496                 do {
497                         tmp = tvb_get_guint8(tvb, offset + size_bytes);
498                         size_bytes += 1;
499
500                         size |= (tmp & 0x7F) << shift;
501                         shift += 7;
502                 } while (tmp & 0x80);
503
504
505                 proto_tree_add_uint(netsync_tree, hf_netsync_size, tvb,
506                                         offset, size_bytes, size );
507                 offset += size_bytes;
508
509                 switch (cmd) {
510                         case NETSYNC_CMD_DONE:
511                                 dissect_netsync_cmd_done( tvb, offset, netsync_tree, size );
512                                 break;
513
514                         case NETSYNC_CMD_ERROR:
515                                 dissect_netsync_cmd_error( tvb, offset, netsync_tree, size );
516                                 break;
517
518                         case NETSYNC_CMD_BYE:
519                                 dissect_netsync_cmd_bye( tvb, offset, netsync_tree, size );
520                                 break;
521
522                         case NETSYNC_CMD_HELLO:
523                                 dissect_netsync_cmd_hello( tvb, offset, netsync_tree, size );
524                                 break;
525
526                         case NETSYNC_CMD_ANONYMOUS:
527                                 dissect_netsync_cmd_anonymous( tvb, offset, netsync_tree, size );
528                                 break;
529
530                         case NETSYNC_CMD_AUTH:
531                                 dissect_netsync_cmd_auth( tvb, offset, netsync_tree, size );
532                                 break;
533
534                         case NETSYNC_CMD_CONFIRM:
535                                 dissect_netsync_cmd_confirm( tvb, offset, netsync_tree, size );
536                                 break;
537
538                         case NETSYNC_CMD_REFINE:
539                                 dissect_netsync_cmd_refine( tvb, offset, netsync_tree, size );
540                                 break;
541
542                         case NETSYNC_CMD_SEND_DATA:
543                                 dissect_netsync_cmd_send_data( tvb, offset, netsync_tree, size );
544                                 break;
545
546                         case NETSYNC_CMD_SEND_DELTA:
547                                 dissect_netsync_cmd_send_delta( tvb, offset, netsync_tree, size );
548                                 break;
549
550                         case NETSYNC_CMD_DATA:
551                                 dissect_netsync_cmd_data( tvb, offset, netsync_tree, size );
552                                 break;
553
554                         case NETSYNC_CMD_DELTA:
555                                 dissect_netsync_cmd_delta( tvb, offset, netsync_tree, size );
556                                 break;
557
558                         case NETSYNC_CMD_NONEXISTENT:
559                                 dissect_netsync_cmd_nonexistent( tvb, offset, netsync_tree, size );
560                                 break;
561
562                         default:
563                                 proto_tree_add_item(netsync_tree, hf_netsync_data, tvb,
564                                         offset, size, FALSE );
565                                 break;
566                 }
567
568                 offset += size;
569
570                 proto_tree_add_item(netsync_tree, hf_netsync_checksum, tvb,
571                                         offset, 4, FALSE );
572                 offset += 4;
573
574
575                 proto_item_append_text(netsync_tree, " V%d, Cmd: %s (%d), Size: %d",
576                                         version, val_to_str(cmd, netsync_cmd_vals, "(0x%x)"), cmd, size );
577
578                 proto_item_set_len(netsync_tree, 1+1+size_bytes+size+4);
579         }
580 }
581
582 static void
583 dissect_netsync(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
584 {
585         tcp_dissect_pdus(tvb, pinfo, tree, netsync_desegment, 7, get_netsync_pdu_len,
586                                         dissect_netsync_pdu);
587 }
588
589 void
590 proto_register_netsync(void)
591 {
592         static hf_register_info hf[] = {
593                 /* General */
594                 { &hf_netsync_version,
595                         { "Version", "netsync.version",
596                           FT_UINT8, BASE_DEC, NULL, 0x0,
597                           "Version", HFILL } },
598                 { &hf_netsync_command,
599                         { "Command", "netsync.command",
600                           FT_UINT8, BASE_HEX, VALS(netsync_cmd_vals), 0x0,
601                           "Command", HFILL } },
602                 { &hf_netsync_size,
603                         { "Size", "netsync.size",
604                           FT_UINT32, BASE_DEC, NULL, 0x0,
605                           "Size", HFILL } },
606                 { &hf_netsync_data,
607                         { "Data", "netsync.data",
608                           FT_BYTES, BASE_HEX, NULL, 0x0,
609                           "Data", HFILL } },
610                 { &hf_netsync_checksum,
611                         { "Checksum", "netsync.checksum",
612                           FT_UINT32, BASE_HEX, NULL, 0x0,
613                           "Checksum", HFILL } },
614                 { &hf_netsync_cmd_hello_keyname,
615                         { "Key Name", "netsync.cmd.hello.keyname",
616                           FT_STRING, BASE_HEX, NULL, 0x0,
617                           "Key Name", HFILL } },
618                 { &hf_netsync_cmd_hello_key,
619                         { "Key", "netsync.cmd.hello.key",
620                           FT_BYTES, BASE_HEX, NULL, 0x0,
621                           "Key", HFILL } },
622                 { &hf_netsync_cmd_nonce,
623                         { "Nonce", "netsync.cmd.nonce",
624                           FT_BYTES, BASE_HEX, NULL, 0x0,
625                           "Nonce", HFILL } },
626                 { &hf_netsync_cmd_anonymous_role,
627                         { "Role", "netsync.cmd.anonymous.role",
628                           FT_UINT8, BASE_DEC, VALS(netsync_role_vals), 0x0,
629                           "Role", HFILL } },
630                 { &hf_netsync_cmd_anonymous_collection,
631                         { "Collection", "netsync.cmd.anonymous.collection",
632                           FT_STRING, BASE_DEC, NULL, 0x0,
633                           "Collection", HFILL } },
634                 { &hf_netsync_cmd_confirm_sig,
635                         { "Signature", "netsync.cmd.confirm.signature",
636                           FT_BYTES, BASE_HEX, NULL, 0x0,
637                           "Signature", HFILL } },
638                 { &hf_netsync_cmd_send_data_type,
639                         { "Type", "netsync.cmd.send_data.type",
640                           FT_UINT8, BASE_DEC, NULL, 0x0,
641                           "Type", HFILL } },
642                 { &hf_netsync_cmd_send_data_id,
643                         { "ID", "netsync.cmd.send_data.id",
644                           FT_BYTES, BASE_HEX, NULL, 0x0,
645                           "ID", HFILL } },
646                 { &hf_netsync_cmd_error_msg,
647                         { "Message", "netsync.cmd.error.msg",
648                           FT_STRING, BASE_DEC, NULL, 0x0,
649                           "Message", HFILL } },
650
651                 { &hf_netsync_cmd_done_level,
652                         { "Level", "netsync.cmd.done.level",
653                           FT_UINT32, BASE_DEC, NULL, 0x0,
654                           "Level", HFILL } },
655                 { &hf_netsync_cmd_auth_role,
656                         { "Role", "netsync.cmd.auth.role",
657                           FT_UINT8, BASE_DEC, VALS(netsync_role_vals), 0x0,
658                           "Role", HFILL } },
659                 { &hf_netsync_cmd_auth_collection,
660                         { "Collection", "netsync.cmd.auth.collection",
661                           FT_STRING, BASE_DEC, NULL, 0x0,
662                           "Collection", HFILL } },
663                 { &hf_netsync_cmd_auth_id,
664                         { "ID", "netsync.cmd.auth.id",
665                           FT_BYTES, BASE_DEC, NULL, 0x0,
666                           "ID", HFILL } },
667                 { &hf_netsync_cmd_auth_nonce1,
668                         { "Nonce 1", "netsync.cmd.auth.nonce1",
669                           FT_BYTES, BASE_DEC, NULL, 0x0,
670                           "Nonce 1", HFILL } },
671                 { &hf_netsync_cmd_auth_nonce2,
672                         { "Nonce 2", "netsync.cmd.auth.nonce2",
673                           FT_BYTES, BASE_DEC, NULL, 0x0,
674                           "Nonce 2", HFILL } },
675                 { &hf_netsync_cmd_auth_sig,
676                         { "Signature", "netsync.cmd.auth.sig",
677                           FT_BYTES, BASE_DEC, NULL, 0x0,
678                           "Signature", HFILL } },
679                 { &hf_netsync_cmd_data_type,
680                         { "Type", "netsync.cmd.data.type",
681                           FT_UINT8, BASE_DEC, NULL, 0x0,
682                           "Type", HFILL } },
683                 { &hf_netsync_cmd_data_id,
684                         { "ID", "netsync.cmd.data.id",
685                           FT_BYTES, BASE_DEC, NULL, 0x0,
686                           "ID", HFILL } },
687                 { &hf_netsync_cmd_data_compressed,
688                         { "Compressed", "netsync.cmd.data.compressed",
689                           FT_UINT8, BASE_DEC, NULL, 0x0,
690                           "Compressed", HFILL } },
691                 { &hf_netsync_cmd_data_payload,
692                         { "Payload", "netsync.cmd.data.payload",
693                           FT_BYTES, BASE_DEC, NULL, 0x0,
694                           "Payload", HFILL } },
695                 { &hf_netsync_cmd_delta_type,
696                         { "Type", "netsync.cmd.delta.type",
697                           FT_UINT8, BASE_DEC, NULL, 0x0,
698                           "Type", HFILL } },
699                 { &hf_netsync_cmd_delta_base_id,
700                         { "Base ID", "netsync.cmd.delta.base_id",
701                           FT_BYTES, BASE_DEC, NULL, 0x0,
702                           "Base ID", HFILL } },
703                 { &hf_netsync_cmd_delta_ident_id,
704                         { "Ident ID", "netsync.cmd.delta.ident_id",
705                           FT_BYTES, BASE_DEC, NULL, 0x0,
706                           "Ident ID", HFILL } },
707                 { &hf_netsync_cmd_delta_compressed,
708                         { "Compressed", "netsync.cmd.delta.compressed",
709                           FT_UINT8, BASE_DEC, NULL, 0x0,
710                           "Compressed", HFILL } },
711                 { &hf_netsync_cmd_delta_payload,
712                         { "Payload", "netsync.cmd.delta.payload",
713                           FT_BYTES, BASE_DEC, NULL, 0x0,
714                           "Payload", HFILL } },
715                 { &hf_netsync_cmd_refine_tree_node,
716                         { "Tree Node", "netsync.cmd.refine.tree_node",
717                           FT_BYTES, BASE_DEC, NULL, 0x0,
718                           "Tree Node", HFILL } },
719                 { &hf_netsync_cmd_send_delta_type,
720                         { "Type", "netsync.cmd.send_delta.type",
721                           FT_UINT8, BASE_DEC, NULL, 0x0,
722                           "Type", HFILL } },
723                 { &hf_netsync_cmd_send_delta_base_id,
724                         { "Base ID", "netsync.cmd.send_delta.base_id",
725                           FT_BYTES, BASE_DEC, NULL, 0x0,
726                           "Base ID", HFILL } },
727                 { &hf_netsync_cmd_send_delta_ident_id,
728                         { "Ident ID", "netsync.cmd.send_delta.ident_id",
729                           FT_BYTES, BASE_DEC, NULL, 0x0,
730                           "Ident ID", HFILL } },
731                 { &hf_netsync_cmd_nonexistent_id,
732                         { "ID", "netsync.cmd.nonexistent.id",
733                           FT_BYTES, BASE_DEC, NULL, 0x0,
734                           "ID", HFILL } },
735                 { &hf_netsync_cmd_nonexistent_type,
736                         { "Type", "netsync.cmd.nonexistent.type",
737                           FT_UINT8, BASE_DEC, NULL, 0x0,
738                           "Type", HFILL } },
739                 { &hf_netsync_cmd_done_type,
740                         { "Type", "netsync.cmd.done.type",
741                           FT_UINT8, BASE_DEC, NULL, 0x0,
742                           "Type", HFILL } }
743
744
745         };
746
747         static gint *ett[] = {
748                 &ett_netsync,
749         };
750
751         module_t *netsync_module;
752
753         proto_netsync = proto_register_protocol("Monotone Netsync", "Netsync", "netsync");
754         proto_register_field_array(proto_netsync, hf, array_length(hf));
755         proto_register_subtree_array(ett, array_length(ett));
756
757         netsync_module = prefs_register_protocol(proto_netsync,
758                                                 proto_reg_handoff_netsync);
759
760         prefs_register_uint_preference(netsync_module, "tcp_port",
761                                         "Monotone Netsync TCP Port",
762                                         "The TCP port on which Monotone Netsync packets will be sent",
763                                         10, &global_tcp_port_netsync);
764
765
766         prefs_register_bool_preference(netsync_module, "desegment_netsync_messages",
767                 "Reassemble Netsync messages spanning multiple TCP segments",
768                 "Whether the Netsync dissector should reassemble messages spanning multiple TCP segments."
769                 " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
770                 &netsync_desegment);
771
772 }
773
774 void
775 proto_reg_handoff_netsync(void)
776 {
777         static dissector_handle_t netsync_handle;
778         static guint tcp_port_netsync;
779         static gboolean initialized = FALSE;
780
781         if (!initialized) {
782                 netsync_handle = create_dissector_handle(dissect_netsync, proto_netsync);
783                 initialized = TRUE;
784         } else {
785                 dissector_delete("tcp.port", tcp_port_netsync, netsync_handle);
786         }
787
788         tcp_port_netsync = global_tcp_port_netsync;
789         dissector_add("tcp.port", global_tcp_port_netsync, netsync_handle);
790 }
791