Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def on_new_data(self):
"""
Deals with incoming RRQ packets. This is called by `run_once` when data
is available on the listening socket.
This method deals with extracting all the relevant information from the
request (like file, transfer mode, path, and options).
If all is good it will run the `get_handler` method, which returns a
`BaseHandler` object. `BaseHandler` is a subclass of a
`multiprocessing.Process` class so calling `start()` on it will cause
a `fork()`.
"""
data, peer = self._listener.recvfrom(constants.DEFAULT_BLKSIZE)
code = struct.unpack("!H", data[:2])[0]
if code != constants.OPCODE_RRQ:
logging.warning(
"unexpected TFTP opcode %d, expected %d" % (code, constants.OPCODE_RRQ)
)
return
# extract options
tokens = list(filter(bool, data[2:].decode("latin-1").split("\x00")))
if len(tokens) < 2 or len(tokens) % 2 != 0:
logging.error(
"Received malformed packet, ignoring "
"(tokens length: {tl})".format(tl=len(tokens))
)
return
def on_new_data(self):
"""
Deals with incoming RRQ packets. This is called by `run_once` when data
is available on the listening socket.
This method deals with extracting all the relevant information from the
request (like file, transfer mode, path, and options).
If all is good it will run the `get_handler` method, which returns a
`BaseHandler` object. `BaseHandler` is a subclass of a
`multiprocessing.Process` class so calling `start()` on it will cause
a `fork()`.
"""
data, peer = self._listener.recvfrom(constants.DEFAULT_BLKSIZE)
code = struct.unpack('!H', data[:2])[0]
if code != constants.OPCODE_RRQ:
logging.warning(
'unexpected TFTP opcode %d, expected %d' %
(code, constants.OPCODE_RRQ)
)
return
# extract options
tokens = list(filter(bool, data[2:].decode('latin-1').split('\x00')))
if len(tokens) < 2 or len(tokens) % 2 != 0:
logging.error(
'Received malformed packet, ignoring '
'(tokens length: {tl})'.format(tl=len(tokens))
)
return
def __init__(self, server_addr, peer, file_path):
self.peer = peer
self.server_addr = server_addr
self.file_path = file_path
self.error = {}
self.options = {}
self.start_time = time.time()
self.packets_sent = 0
self.packets_acked = 0
self.bytes_sent = 0
self.retransmits = 0
self.blksize = constants.DEFAULT_BLKSIZE
def on_new_data(self):
"""
Called when new data is available on the socket.
This method will extract acknowledged block numbers and handle
possible errors.
"""
# Note that we use blocking socket, because it has its own dedicated
# process. We read only 512 bytes.
try:
listener = self._get_listener()
listener.settimeout(self._timeout)
data, peer = listener.recvfrom(constants.DEFAULT_BLKSIZE)
listener.settimeout(None)
except socket.timeout:
return
if peer != self._peer:
logging.error("Unexpected peer: %s, expected %s" % (peer, self._peer))
self._should_stop = True
return
code, block_number = struct.unpack("!HH", data[:4])
if code == constants.OPCODE_ERROR:
# When the client sends an OPCODE_ERROR#
# the block number is the ERR codes in constants.py
self._stats.error = {
"error_code": block_number,
"error_message": data[4:-1].decode("ascii", "ignore"),
}
# An error was reported by the client which terminates the exchange
peer (tuple): (ip, port of) the peer
path (string): requested file
options (dict): a dictionary containing the options the client
wants to negotiate.
stats_callback (callable): a callable that will be executed at the
end of the session. It gets passed an instance of the
`SessionStats` class.
"""
self._timeout = int(options["default_timeout"])
self._server_addr = server_addr
self._reset_timeout()
self._retries = int(options["retries"])
self._block_size = constants.DEFAULT_BLKSIZE
self._last_block_sent = 0
self._retransmits = 0
self._global_retransmits = 0
self._current_block = None
self._should_stop = False
self._waiting_last_ack = False
self._path = path
self._options = options
self._stats_callback = stats_callback
self._response_data = None
self._listener = None
self._peer = peer
logging.info(
"New connection from peer `%s` asking for path `%s`"
% (str(peer), str(path))