Get rid of a debugging fprintf.
[obnox/wireshark/wip.git] / packet-tns.c
1 /* packet-tns.c
2  * Routines for MSX tns packet dissection
3  *
4  * $Id: packet-tns.c,v 1.7 2000/05/31 05:07:50 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * Copied from packet-tftp.c
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
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
35 #endif
36
37 #ifdef HAVE_NETINET_IN_H
38 # include <netinet/in.h>
39 #endif
40
41 #include <string.h>
42 #include <glib.h>
43 #include "packet.h"
44 #include "packet-tns.h"
45
46 static int proto_tns = -1;
47 static int hf_tns_request = -1;
48 static int hf_tns_response = -1;
49 static int hf_tns_length = -1;
50 static int hf_tns_packet_checksum = -1;
51 static int hf_tns_header_checksum = -1;
52 static int hf_tns_packet_type = -1;
53 static int hf_tns_reserved_byte = -1;
54 static int hf_tns_data_flag = -1;
55 static int hf_tns_sns = -1;
56 static int hf_tns_connect = -1;
57 static int hf_tns_version = -1;
58 static int hf_tns_compat_version = -1;
59 static int hf_tns_service_options = -1;
60
61 static gint ett_tns = -1;
62 static gint ett_tns_sns = -1;
63 static gint ett_tns_connect = -1;
64 static gint ett_sql = -1;
65
66 #define TCP_PORT_TNS                    1521
67
68 static const value_string tns_type_vals[] = {
69                 {TNS_TYPE_CONNECT, "Connect" },
70                 {TNS_TYPE_ACCEPT, "Accept" },
71                 {TNS_TYPE_DATA, "Data" },
72                 {TNS_TYPE_RESEND, "Resend"},
73                 {0, NULL}
74 };
75
76
77 /* Handy macro for checking for truncated packet */
78 #define TRUNC(length) if ( ! BYTES_ARE_IN_FRAME(offset, length)) { \
79                         dissect_data(pd,offset,fd,tree); return; }
80
81 static void dissect_tns_sns(const u_char *pd, int offset, frame_data *fd, 
82         proto_tree *tree, proto_tree *tns_tree)
83 {
84         proto_tree *sns_tree = NULL, *ti;
85
86         if ( tree )
87         {
88                 ti = proto_tree_add_text(tns_tree, NullTVB, offset, END_OF_FRAME, "Secure Network Services");
89                 sns_tree = proto_item_add_subtree(ti, ett_tns_sns);
90
91                 proto_tree_add_boolean_hidden(tns_tree, hf_tns_sns, NullTVB, 0, 0, TRUE);
92         }
93                 
94         if ( check_col(fd, COL_INFO) )
95         {
96                 col_append_fstr(fd, COL_INFO, ", SNS");
97         }
98
99         if ( sns_tree )
100         {
101                 dissect_data(pd,offset,fd,sns_tree);
102         }
103 }
104
105 static void dissect_tns_data(const u_char *pd, int offset, frame_data *fd, 
106         proto_tree *tree, proto_tree *tns_tree)
107 {
108
109         TRUNC(2);
110         if ( tree )
111         {
112                 proto_tree_add_uint(tns_tree, hf_tns_data_flag, NullTVB,
113                         offset, 2, pntohs(&pd[offset]));
114         }
115         offset += 2;
116
117         if ( BYTES_ARE_IN_FRAME(offset, 4) )
118         {
119                 if ( pd[offset] == 0xDE && pd[offset+1] == 0xAD &&
120                         pd[offset+2] == 0xBE && pd[offset+3] == 0xEF )
121                 {
122                         dissect_tns_sns(pd,offset,fd,tree,tns_tree);
123                         return;
124                 }
125         }
126         
127         dissect_data(pd,offset,fd,tree);
128         return;
129 }
130
131 static void dissect_tns_connect(const u_char *pd, int offset, frame_data *fd, 
132         proto_tree *tree, proto_tree *tns_tree)
133 {
134         proto_tree *connect_tree = NULL, *ti;
135
136         if ( tree )
137         {
138                 ti = proto_tree_add_text(tns_tree, NullTVB, offset, END_OF_FRAME, "Connect");
139                 connect_tree = proto_item_add_subtree(ti, ett_tns_connect);
140
141                 proto_tree_add_boolean_hidden(tns_tree, hf_tns_connect, NullTVB, 0, 0, TRUE);
142         }
143                 
144         if ( check_col(fd, COL_INFO) )
145         {
146                 col_append_fstr(fd, COL_INFO, ", Connect");
147         }
148
149         TRUNC(2);
150         if ( connect_tree )
151         {
152                 proto_tree_add_uint(connect_tree, hf_tns_version, NullTVB,
153                         offset, 2, pntohs(&pd[offset]));
154         }
155         offset += 2;
156         
157         TRUNC(2);
158         if ( connect_tree )
159         {
160                 proto_tree_add_uint(connect_tree, hf_tns_compat_version, NullTVB,
161                         offset, 2, pntohs(&pd[offset]));
162         }
163         offset += 2;
164
165         TRUNC(2);
166         if ( connect_tree )
167         {
168                 /* need to break down w/ bitfield */
169                 proto_tree_add_uint(connect_tree, hf_tns_service_options, NullTVB,
170                         offset, 2, pntohs(&pd[offset]));
171         }
172         offset += 2;
173
174         if ( connect_tree )
175         {
176                 dissect_data(pd,offset,fd,connect_tree);
177         }
178         return;
179 }
180
181 static void dissect_tns_accept(const u_char *pd, int offset, frame_data *fd, 
182         proto_tree *tree, proto_tree *tns_tree)
183 {
184         dissect_data(pd,offset,fd,tns_tree);
185         return;
186 }
187
188
189 static void
190 dissect_tns(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
191 {
192         proto_tree      *tns_tree = NULL, *ti;
193         guint16 length;
194         guint16 type;
195
196         if (check_col(fd, COL_PROTOCOL))
197                 col_add_str(fd, COL_PROTOCOL, "TNS");
198
199         if (check_col(fd, COL_INFO))
200         {
201                 col_add_fstr(fd, COL_INFO, "%s", 
202                         (pi.match_port == pi.destport) ? "Request" : "Response");         
203         }
204
205         if (tree) 
206         {
207                 ti = proto_tree_add_item(tree, proto_tns, NullTVB, offset, END_OF_FRAME, FALSE);
208                 tns_tree = proto_item_add_subtree(ti, ett_tns);
209
210                 if (pi.match_port == pi.destport)
211                 {
212                         proto_tree_add_boolean_hidden(tns_tree, hf_tns_request, NullTVB,
213                            offset, END_OF_FRAME, TRUE);
214                         proto_tree_add_text(tns_tree, NullTVB, offset, 
215                                 END_OF_FRAME, "Request: <opaque data>" );
216                 }
217                 else
218                 {
219                         proto_tree_add_boolean_hidden(tns_tree, hf_tns_response, NullTVB,
220                                 offset, END_OF_FRAME, TRUE);
221                         proto_tree_add_text(tns_tree, NullTVB, offset, 
222                                 END_OF_FRAME, "Response: <opaque data>");
223                 }
224         }
225
226                 /* check to make sure length is present */
227         if ( ! BYTES_ARE_IN_FRAME(offset, 2)) return;
228
229         length = pntohs(&pd[offset]);
230         if (tree)
231         {
232                 proto_tree_add_uint(tns_tree, hf_tns_length, NullTVB,
233                         offset, 2, length);
234         }
235         TRUNC(length);
236         offset += 2;
237
238         TRUNC(2);
239         if ( tree )
240         {
241                 proto_tree_add_uint(tns_tree, hf_tns_packet_checksum, NullTVB,
242                         offset, 2, pntohs(&pd[offset]));
243         }
244         offset += 2;
245
246         TRUNC(2);
247         type = pd[offset];
248         if ( tree )
249         {
250                 proto_tree_add_uint(tns_tree, hf_tns_packet_type, NullTVB,
251                         offset, 1, type);
252         }
253         offset += 1;
254
255         if ( check_col(fd, COL_INFO))
256         {
257                 col_append_fstr(fd, COL_INFO, ", %s (%d)",
258                         val_to_str(type, tns_type_vals, "Unknown"), type);
259         }
260
261         TRUNC(1);
262         if ( tree )
263         {
264                 proto_tree_add_bytes(tns_tree, hf_tns_reserved_byte, NullTVB,
265                         offset, 1, &pd[offset]);
266         }
267         offset += 1;
268
269         TRUNC(2);
270         if ( tree )
271         {
272                 proto_tree_add_uint(tns_tree, hf_tns_header_checksum, NullTVB,
273                         offset, 2, pntohs(&pd[offset]));
274         }
275         offset += 2;
276
277         switch (type)
278         {
279                 case TNS_TYPE_CONNECT:
280                         dissect_tns_connect(pd,offset,fd,tree,tns_tree);
281                         break;
282                 case TNS_TYPE_ACCEPT:
283                         dissect_tns_accept(pd,offset,fd,tree,tns_tree);
284                         break;
285                 case TNS_TYPE_DATA:
286                         dissect_tns_data(pd,offset,fd,tree,tns_tree);
287                         break;
288                 default:
289                         dissect_data(pd,offset,fd,tns_tree);
290         }
291 }
292
293 void proto_register_tns(void)
294 {
295         static hf_register_info hf[] = {
296                 { &hf_tns_sns, { 
297                         "Secure Network Services", "tns.sns", FT_BOOLEAN, BASE_NONE, 
298                         NULL, 0x0, "Secure Network Services" }},
299                 { &hf_tns_connect, { 
300                         "Connect", "tns.connect", FT_BOOLEAN, BASE_NONE, 
301                         NULL, 0x0, "Connect" }},
302                 { &hf_tns_response, { 
303                         "Response", "tns.response", FT_BOOLEAN, BASE_NONE, 
304                         NULL, 0x0, "TRUE if TNS response" }},
305                 { &hf_tns_request, { 
306                         "Request", "tns.request", FT_BOOLEAN, BASE_NONE, 
307                         NULL, 0x0, "TRUE if TNS request" }},
308                 { &hf_tns_length, {     
309                         "Packet Length", "tns.length", FT_UINT32, BASE_NONE, 
310                         NULL, 0x0, "Length of TNS packet" }},
311                 { &hf_tns_packet_checksum, {    
312                         "Packet Checksum", "tns.packet_checksum", FT_UINT16, BASE_HEX, 
313                         NULL, 0x0, "Checksum of Packet Data" }},
314                 { &hf_tns_header_checksum, {    
315                         "Header Checksum", "tns.header_checksum", FT_UINT16, BASE_HEX, 
316                         NULL, 0x0, "Checksum of Header Data" }},
317                 { &hf_tns_data_flag, {  
318                         "Data Flag", "tns.data_flag", FT_UINT16, BASE_HEX, 
319                         NULL, 0x0, "Data Flag" }},
320                 { &hf_tns_version, {    
321                         "Version", "tns.version", FT_UINT16, BASE_DEC, 
322                         NULL, 0x0, "Version" }},
323                 { &hf_tns_compat_version, {     
324                         "Version (Compatible)", "tns.compat_version", FT_UINT16, BASE_DEC, 
325                         NULL, 0x0, "Version (Compatible)" }},
326                 { &hf_tns_service_options, {    
327                         "Service Options", "tns.service_options", FT_UINT16, BASE_HEX, 
328                         NULL, 0x0, "Service Options" }},
329                 { &hf_tns_reserved_byte, {      
330                         "Reserved Byte", "tns.reserved_byte", FT_BYTES, BASE_HEX, 
331                         NULL, 0x0, "Reserved Byte" }},
332                 { &hf_tns_packet_type, {        
333                         "Packet Type", "tns.type", FT_UINT8, BASE_NONE, 
334                         VALS(tns_type_vals), 0x0, "Type of TNS packet" }}       
335         };
336
337         static gint *ett[] = {
338                 &ett_tns,
339                 &ett_tns_sns,
340                 &ett_tns_connect,
341                 &ett_sql
342         };
343         proto_tns = proto_register_protocol(
344                 "Transparent Network Substrate Protocol", "tns");
345         proto_register_field_array(proto_tns, hf, array_length(hf));
346         proto_register_subtree_array(ett, array_length(ett));
347 }
348
349 void
350 proto_reg_handoff_tns(void)
351 {
352         dissector_add("tcp.port", TCP_PORT_TNS, dissect_tns);
353 }