Skip to content

Commit b8844c0

Browse files
committed
allow portrange as alternative to ephemeral
1 parent ad119ad commit b8844c0

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

partftpy/TftpClient.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def download(
4343
packethook=None,
4444
timeout=SOCK_TIMEOUT,
4545
retries=DEF_TIMEOUT_RETRIES,
46+
ports=None,
4647
):
4748
"""This method initiates a tftp download from the configured remote
4849
host, requesting the filename passed. It writes the file to output,
@@ -70,6 +71,7 @@ def download(
7071
timeout,
7172
retries=retries,
7273
localip=self.localip,
74+
ports=ports
7375
)
7476
self.context.start()
7577
# Download happens here
@@ -97,6 +99,7 @@ def upload(
9799
packethook=None,
98100
timeout=SOCK_TIMEOUT,
99101
retries=DEF_TIMEOUT_RETRIES,
102+
ports=None,
100103
):
101104
"""This method initiates a tftp upload to the configured remote host,
102105
uploading the filename passed. It reads the file from input, which
@@ -121,6 +124,7 @@ def upload(
121124
timeout,
122125
retries=retries,
123126
localip=self.localip,
127+
ports=ports,
124128
)
125129
self.context.start()
126130
# Upload happens here

partftpy/TftpContexts.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,25 @@ def add_dup(self, pkt):
8484
class TftpContext(object):
8585
"""The base class of the contexts."""
8686

87-
def __init__(self, host, port, timeout, retries=DEF_TIMEOUT_RETRIES, localip=""):
87+
def __init__(self, host, port, timeout, retries=DEF_TIMEOUT_RETRIES, localip="", ports=None):
8888
"""Constructor for the base context, setting shared instance
8989
variables."""
9090
self.file_to_transfer = None
9191
self.fileobj = None
9292
self.options = None
9393
self.packethook = None
9494
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
95-
if localip != "":
96-
self.sock.bind((localip, 0))
95+
for n in ports or [0]:
96+
try:
97+
if localip != "":
98+
self.sock.bind((localip, 0))
99+
else:
100+
self.sock.bind(("0.0.0.0", n))
101+
break
102+
except:
103+
continue
104+
log.info("will reply from %s:", self.sock.getsockname())
105+
97106
self.sock.settimeout(timeout)
98107
self.timeout = timeout
99108
self.retries = retries
@@ -239,8 +248,9 @@ def __init__(
239248
dyn_file_func=None,
240249
upload_open=None,
241250
retries=DEF_TIMEOUT_RETRIES,
251+
ports=None,
242252
):
243-
TftpContext.__init__(self, host, port, timeout, retries)
253+
TftpContext.__init__(self, host, port, timeout, retries, ports=ports)
244254
# At this point we have no idea if this is a download or an upload. We
245255
# need to let the start state determine that.
246256
self.state = TftpStateServerStart(self)
@@ -296,8 +306,9 @@ def __init__(
296306
timeout,
297307
retries=DEF_TIMEOUT_RETRIES,
298308
localip="",
309+
ports=None,
299310
):
300-
TftpContext.__init__(self, host, port, timeout, retries, localip)
311+
TftpContext.__init__(self, host, port, timeout, retries, localip, ports)
301312
self.file_to_transfer = filename
302313
self.options = options
303314
self.packethook = packethook
@@ -377,8 +388,9 @@ def __init__(
377388
timeout,
378389
retries=DEF_TIMEOUT_RETRIES,
379390
localip="",
391+
ports=None,
380392
):
381-
TftpContext.__init__(self, host, port, timeout, retries, localip)
393+
TftpContext.__init__(self, host, port, timeout, retries, localip, ports)
382394
# FIXME: should we refactor setting of these params?
383395
self.file_to_transfer = filename
384396
self.options = options

partftpy/TftpServer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def listen(
8585
listenport=DEF_TFTP_PORT,
8686
timeout=SOCK_TIMEOUT,
8787
retries=DEF_TIMEOUT_RETRIES,
88+
ports=None,
8889
):
8990
"""Start a server listening on the supplied interface and port. This
9091
defaults to INADDR_ANY (all interfaces) and UDP port 69. You can also
@@ -177,6 +178,7 @@ def listen(
177178
self.dyn_file_func,
178179
self.upload_open,
179180
retries=retries,
181+
ports=ports,
180182
)
181183
try:
182184
self.sessions[key].start(buffer)

0 commit comments

Comments
 (0)