class VLANIDMessage(MessageStructure): value = Payload() def get_conf(payload): if payload == None or len(payload) == 0: return None vlanid = struct.unpack('!H', payload[0:2])[0] pp = ord(payload[2]) ports = [] for p in range(0, 8): if pp & (1 << (7 - p)): ports.append(p + 1) return (vlanid, ports) def set_conf(conf): if conf == None or conf[0] == None: return '\x00\x00\x00' vlanid = conf[0] ports = conf[1] pp = 0 for p in range(0, 8): if p + 1 in ports: pp = pp | (1 << (7 - p)) return struct.pack('!HB', vlanid, pp) config = FieldProperty(value, onget=get_conf, onset=set_conf)
class PortMirrorMessage(MessageStructure): value = Payload() def get_conf(payload): if len(payload) == 0: return None sp = ord(payload[2]) source_ports = [] for p in range(0, 8): if sp & (1 << (7 - p)): source_ports.append(p + 1) return (ord(payload[0]), source_ports) def set_conf(conf): if conf == None or conf[0] == None: return '\x00\x00\x00' destination_port = conf[0] source_ports = conf[1] sp = 0 for p in range(0, 8): if p + 1 in source_ports: sp = sp + (1 << (7 - p)) return chr(destination_port) + '\x00' + chr(sp) config = FieldProperty(value, onget=get_conf, onset=set_conf)
class IGMPSnoopingStatusMessage(MessageStructure): value = Payload() vlanid = FieldProperty(value, onget=lambda v: None if len(v) == 0 else struct.unpack('!H', v[2:4])[0] if v[0:2] == '\x00\x01' else 0, onset=lambda v: struct.pack('!HH', 1 if v > 0 else 0, v))
class TestCableResultMessage(MessageStructure): value = Payload() port_result_meters = FieldProperty( value, onget=lambda v: None if len(v) == 0 else (ord(v[0]), None, None) if len(v) == 1 else struct.unpack('!BII', v), onset=lambda v: chr(v[0]) if v[1] == None or v[2] == None else struct.pack( '!BII', v[0], v[1], v[2]))
class Frame(Structure): version = UBInt8() operation = UBInt8() result = UBInt16() reserved_0 = Magic('\x00\x00\x00\x00') _host_mac = UBInt8Sequence(6) host_mac = FieldProperty(_host_mac, onget=lambda v: unpack_mac(''.join(map(chr, v))), onset=lambda v: tuple(map(ord, pack_mac(v)))) _device_mac = UBInt8Sequence(6) device_mac = FieldProperty( _device_mac, onget=lambda v: unpack_mac(''.join(map(chr, v))), onset=lambda v: tuple(map(ord, pack_mac(v)))) reserved_1 = Magic('\x00\x00') sequence = UBInt16() signature = Magic('NSDP') reserved_2 = Magic('\x00\x00\x00\x00') messages = FieldArray(Message)
class BroadcastBandwidthMessage(MessageStructure): value = Payload() def get_conf(payload): if payload == None or len(payload) != 5: return None return struct.unpack('!BI', payload) def set_conf(conf): port = conf[0] bandwidth = conf[1] return struct.pack('!BI', port, bandwidth) config = FieldProperty(value, onget=get_conf, onset=set_conf)
class VLANID802Message(MessageStructure): value = Payload() def get_conf(payload): if payload == None or len(payload) == 0: return None vlanid = struct.unpack('!H', payload[0:2])[0] if len(payload) == 2: return (vlanid, None) mp = ord(payload[2]) & (~ord(payload[3])) untag_ports = [] for p in range(0, 8): if mp & (1 << (7 - p)): untag_ports.append(p + 1) mp = ord(payload[3]) tag_ports = [] for p in range(0, 8): if mp & (1 << (7 - p)): tag_ports.append(p + 1) return (vlanid, tag_ports, untag_ports) def set_conf(conf): if conf == None or conf[0] == None: return '\x00\x00\x00\x00' # TODO vlanid = conf[0] tag_ports = conf[1] untag_ports = conf[2] if tag_ports == None or untag_ports == None: return struct.pack('!H', vlanid) tp = 0 for p in range(0, 8): if p + 1 in tag_ports: tp = tp | (1 << (7 - p)) up = tp for p in range(0, 8): if p + 1 in untag_ports: up = up | (1 << (7 - p)) return struct.pack('!HBB', vlanid, up, tp) config = FieldProperty(value, onget=get_conf, onset=set_conf)
class LoopDetectionMessage(MessageStructure): value = Payload() enabled = FieldProperty(value, onget=lambda v: v == '\x01', onset=lambda v: '\x01' if v else '\x00')
class NumberOfPortsMessage(MessageStructure): value = Payload() ports = FieldProperty(value, onget=lambda v: None if len(v) == 0 else ord(v[0]), onset=lambda v: chr(v))
class GatewayMessage(MessageStructure): value = Payload() ip = FieldProperty(value, onget=unpack_ipv4, onset=pack_ipv4)
class EncryptPasswordMessage(MessageStructure): value = Payload() encrypted = FieldProperty(value, onget=lambda v: v == '\x00\x00\x00\x01', onset=lambda v: '\x00\x00\x00\x01' if v else '\x00\x00\x00\x00')
class DHCPStatusMessage(MessageStructure): value = Payload() enabled = FieldProperty(value, onget=lambda v: v == '\x01', onset=lambda v: '\x01' if v else '\x00')
class NetmaskMessage(MessageStructure): value = Payload() netmask = FieldProperty(value, onget=unpack_ipv4, onset=pack_ipv4)
class MyMessage(Structure): _version = UBInt8Sequence(2) version = FieldProperty( _version, onget=lambda v: "%d.%02d" % (v[0], v[1]), onset=lambda v: tuple(int(six.b(x)) for x in v.split(".", 1)))
class BroadcastFilteringMessage(MessageStructure): value = Payload() enabled = FieldProperty(value, onget=lambda v: v == '\x03', onset=lambda v: '\x03' if v else '\x00')
class BlockUnknownMulticastsMessage(MessageStructure): value = Payload() enabled = FieldProperty(value, onget=lambda v: v == '\x01', onset=lambda v: '\x01' if v else '\x00')
class SpeedLinkStatusMessage(MessageStructure): value = Payload() port_speed = FieldProperty(value, onget=lambda v: None if len(v) == 0 else (ord(v[0]), ord(v[1])), onset=lambda v: chr(v[0]) + chr(v[1]) + '\x01')
class IGMPHeaderValidationMessage(MessageStructure): value = Payload() enabled = FieldProperty(value, onget=lambda v: v == '\x01', onset=lambda v: '\x01' if v else '\x00')
class VLANID802DeleteMessage(MessageStructure): value = Payload() vlanid = FieldProperty(value, onget=lambda v: None if len(v) == 0 else struct.unpack('!H', v)[0], onset=lambda v: struct.pack('!H', v))
class QOSMessage(MessageStructure): value = Payload() type = FieldProperty(value, onget=lambda v: None if len(v) == 0 else ord(v), onset=lambda v: chr(v))
class PortbasedQOSMessage(MessageStructure): value = Payload() config = FieldProperty(value, onget=lambda v: None if len(v) == 0 else struct.unpack('!BB', v), onset=lambda v: struct.pack('!BB', v[0], v[1]))
class SuperMessage(Structure): magic = Magic(b'\xAA\xAA') # bitfield options = BitField(8, b1=BitBool(), b2=BitBool(), rest=BitNum(6)) # unsigned big endian ubint8 = UBInt8() ubint16 = UBInt16() ubint24 = UBInt24() ubint32 = UBInt32() ubint64 = UBInt64() # signed big endian sbint8 = SBInt8() sbint16 = SBInt16() sbint32 = SBInt32() sbint64 = SBInt64() # unsigned little endian ulint8 = ULInt8() ulint16 = ULInt16() ulint32 = ULInt32() ulint64 = ULInt64() # signed little endian slint8 = SLInt8() slint16 = SLInt16() slint32 = SLInt32() slint64 = SLInt64() # optional optional_one = ConditionalField(UBInt8(), lambda m: m.options.b1) optional_two = ConditionalField(UBInt8(), lambda m: m.options.b2) # sequences with variable lengths ubseql = LengthField(UBInt8()) ubseq = UBInt8Sequence(ubseql) sbseql = LengthField(UBInt8()) sbseq = SBInt8Sequence(sbseql) # sequences with fixed lengths ubseqf = UBInt8Sequence(5) sbseqf = SBInt8Sequence(5) # don't change anything... for test coverage ulint16_value = FieldProperty(ulint16) ulint16_byte_string = FieldProperty( ulint16, onget=lambda v: str(v), onset=lambda v: struct.unpack(">H", v)[0]) message_type = DispatchField(UBInt8()) submessage_length = LengthField(UBInt16()) submessage = DispatchTarget(submessage_length, message_type, {0xEF: SuperChild}) # checksum starts after beginning magic, ends before # the checksum crc = CRCField(UBInt16(), crc16_ccitt, 2, -3) eof = Magic(b'~')
class MacAddressMessage(MessageStructure): value = Payload() address = FieldProperty(value, onget=unpack_mac, onset=pack_mac)