| 1 | """ |
|---|
| 2 | Implementation of the MetaPacket class for BTSniffing. |
|---|
| 3 | Only used in the context of PM. |
|---|
| 4 | |
|---|
| 5 | """ |
|---|
| 6 | |
|---|
| 7 | from tagger import get_summary |
|---|
| 8 | |
|---|
| 9 | ROLE_STRING_MASTER = 'M' |
|---|
| 10 | ROLE_STRING_SLAVE = 'S' |
|---|
| 11 | |
|---|
| 12 | try: |
|---|
| 13 | from umit.pm.core.logger import log |
|---|
| 14 | except ImportError: |
|---|
| 15 | from sniffcommon import log |
|---|
| 16 | |
|---|
| 17 | try: |
|---|
| 18 | from umit.pm.backend.scapy import MetaPacket |
|---|
| 19 | except ImportError: |
|---|
| 20 | log.debug('Packet.py: not in PM Context') |
|---|
| 21 | class BtMetaPacket(object): |
|---|
| 22 | |
|---|
| 23 | def __init__(self, unit): |
|---|
| 24 | self.pkt = unit |
|---|
| 25 | |
|---|
| 26 | def __getattr__(self, name): |
|---|
| 27 | return getattr(self.pkt, name) |
|---|
| 28 | else: |
|---|
| 29 | |
|---|
| 30 | ### |
|---|
| 31 | ### Only if we are in the context of PM |
|---|
| 32 | ### do we formally define BtMetaPacket |
|---|
| 33 | ### |
|---|
| 34 | |
|---|
| 35 | class BtMetaPacket(MetaPacket): |
|---|
| 36 | """ |
|---|
| 37 | SniffPacket wrapper. Contains application level information as well. |
|---|
| 38 | """ |
|---|
| 39 | |
|---|
| 40 | NOT_IMPLEMENTED = ['hashret', 'answers', 'insert', |
|---|
| 41 | 'complete', 'remove', 'get_datetime', |
|---|
| 42 | 'get_time', 'get_source', 'get_dest', |
|---|
| 43 | 'reset', 'get_protocol_bounds', 'haslayer', |
|---|
| 44 | 'getlayer', 'get_raw_layer', |
|---|
| 45 | 'rebuild_from_raw_payload', 'get_datalink', |
|---|
| 46 | 'copy' ] |
|---|
| 47 | |
|---|
| 48 | def __init__(self, sniffunit, cfields = None): |
|---|
| 49 | super(BtMetaPacket, self).__init__(cfields) |
|---|
| 50 | self.sniffunit = sniffunit |
|---|
| 51 | # def __init__(self, proto=None, cfields=None): |
|---|
| 52 | # self.root = proto |
|---|
| 53 | # self.cfields = cfields or {} |
|---|
| 54 | |
|---|
| 55 | def __div__(self, other): |
|---|
| 56 | """ |
|---|
| 57 | Implemented to maintain consistency with original implementation |
|---|
| 58 | of MetaPacket. Only manipulate custom fields |
|---|
| 59 | """ |
|---|
| 60 | cfields = self.cfields.copy() |
|---|
| 61 | cfields.update(other.cfields) |
|---|
| 62 | self.cfields = cfields |
|---|
| 63 | |
|---|
| 64 | return self |
|---|
| 65 | |
|---|
| 66 | def __getattr__(self, name): |
|---|
| 67 | if name in self.NOT_IMPLEMENTED: |
|---|
| 68 | log.debug('BTMetaPacket call to %s' % name) |
|---|
| 69 | raise NotImplementedError('Not required as yet with BTMetaPackets') |
|---|
| 70 | |
|---|
| 71 | @classmethod |
|---|
| 72 | def new(cls, proto_name): |
|---|
| 73 | """ Keep. For future use when attacking is possible""" |
|---|
| 74 | raise NotImplementedError('Incompatible with BTMetaPackets') |
|---|
| 75 | |
|---|
| 76 | def get_channel(self): |
|---|
| 77 | return str(self.sniffunit.chan) |
|---|
| 78 | |
|---|
| 79 | def get_clock(self): |
|---|
| 80 | return str(self.sniffunit.clock) |
|---|
| 81 | |
|---|
| 82 | def get_role(self): |
|---|
| 83 | return ROLE_STRING_MASTER if self.sniffunit.is_src_master else ROLE_STRING_SLAVE |
|---|
| 84 | |
|---|
| 85 | def get_raw(self): |
|---|
| 86 | return self.sniffunit.payload.rawdata |
|---|
| 87 | |
|---|
| 88 | def summary(self): |
|---|
| 89 | # TODO: Implement packet tagging |
|---|
| 90 | return get_summary(self.sniffunit.payload) |
|---|
| 91 | |
|---|
| 92 | def get_protocol_str(self): |
|---|
| 93 | return '' |
|---|
| 94 | |
|---|
| 95 | def get_protocols(self): |
|---|
| 96 | "@returns a list containing the name of protocols" |
|---|
| 97 | log.debug('BTMetaPacket: call to get_protocols') |
|---|
| 98 | return [] |
|---|
| 99 | |
|---|
| 100 | # Custom fields |
|---|
| 101 | |
|---|
| 102 | def get_cfield(self, name): |
|---|
| 103 | return self.cfields[name] |
|---|
| 104 | |
|---|
| 105 | def set_cfield(self, name, val): |
|---|
| 106 | self.cfields[name] = val |
|---|
| 107 | |
|---|
| 108 | def unset_cfield(self, name): |
|---|
| 109 | del self.cfields[name] |
|---|