def est_connection(self): my_syn = Packet(src=self.client_port, dst=self.server_port, seq_num=0, ack_num=0, data=b'', ctrl_bits=0x02) packed = my_syn.pkt_pack() self.client_socket.sendto(packed, self.server_addr) try: syn_ack = Packet() recv_data = self.client_socket.recv(self.pkt_size) syn_ack.pkt_unpack(recv_data) if syn_ack.get_ack_bit() and syn_ack.ack_num == 1: self.conn_est = True print("Client: Sending ACK for SYNACK") my_syn = Packet(src=self.client_port, dst=self.server_port, seq_num=0, ack_num=1, data=b'', ctrl_bits=0x10) packed = my_syn.pkt_pack() self.client_socket.sendto(packed, self.server_addr) else: print("Client: Connection failed: bad SYNACK") except socket.timeout: print("Client: Connection failed: timeout")
def est_connection(self, syn_pkt: Packet): self.client_port = syn_pkt.src my_syn = Packet(self.server_port, self.client_port, 0, syn_pkt.ack_num + 1, b'', 0x12) # ack bit = 1, syn bit = 1 packed = my_syn.pkt_pack() self.server_socket.sendto(packed, self.client_addr) try: syn_ack = Packet() recv_data = self.server_socket.recv(self.pkt_size) syn_ack.pkt_unpack(recv_data) if syn_ack.get_ack_bit() and syn_ack.ack_num == 1: self.conn_est = True print("Server: Connection established") else: print("Server: Connection failed: bad SYNACK") except socket.timeout: print("Server: Connection failed: timeout")