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