Add some additional sanity checking.
[obnox/wireshark/wip.git] / packet-fcct.c
1 /* packet-fcct.c
2  * Routines for FC Common Transport Protocol (used by GS3 services)
3  * Copyright 2001, Dinesh G Dutt <ddutt@andiamo.com>
4  *
5  * $Id: packet-fcct.c,v 1.2 2003/06/30 21:50:17 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
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41
42 #include <glib.h>
43
44 #ifdef NEED_SNPRINTF_H
45 # include "snprintf.h"
46 #endif
47
48 #include <epan/packet.h>
49 #include "etypes.h"
50 #include "packet-fc.h"
51 #include "packet-fcct.h"
52
53 /* Initialize the protocol and registered fields */
54 static int proto_fcct           = -1;
55 static int hf_fcct_revision     = -1;
56 static int hf_fcct_inid         = -1;
57 static int hf_fcct_gstype       = -1;
58 static int hf_fcct_gssubtype    = -1;
59 static int hf_fcct_options      = -1;
60 static int hf_fcct_server       = -1; /* derived field */
61
62 /* Extended preamble fields */
63 static int hf_fcct_ext_said     = -1;
64 static int hf_fcct_ext_tid      = -1;
65 static int hf_fcct_ext_reqname  = -1;
66 static int hf_fcct_ext_tstamp   = -1;
67 static int hf_fcct_ext_authblk  = -1;
68
69 /* Initialize the subtree pointers */
70 static gint ett_fcct = -1;
71 static gint ett_fcct_ext = -1;  /* for the extended header */
72
73 const value_string fc_ct_rjt_code_vals [] = {
74     {FCCT_RJT_INVCMDCODE, "Invalid Cmd Code"},
75     {FCCT_RJT_INVVERSION, "Invalid Version Level"},
76     {FCCT_RJT_LOGICALERR, "Logical Error"},
77     {FCCT_RJT_INVSIZE,    "Invalid CT_IU Size"},
78     {FCCT_RJT_LOGICALBSY, "Logical Busy"},
79     {FCCT_RJT_PROTOERR,   "Protocol Error"},
80     {FCCT_RJT_GENFAIL,    "Unable to Perform Cmd"},
81     {FCCT_RJT_CMDNOTSUPP, "Cmd Not Supported"},
82     {0, NULL},
83 };
84
85 static const value_string fc_ct_gstype_vals[] = {
86     {FCCT_GSTYPE_KEYSVC, "Key Service"},
87     {FCCT_GSTYPE_ALIASSVC, "Alias Service"},
88     {FCCT_GSTYPE_MGMTSVC, "Management Service"},
89     {FCCT_GSTYPE_TIMESVC, "Time Service"},
90     {FCCT_GSTYPE_DIRSVC, "Directory Service"},
91     {0, NULL},
92 };
93
94 static const value_string fc_ct_gsserver_vals[] = {
95     {FCCT_GSRVR_DNS, "dNS"},
96     {FCCT_GSRVR_IP,  "IP"},
97     {FCCT_GSRVR_FCS, "Fabric Config Server"},
98     {FCCT_GSRVR_UNS, "Unzoned Name Server"},
99     {FCCT_GSRVR_FZS, "Fabric Zone Server"},
100     {FCCT_GSRVR_TS,  "Time Server"},
101     {FCCT_GSRVR_KS,  "Key Server"},
102     {FCCT_GSRVR_AS,  "Alias Server"},
103     {0, NULL},
104 };
105
106 static dissector_table_t fcct_gserver_table;
107 static dissector_handle_t data_handle;
108
109 static guint8
110 get_gs_server (guint8 gstype, guint8 gssubtype)
111 {
112     switch (gstype) {
113     case FCCT_GSTYPE_KEYSVC:
114         return FCCT_GSRVR_KS;
115     case FCCT_GSTYPE_ALIASSVC:
116         if (gssubtype == FCCT_GSSUBTYPE_AS)
117             return FCCT_GSRVR_AS;
118         return FCCT_GSRVR_UNKNOWN;
119     case FCCT_GSTYPE_MGMTSVC:
120         if (gssubtype == FCCT_GSSUBTYPE_FCS)
121             return FCCT_GSRVR_FCS;
122         else if (gssubtype == FCCT_GSSUBTYPE_UNS)
123             return FCCT_GSRVR_UNS;
124         else if (gssubtype == FCCT_GSSUBTYPE_FZS)
125             return FCCT_GSRVR_FZS;
126         else return FCCT_GSRVR_UNKNOWN;
127     case FCCT_GSTYPE_TIMESVC:
128         if (gssubtype == FCCT_GSSUBTYPE_TS)
129             return FCCT_GSRVR_TS;
130         return FCCT_GSRVR_UNKNOWN;
131     case FCCT_GSTYPE_DIRSVC:
132         if (gssubtype == FCCT_GSSUBTYPE_DNS)
133             return FCCT_GSRVR_DNS;
134         else if (gssubtype == FCCT_GSSUBTYPE_IP)
135             return FCCT_GSRVR_IP;
136         return FCCT_GSRVR_UNKNOWN;
137     default:
138         return FCCT_GSRVR_UNKNOWN;
139     }
140 }
141
142 /* Code to actually dissect the packets */
143 static void
144 dissect_fcct (tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
145 {
146
147 /* Set up structures needed to add the protocol subtree and manage it */
148     proto_item *ti;
149     proto_tree *fcct_tree;
150     tvbuff_t *next_tvb;
151     int in_id,
152         offset = 0;
153     guint8 server;
154     fc_ct_preamble cthdr;
155
156     /* Make entries in Protocol column and Info column on summary display */
157     if (check_col(pinfo->cinfo, COL_PROTOCOL)) 
158         col_set_str(pinfo->cinfo, COL_PROTOCOL, "FC_CT");
159     
160     /*
161       cthdr.revision = tvb_get_guint8 (tvb, offset++);
162       cthdr.in_id = tvb_get_ntoh24 (tvb, offset);
163       offset += 3;
164       
165       cthdr.gstype = tvb_get_guint8 (tvb, offset++);
166       cthdr.options = tvb_get_guint8 (tvb, offset++);
167     */
168     tvb_memcpy (tvb, (guint8 *)&cthdr, offset, FCCT_PRMBL_SIZE);
169     cthdr.revision = tvb_get_guint8 (tvb, offset++);
170     cthdr.in_id = tvb_get_ntoh24 (tvb, offset);
171     cthdr.opcode = ntohs (cthdr.opcode);
172     cthdr.maxres_size = ntohs (cthdr.maxres_size);
173
174     if (check_col (pinfo->cinfo, COL_INFO)) {
175         if (cthdr.opcode < FCCT_MSG_REQ_MAX) {
176             col_append_str (pinfo->cinfo, COL_INFO, " Request");
177         }
178         else if (cthdr.opcode == FCCT_MSG_ACC) {
179             col_append_str (pinfo->cinfo, COL_INFO, " Accept");
180         }
181         else if (cthdr.opcode == FCCT_MSG_RJT) {
182             col_append_fstr (pinfo->cinfo, COL_INFO, " Reject (%s)",
183                              val_to_str (cthdr.rjt_code, fc_ct_rjt_code_vals, "0x%x")); 
184         }
185         else {
186             col_append_str (pinfo->cinfo, COL_INFO, " Reserved");
187         }
188     }
189     
190     in_id = cthdr.in_id;
191     in_id = htonl (in_id) >> 8;
192
193     /* Determine server */
194     server = get_gs_server (cthdr.gstype, cthdr.gssubtype);
195     
196     if (tree) {
197         offset = 0;
198         ti = proto_tree_add_protocol_format (tree, proto_fcct, tvb, 0, FCCT_PRMBL_SIZE,
199                                              "FC_CT");
200         fcct_tree = proto_item_add_subtree (ti, ett_fcct);
201
202         proto_tree_add_item (fcct_tree, hf_fcct_revision, tvb, offset++,
203                              sizeof (guint8), 0);
204         proto_tree_add_string (fcct_tree, hf_fcct_inid, tvb, offset, 3,
205                                fc_to_str ((guint8 *)&in_id));
206         offset += 3; /* sizeof FC address */
207         
208         proto_tree_add_item (fcct_tree, hf_fcct_gstype, tvb, offset++,
209                              sizeof (guint8), 0);
210         proto_tree_add_item (fcct_tree, hf_fcct_gssubtype, tvb, offset,
211                              sizeof (guint8), 0);
212         proto_tree_add_uint (fcct_tree, hf_fcct_server, tvb, offset++, 1,
213                              server);
214         proto_tree_add_item (fcct_tree, hf_fcct_options, tvb, offset++,
215                              sizeof (guint8), 0);
216
217     }
218     /* We do not change the starting offset for the next protocol in the
219      * chain since the fc_ct header is common to the sub-protocols.
220      */
221     next_tvb = tvb_new_subset (tvb, 0, -1, -1);
222     if (!dissector_try_port (fcct_gserver_table, server, next_tvb, pinfo,
223                              tree)) {
224         call_dissector (data_handle, next_tvb, pinfo, tree);
225     }
226 }
227
228 /* Register the protocol with Ethereal */
229
230 /* this format is require because a script is used to build the C function
231    that calls all the protocol registration.
232 */
233
234 void
235 proto_register_fcct(void)
236 {                 
237
238 /* Setup list of header fields  See Section 1.6.1 for details*/
239     static hf_register_info hf[] = {
240         { &hf_fcct_revision,
241           {"Revision", "fcct.revision", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL}}, 
242         { &hf_fcct_inid,
243           {"IN_ID", "fcct.in_id", FT_STRING, BASE_HEX, NULL, 0x0, "", HFILL}},
244         { &hf_fcct_gstype,
245           {"GS Type", "fcct.gstype", FT_UINT8, BASE_HEX, VALS(fc_ct_gstype_vals),
246            0x0, "", HFILL}},
247         { &hf_fcct_gssubtype,
248           {"GS Subtype", "fcct.gssubtype", FT_UINT8, BASE_HEX, NULL, 0x0,
249            "", HFILL}},
250         { &hf_fcct_server,
251           {"Server", "fcct.server", FT_UINT8, BASE_HEX,
252            VALS (fc_ct_gsserver_vals), 0x0,
253            "Derived from GS Type & Subtype fields", HFILL}},
254         { &hf_fcct_options,
255           {"Options", "fcct.options", FT_UINT8, BASE_HEX, NULL, 0x0, "",
256            HFILL}},
257         { &hf_fcct_ext_said,
258           {"Auth SAID", "fcct.ext_said", FT_UINT32, BASE_HEX, NULL, 0x0, "",
259            HFILL}},
260         { &hf_fcct_ext_tid,
261           {"Transaction ID", "fcct.ext_tid", FT_UINT32, BASE_HEX, NULL, 0x0, "",
262            HFILL}},
263         { &hf_fcct_ext_reqname,
264           {"Requestor Port Name", "fcct_ext_reqnm", FT_BYTES, BASE_HEX, NULL,
265            0x0, "", HFILL}},
266         { &hf_fcct_ext_tstamp,
267           {"Timestamp", "fcct_ext_tstamp", FT_BYTES, BASE_HEX, NULL, 0x0, "",
268            HFILL}},
269         { &hf_fcct_ext_authblk,
270           {"Auth Hash Blk", "fcct_ext_authblk", FT_BYTES, BASE_HEX, NULL, 0x0,
271            "", HFILL}},
272     };
273
274     /* Setup protocol subtree array */
275     static gint *ett[] = {
276         &ett_fcct,
277         &ett_fcct_ext,
278     };
279
280     /* Register the protocol name and description */
281     proto_fcct = proto_register_protocol("Fibre Channel Common Transport", "FC_CT", "fcct");
282
283     /* Required function calls to register the header fields and subtrees used */
284     proto_register_field_array(proto_fcct, hf, array_length(hf));
285     proto_register_subtree_array(ett, array_length(ett));
286
287     fcct_gserver_table = register_dissector_table ("fcct.server",
288                                                    "Server",
289                                                    FT_UINT8, BASE_HEX);
290 }
291
292 /* If this dissector uses sub-dissector registration add a registration routine.
293    This format is required because a script is used to find these routines and
294    create the code that calls these routines.
295 */
296 void
297 proto_reg_handoff_fcct (void)
298 {
299     dissector_handle_t fcct_handle;
300
301     fcct_handle = create_dissector_handle (dissect_fcct, proto_fcct);
302     dissector_add("fc.ftype", FC_FTYPE_FCCT, fcct_handle);
303
304     data_handle = find_dissector ("data");
305 }
306
307