@@ -84,14 +84,15 @@ def add_dup(self, pkt):
8484class TftpContext (object ):
8585 """The base class of the contexts."""
8686
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 ):
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
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 )
9596 for n in ports or [0 ]:
9697 try :
9798 if localip != "" :
@@ -110,6 +111,7 @@ def __init__(self, host, port, timeout, retries=DEF_TIMEOUT_RETRIES, localip="",
110111 self .next_block = 0
111112 self .factory = TftpPacketFactory ()
112113 # Note, setting the host will also set self.address, as it's a property.
114+ self .address = ""
113115 self .host = host
114116 self .port = port
115117 # The port associated with the TID
@@ -170,7 +172,12 @@ def sethost(self, host):
170172 of the host that is set.
171173 """
172174 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" )
174181
175182 host = property (gethost , sethost )
176183
@@ -191,7 +198,9 @@ def cycle(self):
191198 something, and dispatch appropriate action to that response.
192199 """
193200 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 ]
195204 except socket .timeout :
196205 log .warning ("Timeout waiting for traffic, retrying..." )
197206 raise TftpTimeout ("Timed-out waiting for traffic" )
@@ -247,9 +256,10 @@ def __init__(
247256 dyn_file_func = None ,
248257 upload_open = None ,
249258 retries = DEF_TIMEOUT_RETRIES ,
259+ af_family = socket .AF_INET ,
250260 ports = None ,
251261 ):
252- TftpContext .__init__ (self , host , port , timeout , retries , ports = ports )
262+ TftpContext .__init__ (self , host , port , timeout , retries , af_family = af_family , ports = ports )
253263 # At this point we have no idea if this is a download or an upload. We
254264 # need to let the start state determine that.
255265 self .state = TftpStateServerStart (self )
@@ -305,9 +315,10 @@ def __init__(
305315 timeout ,
306316 retries = DEF_TIMEOUT_RETRIES ,
307317 localip = "" ,
318+ af_family = socket .AF_INET ,
308319 ports = None ,
309320 ):
310- TftpContext .__init__ (self , host , port , timeout , retries , localip , ports )
321+ TftpContext .__init__ (self , host , port , timeout , retries , localip , af_family , ports )
311322 self .file_to_transfer = filename
312323 self .options = options
313324 self .packethook = packethook
0 commit comments