Remove all $Id$ from top of file
[metze/wireshark/wip.git] / epan / dissectors / packet-aim-messaging.c
1 /* packet-aim-messaging.c
2  * Routines for AIM Instant Messenger (OSCAR) dissection, SNAC Messaging
3  * Copyright 2004, Jelmer Vernooij <jelmer@samba.org>
4  * Copyright 2000, Ralf Hoelzer <ralf@well.com>
5  * Copyright 2004, Devin Heitmueller <dheitmueller@netilla.com>
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "config.h"
27
28
29 #include <glib.h>
30
31 #include <string.h>
32
33 #include <epan/packet.h>
34 #include <epan/strutil.h>
35
36 #include "packet-aim.h"
37
38 void proto_register_aim_messaging(void);
39 void proto_reg_handoff_aim_messaging(void);
40
41
42 #define FAMILY_MESSAGING  0x0004
43
44
45 #define INCOMING_CH1_MESSAGE_BLOCK     0x0002
46 #define INCOMING_CH1_SERVER_ACK_REQ    0x0003
47 #define INCOMING_CH1_MESSAGE_AUTH_RESP 0x0004
48 #define INCOMING_CH1_MESSAGE_OFFLINE   0x0006
49 #define INCOMING_CH1_ICON_PRESENT      0x0008
50 #define INCOMING_CH1_BUDDY_REQ         0x0009
51 #define INCOMING_CH1_TYPING            0x000b
52
53 static const aim_tlv aim_messaging_incoming_ch1_tlvs[] = {
54         { INCOMING_CH1_MESSAGE_BLOCK, "Message Block", dissect_aim_tlv_value_messageblock },
55         { INCOMING_CH1_SERVER_ACK_REQ, "Server Ack Requested", dissect_aim_tlv_value_bytes },
56         { INCOMING_CH1_MESSAGE_AUTH_RESP, "Message is Auto Response", dissect_aim_tlv_value_bytes },
57         { INCOMING_CH1_MESSAGE_OFFLINE, "Message was received offline", dissect_aim_tlv_value_bytes },
58         { INCOMING_CH1_ICON_PRESENT, "Icon present", dissect_aim_tlv_value_bytes },
59         { INCOMING_CH1_BUDDY_REQ, "Buddy Req", dissect_aim_tlv_value_bytes },
60         { INCOMING_CH1_TYPING, "Non-direct connect typing notification", dissect_aim_tlv_value_bytes },
61         { 0, NULL, NULL },
62 };
63
64 static int dissect_aim_tlv_value_rendezvous(proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo);
65 static int dissect_aim_tlv_value_extended_data(proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_);
66
67 #define ICBM_CHANNEL_IM         0x0001
68 #define ICBM_CHANNEL_RENDEZVOUS 0x0002
69
70 static const value_string icbm_channel_types[] = {
71         { ICBM_CHANNEL_IM, "IM" },
72         { ICBM_CHANNEL_RENDEZVOUS, "Rendezvous" },
73         { 0, NULL },
74 };
75
76 #define INCOMING_CH2_SERVER_ACK_REQ        0x0003
77 #define INCOMING_CH2_RENDEZVOUS_DATA       0x0005
78
79 static const aim_tlv aim_messaging_incoming_ch2_tlvs[] = {
80         { INCOMING_CH2_SERVER_ACK_REQ, "Server Ack Requested", dissect_aim_tlv_value_bytes },
81         { INCOMING_CH2_RENDEZVOUS_DATA, "Rendez Vous Data", dissect_aim_tlv_value_rendezvous },
82         { 0, NULL, NULL },
83 };
84
85 #define RENDEZVOUS_TLV_CHANNEL                          0x0001
86 #define RENDEZVOUS_TLV_IP_ADDR                          0x0002
87 #define RENDEZVOUS_TLV_INT_IP                           0x0003
88 #define RENDEZVOUS_TLV_EXT_IP                           0x0004
89 #define RENDEZVOUS_TLV_EXT_PORT                         0x0005
90 #define RENDEZVOUS_TLV_DOWNLOAD_URL                     0x0006
91 #define RENDEZVOUS_TLV_VERIFIED_DOWNLOAD_URL            0x0008
92 #define RENDEZVOUS_TLV_SEQ_NUM                          0x000A
93 #define RENDEZVOUS_TLV_CANCEL_REASON                    0x000B
94 #define RENDEZVOUS_TLV_INVITATION                       0x000C
95 #define RENDEZVOUS_TLV_INVITE_MIME_CHARSET              0x000D
96 #define RENDEZVOUS_TLV_INVITE_MIME_LANG                 0x000E
97 #define RENDEZVOUS_TLV_REQ_HOST_CHECK                   0x000F
98 #define RENDEZVOUS_TLV_REQ_USE_ARS                      0x0010
99 #define RENDEZVOUS_TLV_REQ_SECURE                       0x0011
100 #define RENDEZVOUS_TLV_MAX_PROTOCOL_VER                 0x0012
101 #define RENDEZVOUS_TLV_MIN_PROTOCOL_VER                 0x0013
102 #define RENDEZVOUS_TLV_COUNTER_REASON                   0x0014
103 #define RENDEZVOUS_TLV_INVITE_MIME_TYPE                 0x0015
104 #define RENDEZVOUS_TLV_IP_ADDR_XOR                      0x0016
105 #define RENDEZVOUS_TLV_PORT_XOR                         0x0017
106 #define RENDEZVOUS_TLV_ADDR_LIST                        0x0018
107 #define RENDEZVOUS_TLV_SESSION_ID                       0x0019
108 #define RENDEZVOUS_TLV_ROLLOVER_ID                      0x001A
109 #define RENDEZVOUS_TLV_EXTENDED_DATA                    0x2711
110 #define RENDEZVOUS_TLV_ICHAT_INVITEES_DATA              0x277E
111
112 static const aim_tlv aim_rendezvous_tlvs[] = {
113         { RENDEZVOUS_TLV_CHANNEL, "Rendezvous ICBM Channel", dissect_aim_tlv_value_uint16 },
114         { RENDEZVOUS_TLV_IP_ADDR, "Rendezvous IP", dissect_aim_tlv_value_ipv4 },
115         { RENDEZVOUS_TLV_INT_IP, "Internal IP", dissect_aim_tlv_value_ipv4 },
116         { RENDEZVOUS_TLV_EXT_IP, "External IP", dissect_aim_tlv_value_ipv4 },
117         { RENDEZVOUS_TLV_EXT_PORT, "External Port", dissect_aim_tlv_value_uint16 },
118         { RENDEZVOUS_TLV_DOWNLOAD_URL, "Service Support Download URL", dissect_aim_tlv_value_string },
119         { RENDEZVOUS_TLV_VERIFIED_DOWNLOAD_URL, "Verified Service Support Download URL", dissect_aim_tlv_value_string },
120         { RENDEZVOUS_TLV_SEQ_NUM, "Sequence Number", dissect_aim_tlv_value_uint16 },
121         { RENDEZVOUS_TLV_CANCEL_REASON, "Cancel Reason", dissect_aim_tlv_value_uint16 },
122         { RENDEZVOUS_TLV_INVITATION, "Invitation Text", dissect_aim_tlv_value_string },
123         { RENDEZVOUS_TLV_INVITE_MIME_CHARSET, "Data MIME Type", dissect_aim_tlv_value_string },
124         { RENDEZVOUS_TLV_INVITE_MIME_LANG, "Data Language", dissect_aim_tlv_value_string },
125         { RENDEZVOUS_TLV_REQ_HOST_CHECK, "Request Host Check", NULL },
126         { RENDEZVOUS_TLV_REQ_USE_ARS, "Request Data via Rendezvous Server", NULL },
127         { RENDEZVOUS_TLV_REQ_SECURE, "Request SSL Connection", NULL },
128         { RENDEZVOUS_TLV_MAX_PROTOCOL_VER, "Maximum Protocol Version", dissect_aim_tlv_value_uint16 },
129         { RENDEZVOUS_TLV_MIN_PROTOCOL_VER, "Minimum Protocol Version", dissect_aim_tlv_value_uint16 },
130         { RENDEZVOUS_TLV_COUNTER_REASON, "Counter Proposal Reason", dissect_aim_tlv_value_uint16 },
131         { RENDEZVOUS_TLV_INVITE_MIME_TYPE, "Data MIME Type", dissect_aim_tlv_value_string },
132         { RENDEZVOUS_TLV_IP_ADDR_XOR, "XORed Rendezvous IP", dissect_aim_tlv_value_ipv4 },
133         { RENDEZVOUS_TLV_PORT_XOR, "XORed Port", dissect_aim_tlv_value_uint16 },
134         { RENDEZVOUS_TLV_ADDR_LIST, "Address/Port List", dissect_aim_tlv_value_string08_array },
135         { RENDEZVOUS_TLV_SESSION_ID, "Session ID", dissect_aim_tlv_value_string },
136         { RENDEZVOUS_TLV_ROLLOVER_ID, "Rollover ID", dissect_aim_tlv_value_string },
137 /*
138         The dissect_aim_tlv_value_extended_data function does not work for iChat generated rendezvous data
139         { RENDEZVOUS_TLV_EXTENDED_DATA, "Extended Data", dissect_aim_tlv_value_extended_data },
140 */
141         { RENDEZVOUS_TLV_EXTENDED_DATA, "Extended Data", NULL },
142         { RENDEZVOUS_TLV_ICHAT_INVITEES_DATA, "iChat Invitees Data", NULL },
143         { 0, NULL, NULL },
144 };
145
146 #define MINITYPING_FINISHED_SIGN                        0x0000
147 #define MINITYPING_TEXT_TYPED_SIGN                      0x0001
148 #define MINITYPING_BEGUN_SIGN                           0x0002
149
150 static const value_string minityping_type[] _U_ = {
151         {MINITYPING_FINISHED_SIGN, "Typing finished sign" },
152         {MINITYPING_TEXT_TYPED_SIGN, "Text typed sign" },
153         {MINITYPING_BEGUN_SIGN, "Typing begun sign" },
154         {0, NULL }
155 };
156
157 #define RENDEZVOUS_MSG_REQUEST          0
158 #define RENDEZVOUS_MSG_CANCEL           1
159 #define RENDEZVOUS_MSG_ACCEPT           2
160
161 static const value_string rendezvous_msg_types[] = {
162         { RENDEZVOUS_MSG_REQUEST, "Request" },
163         { RENDEZVOUS_MSG_CANCEL, "Cancel" },
164         { RENDEZVOUS_MSG_ACCEPT, "Accept" },
165         { 0, NULL },
166 };
167
168 #define CLIENT_ERR__REASON_UNSUPPORTED_CHANNEL  1
169 #define CLIENT_ERR__REASON_BUSTED_PAYLOAD       2
170 #define CLIENT_ERR__REASON_CHANNEL_SPECIFIC     3
171
172 static const value_string client_err_reason_types[] = {
173         { CLIENT_ERR__REASON_UNSUPPORTED_CHANNEL, "Unsupported Channel" },
174         { CLIENT_ERR__REASON_BUSTED_PAYLOAD, "Busted Payload" },
175         { CLIENT_ERR__REASON_CHANNEL_SPECIFIC, "Channel Specific Error" },
176         { 0, NULL },
177 };
178
179 #define RENDEZVOUS_NAK_PROPOSAL_UNSUPPORTED 0
180 #define RENDEZVOUS_NAK_PROPOSAL_DENIED 1
181 #define RENDEZVOUS_NAK_PROPOSAL_IGNORED 2
182 #define RENDEZVOUS_NAK_BUSTED_PARAMETERS 3
183 #define RENDEZVOUS_NAK_PROPOSAL_TIMED_OUT 4
184 #define RENDEZVOUS_NAK_ONLINE_BUT_NOT_AVAILABLE 5
185 #define RENDEZVOUS_NAK_INSUFFICIENT_RESOURCES 6
186 #define RENDEZVOUS_NAK_RATE_LIMITED 7
187 #define RENDEZVOUS_NAK_NO_DATA 8
188 #define RENDEZVOUS_NAK_VERSION_MISMATCH 9
189 #define RENDEZVOUS_NAK_SECURITY_MISMATCH 10
190 #define RENDEZVOUS_NAK_SERVICE_SPECIFIC_REASON 15
191
192 static const value_string rendezvous_nak_reason_types[] = {
193         { RENDEZVOUS_NAK_PROPOSAL_UNSUPPORTED, "Proposal UUID not supported" },
194         { RENDEZVOUS_NAK_PROPOSAL_DENIED, "Not authorized, or user declined" },
195         { RENDEZVOUS_NAK_PROPOSAL_IGNORED, "Proposal ignored" },
196         { RENDEZVOUS_NAK_BUSTED_PARAMETERS, "Proposal malformed" },
197         { RENDEZVOUS_NAK_PROPOSAL_TIMED_OUT, "Attempt to act on proposal (e.g. connect) timed out" },
198         { RENDEZVOUS_NAK_ONLINE_BUT_NOT_AVAILABLE, "Recipient away or busy" },
199         { RENDEZVOUS_NAK_INSUFFICIENT_RESOURCES, "Recipient had internal error" },
200         { RENDEZVOUS_NAK_RATE_LIMITED, "Recipient was ratelimited" },
201         { RENDEZVOUS_NAK_NO_DATA, "Recipient had nothing to send" },
202         { RENDEZVOUS_NAK_VERSION_MISMATCH, "Incompatible versions" },
203         { RENDEZVOUS_NAK_SECURITY_MISMATCH, "Incompatible security settings" },
204         { RENDEZVOUS_NAK_SERVICE_SPECIFIC_REASON, "Service-specific reject defined by client" },
205         { 0, NULL },
206 };
207
208 #define EXTENDED_DATA_MTYPE_PLAIN 0x01
209 #define EXTENDED_DATA_MTYPE_CHAT 0x02
210 #define EXTENDED_DATA_MTYPE_FILEREQ 0x03
211 #define EXTENDED_DATA_MTYPE_URL 0x04
212 #define EXTENDED_DATA_MTYPE_AUTHREQ 0x06
213 #define EXTENDED_DATA_MTYPE_AUTHDENY 0x07
214 #define EXTENDED_DATA_MTYPE_AUTHOK 0x08
215 #define EXTENDED_DATA_MTYPE_SERVER 0x09
216 #define EXTENDED_DATA_MTYPE_ADDED 0x0C
217 #define EXTENDED_DATA_MTYPE_WWP 0x0D
218 #define EXTENDED_DATA_MTYPE_EEXPRESS 0x0E
219 #define EXTENDED_DATA_MTYPE_CONTACTS 0x13
220 #define EXTENDED_DATA_MTYPE_PLUGIN 0x1A
221 #define EXTENDED_DATA_MTYPE_AUTOAWAY 0xE8
222 #define EXTENDED_DATA_MTYPE_AUTOBUSY 0xE9
223 #define EXTENDED_DATA_MTYPE_AUTONA 0xEA
224 #define EXTENDED_DATA_MTYPE_AUTODND 0xEB
225 #define EXTENDED_DATA_MTYPE_AUTOFFC 0xEC
226
227 static const value_string extended_data_message_types[] = {
228         {EXTENDED_DATA_MTYPE_PLAIN, "Plain text (simple) message"},
229         {EXTENDED_DATA_MTYPE_CHAT, "Chat request message"},
230         {EXTENDED_DATA_MTYPE_FILEREQ, "File request / file ok message"},
231         {EXTENDED_DATA_MTYPE_URL, "URL message (0xFE formatted)"},
232         {EXTENDED_DATA_MTYPE_AUTHREQ, "Authorization request message (0xFE formatted)"},
233         {EXTENDED_DATA_MTYPE_AUTHDENY, "Authorization denied message (0xFE formatted)"},
234         {EXTENDED_DATA_MTYPE_AUTHOK, "Authorization given message (empty)"},
235         {EXTENDED_DATA_MTYPE_SERVER, "Message from OSCAR server (0xFE formatted)"},
236         {EXTENDED_DATA_MTYPE_ADDED, "\"You-were-added\" message (0xFE formatted)"},
237         {EXTENDED_DATA_MTYPE_WWP, "Web pager message (0xFE formatted)"},
238         {EXTENDED_DATA_MTYPE_EEXPRESS, "Email express message (0xFE formatted)"},
239         {EXTENDED_DATA_MTYPE_CONTACTS, "Contact list message"},
240         {EXTENDED_DATA_MTYPE_PLUGIN, "Plugin message described by text string"},
241         {EXTENDED_DATA_MTYPE_AUTOAWAY, "Auto away message"},
242         {EXTENDED_DATA_MTYPE_AUTOBUSY, "Auto occupied message"},
243         {EXTENDED_DATA_MTYPE_AUTONA, "Auto not available message"},
244         {EXTENDED_DATA_MTYPE_AUTODND, "Auto do not disturb message"},
245         {EXTENDED_DATA_MTYPE_AUTOFFC, "Auto free for chat message"},
246         { 0, NULL },
247 };
248
249 #define EXTENDED_DATA_MFLAG_NORMAL 0x01
250 #define EXTENDED_DATA_MFLAG_AUTO 0x03
251 #define EXTENDED_DATA_MFLAG_MULTI 0x80
252
253 #define EVIL_ORIGIN_ANONYMOUS           1
254 #define EVIL_ORIGIN_NONANONYMOUS        2
255
256 static const value_string evil_origins[] = {
257         {EVIL_ORIGIN_ANONYMOUS, "Anonymous"},
258         {EVIL_ORIGIN_NONANONYMOUS, "Non-Anonymous"},
259         {0, NULL },
260 };
261
262 /* Initialize the protocol and registered fields */
263 static int proto_aim_messaging = -1;
264 static int hf_aim_icbm_channel = -1;
265 static int hf_aim_icbm_cookie = -1;
266 static int hf_aim_icbm_msg_flags = -1;
267 static int hf_aim_icbm_max_sender_warnlevel = -1;
268 static int hf_aim_icbm_max_receiver_warnlevel = -1;
269 static int hf_aim_icbm_max_snac_size = -1;
270 static int hf_aim_icbm_min_msg_interval = -1;
271 static int hf_aim_icbm_notification_cookie = -1;
272 static int hf_aim_icbm_notification_channel = -1;
273 static int hf_aim_icbm_notification_type = -1;
274 static int hf_aim_icbm_rendezvous_nak = -1;
275 static int hf_aim_icbm_rendezvous_nak_length = -1;
276 static int hf_aim_message_channel_id = -1;
277 static int hf_aim_icbm_evil = -1;
278 static int hf_aim_evil_warn_level = -1;
279 static int hf_aim_evil_new_warn_level = -1;
280 static int hf_aim_rendezvous_msg_type = -1;
281 static int hf_aim_icbm_client_err_reason = -1;
282 static int hf_aim_icbm_client_err_protocol_version = -1;
283 static int hf_aim_icbm_client_err_client_caps_flags = -1;
284 static int hf_aim_rendezvous_extended_data_message_type = -1;
285 static int hf_aim_rendezvous_extended_data_message_flags = -1;
286 static int hf_aim_rendezvous_extended_data_message_flags_normal = -1;
287 static int hf_aim_rendezvous_extended_data_message_flags_auto = -1;
288 static int hf_aim_rendezvous_extended_data_message_flags_multi = -1;
289 static int hf_aim_rendezvous_extended_data_message_status_code = -1;
290 static int hf_aim_rendezvous_extended_data_message_priority_code = -1;
291 static int hf_aim_rendezvous_extended_data_message_text_length = -1;
292 /* static int hf_aim_rendezvous_extended_data_message_text = -1; */
293
294 /* Initialize the subtree pointers */
295 static gint ett_aim_messaging = -1;
296 static gint ett_aim_rendezvous_data = -1;
297 static gint ett_aim_extended_data = -1;
298 static gint ett_aim_extended_data_message_flags = -1;
299
300 static int
301 dissect_aim_tlv_value_rendezvous(proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo)
302 {
303         int offset = 0;
304         proto_tree *entry = proto_item_add_subtree(ti, ett_aim_rendezvous_data);
305         proto_tree_add_item(entry, hf_aim_rendezvous_msg_type, tvb, offset, 2, ENC_BIG_ENDIAN);
306         offset += 2;
307
308         proto_tree_add_item(entry, hf_aim_icbm_cookie, tvb, offset, 8, ENC_NA);
309         offset += 8;
310
311         offset = dissect_aim_capability(entry, tvb, offset);
312
313         return dissect_aim_tlv_sequence(tvb, pinfo, offset, entry,
314                                         aim_rendezvous_tlvs);
315 }
316
317 static int
318 dissect_aim_msg_outgoing(tvbuff_t *tvb, packet_info *pinfo, proto_tree *msg_tree)
319 {
320         int offset = 0;
321         const aim_tlv *aim_ch_tlvs = NULL;
322         guint16 channel_id;
323         guchar buddyname[MAX_BUDDYNAME_LENGTH+1];
324         int buddyname_length;
325
326         /* ICBM Cookie */
327         proto_tree_add_item(msg_tree, hf_aim_icbm_cookie, tvb, offset, 8, ENC_NA);
328         offset += 8;
329
330         /* Message Channel ID */
331         channel_id = tvb_get_ntohs(tvb, offset);
332         proto_tree_add_item(msg_tree, hf_aim_message_channel_id, tvb, offset, 2,
333                             ENC_BIG_ENDIAN);
334         offset += 2;
335
336         /* Add the outgoing username to the info column */
337         buddyname_length = aim_get_buddyname(buddyname, tvb, offset,
338                                                           offset + 1);
339         col_append_fstr(pinfo->cinfo, COL_INFO, " to: %s",
340                         format_text(buddyname, buddyname_length));
341
342         offset = dissect_aim_buddyname(tvb, pinfo, offset, msg_tree);
343
344         switch(channel_id) {
345         case ICBM_CHANNEL_IM: aim_ch_tlvs = aim_messaging_incoming_ch1_tlvs; break;
346         case ICBM_CHANNEL_RENDEZVOUS: aim_ch_tlvs = aim_messaging_incoming_ch2_tlvs; break;
347         default: return offset;
348         }
349
350         return dissect_aim_tlv_sequence(tvb, pinfo, offset, msg_tree, aim_ch_tlvs);
351 }
352
353
354 static int
355 dissect_aim_msg_incoming(tvbuff_t *tvb, packet_info *pinfo, proto_tree *msg_tree)
356 {
357         int offset = 0;
358         const aim_tlv *aim_ch_tlvs;
359         guint16 channel_id;
360
361         /* ICBM Cookie */
362         proto_tree_add_item(msg_tree, hf_aim_icbm_cookie, tvb, offset, 8, ENC_NA);
363         offset += 8;
364
365         /* Message Channel ID */
366         proto_tree_add_item(msg_tree, hf_aim_message_channel_id, tvb, offset, 2,
367                             ENC_BIG_ENDIAN);
368         channel_id = tvb_get_ntohs(tvb, offset);
369         offset += 2;
370
371         offset = dissect_aim_userinfo(tvb, pinfo, offset, msg_tree);
372
373         switch(channel_id) {
374         case ICBM_CHANNEL_IM: aim_ch_tlvs = aim_messaging_incoming_ch1_tlvs; break;
375         case ICBM_CHANNEL_RENDEZVOUS: aim_ch_tlvs = aim_messaging_incoming_ch2_tlvs; break;
376         default: return offset;
377         }
378
379         return dissect_aim_tlv_sequence(tvb, pinfo, offset, msg_tree, aim_ch_tlvs);
380 }
381
382 static int
383 dissect_aim_msg_params(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *msg_tree)
384 {
385         int offset = 0;
386         proto_tree_add_item(msg_tree, hf_aim_icbm_channel, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
387         proto_tree_add_item(msg_tree, hf_aim_icbm_msg_flags, tvb, offset, 4, ENC_BIG_ENDIAN); offset+=4;
388         proto_tree_add_item(msg_tree, hf_aim_icbm_max_snac_size, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
389         proto_tree_add_item(msg_tree, hf_aim_icbm_max_sender_warnlevel, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
390         proto_tree_add_item(msg_tree, hf_aim_icbm_max_receiver_warnlevel, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
391         proto_tree_add_item(msg_tree, hf_aim_icbm_min_msg_interval, tvb, offset, 4, ENC_BIG_ENDIAN); offset+=4;
392         return offset;
393 }
394
395 static int
396 dissect_aim_msg_evil_req(tvbuff_t *tvb, packet_info *pinfo, proto_tree *msg_tree)
397 {
398         int offset = 0;
399         proto_tree_add_item(msg_tree, hf_aim_icbm_evil, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
400         return dissect_aim_buddyname(tvb, pinfo, offset, msg_tree);
401 }
402
403
404 static int
405 dissect_aim_msg_evil_repl(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *msg_tree)
406 {
407         int offset = 0;
408         proto_tree_add_item(msg_tree, hf_aim_evil_warn_level, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
409         proto_tree_add_item(msg_tree, hf_aim_evil_new_warn_level, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
410         return offset;
411 }
412
413 static int
414 dissect_aim_msg_minityping(tvbuff_t *tvb, packet_info *pinfo, proto_tree *msg_tree)
415 {
416         int offset = 0;
417         proto_tree_add_item(msg_tree,hf_aim_icbm_notification_cookie, tvb, offset, 8, ENC_NA); offset+=8;
418         proto_tree_add_item(msg_tree,hf_aim_icbm_notification_channel, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
419         offset = dissect_aim_buddyname(tvb, pinfo, offset, msg_tree);
420         proto_tree_add_item(msg_tree,hf_aim_icbm_notification_type, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
421         return offset;
422 }
423
424 typedef struct _aim_client_plugin
425 {
426         const char *name;
427         e_uuid_t uuid;
428 } aim_client_plugin;
429
430 static const aim_client_plugin known_client_plugins[] = {
431         { "None",
432          {0x0, 0x0, 0x0,
433          {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}},
434
435         { "Status Manager",
436          {0xD140CF10, 0xE94F, 0x11D3,
437          {0xBC, 0xD2, 0x00, 0x04, 0xAC, 0x96, 0xDD, 0x96}}},
438
439         { NULL, {0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 } } }
440 };
441
442 static const
443 aim_client_plugin *aim_find_plugin ( e_uuid_t uuid)
444 {
445         int i;
446
447         for(i = 0; known_client_plugins[i].name; i++)
448         {
449                 const aim_client_plugin *plugin = &(known_client_plugins[i]);
450
451                 if(memcmp(&(plugin->uuid), &uuid, sizeof(e_uuid_t)) == 0)
452                         return plugin;
453         }
454
455         return NULL;
456 }
457
458 static int
459 dissect_aim_plugin(proto_tree *entry, tvbuff_t *tvb, int offset, e_uuid_t* out_plugin_uuid)
460 {
461         const aim_client_plugin *plugin = NULL;
462         e_uuid_t uuid;
463
464         uuid.Data1 = tvb_get_ntohl(tvb, offset);
465         uuid.Data2 = tvb_get_ntohs(tvb, offset+4);
466         uuid.Data3 = tvb_get_ntohs(tvb, offset+6);
467         tvb_memcpy(tvb, uuid.Data4, offset+8, 8);
468         if (out_plugin_uuid)
469                 *out_plugin_uuid = uuid;
470
471         plugin = aim_find_plugin(uuid);
472
473         proto_tree_add_text(entry, tvb, offset, 16,
474                 "Plugin: %s {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
475                 plugin ? plugin->name:"Unknown", uuid.Data1, uuid.Data2,
476                 uuid.Data3, uuid.Data4[0], uuid.Data4[1], uuid.Data4[2],
477                 uuid.Data4[3], uuid.Data4[4],   uuid.Data4[5], uuid.Data4[6],
478                 uuid.Data4[7]
479         );
480
481         return offset+16;
482 }
483
484 static int
485 dissect_aim_rendezvous_extended_message(tvbuff_t *tvb, proto_tree *msg_tree)
486 {
487         guint8 /*message_type,*/message_flags;
488         int offset = 0;
489         proto_item *ti_flags;
490         proto_tree *flags_entry;
491         guint16 text_length;
492         guint8* text;
493
494         /* TODO: parse and present message_type */
495         /* message_type = tvb_get_guint8(tvb, offset); */
496         proto_tree_add_item(msg_tree, hf_aim_rendezvous_extended_data_message_type, tvb, offset, 1, ENC_BIG_ENDIAN); offset+=1;
497         message_flags = tvb_get_guint8(tvb, offset);
498         ti_flags = proto_tree_add_uint(msg_tree, hf_aim_rendezvous_extended_data_message_flags, tvb, offset, 1, message_flags);
499         flags_entry = proto_item_add_subtree(ti_flags, ett_aim_extended_data_message_flags);
500         proto_tree_add_boolean(flags_entry, hf_aim_rendezvous_extended_data_message_flags_normal, tvb, offset, 1, message_flags);
501         proto_tree_add_boolean(flags_entry, hf_aim_rendezvous_extended_data_message_flags_auto, tvb, offset, 1, message_flags);
502         proto_tree_add_boolean(flags_entry, hf_aim_rendezvous_extended_data_message_flags_multi, tvb, offset, 1, message_flags);
503         offset+=1;
504         proto_tree_add_item(msg_tree, hf_aim_rendezvous_extended_data_message_status_code, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
505         proto_tree_add_item(msg_tree, hf_aim_rendezvous_extended_data_message_priority_code, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
506         text_length = tvb_get_letohs(tvb, offset);
507         proto_tree_add_item(msg_tree, hf_aim_rendezvous_extended_data_message_text_length, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
508         text = tvb_get_string_enc(wmem_packet_scope(), tvb, offset, text_length, ENC_ASCII|ENC_NA);
509         proto_tree_add_text(msg_tree, tvb, offset, text_length, "Text: %s", text); /* offset+=text_length; */
510
511         offset = tvb_length(tvb);
512
513         return offset;
514 }
515
516 static int
517 is_uuid_null(e_uuid_t uuid)
518 {
519         return (uuid.Data1 == 0) &&
520                (uuid.Data2 == 0) &&
521                (uuid.Data3 == 0) &&
522                (uuid.Data4[0] == 0) &&
523                (uuid.Data4[1] == 0) &&
524                (uuid.Data4[2] == 0) &&
525                (uuid.Data4[3] == 0) &&
526                (uuid.Data4[4] == 0) &&
527                (uuid.Data4[5] == 0) &&
528                (uuid.Data4[6] == 0) &&
529                (uuid.Data4[7] == 0);
530 }
531
532 static int
533 dissect_aim_tlv_value_extended_data(proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_)
534 {
535         int offset = 0;
536         guint16 length/*, protocol_version*/;
537         int start_offset;
538         proto_tree *entry;
539         e_uuid_t plugin_uuid;
540
541         entry = proto_item_add_subtree(ti, ett_aim_extended_data);
542         length = tvb_get_letohs(tvb, offset);
543         proto_tree_add_text(entry, tvb, offset, 2, "Length: %d", length); offset+=2;
544         start_offset = offset;
545         /* TODO: parse and present protocol version */
546         /* protocol_version = tvb_get_ntohs(tvb, offset); */
547         proto_tree_add_item(entry, hf_aim_icbm_client_err_protocol_version, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
548
549         offset = dissect_aim_plugin(entry, tvb, offset, &plugin_uuid);
550         proto_tree_add_text(entry, tvb, offset, 2, "Unknown"); offset += 2;
551         proto_tree_add_item(entry, hf_aim_icbm_client_err_client_caps_flags, tvb, offset, 4, ENC_BIG_ENDIAN); offset+=4;
552         proto_tree_add_text(entry, tvb, offset, 1, "Unknown");  offset += 1;
553         proto_tree_add_text(entry, tvb, offset, 2, "Downcounter?"); /* offset += 2;*/
554
555         offset = start_offset + length;
556
557         length = tvb_get_letohs(tvb, offset);
558         proto_tree_add_text(entry, tvb, offset, 2, "Length: %d", length); offset+=2;
559         start_offset = offset;
560         proto_tree_add_text(entry, tvb, offset, 2, "Downcounter?"); offset += 2;
561         proto_tree_add_text(entry, tvb, offset, length-2, "Unknown");
562         offset = start_offset + length;
563
564         if (is_uuid_null(plugin_uuid))
565         {
566                 /* a message follows */
567                 tvbuff_t *subtvb = tvb_new_subset_remaining(tvb, offset);
568                 /* offset += */ dissect_aim_rendezvous_extended_message(subtvb, entry);
569         }
570         else
571         {
572                 /* plugin-specific data follows */
573                 proto_tree_add_text(entry, tvb, offset, -1, "Plugin-specific data");
574         }
575         offset = tvb_length(tvb);
576
577         return offset;
578 }
579
580 static int
581 dissect_aim_msg_ack(tvbuff_t *tvb, packet_info *pinfo, proto_tree *msg_tree)
582 {
583         int offset = 0;
584
585         proto_tree_add_item(msg_tree,hf_aim_icbm_cookie, tvb, offset, 8, ENC_NA); offset+=8;
586
587         proto_tree_add_item(msg_tree, hf_aim_message_channel_id, tvb, offset, 2,
588                             ENC_BIG_ENDIAN); offset += 2;
589
590         offset = dissect_aim_buddyname(tvb, pinfo, offset, msg_tree);
591
592         return offset;
593 }
594
595 static int
596 dissect_aim_msg_client_err(tvbuff_t *tvb, packet_info *pinfo, proto_tree *msg_tree)
597 {
598         int offset = 0;
599         guint16 channel, reason;
600
601         proto_tree_add_item(msg_tree,hf_aim_icbm_cookie, tvb, offset, 8, ENC_NA); offset+=8;
602         channel = tvb_get_ntohs(tvb, offset);
603         proto_tree_add_item(msg_tree,hf_aim_icbm_channel, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
604         offset = dissect_aim_buddyname(tvb, pinfo, offset, msg_tree);
605         reason = tvb_get_ntohs(tvb, offset);
606         proto_tree_add_item(msg_tree, hf_aim_icbm_client_err_reason, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
607
608         if (reason == CLIENT_ERR__REASON_CHANNEL_SPECIFIC && tvb_length_remaining(tvb, offset) > 0)
609         {
610                 switch (channel)
611                 {
612                 case ICBM_CHANNEL_RENDEZVOUS:
613                         proto_tree_add_item(msg_tree, hf_aim_icbm_rendezvous_nak_length, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
614                         proto_tree_add_item(msg_tree, hf_aim_icbm_rendezvous_nak, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
615                         break;
616
617                 default:
618                     {
619                         tvbuff_t *subtvb = tvb_new_subset_remaining(tvb, offset);
620                         proto_item *ti_extended_data = proto_tree_add_text(msg_tree, tvb, offset, -1, "Extended Data");
621                         dissect_aim_tlv_value_extended_data(ti_extended_data, 0, subtvb, pinfo);
622                         break;
623                     }
624                 }
625         }
626
627         return offset;
628 }
629
630 static const aim_subtype aim_fnac_family_messaging[] = {
631         { 0x0001, "Error", dissect_aim_snac_error },
632         { 0x0002, "Set ICBM Parameter", dissect_aim_msg_params },
633         { 0x0003, "Reset ICBM Parameter", NULL },
634         { 0x0004, "Request Parameter Info", NULL},
635         { 0x0005, "Parameter Info", dissect_aim_msg_params },
636         { 0x0006, "Outgoing", dissect_aim_msg_outgoing },
637         { 0x0007, "Incoming", dissect_aim_msg_incoming },
638         { 0x0008, "Evil Request", dissect_aim_msg_evil_req },
639         { 0x0009, "Evil Response", dissect_aim_msg_evil_repl  },
640         { 0x000a, "Missed Call", NULL },
641         { 0x000b, "Client Error", dissect_aim_msg_client_err },
642         { 0x000c, "Acknowledge", dissect_aim_msg_ack },
643         { 0x0014, "Mini Typing Notifications (MTN)", dissect_aim_msg_minityping },
644         { 0, NULL, NULL }
645 };
646
647
648
649 /* Register the protocol with Wireshark */
650 void
651 proto_register_aim_messaging(void)
652 {
653
654         /* Setup list of header fields */
655         static hf_register_info hf[] = {
656                 { &hf_aim_icbm_channel,
657                         { "Channel", "aim_messaging.icbm.channel", FT_UINT16, BASE_HEX, VALS(icbm_channel_types), 0x0, NULL, HFILL },
658                 },
659                 { &hf_aim_icbm_msg_flags,
660                         { "Message Flags", "aim_messaging.icbm.flags", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL },
661                 },
662                 { &hf_aim_icbm_max_snac_size,
663                         { "Max SNAC Size", "aim_messaging.icbm.max_snac", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL },
664                 },
665                 { &hf_aim_icbm_max_sender_warnlevel,
666                         { "Max sender warn level", "aim_messaging.icbm.max_sender_warn-level", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL },
667                 },
668                 { &hf_aim_icbm_max_receiver_warnlevel,
669                         { "max receiver warn level", "aim_messaging.icbm.max_receiver_warnlevel", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL },
670                 },
671                 { &hf_aim_icbm_min_msg_interval,
672                         { "Minimum message interval (milliseconds)", "aim_messaging.icbm.min_msg_interval", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL },
673                 },
674                 { &hf_aim_icbm_cookie,
675                         { "ICBM Cookie", "aim_messaging.icbmcookie", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }
676                 },
677                 { &hf_aim_message_channel_id,
678                         { "Message Channel ID", "aim_messaging.channelid", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }
679                 },
680                 { &hf_aim_icbm_evil,
681                         { "Send Evil Bit As", "aim_messaging.evilreq.origin", FT_UINT16, BASE_DEC, VALS(evil_origins), 0x0, NULL, HFILL },
682                 },
683                 { &hf_aim_evil_warn_level,
684                         { "Old warning level", "aim_messaging.evil.warn_level", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
685                 },
686                 { &hf_aim_evil_new_warn_level,
687                         { "New warning level", "aim_messaging.evil.new_warn_level", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
688                 },
689                 { &hf_aim_icbm_notification_cookie,
690                         { "Notification Cookie", "aim_messaging.notification.cookie", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL },
691                 },
692                 { &hf_aim_icbm_notification_channel,
693                         { "Notification Channel", "aim_messaging.notification.channel", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
694                 },
695                 { &hf_aim_icbm_notification_type,
696                         { "Notification Type", "aim_messaging.notification.type", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
697                 },
698                 { &hf_aim_icbm_rendezvous_nak,
699                         { "Rendezvous NAK reason", "aim_messaging.rendezvous_nak", FT_UINT16, BASE_HEX, VALS(rendezvous_nak_reason_types), 0x0, NULL, HFILL },
700                 },
701                 { &hf_aim_icbm_rendezvous_nak_length,
702                         { "Rendezvous NAK reason length", "aim_messaging.rendezvous_nak_length", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL },
703                 },
704                 { &hf_aim_rendezvous_msg_type,
705                         { "Message Type", "aim_messaging.rendezvous.msg_type", FT_UINT16, BASE_HEX, VALS(rendezvous_msg_types), 0x0, NULL, HFILL },
706                 },
707                 { &hf_aim_icbm_client_err_reason,
708                         { "Reason", "aim_messaging.clienterr.reason", FT_UINT16, BASE_DEC, VALS(client_err_reason_types), 0x0, NULL, HFILL },
709                 },
710                 { &hf_aim_icbm_client_err_protocol_version,
711                         { "Version", "aim_messaging.clienterr.protocol_version", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
712                 },
713                 { &hf_aim_icbm_client_err_client_caps_flags,
714                         { "Client Capabilities Flags", "aim_messaging.clienterr.client_caps_flags", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL },
715                 },
716                 { &hf_aim_rendezvous_extended_data_message_type,
717                         { "Message Type", "aim_messaging.icbm.extended_data.message.type", FT_UINT8, BASE_HEX, VALS(extended_data_message_types), 0x0, NULL, HFILL },
718                 },
719                 { &hf_aim_rendezvous_extended_data_message_flags,
720                         { "Message Flags", "aim_messaging.icbm.extended_data.message.flags", FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL },
721                 },
722                 { &hf_aim_rendezvous_extended_data_message_flags_normal,
723                         { "Normal Message", "aim_messaging.icbm.extended_data.message.flags.normal", FT_BOOLEAN, 16, TFS(&tfs_set_notset), EXTENDED_DATA_MFLAG_NORMAL, NULL, HFILL },
724                 },
725                 { &hf_aim_rendezvous_extended_data_message_flags_auto,
726                         { "Auto Message", "aim_messaging.icbm.extended_data.message.flags.auto", FT_BOOLEAN, 16, TFS(&tfs_set_notset), EXTENDED_DATA_MFLAG_AUTO, NULL, HFILL },
727                 },
728                 { &hf_aim_rendezvous_extended_data_message_flags_multi,
729                         { "Multiple Recipients Message", "aim_messaging.icbm.rendezvous.extended_data.message.flags.multi", FT_BOOLEAN, 16, TFS(&tfs_set_notset), EXTENDED_DATA_MFLAG_MULTI, NULL, HFILL },
730                 },
731                 { &hf_aim_rendezvous_extended_data_message_status_code,
732                         { "Status Code", "aim_messaging.icbm.extended_data.message.status_code", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
733                 },
734                 { &hf_aim_rendezvous_extended_data_message_priority_code,
735                         { "Priority Code", "aim_messaging.icbm.extended_data.message.priority_code", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
736                 },
737                 { &hf_aim_rendezvous_extended_data_message_text_length,
738                         { "Text Length", "aim_messaging.icbm.extended_data.message.text_length", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
739                 },
740 #if 0
741                 { &hf_aim_rendezvous_extended_data_message_text,
742                         { "Text", "aim_messaging.icbm.extended_data.message.text", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL },
743                 }
744 #endif
745         };
746
747         /* Setup protocol subtree array */
748         static gint *ett[] = {
749                 &ett_aim_messaging,
750                 &ett_aim_rendezvous_data,
751                 &ett_aim_extended_data,
752                 &ett_aim_extended_data_message_flags
753         };
754
755         /* Register the protocol name and description */
756         proto_aim_messaging = proto_register_protocol("AIM Messaging", "AIM Messaging", "aim_messaging");
757
758         /* Required function calls to register the header fields and subtrees used */
759         proto_register_field_array(proto_aim_messaging, hf, array_length(hf));
760         proto_register_subtree_array(ett, array_length(ett));
761 }
762
763 void
764 proto_reg_handoff_aim_messaging(void)
765 {
766         aim_init_family(proto_aim_messaging, ett_aim_messaging, FAMILY_MESSAGING, aim_fnac_family_messaging);
767 }