Exemple #1
0
    def connect(self, address: str):
        assert self.server == False, "Error! Server cannot be client."
        self.client = True
        self.dest_hostport = parse_address(address)
        total_timeouts = 1
        local_timeout = time.time() + 15 + self.timeout
        while time.time() < local_timeout:
            self.send_connect(1)
            self.socket.settimeout(self.timeout)
            try:
                self.secnum += 1
                if self.recv_connect(2) == 2:
                    break
                self.secnum -= 1
                info("Error while recieving SYNACK segment, retrying")
            except socket.timeout:
                self.secnum -= 1
                total_timeouts *= 2 if total_timeouts < 32 else 1
                self.timeout *= 2
                info(
                    "Timeout ocurred while waiting for ACK segment, retrying.")
        else:
            self.socket.settimeout(None)
            return 0
        for _ in range(total_timeouts):
            self.send_connect(3)
        self.secnum += 1

        info("Three-Way Handshake completed, hopefully.")
        self.socket.settimeout(None)
        return 1
Exemple #2
0
def listen(address: str) -> Conn:
    localhost, localport = utils.parse_address(address)
    conn = Conn()
    conn.socket.bind((localhost, localport))
    conn.source_ip = localhost
    conn.source_port = localport

    return conn
Exemple #3
0
def listen(address: str) -> Conn:
    host, port = parse_address(address)

    conn = Conn(host, port)

    print('Listening...')

    return conn
Exemple #4
0
def dial(address):
    conn = Conn()

    host, port = parse_address(address)

    conn.socket.connect((host, port))

    return conn
Exemple #5
0
def send_syn(conn, address):
    seq_num = randint(1, 99)
    ack = randint(1, 99)
    conn.dest_host, conn.dest_port = utils.parse_address(address)
    pack = packet.create_syn_packet(conn.port, conn.dest_port, seq_num, ack, conn.host, conn.dest_host)
    conn.socket.sendto(pack, (conn.dest_host, conn.dest_port))
    pack = packet.my_unpack(pack)

    return pack
Exemple #6
0
def listen(address):
    conn = Conn()

    host, port = parse_address(address)

    logger.info(f'socket binded to {address}')
    conn.socket.bind((host, port))
    conn.socket.listen(1)

    return conn
Exemple #7
0
 def get_node_stats(self):
     filepath = os.path.join(data_dir(), 'addrbook.json')
     self.hosts = parse_address(filepath)
     self._connect(NODE_STATS_TIMEOUT)
     result = {
         "current_node_num": self.online_num,
         "online_nodes": self.online_node,
         "timestamp": int(time.time()),
         "time_lapse": self.time_lapse
     }
     self.restore()
     return result
Exemple #8
0
def dial(address: str) -> Conn:
    print('Dialing...')

    host, port = parse_address(address)
    conn = Conn(IP_ADDRESS_CLIENT, PORT_CLIENT)

    conn.dest_host = host
    conn.dest_port = port

    syn_pack = create_syn_packet(conn)
    print('Waiting for SYNACK...')
    pack = wait_for_synack(conn, syn_pack)
    print('SYNACK received')

    conn.dest_seq_num = pack.seq_num

    first_pack = create_first_packet(b'', conn)
    conn.sock.sendto(first_pack.pack(), (conn.dest_host, conn.dest_port))

    return conn
 def runMIPS(self):
   """ Execute the program using the MIPS instruction set. """
   HI, LO = 0, 0
   cur_line = 0
   while cur_line < len(self.instructions):
     instr = self.instructions[cur_line]
     if cur_line in self.labels.values(): pass
     elif instr.operation in ["add", "addu"]:
       self.registers[instr.operand0] = self.registers[instr.operand1] + self.registers[instr.operand2]
     elif instr.operation == "addi":
       self.registers[instr.operand0] = self.registers[instr.operand1] + getimm(instr.operand2, True)
     elif instr.operation == "addiu":
       self.registers[instr.operand0] = self.registers[instr.operand1] + getimm(instr.operand2, False)
     elif instr.operation == "and":
       self.registers[instr.operand0] = self.registers[instr.operand1] & self.registers[instr.operand2]
     elif instr.operation == "andi":
       self.registers[instr.operand0] = self.registers[instr.operand1] & getimm(instr.operand2, False)
     elif instr.operation == "beq":
       if self.registers[instr.operand0] == self.registers[instr.operand1]:
         cur_line = self.labels[instr.operand2] # jump straight to the label rather than the following instruction because we increment the line counter at the end anyway
     elif instr.operation == "bgez":
       if self.registers[instr.operand0] >= 0:
         cur_line = self.labels[instr.operand1]
     elif instr.operation == "bgezal":
       if self.registers[instr.operand0] >= 0:
         self.registers[31] = cur_line + 1
         cur_line = self.labels[instr.operand1]
     elif instr.operation == "bltz":
       if self.registers[instr.operand0] < 0:
         cur_line = self.labels[instr.operand1]
     elif instr.operation == "bltzal":
       if self.registers[instr.operand0] < 0:
         self.registers[31] = cur_line + 1
         cur_line = self.labels[instr.operand1]
     elif instr.operation == "bne":
       if self.registers[instr.operand0] != self.registers[instr.operand1]:
         cur_line = self.labels[instr.operand2]
     elif instr.operation in ["div", "divu"]:
       LO = self.registers[instr.operand0] // self.registers[instr.operand1]
       HI = self.registers[instr.operand0] % self.registers[instr.operand1]
     elif instr.operation == "j":
       cur_line = self.labels[instr.operand0]
     elif instr.operation == "jal":
       self.registers[31] = cur_line + 1
       cur_line = self.labels[instr.operand0]
     elif instr.operation == "jr":
       cur_line = self.registers[instr.operand0]
     elif instr.operation == "la":
       self.registers[instr.operand0] = self.memory.labels[instr.operand1]
     elif instr.operation in ["lb", "lw"]:
       outside, inside = parse_address(instr.operand1)
       address = calcval(outside, self) + calcval(inside, self)
       self.registers[instr.operand0] = self.memory[address]
     elif instr.operation == "lui":
       self.registers[instr.operand0] = getimm(instr.operand1, False) << 16
     elif instr.operation == "mfhi":
       self.registers[instr.operand0] = HI
     elif instr.operation == "mflo":
       self.registers[instr.operand0] = LO
     elif instr.operation in ["mult", "multu"]:
       LO = self.registers[instr.operand0] * self.registers[instr.operand1]
     elif instr.operation == "nor":
       self.registers[instr.operand0] = ~(self.registers[instr.operand1] | self.registers[instr.operand2]) & 0xFFFFFFFF
     elif instr.operation == "or":
       self.registers[instr.operand0] = self.registers[instr.operand1] | self.registers[instr.operand2]
     elif instr.operation == "ori":
       self.registers[instr.operand0] = self.registers[instr.operand1] | getimm(instr.operand2, False)
     elif instr.operation == "sb":
       outside, inside = parse_address(instr.operand1)
       address = calcval(outside, self) + calcval(inside, self)
       self.memory[address] = self.registers[instr.operand0] & 0xFF
     elif instr.operation in ["slt", "sltu"]:
       self.registers[instr.operand0] = int(self.registers[instr.operand1] < self.registers[instr.operand2])
     elif instr.operation == "slti":
       self.registers[instr.operand0] = int(self.registers[instr.operand1] < getimm(instr.operand2, True))
     elif instr.operation == "sltiu":
       self.registers[instr.operand0] = int(self.registers[instr.operand1] < getimm(instr.operand2, False))
     elif instr.operation == "sll":
       self.registers[instr.operand0] = self.registers[instr.operand1] << getimm(instr.operand2, False)
     elif instr.operation == "sllv":
       self.registers[instr.operand0] = self.registers[instr.operand1] << self.registers[instr.operand2]
     elif instr.operation == "sra":
       self.registers[instr.operand0] = self.registers[instr.operand1] >> getimm(instr.operand2, True)
     elif instr.operation == "srl":
       self.registers[instr.operand0] = self.registers[instr.operand1] >> getimm(instr.operand2, False)
     elif instr.operation == "srlv":
       self.registers[instr.operand0] = self.registers[instr.operand1] >> self.registers[instr.operand2]
     elif instr.operation in ["sub", "subu"]:
       self.registers[instr.operand0] = self.registers[instr.operand1] - self.registers[instr.operand2]
     elif instr.operation == "sw":
       outside, inside = parse_address(instr.operand1)
       address = calcval(outside, self) + calcval(inside, self)
       self.memory[address] = self.registers[instr.operand0]
     elif instr.operation == "syscall":
        retval = mips_syscall(self.registers[2])
        if retval: break
     elif instr.operation == "xor":
       self.registers[instr.operand0] = self.registers[instr.operand1] ^ self.registers[instr.operand2]
     elif instr.operation == "xori":
       self.registers[instr.operand0] = self.registers[instr.operand1] ^ getimm(instr.operand2, False)
     elif instr.operation == "break":
       break
     else:
       raise ValueError("Unrecognized operation: {0}".format(instr.operation))
     cur_line += 1
   return self
 def test_is_string_true(self):
     """Does the function return a string?"""
     self.assertTrue(parse_address("ontario st"))
 def test_uppercase(self):
     """Does the function return an uppercase string?"""
     self.assertEqual(parse_address('ontario st'), ('','ONTARIO ST'))
     self.assertEqual(parse_address('w 18th av'), ('','18TH AV W'))
     self.assertEqual(parse_address('456 w 18th av'), ('456','18TH AV W'))
 def test_no_argument(self):
     """Does the function return nothing if you pass it nothing?"""
     self.assertEqual(parse_address(None), ('',''))
Exemple #13
0
 def listen(self, address: str, conn_number: int):
     assert self.client == False, "Error! Clients cannot listen for connections."
     assert self.server == False, "Error! Servers can only listen once."
     self.server = True
     self.source_hostport = parse_address(address)
     self.bound_conns = [None for _ in range(conn_number - 1)]