proxyOutgoingAddress: support IPv6 and multiple IPs

Ivan Shmakov <[email protected]> Sat, 17 Jan 2015 08:40:40 +0000
Newsgroups gmane.comp.web.polipo.user
Message-ID <[email protected]>
--=-=-=
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

	Back in June, I=E2=80=99ve had a use case for Polipo to bind to an
	explicit address when doing outgoing connections.  I=E2=80=99ve
	ended up implementing the feature with the patch MIMEd.

	Unfortunately, around that time the Git repository [1] (which
	still seem to be described as the primary one at [2]) went down,
	leaving me unable to check if my change=E2=80=99s still needed, and I
	dropped the case.

	Now that I=E2=80=99ve fetched the recent Git history from [3] (where the
	development has seem to be moved since) I see that with
	2e2d55d6b391 (which dates back to November), support for a
	similar =E2=80=98proxyOutgoingAddress=E2=80=99 option was added to the cod=
e.

	Sadly, this new option would be unsuitable for my needs, as it
	provides no support for IPv6, /and/ it provides no support for
	specifying several possible addresses to bind to.  (Which is a
	natural prerequisite for an ambivalent application, as in
	dual-stack environments, it needs at least /two/ addresses =E2=80=93 the
	IPv6 one and the IPv4 one =E2=80=93 to try to bind to.)

	I have no spare time right now to try and suggest a patch to
	merge the IPv6 and multiple addresses support to the current
	Polipo, but still provide my patch for any interested party to
	consider.

	TIA.

[1] git://git.wifi.pps.univ-paris-diderot.fr/polipo
[2] http://www.pps.univ-paris-diderot.fr/~jch/software/polipo/
[3] git://github.com/jech/polipo.git

--=20
FSF associate member #7257  http://boycottsystemd.org/  =E2=80=A6 3013 B6A0=
 230E 334A

--=-=-=
Content-Type: text/diff
Content-Disposition: inline

--- a/io.c	2014-05-14 21:00:59 +0000
+++ b/io.c	2014-06-12 12:57:16 +0000
@@ -31,6 +31,7 @@
 #ifdef HAVE_IPV6_PREFER_TEMPADDR
 int useTemporarySourceAddress = 1;
 #endif
+AtomListPtr ioClientBindIPs = NULL;
 
 void
 preinitIo()
@@ -40,6 +41,9 @@ preinitIo()
                              configIntSetter,
                              "Prefer IPv6 temporary source address.");
 #endif
+    CONFIG_VARIABLE(ioClientBindIPs, CONFIG_ATOM_LIST_LOWER,
+                    ("The IP addresses to use"
+                     " for outgoing connections."));
 
 #ifdef HAVE_WINSOCK
     /* Load the winsock dll */
@@ -486,6 +490,41 @@ do_connect(AtomPtr addr, int index, int port,
         return NULL;
     }
 
+    if (ioClientBindIPs != 0) {
+        const AtomListPtr l = ioClientBindIPs;
+        int i;
+#ifdef HAVE_IPv6
+        if (af == 6) {
+            struct sockaddr_in6 ba = { 0 };
+            ba.sin6_family = AF_INET6;
+            for (i = 0; i < l->length; i++) {
+                if (inet_pton  (AF_INET6, l->list[i]->string,
+                                &(ba.sin6_addr)) == 0) {
+                    continue;
+                } else if (bind (fd, &ba, sizeof (ba)) >= 0) {
+                    break;
+                }
+                do_log_error (L_ERROR, errno, "Couldn't bind");
+            }
+        } else
+#endif
+        {
+            for (i = 0; i < l->length; i++) {
+                struct sockaddr_in ba  = { 0 };
+                ba.sin_family = AF_INET;
+                if (inet_pton  (AF_INET,  l->list[i]->string,
+                                &(ba.sin_addr)) == 0) {
+                    continue;
+                } else if (bind (fd, &ba, sizeof (ba)) >= 0) {
+                    break;
+                }
+                do_log_error (L_ERROR, errno, "Couldn't bind");
+            }
+        }
+        /* FIXME: check if i == l->length and issue a warning if
+           necessary */
+    }
+
     /* POLLIN is apparently needed on Windows */
     event = registerFdEvent(fd, POLLIN | POLLOUT,
                             do_scheduled_connect,

--=-=-=
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------
New Year. New Location. New Benefits. New Data Center in Ashburn, VA.
GigeNET is offering a free month of service with a new server in Ashburn.
Choose from 2 high performing configs, both with 100TB of bandwidth.
Higher redundancy.Lower latency.Increased capacity.Completely compliant.
http://p.sf.net/sfu/gigenet
--=-=-=
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Polipo-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/polipo-users

--=-=-=--