# -*- python -*-
#
-# $Id: ethereal_gen.py,v 1.17 2001/12/17 22:31:42 guy Exp $
+# $Id: ethereal_gen.py,v 1.22 2002/01/23 23:15:32 guy Exp $
#
# ethereal_gen.py (part of idl2eth)
#
#
# Omniidl Back-end which parses an IDL list of "Operation" nodes
# passed from ethereal_be2.py and generates "C" code for compiling
-# as a dissector in Ethereal IP protocol anlayser.
+# as a plugin for the Ethereal IP Protocol Analyser.
#
#
# Strategy (sneaky but ...)
# 13. Implement support for plugins [done]
# 14. Dont generate code for empty operations (cf: exceptions without members)
# 15. Generate code to display Enums numerically and symbolically [done]
-# 16. Place structs in subtrees
-# 17. Recursive struct and union handling [started - pass struct and union list to ethereal_gen.py ]
+# 16. Place structs/unions in subtrees
+# 17. Recursive struct and union handling [done ]
+# 18. Improve variable naming for display (eg: structs, unions etc)
#
# Also test, Test, TEST
#
def genCode(self,oplist, atlist, enlist, stlist, unlist): # operation,attribute,enums,struct and union lists
- self.genHelpers(oplist) # sneaky .. call it now, to populate the fn_hash
+
+ self.genHelpers(oplist,stlist,unlist) # sneaky .. call it now, to populate the fn_hash
# so when I come to that operation later, I have the variables to
# declare already.
self.genEthCopyright() # Ethereal Copyright comments.
self.genGPL() # GPL license
self.genIncludes()
- self.genDeclares(oplist)
+ self.genDeclares(oplist,atlist,enlist,stlist,unlist)
self.genProtocol()
self.genRegisteredFields()
self.genOpList(oplist) # string constant declares for operation names
self.genExceptionDelegator(oplist) # finds the helper function to decode a user exception
self.genAttributeHelpers(atlist) # helper function to decode "attributes"
- self.genHelpers(oplist)
+ self.genHelpers(oplist,stlist,unlist) # operation, struct and union decode helper functions
self.genMainEntryStart(oplist)
self.genOpDelegator(oplist)
self.st.out(self.template_Includes)
+
#
# denDeclares
#
+ # generate function prototypes if required
+ #
+ # Currently this is used for struct and union helper function declarations.
#
- def genDeclares(self,oplist):
+
+ def genDeclares(self,oplist,atlist,enlist,stlist,unlist):
if self.DEBUG:
print "XXX genDeclares"
+ # struct prototypes
+
+ self.st.out(self.template_prototype_struct_start)
+ for st in stlist:
+ #print st.repoId()
+ sname = self.namespace(st, "_")
+
+ self.st.out(self.template_prototype_struct_body, stname=st.repoId(),name=sname)
+ self.st.out(self.template_prototype_struct_end)
+
+ # union prototypes
+
+ self.st.out(self.template_prototype_union_start)
+ for un in unlist:
+ sname = self.namespace(un, "_")
+ self.st.out(self.template_prototype_union_body, unname=un.repoId(),name=sname)
+ self.st.out(self.template_prototype_union_end)
+
+
#
self.st.dec_indent()
self.st.out(self.template_main_dissector_switch_msgtype_all_other_msgtype)
self.st.dec_indent()
- #self.st.out(self.template_main_dissector_switch_msgtype_end)
self.st.out(self.template_main_dissector_end)
# genHelpers()
#
# Generate private helper functions for each IDL operation.
+ # Generate private helper functions for each IDL struct.
+ # Generate private helper functions for each IDL union.
#
- # in: oplist
+ #
+ # in: oplist, stlist, unlist
#
- def genHelpers(self,oplist):
+ def genHelpers(self,oplist,stlist,unlist):
for op in oplist:
self.genOperation(op)
-
+ for st in stlist:
+ self.genStructHelper(st)
+ for un in unlist:
+ self.genUnionHelper(un)
#
# genOperation()
#
# Decode function parameters for a GIOP request message
#
- # TODO check for enum
#
def genOperationRequest(self,opnode):
#
# Decode function parameters for a GIOP reply message
#
- # TODO check for enum
def genOperationReply(self,opnode):
self.st.out(self.template_get_CDR_sequence_length, seqname=pn)
self.addvar(self.c_u_octet4)
+
+
def get_CDR_union(self,type,pn):
if self.DEBUG:
print "XXX Union type =" , type, " pn = ",pn
if self.DEBUG:
print "XXX Union ntype =" , ntype
- if ntype.recursive():
- sys.stderr.write( "Error: idl2eth does not handle recursive unions yet \n")
- sys.exit(1)
+ sname = self.namespace(ntype, "_")
+ self.st.out(self.template_union_start, name=sname )
+
+ # Output a call to the union helper function so I can handle recursive union also.
+
+ self.st.out(self.template_decode_union,name=sname)
+
+ self.st.out(self.template_union_end, name=sname )
- st = ntype.switchType().unalias() # may be typedef switch type, so find real type
- #std = st.decl()
+ #
+ # Code to generate Union Helper functions
+ #
+ # in: un - a union node
+ #
+ #
- if self.DEBUG:
- print "XXX Union ntype =" , ntype
- print "XXX Union ntype.switchType =" , st
- #print "XXX Union ntype.switchType().decl() = " , std
- #print "XXX Union std.repoId() = ", std.repoId()
+ def genUnionHelper(self,un):
+ if self.DEBUG:
+ print "XXX Union type =" , un
+ print "XXX Union type.decl()" , un.decl()
+ print "XXX Union Scoped Name" , un.scopedName()
+
+ sname = self.namespace(un, "_")
+ self.curr_sname = sname # update current opnode/exnode/stnode/unnode scoped name
+ if not self.fn_hash_built:
+ self.fn_hash[sname] = [] # init empty list as val for this sname key
+ # but only if the fn_hash is not already built
+
+ self.st.out(self.template_union_helper_function_start, sname=sname, unname=un.repoId())
+ self.st.inc_indent()
+
+ self.st.out(self.template_helper_function_vars_start)
+ self.dumpCvars(sname)
+ self.st.out(self.template_helper_function_vars_end )
+
+ self.st.out(self.template_union_helper_function_get_endianess)
+
+ st = un.switchType().unalias() # may be typedef switch type, so find real type
- self.st.out(self.template_comment_union_code_start, uname=ntype.repoId() )
+ self.st.out(self.template_comment_union_code_start, uname=un.repoId() )
- self.getCDR3(st,pn);
+ self.getCDR3(st,un.identifier());
# Depending on what kind of discriminant I come accross (enum,integer,char,
- # boolean), make sure I cast the return value of the get_XXX accessor
+ # short, boolean), make sure I cast the return value of the get_XXX accessor
# to an appropriate value. Omniidl idlast.CaseLabel.value() accessor will
# return an integer, or an Enumerator object that is then converted to its
# integer equivalent.
if (st.kind() == idltype.tk_enum):
std = st.decl()
self.st.out(self.template_comment_union_code_discriminant, uname=std.repoId() )
- self.st.out(self.template_union_code_save_discriminant_enum, discname=pn )
- self.addvar(self.c_s_disc + pn + ";")
+ self.st.out(self.template_union_code_save_discriminant_enum, discname=un.identifier() )
+ self.addvar(self.c_s_disc + un.identifier() + ";")
elif (st.kind() == idltype.tk_long):
- self.st.out(self.template_union_code_save_discriminant_long, discname=pn )
- self.addvar(self.c_s_disc + pn + ";")
+ self.st.out(self.template_union_code_save_discriminant_long, discname=un.identifier() )
+ self.addvar(self.c_s_disc + un.identifier() + ";")
elif (st.kind() == idltype.tk_short):
- self.st.out(self.template_union_code_save_discriminant_short, discname=pn )
- self.addvar(self.c_s_disc + pn + ";")
+ self.st.out(self.template_union_code_save_discriminant_short, discname=un.identifier() )
+ self.addvar(self.c_s_disc + un.identifier() + ";")
elif (st.kind() == idltype.tk_boolean):
- self.st.out(self.template_union_code_save_discriminant_boolean, discname=pn )
- self.addvar(self.c_s_disc + pn + ";")
+ self.st.out(self.template_union_code_save_discriminant_boolean, discname=un.identifier() )
+ self.addvar(self.c_s_disc + un.identifier() + ";")
elif (st.kind() == idltype.tk_char):
- self.st.out(self.template_union_code_save_discriminant_char, discname=pn )
- self.addvar(self.c_s_disc + pn + ";")
+ self.st.out(self.template_union_code_save_discriminant_char, discname=un.identifier() )
+ self.addvar(self.c_s_disc + un.identifier() + ";")
else:
print "XXX Unknown st.kind() = ", st.kind()
# Loop over all cases in this union
#
- for uc in ntype.cases(): # for all UnionCase objects in this union
- for cl in uc.labels(): # for all Caselabel objects in this UnionCase
+ for uc in un.cases(): # for all UnionCase objects in this union
+ for cl in uc.labels(): # for all Caselabel objects in this UnionCase
# get integer value, even if discriminant is
# an Enumerator node
#
if not cl.default():
- self.st.out(self.template_comment_union_code_label_compare_start, discname=pn,labelval=string_clv )
+ self.st.out(self.template_comment_union_code_label_compare_start, discname=un.identifier(),labelval=string_clv )
self.st.inc_indent()
else:
self.st.out(self.template_comment_union_code_label_default_start )
self.st.out(self.template_comment_union_code_label_compare_end )
else:
self.st.out(self.template_comment_union_code_label_default_end )
-
+
+ self.st.dec_indent()
+ self.st.out(self.template_union_helper_function_end)
+
+
#
# Currently, get_CDR_alias is geared to finding typdef
+ #
+ # Handle structs, including recursive
+ #
+
def get_CDR_struct(self,type,pn):
- self.st.out(self.template_structure_start, name=type.name() )
# If I am a typedef struct {..}; node then find the struct node
else:
ntype = type.decl() # I am a struct node
- if ntype.recursive():
- sys.stderr.write("Error: idl2eth does not handle recursive structs yet \n")
- sys.exit(1)
-
- for m in ntype.members():
+ sname = self.namespace(ntype, "_")
+ self.st.out(self.template_structure_start, name=sname )
+
+ # Output a call to the struct helper function so I can handle recursive structs also.
+
+ self.st.out(self.template_decode_struct,name=sname)
+
+ self.st.out(self.template_structure_end, name=sname )
+
+ #
+ # genStructhelper()
+ #
+ # Generate private helper functions to decode a struct
+ #
+ # in: stnode ( a struct node)
+ #
+
+ def genStructHelper(self,st):
+ if self.DEBUG:
+ print "XXX genStructHelper"
+
+ sname = self.namespace(st, "_")
+ self.curr_sname = sname # update current opnode/exnode/stnode scoped name
+ if not self.fn_hash_built:
+ self.fn_hash[sname] = [] # init empty list as val for this sname key
+ # but only if the fn_hash is not already built
+
+ self.st.out(self.template_struct_helper_function_start, sname=sname, stname=st.repoId())
+ self.st.inc_indent()
+
+ self.st.out(self.template_helper_function_vars_start)
+ self.dumpCvars(sname)
+ self.st.out(self.template_helper_function_vars_end )
+
+ self.st.out(self.template_struct_helper_function_get_endianess)
+
+ for m in st.members():
for decl in m.declarators():
if decl.sizes(): # an array
indices = self.get_indices_from_sizes(decl.sizes())
self.addvar(self.c_i + decl.identifier() + ";")
self.st.inc_indent()
- self.getCDR3(m.memberType(), type.name() + "_" + decl.identifier() )
+ self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )
self.st.dec_indent()
self.st.out(self.template_get_CDR_array_end)
- else:
- self.getCDR3(m.memberType(), type.name() + "_" + decl.identifier() )
+ else:
+ self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )
+
+ self.st.dec_indent()
+ self.st.out(self.template_struct_helper_function_end)
+
- self.st.out(self.template_structure_end, name=type.name())
#
# Generate code to access a sequence of a type
#
+
def get_CDR_sequence(self,type, pn):
self.st.out(self.template_get_CDR_sequence_length, seqname=pn )
/* End struct \"@name@\" */
"""
+ template_union_start = """\
+/* Begin union \"@name@\" */
+"""
+
+
+ template_union_end = """\
+/* End union \"@name@\" */
+"""
+
#include <string.h>
#include <glib.h>
-#include "packet.h"
-#include "proto.h"
+#include <epan/packet.h>
+#include <epan/proto.h>
#include "packet-giop.h"
#include "plugins/plugin_api_defs.h"
gboolean be; /* big endianess */
guint32 offset_saved = (*offset); /* save in case we must back out */
- pinfo->current_proto = \"@disprot@\";
-
if (check_col(pinfo->cinfo, COL_PROTOCOL))
col_set_str(pinfo->cinfo, COL_PROTOCOL, \"@disprot@\");
- if (check_col(pinfo->cinfo, COL_INFO))
- col_clear(pinfo->cinfo, COL_INFO))
+/*
+ * Do not clear COL_INFO, as nothing is being written there by
+ * this dissector yet. So leave it as is from the GIOP dissector.
+ * TODO: add something useful to COL_INFO
+ * if (check_col(pinfo->cinfo, COL_INFO))
+ * col_clear(pinfo->cinfo, COL_INFO);
+ */
if (ptree) {
ti = proto_tree_add_item(ptree, proto_@dissname@, tvb, *offset, tvb_length(tvb) - *offset, FALSE);
}
"""
+
+#
+# template for struct helper code
+#
+
+
+ template_struct_helper_function_start = """\
+
+/* Struct = @stname@ */
+
+static void decode_@sname@_st(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
+
+ gboolean stream_is_big_endian; /* big endianess */
+"""
+
+
+
+
+ #
+ # Template for the helper function
+ # to get stream endianess from header
+ #
+
+ template_struct_helper_function_get_endianess = """\
+
+stream_is_big_endian = is_big_endian(header); /* get stream endianess */
+
+"""
+
+
+ template_struct_helper_function_end = """\
+}
+"""
+
+#
+# template for union helper code
+#
+
+
+ template_union_helper_function_start = """\
+
+/* Union = @unname@ */
+
+static void decode_@sname@_un(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
+
+ gboolean stream_is_big_endian; /* big endianess */
+"""
+
+
+
+
+ #
+ # Template for the helper function
+ # to get stream endianess from header
+ #
+
+ template_union_helper_function_get_endianess = """\
+
+stream_is_big_endian = is_big_endian(header); /* get stream endianess */
+
+"""
+
+
+ template_union_helper_function_end = """\
+}
+"""
+
+
+
#-------------------------------------------------------------#
# Value string templates #
#-------------------------------------------------------------#
"""
template_comment_union_code_label_compare_end = """\
- break;
+ return; /* End Compare for this discriminant type */
}
"""
/* Default Union Case End */
"""
+
+ #
+ # Templates for function prototypes.
+ # This is used in genDeclares() for declaring function prototypes
+ # for structs and union helper functions.
+ #
+
+ template_prototype_struct_start = """
+/* Struct prototype declaration Start */
+
+"""
+
+ template_prototype_struct_end = """
+/* Struct prototype declaration End */
+
+"""
+
+ template_prototype_struct_body = """
+
+/* Struct = @stname@ */
+
+static void decode_@name@_st(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation);
+
+"""
+
+ template_decode_struct = """
+
+decode_@name@_st(tvb, pinfo, tree, offset, header, operation);
+
+"""
+
+
+
+
+ template_prototype_union_start = """
+/* Union prototype declaration Start */
+
+"""
+
+ template_prototype_union_end = """
+/* Union prototype declaration End */
+
+"""
+
+ template_prototype_union_body = """
+
+/* Union = @unname@ */
+
+static void decode_@name@_un(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation);
+
+"""
+
+ template_decode_union = """
+
+decode_@name@_un(tvb, pinfo, tree, offset, header, operation);
+
+"""
+
+