@@ -84,16 +84,25 @@ def add_dup(self, pkt):
84
84
class TftpContext (object ):
85
85
"""The base class of the contexts."""
86
86
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 ):
88
88
"""Constructor for the base context, setting shared instance
89
89
variables."""
90
90
self .file_to_transfer = None
91
91
self .fileobj = None
92
92
self .options = None
93
93
self .packethook = None
94
94
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
+
97
106
self .sock .settimeout (timeout )
98
107
self .timeout = timeout
99
108
self .retries = retries
@@ -239,8 +248,9 @@ def __init__(
239
248
dyn_file_func = None ,
240
249
upload_open = None ,
241
250
retries = DEF_TIMEOUT_RETRIES ,
251
+ ports = None ,
242
252
):
243
- TftpContext .__init__ (self , host , port , timeout , retries )
253
+ TftpContext .__init__ (self , host , port , timeout , retries , ports = ports )
244
254
# At this point we have no idea if this is a download or an upload. We
245
255
# need to let the start state determine that.
246
256
self .state = TftpStateServerStart (self )
@@ -296,8 +306,9 @@ def __init__(
296
306
timeout ,
297
307
retries = DEF_TIMEOUT_RETRIES ,
298
308
localip = "" ,
309
+ ports = None ,
299
310
):
300
- TftpContext .__init__ (self , host , port , timeout , retries , localip )
311
+ TftpContext .__init__ (self , host , port , timeout , retries , localip , ports )
301
312
self .file_to_transfer = filename
302
313
self .options = options
303
314
self .packethook = packethook
@@ -377,8 +388,9 @@ def __init__(
377
388
timeout ,
378
389
retries = DEF_TIMEOUT_RETRIES ,
379
390
localip = "" ,
391
+ ports = None ,
380
392
):
381
- TftpContext .__init__ (self , host , port , timeout , retries , localip )
393
+ TftpContext .__init__ (self , host , port , timeout , retries , localip , ports )
382
394
# FIXME: should we refactor setting of these params?
383
395
self .file_to_transfer = filename
384
396
self .options = options
0 commit comments