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