Example #1
0
  def from_payload(cls, data, src, dst, timestamp):
    if len(data) < 16:
      raise BadPacket("Too small")

    proto, offset = read_long(data, 0)
    if proto == cls.PROTO_VER:
      server_id, offset = read_long(data, offset)
      election_addr, _ = read_string(data, offset)

      return Initial(timestamp, src, dst, server_id, election_addr)

    if len(data) >= cls.OLD_LEN:
      state, offset = read_number(data, 0)
      if PeerState.invalid(state):
        raise BadPacket("Invalid state: %d" % state)

      leader, offset = read_long(data, offset)
      zxid, offset = read_long(data, offset)
      election_epoch, offset = read_long(data, offset)
      peer_epoch, offset = read_long(data, offset) if len(data) > cls.OLD_LEN else (-1, offset)
      config = data[cls.WITH_CONFIG_LEN:] if len(data) > cls.WITH_CONFIG_LEN else ""

      return Notification(
        timestamp,
        src,
        dst,
        state,
        leader,
        zxid,
        election_epoch,
        peer_epoch,
        config
      )

    raise BadPacket("Unknown unknown")
Example #2
0
  def from_payload(cls, data, src, dst, timestamp):
    if len(data) < 16:
      raise BadPacket("Too small")

    proto, offset = read_long(data, 0)
    if proto == cls.PROTO_VER:
      server_id, offset = read_long(data, offset)
      election_addr, _ = read_string(data, offset)

      return Initial(timestamp, src, dst, server_id, election_addr)

    if len(data) >= cls.OLD_LEN:
      state, offset = read_number(data, 0)
      if PeerState.invalid(state):
        raise BadPacket("Invalid state: %d" % state)

      leader, offset = read_long(data, offset)
      zxid, offset = read_long(data, offset)
      election_epoch, offset = read_long(data, offset)
      peer_epoch, offset = read_long(data, offset) if len(data) > cls.OLD_LEN else (-1, offset)
      version = 0
      config = ""
      if len(data) > cls.WITH_VERSION_LEN:
        version, offset = read_number(data, offset)
        if version == 2:
          config, _ = read_string(data, offset)

      return Notification(
        timestamp,
        src,
        dst,
        state,
        leader,
        zxid,
        election_epoch,
        peer_epoch,
        version,
        config
      )

    raise BadPacket("Unknown unknown")
Example #3
0
    def _is_packet_fle_initial(self, packet):
        data = get_ip_packet(packet.load).data.data

        proto, offset = read_long(data, 0)
        if proto != FLE.Initial.PROTO_VER: return False

        server_id, offset = read_long(data, offset)
        if server_id < 0: return False

        election_addr, offset = read_string(data, offset)
        if election_addr.count(":") != 1: return False

        expected_len = 8 + 8 + 4 + len(election_addr)
        if len(data) != expected_len: return False

        return True
Example #4
0
  def _is_packet_fle_initial(self, packet):
    data = get_ip_packet(packet.load).data.data

    proto, offset = read_long(data, 0)
    if proto != FLE.Initial.PROTO_VER: return False

    server_id, offset = read_long(data, offset)
    if server_id < 0: return False

    election_addr, offset = read_string(data, offset)
    if election_addr.count(":") != 1: return False

    expected_len = 8 + 8 + 4 + len(election_addr)
    if len(data) != expected_len: return False

    return True
Example #5
0
def test_bad_unicode():
    bad = b'0\xf5'
    data = struct.Struct('!i').pack(len(bad)) + bad
    assert read_string(data, 0)[0] == "unreadable"
Example #6
0
def test_bad_unicode():
    bad = u'0\xf5'.encode('utf-8')
    data = struct.Struct('!i').pack(len(bad)) + bad
    assert read_string(data, 0)[0] == "unreadable"