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