7f128683631fc1d6794166ad6db8cc2792aeee26
[obnox/wireshark/wip.git] / epan / dissectors / packet-yhoo.c
1 /* packet-yhoo.c
2  * Routines for yahoo messenger packet dissection
3  * Copyright 1999, Nathan Neulinger <nneul@umr.edu>
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 packet-tftp.c
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <string.h>
33 #include <glib.h>
34 #include <epan/packet.h>
35
36 static int proto_yhoo = -1;
37 static int hf_yhoo_version = -1;
38 static int hf_yhoo_len = -1;
39 static int hf_yhoo_service = -1;
40 static int hf_yhoo_connection_id = -1;
41 static int hf_yhoo_magic_id = -1;
42 static int hf_yhoo_unknown1 = -1;
43 static int hf_yhoo_msgtype = -1;
44 static int hf_yhoo_nick1 = -1;
45 static int hf_yhoo_nick2 = -1;
46 static int hf_yhoo_content = -1;
47
48 static gint ett_yhoo = -1;
49
50 #define TCP_PORT_YHOO   5050
51
52 /* This is from yahoolib.h from gtkyahoo */
53
54 /* Service constants */
55 #define YAHOO_SERVICE_LOGON             1
56 #define YAHOO_SERVICE_LOGOFF            2
57 #define YAHOO_SERVICE_ISAWAY            3
58 #define YAHOO_SERVICE_ISBACK            4
59 #define YAHOO_SERVICE_IDLE              5
60 #define YAHOO_SERVICE_MESSAGE           6
61 #define YAHOO_SERVICE_IDACT             7
62 #define YAHOO_SERVICE_IDDEACT           8
63 #define YAHOO_SERVICE_MAILSTAT          9
64 #define YAHOO_SERVICE_USERSTAT          10
65 #define YAHOO_SERVICE_NEWMAIL           11
66 #define YAHOO_SERVICE_CHATINVITE        12
67 #define YAHOO_SERVICE_CALENDAR          13
68 #define YAHOO_SERVICE_NEWPERSONALMAIL   14
69 #define YAHOO_SERVICE_NEWCONTACT        15
70 #define YAHOO_SERVICE_ADDIDENT          16
71 #define YAHOO_SERVICE_ADDIGNORE         17
72 #define YAHOO_SERVICE_PING              18
73 #define YAHOO_SERVICE_GROUPRENAME       19
74 #define YAHOO_SERVICE_SYSMESSAGE        20
75 #define YAHOO_SERVICE_PASSTHROUGH2      22
76 #define YAHOO_SERVICE_CONFINVITE        24
77 #define YAHOO_SERVICE_CONFLOGON         25
78 #define YAHOO_SERVICE_CONFDECLINE       26
79 #define YAHOO_SERVICE_CONFLOGOFF        27
80 #define YAHOO_SERVICE_CONFADDINVITE     28
81 #define YAHOO_SERVICE_CONFMSG           29
82 #define YAHOO_SERVICE_CHATLOGON         30
83 #define YAHOO_SERVICE_CHATLOGOFF        31
84 #define YAHOO_SERVICE_CHATMSG           32
85 #define YAHOO_SERVICE_FILETRANSFER      70
86 #define YAHOO_SERVICE_CHATADDINVITE 157
87 #define YAHOO_SERVICE_AVATAR            188
88 #define YAHOO_SERVICE_PICTURE_CHECKSUM  189
89 #define YAHOO_SERVICE_PICTURE                   190
90 #define YAHOO_SERVICE_PICTURE_UPDATE    193
91 #define YAHOO_SERVICE_PICTURE_UPLOAD    194
92 #define YAHOO_SERVICE_YAHOO6_STATUS_UPDATE 198
93 #define YAHOO_SERVICE_AVATAR_UPDATE             199
94 #define YAHOO_SERVICE_AUDIBLE                   208
95 #define YAHOO_SERVICE_WEBLOGIN                  550
96 #define YAHOO_SERVICE_SMS_MSG                   746
97
98
99 /* Message flags */
100 #define YAHOO_MSGTYPE_NONE      0
101 #define YAHOO_MSGTYPE_NORMAL    1
102 #define YAHOO_MSGTYPE_BOUNCE    2
103 #define YAHOO_MSGTYPE_STATUS    4
104 #define YAHOO_MSGTYPE_OFFLINE   1515563606      /* yuck! */
105
106 struct yahoo_rawpacket
107 {
108         char version[8];                /* 7 chars and trailing null */
109         unsigned char len[4];           /* length - little endian */
110         unsigned char service[4];       /* service - little endian */
111         unsigned char connection_id[4]; /* connection number - little endian */
112         unsigned char magic_id[4];      /* magic number used for http session */
113         unsigned char unknown1[4];
114         unsigned char msgtype[4];
115         char nick1[36];
116         char nick2[36];
117         char content[1];                /* was zero, had problems with aix xlc */
118 };
119
120 static const value_string yhoo_service_vals[] = {
121         {YAHOO_SERVICE_LOGON, "Pager Logon"},
122         {YAHOO_SERVICE_LOGOFF, "Pager Logoff"},
123         {YAHOO_SERVICE_ISAWAY, "Is Away"},
124         {YAHOO_SERVICE_ISBACK, "Is Back"},
125         {YAHOO_SERVICE_IDLE, "Idle"},
126         {YAHOO_SERVICE_MESSAGE, "Message"},
127         {YAHOO_SERVICE_IDACT, "Activate Identity"},
128         {YAHOO_SERVICE_IDDEACT, "Deactivate Identity"},
129         {YAHOO_SERVICE_MAILSTAT, "Mail Status"},
130         {YAHOO_SERVICE_USERSTAT, "User Status"},
131         {YAHOO_SERVICE_NEWMAIL, "New Mail"},
132         {YAHOO_SERVICE_CHATINVITE, "Chat Invitation"},
133         {YAHOO_SERVICE_CALENDAR, "Calendar Reminder"},
134         {YAHOO_SERVICE_NEWPERSONALMAIL, "New Personals Mail"},
135         {YAHOO_SERVICE_NEWCONTACT, "New Friend"},
136         {YAHOO_SERVICE_GROUPRENAME, "Group Renamed"},
137         {YAHOO_SERVICE_ADDIDENT, "Add Identity"},
138         {YAHOO_SERVICE_ADDIGNORE, "Add Ignore"},
139         {YAHOO_SERVICE_PING, "Ping"},
140         {YAHOO_SERVICE_SYSMESSAGE, "System Message"},
141         {YAHOO_SERVICE_CONFINVITE, "Conference Invitation"},
142         {YAHOO_SERVICE_CONFLOGON, "Conference Logon"},
143         {YAHOO_SERVICE_CONFDECLINE, "Conference Decline"},
144         {YAHOO_SERVICE_CONFLOGOFF, "Conference Logoff"},
145         {YAHOO_SERVICE_CONFMSG, "Conference Message"},
146         {YAHOO_SERVICE_CONFADDINVITE, "Conference Additional Invitation"},
147         {YAHOO_SERVICE_CHATLOGON, "Chat Logon"},
148         {YAHOO_SERVICE_CHATLOGOFF, "Chat Logoff"},
149         {YAHOO_SERVICE_CHATMSG, "Chat Message"},
150         {YAHOO_SERVICE_FILETRANSFER, "File Transfer"},
151         {YAHOO_SERVICE_PASSTHROUGH2, "Passthrough 2"},
152         {YAHOO_SERVICE_CHATADDINVITE, "Chat add Invite"},
153         {YAHOO_SERVICE_AVATAR, "Avatar"},
154         {YAHOO_SERVICE_PICTURE_CHECKSUM, "Picture Checksum"},
155         {YAHOO_SERVICE_PICTURE, "Picture"},
156         {YAHOO_SERVICE_PICTURE_UPDATE, "Picture Update"},
157         {YAHOO_SERVICE_PICTURE_UPLOAD, "Picture Upload"},
158         {YAHOO_SERVICE_YAHOO6_STATUS_UPDATE, "Status update"},
159         {YAHOO_SERVICE_AUDIBLE, "Audible"},
160         {YAHOO_SERVICE_WEBLOGIN, "Weblogin"},
161         {YAHOO_SERVICE_SMS_MSG, "SMS Message"},
162         {0, NULL}
163 };
164
165 static const value_string yhoo_msgtype_vals[] = {
166         {YAHOO_MSGTYPE_NONE, "None"},
167         {YAHOO_MSGTYPE_NORMAL, "Normal"},
168         {YAHOO_MSGTYPE_BOUNCE, "Bounce"},
169         {YAHOO_MSGTYPE_STATUS, "Status Update"},
170         {YAHOO_MSGTYPE_OFFLINE, "Request Offline"},
171         {0, NULL}
172 };
173
174 static gboolean
175 dissect_yhoo(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
176 {
177         proto_tree      *yhoo_tree, *ti;
178         int offset = 0;
179
180         if (pinfo->srcport != TCP_PORT_YHOO && pinfo->destport != TCP_PORT_YHOO) {
181                 /* Not the Yahoo port - not a Yahoo Messenger packet. */
182                 return FALSE;
183         }
184
185         /* get at least a full packet structure */
186         if ( tvb_length(tvb) < sizeof(struct yahoo_rawpacket) ) {
187                 /* Not enough data captured; maybe it is a Yahoo
188                    Messenger packet, but it contains too little data to
189                    tell. */
190                 return FALSE;
191         }
192
193         if (tvb_memeql(tvb, offset, "YPNS", 4) != 0 &&
194             tvb_memeql(tvb, offset, "YHOO", 4) != 0) {
195                 /* Not a Yahoo Messenger packet. */
196                 return FALSE;
197         }
198
199         col_set_str(pinfo->cinfo, COL_PROTOCOL, "YHOO");
200
201         offset = 0;
202         if (check_col(pinfo->cinfo, COL_INFO)) {
203                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s: %s",
204                              ( tvb_memeql(tvb, offset + 0, "YPNS", 4) == 0 ) ? "Request" : "Response",
205                              val_to_str(tvb_get_letohl(tvb, offset + 12),
206                                         yhoo_service_vals, "Unknown Service: %u"));
207         }
208
209         if (tree) {
210                 ti = proto_tree_add_item(tree, proto_yhoo, tvb, offset, -1,
211                         FALSE);
212                 yhoo_tree = proto_item_add_subtree(ti, ett_yhoo);
213
214                 proto_tree_add_item(yhoo_tree, hf_yhoo_version, tvb,
215                         offset, 8, ENC_ASCII|ENC_NA);
216                 offset += 8;
217
218                 proto_tree_add_item(yhoo_tree, hf_yhoo_len, tvb,
219                         offset, 4, ENC_LITTLE_ENDIAN);
220                 offset += 4;
221
222                 proto_tree_add_item(yhoo_tree, hf_yhoo_service, tvb,
223                         offset, 4, ENC_LITTLE_ENDIAN);
224                 offset += 4;
225
226                 proto_tree_add_item(yhoo_tree, hf_yhoo_connection_id, tvb,
227                         offset, 4, ENC_LITTLE_ENDIAN);
228                 offset += 4;
229
230                 proto_tree_add_item(yhoo_tree, hf_yhoo_magic_id, tvb,
231                         offset, 4, ENC_LITTLE_ENDIAN);
232                 offset += 4;
233
234                 proto_tree_add_item(yhoo_tree, hf_yhoo_unknown1, tvb,
235                         offset, 4, ENC_LITTLE_ENDIAN);
236                 offset += 4;
237
238                 proto_tree_add_item(yhoo_tree, hf_yhoo_msgtype, tvb,
239                         offset, 4, ENC_LITTLE_ENDIAN);
240                 offset += 4;
241
242                 proto_tree_add_item(yhoo_tree, hf_yhoo_nick1, tvb,
243                         offset, 36, ENC_ASCII|ENC_NA);
244                 offset += 36;
245
246                 proto_tree_add_item(yhoo_tree, hf_yhoo_nick2, tvb,
247                         offset, 36, ENC_ASCII|ENC_NA);
248                 offset += 36;
249
250                 proto_tree_add_item(yhoo_tree, hf_yhoo_content, tvb, -1,
251                         offset, ENC_ASCII|ENC_NA);
252         }
253
254         return TRUE;
255 }
256
257 void
258 proto_register_yhoo(void)
259 {
260         static hf_register_info hf[] = {
261                         { &hf_yhoo_service, {
262                                 "Service Type", "yhoo.service", FT_UINT32, BASE_DEC,
263                                 VALS(yhoo_service_vals), 0, NULL, HFILL }},
264                         { &hf_yhoo_msgtype, {
265                                 "Message Type", "yhoo.msgtype", FT_UINT32, BASE_DEC,
266                                 VALS(yhoo_msgtype_vals), 0, "Message Type Flags", HFILL }},
267                         { &hf_yhoo_connection_id, {
268                                 "Connection ID", "yhoo.connection_id", FT_UINT32, BASE_HEX,
269                                 NULL, 0, NULL, HFILL }},
270                         { &hf_yhoo_magic_id, {
271                                 "Magic ID", "yhoo.magic_id", FT_UINT32, BASE_HEX,
272                                 NULL, 0, NULL, HFILL }},
273                         { &hf_yhoo_unknown1, {
274                                 "Unknown 1", "yhoo.unknown1", FT_UINT32, BASE_HEX,
275                                 NULL, 0, NULL, HFILL }},
276                         { &hf_yhoo_len, {
277                                 "Packet Length", "yhoo.len", FT_UINT32, BASE_DEC,
278                                 NULL, 0, NULL, HFILL }},
279                         { &hf_yhoo_nick1, {
280                                 "Real Nick (nick1)", "yhoo.nick1", FT_STRING, BASE_NONE,
281                                 NULL, 0, NULL, HFILL }},
282                         { &hf_yhoo_nick2, {
283                                 "Active Nick (nick2)", "yhoo.nick2", FT_STRING, BASE_NONE,
284                                 NULL, 0, NULL, HFILL }},
285                         { &hf_yhoo_content, {
286                                 "Content", "yhoo.content", FT_STRING, BASE_NONE,
287                                 NULL, 0, "Data portion of the packet", HFILL }},
288                         { &hf_yhoo_version, {
289                                 "Version", "yhoo.version", FT_STRING, BASE_NONE,
290                                 NULL, 0, "Packet version identifier", HFILL }},
291         };
292         static gint *ett[] = {
293                 &ett_yhoo,
294         };
295
296         proto_yhoo = proto_register_protocol("Yahoo Messenger Protocol",
297             "YHOO", "yhoo");
298
299         proto_register_field_array(proto_yhoo, hf, array_length(hf));
300
301         proto_register_subtree_array(ett, array_length(ett));
302 }
303
304 void
305 proto_reg_handoff_yhoo(void)
306 {
307         /*
308          * DO NOT register for port 5050, as that's used by the
309          * old and new Yahoo messenger protocols.
310          *
311          * Just register as a heuristic TCP dissector, and reject stuff
312          * not to or from that port.
313          */
314         heur_dissector_add("tcp", dissect_yhoo, proto_yhoo);
315 }