Treat TVBs as opaque: use the accessor functions instead of accessing the fields
[obnox/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  * $Id$
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
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
33 #include <glib.h>
34
35 #include <string.h>
36
37 #include <epan/packet.h>
38 #include <epan/strutil.h>
39
40 #include "packet-aim.h"
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_BIG_ENDIAN);
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_BIG_ENDIAN);
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_BIG_ENDIAN);
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_BIG_ENDIAN); 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_item(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_ephemeral_string(tvb, offset, text_length);
509         proto_tree_add_text(msg_tree, tvb, offset, text_length, "Text: %s", text); offset+=text_length;
510         offset = tvb_length(tvb);
511
512         return offset;
513 }
514
515 static int
516 is_uuid_null(e_uuid_t uuid)
517 {
518         return (uuid.Data1 == 0) &&
519                (uuid.Data2 == 0) &&
520                (uuid.Data3 == 0) &&
521                (uuid.Data4[0] == 0) &&
522                (uuid.Data4[1] == 0) &&
523                (uuid.Data4[2] == 0) &&
524                (uuid.Data4[3] == 0) &&
525                (uuid.Data4[4] == 0) &&
526                (uuid.Data4[5] == 0) &&
527                (uuid.Data4[6] == 0) &&
528                (uuid.Data4[7] == 0);
529 }
530
531 static int
532 dissect_aim_tlv_value_extended_data(proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_)
533 {
534         int offset = 0;
535         guint16 length/*, protocol_version*/;
536         int start_offset;
537         proto_tree *entry;
538         e_uuid_t plugin_uuid;
539
540         entry = proto_item_add_subtree(ti, ett_aim_extended_data);
541         length = tvb_get_letohs(tvb, offset);
542         proto_tree_add_text(entry, tvb, offset, 2, "Length: %d", length); offset+=2;
543         start_offset = offset;
544         /* TODO: parse and present protocol version */
545         /* protocol_version = tvb_get_ntohs(tvb, offset); */
546         proto_tree_add_item(entry, hf_aim_icbm_client_err_protocol_version, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
547
548         offset = dissect_aim_plugin(entry, tvb, offset, &plugin_uuid);
549         proto_tree_add_text(entry, tvb, offset, 2, "Unknown"); offset += 2;
550         proto_tree_add_item(entry, hf_aim_icbm_client_err_client_caps_flags, tvb, offset, 4, ENC_BIG_ENDIAN); offset+=4;
551         proto_tree_add_text(entry, tvb, offset, 1, "Unknown");  offset += 1;
552         proto_tree_add_text(entry, tvb, offset, 2, "Downcounter?"); offset += 2;
553
554         offset = start_offset + length;
555
556         length = tvb_get_letohs(tvb, offset);
557         proto_tree_add_text(entry, tvb, offset, 2, "Length: %d", length); offset+=2;
558         start_offset = offset;
559         proto_tree_add_text(entry, tvb, offset, 2, "Downcounter?"); offset += 2;
560         proto_tree_add_text(entry, tvb, offset, length-2, "Unknown");
561         offset = start_offset + length;
562
563         if (is_uuid_null(plugin_uuid))
564         {
565                 /* a message follows */
566                 tvbuff_t *subtvb = tvb_new_subset_remaining(tvb, offset);
567                 offset += dissect_aim_rendezvous_extended_message(subtvb, entry);
568         }
569         else
570         {
571                 /* plugin-specific data follows */
572                 proto_tree_add_text(entry, tvb, offset, -1, "Plugin-specific data");
573         }
574         offset = tvb_length(tvb);
575
576         return offset;
577 }
578
579 static int
580 dissect_aim_msg_ack(tvbuff_t *tvb, packet_info *pinfo, proto_tree *msg_tree)
581 {
582         int offset = 0;
583
584         proto_tree_add_item(msg_tree,hf_aim_icbm_cookie, tvb, offset, 8, ENC_BIG_ENDIAN); offset+=8;
585
586         proto_tree_add_item(msg_tree, hf_aim_message_channel_id, tvb, offset, 2,
587                         ENC_BIG_ENDIAN); offset += 2;
588
589         offset = dissect_aim_buddyname(tvb, pinfo, offset, msg_tree);
590
591         return offset;
592 }
593
594 static int
595 dissect_aim_msg_client_err(tvbuff_t *tvb, packet_info *pinfo, proto_tree *msg_tree)
596 {
597         int offset = 0;
598         guint16 channel, reason;
599
600         proto_tree_add_item(msg_tree,hf_aim_icbm_cookie, tvb, offset, 8, ENC_BIG_ENDIAN); offset+=8;
601         channel = tvb_get_ntohs(tvb, offset);
602         proto_tree_add_item(msg_tree,hf_aim_icbm_channel, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
603         offset = dissect_aim_buddyname(tvb, pinfo, offset, msg_tree);
604         reason = tvb_get_ntohs(tvb, offset);
605         proto_tree_add_item(msg_tree, hf_aim_icbm_client_err_reason, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
606
607         if (reason == CLIENT_ERR__REASON_CHANNEL_SPECIFIC && tvb_length_remaining(tvb, offset) > 0)
608         {
609                 switch (channel)
610                 {
611                 case ICBM_CHANNEL_RENDEZVOUS:
612                         proto_tree_add_item(msg_tree, hf_aim_icbm_rendezvous_nak_length, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
613                         proto_tree_add_item(msg_tree, hf_aim_icbm_rendezvous_nak, tvb, offset, 2, ENC_BIG_ENDIAN); offset+=2;
614                         break;
615
616                 default:
617                     {
618                         tvbuff_t *subtvb = tvb_new_subset_remaining(tvb, offset);
619                         proto_item *ti_extended_data = proto_tree_add_text(msg_tree, tvb, offset, -1, "Extended Data");
620                         dissect_aim_tlv_value_extended_data(ti_extended_data, 0, subtvb, pinfo);
621                         break;
622                     }
623                 }
624         }
625
626         return offset;
627 }
628
629 static const aim_subtype aim_fnac_family_messaging[] = {
630         { 0x0001, "Error", dissect_aim_snac_error },
631         { 0x0002, "Set ICBM Parameter", dissect_aim_msg_params },
632         { 0x0003, "Reset ICBM Parameter", NULL },
633         { 0x0004, "Request Parameter Info", NULL},
634         { 0x0005, "Parameter Info", dissect_aim_msg_params },
635         { 0x0006, "Outgoing", dissect_aim_msg_outgoing },
636         { 0x0007, "Incoming", dissect_aim_msg_incoming },
637         { 0x0008, "Evil Request", dissect_aim_msg_evil_req },
638         { 0x0009, "Evil Response", dissect_aim_msg_evil_repl  },
639         { 0x000a, "Missed Call", NULL },
640         { 0x000b, "Client Error", dissect_aim_msg_client_err },
641         { 0x000c, "Acknowledge", dissect_aim_msg_ack },
642         { 0x0014, "Mini Typing Notifications (MTN)", dissect_aim_msg_minityping },
643         { 0, NULL, NULL }
644 };
645
646
647
648 /* Register the protocol with Wireshark */
649 void
650 proto_register_aim_messaging(void)
651 {
652
653         /* Setup list of header fields */
654         static hf_register_info hf[] = {
655                 { &hf_aim_icbm_channel,
656                         { "Channel", "aim_messaging.icbm.channel", FT_UINT16, BASE_HEX, VALS(icbm_channel_types), 0x0, NULL, HFILL },
657                 },
658                 { &hf_aim_icbm_msg_flags,
659                         { "Message Flags", "aim_messaging.icbm.flags", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL },
660                 },
661                 { &hf_aim_icbm_max_snac_size,
662                         { "Max SNAC Size", "aim_messaging.icbm.max_snac", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL },
663                 },
664                 { &hf_aim_icbm_max_sender_warnlevel,
665                         { "Max sender warn level", "aim_messaging.icbm.max_sender_warn-level", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL },
666                 },
667                 { &hf_aim_icbm_max_receiver_warnlevel,
668                         { "max receiver warn level", "aim_messaging.icbm.max_receiver_warnlevel", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL },
669                 },
670                 { &hf_aim_icbm_min_msg_interval,
671                         { "Minimum message interval (milliseconds)", "aim_messaging.icbm.min_msg_interval", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL },
672                 },
673                 { &hf_aim_icbm_cookie,
674                         { "ICBM Cookie", "aim_messaging.icbmcookie", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }
675                 },
676                 { &hf_aim_message_channel_id,
677                         { "Message Channel ID", "aim_messaging.channelid", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }
678                 },
679                 { &hf_aim_icbm_evil,
680                         { "Send Evil Bit As", "aim_messaging.evilreq.origin", FT_UINT16, BASE_DEC, VALS(evil_origins), 0x0, NULL, HFILL },
681                 },
682                 { &hf_aim_evil_warn_level,
683                         { "Old warning level", "aim_messaging.evil.warn_level", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
684                 },
685                 { &hf_aim_evil_new_warn_level,
686                         { "New warning level", "aim_messaging.evil.new_warn_level", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
687                 },
688                 { &hf_aim_icbm_notification_cookie,
689                         { "Notification Cookie", "aim_messaging.notification.cookie", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL },
690                 },
691                 { &hf_aim_icbm_notification_channel,
692                         { "Notification Channel", "aim_messaging.notification.channel", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
693                 },
694                 { &hf_aim_icbm_notification_type,
695                         { "Notification Type", "aim_messaging.notification.type", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
696                 },
697                 { &hf_aim_icbm_rendezvous_nak,
698                         { "Rendezvous NAK reason", "aim_messaging.rendezvous_nak", FT_UINT16, BASE_HEX, VALS(rendezvous_nak_reason_types), 0x0, NULL, HFILL },
699                 },
700                 { &hf_aim_icbm_rendezvous_nak_length,
701                         { "Rendezvous NAK reason length", "aim_messaging.rendezvous_nak_length", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL },
702                 },
703                 { &hf_aim_rendezvous_msg_type,
704                         { "Message Type", "aim_messaging.rendezvous.msg_type", FT_UINT16, BASE_HEX, VALS(rendezvous_msg_types), 0x0, NULL, HFILL },
705                 },
706                 { &hf_aim_icbm_client_err_reason,
707                         { "Reason", "aim_messaging.clienterr.reason", FT_UINT16, BASE_DEC, VALS(client_err_reason_types), 0x0, NULL, HFILL },
708                 },
709                 { &hf_aim_icbm_client_err_protocol_version,
710                         { "Version", "aim_messaging.clienterr.protocol_version", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
711                 },
712                 { &hf_aim_icbm_client_err_client_caps_flags,
713                         { "Client Capabilities Flags", "aim_messaging.clienterr.client_caps_flags", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL },
714                 },
715                 { &hf_aim_rendezvous_extended_data_message_type,
716                         { "Message Type", "aim_messaging.icbm.extended_data.message.type", FT_UINT8, BASE_HEX, VALS(extended_data_message_types), 0x0, NULL, HFILL },
717                 },
718                 { &hf_aim_rendezvous_extended_data_message_flags,
719                         { "Message Flags", "aim_messaging.icbm.extended_data.message.flags", FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL },
720                 },
721                 { &hf_aim_rendezvous_extended_data_message_flags_normal,
722                         { "Normal Message", "aim_messaging.icbm.extended_data.message.flags.normal", FT_BOOLEAN, 16, TFS(&tfs_set_notset), EXTENDED_DATA_MFLAG_NORMAL, NULL, HFILL },
723                 },
724                 { &hf_aim_rendezvous_extended_data_message_flags_auto,
725                         { "Auto Message", "aim_messaging.icbm.extended_data.message.flags.auto", FT_BOOLEAN, 16, TFS(&tfs_set_notset), EXTENDED_DATA_MFLAG_AUTO, NULL, HFILL },
726                 },
727                 { &hf_aim_rendezvous_extended_data_message_flags_multi,
728                         { "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 },
729                 },
730                 { &hf_aim_rendezvous_extended_data_message_status_code,
731                         { "Status Code", "aim_messaging.icbm.extended_data.message.status_code", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
732                 },
733                 { &hf_aim_rendezvous_extended_data_message_priority_code,
734                         { "Priority Code", "aim_messaging.icbm.extended_data.message.priority_code", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
735                 },
736                 { &hf_aim_rendezvous_extended_data_message_text_length,
737                         { "Text Length", "aim_messaging.icbm.extended_data.message.text_length", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
738                 },
739                 { &hf_aim_rendezvous_extended_data_message_text,
740                         { "Text", "aim_messaging.icbm.extended_data.message.text", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL },
741                 }
742         };
743
744         /* Setup protocol subtree array */
745         static gint *ett[] = {
746                 &ett_aim_messaging,
747                 &ett_aim_rendezvous_data,
748                 &ett_aim_extended_data,
749                 &ett_aim_extended_data_message_flags
750         };
751
752         /* Register the protocol name and description */
753         proto_aim_messaging = proto_register_protocol("AIM Messaging", "AIM Messaging", "aim_messaging");
754
755         /* Required function calls to register the header fields and subtrees used */
756         proto_register_field_array(proto_aim_messaging, hf, array_length(hf));
757         proto_register_subtree_array(ett, array_length(ett));
758 }
759
760 void
761 proto_reg_handoff_aim_messaging(void)
762 {
763         aim_init_family(proto_aim_messaging, ett_aim_messaging, FAMILY_MESSAGING, aim_fnac_family_messaging);
764 }