3 # File : wireshark_be.py
5 # Author : Frank Singleton (frank.singleton@ericsson.com)
7 # Copyright (C) 2001 Frank Singleton, Ericsson Inc.
9 # This file is a backend to "omniidl", used to generate "Wireshark"
10 # dissectors from IDL descriptions. The output language generated
11 # is "C". It will generate code to use the GIOP/IIOP get_CDR_XXX API.
13 # Please see packet-giop.h in Wireshark distro for API description.
14 # Wireshark is available at http://www.wireshark.org/
16 # Omniidl is part of the OmniOrb distribution, and is available at
17 # http://omniorb.sourceforge.net
19 # SPDX-License-Identifier: GPL-2.0-or-later
23 # Omniidl Back-end which parses an IDL data structure provided by the frontend
24 # and generates packet-idl-xxx.[ch] for compiling as a dissector in
25 # Wireshark IP protocol anlayser.
32 # Crawl all the way down all branches until I hit "Operation", "Enum", "Attribute",
33 # "Struct" and "Union" nodes. Then store these nodes in lists.
35 # Pass these lists (via an object ref) to the src code
36 # generator (wireshark_gen) class and let it do the hard work !
39 # Don't forget structs can contain embedded structs etc .. so don't forget
40 # to peek inside and check :-)
45 """Wireshark IDL compiler back-end."""
47 from omniidl import idlast, idltype, idlvisitor, idlutil, output
50 from wireshark_gen import wireshark_gen_C
53 # This class finds the "Operation" nodes ,Enum Nodes, "Attribute" nodes, Struct Nodes
54 # and Union Nodes. Then it hands them off to an instance of the source code generator
55 # class "wireshark_gen"
58 class WiresharkVisitor:
60 DEBUG = 0 # debug flag
62 def __init__(self, st):
64 self.oplist = [] # list of operation nodes
65 self.enlist = [] # list of enum nodes
66 self.atlist = [] # list of attribute nodes
67 self.stlist = [] # list of struct nodes
68 self.unlist = [] # list of union nodes
71 def visitAST(self, node):
73 print "XXX visitAST() node = ", node
75 for n in node.declarations():
76 if isinstance(n, idlast.Module):
78 if isinstance(n, idlast.Interface):
79 self.visitInterface(n)
80 if isinstance(n, idlast.Operation):
81 self.visitOperation(n)
82 if isinstance(n, idlast.Attribute):
83 self.visitAttribute(n)
84 if isinstance(n, idlast.Enum):
86 if isinstance(n, idlast.Struct):
88 if isinstance(n, idlast.Union):
91 # Check for Typedef structs and unions
93 if isinstance(n, idlast.Typedef):
94 self.visitTypedef(n) # who are you ?
97 def visitModule(self, node):
99 print "XXX visitModule() node = ", node
101 for n in node.definitions():
102 if isinstance(n, idlast.Module):
104 if isinstance(n, idlast.Interface):
105 self.visitInterface(n)
106 if isinstance(n, idlast.Operation):
107 self.visitOperation(n)
108 if isinstance(n, idlast.Attribute):
109 self.visitAttribute(n)
110 if isinstance(n, idlast.Enum):
112 if isinstance(n, idlast.Struct):
114 if isinstance(n, idlast.Union):
117 # Check for Typedef structs and unions
119 if isinstance(n, idlast.Typedef):
120 self.visitTypedef(n) # who are you ?
123 def visitInterface(self, node):
125 print "XXX visitInterface() node = ", node
127 for c in node.callables():
128 if isinstance(c, idlast.Operation):
129 self.visitOperation(c)
130 if isinstance(c, idlast.Attribute):
131 self.visitAttribute(c)
133 for d in node.contents():
134 if isinstance(d, idlast.Enum):
137 if isinstance(d, idlast.Struct):
140 if isinstance(d, idlast.Union):
143 # Check for Typedef structs and unions
145 if isinstance(d, idlast.Typedef):
146 self.visitTypedef(d) # who are you ?
152 # populates the operations node list "oplist"
156 def visitOperation(self,opnode):
157 if not opnode in self.oplist:
158 self.oplist.append(opnode) # store operation node
163 # populates the attribute node list "atlist"
167 def visitAttribute(self,atnode):
168 if not atnode in self.atlist:
169 self.atlist.append(atnode) # store attribute node
175 # populates the Enum node list "enlist"
179 def visitEnum(self,enode):
180 if not enode in self.enlist:
181 self.enlist.append(enode) # store enum node if unique
186 # Search to see if its a typedef'd struct, union, or enum
188 # eg: typdef enum colors {red, green, blue } mycolors;
191 def visitTypedef(self,td):
192 d = td.aliasType() # get Type, possibly Declared
193 if isinstance(d,idltype.Declared):
194 self.visitDeclared(d)
200 # Search to see if its a struct, union, or enum
204 def visitDeclared(self,d):
205 if isinstance(d,idltype.Declared):
206 sue = d.decl() # grab the struct or union or enum
208 if isinstance(sue, idlast.Struct):
209 self.visitStruct(sue)
210 if isinstance(sue, idlast.Union):
212 if isinstance(sue, idlast.Enum):
221 # populates the struct node list "stlist"
222 # and checks its members also
226 def visitStruct(self,stnode):
227 if not stnode in self.stlist:
228 self.stlist.append(stnode) # store struct node if unique and avoid recursive loops
229 # if we come across recursive structs
231 for m in stnode.members(): # find embedded struct definitions within this
233 if isinstance(mt,idltype.Declared):
234 self.visitDeclared(mt) # if declared, then check it out
240 # populates the struct node list "unlist"
241 # and checks its members also
245 def visitUnion(self,unnode):
246 if not unnode in self.unlist:
247 self.unlist.append(unnode) # store union node if unique
249 if unnode.constrType(): # enum defined within switch type
250 if isinstance(unnode.switchType(),idltype.Declared):
251 self.visitDeclared(unnode.switchType())
253 for c in unnode.cases():
255 if isinstance(ct,idltype.Declared):
256 self.visitDeclared(ct) # if declared, then check it out
261 st = output.Stream(sys.stdout, 4) # set indent for stream
262 ev = WiresharkVisitor(st) # create visitor object
264 ev.visitAST(tree) # go find some operations
267 # Grab name of main IDL file being compiled.
269 # Assumption: Name is of the form abcdefg.xyz (eg: CosNaming.idl)
272 fname = path.basename(tree.file()) # grab basename only, dont care about path
273 nl = string.split(fname,".")[0] # split name of main IDL file using "." as separator
274 # and grab first field (eg: CosNaming)
278 print "XXX - Operation node ", i, " repoId() = ", i.repoId()
280 print "XXX - Attribute node ", i, " identifiers() = ", i.identifiers()
282 print "XXX - Enum node ", i, " repoId() = ", i.repoId()
284 print "XXX - Struct node ", i, " repoId() = ", i.repoId()
286 print "XXX - Union node ", i, " repoId() = ", i.repoId()
289 # create a C generator object
290 # and generate some C code
293 eg = wireshark_gen_C(ev.st, string.upper(nl), string.lower(nl), string.capitalize(nl) + " Dissector Using GIOP API")
294 eg.genCode(ev.oplist, ev.atlist, ev.enlist, ev.stlist, ev.unlist) # pass them onto the C generator
297 # Editor modelines - http://www.wireshark.org/tools/modelines.html
301 # indent-tabs-mode: nil
304 # vi: set shiftwidth=4 expandtab:
305 # :indentSize=4:noTabs=true: