5 # wireshark_gen.py (part of idl2wrs)
7 # Author : Frank Singleton (frank.singleton@ericsson.com)
9 # Copyright (C) 2001 Frank Singleton, Ericsson Inc.
11 # This file is a backend to "omniidl", used to generate "Wireshark"
12 # dissectors from CORBA IDL descriptions. The output language generated
13 # is "C". It will generate code to use the GIOP/IIOP get_CDR_XXX API.
15 # Please see packet-giop.h in Wireshark distro for API description.
16 # Wireshark is available at http://www.wiresharl.org/
18 # Omniidl is part of the OmniOrb distribution, and is available at
19 # http://www.uk.research.att.com/omniORB/omniORB.html
21 # This program is free software; you can redistribute it and/or modify it
22 # under the terms of the GNU General Public License as published by
23 # the Free Software Foundation; either version 2 of the License, or
24 # (at your option) any later version.
26 # This program is distributed in the hope that it will be useful,
27 # but WITHOUT ANY WARRANTY; without even the implied warranty of
28 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 # General Public License for more details.
31 # You should have received a copy of the GNU General Public License
32 # along with this program; if not, write to the Free Software
33 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
38 # Omniidl Back-end which parses an IDL list of "Operation" nodes
39 # passed from wireshark_be2.py and generates "C" code for compiling
40 # as a plugin for the Wireshark IP Protocol Analyser.
43 # Strategy (sneaky but ...)
45 # problem: I dont know what variables to declare until AFTER the helper functions
46 # have been built, so ...
48 # There are 2 passes through genHelpers, the first one is there just to
49 # make sure the fn_hash data struct is populated properly.
50 # The second pass is the real thing, generating code and declaring
51 # variables (from the 1st pass) properly.
55 """Wireshark IDL compiler back-end."""
57 from omniidl import idlast, idltype, idlutil, output
62 # Output class, generates "C" src code for the sub-dissector
69 # node - a reference to an Operations object.
70 # name - scoped name (Module::Module::Interface:: .. ::Operation
78 # 1. generate hf[] data for searchable fields (but what is searchable?)
79 # 2. add item instead of add_text()
80 # 3. sequence handling [done]
81 # 4. User Exceptions [done]
82 # 5. Fix arrays, and structs containing arrays [done]
84 # 7. Exception can be common to many operations, so handle them outside the
85 # operation helper functions [done]
86 # 8. Automatic variable declaration [done, improve, still get some collisions.add variable delegator function ]
87 # For example, mutlidimensional arrays.
88 # 9. wchar and wstring handling [giop API needs improving]
89 # 10. Support Fixed [done]
90 # 11. Support attributes (get/set) [started, needs language mapping option, perhaps wireshark GUI option
91 # to set the attribute function prefix or suffix ? ] For now the prefix is "_get" and "_set"
92 # eg: attribute string apple => _get_apple and _set_apple
94 # 12. Implement IDL "union" code [done]
95 # 13. Implement support for plugins [done]
96 # 14. Dont generate code for empty operations (cf: exceptions without members)
97 # 15. Generate code to display Enums numerically and symbolically [done]
98 # 16. Place structs/unions in subtrees
99 # 17. Recursive struct and union handling [done ]
100 # 18. Improve variable naming for display (eg: structs, unions etc)
102 # Also test, Test, TEST
109 # For every operation and attribute do
110 # For return val and all parameters do
111 # find basic IDL type for each parameter
113 # output exception handling code
114 # output attribute handling code
118 class wireshark_gen_C:
122 # Turn DEBUG stuff on/off
128 # Some string constants for our templates
130 c_u_octet8 = "guint64 u_octet8;"
131 c_s_octet8 = "gint64 s_octet8;"
132 c_u_octet4 = "guint32 u_octet4;"
133 c_s_octet4 = "gint32 s_octet4;"
134 c_u_octet2 = "guint16 u_octet2;"
135 c_s_octet2 = "gint16 s_octet2;"
136 c_u_octet1 = "guint8 u_octet1;"
137 c_s_octet1 = "gint8 s_octet1;"
139 c_float = "gfloat my_float;"
140 c_double = "gdouble my_double;"
142 c_seq = "gchar *seq = NULL;" # pointer to buffer of gchars
143 c_i = "guint32 i_"; # loop index
144 c_i_lim = "guint32 u_octet4_loop_"; # loop limit
145 c_u_disc = "guint32 disc_u_"; # unsigned int union discriminant variable name (enum)
146 c_s_disc = "gint32 disc_s_"; # signed int union discriminant variable name (other cases, except Enum)
152 def __init__(self, st, protocol_name, dissector_name ,description):
153 self.st = output.Stream(tempfile.TemporaryFile(),4) # for first pass only
155 self.st_save = st # where 2nd pass should go
156 self.protoname = protocol_name # Protocol Name (eg: ECHO)
157 self.dissname = dissector_name # Dissector name (eg: echo)
158 self.description = description # Detailed Protocol description (eg: Echo IDL Example)
159 self.exlist = [] # list of exceptions used in operations.
160 #self.curr_sname # scoped name of current opnode or exnode I am visiting, used for generating "C" var declares
161 self.fn_hash = {} # top level hash to contain key = function/exception and val = list of variable declarations
163 self.fn_hash_built = 0 # flag to indicate the 1st pass is complete, and the fn_hash is correctly
164 # populated with operations/vars and exceptions/vars
170 # Main entry point, controls sequence of
175 def genCode(self,oplist, atlist, enlist, stlist, unlist): # operation,attribute,enums,struct and union lists
178 self.genHelpers(oplist,stlist,unlist) # sneaky .. call it now, to populate the fn_hash
179 # so when I come to that operation later, I have the variables to
182 self.genExceptionHelpers(oplist) # sneaky .. call it now, to populate the fn_hash
183 # so when I come to that exception later, I have the variables to
186 self.genAttributeHelpers(atlist) # sneaky .. call it now, to populate the fn_hash
187 # so when I come to that exception later, I have the variables to
191 self.fn_hash_built = 1 # DONE, so now I know , see genOperation()
193 self.st = self.st_save
194 self.genHeader() # initial dissector comments
195 self.genEthCopyright() # Wireshark Copyright comments.
196 self.genGPL() # GPL license
198 self.genDeclares(oplist,atlist,enlist,stlist,unlist)
200 self.genRegisteredFields()
201 self.genOpList(oplist) # string constant declares for operation names
202 self.genExList(oplist) # string constant declares for user exceptions
203 self.genAtList(atlist) # string constant declares for Attributes
204 self.genEnList(enlist) # string constant declares for Enums
207 self.genExceptionHelpers(oplist) # helper function to decode user exceptions that have members
208 self.genExceptionDelegator(oplist) # finds the helper function to decode a user exception
209 self.genAttributeHelpers(atlist) # helper function to decode "attributes"
211 self.genHelpers(oplist,stlist,unlist) # operation, struct and union decode helper functions
213 self.genMainEntryStart(oplist)
214 self.genOpDelegator(oplist)
215 self.genAtDelegator(atlist)
216 self.genMainEntryEnd()
218 self.gen_proto_register()
219 self.gen_proto_reg_handoff(oplist)
220 self.gen_plugin_register()
222 #self.dumpvars() # debug
229 # Generate Standard Wireshark Header Comments
234 self.st.out(self.template_Header,dissector_name=self.dissname)
236 print "XXX genHeader"
244 # Wireshark Copyright Info
248 def genEthCopyright(self):
250 print "XXX genEthCopyright"
251 self.st.out(self.template_wireshark_copyright)
265 self.st.out(self.template_GPL)
274 def genIncludes(self):
276 print "XXX genIncludes"
278 self.st.out(self.template_Includes)
284 # generate function prototypes if required
286 # Currently this is used for struct and union helper function declarations.
290 def genDeclares(self,oplist,atlist,enlist,stlist,unlist):
292 print "XXX genDeclares"
294 # prototype for start_dissecting()
296 self.st.out(self.template_prototype_start_dissecting)
300 self.st.out(self.template_prototype_struct_start)
303 sname = self.namespace(st, "_")
305 self.st.out(self.template_prototype_struct_body, stname=st.repoId(),name=sname)
306 self.st.out(self.template_prototype_struct_end)
310 self.st.out(self.template_prototype_union_start)
312 sname = self.namespace(un, "_")
313 self.st.out(self.template_prototype_union_body, unname=un.repoId(),name=sname)
314 self.st.out(self.template_prototype_union_end)
324 def genProtocol(self):
325 self.st.out(self.template_protocol, dissector_name=self.dissname)
326 self.st.out(self.template_init_boundary)
330 # genProtoAndRegisteredFields
334 def genRegisteredFields(self):
335 self.st.out(self.template_registered_fields )
343 def genMainEntryStart(self,oplist):
344 self.st.out(self.template_main_dissector_start, dissname=self.dissname, disprot=self.protoname)
346 self.st.out(self.template_main_dissector_switch_msgtype_start)
347 self.st.out(self.template_main_dissector_switch_msgtype_start_request_reply)
355 def genMainEntryEnd(self):
357 self.st.out(self.template_main_dissector_switch_msgtype_end_request_reply)
359 self.st.out(self.template_main_dissector_switch_msgtype_all_other_msgtype)
361 self.st.out(self.template_main_dissector_end)
369 # out: C code for IDL operations
373 # static const char Penguin_Echo_echoShort_op[] = "echoShort" ;
376 def genOpList(self,oplist):
377 self.st.out(self.template_comment_operations_start)
380 sname = self.namespace(n, "_")
381 opname = n.identifier()
382 self.st.out(self.template_operations_declare, sname=sname, opname=opname)
384 self.st.out(self.template_comment_operations_end)
391 # out: C code for IDL User Exceptions that contain members
395 # static const char user_exception_tux_bad_value[] = "IDL:tux/bad_value:1.0" ;
398 def genExList(self,oplist):
400 self.st.out(self.template_comment_user_exceptions_string_declare_start)
402 exlist = self.get_exceptionList(oplist) # grab list of ALL UNIQUE exception nodes
406 print "XXX Exception " , ex.repoId()
407 print "XXX Exception Identifier" , ex.identifier()
408 print "XXX Exception Scoped Name" , ex.scopedName()
410 if (ex.members()): # only if has members
411 sname = self.namespace(ex, "_")
413 self.st.out(self.template_user_exceptions_declare, sname=sname, exname=ex.repoId())
415 self.st.out(self.template_comment_user_exceptions_string_declare_end)
422 # out: C code for IDL attribute decalarations.
424 # NOTE: Mapping of attributes to operation(function) names is tricky.
426 # The actual accessor function names are language-mapping specific. The attribute name
427 # is subject to OMG IDL's name scoping rules; the accessor function names are
428 # guaranteed not to collide with any legal operation names specifiable in OMG IDL.
432 # static const char get_Penguin_Echo_get_width_at[] = "get_width" ;
433 # static const char set_Penguin_Echo_set_width_at[] = "set_width" ;
437 # static const char get_Penguin_Echo_get_width_at[] = "_get_width" ;
438 # static const char set_Penguin_Echo_set_width_at[] = "_set_width" ;
440 # TODO: Implement some language dependant templates to handle naming conventions
441 # language <=> attribute. for C, C++. Java etc
443 # OR, just add a runtime GUI option to select language binding for attributes -- FS
447 # ie: def genAtlist(self,atlist,language)
452 def genAtList(self,atlist):
453 self.st.out(self.template_comment_attributes_start)
456 for i in n.declarators(): #
457 sname = self.namespace(i, "_")
458 atname = i.identifier()
459 self.st.out(self.template_attributes_declare_Java_get, sname=sname, atname=atname)
461 self.st.out(self.template_attributes_declare_Java_set, sname=sname, atname=atname)
463 self.st.out(self.template_comment_attributes_end)
471 # out: C code for IDL Enum decalarations using "static const value_string" template
476 def genEnList(self,enlist):
478 self.st.out(self.template_comment_enums_start)
481 sname = self.namespace(enum, "_")
483 self.st.out(self.template_comment_enum_comment, ename=enum.repoId())
484 self.st.out(self.template_value_string_start, valstringname=sname)
485 for enumerator in enum.enumerators():
486 self.st.out(self.template_value_string_entry, intval=str(self.valFromEnum(enum,enumerator)), description=enumerator.identifier())
489 #atname = n.identifier()
490 self.st.out(self.template_value_string_end, valstringname=sname)
492 self.st.out(self.template_comment_enums_end)
504 # genExceptionDelegator
508 # out: C code for User exception delegator
514 def genExceptionDelegator(self,oplist):
516 self.st.out(self.template_main_exception_delegator_start)
519 exlist = self.get_exceptionList(oplist) # grab list of ALL UNIQUE exception nodes
523 print "XXX Exception " , ex.repoId()
524 print "XXX Exception Identifier" , ex.identifier()
525 print "XXX Exception Scoped Name" , ex.scopedName()
527 if (ex.members()): # only if has members
528 sname = self.namespace(ex, "_")
530 self.st.out(self.template_ex_delegate_code, sname=sname, exname=ex.repoId())
533 self.st.out(self.template_main_exception_delegator_end)
537 # genAttribueHelpers()
539 # Generate private helper functions to decode Attributes.
543 # For readonly attribute - generate get_xxx()
544 # If NOT readonly attribute - also generate set_xxx()
547 def genAttributeHelpers(self,atlist):
549 print "XXX genAttributeHelpers: atlist = ", atlist
551 self.st.out(self.template_attribute_helpers_start)
553 for attrib in atlist:
554 for decl in attrib.declarators():
555 self.genAtHelper(attrib,decl,"get") # get accessor
556 if not attrib.readonly():
557 self.genAtHelper(attrib,decl,"set") # set accessor
559 self.st.out(self.template_attribute_helpers_end)
564 # Generate private helper functions to decode an attribute
566 # in: at - attribute node
567 # in: decl - declarator belonging to this attribute
568 # in: order - to generate a "get" or "set" helper
570 def genAtHelper(self,attrib,decl,order):
572 print "XXX genAtHelper"
574 sname = order + "_" + self.namespace(decl, "_") # must use set or get prefix to avoid collision
575 self.curr_sname = sname # update current opnode/exnode scoped name
577 if not self.fn_hash_built:
578 self.fn_hash[sname] = [] # init empty list as val for this sname key
579 # but only if the fn_hash is not already built
581 self.st.out(self.template_attribute_helper_function_start, sname=sname, atname=decl.repoId())
584 self.st.out(self.template_helper_function_vars_start)
585 self.dumpCvars(sname)
586 self.st.out(self.template_helper_function_vars_end )
589 # TODO - attributes are simple types, so remove array handling
592 if decl.sizes(): # an array
593 indices = self.get_indices_from_sizes(decl.sizes())
594 string_indices = '%i ' % indices # convert int to string
595 self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
596 self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
597 self.addvar(self.c_i + decl.identifier() + ";")
601 self.getCDR3(attrib.attrType(), decl.identifier() )
604 self.st.out(self.template_get_CDR_array_end)
609 self.getCDR3(attrib.attrType(), decl.identifier() )
612 self.st.out(self.template_attribute_helper_function_end)
617 # genExceptionHelpers()
619 # Generate private helper functions to decode Exceptions used
626 def genExceptionHelpers(self,oplist):
627 exlist = self.get_exceptionList(oplist) # grab list of exception nodes
629 print "XXX genExceptionHelpers: exlist = ", exlist
631 self.st.out(self.template_exception_helpers_start)
633 if (ex.members()): # only if has members
634 #print "XXX Exception = " + ex.identifier()
637 self.st.out(self.template_exception_helpers_end)
643 # Generate private helper functions to decode User Exceptions
645 # in: exnode ( an exception node)
648 def genExHelper(self,ex):
650 print "XXX genExHelper"
652 sname = self.namespace(ex, "_")
653 self.curr_sname = sname # update current opnode/exnode scoped name
654 if not self.fn_hash_built:
655 self.fn_hash[sname] = [] # init empty list as val for this sname key
656 # but only if the fn_hash is not already built
658 self.st.out(self.template_exception_helper_function_start, sname=sname, exname=ex.repoId())
661 self.st.out(self.template_helper_function_vars_start)
662 self.dumpCvars(sname)
663 self.st.out(self.template_helper_function_vars_end )
666 for m in ex.members():
667 #print "XXX genExhelper, member = ", m, "member type = ", m.memberType()
670 for decl in m.declarators():
671 #print "XXX genExhelper, d = ", decl
672 if decl.sizes(): # an array
673 indices = self.get_indices_from_sizes(decl.sizes())
674 string_indices = '%i ' % indices # convert int to string
675 self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
676 self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
677 self.addvar(self.c_i + decl.identifier() + ";")
680 self.getCDR3(m.memberType(), ex.identifier() + "_" + decl.identifier() )
683 self.st.out(self.template_get_CDR_array_end)
687 self.getCDR3(m.memberType(), ex.identifier() + "_" + decl.identifier() )
690 self.st.out(self.template_exception_helper_function_end)
696 # Generate private helper functions for each IDL operation.
697 # Generate private helper functions for each IDL struct.
698 # Generate private helper functions for each IDL union.
701 # in: oplist, stlist, unlist
705 def genHelpers(self,oplist,stlist,unlist):
707 self.genOperation(op)
709 self.genStructHelper(st)
711 self.genUnionHelper(un)
716 # Generate private helper functions for a specificIDL operation.
721 def genOperation(self,opnode):
723 print "XXX genOperation called"
725 sname = self.namespace(opnode, "_")
726 if not self.fn_hash_built:
727 self.fn_hash[sname] = [] # init empty list as val for this sname key
728 # but only if the fn_hash is not already built
730 self.curr_sname = sname # update current opnode's scoped name
731 opname = opnode.identifier()
733 self.st.out(self.template_helper_function_comment, repoid=opnode.repoId() )
735 self.st.out(self.template_helper_function_start, sname=sname)
738 self.st.out(self.template_helper_function_vars_start)
739 self.dumpCvars(sname)
740 self.st.out(self.template_helper_function_vars_end )
743 self.st.out(self.template_helper_switch_msgtype_start)
745 self.st.out(self.template_helper_switch_msgtype_request_start)
747 self.genOperationRequest(opnode)
748 self.st.out(self.template_helper_switch_msgtype_request_end)
751 self.st.out(self.template_helper_switch_msgtype_reply_start)
754 self.st.out(self.template_helper_switch_rep_status_start)
757 self.st.out(self.template_helper_switch_msgtype_reply_no_exception_start)
759 self.genOperationReply(opnode)
760 self.st.out(self.template_helper_switch_msgtype_reply_no_exception_end)
763 self.st.out(self.template_helper_switch_msgtype_reply_user_exception_start)
765 self.genOpExceptions(opnode)
766 self.st.out(self.template_helper_switch_msgtype_reply_user_exception_end)
769 self.st.out(self.template_helper_switch_msgtype_reply_default_start)
770 self.st.out(self.template_helper_switch_msgtype_reply_default_end)
772 self.st.out(self.template_helper_switch_rep_status_end)
776 self.st.out(self.template_helper_switch_msgtype_default_start)
777 self.st.out(self.template_helper_switch_msgtype_default_end)
779 self.st.out(self.template_helper_switch_msgtype_end)
783 self.st.out(self.template_helper_function_end, sname=sname)
789 # Decode function parameters for a GIOP request message
793 def genOperationRequest(self,opnode):
794 for p in opnode.parameters():
797 print "XXX parameter = " ,p
798 print "XXX parameter type = " ,p.paramType()
799 print "XXX parameter type kind = " ,p.paramType().kind()
801 self.getCDR3(p.paramType(),p.identifier())
805 # Decode function parameters for a GIOP reply message
809 def genOperationReply(self,opnode):
811 rt = opnode.returnType() # get return type
813 print "XXX opnode = " , opnode
814 print "XXX return type = " , rt
815 print "XXX return type.unalias = " , rt.unalias()
816 print "XXX return type.kind() = " , rt.kind();
819 if (rt.kind() == idltype.tk_alias): # a typdef return val possibly ?
820 #self.getCDR3(rt.decl().alias().aliasType(),"dummy") # return value maybe a typedef
821 #self.get_CDR_alias(rt, "Operation_Return_Value" )
822 self.get_CDR_alias(rt, rt.name() )
825 self.getCDR3(rt, "Operation_Return_Value") # return value is NOT an alias
827 for p in opnode.parameters():
828 if p.is_out(): # out or inout
829 self.getCDR3(p.paramType(),p.identifier())
831 #self.st.dec_indent()
833 def genOpExceptions(self,opnode):
834 for ex in opnode.raises():
837 for m in ex.members():
839 #print m.memberType(), m.memberType().kind()
841 # Delegator for Operations
844 def genOpDelegator(self,oplist):
846 iname = "/".join(op.scopedName()[:-1])
847 opname = op.identifier()
848 sname = self.namespace(op, "_")
849 self.st.out(self.template_op_delegate_code, interface=iname, sname=sname)
852 # Delegator for Attributes
855 def genAtDelegator(self,atlist):
857 for i in a.declarators():
858 atname = i.identifier()
859 sname = self.namespace(i, "_")
860 self.st.out(self.template_at_delegate_code_get, sname=sname)
862 self.st.out(self.template_at_delegate_code_set, sname=sname)
866 # Add a variable declaration to the hash of list
869 def addvar(self, var):
870 if not ( var in self.fn_hash[self.curr_sname] ):
871 self.fn_hash[self.curr_sname].append(var)
874 # Print the variable declaration from the hash of list
879 for fn in self.fn_hash.keys():
881 for v in self.fn_hash[fn]:
884 # Print the "C" variable declaration from the hash of list
885 # for a given scoped operation name (eg: tux_penguin_eat)
889 def dumpCvars(self, sname):
890 for v in self.fn_hash[sname]:
895 # Given an enum node, and a enumerator node, return
896 # the enumerator's numerical value.
898 # eg: enum Color {red,green,blue} should return
902 def valFromEnum(self,enumNode, enumeratorNode):
904 print "XXX valFromEnum, enumNode = ", enumNode, " from ", enumNode.repoId()
905 print "XXX valFromEnum, enumeratorNode = ", enumeratorNode, " from ", enumeratorNode.repoId()
907 if isinstance(enumeratorNode,idlast.Enumerator):
908 value = enumNode.enumerators().index(enumeratorNode)
937 ## tk_longdouble = 25
944 ## tk_abstract_interface = 32
950 # This is the main "iterator" function. It takes a node, and tries to output
951 # a get_CDR_XXX accessor method(s). It can call itself multiple times
952 # if I find nested structures etc.
955 def getCDR3(self,type,name="fred"):
957 pt = type.unalias().kind() # param CDR type
958 pn = name # param name
961 print "XXX getCDR3: kind = " , pt
963 if pt == idltype.tk_ulong:
964 self.get_CDR_ulong(pn)
965 elif pt == idltype.tk_longlong:
966 self.get_CDR_longlong(pn)
967 elif pt == idltype.tk_ulonglong:
968 self.get_CDR_ulonglong(pn)
969 elif pt == idltype.tk_void:
970 self.get_CDR_void(pn)
971 elif pt == idltype.tk_short:
972 self.get_CDR_short(pn)
973 elif pt == idltype.tk_long:
974 self.get_CDR_long(pn)
975 elif pt == idltype.tk_ushort:
976 self.get_CDR_ushort(pn)
977 elif pt == idltype.tk_float:
978 self.get_CDR_float(pn)
979 elif pt == idltype.tk_double:
980 self.get_CDR_double(pn)
981 elif pt == idltype.tk_fixed:
982 self.get_CDR_fixed(type.unalias(),pn)
983 elif pt == idltype.tk_boolean:
984 self.get_CDR_boolean(pn)
985 elif pt == idltype.tk_char:
986 self.get_CDR_char(pn)
987 elif pt == idltype.tk_octet:
988 self.get_CDR_octet(pn)
989 elif pt == idltype.tk_any:
991 elif pt == idltype.tk_string:
992 self.get_CDR_string(pn)
993 elif pt == idltype.tk_wstring:
994 self.get_CDR_wstring(pn)
995 elif pt == idltype.tk_wchar:
996 self.get_CDR_wchar(pn)
997 elif pt == idltype.tk_enum:
999 self.get_CDR_enum(pn,type)
1000 #self.get_CDR_enum(pn)
1002 elif pt == idltype.tk_struct:
1003 self.get_CDR_struct(type,pn)
1004 elif pt == idltype.tk_TypeCode: # will I ever get here ?
1005 self.get_CDR_TypeCode(pn)
1006 elif pt == idltype.tk_sequence and \
1007 type.unalias().seqType().kind() == idltype.tk_octet:
1008 self.get_CDR_sequence_octet(type,pn)
1009 elif pt == idltype.tk_sequence:
1010 self.get_CDR_sequence(type,pn)
1011 elif pt == idltype.tk_objref:
1012 self.get_CDR_objref(type,pn)
1013 elif pt == idltype.tk_array:
1014 self.get_CDR_array(type,pn)
1015 elif pt == idltype.tk_union:
1016 self.get_CDR_union(type,pn)
1017 elif pt == idltype.tk_alias:
1019 print "XXXXX Alias type XXXXX " , type
1020 self.get_CDR_alias(type,pn)
1022 self.genWARNING("Unknown typecode = " + '%i ' % pt) # put comment in source code
1026 # get_CDR_XXX methods are here ..
1031 def get_CDR_ulong(self,pn):
1032 self.st.out(self.template_get_CDR_ulong, varname=pn)
1033 self.addvar(self.c_u_octet4)
1035 def get_CDR_short(self,pn):
1036 self.st.out(self.template_get_CDR_short, varname=pn)
1037 self.addvar(self.c_s_octet2)
1039 def get_CDR_void(self,pn):
1040 self.st.out(self.template_get_CDR_void, varname=pn)
1042 def get_CDR_long(self,pn):
1043 self.st.out(self.template_get_CDR_long, varname=pn)
1044 self.addvar(self.c_s_octet4)
1046 def get_CDR_ushort(self,pn):
1047 self.st.out(self.template_get_CDR_ushort, varname=pn)
1048 self.addvar(self.c_u_octet2)
1050 def get_CDR_float(self,pn):
1051 self.st.out(self.template_get_CDR_float, varname=pn)
1052 self.addvar(self.c_float)
1054 def get_CDR_double(self,pn):
1055 self.st.out(self.template_get_CDR_double, varname=pn)
1056 self.addvar(self.c_double)
1058 def get_CDR_longlong(self,pn):
1059 self.st.out(self.template_get_CDR_longlong, varname=pn)
1060 self.addvar(self.c_s_octet8)
1062 def get_CDR_ulonglong(self,pn):
1063 self.st.out(self.template_get_CDR_ulonglong, varname=pn)
1064 self.addvar(self.c_u_octet8)
1066 def get_CDR_boolean(self,pn):
1067 self.st.out(self.template_get_CDR_boolean, varname=pn)
1068 self.addvar(self.c_u_octet1)
1070 def get_CDR_fixed(self,type,pn):
1072 print "XXXX calling get_CDR_fixed, type = ", type
1073 print "XXXX calling get_CDR_fixed, type.digits() = ", type.digits()
1074 print "XXXX calling get_CDR_fixed, type.scale() = ", type.scale()
1076 string_digits = '%i ' % type.digits() # convert int to string
1077 string_scale = '%i ' % type.scale() # convert int to string
1078 string_length = '%i ' % self.dig_to_len(type.digits()) # how many octets to hilight for a number of digits
1080 self.st.out(self.template_get_CDR_fixed, varname=pn, digits=string_digits, scale=string_scale, length=string_length )
1081 self.addvar(self.c_seq)
1084 def get_CDR_char(self,pn):
1085 self.st.out(self.template_get_CDR_char, varname=pn)
1086 self.addvar(self.c_u_octet1)
1088 def get_CDR_octet(self,pn):
1089 self.st.out(self.template_get_CDR_octet, varname=pn)
1090 self.addvar(self.c_u_octet1)
1092 def get_CDR_any(self,pn):
1093 self.st.out(self.template_get_CDR_any, varname=pn)
1095 def get_CDR_enum(self,pn,type):
1096 #self.st.out(self.template_get_CDR_enum, varname=pn)
1097 sname = self.namespace(type.unalias(), "_")
1098 self.st.out(self.template_get_CDR_enum_symbolic, valstringarray=sname,varname=pn)
1101 self.addvar(self.c_u_octet4)
1103 def get_CDR_string(self,pn):
1104 self.st.out(self.template_get_CDR_string, varname=pn)
1105 self.addvar(self.c_u_octet4)
1106 self.addvar(self.c_seq)
1108 def get_CDR_wstring(self,pn):
1109 self.st.out(self.template_get_CDR_wstring, varname=pn)
1110 self.addvar(self.c_u_octet4)
1111 self.addvar(self.c_seq)
1113 def get_CDR_wchar(self,pn):
1114 self.st.out(self.template_get_CDR_wchar, varname=pn)
1115 self.addvar(self.c_s_octet1)
1116 self.addvar(self.c_seq)
1118 def get_CDR_TypeCode(self,pn):
1119 self.st.out(self.template_get_CDR_TypeCode, varname=pn)
1120 self.addvar(self.c_u_octet4)
1122 def get_CDR_objref(self,type,pn):
1123 self.st.out(self.template_get_CDR_object)
1125 def get_CDR_sequence_len(self,pn):
1126 self.st.out(self.template_get_CDR_sequence_length, seqname=pn)
1127 self.addvar(self.c_u_octet4)
1131 def get_CDR_union(self,type,pn):
1133 print "XXX Union type =" , type, " pn = ",pn
1134 print "XXX Union type.decl()" , type.decl()
1135 print "XXX Union Scoped Name" , type.scopedName()
1137 # If I am a typedef union {..}; node then find the union node
1139 if isinstance(type.decl(), idlast.Declarator):
1140 ntype = type.decl().alias().aliasType().decl()
1142 ntype = type.decl() # I am a union node
1145 print "XXX Union ntype =" , ntype
1147 sname = self.namespace(ntype, "_")
1148 self.st.out(self.template_union_start, name=sname )
1150 # Output a call to the union helper function so I can handle recursive union also.
1152 self.st.out(self.template_decode_union,name=sname)
1154 self.st.out(self.template_union_end, name=sname )
1158 # Code to generate Union Helper functions
1160 # in: un - a union node
1165 def genUnionHelper(self,un):
1167 print "XXX Union type =" , un
1168 print "XXX Union type.decl()" , un.decl()
1169 print "XXX Union Scoped Name" , un.scopedName()
1171 sname = self.namespace(un, "_")
1172 self.curr_sname = sname # update current opnode/exnode/stnode/unnode scoped name
1173 if not self.fn_hash_built:
1174 self.fn_hash[sname] = [] # init empty list as val for this sname key
1175 # but only if the fn_hash is not already built
1177 self.st.out(self.template_union_helper_function_start, sname=sname, unname=un.repoId())
1178 self.st.inc_indent()
1180 self.st.out(self.template_helper_function_vars_start)
1181 self.dumpCvars(sname)
1182 self.st.out(self.template_helper_function_vars_end )
1184 st = un.switchType().unalias() # may be typedef switch type, so find real type
1186 self.st.out(self.template_comment_union_code_start, uname=un.repoId() )
1188 self.getCDR3(st,un.identifier());
1190 # Depending on what kind of discriminant I come accross (enum,integer,char,
1191 # short, boolean), make sure I cast the return value of the get_XXX accessor
1192 # to an appropriate value. Omniidl idlast.CaseLabel.value() accessor will
1193 # return an integer, or an Enumerator object that is then converted to its
1194 # integer equivalent.
1197 # NOTE - May be able to skip some of this stuff, but leave it in for now -- FS
1200 if (st.kind() == idltype.tk_enum):
1202 self.st.out(self.template_comment_union_code_discriminant, uname=std.repoId() )
1203 self.st.out(self.template_union_code_save_discriminant_enum, discname=un.identifier() )
1204 self.addvar(self.c_s_disc + un.identifier() + ";")
1206 elif (st.kind() == idltype.tk_long):
1207 self.st.out(self.template_union_code_save_discriminant_long, discname=un.identifier() )
1208 self.addvar(self.c_s_disc + un.identifier() + ";")
1210 elif (st.kind() == idltype.tk_ulong):
1211 self.st.out(self.template_union_code_save_discriminant_ulong, discname=un.identifier() )
1212 self.addvar(self.c_s_disc + un.identifier() + ";")
1214 elif (st.kind() == idltype.tk_short):
1215 self.st.out(self.template_union_code_save_discriminant_short, discname=un.identifier() )
1216 self.addvar(self.c_s_disc + un.identifier() + ";")
1218 elif (st.kind() == idltype.tk_ushort):
1219 self.st.out(self.template_union_code_save_discriminant_ushort, discname=un.identifier() )
1220 self.addvar(self.c_s_disc + un.identifier() + ";")
1222 elif (st.kind() == idltype.tk_boolean):
1223 self.st.out(self.template_union_code_save_discriminant_boolean, discname=un.identifier() )
1224 self.addvar(self.c_s_disc + un.identifier() + ";")
1226 elif (st.kind() == idltype.tk_char):
1227 self.st.out(self.template_union_code_save_discriminant_char, discname=un.identifier() )
1228 self.addvar(self.c_s_disc + un.identifier() + ";")
1231 print "XXX Unknown st.kind() = ", st.kind()
1234 # Loop over all cases in this union
1237 for uc in un.cases(): # for all UnionCase objects in this union
1238 for cl in uc.labels(): # for all Caselabel objects in this UnionCase
1240 # get integer value, even if discriminant is
1241 # an Enumerator node
1243 if isinstance(cl.value(),idlast.Enumerator):
1245 print "XXX clv.identifier()", cl.value().identifier()
1246 print "XXX clv.repoId()", cl.value().repoId()
1247 print "XXX clv.scopedName()", cl.value().scopedName()
1249 # find index of enumerator in enum declaration
1250 # eg: RED is index 0 in enum Colors { RED, BLUE, GREEN }
1252 clv = self.valFromEnum(std,cl.value())
1257 #print "XXX clv = ",clv
1260 # if char, dont convert to int, but put inside single quotes so that it is understood by C.
1261 # eg: if (disc == 'b')..
1263 # TODO : handle \xxx chars generically from a function or table lookup rather than
1264 # a whole bunch of "if" statements. -- FS
1267 if (st.kind() == idltype.tk_char):
1268 if (clv == '\n'): # newline
1269 string_clv = "'\\n'"
1270 elif (clv == '\t'): # tab
1271 string_clv = "'\\t'"
1273 string_clv = "'" + clv + "'"
1275 string_clv = '%i ' % clv
1278 # If default case, then skp comparison with discriminator
1281 if not cl.default():
1282 self.st.out(self.template_comment_union_code_label_compare_start, discname=un.identifier(),labelval=string_clv )
1283 self.st.inc_indent()
1285 self.st.out(self.template_comment_union_code_label_default_start )
1288 self.getCDR3(uc.caseType(),uc.declarator().identifier())
1290 if not cl.default():
1291 self.st.dec_indent()
1292 self.st.out(self.template_comment_union_code_label_compare_end )
1294 self.st.out(self.template_comment_union_code_label_default_end )
1296 self.st.dec_indent()
1297 self.st.out(self.template_union_helper_function_end)
1302 # Currently, get_CDR_alias is geared to finding typdef
1305 def get_CDR_alias(self,type,pn):
1307 print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1308 print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1309 print "XXX get_CDR_alias, type.decl().alias() = " ,type.decl().alias()
1311 decl = type.decl() # get declarator object
1313 if (decl.sizes()): # a typedef array
1314 indices = self.get_indices_from_sizes(decl.sizes())
1315 string_indices = '%i ' % indices # convert int to string
1316 self.st.out(self.template_get_CDR_array_comment, aname=pn, asize=string_indices)
1318 self.st.out(self.template_get_CDR_array_start, aname=pn, aval=string_indices)
1319 self.addvar(self.c_i + pn + ";")
1320 self.st.inc_indent()
1321 self.getCDR3(type.decl().alias().aliasType(), pn )
1323 self.st.dec_indent()
1324 self.st.out(self.template_get_CDR_array_end)
1327 else: # a simple typdef
1329 print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1330 print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1332 self.getCDR3(type, decl.identifier() )
1340 # Handle structs, including recursive
1343 def get_CDR_struct(self,type,pn):
1345 # If I am a typedef struct {..}; node then find the struct node
1347 if isinstance(type.decl(), idlast.Declarator):
1348 ntype = type.decl().alias().aliasType().decl()
1350 ntype = type.decl() # I am a struct node
1352 sname = self.namespace(ntype, "_")
1353 self.st.out(self.template_structure_start, name=sname )
1355 # Output a call to the struct helper function so I can handle recursive structs also.
1357 self.st.out(self.template_decode_struct,name=sname)
1359 self.st.out(self.template_structure_end, name=sname )
1364 # Generate private helper functions to decode a struct
1366 # in: stnode ( a struct node)
1369 def genStructHelper(self,st):
1371 print "XXX genStructHelper"
1373 sname = self.namespace(st, "_")
1374 self.curr_sname = sname # update current opnode/exnode/stnode scoped name
1375 if not self.fn_hash_built:
1376 self.fn_hash[sname] = [] # init empty list as val for this sname key
1377 # but only if the fn_hash is not already built
1379 self.st.out(self.template_struct_helper_function_start, sname=sname, stname=st.repoId())
1380 self.st.inc_indent()
1382 self.st.out(self.template_helper_function_vars_start)
1383 self.dumpCvars(sname)
1384 self.st.out(self.template_helper_function_vars_end )
1386 for m in st.members():
1387 for decl in m.declarators():
1388 if decl.sizes(): # an array
1389 indices = self.get_indices_from_sizes(decl.sizes())
1390 string_indices = '%i ' % indices # convert int to string
1391 self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)
1392 self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
1393 self.addvar(self.c_i + decl.identifier() + ";")
1395 self.st.inc_indent()
1396 self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )
1397 self.st.dec_indent()
1398 self.st.out(self.template_get_CDR_array_end)
1402 self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )
1404 self.st.dec_indent()
1405 self.st.out(self.template_struct_helper_function_end)
1412 # Generate code to access a sequence of a type
1416 def get_CDR_sequence(self,type, pn):
1417 self.st.out(self.template_get_CDR_sequence_length, seqname=pn )
1418 self.st.out(self.template_get_CDR_sequence_loop_start, seqname=pn )
1419 self.addvar(self.c_i_lim + pn + ";" )
1420 self.addvar(self.c_i + pn + ";")
1422 self.st.inc_indent()
1423 self.getCDR3(type.unalias().seqType(), pn ) # and start all over with the type
1424 self.st.dec_indent()
1426 self.st.out(self.template_get_CDR_sequence_loop_end)
1430 # Generate code to access a sequence of octet
1433 def get_CDR_sequence_octet(self,type, pn):
1434 self.st.out(self.template_get_CDR_sequence_length, seqname=pn)
1435 self.st.out(self.template_get_CDR_sequence_octet, seqname=pn)
1436 self.addvar(self.c_i_lim + pn + ";")
1437 self.addvar("gchar * binary_seq_" + pn + ";")
1438 self.addvar("gchar * text_seq_" + pn + ";")
1442 # Generate code to access arrays,
1444 # This is handled elsewhere. Arrays are either typedefs or in
1447 # TODO - Remove this
1450 def get_CDR_array(self,type, decl):
1452 print "XXX get_CDR_array called "
1453 print "XXX array size = " ,decl.sizes()
1461 # out - scoped operation name, using sep character instead of "::"
1463 # eg: Penguin::Echo::echoWString => Penguin_Echo_echoWString if sep = "_"
1467 def namespace(self,node,sep):
1468 sname = string.replace(idlutil.ccolonName(node.scopedName()), '::', sep)
1469 #print "XXX namespace: sname = " + sname
1474 # generate code for plugin initialisation
1477 def gen_plugin_register(self):
1478 self.st.out(self.template_plugin_register, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1481 # generate register_giop_user_module code, and register only
1482 # unique interfaces that contain operations. Also output
1483 # a heuristic register in case we want to use that.
1485 # TODO - make this a command line option
1493 def gen_proto_reg_handoff(self, oplist):
1495 self.st.out(self.template_proto_reg_handoff_start, dissector_name=self.dissname)
1496 self.st.inc_indent()
1498 for iname in self.get_intlist(oplist):
1499 self.st.out(self.template_proto_reg_handoff_body, dissector_name=self.dissname, protocol_name=self.protoname, interface=iname )
1501 self.st.out(self.template_proto_reg_handoff_heuristic, dissector_name=self.dissname, protocol_name=self.protoname)
1502 self.st.dec_indent()
1504 self.st.out(self.template_proto_reg_handoff_end)
1509 # generate proto_register_<protoname> code,
1513 def gen_proto_register(self):
1514 self.st.out(self.template_proto_register, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1520 # out - a list of unique interface names. This will be used in
1521 # register_giop_user_module(dissect_giop_auto, "TEST IDL", "Penguin/Echo" ); so the operation
1522 # name must be removed from the scope. And we also only want unique interfaces.
1525 def get_intlist(self,oplist):
1526 int_hash = {} # holds a hash of unique interfaces
1528 sc = op.scopedName() # eg: penguin,tux,bite
1529 sc1 = sc[:-1] # drop last entry
1530 sn = idlutil.slashName(sc1) # penguin/tux
1531 if not int_hash.has_key(sn):
1532 int_hash[sn] = 0; # dummy val, but at least key is unique
1533 ret = int_hash.keys()
1542 # out - a list of exception nodes (unique). This will be used in
1543 # to generate dissect_exception_XXX functions.
1548 def get_exceptionList(self,oplist):
1549 ex_hash = {} # holds a hash of unique exceptions.
1551 for ex in op.raises():
1552 if not ex_hash.has_key(ex):
1553 ex_hash[ex] = 0; # dummy val, but at least key is unique
1555 print "XXX Exception = " + ex.identifier()
1556 ret = ex_hash.keys()
1563 # Simple function to take a list of array sizes and find the
1564 # total number of elements
1567 # eg: temp[4][3] = 12 elements
1570 def get_indices_from_sizes(self,sizelist):
1578 # Determine how many octets contain requested number
1579 # of digits for an "fixed" IDL type "on the wire"
1582 def dig_to_len(self,dignum):
1583 return (dignum/2) + 1
1588 # Output some TODO comment
1592 def genTODO(self,message):
1593 self.st.out(self.template_debug_TODO, message=message)
1596 # Output some WARNING comment
1600 def genWARNING(self,message):
1601 self.st.out(self.template_debug_WARNING, message=message)
1604 # Templates for C code
1607 template_comment_operations_start = """\
1609 * IDL Operations Start
1612 template_operations_declare = """static const char @sname@_op[] = \"@opname@\" ;"""
1614 template_comment_operations_end = """
1616 * IDL Operations End
1619 template_helper_function_comment = """\
1624 template_helper_function_vars_start = """
1625 /* Operation specific Variable declarations Begin */
1627 template_helper_function_vars_end = """
1628 /* Operation specific Variable declarations End */
1630 template_helper_function_start = """\
1631 static void decode_@sname@(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header, gchar *operation _U_, gboolean stream_is_big_endian _U_) {
1633 template_helper_function_end = """\
1637 # proto_reg_handoff() templates
1640 template_proto_reg_handoff_start = """
1641 /* register me as handler for these interfaces */
1643 void proto_register_handoff_giop_@dissector_name@(void) {
1646 template_proto_reg_handoff_body = """
1647 /* Register for Explicit Dissection */
1649 register_giop_user_module(dissect_@dissector_name@, \"@protocol_name@\", \"@interface@\", proto_@dissector_name@ ); /* explicit dissector */
1651 template_proto_reg_handoff_heuristic = """
1652 /* Register for Heuristic Dissection */
1654 register_giop_user(dissect_@dissector_name@, \"@protocol_name@\" ,proto_@dissector_name@); /* heuristic dissector */
1656 template_proto_reg_handoff_end = """
1661 # Initialize the protocol
1664 template_protocol = """
1665 /* Initialise the protocol and subtree pointers */
1667 static int proto_@dissector_name@ = -1;
1669 static gint ett_@dissector_name@ = -1;
1672 # Initialize the boundary Alignment
1675 template_init_boundary = """
1676 /* Initialise the initial Alignment */
1678 static guint32 boundary = GIOP_HEADER_SIZE; /* initial value */
1681 # Initialize the Registered fields
1684 template_registered_fields = """
1686 /* Initialise the Registered fields */
1688 /* TODO - Use registered fields */
1691 # plugin_register and plugin_reg_handoff templates
1694 template_plugin_register = """
1695 #ifndef ENABLE_STATIC
1697 G_MODULE_EXPORT void
1698 plugin_register(void)
1700 if (proto_@dissector_name@ == -1) {
1701 proto_register_giop_@dissector_name@();
1705 G_MODULE_EXPORT void
1706 plugin_reg_handoff(void){
1707 proto_register_handoff_giop_@dissector_name@();
1712 # proto_register_<dissector name>(void) templates
1715 template_proto_register = """
1717 /* Register the protocol with Wireshark */
1719 void proto_register_giop_@dissector_name@(void) {
1721 /* setup list of header fields */
1723 static hf_register_info hf[] = {
1724 /* field that indicates the currently ongoing request/reply exchange */
1725 {&hf_operationrequest, {"Request_Operation","@protocol_name@.Request_Operation",FT_STRING,BASE_NONE,NULL,0x0,NULL,HFILL}},
1731 /* setup protocol subtree array */
1733 static gint *ett[] = {
1734 &ett_@dissector_name@,
1737 /* Register the protocol name and description */
1739 proto_@dissector_name@ = proto_register_protocol(\"@description@\" , \"@protocol_name@\", \"giop-@dissector_name@\" );
1741 proto_register_field_array(proto_@dissector_name@, hf, array_length(hf));
1743 proto_register_subtree_array(ett,array_length(ett));
1748 # template for delegation code
1751 template_op_delegate_code = """\
1752 if (strcmp(operation, @sname@_op) == 0
1753 && (!idlname || strcmp(idlname, \"@interface@\") == 0)) {
1754 process_RequestOperation(); /* fill-up Request_Operation field & info column */
1755 tree = start_dissecting(tvb, pinfo, ptree, offset);
1756 decode_@sname@(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian);
1761 # Templates for the helper functions
1766 template_helper_switch_msgtype_start = """\
1768 switch(header->message_type) {
1770 template_helper_switch_msgtype_default_start = """\
1773 /* Unknown GIOP Exception */
1775 g_warning("Unknown GIOP Message");
1777 template_helper_switch_msgtype_default_end = """\
1780 template_helper_switch_msgtype_end = """\
1781 } /* switch(header->message_type) */
1783 template_helper_switch_msgtype_request_start = """\
1786 template_helper_switch_msgtype_request_end = """\
1789 template_helper_switch_msgtype_reply_start = """\
1792 template_helper_switch_msgtype_reply_no_exception_start = """\
1795 template_helper_switch_msgtype_reply_no_exception_end = """\
1798 template_helper_switch_msgtype_reply_user_exception_start = """\
1799 case USER_EXCEPTION:
1801 template_helper_switch_msgtype_reply_user_exception_end = """\
1804 template_helper_switch_msgtype_reply_default_start = """\
1807 /* Unknown Exception */
1809 g_warning("Unknown Exception ");
1811 template_helper_switch_msgtype_reply_default_end = """\
1814 template_helper_switch_msgtype_reply_end = """\
1817 template_helper_switch_msgtype_default_start = """\
1820 /* Unknown GIOP Message */
1822 g_warning("Unknown GIOP Message");
1824 template_helper_switch_msgtype_default_end = """\
1827 template_helper_switch_rep_status_start = """\
1828 switch(header->rep_status) {
1830 template_helper_switch_rep_status_default_start = """\
1833 /* Unknown Reply Status */
1835 g_warning("Unknown Reply Status");
1837 template_helper_switch_rep_status_default_end = """\
1840 template_helper_switch_rep_status_end = """\
1842 } /* switch(header->message_type) */
1848 # Templates for get_CDR_xxx accessors
1851 template_get_CDR_ulong = """\
1852 u_octet4 = get_CDR_ulong(tvb,offset,stream_is_big_endian, boundary);
1854 proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %u",u_octet4);
1857 template_get_CDR_short = """\
1858 s_octet2 = get_CDR_short(tvb,offset,stream_is_big_endian, boundary);
1860 proto_tree_add_text(tree,tvb,*offset-2,2,"@varname@ = %i",s_octet2);
1863 template_get_CDR_void = """\
1864 /* Function returns void */
1866 template_get_CDR_long = """\
1867 s_octet4 = get_CDR_long(tvb,offset,stream_is_big_endian, boundary);
1869 proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %i",s_octet4);
1872 template_get_CDR_ushort = """\
1873 u_octet2 = get_CDR_ushort(tvb,offset,stream_is_big_endian, boundary);
1875 proto_tree_add_text(tree,tvb,*offset-2,2,"@varname@ = %u",u_octet2);
1878 template_get_CDR_float = """\
1879 my_float = get_CDR_float(tvb,offset,stream_is_big_endian, boundary);
1881 proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %.6e",my_float);
1884 template_get_CDR_double = """\
1885 my_double = get_CDR_double(tvb,offset,stream_is_big_endian, boundary);
1887 proto_tree_add_text(tree,tvb,*offset-8,8,"@varname@ = %.15e",my_double);
1890 template_get_CDR_longlong = """\
1891 s_octet8 = get_CDR_long_long(tvb,offset,stream_is_big_endian, boundary);
1893 proto_tree_add_text(tree,tvb,*offset-8,8,"@varname@ = %" G_GINT64_MODIFIER "d",s_octet8);
1896 template_get_CDR_ulonglong = """\
1897 u_octet8 = get_CDR_ulong_long(tvb,offset,stream_is_big_endian, boundary);
1899 proto_tree_add_text(tree,tvb,*offset-8,8,"@varname@ = %" G_GINT64_MODIFIER "u",u_octet8);
1902 template_get_CDR_boolean = """\
1903 u_octet1 = get_CDR_boolean(tvb,offset);
1905 proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1908 template_get_CDR_char = """\
1909 u_octet1 = get_CDR_char(tvb,offset);
1911 proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1914 template_get_CDR_octet = """\
1915 u_octet1 = get_CDR_octet(tvb,offset);
1917 proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1920 template_get_CDR_any = """\
1921 get_CDR_any(tvb,tree,offset,stream_is_big_endian, boundary, header);
1923 template_get_CDR_fixed = """\
1924 get_CDR_fixed(tvb, &seq, offset, @digits@, @scale@);
1926 proto_tree_add_text(tree,tvb,*offset-@length@, @length@, "@varname@ < @digits@, @scale@> = %s",seq);
1929 g_free(seq); /* free buffer */
1932 template_get_CDR_enum_symbolic = """\
1934 u_octet4 = get_CDR_enum(tvb,offset,stream_is_big_endian, boundary);
1936 proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %u (%s)",u_octet4,val_to_str(u_octet4,@valstringarray@,"Unknown Enum Value"));
1939 template_get_CDR_string = """\
1940 u_octet4 = get_CDR_string(tvb, &seq, offset, stream_is_big_endian, boundary);
1942 proto_tree_add_text(tree,tvb,*offset-u_octet4,u_octet4,"@varname@ (%u) = %s",
1943 u_octet4, (u_octet4 > 0) ? seq : \"\");
1946 g_free(seq); /* free buffer */
1949 template_get_CDR_wstring = """\
1950 u_octet4 = get_CDR_wstring(tvb, &seq, offset, stream_is_big_endian, boundary, header);
1952 proto_tree_add_text(tree,tvb,*offset-u_octet4,u_octet4,"@varname@ (%u) = %s",
1953 u_octet4, (u_octet4 > 0) ? seq : \"\");
1956 g_free(seq); /* free buffer */
1959 template_get_CDR_wchar = """\
1960 s_octet1 = get_CDR_wchar(tvb, &seq, offset, header);
1963 proto_tree_add_text(tree,tvb,*offset-1-s_octet1,1,"length = %u",s_octet1);
1966 s_octet1 = -s_octet1;
1969 proto_tree_add_text(tree,tvb,*offset-s_octet1,s_octet1,"@varname@ = %s",seq);
1973 g_free(seq); /* free buffer */
1976 template_get_CDR_TypeCode = """\
1977 u_octet4 = get_CDR_typeCode(tvb, tree, offset, stream_is_big_endian, boundary, header);
1981 template_get_CDR_object = """\
1982 get_CDR_object(tvb, pinfo, tree, offset, stream_is_big_endian, boundary);
1985 template_get_CDR_sequence_length = """\
1986 u_octet4_loop_@seqname@ = get_CDR_ulong(tvb, offset, stream_is_big_endian, boundary);
1988 proto_tree_add_text(tree,tvb,*offset-4, 4 ,"Seq length of @seqname@ = %u",u_octet4_loop_@seqname@);
1991 template_get_CDR_sequence_loop_start = """\
1992 for (i_@seqname@=0; i_@seqname@ < u_octet4_loop_@seqname@; i_@seqname@++) {
1994 template_get_CDR_sequence_loop_end = """\
1998 template_get_CDR_sequence_octet = """\
1999 if (u_octet4_loop_@seqname@ > 0 && tree) {
2000 get_CDR_octet_seq(tvb, &binary_seq_@seqname@, offset,
2001 u_octet4_loop_@seqname@);
2002 text_seq_@seqname@ = make_printable_string(binary_seq_@seqname@,
2003 u_octet4_loop_@seqname@);
2004 proto_tree_add_text(tree, tvb, *offset - u_octet4_loop_@seqname@,
2005 u_octet4_loop_@seqname@, \"@seqname@: %s\", text_seq_@seqname@);
2006 g_free(binary_seq_@seqname@);
2007 g_free(text_seq_@seqname@);
2010 template_get_CDR_array_start = """\
2011 for (i_@aname@=0; i_@aname@ < @aval@; i_@aname@++) {
2013 template_get_CDR_array_end = """\
2016 template_get_CDR_array_comment = """\
2017 /* Array: @aname@[ @asize@] */
2019 template_structure_start = """\
2020 /* Begin struct \"@name@\" */
2022 template_structure_end = """\
2023 /* End struct \"@name@\" */
2026 template_union_start = """\
2027 /* Begin union \"@name@\" */
2029 template_union_end = """\
2030 /* End union \"@name@\" */
2034 # Program Header Template
2037 template_Header = """\
2038 /* packet-@dissector_name@.c
2042 * Routines for IDL dissection
2044 * Autogenerated from idl2wrs
2045 * Copyright 2001 Frank Singleton <frank.singleton@@ericsson.com>
2050 template_wireshark_copyright = """\
2052 * Wireshark - Network traffic analyzer
2054 * Copyright 1999 - 2006 Gerald Combs
2057 static int hf_operationrequest = -1;/* Request_Operation field */
2070 * This program is free software; you can redistribute it and/or
2071 * modify it under the terms of the GNU General Public License
2072 * as published by the Free Software Foundation; either version 2
2073 * of the License, or (at your option) any later version.
2075 * This program is distributed in the hope that it will be useful,
2076 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2077 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2078 * GNU General Public License for more details.
2080 * You should have received a copy of the GNU General Public License
2081 * along with this program; if not, write to the Free Software
2082 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2090 template_Includes = """\
2092 #ifdef HAVE_CONFIG_H
2093 # include "config.h"
2096 #include <gmodule.h>
2100 #include <epan/packet.h>
2101 #include <epan/proto.h>
2102 #include <epan/dissectors/packet-giop.h>
2104 #ifndef ENABLE_STATIC
2105 G_MODULE_EXPORT const gchar version[] = "0.0.1";
2109 /* disable warning: "unreference local variable" */
2110 #pragma warning(disable:4101)
2116 # Main dissector entry templates
2119 template_main_dissector_start = """\
2121 * Called once we accept the packet as being for us; it sets the
2122 * Protocol and Info columns and creates the top-level protocol
2125 static proto_tree *start_dissecting(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset) {
2127 proto_item *ti = NULL;
2128 proto_tree *tree = NULL; /* init later, inside if(tree) */
2130 col_set_str(pinfo->cinfo, COL_PROTOCOL, \"@disprot@\");
2133 * Do not clear COL_INFO, as nothing is being written there by
2134 * this dissector yet. So leave it as is from the GIOP dissector.
2135 * TODO: add something useful to COL_INFO
2136 * col_clear(pinfo->cinfo, COL_INFO);
2140 ti = proto_tree_add_item(ptree, proto_@dissname@, tvb, *offset, -1, ENC_NA);
2141 tree = proto_item_add_subtree(ti, ett_@dissname@);
2146 static gboolean dissect_@dissname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset, MessageHeader *header, gchar *operation, gchar *idlname) {
2148 gboolean stream_is_big_endian; /* big endianess */
2149 proto_tree *tree _U_;
2150 #define process_RequestOperation(){ \\
2152 if(header->message_type == Reply){ col_append_fstr(pinfo->cinfo, COL_INFO, " op = %s",operation); }; /* fill-up info column */ \\
2153 pi=proto_tree_add_string_format_value(ptree,hf_operationrequest,tvb,0,0,operation," %s",operation);PROTO_ITEM_SET_GENERATED(pi); /* fill-up the field */ \\
2156 stream_is_big_endian = is_big_endian(header); /* get endianess */
2158 /* If we have a USER Exception, then decode it and return */
2160 if ((header->message_type == Reply) && (header->rep_status == USER_EXCEPTION)) {
2162 return decode_user_exception(tvb, pinfo, ptree, offset, header, operation, stream_is_big_endian);
2166 template_main_dissector_switch_msgtype_start = """\
2167 switch(header->message_type) {
2169 template_main_dissector_switch_msgtype_start_request_reply = """\
2173 template_main_dissector_switch_msgtype_end_request_reply = """\
2177 template_main_dissector_switch_msgtype_all_other_msgtype = """\
2181 case CloseConnection:
2184 return FALSE; /* not handled yet */
2187 return FALSE; /* not handled yet */
2191 template_main_dissector_end = """\
2195 } /* End of main dissector */
2204 #-------------------------------------------------------------#
2205 # Exception handling templates #
2206 #-------------------------------------------------------------#
2214 template_exception_helpers_start = """\
2215 /* Begin Exception Helper Functions */
2218 template_exception_helpers_end = """\
2220 /* End Exception Helper Functions */
2226 # Templates for declaration of string constants for user exceptions.
2229 template_comment_user_exceptions_string_declare_start = """\
2230 /* Begin Exception (containing members) String Declare */
2233 template_user_exceptions_declare = """static const char user_exception_@sname@[] = \"@exname@\" ; """
2236 template_comment_user_exceptions_string_declare_end = """\
2238 /* End Exception (containing members) String Declare */
2245 # template for Main delegator for exception handling
2248 template_main_exception_delegator_start = """\
2250 * Main delegator for exception handling
2254 static gboolean decode_user_exception(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *ptree _U_, int *offset _U_, MessageHeader *header, gchar *operation _U_, gboolean stream_is_big_endian _U_) {
2256 /*gboolean stream_is_big_endian _U_;*/ /* big endianess */
2257 proto_tree *tree _U_;
2259 if (!header->exception_id)
2265 # template for exception delegation code body
2267 template_ex_delegate_code = """\
2268 if (strcmp(header->exception_id, user_exception_@sname@) == 0) {
2269 tree = start_dissecting(tvb, pinfo, ptree, offset);
2270 decode_ex_@sname@(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian); /* @exname@ */
2277 # End of Main delegator for exception handling
2280 template_main_exception_delegator_end = """\
2283 return FALSE; /* user exception not found */
2289 # template for exception helper code
2293 template_exception_helper_function_start = """\
2294 /* Exception = @exname@ */
2296 static void decode_ex_@sname@(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, gchar *operation _U_, gboolean stream_is_big_endian _U_) {
2299 template_exception_helper_function_end = """\
2305 # template for struct helper code
2309 template_struct_helper_function_start = """\
2310 /* Struct = @stname@ */
2312 static void decode_@sname@_st(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, gchar *operation _U_, gboolean stream_is_big_endian _U_) {
2315 template_struct_helper_function_end = """\
2320 # template for union helper code
2324 template_union_helper_function_start = """\
2325 /* Union = @unname@ */
2327 static void decode_@sname@_un(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, gchar *operation _U_, gboolean stream_is_big_endian _U_) {
2330 template_union_helper_function_end = """\
2336 #-------------------------------------------------------------#
2337 # Value string templates #
2338 #-------------------------------------------------------------#
2340 template_value_string_start = """\
2341 static const value_string @valstringname@[] = {
2343 template_value_string_entry = """\
2344 { @intval@, \"@description@\" }, """
2346 template_value_string_end = """\
2353 #-------------------------------------------------------------#
2354 # Enum handling templates #
2355 #-------------------------------------------------------------#
2357 template_comment_enums_start = """\
2362 template_comment_enums_end = """\
2367 template_comment_enum_comment = """\
2375 #-------------------------------------------------------------#
2376 # Attribute handling templates #
2377 #-------------------------------------------------------------#
2380 template_comment_attributes_start = """\
2382 * IDL Attributes Start
2387 # get/set accessor method names are language mapping dependant.
2390 template_attributes_declare_Java_get = """static const char get_@sname@_at[] = \"_get_@atname@\" ;"""
2391 template_attributes_declare_Java_set = """static const char set_@sname@_at[] = \"_set_@atname@\" ;"""
2393 template_comment_attributes_end = """
2395 * IDL Attributes End
2401 # template for Attribute delegation code
2403 # Note: _get_xxx() should only be called for Reply with NO_EXCEPTION
2404 # Note: _set_xxx() should only be called for Request
2408 template_at_delegate_code_get = """\
2409 if (strcmp(operation, get_@sname@_at) == 0 && (header->message_type == Reply) && (header->rep_status == NO_EXCEPTION) ) {
2410 tree = start_dissecting(tvb, pinfo, ptree, offset);
2411 decode_get_@sname@_at(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian);
2415 template_at_delegate_code_set = """\
2416 if (strcmp(operation, set_@sname@_at) == 0 && (header->message_type == Request) ) {
2417 tree = start_dissecting(tvb, pinfo, ptree, offset);
2418 decode_set_@sname@_at(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian);
2422 template_attribute_helpers_start = """\
2423 /* Begin Attribute Helper Functions */
2425 template_attribute_helpers_end = """\
2427 /* End Attribute Helper Functions */
2431 # template for attribute helper code
2435 template_attribute_helper_function_start = """\
2437 /* Attribute = @atname@ */
2439 static void decode_@sname@_at(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, gchar *operation _U_, gboolean stream_is_big_endian _U_) {
2442 template_attribute_helper_function_end = """\
2447 #-------------------------------------------------------------#
2448 # Debugging templates #
2449 #-------------------------------------------------------------#
2452 # Template for outputting TODO "C" comments
2453 # so user know I need ti improve something.
2456 template_debug_TODO = """\
2458 /* TODO - @message@ */
2461 # Template for outputting WARNING "C" comments
2462 # so user know if I have found a problem.
2465 template_debug_WARNING = """\
2466 /* WARNING - @message@ */
2471 #-------------------------------------------------------------#
2472 # IDL Union templates #
2473 #-------------------------------------------------------------#
2475 template_comment_union_code_start = """\
2477 * IDL Union Start - @uname@
2480 template_comment_union_code_end = """
2482 * IDL union End - @uname@
2485 template_comment_union_code_discriminant = """\
2487 * IDL Union - Discriminant - @uname@
2491 # Cast Unions types to something appropriate
2492 # Enum value cast to guint32, all others cast to gint32
2493 # as omniidl accessor returns integer or Enum.
2496 template_union_code_save_discriminant_enum = """\
2497 disc_s_@discname@ = (gint32) u_octet4; /* save Enum Value discriminant and cast to gint32 */
2499 template_union_code_save_discriminant_long = """\
2500 disc_s_@discname@ = (gint32) s_octet4; /* save gint32 discriminant and cast to gint32 */
2503 template_union_code_save_discriminant_ulong = """\
2504 disc_s_@discname@ = (gint32) u_octet4; /* save guint32 discriminant and cast to gint32 */
2506 template_union_code_save_discriminant_short = """\
2507 disc_s_@discname@ = (gint32) s_octet2; /* save gint16 discriminant and cast to gint32 */
2510 template_union_code_save_discriminant_ushort = """\
2511 disc_s_@discname@ = (gint32) u_octet2; /* save guint16 discriminant and cast to gint32 */
2513 template_union_code_save_discriminant_char = """\
2514 disc_s_@discname@ = (gint32) u_octet1; /* save guint1 discriminant and cast to gint32 */
2516 template_union_code_save_discriminant_boolean = """\
2517 disc_s_@discname@ = (gint32) u_octet1; /* save guint1 discriminant and cast to gint32 */
2519 template_comment_union_code_label_compare_start = """\
2520 if (disc_s_@discname@ == @labelval@) {
2522 template_comment_union_code_label_compare_end = """\
2523 return; /* End Compare for this discriminant type */
2528 template_comment_union_code_label_default_start = """
2529 /* Default Union Case Start */
2531 template_comment_union_code_label_default_end = """\
2532 /* Default Union Case End */
2536 # Templates for function prototypes.
2537 # This is used in genDeclares() for declaring function prototypes
2538 # for structs and union helper functions.
2541 template_prototype_start_dissecting = """
2542 static proto_tree *start_dissecting(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset);
2545 template_prototype_struct_start = """
2546 /* Struct prototype declaration Start */
2548 template_prototype_struct_end = """
2549 /* Struct prototype declaration End */
2551 template_prototype_struct_body = """
2552 /* Struct = @stname@ */
2554 static void decode_@name@_st(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, gchar *operation _U_, gboolean stream_is_big_endian _U_);
2556 template_decode_struct = """
2558 decode_@name@_st(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian);
2560 template_prototype_union_start = """
2561 /* Union prototype declaration Start */
2563 template_prototype_union_end = """
2564 /* Union prototype declaration End */
2567 template_prototype_union_body = """
2569 /* Union = @unname@ */
2571 static void decode_@name@_un(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, gchar *operation _U_, gboolean stream_is_big_endian _U_);
2574 template_decode_union = """
2575 decode_@name@_un(tvb, pinfo, tree, offset, header, operation, stream_is_big_endian);