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