]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Polish: de-duplicate UDP port dialers
authorAmos Jeffries <squid3@treenet.co.nz>
Thu, 29 Mar 2012 09:22:41 +0000 (21:22 +1200)
committerAmos Jeffries <squid3@treenet.co.nz>
Thu, 29 Mar 2012 09:22:41 +0000 (21:22 +1200)
This create a Comm::UdpOpenDialer class which replaces the ICP, HTCP and
SNMP start-listening dialer classes. Their code was very close to
identical anyway.

ICP and HTCP can now also use the dialer Comm::Connection parameter
instead of assuming that the callback relates to the global incoming
port variable.

src/comm/Makefile.am
src/comm/UdpOpenDialer.h [new file with mode: 0644]
src/htcp.cc
src/icp_v2.cc
src/snmp_core.cc

index 0ba83c14adc334dd91ba6896ec3d8f542f91407a..42ff5a4e4fb8cf634fda1cbf4219a2250dcbf650 100644 (file)
@@ -23,6 +23,7 @@ libcomm_la_SOURCES= \
        ModSelectWin32.cc \
        TcpAcceptor.cc \
        TcpAcceptor.h \
+       UdpOpenDialer.h \
        Write.cc \
        Write.h \
        \
diff --git a/src/comm/UdpOpenDialer.h b/src/comm/UdpOpenDialer.h
new file mode 100644 (file)
index 0000000..ed72b8f
--- /dev/null
@@ -0,0 +1,27 @@
+#ifndef SQUID_COMM_UDPOPENDIALER_H
+#define SQUID_COMM_UDPOPENDIALER_H
+
+#include "ipc/StartListening.h"
+
+namespace Comm
+{
+
+/// dials a UDP port-opened call
+class UdpOpenDialer: public CallDialer,
+        public Ipc::StartListeningCb
+{
+public:
+    typedef void (*Handler)((const Comm::ConnectionPointer &conn, int errNo);
+    UdpOpenDialer(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)(conn, errNo); }
+
+public:
+    Handler handler;
+};
+
+} // namespace Comm
+
+#endif /* SQUID_COMM_UDPOPENDIALER_H */
index b243ee55e50bb4e8168bac31eb2a0b1eb1a4f97e..4466443016e5df5ffb698b60f6c46232f0bb6396 100644 (file)
 #include "acl/Acl.h"
 #include "comm.h"
 #include "comm/Loops.h"
+#include "comm/UdpOpenDialer.h"
 #include "htcp.h"
 #include "http.h"
 #include "HttpRequest.h"
 #include "icmp/net_db.h"
-#include "ipc/StartListening.h"
 #include "ip/tools.h"
 #include "MemBuf.h"
 #include "SquidTime.h"
 #include "StoreClient.h"
 #include "compat/xalloc.h"
 
-/// dials htcpIncomingConnectionOpened call
-class HtcpListeningStartedDialer: public CallDialer,
-        public Ipc::StartListeningCb
-{
-public:
-    typedef void (*Handler)(int errNo);
-    HtcpListeningStartedDialer(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;
-};
-
 typedef struct _Countstr Countstr;
 
 typedef struct _htcpHeader htcpHeader;
@@ -246,7 +230,7 @@ enum {
     RR_RESPONSE
 };
 
-static void htcpIncomingConnectionOpened(int errNo);
+static void htcpIncomingConnectionOpened(const Comm::ConnectionPointer &conn, int errNo);
 static uint32_t msg_id_counter = 0;
 
 static Comm::ConnectionPointer htcpOutgoingConn = NULL;
@@ -1507,7 +1491,7 @@ htcpInit(void)
 
     AsyncCall::Pointer call = asyncCall(31, 2,
                                         "htcpIncomingConnectionOpened",
-                                        HtcpListeningStartedDialer(&htcpIncomingConnectionOpened));
+                                        Comm::UdpOpenDialer(&htcpIncomingConnectionOpened));
 
     Ipc::StartListening(SOCK_DGRAM,
                         IPPROTO_UDP,
@@ -1546,17 +1530,17 @@ htcpInit(void)
 }
 
 static void
-htcpIncomingConnectionOpened(int)
+htcpIncomingConnectionOpened(const Comm::ConnectionPointer &conn, int)
 {
-    if (!Comm::IsConnOpen(htcpIncomingConn))
+    if (!Comm::IsConnOpen(conn))
         fatal("Cannot open HTCP Socket");
 
-    Comm::SetSelect(htcpIncomingConn->fd, COMM_SELECT_READ, htcpRecv, NULL, 0);
+    Comm::SetSelect(conn->fd, COMM_SELECT_READ, htcpRecv, NULL, 0);
 
-    debugs(31, DBG_CRITICAL, "Accepting HTCP messages on " << htcpIncomingConn->local);
+    debugs(31, DBG_CRITICAL, "Accepting HTCP messages on " << conn->local);
 
     if (Config.Addrs.udp_outgoing.IsNoAddr()) {
-        htcpOutgoingConn = htcpIncomingConn;
+        htcpOutgoingConn = conn;
         debugs(31, DBG_IMPORTANT, "Sending HTCP messages from " << htcpOutgoingConn->local);
     }
 }
index 436f36df848827280976acc8f6bce2363dafbd0e..596ad17055e4f5358aa819b3ffa05f7a52b54473 100644 (file)
 #include "squid-old.h"
 #include "Store.h"
 #include "comm.h"
+#include "comm/Connection.h"
 #include "comm/Loops.h"
+#include "comm/UdpOpenDialer.h"
 #include "ICP.h"
-#include "comm/Connection.h"
 #include "HttpRequest.h"
 #include "acl/FilledChecklist.h"
 #include "acl/Acl.h"
 #include "icmp/net_db.h"
 #include "ip/Address.h"
 #include "ip/tools.h"
-#include "ipc/StartListening.h"
 #include "ipcache.h"
 #include "rfc1738.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;
-};
-
-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);
@@ -696,7 +679,7 @@ icpConnectionsOpen(void)
 
     AsyncCall::Pointer call = asyncCall(12, 2,
                                         "icpIncomingConnectionOpened",
-                                        IcpListeningStartedDialer(&icpIncomingConnectionOpened));
+                                        Comm::UdpOpenDialer(&icpIncomingConnectionOpened));
 
     Ipc::StartListening(SOCK_DGRAM,
                         IPPROTO_UDP,
@@ -732,22 +715,22 @@ icpConnectionsOpen(void)
 }
 
 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);
     }
 }
index 87b6dc0308bb77a63b30e0a5832636f6c93f1a34..d7c1caff2428cb4fd71cae53114c8789eef8c092 100644 (file)
 #include "snmp_core.h"
 #include "snmp/Forwarder.h"
 
-
-/// dials snmpConnectionOpened call
-class SnmpListeningStartedDialer: public CallDialer,
-        public Ipc::StartListeningCb
-{
-public:
-    typedef void (*Handler)(const Comm::ConnectionPointer &conn, int errNo);
-    SnmpListeningStartedDialer(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)(conn, errNo); }
-
-public:
-    Handler handler;
-};
-
-
 static void snmpPortOpened(const Comm::ConnectionPointer &conn, int errNo);
 
-
 mib_tree_entry *mib_tree_head;
 mib_tree_entry *mib_tree_last;
 
@@ -308,7 +288,7 @@ snmpConnectionOpen(void)
     }
 
     AsyncCall::Pointer call = asyncCall(49, 2, "snmpIncomingConnectionOpened",
-                                        SnmpListeningStartedDialer(&snmpPortOpened));
+                                        Comm::UdpOpenDialer(&snmpPortOpened));
     Ipc::StartListening(SOCK_DGRAM, IPPROTO_UDP, snmpIncomingConn, Ipc::fdnInSnmpSocket, call);
 
     if (!Config.Addrs.snmp_outgoing.IsNoAddr()) {
@@ -325,7 +305,7 @@ snmpConnectionOpen(void)
             snmpOutgoingConn->local.SetIPv4();
         }
         AsyncCall::Pointer call = asyncCall(49, 2, "snmpOutgoingConnectionOpened",
-                                            SnmpListeningStartedDialer(&snmpPortOpened));
+                                            Comm::UdpOpenDialer(&snmpPortOpened));
         Ipc::StartListening(SOCK_DGRAM, IPPROTO_UDP, snmpOutgoingConn, Ipc::fdnOutSnmpSocket, call);
     } else {
         snmpOutgoingConn = snmpIncomingConn;