Rename the routines that handle dissector tables with unsigned integer
[obnox/wireshark/wip.git] / asn1 / disp / packet-disp-template.c
1 /* packet-disp.c
2  * Routines for X.525 (X.500 Directory Shadow Asbtract Service) and X.519 DISP packet dissection
3  * Graeme Lunt 2005
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
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 <glib.h>
31 #include <epan/packet.h>
32 #include <epan/prefs.h>
33 #include <epan/oids.h>
34 #include <epan/asn1.h>
35
36 #include "packet-ber.h"
37 #include "packet-acse.h"
38 #include "packet-ros.h"
39 #include "packet-rtse.h"
40
41 #include "packet-x509if.h"
42 #include "packet-x509af.h"
43 #include "packet-x509sat.h"
44 #include "packet-crmf.h"
45
46 #include "packet-dop.h"
47 #include "packet-dap.h"
48 #include "packet-dsp.h"
49 #include "packet-disp.h"
50
51
52 /* we don't have a separate dissector for X519 -
53    and most of DISP is defined in X525 */
54 #define PNAME  "X.519 Directory Information Shadowing Protocol"
55 #define PSNAME "DISP"
56 #define PFNAME "disp"
57
58 static guint global_disp_tcp_port = 102;
59 static dissector_handle_t tpkt_handle;
60 static void prefs_register_disp(void); /* forward declaration for use in preferences registration */
61
62
63 /* Initialize the protocol and registered fields */
64 static int proto_disp = -1;
65
66 static struct SESSION_DATA_STRUCTURE* session = NULL;
67
68 #include "packet-disp-hf.c"
69
70 /* Initialize the subtree pointers */
71 static gint ett_disp = -1;
72 #include "packet-disp-ett.c"
73
74 #include "packet-disp-fn.c"
75
76 /*
77 * Dissect DISP PDUs inside a ROS PDUs
78 */
79 static void
80 dissect_disp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
81 {
82         int offset = 0;
83         int old_offset;
84         proto_item *item=NULL;
85         proto_tree *tree=NULL;
86         int (*disp_dissector)(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index _U_) = NULL;
87         char *disp_op_name;
88         asn1_ctx_t asn1_ctx;
89
90         asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
91
92         /* do we have operation information from the ROS dissector?  */
93         if( !pinfo->private_data ){
94                 if(parent_tree){
95                         proto_tree_add_text(parent_tree, tvb, offset, -1,
96                                 "Internal error: can't get operation information from ROS dissector.");
97                 }
98                 return  ;
99         } else {
100                 session  = ( (struct SESSION_DATA_STRUCTURE*)(pinfo->private_data) );
101         }
102
103         if(parent_tree){
104                 item = proto_tree_add_item(parent_tree, proto_disp, tvb, 0, -1, FALSE);
105                 tree = proto_item_add_subtree(item, ett_disp);
106         }
107         col_set_str(pinfo->cinfo, COL_PROTOCOL, "DISP");
108         col_clear(pinfo->cinfo, COL_INFO);
109
110         switch(session->ros_op & ROS_OP_MASK) {
111         case (ROS_OP_BIND | ROS_OP_ARGUMENT):   /*  BindInvoke */
112           disp_dissector = dissect_disp_DSAShadowBindArgument;
113           disp_op_name = "Shadow-Bind-Argument";
114           break;
115         case (ROS_OP_BIND | ROS_OP_RESULT):     /*  BindResult */
116           disp_dissector = dissect_disp_DSAShadowBindResult;
117           disp_op_name = "Shadow-Bind-Result";
118           break;
119         case (ROS_OP_BIND | ROS_OP_ERROR):      /*  BindError */
120           disp_dissector = dissect_disp_DSAShadowBindError;
121           disp_op_name = "Shadow-Bind-Error";
122           break;
123         case (ROS_OP_INVOKE | ROS_OP_ARGUMENT): /*  Invoke Argument */
124           switch(session->ros_op & ROS_OP_OPCODE_MASK) {
125           case 1: /* requestShadowUpdate */
126             disp_dissector = dissect_disp_RequestShadowUpdateArgument;
127             disp_op_name = "Request-Shadow-Update-Argument";
128             break;
129           case 2: /* updateShadow*/
130             disp_dissector = dissect_disp_UpdateShadowArgument;
131             disp_op_name = "Update-Shadow-Argument";
132             break;
133           case 3: /* coordinateShadowUpdate */
134             disp_dissector = dissect_disp_CoordinateShadowUpdateArgument;
135             disp_op_name = "Coordinate-Shadow-Update-Argument";
136             break;
137           default:
138             proto_tree_add_text(tree, tvb, offset, -1,"Unsupported DISP opcode (%d)",
139                                 session->ros_op & ROS_OP_OPCODE_MASK);
140             break;
141           }
142           break;
143         case (ROS_OP_INVOKE | ROS_OP_RESULT):   /*  Return Result */
144           switch(session->ros_op & ROS_OP_OPCODE_MASK) {
145           case 1: /* requestShadowUpdate */
146             disp_dissector = dissect_disp_RequestShadowUpdateResult;
147             disp_op_name = "Request-Shadow-Result";
148             break;
149           case 2: /* updateShadow */
150             disp_dissector = dissect_disp_UpdateShadowResult;
151             disp_op_name = "Update-Shadow-Result";
152             break;
153           case 3: /* coordinateShadowUpdate */
154             disp_dissector = dissect_disp_CoordinateShadowUpdateResult;
155             disp_op_name = "Coordinate-Shadow-Update-Result";
156             break;
157           default:
158             proto_tree_add_text(tree, tvb, offset, -1,"Unsupported DISP opcode (%d)",
159                                 session->ros_op & ROS_OP_OPCODE_MASK);
160             break;
161           }
162           break;
163         case (ROS_OP_INVOKE | ROS_OP_ERROR):    /*  Return Error */
164           switch(session->ros_op & ROS_OP_OPCODE_MASK) {
165           case 1: /* shadowError */
166             disp_dissector = dissect_disp_ShadowError;
167             disp_op_name = "Shadow-Error";
168             break;
169           default:
170             proto_tree_add_text(tree, tvb, offset, -1,"Unsupported DISP errcode (%d)",
171                                 session->ros_op & ROS_OP_OPCODE_MASK);
172             break;
173           }
174           break;
175         default:
176           proto_tree_add_text(tree, tvb, offset, -1,"Unsupported DISP PDU");
177           return;
178         }
179
180         if(disp_dissector) {
181           col_set_str(pinfo->cinfo, COL_INFO, disp_op_name);
182
183           while (tvb_reported_length_remaining(tvb, offset) > 0){
184             old_offset=offset;
185             offset=(*disp_dissector)(FALSE, tvb, offset, &asn1_ctx, tree, -1);
186             if(offset == old_offset){
187               proto_tree_add_text(tree, tvb, offset, -1,"Internal error, zero-byte DISP PDU");
188               offset = tvb_length(tvb);
189               break;
190             }
191           }
192         }
193 }
194
195
196 /*--- proto_register_disp -------------------------------------------*/
197 void proto_register_disp(void) {
198
199   /* List of fields */
200   static hf_register_info hf[] =
201   {
202 #include "packet-disp-hfarr.c"
203   };
204
205   /* List of subtrees */
206   static gint *ett[] = {
207     &ett_disp,
208 #include "packet-disp-ettarr.c"
209   };
210   module_t *disp_module;
211
212   /* Register protocol */
213   proto_disp = proto_register_protocol(PNAME, PSNAME, PFNAME);
214   register_dissector("disp", dissect_disp, proto_disp);
215
216   /* Register fields and subtrees */
217   proto_register_field_array(proto_disp, hf, array_length(hf));
218   proto_register_subtree_array(ett, array_length(ett));
219
220   /* Register our configuration options for DISP, particularly our port */
221
222   disp_module = prefs_register_protocol_subtree("OSI/X.500", proto_disp, prefs_register_disp);
223
224   prefs_register_uint_preference(disp_module, "tcp.port", "DISP TCP Port",
225                                  "Set the port for DISP operations (if other"
226                                  " than the default of 102)",
227                                  10, &global_disp_tcp_port);
228
229 }
230
231
232 /*--- proto_reg_handoff_disp --- */
233 void proto_reg_handoff_disp(void) {
234   dissector_handle_t disp_handle;
235
236   #include "packet-disp-dis-tab.c"
237
238   /* APPLICATION CONTEXT */
239
240   oid_add_from_string("id-ac-shadow-consumer-initiated","2.5.3.4");
241   oid_add_from_string("id-ac-shadow-supplier-initiated","2.5.3.5");
242   oid_add_from_string("id-ac-reliable-shadow-consumer-initiated","2.5.3.6");
243   oid_add_from_string("id-ac-reliable-shadow-supplier-initiated","2.5.3.7");
244
245   /* ABSTRACT SYNTAXES */
246
247   disp_handle = find_dissector("disp");
248
249   register_ros_oid_dissector_handle("2.5.9.3", disp_handle, 0, "id-as-directory-shadow", FALSE);
250   register_rtse_oid_dissector_handle("2.5.9.5", disp_handle, 0, "id-as-directory-reliable-shadow", FALSE);
251   register_rtse_oid_dissector_handle("2.5.9.6", disp_handle, 0, "id-as-directory-reliable-binding", FALSE);
252
253   /* OPERATIONAL BINDING */
254   oid_add_from_string("id-op-binding-shadow","2.5.1.0.5.1");
255
256   tpkt_handle = find_dissector("tpkt");
257
258   /* DNs */
259   x509if_register_fmt(hf_disp_contextPrefix, "cp=");
260
261 }
262
263
264 static void
265 prefs_register_disp(void)
266 {
267   static guint tcp_port = 0;
268
269   /* de-register the old port */
270   /* port 102 is registered by TPKT - don't undo this! */
271   if((tcp_port > 0) && (tcp_port != 102) && tpkt_handle)
272     dissector_delete_uint("tcp.port", tcp_port, tpkt_handle);
273
274   /* Set our port number for future use */
275   tcp_port = global_disp_tcp_port;
276
277   if((tcp_port > 0) && (tcp_port != 102) && tpkt_handle)
278     dissector_add_uint("tcp.port", global_disp_tcp_port, tpkt_handle);
279
280 }