@@ -84,14 +84,15 @@ 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 = "" , ports = None ):
87
+ def __init__ (self , host , port , timeout , retries = DEF_TIMEOUT_RETRIES , localip = "" , af_family = socket . AF_INET , 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
- self .sock = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
94
+ self .af_family = af_family
95
+ self .sock = socket .socket (af_family , socket .SOCK_DGRAM )
95
96
for n in ports or [0 ]:
96
97
try :
97
98
if localip != "" :
@@ -110,6 +111,7 @@ def __init__(self, host, port, timeout, retries=DEF_TIMEOUT_RETRIES, localip="",
110
111
self .next_block = 0
111
112
self .factory = TftpPacketFactory ()
112
113
# Note, setting the host will also set self.address, as it's a property.
114
+ self .address = ""
113
115
self .host = host
114
116
self .port = port
115
117
# The port associated with the TID
@@ -170,7 +172,12 @@ def sethost(self, host):
170
172
of the host that is set.
171
173
"""
172
174
self .__host = host
173
- self .address = socket .gethostbyname (host )
175
+ if self .af_family == socket .AF_INET :
176
+ self .address = socket .gethostbyname (host )
177
+ elif self .af_family == socket .AF_INET6 :
178
+ self .address = socket .getaddrinfo (host , 0 )[0 ][4 ][0 ]
179
+ else :
180
+ raise ValueError ("af_family is not supported" )
174
181
175
182
host = property (gethost , sethost )
176
183
@@ -191,7 +198,9 @@ def cycle(self):
191
198
something, and dispatch appropriate action to that response.
192
199
"""
193
200
try :
194
- (buffer , (raddress , rport )) = self .sock .recvfrom (MAX_BLKSIZE )
201
+ buffer , rai = self .sock .recvfrom (MAX_BLKSIZE )
202
+ raddress = rai [0 ]
203
+ rport = rai [1 ]
195
204
except socket .timeout :
196
205
log .warning ("Timeout waiting for traffic, retrying..." )
197
206
raise TftpTimeout ("Timed-out waiting for traffic" )
@@ -247,9 +256,10 @@ def __init__(
247
256
dyn_file_func = None ,
248
257
upload_open = None ,
249
258
retries = DEF_TIMEOUT_RETRIES ,
259
+ af_family = socket .AF_INET ,
250
260
ports = None ,
251
261
):
252
- TftpContext .__init__ (self , host , port , timeout , retries , ports = ports )
262
+ TftpContext .__init__ (self , host , port , timeout , retries , af_family = af_family , ports = ports )
253
263
# At this point we have no idea if this is a download or an upload. We
254
264
# need to let the start state determine that.
255
265
self .state = TftpStateServerStart (self )
@@ -305,9 +315,10 @@ def __init__(
305
315
timeout ,
306
316
retries = DEF_TIMEOUT_RETRIES ,
307
317
localip = "" ,
318
+ af_family = socket .AF_INET ,
308
319
ports = None ,
309
320
):
310
- TftpContext .__init__ (self , host , port , timeout , retries , localip , ports )
321
+ TftpContext .__init__ (self , host , port , timeout , retries , localip , af_family , ports )
311
322
self .file_to_transfer = filename
312
323
self .options = options
313
324
self .packethook = packethook
0 commit comments