]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/TcpAcceptor.h
Merged from trunk
[thirdparty/squid.git] / src / comm / TcpAcceptor.h
1 #ifndef SQUID_COMM_TCPACCEPTOR_H
2 #define SQUID_COMM_TCPACCEPTOR_H
3
4 #include "anyp/forward.h"
5 #include "base/AsyncJob.h"
6 #include "base/CbcPointer.h"
7 #include "base/Subscription.h"
8 #include "comm/Flag.h"
9 #include "comm/forward.h"
10
11 class CommCloseCbParams;
12
13 namespace Comm
14 {
15
16 class AcceptLimiter;
17
18 /**
19 * Listens on a Comm::Connection for new incoming connections and
20 * emits an active Comm::Connection descriptor for the new client.
21 *
22 * Handles all event limiting required to quash inbound connection
23 * floods within the global FD limits of available Squid_MaxFD and
24 * client_ip_max_connections.
25 *
26 * Fills the emitted connection with all connection details able to
27 * be looked up. Currently these are the local/remote IP:port details
28 * and the listening socket transparent-mode flag.
29 */
30 class TcpAcceptor : public AsyncJob
31 {
32 public:
33 typedef CbcPointer<Comm::TcpAcceptor> Pointer;
34
35 private:
36 virtual void start();
37 virtual bool doneAll() const;
38 virtual void swanSong();
39 virtual const char *status() const;
40
41 TcpAcceptor(const TcpAcceptor &); // not implemented.
42
43 public:
44 TcpAcceptor(const Comm::ConnectionPointer &conn, const char *note, const Subscription::Pointer &aSub);
45 TcpAcceptor(const AnyP::PortCfgPointer &listenPort, const char *note, const Subscription::Pointer &aSub);
46
47 /** Subscribe a handler to receive calls back about new connections.
48 * Unsubscribes any existing subscribed handler.
49 */
50 void subscribe(const Subscription::Pointer &aSub);
51
52 /** Remove the currently waiting callback subscription.
53 * Already scheduled callbacks remain scheduled.
54 */
55 void unsubscribe(const char *reason);
56
57 /** Try and accept another connection (synchronous).
58 * If one is pending already the subscribed callback handler will be scheduled
59 * to handle it before this method returns.
60 */
61 void acceptNext();
62
63 /// Call the subscribed callback handler with details about a new connection.
64 void notify(const Comm::Flag flag, const Comm::ConnectionPointer &details) const;
65
66 /// errno code of the last accept() or listen() action if one occurred.
67 int errcode;
68
69 protected:
70 friend class AcceptLimiter;
71 int32_t isLimited; ///< whether this socket is delayed and on the AcceptLimiter queue.
72
73 private:
74 Subscription::Pointer theCallSub; ///< used to generate AsyncCalls handling our events.
75
76 /// conn being listened on for new connections
77 /// Reserved for read-only use.
78 ConnectionPointer conn;
79
80 /// configuration details of the listening port (if provided)
81 AnyP::PortCfgPointer listenPort_;
82
83 /// listen socket closure handler
84 AsyncCall::Pointer closer_;
85
86 /// Method to test if there are enough file descriptors to open a new client connection
87 /// if not the accept() will be postponed
88 static bool okToAccept();
89
90 /// Method callback for whenever an FD is ready to accept a client connection.
91 static void doAccept(int fd, void *data);
92
93 void acceptOne();
94 Comm::Flag oldAccept(Comm::ConnectionPointer &details);
95 void setListen();
96 void handleClosure(const CommCloseCbParams &io);
97
98 CBDATA_CLASS2(TcpAcceptor);
99 };
100
101 } // namespace Comm
102
103 #endif /* SQUID_COMM_TCPACCEPTOR_H */