2009-04-09 03:29:43 +00:00
|
|
|
from TftpShared import *
|
|
|
|
from TftpPacketTypes import *
|
|
|
|
from TftpPacketFactory import *
|
2009-08-16 23:44:57 +00:00
|
|
|
import socket, time, os
|
2009-04-09 03:29:43 +00:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Utility classes
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
class TftpMetrics(object):
|
|
|
|
"""A class representing metrics of the transfer."""
|
|
|
|
def __init__(self):
|
|
|
|
# Bytes transferred
|
|
|
|
self.bytes = 0
|
2009-08-19 02:27:18 +00:00
|
|
|
# Bytes re-sent
|
2009-08-19 02:38:26 +00:00
|
|
|
self.resent_bytes = 0
|
2009-04-09 03:29:43 +00:00
|
|
|
# Duplicate packets received
|
|
|
|
self.dups = {}
|
|
|
|
self.dupcount = 0
|
|
|
|
# Times
|
|
|
|
self.start_time = 0
|
|
|
|
self.end_time = 0
|
|
|
|
self.duration = 0
|
|
|
|
# Rates
|
|
|
|
self.bps = 0
|
|
|
|
self.kbps = 0
|
2009-08-16 02:36:58 +00:00
|
|
|
# Generic errors
|
|
|
|
self.errors = 0
|
2009-04-09 03:29:43 +00:00
|
|
|
|
|
|
|
def compute(self):
|
|
|
|
# Compute transfer time
|
2009-04-11 00:54:20 +00:00
|
|
|
self.duration = self.end_time - self.start_time
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("TftpMetrics.compute: duration is %s" % self.duration)
|
2009-04-11 00:54:20 +00:00
|
|
|
self.bps = (self.bytes * 8.0) / self.duration
|
|
|
|
self.kbps = self.bps / 1024.0
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("TftpMetrics.compute: kbps is %s" % self.kbps)
|
2009-04-09 03:29:43 +00:00
|
|
|
for key in self.dups:
|
2009-08-16 02:36:58 +00:00
|
|
|
self.dupcount += self.dups[key]
|
|
|
|
|
|
|
|
def add_dup(self, blocknumber):
|
|
|
|
"""This method adds a dup for a block number to the metrics."""
|
|
|
|
log.debug("Recording a dup for block %d" % blocknumber)
|
|
|
|
if self.dups.has_key(blocknumber):
|
2009-08-16 23:44:57 +00:00
|
|
|
self.dups[blocknumber] += 1
|
2009-08-16 02:36:58 +00:00
|
|
|
else:
|
2009-08-16 23:44:57 +00:00
|
|
|
self.dups[blocknumber] = 1
|
|
|
|
tftpassert(self.dups[blocknumber] < MAX_DUPS,
|
2009-08-16 02:36:58 +00:00
|
|
|
"Max duplicates for block %d reached" % blocknumber)
|
2009-04-09 03:29:43 +00:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Context classes
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
class TftpContext(object):
|
|
|
|
"""The base class of the contexts."""
|
2009-08-16 02:36:58 +00:00
|
|
|
|
|
|
|
def __init__(self, host, port, timeout):
|
2009-04-09 03:29:43 +00:00
|
|
|
"""Constructor for the base context, setting shared instance
|
|
|
|
variables."""
|
2009-08-16 02:36:58 +00:00
|
|
|
self.file_to_transfer = None
|
|
|
|
self.fileobj = None
|
|
|
|
self.options = None
|
|
|
|
self.packethook = None
|
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
self.sock.settimeout(timeout)
|
|
|
|
self.state = None
|
|
|
|
self.next_block = 0
|
2009-04-09 03:29:43 +00:00
|
|
|
self.factory = TftpPacketFactory()
|
2009-08-16 02:36:58 +00:00
|
|
|
# Note, setting the host will also set self.address, as it's a property.
|
2009-04-09 03:29:43 +00:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
|
|
|
# The port associated with the TID
|
|
|
|
self.tidport = None
|
|
|
|
# Metrics
|
|
|
|
self.metrics = TftpMetrics()
|
2009-08-16 02:36:58 +00:00
|
|
|
# Flag when the transfer is pending completion.
|
|
|
|
self.pending_complete = False
|
2009-08-16 23:44:57 +00:00
|
|
|
# Time when this context last received any traffic.
|
2009-08-19 02:27:18 +00:00
|
|
|
# FIXME: does this belong in metrics?
|
2009-08-16 23:44:57 +00:00
|
|
|
self.last_update = 0
|
2009-08-19 02:27:18 +00:00
|
|
|
# The last DAT packet we sent, if applicable, to make resending easy.
|
|
|
|
self.last_dat_pkt = None
|
2009-08-16 02:36:58 +00:00
|
|
|
|
|
|
|
def checkTimeout(self, now):
|
2009-08-16 23:44:57 +00:00
|
|
|
"""Compare current time with last_update time, and raise an exception
|
|
|
|
if we're over SOCK_TIMEOUT time."""
|
|
|
|
if now - self.last_update > SOCK_TIMEOUT:
|
|
|
|
raise TftpException, "Timeout waiting for traffic"
|
2009-04-09 03:29:43 +00:00
|
|
|
|
|
|
|
def start(self):
|
2009-08-19 02:27:18 +00:00
|
|
|
raise NotImplementedError, "Abstract method"
|
2009-04-09 03:29:43 +00:00
|
|
|
|
|
|
|
def end(self):
|
2009-08-19 02:27:18 +00:00
|
|
|
raise NotImplementedError, "Abstract method"
|
2009-06-20 21:30:44 +00:00
|
|
|
|
2009-04-09 03:29:43 +00:00
|
|
|
def gethost(self):
|
|
|
|
"Simple getter method for use in a property."
|
|
|
|
return self.__host
|
2009-06-20 21:30:44 +00:00
|
|
|
|
2009-04-09 03:29:43 +00:00
|
|
|
def sethost(self, host):
|
|
|
|
"""Setter method that also sets the address property as a result
|
|
|
|
of the host that is set."""
|
|
|
|
self.__host = host
|
|
|
|
self.address = socket.gethostbyname(host)
|
2009-06-20 21:30:44 +00:00
|
|
|
|
2009-04-09 03:29:43 +00:00
|
|
|
host = property(gethost, sethost)
|
|
|
|
|
2009-06-20 21:30:44 +00:00
|
|
|
def setNextBlock(self, block):
|
2009-04-09 03:29:43 +00:00
|
|
|
if block > 2 ** 16:
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Block number rollover to 0 again")
|
2009-04-09 03:29:43 +00:00
|
|
|
block = 0
|
|
|
|
self.__eblock = block
|
|
|
|
|
2009-06-20 21:30:44 +00:00
|
|
|
def getNextBlock(self):
|
2009-04-09 03:29:43 +00:00
|
|
|
return self.__eblock
|
|
|
|
|
2009-06-20 21:30:44 +00:00
|
|
|
next_block = property(getNextBlock, setNextBlock)
|
2009-04-09 03:29:43 +00:00
|
|
|
|
|
|
|
def cycle(self):
|
|
|
|
"""Here we wait for a response from the server after sending it
|
|
|
|
something, and dispatch appropriate action to that response."""
|
2009-08-16 02:36:58 +00:00
|
|
|
# FIXME: This won't work very well in a server context with multiple
|
|
|
|
# sessions running.
|
2009-04-09 03:29:43 +00:00
|
|
|
for i in range(TIMEOUT_RETRIES):
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("In cycle, receive attempt %d" % i)
|
2009-04-09 03:29:43 +00:00
|
|
|
try:
|
|
|
|
(buffer, (raddress, rport)) = self.sock.recvfrom(MAX_BLKSIZE)
|
|
|
|
except socket.timeout, err:
|
2009-08-16 02:36:58 +00:00
|
|
|
log.warn("Timeout waiting for traffic, retrying...")
|
2009-04-09 03:29:43 +00:00
|
|
|
continue
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise TftpException, "Hit max timeouts, giving up."
|
|
|
|
|
2009-04-11 00:54:20 +00:00
|
|
|
# Ok, we've received a packet. Log it.
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("Received %d bytes from %s:%s"
|
2009-04-09 03:29:43 +00:00
|
|
|
% (len(buffer), raddress, rport))
|
2009-08-16 23:44:57 +00:00
|
|
|
# And update our last updated time.
|
|
|
|
self.last_update = time.time()
|
2009-04-09 03:29:43 +00:00
|
|
|
|
2009-04-11 00:54:20 +00:00
|
|
|
# Decode it.
|
|
|
|
recvpkt = self.factory.parse(buffer)
|
|
|
|
|
2009-04-09 03:29:43 +00:00
|
|
|
# Check for known "connection".
|
|
|
|
if raddress != self.address:
|
2009-08-16 02:36:58 +00:00
|
|
|
log.warn("Received traffic from %s, expected host %s. Discarding"
|
2009-04-09 03:29:43 +00:00
|
|
|
% (raddress, self.host))
|
|
|
|
|
2009-04-11 02:25:45 +00:00
|
|
|
if self.tidport and self.tidport != rport:
|
2009-08-16 02:36:58 +00:00
|
|
|
log.warn("Received traffic from %s:%s but we're "
|
2009-04-09 03:29:43 +00:00
|
|
|
"connected to %s:%s. Discarding."
|
|
|
|
% (raddress, rport,
|
2009-04-11 02:25:45 +00:00
|
|
|
self.host, self.tidport))
|
2009-04-09 03:29:43 +00:00
|
|
|
|
|
|
|
# If there is a packethook defined, call it. We unconditionally
|
|
|
|
# pass all packets, it's up to the client to screen out different
|
|
|
|
# kinds of packets. This way, the client is privy to things like
|
|
|
|
# negotiated options.
|
|
|
|
if self.packethook:
|
|
|
|
self.packethook(recvpkt)
|
|
|
|
|
|
|
|
# And handle it, possibly changing state.
|
|
|
|
self.state = self.state.handle(recvpkt, raddress, rport)
|
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
class TftpContextServer(TftpContext):
|
|
|
|
"""The context for the server."""
|
|
|
|
def __init__(self, host, port, timeout, root, dyn_file_func):
|
|
|
|
TftpContext.__init__(self,
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
timeout)
|
|
|
|
# At this point we have no idea if this is a download or an upload. We
|
|
|
|
# need to let the start state determine that.
|
2009-08-16 23:44:57 +00:00
|
|
|
self.state = TftpStateServerStart(self)
|
2009-08-16 02:36:58 +00:00
|
|
|
self.root = root
|
|
|
|
self.dyn_file_func = dyn_file_func
|
2009-08-16 23:44:57 +00:00
|
|
|
# In a server, the tidport is the same as the port. This is also true
|
|
|
|
# with symmetric UDP, which we haven't implemented yet.
|
|
|
|
self.tidport = port
|
2009-08-16 02:36:58 +00:00
|
|
|
|
|
|
|
def start(self, buffer):
|
|
|
|
"""Start the state cycle. Note that the server context receives an
|
2009-08-16 23:44:57 +00:00
|
|
|
initial packet in its start method. Also note that the server does not
|
|
|
|
loop on cycle(), as it expects the TftpServer object to manage
|
|
|
|
that."""
|
|
|
|
log.debug("In TftpContextServer.start")
|
2009-08-16 02:36:58 +00:00
|
|
|
self.metrics.start_time = time.time()
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Set metrics.start_time to %s" % self.metrics.start_time)
|
2009-08-16 23:44:57 +00:00
|
|
|
# And update our last updated time.
|
|
|
|
self.last_update = time.time()
|
2009-08-16 02:36:58 +00:00
|
|
|
|
|
|
|
pkt = self.factory.parse(buffer)
|
|
|
|
log.debug("TftpContextServer.start() - factory returned a %s" % pkt)
|
|
|
|
|
|
|
|
# Call handle once with the initial packet. This should put us into
|
|
|
|
# the download or the upload state.
|
|
|
|
self.state = self.state.handle(pkt,
|
|
|
|
self.host,
|
|
|
|
self.port)
|
|
|
|
|
2009-08-16 23:44:57 +00:00
|
|
|
# FIXME
|
|
|
|
# How do we ensure that the server closes files, even on error?
|
2009-08-16 02:36:58 +00:00
|
|
|
|
2009-08-19 02:27:18 +00:00
|
|
|
def end(self):
|
|
|
|
"""Finish up the context."""
|
|
|
|
self.metrics.end_time = time.time()
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Set metrics.end_time to %s" % self.metrics.end_time)
|
2009-08-19 02:27:18 +00:00
|
|
|
self.metrics.compute()
|
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
class TftpContextClientUpload(TftpContext):
|
2009-06-20 21:30:44 +00:00
|
|
|
"""The upload context for the client during an upload."""
|
2009-08-16 23:44:57 +00:00
|
|
|
def __init__(self,
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
filename,
|
|
|
|
input,
|
|
|
|
options,
|
|
|
|
packethook,
|
|
|
|
timeout):
|
2009-08-16 02:36:58 +00:00
|
|
|
TftpContext.__init__(self,
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
timeout)
|
|
|
|
self.file_to_transfer = filename
|
|
|
|
self.options = options
|
|
|
|
self.packethook = packethook
|
2009-06-20 21:30:44 +00:00
|
|
|
self.fileobj = open(input, "wb")
|
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("TftpContextClientUpload.__init__()")
|
|
|
|
log.debug("file_to_transfer = %s, options = %s" %
|
2009-06-20 21:30:44 +00:00
|
|
|
(self.file_to_transfer, self.options))
|
|
|
|
|
|
|
|
def start(self):
|
2009-09-24 19:32:37 +00:00
|
|
|
log.info("Sending tftp upload request to %s" % self.host)
|
2009-08-16 02:36:58 +00:00
|
|
|
log.info(" filename -> %s" % self.file_to_transfer)
|
|
|
|
log.info(" options -> %s" % self.options)
|
2009-06-20 21:30:44 +00:00
|
|
|
|
|
|
|
self.metrics.start_time = time.time()
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Set metrics.start_time to %s" % self.metrics.start_time)
|
2009-06-20 21:30:44 +00:00
|
|
|
|
|
|
|
# FIXME: put this in a sendWRQ method?
|
|
|
|
pkt = TftpPacketWRQ()
|
|
|
|
pkt.filename = self.file_to_transfer
|
|
|
|
pkt.mode = "octet" # FIXME - shouldn't hardcode this
|
|
|
|
pkt.options = self.options
|
|
|
|
self.sock.sendto(pkt.encode().buffer, (self.host, self.port))
|
|
|
|
self.next_block = 1
|
|
|
|
|
|
|
|
self.state = TftpStateSentWRQ(self)
|
|
|
|
|
|
|
|
try:
|
|
|
|
while self.state:
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("State is %s" % self.state)
|
2009-06-20 21:30:44 +00:00
|
|
|
self.cycle()
|
|
|
|
finally:
|
|
|
|
self.fileobj.close()
|
|
|
|
|
|
|
|
def end(self):
|
2009-08-19 02:27:18 +00:00
|
|
|
"""Finish up the context."""
|
|
|
|
self.metrics.end_time = time.time()
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Set metrics.end_time to %s" % self.metrics.end_time)
|
2009-08-19 02:27:18 +00:00
|
|
|
self.metrics.compute()
|
2009-06-20 21:30:44 +00:00
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
class TftpContextClientDownload(TftpContext):
|
2009-06-20 21:30:44 +00:00
|
|
|
"""The download context for the client during a download."""
|
2009-08-16 23:44:57 +00:00
|
|
|
def __init__(self,
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
filename,
|
|
|
|
output,
|
|
|
|
options,
|
|
|
|
packethook,
|
|
|
|
timeout):
|
2009-08-16 02:36:58 +00:00
|
|
|
TftpContext.__init__(self,
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
timeout)
|
2009-08-16 23:44:57 +00:00
|
|
|
# FIXME: should we refactor setting of these params?
|
|
|
|
self.file_to_transfer = filename
|
|
|
|
self.options = options
|
|
|
|
self.packethook = packethook
|
2009-06-20 21:30:44 +00:00
|
|
|
# FIXME - need to support alternate return formats than files?
|
|
|
|
# File-like objects would be ideal, ala duck-typing.
|
|
|
|
self.fileobj = open(output, "wb")
|
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("TftpContextClientDownload.__init__()")
|
|
|
|
log.debug("file_to_transfer = %s, options = %s" %
|
2009-06-20 21:30:44 +00:00
|
|
|
(self.file_to_transfer, self.options))
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
"""Initiate the download."""
|
2009-09-24 19:32:37 +00:00
|
|
|
log.info("Sending tftp download request to %s" % self.host)
|
2009-08-16 02:36:58 +00:00
|
|
|
log.info(" filename -> %s" % self.file_to_transfer)
|
|
|
|
log.info(" options -> %s" % self.options)
|
2009-06-20 21:30:44 +00:00
|
|
|
|
|
|
|
self.metrics.start_time = time.time()
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Set metrics.start_time to %s" % self.metrics.start_time)
|
2009-06-20 21:30:44 +00:00
|
|
|
|
|
|
|
# FIXME: put this in a sendRRQ method?
|
|
|
|
pkt = TftpPacketRRQ()
|
|
|
|
pkt.filename = self.file_to_transfer
|
|
|
|
pkt.mode = "octet" # FIXME - shouldn't hardcode this
|
|
|
|
pkt.options = self.options
|
|
|
|
self.sock.sendto(pkt.encode().buffer, (self.host, self.port))
|
|
|
|
self.next_block = 1
|
|
|
|
|
|
|
|
self.state = TftpStateSentRRQ(self)
|
|
|
|
|
|
|
|
try:
|
|
|
|
while self.state:
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("State is %s" % self.state)
|
2009-06-20 21:30:44 +00:00
|
|
|
self.cycle()
|
|
|
|
finally:
|
|
|
|
self.fileobj.close()
|
|
|
|
|
|
|
|
def end(self):
|
|
|
|
"""Finish up the context."""
|
|
|
|
self.metrics.end_time = time.time()
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Set metrics.end_time to %s" % self.metrics.end_time)
|
2009-06-20 21:30:44 +00:00
|
|
|
self.metrics.compute()
|
|
|
|
|
|
|
|
|
2009-04-09 03:29:43 +00:00
|
|
|
###############################################################################
|
|
|
|
# State classes
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
class TftpState(object):
|
|
|
|
"""The base class for the states."""
|
|
|
|
|
|
|
|
def __init__(self, context):
|
|
|
|
"""Constructor for setting up common instance variables. The involved
|
|
|
|
file object is required, since in tftp there's always a file
|
|
|
|
involved."""
|
|
|
|
self.context = context
|
|
|
|
|
|
|
|
def handle(self, pkt, raddress, rport):
|
|
|
|
"""An abstract method for handling a packet. It is expected to return
|
|
|
|
a TftpState object, either itself or a new state."""
|
|
|
|
raise NotImplementedError, "Abstract method"
|
|
|
|
|
2009-06-20 21:30:44 +00:00
|
|
|
def handleOACK(self, pkt):
|
|
|
|
"""This method handles an OACK from the server, syncing any accepted
|
|
|
|
options."""
|
|
|
|
if pkt.options.keys() > 0:
|
|
|
|
if pkt.match_options(self.context.options):
|
2009-08-16 02:36:58 +00:00
|
|
|
log.info("Successful negotiation of options")
|
2009-06-20 21:30:44 +00:00
|
|
|
# Set options to OACK options
|
|
|
|
self.context.options = pkt.options
|
|
|
|
for key in self.context.options:
|
2009-08-16 02:36:58 +00:00
|
|
|
log.info(" %s = %s" % (key, self.context.options[key]))
|
2009-06-20 21:30:44 +00:00
|
|
|
else:
|
2009-09-24 19:32:37 +00:00
|
|
|
log.error("Failed to negotiate options")
|
2009-06-20 21:30:44 +00:00
|
|
|
raise TftpException, "Failed to negotiate options"
|
|
|
|
else:
|
|
|
|
raise TftpException, "No options found in OACK"
|
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
def returnSupportedOptions(self, options):
|
|
|
|
"""This method takes a requested options list from a client, and
|
|
|
|
returns the ones that are supported."""
|
|
|
|
# We support the options blksize and tsize right now.
|
|
|
|
# FIXME - put this somewhere else?
|
|
|
|
accepted_options = {}
|
|
|
|
for option in options:
|
|
|
|
if option == 'blksize':
|
|
|
|
# Make sure it's valid.
|
|
|
|
if int(options[option]) > MAX_BLKSIZE:
|
2009-08-16 23:44:57 +00:00
|
|
|
log.info("Client requested blksize greater than %d "
|
|
|
|
"setting to maximum" % MAX_BLKSIZE)
|
2009-08-16 02:36:58 +00:00
|
|
|
accepted_options[option] = MAX_BLKSIZE
|
2009-08-16 23:44:57 +00:00
|
|
|
elif int(options[option]) < MIN_BLKSIZE:
|
|
|
|
log.info("Client requested blksize less than %d "
|
|
|
|
"setting to minimum" % MIN_BLKSIZE)
|
|
|
|
accepted_options[option] = MIN_BLKSIZE
|
2009-08-16 02:36:58 +00:00
|
|
|
else:
|
2009-08-16 23:44:57 +00:00
|
|
|
accepted_options[option] = options[option]
|
|
|
|
elif option == 'tsize':
|
|
|
|
log.debug("tsize option is set")
|
|
|
|
accepted_options['tsize'] = 1
|
|
|
|
else:
|
|
|
|
log.info("Dropping unsupported option '%s'" % option)
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Returning these accepted options: %s" % accepted_options)
|
2009-08-16 02:36:58 +00:00
|
|
|
return accepted_options
|
|
|
|
|
|
|
|
def serverInitial(self, pkt, raddress, rport):
|
|
|
|
"""This method performs initial setup for a server context transfer,
|
|
|
|
put here to refactor code out of the TftpStateServerRecvRRQ and
|
|
|
|
TftpStateServerRecvWRQ classes, since their initial setup is
|
|
|
|
identical. The method returns a boolean, sendoack, to indicate whether
|
|
|
|
it is required to send an OACK to the client."""
|
|
|
|
options = pkt.options
|
|
|
|
sendoack = False
|
|
|
|
if not options:
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Setting default options, blksize")
|
2009-08-16 02:36:58 +00:00
|
|
|
# FIXME: put default options elsewhere
|
|
|
|
self.context.options = { 'blksize': DEF_BLKSIZE }
|
|
|
|
else:
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Options requested: %s" % options)
|
2009-08-16 02:36:58 +00:00
|
|
|
self.context.options = self.returnSupportedOptions(options)
|
|
|
|
sendoack = True
|
|
|
|
|
|
|
|
# FIXME - only octet mode is supported at this time.
|
|
|
|
if pkt.mode != 'octet':
|
|
|
|
self.sendError(TftpErrors.IllegalTftpOp)
|
|
|
|
raise TftpException, \
|
|
|
|
"Only octet transfers are supported at this time."
|
|
|
|
|
|
|
|
# test host/port of client end
|
|
|
|
if self.context.host != raddress or self.context.port != rport:
|
|
|
|
self.sendError(TftpErrors.UnknownTID)
|
|
|
|
log.error("Expected traffic from %s:%s but received it "
|
|
|
|
"from %s:%s instead."
|
|
|
|
% (self.context.host,
|
|
|
|
self.context.port,
|
|
|
|
raddress,
|
|
|
|
rport))
|
|
|
|
# FIXME: increment an error count?
|
|
|
|
# Return same state, we're still waiting for valid traffic.
|
|
|
|
return self
|
|
|
|
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Requested filename is %s" % pkt.filename)
|
2009-08-16 02:36:58 +00:00
|
|
|
# There are no os.sep's allowed in the filename.
|
|
|
|
# FIXME: Should we allow subdirectories?
|
|
|
|
if pkt.filename.find(os.sep) >= 0:
|
|
|
|
self.sendError(TftpErrors.IllegalTftpOp)
|
|
|
|
raise TftpException, "%s found in filename, not permitted" % os.sep
|
|
|
|
|
|
|
|
self.context.file_to_transfer = pkt.filename
|
|
|
|
|
|
|
|
return sendoack
|
|
|
|
|
|
|
|
def sendDAT(self, resend=False):
|
|
|
|
"""This method sends the next DAT packet based on the data in the
|
|
|
|
context. It returns a boolean indicating whether the transfer is
|
|
|
|
finished."""
|
2009-06-20 21:30:44 +00:00
|
|
|
finished = False
|
|
|
|
blocknumber = self.context.next_block
|
2009-08-16 23:44:57 +00:00
|
|
|
tftpassert( blocknumber > 0, "There is no block zero!" )
|
2009-08-19 02:27:18 +00:00
|
|
|
dat = None
|
|
|
|
if resend:
|
|
|
|
log.warn("Resending block number %d" % blocknumber)
|
|
|
|
dat = self.context.last_dat_pkt
|
2009-08-19 02:38:26 +00:00
|
|
|
self.context.metrics.resent_bytes += len(dat.data)
|
2009-08-19 02:27:18 +00:00
|
|
|
self.context.metrics.add_dup(dat)
|
|
|
|
else:
|
2009-06-20 21:30:44 +00:00
|
|
|
blksize = int(self.context.options['blksize'])
|
|
|
|
buffer = self.context.fileobj.read(blksize)
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("Read %d bytes into buffer" % len(buffer))
|
2009-06-20 21:30:44 +00:00
|
|
|
if len(buffer) < blksize:
|
2009-08-16 23:44:57 +00:00
|
|
|
log.info("Reached EOF on file %s"
|
|
|
|
% self.context.file_to_transfer)
|
2009-06-20 21:30:44 +00:00
|
|
|
finished = True
|
2009-08-19 02:27:18 +00:00
|
|
|
dat = TftpPacketDAT()
|
|
|
|
dat.data = buffer
|
|
|
|
dat.blocknumber = blocknumber
|
|
|
|
self.context.metrics.bytes += len(dat.data)
|
|
|
|
log.debug("Sending DAT packet %d" % dat.blocknumber)
|
2009-06-20 21:30:44 +00:00
|
|
|
self.context.sock.sendto(dat.encode().buffer,
|
|
|
|
(self.context.host, self.context.port))
|
|
|
|
if self.context.packethook:
|
|
|
|
self.context.packethook(dat)
|
2009-08-19 02:27:18 +00:00
|
|
|
self.context.last_dat_pkt = dat
|
2009-06-20 21:30:44 +00:00
|
|
|
return finished
|
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
def sendACK(self, blocknumber=None):
|
|
|
|
"""This method sends an ack packet to the block number specified. If
|
|
|
|
none is specified, it defaults to the next_block property in the
|
|
|
|
parent context."""
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("In sendACK, blocknumber is %s" % blocknumber)
|
2009-08-16 23:44:57 +00:00
|
|
|
if blocknumber is None:
|
2009-08-16 02:36:58 +00:00
|
|
|
blocknumber = self.context.next_block
|
2009-09-24 19:32:37 +00:00
|
|
|
log.info("Sending ack to block %d" % blocknumber)
|
2009-08-16 02:36:58 +00:00
|
|
|
ackpkt = TftpPacketACK()
|
|
|
|
ackpkt.blocknumber = blocknumber
|
|
|
|
self.context.sock.sendto(ackpkt.encode().buffer,
|
|
|
|
(self.context.host,
|
|
|
|
self.context.tidport))
|
|
|
|
|
|
|
|
def sendError(self, errorcode):
|
|
|
|
"""This method uses the socket passed, and uses the errorcode to
|
|
|
|
compose and send an error packet."""
|
|
|
|
log.debug("In sendError, being asked to send error %d" % errorcode)
|
|
|
|
errpkt = TftpPacketERR()
|
|
|
|
errpkt.errorcode = errorcode
|
|
|
|
self.context.sock.sendto(errpkt.encode().buffer,
|
|
|
|
(self.context.host,
|
|
|
|
self.context.tidport))
|
|
|
|
|
|
|
|
def sendOACK(self):
|
|
|
|
"""This method sends an OACK packet with the options from the current
|
|
|
|
context."""
|
2009-08-16 23:44:57 +00:00
|
|
|
log.debug("In sendOACK with options %s" % self.context.options)
|
2009-08-16 02:36:58 +00:00
|
|
|
pkt = TftpPacketOACK()
|
2009-08-16 23:44:57 +00:00
|
|
|
pkt.options = self.context.options
|
2009-08-16 02:36:58 +00:00
|
|
|
self.context.sock.sendto(pkt.encode().buffer,
|
|
|
|
(self.context.host,
|
|
|
|
self.context.tidport))
|
|
|
|
|
2009-04-09 03:29:43 +00:00
|
|
|
def handleDat(self, pkt):
|
2009-08-16 02:36:58 +00:00
|
|
|
"""This method handles a DAT packet during a client download, or a
|
|
|
|
server upload."""
|
2009-09-24 19:32:37 +00:00
|
|
|
log.info("Handling DAT packet - block %d" % pkt.blocknumber)
|
|
|
|
log.debug("Expecting block %s" % self.context.next_block)
|
2009-06-20 21:30:44 +00:00
|
|
|
if pkt.blocknumber == self.context.next_block:
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Good, received block %d in sequence"
|
2009-04-09 03:29:43 +00:00
|
|
|
% pkt.blocknumber)
|
2009-06-20 21:30:44 +00:00
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
self.sendACK()
|
2009-06-20 21:30:44 +00:00
|
|
|
self.context.next_block += 1
|
2009-04-09 03:29:43 +00:00
|
|
|
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Writing %d bytes to output file"
|
2009-04-09 03:29:43 +00:00
|
|
|
% len(pkt.data))
|
|
|
|
self.context.fileobj.write(pkt.data)
|
|
|
|
self.context.metrics.bytes += len(pkt.data)
|
|
|
|
# Check for end-of-file, any less than full data packet.
|
|
|
|
if len(pkt.data) < int(self.context.options['blksize']):
|
2009-09-24 19:32:37 +00:00
|
|
|
log.info("End of file detected")
|
2009-04-09 03:29:43 +00:00
|
|
|
return None
|
|
|
|
|
2009-06-20 21:30:44 +00:00
|
|
|
elif pkt.blocknumber < self.context.next_block:
|
2009-08-16 23:44:57 +00:00
|
|
|
if pkt.blocknumber == 0:
|
|
|
|
log.warn("There is no block zero!")
|
|
|
|
self.sendError(TftpErrors.IllegalTftpOp)
|
|
|
|
raise TftpException, "There is no block zero!"
|
2009-09-24 19:32:37 +00:00
|
|
|
log.warn("Dropping duplicate block %d" % pkt.blocknumber)
|
2009-08-16 02:36:58 +00:00
|
|
|
self.context.metrics.add_dup(pkt.blocknumber)
|
|
|
|
log.debug("ACKing block %d again, just in case" % pkt.blocknumber)
|
|
|
|
self.sendACK(pkt.blocknumber)
|
2009-04-09 03:29:43 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
# FIXME: should we be more tolerant and just discard instead?
|
2009-04-11 00:54:20 +00:00
|
|
|
msg = "Whoa! Received future block %d but expected %d" \
|
2009-06-20 21:30:44 +00:00
|
|
|
% (pkt.blocknumber, self.context.next_block)
|
2009-08-16 02:36:58 +00:00
|
|
|
log.error(msg)
|
2009-04-09 03:29:43 +00:00
|
|
|
raise TftpException, msg
|
|
|
|
|
|
|
|
# Default is to ack
|
2009-08-16 02:36:58 +00:00
|
|
|
return TftpStateExpectDAT(self.context)
|
|
|
|
|
|
|
|
class TftpStateServerRecvRRQ(TftpState):
|
|
|
|
"""This class represents the state of the TFTP server when it has just
|
|
|
|
received an RRQ packet."""
|
|
|
|
def handle(self, pkt, raddress, rport):
|
|
|
|
"Handle an initial RRQ packet as a server."
|
|
|
|
log.debug("In TftpStateServerRecvRRQ.handle")
|
|
|
|
sendoack = self.serverInitial(pkt, raddress, rport)
|
|
|
|
path = self.context.root + os.sep + self.context.file_to_transfer
|
|
|
|
log.info("Opening file %s for reading" % path)
|
|
|
|
if os.path.exists(path):
|
|
|
|
# Note: Open in binary mode for win32 portability, since win32
|
|
|
|
# blows.
|
|
|
|
self.context.fileobj = open(path, "rb")
|
|
|
|
elif self.dyn_file_func:
|
|
|
|
log.debug("No such file %s but using dyn_file_func" % path)
|
|
|
|
self.context.fileobj = \
|
|
|
|
self.dyn_file_func(self.context.file_to_transfer)
|
|
|
|
else:
|
|
|
|
send.sendError(TftpErrors.FileNotFound)
|
|
|
|
raise TftpException, "File not found: %s" % path
|
|
|
|
|
|
|
|
# Options negotiation.
|
|
|
|
if sendoack:
|
2009-08-16 23:44:57 +00:00
|
|
|
# Note, next_block is 0 here since that's the proper
|
|
|
|
# acknowledgement to an OACK.
|
|
|
|
# FIXME: perhaps we do need a TftpStateExpectOACK class...
|
2009-08-16 02:36:58 +00:00
|
|
|
self.sendOACK()
|
|
|
|
else:
|
2009-08-16 23:44:57 +00:00
|
|
|
self.context.next_block = 1
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("No requested options, starting send...")
|
|
|
|
self.context.pending_complete = self.sendDAT()
|
2009-08-16 23:44:57 +00:00
|
|
|
# Note, we expect an ack regardless of whether we sent a DAT or an
|
|
|
|
# OACK.
|
|
|
|
return TftpStateExpectACK(self.context)
|
2009-08-16 02:36:58 +00:00
|
|
|
|
|
|
|
# Note, we don't have to check any other states in this method, that's
|
|
|
|
# up to the caller.
|
|
|
|
|
|
|
|
class TftpStateServerRecvWRQ(TftpState):
|
|
|
|
"""This class represents the state of the TFTP server when it has just
|
|
|
|
received a WRQ packet."""
|
|
|
|
def handle(self, pkt, raddress, rport):
|
|
|
|
"Handle an initial WRQ packet as a server."
|
|
|
|
log.debug("In TftpStateServerRecvWRQ.handle")
|
|
|
|
sendoack = self.serverInitial(pkt, raddress, rport)
|
|
|
|
path = self.context.root + os.sep + self.context.file_to_transfer
|
|
|
|
log.info("Opening file %s for writing" % path)
|
|
|
|
if os.path.exists(path):
|
|
|
|
# FIXME: correct behavior?
|
|
|
|
log.warn("File %s exists already, overwriting...")
|
|
|
|
# FIXME: I think we should upload to a temp file and not overwrite the
|
|
|
|
# existing file until the file is successfully uploaded.
|
|
|
|
self.context.fileobj = open(path, "wb")
|
|
|
|
|
|
|
|
# Options negotiation.
|
|
|
|
if sendoack:
|
|
|
|
log.debug("Sending OACK to client")
|
|
|
|
self.sendOACK()
|
|
|
|
else:
|
|
|
|
log.debug("No requested options, starting send...")
|
|
|
|
self.sendACK()
|
|
|
|
# We may have sent an OACK, but we're expecting a DAT as the response
|
|
|
|
# to either the OACK or an ACK, so lets unconditionally use the
|
|
|
|
# TftpStateExpectDAT state.
|
|
|
|
return TftpStateExpectDAT(self.context)
|
|
|
|
|
|
|
|
# Note, we don't have to check any other states in this method, that's
|
|
|
|
# up to the caller.
|
|
|
|
|
|
|
|
class TftpStateServerStart(TftpState):
|
|
|
|
"""The start state for the server."""
|
|
|
|
def handle(self, pkt, raddress, rport):
|
|
|
|
"""Handle a packet we just received."""
|
|
|
|
log.debug("In TftpStateServerStart.handle")
|
|
|
|
if isinstance(pkt, TftpPacketRRQ):
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Handling an RRQ packet")
|
2009-08-16 02:36:58 +00:00
|
|
|
return TftpStateServerRecvRRQ(self.context).handle(pkt,
|
|
|
|
raddress,
|
|
|
|
rport)
|
|
|
|
elif isinstance(pkt, TftpPacketWRQ):
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Handling a WRQ packet")
|
2009-08-16 02:36:58 +00:00
|
|
|
return TftpStateServerRecvWRQ(self.context).handle(pkt,
|
|
|
|
raddress,
|
|
|
|
rport)
|
|
|
|
else:
|
|
|
|
self.sendError(TftpErrors.IllegalTftpOp)
|
|
|
|
raise TftpException, \
|
|
|
|
"Invalid packet to begin up/download: %s" % pkt
|
|
|
|
|
|
|
|
class TftpStateExpectACK(TftpState):
|
|
|
|
"""This class represents the state of the transfer when a DAT was just
|
|
|
|
sent, and we are waiting for an ACK from the server. This class is the
|
|
|
|
same one used by the client during the upload, and the server during the
|
|
|
|
download."""
|
|
|
|
def handle(self, pkt, raddress, rport):
|
|
|
|
"Handle a packet, hopefully an ACK since we just sent a DAT."
|
|
|
|
if isinstance(pkt, TftpPacketACK):
|
|
|
|
log.info("Received ACK for packet %d" % pkt.blocknumber)
|
|
|
|
# Is this an ack to the one we just sent?
|
|
|
|
if self.context.next_block == pkt.blocknumber:
|
|
|
|
if self.context.pending_complete:
|
|
|
|
log.info("Received ACK to final DAT, we're done.")
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
log.debug("Good ACK, sending next DAT")
|
2009-08-16 23:44:57 +00:00
|
|
|
self.context.next_block += 1
|
|
|
|
log.debug("Incremented next_block to %d"
|
|
|
|
% (self.context.next_block))
|
2009-08-16 02:36:58 +00:00
|
|
|
self.context.pending_complete = self.sendDAT()
|
2009-04-09 03:29:43 +00:00
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
elif pkt.blocknumber < self.context.next_block:
|
|
|
|
self.context.metrics.add_dup(pkt.blocknumber)
|
|
|
|
|
|
|
|
else:
|
|
|
|
log.warn("Oooh, time warp. Received ACK to packet we "
|
|
|
|
"didn't send yet. Discarding.")
|
|
|
|
self.context.metrics.errors += 1
|
|
|
|
return self
|
|
|
|
elif isinstance(pkt, TftpPacketERR):
|
|
|
|
log.error("Received ERR packet from peer: %s" % str(pkt))
|
|
|
|
raise TftpException, \
|
|
|
|
"Received ERR packet from peer: %s" % str(pkt)
|
|
|
|
else:
|
|
|
|
log.warn("Discarding unsupported packet: %s" % str(pkt))
|
|
|
|
return self
|
|
|
|
|
|
|
|
class TftpStateExpectDAT(TftpState):
|
|
|
|
"""Just sent an ACK packet. Waiting for DAT."""
|
|
|
|
def handle(self, pkt, raddress, rport):
|
|
|
|
"""Handle the packet in response to an ACK, which should be a DAT."""
|
|
|
|
if isinstance(pkt, TftpPacketDAT):
|
|
|
|
return self.handleDat(pkt)
|
|
|
|
|
|
|
|
# Every other packet type is a problem.
|
2009-09-24 19:32:37 +00:00
|
|
|
elif isinstance(pkt, TftpPacketACK):
|
2009-08-16 02:36:58 +00:00
|
|
|
# Umm, we ACK, you don't.
|
|
|
|
self.sendError(TftpErrors.IllegalTftpOp)
|
|
|
|
raise TftpException, "Received ACK from peer when expecting DAT"
|
|
|
|
|
2009-09-24 19:32:37 +00:00
|
|
|
elif isinstance(pkt, TftpPacketWRQ):
|
2009-08-16 02:36:58 +00:00
|
|
|
self.sendError(TftpErrors.IllegalTftpOp)
|
|
|
|
raise TftpException, "Received WRQ from peer when expecting DAT"
|
|
|
|
|
2009-09-24 19:32:37 +00:00
|
|
|
elif isinstance(pkt, TftpPacketERR):
|
2009-08-16 02:36:58 +00:00
|
|
|
self.sendError(TftpErrors.IllegalTftpOp)
|
2009-09-24 19:32:37 +00:00
|
|
|
raise TftpException, "Received ERR from peer: " + str(pkt)
|
2009-08-16 02:36:58 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
self.sendError(TftpErrors.IllegalTftpOp)
|
2009-09-24 19:32:37 +00:00
|
|
|
raise TftpException, "Received unknown packet type from peer: " + str(pkt)
|
2009-08-16 02:36:58 +00:00
|
|
|
|
|
|
|
class TftpStateSentWRQ(TftpState):
|
2009-06-20 21:30:44 +00:00
|
|
|
"""Just sent an WRQ packet for an upload."""
|
|
|
|
def handle(self, pkt, raddress, rport):
|
|
|
|
"""Handle a packet we just received."""
|
|
|
|
if not self.context.tidport:
|
|
|
|
self.context.tidport = rport
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("Set remote port for session to %s" % rport)
|
2009-06-20 21:30:44 +00:00
|
|
|
|
|
|
|
# If we're going to successfully transfer the file, then we should see
|
|
|
|
# either an OACK for accepted options, or an ACK to ignore options.
|
|
|
|
if isinstance(pkt, TftpPacketOACK):
|
2009-09-24 19:32:37 +00:00
|
|
|
log.info("Received OACK from server")
|
2009-06-20 21:30:44 +00:00
|
|
|
try:
|
|
|
|
self.handleOACK(pkt)
|
|
|
|
except TftpException, err:
|
2009-09-24 19:32:37 +00:00
|
|
|
log.error("Failed to negotiate options")
|
2009-08-16 02:36:58 +00:00
|
|
|
self.sendError(TftpErrors.FailedNegotiation)
|
2009-06-20 21:30:44 +00:00
|
|
|
raise
|
|
|
|
else:
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Sending first DAT packet")
|
2009-08-16 02:36:58 +00:00
|
|
|
self.context.pending_complete = self.sendDAT()
|
|
|
|
log.debug("Changing state to TftpStateExpectACK")
|
|
|
|
return TftpStateExpectACK(self.context)
|
2009-06-20 21:30:44 +00:00
|
|
|
|
|
|
|
elif isinstance(pkt, TftpPacketACK):
|
2009-09-24 19:32:37 +00:00
|
|
|
log.info("Received ACK from server")
|
|
|
|
log.debug("Apparently the server ignored our options")
|
2009-06-20 21:30:44 +00:00
|
|
|
# The block number should be zero.
|
|
|
|
if pkt.blocknumber == 0:
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Ack blocknumber is zero as expected")
|
|
|
|
log.debug("Sending first DAT packet")
|
2009-08-16 02:36:58 +00:00
|
|
|
self.pending_complete = self.context.sendDAT()
|
|
|
|
log.debug("Changing state to TftpStateExpectACK")
|
|
|
|
return TftpStateExpectACK(self.context)
|
2009-06-20 21:30:44 +00:00
|
|
|
else:
|
2009-09-24 19:32:37 +00:00
|
|
|
log.warn("Discarding ACK to block %s" % pkt.blocknumber)
|
|
|
|
log.debug("Still waiting for valid response from server")
|
2009-06-20 21:30:44 +00:00
|
|
|
return self
|
|
|
|
|
|
|
|
elif isinstance(pkt, TftpPacketERR):
|
2009-08-16 02:36:58 +00:00
|
|
|
self.sendError(TftpErrors.IllegalTftpOp)
|
2009-06-20 21:30:44 +00:00
|
|
|
raise TftpException, "Received ERR from server: " + str(pkt)
|
|
|
|
|
|
|
|
elif isinstance(pkt, TftpPacketRRQ):
|
2009-08-16 02:36:58 +00:00
|
|
|
self.sendError(TftpErrors.IllegalTftpOp)
|
2009-06-20 21:30:44 +00:00
|
|
|
raise TftpException, "Received RRQ from server while in upload"
|
|
|
|
|
|
|
|
elif isinstance(pkt, TftpPacketDAT):
|
2009-08-16 02:36:58 +00:00
|
|
|
self.sendError(TftpErrors.IllegalTftpOp)
|
2009-06-20 21:30:44 +00:00
|
|
|
raise TftpException, "Received DAT from server while in upload"
|
|
|
|
|
|
|
|
else:
|
2009-08-16 02:36:58 +00:00
|
|
|
self.sendError(TftpErrors.IllegalTftpOp)
|
2009-06-20 21:30:44 +00:00
|
|
|
raise TftpException, "Received unknown packet type from server: " + str(pkt)
|
|
|
|
|
|
|
|
# By default, no state change.
|
|
|
|
return self
|
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
class TftpStateSentRRQ(TftpState):
|
2009-04-09 03:29:43 +00:00
|
|
|
"""Just sent an RRQ packet."""
|
|
|
|
def handle(self, pkt, raddress, rport):
|
|
|
|
"""Handle the packet in response to an RRQ to the server."""
|
2009-04-11 00:54:20 +00:00
|
|
|
if not self.context.tidport:
|
|
|
|
self.context.tidport = rport
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("Set remote port for session to %s" % rport)
|
2009-04-09 03:29:43 +00:00
|
|
|
|
|
|
|
# Now check the packet type and dispatch it properly.
|
|
|
|
if isinstance(pkt, TftpPacketOACK):
|
2009-09-24 19:32:37 +00:00
|
|
|
log.info("Received OACK from server")
|
2009-06-20 21:30:44 +00:00
|
|
|
try:
|
|
|
|
self.handleOACK(pkt)
|
|
|
|
except TftpException, err:
|
2009-09-24 19:32:37 +00:00
|
|
|
log.error("Failed to negotiate options: %s" % str(err))
|
2009-08-16 02:36:58 +00:00
|
|
|
self.sendError(TftpErrors.FailedNegotiation)
|
2009-06-20 21:30:44 +00:00
|
|
|
raise
|
|
|
|
else:
|
2009-09-24 19:32:37 +00:00
|
|
|
log.debug("Sending ACK to OACK")
|
2009-06-20 21:30:44 +00:00
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
self.sendACK(blocknumber=0)
|
2009-06-20 21:30:44 +00:00
|
|
|
|
2009-08-16 02:36:58 +00:00
|
|
|
log.debug("Changing state to TftpStateExpectDAT")
|
|
|
|
return TftpStateExpectDAT(self.context)
|
2009-04-09 03:29:43 +00:00
|
|
|
|
|
|
|
elif isinstance(pkt, TftpPacketDAT):
|
2009-04-11 02:25:45 +00:00
|
|
|
# If there are any options set, then the server didn't honour any
|
|
|
|
# of them.
|
2009-09-24 19:32:37 +00:00
|
|
|
log.info("Received DAT from server")
|
2009-04-11 02:25:45 +00:00
|
|
|
if self.context.options:
|
2009-09-24 19:32:37 +00:00
|
|
|
log.info("Server ignored options, falling back to defaults")
|
2009-04-11 02:25:45 +00:00
|
|
|
self.context.options = { 'blksize': DEF_BLKSIZE }
|
2009-04-09 03:29:43 +00:00
|
|
|
return self.handleDat(pkt)
|
|
|
|
|
|
|
|
# Every other packet type is a problem.
|
2009-09-24 19:32:37 +00:00
|
|
|
elif isinstance(pkt, TftpPacketACK):
|
2009-04-09 03:29:43 +00:00
|
|
|
# Umm, we ACK, the server doesn't.
|
2009-08-16 02:36:58 +00:00
|
|
|
self.sendError(TftpErrors.IllegalTftpOp)
|
2009-04-09 03:29:43 +00:00
|
|
|
raise TftpException, "Received ACK from server while in download"
|
|
|
|
|
2009-09-24 19:32:37 +00:00
|
|
|
elif isinstance(pkt, TftpPacketWRQ):
|
2009-08-16 02:36:58 +00:00
|
|
|
self.sendError(TftpErrors.IllegalTftpOp)
|
2009-04-09 03:29:43 +00:00
|
|
|
raise TftpException, "Received WRQ from server while in download"
|
|
|
|
|
2009-09-24 19:32:37 +00:00
|
|
|
elif isinstance(pkt, TftpPacketERR):
|
2009-08-16 02:36:58 +00:00
|
|
|
self.sendError(TftpErrors.IllegalTftpOp)
|
2009-09-24 19:32:37 +00:00
|
|
|
raise TftpException, "Received ERR from server: " + str(pkt)
|
2009-04-09 03:29:43 +00:00
|
|
|
|
|
|
|
else:
|
2009-08-16 02:36:58 +00:00
|
|
|
self.sendError(TftpErrors.IllegalTftpOp)
|
2009-09-24 19:32:37 +00:00
|
|
|
raise TftpException, "Received unknown packet type from server: " + str(pkt)
|
2009-04-09 03:29:43 +00:00
|
|
|
|
|
|
|
# By default, no state change.
|
|
|
|
return self
|