[PATCH] Address family preference (TODO #93)

Christiaan Welvaart <[email protected]> Wed, 12 Nov 2014 15:44:25 +0100 (CET)
Newsgroups gmane.comp.web.privoxy.devel
Message-ID <[email protected]>
Add a configuration item that tells privoxy to request a specific address 
family from the OS for outgoing connections as decribed in TODO #93. For 
TODO #94 it could be changed to a list, where one would write:
   address-family-preference IPv4,IPv6


diff -up privoxy-cvs/doc/source/p-config.sgml.address-family-preference privoxy-cvs/doc/source/p-config.sgml
--- privoxy-cvs/doc/source/p-config.sgml.address-family-preference	2014-11-12 15:18:15.026692015 +0100
+++ privoxy-cvs/doc/source/p-config.sgml	2014-11-12 15:26:25.203946027 +0100
@@ -3347,6 +3347,44 @@ forward-socks4, forward-socks4a, forward
  </literallayout>]]>
  </sect3>

+<sect3 renderas="sect4" id="address-family-preference"><title>address-family-preference</title>
+<variablelist>
+ <varlistentry>
+  <term>Specifies:</term>
+  <listitem>
+   <para>
+    Whether a specific address family (IPv4 or IPv6) is preferred for outgoing connections.
+   </para>
+  </listitem>
+ </varlistentry>
+ <varlistentry>
+  <term>Type of value:</term>
+  <listitem>
+   <para>
+    <replaceable>none or ipv4 or ipv6</replaceable>
+   </para>
+  </listitem>
+ </varlistentry>
+ <varlistentry>
+  <term>Default value:</term>
+  <listitem>
+   <para>none</para>
+  </listitem>
+ </varlistentry>
+ <varlistentry>
+  <term>Notes:</term>
+  <listitem>
+   <para>
+     By default &my-app; performs generic DNS lookups, often preferring IPv6 in practice. 
+     When this option is set to something other than none, addresses from the specified 
+     family are requested from the OS.
+   </para>
+  </listitem>
+ </varlistentry>
+</variablelist>
+<![%config-file;[<literallayout>@@#address-family-preference  none</literallayout>]]>
+</sect3>
+

  </sect2>

diff -up privoxy-cvs/jbsockets.c.address-family-preference privoxy-cvs/jbsockets.c
--- privoxy-cvs/jbsockets.c.address-family-preference	2014-11-12 15:18:15.026692015 +0100
+++ privoxy-cvs/jbsockets.c	2014-11-12 15:26:25.205945988 +0100
@@ -216,7 +216,17 @@ static jb_socket rfc2553_connect_to(cons
     }

     memset((char *)&hints, 0, sizeof(hints));
-   hints.ai_family = AF_UNSPEC;
+   switch(csp->config->address_family_preference)
+   {
+      case ADDRESS_FAMILY_PREFERENCE_IPV4:
+         hints.ai_family = AF_INET;
+         break;
+      case ADDRESS_FAMILY_PREFERENCE_IPV6:
+         hints.ai_family = AF_INET6;
+         break;
+      default:
+         hints.ai_family = AF_UNSPEC;
+   }
     hints.ai_socktype = SOCK_STREAM;
     hints.ai_flags = AI_NUMERICSERV; /* avoid service look-up */
  #ifdef AI_ADDRCONFIG
diff -up privoxy-cvs/loadcfg.c.address-family-preference privoxy-cvs/loadcfg.c
--- privoxy-cvs/loadcfg.c.address-family-preference	2014-11-12 15:18:15.027691995 +0100
+++ privoxy-cvs/loadcfg.c	2014-11-12 15:26:25.206945968 +0100
@@ -121,6 +121,7 @@ static struct file_list *current_configf
  #define hash_actions_file                1196306641U /* "actionsfile" */
  #define hash_accept_intercepted_requests 1513024973U /* "accept-intercepted-requests" */
  #define hash_admin_address               4112573064U /* "admin-address" */
+#define hash_address_family_preference   3725998957U /* "address-family-preference */
  #define hash_allow_cgi_request_crunching  258915987U /* "allow-cgi-request-crunching" */
  #define hash_buffer_limit                1881726070U /* "buffer-limit */
  #define hash_client_header_order         2701453514U /* "client-header-order" */
@@ -913,6 +914,30 @@ struct configuration_spec * load_config(
              break;

  /* *************************************************************************
+ * address-family-preference ipv4|IPv4|ipv6|IPv6|none
+ * *************************************************************************/
+         case hash_address_family_preference:
+            if (!strcmp(arg, "ipv4") || !strcmp(arg, "IPv4"))
+            {
+               config->address_family_preference = ADDRESS_FAMILY_PREFERENCE_IPV4;
+            }
+            else if (!strcmp(arg, "ipv6") || !strcmp(arg, "IPv6"))
+            {
+               config->address_family_preference = ADDRESS_FAMILY_PREFERENCE_IPV6;
+            }
+            else if (!strcmp(arg, "none"))
+            {
+               config->address_family_preference = ADDRESS_FAMILY_PREFERENCE_NONE;
+            }
+            else
+            {
+               log_error(LOG_LEVEL_FATAL,
+                  "Directive %s used with invalid argument '%s'. Use 'ipv6', 'ipv4', or 'none'.",
+                  cmd, arg);
+            }
+            break;
+
+/* *************************************************************************
   * forward url-pattern (.|http-proxy-host[:port])
   * *************************************************************************/
           case hash_forward:
diff -up privoxy-cvs/project.h.address-family-preference privoxy-cvs/project.h
--- privoxy-cvs/project.h.address-family-preference	2014-11-12 15:18:15.027691995 +0100
+++ privoxy-cvs/project.h	2014-11-12 15:26:25.206945968 +0100
@@ -881,6 +881,16 @@ struct reusable_connection
   */
  #define MAX_LISTENING_SOCKETS 10

+enum address_family_preference
+{
+   /** No preference, let the OS decide. */
+   ADDRESS_FAMILY_PREFERENCE_NONE = 0,
+   /** Ask the OS for IPv4 addresses only. */
+   ADDRESS_FAMILY_PREFERENCE_IPV4,
+   /** Ask the OS for IPv6 addresses only. */
+   ADDRESS_FAMILY_PREFERENCE_IPV6
+};
+
  /**
   * The state of a Privoxy processing thread.
   */
@@ -1343,6 +1353,9 @@ struct configuration_spec
     /* Assumed server-side keep alive timeout if none is specified. */
     unsigned int default_server_timeout;
  #endif
+ 
+   /** Preference for the address family for outgoing connections. */
+   enum address_family_preference address_family_preference;

  #ifdef FEATURE_COMPRESSION
     int compression_level;

------------------------------------------------------------------------------
Comprehensive Server Monitoring with Site24x7.
Monitor 10 servers for $9/Month.
Get alerted through email, SMS, voice calls or mobile push notifications.
Take corrective actions from your mobile device.
http://pubads.g.doubleclick.net/gampad/clk?id=154624111&iu=/4140/ostg.clktrk