2001-06-18 05:27:18 +00:00
|
|
|
# -*- python -*-
|
|
|
|
#
|
2004-07-18 00:24:25 +00:00
|
|
|
# $Id$
|
2001-06-18 05:27:18 +00:00
|
|
|
#
|
2006-06-06 23:27:21 +00:00
|
|
|
# File : wireshark_be.py
|
2001-06-18 05:27:18 +00:00
|
|
|
#
|
|
|
|
# Author : Frank Singleton (frank.singleton@ericsson.com)
|
|
|
|
#
|
|
|
|
# Copyright (C) 2001 Frank Singleton, Ericsson Inc.
|
|
|
|
#
|
2006-05-29 16:32:08 +00:00
|
|
|
# This file is a backend to "omniidl", used to generate "Wireshark"
|
2001-06-18 05:27:18 +00:00
|
|
|
# dissectors from IDL descriptions. The output language generated
|
|
|
|
# is "C". It will generate code to use the GIOP/IIOP get_CDR_XXX API.
|
|
|
|
#
|
|
|
|
#
|
2006-05-22 08:14:01 +00:00
|
|
|
# Please see packet-giop.h in Wireshark distro for API description.
|
2006-05-29 16:32:08 +00:00
|
|
|
# Wireshark is available at http://www.wireshark.org/
|
2001-06-18 05:27:18 +00:00
|
|
|
#
|
|
|
|
# Omniidl is part of the OmniOrb distribution, and is available at
|
|
|
|
# http://www.uk.research.att.com/omniORB/omniORB.html
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
# 02111-1307, USA.
|
|
|
|
#
|
|
|
|
# Description:
|
|
|
|
#
|
|
|
|
# Omniidl Back-end which parses an IDL data structure provided by the frontend
|
|
|
|
# and generates packet-idl-xxx.[ch] for compiling as a dissector in
|
2006-05-29 16:32:08 +00:00
|
|
|
# Wireshark IP protocol anlayser.
|
2001-06-18 05:27:18 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Strategy.
|
|
|
|
#
|
2001-11-19 22:58:49 +00:00
|
|
|
# Crawl all the way down all branches until I hit "Operation", "Enum", "Attribute",
|
|
|
|
# "Struct" and "Union" nodes. Then store these nodes in lists.
|
2001-06-18 05:27:18 +00:00
|
|
|
#
|
2001-11-19 22:58:49 +00:00
|
|
|
# Pass these lists (via an object ref) to the src code
|
2006-05-29 16:32:08 +00:00
|
|
|
# generator (wireshark_gen) class and let it do the hard work !
|
2001-06-18 05:27:18 +00:00
|
|
|
#
|
|
|
|
#
|
2001-11-19 22:58:49 +00:00
|
|
|
# Dont forget structs can contain embedded structs etc .. so dont forget
|
|
|
|
# to peek inside and check :-)
|
|
|
|
#
|
|
|
|
#
|
2001-06-18 05:27:18 +00:00
|
|
|
|
|
|
|
|
2006-05-29 16:32:08 +00:00
|
|
|
"""Wireshark IDL compiler back-end."""
|
2001-06-18 05:27:18 +00:00
|
|
|
|
|
|
|
from omniidl import idlast, idltype, idlvisitor, idlutil, output
|
|
|
|
import sys, string
|
2001-08-30 19:31:53 +00:00
|
|
|
from os import path
|
2006-05-29 16:32:08 +00:00
|
|
|
from wireshark_gen import wireshark_gen_C
|
2001-06-18 05:27:18 +00:00
|
|
|
|
|
|
|
#
|
2001-11-19 22:58:49 +00:00
|
|
|
# This class finds the "Operation" nodes ,Enum Nodes, "Attribute" nodes, Struct Nodes
|
|
|
|
# and Union Nodes. Then it hands them off to an instance of the source code generator
|
2006-05-29 16:32:08 +00:00
|
|
|
# class "wireshark_gen"
|
2001-06-18 05:27:18 +00:00
|
|
|
#
|
|
|
|
|
2006-05-29 16:32:08 +00:00
|
|
|
class WiresharkVisitor:
|
2001-11-19 22:58:49 +00:00
|
|
|
|
|
|
|
DEBUG = 0 # debug flag
|
2001-06-18 05:27:18 +00:00
|
|
|
|
|
|
|
def __init__(self, st):
|
|
|
|
self.st = st
|
|
|
|
self.oplist = [] # list of operation nodes
|
2001-10-12 17:14:41 +00:00
|
|
|
self.enlist = [] # list of enum nodes
|
2001-07-27 18:35:22 +00:00
|
|
|
self.atlist = [] # list of attribute nodes
|
2001-11-19 22:58:49 +00:00
|
|
|
self.stlist = [] # list of struct nodes
|
|
|
|
self.unlist = [] # list of union nodes
|
2001-07-27 18:35:22 +00:00
|
|
|
|
2001-06-18 05:27:18 +00:00
|
|
|
|
|
|
|
def visitAST(self, node):
|
2001-11-19 22:58:49 +00:00
|
|
|
if self.DEBUG:
|
|
|
|
print "XXX visitAST() node = ", node
|
|
|
|
|
2001-06-18 05:27:18 +00:00
|
|
|
for n in node.declarations():
|
|
|
|
if isinstance(n, idlast.Module):
|
|
|
|
self.visitModule(n)
|
|
|
|
if isinstance(n, idlast.Interface):
|
|
|
|
self.visitInterface(n)
|
|
|
|
if isinstance(n, idlast.Operation):
|
|
|
|
self.visitOperation(n)
|
2001-07-27 18:35:22 +00:00
|
|
|
if isinstance(n, idlast.Attribute):
|
|
|
|
self.visitAttribute(n)
|
2001-10-12 17:14:41 +00:00
|
|
|
if isinstance(n, idlast.Enum):
|
|
|
|
self.visitEnum(n)
|
2001-11-19 22:58:49 +00:00
|
|
|
if isinstance(n, idlast.Struct):
|
|
|
|
self.visitStruct(n)
|
|
|
|
if isinstance(n, idlast.Union):
|
|
|
|
self.visitUnion(n)
|
2001-06-18 05:27:18 +00:00
|
|
|
|
2001-11-19 22:58:49 +00:00
|
|
|
# Check for Typedef structs and unions
|
|
|
|
|
|
|
|
if isinstance(n, idlast.Typedef):
|
|
|
|
self.visitTypedef(n) # who are you ?
|
|
|
|
|
|
|
|
|
2001-06-18 05:27:18 +00:00
|
|
|
def visitModule(self, node):
|
2001-11-19 22:58:49 +00:00
|
|
|
if self.DEBUG:
|
|
|
|
print "XXX visitModule() node = ", node
|
|
|
|
|
2001-06-18 05:27:18 +00:00
|
|
|
for n in node.definitions():
|
|
|
|
if isinstance(n, idlast.Module):
|
|
|
|
self.visitModule(n)
|
|
|
|
if isinstance(n, idlast.Interface):
|
|
|
|
self.visitInterface(n)
|
|
|
|
if isinstance(n, idlast.Operation):
|
|
|
|
self.visitOperation(n)
|
2001-07-27 18:35:22 +00:00
|
|
|
if isinstance(n, idlast.Attribute):
|
|
|
|
self.visitAttribute(n)
|
2001-10-12 17:14:41 +00:00
|
|
|
if isinstance(n, idlast.Enum):
|
|
|
|
self.visitEnum(n)
|
2001-11-19 22:58:49 +00:00
|
|
|
if isinstance(n, idlast.Struct):
|
|
|
|
self.visitStruct(n)
|
|
|
|
if isinstance(n, idlast.Union):
|
|
|
|
self.visitUnion(n)
|
|
|
|
|
|
|
|
# Check for Typedef structs and unions
|
|
|
|
|
|
|
|
if isinstance(n, idlast.Typedef):
|
|
|
|
self.visitTypedef(n) # who are you ?
|
|
|
|
|
|
|
|
|
2001-06-18 05:27:18 +00:00
|
|
|
def visitInterface(self, node):
|
2001-11-19 22:58:49 +00:00
|
|
|
if self.DEBUG:
|
|
|
|
print "XXX visitInterface() node = ", node
|
|
|
|
|
2001-07-27 18:35:22 +00:00
|
|
|
for c in node.callables():
|
|
|
|
if isinstance(c, idlast.Operation):
|
|
|
|
self.visitOperation(c)
|
|
|
|
if isinstance(c, idlast.Attribute):
|
|
|
|
self.visitAttribute(c)
|
|
|
|
|
2001-10-12 17:14:41 +00:00
|
|
|
for d in node.contents():
|
|
|
|
if isinstance(d, idlast.Enum):
|
2001-11-19 22:58:49 +00:00
|
|
|
self.visitEnum(d)
|
|
|
|
|
|
|
|
if isinstance(d, idlast.Struct):
|
|
|
|
self.visitStruct(d)
|
|
|
|
|
|
|
|
if isinstance(d, idlast.Union):
|
|
|
|
self.visitUnion(d)
|
|
|
|
|
|
|
|
# Check for Typedef structs and unions
|
|
|
|
|
|
|
|
if isinstance(d, idlast.Typedef):
|
|
|
|
self.visitTypedef(d) # who are you ?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-06-18 05:27:18 +00:00
|
|
|
#
|
|
|
|
# visitOperation
|
|
|
|
#
|
|
|
|
# populates the operations node list "oplist"
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
def visitOperation(self,opnode):
|
2001-11-19 22:58:49 +00:00
|
|
|
if not opnode in self.oplist:
|
|
|
|
self.oplist.append(opnode) # store operation node
|
2001-07-27 18:35:22 +00:00
|
|
|
|
2001-06-18 05:27:18 +00:00
|
|
|
#
|
2001-07-27 18:35:22 +00:00
|
|
|
# visitAttribute
|
2001-06-18 05:27:18 +00:00
|
|
|
#
|
2001-07-27 18:35:22 +00:00
|
|
|
# populates the attribute node list "atlist"
|
2001-06-18 05:27:18 +00:00
|
|
|
#
|
2001-07-27 18:35:22 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
def visitAttribute(self,atnode):
|
2001-11-19 22:58:49 +00:00
|
|
|
if not atnode in self.atlist:
|
|
|
|
self.atlist.append(atnode) # store attribute node
|
|
|
|
|
2001-06-18 05:27:18 +00:00
|
|
|
|
2001-10-12 17:14:41 +00:00
|
|
|
#
|
|
|
|
# visitEnum
|
|
|
|
#
|
2001-11-19 22:58:49 +00:00
|
|
|
# populates the Enum node list "enlist"
|
2001-10-12 17:14:41 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
def visitEnum(self,enode):
|
2001-11-19 22:58:49 +00:00
|
|
|
if not enode in self.enlist:
|
|
|
|
self.enlist.append(enode) # store enum node if unique
|
|
|
|
|
|
|
|
#
|
|
|
|
# visitTypedef
|
|
|
|
#
|
|
|
|
# Search to see if its a typedef'd struct, union, or enum
|
|
|
|
#
|
|
|
|
# eg: typdef enum colors {red, green, blue } mycolors;
|
|
|
|
#
|
|
|
|
|
|
|
|
def visitTypedef(self,td):
|
|
|
|
d = td.aliasType() # get Type, possibly Declared
|
|
|
|
if isinstance(d,idltype.Declared):
|
|
|
|
self.visitDeclared(d)
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# visitDeclared
|
|
|
|
#
|
|
|
|
# Search to see if its a struct, union, or enum
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
def visitDeclared(self,d):
|
|
|
|
if isinstance(d,idltype.Declared):
|
|
|
|
sue = d.decl() # grab the struct or union or enum
|
|
|
|
|
|
|
|
if isinstance(sue, idlast.Struct):
|
|
|
|
self.visitStruct(sue)
|
|
|
|
if isinstance(sue, idlast.Union):
|
|
|
|
self.visitUnion(sue)
|
|
|
|
if isinstance(sue, idlast.Enum):
|
|
|
|
self.visitEnum(sue)
|
|
|
|
|
2001-10-12 17:14:41 +00:00
|
|
|
|
|
|
|
|
2001-11-19 22:58:49 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# visitStruct
|
|
|
|
#
|
|
|
|
# populates the struct node list "stlist"
|
|
|
|
# and checks its members also
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
def visitStruct(self,stnode):
|
|
|
|
if not stnode in self.stlist:
|
|
|
|
self.stlist.append(stnode) # store struct node if unique and avoid recursive loops
|
|
|
|
# if we come across recursive structs
|
|
|
|
|
|
|
|
for m in stnode.members(): # find embedded struct definitions within this
|
|
|
|
mt = m.memberType()
|
|
|
|
if isinstance(mt,idltype.Declared):
|
|
|
|
self.visitDeclared(mt) # if declared, then check it out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# visitUnion
|
|
|
|
#
|
|
|
|
# populates the struct node list "unlist"
|
|
|
|
# and checks its members also
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
def visitUnion(self,unnode):
|
|
|
|
if not unnode in self.unlist:
|
|
|
|
self.unlist.append(unnode) # store union node if unique
|
|
|
|
|
|
|
|
if unnode.constrType(): # enum defined within switch type
|
|
|
|
if isinstance(unnode.switchType(),idltype.Declared):
|
|
|
|
self.visitDeclared(unnode.switchType())
|
|
|
|
|
|
|
|
for c in unnode.cases():
|
|
|
|
ct = c.caseType()
|
|
|
|
if isinstance(ct,idltype.Declared):
|
|
|
|
self.visitDeclared(ct) # if declared, then check it out
|
|
|
|
|
2001-06-18 05:27:18 +00:00
|
|
|
|
2001-11-19 22:58:49 +00:00
|
|
|
|
|
|
|
|
2001-06-18 05:27:18 +00:00
|
|
|
def run(tree, args):
|
|
|
|
|
|
|
|
st = output.Stream(sys.stdout, 4) # set indent for stream
|
2006-05-29 16:32:08 +00:00
|
|
|
ev = WiresharkVisitor(st) # create visitor object
|
2001-06-18 05:27:18 +00:00
|
|
|
|
|
|
|
ev.visitAST(tree) # go find some operations
|
2001-07-17 18:36:46 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Grab name of main IDL file being compiled.
|
|
|
|
#
|
|
|
|
# Assumption: Name is of the form abcdefg.xyz (eg: CosNaming.idl)
|
|
|
|
#
|
2001-11-19 22:58:49 +00:00
|
|
|
|
2001-08-30 19:31:53 +00:00
|
|
|
fname = path.basename(tree.file()) # grab basename only, dont care about path
|
|
|
|
nl = string.split(fname,".")[0] # split name of main IDL file using "." as separator
|
2001-07-17 18:36:46 +00:00
|
|
|
# and grab first field (eg: CosNaming)
|
2001-06-18 05:27:18 +00:00
|
|
|
|
2001-11-19 22:58:49 +00:00
|
|
|
if ev.DEBUG:
|
|
|
|
for i in ev.oplist:
|
|
|
|
print "XXX - Operation node ", i, " repoId() = ", i.repoId()
|
|
|
|
for i in ev.atlist:
|
|
|
|
print "XXX - Attribute node ", i, " identifiers() = ", i.identifiers()
|
|
|
|
for i in ev.enlist:
|
|
|
|
print "XXX - Enum node ", i, " repoId() = ", i.repoId()
|
|
|
|
for i in ev.stlist:
|
|
|
|
print "XXX - Struct node ", i, " repoId() = ", i.repoId()
|
|
|
|
for i in ev.unlist:
|
|
|
|
print "XXX - Union node ", i, " repoId() = ", i.repoId()
|
|
|
|
|
|
|
|
|
2001-06-18 05:27:18 +00:00
|
|
|
# create a C generator object
|
|
|
|
# and generate some C code
|
2001-11-19 22:58:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2001-07-17 18:36:46 +00:00
|
|
|
|
2006-05-29 16:32:08 +00:00
|
|
|
eg = wireshark_gen_C(ev.st, string.upper(nl), string.lower(nl), string.capitalize(nl) + " Dissector Using GIOP API")
|
2001-11-19 22:58:49 +00:00
|
|
|
eg.genCode(ev.oplist, ev.atlist, ev.enlist, ev.stlist, ev.unlist) # pass them onto the C generator
|
2001-06-18 05:27:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|