2010-07-12 01:19:01 +00:00
|
|
|
"""This module implements the TftpPacketFactory class, which can take a binary
|
|
|
|
buffer, and return the appropriate TftpPacket object to represent it, via the
|
|
|
|
parse() method."""
|
|
|
|
|
2006-12-17 06:08:05 +00:00
|
|
|
from TftpShared import *
|
|
|
|
from TftpPacketTypes import *
|
|
|
|
|
|
|
|
class TftpPacketFactory(object):
|
2007-03-31 02:03:36 +00:00
|
|
|
"""This class generates TftpPacket objects. It is responsible for parsing
|
|
|
|
raw buffers off of the wire and returning objects representing them, via
|
|
|
|
the parse() method."""
|
2006-12-17 06:08:05 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.classes = {
|
|
|
|
1: TftpPacketRRQ,
|
|
|
|
2: TftpPacketWRQ,
|
|
|
|
3: TftpPacketDAT,
|
|
|
|
4: TftpPacketACK,
|
|
|
|
5: TftpPacketERR,
|
|
|
|
6: TftpPacketOACK
|
|
|
|
}
|
|
|
|
|
|
|
|
def parse(self, buffer):
|
|
|
|
"""This method is used to parse an existing datagram into its
|
2007-03-31 02:03:36 +00:00
|
|
|
corresponding TftpPacket object. The buffer is the raw bytes off of
|
|
|
|
the network."""
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("parsing a %d byte packet" % len(buffer))
|
2006-12-17 06:08:05 +00:00
|
|
|
(opcode,) = struct.unpack("!H", buffer[:2])
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("opcode is %d" % opcode)
|
2007-03-31 02:03:36 +00:00
|
|
|
packet = self.__create(opcode)
|
2006-12-17 06:08:05 +00:00
|
|
|
packet.buffer = buffer
|
|
|
|
return packet.decode()
|
2007-03-31 02:03:36 +00:00
|
|
|
|
|
|
|
def __create(self, opcode):
|
|
|
|
"""This method returns the appropriate class object corresponding to
|
|
|
|
the passed opcode."""
|
2009-09-24 19:32:37 +00:00
|
|
|
tftpassert(self.classes.has_key(opcode),
|
2007-03-31 02:03:36 +00:00
|
|
|
"Unsupported opcode: %d" % opcode)
|
|
|
|
|
|
|
|
packet = self.classes[opcode]()
|
|
|
|
|
|
|
|
return packet
|