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