Let dynamic filters and taggers support a $listen-address variable
authorFabian Keil <fk@fabiankeil.de>
Wed, 25 May 2016 10:50:55 +0000 (10:50 +0000)
committerFabian Keil <fk@fabiankeil.de>
Wed, 25 May 2016 10:50:55 +0000 (10:50 +0000)
... which contains the address the request came in on.
For external filters the variable is called $PRIVOXY_LISTEN_ADDRESS.

Original patch contributed by pursievro.

default.filter
doc/source/user-manual.sgml
filters.c
jbsockets.c
jcc.c
loaders.c
project.h
user.filter

index 46408c0..9815fea 100644 (file)
@@ -2,7 +2,7 @@
 # 
 #  File        :  $Source: /cvsroot/ijbswa/current/default.filter,v $
 # 
 # 
 #  File        :  $Source: /cvsroot/ijbswa/current/default.filter,v $
 # 
-#  $Id: default.filter,v 1.91 2014/06/20 09:46:13 fabiankeil Exp $
+#  $Id: default.filter,v 1.92 2014/10/17 14:45:10 fabiankeil Exp $
 #
 #  Purpose     :  Rules to process the content of web pages
 # 
 #
 #  Purpose     :  Rules to process the content of web pages
 # 
@@ -57,7 +57,8 @@
 #     quoting.
 #
 # 'D' (Dynamic) allows the use of variables. Supported variables are:
 #     quoting.
 #
 # 'D' (Dynamic) allows the use of variables. Supported variables are:
-#     $host, $origin (the IP address the request came from), $path and $url.
+#     $host, $listen-address, $origin (the IP address the request came
+#     from), $path and $url.
 #
 #     Note that '$' is a bad choice as delimiter for dynamic filters as you
 #     might end up with unintended variables if you use a variable name
 #
 #     Note that '$' is a bad choice as delimiter for dynamic filters as you
 #     might end up with unintended variables if you use a variable name
index 0a8588d..4151b42 100644 (file)
@@ -36,7 +36,7 @@
                 This file belongs into
                 ijbswa.sourceforge.net:/home/groups/i/ij/ijbswa/htdocs/
 
                 This file belongs into
                 ijbswa.sourceforge.net:/home/groups/i/ij/ijbswa/htdocs/
 
- $Id: user-manual.sgml,v 2.210 2016/05/03 13:20:20 fabiankeil Exp $
+ $Id: user-manual.sgml,v 2.211 2016/05/22 12:42:11 fabiankeil Exp $
 
  Copyright (C) 2001-2016 Privoxy Developers https://www.privoxy.org/
  See LICENSE.
 
  Copyright (C) 2001-2016 Privoxy Developers https://www.privoxy.org/
  See LICENSE.
@@ -62,7 +62,7 @@
  </subscript>
 </pubdate>
 
  </subscript>
 </pubdate>
 
-<pubdate>$Id: user-manual.sgml,v 2.210 2016/05/03 13:20:20 fabiankeil Exp $</pubdate>
+<pubdate>$Id: user-manual.sgml,v 2.211 2016/05/22 12:42:11 fabiankeil Exp $</pubdate>
 
 <!--
 
 
 <!--
 
@@ -6773,8 +6773,10 @@ stupid-server.example.com/</screen>
 <para>
  The non-standard option letter <literal>D</literal> (dynamic) allows
  to use the variables $host, $origin (the IP address the request came from),
 <para>
  The non-standard option letter <literal>D</literal> (dynamic) allows
  to use the variables $host, $origin (the IP address the request came from),
- $path and $url. They will be replaced with the value they refer to before
- the filter is executed.
+ $path, $url and $listen-address (the address on which Privoxy accepted the
+ client request. Example: 127.0.0.1:8118).
+ They will be replaced with the value they refer to before the filter
+ is executed.
 </para>
 
 <para>
 </para>
 
 <para>
@@ -7531,9 +7533,10 @@ pre-defined filters for your convenience:
 </para>
 <para>
  External filters read the content from STDIN and write the rewritten
 </para>
 <para>
  External filters read the content from STDIN and write the rewritten
- content to STDOUT. The environment variables PRIVOXY_URL, PRIVOXY_PATH,
- PRIVOXY_HOST, PRIVOXY_ORIGIN can be used to get some details about the
- client request.
+ content to STDOUT.
+ The environment variables PRIVOXY_URL, PRIVOXY_PATH, PRIVOXY_HOST,
+ PRIVOXY_ORIGIN, PRIVOXY_LISTEN_ADDRESS can be used to get some details
+ about the client request.
 </para>
 <para>
  &my-app; will temporary store the content to filter in the
 </para>
 <para>
  &my-app; will temporary store the content to filter in the
index c3aa3fd..d4cec75 100644 (file)
--- a/filters.c
+++ b/filters.c
@@ -1,4 +1,4 @@
-const char filters_rcs[] = "$Id: filters.c,v 1.200 2016/02/26 12:29:38 fabiankeil Exp $";
+const char filters_rcs[] = "$Id: filters.c,v 1.201 2016/03/17 10:40:53 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/filters.c,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/filters.c,v $
@@ -935,6 +935,7 @@ pcrs_job *compile_dynamic_pcrs_job_list(const struct client_state *csp, const st
       {"path",   csp->http->path,  1},
       {"host",   csp->http->host,  1},
       {"origin", csp->ip_addr_str, 1},
       {"path",   csp->http->path,  1},
       {"host",   csp->http->host,  1},
       {"origin", csp->ip_addr_str, 1},
+      {"listen-address", csp->listen_addr_str, 1},
       {NULL,     NULL,             1}
    };
 
       {NULL,     NULL,             1}
    };
 
@@ -1771,6 +1772,7 @@ static void set_privoxy_variables(const struct client_state *csp)
       { "PRIVOXY_PATH",   csp->http->path  },
       { "PRIVOXY_HOST",   csp->http->host  },
       { "PRIVOXY_ORIGIN", csp->ip_addr_str },
       { "PRIVOXY_PATH",   csp->http->path  },
       { "PRIVOXY_HOST",   csp->http->host  },
       { "PRIVOXY_ORIGIN", csp->ip_addr_str },
+      { "PRIVOXY_LISTEN_ADDRESS", csp->listen_addr_str },
    };
 
    for (i = 0; i < SZ(env); i++)
    };
 
    for (i = 0; i < SZ(env); i++)
index 48f43c9..c1a4080 100644 (file)
@@ -1,4 +1,4 @@
-const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.134 2015/11/06 13:37:35 fabiankeil Exp $";
+const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.135 2016/01/16 12:33:35 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jbsockets.c,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jbsockets.c,v $
@@ -1221,6 +1221,7 @@ int accept_connection(struct client_state * csp, jb_socket fds[])
    int max_selected_socket;
    fd_set selected_fds;
    jb_socket fd;
    int max_selected_socket;
    fd_set selected_fds;
    jb_socket fd;
+   size_t listen_addr_size;
 
    c_length = sizeof(client);
 
 
    c_length = sizeof(client);
 
@@ -1348,6 +1349,25 @@ int accept_connection(struct client_state * csp, jb_socket fds[])
    csp->ip_addr_long = ntohl(client.sin_addr.s_addr);
 #endif /* def HAVE_RFC2553 */
 
    csp->ip_addr_long = ntohl(client.sin_addr.s_addr);
 #endif /* def HAVE_RFC2553 */
 
+   /*
+    * Save the name and port of the accepting socket for later lookup.
+    *
+    * The string needs space for strlen(...) + 7 characters:
+    * strlen(haddr[i]) + 1 (':') + 5 (port digits) + 1 ('\0')
+    */
+   listen_addr_size = strlen(csp->config->haddr[i]) + 7;
+   csp->listen_addr_str = malloc_or_die(listen_addr_size);
+   retval = snprintf(csp->listen_addr_str, listen_addr_size,
+      "%s:%d", csp->config->haddr[i], csp->config->hport[i]);
+   if ((-1 == retval) || listen_addr_size <= retval)
+   {
+      log_error(LOG_LEVEL_ERROR,
+         "Server name (%s) and port number (%d) ASCII decimal representation"
+         "don't fit into %d bytes",
+         csp->config->haddr[i], csp->config->hport[i], listen_addr_size);
+      return 0;
+   }
+
    return 1;
 
 }
    return 1;
 
 }
diff --git a/jcc.c b/jcc.c
index ad210ab..082bb02 100644 (file)
--- a/jcc.c
+++ b/jcc.c
@@ -1,4 +1,4 @@
-const char jcc_rcs[] = "$Id: jcc.c,v 1.442 2016/03/17 10:40:53 fabiankeil Exp $";
+const char jcc_rcs[] = "$Id: jcc.c,v 1.443 2016/05/22 12:43:07 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jcc.c,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jcc.c,v $
@@ -3992,6 +3992,12 @@ static void listen_loop(void)
          "Waiting for the next client connection. Currently active threads: %d",
          active_threads);
 
          "Waiting for the next client connection. Currently active threads: %d",
          active_threads);
 
+      /*
+       * This config may be outdated, but for accept_connection()
+       * it's fresh enough.
+       */
+      csp->config = config;
+
       if (!accept_connection(csp, bfds))
       {
          log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
       if (!accept_connection(csp, bfds))
       {
          log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
@@ -4051,6 +4057,7 @@ static void listen_loop(void)
             "Connection from %s on socket %d dropped due to ACL", csp->ip_addr_str, csp->cfd);
          close_socket(csp->cfd);
          freez(csp->ip_addr_str);
             "Connection from %s on socket %d dropped due to ACL", csp->ip_addr_str, csp->cfd);
          close_socket(csp->cfd);
          freez(csp->ip_addr_str);
+         freez(csp->listen_addr_str);
          freez(csp_list);
          continue;
       }
          freez(csp_list);
          continue;
       }
@@ -4066,6 +4073,7 @@ static void listen_loop(void)
             strlen(TOO_MANY_CONNECTIONS_RESPONSE));
          close_socket(csp->cfd);
          freez(csp->ip_addr_str);
             strlen(TOO_MANY_CONNECTIONS_RESPONSE));
          close_socket(csp->cfd);
          freez(csp->ip_addr_str);
+         freez(csp->listen_addr_str);
          freez(csp_list);
          continue;
       }
          freez(csp_list);
          continue;
       }
index 92e0353..1fdeabf 100644 (file)
--- a/loaders.c
+++ b/loaders.c
@@ -1,4 +1,4 @@
-const char loaders_rcs[] = "$Id: loaders.c,v 1.103 2016/05/08 10:45:32 fabiankeil Exp $";
+const char loaders_rcs[] = "$Id: loaders.c,v 1.104 2016/05/22 12:43:07 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/loaders.c,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/loaders.c,v $
@@ -185,6 +185,7 @@ unsigned int sweep(void)
 #ifdef FEATURE_CLIENT_TAGS
          freez(csp->client_address);
 #endif
 #ifdef FEATURE_CLIENT_TAGS
          freez(csp->client_address);
 #endif
+         freez(csp->listen_addr_str);
          freez(csp->client_iob->buf);
          freez(csp->iob->buf);
          freez(csp->error_message);
          freez(csp->client_iob->buf);
          freez(csp->iob->buf);
          freez(csp->error_message);
index 32a63d5..9214137 100644 (file)
--- a/project.h
+++ b/project.h
@@ -1,7 +1,7 @@
 #ifndef PROJECT_H_INCLUDED
 #define PROJECT_H_INCLUDED
 /** Version string. */
 #ifndef PROJECT_H_INCLUDED
 #define PROJECT_H_INCLUDED
 /** Version string. */
-#define PROJECT_H_VERSION "$Id: project.h,v 1.214 2016/03/30 11:13:25 fabiankeil Exp $"
+#define PROJECT_H_VERSION "$Id: project.h,v 1.215 2016/05/22 12:43:07 fabiankeil Exp $"
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/project.h,v $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/project.h,v $
@@ -927,6 +927,10 @@ struct client_state
    unsigned long ip_addr_long;
 #endif /* def HAVE_RFC2553 */
 
    unsigned long ip_addr_long;
 #endif /* def HAVE_RFC2553 */
 
+   /** The host name and port (as a string of the form '<hostname>:<port>')
+       of the server socket to which the client connected. */
+   char *listen_addr_str;
+
    /** The URL that was requested */
    struct http_request http[1];
 
    /** The URL that was requested */
    struct http_request http[1];
 
index b138ff2..976953c 100644 (file)
@@ -2,7 +2,7 @@
 # 
 #  File        :  $Source: /cvsroot/ijbswa/current/user.filter,v $
 # 
 # 
 #  File        :  $Source: /cvsroot/ijbswa/current/user.filter,v $
 # 
-#  $Id: user.filter,v 1.2 2006/07/18 14:48:47 david__schmidt Exp $
+#  $Id: user.filter,v 1.3 2008/05/21 20:17:03 fabiankeil Exp $
 #
 #  Purpose     :  Rules to process the content of web pages
 # 
 #
 #  Purpose     :  Rules to process the content of web pages
 # 
@@ -63,7 +63,8 @@
 #     quoting.
 #
 # 'D' (Dynamic) allows the use of variables. Supported variables are:
 #     quoting.
 #
 # 'D' (Dynamic) allows the use of variables. Supported variables are:
-#     $host, $origin (the IP address the request came from), $path and $url.
+#     $host, $listen-address, $origin (the IP address the request came
+#     from), $path and $url.
 #
 #     Note that '$' is a bad choice as delimiter for dynamic filters as you
 #     might end up with unintended variables if you use a variable name
 #
 #     Note that '$' is a bad choice as delimiter for dynamic filters as you
 #     might end up with unintended variables if you use a variable name