#include <stdio.h> not needed.
[obnox/wireshark/wip.git] / asn1 / dsp / packet-dsp-template.c
1 /* packet-dsp.c
2  * Routines for X.518 (X.500 Distributed Operations)  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
42 #include "packet-x509if.h"
43 #include "packet-x509af.h"
44 #include "packet-x509sat.h"
45
46 #include "packet-dap.h"
47 #include "packet-dsp.h"
48
49
50 #define PNAME  "X.519 Directory System Protocol"
51 #define PSNAME "DSP"
52 #define PFNAME "dsp"
53
54 static guint global_dsp_tcp_port = 102;
55 static dissector_handle_t tpkt_handle;
56 void prefs_register_dsp(void); /* forward declaration for use in preferences registration */
57
58
59 /* Initialize the protocol and registered fields */
60 static int proto_dsp = -1;
61
62 static struct SESSION_DATA_STRUCTURE* session = NULL;
63
64 #include "packet-dsp-hf.c"
65
66 /* Initialize the subtree pointers */
67 static gint ett_dsp = -1;
68 #include "packet-dsp-ett.c"
69
70 #include "packet-dsp-fn.c"
71
72 /*
73 * Dissect X518 PDUs inside a ROS PDUs
74 */
75 static void
76 dissect_dsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
77 {
78         int offset = 0;
79         int old_offset;
80         proto_item *item=NULL;
81         proto_tree *tree=NULL;
82         int (*dsp_dissector)(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset, asn1_ctx_t *actx, proto_tree *tree, int hf_index _U_) = NULL;
83         char *dsp_op_name;
84         asn1_ctx_t asn1_ctx;
85
86         asn1_ctx_init(&asn1_ctx, ASN1_ENC_BER, TRUE, pinfo);
87
88         /* do we have operation information from the ROS dissector?  */
89         if( !pinfo->private_data ){
90                 if(parent_tree){
91                         proto_tree_add_text(parent_tree, tvb, offset, -1,
92                                 "Internal error: can't get operation information from ROS dissector.");
93                 } 
94                 return  ;
95         } else {
96                 session  = ( (struct SESSION_DATA_STRUCTURE*)(pinfo->private_data) );
97         }
98
99         if(parent_tree){
100                 item = proto_tree_add_item(parent_tree, proto_dsp, tvb, 0, -1, FALSE);
101                 tree = proto_item_add_subtree(item, ett_dsp);
102         }
103         col_set_str(pinfo->cinfo, COL_PROTOCOL, "DAP");
104         col_clear(pinfo->cinfo, COL_INFO);
105
106         switch(session->ros_op & ROS_OP_MASK) {
107         case (ROS_OP_BIND | ROS_OP_ARGUMENT):   /*  BindInvoke */
108           dsp_dissector = dissect_dsp_DSASystemBindArgument;
109           dsp_op_name = "System-Bind-Argument";
110           break;
111         case (ROS_OP_BIND | ROS_OP_RESULT):     /*  BindResult */
112           dsp_dissector = dissect_dsp_DSASystemBindResult;
113           dsp_op_name = "System-Bind-Result";
114           break;
115         case (ROS_OP_BIND | ROS_OP_ERROR):      /*  BindError */
116           dsp_dissector = dissect_dsp_DSASystemBindError;
117           dsp_op_name = "System-Bind-Error";
118           break;
119         case (ROS_OP_INVOKE | ROS_OP_ARGUMENT): /*  Invoke Argument */
120           switch(session->ros_op & ROS_OP_OPCODE_MASK) {
121           case 1: /* read */
122             dsp_dissector = dissect_dsp_ChainedReadArgument;
123             dsp_op_name = "Chained-Read-Argument";
124             break;
125           case 2: /* compare */
126             dsp_dissector = dissect_dsp_ChainedCompareArgument;
127             dsp_op_name = "Chained-Compare-Argument";
128             break;
129           case 3: /* abandon */
130             dsp_dissector = dissect_dsp_ChainedAbandonArgument;
131             dsp_op_name = "Chained-Abandon-Argument";
132             break;
133           case 4: /* list */
134             dsp_dissector = dissect_dsp_ChainedListArgument;
135             dsp_op_name = "Chained-List-Argument";
136             break;
137           case 5: /* search */
138             dsp_dissector = dissect_dsp_ChainedSearchArgument;
139             dsp_op_name = "Chained-Search-Argument";
140             break;
141           case 6: /* addEntry */
142             dsp_dissector = dissect_dsp_ChainedAddEntryArgument;
143             dsp_op_name = "Chained-Add-Entry-Argument";
144             break;
145           case 7: /* removeEntry */
146             dsp_dissector = dissect_dsp_ChainedRemoveEntryArgument;
147             dsp_op_name = "Chained-Remove-Entry-Argument";
148             break;
149           case 8: /* modifyEntry */
150             dsp_dissector = dissect_dsp_ChainedModifyEntryArgument;
151             dsp_op_name = "ChainedModify-Entry-Argument";
152             break;
153           case 9: /* modifyDN */
154             dsp_dissector = dissect_dsp_ChainedModifyDNArgument;
155             dsp_op_name = "ChainedModify-DN-Argument";
156             break;
157           default:
158             proto_tree_add_text(tree, tvb, offset, -1,"Unsupported DSP opcode (%d)",
159                                 session->ros_op & ROS_OP_OPCODE_MASK);
160             break;
161           }
162           break;
163         case (ROS_OP_INVOKE | ROS_OP_RESULT):   /*  Return Result */
164           switch(session->ros_op & ROS_OP_OPCODE_MASK) {
165           case 1: /* read */
166             dsp_dissector = dissect_dsp_ChainedReadResult;
167             dsp_op_name = "Chained-Read-Result";
168             break;
169           case 2: /* compare */
170             dsp_dissector = dissect_dsp_ChainedCompareResult;
171             dsp_op_name = "Chained-Compare-Result";
172             break;
173           case 3: /* abandon */
174             dsp_dissector = dissect_dsp_ChainedAbandonResult;
175             dsp_op_name = "Chained-Abandon-Result";
176             break;
177           case 4: /* list */
178             dsp_dissector = dissect_dsp_ChainedListResult;
179             dsp_op_name = "Chained-List-Result";
180             break;
181           case 5: /* search */
182             dsp_dissector = dissect_dsp_ChainedSearchResult;
183             dsp_op_name = "Chained-Search-Result";
184             break;
185           case 6: /* addEntry */
186             dsp_dissector = dissect_dsp_ChainedAddEntryResult;
187             dsp_op_name = "Chained-Add-Entry-Result";
188             break;
189           case 7: /* removeEntry */
190             dsp_dissector = dissect_dsp_ChainedRemoveEntryResult;
191             dsp_op_name = "Chained-Remove-Entry-Result";
192             break;
193           case 8: /* modifyEntry */
194             dsp_dissector = dissect_dsp_ChainedModifyEntryResult;
195             dsp_op_name = "Chained-Modify-Entry-Result";
196             break;
197           case 9: /* modifyDN */
198             dsp_dissector = dissect_dsp_ChainedModifyDNResult;
199             dsp_op_name = "ChainedModify-DN-Result";
200             break;
201           default:
202             proto_tree_add_text(tree, tvb, offset, -1,"Unsupported DSP opcode");
203             break;
204           }
205           break;
206         case (ROS_OP_INVOKE | ROS_OP_ERROR):    /*  Return Error */
207           switch(session->ros_op & ROS_OP_OPCODE_MASK) {
208           case 1: /* attributeError */
209             dsp_dissector = dissect_dap_AttributeError;
210             dsp_op_name = "Attribute-Error";
211             break;
212           case 2: /* nameError */
213             dsp_dissector = dissect_dap_NameError;
214             dsp_op_name = "Name-Error";
215             break;
216           case 3: /* serviceError */
217             dsp_dissector = dissect_dap_ServiceError;
218             dsp_op_name = "Service-Error";
219             break;
220           case 4: /* referral */
221             dsp_dissector = dissect_dap_Referral;
222             dsp_op_name = "Referral";
223             break;
224           case 5: /* abandoned */
225             dsp_dissector = dissect_dap_Abandoned;
226             dsp_op_name = "Abandoned";
227             break;
228           case 6: /* securityError */
229             dsp_dissector = dissect_dap_SecurityError;
230             dsp_op_name = "Security-Error";
231             break;
232           case 7: /* abandonFailed */
233             dsp_dissector = dissect_dap_AbandonFailedError;
234             dsp_op_name = "Abandon-Failed-Error";
235             break;
236           case 8: /* updateError */
237             dsp_dissector = dissect_dap_UpdateError;
238             dsp_op_name = "Update-Error";
239             break;
240           case 9: /* DSAReferral */
241             dsp_dissector = dissect_dsp_DSAReferral;
242             dsp_op_name = "DSA-Referral";
243             break;
244           default:
245             proto_tree_add_text(tree, tvb, offset, -1,"Unsupported DSP errcode");
246             break;
247           }
248           break;
249         default:
250           proto_tree_add_text(tree, tvb, offset, -1,"Unsupported DSP PDU");
251           return;
252         }
253
254         if(dsp_dissector) {
255     col_set_str(pinfo->cinfo, COL_INFO, dsp_op_name);
256
257           while (tvb_reported_length_remaining(tvb, offset) > 0){
258             old_offset=offset;
259             offset=(*dsp_dissector)(FALSE, tvb, offset, &asn1_ctx, tree, -1);
260             if(offset == old_offset){
261               proto_tree_add_text(tree, tvb, offset, -1,"Internal error, zero-byte DSP PDU");
262               break;
263             }
264           }
265         }
266 }
267
268
269 /*--- proto_register_dsp -------------------------------------------*/
270 void proto_register_dsp(void) {
271
272   /* List of fields */
273   static hf_register_info hf[] =
274   {
275 #include "packet-dsp-hfarr.c"
276   };
277
278   /* List of subtrees */
279   static gint *ett[] = {
280     &ett_dsp,
281 #include "packet-dsp-ettarr.c"
282   };
283   module_t *dsp_module;
284
285   /* Register protocol */
286   proto_dsp = proto_register_protocol(PNAME, PSNAME, PFNAME);
287
288   register_dissector("dsp", dissect_dsp, proto_dsp);
289
290   /* Register fields and subtrees */
291   proto_register_field_array(proto_dsp, hf, array_length(hf));
292   proto_register_subtree_array(ett, array_length(ett));
293
294   /* Register our configuration options for DSP, particularly our port */
295
296   dsp_module = prefs_register_protocol_subtree("OSI/X.500", proto_dsp, prefs_register_dsp);
297
298   prefs_register_uint_preference(dsp_module, "tcp.port", "DSP TCP Port",
299                                  "Set the port for DSP operations (if other"
300                                  " than the default of 102)",
301                                  10, &global_dsp_tcp_port);
302
303
304 }
305
306
307 /*--- proto_reg_handoff_dsp --- */
308 void proto_reg_handoff_dsp(void) {
309   dissector_handle_t dsp_handle;
310
311 #include "packet-dsp-dis-tab.c" 
312
313   /* APPLICATION CONTEXT */
314
315   oid_add_from_string("id-ac-directory-system","2.5.3.2");
316
317   /* ABSTRACT SYNTAXES */
318     
319   /* remember the tpkt handler for change in preferences */
320   tpkt_handle = find_dissector("tpkt");
321
322   /* Register DSP with ROS (with no use of RTSE) */
323   dsp_handle = find_dissector("dsp");
324   register_ros_oid_dissector_handle("2.5.9.2", dsp_handle, 0, "id-as-directory-system", FALSE); 
325
326 }
327
328 void prefs_register_dsp(void) {
329   static guint tcp_port = 0;
330
331   /* de-register the old port */
332   /* port 102 is registered by TPKT - don't undo this! */
333   if((tcp_port > 0) && (tcp_port != 102) && tpkt_handle)
334     dissector_delete("tcp.port", tcp_port, tpkt_handle);
335
336   /* Set our port number for future use */
337   tcp_port = global_dsp_tcp_port;
338
339   if((tcp_port > 0) && (tcp_port != 102) && tpkt_handle)
340     dissector_add("tcp.port", global_dsp_tcp_port, tpkt_handle);
341
342 }