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