]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Author: Pawel Worach <pawel.worach@gmail.com>
authoramosjeffries <>
Tue, 13 Nov 2007 06:10:37 +0000 (06:10 +0000)
committeramosjeffries <>
Tue, 13 Nov 2007 06:10:37 +0000 (06:10 +0000)
Enable squid to lookup /etc/services for named peer ports.

Here is patch so you can use port names from /etc/services in
squid.conf for cache_peers like so:
   cache_peer upstream.example.net parent http-cache icpv2

assumng you have something like this in /etc/services
  http-cache   8080/tcp
  icpv2           3130/udp

This became needed here where we have the same squid.conf's deployed
across a cluster of reverse proxies and we control originserver
addresses via a hosts file and originserver  ports via /etc/services
locally on each node.

src/cache_cf.cc

index f3660f27cbf227b4d1af2387218310286ed8dc95..681f0f0b5429692645b37dc663652658961ff554 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: cache_cf.cc,v 1.524 2007/10/31 04:52:16 amosjeffries Exp $
+ * $Id: cache_cf.cc,v 1.525 2007/11/12 23:10:37 amosjeffries Exp $
  *
  * DEBUG: section 3     Configuration File Parsing
  * AUTHOR: Harvest Derived
@@ -1538,6 +1538,49 @@ dump_peer(StoreEntry * entry, const char *name, peer * p)
     }
 }
 
+/**
+ \param proto  'tcp' or 'udp' for protocol
+ \returns       Port the named service is supposed to be listening on.
+ */
+static u_short
+GetService(const char *proto)
+{
+    struct servent *port = NULL;
+    /** Parses a port number or service name from the squid.conf */
+    char *token = strtok(NULL, w_space);
+    if (token == NULL) {
+       self_destruct();
+       return -1; /* NEVER REACHED */
+    }
+    /** Returns either the service port number from /etc/services */
+    port = getservbyname(token, proto);
+    if (port != NULL) {
+        return ntohs((u_short)port->s_port);
+    }
+    /** Or a numeric translation of the config text. */
+    return xatos(token);
+}
+
+/**
+ \returns       Port the named TCP service is supposed to be listening on.
+ \copydoc GetService(const char *proto)
+ */
+inline u_short
+GetTcpService(void)
+{
+    return GetService("tcp");
+}
+
+/**
+ \returns       Port the named UDP service is supposed to be listening on.
+ \copydoc GetService(const char *proto)
+ */
+inline u_short
+GetUdpService(void)
+{
+    return GetService("udp");
+}
+
 static void
 parse_peer(peer ** head)
 {
@@ -1568,12 +1611,12 @@ parse_peer(peer ** head)
         p->options.no_netdb_exchange = 1;
     }
 
-    p->http_port = GetShort();
+    p->http_port = GetTcpService();
 
     if (!p->http_port)
         self_destruct();
 
-    p->icp.port = GetShort();
+    p->icp.port = GetUdpService();
 
     while ((token = strtok(NULL, w_space))) {
         if (!strcasecmp(token, "proxy-only")) {