Commits

Dave St.Germain committed 9f2fff6

failure handling for when ipv6 isn't available

Comments (0)

Files changed (4)

     def _send(self, data):
         asyncore.dispatcher_with_send.send(self, data)
 
-    def _do_proxy(self, peer):
+    def _do_proxy(self, peer, to_address):
         self.peer = peer
         self.bufsize = 4096
     

blocking_socks.py

         self.wfile.write(data)
         
     
-    def _do_proxy(self, peer):
+    def _do_proxy(self, peer, to_address):
         if hasattr(self, 'kqueue_proxy'):
             self.kqueue_proxy(self.request, peer)
         else:
             self.select_proxy(self.request, peer)
-        logging.info('disconnected from %s:%s' % self.client_address)
+        logging.info('disconnected from %s:%s' % to_address)
 
     
 class SocksServer(SocketServer.ThreadingTCPServer):
         """
         raise NotImplementedError
 
-    def _do_proxy(self, peer):
+    def _do_proxy(self, peer, to_address):
         """
         override this to call select_proxy or kqueue_proxy or something else
         """
         if peer:            
             self.respond(self.GRANTED5 if self.version == 5 else self.GRANTED, to_address)
             self.server.total_connections += 1
-            self._do_proxy(peer)
+            self._do_proxy(peer, to_address)
         else:
             self.reject()
             
 class CompressibleSSHClient(SSHClient):
     def connect(self, hostname, port=22, username=None, password=None, pkey=None,
                 key_filename=None, timeout=None, allow_agent=True, look_for_keys=True):
-        for (family, socktype, proto, canonname, sockaddr) in socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
-            if socktype == socket.SOCK_STREAM:
-                af = family
-                addr = sockaddr
+        addrs = [(family, sockaddr) for (family, socktype, proto, canonname, sockaddr) in socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM) if socktype == socket.SOCK_STREAM]
+        if not addrs:
+            raise SSHException('No suitable address family for %s' % hostname)
+        for af, addr in addrs:
+            sock = socket.socket(af, socket.SOCK_STREAM)
+            if timeout is not None:
+                try:
+                    sock.settimeout(timeout)
+                except:
+                    pass
+            try:
+                sock.connect(addr)
+            except socket.error:
+                continue
+            else:
                 break
         else:
-            raise SSHException('No suitable address family for %s' % hostname)
-        sock = socket.socket(af, socket.SOCK_STREAM)
-        if timeout is not None:
-            try:
-                sock.settimeout(timeout)
-            except:
-                pass
-        sock.connect(addr)
+            raise SSHException("No suitable addresses: %s" % addrs)
         t = self._transport = Transport(sock)
         
         #####