Beispiel #1
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]
Beispiel #2
0
    def receive(self, packet):
        """Receives and parses a packet from the IRC server."""
        segment = packet.getSegment()
        msg = segment.getMessage()

        # Packet contains a message from the server;
        # first, read message contained in the packet

        # Received CONNECT confirmation
        if msg[0] == '0':
            self.serverIp = packet.getOriginIp()
        # Received USER confirmation
        elif msg[0] == '1':
            pass
        # Received QUIT confirmation
        elif msg[0] == '2':
            self.mustClose = True

        # Then, create and send an ACK packet
        ackSegment = TcpSegment("", self.clientPort, self.serverPort)

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

        ackSegment.setACK()
        ackSegment.setAckNumber(self.ackNumber)
        ackSegment.setSeqNumber(self.seqNumber)

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

        if self.mustClose:
            self.mustClose = False
            finSegment = TcpSegment("", self.clientPort, self.serverPort)
            finSegment.setFIN()
            finSegment.setAckNumber(self.ackNumber)
            finSegment.setSeqNumber(self.seqNumber)
            finPacket = IpDatagram(finSegment, self.clientIp, self.serverIp)
            self.clientPort = None
            self.serverIp = None
            return [ackPacket, finPacket]

        return ackPacket
Beispiel #3
0
    def receive(self, packet):
        """Receives and parses a packet from the IRC server."""
        segment = packet.getSegment()
        msg = segment.getMessage()

        # Packet contains a message from the server;
        # first, read message contained in the packet

        # Received CONNECT confirmation
        if msg[0] == '0':
            self.serverIp = packet.getOriginIp()
        # Received USER confirmation
        elif msg[0] == '1':
            pass
        # Received QUIT confirmation
        elif msg[0] == '2':
            self.mustClose = True

        # Then, create and send an ACK packet
        ackSegment = TcpSegment("", self.clientPort, self.serverPort)

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

        ackSegment.setACK()
        ackSegment.setAckNumber(self.ackNumber)
        ackSegment.setSeqNumber(self.seqNumber)

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

        if self.mustClose:
            self.mustClose = False
            finSegment = TcpSegment("", self.clientPort, self.serverPort)
            finSegment.setFIN()
            finSegment.setAckNumber(self.ackNumber)
            finSegment.setSeqNumber(self.seqNumber)
            finPacket = IpDatagram(finSegment, self.clientIp, self.serverIp)
            self.clientPort = None
            self.serverIp = None
            return [ackPacket, finPacket]

        return ackPacket