Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
-----------------------------------------------
RRQ/ | 01/02 | Filename | 0 | Mode | 0 |
WRQ -----------------------------------------------
"""
def __init__(self):
TftpPacketInitial.__init__(self)
self.opcode = 2
def __str__(self):
s = 'WRQ packet: filename = %s' % self.filename
s += ' mode = %s' % self.mode
if self.options:
s += '\n options = %s' % self.options
return s
class TftpPacketDAT(TftpPacket):
"""
::
2 bytes 2 bytes n bytes
---------------------------------
DATA | 03 | Block # | Data |
---------------------------------
"""
def __init__(self):
TftpPacket.__init__(self)
self.opcode = 3
self.blocknumber = 0
self.data = None
def __str__(self):
s = 'DAT packet: block %s' % self.blocknumber
return self
def decode(self):
"""Decode self.buffer into instance variables. It returns self for
easy method chaining."""
# We know the first 2 bytes are the opcode. The second two are the
# block number.
(self.blocknumber,) = struct.unpack(str("!H"), self.buffer[2:4])
log.debug("decoding DAT packet, block number %d", self.blocknumber)
log.debug("should be %d bytes in the packet total", len(self.buffer))
# Everything else is data.
self.data = self.buffer[4:]
log.debug("found %d bytes of data", len(self.data))
return self
class TftpPacketACK(TftpPacket):
"""
::
2 bytes 2 bytes
-------------------
ACK | 04 | Block # |
--------------------
"""
def __init__(self):
TftpPacket.__init__(self)
self.opcode = 4
self.blocknumber = 0
def __str__(self):
return 'ACK packet: block %d' % self.blocknumber
return self
def decode(self):
"""Decode self.buffer into instance variables. It returns self for
easy method chaining."""
# We know the first 2 bytes are the opcode. The second two are the
# block number.
(self.blocknumber,) = struct.unpack(str("!H"), self.buffer[2:4])
log.debug("decoding DAT packet, block number %d", self.blocknumber)
log.debug("should be %d bytes in the packet total", len(self.buffer))
# Everything else is data.
self.data = self.buffer[4:]
log.debug("found %d bytes of data", len(self.data))
return self
class TftpPacketACK(TftpPacket):
"""
::
2 bytes 2 bytes
-------------------
ACK | 04 | Block # |
--------------------
"""
def __init__(self):
TftpPacket.__init__(self)
self.opcode = 4
self.blocknumber = 0
def __str__(self):
return 'ACK packet: block %d' % self.blocknumber
log.debug("encoding ACK: opcode = %d, block = %d",
self.opcode, self.blocknumber)
self.buffer = struct.pack(str("!HH"), self.opcode, self.blocknumber)
return self
def decode(self):
if len(self.buffer) > 4:
log.debug("detected TFTP ACK but request is too large, will truncate")
log.debug("buffer was: %s", repr(self.buffer))
self.buffer = self.buffer[0:4]
self.opcode, self.blocknumber = struct.unpack(str("!HH"), self.buffer)
log.debug("decoded ACK packet: opcode = %d, block = %d",
self.opcode, self.blocknumber)
return self
class TftpPacketERR(TftpPacket):
"""
::
2 bytes 2 bytes string 1 byte
----------------------------------------
ERROR | 05 | ErrorCode | ErrMsg | 0 |
----------------------------------------
Error Codes
Value Meaning
0 Not defined, see error message (if any).
1 File not found.
2 Access violation.
3 Disk full or allocation exceeded.
-----------------------------------------------
RRQ/ | 01/02 | Filename | 0 | Mode | 0 |
WRQ -----------------------------------------------
"""
def __init__(self):
TftpPacketInitial.__init__(self)
self.opcode = 2
def __str__(self):
s = 'WRQ packet: filename = %s' % self.filename
s += ' mode = %s' % self.mode
if self.options:
s += '\n options = %s' % self.options
return s
class TftpPacketDAT(TftpPacket):
"""
::
2 bytes 2 bytes n bytes
---------------------------------
DATA | 03 | Block # | Data |
---------------------------------
"""
def __init__(self):
TftpPacket.__init__(self)
self.opcode = 3
self.blocknumber = 0
self.data = None
def __str__(self):
s = 'DAT packet: block %s' % self.blocknumber