From: Amos Jeffries Date: Thu, 29 Mar 2012 09:22:41 +0000 (+1200) Subject: Polish: de-duplicate UDP port dialers X-Git-Tag: BumpSslServerFirst.take08~7^2~32 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=09cee7438e78a60401a69e6256f851ad433ca5be;p=thirdparty%2Fsquid.git Polish: de-duplicate UDP port dialers 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. --- diff --git a/src/comm/Makefile.am b/src/comm/Makefile.am index 0ba83c14ad..42ff5a4e4f 100644 --- a/src/comm/Makefile.am +++ b/src/comm/Makefile.am @@ -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 index 0000000000..ed72b8ff06 --- /dev/null +++ b/src/comm/UdpOpenDialer.h @@ -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 */ diff --git a/src/htcp.cc b/src/htcp.cc index b243ee55e5..4466443016 100644 --- a/src/htcp.cc +++ b/src/htcp.cc @@ -39,11 +39,11 @@ #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" @@ -52,22 +52,6 @@ #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); } } diff --git a/src/icp_v2.cc b/src/icp_v2.cc index 436f36df84..596ad17055 100644 --- a/src/icp_v2.cc +++ b/src/icp_v2.cc @@ -38,9 +38,10 @@ #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" @@ -52,28 +53,10 @@ #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); } } diff --git a/src/snmp_core.cc b/src/snmp_core.cc index 87b6dc0308..d7c1caff24 100644 --- a/src/snmp_core.cc +++ b/src/snmp_core.cc @@ -41,28 +41,8 @@ #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;