Prevent the word "desegmentation" at the GUI, but use reassembling at that places...
[obnox/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  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
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 <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <ctype.h>
36
37 #include <glib.h>
38
39 #include "isprint.h"
40
41 #include <epan/packet.h>
42 #include <epan/strutil.h>
43
44 #include "packet-tcp.h"
45 #include "packet-aim.h"
46 #include "prefs.h"
47
48 #define TCP_PORT_AIM 5190
49
50 #define STRIP_TAGS 1
51
52 /* channels */
53 #define CHANNEL_NEW_CONN    0x01
54 #define CHANNEL_SNAC_DATA   0x02
55 #define CHANNEL_FLAP_ERR    0x03
56 #define CHANNEL_CLOSE_CONN  0x04
57 #define CHANNEL_KEEP_ALIVE  0x05
58
59 #define CLI_COOKIE                  0x01
60
61 #define FAMILY_ALL_ERROR_INVALID_HEADER                            0x0001
62 #define FAMILY_ALL_ERROR_SERVER_RATE_LIMIT_EXCEEDED    0x0002
63 #define FAMILY_ALL_ERROR_CLIENT_RATE_LIMIT_EXCEEDED    0x0003
64 #define FAMILY_ALL_ERROR_RECIPIENT_NOT_LOGGED_IN       0x0004
65 #define FAMILY_ALL_ERROR_REQUESTED_SERVICE_UNAVAILABLE 0x0005
66 #define FAMILY_ALL_ERROR_REQUESTED_SERVICE_NOT_DEFINED 0x0006
67 #define FAMILY_ALL_ERROR_OBSOLETE_SNAC                             0x0007
68 #define FAMILY_ALL_ERROR_NOT_SUPPORTED_BY_SERVER           0x0008
69 #define FAMILY_ALL_ERROR_NOT_SUPPORTED_BY_CLIENT           0x0009
70 #define FAMILY_ALL_ERROR_REFUSED_BY_CLIENT             0x000a
71 #define FAMILY_ALL_ERROR_REPLY_TOO_BIG                 0x000b
72 #define FAMILY_ALL_ERROR_RESPONSES_LOST                0x000c
73 #define FAMILY_ALL_ERROR_REQUEST_DENIED                0x000d
74 #define FAMILY_ALL_ERROR_INCORRECT_SNAC_FORMAT         0x000e
75 #define FAMILY_ALL_ERROR_INSUFFICIENT_RIGHTS           0x000f
76 #define FAMILY_ALL_ERROR_RECIPIENT_BLOCKED             0x0010
77 #define FAMILY_ALL_ERROR_SENDER_TOO_EVIL               0x0011
78 #define FAMILY_ALL_ERROR_RECEIVER_TOO_EVIL             0x0012
79 #define FAMILY_ALL_ERROR_USER_TEMP_UNAVAILABLE         0x0013
80 #define FAMILY_ALL_ERROR_NO_MATCH                      0x0014
81 #define FAMILY_ALL_ERROR_LIST_OVERFLOW                 0x0015
82 #define FAMILY_ALL_ERROR_REQUEST_AMBIGUOUS             0x0016
83 #define FAMILY_ALL_ERROR_SERVER_QUEUE_FULL             0x0017
84 #define FAMILY_ALL_ERROR_NOT_WHILE_ON_AOL              0x0018
85
86 static const value_string aim_flap_channels[] = {
87     { CHANNEL_NEW_CONN, "New Connection" },
88     { CHANNEL_SNAC_DATA, "SNAC Data" },
89     { CHANNEL_FLAP_ERR, "FLAP-Level Error" },
90     { CHANNEL_CLOSE_CONN, "Close Connection" },
91     { CHANNEL_KEEP_ALIVE, "Keep Alive" },
92     { 0, NULL }
93 };
94
95 static const value_string aim_snac_errors[] = {
96   { FAMILY_ALL_ERROR_INVALID_HEADER, "Invalid SNAC Header" },
97   { FAMILY_ALL_ERROR_SERVER_RATE_LIMIT_EXCEEDED, "Server rate limit exceeded" },
98   { FAMILY_ALL_ERROR_CLIENT_RATE_LIMIT_EXCEEDED, "Client rate limit exceeded" },
99   { FAMILY_ALL_ERROR_RECIPIENT_NOT_LOGGED_IN, "Recipient not logged in" },
100   { FAMILY_ALL_ERROR_REQUESTED_SERVICE_UNAVAILABLE, "Requested service unavailable" },
101   { FAMILY_ALL_ERROR_REQUESTED_SERVICE_NOT_DEFINED, "Requested service not defined" },
102   { FAMILY_ALL_ERROR_OBSOLETE_SNAC, "Obsolete SNAC issued" },
103   { FAMILY_ALL_ERROR_NOT_SUPPORTED_BY_SERVER, "Not supported by server" },
104   { FAMILY_ALL_ERROR_NOT_SUPPORTED_BY_CLIENT, "Not supported by client" },
105   { FAMILY_ALL_ERROR_REFUSED_BY_CLIENT, "Refused by client" },
106   { FAMILY_ALL_ERROR_REPLY_TOO_BIG, "Reply too big" },
107   { FAMILY_ALL_ERROR_RESPONSES_LOST, "Responses lost" },
108   { FAMILY_ALL_ERROR_REQUEST_DENIED, "Request denied" },
109   { FAMILY_ALL_ERROR_INCORRECT_SNAC_FORMAT, "Incorrect SNAC format" },
110   { FAMILY_ALL_ERROR_INSUFFICIENT_RIGHTS, "Insufficient rights" },
111   { FAMILY_ALL_ERROR_RECIPIENT_BLOCKED, "Recipient blocked" },
112   { FAMILY_ALL_ERROR_SENDER_TOO_EVIL, "Sender too evil" },
113   { FAMILY_ALL_ERROR_RECEIVER_TOO_EVIL, "Receiver too evil" },
114   { FAMILY_ALL_ERROR_USER_TEMP_UNAVAILABLE, "User temporarily unavailable" },
115   { FAMILY_ALL_ERROR_NO_MATCH, "No match" },
116   { FAMILY_ALL_ERROR_LIST_OVERFLOW, "List overflow" },
117   { FAMILY_ALL_ERROR_REQUEST_AMBIGUOUS, "Request ambiguous" },
118   { FAMILY_ALL_ERROR_SERVER_QUEUE_FULL, "Server queue full" },
119   { FAMILY_ALL_ERROR_NOT_WHILE_ON_AOL, "Not while on AOL" },
120   { 0, NULL }
121 };
122
123 #define AIM_CLIENT_TLV_SCREEN_NAME                              0x0001
124 #define AIM_CLIENT_TLV_NEW_ROASTED_PASSWORD                             0x0002
125 #define AIM_CLIENT_TLV_CLIENT_ID_STRING                         0x0003
126 #define AIM_CLIENT_TLV_ERRORURL                                 0x0004
127 #define AIM_CLIENT_TLV_BOS_SERVER_STRING       0x0005
128 #define AIM_CLIENT_TLV_AUTH_COOKIE             0x0006
129 #define AIM_CLIENT_TLV_ERRORCODE                            0x0008
130 #define AIM_CLIENT_TLV_DISCONNECT_REASON                0x0009
131 #define AIM_CLIENT_TLV_RECONNECT_HOST              0x000a
132 #define AIM_CLIENT_TLV_URL                                         0x000b
133 #define AIM_CLIENT_TLV_DEBUG_DATA                          0x000c
134 #define AIM_CLIENT_TLV_GENERIC_SERVICE_ID      0x000d
135 #define AIM_CLIENT_TLV_CLIENT_COUNTRY          0x000e
136 #define AIM_CLIENT_TLV_CLIENT_LANGUAGE         0x000f
137 #define AIM_CLIENT_TLV_EMAILADDR                            0x0011
138 #define AIM_CLIENT_TLV_OLD_ROASTED_PASSWORD     0x0012
139 #define AIM_CLIENT_TLV_REGSTATUS                            0x0013
140 #define AIM_CLIENT_TLV_CLIENT_DISTRIBUTION_NUM 0x0014
141 #define AIM_CLIENT_TLV_INVITEMESSAGE                    0x0015
142 #define AIM_CLIENT_TLV_CLIENT_ID               0x0016
143 #define AIM_CLIENT_TLV_CLIENT_MAJOR_VERSION    0x0017
144 #define AIM_CLIENT_TLV_CLIENT_MINOR_VERSION    0x0018
145 #define AIM_CLIENT_TLV_CLIENT_LESSER_VERSION   0x0019
146 #define AIM_CLIENT_TLV_CLIENT_BUILD_NUMBER     0x001a
147 #define AIM_CLIENT_TLV_PASSWORD                         0x0025
148 #define AIM_CLIENT_TLV_LATESTBETABUILD          0x0040
149 #define AIM_CLIENT_TLV_LATESTBETAURL            0x0041
150 #define AIM_CLIENT_TLV_LATESTBETAINFO           0x0042
151 #define AIM_CLIENT_TLV_LATESTBETANAME           0x0043
152 #define AIM_CLIENT_TLV_LATESTRELEASEBUILD       0x0044
153 #define AIM_CLIENT_TLV_LATESTRELEASEURL         0x0045
154 #define AIM_CLIENT_TLV_LATESTRELEASEINFO        0x0046
155 #define AIM_CLIENT_TLV_LATESTRELEASENAME        0x0047
156 #define AIM_CLIENT_TLV_BETA_DIGEST_SIG          0x0048
157 #define AIM_CLIENT_TLV_RELEASE_DIGEST_SIG   0x0049
158 #define AIM_CLIENT_TLV_CLIENTUSESSI                     0x004a
159 #define AIM_CLIENT_TLV_CHANGE_PASSWORD_URL              0x0054
160
161 const aim_tlv client_tlvs[] = {
162   {  AIM_CLIENT_TLV_SCREEN_NAME, "Screen name", dissect_aim_tlv_value_string },
163   {  AIM_CLIENT_TLV_NEW_ROASTED_PASSWORD, "Roasted password array", dissect_aim_tlv_value_bytes  },
164   {  AIM_CLIENT_TLV_OLD_ROASTED_PASSWORD, "Old roasted password array", dissect_aim_tlv_value_bytes  },
165   {  AIM_CLIENT_TLV_CLIENT_ID_STRING, "Client id string (name, version)", dissect_aim_tlv_value_string },
166   {  AIM_CLIENT_TLV_CLIENT_ID, "Client id number", dissect_aim_tlv_value_uint16 },
167   {  AIM_CLIENT_TLV_CLIENT_MAJOR_VERSION, "Client major version", dissect_aim_tlv_value_uint16 },
168   {  AIM_CLIENT_TLV_CLIENT_MINOR_VERSION, "Client minor version", dissect_aim_tlv_value_uint16 },
169   {  AIM_CLIENT_TLV_CLIENT_LESSER_VERSION, "Client lesser version", dissect_aim_tlv_value_uint16 },
170   {  AIM_CLIENT_TLV_CLIENT_BUILD_NUMBER, "Client build number", dissect_aim_tlv_value_uint16 },
171   {  AIM_CLIENT_TLV_CLIENT_DISTRIBUTION_NUM, "Client distribution number", dissect_aim_tlv_value_uint16 },
172   {  AIM_CLIENT_TLV_CLIENT_LANGUAGE, "Client language", dissect_aim_tlv_value_string },
173   {  AIM_CLIENT_TLV_CLIENT_COUNTRY, "Client country", dissect_aim_tlv_value_string },
174   {  AIM_CLIENT_TLV_BOS_SERVER_STRING, "BOS server string", dissect_aim_tlv_value_string },
175   {  AIM_CLIENT_TLV_AUTH_COOKIE, "Authorization cookie", dissect_aim_tlv_value_bytes },
176   {  AIM_CLIENT_TLV_ERRORURL, "Error URL", dissect_aim_tlv_value_string },
177   {  AIM_CLIENT_TLV_ERRORCODE, "Error Code", dissect_aim_tlv_value_uint16 }, 
178   {  AIM_CLIENT_TLV_DISCONNECT_REASON, "Disconnect Reason", dissect_aim_tlv_value_uint16 },
179   {  AIM_CLIENT_TLV_RECONNECT_HOST, "Reconnect Hostname", dissect_aim_tlv_value_string },
180   {  AIM_CLIENT_TLV_URL, "URL", dissect_aim_tlv_value_string },
181   {  AIM_CLIENT_TLV_DEBUG_DATA, "Debug Data", dissect_aim_tlv_value_uint16 },
182   {  AIM_CLIENT_TLV_EMAILADDR, "Account Email address", dissect_aim_tlv_value_string },
183   {  AIM_CLIENT_TLV_REGSTATUS, "Registration Status", dissect_aim_tlv_value_uint16 },
184   {  AIM_CLIENT_TLV_LATESTBETABUILD, "Latest Beta Build", dissect_aim_tlv_value_uint32 },
185   {  AIM_CLIENT_TLV_LATESTBETAURL, "Latest Beta URL", dissect_aim_tlv_value_string },
186   {  AIM_CLIENT_TLV_LATESTBETAINFO, "Latest Beta Info", dissect_aim_tlv_value_string },
187   {  AIM_CLIENT_TLV_LATESTBETANAME, "Latest Beta Name", dissect_aim_tlv_value_string },
188   {  AIM_CLIENT_TLV_LATESTRELEASEBUILD, "Latest Release Build", dissect_aim_tlv_value_uint32 },
189   {  AIM_CLIENT_TLV_LATESTRELEASEURL, "Latest Release URL", dissect_aim_tlv_value_string },
190   {  AIM_CLIENT_TLV_LATESTRELEASEINFO, "Latest Release Info", dissect_aim_tlv_value_string  },
191   {  AIM_CLIENT_TLV_LATESTRELEASENAME, "Latest Release Name", dissect_aim_tlv_value_string },
192   {  AIM_CLIENT_TLV_BETA_DIGEST_SIG, "Beta Digest Signature (MD5)" , dissect_aim_tlv_value_bytes },
193   {  AIM_CLIENT_TLV_RELEASE_DIGEST_SIG, "Release Digest Signature (MD5)", dissect_aim_tlv_value_bytes },
194   {  AIM_CLIENT_TLV_CLIENTUSESSI, "Use SSI", dissect_aim_tlv_value_uint8 },
195   {  AIM_CLIENT_TLV_GENERIC_SERVICE_ID, "Service (Family) ID", dissect_aim_tlv_value_uint16 },
196   { AIM_CLIENT_TLV_CHANGE_PASSWORD_URL, "Change password url", dissect_aim_tlv_value_string },
197   { 0, "Unknown", NULL },
198 };
199
200
201 static int dissect_aim_tlv_value_userstatus(proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb);
202 static int dissect_aim_tlv_value_dcinfo(proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb);
203
204 #define AIM_ONLINEBUDDY_USERCLASS      0x0001
205 #define AIM_ONLINEBUDDY_ONSINCE        0x0003
206 #define AIM_ONLINEBUDDY_IDLETIME       0x0004
207 #define AIM_ONLINEBUDDY_MEMBERSINCE        0x0005
208 #define AIM_ONLINEBUDDY_STATUS         0x0006
209 #define AIM_ONLINEBUDDY_TIMEUPDATE         0x0011
210 #define AIM_ONLINEBUDDY_IPADDR         0x000a
211 #define AIM_ONLINEBUDDY_DCINFO             0x000c
212 #define AIM_ONLINEBUDDY_CAPINFO        0x000d
213 #define AIM_ONLINEBUDDY_UNKNOWN        0x000e
214 #define AIM_ONLINEBUDDY_SESSIONLEN     0x000f
215 #define AIM_ONLINEBUDDY_ICQSESSIONLEN  0x0010
216
217 const aim_tlv onlinebuddy_tlvs[] = {
218   { AIM_ONLINEBUDDY_USERCLASS, "User class", dissect_aim_tlv_value_userclass },
219   { AIM_ONLINEBUDDY_ONSINCE, "Online since", dissect_aim_tlv_value_uint32 },
220   { AIM_ONLINEBUDDY_IDLETIME, "Idle time (sec)", dissect_aim_tlv_value_uint16 },
221   { AIM_ONLINEBUDDY_STATUS, "Online status", dissect_aim_tlv_value_userstatus },
222   { AIM_ONLINEBUDDY_IPADDR, "User IP Address", dissect_aim_tlv_value_ipv4 },
223   { AIM_ONLINEBUDDY_DCINFO, "DC Info", dissect_aim_tlv_value_dcinfo},
224   { AIM_ONLINEBUDDY_CAPINFO, "Capability Info", dissect_aim_tlv_value_bytes },
225   { AIM_ONLINEBUDDY_MEMBERSINCE, "Member since", dissect_aim_tlv_value_time },
226   { AIM_ONLINEBUDDY_UNKNOWN, "Unknown", dissect_aim_tlv_value_uint16 },
227   { AIM_ONLINEBUDDY_TIMEUPDATE, "Time update", dissect_aim_tlv_value_bytes },
228   { AIM_ONLINEBUDDY_SESSIONLEN, "Session Length (sec)", dissect_aim_tlv_value_uint32 },
229   { AIM_ONLINEBUDDY_ICQSESSIONLEN, "ICQ Session Length (sec)", dissect_aim_tlv_value_uint32 },
230   { 0, "Unknown", NULL }
231 };
232
233 struct aim_family {
234         guint16 family;
235         const char *name;
236         const value_string *subtypes;
237 };
238
239 static GList *families = NULL;
240
241 #define AIM_MOTD_TLV_MOTD                                          0x000B
242
243 const aim_tlv motd_tlvs[] = {
244   { AIM_MOTD_TLV_MOTD, "Message of the day message", dissect_aim_tlv_value_string },
245   { 0, "Unknown", NULL }
246 };
247
248 #define FAMILY_GENERIC_REDIRECT_SERVER_ADDRESS             0x0005
249 #define FAMILY_GENERIC_REDIRECT_AUTH_COOKIE                        0x0006
250 #define FAMILY_GENERIC_REDIRECT_FAMILY_ID              0x000D
251
252 static const aim_tlv aim_fnac_family_generic_redirect_tlv[] = {
253   { FAMILY_GENERIC_REDIRECT_SERVER_ADDRESS, "Server address and (optional) port", dissect_aim_tlv_value_string },
254   { FAMILY_GENERIC_REDIRECT_AUTH_COOKIE, "Authorization cookie", dissect_aim_tlv_value_string },
255   { FAMILY_GENERIC_REDIRECT_FAMILY_ID, "Family ID", dissect_aim_tlv_value_uint16 },
256   { 0, "Unknown", NULL }
257 };
258
259 #define FAMILY_GENERIC_MOTD_MOTDTYPE_MDT_UPGRADE       0x0001
260 #define FAMILY_GENERIC_MOTD_MOTDTYPE_ADV_UPGRADE       0x0002
261 #define FAMILY_GENERIC_MOTD_MOTDTYPE_SYS_BULLETIN      0x0003
262 #define FAMILY_GENERIC_MOTD_MOTDTYPE_NORMAL            0x0004
263 #define FAMILY_GENERIC_MOTD_MOTDTYPE_NEWS              0x0006
264
265 static const value_string aim_snac_generic_motd_motdtypes[] = {
266   { FAMILY_GENERIC_MOTD_MOTDTYPE_MDT_UPGRADE, "Mandatory Upgrade Needed Notice" },
267   { FAMILY_GENERIC_MOTD_MOTDTYPE_ADV_UPGRADE, "Advisable Upgrade Notice" },
268   { FAMILY_GENERIC_MOTD_MOTDTYPE_SYS_BULLETIN, "AIM/ICQ Service System Announcements" },
269   { FAMILY_GENERIC_MOTD_MOTDTYPE_NORMAL, "Standard Notice" },
270   { FAMILY_GENERIC_MOTD_MOTDTYPE_NEWS, "News from AOL service" },
271   { 0, NULL }
272 };
273
274 #define CLASS_UNCONFIRMED            0x0001
275 #define CLASS_ADMINISTRATOR          0x0002
276 #define CLASS_AOL                    0x0004
277 #define CLASS_COMMERCIAL             0x0008
278 #define CLASS_FREE                   0x0010
279 #define CLASS_AWAY                   0x0020
280 #define CLASS_ICQ                    0x0040
281 #define CLASS_WIRELESS               0x0080
282 #define CLASS_UNKNOWN100             0x0100
283 #define CLASS_UNKNOWN200             0x0200
284 #define CLASS_UNKNOWN400             0x0400
285 #define CLASS_UNKNOWN800             0x0800
286
287 static int dissect_aim(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
288 static guint get_aim_pdu_len(tvbuff_t *tvb, int offset);
289 static void dissect_aim_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree);
290
291 static void dissect_aim_newconn(tvbuff_t *tvb, packet_info *pinfo, int offset, proto_tree *tree);
292 static void dissect_aim_snac(tvbuff_t *tvb, packet_info *pinfo, 
293                              int offset, proto_tree *tree, proto_tree *root_tree);
294 static void dissect_aim_flap_err(tvbuff_t *tvb, packet_info *pinfo, 
295                                  int offset, proto_tree *tree);
296 static void dissect_aim_keep_alive(tvbuff_t *tvb, packet_info *pinfo, 
297                                    int offset, proto_tree *tree);
298 static void dissect_aim_close_conn(tvbuff_t *tvb, packet_info *pinfo, 
299                                    int offset, proto_tree *tree);
300 static void dissect_aim_unknown_channel(tvbuff_t *tvb, packet_info *pinfo, 
301                                         int offset, proto_tree *tree);
302
303 static dissector_table_t subdissector_table;
304
305 /* Initialize the protocol and registered fields */
306 static int proto_aim = -1;
307 static int hf_aim_cmd_start = -1;
308 static int hf_aim_channel = -1;
309 static int hf_aim_seqno = -1;
310 static int hf_aim_data = -1;
311 static int hf_aim_data_len = -1;
312 static int hf_aim_signon_challenge_len = -1;
313 static int hf_aim_signon_challenge = -1;
314 static int hf_aim_fnac_family = -1;
315 static int hf_aim_fnac_subtype = -1;
316 static int hf_aim_fnac_flags = -1;
317 static int hf_aim_fnac_id = -1;
318 static int hf_aim_infotype = -1;
319 static int hf_aim_buddyname_len = -1;
320 static int hf_aim_buddyname = -1;
321 static int hf_aim_userinfo_warninglevel = -1;
322 static int hf_aim_snac_error = -1;
323 static int hf_aim_tlvcount = -1;
324 static int hf_aim_authcookie = -1;
325 static int hf_aim_userclass_unconfirmed = -1;
326 static int hf_aim_userclass_administrator = -1;
327 static int hf_aim_userclass_aol = -1;
328 static int hf_aim_userclass_commercial = -1;
329 static int hf_aim_userclass_free = -1;
330 static int hf_aim_userclass_away = -1;
331 static int hf_aim_userclass_icq = -1;
332 static int hf_aim_userclass_wireless = -1;
333 static int hf_aim_userclass_unknown100 = -1;
334 static int hf_aim_userclass_unknown200 = -1;
335 static int hf_aim_userclass_unknown400 = -1;
336 static int hf_aim_userclass_unknown800 = -1;
337 static int hf_aim_messageblock_featuresdes = -1;
338 static int hf_aim_messageblock_featureslen = -1;
339 static int hf_aim_messageblock_features = -1;
340 static int hf_aim_messageblock_info = -1;
341 static int hf_aim_messageblock_len = -1;
342 static int hf_aim_messageblock_charset = -1;
343 static int hf_aim_messageblock_charsubset = -1;
344 static int hf_aim_messageblock_message = -1;
345
346
347 /* Initialize the subtree pointers */
348 static gint ett_aim          = -1;
349 static gint ett_aim_buddyname= -1;
350 static gint ett_aim_fnac     = -1;
351 static gint ett_aim_tlv      = -1;
352 static gint ett_aim_userclass = -1;
353 static gint ett_aim_messageblock = -1;
354
355 /* desegmentation of AIM over TCP */
356 static gboolean aim_desegment = TRUE;
357
358 /* Code to actually dissect the packets */
359 static int dissect_aim(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
360 {
361 /* check, if this is really an AIM packet, they start with 0x2a */
362 /* XXX - I've seen some stuff starting with 0x5a followed by 0x2a */
363
364   if(tvb_bytes_exist(tvb, 0, 1) && tvb_get_guint8(tvb, 0) != 0x2a) {
365     /* Not an instant messenger packet, just happened to use the same port */
366     /* XXX - if desegmentation disabled, this might be a continuation
367        packet, not a non-AIM packet */
368     return 0;
369   }
370
371   tcp_dissect_pdus(tvb, pinfo, tree, aim_desegment, 6, get_aim_pdu_len,
372         dissect_aim_pdu);
373   return tvb_length(tvb);
374 }
375
376 static guint get_aim_pdu_len(tvbuff_t *tvb, int offset)
377 {
378   guint16 plen;
379
380   /*
381    * Get the length of the AIM packet.
382    */
383   plen = tvb_get_ntohs(tvb, offset + 4);
384
385   /*
386    * That length doesn't include the length of the header itself; add that in.
387    */
388   return plen + 6;
389 }
390
391 static void dissect_aim_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
392 {
393   /* Header fields */
394   unsigned char  hdr_channel;           /* channel ID */
395   unsigned short hdr_sequence_no;       /* Internal frame sequence number, not needed */
396   unsigned short hdr_data_field_length; /* length of data within frame */
397
398   int offset=0;
399
400 /* Set up structures we will need to add the protocol subtree and manage it */
401   proto_item *ti;
402   proto_tree *aim_tree = NULL;
403
404 /* Make entries in Protocol column and Info column on summary display */
405   if (check_col(pinfo->cinfo, COL_PROTOCOL))
406     col_set_str(pinfo->cinfo, COL_PROTOCOL, "AIM");
407
408   if (check_col(pinfo->cinfo, COL_INFO))
409     col_add_str(pinfo->cinfo, COL_INFO, "AOL Instant Messenger");
410
411   /* get relevant header information */
412   offset += 1;          /* XXX - put the identifier into the tree? */   
413   hdr_channel           = tvb_get_guint8(tvb, offset);
414   offset += 1;
415   hdr_sequence_no       = tvb_get_ntohs(tvb, offset);
416   offset += 2;
417   hdr_data_field_length = tvb_get_ntohs(tvb, offset);
418   offset += 2;
419
420 /* In the interest of speed, if "tree" is NULL, don't do any work not
421    necessary to generate protocol tree items. */
422   if (tree) {
423     ti = proto_tree_add_item(tree, proto_aim, tvb, 0, -1, FALSE);
424     aim_tree = proto_item_add_subtree(ti, ett_aim);
425     proto_tree_add_uint(aim_tree, hf_aim_cmd_start, tvb, 0, 1, '*');
426     proto_tree_add_item(aim_tree, hf_aim_channel, tvb, 1, 1, FALSE);
427     proto_tree_add_uint(aim_tree, hf_aim_seqno, tvb, 2, 2, hdr_sequence_no);
428     proto_tree_add_uint(aim_tree, hf_aim_data_len, tvb, 4, 2, hdr_data_field_length);
429
430   }
431
432   switch(hdr_channel)
433   {
434     case CHANNEL_NEW_CONN:
435       dissect_aim_newconn(tvb, pinfo, offset, aim_tree);
436       break;
437     case CHANNEL_SNAC_DATA:
438       dissect_aim_snac(tvb, pinfo, offset, aim_tree, tree);
439       break;
440     case CHANNEL_FLAP_ERR:
441       dissect_aim_flap_err(tvb, pinfo, offset, aim_tree);
442       break;
443     case CHANNEL_CLOSE_CONN:
444       dissect_aim_close_conn(tvb, pinfo, offset, aim_tree);
445       break;
446     case CHANNEL_KEEP_ALIVE:
447      dissect_aim_keep_alive(tvb, pinfo, offset, aim_tree);
448      break;
449     default:
450       dissect_aim_unknown_channel(tvb, pinfo, offset, aim_tree);
451       break;
452   }
453
454 }
455
456 const char *aim_get_subtypename( guint16 famnum, guint16 subtype )
457 {
458         GList *gl = families;
459         while(gl) {
460                 struct aim_family *fam = gl->data;
461                 if(fam->family == famnum) return match_strval(subtype, fam->subtypes);
462                 gl = gl->next;
463         }
464
465         return NULL;
466
467 }
468
469 const char *aim_get_familyname( guint16 famnum ) 
470 {
471         GList *gl = families;
472         while(gl) {
473                 struct aim_family *fam = gl->data;
474                 if(fam->family == famnum) return fam->name;
475                 gl = gl->next;
476         }
477
478         return NULL;
479 }
480
481 int aim_get_buddyname( char *name, tvbuff_t *tvb, int len_offset, int name_offset)
482 {
483   guint8 buddyname_length;
484
485   buddyname_length = tvb_get_guint8(tvb, len_offset);
486
487   if(buddyname_length > MAX_BUDDYNAME_LENGTH ) buddyname_length = MAX_BUDDYNAME_LENGTH;
488   tvb_get_nstringz0(tvb, name_offset, buddyname_length + 1, name);
489
490   return buddyname_length;
491 }
492
493
494 void aim_get_message( guchar *msg, tvbuff_t *tvb, int msg_offset, int msg_length)
495 {
496   int i,j,c;
497   int bracket = FALSE;
498   int max, tagchars = 0;
499   int new_offset = msg_offset;
500   int new_length = msg_length;
501
502
503
504   /* make sure nothing bigger than 1000 bytes is printed */
505   if( msg_length > 999 ) return;
506
507   memset( msg, '\0', 1000);
508   i = 0;
509   c = 0;
510
511   /* loop until HTML tag is reached - quick&dirty way to find start of message
512    * (it is nearly impossible to find the correct start offset for all client versions) */
513   while( (tagchars < 6) && (new_length > 5) )
514   {
515      j = tvb_get_guint8(tvb, new_offset);
516      if( ( (j == '<') && (tagchars == 0) ) ||
517          ( (j == 'h') && (tagchars == 1) ) ||
518          ( (j == 'H') && (tagchars == 1) ) ||
519          ( (j == 't') && (tagchars == 2) ) ||
520          ( (j == 'T') && (tagchars == 2) ) ||
521          ( (j == 'm') && (tagchars == 3) ) ||
522          ( (j == 'M') && (tagchars == 3) ) ||
523          ( (j == 'l') && (tagchars == 4) ) ||
524          ( (j == 'L') && (tagchars == 4) ) ||
525          ( (j == '>') && (tagchars == 5) ) ) tagchars++;
526      new_offset++;
527      new_length--;
528   }
529
530   /* set offset and length of message to after the first HTML tag */
531   msg_offset = new_offset;
532   msg_length = new_length;
533   max = msg_length - 1;
534   tagchars = 0;
535
536   /* find the rest of the message until either a </html> is reached or the end of the frame.
537    * All other HTML tags are stripped to display only the raw message (printable characters) */
538   while( (c < max) && (tagchars < 7) )
539   {
540      j = tvb_get_guint8(tvb, msg_offset+c);
541
542
543      /* make sure this is an HTML tag by checking the order of the chars */
544      if( ( (j == '<') && (tagchars == 0) ) ||
545          ( (j == '/') && (tagchars == 1) ) ||
546          ( (j == 'h') && (tagchars == 2) ) ||
547          ( (j == 'H') && (tagchars == 2) ) ||
548          ( (j == 't') && (tagchars == 3) ) ||
549          ( (j == 'T') && (tagchars == 3) ) ||
550          ( (j == 'm') && (tagchars == 4) ) ||
551          ( (j == 'M') && (tagchars == 4) ) ||
552          ( (j == 'l') && (tagchars == 5) ) ||
553          ( (j == 'L') && (tagchars == 5) ) ||
554          ( (j == '>') && (tagchars == 6) ) ) tagchars++;
555
556 #ifdef STRIP_TAGS
557      if( j == '<' ) bracket = TRUE;
558      if( j == '>' ) bracket = FALSE;
559      if( (isprint(j) ) && (bracket == FALSE) && (j != '>'))
560 #else
561      if( isprint(j) )
562 #endif
563      {
564        msg[i] = j;
565        i++;
566      }
567      c++;
568   }
569 }
570
571 void aim_init_family(guint16 family, const char *name, const value_string *subtypes) 
572 {
573         struct aim_family *fam = g_new(struct aim_family, 1);
574         fam->name = g_strdup(name);
575         fam->family = family;
576         fam->subtypes = subtypes;
577         families = g_list_append(families, fam);
578 }
579
580 static void dissect_aim_newconn(tvbuff_t *tvb, packet_info *pinfo, 
581                                 int offset, proto_tree *tree)
582 {
583   if (check_col(pinfo->cinfo, COL_INFO)) 
584     col_add_fstr(pinfo->cinfo, COL_INFO, "New Connection");
585
586   if (tvb_length_remaining(tvb, offset) > 0) {
587           proto_tree_add_item(tree, hf_aim_authcookie, tvb, offset, 4, FALSE);
588           offset+=4;
589           while(tvb_reported_length_remaining(tvb, offset) > 0) {
590                   offset = dissect_aim_tlv(tvb, pinfo, offset, tree, client_tlvs);
591           }
592   }
593
594   if (tvb_length_remaining(tvb, offset) > 0)
595     proto_tree_add_item(tree, hf_aim_data, tvb, offset, -1, FALSE);
596 }
597
598
599 int dissect_aim_snac_error(tvbuff_t *tvb, packet_info *pinfo, 
600                              int offset, proto_tree *aim_tree)
601 {
602   char *name;
603   if ((name = match_strval(tvb_get_ntohs(tvb, offset), aim_snac_errors)) != NULL) {
604      if (check_col(pinfo->cinfo, COL_INFO))
605                 col_add_fstr(pinfo->cinfo, COL_INFO, name);
606   }
607
608   proto_tree_add_item (aim_tree, hf_aim_snac_error,
609                            tvb, offset, 2, FALSE);
610   return tvb_length_remaining(tvb, 2);
611 }
612
613 int dissect_aim_userinfo(tvbuff_t *tvb, packet_info *pinfo, 
614                                  int offset, proto_tree *tree) 
615 {
616         offset = dissect_aim_buddyname(tvb, pinfo, offset, tree);
617
618     proto_tree_add_item(tree, hf_aim_userinfo_warninglevel, tvb, offset, 2, FALSE);
619     offset += 2;
620
621         return dissect_aim_tlv_list(tvb, pinfo, offset, tree, onlinebuddy_tlvs);
622 }
623
624 static void dissect_aim_snac(tvbuff_t *tvb, packet_info *pinfo, 
625                              int offset, proto_tree *aim_tree, proto_tree *root_tree)
626 {
627   guint16 family;
628   guint16 subtype;
629   guint16 flags;
630   guint32 id;
631   proto_item *ti1;
632   struct aiminfo aiminfo;
633   const char *fam_name, *subtype_name;
634   proto_tree *aim_tree_fnac = NULL;
635   tvbuff_t *subtvb;
636   int orig_offset;
637
638   orig_offset = offset;
639   family = tvb_get_ntohs(tvb, offset);
640   fam_name = aim_get_familyname(family);
641   offset += 2;
642   subtype = tvb_get_ntohs(tvb, offset);
643   subtype_name = aim_get_subtypename(family, subtype);
644   offset += 2;
645   flags = tvb_get_ntohs(tvb, offset);
646   offset += 2;
647   id = tvb_get_ntohl(tvb, offset);
648   offset += 4;
649   
650   if (check_col(pinfo->cinfo, COL_INFO)) {
651     col_add_fstr(pinfo->cinfo, COL_INFO, "SNAC data");
652   }
653   
654   if( aim_tree )
655     {
656       offset = orig_offset;
657       ti1 = proto_tree_add_text(aim_tree, tvb, 6, 10, "FNAC");
658       aim_tree_fnac = proto_item_add_subtree(ti1, ett_aim_fnac);
659
660       proto_tree_add_text (aim_tree_fnac, 
661                            tvb, offset, 2, "Family: %s (0x%04x)", fam_name?fam_name:"Unknown", family);
662       offset += 2;
663
664       proto_tree_add_text (aim_tree_fnac, 
665                            tvb, offset, 2, "Subtype: %s (0x%04x)", subtype_name?subtype_name:"Unknown", subtype);
666       offset += 2;
667
668       proto_tree_add_uint(aim_tree_fnac, hf_aim_fnac_flags, tvb, offset, 
669                           2, flags);
670       offset += 2;
671       proto_tree_add_uint(aim_tree_fnac, hf_aim_fnac_id, tvb, offset,
672                           4, id);
673       offset += 4;
674     }
675
676   subtvb = tvb_new_subset(tvb, offset, -1, -1);
677   aiminfo.tcpinfo = pinfo->private_data;
678   aiminfo.family = family;
679   aiminfo.subtype = subtype;
680   pinfo->private_data = &aiminfo;
681   
682   if (check_col(pinfo->cinfo, COL_INFO)) {
683      if(fam_name) col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", fam_name);
684          else col_append_fstr(pinfo->cinfo, COL_INFO, ", Family: 0x%04x", family);
685          if(subtype_name) col_append_fstr(pinfo->cinfo, COL_INFO, ", %s", subtype_name);
686          else col_append_fstr(pinfo->cinfo, COL_INFO, ", Subtype: 0x%04x", subtype);
687   }
688
689   if(tvb_length_remaining(tvb, offset) == 0 || !dissector_try_port(subdissector_table, family, subtvb, pinfo, root_tree)) {
690     /* Show the undissected payload */
691     if (tvb_length_remaining(tvb, offset) > 0)
692       proto_tree_add_item(aim_tree, hf_aim_data, tvb, offset, -1, FALSE);
693   }
694 }
695
696 static void dissect_aim_flap_err(tvbuff_t *tvb, packet_info *pinfo, 
697                                  int offset, proto_tree *tree)
698 {
699   if (check_col(pinfo->cinfo, COL_INFO)) {
700     col_add_fstr(pinfo->cinfo, COL_INFO, "FLAP error");
701   }
702
703   /* Show the undissected payload */
704   if (tvb_length_remaining(tvb, offset) > 0)
705     proto_tree_add_item(tree, hf_aim_data, tvb, offset, -1, FALSE);
706 }
707
708 static void dissect_aim_keep_alive(tvbuff_t *tvb, packet_info *pinfo, 
709                                    int offset, proto_tree *tree)
710 {
711   if (check_col(pinfo->cinfo, COL_INFO)) {
712     col_add_fstr(pinfo->cinfo, COL_INFO, "Keep Alive");
713   }
714
715   /* Show the undissected payload */
716   if (tvb_length_remaining(tvb, offset) > 0)
717     proto_tree_add_item(tree, hf_aim_data, tvb, offset, -1, FALSE);
718 }
719
720 static void dissect_aim_close_conn(tvbuff_t *tvb, packet_info *pinfo, 
721                                    int offset, proto_tree *tree)
722 {
723   if (check_col(pinfo->cinfo, COL_INFO)) {
724     col_add_fstr(pinfo->cinfo, COL_INFO, "Close Connection");
725   }       
726   
727   while(tvb_reported_length_remaining(tvb, offset) > 0) {
728           offset = dissect_aim_tlv(tvb, pinfo, offset, tree, client_tlvs);
729   }
730 }
731
732 static void dissect_aim_unknown_channel(tvbuff_t *tvb, packet_info *pinfo, 
733                                         int offset, proto_tree *tree)
734 {
735   if (check_col(pinfo->cinfo, COL_INFO)) {
736     col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown Channel");
737   }
738
739   /* Show the undissected payload */
740   if (tvb_length_remaining(tvb, offset) > 0)
741     proto_tree_add_item(tree, hf_aim_data, tvb, offset, -1, FALSE);
742 }
743
744 int dissect_aim_buddyname(tvbuff_t *tvb, packet_info *pinfo _U_, int offset,
745                        proto_tree *tree)
746 {
747   guint8 buddyname_length = 0;
748   char *buddyname;
749   proto_item *ti = NULL;
750   proto_tree *buddy_tree = NULL;
751
752   buddyname_length = tvb_get_guint8(tvb, offset);
753   offset++;
754   buddyname = tvb_get_string(tvb, offset, buddyname_length);
755
756   if(tree) {
757       ti = proto_tree_add_text(tree, tvb, offset-1, 1+buddyname_length,
758                                "Buddy: %s",
759                                format_text(buddyname, buddyname_length));
760       buddy_tree = proto_item_add_subtree(ti, ett_aim_buddyname);
761   }
762
763    proto_tree_add_item(buddy_tree, hf_aim_buddyname_len, tvb, offset-1, 1, FALSE);
764    proto_tree_add_item(buddy_tree, hf_aim_buddyname, tvb, offset, buddyname_length, FALSE);
765    return offset+buddyname_length;
766 }
767
768
769 int dissect_aim_tlv_value_client_capabilities(proto_item *ti _U_, guint16 valueid _U_, tvbuff_t *tvb)
770 {
771         /* FIXME */
772         return tvb_length(tvb);
773 }
774
775 int dissect_aim_tlv_value_time(proto_item *ti _U_, guint16 valueid _U_, tvbuff_t *tvb)
776 {
777         /* FIXME */
778         return tvb_length(tvb);
779 }
780
781 int dissect_aim_userclass(tvbuff_t *tvb, int offset, int len, proto_item *ti, guint32 flags)
782 {
783         proto_tree *entry;
784
785         entry = proto_item_add_subtree(ti, ett_aim_userclass);
786         proto_tree_add_boolean(entry, hf_aim_userclass_unconfirmed, tvb, offset, len, flags);
787         proto_tree_add_boolean(entry, hf_aim_userclass_administrator, tvb, offset, len, flags);
788         proto_tree_add_boolean(entry, hf_aim_userclass_aol, tvb, offset, len, flags);
789         proto_tree_add_boolean(entry, hf_aim_userclass_commercial, tvb, offset, len, flags);
790         proto_tree_add_boolean(entry, hf_aim_userclass_free, tvb, offset, len, flags);
791         proto_tree_add_boolean(entry, hf_aim_userclass_away, tvb, offset, len, flags);
792         proto_tree_add_boolean(entry, hf_aim_userclass_icq, tvb, offset, len, flags);
793         proto_tree_add_boolean(entry, hf_aim_userclass_wireless, tvb, offset, len, flags);
794         proto_tree_add_boolean(entry, hf_aim_userclass_unknown100, tvb, offset, len, flags);
795         proto_tree_add_boolean(entry, hf_aim_userclass_unknown200, tvb, offset, len, flags);
796         proto_tree_add_boolean(entry, hf_aim_userclass_unknown400, tvb, offset, len, flags);
797         proto_tree_add_boolean(entry, hf_aim_userclass_unknown800, tvb, offset, len, flags);
798         return offset+len;
799 }
800
801 int dissect_aim_tlv_value_userclass(proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb)
802 {
803         guint16 value16 = tvb_get_ntohs(tvb, 0);
804         proto_item_set_text(ti, "Value: 0x%04x", value16);
805         return dissect_aim_userclass(tvb, 0, 2, ti, value16);
806 }
807
808 static int dissect_aim_tlv_value_userstatus(proto_item *ti _U_, guint16 valueid _U_, tvbuff_t *tvb)
809 {
810         /* FIXME */
811         return tvb_length(tvb);
812 }
813
814 static int dissect_aim_tlv_value_dcinfo(proto_item *ti _U_, guint16 valueid _U_, tvbuff_t *tvb)
815 {
816         /* FIXME */
817         return tvb_length(tvb);
818 }
819
820 int dissect_aim_tlv_value_string (proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb)
821 {
822    guint8 *buf;
823    gint string_len;
824
825    string_len = tvb_length(tvb);
826    buf = tvb_get_string(tvb, 0, string_len);
827    proto_item_set_text(ti, "Value: %s", format_text(buf, string_len));
828    g_free(buf);
829    return string_len;
830 }
831
832 int dissect_aim_tlv_value_bytes (proto_item *ti _U_, guint16 valueid _U_, tvbuff_t *tvb _U_)
833 {
834    return tvb_length(tvb);
835 }
836
837 int dissect_aim_tlv_value_uint8 (proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb){
838   guint8 value8 = tvb_get_guint8(tvb, 0);
839   proto_item_set_text(ti, "Value: %d", value8);
840   return 1;
841 }
842
843 int dissect_aim_tlv_value_uint16 (proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb){
844   guint16 value16 = tvb_get_ntohs(tvb, 0);
845   proto_item_set_text(ti, "Value: %d", value16);
846   return 2;
847 }
848
849 int dissect_aim_tlv_value_ipv4 (proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb){
850   /* FIXME: Somewhat more readable format ? */
851   guint32 value32 = tvb_get_ntoh24(tvb, 0);
852   proto_item_set_text(ti, "Value: %d", value32);
853   return 4;
854 }
855
856 int dissect_aim_tlv_value_uint32 (proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb){
857   guint32 value32 = tvb_get_ntoh24(tvb, 0);
858   proto_item_set_text(ti, "Value: %d", value32);
859   return 4;
860 }
861
862 int dissect_aim_tlv_value_messageblock (proto_item *ti, guint16 valueid _U_, tvbuff_t *tvb){
863   proto_tree *entry;
864   guint8 *buf;
865   guint16 featurelen;
866   guint16 blocklen;
867   int offset=0;
868
869   /* Setup a new subtree */
870   entry = proto_item_add_subtree(ti, ett_aim_messageblock);
871
872   /* Features descriptor */
873   proto_tree_add_item(entry, hf_aim_messageblock_featuresdes, tvb, offset, 2,
874                       FALSE);
875   offset += 2;
876
877   /* Features Length */
878   featurelen = tvb_get_ntohs(tvb, offset);
879   proto_tree_add_item(entry, hf_aim_messageblock_featureslen, tvb, offset, 2,
880                       FALSE);
881   offset += 2;
882
883   /* Features (should be expanded further @@@@@@@ ) */
884   proto_tree_add_item(entry, hf_aim_messageblock_features, tvb, offset, 
885                       featurelen, FALSE);
886   offset += featurelen;
887
888   /* There can be multiple messages in this message block */
889   while (tvb_length_remaining(tvb, offset) > 0) {
890     /* Info field */
891     proto_tree_add_item(entry, hf_aim_messageblock_info, tvb, offset, 2,
892                         FALSE);
893     offset += 2;
894     
895     /* Block length (includes charset and charsubset) */
896     blocklen = tvb_get_ntohs(tvb, offset);
897     proto_tree_add_item(entry, hf_aim_messageblock_len, tvb, offset, 2,
898                         FALSE);
899     offset += 2;
900     
901     /* Character set */
902     proto_tree_add_item(entry, hf_aim_messageblock_charset, tvb, offset, 2,
903                         FALSE);
904     offset += 2;
905     
906     /* Character subset */
907     proto_tree_add_item(entry, hf_aim_messageblock_charsubset, tvb, offset, 2,
908                         FALSE);
909     offset += 2;
910
911     /* The actual message */
912     buf = tvb_get_string(tvb, offset, blocklen - 4 );
913     proto_item_set_text(ti, "Message: %s",
914                         format_text(buf, blocklen - 4));
915     proto_tree_add_item(entry, hf_aim_messageblock_message, tvb, offset, 
916                         blocklen-4,
917                         FALSE);
918     offset += tvb_length_remaining(tvb, offset);
919     g_free(buf);
920   }
921
922   return offset;
923 }
924
925 /* Dissect a TLV value */
926 int dissect_aim_tlv(tvbuff_t *tvb, packet_info *pinfo _U_, 
927                            int offset, proto_tree *tree, const aim_tlv *tlv)
928 {
929   guint16 valueid;
930   guint16 length;
931   int i = 0;
932   const aim_tlv *tmp;
933   proto_item *ti1;
934   proto_tree *tlv_tree;
935   int orig_offset;
936
937   /* Record the starting offset so we can reuse it at the second pass */
938   orig_offset = offset;
939
940   /* Get the value ID */
941   valueid = tvb_get_ntohs(tvb, offset);
942   offset += 2;
943
944   /* Figure out which entry applies from the tlv list */
945   tmp = tlv;
946   while (tmp[i].valueid) {
947     if (tmp[i].valueid == valueid) {
948       /* We found a match */
949       break;
950     }
951     i++;
952   }
953
954   /* At this point, we are either pointing at the correct record, or 
955      we didn't find the record, and are pointing at the last item in the 
956      list */
957
958   length = tvb_get_ntohs(tvb, offset);
959   offset += 2;
960   offset += length;
961
962   if (tree) {
963     offset = orig_offset;
964     
965     ti1 = proto_tree_add_text(tree, tvb, offset, length + 4, "TLV: %s", tmp[i].desc);
966
967     tlv_tree = proto_item_add_subtree(ti1, ett_aim_tlv);
968
969     proto_tree_add_text(tlv_tree, tvb, offset, 2,
970                         "Value ID: %s (0x%04x)", tmp[i].desc, valueid);
971     offset += 2;
972     
973     proto_tree_add_text(tlv_tree, tvb, offset, 2,
974                         "Length: %d", length);
975     offset += 2;
976
977     ti1 = proto_tree_add_text(tlv_tree, tvb, offset, length,
978                               "Value");
979         
980     if (tmp[i].dissector) {
981       tmp[i].dissector(ti1, valueid, tvb_new_subset(tvb, offset, length, length));
982     } 
983
984     offset += length;
985   }
986
987   /* Return the new length */
988   return offset;
989 }
990
991 int dissect_aim_tlv_list(tvbuff_t *tvb, packet_info *pinfo _U_, 
992                            int offset, proto_tree *tree, const aim_tlv *tlv_table)
993 {
994     guint16 i, tlv_count = tvb_get_ntohs(tvb, offset);
995
996     proto_tree_add_item(tree, hf_aim_tlvcount, tvb, offset, 2, FALSE);
997     offset += 2;
998
999     for(i = 0; i < tlv_count; i++) {
1000       offset = dissect_aim_tlv(tvb, pinfo, offset, tree, tlv_table);
1001     }
1002     return offset;
1003 }
1004
1005 /* Register the protocol with Ethereal */
1006 void
1007 proto_register_aim(void)
1008 {
1009
1010 /* Setup list of header fields */
1011   static hf_register_info hf[] = {
1012     { &hf_aim_cmd_start,
1013       { "Command Start", "aim.cmd_start", FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL }
1014     },
1015     { &hf_aim_channel,
1016       { "Channel ID", "aim.channel", FT_UINT8, BASE_HEX, VALS(aim_flap_channels), 0x0, "", HFILL }
1017     },
1018     { &hf_aim_seqno,
1019       { "Sequence Number", "aim.seqno", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }
1020     },
1021         { &hf_aim_authcookie,
1022           { "Authentication Cookie", "aim.authcookie", FT_BYTES, BASE_DEC, NULL, 0x0, "", HFILL },
1023         },
1024     { &hf_aim_data_len,
1025       { "Data Field Length", "aim.datalen", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }
1026     },
1027     { &hf_aim_data,
1028       { "Data", "aim.data", FT_BYTES, BASE_HEX, NULL, 0x0, "", HFILL }
1029     },
1030     { &hf_aim_signon_challenge_len,
1031       { "Signon challenge length", "aim.signon.challengelen", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL }
1032     },
1033     { &hf_aim_signon_challenge,
1034       { "Signon challenge", "aim.signon.challenge", FT_STRING, BASE_HEX, NULL, 0x0, "", HFILL }
1035     },
1036     { &hf_aim_fnac_family,
1037       { "FNAC Family ID", "aim.fnac.family", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL }
1038     },
1039     { &hf_aim_fnac_subtype,
1040       { "FNAC Subtype ID", "aim.fnac.subtype", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL }
1041     },
1042     { &hf_aim_fnac_flags,
1043       { "FNAC Flags", "aim.fnac.flags", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL }
1044     },
1045     { &hf_aim_fnac_id,
1046       { "FNAC ID", "aim.fnac.id", FT_UINT32, BASE_HEX, NULL, 0x0, "", HFILL }
1047     },
1048     { &hf_aim_infotype,
1049       { "Infotype", "aim.infotype", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL }
1050     },
1051     { &hf_aim_buddyname_len,
1052       { "Buddyname len", "aim.buddynamelen", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
1053     },
1054     { &hf_aim_buddyname,
1055       { "Buddy Name", "aim.buddyname", FT_STRING, BASE_NONE, NULL, 0x0, "", HFILL }
1056     },
1057     { &hf_aim_tlvcount,
1058       { "TLV Count", "aim.tlvcount", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL },
1059     },
1060         { &hf_aim_snac_error,
1061           { "SNAC Error", "aim.snac.error", FT_UINT16,
1062                   BASE_HEX, VALS(aim_snac_errors), 0x0, "", HFILL },
1063         },
1064         { &hf_aim_userclass_unconfirmed,
1065                 { "AOL Unconfirmed user flag", "aim.userclass.unconfirmed", FT_BOOLEAN, 32, TFS(&flags_set_truth), CLASS_UNCONFIRMED, "", HFILL },
1066         },
1067         { &hf_aim_userclass_administrator,
1068                 { "AOL Administrator flag", "aim.userclass.administrator", FT_BOOLEAN, 32, TFS(&flags_set_truth), CLASS_ADMINISTRATOR, "", HFILL },
1069         },
1070         { &hf_aim_userclass_aol,
1071                 { "AOL Staff User Flag", "aim.userclass.staff", FT_BOOLEAN, 32, TFS(&flags_set_truth), CLASS_AOL, "", HFILL },
1072         },
1073         { &hf_aim_userclass_commercial,
1074                 { "AOL commercial account flag", "aim.userclass.commercial", FT_BOOLEAN, 32, TFS(&flags_set_truth), CLASS_COMMERCIAL, "", HFILL },
1075         },
1076         { &hf_aim_userclass_free,
1077                 { "ICQ non-commercial account flag", "aim.userclass.noncommercial", FT_BOOLEAN, 32, TFS(&flags_set_truth), CLASS_FREE, "", HFILL },
1078         },
1079         { &hf_aim_userclass_away,
1080                 { "AOL away status flag", "aim.userclass.away", FT_BOOLEAN, 32, TFS(&flags_set_truth), CLASS_AWAY, "", HFILL },
1081         },
1082         { &hf_aim_userclass_icq,
1083                 { "ICQ user sign", "aim.userclass.icq", FT_BOOLEAN, 32, TFS(&flags_set_truth), CLASS_ICQ, "", HFILL },
1084         },
1085         { &hf_aim_userclass_wireless,
1086                 { "AOL wireless user", "aim.userclass.wireless", FT_BOOLEAN, 32, TFS(&flags_set_truth), CLASS_WIRELESS, "", HFILL },
1087         },
1088         { &hf_aim_userclass_unknown100,
1089                 { "Unknown bit", "aim.userclass.unknown100", FT_BOOLEAN, 32, TFS(&flags_set_truth), CLASS_UNKNOWN100, "", HFILL },
1090         },
1091         { &hf_aim_userclass_unknown200,
1092                 { "Unknown bit", "aim.userclass.unknown200", FT_BOOLEAN, 32, TFS(&flags_set_truth), CLASS_UNKNOWN200, "", HFILL },
1093         },
1094         { &hf_aim_userclass_unknown400,
1095                 { "Unknown bit", "aim.userclass.unknown400", FT_BOOLEAN, 32, TFS(&flags_set_truth), CLASS_UNKNOWN400, "", HFILL },
1096         },
1097         { &hf_aim_userclass_unknown800,
1098                 { "Unknown bit", "aim.userclass.unknown800", FT_BOOLEAN, 32, TFS(&flags_set_truth), CLASS_UNKNOWN800, "", HFILL },
1099         },
1100         { &hf_aim_userinfo_warninglevel,
1101                 { "Warning Level", "aim.userinfo.warninglevel", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL },
1102         },
1103     { &hf_aim_messageblock_featuresdes,
1104                 { "Features", "aim.messageblock.featuresdes", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL },
1105         },
1106     { &hf_aim_messageblock_featureslen,
1107                 { "Features Length", "aim.messageblock.featureslen", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL },
1108         },
1109     { &hf_aim_messageblock_features,
1110                 { "Features", "aim.messageblock.features", FT_BYTES, BASE_HEX, NULL, 0x0, "", HFILL },
1111         },
1112     { &hf_aim_messageblock_info,
1113                 { "Block info", "aim.messageblock.info", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL },
1114         },
1115     { &hf_aim_messageblock_len,
1116                 { "Block length", "aim.messageblock.length", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL },
1117         },
1118     { &hf_aim_messageblock_charset,
1119                 { "Block Character set", "aim.messageblock.charset", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL },
1120         },
1121     { &hf_aim_messageblock_charsubset,
1122                 { "Block Character subset", "aim.messageblock.charsubset", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL },
1123         },
1124     { &hf_aim_messageblock_message,
1125                 { "Message", "aim.messageblock.message", FT_STRING, BASE_NONE, NULL, 0x0, "", HFILL },
1126         },
1127   };
1128
1129   /* Setup protocol subtree array */
1130   static gint *ett[] = {
1131           &ett_aim,
1132           &ett_aim_fnac,
1133           &ett_aim_tlv,
1134           &ett_aim_buddyname,
1135           &ett_aim_userclass,
1136           &ett_aim_messageblock
1137   };
1138   module_t *aim_module;
1139
1140   /* Register the protocol name and description */
1141   proto_aim = proto_register_protocol("AOL Instant Messenger", "AIM", "aim");
1142
1143 /* Required function calls to register the header fields and subtrees used */
1144   proto_register_field_array(proto_aim, hf, array_length(hf));
1145   proto_register_subtree_array(ett, array_length(ett));
1146
1147   aim_module = prefs_register_protocol(proto_aim, NULL);
1148   prefs_register_bool_preference(aim_module, "desegment",
1149     "Reassemble AIM messages spanning multiple TCP segments",
1150     "Whether the AIM dissector should reassemble messages spanning multiple TCP segments."
1151     " To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
1152     &aim_desegment);
1153
1154   subdissector_table = register_dissector_table("aim.family", 
1155                 "Family ID", FT_UINT16, BASE_HEX);
1156 }
1157
1158 void
1159 proto_reg_handoff_aim(void)
1160 {
1161   dissector_handle_t aim_handle;
1162
1163   aim_handle = new_create_dissector_handle(dissect_aim, proto_aim);
1164   dissector_add("tcp.port", TCP_PORT_AIM, aim_handle);
1165 }