2010-07-11 23:27:49 +00:00
|
|
|
"""This module implements the packet types of TFTP itself, and the
|
|
|
|
corresponding encode and decode methods for them."""
|
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
import struct
|
|
|
|
from TftpShared import *
|
|
|
|
|
2007-02-17 03:04:32 +00:00
|
|
|
class TftpSession(object):
|
|
|
|
"""This class is the base class for the tftp client and server. Any shared
|
|
|
|
code should be in this class."""
|
2009-08-16 23:44:57 +00:00
|
|
|
# FIXME: do we need this anymore?
|
|
|
|
pass
|
2007-02-17 03:04:32 +00:00
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
class TftpPacketWithOptions(object):
|
|
|
|
"""This class exists to permit some TftpPacket subclasses to share code
|
|
|
|
regarding options handling. It does not inherit from TftpPacket, as the
|
|
|
|
goal is just to share code here, and not cause diamond inheritance."""
|
2007-03-31 02:03:36 +00:00
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
def __init__(self):
|
2009-08-16 02:36:58 +00:00
|
|
|
self.options = {}
|
2006-12-17 06:08:05 +00:00
|
|
|
|
|
|
|
def setoptions(self, options):
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("in TftpPacketWithOptions.setoptions")
|
|
|
|
log.debug("options: " + str(options))
|
2006-12-17 06:08:05 +00:00
|
|
|
myoptions = {}
|
|
|
|
for key in options:
|
|
|
|
newkey = str(key)
|
|
|
|
myoptions[newkey] = str(options[key])
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("populated myoptions with %s = %s"
|
2006-12-17 06:08:05 +00:00
|
|
|
% (newkey, myoptions[newkey]))
|
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("setting options hash to: " + str(myoptions))
|
2008-10-05 00:42:27 +00:00
|
|
|
self._options = myoptions
|
2006-12-17 06:08:05 +00:00
|
|
|
|
|
|
|
def getoptions(self):
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("in TftpPacketWithOptions.getoptions")
|
2008-10-05 00:42:27 +00:00
|
|
|
return self._options
|
2006-12-17 06:08:05 +00:00
|
|
|
|
|
|
|
# Set up getter and setter on options to ensure that they are the proper
|
|
|
|
# type. They should always be strings, but we don't need to force the
|
|
|
|
# client to necessarily enter strings if we can avoid it.
|
|
|
|
options = property(getoptions, setoptions)
|
|
|
|
|
|
|
|
def decode_options(self, buffer):
|
|
|
|
"""This method decodes the section of the buffer that contains an
|
|
|
|
unknown number of options. It returns a dictionary of option names and
|
|
|
|
values."""
|
|
|
|
format = "!"
|
|
|
|
options = {}
|
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("decode_options: buffer is: " + repr(buffer))
|
|
|
|
log.debug("size of buffer is %d bytes" % len(buffer))
|
2006-12-17 06:08:05 +00:00
|
|
|
if len(buffer) == 0:
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("size of buffer is zero, returning empty hash")
|
2006-12-17 06:08:05 +00:00
|
|
|
return {}
|
|
|
|
|
|
|
|
# Count the nulls in the buffer. Each one terminates a string.
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("about to iterate options buffer counting nulls")
|
2006-12-17 06:08:05 +00:00
|
|
|
length = 0
|
|
|
|
for c in buffer:
|
2009-08-16 02:36:58 +00:00
|
|
|
#log.debug("iterating this byte: " + repr(c))
|
2006-12-17 06:08:05 +00:00
|
|
|
if ord(c) == 0:
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("found a null at length %d" % length)
|
2006-12-17 06:08:05 +00:00
|
|
|
if length > 0:
|
|
|
|
format += "%dsx" % length
|
|
|
|
length = -1
|
|
|
|
else:
|
|
|
|
raise TftpException, "Invalid options in buffer"
|
|
|
|
length += 1
|
2009-09-24 19:32:37 +00:00
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("about to unpack, format is: %s" % format)
|
2006-12-17 06:08:05 +00:00
|
|
|
mystruct = struct.unpack(format, buffer)
|
2009-09-24 19:32:37 +00:00
|
|
|
|
|
|
|
tftpassert(len(mystruct) % 2 == 0,
|
2006-12-17 06:08:05 +00:00
|
|
|
"packet with odd number of option/value pairs")
|
2009-09-24 19:32:37 +00:00
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
for i in range(0, len(mystruct), 2):
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("setting option %s to %s" % (mystruct[i], mystruct[i+1]))
|
2006-12-17 06:08:05 +00:00
|
|
|
options[mystruct[i]] = mystruct[i+1]
|
|
|
|
|
|
|
|
return options
|
|
|
|
|
|
|
|
class TftpPacket(object):
|
|
|
|
"""This class is the parent class of all tftp packet classes. It is an
|
|
|
|
abstract class, providing an interface, and should not be instantiated
|
|
|
|
directly."""
|
|
|
|
def __init__(self):
|
|
|
|
self.opcode = 0
|
|
|
|
self.buffer = None
|
|
|
|
|
|
|
|
def encode(self):
|
|
|
|
"""The encode method of a TftpPacket takes keyword arguments specific
|
|
|
|
to the type of packet, and packs an appropriate buffer in network-byte
|
|
|
|
order suitable for sending over the wire.
|
2009-09-24 19:32:37 +00:00
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
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.
|
2009-09-24 19:32:37 +00:00
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
This is an abstract method."""
|
|
|
|
raise NotImplementedError, "Abstract method"
|
|
|
|
|
|
|
|
class TftpPacketInitial(TftpPacket, TftpPacketWithOptions):
|
2009-09-24 19:32:37 +00:00
|
|
|
"""This class is a common parent class for the RRQ and WRQ packets, as
|
2006-12-17 06:08:05 +00:00
|
|
|
they share quite a bit of code."""
|
|
|
|
def __init__(self):
|
|
|
|
TftpPacket.__init__(self)
|
2008-10-05 00:42:27 +00:00
|
|
|
TftpPacketWithOptions.__init__(self)
|
2006-12-17 06:08:05 +00:00
|
|
|
self.filename = None
|
|
|
|
self.mode = None
|
2009-09-24 19:32:37 +00:00
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
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")
|
|
|
|
|
|
|
|
ptype = None
|
|
|
|
if self.opcode == 1: ptype = "RRQ"
|
|
|
|
else: ptype = "WRQ"
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("Encoding %s packet, filename = %s, mode = %s"
|
2006-12-17 06:08:05 +00:00
|
|
|
% (ptype, self.filename, self.mode))
|
|
|
|
for key in self.options:
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug(" Option %s = %s" % (key, self.options[key]))
|
2009-09-24 19:32:37 +00:00
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
format = "!H"
|
|
|
|
format += "%dsx" % len(self.filename)
|
|
|
|
if self.mode == "octet":
|
|
|
|
format += "5sx"
|
|
|
|
else:
|
|
|
|
raise AssertionError, "Unsupported mode: %s" % mode
|
|
|
|
# Add options.
|
|
|
|
options_list = []
|
|
|
|
if self.options.keys() > 0:
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("there are options to encode")
|
2006-12-17 06:08:05 +00:00
|
|
|
for key in self.options:
|
2008-10-09 01:31:34 +00:00
|
|
|
# Populate the option name
|
2006-12-17 06:08:05 +00:00
|
|
|
format += "%dsx" % len(key)
|
|
|
|
options_list.append(key)
|
2008-10-09 01:31:34 +00:00
|
|
|
# Populate the option value
|
|
|
|
format += "%dsx" % len(str(self.options[key]))
|
|
|
|
options_list.append(str(self.options[key]))
|
2006-12-17 06:08:05 +00:00
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("format is %s" % format)
|
|
|
|
log.debug("options_list is %s" % options_list)
|
|
|
|
log.debug("size of struct is %d" % struct.calcsize(format))
|
2006-12-17 06:08:05 +00:00
|
|
|
|
|
|
|
self.buffer = struct.pack(format,
|
|
|
|
self.opcode,
|
|
|
|
self.filename,
|
|
|
|
self.mode,
|
|
|
|
*options_list)
|
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("buffer is " + repr(self.buffer))
|
2006-12-17 06:08:05 +00:00
|
|
|
return self
|
2009-09-24 19:32:37 +00:00
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
def decode(self):
|
|
|
|
tftpassert(self.buffer, "Can't decode, buffer is empty")
|
|
|
|
|
|
|
|
# FIXME - this shares a lot of code with decode_options
|
|
|
|
nulls = 0
|
|
|
|
format = ""
|
|
|
|
nulls = length = tlength = 0
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("in decode: about to iterate buffer counting nulls")
|
2006-12-17 06:08:05 +00:00
|
|
|
subbuf = self.buffer[2:]
|
|
|
|
for c in subbuf:
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("iterating this byte: " + repr(c))
|
2006-12-17 06:08:05 +00:00
|
|
|
if ord(c) == 0:
|
|
|
|
nulls += 1
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("found a null at length %d, now have %d"
|
2006-12-17 06:08:05 +00:00
|
|
|
% (length, nulls))
|
|
|
|
format += "%dsx" % length
|
|
|
|
length = -1
|
|
|
|
# At 2 nulls, we want to mark that position for decoding.
|
|
|
|
if nulls == 2:
|
|
|
|
break
|
|
|
|
length += 1
|
|
|
|
tlength += 1
|
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("hopefully found end of mode at length %d" % tlength)
|
2006-12-17 06:08:05 +00:00
|
|
|
# length should now be the end of the mode.
|
|
|
|
tftpassert(nulls == 2, "malformed packet")
|
|
|
|
shortbuf = subbuf[:tlength+1]
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("about to unpack buffer with format: %s" % format)
|
|
|
|
log.debug("unpacking buffer: " + repr(shortbuf))
|
2006-12-17 06:08:05 +00:00
|
|
|
mystruct = struct.unpack(format, shortbuf)
|
|
|
|
|
|
|
|
tftpassert(len(mystruct) == 2, "malformed packet")
|
|
|
|
self.filename = mystruct[0]
|
2010-10-13 22:46:29 +00:00
|
|
|
self.mode = mystruct[1].lower() # force lc - bug 17
|
|
|
|
log.debug("set filename to %s" % self.filename)
|
|
|
|
log.debug("set mode to %s" % self.mode)
|
2006-12-17 06:08:05 +00:00
|
|
|
|
|
|
|
self.options = self.decode_options(subbuf[tlength+1:])
|
|
|
|
return self
|
|
|
|
|
|
|
|
class TftpPacketRRQ(TftpPacketInitial):
|
|
|
|
"""
|
2010-07-11 23:27:49 +00:00
|
|
|
::
|
|
|
|
|
|
|
|
2 bytes string 1 byte string 1 byte
|
|
|
|
-----------------------------------------------
|
|
|
|
RRQ/ | 01/02 | Filename | 0 | Mode | 0 |
|
|
|
|
WRQ -----------------------------------------------
|
2006-12-17 06:08:05 +00:00
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
TftpPacketInitial.__init__(self)
|
|
|
|
self.opcode = 1
|
|
|
|
|
2008-10-05 00:42:27 +00:00
|
|
|
def __str__(self):
|
|
|
|
s = 'RRQ packet: filename = %s' % self.filename
|
|
|
|
s += ' mode = %s' % self.mode
|
|
|
|
if self.options:
|
|
|
|
s += '\n options = %s' % self.options
|
|
|
|
return s
|
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
class TftpPacketWRQ(TftpPacketInitial):
|
|
|
|
"""
|
2010-07-11 23:27:49 +00:00
|
|
|
::
|
|
|
|
|
|
|
|
2 bytes string 1 byte string 1 byte
|
|
|
|
-----------------------------------------------
|
|
|
|
RRQ/ | 01/02 | Filename | 0 | Mode | 0 |
|
|
|
|
WRQ -----------------------------------------------
|
2006-12-17 06:08:05 +00:00
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
TftpPacketInitial.__init__(self)
|
|
|
|
self.opcode = 2
|
|
|
|
|
2008-10-05 00:42:27 +00:00
|
|
|
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
|
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
class TftpPacketDAT(TftpPacket):
|
|
|
|
"""
|
2010-07-11 23:27:49 +00:00
|
|
|
::
|
|
|
|
|
|
|
|
2 bytes 2 bytes n bytes
|
|
|
|
---------------------------------
|
|
|
|
DATA | 03 | Block # | Data |
|
|
|
|
---------------------------------
|
2006-12-17 06:08:05 +00:00
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
TftpPacket.__init__(self)
|
|
|
|
self.opcode = 3
|
|
|
|
self.blocknumber = 0
|
|
|
|
self.data = None
|
|
|
|
|
2008-10-05 00:42:27 +00:00
|
|
|
def __str__(self):
|
|
|
|
s = 'DAT packet: block %s' % self.blocknumber
|
|
|
|
if self.data:
|
|
|
|
s += '\n data: %d bytes' % len(self.data)
|
|
|
|
return s
|
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
def encode(self):
|
|
|
|
"""Encode the DAT packet. This method populates self.buffer, and
|
|
|
|
returns self for easy method chaining."""
|
|
|
|
if len(self.data) == 0:
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("Encoding an empty DAT packet")
|
2006-12-17 06:08:05 +00:00
|
|
|
format = "!HH%ds" % len(self.data)
|
2009-09-24 19:32:37 +00:00
|
|
|
self.buffer = struct.pack(format,
|
|
|
|
self.opcode,
|
|
|
|
self.blocknumber,
|
2006-12-17 06:08:05 +00:00
|
|
|
self.data)
|
|
|
|
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("!H", self.buffer[2:4])
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("decoding DAT packet, block number %d" % self.blocknumber)
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("should be %d bytes in the packet total"
|
2006-12-17 06:08:05 +00:00
|
|
|
% len(self.buffer))
|
|
|
|
# Everything else is data.
|
|
|
|
self.data = self.buffer[4:]
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("found %d bytes of data"
|
2006-12-17 06:08:05 +00:00
|
|
|
% len(self.data))
|
|
|
|
return self
|
|
|
|
|
|
|
|
class TftpPacketACK(TftpPacket):
|
|
|
|
"""
|
2010-07-11 23:27:49 +00:00
|
|
|
::
|
|
|
|
|
|
|
|
2 bytes 2 bytes
|
|
|
|
-------------------
|
|
|
|
ACK | 04 | Block # |
|
|
|
|
--------------------
|
2006-12-17 06:08:05 +00:00
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
TftpPacket.__init__(self)
|
|
|
|
self.opcode = 4
|
|
|
|
self.blocknumber = 0
|
|
|
|
|
2008-10-05 00:42:27 +00:00
|
|
|
def __str__(self):
|
|
|
|
return 'ACK packet: block %d' % self.blocknumber
|
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
def encode(self):
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("encoding ACK: opcode = %d, block = %d"
|
2006-12-17 06:08:05 +00:00
|
|
|
% (self.opcode, self.blocknumber))
|
|
|
|
self.buffer = struct.pack("!HH", self.opcode, self.blocknumber)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def decode(self):
|
|
|
|
self.opcode, self.blocknumber = struct.unpack("!HH", self.buffer)
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("decoded ACK packet: opcode = %d, block = %d"
|
2006-12-17 06:08:05 +00:00
|
|
|
% (self.opcode, self.blocknumber))
|
|
|
|
return self
|
|
|
|
|
|
|
|
class TftpPacketERR(TftpPacket):
|
|
|
|
"""
|
2010-07-11 23:27:49 +00:00
|
|
|
::
|
|
|
|
|
|
|
|
2 bytes 2 bytes string 1 byte
|
|
|
|
----------------------------------------
|
|
|
|
ERROR | 05 | ErrorCode | ErrMsg | 0 |
|
|
|
|
----------------------------------------
|
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
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.
|
|
|
|
4 Illegal TFTP operation.
|
|
|
|
5 Unknown transfer ID.
|
|
|
|
6 File already exists.
|
|
|
|
7 No such user.
|
|
|
|
8 Failed to negotiate options
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
TftpPacket.__init__(self)
|
|
|
|
self.opcode = 5
|
|
|
|
self.errorcode = 0
|
2009-09-24 19:32:37 +00:00
|
|
|
# FIXME: We don't encode the errmsg...
|
2006-12-17 06:08:05 +00:00
|
|
|
self.errmsg = None
|
|
|
|
# FIXME - integrate in TftpErrors references?
|
|
|
|
self.errmsgs = {
|
|
|
|
1: "File not found",
|
|
|
|
2: "Access violation",
|
|
|
|
3: "Disk full or allocation exceeded",
|
|
|
|
4: "Illegal TFTP operation",
|
|
|
|
5: "Unknown transfer ID",
|
|
|
|
6: "File already exists",
|
|
|
|
7: "No such user",
|
|
|
|
8: "Failed to negotiate options"
|
|
|
|
}
|
|
|
|
|
2008-10-05 00:42:27 +00:00
|
|
|
def __str__(self):
|
2008-10-06 03:06:41 +00:00
|
|
|
s = 'ERR packet: errorcode = %d' % self.errorcode
|
|
|
|
s += '\n msg = %s' % self.errmsgs.get(self.errorcode, '')
|
|
|
|
return s
|
2008-10-05 00:42:27 +00:00
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
def encode(self):
|
|
|
|
"""Encode the DAT packet based on instance variables, populating
|
|
|
|
self.buffer, returning self."""
|
|
|
|
format = "!HH%dsx" % len(self.errmsgs[self.errorcode])
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("encoding ERR packet with format %s" % format)
|
2006-12-17 06:08:05 +00:00
|
|
|
self.buffer = struct.pack(format,
|
|
|
|
self.opcode,
|
|
|
|
self.errorcode,
|
|
|
|
self.errmsgs[self.errorcode])
|
|
|
|
return self
|
|
|
|
|
|
|
|
def decode(self):
|
|
|
|
"Decode self.buffer, populating instance variables and return self."
|
2009-09-24 19:32:37 +00:00
|
|
|
buflen = len(self.buffer)
|
|
|
|
tftpassert(buflen >= 4, "malformed ERR packet, too short")
|
|
|
|
log.debug("Decoding ERR packet, length %s bytes" % buflen)
|
|
|
|
if buflen == 4:
|
|
|
|
log.debug("Allowing this affront to the RFC of a 4-byte packet")
|
|
|
|
format = "!HH"
|
|
|
|
log.debug("Decoding ERR packet with format: %s" % format)
|
|
|
|
self.opcode, self.errorcode = struct.unpack(format,
|
|
|
|
self.buffer)
|
|
|
|
else:
|
|
|
|
log.debug("Good ERR packet > 4 bytes")
|
|
|
|
format = "!HH%dsx" % (len(self.buffer) - 5)
|
|
|
|
log.debug("Decoding ERR packet with format: %s" % format)
|
|
|
|
self.opcode, self.errorcode, self.errmsg = struct.unpack(format,
|
|
|
|
self.buffer)
|
2009-08-16 02:36:58 +00:00
|
|
|
log.error("ERR packet - errorcode: %d, message: %s"
|
2006-12-17 06:08:05 +00:00
|
|
|
% (self.errorcode, self.errmsg))
|
|
|
|
return self
|
2009-09-24 19:32:37 +00:00
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
class TftpPacketOACK(TftpPacket, TftpPacketWithOptions):
|
|
|
|
"""
|
2010-07-11 23:27:49 +00:00
|
|
|
::
|
|
|
|
|
|
|
|
+-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+
|
|
|
|
| opc | opt1 | 0 | value1 | 0 | optN | 0 | valueN | 0 |
|
|
|
|
+-------+---~~---+---+---~~---+---+---~~---+---+---~~---+---+
|
2006-12-17 06:08:05 +00:00
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
TftpPacket.__init__(self)
|
2008-10-05 00:42:27 +00:00
|
|
|
TftpPacketWithOptions.__init__(self)
|
2006-12-17 06:08:05 +00:00
|
|
|
self.opcode = 6
|
2008-10-05 00:42:27 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'OACK packet:\n options = %s' % self.options
|
2009-09-24 19:32:37 +00:00
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
def encode(self):
|
|
|
|
format = "!H" # opcode
|
|
|
|
options_list = []
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("in TftpPacketOACK.encode")
|
2006-12-17 06:08:05 +00:00
|
|
|
for key in self.options:
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("looping on option key %s" % key)
|
|
|
|
log.debug("value is %s" % self.options[key])
|
2006-12-17 06:08:05 +00:00
|
|
|
format += "%dsx" % len(key)
|
|
|
|
format += "%dsx" % len(self.options[key])
|
|
|
|
options_list.append(key)
|
|
|
|
options_list.append(self.options[key])
|
|
|
|
self.buffer = struct.pack(format, self.opcode, *options_list)
|
|
|
|
return self
|
2009-09-24 19:32:37 +00:00
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
def decode(self):
|
|
|
|
self.options = self.decode_options(self.buffer[2:])
|
|
|
|
return self
|
2009-09-24 19:32:37 +00:00
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
def match_options(self, options):
|
|
|
|
"""This method takes a set of options, and tries to match them with
|
|
|
|
its own. It can accept some changes in those options from the server as
|
|
|
|
part of a negotiation. Changed or unchanged, it will return a dict of
|
|
|
|
the options so that the session can update itself to the negotiated
|
|
|
|
options."""
|
|
|
|
for name in self.options:
|
|
|
|
if options.has_key(name):
|
|
|
|
if name == 'blksize':
|
|
|
|
# We can accept anything between the min and max values.
|
|
|
|
size = self.options[name]
|
|
|
|
if size >= MIN_BLKSIZE and size <= MAX_BLKSIZE:
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("negotiated blksize of %d bytes" % size)
|
2006-12-17 06:08:05 +00:00
|
|
|
options[blksize] = size
|
|
|
|
else:
|
|
|
|
raise TftpException, "Unsupported option: %s" % name
|
|
|
|
return True
|