#include <epan/conversation.h> and/or #include <epan/emem.h> not req'd ...
[obnox/wireshark/wip.git] / epan / dissectors / packet-ymsg.c
1 /* packet-ymsg.c
2  * Routines for Yahoo Messenger YMSG protocol packet version 13 dissection
3  * Copyright 2003, Wayne Parrott <wayne_p@pacific.net.au>
4  * Copied from packet-yhoo.c and updated
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32
33 #include <string.h>
34 #include <glib.h>
35 #include <epan/packet.h>
36
37 #include "packet-tcp.h"
38 #include <epan/prefs.h>
39
40 static int proto_ymsg = -1;
41 static int hf_ymsg_version = -1;
42 static int hf_ymsg_len = -1;
43 static int hf_ymsg_service = -1;
44 static int hf_ymsg_status = -1;
45 static int hf_ymsg_session_id = -1;
46
47 static int hf_ymsg_content = -1;
48 static int hf_ymsg_content_line = -1;
49 static int hf_ymsg_content_line_key = -1;
50 static int hf_ymsg_content_line_value = -1;
51
52 static gint ett_ymsg = -1;
53 static gint ett_ymsg_content = -1;
54 static gint ett_ymsg_content_line = -1;
55
56 #define TCP_PORT_YMSG   23      /* XXX - this is Telnet! */
57 #define TCP_PORT_YMSG_2 25      /* And this is SMTP! */
58 #define TCP_PORT_YMSG_3 5050    /* This, however, is regular Yahoo Messenger */
59
60 /* desegmentation of YMSG over TCP */
61 static gboolean ymsg_desegment = TRUE;
62
63 /*
64  * This is from yahoolib2.c from libyahoo2.
65  *
66  * See also
67  *
68  *      http://libyahoo2.sourceforge.net/ymsg-9.txt
69  *
70  * and
71  *
72  *      http://www.venkydude.com/articles/yahoo.htm
73  *
74  * and
75  *
76  *      http://www.cse.iitb.ac.in/~varunk/YahooProtocol.htm
77  *
78  * and
79  *
80  *      http://www.geocrawler.com/archives/3/4893/2002/1/0/7459037/
81  *
82  * and
83  *
84  *      http://www.geocities.com/ziggycubbe/ym.html
85  */
86
87 /* Service constants */
88 enum yahoo_service { /* these are easier to see in hex */
89         YAHOO_SERVICE_LOGON = 1,
90         YAHOO_SERVICE_LOGOFF,
91         YAHOO_SERVICE_ISAWAY,
92         YAHOO_SERVICE_ISBACK,
93         YAHOO_SERVICE_IDLE, /* 5 (placemarker) */
94         YAHOO_SERVICE_MESSAGE,
95         YAHOO_SERVICE_IDACT,
96         YAHOO_SERVICE_IDDEACT,
97         YAHOO_SERVICE_MAILSTAT,
98         YAHOO_SERVICE_USERSTAT, /* 0xa */
99         YAHOO_SERVICE_NEWMAIL,
100         YAHOO_SERVICE_CHATINVITE,
101         YAHOO_SERVICE_CALENDAR,
102         YAHOO_SERVICE_NEWPERSONALMAIL,
103         YAHOO_SERVICE_NEWCONTACT,
104         YAHOO_SERVICE_ADDIDENT, /* 0x10 */
105         YAHOO_SERVICE_ADDIGNORE,
106         YAHOO_SERVICE_PING,
107         YAHOO_SERVICE_GOTGROUPRENAME, /* < 1, 36(old), 37(new) */
108         YAHOO_SERVICE_SYSMESSAGE = 0x14,
109         YAHOO_SERVICE_SKINNAME = 0x15,
110         YAHOO_SERVICE_PASSTHROUGH2 = 0x16,
111         YAHOO_SERVICE_CONFINVITE = 0x18,
112         YAHOO_SERVICE_CONFLOGON,
113         YAHOO_SERVICE_CONFDECLINE,
114         YAHOO_SERVICE_CONFLOGOFF,
115         YAHOO_SERVICE_CONFADDINVITE,
116         YAHOO_SERVICE_CONFMSG,
117         YAHOO_SERVICE_CHATLOGON,
118         YAHOO_SERVICE_CHATLOGOFF,
119         YAHOO_SERVICE_CHATMSG = 0x20,
120         YAHOO_SERVICE_GAMELOGON = 0x28,
121         YAHOO_SERVICE_GAMELOGOFF,
122         YAHOO_SERVICE_GAMEMSG = 0x2a,
123         YAHOO_SERVICE_FILETRANSFER = 0x46,
124         YAHOO_SERVICE_VOICECHAT = 0x4A,
125         YAHOO_SERVICE_NOTIFY,
126         YAHOO_SERVICE_VERIFY,
127         YAHOO_SERVICE_P2PFILEXFER,
128         YAHOO_SERVICE_PEERTOPEER = 0x4F,        /* Checks if P2P possible */
129         YAHOO_SERVICE_WEBCAM,
130         YAHOO_SERVICE_AUTHRESP = 0x54,
131         YAHOO_SERVICE_LIST,
132         YAHOO_SERVICE_AUTH = 0x57,
133         YAHOO_SERVICE_ADDBUDDY = 0x83,
134         YAHOO_SERVICE_REMBUDDY,
135         YAHOO_SERVICE_IGNORECONTACT,    /* > 1, 7, 13 < 1, 66, 13, 0*/
136         YAHOO_SERVICE_REJECTCONTACT,
137         YAHOO_SERVICE_GROUPRENAME = 0x89, /* > 1, 65(new), 66(0), 67(old) */
138         YAHOO_SERVICE_CHATONLINE = 0x96, /* > 109(id), 1, 6(abcde) < 0,1*/
139         YAHOO_SERVICE_CHATGOTO,
140         YAHOO_SERVICE_CHATJOIN, /* > 1 104-room 129-1600326591 62-2 */
141         YAHOO_SERVICE_CHATLEAVE,
142         YAHOO_SERVICE_CHATEXIT = 0x9b,
143         YAHOO_SERVICE_CHATADDINVITE = 0x9d,
144         YAHOO_SERVICE_CHATLOGOUT = 0xa0,
145         YAHOO_SERVICE_CHATPING,
146         YAHOO_SERVICE_COMMENT = 0xa8,
147         YAHOO_SERVICE_GAME_INVITE = 0xb7,
148         YAHOO_SERVICE_STEALTH_PERM = 0xb9,
149         YAHOO_SERVICE_STEALTH_SESSION = 0xba,
150         YAHOO_SERVICE_AVATAR = 0xbc,
151         YAHOO_SERVICE_PICTURE_CHECKSUM = 0xbd,
152         YAHOO_SERVICE_PICTURE = 0xbe,
153         YAHOO_SERVICE_PICTURE_UPDATE = 0xc1,
154         YAHOO_SERVICE_PICTURE_UPLOAD = 0xc2,
155         YAHOO_SERVICE_YAB_UPDATE = 0xc4,
156         YAHOO_SERVICE_Y6_VISIBLE_TOGGLE = 0xc5, /* YMSG13, key 13: 2 = invisible, 1 = visible */
157         YAHOO_SERVICE_Y6_STATUS_UPDATE = 0xc6,  /* YMSG13 */
158         YAHOO_SERVICE_PICTURE_STATUS = 0xc7,    /* YMSG13, key 213: 0 = none, 1 = avatar, 2 = picture */
159         YAHOO_SERVICE_VERIFY_ID_EXISTS = 0xc8,
160         YAHOO_SERVICE_AUDIBLE = 0xd0,
161         YAHOO_SERVICE_Y7_PHOTO_SHARING = 0xd2,
162         YAHOO_SERVICE_Y7_CONTACT_DETAILS = 0xd3,/* YMSG13 */
163         YAHOO_SERVICE_Y7_CHAT_SESSION = 0xd4,   
164         YAHOO_SERVICE_Y7_AUTHORIZATION = 0xd6,  /* YMSG13 */
165         YAHOO_SERVICE_Y7_FILETRANSFER = 0xdc,   /* YMSG13 */
166         YAHOO_SERVICE_Y7_FILETRANSFERINFO,      /* YMSG13 */
167         YAHOO_SERVICE_Y7_FILETRANSFERACCEPT,    /* YMSG13 */
168         YAHOO_SERVICE_Y7_MINGLE = 0xe1, /* YMSG13 */
169         YAHOO_SERVICE_Y7_CHANGE_GROUP = 0xe7, /* YMSG13 */
170         YAHOO_SERVICE_WEBLOGIN = 0x0226,
171         YAHOO_SERVICE_SMS_MSG = 0x02ea
172 };
173
174 /* Message flags */
175 enum yahoo_status {
176         YAHOO_STATUS_AVAILABLE = 0,
177         YAHOO_STATUS_BRB,
178         YAHOO_STATUS_BUSY,
179         YAHOO_STATUS_NOTATHOME,
180         YAHOO_STATUS_NOTATDESK,
181         YAHOO_STATUS_NOTINOFFICE,
182         YAHOO_STATUS_ONPHONE,
183         YAHOO_STATUS_ONVACATION,
184         YAHOO_STATUS_OUTTOLUNCH,
185         YAHOO_STATUS_STEPPEDOUT,
186         YAHOO_STATUS_INVISIBLE = 12,
187         YAHOO_STATUS_CUSTOM = 99,
188         YAHOO_STATUS_IDLE = 999,
189         YAHOO_STATUS_WEBLOGIN = 0x5a55aa55,
190         YAHOO_STATUS_OFFLINE = 0x5a55aa56, /* don't ask */
191         YAHOO_STATUS_TYPING = 0x16
192 };
193
194 enum ypacket_status {
195         YPACKET_STATUS_DISCONNECTED = -1,
196         YPACKET_STATUS_DEFAULT = 0,
197         YPACKET_STATUS_SERVERACK = 1,
198         YPACKET_STATUS_GAME     = 0x2,
199         YPACKET_STATUS_AWAY     = 0x4,
200         YPACKET_STATUS_CONTINUED = 0x5,
201         YPACKET_STATUS_INVISIBLE = 12,
202         YPACKET_STATUS_NOTIFY = 0x16, /* TYPING */
203         YPACKET_STATUS_WEBLOGIN = 0x5a55aa55,
204         YPACKET_STATUS_OFFLINE = 0x5a55aa56
205 };
206
207 struct yahoo_rawpacket
208 {
209         char ymsg[4];                   /* Packet identification string (YMSG) */
210         unsigned char version[4];       /* 4 bytes, little endian? */
211         unsigned char len[2];           /* length - little endian */
212         unsigned char service[2];       /* service - little endian */
213         unsigned char status[4];        /* Status - online, away etc.*/
214         unsigned char session_id[4];    /* Session ID */
215         char content[6];                /* 6 is the minimum size of the content */
216 };
217
218 static const value_string ymsg_service_vals[] = {
219         {YAHOO_SERVICE_LOGON, "Pager Logon"},
220         {YAHOO_SERVICE_LOGOFF, "Pager Logoff"},
221         {YAHOO_SERVICE_ISAWAY, "Is Away"},
222         {YAHOO_SERVICE_ISBACK, "Is Back"},
223         {YAHOO_SERVICE_IDLE, "Idle"},
224         {YAHOO_SERVICE_MESSAGE, "Message"},
225         {YAHOO_SERVICE_IDACT, "Activate Identity"},
226         {YAHOO_SERVICE_IDDEACT, "Deactivate Identity"},
227         {YAHOO_SERVICE_MAILSTAT, "Mail Status"},
228         {YAHOO_SERVICE_USERSTAT, "User Status"},
229         {YAHOO_SERVICE_NEWMAIL, "New Mail"},
230         {YAHOO_SERVICE_CHATINVITE, "Chat Invitation"},
231         {YAHOO_SERVICE_CALENDAR, "Calendar Reminder"},
232         {YAHOO_SERVICE_NEWPERSONALMAIL, "New Personals Mail"},
233         {YAHOO_SERVICE_NEWCONTACT, "New Friend"},
234         {YAHOO_SERVICE_ADDIDENT, "Add Identity"},
235         {YAHOO_SERVICE_ADDIGNORE, "Add Ignore"},
236         {YAHOO_SERVICE_PING, "Ping"},
237         {YAHOO_SERVICE_GOTGROUPRENAME, "Got Group Rename"},
238         {YAHOO_SERVICE_SYSMESSAGE, "System Message"},
239         {YAHOO_SERVICE_SKINNAME, "Skinname"},
240         {YAHOO_SERVICE_PASSTHROUGH2, "Passthrough 2"},
241         {YAHOO_SERVICE_CONFINVITE, "Conference Invitation"},
242         {YAHOO_SERVICE_CONFLOGON, "Conference Logon"},
243         {YAHOO_SERVICE_CONFDECLINE, "Conference Decline"},
244         {YAHOO_SERVICE_CONFLOGOFF, "Conference Logoff"},
245         {YAHOO_SERVICE_CONFADDINVITE, "Conference Additional Invitation"},
246         {YAHOO_SERVICE_CONFMSG, "Conference Message"},
247         {YAHOO_SERVICE_CHATLOGON, "Chat Logon"},
248         {YAHOO_SERVICE_CHATLOGOFF, "Chat Logoff"},
249         {YAHOO_SERVICE_CHATMSG, "Chat Message"},
250         {YAHOO_SERVICE_GAMELOGON, "Game Logon"},
251         {YAHOO_SERVICE_GAMELOGOFF, "Game Logoff"},
252         {YAHOO_SERVICE_GAMEMSG, "Game Message"},
253         {YAHOO_SERVICE_FILETRANSFER, "File Transfer"},
254         {YAHOO_SERVICE_VOICECHAT, "Voice Chat"},
255         {YAHOO_SERVICE_NOTIFY, "Notify"},
256         {YAHOO_SERVICE_VERIFY, "Verify"},
257         {YAHOO_SERVICE_P2PFILEXFER, "P2P File Transfer"}, 
258         {YAHOO_SERVICE_PEERTOPEER, "Peer To Peer"},
259         {YAHOO_SERVICE_WEBCAM, "WebCam"},
260         {YAHOO_SERVICE_AUTHRESP, "Authentication Response"},
261         {YAHOO_SERVICE_LIST, "List"},
262         {YAHOO_SERVICE_AUTH, "Authentication"},
263         {YAHOO_SERVICE_ADDBUDDY, "Add Buddy"},
264         {YAHOO_SERVICE_REMBUDDY, "Remove Buddy"},
265         {YAHOO_SERVICE_IGNORECONTACT, "Ignore Contact"},
266         {YAHOO_SERVICE_REJECTCONTACT, "Reject Contact"},
267         {YAHOO_SERVICE_GROUPRENAME, "Group Rename"},
268         {YAHOO_SERVICE_CHATONLINE, "Chat Online"},
269         {YAHOO_SERVICE_CHATGOTO, "Chat Goto"},
270         {YAHOO_SERVICE_CHATJOIN, "Chat Join"},
271         {YAHOO_SERVICE_CHATLEAVE, "Chat Leave"},
272         {YAHOO_SERVICE_CHATEXIT, "Chat Exit"},
273         {YAHOO_SERVICE_CHATADDINVITE, "Chat Invite"},
274         {YAHOO_SERVICE_CHATLOGOUT, "Chat Logout"},
275         {YAHOO_SERVICE_CHATPING, "Chat Ping"},
276         {YAHOO_SERVICE_COMMENT, "Comment"},
277         {YAHOO_SERVICE_GAME_INVITE,"Game Invite"},
278         {YAHOO_SERVICE_STEALTH_PERM, "Stealth Permanent"},
279         {YAHOO_SERVICE_STEALTH_SESSION, "Stealth Session"},
280         {YAHOO_SERVICE_AVATAR,"Avatar"},
281         {YAHOO_SERVICE_PICTURE_CHECKSUM,"Picture Checksum"},
282         {YAHOO_SERVICE_PICTURE,"Picture"},
283         {YAHOO_SERVICE_PICTURE_UPDATE,"Picture Update"},
284         {YAHOO_SERVICE_PICTURE_UPLOAD,"Picture Upload"},
285         {YAHOO_SERVICE_YAB_UPDATE,"Yahoo Address Book Update"},
286         {YAHOO_SERVICE_Y6_VISIBLE_TOGGLE, "Y6 Visibility Toggle"},
287         {YAHOO_SERVICE_Y6_STATUS_UPDATE, "Y6 Status Update"},
288         {YAHOO_SERVICE_PICTURE_STATUS, "Picture Sharing Status"},
289         {YAHOO_SERVICE_VERIFY_ID_EXISTS, "Verify ID Exists"},
290         {YAHOO_SERVICE_AUDIBLE, "Audible"},
291         {YAHOO_SERVICE_Y7_CONTACT_DETAILS,"Y7 Contact Details"},
292         {YAHOO_SERVICE_Y7_CHAT_SESSION, "Y7 Chat Session"},
293         {YAHOO_SERVICE_Y7_AUTHORIZATION,"Y7 Buddy Authorization"},
294         {YAHOO_SERVICE_Y7_FILETRANSFER,"Y7 File Transfer"},
295         {YAHOO_SERVICE_Y7_FILETRANSFERINFO,"Y7 File Transfer Information"},
296         {YAHOO_SERVICE_Y7_FILETRANSFERACCEPT,"Y7 File Transfer Accept"},
297         {YAHOO_SERVICE_Y7_CHANGE_GROUP, "Y7 Change Group"},
298         {YAHOO_SERVICE_WEBLOGIN,"WebLogin"},
299         {YAHOO_SERVICE_SMS_MSG,"SMS Message"},
300         {0, NULL}
301 };
302
303 static const value_string ymsg_status_vals[] = {
304         {YPACKET_STATUS_DISCONNECTED,"Disconnected"},
305         {YPACKET_STATUS_DEFAULT,"Default"},
306         {YPACKET_STATUS_SERVERACK,"Server Ack"},
307         {YPACKET_STATUS_GAME,"Playing Game"},
308         {YPACKET_STATUS_AWAY, "Away"},
309         {YPACKET_STATUS_CONTINUED,"More Packets??"},
310         {YPACKET_STATUS_NOTIFY, "Notify"},
311         {YPACKET_STATUS_WEBLOGIN,"Web Login"},
312         {YPACKET_STATUS_OFFLINE,"Offline"},
313         {0, NULL}
314 };
315
316 static guint get_ymsg_pdu_len(packet_info *pinfo, tvbuff_t *tvb, int offset);
317 static void dissect_ymsg_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
318
319 /* Find the end of the current content line and return its length */
320 static int get_content_item_length(tvbuff_t *tvb, int offset)
321 {
322         int origoffset = offset;
323
324         /* Keep reading until the magic delimiter (or end of tvb) is found */
325         while (tvb_length_remaining(tvb, offset) >= 2) {
326                 if (tvb_get_ntohs(tvb, offset) == 0xc080) {
327                         break;
328                 }
329                 offset++;
330         }
331         return offset - origoffset;
332 }
333
334
335 static gboolean
336 dissect_ymsg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
337 {
338
339   if (tvb_memeql(tvb, 0, "YMSG", 4) == -1) {
340     /* Not a Yahoo Messenger packet. */
341     return FALSE;
342   }
343   
344   tcp_dissect_pdus(tvb, pinfo, tree, ymsg_desegment, 8, get_ymsg_pdu_len,
345                    dissect_ymsg_pdu);
346   return TRUE;
347 }
348
349 static guint
350 get_ymsg_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
351 {
352   guint16 plen;
353
354   /*
355    * Get the length of the YMSG packet.
356    */
357   plen = tvb_get_ntohs(tvb, offset + 8);
358
359   /*
360    * That length doesn't include the length of the header itself; add that in.
361    */
362   return plen + 20;
363 }
364
365 static void
366 dissect_ymsg_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
367 {
368         proto_tree      *ymsg_tree, *ti;
369         proto_item      *content_item;
370         proto_tree      *content_tree;
371         char *keybuf;
372         char *valbuf;
373         int headersize = sizeof(struct yahoo_rawpacket)-6;
374         int keylen = 0;
375         int vallen = 0;
376         int offset = 0;
377         int content_len = 0;
378
379         if (check_col(pinfo->cinfo, COL_PROTOCOL))
380                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "YMSG");
381
382         offset = 0;
383         if (check_col(pinfo->cinfo, COL_INFO)) {
384                 col_add_fstr(pinfo->cinfo, COL_INFO,
385                         "%s (status=%s)   ",
386                         val_to_str(tvb_get_ntohs(tvb, offset + 10),
387                                  ymsg_service_vals, "Unknown Service: %u"),
388                         val_to_str(tvb_get_ntohl(tvb, offset + 12),
389                                  ymsg_status_vals, "Unknown Status: %u")
390                 );
391         }
392
393         if (tree) {
394                 ti = proto_tree_add_item(tree, proto_ymsg, tvb, offset, -1, FALSE);
395                 ymsg_tree = proto_item_add_subtree(ti, ett_ymsg);
396
397                 offset += 4; /* skip the YMSG string */
398
399                 /* Version */
400                 proto_tree_add_item(ymsg_tree, hf_ymsg_version, tvb, offset, 2, FALSE);
401                 offset += 2;
402
403                 offset += 2;    /* XXX - padding? */
404
405                 /* Length */
406                 content_len = tvb_get_ntohs(tvb, offset);
407                 proto_tree_add_item(ymsg_tree, hf_ymsg_len, tvb, offset, 2, FALSE);
408                 offset += 2;
409
410                 /* Service */
411                 proto_item_append_text(ti, " (%s)",
412                                        val_to_str(tvb_get_ntohs(tvb, offset),
413                                                   ymsg_service_vals,
414                                                   "Unknown"));
415
416                 proto_tree_add_item(ymsg_tree, hf_ymsg_service, tvb, offset, 2, FALSE);
417                 offset += 2;
418
419                 /* Status */
420                 proto_tree_add_item(ymsg_tree, hf_ymsg_status, tvb, offset, 4, FALSE);
421                 offset += 4;
422
423                 /* Session id */
424                 proto_tree_add_item(ymsg_tree, hf_ymsg_session_id, tvb, offset, 4, TRUE);
425                 offset += 4;
426
427                 /* Contents */
428                 if (content_len) {
429                         /* Create content subtree */
430                         content_item = proto_tree_add_item(ymsg_tree, hf_ymsg_content, tvb,
431                                                            offset, -1, TRUE);
432                         content_tree = proto_item_add_subtree(content_item, ett_ymsg_content);
433
434                         /* Each entry consists of:
435                            <key string> <delimiter> <value string> <delimiter>
436                         */
437                         
438                         /* Parse and show each line of the contents */
439                         for (;;)
440                         {
441                                 proto_item  *ti = NULL;
442                                 proto_tree  *content_line_tree = NULL;
443                                 
444                                 /* Don't continue unless there is room for another whole item.
445                                    (including 2 2-byte delimiters */
446                                 if (offset >= (headersize+content_len-4))
447                                 {
448                                         break;
449                                 }
450                                 
451                                 /* Get the length of the key */
452                                 keylen = get_content_item_length(tvb, offset);
453                                 /* Extract the key */
454                                 keybuf = tvb_format_text(tvb, offset, keylen);
455
456                                 /* Get the length of the value */
457                                 vallen = get_content_item_length(tvb, offset+keylen+2);
458                                 /* Extract the value */
459                                 valbuf = tvb_format_text(tvb, offset+keylen+2, vallen);
460
461                                 /* Add a text item with the key... */
462                                 ti =  proto_tree_add_string_format(content_tree, hf_ymsg_content_line, tvb,
463                                                                    offset, keylen+2+vallen+2,
464                                                                    "", "%s:%s", keybuf, valbuf);
465                                 content_line_tree = proto_item_add_subtree(ti, ett_ymsg_content_line);
466
467                                 /* And add the key and value separately inside */
468                                 proto_tree_add_item(content_line_tree, hf_ymsg_content_line_key, tvb,
469                                                     offset, keylen, FALSE);
470                                 proto_tree_add_item(content_line_tree, hf_ymsg_content_line_value, tvb,
471                                                     offset+keylen+2, vallen, FALSE);
472
473                                 /* Move beyone key and value lines */
474                                 offset += keylen+2+vallen+2;
475                         }
476                 }
477         }
478
479         col_set_fence(pinfo->cinfo, COL_INFO);
480
481         return;
482 }
483
484 void
485 proto_register_ymsg(void)
486 {
487         static hf_register_info hf[] = {
488                         { &hf_ymsg_version, {
489                                 "Version", "ymsg.version", FT_UINT16, BASE_DEC,
490                                 NULL, 0, "Packet version identifier", HFILL }},
491                         { &hf_ymsg_len, {
492                                 "Packet Length", "ymsg.len", FT_UINT16, BASE_DEC,
493                                 NULL, 0, "Packet Length", HFILL }},
494                         { &hf_ymsg_service, {
495                                 "Service", "ymsg.service", FT_UINT16, BASE_DEC,
496                                 VALS(ymsg_service_vals), 0, "Service Type", HFILL }},
497                         { &hf_ymsg_status, {
498                                 "Status", "ymsg.status", FT_UINT32, BASE_DEC,
499                                 VALS(ymsg_status_vals), 0, "Message Type Flags", HFILL }},
500                         { &hf_ymsg_session_id, {
501                                 "Session ID", "ymsg.session_id", FT_UINT32, BASE_HEX,
502                                 NULL, 0, "Connection ID", HFILL }},
503
504                         { &hf_ymsg_content, {
505                                 "Content", "ymsg.content", FT_STRING, BASE_NONE,
506                                 NULL, 0, "Data portion of the packet", HFILL }},
507                         { &hf_ymsg_content_line, {
508                                 "Content-line", "ymsg.content-line", FT_STRING, BASE_NONE,
509                                 NULL, 0, "Data portion of the packet", HFILL }},
510                         { &hf_ymsg_content_line_key, {
511                                 "Key", "ymsg.content-line.key", FT_STRING, BASE_NONE,
512                                 NULL, 0, "Content line key", HFILL }},
513                         { &hf_ymsg_content_line_value, {
514                                 "Value", "ymsg.content-line.value", FT_STRING, BASE_NONE,
515                                 NULL, 0, "Content line value", HFILL }}
516         };
517         static gint *ett[] = {
518                 &ett_ymsg,
519                 &ett_ymsg_content,
520                 &ett_ymsg_content_line
521         };
522         module_t *ymsg_module;
523
524         proto_ymsg = proto_register_protocol("Yahoo YMSG Messenger Protocol",
525             "YMSG", "ymsg");
526
527         proto_register_field_array(proto_ymsg, hf, array_length(hf));
528
529         proto_register_subtree_array(ett, array_length(ett));
530
531         ymsg_module = prefs_register_protocol(proto_ymsg, NULL);
532         prefs_register_bool_preference(ymsg_module, "desegment",
533                                        "Reassemble YMSG messages spanning multiple TCP segments",
534                                        "Whether the YMSG dissector should reassemble messages spanning multiple TCP segments. "
535                                        "To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
536                                        &ymsg_desegment);
537 }
538
539 void
540 proto_reg_handoff_ymsg(void)
541 {
542         /*
543          * DO NOT register for port 23, as that's Telnet, or for port
544          * 25, as that's SMTP.
545          *
546          * Also, DO NOT register for port 5050, as that's used by the
547          * old and new Yahoo messenger protocols.
548          *
549          * Just register as a heuristic TCP dissector, and reject stuff
550          * that doesn't begin with a YMSG signature.
551          */
552         heur_dissector_add("tcp", dissect_ymsg, proto_ymsg);
553 }