Skip to content

Commit

Permalink
allow portrange as alternative to ephemeral
Browse files Browse the repository at this point in the history
  • Loading branch information
9001 committed Feb 10, 2024
1 parent ad119ad commit b8844c0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
4 changes: 4 additions & 0 deletions partftpy/TftpClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def download(
packethook=None,
timeout=SOCK_TIMEOUT,
retries=DEF_TIMEOUT_RETRIES,
ports=None,
):
"""This method initiates a tftp download from the configured remote
host, requesting the filename passed. It writes the file to output,
Expand Down Expand Up @@ -70,6 +71,7 @@ def download(
timeout,
retries=retries,
localip=self.localip,
ports=ports
)
self.context.start()
# Download happens here
Expand Down Expand Up @@ -97,6 +99,7 @@ def upload(
packethook=None,
timeout=SOCK_TIMEOUT,
retries=DEF_TIMEOUT_RETRIES,
ports=None,
):
"""This method initiates a tftp upload to the configured remote host,
uploading the filename passed. It reads the file from input, which
Expand All @@ -121,6 +124,7 @@ def upload(
timeout,
retries=retries,
localip=self.localip,
ports=ports,
)
self.context.start()
# Upload happens here
Expand Down
24 changes: 18 additions & 6 deletions partftpy/TftpContexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,25 @@ def add_dup(self, pkt):
class TftpContext(object):
"""The base class of the contexts."""

def __init__(self, host, port, timeout, retries=DEF_TIMEOUT_RETRIES, localip=""):
def __init__(self, host, port, timeout, retries=DEF_TIMEOUT_RETRIES, localip="", ports=None):
"""Constructor for the base context, setting shared instance
variables."""
self.file_to_transfer = None
self.fileobj = None
self.options = None
self.packethook = None
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
if localip != "":
self.sock.bind((localip, 0))
for n in ports or [0]:
try:
if localip != "":
self.sock.bind((localip, 0))
else:
self.sock.bind(("0.0.0.0", n))
break
except:
continue
log.info("will reply from %s:", self.sock.getsockname())

self.sock.settimeout(timeout)
self.timeout = timeout
self.retries = retries
Expand Down Expand Up @@ -239,8 +248,9 @@ def __init__(
dyn_file_func=None,
upload_open=None,
retries=DEF_TIMEOUT_RETRIES,
ports=None,
):
TftpContext.__init__(self, host, port, timeout, retries)
TftpContext.__init__(self, host, port, timeout, retries, ports=ports)
# At this point we have no idea if this is a download or an upload. We
# need to let the start state determine that.
self.state = TftpStateServerStart(self)
Expand Down Expand Up @@ -296,8 +306,9 @@ def __init__(
timeout,
retries=DEF_TIMEOUT_RETRIES,
localip="",
ports=None,
):
TftpContext.__init__(self, host, port, timeout, retries, localip)
TftpContext.__init__(self, host, port, timeout, retries, localip, ports)
self.file_to_transfer = filename
self.options = options
self.packethook = packethook
Expand Down Expand Up @@ -377,8 +388,9 @@ def __init__(
timeout,
retries=DEF_TIMEOUT_RETRIES,
localip="",
ports=None,
):
TftpContext.__init__(self, host, port, timeout, retries, localip)
TftpContext.__init__(self, host, port, timeout, retries, localip, ports)
# FIXME: should we refactor setting of these params?
self.file_to_transfer = filename
self.options = options
Expand Down
2 changes: 2 additions & 0 deletions partftpy/TftpServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def listen(
listenport=DEF_TFTP_PORT,
timeout=SOCK_TIMEOUT,
retries=DEF_TIMEOUT_RETRIES,
ports=None,
):
"""Start a server listening on the supplied interface and port. This
defaults to INADDR_ANY (all interfaces) and UDP port 69. You can also
Expand Down Expand Up @@ -177,6 +178,7 @@ def listen(
self.dyn_file_func,
self.upload_open,
retries=retries,
ports=ports,
)
try:
self.sessions[key].start(buffer)
Expand Down

0 comments on commit b8844c0

Please sign in to comment.