コード例 #1
0
ファイル: host.py プロジェクト: redes-2015/simula-rede
    def __tcpEstablishConnection(self, serverIp):
        """Establishes a TCP connection by doing a handshake with
           the server."""
        clientPort = random.randint(1025, 65530)

        # Sends a SYN message to establish connection
        segment = TcpSegment("", clientPort, self.ircPort)
        segment.setSYN()
        datagram = IpDatagram(segment, self.ipAddr, serverIp)
        datagram.setId(self.__nextPacketId())
        self.link.putTargetQueue(datagram)
        packet = self.netQueue.get()

        # Receives packet containing server's SYN
        segment = TcpSegment("", clientPort, self.ircPort)
        segment.setAckNumber(packet.getSegment().getAckNumber())
        segment.setACK()
        segment.setSeqNumber(1)
        segment.setSeqNumber(1)
        datagram = IpDatagram(segment, self.ipAddr, packet.getOriginIp())
        self.netQueue.task_done()
        datagram.setId(self.__nextPacketId())
        self.link.putTargetQueue(datagram)

        # Defines the randomized client port number
        self.application.setClientPort(clientPort)
コード例 #2
0
    def __tcpEstablishConnection(self, serverIp):
        """Establishes a TCP connection by doing a handshake with
           the server."""
        clientPort = random.randint(1025, 65530)

        # Sends a SYN message to establish connection
        segment = TcpSegment("", clientPort, self.ircPort)
        segment.setSYN()
        datagram = IpDatagram(segment, self.ipAddr, serverIp)
        datagram.setId(self.__nextPacketId())
        self.link.putTargetQueue(datagram)
        packet = self.netQueue.get()

        # Receives packet containing server's SYN
        segment = TcpSegment("", clientPort, self.ircPort)
        segment.setAckNumber(packet.getSegment().getAckNumber())
        segment.setACK()
        segment.setSeqNumber(1)
        segment.setSeqNumber(1)
        datagram = IpDatagram(segment, self.ipAddr, packet.getOriginIp())
        self.netQueue.task_done()
        datagram.setId(self.__nextPacketId())
        self.link.putTargetQueue(datagram)

        # Defines the randomized client port number
        self.application.setClientPort(clientPort)
コード例 #3
0
    def receive(self, packet):
        """Receives a packet. Based on its message, returns
           a response packet."""
        # Reads basic data from received packet
        clientIp = packet.getOriginIp()
        segment = packet.getSegment()
        clientPort = segment.getOriginPort()
        msg = packet.getSegment().getMessage()

        # Adjusts ack and seq numbers
        ackNumber = segment.getSeqNumber() + segment.getMessageSize()
        seqNumber = segment.getAckNumber()

        # Received a SYN packet; establish connection with client
        if segment.getSYN() is True:
            clientIp = packet.getOriginIp()
            synSegment = TcpSegment("", self.serverPort, clientPort)
            synSegment.setSYN()
            synSegment.setACK()
            synSegment.setAckNumber(1)
            synSegment.setSeqNumber(seqNumber)
            return IpDatagram(synSegment, self.serverIp, clientIp)

        # Received a FIN packet; close connection with client
        elif segment.getFIN() is True:
            clientIp = packet.getOriginIp()
            finSegment = TcpSegment("", self.serverPort, clientPort)
            finSegment.setFIN()
            finSegment.setACK()
            finSegment.setAckNumber(ackNumber + 1)
            finSegment.setSeqNumber(seqNumber)
            return IpDatagram(finSegment, self.serverIp, clientIp)

        # Received an ACK packet; do nothing
        elif msg == "":
            return None

        # Packet contains a message from the client;
        # send an ACK packet and a message packet

        # ACK packet
        ackSegment = TcpSegment("", self.serverPort, clientPort)
        ackSegment.setACK()
        ackSegment.setAckNumber(ackNumber)
        ackSegment.setSeqNumber(seqNumber)

        ackPacket = IpDatagram(ackSegment, self.serverIp, clientIp)

        # Response packet (contains a non-empty message)
        respMessage = self.__parseMessage([clientIp, clientPort], msg)

        respSegment = TcpSegment(respMessage, self.serverPort, clientPort)
        respSegment.setACK()
        respSegment.setAckNumber(ackNumber)
        respSegment.setSeqNumber(seqNumber)

        respPacket = IpDatagram(respSegment, self.serverIp, clientIp)

        return [ackPacket, respPacket]