bugfix to a bug reported by Ian Schorr:
[obnox/wireshark/wip.git] / packet-aim-buddylist.c
1 /* packet-aim-buddylist.c
2  * Routines for AIM Instant Messenger (OSCAR) dissection, SNAC Buddylist
3  * Copyright 2004, Jelmer Vernooij <jelmer@samba.org>
4  *
5  * $Id: packet-aim-buddylist.c,v 1.4 2004/06/16 07:51:21 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <ctype.h>
34
35 #include <glib.h>
36
37 #include <epan/packet.h>
38 #include <epan/strutil.h>
39
40 #include "packet-aim.h"
41 #include "prefs.h"
42
43 #define FAMILY_BUDDYLIST  0x0003
44
45 /* Family Buddy List */
46 #define FAMILY_BUDDYLIST_ERROR        0x0001
47 #define FAMILY_BUDDYLIST_REQRIGHTS    0x0002
48 #define FAMILY_BUDDYLIST_RIGHTSINFO   0x0003
49 #define FAMILY_BUDDYLIST_ADDBUDDY     0x0004
50 #define FAMILY_BUDDYLIST_REMBUDDY     0x0005
51 #define FAMILY_BUDDYLIST_WATCHERS_REQ 0x0006
52 #define FAMILY_BUDDYLIST_WATCHERS_REP 0x0007
53 #define FAMILY_BUDDYLIST_REJECT       0x000a
54 #define FAMILY_BUDDYLIST_ONCOMING     0x000b
55 #define FAMILY_BUDDYLIST_OFFGOING     0x000c
56 #define FAMILY_BUDDYLIST_DEFAULT      0xffff
57
58 static const value_string aim_fnac_family_buddylist[] = {
59   { FAMILY_BUDDYLIST_ERROR, "Error" },
60   { FAMILY_BUDDYLIST_REQRIGHTS, "Request Rights" },
61   { FAMILY_BUDDYLIST_RIGHTSINFO, "Rights Info" },
62   { FAMILY_BUDDYLIST_ADDBUDDY, "Add Buddy" },
63   { FAMILY_BUDDYLIST_REMBUDDY, "Remove Buddy" },
64   { FAMILY_BUDDYLIST_REJECT, "Reject Buddy" }, 
65   { FAMILY_BUDDYLIST_ONCOMING, "Oncoming Buddy" },
66   { FAMILY_BUDDYLIST_OFFGOING, "Offgoing Buddy" },
67   { FAMILY_BUDDYLIST_DEFAULT, "Buddy Default" },
68   { 0, NULL }
69 };
70
71 #define AIM_BUDDYLIST_TLV_MAX_CONTACT_ENTRIES           0x0001
72 #define AIM_BUDDYLIST_TLV_MAX_WATCHER_ENTRIES           0x0002
73 #define AIM_BUDDYLIST_TLV_MAX_ONLINE_NOTIFICATIONS      0x0003
74
75 const aim_tlv buddylist_tlvs[] = {
76         { AIM_BUDDYLIST_TLV_MAX_CONTACT_ENTRIES, "Max number of contact list entries", dissect_aim_tlv_value_uint16 },
77         { AIM_BUDDYLIST_TLV_MAX_WATCHER_ENTRIES, "Max number of watcher list entries", dissect_aim_tlv_value_uint16 },
78         { AIM_BUDDYLIST_TLV_MAX_ONLINE_NOTIFICATIONS, "Max online notifications", dissect_aim_tlv_value_uint16 },
79         {0, NULL, NULL }
80 };
81
82
83 /* Initialize the protocol and registered fields */
84 static int proto_aim_buddylist = -1;
85 static int hf_aim_buddyname_len = -1;
86 static int hf_aim_buddyname = -1;
87 static int hf_aim_userinfo_warninglevel = -1;
88 static int hf_aim_userinfo_tlvcount = -1;
89
90 /* Initialize the subtree pointers */
91 static gint ett_aim_buddylist = -1;
92
93 static int dissect_aim_snac_buddylist(tvbuff_t *tvb, packet_info *pinfo, 
94                                        proto_tree *tree)
95 {
96   guint8 buddyname_length = 0;
97   char buddyname[MAX_BUDDYNAME_LENGTH + 1];
98   guint16 tlv_count = 0;
99   struct aiminfo *aiminfo = pinfo->private_data;
100   int offset = 0;
101   proto_item *ti;
102   proto_tree *buddy_tree = NULL;
103
104   if(tree) {
105           ti = proto_tree_add_text(tree, tvb, 0, -1, "Buddy List Service");
106           buddy_tree = proto_item_add_subtree(ti, ett_aim_buddylist);   
107   }
108
109
110   switch(aiminfo->subtype)
111     {
112         case FAMILY_BUDDYLIST_REQRIGHTS:
113         case FAMILY_BUDDYLIST_WATCHERS_REQ:
114                 /* No data */
115                 return 0;
116         case FAMILY_BUDDYLIST_REMBUDDY:
117         case FAMILY_BUDDYLIST_ADDBUDDY:
118         case FAMILY_BUDDYLIST_WATCHERS_REP:
119                 while(tvb_length_remaining(tvb, offset) > 0) {
120                         offset = dissect_aim_buddyname( tvb, pinfo, offset, buddy_tree);
121                 }
122                 return offset;
123         case FAMILY_BUDDYLIST_ERROR:
124       return dissect_aim_snac_error(tvb, pinfo, offset, buddy_tree);
125         case FAMILY_BUDDYLIST_RIGHTSINFO:
126                 while(tvb_length_remaining(tvb, offset) > 0) {
127                         offset = dissect_aim_tlv( tvb, pinfo, offset, buddy_tree, buddylist_tlvs);
128                 }
129                 return offset;
130         case FAMILY_BUDDYLIST_REJECT:
131                 return dissect_aim_buddyname(tvb, pinfo, offset, buddy_tree);
132     case FAMILY_BUDDYLIST_ONCOMING:
133       buddyname_length = aim_get_buddyname( buddyname, tvb, offset, offset + 1 );
134
135       if (check_col(pinfo->cinfo, COL_INFO)) {
136         col_add_fstr(pinfo->cinfo, COL_INFO, "Oncoming Buddy");
137         col_append_fstr(pinfo->cinfo, COL_INFO, ": %s",
138                         format_text(buddyname, buddyname_length));
139       }
140       
141       if (buddy_tree) {
142         proto_tree_add_text(buddy_tree, tvb, offset + 1, buddyname_length, 
143                             "Screen Name: %s",
144                             format_text(buddyname, buddyname_length));
145       }
146       offset += buddyname_length + 1;
147
148       /* Warning level */
149       proto_tree_add_item(buddy_tree, hf_aim_userinfo_warninglevel, tvb, offset, 
150                           2, FALSE);
151       offset += 2;
152       
153       /* TLV Count */
154       tlv_count = tvb_get_ntohs(tvb, offset);
155       proto_tree_add_item(buddy_tree, hf_aim_userinfo_tlvcount, tvb, offset, 
156                           2, FALSE);
157       offset += 2;
158
159       while (tvb_length_remaining(tvb, offset) > 0) {
160         offset = dissect_aim_tlv(tvb, pinfo, offset, buddy_tree, onlinebuddy_tlvs);
161       }
162
163       return offset;
164       
165     case FAMILY_BUDDYLIST_OFFGOING:
166       buddyname_length = aim_get_buddyname( buddyname, tvb, offset, offset + 1 );
167       
168       if (check_col(pinfo->cinfo, COL_INFO)) {
169         col_add_fstr(pinfo->cinfo, COL_INFO, "Offgoing Buddy");
170         col_append_fstr(pinfo->cinfo, COL_INFO, ": %s",
171                         format_text(buddyname, buddyname_length));
172       }
173       
174       if (buddy_tree) {
175         proto_tree_add_text(buddy_tree, tvb, offset + 1, buddyname_length, 
176                             "Screen Name: %s",
177                             format_text(buddyname, buddyname_length));
178       }
179       offset += buddyname_length + 1;
180
181       /* Warning level */
182       proto_tree_add_item(buddy_tree, hf_aim_userinfo_warninglevel, tvb, offset, 
183                           2, FALSE);
184       offset += 2;
185
186           return dissect_aim_tlv_list(tvb, pinfo, offset, buddy_tree, onlinebuddy_tlvs);
187         default:
188           return 0;
189     }
190 }
191
192
193 /* Register the protocol with Ethereal */
194 void
195 proto_register_aim_buddylist(void)
196 {
197
198 /* Setup list of header fields */
199   static hf_register_info hf[] = {
200     { &hf_aim_buddyname_len,
201       { "Buddyname len", "aim.buddynamelen", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }
202     },
203     { &hf_aim_buddyname,
204       { "Buddy Name", "aim.buddyname", FT_STRING, BASE_NONE, NULL, 0x0, "", HFILL }
205     },
206     { &hf_aim_userinfo_warninglevel,
207       { "Warning Level", "aim.userinfo.warninglevel", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL },
208     },
209     { &hf_aim_userinfo_tlvcount,
210       { "TLV Count", "aim.userinfo.tlvcount", FT_UINT16, BASE_DEC, NULL, 0x0, "", HFILL },
211     },
212   };
213
214 /* Setup protocol subtree array */
215   static gint *ett[] = {
216     &ett_aim_buddylist,
217   };
218
219 /* Register the protocol name and description */
220   proto_aim_buddylist = proto_register_protocol("AIM Buddylist Service", "AIM Buddylist", "aim_buddylist");
221
222 /* Required function calls to register the header fields and subtrees used */
223   proto_register_field_array(proto_aim_buddylist, hf, array_length(hf));
224   proto_register_subtree_array(ett, array_length(ett));
225 }
226
227 void
228 proto_reg_handoff_aim_buddylist(void)
229 {
230   dissector_handle_t aim_handle;
231
232   aim_handle = new_create_dissector_handle(dissect_aim_snac_buddylist, proto_aim_buddylist);
233   dissector_add("aim.family", FAMILY_BUDDYLIST, aim_handle);
234   aim_init_family(FAMILY_BUDDYLIST, "Buddylist", aim_fnac_family_buddylist);
235 }