29a118b513d70be23530512dd698d1a72f88c13f
[metze/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  * 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 <glib.h>
31 #include <epan/packet.h>
32 #include <epan/prefs.h>
33 #include <epan/conversation.h>
34
35 #include <stdio.h>
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 guint tcp_port = 0;
56 static dissector_handle_t tpkt_handle = NULL;
57 void prefs_register_dsp(void); /* forwad declaration for use in preferences registration */
58
59
60 /* Initialize the protocol and registered fields */
61 int proto_dsp = -1;
62
63 static struct SESSION_DATA_STRUCTURE* session = NULL;
64
65 #include "packet-dsp-hf.c"
66
67 /* Initialize the subtree pointers */
68 static gint ett_dsp = -1;
69 #include "packet-dsp-ett.c"
70
71 #include "packet-dsp-fn.c"
72
73 /*
74 * Dissect X518 PDUs inside a ROS PDUs
75 */
76 static void
77 dissect_dsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree)
78 {
79         int offset = 0;
80         int old_offset;
81         proto_item *item=NULL;
82         proto_tree *tree=NULL;
83         int (*dsp_dissector)(gboolean implicit_tag _U_, tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree, int hf_index _U_) = NULL;
84         char *dsp_op_name;
85
86         /* do we have operation information from the ROS dissector?  */
87         if( !pinfo->private_data ){
88                 if(parent_tree){
89                         proto_tree_add_text(parent_tree, tvb, offset, -1,
90                                 "Internal error: can't get operation information from ROS dissector.");
91                 } 
92                 return  ;
93         } else {
94                 session  = ( (struct SESSION_DATA_STRUCTURE*)(pinfo->private_data) );
95         }
96
97         if(parent_tree){
98                 item = proto_tree_add_item(parent_tree, proto_dsp, tvb, 0, -1, FALSE);
99                 tree = proto_item_add_subtree(item, ett_dsp);
100         }
101         if (check_col(pinfo->cinfo, COL_PROTOCOL))
102                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "DAP");
103         if (check_col(pinfo->cinfo, COL_INFO))
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           if (check_col(pinfo->cinfo, COL_INFO))
256             col_add_str(pinfo->cinfo, COL_INFO, dsp_op_name);
257
258           while (tvb_reported_length_remaining(tvb, offset) > 0){
259             old_offset=offset;
260             offset=(*dsp_dissector)(FALSE, tvb, offset, pinfo , tree, -1);
261             if(offset == old_offset){
262               proto_tree_add_text(tree, tvb, offset, -1,"Internal error, zero-byte DSP PDU");
263               offset = tvb_length(tvb);
264               break;
265             }
266           }
267         }
268 }
269
270
271 /*--- proto_register_dsp -------------------------------------------*/
272 void proto_register_dsp(void) {
273
274   /* List of fields */
275   static hf_register_info hf[] =
276   {
277 #include "packet-dsp-hfarr.c"
278   };
279
280   /* List of subtrees */
281   static gint *ett[] = {
282     &ett_dsp,
283 #include "packet-dsp-ettarr.c"
284   };
285   module_t *dsp_module;
286
287   /* Register protocol */
288   proto_dsp = proto_register_protocol(PNAME, PSNAME, PFNAME);
289
290   /* initially disable the protocol */
291   proto_set_decoding(proto_dsp, FALSE);
292
293   register_dissector("dsp", dissect_dsp, proto_dsp);
294
295   /* Register fields and subtrees */
296   proto_register_field_array(proto_dsp, hf, array_length(hf));
297   proto_register_subtree_array(ett, array_length(ett));
298
299   /* Register our configuration options for DSP, particularly our port */
300
301 #ifdef PREFERENCE_GROUPING
302   dsp_module = prefs_register_protocol_subtree("OSI/X.500", proto_dsp, prefs_register_dsp);
303 #else
304   dsp_module = prefs_register_protocol(proto_dsp, prefs_register_dsp);
305 #endif
306
307   prefs_register_uint_preference(dsp_module, "tcp.port", "DSP TCP Port",
308                                  "Set the port for DSP operations (if other"
309                                  " than the default of 102)",
310                                  10, &global_dsp_tcp_port);
311
312
313 }
314
315
316 /*--- proto_reg_handoff_dsp --- */
317 void proto_reg_handoff_dsp(void) {
318   dissector_handle_t handle = NULL;
319
320 #include "packet-dsp-dis-tab.c" 
321
322   /* APPLICATION CONTEXT */
323
324   register_ber_oid_name("2.5.3.2", "id-ac-directory-system");
325
326   /* ABSTRACT SYNTAXES */
327     
328   /* Register DSP with ROS (with no use of RTSE) */
329   if((handle = find_dissector("dsp"))) {
330     register_ros_oid_dissector_handle("2.5.9.2", handle, 0, "id-as-directory-system", FALSE); 
331   }
332
333
334 }
335
336 void prefs_register_dsp(void) {
337
338   /* de-register the old port */
339   /* port 102 is registered by TPKT - don't undo this! */
340   if((tcp_port != 102) && tpkt_handle)
341     dissector_delete("tcp.port", tcp_port, tpkt_handle);
342
343   /* Set our port number for future use */
344   tcp_port = global_dsp_tcp_port;
345
346   if((tcp_port > 0) && (tcp_port != 102) && tpkt_handle)
347     dissector_add("tcp.port", global_dsp_tcp_port, tpkt_handle);
348
349 }