From Jelmer Vernooij:
[obnox/wireshark/wip.git] / epan / dissectors / packet-aim-ssi.c
1 /* packet-aim-ssi.c
2  * Routines for AIM Instant Messenger (OSCAR) dissection, SNAC SSI
3  * Copyright 2004, Jelmer Vernooij <jelmer@samba.org>
4  * Copyright 2000, Ralf Hoelzer <ralf@well.com>
5  *
6  * $Id$
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35
36 #include <glib.h>
37
38 #include <epan/packet.h>
39 #include <epan/strutil.h>
40
41 #include "packet-aim.h"
42
43 #define FAMILY_SSI        0x0013
44
45 /* Family Server-Stored Buddy Lists */
46 #define FAMILY_SSI_ERROR              0x0001
47 #define FAMILY_SSI_REQRIGHTS          0x0002
48 #define FAMILY_SSI_RIGHTSINFO         0x0003
49 #define FAMILY_SSI_REQLIST_FIRSTTIME  0x0004
50 #define FAMILY_SSI_REQLIST            0x0005
51 #define FAMILY_SSI_LIST               0x0006
52 #define FAMILY_SSI_ACTIVATE           0x0007
53 #define FAMILY_SSI_ADD                0x0008
54 #define FAMILY_SSI_MOD                0x0009
55 #define FAMILY_SSI_DEL                0x000a
56 #define FAMILY_SSI_SRVACK             0x000e
57 #define FAMILY_SSI_NOLIST             0x000f
58 #define FAMILY_SSI_EDITSTART          0x0011
59 #define FAMILY_SSI_EDITSTOP           0x0012
60 #define FAMILY_SSI_GRANT_FUTURE_AUTH  0x0014
61 #define FAMILY_SSI_FUTUR_AUTH_GRANTED 0x0015
62 #define FAMILY_SSI_SEND_AUTH_REQ      0x0018
63 #define FAMILY_SSI_AUTH_REQ           0x0019
64 #define FAMILY_SSI_SEND_AUTH_REPLY    0x001a
65 #define FAMILY_SSI_AUTH_REPLY         0x001b
66 #define FAMILY_SSI_WAS_ADDED          0x001c
67
68 static const value_string aim_fnac_family_ssi[] = {
69   { FAMILY_SSI_ERROR, "Error" },
70   { FAMILY_SSI_REQRIGHTS, "Request Rights" },
71   { FAMILY_SSI_RIGHTSINFO, "Rights Info" },
72   { FAMILY_SSI_REQLIST_FIRSTTIME, "Request List (first time)" },
73   { FAMILY_SSI_REQLIST, "Request List" },
74   { FAMILY_SSI_LIST, "List" },
75   { FAMILY_SSI_ACTIVATE, "Activate" },
76   { FAMILY_SSI_ADD, "Add Buddy" },
77   { FAMILY_SSI_MOD, "Modify Buddy" },
78   { FAMILY_SSI_DEL, "Delete Buddy" },
79   { FAMILY_SSI_SRVACK, "Server Ack" },
80   { FAMILY_SSI_NOLIST, "No List" },
81   { FAMILY_SSI_EDITSTART, "Edit Start" },
82   { FAMILY_SSI_EDITSTOP, "Edit Stop" },
83   { FAMILY_SSI_GRANT_FUTURE_AUTH, "Grant Future Authorization to Client" },
84   { FAMILY_SSI_FUTUR_AUTH_GRANTED, "Future Authorization Granted" },
85   { FAMILY_SSI_SEND_AUTH_REQ, "Send Authentication Request" },
86   { FAMILY_SSI_AUTH_REQ, "Authentication Request" },
87   { FAMILY_SSI_SEND_AUTH_REPLY, "Send Authentication Reply" },
88   { FAMILY_SSI_AUTH_REPLY, "Authentication Reply" },
89   { FAMILY_SSI_WAS_ADDED, "Remote User Added Client To List" },
90   { 0, NULL }
91 };
92
93 #define FAMILY_SSI_TYPE_BUDDY         0x0000
94 #define FAMILY_SSI_TYPE_GROUP         0x0001
95 #define FAMILY_SSI_TYPE_PERMIT        0x0002
96 #define FAMILY_SSI_TYPE_DENY          0x0003
97 #define FAMILY_SSI_TYPE_PDINFO        0x0004
98 #define FAMILY_SSI_TYPE_PRESENCEPREFS 0x0005
99 #define FAMILY_SSI_TYPE_ICONINFO      0x0014
100
101 static const value_string aim_fnac_family_ssi_types[] = {
102   { FAMILY_SSI_TYPE_BUDDY, "Buddy" },
103   { FAMILY_SSI_TYPE_GROUP, "Group" },
104   { FAMILY_SSI_TYPE_PERMIT, "Permit" },
105   { FAMILY_SSI_TYPE_DENY, "Deny" },
106   { FAMILY_SSI_TYPE_PDINFO, "PDINFO" },
107   { FAMILY_SSI_TYPE_PRESENCEPREFS, "Presence Preferences" },
108   { FAMILY_SSI_TYPE_ICONINFO, "Icon Info" },
109   { 0, NULL }
110 };
111
112 #define SSI_RIGHTSINFO_TLV_MAX_ITEMS    0x0004
113
114 static const aim_tlv ssi_rightsinfo_tlvs[] = {
115         { SSI_RIGHTSINFO_TLV_MAX_ITEMS, "Maximums For Items", dissect_aim_tlv_value_bytes }, 
116         { 0, NULL, NULL },
117 };
118
119 static int dissect_aim_snac_ssi_list(tvbuff_t *tvb, packet_info *pinfo _U_, 
120                                       int offset, proto_tree *tree, guint16 subtype _U_);
121
122 /* Initialize the protocol and registered fields */
123 static int proto_aim_ssi = -1;
124 static int hf_aim_fnac_subtype_ssi_version = -1;
125 static int hf_aim_fnac_subtype_ssi_numitems = -1;
126 static int hf_aim_fnac_subtype_ssi_last_change_time = -1;
127 static int hf_aim_fnac_subtype_ssi_buddyname_len = -1;
128 static int hf_aim_fnac_subtype_ssi_buddyname = -1;
129 static int hf_aim_fnac_subtype_ssi_gid = -1;
130 static int hf_aim_fnac_subtype_ssi_bid = -1;
131 static int hf_aim_fnac_subtype_ssi_type = -1;
132 static int hf_aim_fnac_subtype_ssi_tlvlen = -1;
133 static int hf_aim_fnac_subtype_ssi_data = -1;
134
135 /* Initialize the subtree pointers */
136 static gint ett_aim_ssi      = -1;
137 static gint ett_ssi      = -1;
138
139 static int dissect_ssi_item(tvbuff_t *tvb, packet_info *pinfo _U_, 
140                                       int offset, proto_tree *ssi_entry, 
141                                       guint16 subtype _U_)
142 {
143   guint16 buddyname_length = 0;
144   int endoffset;
145   guint16 tlv_len = 0;
146          /* Buddy Name Length */
147     buddyname_length = tvb_get_ntohs(tvb, offset);
148     proto_tree_add_item(ssi_entry, hf_aim_fnac_subtype_ssi_buddyname_len, 
149                         tvb, offset, 2, FALSE);
150     offset += 2;
151     
152     /* Buddy Name */
153     if (buddyname_length > 0) {
154       proto_tree_add_item(ssi_entry, hf_aim_fnac_subtype_ssi_buddyname, tvb, 
155                           offset, buddyname_length, FALSE);
156       offset += buddyname_length;
157     }
158     
159     /* Buddy group ID */
160     proto_tree_add_item(ssi_entry, hf_aim_fnac_subtype_ssi_gid, tvb, offset, 
161                         2, FALSE);
162     offset += 2;
163     
164     /* Buddy ID */
165     proto_tree_add_item(ssi_entry, hf_aim_fnac_subtype_ssi_bid, tvb, offset, 
166                         2, FALSE);
167     offset += 2;
168     
169     /* Buddy Type */
170     proto_tree_add_item(ssi_entry, hf_aim_fnac_subtype_ssi_type, tvb, offset,
171                         2, FALSE);
172     offset += 2;
173     
174     /* Size of the following TLV in bytes (as opposed to the number of 
175        TLV objects in the chain) */
176     tlv_len = tvb_get_ntohs(tvb, offset);
177     proto_tree_add_item(ssi_entry, hf_aim_fnac_subtype_ssi_tlvlen, tvb, 
178                         offset, 2, FALSE);
179     offset += 2;
180     
181         endoffset = offset;
182     /* For now, we just dump the TLV contents as-is, since there is not a
183        TLV dissection utility that works based on total chain length */
184         while(endoffset < offset+tlv_len) {
185         endoffset = dissect_aim_tlv(tvb, pinfo, endoffset, ssi_entry, client_tlvs);
186     }
187         return endoffset;
188 }
189
190 static int dissect_aim_snac_ssi(tvbuff_t *tvb, packet_info *pinfo _U_, 
191                                  proto_tree *tree)
192 {
193         struct aiminfo *aiminfo = pinfo->private_data;
194         int offset = 0;
195     proto_item *ti = NULL;
196     proto_tree *ssi_tree = NULL;
197                                                                                 
198     if(tree) {
199                 ti = proto_tree_add_text(tree, tvb, 0, -1,"AIM Server Side Information Service");
200         ssi_tree = proto_item_add_subtree(ti, ett_ssi);
201     }
202
203   switch(aiminfo->subtype)
204     {    
205         case FAMILY_SSI_ERROR:
206       return dissect_aim_snac_error(tvb, pinfo, offset, ssi_tree);
207     case FAMILY_SSI_LIST:
208           return dissect_aim_snac_ssi_list(tvb, pinfo, offset, ssi_tree, aiminfo->subtype);
209         case FAMILY_SSI_RIGHTSINFO:
210                 while(tvb_length_remaining(tvb, offset) > 0) {
211                         offset = dissect_aim_tlv(tvb, pinfo, offset, ssi_tree, ssi_rightsinfo_tlvs);
212                 }
213           return offset;
214         case FAMILY_SSI_REQRIGHTS:
215         case FAMILY_SSI_ACTIVATE:
216         case FAMILY_SSI_EDITSTART:
217         case FAMILY_SSI_EDITSTOP:
218         case FAMILY_SSI_REQLIST_FIRSTTIME:
219           /* No data */
220           return 0;
221         case FAMILY_SSI_ADD:
222         case FAMILY_SSI_MOD:
223         case FAMILY_SSI_DEL:
224       return dissect_ssi_item(tvb, pinfo, offset, ssi_tree, aiminfo->subtype);
225         case FAMILY_SSI_WAS_ADDED:
226           return dissect_aim_buddyname(tvb, pinfo, offset, ssi_tree);
227         case FAMILY_SSI_REQLIST:
228         case FAMILY_SSI_SRVACK:
229         case FAMILY_SSI_NOLIST:
230         case FAMILY_SSI_GRANT_FUTURE_AUTH:
231         case FAMILY_SSI_FUTUR_AUTH_GRANTED:
232         case FAMILY_SSI_SEND_AUTH_REQ: 
233         case FAMILY_SSI_AUTH_REQ:
234         case FAMILY_SSI_SEND_AUTH_REPLY:
235         case FAMILY_SSI_AUTH_REPLY:
236                 
237           /* FIXME */
238           return 0;
239          
240     default:
241           return 0;
242     }
243 }
244
245 static int dissect_aim_snac_ssi_list(tvbuff_t *tvb, packet_info *pinfo _U_, 
246                                       int offset, proto_tree *tree, 
247                                       guint16 subtype)
248 {
249   proto_item *ti;
250   proto_tree *ssi_entry = NULL;
251   guint16 num_items, i;
252
253   /* SSI Version */
254   proto_tree_add_item(tree, hf_aim_fnac_subtype_ssi_version, tvb, offset, 1,
255                       FALSE);
256   offset += 1;
257   
258   /* Number of items */
259   proto_tree_add_item(tree, hf_aim_fnac_subtype_ssi_numitems, tvb, offset, 2,
260                       FALSE);
261   num_items = tvb_get_ntohs(tvb, offset);
262   offset += 2;
263   
264   for(i = 0; i < num_items; i++) {
265     ti = proto_tree_add_text(tree, tvb, offset, tvb_get_ntohs(tvb, offset+10)+10, "SSI Entry");
266     ssi_entry = proto_item_add_subtree(ti, ett_aim_ssi);
267     offset = dissect_ssi_item(tvb, pinfo, offset, ssi_entry, subtype);
268      }
269   proto_tree_add_item(tree, hf_aim_fnac_subtype_ssi_last_change_time, tvb, offset, 4, FALSE);
270   return offset;
271 }
272
273 /* Register the protocol with Ethereal */
274 void
275 proto_register_aim_ssi(void)
276 {
277
278 /* Setup list of header fields */
279   static hf_register_info hf[] = {
280     { &hf_aim_fnac_subtype_ssi_version,
281       { "SSI Version", "aim.fnac.ssi.version", FT_UINT8, BASE_HEX, NULL, 0x0, "", HFILL }
282     },
283     { &hf_aim_fnac_subtype_ssi_numitems,
284       { "SSI Object count", "aim.fnac.ssi.numitems", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL }
285     },
286     { &hf_aim_fnac_subtype_ssi_last_change_time,
287       { "SSI Last Change Time", "aim.fnac.ssi.last_change_time", FT_UINT32, BASE_HEX, NULL, 0x0, "", HFILL }
288     },
289     { &hf_aim_fnac_subtype_ssi_buddyname_len,
290       { "SSI Buddy Name length", "aim.fnac.ssi.buddyname_len", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL }
291     },
292     { &hf_aim_fnac_subtype_ssi_buddyname,
293       { "Buddy Name", "aim.fnac.ssi.buddyname", FT_STRING, BASE_NONE, NULL, 0x0, "", HFILL }
294     },
295     { &hf_aim_fnac_subtype_ssi_gid,
296       { "SSI Buddy Group ID", "aim.fnac.ssi.gid", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL }
297     },
298     { &hf_aim_fnac_subtype_ssi_bid,
299       { "SSI Buddy ID", "aim.fnac.ssi.bid", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL }
300     },
301     { &hf_aim_fnac_subtype_ssi_type,
302       { "SSI Buddy type", "aim.fnac.ssi.type", FT_UINT16, BASE_HEX, VALS(aim_fnac_family_ssi_types), 0x0, "", HFILL }
303     },
304     { &hf_aim_fnac_subtype_ssi_tlvlen,
305       { "SSI TLV Len", "aim.fnac.ssi.tlvlen", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL }
306     },
307     { &hf_aim_fnac_subtype_ssi_data,
308       { "SSI Buddy Data", "aim.fnac.ssi.data", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL }
309     },
310   };
311
312 /* Setup protocol subtree array */
313   static gint *ett[] = {
314     &ett_aim_ssi,
315         &ett_ssi,
316   };
317
318 /* Register the protocol name and description */
319   proto_aim_ssi = proto_register_protocol("AIM Server Side Info", "AIM SSI", "aim_ssi");
320
321 /* Required function calls to register the header fields and subtrees used */
322   proto_register_field_array(proto_aim_ssi, hf, array_length(hf));
323   proto_register_subtree_array(ett, array_length(ett));
324 }
325
326 void
327 proto_reg_handoff_aim_ssi(void)
328 {
329   dissector_handle_t aim_handle;
330
331   aim_handle = new_create_dissector_handle(dissect_aim_snac_ssi, proto_aim_ssi);
332   dissector_add("aim.family", FAMILY_SSI, aim_handle);
333   aim_init_family(FAMILY_SSI, "SSI", aim_fnac_family_ssi);
334 }