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 # This program is free software; you can redistribute it and/or modify it
20 # under the terms of the GNU General Public License as published by
21 # the Free Software Foundation; either version 2 of the License, or
22 # (at your option) any later version.
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 # General Public License for more details.
29 # You should have received a copy of the GNU General Public License
30 # along with this program; if not, write to the Free Software
31 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35 # Omniidl Back-end which parses an IDL data structure provided by the frontend
36 # and generates packet-idl-xxx.[ch] for compiling as a dissector in
37 # Wireshark IP protocol anlayser.
44 # Crawl all the way down all branches until I hit "Operation", "Enum", "Attribute",
45 # "Struct" and "Union" nodes. Then store these nodes in lists.
47 # Pass these lists (via an object ref) to the src code
48 # generator (wireshark_gen) class and let it do the hard work !
51 # Don't forget structs can contain embedded structs etc .. so don't forget
52 # to peek inside and check :-)
57 """Wireshark IDL compiler back-end."""
59 from omniidl import idlast, idltype, idlvisitor, idlutil, output
62 from wireshark_gen import wireshark_gen_C
65 # This class finds the "Operation" nodes ,Enum Nodes, "Attribute" nodes, Struct Nodes
66 # and Union Nodes. Then it hands them off to an instance of the source code generator
67 # class "wireshark_gen"
70 class WiresharkVisitor:
72 DEBUG = 0 # debug flag
74 def __init__(self, st):
76 self.oplist = [] # list of operation nodes
77 self.enlist = [] # list of enum nodes
78 self.atlist = [] # list of attribute nodes
79 self.stlist = [] # list of struct nodes
80 self.unlist = [] # list of union nodes
83 def visitAST(self, node):
85 print "XXX visitAST() node = ", node
87 for n in node.declarations():
88 if isinstance(n, idlast.Module):
90 if isinstance(n, idlast.Interface):
91 self.visitInterface(n)
92 if isinstance(n, idlast.Operation):
93 self.visitOperation(n)
94 if isinstance(n, idlast.Attribute):
95 self.visitAttribute(n)
96 if isinstance(n, idlast.Enum):
98 if isinstance(n, idlast.Struct):
100 if isinstance(n, idlast.Union):
103 # Check for Typedef structs and unions
105 if isinstance(n, idlast.Typedef):
106 self.visitTypedef(n) # who are you ?
109 def visitModule(self, node):
111 print "XXX visitModule() node = ", node
113 for n in node.definitions():
114 if isinstance(n, idlast.Module):
116 if isinstance(n, idlast.Interface):
117 self.visitInterface(n)
118 if isinstance(n, idlast.Operation):
119 self.visitOperation(n)
120 if isinstance(n, idlast.Attribute):
121 self.visitAttribute(n)
122 if isinstance(n, idlast.Enum):
124 if isinstance(n, idlast.Struct):
126 if isinstance(n, idlast.Union):
129 # Check for Typedef structs and unions
131 if isinstance(n, idlast.Typedef):
132 self.visitTypedef(n) # who are you ?
135 def visitInterface(self, node):
137 print "XXX visitInterface() node = ", node
139 for c in node.callables():
140 if isinstance(c, idlast.Operation):
141 self.visitOperation(c)
142 if isinstance(c, idlast.Attribute):
143 self.visitAttribute(c)
145 for d in node.contents():
146 if isinstance(d, idlast.Enum):
149 if isinstance(d, idlast.Struct):
152 if isinstance(d, idlast.Union):
155 # Check for Typedef structs and unions
157 if isinstance(d, idlast.Typedef):
158 self.visitTypedef(d) # who are you ?
164 # populates the operations node list "oplist"
168 def visitOperation(self,opnode):
169 if not opnode in self.oplist:
170 self.oplist.append(opnode) # store operation node
175 # populates the attribute node list "atlist"
179 def visitAttribute(self,atnode):
180 if not atnode in self.atlist:
181 self.atlist.append(atnode) # store attribute node
187 # populates the Enum node list "enlist"
191 def visitEnum(self,enode):
192 if not enode in self.enlist:
193 self.enlist.append(enode) # store enum node if unique
198 # Search to see if its a typedef'd struct, union, or enum
200 # eg: typdef enum colors {red, green, blue } mycolors;
203 def visitTypedef(self,td):
204 d = td.aliasType() # get Type, possibly Declared
205 if isinstance(d,idltype.Declared):
206 self.visitDeclared(d)
212 # Search to see if its a struct, union, or enum
216 def visitDeclared(self,d):
217 if isinstance(d,idltype.Declared):
218 sue = d.decl() # grab the struct or union or enum
220 if isinstance(sue, idlast.Struct):
221 self.visitStruct(sue)
222 if isinstance(sue, idlast.Union):
224 if isinstance(sue, idlast.Enum):
233 # populates the struct node list "stlist"
234 # and checks its members also
238 def visitStruct(self,stnode):
239 if not stnode in self.stlist:
240 self.stlist.append(stnode) # store struct node if unique and avoid recursive loops
241 # if we come across recursive structs
243 for m in stnode.members(): # find embedded struct definitions within this
245 if isinstance(mt,idltype.Declared):
246 self.visitDeclared(mt) # if declared, then check it out
252 # populates the struct node list "unlist"
253 # and checks its members also
257 def visitUnion(self,unnode):
258 if not unnode in self.unlist:
259 self.unlist.append(unnode) # store union node if unique
261 if unnode.constrType(): # enum defined within switch type
262 if isinstance(unnode.switchType(),idltype.Declared):
263 self.visitDeclared(unnode.switchType())
265 for c in unnode.cases():
267 if isinstance(ct,idltype.Declared):
268 self.visitDeclared(ct) # if declared, then check it out
273 st = output.Stream(sys.stdout, 4) # set indent for stream
274 ev = WiresharkVisitor(st) # create visitor object
276 ev.visitAST(tree) # go find some operations
279 # Grab name of main IDL file being compiled.
281 # Assumption: Name is of the form abcdefg.xyz (eg: CosNaming.idl)
284 fname = path.basename(tree.file()) # grab basename only, dont care about path
285 nl = string.split(fname,".")[0] # split name of main IDL file using "." as separator
286 # and grab first field (eg: CosNaming)
290 print "XXX - Operation node ", i, " repoId() = ", i.repoId()
292 print "XXX - Attribute node ", i, " identifiers() = ", i.identifiers()
294 print "XXX - Enum node ", i, " repoId() = ", i.repoId()
296 print "XXX - Struct node ", i, " repoId() = ", i.repoId()
298 print "XXX - Union node ", i, " repoId() = ", i.repoId()
301 # create a C generator object
302 # and generate some C code
305 eg = wireshark_gen_C(ev.st, string.upper(nl), string.lower(nl), string.capitalize(nl) + " Dissector Using GIOP API")
306 eg.genCode(ev.oplist, ev.atlist, ev.enlist, ev.stlist, ev.unlist) # pass them onto the C generator
309 # Editor modelines - http://www.wireshark.org/tools/modelines.html
313 # indent-tabs-mode: nil
316 # vi: set shiftwidth=4 expandtab:
317 # :indentSize=4:noTabs=true: