def handle(self, pkt, raddress, rport):
        """Handle the packet in response to an RRQ to the server."""
        if not self.context.tidport:
            self.context.tidport = rport
            log.info("Set remote port for session to %s" % rport)

        # Now check the packet type and dispatch it properly.
        if isinstance(pkt, TftpPacketOACK):
            log.info("Received OACK from server")
            try:
                self.handleOACK(pkt)
            except TftpException as err:
                log.error("Failed to negotiate options: %s" % str(err))
                self.sendError(TftpErrors.FailedNegotiation)
                raise
            else:
                log.debug("Sending ACK to OACK")

                self.sendACK(blocknumber=0)

                log.debug("Changing state to TftpStateExpectDAT")
                return TftpStateExpectDAT(self.context)

        elif isinstance(pkt, TftpPacketDAT):
            # If there are any options set, then the server didn't honour any
            # of them.
            log.info("Received DAT from server")
            if self.context.options:
                log.info("Server ignored options, falling back to defaults")
                self.context.options = { 'blksize': DEF_BLKSIZE }
            return self.handleDat(pkt)

        # Every other packet type is a problem.
        elif isinstance(pkt, TftpPacketACK):
            # Umm, we ACK, the server doesn't.
            self.sendError(TftpErrors.IllegalTftpOp)
            raise TftpException("Received ACK from server while in download")

        elif isinstance(pkt, TftpPacketWRQ):
            self.sendError(TftpErrors.IllegalTftpOp)
            raise TftpException("Received WRQ from server while in download")

        elif isinstance(pkt, TftpPacketERR):
            self.sendError(TftpErrors.IllegalTftpOp)
            log.debug("Received ERR packet: %s", pkt)
            if pkt.errorcode == TftpErrors.FileNotFound:
                raise TftpFileNotFoundError("File not found")
            else:
                raise TftpException("Received ERR from server: {}".format(pkt))

        else:
            self.sendError(TftpErrors.IllegalTftpOp)
            raise TftpException("Received unknown packet type from server: %s" % pkt)

        # By default, no state change.
        return self
def handle ( self , pkt , raddress , rport ) : if not self . context . tidport : self . context . tidport = rport log . info ( "Set remote port for session to %s" % rport ) if isinstance ( pkt , TftpPacketOACK ) : log . info ( "Received OACK from server" ) try : self . handleOACK ( pkt ) except TftpException as err : log . error ( "Failed to negotiate options: %s" % str ( err ) ) self . sendError ( TftpErrors . FailedNegotiation ) raise else : log . debug ( "Sending ACK to OACK" ) self . sendACK ( blocknumber = 0 ) log . debug ( "Changing state to TftpStateExpectDAT" ) return TftpStateExpectDAT ( self . context ) elif isinstance ( pkt , TftpPacketDAT ) : log . info ( "Received DAT from server" ) if self . context . options : log . info ( "Server ignored options, falling back to defaults" ) self . context . options = { 'blksize' : DEF_BLKSIZE } return self . handleDat ( pkt ) elif isinstance ( pkt , TftpPacketACK ) : self . sendError ( TftpErrors . IllegalTftpOp ) raise TftpException ( "Received ACK from server while in download" ) elif isinstance ( pkt , TftpPacketWRQ ) : self . sendError ( TftpErrors . IllegalTftpOp ) raise TftpException ( "Received WRQ from server while in download" ) elif isinstance ( pkt , TftpPacketERR ) : self . sendError ( TftpErrors . IllegalTftpOp ) log . debug ( "Received ERR packet: %s" , pkt ) if pkt . errorcode == TftpErrors . FileNotFound : raise TftpFileNotFoundError ( "File not found" ) else : raise TftpException ( "Received ERR from server: {}" . format ( pkt ) ) else : self . sendError ( TftpErrors . IllegalTftpOp ) raise TftpException ( "Received unknown packet type from server: %s" % pkt ) return self
Handle the packet in response to an RRQ to the server.