Fix various Visual C++ analysis warnings.
[metze/wireshark/wip.git] / epan / dissectors / packet-aim.c
1 /* packet-aim.c
2  * Routines for AIM Instant Messenger (OSCAR) dissection
3  * Copyright 2000, Ralf Hoelzer <ralf@well.com>
4  * Copyright 2004, Jelmer Vernooij <jelmer@samba.org>
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 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35
36 #include <glib.h>
37
38 #include "isprint.h"
39
40 #include <epan/packet.h>
41 #include <epan/strutil.h>
42
43 #include "packet-tcp.h"
44 #include "packet-aim.h"
45 #include <epan/prefs.h>
46
47 #define TCP_PORT_AIM 5190
48
49 #define STRIP_TAGS 1
50
51 /* channels */
52 #define CHANNEL_NEW_CONN    0x01
53 #define CHANNEL_SNAC_DATA   0x02
54 #define CHANNEL_FLAP_ERR    0x03
55 #define CHANNEL_CLOSE_CONN  0x04
56 #define CHANNEL_KEEP_ALIVE  0x05
57
58 #define FAMILY_ALL_ERROR_INVALID_HEADER                0x0001
59 #define FAMILY_ALL_ERROR_SERVER_RATE_LIMIT_EXCEEDED    0x0002
60 #define FAMILY_ALL_ERROR_CLIENT_RATE_LIMIT_EXCEEDED    0x0003
61 #define FAMILY_ALL_ERROR_RECIPIENT_NOT_LOGGED_IN       0x0004
62 #define FAMILY_ALL_ERROR_REQUESTED_SERVICE_UNAVAILABLE 0x0005
63 #define FAMILY_ALL_ERROR_REQUESTED_SERVICE_NOT_DEFINED 0x0006
64 #define FAMILY_ALL_ERROR_OBSOLETE_SNAC                 0x0007
65 #define FAMILY_ALL_ERROR_NOT_SUPPORTED_BY_SERVER       0x0008
66 #define FAMILY_ALL_ERROR_NOT_SUPPORTED_BY_CLIENT       0x0009
67 #define FAMILY_ALL_ERROR_REFUSED_BY_CLIENT             0x000a
68 #define FAMILY_ALL_ERROR_REPLY_TOO_BIG                 0x000b
69 #define FAMILY_ALL_ERROR_RESPONSES_LOST                0x000c
70 #define FAMILY_ALL_ERROR_REQUEST_DENIED                0x000d
71 #define FAMILY_ALL_ERROR_INCORRECT_SNAC_FORMAT         0x000e
72 #define FAMILY_ALL_ERROR_INSUFFICIENT_RIGHTS           0x000f
73 #define FAMILY_ALL_ERROR_RECIPIENT_BLOCKED             0x0010
74 #define FAMILY_ALL_ERROR_SENDER_TOO_EVIL               0x0011
75 #define FAMILY_ALL_ERROR_RECEIVER_TOO_EVIL             0x0012
76 #define FAMILY_ALL_ERROR_USER_TEMP_UNAVAILABLE         0x0013
77 #define FAMILY_ALL_ERROR_NO_MATCH                      0x0014
78 #define FAMILY_ALL_ERROR_LIST_OVERFLOW                 0x0015
79 #define FAMILY_ALL_ERROR_REQUEST_AMBIGUOUS             0x0016
80 #define FAMILY_ALL_ERROR_SERVER_QUEUE_FULL             0x0017
81 #define FAMILY_ALL_ERROR_NOT_WHILE_ON_AOL              0x0018
82
83 static const value_string aim_flap_channels[] = {
84         { CHANNEL_NEW_CONN, "New Connection" },
85         { CHANNEL_SNAC_DATA, "SNAC Data" },
86         { CHANNEL_FLAP_ERR, "FLAP-Level Error" },
87         { CHANNEL_CLOSE_CONN, "Close Connection" },
88         { CHANNEL_KEEP_ALIVE, "Keep Alive" },
89         { 0, NULL }
90 };
91
92 static const value_string aim_snac_errors[] = {
93         { FAMILY_ALL_ERROR_INVALID_HEADER, "Invalid SNAC Header" },
94         { FAMILY_ALL_ERROR_SERVER_RATE_LIMIT_EXCEEDED, "Server rate limit exceeded" },
95         { FAMILY_ALL_ERROR_CLIENT_RATE_LIMIT_EXCEEDED, "Client rate limit exceeded" },
96         { FAMILY_ALL_ERROR_RECIPIENT_NOT_LOGGED_IN, "Recipient not logged in" },
97         { FAMILY_ALL_ERROR_REQUESTED_SERVICE_UNAVAILABLE, "Requested service unavailable" },
98         { FAMILY_ALL_ERROR_REQUESTED_SERVICE_NOT_DEFINED, "Requested service not defined" },
99         { FAMILY_ALL_ERROR_OBSOLETE_SNAC, "Obsolete SNAC issued" },
100         { FAMILY_ALL_ERROR_NOT_SUPPORTED_BY_SERVER, "Not supported by server" },
101         { FAMILY_ALL_ERROR_NOT_SUPPORTED_BY_CLIENT, "Not supported by client" },
102         { FAMILY_ALL_ERROR_REFUSED_BY_CLIENT, "Refused by client" },
103         { FAMILY_ALL_ERROR_REPLY_TOO_BIG, "Reply too big" },
104         { FAMILY_ALL_ERROR_RESPONSES_LOST, "Responses lost" },
105         { FAMILY_ALL_ERROR_REQUEST_DENIED, "Request denied" },
106         { FAMILY_ALL_ERROR_INCORRECT_SNAC_FORMAT, "Incorrect SNAC format" },
107         { FAMILY_ALL_ERROR_INSUFFICIENT_RIGHTS, "Insufficient rights" },
108         { FAMILY_ALL_ERROR_RECIPIENT_BLOCKED, "Recipient blocked" },
109         { FAMILY_ALL_ERROR_SENDER_TOO_EVIL, "Sender too evil" },
110         { FAMILY_ALL_ERROR_RECEIVER_TOO_EVIL, "Receiver too evil" },
111         { FAMILY_ALL_ERROR_USER_TEMP_UNAVAILABLE, "User temporarily unavailable" },
112         { FAMILY_ALL_ERROR_NO_MATCH, "No match" },
113         { FAMILY_ALL_ERROR_LIST_OVERFLOW, "List overflow" },
114         { FAMILY_ALL_ERROR_REQUEST_AMBIGUOUS, "Request ambiguous" },
115         { FAMILY_ALL_ERROR_SERVER_QUEUE_FULL, "Server queue full" },
116         { FAMILY_ALL_ERROR_NOT_WHILE_ON_AOL, "Not while on AOL" },
117         { 0, NULL }
118 };
119
120 #define AIM_CLIENT_TLV_SCREEN_NAME             0x0001
121 #define AIM_CLIENT_TLV_NEW_ROASTED_PASSWORD    0x0002
122 #define AIM_CLIENT_TLV_CLIENT_ID_STRING        0x0003
123 #define AIM_CLIENT_TLV_ERRORURL                0x0004
124 #define AIM_CLIENT_TLV_BOS_SERVER_STRING       0x0005
125 #define AIM_CLIENT_TLV_AUTH_COOKIE             0x0006
126 #define AIM_CLIENT_TLV_ERRORCODE               0x0008
127 #define AIM_CLIENT_TLV_DISCONNECT_REASON       0x0009
128 #define AIM_CLIENT_TLV_RECONNECT_HOST          0x000a
129 #define AIM_CLIENT_TLV_URL                     0x000b
130 #define AIM_CLIENT_TLV_DEBUG_DATA              0x000c
131 #define AIM_CLIENT_TLV_FAMILY_ID               0x000d
132 #define AIM_CLIENT_TLV_CLIENT_COUNTRY          0x000e
133 #define AIM_CLIENT_TLV_CLIENT_LANGUAGE         0x000f
134 #define AIM_CLIENT_TLV_EMAILADDR               0x0011
135 #define AIM_CLIENT_TLV_OLD_ROASTED_PASSWORD    0x0012
136 #define AIM_CLIENT_TLV_REGSTATUS               0x0013
137 #define AIM_CLIENT_TLV_CLIENT_DISTRIBUTION_NUM 0x0014
138 #define AIM_CLIENT_TLV_INVITEMESSAGE           0x0015
139 #define AIM_CLIENT_TLV_CLIENT_ID               0x0016
140 #define AIM_CLIENT_TLV_CLIENT_MAJOR_VERSION    0x0017
141 #define AIM_CLIENT_TLV_CLIENT_MINOR_VERSION    0x0018
142 #define AIM_CLIENT_TLV_CLIENT_LESSER_VERSION   0x0019
143 #define AIM_CLIENT_TLV_CLIENT_BUILD_NUMBER     0x001a
144 #define AIM_CLIENT_TLV_PASSWORD_MD5            0x0025
145 #define AIM_CLIENT_TLV_LATESTBETABUILD         0x0040
146 #define AIM_CLIENT_TLV_LATESTBETAURL           0x0041
147 #define AIM_CLIENT_TLV_LATESTBETAINFO          0x0042
148 #define AIM_CLIENT_TLV_LATESTBETANAME          0x0043
149 #define AIM_CLIENT_TLV_LATESTRELEASEBUILD      0x0044
150 #define AIM_CLIENT_TLV_LATESTRELEASEURL        0x0045
151 #define AIM_CLIENT_TLV_LATESTRELEASEINFO       0x0046
152 #define AIM_CLIENT_TLV_LATESTRELEASENAME       0x0047
153 #define AIM_CLIENT_TLV_BETA_DIGEST_SIG         0x0048
154 #define AIM_CLIENT_TLV_RELEASE_DIGEST_SIG      0x0049
155 #define AIM_CLIENT_TLV_CLIENTUSESSI            0x004a
156 #define AIM_CLIENT_TLV_CHANGE_PASSWORD_URL     0x0054
157 #define AIM_CLIENT_TLV_AWAITING_AUTH           0x0066
158 #define AIM_CLIENT_TLV_MEMBERS                 0x00c8
159 #define AIM_CLIENT_TLV_VISIBILITY_BITS         0x00c9
160 #define AIM_CLIENT_TLV_PRIVACY                 0x00ca
161 #define AIM_CLIENT_TLV_VISIBLE_CLASS           0x00cb
162 #define AIM_CLIENT_TLV_VISIBLE_MISC            0x00cc
163 #define AIM_CLIENT_TLV_ICQ2K_SHORTCUT          0x00cd
164 #define AIM_CLIENT_TLV_FIRST_LOADED_TIME       0x00d4
165 #define AIM_CLIENT_TLV_BUDDY_ICON_MD5SUM       0x00d5
166 #define AIM_CLIENT_TLV_GIVEN_NAME              0x0131
167 #define AIM_CLIENT_TLV_LOCAL_EMAIL             0x0137
168 #define AIM_CLIENT_TLV_LOCAL_SMS               0x013a
169 #define AIM_CLIENT_TLV_LOCAL_COMMENT           0x013c
170 #define AIM_CLIENT_TLV_LOCAL_PERSONAL_ALERT    0x013d
171 #define AIM_CLIENT_TLV_LOCAL_PERSONAL_SOUND    0x013e
172 #define AIM_CLIENT_TLV_FIRST_MESSAGE_SENT      0x0145
173
174 const aim_tlv aim_client_tlvs[] = {
175         { AIM_CLIENT_TLV_SCREEN_NAME, "Screen name", dissect_aim_tlv_value_string },
176         { AIM_CLIENT_TLV_NEW_ROASTED_PASSWORD, "Roasted password array", dissect_aim_tlv_value_bytes  },
177         { AIM_CLIENT_TLV_OLD_ROASTED_PASSWORD, "Old roasted password array", dissect_aim_tlv_value_bytes  },
178         { AIM_CLIENT_TLV_CLIENT_ID_STRING, "Client id string (name, version)", dissect_aim_tlv_value_string },
179         { AIM_CLIENT_TLV_CLIENT_ID, "Client id number", dissect_aim_tlv_value_uint16 },
180         { AIM_CLIENT_TLV_CLIENT_MAJOR_VERSION, "Client major version", dissect_aim_tlv_value_uint16 },
181         { AIM_CLIENT_TLV_CLIENT_MINOR_VERSION, "Client minor version", dissect_aim_tlv_value_uint16 },
182         { AIM_CLIENT_TLV_CLIENT_LESSER_VERSION, "Client lesser version", dissect_aim_tlv_value_uint16 },
183         { AIM_CLIENT_TLV_CLIENT_BUILD_NUMBER, "Client build number", dissect_aim_tlv_value_uint16 },
184         { AIM_CLIENT_TLV_PASSWORD_MD5, "Password Hash (MD5)", dissect_aim_tlv_value_bytes },
185         { AIM_CLIENT_TLV_CLIENT_DISTRIBUTION_NUM, "Client distribution number", dissect_aim_tlv_value_uint32 },
186         { AIM_CLIENT_TLV_CLIENT_LANGUAGE, "Client language", dissect_aim_tlv_value_string },
187         { AIM_CLIENT_TLV_CLIENT_COUNTRY, "Client country", dissect_aim_tlv_value_string },
188         { AIM_CLIENT_TLV_BOS_SERVER_STRING, "BOS server string", dissect_aim_tlv_value_string },
189         { AIM_CLIENT_TLV_AUTH_COOKIE, "Authorization cookie", dissect_aim_tlv_value_bytes },
190         { AIM_CLIENT_TLV_ERRORURL, "Error URL", dissect_aim_tlv_value_string },
191         { AIM_CLIENT_TLV_ERRORCODE, "Error Code", dissect_aim_tlv_value_uint16 },
192         { AIM_CLIENT_TLV_DISCONNECT_REASON, "Disconnect Reason", dissect_aim_tlv_value_uint16 },
193         { AIM_CLIENT_TLV_RECONNECT_HOST, "Reconnect Hostname", dissect_aim_tlv_value_string },
194         { AIM_CLIENT_TLV_URL, "URL", dissect_aim_tlv_value_string },
195         { AIM_CLIENT_TLV_DEBUG_DATA, "Debug Data", dissect_aim_tlv_value_uint16 },
196         { AIM_CLIENT_TLV_EMAILADDR, "Account Email address", dissect_aim_tlv_value_string },
197         { AIM_CLIENT_TLV_REGSTATUS, "Registration Status", dissect_aim_tlv_value_uint16 },
198         { AIM_CLIENT_TLV_LATESTBETABUILD, "Latest Beta Build", dissect_aim_tlv_value_uint32 },
199         { AIM_CLIENT_TLV_LATESTBETAURL, "Latest Beta URL", dissect_aim_tlv_value_string },
200         { AIM_CLIENT_TLV_LATESTBETAINFO, "Latest Beta Info", dissect_aim_tlv_value_string },
201         { AIM_CLIENT_TLV_LATESTBETANAME, "Latest Beta Name", dissect_aim_tlv_value_string },
202         { AIM_CLIENT_TLV_LATESTRELEASEBUILD, "Latest Release Build", dissect_aim_tlv_value_uint32 },
203         { AIM_CLIENT_TLV_LATESTRELEASEURL, "Latest Release URL", dissect_aim_tlv_value_string },
204         { AIM_CLIENT_TLV_LATESTRELEASEINFO, "Latest Release Info", dissect_aim_tlv_value_string  },
205         { AIM_CLIENT_TLV_LATESTRELEASENAME, "Latest Release Name", dissect_aim_tlv_value_string },
206         { AIM_CLIENT_TLV_BETA_DIGEST_SIG, "Beta Digest Signature (MD5)" , dissect_aim_tlv_value_bytes },
207         { AIM_CLIENT_TLV_RELEASE_DIGEST_SIG, "Release Digest Signature (MD5)", dissect_aim_tlv_value_bytes },
208         { AIM_CLIENT_TLV_CLIENTUSESSI, "Use SSI", dissect_aim_tlv_value_uint8 },
209         { AIM_CLIENT_TLV_FAMILY_ID, "Service (SNAC Family) ID", dissect_aim_tlv_value_uint16 },
210         { AIM_CLIENT_TLV_CHANGE_PASSWORD_URL, "Change password url", dissect_aim_tlv_value_string },
211         { AIM_CLIENT_TLV_AWAITING_AUTH, "Awaiting Authorization", dissect_aim_tlv_value_bytes },
212         { AIM_CLIENT_TLV_MEMBERS, "Members of this Group", dissect_aim_tlv_value_bytes },
213         { AIM_CLIENT_TLV_VISIBILITY_BITS, "Bitfield", dissect_aim_tlv_value_bytes },
214         { AIM_CLIENT_TLV_PRIVACY, "Privacy Settings" , dissect_aim_tlv_value_uint8 },
215         { AIM_CLIENT_TLV_VISIBLE_CLASS, "Visible To Classes", dissect_aim_tlv_value_userclass },
216         { AIM_CLIENT_TLV_VISIBLE_MISC, "Allow Others to See Data", dissect_aim_tlv_value_bytes },
217         { AIM_CLIENT_TLV_ICQ2K_SHORTCUT, "ICQ2K Shortcut List", dissect_aim_tlv_value_string },
218         { AIM_CLIENT_TLV_FIRST_LOADED_TIME, "First Time Buddy Was Added (Unix Timestamp)" , dissect_aim_tlv_value_uint32 },
219         { AIM_CLIENT_TLV_BUDDY_ICON_MD5SUM, "MD5SUM of Current Buddy Icon", dissect_aim_tlv_value_bytes },
220         { AIM_CLIENT_TLV_GIVEN_NAME, "Locally Specified Buddy Name", dissect_aim_tlv_value_string },
221         { AIM_CLIENT_TLV_LOCAL_EMAIL, "Locally Specified Buddy Email", dissect_aim_tlv_value_string },
222         { AIM_CLIENT_TLV_LOCAL_SMS, "Locally Specified Buddy SMS", dissect_aim_tlv_value_string },
223         { AIM_CLIENT_TLV_LOCAL_COMMENT, "Locally Specified Buddy Comment", dissect_aim_tlv_value_string },
224         { AIM_CLIENT_TLV_LOCAL_PERSONAL_ALERT, "Personal Alert for Buddy", dissect_aim_tlv_value_uint16 },
225         { AIM_CLIENT_TLV_LOCAL_PERSONAL_SOUND, "Personal Sound for Buddy", dissect_aim_tlv_value_string },
226         { AIM_CLIENT_TLV_FIRST_MESSAGE_SENT, "First Time Message Sent to Buddy (Unix Timestamp)", dissect_aim_tlv_value_uint32 },
227         { 0, NULL, NULL }
228 };
229
230
231 static int dissect_aim_tlv_value_userstatus(proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_);
232 static int dissect_aim_tlv_value_dcinfo(proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_);
233 static int dissect_aim_tlv_value_client_short_capabilities(proto_item *ti, guint16, tvbuff_t *, packet_info *);
234
235 #define AIM_ONLINEBUDDY_USERCLASS      0x0001
236 #define AIM_ONLINEBUDDY_ONSINCE        0x0003
237 #define AIM_ONLINEBUDDY_IDLETIME       0x0004
238 #define AIM_ONLINEBUDDY_MEMBERSINCE    0x0005
239 #define AIM_ONLINEBUDDY_STATUS         0x0006
240 #define AIM_ONLINEBUDDY_IPADDR         0x000a
241 #define AIM_ONLINEBUDDY_DCINFO         0x000c
242 #define AIM_ONLINEBUDDY_CAPINFO        0x000d
243 #define AIM_ONLINEBUDDY_SESSIONLEN     0x000f
244 #define AIM_ONLINEBUDDY_ICQSESSIONLEN  0x0010
245 #define AIM_ONLINEBUDDY_TIMEUPDATE     0x0011
246 #define AIM_ONLINEBUDDY_MYINSTANCENUM  0x0014
247 #define AIM_ONLINEBUDDY_SHORTCAPS      0x0019
248 #define AIM_ONLINEBUDDY_BARTINFO       0x001d
249 #define AIM_ONLINEBUDDY_NICKFLAGS2     0x001f
250 #define AIM_ONLINEBUDDY_BUDDYFEEDTIME  0x0023
251 #define AIM_ONLINEBUDDY_SIGTIME        0x0026
252 #define AIM_ONLINEBUDDY_AWAYTIME       0x0027
253 #define AIM_ONLINEBUDDY_GEOCOUNTRY     0x002a
254
255 const aim_tlv aim_onlinebuddy_tlvs[] = {
256         { AIM_ONLINEBUDDY_USERCLASS, "User class", dissect_aim_tlv_value_userclass },
257         { AIM_ONLINEBUDDY_ONSINCE, "Online since", dissect_aim_tlv_value_uint32 },
258         { AIM_ONLINEBUDDY_IDLETIME, "Idle time (sec)", dissect_aim_tlv_value_uint16 },
259         { AIM_ONLINEBUDDY_MEMBERSINCE, "Member since", dissect_aim_tlv_value_time },
260         { AIM_ONLINEBUDDY_STATUS, "Online status", dissect_aim_tlv_value_userstatus },
261         { AIM_ONLINEBUDDY_IPADDR, "User IP Address", dissect_aim_tlv_value_ipv4 },
262         { AIM_ONLINEBUDDY_DCINFO, "DC Info", dissect_aim_tlv_value_dcinfo},
263         { AIM_ONLINEBUDDY_CAPINFO, "Capability Info", dissect_aim_tlv_value_client_capabilities },
264         { AIM_ONLINEBUDDY_TIMEUPDATE, "Time update", dissect_aim_tlv_value_bytes },
265         { AIM_ONLINEBUDDY_SESSIONLEN, "Session Length (sec)", dissect_aim_tlv_value_uint32 },
266         { AIM_ONLINEBUDDY_ICQSESSIONLEN, "ICQ Session Length (sec)", dissect_aim_tlv_value_uint32 },
267         { AIM_ONLINEBUDDY_MYINSTANCENUM, "Client instance number", dissect_aim_tlv_value_uint8 },
268         { AIM_ONLINEBUDDY_SHORTCAPS, "Short Capabilities", dissect_aim_tlv_value_client_short_capabilities },
269         { AIM_ONLINEBUDDY_BARTINFO, "BART Info", dissect_aim_tlv_value_bytes },
270         { AIM_ONLINEBUDDY_NICKFLAGS2, "Upper bytes of Nick Flags", dissect_aim_tlv_value_bytes },
271         { AIM_ONLINEBUDDY_BUDDYFEEDTIME, "Last Buddy Feed update", dissect_aim_tlv_value_time },
272         { AIM_ONLINEBUDDY_SIGTIME, "Profile set time", dissect_aim_tlv_value_time },
273         { AIM_ONLINEBUDDY_AWAYTIME, "Away set time", dissect_aim_tlv_value_time },
274         { AIM_ONLINEBUDDY_GEOCOUNTRY, "Country code", dissect_aim_tlv_value_string },
275         { 0, NULL, NULL }
276 };
277
278 #define DC_DISABLED             0x0000
279 #define DC_HTTPS                0x0001
280 #define DC_SOCKS                0x0002
281 #define DC_NORMAL               0x0003
282 #define DC_IMPOSSIBLE   0x0004
283
284 static const value_string dc_types[] = {
285         { DC_DISABLED, "DC disabled" },
286         { DC_HTTPS, "DC thru firewall or HTTPS proxy" },
287         { DC_SOCKS, "DC thru SOCKS proxy" },
288         { DC_NORMAL, "Regular connection" },
289         { DC_IMPOSSIBLE, "DC not possible " },
290         { 0, NULL },
291 };
292
293 #define PROTO_VERSION_ICQ98     0x0004
294 #define PROTO_VERSION_ICQ99     0x0006
295 #define PROTO_VERSION_ICQ2K     0x0007
296 #define PROTO_VERSION_ICQ2K1    0x0008
297 #define PROTO_VERSION_ICQLITE   0x0009
298 #define PROTO_VERSION_ICQ2K3B   0x000A
299
300 static const value_string protocol_versions[] = {
301         { PROTO_VERSION_ICQ98, "ICQ '98" },
302         { PROTO_VERSION_ICQ99, "ICQ '99" },
303         { PROTO_VERSION_ICQ2K, "ICQ 2000" },
304         { PROTO_VERSION_ICQ2K1, "ICQ 2001" },
305         { PROTO_VERSION_ICQLITE, "ICQ Lite" },
306         { PROTO_VERSION_ICQ2K3B, "ICQ 2003B" },
307         { 0, NULL },
308 };
309
310 static GList *families = NULL;
311
312 #define AIM_MOTD_TLV_MOTD                                          0x000B
313
314 const aim_tlv aim_motd_tlvs[] = {
315         { AIM_MOTD_TLV_MOTD, "Message of the day message", dissect_aim_tlv_value_string },
316         { 0, NULL, NULL }
317 };
318
319 #define CLASS_UNCONFIRMED            0x00000001
320 #define CLASS_ADMINISTRATOR          0x00000002
321 #define CLASS_AOL                    0x00000004
322 #define CLASS_COMMERCIAL             0x00000008
323 #define CLASS_AIM                    0x00000010
324 #define CLASS_AWAY                   0x00000020
325 #define CLASS_ICQ                    0x00000040
326 #define CLASS_WIRELESS               0x00000080
327 #define CLASS_UNKNOWN100             0x00000100
328 #define CLASS_IMF                    0x00000200
329 #define CLASS_BOT                    0x00000400
330 #define CLASS_UNKNOWN800             0x00000800
331 #define CLASS_ONE_WAY_WIRELESS       0x00001000
332 #define CLASS_UNKNOWN2000            0x00002000
333 #define CLASS_UNKNOWN4000            0x00004000
334 #define CLASS_UNKNOWN8000            0x00008000
335 #define CLASS_UNKNOWN10000           0x00010000
336 #define CLASS_UNKNOWN20000           0x00020000
337 #define CLASS_NO_KNOCK_KNOCK         0x00040000
338 #define CLASS_FORWARD_MOBILE         0x00080000
339
340 #define FNAC_FLAG_NEXT_IS_RELATED        0x0001
341 #define FNAC_FLAG_CONTAINS_VERSION       0x8000
342
343 #define FNAC_TLV_FAMILY_VERSION  0x0001
344
345 static const aim_tlv aim_fnac_tlvs[] = {
346         { FNAC_TLV_FAMILY_VERSION, "SNAC Family Version", dissect_aim_tlv_value_uint16 },
347         { 0, NULL, NULL }
348 };
349
350 #define SSI_OP_RESULT_SUCCESS            0
351 #define SSI_OP_RESULT_DB_ERROR           1
352 #define SSI_OP_RESULT_NOT_FOUND          2
353 #define SSI_OP_RESULT_ALREADY_EXISTS     3
354 #define SSI_OP_RESULT_UNAVAILABLE        5
355 #define SSI_OP_RESULT_BAD_REQUEST        10
356 #define SSI_OP_RESULT_DB_TIME_OUT        11
357 #define SSI_OP_RESULT_OVER_ROW_LIMIT     12
358 #define SSI_OP_RESULT_NOT_EXECUTED       13
359 #define SSI_OP_RESULT_AUTH_REQUIRED      14
360 #define SSI_OP_RESULT_BAD_LOGINID        16
361 #define SSI_OP_RESULT_OVER_BUDDY_LIMIT   17
362 #define SSI_OP_RESULT_INSERT_SMART_GROUP 20
363 #define SSI_OP_RESULT_TIMEOUT            26
364
365 static const value_string aim_ssi_result_codes[] = {
366         { SSI_OP_RESULT_SUCCESS, "Success" },
367         { SSI_OP_RESULT_DB_ERROR, "Some kind of database error" },
368         { SSI_OP_RESULT_NOT_FOUND, "Item was not found for an update or delete" },
369         { SSI_OP_RESULT_ALREADY_EXISTS, "Item already exists for an insert" },
370         { SSI_OP_RESULT_UNAVAILABLE, "Server or database is not available" },
371         { SSI_OP_RESULT_BAD_REQUEST, "Request was not formed well" },
372         { SSI_OP_RESULT_DB_TIME_OUT, "Database timed out" },
373         { SSI_OP_RESULT_OVER_ROW_LIMIT, "Too many items of this class for an insert" },
374         { SSI_OP_RESULT_NOT_EXECUTED, "Not executed due to other error in same request" },
375         { SSI_OP_RESULT_AUTH_REQUIRED, "Buddy List authorization required" },
376         { SSI_OP_RESULT_BAD_LOGINID, "Bad loginId" },
377         { SSI_OP_RESULT_OVER_BUDDY_LIMIT, "Too many buddies" },
378         { SSI_OP_RESULT_INSERT_SMART_GROUP, "Attempt to added a Buddy to a smart group" },
379         { SSI_OP_RESULT_TIMEOUT, "General timeout" },
380         { 0, NULL }
381 };
382
383 static dissector_table_t subdissector_table;
384
385 /* Initialize the protocol and registered fields */
386 static int proto_aim = -1;
387 static int hf_aim_cmd_start = -1;
388 static int hf_aim_channel = -1;
389 static int hf_aim_seqno = -1;
390 static int hf_aim_data = -1;
391 static int hf_aim_data_len = -1;
392 static int hf_aim_signon_challenge_len = -1;
393 static int hf_aim_signon_challenge = -1;
394 static int hf_aim_fnac_family = -1;
395 static int hf_aim_fnac_subtype = -1;
396 static int hf_aim_fnac_flags = -1;
397 static int hf_aim_fnac_flag_next_is_related = -1;
398 static int hf_aim_fnac_flag_contains_version = -1;
399 static int hf_aim_fnac_id = -1;
400 static int hf_aim_infotype = -1;
401 static int hf_aim_buddyname_len = -1;
402 static int hf_aim_buddyname = -1;
403 static int hf_aim_userinfo_warninglevel = -1;
404 static int hf_aim_snac_error = -1;
405 static int hf_aim_ssi_result_code = -1;
406 static int hf_aim_tlvcount = -1;
407 static int hf_aim_version = -1;
408 static int hf_aim_userclass_unconfirmed = -1;
409 static int hf_aim_userclass_administrator = -1;
410 static int hf_aim_userclass_aol = -1;
411 static int hf_aim_userclass_commercial = -1;
412 static int hf_aim_userclass_aim = -1;
413 static int hf_aim_userclass_away = -1;
414 static int hf_aim_userclass_icq = -1;
415 static int hf_aim_userclass_wireless = -1;
416 static int hf_aim_userclass_unknown100 = -1;
417 static int hf_aim_userclass_imf = -1;
418 static int hf_aim_userclass_bot = -1;
419 static int hf_aim_userclass_unknown800 = -1;
420 static int hf_aim_userclass_one_way_wireless = -1;
421 static int hf_aim_userclass_unknown2000 = -1;
422 static int hf_aim_userclass_unknown4000 = -1;
423 static int hf_aim_userclass_unknown8000 = -1;
424 static int hf_aim_userclass_unknown10000 = -1;
425 static int hf_aim_userclass_unknown20000 = -1;
426 static int hf_aim_userclass_no_knock_knock = -1;
427 static int hf_aim_userclass_forward_mobile = -1;
428 static int hf_aim_nickinfo_caps = -1;
429 static int hf_aim_nickinfo_short_caps = -1;
430 static int hf_aim_messageblock_featuresdes = -1;
431 static int hf_aim_messageblock_featureslen = -1;
432 static int hf_aim_messageblock_features = -1;
433 static int hf_aim_messageblock_info = -1;
434 static int hf_aim_messageblock_len = -1;
435 static int hf_aim_messageblock_charset = -1;
436 static int hf_aim_messageblock_charsubset = -1;
437 static int hf_aim_messageblock_message = -1;
438
439 static int hf_aim_dcinfo_ip = -1;
440 static int hf_aim_dcinfo_tcpport = -1;
441 static int hf_aim_dcinfo_type = -1;
442 static int hf_aim_dcinfo_proto_version = -1;
443 static int hf_aim_dcinfo_auth_cookie = -1;
444 static int hf_aim_dcinfo_webport = -1;
445 static int hf_aim_dcinfo_client_future = -1;
446 static int hf_aim_dcinfo_last_info_update = -1;
447 static int hf_aim_dcinfo_last_ext_info_update = -1;
448 static int hf_aim_dcinfo_last_ext_status_update = -1;
449 static int hf_aim_dcinfo_unknown = -1;
450
451 /* Initialize the subtree pointers */
452 static gint ett_aim          = -1;
453 static gint ett_aim_dcinfo       = -1;
454 static gint ett_aim_buddyname= -1;
455 static gint ett_aim_fnac     = -1;
456 static gint ett_aim_fnac_flags = -1;
457 static gint ett_aim_tlv      = -1;
458 static gint ett_aim_userclass = -1;
459 static gint ett_aim_messageblock = -1;
460 static gint ett_aim_nickinfo_caps = -1;
461 static gint ett_aim_nickinfo_short_caps = -1;
462 static gint ett_aim_string08_array = -1;
463
464 /* desegmentation of AIM over TCP */
465 static gboolean aim_desegment = TRUE;
466
467
468 const aim_subtype
469 *aim_get_subtype( guint16 famnum, guint16 subtype )
470 {
471         GList *gl = families;
472         while(gl) {
473                 aim_family *fam = gl->data;
474                 if(fam->family == famnum) {
475                         int i;
476                         for(i = 0; fam->subtypes[i].name; i++) {
477                                 if(fam->subtypes[i].id == subtype) return &(fam->subtypes[i]);
478                         }
479                 }
480                 gl = gl->next;
481         }
482
483         return NULL;
484
485 }
486
487 const aim_family
488 *aim_get_family( guint16 famnum )
489 {
490         GList *gl = families;
491         while(gl) {
492                 aim_family *fam = gl->data;
493                 if(fam->family == famnum) return fam;
494                 gl = gl->next;
495         }
496
497         return NULL;
498 }
499
500 int
501 aim_get_buddyname( guchar *name, tvbuff_t *tvb, int len_offset, int name_offset)
502 {
503         guint8 buddyname_length;
504
505         buddyname_length = tvb_get_guint8(tvb, len_offset);
506
507         if(buddyname_length > MAX_BUDDYNAME_LENGTH )
508                 buddyname_length = MAX_BUDDYNAME_LENGTH;
509
510         tvb_get_nstringz0(tvb, name_offset, buddyname_length + 1, name);
511
512         return buddyname_length;
513 }
514
515
516 void
517 aim_get_message( guchar *msg, tvbuff_t *tvb, int msg_offset, int msg_length)
518 {
519         int i,j,c;
520         int bracket = FALSE;
521         int max, tagchars = 0;
522         int new_offset = msg_offset;
523         int new_length = msg_length;
524
525
526         /* make sure nothing bigger than 1000 bytes is printed */
527         if( msg_length > 999 ) return;
528
529         memset( msg, '\0', 1000);
530         i = 0;
531         c = 0;
532
533         /* loop until HTML tag is reached - quick&dirty way to find start of message
534          * (it is nearly impossible to find the correct start offset for all client versions) */
535         while( (tagchars < 6) && (new_length > 5) )
536         {
537                 j = tvb_get_guint8(tvb, new_offset);
538                 if( ( (j == '<') && (tagchars == 0) ) ||
539                     ( (j == 'h') && (tagchars == 1) ) ||
540                     ( (j == 'H') && (tagchars == 1) ) ||
541                     ( (j == 't') && (tagchars == 2) ) ||
542                     ( (j == 'T') && (tagchars == 2) ) ||
543                     ( (j == 'm') && (tagchars == 3) ) ||
544                     ( (j == 'M') && (tagchars == 3) ) ||
545                     ( (j == 'l') && (tagchars == 4) ) ||
546                     ( (j == 'L') && (tagchars == 4) ) ||
547                     ( (j == '>') && (tagchars == 5) ) ) tagchars++;
548                 new_offset++;
549                 new_length--;
550         }
551
552         /* set offset and length of message to after the first HTML tag */
553         msg_offset = new_offset;
554         msg_length = new_length;
555         max = msg_length - 1;
556         tagchars = 0;
557
558         /* find the rest of the message until either a </html> is reached or the end of the frame.
559          * All other HTML tags are stripped to display only the raw message (printable characters) */
560         while( (c < max) && (tagchars < 7) )
561         {
562                 j = tvb_get_guint8(tvb, msg_offset+c);
563
564
565                 /* make sure this is an HTML tag by checking the order of the chars */
566                 if( ( (j == '<') && (tagchars == 0) ) ||
567                     ( (j == '/') && (tagchars == 1) ) ||
568                     ( (j == 'h') && (tagchars == 2) ) ||
569                     ( (j == 'H') && (tagchars == 2) ) ||
570                     ( (j == 't') && (tagchars == 3) ) ||
571                     ( (j == 'T') && (tagchars == 3) ) ||
572                     ( (j == 'm') && (tagchars == 4) ) ||
573                     ( (j == 'M') && (tagchars == 4) ) ||
574                     ( (j == 'l') && (tagchars == 5) ) ||
575                     ( (j == 'L') && (tagchars == 5) ) ||
576                     ( (j == '>') && (tagchars == 6) ) ) tagchars++;
577
578 #ifdef STRIP_TAGS
579                 if( j == '<' ) bracket = TRUE;
580                 if( j == '>' ) bracket = FALSE;
581                 if( (isprint(j) ) && (bracket == FALSE) && (j != '>'))
582 #else
583                         if( isprint(j) )
584 #endif
585                         {
586                                 msg[i] = j;
587                                 i++;
588                         }
589                 c++;
590         }
591 }
592
593 void
594 aim_init_family(int proto, int ett, guint16 family, const aim_subtype *subtypes)
595 {
596         aim_family *fam = g_new(aim_family, 1);
597         fam->proto = find_protocol_by_id(proto);
598         fam->name = proto_get_protocol_short_name(fam->proto);
599         fam->family = family;
600         fam->subtypes = subtypes;
601         families = g_list_append(families, fam);
602
603         fam->proto_id = proto;
604         fam->ett = ett;
605 }
606
607 static void
608 dissect_aim_newconn(tvbuff_t *tvb, packet_info *pinfo, int offset,
609                     proto_tree *tree)
610 {
611         col_set_str(pinfo->cinfo, COL_INFO, "New Connection");
612
613         if (tvb_length_remaining(tvb, offset) > 0) {
614                 proto_tree_add_item(tree, hf_aim_version, tvb, offset, 4, FALSE);
615                 offset+=4;
616                 offset = dissect_aim_tlv_sequence(tvb, pinfo, offset, tree, aim_client_tlvs);
617         }
618
619         if (tvb_length_remaining(tvb, offset) > 0)
620                 proto_tree_add_item(tree, hf_aim_data, tvb, offset, -1, FALSE);
621 }
622
623
624 int
625 dissect_aim_snac_error(tvbuff_t *tvb, packet_info *pinfo, proto_tree *aim_tree)
626 {
627         const char *name;
628
629         if ((name = match_strval(tvb_get_ntohs(tvb, 0), aim_snac_errors)) != NULL) {
630                 col_add_str(pinfo->cinfo, COL_INFO, name);
631         }
632
633         proto_tree_add_item (aim_tree, hf_aim_snac_error, tvb, 0, 2, FALSE);
634
635         return dissect_aim_tlv_sequence(tvb, pinfo, 2, aim_tree, aim_client_tlvs);
636 }
637
638 int
639 dissect_aim_ssi_result(tvbuff_t *tvb, packet_info *pinfo, proto_tree *aim_tree)
640 {
641         const char *name;
642
643         if ((name = match_strval(tvb_get_ntohs(tvb, 0), aim_ssi_result_codes)) != NULL) {
644                 col_add_str(pinfo->cinfo, COL_INFO, name);
645         }
646
647         proto_tree_add_item (aim_tree, hf_aim_ssi_result_code, tvb, 0, 2, FALSE);
648
649         return 2;
650 }
651
652 int
653 dissect_aim_userinfo(tvbuff_t *tvb, packet_info *pinfo,
654                      int offset, proto_tree *tree)
655 {
656         offset = dissect_aim_buddyname(tvb, pinfo, offset, tree);
657
658         proto_tree_add_item(tree, hf_aim_userinfo_warninglevel, tvb, offset, 2, FALSE);
659         offset += 2;
660
661         return dissect_aim_tlv_list(tvb, pinfo, offset, tree, aim_onlinebuddy_tlvs);
662 }
663
664 static int
665 dissect_aim_fnac_flags(tvbuff_t *tvb, int offset, int len, proto_item *ti,
666                        guint16 flags)
667 {
668         proto_tree *entry = proto_item_add_subtree(ti, ett_aim_fnac_flags);
669         proto_tree_add_boolean(entry, hf_aim_fnac_flag_next_is_related, tvb, offset, len, flags);
670         proto_tree_add_boolean(entry, hf_aim_fnac_flag_contains_version, tvb, offset, len, flags);
671
672         return offset + len;
673 }
674
675 static void
676 dissect_aim_snac(tvbuff_t *tvb, packet_info *pinfo, int offset,
677                  proto_tree *aim_tree, proto_tree *root_tree)
678 {
679         guint16 family_id;
680         guint16 subtype_id;
681         guint16 flags;
682         guint32 id;
683         proto_item *ti1;
684         struct aiminfo aiminfo;
685         proto_tree *aim_tree_fnac = NULL;
686         tvbuff_t *subtvb;
687         int orig_offset;
688         const aim_subtype *subtype;
689         proto_tree *family_tree = NULL;
690         const aim_family *family;
691         void* pd_save;
692
693         orig_offset = offset;
694         family_id = tvb_get_ntohs(tvb, offset);
695         family = aim_get_family(family_id);
696         offset += 2;
697         subtype_id = tvb_get_ntohs(tvb, offset);
698         subtype = aim_get_subtype(family_id, subtype_id);
699         offset += 2;
700         flags = tvb_get_ntohs(tvb, offset);
701         offset += 2;
702         id = tvb_get_ntohl(tvb, offset);
703         offset += 4;
704
705
706         if( aim_tree && subtype != NULL )
707         {
708                 offset = orig_offset;
709                 ti1 = proto_tree_add_text(aim_tree, tvb, 6, 10,
710                                           "FNAC: Family: %s (0x%04x), Subtype: %s (0x%04x)",
711                                           family ? family->name : "Unknown", family_id,
712                                           (subtype && subtype->name) ? subtype->name : "Unknown", subtype_id);
713                 aim_tree_fnac = proto_item_add_subtree(ti1, ett_aim_fnac);
714
715                 proto_tree_add_uint_format_value (aim_tree_fnac, hf_aim_fnac_family,
716                                                   tvb, offset, 2, family_id, "%s (0x%04x)",
717                                                   family ? family->name : "Unknown", family_id);
718                 offset += 2;
719
720                 proto_tree_add_uint_format_value (aim_tree_fnac, hf_aim_fnac_subtype,
721                                                   tvb, offset, 2, subtype_id, "%s (0x%04x)",
722                                                   (subtype && subtype->name) ? subtype->name : "Unknown", subtype_id);
723
724                 offset += 2;
725
726                 ti1 = proto_tree_add_uint(aim_tree_fnac, hf_aim_fnac_flags, tvb, offset,
727                                           2, flags);
728
729                 offset = dissect_aim_fnac_flags(tvb, offset, 2, ti1, flags);
730
731                 proto_tree_add_uint(aim_tree_fnac, hf_aim_fnac_id, tvb, offset,
732                                     4, id);
733                 offset += 4;
734         }
735
736         if(flags & FNAC_FLAG_CONTAINS_VERSION)
737         {
738                 guint16 len = tvb_get_ntohs(tvb, offset);
739                 int oldoffset;
740                 offset+=2;
741                 oldoffset = offset;
742
743                 while(offset < oldoffset + len) {
744                         offset = dissect_aim_tlv(tvb, pinfo, offset, aim_tree, aim_fnac_tlvs);
745                 }
746         }
747
748         subtvb = tvb_new_subset_remaining(tvb, offset);
749         aiminfo.tcpinfo = pinfo->private_data;
750         aiminfo.family = family_id;
751         aiminfo.subtype = subtype_id;
752         pd_save = pinfo->private_data;
753         pinfo->private_data = &aiminfo;
754
755         if (family)
756                 col_set_str(pinfo->cinfo, COL_PROTOCOL, family->name);
757
758         if(subtype != NULL && family != NULL)
759         {
760                 col_set_str(pinfo->cinfo, COL_INFO, family->name);
761                 col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", subtype->name);
762         } else {
763                 col_set_str(pinfo->cinfo, COL_INFO, "SNAC data");
764
765                 if(family)
766                         col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", family->name);
767                 else
768                         col_append_fstr(pinfo->cinfo, COL_INFO, ", Family: 0x%04x", family_id);
769
770                 col_append_fstr(pinfo->cinfo, COL_INFO, ", Subtype: 0x%04x", subtype_id);
771         }
772
773         if(aim_tree && family != NULL)
774         {
775                 proto_item *ti = proto_tree_add_item(root_tree, family->proto_id, subtvb, 0, -1, FALSE);
776                 family_tree = proto_item_add_subtree(ti, family->ett);
777                 if(subtype)
778                         proto_item_append_text(ti, ", %s", subtype->name);
779         }
780
781         if(tvb_length_remaining(tvb, offset) > 0 && subtype != NULL && subtype->dissector)
782         {
783                 subtype->dissector(subtvb, pinfo, family_tree);
784         }
785
786         pinfo->private_data = pd_save;
787 }
788
789 static void
790 dissect_aim_flap_err(tvbuff_t *tvb, packet_info *pinfo, int offset,
791                      proto_tree *tree)
792 {
793         col_set_str(pinfo->cinfo, COL_INFO, "FLAP error");
794
795         /* Show the undissected payload */
796         if (tvb_length_remaining(tvb, offset) > 0)
797                 proto_tree_add_item(tree, hf_aim_data, tvb, offset, -1, FALSE);
798 }
799
800 static void
801 dissect_aim_keep_alive(tvbuff_t *tvb, packet_info *pinfo, int offset,
802                        proto_tree *tree)
803 {
804         col_set_str(pinfo->cinfo, COL_INFO, "Keep Alive");
805
806         /* Show the undissected payload */
807         if (tvb_length_remaining(tvb, offset) > 0)
808                 proto_tree_add_item(tree, hf_aim_data, tvb, offset, -1, FALSE);
809 }
810
811 static void
812 dissect_aim_close_conn(tvbuff_t *tvb, packet_info *pinfo, int offset,
813                        proto_tree *tree)
814 {
815         col_set_str(pinfo->cinfo, COL_INFO, "Close Connection");
816
817         offset = dissect_aim_tlv_sequence(tvb, pinfo, offset, tree, aim_client_tlvs);
818 }
819
820 static void
821 dissect_aim_unknown_channel(tvbuff_t *tvb, packet_info *pinfo, int offset,
822                             proto_tree *tree)
823 {
824         col_set_str(pinfo->cinfo, COL_INFO, "Unknown Channel");
825
826         /* Show the undissected payload */
827         if (tvb_length_remaining(tvb, offset) > 0)
828                 proto_tree_add_item(tree, hf_aim_data, tvb, offset, -1, FALSE);
829 }
830
831 int
832 dissect_aim_buddyname(tvbuff_t *tvb, packet_info *pinfo _U_, int offset,
833                       proto_tree *tree)
834 {
835         guint8 buddyname_length = 0;
836         proto_item *ti = NULL;
837         proto_tree *buddy_tree = NULL;
838
839         buddyname_length = tvb_get_guint8(tvb, offset);
840         offset++;
841
842         if(tree)
843         {
844                 ti = proto_tree_add_text(tree, tvb, offset-1, 1+buddyname_length,
845                                          "Buddy: %s",
846                                          tvb_format_text(tvb, offset, buddyname_length));
847                 buddy_tree = proto_item_add_subtree(ti, ett_aim_buddyname);
848                 proto_tree_add_item(buddy_tree, hf_aim_buddyname_len, tvb, offset-1, 1, FALSE);
849                 proto_tree_add_item(buddy_tree, hf_aim_buddyname, tvb, offset, buddyname_length, FALSE);
850         }
851
852         return offset+buddyname_length;
853 }
854
855 typedef struct _aim_client_capability
856 {
857         const char *name;
858         e_guid_t clsid;
859 } aim_client_capability;
860
861 static const aim_client_capability known_client_caps[] = {
862         { "Send File",
863           {0x09461343, 0x4c7f, 0x11d1,
864             { 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
865
866         { "Recv File",
867             { 0x09461348, 0x4c7f, 0x11d1,
868                    { 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
869
870         { "Short Caps",
871          {0x09460000, 0x4c7f, 0x11d1,
872            { 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
873
874         { "Secure IM",
875          {0x09460001, 0x4c7f, 0x11d1,
876            { 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
877
878         { "XHTML IM",
879          {0x09460002, 0x4c7f, 0x11d1,
880            { 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
881
882         { "Video Chat",
883          {0x09460100, 0x4c7f, 0x11d1,
884            { 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
885
886         { "Live Video",
887          {0x09460101, 0x4c7f, 0x11d1,
888            { 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
889
890         { "Camera",
891          {0x09460102, 0x4c7f, 0x11d1,
892            { 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
893
894         { "Microphone",
895          {0x09460103, 0x4c7f, 0x11d1,
896            { 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
897
898         { "Live Audio",
899          {0x09460104, 0x4c7f, 0x11d1,
900            { 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
901
902         { "iChatAV info",
903          {0x09460105, 0x4c7f, 0x11d1,
904            { 0x82, 0x22, 0x44, 0x45, 0x45, 0x53, 0x54, 0x00}}},
905
906         { "Host Status Text Aware",
907          {0x0946010A, 0x4c7f, 0x11d1,
908            { 0x82, 0x22, 0x44, 0x45, 0x45, 0x53, 0x54, 0x00}}},
909
910         { "Realtime IM",
911          {0x0946010B, 0x4c7f, 0x11d1,
912            { 0x82, 0x22, 0x44, 0x45, 0x45, 0x53, 0x54, 0x00}}},
913
914         { "Smart Caps",
915          {0x094601FF, 0x4c7f, 0x11d1,
916            { 0x82, 0x22, 0x44, 0x45, 0x45, 0x53, 0x54, 0x00}}},
917
918         { "Hiptop",
919          {0x09461323, 0x4c7f, 0x11d1,
920            { 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
921
922         { "Voice Chat",
923          {0x09461341, 0x4c7f, 0x11d1,
924                  { 0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
925
926         { "File Transfer",
927          {0x09461343, 0x4c7f, 0x11d1,
928                  {0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
929
930         { "Direct ICQ Communication",
931          {0x09461344, 0x4c7f, 0x11d1,
932                  {0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
933
934         { "Direct ICBM",
935          {0x09461345, 0x4c7f, 0x11d1,
936                  {0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
937
938         { "Buddy Icon",
939          {0x09461346, 0x4c7f, 0x11d1,
940                  {0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
941
942         { "Add-Ins",
943          {0x09461347, 0x4c7f, 0x11d1,
944                  {0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
945
946         { "File Sharing",
947          {0x09461348, 0x4c7f, 0x11d1,
948                  {0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
949
950         { "ICQ Server Relaying",
951          {0x09461349, 0x4c7f, 0x11d1,
952                  {0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
953
954         { "Games",
955          {0x0946134a, 0x4c7f, 0x11d1,
956                  {0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
957
958         { "Games",
959          {0x0946134a, 0x4c7f, 0x11d1,
960                  {0x22, 0x82, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
961
962         { "Send Buddy List",
963          {0x0946134b, 0x4c7f, 0x11d1,
964                  {0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
965
966         { "AIM/ICQ Interoperability",
967          {0x0946134d, 0x4c7f, 0x11d1,
968                  {0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
969
970         { "ICQ UTF8 Support",
971          {0x0946134e, 0x4c7f, 0x11d1,
972                  {0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
973
974         { "Old ICQ UTF8 Support",
975          {0x2e7a6475, 0xfadf, 0x4dc8,
976                  {0x88, 0x6f, 0xea, 0x35, 0x95, 0xfd, 0xb6, 0xdf}}},
977
978         { "Chat",
979          {0x748f2420, 0x6287, 0x11d1,
980                  {0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}}},
981
982         { "ICQ Rich Text Format Messages",
983          {0x97b12751, 0x243c, 0x4334,
984                  {0xad, 0x22, 0xd6, 0xab, 0xf7, 0x3f, 0x14, 0x92}}},
985
986         { "AP User",
987          {0xaa4a32b5, 0xf884, 0x48c6,
988                  {0xa3, 0xd7, 0x8c, 0x50, 0x97, 0x19, 0xfd, 0x5b}}},
989
990         { "Trillian Encryption",
991          {0xf2e7c7f4, 0xfead, 0x4dfb,
992                  {0xb2, 0x35, 0x36, 0x79, 0x8b, 0xdf, 0x00, 0x00}}},
993
994         { NULL, {0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 } } }
995 };
996
997 static const aim_client_capability *
998 aim_find_capability (e_guid_t clsid)
999 {
1000         int i;
1001
1002         for(i = 0; known_client_caps[i].name; i++)
1003         {
1004                 const aim_client_capability *caps = &(known_client_caps[i]);
1005
1006                 if(memcmp(&(caps->clsid), &clsid, sizeof(e_guid_t)) == 0)
1007                         return caps;
1008         }
1009
1010         return NULL;
1011 }
1012
1013 static const aim_client_capability *
1014 aim_find_short_capability(guint16 shortid)
1015 {
1016         e_guid_t clsid = {0x09460000, 0x4c7f, 0x11d1, {0x82, 0x22, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}};
1017         clsid.data1 |= shortid;
1018
1019         return aim_find_capability(clsid);
1020 }
1021
1022 int
1023 dissect_aim_capability(proto_tree *entry, tvbuff_t *tvb, int offset)
1024 {
1025         const aim_client_capability *caps;
1026         e_guid_t clsid;
1027
1028         tvb_get_ntohguid(tvb, offset, &clsid);
1029         caps = aim_find_capability(clsid);
1030
1031         proto_tree_add_guid_format(entry, hf_aim_nickinfo_caps, tvb, offset, 16,
1032                 &clsid,
1033                 "%s {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
1034                 caps?caps->name:"Unknown", clsid.data1, clsid.data2,
1035                 clsid.data3, clsid.data4[0], clsid.data4[1], clsid.data4[2],
1036                 clsid.data4[3], clsid.data4[4], clsid.data4[5], clsid.data4[6],
1037                 clsid.data4[7]
1038         );
1039
1040         return offset+16;
1041 }
1042
1043 static int
1044 dissect_aim_short_capability(proto_tree *entry, tvbuff_t *tvb, int offset)
1045 {
1046         const aim_client_capability *caps;
1047         guint16 shortid;
1048
1049         shortid = tvb_get_ntohs(tvb, offset);
1050         caps = aim_find_short_capability(shortid);
1051
1052         proto_tree_add_uint_format(entry, hf_aim_nickinfo_short_caps, tvb, offset, 2,
1053                 shortid,
1054                 "%s (0x%04x)",
1055                 caps?caps->name:"Unknown", shortid
1056         );
1057
1058         return offset+2;
1059 }
1060
1061 int
1062 dissect_aim_tlv_value_client_capabilities(proto_item *ti _U_, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_)
1063 {
1064         int offset = 0;
1065         proto_tree *entry;
1066
1067         proto_item_set_text(ti, "Client Capabilities List");
1068
1069         entry = proto_item_add_subtree(ti, ett_aim_nickinfo_caps);
1070
1071         while (tvb_length_remaining(tvb, offset) > 0) {
1072                 offset = dissect_aim_capability(entry, tvb, offset);
1073         }
1074
1075         return tvb_length(tvb);
1076 }
1077
1078 static int
1079 dissect_aim_tlv_value_client_short_capabilities(proto_item *ti _U_, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_)
1080 {
1081         int offset = 0;
1082         proto_tree *entry;
1083
1084         proto_item_set_text(ti, "Short Client Capabilities List");
1085
1086         entry = proto_item_add_subtree(ti, ett_aim_nickinfo_short_caps);
1087
1088         while (tvb_length_remaining(tvb, offset) > 0) {
1089                 offset = dissect_aim_short_capability(entry, tvb, offset);
1090         }
1091
1092         return tvb_length(tvb);
1093 }
1094
1095 int
1096 dissect_aim_tlv_value_time(proto_item *ti _U_, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_)
1097 {
1098         /* FIXME */
1099         return tvb_length(tvb);
1100 }
1101
1102 int
1103 dissect_aim_userclass(tvbuff_t *tvb, int offset, int len, proto_item *ti, guint32 flags)
1104 {
1105         proto_tree *entry;
1106
1107         entry = proto_item_add_subtree(ti, ett_aim_userclass);
1108         proto_tree_add_boolean(entry, hf_aim_userclass_unconfirmed, tvb, offset, len, flags);
1109         proto_tree_add_boolean(entry, hf_aim_userclass_administrator, tvb, offset, len, flags);
1110         proto_tree_add_boolean(entry, hf_aim_userclass_aol, tvb, offset, len, flags);
1111         proto_tree_add_boolean(entry, hf_aim_userclass_commercial, tvb, offset, len, flags);
1112         proto_tree_add_boolean(entry, hf_aim_userclass_aim, tvb, offset, len, flags);
1113         proto_tree_add_boolean(entry, hf_aim_userclass_away, tvb, offset, len, flags);
1114         proto_tree_add_boolean(entry, hf_aim_userclass_icq, tvb, offset, len, flags);
1115         proto_tree_add_boolean(entry, hf_aim_userclass_wireless, tvb, offset, len, flags);
1116         proto_tree_add_boolean(entry, hf_aim_userclass_unknown100, tvb, offset, len, flags);
1117         proto_tree_add_boolean(entry, hf_aim_userclass_imf, tvb, offset, len, flags);
1118         proto_tree_add_boolean(entry, hf_aim_userclass_bot, tvb, offset, len, flags);
1119         proto_tree_add_boolean(entry, hf_aim_userclass_unknown800, tvb, offset, len, flags);
1120         proto_tree_add_boolean(entry, hf_aim_userclass_one_way_wireless, tvb, offset, len, flags);
1121         proto_tree_add_boolean(entry, hf_aim_userclass_unknown2000, tvb, offset, len, flags);
1122         proto_tree_add_boolean(entry, hf_aim_userclass_unknown4000, tvb, offset, len, flags);
1123         proto_tree_add_boolean(entry, hf_aim_userclass_unknown8000, tvb, offset, len, flags);
1124         proto_tree_add_boolean(entry, hf_aim_userclass_unknown10000, tvb, offset, len, flags);
1125         proto_tree_add_boolean(entry, hf_aim_userclass_unknown20000, tvb, offset, len, flags);
1126         proto_tree_add_boolean(entry, hf_aim_userclass_no_knock_knock, tvb, offset, len, flags);
1127         proto_tree_add_boolean(entry, hf_aim_userclass_forward_mobile, tvb, offset, len, flags);
1128
1129         return offset+len;
1130 }
1131
1132 int
1133 dissect_aim_tlv_value_userclass(proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_)
1134 {
1135         guint16 value16 = tvb_get_ntohs(tvb, 0);
1136         proto_item_set_text(ti, "Value: 0x%04x", value16);
1137         return dissect_aim_userclass(tvb, 0, 2, ti, value16);
1138 }
1139
1140 static int
1141 dissect_aim_tlv_value_userstatus(proto_item *ti _U_, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_)
1142 {
1143         /* FIXME */
1144         return tvb_length(tvb);
1145 }
1146
1147 static int
1148 dissect_aim_tlv_value_dcinfo(proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_)
1149 {
1150         int offset = 0;
1151
1152         proto_tree *dctree = proto_item_add_subtree(ti, ett_aim_dcinfo);
1153
1154         proto_tree_add_item(dctree, hf_aim_dcinfo_ip , tvb, offset, 4, FALSE); offset+=4;
1155         proto_tree_add_item(dctree, hf_aim_dcinfo_tcpport, tvb, offset, 4, FALSE); offset+=4;
1156         proto_tree_add_item(dctree, hf_aim_dcinfo_type, tvb, offset, 1, FALSE); offset+=1;
1157         proto_tree_add_item(dctree, hf_aim_dcinfo_proto_version, tvb, offset, 2, FALSE); offset+=2;
1158         proto_tree_add_item(dctree, hf_aim_dcinfo_auth_cookie, tvb, offset, 4, FALSE); offset+=2;
1159         proto_tree_add_item(dctree, hf_aim_dcinfo_webport, tvb, offset, 4, FALSE); offset+=4;
1160         proto_tree_add_item(dctree, hf_aim_dcinfo_client_future, tvb, offset, 4, FALSE); offset+=4;
1161         proto_tree_add_item(dctree, hf_aim_dcinfo_last_info_update, tvb, offset, 4, FALSE); offset+=4;
1162         proto_tree_add_item(dctree, hf_aim_dcinfo_last_ext_info_update, tvb, offset, 4, FALSE); offset+=4;
1163         proto_tree_add_item(dctree, hf_aim_dcinfo_last_ext_status_update, tvb, offset, 4, FALSE); offset+=4;
1164         proto_tree_add_item(dctree, hf_aim_dcinfo_unknown, tvb, offset, 2, FALSE); offset+=2;
1165
1166         return offset;
1167 }
1168
1169 int
1170 dissect_aim_tlv_value_string (proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_)
1171 {
1172         guint8 *buf;
1173         gint string_len;
1174
1175         string_len = tvb_length(tvb);
1176         buf = tvb_get_ephemeral_string(tvb, 0, string_len);
1177         proto_item_set_text(ti, "Value: %s", format_text(buf, string_len));
1178
1179         return string_len;
1180 }
1181
1182 int
1183 dissect_aim_tlv_value_string08_array (proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_)
1184 {
1185         proto_tree *entry;
1186         gint offset=0;
1187
1188         entry = proto_item_add_subtree(ti, ett_aim_string08_array);
1189
1190         while (tvb_length_remaining(tvb, offset) > 1)
1191         {
1192                 guint8 string_len = tvb_get_guint8(tvb, offset++);
1193                 guint8 *buf = tvb_get_ephemeral_string(tvb, offset, string_len);
1194                 proto_tree_add_text(entry, tvb, offset, string_len, "%s",
1195                                     format_text(buf, string_len));
1196                 offset += string_len;
1197         }
1198
1199         return offset;
1200 }
1201
1202 int
1203 dissect_aim_tlv_value_bytes (proto_item *ti _U_, guint16 valueid _U_, tvbuff_t *tvb _U_, packet_info *pinfo _U_)
1204 {
1205         return tvb_length(tvb);
1206 }
1207
1208 int
1209 dissect_aim_tlv_value_uint8 (proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_)
1210 {
1211         guint8 value8 = tvb_get_guint8(tvb, 0);
1212         proto_item_set_text(ti, "Value: %d", value8);
1213         return 1;
1214 }
1215
1216 int
1217 dissect_aim_tlv_value_uint16 (proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_)
1218 {
1219         guint16 value16 = tvb_get_ntohs(tvb, 0);
1220         proto_item_set_text(ti, "Value: %d", value16);
1221         return 2;
1222 }
1223
1224 int
1225 dissect_aim_tlv_value_ipv4 (proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_)
1226 {
1227         guint32 ipv4_address = tvb_get_ipv4(tvb, 0);
1228         proto_item_set_text(ti, "Value: %s", ip_to_str((guint8 *)&ipv4_address));
1229         return 4;
1230 }
1231
1232 int
1233 dissect_aim_tlv_value_uint32 (proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_)
1234 {
1235         guint32 value32 = tvb_get_ntohl(tvb, 0);
1236         proto_item_set_text(ti, "Value: %d", value32);
1237         return 4;
1238 }
1239
1240 int
1241 dissect_aim_tlv_value_messageblock (proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb, packet_info *pinfo _U_)
1242 {
1243         proto_tree *entry;
1244         guint8 *buf;
1245         guint16 featurelen;
1246         guint16 blocklen;
1247         int offset=0;
1248
1249         /* Setup a new subtree */
1250         entry = proto_item_add_subtree(ti, ett_aim_messageblock);
1251
1252         /* Features descriptor */
1253         proto_tree_add_item(entry, hf_aim_messageblock_featuresdes, tvb, offset,
1254                             2, FALSE);
1255         offset += 2;
1256
1257         /* Features Length */
1258         featurelen = tvb_get_ntohs(tvb, offset);
1259         proto_tree_add_item(entry, hf_aim_messageblock_featureslen, tvb, offset,
1260                             2, FALSE);
1261         offset += 2;
1262
1263         /* Features (should be expanded further @@@@@@@ ) */
1264         proto_tree_add_item(entry, hf_aim_messageblock_features, tvb, offset,
1265                             featurelen, FALSE);
1266         offset += featurelen;
1267
1268         /* There can be multiple messages in this message block */
1269         while (tvb_length_remaining(tvb, offset) > 0) {
1270                 /* Info field */
1271                 proto_tree_add_item(entry, hf_aim_messageblock_info, tvb,
1272                                     offset, 2, FALSE);
1273                 offset += 2;
1274
1275                 /* Block length (includes charset and charsubset) */
1276                 blocklen = tvb_get_ntohs(tvb, offset);
1277                 if (blocklen <= 4)
1278                 {
1279                         proto_tree_add_text(entry, tvb, offset, 2,
1280                                             "Invalid block length: %d", blocklen);
1281                         break;
1282                 }
1283                 proto_tree_add_item(entry, hf_aim_messageblock_len, tvb, offset,
1284                                     2, FALSE);
1285                 offset += 2;
1286
1287                 /* Character set */
1288                 proto_tree_add_item(entry, hf_aim_messageblock_charset, tvb,
1289                                     offset, 2, FALSE);
1290                 offset += 2;
1291
1292                 /* Character subset */
1293                 proto_tree_add_item(entry, hf_aim_messageblock_charsubset, tvb,
1294                                     offset, 2, FALSE);
1295                 offset += 2;
1296
1297                 /* The actual message */
1298                 buf = tvb_get_ephemeral_string(tvb, offset, blocklen - 4);
1299                 proto_item_set_text(ti, "Message: %s",
1300                                     format_text(buf, blocklen - 4));
1301                 proto_tree_add_item(entry, hf_aim_messageblock_message, tvb,
1302                                     offset, blocklen-4, FALSE);
1303
1304                 offset += tvb_length_remaining(tvb, offset);
1305         }
1306
1307         return offset;
1308 }
1309
1310 /* Dissect a TLV value */
1311 int
1312 dissect_aim_tlv(tvbuff_t *tvb, packet_info *pinfo _U_, int offset,
1313                 proto_tree *tree, const aim_tlv *tlv)
1314 {
1315         guint16 valueid;
1316         guint16 length;
1317         int i = 0;
1318         const aim_tlv *tmp;
1319         const char *desc;
1320         proto_item *ti1;
1321         proto_tree *tlv_tree;
1322         int orig_offset;
1323
1324         /* Record the starting offset so we can reuse it at the second pass */
1325         orig_offset = offset;
1326
1327         /* Get the value ID */
1328         valueid = tvb_get_ntohs(tvb, offset);
1329         offset += 2;
1330
1331         /* Figure out which entry applies from the tlv list */
1332         tmp = tlv;
1333         while (tmp[i].valueid) {
1334                 if (tmp[i].valueid == valueid) {
1335                         /* We found a match */
1336                         break;
1337                 }
1338                 i++;
1339         }
1340
1341         /* At this point, we are either pointing at the correct record, or
1342            we didn't find the record, and are pointing at the last item in the
1343            list */
1344
1345         length = tvb_get_ntohs(tvb, offset);
1346         offset += 2;
1347         offset += length;
1348
1349         if (tree) {
1350                 offset = orig_offset;
1351
1352                 if (tmp[i].desc != NULL)
1353                         desc = tmp[i].desc;
1354                 else
1355                         desc = "Unknown";
1356
1357                 ti1 = proto_tree_add_text(tree, tvb, offset, length + 4, "TLV: %s", desc);
1358
1359                 tlv_tree = proto_item_add_subtree(ti1, ett_aim_tlv);
1360
1361                 proto_tree_add_text(tlv_tree, tvb, offset, 2,
1362                                     "Value ID: %s (0x%04x)", desc, valueid);
1363                 offset += 2;
1364
1365                 proto_tree_add_text(tlv_tree, tvb, offset, 2,
1366                                     "Length: %d", length);
1367                 offset += 2;
1368
1369                 ti1 = proto_tree_add_text(tlv_tree, tvb, offset, length,
1370                                           "Value");
1371
1372                 if (tmp[i].dissector) {
1373                         tmp[i].dissector(ti1, valueid, tvb_new_subset(tvb, offset, length, length), pinfo);
1374                 }
1375
1376                 offset += length;
1377         }
1378
1379         /* Return the new length */
1380         return offset;
1381 }
1382
1383 int
1384 dissect_aim_tlv_sequence(tvbuff_t *tvb, packet_info *pinfo, int offset,
1385                          proto_tree *tree, const aim_tlv *tlv_table)
1386 {
1387         while (tvb_length_remaining(tvb, offset) > 0) {
1388                 offset = dissect_aim_tlv(tvb, pinfo, offset, tree, tlv_table);
1389         }
1390
1391         return offset;
1392 }
1393
1394 int
1395 dissect_aim_tlv_list(tvbuff_t *tvb, packet_info *pinfo, int offset,
1396                      proto_tree *tree, const aim_tlv *tlv_table)
1397 {
1398         guint16 i, tlv_count = tvb_get_ntohs(tvb, offset);
1399
1400         proto_tree_add_item(tree, hf_aim_tlvcount, tvb, offset, 2, FALSE);
1401         offset += 2;
1402
1403         for(i = 0; i < tlv_count; i++) {
1404                 offset = dissect_aim_tlv(tvb, pinfo, offset, tree, tlv_table);
1405         }
1406
1407         return offset;
1408 }
1409
1410 static guint
1411 get_aim_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
1412 {
1413         guint16 plen;
1414
1415         /*
1416         * Get the length of the AIM packet.
1417         */
1418         plen = tvb_get_ntohs(tvb, offset + 4);
1419
1420         /*
1421         * That length doesn't include the length of the header itself; add that in.
1422         */
1423         return plen + 6;
1424 }
1425
1426 static void
1427 dissect_aim_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1428 {
1429         /* Header fields */
1430         unsigned char  hdr_channel;           /* channel ID */
1431         unsigned short hdr_sequence_no;       /* Internal frame sequence number, not needed */
1432         unsigned short hdr_data_field_length; /* length of data within frame */
1433
1434         int offset=0;
1435
1436 /* Set up structures we will need to add the protocol subtree and manage it */
1437         proto_item *ti;
1438         proto_tree *aim_tree = NULL;
1439
1440 /* Make entries in Protocol column and Info column on summary display */
1441         col_set_str(pinfo->cinfo, COL_PROTOCOL, "AIM");
1442
1443         col_set_str(pinfo->cinfo, COL_INFO, "AOL Instant Messenger");
1444
1445         /* get relevant header information */
1446         offset += 1;          /* XXX - put the identifier into the tree? */
1447         hdr_channel           = tvb_get_guint8(tvb, offset);
1448         offset += 1;
1449         hdr_sequence_no       = tvb_get_ntohs(tvb, offset);
1450         offset += 2;
1451         hdr_data_field_length = tvb_get_ntohs(tvb, offset);
1452         offset += 2;
1453
1454 /* In the interest of speed, if "tree" is NULL, don't do any work not
1455    necessary to generate protocol tree items. */
1456         if (tree) {
1457                 ti = proto_tree_add_item(tree, proto_aim, tvb, 0, -1, FALSE);
1458                 aim_tree = proto_item_add_subtree(ti, ett_aim);
1459                 proto_tree_add_uint(aim_tree, hf_aim_cmd_start, tvb, 0, 1, '*');
1460                 proto_tree_add_item(aim_tree, hf_aim_channel, tvb, 1, 1, FALSE);
1461                 proto_tree_add_uint(aim_tree, hf_aim_seqno, tvb, 2, 2, hdr_sequence_no);
1462                 proto_tree_add_uint(aim_tree, hf_aim_data_len, tvb, 4, 2, hdr_data_field_length);
1463
1464         }
1465
1466         switch(hdr_channel)
1467         {
1468         case CHANNEL_NEW_CONN:
1469                 dissect_aim_newconn(tvb, pinfo, offset, aim_tree);
1470                 break;
1471         case CHANNEL_SNAC_DATA:
1472                 dissect_aim_snac(tvb, pinfo, offset, aim_tree, tree);
1473                 break;
1474         case CHANNEL_FLAP_ERR:
1475                 dissect_aim_flap_err(tvb, pinfo, offset, aim_tree);
1476                 break;
1477         case CHANNEL_CLOSE_CONN:
1478                 dissect_aim_close_conn(tvb, pinfo, offset, aim_tree);
1479                 break;
1480         case CHANNEL_KEEP_ALIVE:
1481                 dissect_aim_keep_alive(tvb, pinfo, offset, aim_tree);
1482                 break;
1483         default:
1484                 dissect_aim_unknown_channel(tvb, pinfo, offset, aim_tree);
1485                 break;
1486         }
1487
1488 }
1489
1490 /* Code to actually dissect the packets */
1491 static int
1492 dissect_aim(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1493 {
1494         /* check, if this is really an AIM packet, they start with 0x2a */
1495         /* XXX - I've seen some stuff starting with 0x5a followed by 0x2a */
1496
1497         if(tvb_length(tvb) >= 1 && tvb_get_guint8(tvb, 0) != 0x2a)
1498         {
1499                 /* Not an instant messenger packet, just happened to use the
1500                  * same port
1501                  *
1502                  * XXX - if desegmentation disabled, this might be a continuation
1503                  * packet, not a non-AIM packet
1504                  */
1505                 return 0;
1506         }
1507
1508         tcp_dissect_pdus(tvb, pinfo, tree, aim_desegment, 6, get_aim_pdu_len,
1509                          dissect_aim_pdu);
1510         return tvb_length(tvb);
1511 }
1512
1513
1514 /* Register the protocol with Wireshark */
1515 void
1516 proto_register_aim(void)
1517 {
1518
1519 /* Setup list of header fields */
1520         static hf_register_info hf[] = {
1521                 { &hf_aim_cmd_start,
1522                   { "Command Start", "aim.cmd_start", FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }
1523                 },
1524                 { &hf_aim_channel,
1525                   { "Channel ID", "aim.channel", FT_UINT8, BASE_HEX, VALS(aim_flap_channels), 0x0, NULL, HFILL }
1526                 },
1527                 { &hf_aim_seqno,
1528                   { "Sequence Number", "aim.seqno", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }
1529                 },
1530                 { &hf_aim_version,
1531                   { "Protocol Version", "aim.version", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL },
1532                 },
1533                 { &hf_aim_data_len,
1534                   { "Data Field Length", "aim.datalen", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }
1535                 },
1536                 { &hf_aim_data,
1537                   { "Data", "aim.data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }
1538                 },
1539                 { &hf_aim_signon_challenge_len,
1540                   { "Signon challenge length", "aim.signon.challengelen", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }
1541                 },
1542                 { &hf_aim_signon_challenge,
1543                   { "Signon challenge", "aim.signon.challenge", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }
1544                 },
1545                 { &hf_aim_fnac_family,
1546                   { "FNAC Family ID", "aim.fnac.family", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }
1547                 },
1548                 { &hf_aim_fnac_subtype,
1549                   { "FNAC Subtype ID", "aim.fnac.subtype", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }
1550                 },
1551                 { &hf_aim_fnac_flags,
1552                   { "FNAC Flags", "aim.fnac.flags", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }
1553                 },
1554                 { &hf_aim_fnac_id,
1555                   { "FNAC ID", "aim.fnac.id", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL }
1556                 },
1557                 { &hf_aim_infotype,
1558                   { "Infotype", "aim.infotype", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }
1559                 },
1560                 { &hf_aim_buddyname_len,
1561                   { "Buddyname len", "aim.buddynamelen", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }
1562                 },
1563                 { &hf_aim_buddyname,
1564                   { "Buddy Name", "aim.buddyname", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }
1565                 },
1566                 { &hf_aim_tlvcount,
1567                   { "TLV Count", "aim.tlvcount", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
1568                 },
1569                 { &hf_aim_snac_error,
1570                   { "SNAC Error", "aim.snac.error", FT_UINT16, BASE_HEX, VALS(aim_snac_errors), 0x0, NULL, HFILL },
1571                 },
1572                 { &hf_aim_userclass_unconfirmed,
1573                   { "AOL Unconfirmed account flag", "aim.userclass.unconfirmed", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_UNCONFIRMED, NULL, HFILL },
1574                 },
1575                 { &hf_aim_userclass_administrator,
1576                   { "AOL Administrator flag", "aim.userclass.administrator", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_ADMINISTRATOR, NULL, HFILL },
1577                 },
1578                 { &hf_aim_userclass_aol,
1579                   { "AOL Staff User Flag", "aim.userclass.staff", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_AOL, NULL, HFILL },
1580                 },
1581                 { &hf_aim_userclass_commercial,
1582                   { "AOL commercial account flag", "aim.userclass.commercial", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_COMMERCIAL, NULL, HFILL },
1583                 },
1584                 { &hf_aim_userclass_aim,
1585                   { "AIM user flag", "aim.userclass.free", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_AIM, NULL, HFILL },
1586                 },
1587                 { &hf_aim_userclass_away,
1588                   { "AOL away status flag", "aim.userclass.away", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_AWAY, NULL, HFILL },
1589                 },
1590                 { &hf_aim_userclass_icq,
1591                   { "ICQ user sign", "aim.userclass.icq", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_ICQ, NULL, HFILL },
1592                 },
1593                 { &hf_aim_userclass_wireless,
1594                   { "AOL wireless user", "aim.userclass.wireless", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_WIRELESS, NULL, HFILL },
1595                 },
1596                 { &hf_aim_userclass_unknown100,
1597                   { "Unknown bit", "aim.userclass.unknown100", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_UNKNOWN100, NULL, HFILL },
1598                 },
1599                 { &hf_aim_userclass_imf,
1600                   { "Using IM Forwarding", "aim.userclass.imf", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_IMF, NULL, HFILL },
1601                 },
1602                 { &hf_aim_userclass_bot,
1603                   { "Bot User", "aim.userclass.bot", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_BOT, NULL, HFILL },
1604                 },
1605                 { &hf_aim_userclass_unknown800,
1606                   { "Unknown bit", "aim.userclass.unknown800", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_UNKNOWN800, NULL, HFILL },
1607                 },
1608                 { &hf_aim_userclass_one_way_wireless,
1609                   { "One Way Wireless Device", "aim.userclass.one_way_wireless", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_ONE_WAY_WIRELESS, NULL, HFILL },
1610                 },
1611                 { &hf_aim_userclass_unknown2000,
1612                   { "Unknown bit", "aim.userclass.unknown2000", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_UNKNOWN2000, NULL, HFILL },
1613                 },
1614                 { &hf_aim_userclass_unknown4000,
1615                   { "Unknown bit", "aim.userclass.unknown4000", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_UNKNOWN4000, NULL, HFILL },
1616                 },
1617                 { &hf_aim_userclass_unknown8000,
1618                   { "Unknown bit", "aim.userclass.unknown8000", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_UNKNOWN8000, NULL, HFILL },
1619                 },
1620                 { &hf_aim_userclass_unknown10000,
1621                   { "Unknown bit", "aim.userclass.unknown10000", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_UNKNOWN10000, NULL, HFILL },
1622                 },
1623                 { &hf_aim_userclass_unknown20000,
1624                   { "Unknown bit", "aim.userclass.unknown20000", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_UNKNOWN20000, NULL, HFILL },
1625                 },
1626                 { &hf_aim_userclass_no_knock_knock,
1627                   { "Do not display the 'not on Buddy List' knock-knock", "aim.userclass.no_knock_knock", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_NO_KNOCK_KNOCK, NULL, HFILL },
1628                 },
1629                 { &hf_aim_userclass_forward_mobile,
1630                   { "Forward to mobile if not active", "aim.userclass.forward_mobile", FT_BOOLEAN, 32, TFS(&tfs_set_notset), CLASS_FORWARD_MOBILE, NULL, HFILL },
1631                 },
1632                 { &hf_aim_nickinfo_caps,
1633                   { "Client capabilities", "aim.nickinfo.caps", FT_GUID, BASE_NONE, NULL, 0x0, NULL, HFILL },
1634                 },
1635                 { &hf_aim_nickinfo_short_caps,
1636                   { "Short client capabilities", "aim.nickinfo.short_caps", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL },
1637                 },
1638                 { &hf_aim_fnac_flag_next_is_related,
1639                   { "Followed By SNAC with related information", "aim.fnac.flags.next_is_related", FT_BOOLEAN, 16, TFS(&tfs_set_notset), FNAC_FLAG_NEXT_IS_RELATED, NULL, HFILL },
1640                 },
1641                 { &hf_aim_fnac_flag_contains_version,
1642                   { "Contains Version of Family this SNAC is in", "aim.fnac.flags.contains_version", FT_BOOLEAN, 16, TFS(&tfs_set_notset), FNAC_FLAG_CONTAINS_VERSION, NULL, HFILL },
1643                 },
1644                 { &hf_aim_userinfo_warninglevel,
1645                   { "Warning Level", "aim.userinfo.warninglevel", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
1646                 },
1647                 { &hf_aim_messageblock_featuresdes,
1648                   { "Features", "aim.messageblock.featuresdes", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL },
1649                 },
1650                 { &hf_aim_messageblock_featureslen,
1651                   { "Features Length", "aim.messageblock.featureslen", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
1652                 },
1653                 { &hf_aim_messageblock_features,
1654                   { "Features", "aim.messageblock.features", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL },
1655                 },
1656                 { &hf_aim_messageblock_info,
1657                   { "Block info", "aim.messageblock.info", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL },
1658                 },
1659                 { &hf_aim_messageblock_len,
1660                   { "Block length", "aim.messageblock.length", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
1661                 },
1662                 { &hf_aim_messageblock_charset,
1663                   { "Block Character set", "aim.messageblock.charset", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL },
1664                 },
1665                 { &hf_aim_messageblock_charsubset,
1666                   { "Block Character subset", "aim.messageblock.charsubset", FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL },
1667                 },
1668                 { &hf_aim_messageblock_message,
1669                   { "Message", "aim.messageblock.message", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL },
1670                 },
1671                 { &hf_aim_dcinfo_ip,
1672                   { "Internal IP address", "aim.dcinfo.addr", FT_IPv4, BASE_NONE, NULL, 0x0, NULL, HFILL },
1673                 },
1674                 { &hf_aim_dcinfo_tcpport,
1675                   { "TCP Port", "aim.dcinfo.tcpport", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL },
1676                 },
1677                 { &hf_aim_dcinfo_type,
1678                   { "Type", "aim.dcinfo.type", FT_UINT8, BASE_HEX, VALS(dc_types), 0x0, NULL, HFILL },
1679                 },
1680                 { &hf_aim_dcinfo_proto_version,
1681                   { "Protocol Version", "aim.dcinfo.proto_version", FT_UINT16, BASE_DEC, VALS(protocol_versions), 0x0, NULL, HFILL },
1682                 },
1683                 { &hf_aim_dcinfo_auth_cookie,
1684                   { "Authorization Cookie", "aim.dcinfo.auth_cookie", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL },
1685                 },
1686                 { &hf_aim_dcinfo_webport,
1687                   { "Web Front Port", "aim.dcinfo.webport", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL },
1688                 },
1689                 { &hf_aim_dcinfo_client_future,
1690                   { "Client Futures", "aim.dcinfo.client_futures", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL },
1691                 },
1692                 { &hf_aim_dcinfo_last_info_update,
1693                   { "Last Info Update", "aim.dcinfo.last_info_update", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL },
1694                 },
1695                 { &hf_aim_dcinfo_last_ext_info_update,
1696                   { "Last Extended Info Update", "aim.dcinfo.last_ext_info_update", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL },
1697                 },
1698                 { &hf_aim_dcinfo_last_ext_status_update,
1699                   { "Last Extended Status Update", "aim.dcinfo.last_ext_status_update", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL },
1700                 },
1701                 { &hf_aim_dcinfo_unknown,
1702                   { "Unknown", "aim.dcinfo.unknown", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL },
1703                 },
1704                 { &hf_aim_ssi_result_code,
1705                   { "Last SSI operation result code", "aim.ssi.code", FT_UINT16, BASE_HEX, VALS(aim_ssi_result_codes), 0x0, NULL, HFILL },
1706                 }
1707         };
1708
1709         /* Setup protocol subtree array */
1710         static gint *ett[] = {
1711                 &ett_aim,
1712                 &ett_aim_dcinfo,
1713                 &ett_aim_fnac,
1714                 &ett_aim_fnac_flags,
1715                 &ett_aim_tlv,
1716                 &ett_aim_buddyname,
1717                 &ett_aim_userclass,
1718                 &ett_aim_messageblock,
1719                 &ett_aim_nickinfo_caps,
1720                 &ett_aim_nickinfo_short_caps,
1721                 &ett_aim_string08_array
1722         };
1723         module_t *aim_module;
1724
1725         /* Register the protocol name and description */
1726         proto_aim = proto_register_protocol("AOL Instant Messenger", "AIM", "aim");
1727
1728         /* Required function calls to register the header fields and subtrees used */
1729         proto_register_field_array(proto_aim, hf, array_length(hf));
1730         proto_register_subtree_array(ett, array_length(ett));
1731
1732         aim_module = prefs_register_protocol(proto_aim, NULL);
1733
1734         prefs_register_bool_preference(aim_module, "desegment",
1735                                        "Reassemble AIM messages spanning multiple TCP segments",
1736                                        "Whether the AIM dissector should reassemble messages spanning multiple TCP segments."
1737                                        " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
1738                                        &aim_desegment);
1739
1740         subdissector_table = register_dissector_table("aim.family", "Family ID", FT_UINT16, BASE_HEX);
1741 }
1742
1743 void
1744 proto_reg_handoff_aim(void)
1745 {
1746         dissector_handle_t aim_handle;
1747
1748         aim_handle = new_create_dissector_handle(dissect_aim, proto_aim);
1749         dissector_add_uint("tcp.port", TCP_PORT_AIM, aim_handle);
1750 }