Move the GSM SMS dissection to a dedicated subdissector (currently still within
[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.3 2003/10/22 21:21:05 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 (tvb_memeql(tvb, offset, "YMSG", 4) == -1) {
272                 /* Not a Yahoo Messenger packet. */
273                 return FALSE;
274         }
275
276         if (check_col(pinfo->cinfo, COL_PROTOCOL))
277                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "YMSG");
278
279         offset = 0;
280         if (check_col(pinfo->cinfo, COL_INFO)) {
281                 col_add_fstr(pinfo->cinfo, COL_INFO,
282                         "%s, %s",
283                         val_to_str(tvb_get_ntohs(tvb, offset + 10),
284                                  ymsg_service_vals, "Unknown Service: %u"),
285                         val_to_str(tvb_get_ntohl(tvb, offset + 12),
286                                  ymsg_status_vals, "Unknown Status: %u")
287                 );
288         }
289
290         if (tree) {
291                 ti = proto_tree_add_item(tree, proto_ymsg, tvb, offset, -1,
292                                 FALSE);
293                 ymsg_tree = proto_item_add_subtree(ti, ett_ymsg);
294
295                 offset += 4; /* skip the YMSG string */
296
297                 proto_tree_add_item(ymsg_tree, hf_ymsg_version, tvb,
298                         offset, 2, FALSE);
299                 offset += 2;
300
301                 offset += 2;    /* XXX - padding? */
302
303                 content_len = tvb_get_ntohs(tvb, offset);
304                 proto_tree_add_item(ymsg_tree, hf_ymsg_len, tvb,
305                         offset, 2, FALSE);
306                 offset += 2;
307
308                 proto_tree_add_item(ymsg_tree, hf_ymsg_service, tvb,
309                         offset, 2, FALSE);
310                 offset += 2;
311
312                 proto_tree_add_item(ymsg_tree, hf_ymsg_status, tvb,
313                         offset, 4, FALSE);
314                 offset += 4;
315
316                 proto_tree_add_item(ymsg_tree, hf_ymsg_session_id, tvb,
317                         offset, 4, TRUE);
318                 offset += 4;
319
320                 content_item = proto_tree_add_item(ymsg_tree, hf_ymsg_content, tvb,
321                                 offset, -1, TRUE);
322                 content_tree = proto_item_add_subtree(content_item, ett_ymsg_content);
323
324                 for (;;) {
325                         if (offset >= headersize+content_len) {
326                                 break;
327                         }
328                         keylen = get_content_item_length (tvb, offset);
329                         keybuf = tvb_format_text(tvb, offset, keylen);
330                         vallen = get_content_item_length (tvb, offset+keylen+2);
331                         content_item = proto_tree_add_text(content_tree, tvb, offset, keylen+vallen+4, "%s: ", keybuf);
332                         valbuf = tvb_format_text(tvb, offset+keylen+2, vallen);
333                         proto_item_append_text(content_item, "%s", valbuf);
334                         offset += keylen+vallen+4;
335                 }
336         }
337
338         return TRUE;
339 }
340
341 void
342 proto_register_ymsg(void)
343 {
344         static hf_register_info hf[] = {
345                         { &hf_ymsg_version, {
346                                 "Version", "ymsg.version", FT_UINT16, BASE_DEC,
347                                 NULL, 0, "Packet version identifier", HFILL }},
348                         { &hf_ymsg_len, {
349                                 "Packet Length", "ymsg.len", FT_UINT16, BASE_DEC,
350                                 NULL, 0, "Packet Length", HFILL }},
351                         { &hf_ymsg_service, {
352                                 "Service", "ymsg.service", FT_UINT16, BASE_DEC,
353                                 VALS(ymsg_service_vals), 0, "Service Type", HFILL }},
354                         { &hf_ymsg_status, {
355                                 "Status", "ymsg.status", FT_UINT32, BASE_DEC,
356                                 VALS(ymsg_status_vals), 0, "Message Type Flags", HFILL }},
357                         { &hf_ymsg_session_id, {
358                                 "Session ID", "ymsg.session_id", FT_UINT32, BASE_HEX,
359                                 NULL, 0, "Connection ID", HFILL }},
360                         { &hf_ymsg_content, {
361                                 "Content", "ymsg.content", FT_STRING, 0,
362                                 NULL, 0, "Data portion of the packet", HFILL }},
363         };
364         static gint *ett[] = {
365                 &ett_ymsg,
366                 &ett_ymsg_content,
367         };
368
369         proto_ymsg = proto_register_protocol("Yahoo YMSG Messenger Protocol",
370             "YMSG", "ymsg");
371
372         proto_register_field_array(proto_ymsg, hf, array_length(hf));
373
374         proto_register_subtree_array(ett, array_length(ett));
375 }
376
377 void
378 proto_reg_handoff_ymsg(void)
379 {
380         /*
381          * DO NOT register for port 23, as that's Telnet, or for port
382          * 25, as that's SMTP.
383          *
384          * Also, DO NOT register for port 5050, as that's used by the
385          * old and new Yahoo messenger protocols.
386          *
387          * Just register as a heuristic TCP dissector, and reject stuff
388          * not to or from any of those ports.
389          */
390         heur_dissector_add("tcp", dissect_ymsg, proto_ymsg);
391 }