Пример #1
0
 def unpack(cls, unpacker: Unpacker) -> "ClaimPredicate":
     type = ClaimPredicateType.unpack(unpacker)
     if type == ClaimPredicateType.CLAIM_PREDICATE_UNCONDITIONAL:
         return cls(type)
     if type == ClaimPredicateType.CLAIM_PREDICATE_AND:
         length = unpacker.unpack_uint()
         and_predicates = []
         for _ in range(length):
             and_predicates.append(ClaimPredicate.unpack(unpacker))
         return cls(type, and_predicates=and_predicates)
     if type == ClaimPredicateType.CLAIM_PREDICATE_OR:
         length = unpacker.unpack_uint()
         or_predicates = []
         for _ in range(length):
             or_predicates.append(ClaimPredicate.unpack(unpacker))
         return cls(type, or_predicates=or_predicates)
     if type == ClaimPredicateType.CLAIM_PREDICATE_NOT:
         # not_predicate is optional.
         not_predicate = ClaimPredicate.unpack(
             unpacker) if unpacker.unpack_uint() else None
         if not_predicate is None:
             raise ValueError("not_predicate should not be None.")
         return cls(type, not_predicate=not_predicate)
     if type == ClaimPredicateType.CLAIM_PREDICATE_BEFORE_ABSOLUTE_TIME:
         abs_before = Int64.unpack(unpacker)
         if abs_before is None:
             raise ValueError("abs_before should not be None.")
         return cls(type, abs_before=abs_before)
     if type == ClaimPredicateType.CLAIM_PREDICATE_BEFORE_RELATIVE_TIME:
         rel_before = Int64.unpack(unpacker)
         if rel_before is None:
             raise ValueError("rel_before should not be None.")
         return cls(type, rel_before=rel_before)
     return cls(type)
Пример #2
0
def read_datagram(addr, data):
    """Yield all record (flow and counter records) from the sFlow v5
    datagram given by up, which is expected to be an xdrlib.Unpacker
    object."""

    up = Unpacker(data)

    version = up.unpack_int()
    if not version == 5:
        hexdump_bytes(data)
        raise Exception()

    af = up.unpack_int()
    if af == 1:                 # IPv4
        agent_address = ntohl(up.unpack_uint())
    else:
        raise Exception()

    sf = Datagram(addr, agent_address)

    sub_agent_id = up.unpack_uint()
    sequence_number = up.unpack_uint()
    uptime = up.unpack_uint()
    nb_sample_records = up.unpack_uint()

    # Iterating over sample records
    for i in range(nb_sample_records):
        try:
            return read_sample_record(up, sf)
        except EOFError:
            stderr.write("read_sample_datagram: EOFError reading sample_record,", \
                      "Premature end of data stream, Skipping record\n")
            up.set_position(len(up.get_buffer()))
            break
Пример #3
0
    def _parse_raw_sflow_datagram(self, unpacker: Unpacker):
        self.sflow_version = unpacker.unpack_int()
        if not self.sflow_version == 5:
            logging.debug("Unimplemented sFlow version: {}".format(
                self.sflow_version))
            # TODO: read remainder if needed
            return

        self.agent_ip_version = unpacker.unpack_int()
        if self.agent_ip_version == 1:
            self.agent_ip_address = ntohl(unpacker.unpack_uint())
        # TODO: implement other versions
        else:
            logging.debug("Unimplemented agent IP version: {}".format(
                self.agent_ip_version))
            return

        self.sub_agent_id = unpacker.unpack_uint()
        self.sequence_number = unpacker.unpack_uint()
        self.switch_uptime = unpacker.unpack_uint()

        samples_in_datagram = unpacker.unpack_uint()
        for _ in range(samples_in_datagram):
            try:
                self.samples.append(SFlowSample(unpacker))
            except Exception as e:
                logging.warning("Bad sample")
                raise e
Пример #4
0
def read_datagram(addr, data):
    """Yield all record (flow and counter records) from the sFlow v5
    datagram given by up, which is expected to be an xdrlib.Unpacker
    object."""

    up = Unpacker(data)

    version = up.unpack_int()
    if not version == 5:
        hexdump_bytes(data)
        raise Exception()

    af = up.unpack_int()
    if af == 1:  # IPv4
        agent_address = ntohl(up.unpack_uint())
    else:
        raise Exception()

    sf = Datagram(addr, agent_address)

    sub_agent_id = up.unpack_uint()
    sequence_number = up.unpack_uint()
    uptime = up.unpack_uint()
    nb_sample_records = up.unpack_uint()

    # Iterating over sample records
    for i in range(nb_sample_records):
        try:
            return read_sample_record(up, sf)
        except EOFError:
            stderr.write("read_sample_datagram: EOFError reading sample_record,", \
                      "Premature end of data stream, Skipping record\n")
            up.set_position(len(up.get_buffer()))
            break
Пример #5
0
 def unpack(cls, unpacker: Unpacker) -> "AccountEntry":
     account_id = AccountID.unpack(unpacker)
     balance = Int64.unpack(unpacker)
     seq_num = SequenceNumber.unpack(unpacker)
     num_sub_entries = Uint32.unpack(unpacker)
     inflation_dest = AccountID.unpack(unpacker) if unpacker.unpack_uint() else None
     flags = Uint32.unpack(unpacker)
     home_domain = String32.unpack(unpacker)
     thresholds = Thresholds.unpack(unpacker)
     length = unpacker.unpack_uint()
     signers = []
     for _ in range(length):
         signers.append(Signer.unpack(unpacker))
     ext = AccountEntryExt.unpack(unpacker)
     return cls(
         account_id=account_id,
         balance=balance,
         seq_num=seq_num,
         num_sub_entries=num_sub_entries,
         inflation_dest=inflation_dest,
         flags=flags,
         home_domain=home_domain,
         thresholds=thresholds,
         signers=signers,
         ext=ext,
     )
Пример #6
0
    def _parse_raw_sflow_counter_sample(self, unpacker: Unpacker):
        self.sequence_number = unpacker.unpack_uint()
        self.source_id = unpacker.unpack_uint()
        counters_in_sample = unpacker.unpack_uint()

        for _ in range(counters_in_sample):
            self.counters.append(SFlowCounterRecord(unpacker))
Пример #7
0
    def _parse_raw_sflow_datagram(self, unpacker: Unpacker):
        self.sflow_version = unpacker.unpack_int()
        if not self.sflow_version == 5:
            logging.debug("Unimplemented sFlow version: {}".format(self.sflow_version))
            # TODO: read remainder if needed
            return

        self.agent_ip_version = unpacker.unpack_int()
        if self.agent_ip_version == 1:
            self.agent_ip_address = ntohl(unpacker.unpack_uint())
        # TODO: implement other versions
        else:
            logging.debug("Unimplemented agent IP version: {}".format(self.agent_ip_version))
            return

        self.sub_agent_id = unpacker.unpack_uint()
        self.sequence_number = unpacker.unpack_uint()
        self.switch_uptime = unpacker.unpack_uint()

        samples_in_datagram = unpacker.unpack_uint()
        for _ in range(samples_in_datagram):
            try:
                self.samples.append(SFlowSample(unpacker))
            except Exception as e:
                logging.warning("Bad sample")
                raise e
Пример #8
0
 def is_tag_full(wrapped_message):
     unpacker = Unpacker(wrapped_message)
     try:
         unpacker.unpack_uint()
         unpacker.unpack_string()
     except EOFError:
         return False
     return True
Пример #9
0
 def unpack(cls, unpacker: Unpacker) -> "SetOptionsOp":
     inflation_dest = AccountID.unpack(
         unpacker) if unpacker.unpack_uint() else None
     clear_flags = Uint32.unpack(
         unpacker) if unpacker.unpack_uint() else None
     set_flags = Uint32.unpack(unpacker) if unpacker.unpack_uint() else None
     master_weight = Uint32.unpack(
         unpacker) if unpacker.unpack_uint() else None
     low_threshold = Uint32.unpack(
         unpacker) if unpacker.unpack_uint() else None
     med_threshold = Uint32.unpack(
         unpacker) if unpacker.unpack_uint() else None
     high_threshold = Uint32.unpack(
         unpacker) if unpacker.unpack_uint() else None
     home_domain = String32.unpack(
         unpacker) if unpacker.unpack_uint() else None
     signer = Signer.unpack(unpacker) if unpacker.unpack_uint() else None
     return cls(
         inflation_dest=inflation_dest,
         clear_flags=clear_flags,
         set_flags=set_flags,
         master_weight=master_weight,
         low_threshold=low_threshold,
         med_threshold=med_threshold,
         high_threshold=high_threshold,
         home_domain=home_domain,
         signer=signer,
     )
Пример #10
0
 def unpack(cls, unpacker: Unpacker) -> "SCPNomination":
     quorum_set_hash = Hash.unpack(unpacker)
     length = unpacker.unpack_uint()
     votes = []
     for _ in range(length):
         votes.append(Value.unpack(unpacker))
     length = unpacker.unpack_uint()
     accepted = []
     for _ in range(length):
         accepted.append(Value.unpack(unpacker))
     return cls(quorum_set_hash=quorum_set_hash, votes=votes, accepted=accepted,)
Пример #11
0
 def unpack(cls, unpacker: Unpacker) -> "SCPQuorumSet":
     threshold = Uint32.unpack(unpacker)
     length = unpacker.unpack_uint()
     validators = []
     for _ in range(length):
         validators.append(PublicKey.unpack(unpacker))
     length = unpacker.unpack_uint()
     inner_sets = []
     for _ in range(length):
         inner_sets.append(SCPQuorumSet.unpack(unpacker))
     return cls(threshold=threshold, validators=validators, inner_sets=inner_sets,)
Пример #12
0
    def unpack(self, u: xdrlib.Unpacker):
        last = u.unpack_uint() + u.get_position()
        ret = ReturnCode.unpack(u)

        if ret == ReturnCode.OK:
            last2 = u.unpack_uint() + u.get_position()
            if u.get_position() < last2:
                self.digest = u.unpack_fopaque(20)
            if u.get_position() < last2:
                self.salt = u.unpack_fopaque(8)
            u.set_position(last2)
        u.set_position(last)
Пример #13
0
 def unpack(cls, unpacker: Unpacker) -> "LedgerSCPMessages":
     ledger_seq = Uint32.unpack(unpacker)
     length = unpacker.unpack_uint()
     messages = []
     for _ in range(length):
         messages.append(SCPEnvelope.unpack(unpacker))
     return cls(ledger_seq=ledger_seq, messages=messages,)
Пример #14
0
    def __init__(self, unpacker: Unpacker):
        self.counter_format = None
        self.counter = None

        self.counter_format = unpacker.unpack_uint()
        counter_data = unpacker.unpack_opaque()
        unpacker_counter_data = Unpacker(counter_data)

        if self.counter_format == SFlowCounterRecord.COUNTER_DATA_GENERIC_INTERFACE:
            self.counter = GenericInterfaceCounters(unpacker_counter_data)
        elif self.counter_format == SFlowCounterRecord.COUNTER_DATA_ETHERNET_INTERFACE:
            self.counter = EthernetInterfaceCounters(unpacker_counter_data)
        elif self.counter_format == SFlowCounterRecord.COUNTER_DATA_TOKEN_RING:
            pass
            self.counter = TokenRingCounters(unpacker_counter_data)
        elif self.counter_format == SFlowCounterRecord.COUNTER_DATA_VG_INTERFACE:
            pass
            self.counter = VgInterfaceCounters(unpacker_counter_data)
        elif self.counter_format == SFlowCounterRecord.COUNTER_DATA_VLAN:
            self.counter = VlanCounters(unpacker_counter_data)
        elif self.counter_format == SFlowCounterRecord.COUNTER_DATA_PROCESSOR:
            self.counter = ProcessorCounters(unpacker_counter_data)
        else:
            logging.debug('read_flow_record:Unimplemented data_format (%d)' %
                          self.flow_format)
Пример #15
0
    def unpack(cls, unpacker: Unpacker) -> "PeerStatList":
        length = unpacker.unpack_uint()
        peer_stat_list = []
        for _ in range(length):
            peer_stat_list.append(PeerStats.unpack(unpacker))

        return cls(peer_stat_list)
Пример #16
0
def gmetric_read(header_msg, data_msg):
    header = Unpacker(header_msg)
    data = Unpacker(data_msg)
    values = dict()
    header.unpack_int()
    values['HOSTNAME'] = str(header.unpack_string().decode('ascii'))
    values['NAME'] = str(header.unpack_string().decode('ascii'))
    values['SPOOFENABLED'] = header.unpack_int()
    values['TYPE'] = str(header.unpack_string().decode('ascii'))
    values['NAME'] = str(header.unpack_string().decode('ascii'))
    values['UNITS'] = str(header.unpack_string().decode('ascii'))
    values['SLOPE'] = slope_int2str[header.unpack_int()]
    values['TMAX'] = header.unpack_uint()
    values['DMAX'] = header.unpack_uint()
    if header.unpack_int() == 1:
        header.unpack_string()
        values['GROUP'] = str(header.unpack_string().decode('ascii'))
    # Actual data in the second packet
    data.unpack_int()
    values['HOSTNAME'] = str(data.unpack_string().decode('ascii'))
    values['NAME'] = str(data.unpack_string().decode('ascii'))
    values['SPOOFENABLED'] = data.unpack_int()
    data.unpack_string()
    values['VAL'] = str(data.unpack_string().decode('ascii'))
    header.done()
    data.done()
    return values
Пример #17
0
 def unpack(self, u: xdrlib.Unpacker):
     last = u.unpack_uint() + u.get_position()
     if u.get_position() < last:
         self.time_A = struct.unpack('>Q', u.unpack_fopaque(8))[0]
     if u.get_position() < last:
         self.time_B = struct.unpack('>Q', u.unpack_fopaque(8))[0]
     u.set_position(last)
Пример #18
0
 def unpack(cls, unpacker: Unpacker) -> "SCPHistoryEntryV0":
     length = unpacker.unpack_uint()
     quorum_sets = []
     for _ in range(length):
         quorum_sets.append(SCPQuorumSet.unpack(unpacker))
     ledger_messages = LedgerSCPMessages.unpack(unpacker)
     return cls(quorum_sets=quorum_sets, ledger_messages=ledger_messages,)
Пример #19
0
    def unpack(cls, unpacker: Unpacker) -> "LedgerEntryChanges":
        length = unpacker.unpack_uint()
        ledger_entry_changes = []
        for _ in range(length):
            ledger_entry_changes.append(LedgerEntryChange.unpack(unpacker))

        return cls(ledger_entry_changes)
Пример #20
0
 def unpack(cls, unpacker: Unpacker) -> "Operation":
     source_account = (MuxedAccount.unpack(unpacker)
                       if unpacker.unpack_uint() else None)
     body = OperationBody.unpack(unpacker)
     return cls(
         source_account=source_account,
         body=body,
     )
 def unpack(cls, unpacker: Unpacker) -> "TransactionResultSet":
     length = unpacker.unpack_uint()
     results = []
     for _ in range(length):
         results.append(TransactionResultPair.unpack(unpacker))
     return cls(
         results=results,
     )
Пример #22
0
 def get_tlv(wrapped_message):
     unpacker = Unpacker(wrapped_message)
     tag = unpacker.unpack_uint()
     message = unpacker.unpack_string()
     pos = unpacker.get_position()
     buff = unpacker.get_buffer()
     rest = buff[pos:]
     return tag, message, rest
 def unpack(cls, unpacker: Unpacker) -> "SCPStatementPrepare":
     quorum_set_hash = Hash.unpack(unpacker)
     ballot = SCPBallot.unpack(unpacker)
     prepared = SCPBallot.unpack(
         unpacker) if unpacker.unpack_uint() else None
     prepared_prime = SCPBallot.unpack(
         unpacker) if unpacker.unpack_uint() else None
     n_c = Uint32.unpack(unpacker)
     n_h = Uint32.unpack(unpacker)
     return cls(
         quorum_set_hash=quorum_set_hash,
         ballot=ballot,
         prepared=prepared,
         prepared_prime=prepared_prime,
         n_c=n_c,
         n_h=n_h,
     )
Пример #24
0
 def unpack(cls, unpacker: Unpacker) -> "ManageDataOp":
     data_name = String64.unpack(unpacker)
     data_value = DataValue.unpack(
         unpacker) if unpacker.unpack_uint() else None
     return cls(
         data_name=data_name,
         data_value=data_value,
     )
Пример #25
0
    def parse(self, raw_data):
        packet = SFlowPacket()
        data = Unpacker(raw_data)

        # sFlow version (2|4|5)
        packet.version = data.unpack_uint()
        if packet.version != 5:
            logging.error("Only support version 5.")
            raise RuntimeError("Only support version 5.")
        logging.debug("Get version {0}".format(packet.version))

        # IP version of the Agent/Switch (1=v4|2=v6)
        packet.agent_ip_version = data.unpack_uint()
        if packet.agent_ip_version != 1:
            logging.error("Only support IPv4.")
            raise RuntimeError("Only support IPv4.")

        # Agent IP address (v4=4byte|v6=16byte)
        packet.agent_ip_address = ntohl(data.unpack_uint())

        # sub agent id
        packet.sub_agent_id = data.unpack_uint()

        # datagram sequence number
        packet.datagram_sequence_num = data.unpack_uint()

        # switch uptime in ms
        packet.switch_uptime = data.unpack_uint()

        # how many samples in datagram
        packet.sample_amount = data.unpack_uint()

        self._parse_samples(packet, data)

        return packet
Пример #26
0
    def parse(self, raw_data):
        packet = SFlowPacket()
        data = Unpacker(raw_data)

        # sFlow version (2|4|5)
        packet.version = data.unpack_uint()
        if packet.version != 5:
            logging.error("Only support version 5.")
            raise RuntimeError("Only support version 5.")
        logging.debug("Get version {0}".format(packet.version))

        # IP version of the Agent/Switch (1=v4|2=v6)
        packet.agent_ip_version = data.unpack_uint()
        if packet.agent_ip_version != 1:
            logging.error("Only support IPv4.")
            raise RuntimeError("Only support IPv4.")

        # Agent IP address (v4=4byte|v6=16byte)
        packet.agent_ip_address = ntohl(data.unpack_uint())

        # sub agent id
        packet.sub_agent_id = data.unpack_uint()

        # datagram sequence number
        packet.datagram_sequence_num = data.unpack_uint()

        # switch uptime in ms
        packet.switch_uptime = data.unpack_uint()

        # how many samples in datagram
        packet.sample_amount = data.unpack_uint()

        self._parse_samples(packet, data)

        return packet
Пример #27
0
 def unpack(cls, unpacker: Unpacker) -> "InflationResult":
     code = InflationResultCode.unpack(unpacker)
     if code == InflationResultCode.INFLATION_SUCCESS:
         length = unpacker.unpack_uint()
         payouts = []
         for _ in range(length):
             payouts.append(InflationPayout.unpack(unpacker))
         return cls(code, payouts=payouts)
     raise ValueError("Invalid code.")
Пример #28
0
    def _parse_raw_sflow_flow_sample(self, unpacker: Unpacker):
        self.sequence_number = unpacker.unpack_uint()
        self.source_id = unpacker.unpack_uint()
        self.sampling_rate = unpacker.unpack_uint()
        self.sample_pool = unpacker.unpack_uint()
        self.drops = unpacker.unpack_uint()
        self.input_if = unpacker.unpack_uint()
        self.output_if = unpacker.unpack_uint()

        flows_in_sample = unpacker.unpack_uint()
        for _ in range(flows_in_sample):
            self.flows.append(SFlowFlowRecord(unpacker))
Пример #29
0
 def unpack(cls, unpacker: Unpacker) -> "TransactionSet":
     previous_ledger_hash = Hash.unpack(unpacker)
     length = unpacker.unpack_uint()
     txs = []
     for _ in range(length):
         txs.append(TransactionEnvelope.unpack(unpacker))
     return cls(
         previous_ledger_hash=previous_ledger_hash,
         txs=txs,
     )
 def unpack(cls, unpacker: Unpacker) -> "ManageOfferSuccessResult":
     length = unpacker.unpack_uint()
     offers_claimed = []
     for _ in range(length):
         offers_claimed.append(ClaimOfferAtom.unpack(unpacker))
     offer = ManageOfferSuccessResultOffer.unpack(unpacker)
     return cls(
         offers_claimed=offers_claimed,
         offer=offer,
     )
Пример #31
0
 def unpack(cls, unpacker: Unpacker) -> "TransactionV1Envelope":
     tx = Transaction.unpack(unpacker)
     length = unpacker.unpack_uint()
     signatures = []
     for _ in range(length):
         signatures.append(DecoratedSignature.unpack(unpacker))
     return cls(
         tx=tx,
         signatures=signatures,
     )
 def unpack(cls, unpacker: Unpacker) -> "TransactionMetaV1":
     tx_changes = LedgerEntryChanges.unpack(unpacker)
     length = unpacker.unpack_uint()
     operations = []
     for _ in range(length):
         operations.append(OperationMeta.unpack(unpacker))
     return cls(
         tx_changes=tx_changes,
         operations=operations,
     )
Пример #33
0
    def unpack(self, u: xdrlib.Unpacker):
        self.response_from_agent = u.unpack_bool()
        self.agent_id = u.unpack_opaque()

        n = u.unpack_uint()
        self.controllers = []
        for i in range(n):
            c = AccessibleController()
            c.trayId = u.unpack_uint()
            c.slot = u.unpack_uint()
            c.wwn = u.unpack_opaque()
            c.token = u.unpack_fopaque(12)
            c.controller_refs = []

            m = u.unpack_uint()
            for j in range(m):
                c.controller_refs.append(u.unpack_fopaque(12))

            self.controllers.append(c)
Пример #34
0
 def unpack(cls, unpacker: Unpacker) -> "Transaction":
     source_account = MuxedAccount.unpack(unpacker)
     fee = Uint32.unpack(unpacker)
     seq_num = SequenceNumber.unpack(unpacker)
     time_bounds = TimeBounds.unpack(unpacker) if unpacker.unpack_uint() else None
     memo = Memo.unpack(unpacker)
     length = unpacker.unpack_uint()
     operations = []
     for _ in range(length):
         operations.append(Operation.unpack(unpacker))
     ext = TransactionExt.unpack(unpacker)
     return cls(
         source_account=source_account,
         fee=fee,
         seq_num=seq_num,
         time_bounds=time_bounds,
         memo=memo,
         operations=operations,
         ext=ext,
     )
Пример #35
0
    def unpack(self, u: xdrlib.Unpacker):
        last = u.unpack_uint() + u.get_position()
        if u.get_position() < last:
            last2 = u.unpack_uint()
            self.canditate_selection_type = CandidateSelectionType(
                u.unpack_uint())
            if self.canditate_selection_type == CandidateSelectionType.CANDIDATE_SEL_MANUAL:
                last3 = u.unpack_uint() + u.get_position()
                if u.get_position() < last3:
                    nb = u.unpack_uint()
                    self.drive_refs = []
                    for i in range(nb):
                        self.drive_refs.append(u.unpack_fopaque(20))
                u.set_position(last3)
            u.set_position(last2)

        if u.get_position() < last:
            self.raid_level = RaidLevel(u.unpack_int())
        if u.get_position() < last:
            self.phyiscal_drive_type = PhysicalDriveType(u.unpack_int())
Пример #36
0
 def unpack(cls, unpacker: Unpacker) -> "StellarValue":
     tx_set_hash = Hash.unpack(unpacker)
     close_time = TimePoint.unpack(unpacker)
     length = unpacker.unpack_uint()
     upgrades = []
     for _ in range(length):
         upgrades.append(UpgradeType.unpack(unpacker))
     ext = StellarValueExt.unpack(unpacker)
     return cls(
         tx_set_hash=tx_set_hash, close_time=close_time, upgrades=upgrades, ext=ext,
     )
Пример #37
0
 def unpack(cls,
            unpacker: Unpacker) -> "PathPaymentStrictReceiveResultSuccess":
     length = unpacker.unpack_uint()
     offers = []
     for _ in range(length):
         offers.append(ClaimOfferAtom.unpack(unpacker))
     last = SimplePaymentResult.unpack(unpacker)
     return cls(
         offers=offers,
         last=last,
     )
Пример #38
0
def gmetric_read(msg):
    unpacker = Unpacker(msg)
    values = dict()
    unpacker.unpack_int()
    values['TYPE'] = unpacker.unpack_string()
    values['NAME'] = unpacker.unpack_string()
    values['VAL'] = unpacker.unpack_string()
    values['UNITS'] = unpacker.unpack_string()
    values['SLOPE'] = slope_int2str[unpacker.unpack_int()]
    values['TMAX'] = unpacker.unpack_uint()
    values['DMAX'] = unpacker.unpack_uint()
    unpacker.done()
    return values
Пример #39
0
def gmetric_read(msg):
    unpacker = Unpacker(msg)
    values = dict()
    unpacker.unpack_int()
    values["TYPE"] = unpacker.unpack_string()
    values["NAME"] = unpacker.unpack_string()
    values["VAL"] = unpacker.unpack_string()
    values["UNITS"] = unpacker.unpack_string()
    values["SLOPE"] = slope_int2str[unpacker.unpack_int()]
    values["TMAX"] = unpacker.unpack_uint()
    values["DMAX"] = unpacker.unpack_uint()
    unpacker.done()
    return values
Пример #40
0
def _length_from_bytes(four_bytes):
    """
    The RPC standard calls for the length of a message to be sent as the least
    significant 31 bits of an XDR encoded unsigned integer.  The most
    significant bit encodes a True/False bit which indicates that this message
    will be the last.
    """
    from xdrlib import Unpacker
    unpacker = Unpacker(four_bytes)
    val = unpacker.unpack_uint()
    unpacker.done()
    if val < 2**31:
        return (val, False)
    return (val-2**31, True)
Пример #41
0
 def datagramReceived(self, datagram, address):
     values = dict()
     unpacker = Unpacker(datagram)
     packet_type = unpacker.unpack_uint()
     if packet_type == 128:
         self.unpack_meta(unpacker)
         return
     elif packet_type == 136:
         #unpack_metareq function works, but serves no purpose right now
         #commented out unless anyone comes up with a good reason to respond
         #to metadata requests.
         #self.unpack_metareq(unpacker)
         return
     elif 128 < packet_type < 136:
         self.unpack_data(unpacker, packet_type, address)
         return
     else:
         return
Пример #42
0
    def __init__(self, unpacker: Unpacker):
        self.flow_format = None
        self.flow = None

        self.flow_format = unpacker.unpack_uint()
        flow_data = unpacker.unpack_opaque()
        unpacker_flow_data = Unpacker(flow_data)

        if self.flow_format == SFlowFlowRecord.FLOW_DATA_RAW_HEADER:
            self.flow = FlowDataRawHeader(unpacker_flow_data)
        elif self.flow_format == SFlowFlowRecord.FLOW_DATA_ETHERNET_HEADER:
            self.flow = FlowDataEthernetHeader(unpacker_flow_data)
        elif self.flow_format == SFlowFlowRecord.FLOW_DATA_IPV4_HEADER:
            self.flow = FlowDataIPv4Header(unpacker_flow_data)
        elif self.flow_format == SFlowFlowRecord.FLOW_DATA_EXT_SWITCH:
            self.flow = FlowDataExtSwitch(unpacker_flow_data)
        else:
            logging.debug('read_flow_record:Unimplemented data_format (%d)' % self.flow_format)
Пример #43
0
    def __init__(self, unpacker: Unpacker):
        self.type = None
        self.sample = None

        self.type = unpacker.unpack_uint()

        if self.type == SFlowSample.SAMPLE_DATA_FLOW_SAMPLE:
            self.sample = SFlowFlowSample(unpacker)

        elif self.type == SFlowSample.SAMPLE_DATA_COUNTER_SAMPLE:
            self.sample = SFlowCounterSample(unpacker)

        elif self.type == SFlowSample.SAMPLE_DATA_EXPANDED_FLOW_SAMPLE:
            self.sample = SFlowExpandedFlowSample(unpacker)

        elif self.type == SFlowSample.SAMPLE_DATA_EXPANDED_COUNTER_SAMPLE:
            self.sample = SFlowExpandedCounterSample(unpacker)

        else:
            logging.debug("Unknown data format: {}".format(type))
            logging.debug(unpacker.unpack_opaque())
Пример #44
0
    def __init__(self, unpacker: Unpacker):
        self.counter_format = None
        self.counter = None

        self.counter_format = unpacker.unpack_uint()
        counter_data = unpacker.unpack_opaque()
        unpacker_counter_data = Unpacker(counter_data)

        if self.counter_format == SFlowCounterRecord.COUNTER_DATA_GENERIC_INTERFACE:
            self.counter = GenericInterfaceCounters(unpacker_counter_data)
        elif self.counter_format == SFlowCounterRecord.COUNTER_DATA_ETHERNET_INTERFACE:
            self.counter = EthernetInterfaceCounters(unpacker_counter_data)
        elif self.counter_format == SFlowCounterRecord.COUNTER_DATA_TOKEN_RING:
            pass
            self.counter = TokenRingCounters(unpacker_counter_data)
        elif self.counter_format == SFlowCounterRecord.COUNTER_DATA_VG_INTERFACE:
            pass
            self.counter = VgInterfaceCounters(unpacker_counter_data)
        elif self.counter_format == SFlowCounterRecord.COUNTER_DATA_VLAN:
            self.counter = VlanCounters(unpacker_counter_data)
        elif self.counter_format == SFlowCounterRecord.COUNTER_DATA_PROCESSOR:
            self.counter = ProcessorCounters(unpacker_counter_data)
        else:
            logging.debug('read_flow_record:Unimplemented data_format (%d)' % self.flow_format)
Пример #45
0
    def _parse_raw_generic_interface_counters(self, unpacker: Unpacker):
        # Unpack Generic Interface Counters
        #     unsigned int ifIndex;
        #     unsigned int ifType;
        #     unsigned hyper ifSpeed;
        #     unsigned int ifDirection;      derived from MAU MIB (RFC 2668)
        #                                    0 = unkown, 1=full-duplex, 2=half-duplex,
        #                                    3 = in, 4=out
        #     unsigned int ifStatus;         bit field with the following bits assigned
        #                                    bit 0 = ifAdminStatus (0 = down, 1 = up)
        #                                    bit 1 = ifOperStatus (0 = down, 1 = up)
        #     unsigned hyper ifInOctets;
        #     unsigned int ifInUcastPkts;
        #     unsigned int ifInMulticastPkts;
        #     unsigned int ifInBroadcastPkts;
        #     unsigned int ifInDiscards;
        #     unsigned int ifInErrors;
        #     unsigned int ifInUnknownProtos;
        #     unsigned hyper ifOutOctets;
        #     unsigned int ifOutUcastPkts;
        #     unsigned int ifOutMulticastPkts;
        #     unsigned int ifOutBroadcastPkts;
        #     unsigned int ifOutDiscards;
        #     unsigned int ifOutErrors;
        #     unsigned int ifPromiscuousMode;

        self.if_index = unpacker.unpack_uint()
        self.if_type = unpacker.unpack_uint()
        self.if_speed = unpacker.unpack_uhyper()
        self.if_direction = unpacker.unpack_uint()
        self.if_status = unpacker.unpack_uint()
        self.if_in_octets = unpacker.unpack_uhyper()
        self.if_in_ucasts = unpacker.unpack_uint()
        self.if_in_mcasts = unpacker.unpack_uint()
        self.if_in_bcasts = unpacker.unpack_uint()
        self.if_in_discards = unpacker.unpack_uint()
        self.if_in_errors = unpacker.unpack_uint()
        self.if_in_unknown_protos = unpacker.unpack_uint()
        self.if_out_octets = unpacker.unpack_uhyper()
        self.if_out_ucasts = unpacker.unpack_uint()
        self.if_out_mcasts = unpacker.unpack_uint()
        self.if_out_bcasts = unpacker.unpack_uint()
        self.if_out_discards = unpacker.unpack_uint()
        self.if_out_errors = unpacker.unpack_uint()
        self.if_promiscuous_mode = unpacker.unpack_uint()
Пример #46
0
    def _parse_raw_expanded_sflow_flow_sample(self, unpacker: Unpacker):
        self.sequence_number = unpacker.unpack_uint()
        self.source_id['type'] = unpacker.unpack_uint()
        self.source_id['index'] = unpacker.unpack_uint()
        self.sampling_rate = unpacker.unpack_uint()
        self.sample_pool = unpacker.unpack_uint()
        self.drops = unpacker.unpack_uint()
        self.input_if['format'] = unpacker.unpack_uint()
        self.input_if['value'] = unpacker.unpack_uint()
        self.output_if['format'] = unpacker.unpack_uint()
        self.output_if['value'] = unpacker.unpack_uint()

        flows_in_sample = unpacker.unpack_uint()
        for _ in range(flows_in_sample):
            self.flows.append(SFlowFlowRecord(unpacker))
Пример #47
0
    def _parse_raw_ethernet_interface_counters(self, unpacker: Unpacker):
        # Unpack ethernet_counters structure
        #      unsigned int dot3_stats_alignment_errors;
        #      unsigned int dot3_stats_fcs_errors;
        #      unsigned int dot3_stats_single_collision_frames;
        #      unsigned int dot3_stats_multiple_collision_frames;
        #      unsigned int dot3_stats_sqe_test_errors;
        #      unsigned int dot3_stats_deferred_transmissions;
        #      unsigned int dot3_stats_late_collisions;
        #      unsigned int dot3_stats_excessive_collisions;
        #      unsigned int dot3_stats_internal_mac_transmit_errors;
        #      unsigned int dot3_stats_carrier_sense_errors;
        #      unsigned int dot3_stats_frame_too_longs;
        #      unsigned int dot3_stats_internal_mac_receive_errors;
        #      unsigned int dot3_stats_symbol_errors;

        self.dot3_stats_alignment_errors = unpacker.unpack_uint()
        self.dot3_stats_fcs_errors = unpacker.unpack_uint()
        self.dot3_stats_single_collision_frames = unpacker.unpack_uint()
        self.dot3_stats_multiple_collision_frames = unpacker.unpack_uint()
        self.dot3_stats_sqe_test_errors = unpacker.unpack_uint()
        self.dot3_stats_deferred_transmissions = unpacker.unpack_uint()
        self.dot3_stats_late_collisions = unpacker.unpack_uint()
        self.dot3_stats_excessive_collisions = unpacker.unpack_uint()
        self.dot3_stats_internal_mac_transmit_errors = unpacker.unpack_uint()
        self.dot3_stats_carrier_sense_errors = unpacker.unpack_uint()
        self.dot3_stats_frame_too_longs = unpacker.unpack_uint()
        self.dot3_stats_internal_mac_receive_errors = unpacker.unpack_uint()
        self.dot3_stats_symbol_errors = unpacker.unpack_uint()
Пример #48
0
class TprTopology:
	funcNames = [
		"bonds", "g96bonds", "morse", "cubicbonds", "connbonds",
		"harmonic", "fenebonds", "tabbonds", "tabbondsnc",
		"angles", "g96angles", "cross_bond_bond",
		"cross_bond_angle", "urey_bradley", "qangles",
		"tabangles", "pdihs", "rbdihs", "fourdihs", "idihs",
		"pidihs", "tabdihs", "lj14", "coul14", "ljc14_q",
		"ljc_nb", "lj_sr", "bham", "lj_lr", "bham_lr",
		"dispcorr", "coul_sr", "coul_lr", "rf_excl",
		"coul_recip", "dpd", "polarization", "waterpol",
		"thole", "posres", "disres", "drviol", "orires",
		"ordev", "angres", "angresz", "dihres", "dihviol",
		"constr", "constrnc", "settle", "vsite2", "vsite3",
		"vsite3fd", "vsite3fad", "vsite3out", "vsite4fd",
		"vsite4fdn", "vsiten", "com_pull", "eqm", "epot",
		"ekin", "etot", "econs", "temp", "pres", "dv/dl",
		"dk/dl", "dg/dl_con"
	]
	def __init__(self, topology):
		from OpenSave import osOpen
		topFile = osOpen(topology, 'rb')
		import os
		self.topFileSize = os.stat(topology).st_size
		from xdrlib import Unpacker
		self.fileString = FileString(topFile, 0, self.topFileSize)
		self.xdr = Unpacker(self.fileString)
		version = self._readHeader()
		self._readTopology(version)

	def _do_block(self, version):
		if version < 44:
			for i in range(256):
				self.xdr.unpack_uint()
		blockNR = self.xdr.unpack_uint()
		if version < 51:
			blockNRA = self.xdr.unpack_uint()
		for i in range(blockNR + 1):
			self.xdr.unpack_uint()
		if version < 51:
			for i in range(blockNRA):
				self.xdr.unpack_uint()

	def _do_blocka(self, version):
		if version < 44:
			for i in range(256):
				self.xdr.unpack_uint()
		blockNR = self.xdr.unpack_uint()
		blockNRA = self.xdr.unpack_uint()
		for i in range(blockNR + blockNRA + 1):
			self.xdr.unpack_uint()

	def _do_ffparams(self, version):
		self.funcNumber = {}
		for i, fn in enumerate(self.funcNames):
			self.funcNumber[fn] = i
		self.funcNumberUpdate = [
			(20, "cubicbonds"), (20, "connbonds"), (20, "harmonic"),
			(34, "fenebonds"), (43, "tabbonds"),
			(43, "tabbondsnc"), (30, "cross_bond_bond"),
			(30, "cross_bond_angle"), (30, "urey_bradley"),
			(34, "qangles"), (43, "tabangles"),
			(26, "fourdihs"), (26, "pdihs"), (43, "tabdihs"),
			(41, "ljc14_q"), (41, "ljc_nb"),
			(32, "bham_lr"), (32, "rf_excl"), (32, "coul_recip"),
			(46, "dpd"), (30, "polarization"), (36, "thole"),
			(22, "drviol"), (22, "orires"), (22, "ordev"),
			(26, "dihres"), (26, "dihviol"),
			(49, "vsite4fdn"), (50, "vsiten"), (46, "com_pull"),
			(20, "eqm"), (46, "econs"), (54, "dg/dl_con")
		]
		self.xdr.unpack_uint()
		if version < 57:
			self.xdr.unpack_uint()
		ntypes = self.xdr.unpack_uint()

		functions = []
		for i in range(ntypes):
			fnum = self.xdr.unpack_uint()
			for vn, funcName in self.funcNumberUpdate:
				if version < vn \
				and fnum >= self.funcNumber[funcName]:
					fnum += 1
			funcName = self.funcNames[fnum]
			functions.append(funcName)
		if version >= 57:
			self.unpackFloatFunc()
		for i, funcName in enumerate(functions):
			if funcName in ["angles", "g96angles", "bonds",
					"g96bonds", "harmonic", "idihs",
					"cross_bond_angle", "urey_bradley",
					"thole", "lj14", "ljc_nb"]:
				floatInts = "rrrr"
			elif funcName in ["fenebonds", "lj_sr", "constr",
					"constrnc", "settle", "vsite3",
					"vsite3fd", "vsite3fad"]:
				floatInts = "rr"
			elif funcName in ["cross_bond_bond", "bham", "morse",
					"cubic_bonds", "vsite3out", "vsite4fd",
					"vsite4fdn"]:
				floatInts = "rrr"
			elif funcName in ["qangles", "waterpol"]:
				floatInts = "rrrrrr"
			elif funcName in ["connbonds"]:
				floatInts = ""
			elif funcName in ["polarization", "vsite2"]:
				floatInts = "r"
			elif funcName in ["pdihs", "pidihs"]:
				floatInts = "rrrri"
			elif funcName in ["disres"]:
				floatInts = "iirrrr"
			elif funcName in ["orires"]:
				floatInts = "iiirrr"
			elif funcName in ["dihres"]:
				floatInts = "iirrr"
			elif funcName in ["posres"]:
				if version < 27:
					floatInts = "rrrrrr"
				else:
					floatInts = "rrrrrrrrrrrr"
			elif funcName in ["rbdihs"]:
				if version >= 25:
					floatInts = "rrrrrrrrrrrr"
				else:
					floatInts = "rrrrrr"
			elif funcName in ["fourdihs"]:
				floatInts = "rrrrrrrrrrrr"
			elif funcName in ["tabbonds", "tabbondsnc", "tabangles",
					"tabdihs"]:
				floatInts = "rir"
			elif funcName in ["ljc14_q"]:
				floatInts = "rrrrr"
			elif funcName in ["angres", "angresz"]:
				if version < 42:
					floatInts = "rrrr"
				else:
					floatInts = "rrrri"
			elif funcName in ["vsiten"]:
				floatInts = "ir"
			else:
				raise ValueError("Don't know correct"
						" parameters for '%s' function"
						% funcName)
			for fi in floatInts:
				if fi == 'r':
					t = self.unpackFloatFunc()
				else:
					t = self.xdr.unpack_uint()

	def _do_ilists(self, version):
		# bonds is first func, at least
		seen = set()
		self.bonds = []
		checklist = set()
		def addBond(a1, a2):
			if (a1, a2) in checklist:
				return
			self.bonds.append((a1, a2))
			checklist.add((a1, a2))
			checklist.add((a2, a1))
			seen.add(a1)
			seen.add(a2)
		for fname in self.funcNames:
			for uv, uname in self.funcNumberUpdate:
				if version < uv and fname == uname:
					skip = True
					break
			else:
				skip = False
			if skip:
				continue
			if version < 44:
				for i in range(256):
					self.xdr.unpack_uint()
			nr = self.xdr.unpack_uint()
			if fname == "bonds":
				for i in range(nr/3):
					self.xdr.unpack_uint()
					a1index = self.xdr.unpack_uint()
					a2index = self.xdr.unpack_uint()
					addBond(a1index, a2index)
			elif fname in ["angles", "g96angles"]:
				for i in range(nr/4):
					self.xdr.unpack_uint()
					a1index = self.xdr.unpack_uint()
					a2index = self.xdr.unpack_uint()
					a3index = self.xdr.unpack_uint()
					addBond(a1index, a2index)
					addBond(a2index, a3index)
			else:
				for i in range(nr):
					self.xdr.unpack_uint()
		# okay, the above doesn't hook up water (and maybe methane?)...
		hyds = {}
		heavys = {}
		for i, element in enumerate(self.elements):
			if i in seen:
				continue
			key = self.resNums[i]
			if element.number == 1:
				hyds.setdefault(key, []).append(i)
			else:
				heavys.setdefault(key, []).append(i)
		for i, heavysList in heavys.items():
			if len(heavysList) != 1 or i not in hyds:
				# beats me what to do
				continue
			heavy = heavysList[0]
			for hyd in hyds[i]:
				self.bonds.append((heavy, hyd))
		self.molInfo.append((self.atomNames, self.resNames,
				self.resIndices, self.bonds, self.elements))
		
	def _readHeader(self):
		# version string
		replyobj.info("%s\n" % self._readString())
		realSize = self.xdr.unpack_uint()
		if realSize == 4:
			self.unpackFloatFunc = self.xdr.unpack_float
			replyobj.info("using floats\n")
		elif realSize == 8:
			self.unpackFloatFunc = self.xdr.unpack_double
			replyobj.info("using doubles\n")
		else:
			raise ValueError("Floating-point values in .tpr file"
				" are not the same as either single-precision"
				" or double-precision floating point on this"
				" machine")
		version = self.xdr.unpack_uint()
		if version >= 26:
			generation = self.xdr.unpack_uint()
		else:
			generation = 0
		replyobj.info("version %d, generation %d\n"
						% (version, generation))
		natoms = self.xdr.unpack_uint()
		replyobj.info("%d atoms\n" % natoms)
		if version >= 28:
			tempCouplingGroups = self.xdr.unpack_uint()
		curStep = self.xdr.unpack_uint()
		curTime = self.unpackFloatFunc()
		curLambda = self.unpackFloatFunc()
		hasInputRec = self.xdr.unpack_uint()
		hasTopology = self.xdr.unpack_uint()
		if not hasTopology:
			raise ValueError(
				".tpr file does not have topology section")

		hasCoord = self.xdr.unpack_uint()
		hasVelocities = self.xdr.unpack_uint()
		hasForces = self.xdr.unpack_uint()
		hasBbox = self.xdr.unpack_uint()

		if hasBbox:
			for i in range(9):
				self.unpackFloatFunc()
			if version >= 51:
				for i in range(9):
					self.unpackFloatFunc()
			if version >= 28:
				for i in range(9):
					self.unpackFloatFunc()
				if version < 56:
					for i in range(9):
						self.unpackFloatFunc()

		if version >= 28 and tempCouplingGroups > 0:
			for i in range(tempCouplingGroups):
				self.unpackFloatFunc()
				self.unpackFloatFunc()

		if version < 26 and hasInputRec:
			raise ValueError("Cannot read version 26 or earlier"
				" .tpr files")

		return version

	def _readString(self):
		strlen = self.xdr.unpack_uint()
		self.xdr.unpack_uint()
		return self.xdr.unpack_fstring(strlen-1)

	def _readTopology(self, version):
		self.molInfo = []
		numStrings = self.xdr.unpack_uint()
		symbols = []
		for i in range(numStrings):
			symbols.append(self._readString())
		name = symbols[self.xdr.unpack_uint()]
		replyobj.info("%s\n" % name)

		if version >= 57:
			self._do_ffparams(version)
			numMolTypes = self.xdr.unpack_uint()
		else:
			numMolTypes = 1

		from Trajectory import determineElementFromMass
		for i in range(numMolTypes):
			if version >= 57:
				molName = symbols[self.xdr.unpack_uint()]
				replyobj.info("mol name: %s\n" % molName)
				

			numAtoms = self.xdr.unpack_uint()
			replyobj.info("%d atoms\n" % numAtoms)
			numResidues = self.xdr.unpack_uint()
			replyobj.info("%d residues\n" % numResidues)
			if version < 57:
				numGroupNames = self.xdr.unpack_uint()
				if version < 23:
					numGroups = 8
				elif version < 39:
					numGroups = 9
				else:
					numGroups = 10
			self.elements = []
			self.resNums = []
			for i in range(numAtoms):
				mass = self.unpackFloatFunc()
				self.elements.append(
						determineElementFromMass(mass))
				charge = self.unpackFloatFunc()
				self.unpackFloatFunc()
				self.unpackFloatFunc()
				for j in range(3):
					self.xdr.unpack_uint()
				resNum = self.xdr.unpack_uint()
				self.resNums.append(resNum)
				if version >= 52:
					self.xdr.unpack_uint()
				if version < 57:
					for j in range(numGroups):
						self.xdr.unpack_uint()
			self.atomNames = [symbols[self.xdr.unpack_uint()]
						for i in range(numAtoms)]
			for i in range(numAtoms*2):
				self.xdr.unpack_uint()
			self.resNames = [symbols[self.xdr.unpack_uint()]
						for i in range(numResidues)]
			self.resIndices = []
			curResNum = None
			for i, rn in enumerate(self.resNums):
				if curResNum != rn:
					self.resIndices.append(i+1)
					curResNum = rn
			
			if version < 57:
				# group names
				for i in range(numGroupNames):
					gn = symbols[self.xdr.unpack_uint()]

				# group contents
				for i in range(numGroups):
					grpN = self.xdr.unpack_uint()
					for j in range(grpN):
						self.xdr.unpack_uint()

			if version >= 57:
				self._do_ilists(version)
				self._do_block(version)

			self._do_blocka(version)

		if version >= 57:
			self.atomNames = []
			self.resNames = []
			self.resIndices = []
			self.bonds = []
			self.elements = []
		
			numMolBlocks = self.xdr.unpack_uint()
			for i in range(numMolBlocks):
				self.xdr.unpack_uint()
				repeat = self.xdr.unpack_uint()
				self.xdr.unpack_uint()
				# position restraints
				for j in range(2):
					npr = self.xdr.unpack_uint()
					for k in range(npr):
						self.unpackFloatFunc()
						self.unpackFloatFunc()
						self.unpackFloatFunc()
				atomNames, resNames, resIndices, bonds, \
						elements = self.molInfo[i]
				for j in range(repeat):
					aBase = len(self.atomNames)
					self.atomNames.extend(atomNames)
					self.resNames.extend(resNames)
					for ri in resIndices:
						self.resIndices.append(
								aBase + ri)
					for a1, a2 in bonds:
						self.bonds.append((aBase + a1,
								aBase + a2))
					self.elements.extend(elements)
		
			self.xdr.unpack_uint()

		# atom types
		if version > 25:
			nr = self.xdr.unpack_uint()
			# radii
			for i in range(nr):
				self.unpackFloatFunc()
			# volume
			for i in range(nr):
				self.unpackFloatFunc()
			# surface tension
			for i in range(nr):
				self.unpackFloatFunc()
			if version >= 40:
				# atom number
				for i in range(nr):
					self.xdr.unpack_uint()

		if version < 57:
			self._do_ffparams(version)
			if version >= 54:
				self.unpackFloatFunc()
			self._do_ilists(version)