Some minor bugfixes for netlogon
[obnox/wireshark/wip.git] / packet-tns.c
1 /* packet-tns.c
2  * Routines for Oracle TNS packet dissection
3  *
4  * $Id: packet-tns.c,v 1.29 2002/02/01 04:34:15 gram Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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 <epan/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_connect = -1;
55 static int hf_tns_version = -1;
56 static int hf_tns_compat_version = -1;
57 static int hf_tns_service_options = -1;
58 static int hf_tns_sdu_size = -1;
59 static int hf_tns_max_tdu_size = -1;
60 static int hf_tns_nt_proto_characteristics = -1;
61 static int hf_tns_line_turnaround = -1;
62 static int hf_tns_value_of_one = -1;
63 static int hf_tns_connect_data_length = -1;
64 static int hf_tns_connect_data_offset = -1;
65 static int hf_tns_connect_data_max = -1;
66 static int hf_tns_connect_flags0 = -1;
67 static int hf_tns_connect_flags1 = -1;
68 static int hf_tns_connect_data = -1;
69 static int hf_tns_trace_cf1 = -1;
70 static int hf_tns_trace_cf2 = -1;
71 static int hf_tns_trace_cid = -1;
72
73 static int hf_tns_accept = -1;
74 static int hf_tns_accept_data_length = -1;
75 static int hf_tns_accept_data_offset = -1;
76 static int hf_tns_accept_data = -1;
77
78 static int hf_tns_refuse = -1;
79 static int hf_tns_refuse_reason_user = -1;
80 static int hf_tns_refuse_reason_system = -1;
81 static int hf_tns_refuse_data_length = -1;
82 static int hf_tns_refuse_data = -1;
83
84 static int hf_tns_abort = -1;
85 static int hf_tns_abort_reason_user = -1;
86 static int hf_tns_abort_reason_system = -1;
87 static int hf_tns_abort_data = -1;
88
89 static int hf_tns_marker = -1;
90 static int hf_tns_marker_type = -1;
91 static int hf_tns_marker_data_byte = -1;
92 static int hf_tns_marker_data = -1;
93
94 static int hf_tns_redirect = -1;
95 static int hf_tns_redirect_data_length = -1;
96 static int hf_tns_redirect_data = -1;
97
98 static int hf_tns_control = -1;
99 static int hf_tns_control_cmd = -1;
100 static int hf_tns_control_data = -1;
101
102 static int hf_tns_data_flag = -1;
103 static int hf_tns_data_flag_send = -1;
104 static int hf_tns_data_flag_rc = -1;
105 static int hf_tns_data_flag_c = -1;
106 static int hf_tns_data_flag_reserved = -1;
107 static int hf_tns_data_flag_more = -1;
108 static int hf_tns_data_flag_eof = -1;
109 static int hf_tns_data_flag_dic = -1;
110 static int hf_tns_data_flag_rts = -1;
111 static int hf_tns_data_flag_sntt = -1;
112 static int hf_tns_data = -1;
113
114 static gint ett_tns = -1;
115 static gint ett_tns_connect = -1;
116 static gint ett_tns_accept = -1;
117 static gint ett_tns_refuse = -1;
118 static gint ett_tns_abort = -1;
119 static gint ett_tns_redirect = -1;
120 static gint ett_tns_marker = -1;
121 static gint ett_tns_attention = -1;
122 static gint ett_tns_control = -1;
123 static gint ett_tns_data = -1;
124 static gint ett_tns_data_flag = -1;
125 static gint ett_sql = -1;
126
127 static dissector_handle_t data_handle;
128
129 #define TCP_PORT_TNS                    1521
130
131 static const value_string tns_type_vals[] = {
132         {TNS_TYPE_CONNECT,   "Connect" },
133         {TNS_TYPE_ACCEPT,    "Accept" },
134         {TNS_TYPE_ACK,       "Acknowledge" },
135         {TNS_TYPE_REFUSE,    "Refuse" },
136         {TNS_TYPE_REDIRECT,  "Redirect" },
137         {TNS_TYPE_DATA,      "Data" },
138         {TNS_TYPE_NULL,      "Null" },
139         {TNS_TYPE_ABORT,     "Abort" },
140         {TNS_TYPE_RESEND,    "Resend"},
141         {TNS_TYPE_MARKER,    "Marker"},
142         {TNS_TYPE_ATTENTION, "Attention"},
143         {TNS_TYPE_CONTROL,   "Control"},
144         {0, NULL}
145 };
146
147 static const value_string tns_marker_types[] = {
148         {0, "Data Marker - 0 Data Bytes"},
149         {1, "Data Marker - 1 Data Bytes"},
150         {2, "Attention Marker"},
151         {0, NULL}
152 };
153
154 static const value_string tns_control_cmds[] = {
155         {1, "Oracle Trace Command"},
156         {0, NULL}
157 };
158
159 static void dissect_tns_data(tvbuff_t *tvb, int offset, packet_info *pinfo,
160         proto_tree *tree, proto_tree *tns_tree)
161 {
162         proto_tree *data_tree = NULL, *ti;
163         int is_sns = 0;
164         
165         if ( tvb_bytes_exist(tvb, offset+2, 4) )
166         {
167                 if ( tvb_get_guint8(tvb, offset+2) == 0xDE &&
168                      tvb_get_guint8(tvb, offset+3) == 0xAD &&
169                      tvb_get_guint8(tvb, offset+4) == 0xBE &&
170                      tvb_get_guint8(tvb, offset+5) == 0xEF )
171                 {
172                         is_sns = 1;
173                 }
174         }
175
176         if ( tree )
177         {
178                 if ( is_sns )
179                 {
180                         ti = proto_tree_add_text(tns_tree, tvb, offset, -1,
181                             "Secure Network Services");
182                 }
183                 else
184                 {
185                         ti = proto_tree_add_text(tns_tree, tvb, offset, -1,
186                             "Data");            
187                 }
188                 data_tree = proto_item_add_subtree(ti, ett_tns_data);
189
190                 proto_tree_add_boolean_hidden(tns_tree, hf_tns_data, tvb, 0, 0,
191                                 TRUE);
192         }
193
194         if ( tree )
195         {
196                 proto_tree *df_tree = NULL;
197                 
198                 ti = proto_tree_add_item(data_tree, hf_tns_data_flag, tvb, offset, 2, FALSE);
199                 
200                 df_tree = proto_item_add_subtree(ti, ett_tns_data_flag);
201                 proto_tree_add_item(df_tree, hf_tns_data_flag_send, tvb, offset, 2, FALSE);
202                 proto_tree_add_item(df_tree, hf_tns_data_flag_rc, tvb, offset, 2, FALSE);
203                 proto_tree_add_item(df_tree, hf_tns_data_flag_c, tvb, offset, 2, FALSE);
204                 proto_tree_add_item(df_tree, hf_tns_data_flag_reserved, tvb, offset, 2, FALSE);
205                 proto_tree_add_item(df_tree, hf_tns_data_flag_more, tvb, offset, 2, FALSE);
206                 proto_tree_add_item(df_tree, hf_tns_data_flag_eof, tvb, offset, 2, FALSE);
207                 proto_tree_add_item(df_tree, hf_tns_data_flag_dic, tvb, offset, 2, FALSE);
208                 proto_tree_add_item(df_tree, hf_tns_data_flag_rts, tvb, offset, 2, FALSE);
209                 proto_tree_add_item(df_tree, hf_tns_data_flag_sntt, tvb, offset, 2, FALSE);
210         }
211         offset += 2;
212
213         if ( check_col(pinfo->cinfo, COL_INFO) )
214         {
215                 if ( is_sns )
216                 {
217                         col_append_fstr(pinfo->cinfo, COL_INFO, ", SNS");
218                 }
219                 else
220                 {
221                         col_append_fstr(pinfo->cinfo, COL_INFO, ", Data");
222                 }       
223         }
224         
225         if ( data_tree )
226         {
227                 call_dissector(data_handle,tvb_new_subset(tvb,offset,-1,tvb_reported_length_remaining(tvb,offset)),pinfo,data_tree);
228         }
229
230         return;
231 }
232
233 static void dissect_tns_connect(tvbuff_t *tvb, int offset, packet_info *pinfo,
234         proto_tree *tree, proto_tree *tns_tree)
235 {
236         proto_tree *connect_tree = NULL, *ti;
237         int cd_offset;
238         int cd_len;
239         int tns_offset = offset-8;
240
241         if ( tree )
242         {
243                 ti = proto_tree_add_text(tns_tree, tvb, offset, -1,
244                     "Connect");
245                 connect_tree = proto_item_add_subtree(ti, ett_tns_connect);
246
247                 proto_tree_add_boolean_hidden(tns_tree, hf_tns_connect, tvb,
248                     0, 0, TRUE);
249         }
250                 
251         if ( check_col(pinfo->cinfo, COL_INFO) )
252         {
253                 col_append_str(pinfo->cinfo, COL_INFO, ", Connect");
254         }
255
256         if ( connect_tree )
257         {
258                 proto_tree_add_item(connect_tree, hf_tns_version, tvb,
259                         offset, 2, FALSE);
260         }
261         offset += 2;
262         
263         if ( connect_tree )
264         {
265                 proto_tree_add_item(connect_tree, hf_tns_compat_version, tvb,
266                         offset, 2, FALSE);
267         }
268         offset += 2;
269
270         if ( connect_tree )
271         {
272                 /* need to break down w/ bitfield */
273                 proto_tree_add_item(connect_tree, hf_tns_service_options, tvb,
274                         offset, 2, FALSE);
275         }
276         offset += 2;
277
278         if ( connect_tree )
279         {
280                 proto_tree_add_item(connect_tree, hf_tns_sdu_size, tvb,
281                         offset, 2, FALSE);
282         }
283         offset += 2;
284
285         if ( connect_tree )
286         {
287                 proto_tree_add_item(connect_tree, hf_tns_max_tdu_size, tvb,
288                         offset, 2, FALSE);
289         }
290         offset += 2;
291
292         if ( connect_tree )
293         {
294                 proto_tree_add_item(connect_tree, hf_tns_nt_proto_characteristics, tvb,
295                         offset, 2, FALSE);
296         }
297         offset += 2;
298
299         if ( connect_tree )
300         {
301                 proto_tree_add_item(connect_tree, hf_tns_line_turnaround, tvb,
302                         offset, 2, FALSE);
303         }
304         offset += 2;
305
306         if ( connect_tree )
307         {
308                 proto_tree_add_item(connect_tree, hf_tns_value_of_one, tvb,
309                         offset, 2, FALSE);
310         }
311         offset += 2;
312
313         cd_len = tvb_get_ntohs(tvb, offset);
314         if ( connect_tree )
315         {
316                 proto_tree_add_uint(connect_tree, hf_tns_connect_data_length, tvb,
317                         offset, 2, cd_len);
318         }
319         offset += 2;
320
321         cd_offset = tvb_get_ntohs(tvb, offset);
322         if ( connect_tree )
323         {
324                 proto_tree_add_uint(connect_tree, hf_tns_connect_data_offset, tvb,
325                         offset, 2, cd_offset);
326         }
327         offset += 2;
328
329         if ( connect_tree )
330         {
331                 proto_tree_add_item(connect_tree, hf_tns_connect_data_max, tvb,
332                         offset, 4, FALSE);
333         }
334         offset += 4;
335
336         if ( connect_tree )
337         {
338                 proto_tree_add_item(connect_tree, hf_tns_connect_flags0, tvb,
339                         offset, 1, FALSE);
340         }
341         offset += 1;
342
343         if ( connect_tree )
344         {
345                 proto_tree_add_item(connect_tree, hf_tns_connect_flags1, tvb,
346                         offset, 1, FALSE);
347         }
348         offset += 1;
349
350         if ( connect_tree )
351         {
352                 proto_tree_add_item(connect_tree, hf_tns_trace_cf1, tvb,
353                         offset, 2, FALSE);
354         }
355         offset += 2;
356
357         if ( connect_tree )
358         {
359                 proto_tree_add_item(connect_tree, hf_tns_trace_cf2, tvb,
360                         offset, 2, FALSE);
361         }
362         offset += 2;
363
364         if ( connect_tree )
365         {
366                 proto_tree_add_item(connect_tree, hf_tns_trace_cid, tvb,
367                         offset, 2, FALSE);
368         }
369         offset += 2;
370
371         if ( connect_tree && cd_len > 0)
372         {
373                 proto_tree_add_item(connect_tree, hf_tns_connect_data, tvb,
374                         tns_offset+cd_offset, -1, FALSE);
375         }
376         return;
377 }
378
379 static void dissect_tns_accept(tvbuff_t *tvb, int offset, packet_info *pinfo,
380         proto_tree *tree, proto_tree *tns_tree)
381 {
382         proto_tree *accept_tree = NULL, *ti;
383         int accept_offset;
384         int accept_len;
385         int tns_offset = offset-8;
386
387         if ( tree )
388         {
389                 ti = proto_tree_add_text(tns_tree, tvb, offset, -1,
390                     "Accept");
391                 accept_tree = proto_item_add_subtree(ti, ett_tns_accept);
392
393                 proto_tree_add_boolean_hidden(tns_tree, hf_tns_accept, tvb,
394                     0, 0, TRUE);
395         }
396                 
397         if ( check_col(pinfo->cinfo, COL_INFO) )
398         {
399                 col_append_str(pinfo->cinfo, COL_INFO, ", Accept");
400         }
401
402         if ( accept_tree )
403         {
404                 proto_tree_add_item(accept_tree, hf_tns_version, tvb,
405                         offset, 2, FALSE);
406         }
407         offset += 2;
408         
409         if ( accept_tree )
410         {
411                 proto_tree_add_item(accept_tree, hf_tns_service_options, tvb,
412                         offset, 2, FALSE);
413         }
414         offset += 2;
415
416         if ( accept_tree )
417         {
418                 proto_tree_add_item(accept_tree, hf_tns_sdu_size, tvb,
419                         offset, 2, FALSE);
420         }
421         offset += 2;
422
423         if ( accept_tree )
424         {
425                 proto_tree_add_item(accept_tree, hf_tns_max_tdu_size, tvb,
426                         offset, 2, FALSE);
427         }
428         offset += 2;
429
430         if ( accept_tree )
431         {
432                 proto_tree_add_item(accept_tree, hf_tns_value_of_one, tvb,
433                         offset, 2, FALSE);
434         }
435         offset += 2;
436
437         accept_len = tvb_get_ntohs(tvb, offset);
438         if ( accept_tree )
439         {
440                 proto_tree_add_uint(accept_tree, hf_tns_accept_data_length, tvb,
441                         offset, 2, accept_len);
442         }
443         offset += 2;
444
445         accept_offset = tvb_get_ntohs(tvb, offset);
446         if ( accept_tree )
447         {
448                 proto_tree_add_uint(accept_tree, hf_tns_accept_data_offset, tvb,
449                         offset, 2, accept_offset);
450         }
451         offset += 2;
452
453         if ( accept_tree )
454         {
455                 proto_tree_add_item(accept_tree, hf_tns_connect_flags0, tvb,
456                         offset, 1, FALSE);
457         }
458         offset += 1;
459
460         if ( accept_tree )
461         {
462                 proto_tree_add_item(accept_tree, hf_tns_connect_flags1, tvb,
463                         offset, 1, FALSE);
464         }
465         offset += 1;
466
467         if ( accept_tree && accept_len > 0)
468         {
469                 proto_tree_add_item(accept_tree, hf_tns_accept_data, tvb,
470                         tns_offset+accept_offset, -1, FALSE);
471         }
472         return;
473 }
474
475
476 static void dissect_tns_refuse(tvbuff_t *tvb, int offset, packet_info *pinfo,
477         proto_tree *tree, proto_tree *tns_tree)
478 {
479         proto_tree *refuse_tree = NULL, *ti;
480
481         if ( tree )
482         {
483                 ti = proto_tree_add_text(tns_tree, tvb, offset, -1,
484                     "Refuse");
485                 refuse_tree = proto_item_add_subtree(ti, ett_tns_refuse);
486
487                 proto_tree_add_boolean_hidden(tns_tree, hf_tns_refuse, tvb,
488                     0, 0, TRUE);
489         }
490                 
491         if ( check_col(pinfo->cinfo, COL_INFO) )
492         {
493                 col_append_str(pinfo->cinfo, COL_INFO, ", Refuse");
494         }
495
496         if ( refuse_tree )
497         {
498                 proto_tree_add_item(refuse_tree, hf_tns_refuse_reason_user, tvb,
499                         offset, 1, FALSE);
500         }
501         offset += 1;
502
503         if ( refuse_tree )
504         {
505                 proto_tree_add_item(refuse_tree, hf_tns_refuse_reason_system, tvb,
506                         offset, 1, FALSE);
507         }
508         offset += 1;
509
510         if ( refuse_tree )
511         {
512                 proto_tree_add_item(refuse_tree, hf_tns_refuse_data_length, tvb,
513                         offset, 2, FALSE);
514         }
515         offset += 2;
516
517         if ( refuse_tree )
518         {
519                 proto_tree_add_item(refuse_tree, hf_tns_refuse_data, tvb,
520                         offset, -1, FALSE);
521         }
522         return;
523 }
524
525
526 static void dissect_tns_abort(tvbuff_t *tvb, int offset, packet_info *pinfo,
527         proto_tree *tree, proto_tree *tns_tree)
528 {
529         proto_tree *abort_tree = NULL, *ti;
530
531         if ( tree )
532         {
533                 ti = proto_tree_add_text(tns_tree, tvb, offset, -1,
534                     "Abort");
535                 abort_tree = proto_item_add_subtree(ti, ett_tns_abort);
536
537                 proto_tree_add_boolean_hidden(tns_tree, hf_tns_abort, tvb,
538                     0, 0, TRUE);
539         }
540                 
541         if ( check_col(pinfo->cinfo, COL_INFO) )
542         {
543                 col_append_str(pinfo->cinfo, COL_INFO, ", Abort");
544         }
545
546         if ( abort_tree )
547         {
548                 proto_tree_add_item(abort_tree, hf_tns_abort_reason_user, tvb,
549                         offset, 1, FALSE);
550         }
551         offset += 1;
552
553         if ( abort_tree )
554         {
555                 proto_tree_add_item(abort_tree, hf_tns_abort_reason_system, tvb,
556                         offset, 1, FALSE);
557         }
558         offset += 1;
559
560         if ( abort_tree )
561         {
562                 proto_tree_add_item(abort_tree, hf_tns_abort_data, tvb,
563                         offset, -1, FALSE);
564         }
565         return;
566 }
567
568
569 static void dissect_tns_marker(tvbuff_t *tvb, int offset, packet_info *pinfo,
570         proto_tree *tree, proto_tree *tns_tree, int is_attention)
571 {
572         proto_tree *marker_tree = NULL, *ti;
573
574         if ( tree )
575         {
576                 if ( is_attention )
577                 {
578                         ti = proto_tree_add_text(tns_tree, tvb, offset, -1,
579                             "Marker");
580                 }
581                 else
582                 {
583                         ti = proto_tree_add_text(tns_tree, tvb, offset, -1,
584                             "Attention");
585                 }
586
587                 marker_tree = proto_item_add_subtree(ti, ett_tns_marker);
588                 proto_tree_add_boolean_hidden(tns_tree, hf_tns_marker, tvb,
589                     0, 0, TRUE);
590         }
591                 
592         if ( check_col(pinfo->cinfo, COL_INFO) )
593         {
594                 if ( is_attention )
595                 {
596                         col_append_str(pinfo->cinfo, COL_INFO, ", Marker");
597                 }
598                 else
599                 {
600                         col_append_str(pinfo->cinfo, COL_INFO, ", Attention");
601                 }
602         }
603
604         if ( marker_tree )
605         {
606                 proto_tree_add_item(marker_tree, hf_tns_marker_type, tvb,
607                         offset, 1, FALSE);
608         }
609         offset += 1;
610
611         if ( marker_tree )
612         {
613                 proto_tree_add_item(marker_tree, hf_tns_marker_data_byte, tvb,
614                         offset, 1, FALSE);
615         }
616         offset += 1;
617
618         if ( marker_tree )
619         {
620                 proto_tree_add_item(marker_tree, hf_tns_marker_data_byte, tvb,
621                         offset, 1, FALSE);
622         }
623         offset += 1;
624
625         return;
626 }
627
628 static void dissect_tns_redirect(tvbuff_t *tvb, int offset, packet_info *pinfo,
629         proto_tree *tree, proto_tree *tns_tree)
630 {
631         proto_tree *redirect_tree = NULL, *ti;
632
633         if ( tree )
634         {
635                 ti = proto_tree_add_text(tns_tree, tvb, offset, -1,
636                     "Redirect");
637                 redirect_tree = proto_item_add_subtree(ti, ett_tns_redirect);
638
639                 proto_tree_add_boolean_hidden(tns_tree, hf_tns_redirect, tvb,
640                     0, 0, TRUE);
641         }
642                 
643         if ( check_col(pinfo->cinfo, COL_INFO) )
644         {
645                 col_append_str(pinfo->cinfo, COL_INFO, ", Redirect");
646         }
647
648         if ( redirect_tree )
649         {
650                 proto_tree_add_item(redirect_tree, hf_tns_redirect_data_length, tvb,
651                         offset, 2, FALSE);
652         }
653         offset += 2;
654
655         if ( redirect_tree )
656         {
657                 proto_tree_add_item(redirect_tree, hf_tns_redirect_data, tvb,
658                         offset, -1, FALSE);
659         }
660         return;
661 }
662
663 static void dissect_tns_control(tvbuff_t *tvb, int offset, packet_info *pinfo,
664         proto_tree *tree, proto_tree *tns_tree)
665 {
666         proto_tree *control_tree = NULL, *ti;
667
668         if ( tree )
669         {
670                 ti = proto_tree_add_text(tns_tree, tvb, offset, -1,
671                     "Control");
672                 control_tree = proto_item_add_subtree(ti, ett_tns_control);
673
674                 proto_tree_add_boolean_hidden(tns_tree, hf_tns_control, tvb,
675                     0, 0, TRUE);
676         }
677                 
678         if ( check_col(pinfo->cinfo, COL_INFO) )
679         {
680                 col_append_str(pinfo->cinfo, COL_INFO, ", Control");
681         }
682
683         if ( control_tree )
684         {
685                 proto_tree_add_item(control_tree, hf_tns_control_cmd, tvb,
686                         offset, 2, FALSE);
687         }
688         offset += 2;
689
690         if ( control_tree )
691         {
692                 proto_tree_add_item(control_tree, hf_tns_control_data, tvb,
693                         offset, -1, FALSE);
694         }
695         return;
696 }
697
698
699
700 static void
701 dissect_tns(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
702 {
703         proto_tree      *tns_tree = NULL, *ti;
704         int offset = 0;
705         guint16 length;
706         guint16 type;
707
708         if (check_col(pinfo->cinfo, COL_PROTOCOL))
709                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TNS");
710
711         if (check_col(pinfo->cinfo, COL_INFO))
712         {
713                 col_add_str(pinfo->cinfo, COL_INFO,
714                         (pinfo->match_port == pinfo->destport) ? "Request" : "Response");         
715         }
716
717         if (tree) 
718         {
719                 ti = proto_tree_add_item(tree, proto_tns, tvb, 0, -1, FALSE);
720                 tns_tree = proto_item_add_subtree(ti, ett_tns);
721
722                 if (pinfo->match_port == pinfo->destport)
723                 {
724                         proto_tree_add_boolean_hidden(tns_tree, hf_tns_request,
725                            tvb, offset, 0, TRUE);
726                 }
727                 else
728                 {
729                         proto_tree_add_boolean_hidden(tns_tree, hf_tns_response,
730                             tvb, offset, 0, TRUE);
731                 }
732         }
733
734         length = tvb_get_ntohs(tvb, offset);
735         if (tree)
736         {
737                 proto_tree_add_uint(tns_tree, hf_tns_length, tvb,
738                         offset, 2, length);
739         }
740         offset += 2;
741
742         if ( tree )
743         {
744                 proto_tree_add_item(tns_tree, hf_tns_packet_checksum, tvb,
745                         offset, 2, FALSE);
746         }
747         offset += 2;
748
749         type = tvb_get_guint8(tvb, offset);
750         if ( tree )
751         {
752                 proto_tree_add_uint(tns_tree, hf_tns_packet_type, tvb,
753                         offset, 1, type);
754         }
755         offset += 1;
756
757         if ( check_col(pinfo->cinfo, COL_INFO))
758         {
759                 col_append_fstr(pinfo->cinfo, COL_INFO, ", %s (%u)",
760                         val_to_str(type, tns_type_vals, "Unknown"), type);
761         }
762
763         if ( tree )
764         {
765                 proto_tree_add_item(tns_tree, hf_tns_reserved_byte, tvb,
766                         offset, 1, FALSE);
767         }
768         offset += 1;
769
770         if ( tree )
771         {
772                 proto_tree_add_item(tns_tree, hf_tns_header_checksum, tvb,
773                         offset, 2, FALSE);
774         }
775         offset += 2;
776
777         switch (type)
778         {
779                 case TNS_TYPE_CONNECT:
780                         dissect_tns_connect(tvb,offset,pinfo,tree,tns_tree);
781                         break;
782                 case TNS_TYPE_ACCEPT:
783                         dissect_tns_accept(tvb,offset,pinfo,tree,tns_tree);
784                         break;
785                 case TNS_TYPE_REFUSE:
786                         dissect_tns_refuse(tvb,offset,pinfo,tree,tns_tree);
787                         break;
788                 case TNS_TYPE_REDIRECT:
789                         dissect_tns_redirect(tvb,offset,pinfo,tree,tns_tree);
790                         break;
791                 case TNS_TYPE_ABORT:
792                         dissect_tns_abort(tvb,offset,pinfo,tree,tns_tree);
793                         break;
794                 case TNS_TYPE_MARKER:
795                         dissect_tns_marker(tvb,offset,pinfo,tree,tns_tree, 0);
796                         break;
797                 case TNS_TYPE_ATTENTION:
798                         dissect_tns_marker(tvb,offset,pinfo,tree,tns_tree, 1);
799                         break;
800                 case TNS_TYPE_CONTROL:
801                         dissect_tns_control(tvb,offset,pinfo,tree,tns_tree);
802                         break;
803                 case TNS_TYPE_DATA:
804                         dissect_tns_data(tvb,offset,pinfo,tree,tns_tree);
805                         break;
806                 default:
807                         call_dissector(data_handle,tvb_new_subset(tvb,offset,-1,tvb_reported_length_remaining(tvb,offset)),pinfo,tns_tree);
808         }
809 }
810
811 void proto_register_tns(void)
812 {
813         static hf_register_info hf[] = {
814                 { &hf_tns_response, { 
815                         "Response", "tns.response", FT_BOOLEAN, BASE_NONE, 
816                         NULL, 0x0, "TRUE if TNS response", HFILL }},
817                 { &hf_tns_request, { 
818                         "Request", "tns.request", FT_BOOLEAN, BASE_NONE, 
819                         NULL, 0x0, "TRUE if TNS request", HFILL }},
820                 { &hf_tns_length, {     
821                         "Packet Length", "tns.length", FT_UINT16, BASE_DEC, 
822                         NULL, 0x0, "Length of TNS packet", HFILL }},
823                 { &hf_tns_packet_checksum, {    
824                         "Packet Checksum", "tns.packet_checksum", FT_UINT16, BASE_HEX, 
825                         NULL, 0x0, "Checksum of Packet Data", HFILL }},
826                 { &hf_tns_header_checksum, {    
827                         "Header Checksum", "tns.header_checksum", FT_UINT16, BASE_HEX, 
828                         NULL, 0x0, "Checksum of Header Data", HFILL }},
829                         
830                 { &hf_tns_version, {    
831                         "Version", "tns.version", FT_UINT16, BASE_DEC, 
832                         NULL, 0x0, "Version", HFILL }},
833                 { &hf_tns_compat_version, {     
834                         "Version (Compatible)", "tns.compat_version", FT_UINT16, BASE_DEC, 
835                         NULL, 0x0, "Version (Compatible)", HFILL }},
836                 { &hf_tns_service_options, {    
837                         "Service Options", "tns.service_options", FT_UINT16, BASE_HEX, 
838                         NULL, 0x0, "Service Options", HFILL }},
839                 { &hf_tns_sdu_size, {   
840                         "Session Data Unit Size", "tns.sdu_size", FT_UINT16, BASE_DEC, 
841                         NULL, 0x0, "Session Data Unit Size", HFILL }},
842                 { &hf_tns_max_tdu_size, {       
843                         "Maximum Transmission Data Unit Size", "tns.max_tdu_size", FT_UINT16, BASE_DEC, 
844                         NULL, 0x0, "Maximum Transmission Data Unit Size", HFILL }},
845                 { &hf_tns_nt_proto_characteristics, {   
846                         "NT Protocol Characteristics", "tns.nt_proto_characteristics", FT_UINT16, BASE_HEX, 
847                         NULL, 0x0, "NT Protocol Characteristics", HFILL }},
848                 { &hf_tns_line_turnaround, {    
849                         "Line Turnaround Value", "tns.line_turnaround", FT_UINT16, BASE_DEC, 
850                         NULL, 0x0, "Line Turnaround Value", HFILL }},
851                 { &hf_tns_value_of_one, {       
852                         "Value of 1 in Hardware", "tns.value_of_one", FT_BYTES, BASE_NONE, 
853                         NULL, 0x0, "Value of 1 in Hardware", HFILL }},          
854
855                 { &hf_tns_connect, { 
856                         "Connect", "tns.connect", FT_BOOLEAN, BASE_NONE, 
857                         NULL, 0x0, "Connect", HFILL }},
858                 { &hf_tns_connect_data_length, {        
859                         "Length of Connect Data", "tns.connect_data_length", FT_UINT16, BASE_DEC, 
860                         NULL, 0x0, "Length of Connect Data", HFILL }},
861                 { &hf_tns_connect_data_offset, {        
862                         "Offset to Connect Data", "tns.connect_data_offset", FT_UINT16, BASE_DEC, 
863                         NULL, 0x0, "Offset to Connect Data", HFILL }},
864                 { &hf_tns_connect_data_max, {   
865                         "Maximum Receivable Connect Data", "tns.connect_data_max", FT_UINT32, BASE_DEC, 
866                         NULL, 0x0, "Maximum Receivable Connect Data", HFILL }},
867                 { &hf_tns_connect_flags0, {     
868                         "Connect Flags 0", "tns.connect_flags0", FT_UINT8, BASE_HEX, 
869                         NULL, 0x0, "Connect Flags 0", HFILL }},
870                 { &hf_tns_connect_flags1, {     
871                         "Connect Flags 1", "tns.connect_flags1", FT_UINT8, BASE_HEX, 
872                         NULL, 0x0, "Connect Flags 1", HFILL }},
873                 { &hf_tns_trace_cf1, {  
874                         "Trace Cross Facility Item 1", "tns.trace_cf1", FT_UINT16, BASE_HEX, 
875                         NULL, 0x0, "Trace Cross Facility Item 1", HFILL }},
876                 { &hf_tns_trace_cf2, {  
877                         "Trace Cross Facility Item 2", "tns.trace_cf2", FT_UINT16, BASE_HEX, 
878                         NULL, 0x0, "Trace Cross Facility Item 2", HFILL }},
879                 { &hf_tns_trace_cid, {  
880                         "Trace Unique Connection ID", "tns.trace_cid", FT_UINT16, BASE_HEX, 
881                         NULL, 0x0, "Trace Unique Connection ID", HFILL }},
882                 { &hf_tns_connect_data, {       
883                         "Connect Data", "tns.connect_data", FT_STRING, BASE_NONE, 
884                         NULL, 0x0, "Connect Data", HFILL }},
885
886                 { &hf_tns_accept, { 
887                         "Accept", "tns.accept", FT_BOOLEAN, BASE_NONE, 
888                         NULL, 0x0, "Accept", HFILL }},          
889                 { &hf_tns_accept_data_length, {         
890                         "Accept Data Length", "tns.accept_data_length", FT_UINT16, BASE_HEX, 
891                         NULL, 0x0, "Length of Accept Data", HFILL }},
892                 { &hf_tns_accept_data, {        
893                         "Accept Data", "tns.accept_data", FT_STRING, BASE_NONE, 
894                         NULL, 0x0, "Accept Data", HFILL }},
895                 { &hf_tns_accept_data_offset, {         
896                         "Offset to Accept Data", "tns.accept_data_offset", FT_UINT16, BASE_DEC, 
897                         NULL, 0x0, "Offset to Accept Data", HFILL }},
898
899
900                 { &hf_tns_refuse, { 
901                         "Refuse", "tns.refuse", FT_BOOLEAN, BASE_NONE, 
902                         NULL, 0x0, "Refuse", HFILL }},          
903                 { &hf_tns_refuse_reason_user, {         
904                         "Refuse Reason (User)", "tns.refuse_reason_user", FT_UINT8, BASE_HEX, 
905                         NULL, 0x0, "Refuse Reason from Application", HFILL }},
906                 { &hf_tns_refuse_reason_system, {       
907                         "Refuse Reason (User)", "tns.refuse_reason_system", FT_UINT8, BASE_HEX, 
908                         NULL, 0x0, "Refuse Reason from System", HFILL }},
909                 { &hf_tns_refuse_data_length, {         
910                         "Refuse Data Length", "tns.refuse_data_length", FT_UINT16, BASE_HEX, 
911                         NULL, 0x0, "Length of Refuse Data", HFILL }},
912                 { &hf_tns_refuse_data, {        
913                         "Refuse Data", "tns.refuse_data", FT_STRING, BASE_NONE, 
914                         NULL, 0x0, "Refuse Data", HFILL }},
915
916                 { &hf_tns_abort, { 
917                         "Abort", "tns.abort", FT_BOOLEAN, BASE_NONE, 
918                         NULL, 0x0, "Abort", HFILL }},           
919                 { &hf_tns_abort_reason_user, {  
920                         "Abort Reason (User)", "tns.abort_reason_user", FT_UINT8, BASE_HEX, 
921                         NULL, 0x0, "Abort Reason from Application", HFILL }},
922                 { &hf_tns_abort_reason_system, {        
923                         "Abort Reason (User)", "tns.abort_reason_system", FT_UINT8, BASE_HEX, 
924                         NULL, 0x0, "Abort Reason from System", HFILL }},
925                 { &hf_tns_abort_data, {         
926                         "Abort Data", "tns.abort_data", FT_STRING, BASE_NONE, 
927                         NULL, 0x0, "Abort Data", HFILL }},
928
929                 { &hf_tns_marker, { 
930                         "Marker", "tns.marker", FT_BOOLEAN, BASE_NONE, 
931                         NULL, 0x0, "Marker", HFILL }},                          
932                 { &hf_tns_marker_type, {        
933                         "Marker Type", "tns.marker.type", FT_UINT8, BASE_HEX, 
934                         VALS(tns_marker_types), 0x0, "Marker Type", HFILL }},
935                 { &hf_tns_marker_data_byte, {   
936                         "Marker Data Byte", "tns.marker.databyte", FT_UINT8, BASE_HEX, 
937                         NULL, 0x0, "Marker Data Byte", HFILL }},
938                 { &hf_tns_marker_data, {        
939                         "Marker Data", "tns.marker.data", FT_UINT16, BASE_HEX, 
940                         NULL, 0x0, "Marker Data", HFILL }},
941
942                 { &hf_tns_control, { 
943                         "Control", "tns.control", FT_BOOLEAN, BASE_NONE, 
944                         NULL, 0x0, "Control", HFILL }},         
945                 { &hf_tns_control_cmd, {        
946                         "Control Command", "tns.control.cmd", FT_UINT16, BASE_HEX, 
947                         VALS(tns_control_cmds), 0x0, "Control Command", HFILL }},
948                 { &hf_tns_control_data, {       
949                         "Control Data", "tns.control.data", FT_BYTES, BASE_HEX, 
950                         NULL, 0x0, "Control Data", HFILL }},
951
952                 { &hf_tns_redirect, { 
953                         "Redirect", "tns.redirect", FT_BOOLEAN, BASE_NONE, 
954                         NULL, 0x0, "Redirect", HFILL }},                
955                 { &hf_tns_redirect_data_length, {       
956                         "Redirect Data Length", "tns.redirect_data_length", FT_UINT16, BASE_HEX, 
957                         NULL, 0x0, "Length of Redirect Data", HFILL }},
958                 { &hf_tns_redirect_data, {      
959                         "Redirect Data", "tns.redirect_data", FT_STRING, BASE_NONE, 
960                         NULL, 0x0, "Redirect Data", HFILL }},
961
962                 { &hf_tns_data, { 
963                         "Data", "tns.data", FT_BOOLEAN, BASE_NONE, 
964                         NULL, 0x0, "Data", HFILL }},            
965                 { &hf_tns_data_flag, {  
966                         "Data Flag", "tns.data_flag", FT_UINT16, BASE_HEX, 
967                         NULL, 0x0, "Data Flag", HFILL }},
968                 { &hf_tns_data_flag_send, {     
969                         "Send Token", "tns.data_flag.send", FT_UINT16, BASE_BIN, 
970                         NULL, 0x1, "Send Token", HFILL }},
971                 { &hf_tns_data_flag_rc, {       
972                         "Request Confirmation", "tns.data_flag.rc", FT_UINT16, BASE_BIN, 
973                         NULL, 0x2, "Request Confirmation", HFILL }},
974                 { &hf_tns_data_flag_c, {        
975                         "Confirmation", "tns.data_flag.c", FT_UINT16, BASE_BIN, 
976                         NULL, 0x4, "Confirmation", HFILL }},
977                 { &hf_tns_data_flag_reserved, {         
978                         "Reserved", "tns.data_flag.reserved", FT_UINT16, BASE_BIN, 
979                         NULL, 0x8, "Reserved", HFILL }},
980                 { &hf_tns_data_flag_more, {     
981                         "More Data to Come", "tns.data_flag.more", FT_UINT16, BASE_BIN, 
982                         NULL, 0x20, "More Data to Come", HFILL }},
983                 { &hf_tns_data_flag_eof, {      
984                         "End of File", "tns.data_flag.eof", FT_UINT16, BASE_BIN, 
985                         NULL, 0x40, "End of File", HFILL }},
986                 { &hf_tns_data_flag_dic, {      
987                         "Do Immediate Confirmation", "tns.data_flag.dic", FT_UINT16, BASE_BIN, 
988                         NULL, 0x80, "Do Immediate Confirmation", HFILL }},
989                 { &hf_tns_data_flag_rts, {      
990                         "Request To Send", "tns.data_flag.rts", FT_UINT16, BASE_BIN, 
991                         NULL, 0x100, "Request To Send", HFILL }},
992                 { &hf_tns_data_flag_sntt, {     
993                         "Send NT Trailer", "tns.data_flag.sntt", FT_UINT16, BASE_BIN, 
994                         NULL, 0x200, "Send NT Trailer", HFILL }},
995
996
997                 { &hf_tns_reserved_byte, {      
998                         "Reserved Byte", "tns.reserved_byte", FT_BYTES, BASE_HEX, 
999                         NULL, 0x0, "Reserved Byte", HFILL }},
1000                 { &hf_tns_packet_type, {        
1001                         "Packet Type", "tns.type", FT_UINT8, BASE_DEC, 
1002                         VALS(tns_type_vals), 0x0, "Type of TNS packet", HFILL }}        
1003
1004         };
1005
1006         static gint *ett[] = {
1007                 &ett_tns,
1008                 &ett_tns_connect,
1009                 &ett_tns_accept,
1010                 &ett_tns_refuse,
1011                 &ett_tns_abort,
1012                 &ett_tns_redirect,
1013                 &ett_tns_marker,
1014                 &ett_tns_attention,
1015                 &ett_tns_control,
1016                 &ett_tns_data,
1017                 &ett_tns_data_flag,
1018                 &ett_sql
1019         };
1020         proto_tns = proto_register_protocol(
1021                 "Transparent Network Substrate Protocol", "TNS", "tns");
1022         proto_register_field_array(proto_tns, hf, array_length(hf));
1023         proto_register_subtree_array(ett, array_length(ett));
1024 }
1025
1026 void
1027 proto_reg_handoff_tns(void)
1028 {
1029         dissector_handle_t tns_handle;
1030
1031         tns_handle = create_dissector_handle(dissect_tns, proto_tns);
1032         dissector_add("tcp.port", TCP_PORT_TNS, tns_handle);
1033         data_handle = find_dissector("data");
1034 }