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