Member of The Internet Defense League Últimos cambios
Últimos Cambios
Blog personal: El hilo del laberinto Geocaching

Conexiones salientes con una IP de origen determinada

Última Actualización: 15 de marzo de 2008 - Sábado

BitTornado permite configurar las IPs de escucha para conexiones entrantes. Pero no permite hacer lo mismo con las conexiones salientes. Es decir, en una máquina con múltiples IPs, elegir una de ellas como origen de las conexiones P2P salientes.

Dado que en mis sistemas hago "Policy Routing", y uno de los criterios es la IP origen, es importante controlar este parámetro.

El parche es el siguiente:

Index: BitTornado/BT1/Encrypter.py
===================================================================
--- BitTornado/BT1/Encrypter.py (revision 7)
+++ BitTornado/BT1/Encrypter.py (working copy)
@@ -272,7 +272,7 @@
             if self.config['security'] and ip != 'unknown' and ip == dns[0]:
                 return True
         try:
-            c = self.raw_server.start_connection(dns)
+            c = self.raw_server.start_connection(dns,own_ip=self.config['bind'])
             con = Connection(self, c, id)
             self.connections[c] = con
             c.set_handler(con)
Index: BitTornado/SocketHandler.py
===================================================================
--- BitTornado/SocketHandler.py (revision 5)
+++ BitTornado/SocketHandler.py (working copy)
@@ -148,7 +148,7 @@
         addrinfos = []
         self.servers = {}
         self.interfaces = []
-        # if bind != "" thread it as a comma seperated list and bind to all
+        # if bind != "" treat it as a comma seperated list and bind to all
         # addresses (can be ips or hostnames) else bind to default ipv6 and
         # ipv4 address
         if bind:
@@ -232,11 +232,13 @@
         self.handler = handler


-    def start_connection_raw(self, dns, socktype = socket.AF_INET, handler = None):
+    def start_connection_raw(self, dns, socktype = socket.AF_INET, handler = None, own_ip=""):
         if handler is None:
             handler = self.handler
         sock = socket.socket(socktype, socket.SOCK_STREAM)
         sock.setblocking(0)
+        if own_ip :
+          sock.bind((own_ip.split(",")[0].strip(),0))
         try:
             sock.connect_ex(dns)
         except socket.error:
@@ -249,11 +251,11 @@
         return s


-    def start_connection(self, dns, handler = None, randomize = False):
+    def start_connection(self, dns, handler = None, randomize = False, own_ip=""):
         if handler is None:
             handler = self.handler
         if sys.version_info < (2,2):
-            s = self.start_connection_raw(dns,socket.AF_INET,handler)
+            s = self.start_connection_raw(dns,socket.AF_INET,handler,own_ip=own_ip)
         else:
             if self.ipv6_enable:
                 socktype = socket.AF_UNSPEC
@@ -270,7 +272,7 @@
                 shuffle(addrinfos)
             for addrinfo in addrinfos:
                 try:
-                    s = self.start_connection_raw(addrinfo[4],addrinfo[0],handler)
+                    s = self.start_connection_raw(addrinfo[4],addrinfo[0],handler,own_ip=own_ip)
                     break
                 except:
                     pass
Index: BitTornado/ServerPortHandler.py
===================================================================
--- BitTornado/ServerPortHandler.py     (revision 5)
+++ BitTornado/ServerPortHandler.py     (working copy)
@@ -54,10 +54,10 @@
 #    def bind(self, port, bind = '', reuse = False):
 #        pass    # not handled here

-    def start_connection(self, dns, handler = None):
+    def start_connection(self, dns, handler = None, own_ip=""):
         if not handler:
             handler = self.handler
-        c = self.rawserver.start_connection(dns, handler)
+        c = self.rawserver.start_connection(dns, handler, own_ip=own_ip)
         return c

 #    def listen_forever(self, handler):
Index: BitTornado/RawServer.py
===================================================================
--- BitTornado/RawServer.py     (revision 5)
+++ BitTornado/RawServer.py     (working copy)
@@ -91,11 +91,11 @@
         return self.sockethandler.find_and_bind(minport, maxport, bind, reuse,
                                  ipv6_socket_style, upnp, randomizer)

-    def start_connection_raw(self, dns, socktype, handler = None):
-        return self.sockethandler.start_connection_raw(dns, socktype, handler)
+    def start_connection_raw(self, dns, socktype, handler = None, own_ip=""):
+        return self.sockethandler.start_connection_raw(dns, socktype, handler, own_ip=own_ip)

-    def start_connection(self, dns, handler = None, randomize = False):
-        return self.sockethandler.start_connection(dns, handler, randomize)
+    def start_connection(self, dns, handler = None, randomize = False, own_ip=""):
+        return self.sockethandler.start_connection(dns, handler, randomize, own_ip=own_ip)

     def get_stats(self):
         return self.sockethandler.get_stats()

El parche toma el parámetro "bind", de existir, y lo usa como IP de origen. Naturalmente dicha IP debe pertenecer a la máquina. En caso de que "bind" indique varias IPs, se toma la primera de ellas. Si no existe ningún parámetro "bind", no nos vinculamos a ninguna IP concreta, dejando que el sistema operativo decida.


Historia

  • 15/Mar/08: Primera versión de esta página.



Python Zope ©2008 jcea@jcea.es

Más información sobre los OpenBadges

Donación BitCoin: 19niBN42ac2pqDQFx6GJZxry2JQSFvwAfS