]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/icp_v2.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / icp_v2.cc
index 4e4cf2f62bac039547d96eed37da50f9dfbaf95a..075377a3df3c71571e39d987da0851cebb75bb58 100644 (file)
  */
 
 #include "squid.h"
-#include "Store.h"
+#include "AccessLogEntry.h"
+#include "acl/Acl.h"
+#include "acl/FilledChecklist.h"
 #include "comm.h"
-#include "comm/Loops.h"
-#include "ICP.h"
 #include "comm/Connection.h"
+#include "comm/Loops.h"
+#include "comm/UdpOpenDialer.h"
 #include "HttpRequest.h"
-#include "acl/FilledChecklist.h"
-#include "acl/Acl.h"
-#include "AccessLogEntry.h"
-#include "wordlist.h"
-#include "SquidTime.h"
-#include "SwapDir.h"
 #include "icmp/net_db.h"
+#include "ICP.h"
 #include "ip/Address.h"
 #include "ip/tools.h"
-#include "ipc/StartListening.h"
 #include "ipcache.h"
+#include "md5.h"
+#include "protos.h"
 #include "rfc1738.h"
+#include "SquidTime.h"
+#include "StatCounters.h"
+#include "Store.h"
+#include "SwapDir.h"
+#include "wordlist.h"
 
-/// dials icpIncomingConnectionOpened call
-class IcpListeningStartedDialer: public CallDialer,
-        public Ipc::StartListeningCb
-{
-public:
-    typedef void (*Handler)(int errNo);
-    IcpListeningStartedDialer(Handler aHandler):
-            handler(aHandler) {}
-
-    virtual void print(std::ostream &os) const { startPrint(os) << ')'; }
-    virtual bool canDial(AsyncCall &) const { return true; }
-    virtual void dial(AsyncCall &) { (handler)(errNo); }
-
-public:
-    Handler handler;
-};
+#if HAVE_ERRNO_H
+#include <errno.h>
+#endif
 
-static void icpIncomingConnectionOpened(int errNo);
+static void icpIncomingConnectionOpened(const Comm::ConnectionPointer &conn, int errNo);
 
 /// \ingroup ServerProtocolICPInternal2
 static void icpLogIcp(const Ip::Address &, log_type, int, const char *, int);
@@ -83,9 +73,6 @@ static void icpHandleIcpV2(int, Ip::Address &, char *, int);
 /// \ingroup ServerProtocolICPInternal2
 static void icpCount(void *, int, size_t, int);
 
-/// \ingroup ServerProtocolICPInternal2
-static void icpGetOutgoingIpAddress();
-
 /**
  \ingroup ServerProtocolICPInternal2
  * IcpQueueHead is global so comm_incoming() knows whether or not
@@ -100,21 +87,6 @@ Comm::ConnectionPointer icpIncomingConn = NULL;
 /// \ingroup ServerProtocolICPInternal2
 Comm::ConnectionPointer icpOutgoingConn = NULL;
 
-/** \ingroup ServerProtocolICPInternal2
- * ICP v2 uses the outgoing address as host ID.
- * NP: this *may* be identical to icpOutgoingConn->local
- * but when IN/OUT sockets are shared we can't guarantee that
- * so a separate variable is used for now.
- *
- * We have one for private use (sent only by this local cache)
- * and one for public use (for external caches to contact us)
- */
-Ip::Address theIcpPrivateHostID;
-
-/// \see theIcpPrivateHostID
-Ip::Address theIcpPublicHostID;
-
-
 /* icp_common_t */
 _icp_common_t::_icp_common_t() : opcode(ICP_INVALID), version(0), length(0), reqnum(0), flags(0), pad(0), shostid(0)
 {}
@@ -161,7 +133,6 @@ ICPState::~ICPState()
     HTTPMSGUNLOCK(request);
 }
 
-
 /* End ICPState */
 
 /* ICP2State */
@@ -220,7 +191,7 @@ ICP2State::created(StoreEntry *newEntry)
 static void
 icpLogIcp(const Ip::Address &caddr, log_type logcode, int len, const char *url, int delay)
 {
-    AccessLogEntry al;
+    AccessLogEntry::Pointer al = new AccessLogEntry();
 
     if (LOG_TAG_NONE == logcode)
         return;
@@ -233,19 +204,19 @@ icpLogIcp(const Ip::Address &caddr, log_type logcode, int len, const char *url,
     if (!Config.onoff.log_udp)
         return;
 
-    al.icp.opcode = ICP_QUERY;
+    al->icp.opcode = ICP_QUERY;
 
-    al.url = url;
+    al->url = url;
 
-    al.cache.caddr = caddr;
+    al->cache.caddr = caddr;
 
-    al.cache.replySize = len;
+    al->cache.replySize = len;
 
-    al.cache.code = logcode;
+    al->cache.code = logcode;
 
-    al.cache.msec = delay;
+    al->cache.msec = delay;
 
-    accessLogLog(&al, NULL);
+    accessLogLog(al, NULL);
 }
 
 /// \ingroup ServerProtocolICPInternal2
@@ -299,7 +270,7 @@ _icp_common_t::createMessage(
 
     headerp->pad = htonl(pad);
 
-    theIcpPrivateHostID.GetInAddr( *((struct in_addr*)&headerp->shostid) );
+    headerp->shostid = 0;
 
     urloffset = buf + sizeof(icp_common_t);
 
@@ -353,10 +324,10 @@ icpUdpSend(int fd,
         }
 
         Comm::SetSelect(fd, COMM_SELECT_WRITE, icpUdpSendQueue, NULL, 0);
-        statCounter.icp.replies_queued++;
+        ++statCounter.icp.replies_queued;
     } else {
         /* don't queue it */
-        statCounter.icp.replies_dropped++;
+        ++statCounter.icp.replies_dropped;
     }
 
     return x;
@@ -540,8 +511,8 @@ void
 _icp_common_t::handleReply(char *buf, Ip::Address &from)
 {
     if (neighbors_do_private_keys && reqnum == 0) {
-        debugs(12, 0, "icpHandleIcpV2: Neighbor " << from << " returned reqnum = 0");
-        debugs(12, 0, "icpHandleIcpV2: Disabling use of private keys");
+        debugs(12, DBG_CRITICAL, "icpHandleIcpV2: Neighbor " << from << " returned reqnum = 0");
+        debugs(12, DBG_CRITICAL, "icpHandleIcpV2: Disabling use of private keys");
         neighbors_do_private_keys = 0;
     }
 
@@ -596,7 +567,7 @@ icpHandleIcpV2(int fd, Ip::Address &from, char *buf, int len)
         break;
 
     default:
-        debugs(12, 0, "icpHandleIcpV2: UNKNOWN OPCODE: " << header.opcode << " from " << from);
+        debugs(12, DBG_CRITICAL, "icpHandleIcpV2: UNKNOWN OPCODE: " << header.opcode << " from " << from);
 
         break;
     }
@@ -629,10 +600,11 @@ icpHandleUdp(int sock, void *data)
     LOCAL_ARRAY(char, buf, SQUID_UDP_SO_RCVBUF);
     int len;
     int icp_version;
-    int max = INCOMING_ICP_MAX;
+    int max = INCOMING_UDP_MAX;
     Comm::SetSelect(sock, COMM_SELECT_READ, icpHandleUdp, NULL, 0);
 
-    while (max--) {
+    while (max) {
+        --max;
         len = comm_udp_recvfrom(sock,
                                 buf,
                                 SQUID_UDP_SO_RCVBUF - 1,
@@ -646,7 +618,7 @@ icpHandleUdp(int sock, void *data)
             if (ignoreErrno(errno))
                 break;
 
-#ifdef _SQUID_LINUX_
+#if _SQUID_LINUX_
             /* Some Linux systems seem to set the FD for reading and then
              * return ECONNREFUSED when sendto() fails and generates an ICMP
              * port unreachable message. */
@@ -654,12 +626,12 @@ icpHandleUdp(int sock, void *data)
             if (errno != ECONNREFUSED && errno != EHOSTUNREACH)
 #endif
 
-                debugs(50, 1, "icpHandleUdp: FD " << sock << " recvfrom: " << xstrerror());
+                debugs(50, DBG_IMPORTANT, "icpHandleUdp: FD " << sock << " recvfrom: " << xstrerror());
 
             break;
         }
 
-        (*N)++;
+        ++(*N);
         icpCount(buf, RECV, (size_t) len, 0);
         buf[len] = '\0';
         debugs(12, 4, "icpHandleUdp: FD " << sock << ": received " <<
@@ -685,13 +657,13 @@ icpHandleUdp(int sock, void *data)
         else if (icp_version == ICP_VERSION_3)
             icpHandleIcpV3(sock, from, buf, len);
         else
-            debugs(12, 1, "WARNING: Unused ICP version " << icp_version <<
+            debugs(12, DBG_IMPORTANT, "WARNING: Unused ICP version " << icp_version <<
                    " received from " << from);
     }
 }
 
 void
-icpConnectionsOpen(void)
+icpOpenPorts(void)
 {
     uint16_t port;
 
@@ -713,7 +685,7 @@ icpConnectionsOpen(void)
 
     AsyncCall::Pointer call = asyncCall(12, 2,
                                         "icpIncomingConnectionOpened",
-                                        IcpListeningStartedDialer(&icpIncomingConnectionOpened));
+                                        Comm::UdpOpenDialer(&icpIncomingConnectionOpened));
 
     Ipc::StartListening(SOCK_DGRAM,
                         IPPROTO_UDP,
@@ -745,57 +717,28 @@ icpConnectionsOpen(void)
 
         Comm::SetSelect(icpOutgoingConn->fd, COMM_SELECT_READ, icpHandleUdp, NULL, 0);
         fd_note(icpOutgoingConn->fd, "Outgoing ICP socket");
-        icpGetOutgoingIpAddress();
     }
 }
 
-// Ensure that we have the IP address(es) to use for Host ID.
-// The outgoing address is used as 'private' host ID used only on packets we send
-static void
-icpGetOutgoingIpAddress()
-{
-    struct addrinfo *xai = NULL;
-    theIcpPrivateHostID.SetEmpty();
-    theIcpPrivateHostID.InitAddrInfo(xai);
-    if (getsockname(icpOutgoingConn->fd, xai->ai_addr, &xai->ai_addrlen) < 0)
-        debugs(50, DBG_IMPORTANT, "ERROR: Unable to identify ICP host ID to use for " << icpOutgoingConn
-               << ": getsockname: " << xstrerror());
-    else
-        theIcpPrivateHostID = *xai;
-    theIcpPrivateHostID.FreeAddrInfo(xai);
-}
-
 static void
-icpIncomingConnectionOpened(int errNo)
+icpIncomingConnectionOpened(const Comm::ConnectionPointer &conn, int errNo)
 {
-    if (!Comm::IsConnOpen(icpIncomingConn))
+    if (!Comm::IsConnOpen(conn))
         fatal("Cannot open ICP Port");
 
-    Comm::SetSelect(icpIncomingConn->fd, COMM_SELECT_READ, icpHandleUdp, NULL, 0);
+    Comm::SetSelect(conn->fd, COMM_SELECT_READ, icpHandleUdp, NULL, 0);
 
     for (const wordlist *s = Config.mcast_group_list; s; s = s->next)
-        ipcache_nbgethostbyname(s->key, mcastJoinGroups, NULL); // XXX: pass the icpIncomingConn for mcastJoinGroups usage.
+        ipcache_nbgethostbyname(s->key, mcastJoinGroups, NULL); // XXX: pass the conn for mcastJoinGroups usage.
 
-    debugs(12, DBG_IMPORTANT, "Accepting ICP messages on " << icpIncomingConn->local);
+    debugs(12, DBG_IMPORTANT, "Accepting ICP messages on " << conn->local);
 
-    fd_note(icpIncomingConn->fd, "Incoming ICP port");
+    fd_note(conn->fd, "Incoming ICP port");
 
     if (Config.Addrs.udp_outgoing.IsNoAddr()) {
-        icpOutgoingConn = icpIncomingConn;
+        icpOutgoingConn = conn;
         debugs(12, DBG_IMPORTANT, "Sending ICP messages from " << icpOutgoingConn->local);
-        icpGetOutgoingIpAddress();
     }
-
-    // Ensure that we have the IP address(es) to use for Host ID.
-    // The listening address is used as 'public' host ID which can be used to contact us
-    struct addrinfo *xai = NULL;
-    theIcpPublicHostID.InitAddrInfo(xai); // reset xai
-    if (getsockname(icpIncomingConn->fd, xai->ai_addr, &xai->ai_addrlen) < 0)
-        debugs(50, DBG_IMPORTANT, "ERROR: Unable to identify ICP host ID to use for " << icpIncomingConn
-               << ": getsockname: " << xstrerror());
-    else
-        theIcpPublicHostID = *xai;
-    theIcpPublicHostID.FreeAddrInfo(xai);
 }
 
 /**
@@ -828,7 +771,7 @@ icpConnectionShutdown(void)
 }
 
 void
-icpConnectionClose(void)
+icpClosePorts(void)
 {
     icpConnectionShutdown();
 
@@ -847,36 +790,36 @@ icpCount(void *buf, int which, size_t len, int delay)
         return;
 
     if (SENT == which) {
-        statCounter.icp.pkts_sent++;
+        ++statCounter.icp.pkts_sent;
         kb_incr(&statCounter.icp.kbytes_sent, len);
 
         if (ICP_QUERY == icp->opcode) {
-            statCounter.icp.queries_sent++;
+            ++statCounter.icp.queries_sent;
             kb_incr(&statCounter.icp.q_kbytes_sent, len);
         } else {
-            statCounter.icp.replies_sent++;
+            ++statCounter.icp.replies_sent;
             kb_incr(&statCounter.icp.r_kbytes_sent, len);
             /* this is the sent-reply service time */
-            statHistCount(&statCounter.icp.reply_svc_time, delay);
+            statCounter.icp.replySvcTime.count(delay);
         }
 
         if (ICP_HIT == icp->opcode)
-            statCounter.icp.hits_sent++;
+            ++statCounter.icp.hits_sent;
     } else if (RECV == which) {
-        statCounter.icp.pkts_recv++;
+        ++statCounter.icp.pkts_recv;
         kb_incr(&statCounter.icp.kbytes_recv, len);
 
         if (ICP_QUERY == icp->opcode) {
-            statCounter.icp.queries_recv++;
+            ++statCounter.icp.queries_recv;
             kb_incr(&statCounter.icp.q_kbytes_recv, len);
         } else {
-            statCounter.icp.replies_recv++;
+            ++statCounter.icp.replies_recv;
             kb_incr(&statCounter.icp.r_kbytes_recv, len);
-            /* statCounter.icp.query_svc_time set in clientUpdateCounters */
+            /* statCounter.icp.querySvcTime set in clientUpdateCounters */
         }
 
         if (ICP_HIT == icp->opcode)
-            statCounter.icp.hits_recv++;
+            ++statCounter.icp.hits_recv;
     }
 }