2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 05 Listener Socket Handler */
12 #include "anyp/PortCfg.h"
13 #include "base/TextException.h"
14 #include "client_db.h"
15 #include "comm/AcceptLimiter.h"
16 #include "comm/comm_internal.h"
17 #include "comm/Connection.h"
18 #include "comm/Loops.h"
19 #include "comm/TcpAcceptor.h"
20 #include "CommCalls.h"
21 #include "eui/Config.h"
25 #include "ip/Intercept.h"
26 #include "ip/QosConfig.h"
27 #include "MasterXaction.h"
28 #include "profiler/Profiler.h"
29 #include "SquidConfig.h"
30 #include "SquidTime.h"
31 #include "StatCounters.h"
34 #ifdef HAVE_NETINET_TCP_H
35 // required for accept_filter to build.
36 #include <netinet/tcp.h>
39 CBDATA_NAMESPACED_CLASS_INIT(Comm
, TcpAcceptor
);
41 Comm::TcpAcceptor::TcpAcceptor(const Comm::ConnectionPointer
&newConn
, const char *, const Subscription::Pointer
&aSub
) :
42 AsyncJob("Comm::TcpAcceptor"),
50 Comm::TcpAcceptor::TcpAcceptor(const AnyP::PortCfgPointer
&p
, const char *, const Subscription::Pointer
&aSub
) :
51 AsyncJob("Comm::TcpAcceptor"),
60 Comm::TcpAcceptor::subscribe(const Subscription::Pointer
&aSub
)
62 debugs(5, 5, HERE
<< status() << " AsyncCall Subscription: " << aSub
);
63 unsubscribe("subscription change");
68 Comm::TcpAcceptor::unsubscribe(const char *reason
)
70 debugs(5, 5, HERE
<< status() << " AsyncCall Subscription " << theCallSub
<< " removed: " << reason
);
75 Comm::TcpAcceptor::start()
77 debugs(5, 5, HERE
<< status() << " AsyncCall Subscription: " << theCallSub
);
79 Must(IsConnOpen(conn
));
85 // if no error so far start accepting connections.
87 SetSelect(conn
->fd
, COMM_SELECT_READ
, doAccept
, this, 0);
91 Comm::TcpAcceptor::doneAll() const
93 // stop when FD is closed
94 if (!IsConnOpen(conn
)) {
95 return AsyncJob::doneAll();
98 // stop when handlers are gone
99 if (theCallSub
== NULL
) {
100 return AsyncJob::doneAll();
103 // open FD with handlers...keep accepting.
108 Comm::TcpAcceptor::swanSong()
111 unsubscribe("swanSong");
112 if (IsConnOpen(conn
)) {
114 comm_remove_close_handler(conn
->fd
, closer_
);
119 AcceptLimiter::Instance().removeDead(this);
120 AsyncJob::swanSong();
124 Comm::TcpAcceptor::status() const
127 return "[nil connection]";
129 static char ipbuf
[MAX_IPSTRLEN
] = {'\0'};
130 if (ipbuf
[0] == '\0')
131 conn
->local
.toHostStr(ipbuf
, MAX_IPSTRLEN
);
135 buf
.appendf(" FD %d, %s",conn
->fd
, ipbuf
);
137 const char *jobStatus
= AsyncJob::status();
138 buf
.append(jobStatus
, strlen(jobStatus
));
140 return buf
.content();
144 * New-style listen and accept routines
146 * setListen simply registers our interest in an FD for listening.
147 * The constructor takes a callback to call when an FD has been
148 * accept()ed some time later.
151 Comm::TcpAcceptor::setListen()
154 if (listen(conn
->fd
, Squid_MaxFD
>> 2) < 0) {
156 debugs(50, DBG_CRITICAL
, "ERROR: listen(" << status() << ", " << (Squid_MaxFD
>> 2) << "): " << xstrerr(errcode
));
160 if (Config
.accept_filter
&& strcmp(Config
.accept_filter
, "none") != 0) {
161 #ifdef SO_ACCEPTFILTER
162 struct accept_filter_arg afa
;
163 bzero(&afa
, sizeof(afa
));
164 debugs(5, DBG_IMPORTANT
, "Installing accept filter '" << Config
.accept_filter
<< "' on " << conn
);
165 xstrncpy(afa
.af_name
, Config
.accept_filter
, sizeof(afa
.af_name
));
166 if (setsockopt(conn
->fd
, SOL_SOCKET
, SO_ACCEPTFILTER
, &afa
, sizeof(afa
)) < 0)
167 debugs(5, DBG_CRITICAL
, "WARNING: SO_ACCEPTFILTER '" << Config
.accept_filter
<< "': '" << xstrerror());
168 #elif defined(TCP_DEFER_ACCEPT)
170 if (strncmp(Config
.accept_filter
, "data=", 5) == 0)
171 seconds
= atoi(Config
.accept_filter
+ 5);
172 if (setsockopt(conn
->fd
, IPPROTO_TCP
, TCP_DEFER_ACCEPT
, &seconds
, sizeof(seconds
)) < 0)
173 debugs(5, DBG_CRITICAL
, "WARNING: TCP_DEFER_ACCEPT '" << Config
.accept_filter
<< "': '" << xstrerror());
175 debugs(5, DBG_CRITICAL
, "WARNING: accept_filter not supported on your OS");
181 // Set TOS if needed.
182 // To correctly implement TOS values on listening sockets, probably requires
183 // more work to inherit TOS values to created connection objects.
185 Ip::Qos::setSockTos(conn
, conn
->tos
)
188 Ip::Qos::setSockNfmark(conn
, conn
->nfmark
);
192 typedef CommCbMemFunT
<Comm::TcpAcceptor
, CommCloseCbParams
> Dialer
;
193 closer_
= JobCallback(5, 4, Dialer
, this, Comm::TcpAcceptor::handleClosure
);
194 comm_add_close_handler(conn
->fd
, closer_
);
197 /// called when listening descriptor is closed by an external force
198 /// such as clientHttpConnectionsClose()
200 Comm::TcpAcceptor::handleClosure(const CommCloseCbParams
&)
208 * This private callback is called whenever a filedescriptor is ready
209 * to dupe itself and fob off an accept()ed connection
211 * It will either do that accept operation. Or if there are not enough FD
212 * available to do the clone safely will push the listening FD into a list
213 * of deferred operations. The list gets kicked and the dupe/accept() actually
214 * done later when enough sockets become available.
217 Comm::TcpAcceptor::doAccept(int fd
, void *data
)
220 debugs(5, 2, HERE
<< "New connection on FD " << fd
);
223 TcpAcceptor
*afd
= static_cast<TcpAcceptor
*>(data
);
226 AcceptLimiter::Instance().defer(afd
);
230 SetSelect(fd
, COMM_SELECT_READ
, Comm::TcpAcceptor::doAccept
, afd
, 0);
232 } catch (const std::exception
&e
) {
233 fatalf("FATAL: error while accepting new client connection: %s\n", e
.what());
235 fatal("FATAL: error while accepting new client connection: [unknown]\n");
240 Comm::TcpAcceptor::okToAccept()
242 static time_t last_warn
= 0;
244 if (fdNFree() >= RESERVED_FD
)
247 if (last_warn
+ 15 < squid_curtime
) {
248 debugs(5, DBG_CRITICAL
, "WARNING! Your cache is running out of filedescriptors");
249 last_warn
= squid_curtime
;
256 Comm::TcpAcceptor::acceptOne()
259 * We don't worry about running low on FDs here. Instead,
260 * doAccept() will use AcceptLimiter if we reach the limit
264 /* Accept a new connection */
265 ConnectionPointer newConnDetails
= new Connection();
266 const Comm::Flag flag
= oldAccept(newConnDetails
);
268 /* Check for errors */
269 if (!newConnDetails
->isOpen()) {
271 if (flag
== Comm::NOMESSAGE
) {
272 /* register interest again */
273 debugs(5, 5, HERE
<< "try later: " << conn
<< " handler Subscription: " << theCallSub
);
274 SetSelect(conn
->fd
, COMM_SELECT_READ
, doAccept
, this, 0);
278 // A non-recoverable error; notify the caller */
279 debugs(5, 5, HERE
<< "non-recoverable error:" << status() << " handler Subscription: " << theCallSub
);
280 notify(flag
, newConnDetails
);
281 mustStop("Listener socket closed");
285 debugs(5, 5, HERE
<< "Listener: " << conn
<<
286 " accepted new connection " << newConnDetails
<<
287 " handler Subscription: " << theCallSub
);
288 notify(flag
, newConnDetails
);
292 Comm::TcpAcceptor::acceptNext()
294 Must(IsConnOpen(conn
));
295 debugs(5, 2, HERE
<< "connection on " << conn
);
300 Comm::TcpAcceptor::notify(const Comm::Flag flag
, const Comm::ConnectionPointer
&newConnDetails
) const
302 // listener socket handlers just abandon the port with Comm::ERR_CLOSING
303 // it should only happen when this object is deleted...
304 if (flag
== Comm::ERR_CLOSING
) {
308 if (theCallSub
!= NULL
) {
309 AsyncCall::Pointer call
= theCallSub
->callback();
310 CommAcceptCbParams
¶ms
= GetCommParams
<CommAcceptCbParams
>(call
);
311 params
.xaction
= new MasterXaction
;
312 params
.xaction
->squidPort
= listenPort_
;
313 params
.fd
= conn
->fd
;
314 params
.conn
= params
.xaction
->tcpClient
= newConnDetails
;
316 params
.xerrno
= errcode
;
317 ScheduleCallHere(call
);
322 * accept() and process
323 * Wait for an incoming connection on our listener socket.
325 * \retval Comm::OK success. details parameter filled.
326 * \retval Comm::NOMESSAGE attempted accept() but nothing useful came in.
327 * \retval Comm::COMM_ERROR an outright failure occured.
328 * Or if this client has too many connections already.
331 Comm::TcpAcceptor::oldAccept(Comm::ConnectionPointer
&details
)
333 PROF_start(comm_accept
);
334 ++statCounter
.syscalls
.sock
.accepts
;
336 struct addrinfo
*gai
= NULL
;
337 Ip::Address::InitAddr(gai
);
339 errcode
= 0; // reset local errno copy.
340 if ((sock
= accept(conn
->fd
, gai
->ai_addr
, &gai
->ai_addrlen
)) < 0) {
341 errcode
= errno
; // store last accept errno locally.
343 Ip::Address::FreeAddr(gai
);
345 PROF_stop(comm_accept
);
347 if (ignoreErrno(errno
)) {
348 debugs(50, 5, HERE
<< status() << ": " << xstrerror());
349 return Comm::NOMESSAGE
;
350 } else if (ENFILE
== errno
|| EMFILE
== errno
) {
351 debugs(50, 3, HERE
<< status() << ": " << xstrerror());
352 return Comm::COMM_ERROR
;
354 debugs(50, DBG_IMPORTANT
, HERE
<< status() << ": " << xstrerror());
355 return Comm::COMM_ERROR
;
361 details
->remote
= *gai
;
363 if ( Config
.client_ip_max_connections
>= 0) {
364 if (clientdbEstablished(details
->remote
, 0) > Config
.client_ip_max_connections
) {
365 debugs(50, DBG_IMPORTANT
, "WARNING: " << details
->remote
<< " attempting more than " << Config
.client_ip_max_connections
<< " connections.");
366 Ip::Address::FreeAddr(gai
);
367 PROF_stop(comm_accept
);
368 return Comm::COMM_ERROR
;
372 // lookup the local-end details of this new connection
373 Ip::Address::InitAddr(gai
);
374 details
->local
.setEmpty();
375 if (getsockname(sock
, gai
->ai_addr
, &gai
->ai_addrlen
) != 0) {
376 debugs(50, DBG_IMPORTANT
, "ERROR: getsockname() failed to locate local-IP on " << details
<< ": " << xstrerror());
377 Ip::Address::FreeAddr(gai
);
378 PROF_stop(comm_accept
);
379 return Comm::COMM_ERROR
;
381 details
->local
= *gai
;
382 Ip::Address::FreeAddr(gai
);
385 // XXX : these are not all HTTP requests. use a note about type and ip:port details->
386 // so we end up with a uniform "(HTTP|FTP-data|HTTPS|...) remote-ip:remote-port"
387 fd_open(sock
, FD_SOCKET
, "HTTP Request");
389 fdd_table
[sock
].close_file
= NULL
;
390 fdd_table
[sock
].close_line
= 0;
392 fde
*F
= &fd_table
[sock
];
393 details
->remote
.toStr(F
->ipaddr
,MAX_IPSTRLEN
);
394 F
->remote_port
= details
->remote
.port();
395 F
->local_addr
= details
->local
;
396 F
->sock_family
= details
->local
.isIPv6()?AF_INET6
:AF_INET
;
399 commSetCloseOnExec(sock
);
400 commSetNonBlocking(sock
);
402 /* IFF the socket is (tproxy) transparent, pass the flag down to allow spoofing */
403 F
->flags
.transparent
= fd_table
[conn
->fd
].flags
.transparent
; // XXX: can we remove this line yet?
405 // Perform NAT or TPROXY operations to retrieve the real client/dest IP addresses
406 if (conn
->flags
&(COMM_TRANSPARENT
|COMM_INTERCEPTION
) && !Ip::Interceptor
.Lookup(details
, conn
)) {
407 debugs(50, DBG_IMPORTANT
, "ERROR: NAT/TPROXY lookup failed to locate original IPs on " << details
);
409 PROF_stop(comm_accept
);
410 return Comm::COMM_ERROR
;
414 if (Eui::TheConfig
.euiLookup
) {
415 if (details
->remote
.isIPv4()) {
416 details
->remoteEui48
.lookup(details
->remote
);
417 } else if (details
->remote
.isIPv6()) {
418 details
->remoteEui64
.lookup(details
->remote
);
423 PROF_stop(comm_accept
);