]> git.ipfire.org Git - thirdparty/squid.git/blame_incremental - src/comm/TcpAcceptor.h
Simplify appending SBuf to String (#2108)
[thirdparty/squid.git] / src / comm / TcpAcceptor.h
... / ...
CommitLineData
1/*
2 * Copyright (C) 1996-2025 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_SRC_COMM_TCPACCEPTOR_H
10#define SQUID_SRC_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
19class CommCloseCbParams;
20
21namespace Comm
22{
23
24class 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 */
38class TcpAcceptor : public AsyncJob
39{
40 CBDATA_CHILD(TcpAcceptor);
41
42public:
43 typedef CbcPointer<Comm::TcpAcceptor> Pointer;
44
45private:
46 void start() override;
47 bool doneAll() const override;
48 void swanSong() override;
49 const char *status() const override;
50
51 TcpAcceptor(const TcpAcceptor &); // not implemented.
52
53public:
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
57protected:
58 /** Subscribe a handler to receive calls back about new connections.
59 * Unsubscribes any existing subscribed handler.
60 */
61 void subscribe(const Subscription::Pointer &aSub);
62
63 /** Remove the currently waiting callback subscription.
64 * Already scheduled callbacks remain scheduled.
65 */
66 void unsubscribe(const char *reason);
67
68 /** Try and accept another connection (synchronous).
69 * If one is pending already the subscribed callback handler will be scheduled
70 * to handle it before this method returns.
71 */
72 void acceptNext();
73
74 /// Call the subscribed callback handler with details about a new connection.
75 void notify(const Comm::Flag flag, const Comm::ConnectionPointer &details) const;
76
77 /// errno code of the last accept() or listen() action if one occurred.
78 int errcode;
79
80 /// Method to test if there are enough file descriptors to open a new client connection
81 /// if not the accept() will be postponed
82 static bool okToAccept();
83
84 friend class AcceptLimiter;
85
86private:
87 Subscription::Pointer theCallSub; ///< used to generate AsyncCalls handling our events.
88
89 /// conn being listened on for new connections
90 /// Reserved for read-only use.
91 ConnectionPointer conn;
92
93 /// configuration details of the listening port (if provided)
94 AnyP::PortCfgPointer listenPort_;
95
96 /// listen socket closure handler
97 AsyncCall::Pointer closer_;
98
99 /// Method callback for whenever an FD is ready to accept a client connection.
100 static void doAccept(int fd, void *data);
101
102 void acceptOne();
103 bool acceptInto(Comm::ConnectionPointer &);
104 void setListen();
105 void handleClosure(const CommCloseCbParams &io);
106 /// whether we are listening on one of the squid.conf *ports
107 bool intendedForUserConnections() const { return bool(listenPort_); }
108 void logAcceptError(const ConnectionPointer &tcpClient) const;
109};
110
111} // namespace Comm
112
113#endif /* SQUID_SRC_COMM_TCPACCEPTOR_H */
114