summaryrefslogtreecommitdiffstats
path: root/tools/wireshark_be.py
blob: 02fcf5ab3503baf6a65422f972063b7e5ff065b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# -*- python -*-
#
#    File      : wireshark_be.py
#
#    Author    : Frank Singleton (frank.singleton@ericsson.com)
#
#    Copyright (C) 2001 Frank Singleton, Ericsson Inc.
#
#  This file is a backend to "omniidl", used to generate "Wireshark"
#  dissectors from IDL descriptions. The output language generated
#  is "C". It will generate code to use the GIOP/IIOP get_CDR_XXX API.
#
#  Please see packet-giop.h in Wireshark distro for API description.
#  Wireshark is available at https://www.wireshark.org/
#
#  Omniidl is part of the OmniOrb distribution, and is available at
#  http://omniorb.sourceforge.net
#
#  SPDX-License-Identifier: GPL-2.0-or-later


# 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 Wireshark.
#
#
# Strategy.
#
# Crawl all the way down all branches until I hit  "Operation", "Enum", "Attribute",
# "Struct" and "Union" nodes.  Then store these nodes in lists.
#
# Pass these lists (via an object ref) to the src code
# generator (wireshark_gen) class and let it do the hard work !
#
#
# Don't forget structs can contain embedded structs etc .. so don't forget
# to peek inside and check :-)


"""Wireshark IDL compiler back-end."""

from __future__ import print_function

import string
import sys
from os import path

from omniidl import idlast, idltype, output

from wireshark_gen import wireshark_gen_C


class WiresharkVisitor:
    """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
    class "wireshark_gen" """

    def __init__(self, st, debug=False):
        self.DEBUG = debug
        self.st = st
        self.oplist = []  # list of operation nodes
        self.enlist = []  # list of enum nodes
        self.atlist = []  # list of attribute nodes
        self.stlist = []  # list of struct nodes
        self.unlist = []  # list of union nodes

    def visitAST(self, node):
        if self.DEBUG:
            print("XXX visitAST() node = ", node)

        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)
            if isinstance(n, idlast.Attribute):
                self.visitAttribute(n)
            if isinstance(n, idlast.Enum):
                self.visitEnum(n)
            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 ?

    def visitModule(self, node):
        if self.DEBUG:
            print("XXX visitModule() node = ", node)

        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)
            if isinstance(n, idlast.Attribute):
                self.visitAttribute(n)
            if isinstance(n, idlast.Enum):
                self.visitEnum(n)
            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 ?

    def visitInterface(self, node):
        if self.DEBUG:
            print("XXX visitInterface() node = ", node)

        for c in node.callables():
            if isinstance(c, idlast.Operation):
                self.visitOperation(c)
            if isinstance(c, idlast.Attribute):
                self.visitAttribute(c)

        for d in node.contents():
            if isinstance(d, idlast.Enum):
                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 ?

    def visitOperation(self, opnode):
        """populates the operations node list "oplist" """
        if opnode not in self.oplist:
            self.oplist.append(opnode)  # store operation node

    def visitAttribute(self, atnode):
        """populates the attribute node list "atlist" """
        if atnode not in self.atlist:
            self.atlist.append(atnode)  # store attribute node

    def visitEnum(self, enode):
        """populates the Enum node list "enlist" """
        if enode not in self.enlist:
            self.enlist.append(enode)  # store enum node if unique

    def visitTypedef(self, td):
        """Search to see if its a typedef'd struct, union, or enum

        eg: typdef enum colors {red, green, blue } mycolors;
        """

        d = td.aliasType()  # get Type, possibly Declared
        if isinstance(d, idltype.Declared):
            self.visitDeclared(d)

    def visitDeclared(self, d):
        """Search to see if its a struct, union, or enum"""
        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)

    def visitStruct(self, stnode):
        # populates the struct node list "stlist"
        # and checks its members also
        if stnode not 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

    def visitUnion(self, unnode):
        # populates the struct node list "unlist"
        # and checks its members also
        if unnode not 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


def run(tree, args):

    DEBUG = "debug" in args
    AGGRESSIVE = "aggressive" in args

    st = output.Stream(sys.stdout, 4)  # set indent for stream
    ev = WiresharkVisitor(st, DEBUG)  # create visitor object

    ev.visitAST(tree)  # go find some operations

    # Grab name of main IDL file being compiled.
    #
    # Assumption: Name is of the form   abcdefg.xyz  (eg: CosNaming.idl)

    fname = path.basename(tree.file())  # grab basename only, don't care about path
    nl = fname.split(".")[0]  # split name of main IDL file using "." as separator
                                      # and grab first field (eg: CosNaming)

    if 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())

    # create a C generator object
    # and generate some C code

    eg = wireshark_gen_C(ev.st,
                         nl.upper(),
                         nl.lower(),
                         nl.capitalize() + " Dissector Using GIOP API",
                         debug=DEBUG,
                         aggressive=AGGRESSIVE)

    eg.genCode(ev.oplist, ev.atlist, ev.enlist, ev.stlist, ev.unlist)  # pass them onto the C generator

#
# Editor modelines  -  https://www.wireshark.org/tools/modelines.html
#
# Local variables:
# c-basic-offset: 4
# indent-tabs-mode: nil
# End:
#
# vi: set shiftwidth=4 expandtab:
# :indentSize=4:noTabs=true:
#