#include <string.h> and/or #include <stdio.h> not needed.
[obnox/wireshark/wip.git] / epan / dissectors / packet-aim-chat.c
1 /* packet-aim-chat.c
2  * Routines for AIM Instant Messenger (OSCAR) dissection, SNAC Chat
3  * Copyright 2004, Jelmer Vernooij <jelmer@samba.org>
4  * Copyright 2000, Ralf Hoelzer <ralf@well.com>
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
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 <stdlib.h>
32 #include <ctype.h>
33
34 #include <glib.h>
35
36 #include <epan/packet.h>
37 #include <epan/strutil.h>
38 #include <epan/emem.h>
39
40 #include "packet-tcp.h"
41 #include "packet-aim.h"
42
43 /* SNAC families */
44 #define FAMILY_CHAT       0x000E
45
46 #define AIM_CHAT_TLV_BROWSABLE_TREE             0x001
47 #define AIM_CHAT_TLV_CLASS_EXCLUSIVE            0x002
48 #define AIM_CHAT_TLV_MAX_CONCURRENT_ROOMS       0x003
49 #define AIM_CHAT_TLV_MAX_ROOM_NAME_LEN          0x004
50 #define AIM_CHAT_TLV_ROOT_ROOMS                         0x005
51 #define AIM_CHAT_TLV_SEARCH_TAGS                        0x006
52 #define AIM_CHAT_TLV_CHILD_ROOMS                        0x065
53 #define AIM_CHAT_TLV_CONTAINS_USER_CLASS        0x066
54 #define AIM_CHAT_TLV_CONTAINS_USER_ARRAY        0x067
55
56 static const aim_tlv chat_tlvs[] _U_ = {
57         { AIM_CHAT_TLV_BROWSABLE_TREE, "Browsable tree", dissect_aim_tlv_value_bytes },
58         { AIM_CHAT_TLV_CLASS_EXCLUSIVE, "Exclusively for class", dissect_aim_tlv_value_userclass },
59         { AIM_CHAT_TLV_MAX_CONCURRENT_ROOMS, "Max. number of concurrent rooms", dissect_aim_tlv_value_uint8 },
60         { AIM_CHAT_TLV_MAX_ROOM_NAME_LEN, "Max. length of room name", dissect_aim_tlv_value_uint8 },
61         { AIM_CHAT_TLV_ROOT_ROOMS, "Root Rooms", dissect_aim_tlv_value_bytes },
62         { AIM_CHAT_TLV_SEARCH_TAGS, "Search Tags", dissect_aim_tlv_value_bytes },
63         { AIM_CHAT_TLV_CHILD_ROOMS, "Child Rooms", dissect_aim_tlv_value_bytes },
64         { AIM_CHAT_TLV_CONTAINS_USER_CLASS, "Contains User Class", dissect_aim_tlv_value_bytes },
65         { AIM_CHAT_TLV_CONTAINS_USER_ARRAY, "Contains User Array", dissect_aim_tlv_value_bytes },
66         { 0, NULL, NULL }
67 };
68
69 /* Initialize the protocol and registered fields */
70 static int proto_aim_chat = -1;
71
72 /* Initialize the subtree pointers */
73 static gint ett_aim_chat          = -1;
74
75 static int dissect_aim_chat_userinfo_list(tvbuff_t *tvb, packet_info *pinfo, proto_tree *chat_tree)
76 {
77         int offset = 0;
78         while(tvb_length_remaining(tvb, offset) > 0) {
79                 offset = dissect_aim_userinfo(tvb, pinfo, offset, chat_tree);
80         }
81         return offset;
82 }
83
84 static int dissect_aim_chat_outgoing_msg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *chat_tree _U_)
85 {
86         guchar *buddyname;
87         guchar *msg;
88         int buddyname_length;
89
90         buddyname=ep_alloc(MAX_BUDDYNAME_LENGTH+1);
91         msg=ep_alloc(1000);
92         buddyname_length = aim_get_buddyname( buddyname, tvb, 30, 31 );
93
94         /* channel message from client */
95         aim_get_message( msg, tvb, 40 + buddyname_length, tvb_length(tvb) 
96                                          - 40 - buddyname_length );
97
98         col_append_fstr(pinfo->cinfo, COL_INFO, " -> %s", msg);
99         
100         return tvb_length(tvb);
101 }
102
103
104 static int dissect_aim_chat_incoming_msg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *chat_tree)
105 {
106         guchar *buddyname;
107         guchar *msg;
108         /* channel message to client */
109         int buddyname_length;
110         
111         buddyname=ep_alloc(MAX_BUDDYNAME_LENGTH+1);
112         msg=ep_alloc(1000);
113         buddyname_length = aim_get_buddyname( buddyname, tvb, 30, 31 );
114
115         aim_get_message( msg, tvb, 36 + buddyname_length, tvb_length(tvb) 
116                                          - 36 - buddyname_length );
117
118         col_append_fstr(pinfo->cinfo, COL_INFO, "from: %s", buddyname);
119         col_append_fstr(pinfo->cinfo, COL_INFO, " -> %s", msg);
120
121         if(chat_tree) {
122                 proto_tree_add_text(chat_tree, tvb, 31, buddyname_length, 
123                                                         "Screen Name: %s",
124                                                         format_text(buddyname, buddyname_length));
125         }
126         return tvb_length(tvb);
127 }
128
129 static const aim_subtype aim_fnac_family_chat[] = {
130   { 0x0001, "Error", dissect_aim_snac_error },
131   { 0x0002, "Room Info Update", NULL },
132   { 0x0003, "User Join", dissect_aim_chat_userinfo_list },
133   { 0x0004, "User Leave", dissect_aim_chat_userinfo_list },
134   { 0x0005, "Outgoing Message", dissect_aim_chat_outgoing_msg },
135   { 0x0006, "Incoming Message", dissect_aim_chat_incoming_msg },
136   { 0x0007, "Evil Request", NULL },
137   { 0x0008, "Evil Reply", NULL },
138   { 0, NULL, NULL }
139 };
140
141 /* Register the protocol with Wireshark */
142 void
143 proto_register_aim_chat(void)
144 {
145
146 /* Setup list of header fields */
147 /*FIXME
148   static hf_register_info hf[] = {
149   };*/
150
151 /* Setup protocol subtree array */
152   static gint *ett[] = {
153     &ett_aim_chat,
154   };
155
156 /* Register the protocol name and description */
157   proto_aim_chat = proto_register_protocol("AIM Chat Service", "AIM Chat", "aim_chat");
158
159 /* Required function calls to register the header fields and subtrees used */
160 /*FIXME
161   proto_register_field_array(proto_aim_chat, hf, array_length(hf));*/
162   proto_register_subtree_array(ett, array_length(ett));
163 }
164
165 void
166 proto_reg_handoff_aim_chat(void)
167 {
168   aim_init_family(proto_aim_chat, ett_aim_chat, FAMILY_CHAT, aim_fnac_family_chat);
169 }