Added tap functionality to UDP
[obnox/wireshark/wip.git] / packet-ymsg.c
1 /* packet-ymsg.c
2  * Routines for Yahoo Messenger YMSG protocol packet version 9 dissection
3  * Copyright 2003, Wayne Parrott <wayne_p@pacific.net.au>
4  * Copied from packet-yhoo.c and updated
5  *
6  * $Id: packet-ymsg.c,v 1.2 2003/03/01 04:12:06 guy Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
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
37 static int proto_ymsg = -1;
38 static int hf_ymsg_version = -1;
39 static int hf_ymsg_len = -1;
40 static int hf_ymsg_service = -1;
41 static int hf_ymsg_status = -1;
42 static int hf_ymsg_session_id = -1;
43 static int hf_ymsg_content = -1;
44
45 static gint ett_ymsg = -1;
46 static gint ett_ymsg_content = -1;
47
48 #define TCP_PORT_YMSG   23      /* XXX - this is Telnet! */
49 #define TCP_PORT_YMSG_2 25      /* And this is SMTP! */
50 #define TCP_PORT_YMSG_3 5050    /* This, however, is regular Yahoo Messenger */
51
52 /*
53  * This is from yahoolib2.c from libyahoo2.
54  *
55  * See also
56  *
57  *      http://libyahoo2.sourceforge.net/ymsg-9.txt
58  *
59  * and
60  *
61  *      http://www.venkydude.com/articles/yahoo.htm
62  *
63  * and
64  *
65  *      http://www.cse.iitb.ac.in/~varunk/YahooProtocol.htm
66  *
67  * and
68  *
69  *      http://www.geocrawler.com/archives/3/4893/2002/1/0/7459037/
70  *
71  * and
72  *
73  *      http://www.geocities.com/ziggycubbe/ym.html
74  */
75
76 /* Service constants */
77 enum yahoo_service { /* these are easier to see in hex */
78         YAHOO_SERVICE_LOGON = 1,
79         YAHOO_SERVICE_LOGOFF,
80         YAHOO_SERVICE_ISAWAY,
81         YAHOO_SERVICE_ISBACK,
82         YAHOO_SERVICE_IDLE, /* 5 (placemarker) */
83         YAHOO_SERVICE_MESSAGE,
84         YAHOO_SERVICE_IDACT,
85         YAHOO_SERVICE_IDDEACT,
86         YAHOO_SERVICE_MAILSTAT,
87         YAHOO_SERVICE_USERSTAT, /* 0xa */
88         YAHOO_SERVICE_NEWMAIL,
89         YAHOO_SERVICE_CHATINVITE,
90         YAHOO_SERVICE_CALENDAR,
91         YAHOO_SERVICE_NEWPERSONALMAIL,
92         YAHOO_SERVICE_NEWCONTACT,
93         YAHOO_SERVICE_ADDIDENT, /* 0x10 */
94         YAHOO_SERVICE_ADDIGNORE,
95         YAHOO_SERVICE_PING,
96         YAHOO_SERVICE_GOTGROUPRENAME, /* < 1, 36(old), 37(new) */
97         YAHOO_SERVICE_SYSMESSAGE = 0x14,
98         YAHOO_SERVICE_PASSTHROUGH2 = 0x16,
99         YAHOO_SERVICE_CONFINVITE = 0x18,
100         YAHOO_SERVICE_CONFLOGON,
101         YAHOO_SERVICE_CONFDECLINE,
102         YAHOO_SERVICE_CONFLOGOFF,
103         YAHOO_SERVICE_CONFADDINVITE,
104         YAHOO_SERVICE_CONFMSG,
105         YAHOO_SERVICE_CHATLOGON,
106         YAHOO_SERVICE_CHATLOGOFF,
107         YAHOO_SERVICE_CHATMSG = 0x20,
108         YAHOO_SERVICE_GAMELOGON = 0x28,
109         YAHOO_SERVICE_GAMELOGOFF,
110         YAHOO_SERVICE_GAMEMSG = 0x2a,
111         YAHOO_SERVICE_FILETRANSFER = 0x46,
112         YAHOO_SERVICE_VOICECHAT = 0x4A,
113         YAHOO_SERVICE_NOTIFY,
114         YAHOO_SERVICE_VERIFY,
115         YAHOO_SERVICE_P2PFILEXFER,
116         YAHOO_SERVICE_PEERTOPEER = 0x4F,        /* Checks if P2P possible */
117         YAHOO_SERVICE_AUTHRESP = 0x54,
118         YAHOO_SERVICE_LIST,
119         YAHOO_SERVICE_AUTH = 0x57,
120         YAHOO_SERVICE_ADDBUDDY = 0x83,
121         YAHOO_SERVICE_REMBUDDY,
122         YAHOO_SERVICE_IGNORECONTACT,    /* > 1, 7, 13 < 1, 66, 13, 0*/
123         YAHOO_SERVICE_REJECTCONTACT,
124         YAHOO_SERVICE_GROUPRENAME = 0x89, /* > 1, 65(new), 66(0), 67(old) */
125         YAHOO_SERVICE_CHATONLINE = 0x96, /* > 109(id), 1, 6(abcde) < 0,1*/
126         YAHOO_SERVICE_CHATGOTO,
127         YAHOO_SERVICE_CHATJOIN, /* > 1 104-room 129-1600326591 62-2 */
128         YAHOO_SERVICE_CHATLEAVE,
129         YAHOO_SERVICE_CHATEXIT = 0x9b,
130         YAHOO_SERVICE_CHATLOGOUT = 0xa0,
131         YAHOO_SERVICE_CHATPING,
132         YAHOO_SERVICE_COMMENT = 0xa8
133 };
134
135 /* Message flags */
136 enum yahoo_status {
137         YAHOO_STATUS_AVAILABLE = 0,
138         YAHOO_STATUS_BRB,
139         YAHOO_STATUS_BUSY,
140         YAHOO_STATUS_NOTATHOME,
141         YAHOO_STATUS_NOTATDESK,
142         YAHOO_STATUS_NOTINOFFICE,
143         YAHOO_STATUS_ONPHONE,
144         YAHOO_STATUS_ONVACATION,
145         YAHOO_STATUS_OUTTOLUNCH,
146         YAHOO_STATUS_STEPPEDOUT,
147         YAHOO_STATUS_INVISIBLE = 12,
148         YAHOO_STATUS_CUSTOM = 99,
149         YAHOO_STATUS_IDLE = 999,
150         YAHOO_STATUS_OFFLINE = 0x5a55aa56, /* don't ask */
151         YAHOO_STATUS_TYPING = 0x16
152 };
153
154 struct yahoo_rawpacket
155 {
156         char ymsg[4];                   /* Packet identification string (YMSG) */
157         unsigned char version[4];       /* 4 bytes, little endian? */
158         unsigned char len[2];           /* length - little endian */
159         unsigned char service[2];       /* service - little endian */
160         unsigned char status[4];        /* Status - online, away etc.*/
161         unsigned char session_id[4];    /* Session ID */
162         char content[6];                /* 6 is the minimum size of the content */
163 };
164
165 static const value_string ymsg_service_vals[] = {
166         {YAHOO_SERVICE_LOGON, "Pager Logon"},
167         {YAHOO_SERVICE_LOGOFF, "Pager Logoff"},
168         {YAHOO_SERVICE_ISAWAY, "Is Away"},
169         {YAHOO_SERVICE_ISBACK, "Is Back"},
170         {YAHOO_SERVICE_IDLE, "Idle"},
171         {YAHOO_SERVICE_MESSAGE, "Message"},
172         {YAHOO_SERVICE_IDACT, "Activate Identity"},
173         {YAHOO_SERVICE_IDDEACT, "Deactivate Identity"},
174         {YAHOO_SERVICE_MAILSTAT, "Mail Status"},
175         {YAHOO_SERVICE_USERSTAT, "User Status"},
176         {YAHOO_SERVICE_NEWMAIL, "New Mail"},
177         {YAHOO_SERVICE_CHATINVITE, "Chat Invitation"},
178         {YAHOO_SERVICE_CALENDAR, "Calendar Reminder"},
179         {YAHOO_SERVICE_NEWPERSONALMAIL, "New Personals Mail"},
180         {YAHOO_SERVICE_NEWCONTACT, "New Friend"},
181         {YAHOO_SERVICE_ADDIDENT, "Add Identity"},
182         {YAHOO_SERVICE_ADDIGNORE, "Add Ignore"},
183         {YAHOO_SERVICE_PING, "Ping"},
184         {YAHOO_SERVICE_GOTGROUPRENAME, "YAHOO_SERVICE_GOTGROUPRENAME"},
185         {YAHOO_SERVICE_SYSMESSAGE, "System Message"},
186         {YAHOO_SERVICE_PASSTHROUGH2, "Passthrough 2"},
187         {YAHOO_SERVICE_CONFINVITE, "Conference Invitation"},
188         {YAHOO_SERVICE_CONFLOGON, "Conference Logon"},
189         {YAHOO_SERVICE_CONFDECLINE, "Conference Decline"},
190         {YAHOO_SERVICE_CONFLOGOFF, "Conference Logoff"},
191         {YAHOO_SERVICE_CONFADDINVITE, "Conference Additional Invitation"},
192         {YAHOO_SERVICE_CONFMSG, "Conference Message"},
193         {YAHOO_SERVICE_CHATLOGON, "Chat Logon"},
194         {YAHOO_SERVICE_CHATLOGOFF, "Chat Logoff"},
195         {YAHOO_SERVICE_CHATMSG, "Chat Message"},
196         {YAHOO_SERVICE_GAMELOGON, "Game Logon"},
197         {YAHOO_SERVICE_GAMELOGOFF, "Game Logoff"},
198         {YAHOO_SERVICE_GAMEMSG, "Game Message"},
199         {YAHOO_SERVICE_FILETRANSFER, "File Transfer"},
200         {YAHOO_SERVICE_VOICECHAT, "Voice Chat"},
201         {YAHOO_SERVICE_NOTIFY, "YAHOO_SERVICE_NOTIFY"},
202         {YAHOO_SERVICE_VERIFY, "YAHOO_SERVICE_VERIFY"},
203         {YAHOO_SERVICE_P2PFILEXFER, "YAHOO_SERVICE_P2PFILEXFER"}, 
204         {YAHOO_SERVICE_PEERTOPEER, "YAHOO_SERVICE_PEERTOPEER"},
205         {YAHOO_SERVICE_AUTHRESP, "YAHOO_SERVICE_AUTHRESP"},
206         {YAHOO_SERVICE_LIST, "YAHOO_SERVICE_LIST"},
207         {YAHOO_SERVICE_AUTH, "YAHOO_SERVICE_AUTH"},
208         {YAHOO_SERVICE_ADDBUDDY, "YAHOO_SERVICE_ADDBUDDY"},
209         {YAHOO_SERVICE_REMBUDDY, "YAHOO_SERVICE_REMBUDDY"},
210         {YAHOO_SERVICE_IGNORECONTACT, "YAHOO_SERVICE_IGNORECONTACT"},
211         {YAHOO_SERVICE_REJECTCONTACT, "YAHOO_SERVICE_REJECTCONTACT"},
212         {YAHOO_SERVICE_GROUPRENAME, "Group Renamed"},
213         {YAHOO_SERVICE_CHATONLINE, "YAHOO_SERVICE_CHATONLINE"},
214         {YAHOO_SERVICE_CHATGOTO, "YAHOO_SERVICE_CHATGOTO"},
215         {YAHOO_SERVICE_CHATJOIN, "YAHOO_SERVICE_CHATJOIN"},
216         {YAHOO_SERVICE_CHATLEAVE, "YAHOO_SERVICE_CHATLEAVE"},
217         {YAHOO_SERVICE_CHATEXIT, "YAHOO_SERVICE_CHATEXIT"},
218         {YAHOO_SERVICE_CHATLOGOUT, "YAHOO_SERVICE_CHATLOGOUT"},
219         {YAHOO_SERVICE_CHATPING, "YAHOO_SERVICE_CHATPING"},
220         {YAHOO_SERVICE_COMMENT, "YAHOO_SERVICE_COMMENT"},
221         {0, NULL}
222 };
223
224 static const value_string ymsg_status_vals[] = {
225         {YAHOO_STATUS_AVAILABLE, "YAHOO_STATUS_AVAILABLE"},
226         {YAHOO_STATUS_BRB, "YAHOO_STATUS_BRB"},
227         {YAHOO_STATUS_BUSY, "YAHOO_STATUS_BUSY"},
228         {YAHOO_STATUS_NOTATHOME, "YAHOO_STATUS_NOTATHOME"},
229         {YAHOO_STATUS_NOTATDESK, "YAHOO_STATUS_NOTATDESK"},
230         {YAHOO_STATUS_NOTINOFFICE, "YAHOO_STATUS_NOTINOFFICE"},
231         {YAHOO_STATUS_ONPHONE, "YAHOO_STATUS_ONPHONE"},
232         {YAHOO_STATUS_ONVACATION, "YAHOO_STATUS_ONVACATION"},
233         {YAHOO_STATUS_OUTTOLUNCH, "YAHOO_STATUS_OUTTOLUNCH"},
234         {YAHOO_STATUS_STEPPEDOUT, "YAHOO_STATUS_STEPPEDOUT"},
235         {YAHOO_STATUS_INVISIBLE, "YAHOO_STATUS_INVISIBLE"},
236         {YAHOO_STATUS_CUSTOM, "YAHOO_STATUS_CUSTOM"},
237         {YAHOO_STATUS_IDLE, "YAHOO_STATUS_IDLE"},
238         {YAHOO_STATUS_OFFLINE, "YAHOO_STATUS_OFFLINE"},
239         {YAHOO_STATUS_TYPING, "YAHOO_STATUS_TYPING"},
240         {0, NULL}
241 };
242
243 int
244 get_content_item_length(tvbuff_t *tvb, int offset) {
245         int origoffset = offset;
246         guint16 curdata;
247         for (;;) {
248                 curdata = tvb_get_ntohs(tvb, offset);
249                 if (curdata == 0xc080) {
250                         break;
251                 }
252                 offset++;
253         }
254         return offset - origoffset;
255 }
256
257 static gboolean
258 dissect_ymsg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
259 {
260         proto_tree      *ymsg_tree, *ti;
261         proto_item      *content_item;
262         proto_tree      *content_tree;
263         char *keybuf;
264         char *valbuf;
265         int headersize = sizeof(struct yahoo_rawpacket)-6;
266         int keylen = 0;
267         int vallen = 0;
268         int offset = 0;
269         int content_len = 0;
270
271         if ((pinfo->srcport != TCP_PORT_YMSG && pinfo->destport != TCP_PORT_YMSG) &&
272             (pinfo->srcport != TCP_PORT_YMSG_2 && pinfo->destport != TCP_PORT_YMSG_2) &&
273             (pinfo->srcport != TCP_PORT_YMSG_3 && pinfo->destport != TCP_PORT_YMSG_3)) {
274                 /* Not the Yahoo port - not a Yahoo Messenger packet. */
275                 return FALSE;
276         }
277
278         /* get at least a full packet structure */
279         if ( !tvb_bytes_exist(tvb, 0, headersize) ) {
280                 /* Not enough data captured; maybe it is a Yahoo
281                    Messenger packet, but it contains too little data to
282                    tell. */
283                 return FALSE;
284         }
285
286         if (memcmp(tvb_get_ptr(tvb, offset, 4), "YMSG", 4) != 0) {
287                 /* Not a Yahoo Messenger packet. */
288                 return FALSE;
289         }
290
291         if (check_col(pinfo->cinfo, COL_PROTOCOL))
292                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "YMSG");
293
294         offset = 0;
295         if (check_col(pinfo->cinfo, COL_INFO)) {
296                 col_add_fstr(pinfo->cinfo, COL_INFO,
297                         "%s, %s",
298                         val_to_str(tvb_get_ntohs(tvb, offset + 10),
299                                  ymsg_service_vals, "Unknown Service: %u"),
300                         val_to_str(tvb_get_ntohl(tvb, offset + 12),
301                                  ymsg_status_vals, "Unknown Status: %u")
302                 );
303         }
304
305         if (tree) {
306                 ti = proto_tree_add_item(tree, proto_ymsg, tvb, offset, -1,
307                                 FALSE);
308                 ymsg_tree = proto_item_add_subtree(ti, ett_ymsg);
309
310                 offset += 4; /* skip the YMSG string */
311
312                 proto_tree_add_item(ymsg_tree, hf_ymsg_version, tvb,
313                         offset, 4, TRUE);
314                 offset += 4;
315
316                 content_len = tvb_get_ntohs(tvb, offset);
317                 proto_tree_add_item(ymsg_tree, hf_ymsg_len, tvb,
318                         offset, 2, FALSE);
319                 offset += 2;
320
321                 proto_tree_add_item(ymsg_tree, hf_ymsg_service, tvb,
322                         offset, 2, FALSE);
323                 offset += 2;
324
325                 proto_tree_add_item(ymsg_tree, hf_ymsg_status, tvb,
326                         offset, 4, FALSE);
327                 offset += 4;
328
329                 proto_tree_add_item(ymsg_tree, hf_ymsg_session_id, tvb,
330                         offset, 4, TRUE);
331                 offset += 4;
332
333                 content_item = proto_tree_add_item(ymsg_tree, hf_ymsg_content, tvb,
334                                 offset, -1, TRUE);
335                 content_tree = proto_item_add_subtree(content_item, ett_ymsg_content);
336
337                 for (;;) {
338                         if (offset >= headersize+content_len) {
339                                 break;
340                         }
341                         keylen = get_content_item_length (tvb, offset);
342                         keybuf = tvb_format_text(tvb, offset, keylen);
343                         vallen = get_content_item_length (tvb, offset+keylen+2);
344                         content_item = proto_tree_add_text(content_tree, tvb, offset, keylen+vallen+4, "%s: ", keybuf);
345                         valbuf = tvb_format_text(tvb, offset+keylen+2, vallen);
346                         proto_item_append_text(content_item, "%s", valbuf);
347                         offset += keylen+vallen+4;
348                 }
349         }
350
351         return TRUE;
352 }
353
354 void
355 proto_register_ymsg(void)
356 {
357         static hf_register_info hf[] = {
358                         { &hf_ymsg_version, {
359                                 "Version", "ymsg.version", FT_UINT32, BASE_DEC,
360                                 NULL, 0, "Packet version identifier", HFILL }},
361                         { &hf_ymsg_len, {
362                                 "Packet Length", "ymsg.len", FT_UINT16, BASE_DEC,
363                                 NULL, 0, "Packet Length", HFILL }},
364                         { &hf_ymsg_service, {
365                                 "Service", "ymsg.service", FT_UINT16, BASE_DEC,
366                                 VALS(ymsg_service_vals), 0, "Service Type", HFILL }},
367                         { &hf_ymsg_status, {
368                                 "Status", "ymsg.status", FT_UINT32, BASE_DEC,
369                                 VALS(ymsg_status_vals), 0, "Message Type Flags", HFILL }},
370                         { &hf_ymsg_session_id, {
371                                 "Session ID", "ymsg.session_id", FT_UINT32, BASE_HEX,
372                                 NULL, 0, "Connection ID", HFILL }},
373                         { &hf_ymsg_content, {
374                                 "Content", "ymsg.content", FT_STRING, 0,
375                                 NULL, 0, "Data portion of the packet", HFILL }},
376         };
377         static gint *ett[] = {
378                 &ett_ymsg,
379                 &ett_ymsg_content,
380         };
381
382         proto_ymsg = proto_register_protocol("Yahoo YMSG Messenger Protocol",
383             "YMSG", "ymsg");
384
385         proto_register_field_array(proto_ymsg, hf, array_length(hf));
386
387         proto_register_subtree_array(ett, array_length(ett));
388 }
389
390 void
391 proto_reg_handoff_ymsg(void)
392 {
393         /*
394          * DO NOT register for port 23, as that's Telnet, or for port
395          * 25, as that's SMTP.
396          *
397          * Also, DO NOT register for port 5050, as that's used by the
398          * old and new Yahoo messenger protocols.
399          *
400          * Just register as a heuristic TCP dissector, and reject stuff
401          * not to or from any of those ports.
402          */
403         heur_dissector_add("tcp", dissect_ymsg, proto_ymsg);
404 }