root/branch/PacketManipulator/PM/Backend/UMPA/__init__.py @ 3500

Revision 3500, 6.4 kB (checked in by nopper, 5 years ago)

rewriting UMPA backend

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (C) 2008 Adriano Monteiro Marques
4#
5# Author: Francesco Piccinno <stack.box@gmail.com>
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
21import os, os.path
22
23from umpa import protocols
24from umpa.packets import Packet
25from umpa.protocols._protocols import Protocol
26from umpa.protocols._fields import *
27
28from inspect import isclass
29
30# Globals UMPA protocols
31gprotos = []
32
33# Locals User defined protocols
34lprotos = []
35
36# We need to get all the protocols from the __path__
37# of protocols and also import the protocols defined
38# by the user from the .umit/umpa/ directory
39
40def load_gprotocols():
41    path = protocols.__path__[0]
42    glob = []
43
44    for fname in os.listdir(path):
45        if not fname.lower().endswith(".py") or fname[0] == "_":
46            continue
47
48        try:
49            # We'll try to load this
50            module = __import__(
51                "%s.%s" % (protocols.__name__, fname.replace(".py", "")),
52                fromlist=[protocols]
53            )
54
55            glob.extend(
56                filter(lambda x: not isinstance(x, Protocol), module.protocols)
57            )
58
59        except Exception, err:
60            print "Ignoring exception", err
61
62    return glob
63
64gprotos = load_gprotocols()
65
66###############################################################################
67# Protocols functions
68###############################################################################
69def get_protocols():
70    return gprotos
71
72def get_proto_class_name(protok):
73    return protok.__name__
74
75def get_proto_name(proto_inst):
76    return get_proto_class_name(proto_inst.__class__)
77
78def get_proto(proto_name):
79    for proto in gprotos:
80        if proto.__name__ == proto_name:
81            return proto
82
83    for proto in lprotos:
84        if proto.__name__ == proto_name:
85            return proto
86
87    print "Protocol named %s not found." % proto_name
88    return None
89
90def get_proto_layer(proto):
91    return proto.layer
92
93def get_proto_fields(proto_inst):
94    return proto_inst.get_fields()
95
96###############################################################################
97# Packet functions
98###############################################################################
99
100def get_packet_protos(packet):
101    for proto in packet.root.protos:
102        yield proto
103
104def get_packet_raw(metapack):
105    return metapack.root.get_raw()
106
107###############################################################################
108# Fields functions
109###############################################################################
110
111def get_field_desc(field):
112    return field.__doc__
113
114def get_field_name(field):
115    return field.name
116
117def get_field_value(proto, field):
118    return field.get()
119
120def set_field_value(proto, field, value):
121    field.set(value)
122
123def get_field_value_repr(proto, field):
124    ret = get_field_value(proto, field)
125    out = ""
126
127    if isinstance(ret, dict):
128        for it in ret:
129            if ret[it].get():
130                out += "+%s" % it
131
132        return out[1:]
133
134    if isinstance(ret, (list, tuple)):
135        for it in ret:
136            out += "+%s" % it
137
138        return out[1:]
139
140    return str(ret)
141
142def get_field_size(proto, field):
143    return field.bits
144
145def get_field_offset(packet, proto, field):
146    return proto.get_offset(field)
147
148def get_field_enumeration_s2i(field):
149    return field.enumerable.items()
150
151def get_field_enumeration_i2s(field):
152    return [(v, k) for (k, v) in field.enumerable.items()]
153
154def is_field_autofilled(field):
155    return field.auto
156
157###############################################################################
158# Flag fields functions
159###############################################################################
160
161def set_keyflag_value(proto, flag, key, value):
162    return flag.get()[key].set(value)
163
164def get_keyflag_value(proto, flag, key):
165    return flag.get()[key].get()
166
167def get_flag_keys(flag_inst):
168    for key in flag_inst._ordered_fields:
169        yield key
170
171###############################################################################
172# Checking functions
173###############################################################################
174
175def is_field(field):
176    return isinstance(field, Field)
177
178def is_flags(field):
179    return isinstance(field, Flags)
180
181def is_proto(proto):
182    return isinstance(proto, Protocol)
183
184def implements(obj, klass):
185    return isinstance(obj, klass)
186
187class MetaPacket:
188    def __init__(self, proto=None):
189        self.root = Packet(proto, strict=False)
190
191    def insert(self, proto, layer):
192        # Only append for the moment
193        if layer == -1:
194            self.root.include(proto.root.protos[0])
195            return True
196
197        return False
198
199    def get_protocol_str(self):
200        return get_proto_name(self.root)
201
202    def summary(self):
203        # We need to ask for a method here
204        return "%s packet" % self.get_protocol_str()
205
206    def get_time(self):
207        # We need to ask for a method here
208        return "N/A"
209
210    def get_dest(self):
211        # We need to ask for a method here
212        return "N/A"
213
214    def get_source(self):
215        # We need to ask for a method here
216        return "N/A"
217
218###############################################################################
219# Functions used by dialogs but not defined
220###############################################################################
221
222def find_all_devs():
223    return []
224
225PMField = Field
226PMFlagsField = Flags
227
228PMBitField          = BitField
229PMIPField           = IPv4AddrField
230PMByteField         = None
231PMShortField        = None
232PMLEShortField      = None
233PMIntField          = IntField
234PMSignedIntField    = None
235PMLEIntField        = None
236PMLESignedIntField  = None
237PMLongField         = None
238PMLELongField       = None
239PMStrField          = None
240PMLenField          = None
241PMRDLenField        = None
242PMFieldLenField     = None
243PMBCDFloatField     = None
244PMEnumField         = EnumField
Note: See TracBrowser for help on using the browser.