| 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 | |
|---|
| 21 | import os, os.path |
|---|
| 22 | |
|---|
| 23 | from umpa import protocols |
|---|
| 24 | from umpa.packets import Packet |
|---|
| 25 | from umpa.protocols._protocols import Protocol |
|---|
| 26 | from umpa.protocols._fields import * |
|---|
| 27 | |
|---|
| 28 | from inspect import isclass |
|---|
| 29 | |
|---|
| 30 | # Globals UMPA protocols |
|---|
| 31 | gprotos = [] |
|---|
| 32 | |
|---|
| 33 | # Locals User defined protocols |
|---|
| 34 | lprotos = [] |
|---|
| 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 | |
|---|
| 40 | def 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 | |
|---|
| 64 | gprotos = load_gprotocols() |
|---|
| 65 | |
|---|
| 66 | ############################################################################### |
|---|
| 67 | # Protocols functions |
|---|
| 68 | ############################################################################### |
|---|
| 69 | def get_protocols(): |
|---|
| 70 | return gprotos |
|---|
| 71 | |
|---|
| 72 | def get_proto_class_name(protok): |
|---|
| 73 | return protok.__name__ |
|---|
| 74 | |
|---|
| 75 | def get_proto_name(proto_inst): |
|---|
| 76 | return get_proto_class_name(proto_inst.__class__) |
|---|
| 77 | |
|---|
| 78 | def 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 | |
|---|
| 90 | def get_proto_layer(proto): |
|---|
| 91 | return proto.layer |
|---|
| 92 | |
|---|
| 93 | def get_proto_fields(proto_inst): |
|---|
| 94 | return proto_inst.get_fields() |
|---|
| 95 | |
|---|
| 96 | ############################################################################### |
|---|
| 97 | # Packet functions |
|---|
| 98 | ############################################################################### |
|---|
| 99 | |
|---|
| 100 | def get_packet_protos(packet): |
|---|
| 101 | for proto in packet.root.protos: |
|---|
| 102 | yield proto |
|---|
| 103 | |
|---|
| 104 | def get_packet_raw(metapack): |
|---|
| 105 | return metapack.root.get_raw() |
|---|
| 106 | |
|---|
| 107 | ############################################################################### |
|---|
| 108 | # Fields functions |
|---|
| 109 | ############################################################################### |
|---|
| 110 | |
|---|
| 111 | def get_field_desc(field): |
|---|
| 112 | return field.__doc__ |
|---|
| 113 | |
|---|
| 114 | def get_field_name(field): |
|---|
| 115 | return field.name |
|---|
| 116 | |
|---|
| 117 | def get_field_value(proto, field): |
|---|
| 118 | return field.get() |
|---|
| 119 | |
|---|
| 120 | def set_field_value(proto, field, value): |
|---|
| 121 | field.set(value) |
|---|
| 122 | |
|---|
| 123 | def 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 | |
|---|
| 142 | def get_field_size(proto, field): |
|---|
| 143 | return field.bits |
|---|
| 144 | |
|---|
| 145 | def get_field_offset(packet, proto, field): |
|---|
| 146 | return proto.get_offset(field) |
|---|
| 147 | |
|---|
| 148 | def get_field_enumeration_s2i(field): |
|---|
| 149 | return field.enumerable.items() |
|---|
| 150 | |
|---|
| 151 | def get_field_enumeration_i2s(field): |
|---|
| 152 | return [(v, k) for (k, v) in field.enumerable.items()] |
|---|
| 153 | |
|---|
| 154 | def is_field_autofilled(field): |
|---|
| 155 | return field.auto |
|---|
| 156 | |
|---|
| 157 | ############################################################################### |
|---|
| 158 | # Flag fields functions |
|---|
| 159 | ############################################################################### |
|---|
| 160 | |
|---|
| 161 | def set_keyflag_value(proto, flag, key, value): |
|---|
| 162 | return flag.get()[key].set(value) |
|---|
| 163 | |
|---|
| 164 | def get_keyflag_value(proto, flag, key): |
|---|
| 165 | return flag.get()[key].get() |
|---|
| 166 | |
|---|
| 167 | def get_flag_keys(flag_inst): |
|---|
| 168 | for key in flag_inst._ordered_fields: |
|---|
| 169 | yield key |
|---|
| 170 | |
|---|
| 171 | ############################################################################### |
|---|
| 172 | # Checking functions |
|---|
| 173 | ############################################################################### |
|---|
| 174 | |
|---|
| 175 | def is_field(field): |
|---|
| 176 | return isinstance(field, Field) |
|---|
| 177 | |
|---|
| 178 | def is_flags(field): |
|---|
| 179 | return isinstance(field, Flags) |
|---|
| 180 | |
|---|
| 181 | def is_proto(proto): |
|---|
| 182 | return isinstance(proto, Protocol) |
|---|
| 183 | |
|---|
| 184 | def implements(obj, klass): |
|---|
| 185 | return isinstance(obj, klass) |
|---|
| 186 | |
|---|
| 187 | class 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 | |
|---|
| 222 | def find_all_devs(): |
|---|
| 223 | return [] |
|---|
| 224 | |
|---|
| 225 | PMField = Field |
|---|
| 226 | PMFlagsField = Flags |
|---|
| 227 | |
|---|
| 228 | PMBitField = BitField |
|---|
| 229 | PMIPField = IPv4AddrField |
|---|
| 230 | PMByteField = None |
|---|
| 231 | PMShortField = None |
|---|
| 232 | PMLEShortField = None |
|---|
| 233 | PMIntField = IntField |
|---|
| 234 | PMSignedIntField = None |
|---|
| 235 | PMLEIntField = None |
|---|
| 236 | PMLESignedIntField = None |
|---|
| 237 | PMLongField = None |
|---|
| 238 | PMLELongField = None |
|---|
| 239 | PMStrField = None |
|---|
| 240 | PMLenField = None |
|---|
| 241 | PMRDLenField = None |
|---|
| 242 | PMFieldLenField = None |
|---|
| 243 | PMBCDFloatField = None |
|---|
| 244 | PMEnumField = EnumField |
|---|