コード例 #1
0
ファイル: Reconciliator.py プロジェクト: loofool/switzerland
def makebatch(timestamp, hashes, alice, rec):
    """
  A factory for the special dicts that represent our batches.
  hashes is a string of concatenated hashes
  TIMESTAMP is a constant that must not collide with the hashes
  """

    batch = {TIMESTAMP: timestamp}
    pos = 0
    for n in xrange(Protocol.hashes_in_batch(hashes)):
        hash = hashes[pos:pos + Protocol.hash_length]
        # debugging madness
        hash, ipid = hash[:-2], hash[-2:]
        if hash_archival:
            if alice:
                alice_ipids.setdefault(hash, []).append(ipid)
                alice_hashes.setdefault(ipid, []).append(hash)
                alice_flows_by_hash.setdefault(hash, []).append(rec)
            else:
                bob_ipids.setdefault(hash, []).append(ipid)
                bob_hashes.setdefault(ipid, []).append(hash)
                bob_flows_by_hash.setdefault(hash, []).append(rec)

        batch.setdefault(hash, 0)
        batch[hash] += 1
        pos += Protocol.hash_length
    return batch
コード例 #2
0
  def handshake(self):
    """
    Confirm that we are talking to a Switzerland client, with the appropriate
    protocol version.  If we return false the Protocl should clean up for us.
    """
    
    self.debug_note("attempting handshake with %s" % repr(self.peer))
    self.socket.settimeout(30)
    try:
      msg = self.socket.recv(len(Protocol.handshake1))
    except s.timeout:
      self.protocol_error("Timeout before handshake")
      
    if msg[:len(Protocol.handshake1) - 2] != Protocol.handshake1[:-2]:
      self.debug_note("Not the start of a Switzerland session:\n"+Protocol.handshake1[:-2], seriousness=11)
      return False

    incoming_prot_ver = Protocol.parse_version(msg[-2:])

    if incoming_prot_ver not in Protocol.supported_protocol_versions:
      self.socket.send(Protocol.no_common_version)
      self.debug_note("Alice is using unsupported protocol version %d" 
                   % incoming_prot_ver, seriousness=11)
      return False
    self.socket.setblocking(1)
    self.socket.send(Protocol.handshake2)
    self.debug_note("completed handshake with %s" % repr(self.peer))
    return True
コード例 #3
0
    def handshake(self):
        """
    Confirm that we are talking to a Switzerland client, with the appropriate
    protocol version.  If we return false the Protocl should clean up for us.
    """

        self.debug_note("attempting handshake with %s" % repr(self.peer))
        self.socket.settimeout(30)
        try:
            msg = self.socket.recv(len(Protocol.handshake1))
        except s.timeout:
            self.protocol_error("Timeout before handshake")

        if msg[:len(Protocol.handshake1) - 2] != Protocol.handshake1[:-2]:
            self.debug_note("Not the start of a Switzerland session:\n" +
                            Protocol.handshake1[:-2],
                            seriousness=11)
            return False

        incoming_prot_ver = Protocol.parse_version(msg[-2:])

        if incoming_prot_ver not in Protocol.supported_protocol_versions:
            self.socket.send(Protocol.no_common_version)
            self.debug_note("Alice is using unsupported protocol version %d" %
                            incoming_prot_ver,
                            seriousness=11)
            return False
        self.socket.setblocking(1)
        self.socket.send(Protocol.handshake2)
        self.debug_note("completed handshake with %s" % repr(self.peer))
        return True
コード例 #4
0
def makebatch(timestamp, hashes, alice, rec):
  """
  A factory for the special dicts that represent our batches.
  hashes is a string of concatenated hashes
  TIMESTAMP is a constant that must not collide with the hashes
  """
  
  batch = {TIMESTAMP:timestamp}
  pos = 0
  for n in xrange(Protocol.hashes_in_batch(hashes)):
    hash = hashes[pos : pos + Protocol.hash_length]
    # debugging madness
    hash, ipid = hash[:-2], hash[-2:]
    if hash_archival:
      if alice:
        alice_ipids.setdefault(hash,[]).append(ipid)
        alice_hashes.setdefault(ipid,[]).append(hash)
        alice_flows_by_hash.setdefault(hash,[]).append(rec)
      else:
        bob_ipids.setdefault(hash,[]).append(ipid)
        bob_hashes.setdefault(ipid,[]).append(hash)
        bob_flows_by_hash.setdefault(hash,[]).append(rec)

    batch.setdefault(hash, 0)
    batch[hash] += 1
    pos += Protocol.hash_length
  return batch
コード例 #5
0
    self.socket.settimeout(30)
    try:
      msg = self.socket.recv(len(Protocol.handshake2))
    except s.timeout:
      self.debug_note("Timeout during handshake", seriousness=11)
      return False
    except s.error, e:
      log.error("error during handshake: %s" % e)
      return False
    except:
      log.error("error during handshake")
      return False

    if msg[:-2] == Protocol.no_common_version[:-2]:
      log.error("Server refuses to speak protocols before version %d; we need version %d" % (Protocol.parse_version(msg[-2:]), Protocol.protocol_version))
      return False
    
    elif msg[:-2] != Protocol.handshake2[:-2]:
      log.error("Handshake failed, wasn't expecting:\n" + msg)
      return False

    log.debug("Started session with version %d" % Protocol.parse_version(msg[-2:]))
    self.socket.setblocking(1)
    return True

  def setup(self):
    "Handshake successful; now do initial housekeeping"
    self.send_myip()

  def lookup_flow_by_id(self, flow_id, caller):
コード例 #6
0
ファイル: AliceLink.py プロジェクト: loofool/switzerland
            msg = self.socket.recv(len(Protocol.handshake2))
        except s.timeout:
            self.debug_note("Timeout during handshake", seriousness=11)
            return False
        except s.error, e:
            log.error("error during handshake: %s" % e)
            return False
        except:
            log.error("error during handshake")
            return False

        if msg[:-2] == Protocol.no_common_version[:-2]:
            log.error(
                "Server refuses to speak protocols before version %d; we need version %d"
                %
                (Protocol.parse_version(msg[-2:]), Protocol.protocol_version))
            return False

        elif msg[:-2] != Protocol.handshake2[:-2]:
            log.error("Handshake failed, wasn't expecting:\n" + msg)
            return False

        log.debug("Started session with version %d" %
                  Protocol.parse_version(msg[-2:]))
        self.socket.setblocking(1)
        return True

    def setup(self):
        "Handshake successful; now do initial housekeeping"
        self.send_myip()