From Toralf Foerster via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=5376 :
[obnox/wireshark/wip.git] / epan / dissectors / packet-sametime.c
1 /* packet-sametime.c
2  * Routines for SAMETIME dissection
3  * Copyright 2010, Toralf Foerster <toralf.foerster [AT] gmx.de>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * Copied from WHATEVER_FILE_YOU_USED (where "WHATEVER_FILE_YOU_USED"
12  * is a dissector file; if you just copied this from README.developer,
13  * don't bother with the "Copied from" - you don't even need to put
14  * in a "Copied from" if you copied an existing dissector, especially
15  * if the bulk of the code in the new dissector is your code)
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, write to the Free Software Foundation, Inc.,
29  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <epan/packet.h>
37 #include <epan/dissectors/packet-tcp.h>
38 #include <epan/prefs.h>
39 #include <epan/tap.h>
40 #include <epan/stats_tree.h>
41
42 #define DEFAULT_SAMETIME_PORT   1533
43
44 static int proto_sametime = -1;
45 static guint global_sametime_port = DEFAULT_SAMETIME_PORT;
46 static dissector_handle_t sametime_handle;
47
48 /* Preference */
49 static gboolean global_sametime_show_length = FALSE;
50 static gboolean global_sametime_reassemble_packets = TRUE;
51
52 /*heart beat*/
53 static int hf_sametime_heartbeat = -1;
54
55 /*sametime message header*/
56 static int hf_sametime_message_length = -1;
57 static int hf_sametime_message_type = -1;
58 static int hf_sametime_message_options = -1;
59 static int hf_sametime_message_options_attribute = -1;
60 static int hf_sametime_message_options_encrypted = -1;
61 static int hf_sametime_message_channel = -1;
62
63 /*common types*/
64 static int hf_sametime_field_length = -1;
65 static int hf_sametime_field_text = -1;
66 static int hf_sametime_code = -1;
67 static int hf_sametime_login_type = -1;
68 static int hf_sametime_time = -1;
69
70 /*handshake*/
71 static int hf_sametime_handshake_major = -1;
72 static int hf_sametime_handshake_minor = -1;
73 static int hf_sametime_handshake_srvrcalc_addr = -1;
74 static int hf_sametime_handshake_loclcalc_addr = -1;
75
76 /*channel*/
77 static int hf_sametime_channel_service = -1;
78 static int hf_sametime_channel_id = -1;
79 static int hf_sametime_channel_send_type = -1;
80 static int hf_sametime_channel_awareness = -1;
81
82 /*user status*/
83 static int hf_sametime_user_status = -1;
84
85 /*location*/
86 static int hf_sametime_location_country = -1;
87 static int hf_sametime_location_postalcode = -1;
88 static int hf_sametime_location_province = -1;
89 static int hf_sametime_location_city = -1;
90 static int hf_sametime_location_phone = -1;
91 static int hf_sametime_location_name = -1;
92 static int hf_sametime_location_timezone = -1;
93
94 /*packet detail tree*/
95 static gint ett_sametime = -1;
96 static gint ett_sametime_options = -1;
97
98 /*statistics*/
99 static int sametime_tap = -1;
100 static const guint8* st_str_packet = "Sametime Message Count";
101 static const guint8* st_str_message_type = "Message Type";
102 static const guint8* st_str_send_type = "Send Type";
103 static const guint8* st_str_user_status = "User Status";
104 static int st_node_packet = -1;
105 static int st_node_message_type = -1;
106 static int st_node_send_type = -1;
107 static int st_node_user_status = -1;
108
109 typedef struct SametimeTap {
110         gint message_type;
111         gint send_type;
112         gint user_status;
113 } SametimeTap;
114
115 #define SAMETIME_MESSAGETYPE_HEARTBEAT          0x80
116 #define SAMETIME_MESSAGETYPE_HANDSHAKE          0x0000
117
118 #define SAMETIME_MESSAGETYPE_HANDSHAKE_ACK      0x8000
119 #define SAMETIME_MESSAGETYPE_LOGIN              0x0001
120 #define SAMETIME_MESSAGETYPE_LOGIN_ACK          0x8001
121 #define SAMETIME_MESSAGETYPE_LOGIN_REDIRECT     0x0018
122 #define SAMETIME_MESSAGETYPE_LOGIN_CONTINUE     0x0016
123
124 #define SAMETIME_MESSAGETYPE_CHANNEL_CREATE     0x0002
125 #define SAMETIME_MESSAGETYPE_CHANNEL_DESTROY    0x0003
126 #define SAMETIME_MESSAGETYPE_CHANNEL_SEND       0x0004
127 #define SAMETIME_MESSAGETYPE_CHANNEL_ACCEPT     0x0006
128
129 #define SAMETIME_MESSAGETYPE_SET_USER_STATUS    0x0009
130 #define SAMETIME_MESSAGETYPE_SET_PRIVACY_LIST   0x000b
131 #define SAMETIME_MESSAGETYPE_SENSE_SERVICE      0x0011
132 #define SAMETIME_MESSAGETYPE_ADMIN              0x0019
133 #define SAMETIME_MESSAGETYPE_ANNOUNCE           0x0022
134
135 static const value_string messagetypenames[] = {
136         {  SAMETIME_MESSAGETYPE_HEARTBEAT,              "HEARTBEAT" },
137
138         {  SAMETIME_MESSAGETYPE_HANDSHAKE,              "HANDSHAKE" },
139         {  SAMETIME_MESSAGETYPE_HANDSHAKE_ACK,          "HANDSHAKE_ACK" },
140         {  SAMETIME_MESSAGETYPE_LOGIN,                  "LOGIN" },
141         {  SAMETIME_MESSAGETYPE_LOGIN_ACK,              "LOGIN_ACK" },
142         {  SAMETIME_MESSAGETYPE_LOGIN_REDIRECT,         "LOGIN_REDIRECT" },
143         {  SAMETIME_MESSAGETYPE_LOGIN_CONTINUE,         "LOGIN_CONTINUE" },
144
145         {  SAMETIME_MESSAGETYPE_CHANNEL_CREATE,         "CHANNEL_CREATE" },
146         {  SAMETIME_MESSAGETYPE_CHANNEL_DESTROY,        "CHANNEL_DESTROY" },
147         {  SAMETIME_MESSAGETYPE_CHANNEL_SEND,           "CHANNEL_SEND" },
148         {  SAMETIME_MESSAGETYPE_CHANNEL_ACCEPT,         "CHANNEL_ACCEPT" },
149
150         {  SAMETIME_MESSAGETYPE_SET_USER_STATUS,        "SET_USER_STATUS" },
151         {  SAMETIME_MESSAGETYPE_SET_PRIVACY_LIST,       "SET_PRIVACY_LIST" },
152         {  SAMETIME_MESSAGETYPE_SENSE_SERVICE,          "SENSE_SERVICE" },
153         {  SAMETIME_MESSAGETYPE_ADMIN,                  "ADMIN" },
154         {  SAMETIME_MESSAGETYPE_ANNOUNCE,               "ANNOUNCE" },
155
156         { 0, NULL }
157 };
158
159 #define SAMETIME_MESSAGEOPTION_ENCRYPT          0x4000
160 #define SAMETIME_MESSAGEOPTION_HAS_ATTRIBS      0x8000
161
162 static const value_string optionnames[] = {
163         {  0x0,                                 "" },
164         {  SAMETIME_MESSAGEOPTION_ENCRYPT,      "ENCRYPT" },
165         {  SAMETIME_MESSAGEOPTION_HAS_ATTRIBS,  "HAS_ATTRIBS" },
166         { 0, NULL }
167 };
168
169 static const value_string userstatusnames[] = {
170         { 0x0020, "ACTIVE" },   /* I am available */
171         { 0x0040, "IDLE" },     /* ? */
172         { 0x0060, "AWAY" },     /* I am away */
173         { 0x0080, "BUSY" },     /* Please do not disturb me */
174
175         { 0x0008, "MEETING" },  /* I have a meeting */
176         { 0, NULL }
177 };
178
179 #define SAMETIME_SENDTYPE_AWARE_ADD             0x0068
180 #define SAMETIME_SENDTYPE_OPT_DO_SET            0x00c9
181 #define SAMETIME_SENDTYPE_AWARE_SNAPSHOT        0x01f4
182 #define SAMETIME_SENDTYPE_AWARE_UPDATE          0x01f5
183 #define SAMETIME_SENDTYPE_OPT_GOT_SET           0x0259
184
185 static const value_string sendtypenames[] = {
186         { SAMETIME_SENDTYPE_AWARE_ADD,          "AWARE_ADD" },
187         { 0x0069,                               "AWARE_REMOVE" },
188         { SAMETIME_SENDTYPE_OPT_DO_SET,         "OPT_DO_SET" },
189         { 0x00cb,                               "OPT_WATCH" },
190         { SAMETIME_SENDTYPE_AWARE_SNAPSHOT,     "AWARE_SNAPSHOT" },
191         { SAMETIME_SENDTYPE_AWARE_UPDATE,       "AWARE_UPDATE" },
192         { SAMETIME_SENDTYPE_OPT_GOT_SET,        "OPT_GOT_SET" },
193         { 0x025a,                               "?" },
194         { 0x025d,                               "OPT_DID_SET" },
195
196         { 0, NULL }
197 };
198
199 static const value_string awarenessnames[] = {
200         { 0x0002, "USER" },
201         { 0x0003, "GROUP" },
202         { 0x0008, "SERVER" },
203         { 0, NULL }
204 };
205
206 static const value_string codenames[] = {
207         { 0x00000011, "SERVICE_AWARE" },
208         { 0x00000015, "SERVICE_RESOLVE" },
209         { 0x00000018, "SERVICE_STORAGE" },
210         { 0x0000001a, "SERVICE_DIRECTORY" },
211
212         { 0x80000011, "ERR_USER_SKETCHY" },
213         { 0x80000015, "ERR_TOKEN_INVALID" },
214         { 0x80000018, "ERR_PORT_IN_USE" },
215         { 0x80000022, "ERR_CHANNEL_DESTROYED" },
216
217         { 0, NULL }
218 };
219
220
221 static int
222 add_text_item(tvbuff_t *tvb, proto_tree *tree, int offset, int hf)
223 {
224         guint16 length;
225
226         /* heuristic rule, string should start w/ valid character(s) */
227         if (! tvb_get_guint8(tvb, offset + 2))
228                 return 0;
229
230         length = tvb_get_ntohs(tvb, offset);
231         if (length)     {
232                 /* the string length must not exceed the packet length */
233                 if (length > tvb_length_remaining(tvb, offset + 2))
234                         return 0;
235
236                 /* add string length only if preferences is set */
237                 if (global_sametime_show_length)
238                         proto_tree_add_item(tree, hf_sametime_field_length, tvb, offset, 2, FALSE);
239
240                 /* add string */
241                 proto_tree_add_string(tree, hf, tvb, offset + 2, length, tvb_get_string(tvb, offset + 2, length));
242         }
243
244         return 2 + length;
245 }
246
247
248 static guint16
249 dissect_set_user_status(tvbuff_t *tvb, proto_tree *tree, int offset)
250 {
251         guint16 user_status;
252
253         user_status = tvb_get_ntohs(tvb, offset);
254         proto_item_append_text(tree, ", %s", val_to_str(user_status, userstatusnames, "0x%04x"));
255         proto_tree_add_item(tree, hf_sametime_user_status, tvb, offset, 2, FALSE);
256         offset += 2;
257         proto_tree_add_item(tree, hf_sametime_time, tvb, offset, 4, FALSE);
258         offset += 4;
259         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
260
261         return user_status;
262 }
263
264
265 static int
266 dissect_handshake(tvbuff_t *tvb, proto_tree *tree, int offset)
267 {
268         proto_tree_add_item(tree, hf_sametime_handshake_major, tvb, offset, 2, FALSE);
269         offset += 2;
270         proto_tree_add_item(tree, hf_sametime_handshake_minor, tvb, offset, 2, FALSE);
271         offset += 2;
272         offset += 4;
273         proto_tree_add_item(tree, hf_sametime_handshake_srvrcalc_addr, tvb, offset, 4, FALSE);
274         offset += 4;
275         proto_tree_add_item(tree, hf_sametime_login_type, tvb, offset, 2, FALSE);
276         offset += 2;
277         proto_tree_add_item(tree, hf_sametime_handshake_loclcalc_addr, tvb, offset, 4, FALSE);
278         offset += 4;
279         offset += 6;
280         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
281         offset += 8;
282         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
283
284         return offset;
285 }
286
287
288 static void
289 dissect_handshake_ack(tvbuff_t *tvb, proto_tree *tree, int offset)
290 {
291         proto_tree_add_item(tree, hf_sametime_handshake_major, tvb, offset, 2, FALSE);
292         offset += 2;
293         proto_tree_add_item(tree, hf_sametime_handshake_minor, tvb, offset, 2, FALSE);
294         offset += 2;
295         proto_tree_add_item(tree, hf_sametime_handshake_loclcalc_addr, tvb, offset, 4, FALSE);
296         offset += 4;
297         offset += 4;
298         offset += 4;
299         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
300 }
301
302
303 static void
304 dissect_login(tvbuff_t *tvb, proto_tree *tree, int offset)
305 {
306         offset += 2;
307         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
308 }
309
310
311 static void
312 dissect_login_redirect(tvbuff_t *tvb, proto_tree *tree, int offset)
313 {
314         offset += 2;
315         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
316 }
317
318
319 static void
320 dissect_login_ack(tvbuff_t *tvb, proto_tree *tree, int offset)
321 {
322         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
323         proto_tree_add_item(tree, hf_sametime_login_type, tvb, offset, 2, FALSE);
324         offset += 2;
325         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
326         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
327         offset += 3;
328         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
329         proto_tree_add_item(tree, hf_sametime_handshake_loclcalc_addr, tvb, offset, 4, FALSE);
330         offset += 4;
331         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
332         offset += 21;
333         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
334         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
335 }
336
337
338 static void
339 dissect_channel_create(tvbuff_t *tvb, proto_tree *tree, int offset)
340 {
341         offset += 4;
342         proto_tree_add_item(tree, hf_sametime_channel_id, tvb, offset, 4, FALSE);
343         offset += 4;
344         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
345         proto_tree_add_item(tree, hf_sametime_channel_service, tvb, offset, 4, FALSE);
346         offset += 4;
347         offset += 8;
348         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
349 }
350
351
352 static guint16
353 dissect_channel_send(tvbuff_t *tvb, proto_tree *tree, int offset)
354 {
355         guint16 send_type, awareness;
356         guint na;
357
358         send_type = tvb_get_ntohs(tvb, offset);
359         proto_item_append_text(tree, ", %s", val_to_str(send_type, sendtypenames, "0x%04x"));
360         proto_tree_add_item(tree, hf_sametime_channel_send_type, tvb, offset, 2, FALSE);
361         offset += 2;
362
363         switch (send_type)      {
364         case SAMETIME_SENDTYPE_AWARE_ADD:
365                 offset += 8;
366                 awareness = tvb_get_ntohs(tvb, offset);
367                 proto_item_append_text(tree, ", %s", val_to_str(awareness, awarenessnames, "0x%04x"));
368                 proto_tree_add_item(tree, hf_sametime_channel_awareness, tvb, offset, 2, FALSE);
369                 offset += 2;
370                 offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
371
372                 break;
373
374         case SAMETIME_SENDTYPE_OPT_DO_SET:
375                 offset += 20;
376                 na = tvb_get_ntohl(tvb, offset);
377                 offset += 4;
378                 if (na == 0x33) {
379                         offset += add_text_item(tvb, tree, offset, hf_sametime_location_country);
380                         offset += add_text_item(tvb, tree, offset, hf_sametime_location_postalcode);
381                         offset += add_text_item(tvb, tree, offset, hf_sametime_location_province);
382                         offset += add_text_item(tvb, tree, offset, hf_sametime_location_city);
383                         offset += add_text_item(tvb, tree, offset, hf_sametime_location_phone);
384                         offset += 1;
385                         offset += add_text_item(tvb, tree, offset, hf_sametime_location_name);
386                         offset += add_text_item(tvb, tree, offset, hf_sametime_location_timezone);
387                 } else  {
388                         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
389                 }
390
391                 break;
392
393         case SAMETIME_SENDTYPE_OPT_GOT_SET:
394                 offset += 8;
395                 awareness = tvb_get_ntohs(tvb, offset);
396                 proto_item_append_text(tree, ", %s", val_to_str(awareness, awarenessnames, "0x%04x"));
397                 proto_tree_add_item(tree, hf_sametime_channel_awareness, tvb, offset, 2, FALSE);
398                 offset += 2;
399                 while (tvb_length_remaining(tvb, offset) > 2)   {
400                         int n = add_text_item(tvb, tree, offset, hf_sametime_field_text);
401                         offset += (n) ? n : 1;
402                 }
403
404                 break;
405
406         case SAMETIME_SENDTYPE_AWARE_SNAPSHOT:
407                 offset += 12;
408                 awareness = tvb_get_ntohs(tvb, offset);
409                 proto_item_append_text(tree, ", %s", val_to_str(awareness, awarenessnames, "0x%04x"));
410                 proto_tree_add_item(tree, hf_sametime_channel_awareness, tvb, offset, 2, FALSE);
411                 offset += 2;
412                 offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
413
414                 break;
415
416         case SAMETIME_SENDTYPE_AWARE_UPDATE:
417                 offset += 4;
418                 offset += 4;
419                 awareness = tvb_get_ntohs(tvb, offset);
420                 proto_item_append_text(tree, ", %s", val_to_str(awareness, awarenessnames, "0x%04x"));
421                 proto_tree_add_item(tree, hf_sametime_channel_awareness, tvb, offset, 2, FALSE);
422                 offset += 2;
423                 offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
424                 offset += 4;
425                 if (tvb_get_guint8(tvb, offset))        {
426                         offset += 1;
427                         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
428                         dissect_set_user_status(tvb, tree, offset);
429                 }
430
431                 break;
432
433         case 0x0000:
434                 offset += 14;
435                 offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
436
437                 break;
438
439         case 0x0002:
440                 offset += 8;
441                 offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
442                 offset += 3;
443                 offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
444
445                 break;
446
447         case 0x0005:    /* XML */
448                 if (26 <= tvb_length_remaining(tvb, offset + 2))        {
449                         offset += 26;
450                         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
451                 }
452
453                 break;
454
455         case 0x0007:
456                 offset += 8;
457                 if (4 <= tvb_length_remaining(tvb, offset + 2)) {
458                         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
459                         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
460                         offset += 3;
461                         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
462                         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
463                 }
464
465                 break;
466
467         case 0x025a:
468                 offset += 10;
469                 offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
470
471                 break;
472
473         default:
474                 break;
475         }
476
477         return send_type;
478 }
479
480
481 static void
482 dissect_channel_accept(tvbuff_t *tvb, proto_tree *tree, int offset)
483 {
484         offset += 34;
485         if (tvb_length_remaining(tvb, offset + 2))      {
486                 offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
487                 if (tvb_get_guint8(tvb, offset))        {
488                         offset += 1;
489                         offset += add_text_item(tvb, tree, offset, hf_sametime_field_text);
490                         dissect_set_user_status(tvb, tree, offset);
491                 }
492         }
493 }
494
495
496 static void
497 dissect_sense_service(tvbuff_t *tvb, proto_tree *tree, int offset)
498 {
499         guint32 code;
500
501         code = tvb_get_ntohl(tvb, offset);
502         proto_item_append_text(tree, ", %s", val_to_str(code, codenames, "0x%04x"));
503         proto_tree_add_item(tree, hf_sametime_code, tvb, offset, 4, FALSE);
504         offset += 4;
505 }
506
507
508 /*
509         here we really dissect the message(s)
510 */
511 static void
512 dissect_sametime_content(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
513 {
514         proto_tree *sametime_tree;
515         proto_item *ti;
516         static SametimeTap *sinfo;
517         gint message_type;
518         int packet_length, offset = 0;
519
520         /* we expect either 1 heartbeat byte (0x80) or a sametime message */
521         packet_length = tvb_length_remaining(tvb, offset);
522         if (packet_length == 1) {
523                 message_type = tvb_get_guint8(tvb, 0);
524
525         } else if (packet_length < 12)  {
526                 message_type = -1;
527
528         } else  {
529                 message_type = tvb_get_ntohs(tvb, 4);
530         }
531
532         /* add message type */
533         col_append_str(pinfo->cinfo, COL_INFO, val_to_str(message_type, messagetypenames, "0x%04x"));
534         col_append_str(pinfo->cinfo, COL_INFO, " ");
535
536         /* message type statistic */
537         sinfo = ep_alloc(sizeof(struct SametimeTap ));
538         sinfo->message_type = message_type;
539         sinfo->send_type = -1;
540         sinfo->user_status = -1;
541
542         /* packet detail tree */
543         ti = proto_tree_add_item(tree, proto_sametime, tvb, offset, -1, FALSE);
544         sametime_tree = proto_item_add_subtree(ti, ett_sametime);
545         proto_item_append_text(sametime_tree, ", %s", val_to_str(message_type, messagetypenames, "0x%04x"));
546
547         /* dissect message */
548         if (message_type == SAMETIME_MESSAGETYPE_HEARTBEAT)     {
549                 proto_tree_add_item(sametime_tree, hf_sametime_heartbeat, tvb, offset, 1, FALSE);
550
551         } else if (message_type != -1)  {
552                 proto_tree *options_tree;
553                 proto_item *op;
554
555                 /* first 4 bytes gives the length of the sametime message */
556                 if (global_sametime_show_length)        {
557                         proto_tree_add_item(sametime_tree, hf_sametime_message_length, tvb, offset, 4, FALSE);
558                 }
559                 offset += 4;
560
561                 /* next 2 bytes gives the message type */
562                 proto_tree_add_item(sametime_tree, hf_sametime_message_type, tvb, offset, 2, FALSE);
563                 offset += 2;
564
565                 /* next 2 bytes are the message options */
566                 op = proto_tree_add_item(sametime_tree, hf_sametime_message_options, tvb, offset, 2, FALSE);
567                 options_tree = proto_item_add_subtree(op, ett_sametime_options);
568                 proto_tree_add_item(options_tree, hf_sametime_message_options_attribute, tvb, offset, 2, FALSE);
569                 proto_tree_add_item(options_tree, hf_sametime_message_options_encrypted, tvb, offset, 2, FALSE);
570                 offset += 2;
571
572                 /* next 4 bytes contains the channel id */
573                 proto_tree_add_item(sametime_tree, hf_sametime_message_channel, tvb, offset, 4, FALSE);
574                 offset += 4;
575
576                 switch (message_type)
577                 {
578                 case SAMETIME_MESSAGETYPE_HANDSHAKE:
579                         dissect_handshake(tvb, sametime_tree, offset);
580                         break;
581
582                 case SAMETIME_MESSAGETYPE_HANDSHAKE_ACK:
583                         dissect_handshake_ack(tvb, sametime_tree, offset);
584                         break;
585
586                 case SAMETIME_MESSAGETYPE_LOGIN:
587                         dissect_login(tvb, sametime_tree, offset);
588                         break;
589
590                 case SAMETIME_MESSAGETYPE_LOGIN_REDIRECT:
591                         dissect_login_redirect(tvb, sametime_tree, offset);
592                         break;
593
594                 case SAMETIME_MESSAGETYPE_LOGIN_ACK:
595                         dissect_login_ack(tvb, sametime_tree, offset);
596                         break;
597
598                 case SAMETIME_MESSAGETYPE_CHANNEL_CREATE:
599                         dissect_channel_create(tvb, sametime_tree, offset);
600                         break;
601
602                 case SAMETIME_MESSAGETYPE_CHANNEL_SEND:
603                         sinfo->send_type = dissect_channel_send(tvb, sametime_tree, offset);
604                         break;
605
606                 case SAMETIME_MESSAGETYPE_CHANNEL_ACCEPT:
607                         dissect_channel_accept(tvb, sametime_tree, offset);
608                         break;
609
610                 case SAMETIME_MESSAGETYPE_SET_USER_STATUS:
611                         sinfo->user_status = dissect_set_user_status(tvb, sametime_tree, offset);
612                         break;
613
614                 case SAMETIME_MESSAGETYPE_SENSE_SERVICE:
615                         dissect_sense_service(tvb, sametime_tree, offset);
616                         break;
617
618                 default:
619                         break;
620                 }
621         }
622
623         tap_queue_packet(sametime_tap, pinfo, sinfo);
624 }
625
626
627 /*
628         tick statistics
629 */
630 static int
631 sametime_stats_tree_packet(stats_tree* st, packet_info* pinfo _U_, epan_dissect_t* edt _U_, const void* p)
632 {
633         struct SametimeTap *pi = (struct SametimeTap *)p;
634
635         tick_stat_node(st, st_str_packet, 0, FALSE);
636         if (pi->message_type != -1)
637                 stats_tree_tick_pivot(st, st_node_message_type, val_to_str(pi->message_type, messagetypenames, "Unknown (0x%04x)"));
638
639         if (pi->send_type != -1)
640                 stats_tree_tick_pivot(st, st_node_send_type, val_to_str(pi->send_type, sendtypenames, "Unknown (0x%04x)"));
641
642         if (pi->user_status != -1)
643                 stats_tree_tick_pivot(st, st_node_user_status, val_to_str(pi->user_status, userstatusnames, "Unknown (0x%04x)"));
644
645         return 1;
646 }
647
648
649 /*
650         init statistic
651 */
652 static void
653 sametime_stats_tree_init(stats_tree* st)
654 {
655         st_node_packet = stats_tree_create_node(st, st_str_packet, 0, TRUE);
656         st_node_message_type = stats_tree_create_pivot(st, st_str_message_type, st_node_packet);
657         st_node_send_type = stats_tree_create_pivot(st, st_str_send_type, st_node_packet);
658         st_node_user_status = stats_tree_create_pivot(st, st_str_user_status, st_node_packet);
659 }
660
661
662 /*
663         length of the sametime message
664 */
665 static guint
666 get_sametime_message_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
667 {       guint32 N = tvb_length_remaining(tvb, offset);
668
669         return (N < 4) ? N : tvb_get_ntohl(tvb, offset) + 4;
670 }
671
672
673 /*
674         the dissector itself
675 */
676 static void
677 dissect_sametime(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
678 {
679         col_set_str(pinfo->cinfo, COL_PROTOCOL, "SAMETIME");
680         col_clear(pinfo->cinfo,COL_INFO);
681
682         tcp_dissect_pdus(tvb, pinfo, tree, global_sametime_reassemble_packets, 4,
683                          get_sametime_message_len, dissect_sametime_content);
684 }
685
686
687 void
688 proto_register_sametime(void)
689 {
690         static hf_register_info hf[] = {
691                 /*tcp payload is one byte : SAMETIME_MESSAGETYPE_HEARTBEAT*/
692                 { &hf_sametime_heartbeat,
693                         { "heartbeat", "sametime.heartbeat",
694                         FT_UINT8, BASE_HEX,
695                         NULL, 0x0,
696                         NULL, HFILL }
697                 },
698
699                 /*sametime message header*/
700                 { &hf_sametime_message_length,
701                         { "msg length", "sametime.message_length",
702                         FT_UINT32, BASE_DEC,
703                         NULL, 0,
704                         NULL, HFILL }
705                 },
706                 { &hf_sametime_message_type,
707                         { "msg type", "sametime.message_type",
708                         FT_UINT16, BASE_HEX,
709                         VALS(messagetypenames), 0x0,
710                         NULL, HFILL }
711                 },
712                 { &hf_sametime_message_options,
713                         { "msg options", "sametime.message_options",
714                         FT_UINT16, BASE_HEX,
715                         VALS(optionnames), 0x0,
716                         NULL, HFILL }
717                 },
718                 { &hf_sametime_message_options_encrypted,
719                         { "ENCRYPT", "sametime.message_options.encrypted",
720                         FT_BOOLEAN, 16,
721                         NULL, SAMETIME_MESSAGEOPTION_ENCRYPT,
722                         NULL, HFILL }
723                 },
724                 { &hf_sametime_message_options_attribute,
725                         { "HAS_ATTRIBS", "sametime.message_options.attribute",
726                         FT_BOOLEAN, 16,
727                         NULL, SAMETIME_MESSAGEOPTION_HAS_ATTRIBS,
728                         NULL, HFILL }
729                 },
730                 { &hf_sametime_message_channel,
731                         { "msg channel", "sametime.message_channel",
732                         FT_UINT32, BASE_DEC,
733                         NULL, 0,
734                         NULL, HFILL }
735                 },
736
737                 /*common fields within various message types*/
738                 { &hf_sametime_field_length,
739                         { "length", "sametime.field_length",
740                         FT_UINT16, BASE_DEC,
741                         NULL, 0,
742                         NULL, HFILL }
743                 },
744                 { &hf_sametime_field_text,
745                         { "text", "sametime.field_text",
746                         FT_STRING, BASE_NONE,
747                         NULL, 0x0,
748                         NULL, HFILL }
749                 },
750                 { &hf_sametime_code,
751                         { "code", "sametime.code",
752                         FT_UINT32, BASE_HEX,
753                         VALS(codenames), 0x0,
754                         NULL, HFILL }
755                 },
756                 { &hf_sametime_login_type,
757                         { "login type", "sametime.login_type",
758                         FT_UINT16, BASE_HEX,
759                         NULL, 0x0,
760                         NULL, HFILL }
761                 },
762                 { &hf_sametime_time,
763                         { "time", "sametime.time",
764                         FT_UINT32, BASE_DEC,
765                         NULL, 0,
766                         NULL, HFILL }
767                 },
768
769                 /*type handshake*/
770                 { &hf_sametime_handshake_major,
771                         { "major", "sametime.handshake.major",
772                         FT_UINT16, BASE_HEX,
773                         NULL, 0x0,
774                         NULL, HFILL }
775                 },
776                 { &hf_sametime_handshake_minor,
777                         { "minor", "sametime.handshake.minor",
778                         FT_UINT16, BASE_HEX,
779                         NULL, 0x0,
780                         NULL, HFILL }
781                 },
782                 { &hf_sametime_handshake_srvrcalc_addr,
783                         { "srvr", "sametime.handshake.srvrcalc_addr",
784                         FT_IPv4, BASE_NONE,
785                         NULL, 0x0,
786                         NULL, HFILL }
787                 },
788                 { &hf_sametime_handshake_loclcalc_addr,
789                         { "locl", "sametime.handshake.loclcalc_addr",
790                         FT_IPv4, BASE_NONE,
791                         NULL, 0x0,
792                         NULL, HFILL }
793                 },
794
795                 /*type channel*/
796                 { &hf_sametime_channel_service,
797                         { "service id", "sametime.channel.service",
798                         FT_UINT32, BASE_DEC,
799                         NULL, 0,
800                         NULL, HFILL }
801                 },
802                 { &hf_sametime_channel_id,
803                         { "channel id", "sametime.channel.id",
804                         FT_UINT32, BASE_DEC,
805                         NULL, 0,
806                         NULL, HFILL }
807                 },
808                 { &hf_sametime_channel_send_type,
809                         { "send type", "sametime.channel.send_type",
810                         FT_UINT16, BASE_HEX,
811                         VALS(sendtypenames), 0,
812                         NULL, HFILL }
813                 },
814                 { &hf_sametime_channel_awareness,
815                         { "awareness", "sametime.channel.awareness",
816                         FT_UINT16, BASE_HEX,
817                         VALS(awarenessnames), 0x0,
818                         NULL, HFILL }
819                 },
820
821                 /*type user status*/
822                 { &hf_sametime_user_status,
823                         { "user status", "sametime.user_status_type",
824                         FT_UINT16, BASE_HEX,
825                         VALS(userstatusnames), 0x0,
826                         NULL, HFILL }
827                 },
828
829                  /*type location*/
830                 { &hf_sametime_location_name,
831                         { "name", "sametime.location.name",
832                         FT_STRING, BASE_NONE,
833                         NULL, 0x0,
834                         NULL, HFILL }
835                 },
836                 { &hf_sametime_location_city,
837                         { "city", "sametime.location.city",
838                         FT_STRING, BASE_NONE,
839                         NULL, 0x0,
840                         NULL, HFILL }
841                 },
842                 { &hf_sametime_location_province,
843                         { "province", "sametime.location.province",
844                         FT_STRING, BASE_NONE,
845                         NULL, 0x0,
846                         NULL, HFILL }
847                 },
848                 { &hf_sametime_location_postalcode,
849                         { "postal code", "sametime.location.postalcode",
850                         FT_STRING, BASE_NONE,
851                         NULL, 0x0,
852                         NULL, HFILL }
853                 },
854                 { &hf_sametime_location_country,
855                         { "country", "sametime.location.country",
856                         FT_STRING, BASE_NONE,
857                         NULL, 0x0,
858                         NULL, HFILL }
859                 },
860                 { &hf_sametime_location_phone,
861                         { "phone", "sametime.location.phone",
862                         FT_STRING, BASE_NONE,
863                         NULL, 0x0,
864                         NULL, HFILL }
865                 },
866                 { &hf_sametime_location_timezone,
867                         { "time zone", "sametime.location.timezone",
868                         FT_STRING, BASE_NONE,
869                         NULL, 0x0,
870                         NULL, HFILL }
871                 },
872         };
873
874         static gint *ett[] = {
875                 &ett_sametime,
876                 &ett_sametime_options
877         };
878
879         module_t *sametime_module;
880
881         proto_sametime = proto_register_protocol (
882                 "Sametime Protocol", /* name */
883                 "SAMETIME",          /* short name */
884                 "sametime"           /* abbrev */
885                 );
886         proto_register_field_array(proto_sametime, hf, array_length(hf));
887         proto_register_subtree_array(ett, array_length(ett));
888
889         sametime_tap = register_tap("sametime");
890
891         /* Preference setting */
892         sametime_module = prefs_register_protocol(proto_sametime, NULL);
893         prefs_register_bool_preference(sametime_module, "show_length",
894                                        "Show length",
895                                        "Show length of text field",
896                                        &global_sametime_show_length);
897         prefs_register_bool_preference(sametime_module, "reassemble",
898                                        "Reassemble","reassemble packets",
899                                        &global_sametime_reassemble_packets);
900         prefs_register_uint_preference(sametime_module, "tcp_port",
901                                        "SAMETIME port number",
902                                        "port number for sametime traffic",
903                                        10, &global_sametime_port);
904 }
905
906 /*
907         create / register
908 */
909 void
910 proto_reg_handoff_sametime(void)
911 {
912         static gboolean initialized = FALSE;
913         static guint saved_sametime_tcp_port;
914
915         if (!initialized) {
916                 sametime_handle = create_dissector_handle(dissect_sametime, proto_sametime);
917                 initialized = TRUE;
918         } else {
919                 dissector_delete("tcp.port", saved_sametime_tcp_port, sametime_handle);
920         }
921
922         dissector_add("tcp.port", global_sametime_port, sametime_handle);
923         saved_sametime_tcp_port = global_sametime_port;
924
925         stats_tree_register("sametime", "sametime", "Sametime/Messages", 0,
926                             sametime_stats_tree_packet,
927                             sametime_stats_tree_init, NULL );
928 }
929