Ignore nested comments in tokenize().
[privoxy.git] / filters.c
index 245fa0f..e2c5438 100644 (file)
--- a/filters.c
+++ b/filters.c
@@ -1,4 +1,4 @@
-const char filters_rcs[] = "$Id: filters.c,v 1.117 2009/04/17 11:35:28 fabiankeil Exp $";
+const char filters_rcs[] = "$Id: filters.c,v 1.119 2009/04/17 11:38:28 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/filters.c,v $
@@ -40,6 +40,12 @@ const char filters_rcs[] = "$Id: filters.c,v 1.117 2009/04/17 11:35:28 fabiankei
  *
  * Revisions   :
  *    $Log: filters.c,v $
+ *    Revision 1.119  2009/04/17 11:38:28  fabiankeil
+ *    Add and use parse_forwarder_address() to reduce code duplication.
+ *
+ *    Revision 1.118  2009/04/17 11:37:03  fabiankeil
+ *    Allow IPv6 addresses in forward-override{}.
+ *
  *    Revision 1.117  2009/04/17 11:35:28  fabiankeil
  *    Fix compiler warning.
  *
@@ -677,10 +683,10 @@ const char filters_rcs[] = "$Id: filters.c,v 1.117 2009/04/17 11:35:28 fabiankei
 #include <string.h>
 #include <assert.h>
 
-#ifdef HAVE_GETADDRINFO
+#ifdef HAVE_RFC2553
 #include <netdb.h>
 #include <sys/socket.h>
-#endif /* def HAVE_GETADDRINFO */
+#endif /* def HAVE_RFC2553 */
 
 #ifndef _WIN32
 #ifndef __OS2__
@@ -729,7 +735,7 @@ static jb_err remove_chunked_transfer_coding(char *buffer, size_t *size);
 static jb_err prepare_for_filtering(struct client_state *csp);
 
 #ifdef FEATURE_ACL
-#ifdef HAVE_GETADDRINFO
+#ifdef HAVE_RFC2553
 /*********************************************************************
  *
  * Function    :  sockaddr_storage_to_ip
@@ -871,7 +877,7 @@ int match_sockaddr(const struct sockaddr_storage *network,
 
    return 1;
 }
-#endif /* def HAVE_GETADDRINFO */
+#endif /* def HAVE_RFC2553 */
 
 
 /*********************************************************************
@@ -904,7 +910,7 @@ int block_acl(const struct access_control_addr *dst, const struct client_state *
    while (acl != NULL)
    {
       if (
-#ifdef HAVE_GETADDRINFO
+#ifdef HAVE_RFC2553
             match_sockaddr(&acl->src->addr, &acl->src->mask, &csp->tcp_addr)
 #else
             (csp->ip_addr_long & acl->src->mask) == acl->src->addr
@@ -920,7 +926,7 @@ int block_acl(const struct access_control_addr *dst, const struct client_state *
             }
          }
          else if (
-#ifdef HAVE_GETADDRINFO
+#ifdef HAVE_RFC2553
                /*
                 * XXX: An undefined acl->dst is full of zeros and should be
                 * considered a wildcard address. sockaddr_storage_to_ip()
@@ -971,18 +977,18 @@ int block_acl(const struct access_control_addr *dst, const struct client_state *
 int acl_addr(const char *aspec, struct access_control_addr *aca)
 {
    int i, masklength;
-#ifdef HAVE_GETADDRINFO
+#ifdef HAVE_RFC2553
    struct addrinfo hints, *result;
    uint8_t *mask_data;
    in_port_t *mask_port;
    unsigned int addr_len;
 #else
    long port;
-#endif /* def HAVE_GETADDRINFO */
+#endif /* def HAVE_RFC2553 */
    char *p;
    char *acl_spec = NULL;
 
-#ifdef HAVE_GETADDRINFO
+#ifdef HAVE_RFC2553
    /* XXX: Depend on ai_family */
    masklength = 128;
 #else
@@ -1013,7 +1019,7 @@ int acl_addr(const char *aspec, struct access_control_addr *aca)
    }
 
    if ((masklength < 0) ||
-#ifdef HAVE_GETADDRINFO
+#ifdef HAVE_RFC2553
          (masklength > 128)
 #else
          (masklength > 32)
@@ -1039,7 +1045,7 @@ int acl_addr(const char *aspec, struct access_control_addr *aca)
       p = strchr(acl_spec, ':');
    }
 
-#ifdef HAVE_GETADDRINFO
+#ifdef HAVE_RFC2553
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
@@ -1082,10 +1088,10 @@ int acl_addr(const char *aspec, struct access_control_addr *aca)
       /* XXX: This will be logged as parse error. */
       return(-1);
    }
-#endif /* def HAVE_GETADDRINFO */
+#endif /* def HAVE_RFC2553 */
 
    /* build the netmask */
-#ifdef HAVE_GETADDRINFO
+#ifdef HAVE_RFC2553
    /* Clip masklength according to current family. */
    if ((aca->addr.ss_family == AF_INET) && (masklength > 32))
    {
@@ -1142,7 +1148,7 @@ int acl_addr(const char *aspec, struct access_control_addr *aca)
     * (i.e. save on the network portion of the address).
     */
    aca->addr = aca->addr & aca->mask;
-#endif /* def HAVE_GETADDRINFO */
+#endif /* def HAVE_RFC2553 */
 
    return(0);
 
@@ -2805,30 +2811,9 @@ const static struct forward_spec *get_forward_override_settings(struct client_st
       if (NULL != socks_proxy)
       {
          /* Parse the SOCKS proxy host[:port] */
-         char *p = socks_proxy;
-         fwd->gateway_host = strdup(socks_proxy);
-
-         if ((*fwd->gateway_host == '[')
-            && (NULL != (p = strchr(fwd->gateway_host, ']'))))
-         {
-            *p++ = '\0';
-            memmove(fwd->gateway_host, fwd->gateway_host + 1,
-               (size_t)(p - fwd->gateway_host));
-            if (*p == ':')
-            {
-               fwd->gateway_port = (int)strtol(++p, NULL, 0);
-            }
-         }
-         else if (NULL != (p = strchr(fwd->gateway_host, ':')))
-         {
-            *p++ = '\0';
-            fwd->gateway_port = (int)strtol(p, NULL, 0);
-         }
-
-         if (fwd->gateway_port <= 0)
-         {
-            fwd->gateway_port = 1080;
-         }
+         fwd->gateway_port = 1080;
+         parse_forwarder_address(socks_proxy,
+            &fwd->gateway_host, &fwd->gateway_port);
 
          http_parent = vec[2];
       }
@@ -2844,30 +2829,9 @@ const static struct forward_spec *get_forward_override_settings(struct client_st
    /* Parse http forwarding settings */
    if (strcmp(http_parent, ".") != 0)
    {
-      char *p = http_parent;
-      fwd->forward_host = strdup(http_parent);
-
-      if ((*fwd->forward_host == '[')
-         && (NULL != (p = strchr(fwd->forward_host, ']'))))
-      {
-         *p++ = '\0';
-         memmove(fwd->forward_host, fwd->forward_host + 1,
-            (size_t)(p - fwd->forward_host));
-         if (*p == ':')
-         {
-            fwd->forward_port = (int)strtol(++p, NULL, 0);
-         }
-      }
-      else if (NULL != (p = strchr(fwd->forward_host, ':')))
-      {
-         *p++ = '\0';
-         fwd->forward_port = (int)strtol(p, NULL, 0);
-      }
-
-      if (fwd->forward_port <= 0)
-      {
-         fwd->forward_port = 8000;
-      }
+      fwd->forward_port = 8000;
+      parse_forwarder_address(http_parent,
+         &fwd->forward_host, &fwd->forward_port);
    }
 
    assert (NULL != fwd);