Exemplo n.º 1
0
    def rec(self, recv_window):
    	#if self.connection_status != Status.IDLE:
    	#	raise CRPException("Status is not idle!")

    	resends_rem = self.resend_limit
    	while resends_rem > 0:
    		try:
    			self.socket.settimeout(5)
    			recv_data, recv_addr = self.recv_packet(self.recv_window)
    		except socket.timeout:
    			resends_rem -= 1
    			continue
    		else:
    			recv_pckt = CRPPacket(packet=recv_data)
    			if recv_pckt.calc_checksum() == recv_pckt.checksum and recv_pckt.data != str(None) \
		        and self.connection_status == Status.CONNECTED:
		            print (recv_pckt.data)
		            self.send_ACK()
		        elif recv_pckt.calc_checksum() != recv_pckt.checksum:
		            raise CRPException(CRPException.CHECKSUM_INVALID)

		        if recv_pckt.FIN and not recv_pckt.ACK:
		            self.send_FINACK()
		            print("Disconnected from Client.")
		            self.connection_status = Status.LISTEN
		            return (recv_data, recv_addr)
		        else:
		            return (recv_data, recv_addr)

		if resend_rem == 0:
			raise CRPException(CRPException.TIMEOUT)
Exemplo n.º 2
0
    def send_FINACK(self):
        fin_ack_pckt = CRPPacket(src_port=self.src_addr[1], dest_pstevejobsort=self.dest_addr[1], \
        	seq_num=self.sequence_num, ack_num=self.ack_num)
        fin_ack_pckt.FIN = 1
        fin_ack_pckt.ACK = 1
        self.sequence_num += 1
        resends_rem = self.resend_limit
        recv_pckt = None

        while resends_rem > 0:
            self.send_packet(fin_ack_pckt.get_packet())
            try:
                recv_data, recv_addr = self.rec(self.recv_window)
                recv_pckt = CRPPacket(packet=recv_data)
            except CRPException as exception:
                if (exception.type == CRPException.CHECKSUM_INVALID):
                    continue
            else:
                if recv_pckt != None and recv_pckt.ACK:
                    break
                elif recv_pckt != None and recv_pckt.FIN:
                    resends_rem = self.resend_limit

        if resends_rem == 0:
        	raise CRPException(CRPException.TIMEOUT)
Exemplo n.º 3
0
    def bind(self, src_addr):
        if src_addr:
            self.src_addr = src_addr

        if self.is_server:
            self.connection_status = Status.LISTEN

        if self.src_addr:
            self.socket.bind(src_addr)
        else:
            raise CRPException('Source address not specified.')
Exemplo n.º 4
0
    def connect(self, dest_addr):
        if self.src_addr is None:
            raise CRPException('Socket does not have a source address to bind to.')

        self.dest_addr = dest_addr

        self.sequence_num = 0

        syn_ack = self.send_SYN()

        self.ack_num = int(syn_ack.seq_num) + 1
        #ack_num = syn_ack.header.fields["seq"]
        #self.ack_num.set(ack_num + 1)

        self.send_ACK()

        self.sender = True

        self.connection_status = Status.CONNECTED
        print("Connection successful")
Exemplo n.º 5
0
    def close(self):
        if self.src_addr is None:
            raise CRPException('Socket does not have a source address to bind to.')

        self.sequence_num += 1

        fin_ack = None

        if (self.dest_addr is not None):
            fin_ack = self.send_FIN()

        if fin_ack != None and fin_ack.ACK and fin_ack.FIN:
            self.send_ACK()
            self.socket.close()
            self.sender = False

            self.connection_status = Status.CLOSED
            print("Disconnected from server.")
        else:
            self.socket.close()
Exemplo n.º 6
0
    def send_SYN(self):
        syn_pckt = CRPPacket(src_port=self.src_addr[1], dest_port=self.dest_addr[1], seq_num=self.sequence_num)
        syn_pckt.SYN = 1
        self.sequence_num += 1
        resends_rem = self.resend_limit
        recv_pckt = None

        while resends_rem > 0:
            self.send_packet(syn_pckt.get_packet())
            try:
                recv_data, recv_addr = self.rec(self.recv_window)
                recv_pckt = CRPPacket(packet=recv_data)
            except CRPException as exception:
                if (exception.type == CRPException.CHECKSUM_INVALID):
                    continue
            else:
                if recv_pckt != None and recv_pckt.SYN and recv_pckt.ACK:
                    break

        if resends_rem == 0:
        	raise CRPException(CRPException.TIMEOUT)

        return recv_pckt