Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
order suitable for sending over the wire.
This is an abstract method."""
raise NotImplementedError("Abstract method")
def decode(self):
"""The decode method of a TftpPacket takes a buffer off of the wire in
network-byte order, and decodes it, populating internal properties as
appropriate. This can only be done once the first 2-byte opcode has
already been decoded, but the data section does include the entire
datagram.
This is an abstract method."""
raise NotImplementedError("Abstract method")
class TftpPacketInitial(TftpPacket, TftpPacketWithOptions):
"""This class is a common parent class for the RRQ and WRQ packets, as
they share quite a bit of code."""
def __init__(self):
TftpPacket.__init__(self)
TftpPacketWithOptions.__init__(self)
self.filename = None
self.mode = None
def encode(self):
"""Encode the packet's buffer from the instance variables."""
tftpassert(self.filename, "filename required in initial packet")
tftpassert(self.mode, "mode required in initial packet")
# Make sure filename and mode are bytestrings.
filename = self.filename
mode = self.mode
if not isinstance(filename, bytes):
log.debug("Allowing this affront to the RFC of a 4-byte packet")
fmt = b"!HH"
log.debug("Decoding ERR packet with fmt: %s", fmt)
self.opcode, self.errorcode = struct.unpack(fmt,
self.buffer)
else:
log.debug("Good ERR packet > 4 bytes")
fmt = b"!HH%dsx" % (len(self.buffer) - 5)
log.debug("Decoding ERR packet with fmt: %s", fmt)
self.opcode, self.errorcode, self.errmsg = struct.unpack(fmt,
self.buffer)
log.error("ERR packet - errorcode: %d, message: %s"
% (self.errorcode, self.errmsg))
return self
class TftpPacketOACK(TftpPacket, TftpPacketWithOptions):
"""
::
+-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+
| opc | opt1 | 0 | value1 | 0 | optN | 0 | valueN | 0 |
+-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+
"""
def __init__(self):
TftpPacket.__init__(self)
TftpPacketWithOptions.__init__(self)
self.opcode = 6
def __str__(self):
return 'OACK packet:\n options = %s' % self.options
def encode(self):
order suitable for sending over the wire.
This is an abstract method."""
raise NotImplementedError("Abstract method")
def decode(self):
"""The decode method of a TftpPacket takes a buffer off of the wire in
network-byte order, and decodes it, populating internal properties as
appropriate. This can only be done once the first 2-byte opcode has
already been decoded, but the data section does include the entire
datagram.
This is an abstract method."""
raise NotImplementedError("Abstract method")
class TftpPacketInitial(TftpPacket, TftpPacketWithOptions):
"""This class is a common parent class for the RRQ and WRQ packets, as
they share quite a bit of code."""
def __init__(self):
TftpPacket.__init__(self)
TftpPacketWithOptions.__init__(self)
self.filename = None
self.mode = None
def encode(self):
"""Encode the packet's buffer from the instance variables."""
tftpassert(self.filename, "filename required in initial packet")
tftpassert(self.mode, "mode required in initial packet")
# Make sure filename and mode are bytestrings.
filename = self.filename
mode = self.mode
if not isinstance(filename, bytes):
log.debug("Allowing this affront to the RFC of a 4-byte packet")
fmt = b"!HH"
log.debug("Decoding ERR packet with fmt: %s", fmt)
self.opcode, self.errorcode = struct.unpack(fmt,
self.buffer)
else:
log.debug("Good ERR packet > 4 bytes")
fmt = b"!HH%dsx" % (len(self.buffer) - 5)
log.debug("Decoding ERR packet with fmt: %s", fmt)
self.opcode, self.errorcode, self.errmsg = struct.unpack(fmt,
self.buffer)
log.error("ERR packet - errorcode: %d, message: %s"
% (self.errorcode, self.errmsg))
return self
class TftpPacketOACK(TftpPacket, TftpPacketWithOptions):
"""
::
+-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+
| opc | opt1 | 0 | value1 | 0 | optN | 0 | valueN | 0 |
+-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+
"""
def __init__(self):
TftpPacket.__init__(self)
TftpPacketWithOptions.__init__(self)
self.opcode = 6
def __str__(self):
return 'OACK packet:\n options = %s' % self.options
def encode(self):