]> git.ipfire.org Git - thirdparty/squid.git/blame - src/comm/TcpAcceptor.cc
Bug 2179: debug_options ALL,1 3,9 83,9 has no effect
[thirdparty/squid.git] / src / comm / TcpAcceptor.cc
CommitLineData
04f55905 1/*
b510f3a1 2 * DEBUG: section 05 Listener Socket Handler
04f55905
AJ
3 * AUTHOR: Harvest Derived
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 *
32 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
33 */
34
582c2af2 35#include "squid.h"
a9870624 36#include "base/TextException.h"
95e6d864 37#include "client_db.h"
04f55905 38#include "comm/AcceptLimiter.h"
582c2af2 39#include "CommCalls.h"
04f55905 40#include "comm/comm_internal.h"
5b67dfa4 41#include "comm/Connection.h"
d841c88d 42#include "comm/Loops.h"
cbff89ba 43#include "comm/TcpAcceptor.h"
c4ad1349 44#include "fd.h"
04f55905 45#include "fde.h"
67679543 46#include "globals.h"
40d34a62 47#include "ip/Intercept.h"
582c2af2 48#include "profiler/Profiler.h"
4d5904f7 49#include "SquidConfig.h"
04f55905 50#include "SquidTime.h"
e4f1fdae 51#include "StatCounters.h"
04f55905 52
21d845b1
FC
53#if HAVE_ERRNO_H
54#include <errno.h>
55#endif
f842580f
AJ
56#ifdef HAVE_NETINET_TCP_H
57// required for accept_filter to build.
58#include <netinet/tcp.h>
59#endif
21d845b1 60
f842580f 61CBDATA_NAMESPACED_CLASS_INIT(Comm, TcpAcceptor);
a9870624 62
8bbb16e3 63Comm::TcpAcceptor::TcpAcceptor(const Comm::ConnectionPointer &newConn, const char *note, const Subscription::Pointer &aSub) :
cbff89ba 64 AsyncJob("Comm::TcpAcceptor"),
0ba55a12
AJ
65 errcode(0),
66 isLimited(0),
5b67dfa4
AJ
67 theCallSub(aSub),
68 conn(newConn)
cbff89ba 69{}
0ba55a12
AJ
70
71void
cbff89ba 72Comm::TcpAcceptor::subscribe(const Subscription::Pointer &aSub)
0ba55a12 73{
cbff89ba 74 debugs(5, 5, HERE << status() << " AsyncCall Subscription: " << aSub);
5b67dfa4
AJ
75 unsubscribe("subscription change");
76 theCallSub = aSub;
4c5518e5
AJ
77}
78
0ba55a12 79void
cbff89ba 80Comm::TcpAcceptor::unsubscribe(const char *reason)
0ba55a12 81{
cbff89ba 82 debugs(5, 5, HERE << status() << " AsyncCall Subscription " << theCallSub << " removed: " << reason);
5b67dfa4 83 theCallSub = NULL;
0ba55a12
AJ
84}
85
a9870624 86void
cbff89ba 87Comm::TcpAcceptor::start()
a9870624 88{
cbff89ba 89 debugs(5, 5, HERE << status() << " AsyncCall Subscription: " << theCallSub);
a9870624
AJ
90
91 Must(IsConnOpen(conn));
92
93 setListen();
94
95 // if no error so far start accepting connections.
96 if (errcode == 0)
8bbb16e3 97 SetSelect(conn->fd, COMM_SELECT_READ, doAccept, this, 0);
a9870624
AJ
98}
99
100bool
cbff89ba 101Comm::TcpAcceptor::doneAll() const
a9870624 102{
6f8536c0 103 // stop when FD is closed
a9870624 104 if (!IsConnOpen(conn)) {
a9870624
AJ
105 return AsyncJob::doneAll();
106 }
107
5b67dfa4
AJ
108 // stop when handlers are gone
109 if (theCallSub == NULL) {
a9870624
AJ
110 return AsyncJob::doneAll();
111 }
112
5b67dfa4 113 // open FD with handlers...keep accepting.
a9870624
AJ
114 return false;
115}
116
117void
cbff89ba 118Comm::TcpAcceptor::swanSong()
a9870624
AJ
119{
120 debugs(5,5, HERE);
5b67dfa4 121 unsubscribe("swanSong");
a9870624 122 conn = NULL;
5b67dfa4 123 AcceptLimiter::Instance().removeDead(this);
a9870624
AJ
124 AsyncJob::swanSong();
125}
126
cbff89ba
AJ
127const char *
128Comm::TcpAcceptor::status() const
129{
8bbb16e3
AJ
130 if (conn == NULL)
131 return "[nil connection]";
132
cbff89ba
AJ
133 static char ipbuf[MAX_IPSTRLEN] = {'\0'};
134 if (ipbuf[0] == '\0')
8bbb16e3 135 conn->local.ToHostname(ipbuf, MAX_IPSTRLEN);
cbff89ba
AJ
136
137 static MemBuf buf;
138 buf.reset();
8bbb16e3 139 buf.Printf(" FD %d, %s",conn->fd, ipbuf);
cbff89ba
AJ
140
141 const char *jobStatus = AsyncJob::status();
142 buf.append(jobStatus, strlen(jobStatus));
143
144 return buf.content();
145}
146
0ba55a12
AJ
147/**
148 * New-style listen and accept routines
149 *
150 * setListen simply registers our interest in an FD for listening.
151 * The constructor takes a callback to call when an FD has been
152 * accept()ed some time later.
153 */
154void
cbff89ba 155Comm::TcpAcceptor::setListen()
0ba55a12
AJ
156{
157 errcode = 0; // reset local errno copy.
a9870624 158 if (listen(conn->fd, Squid_MaxFD >> 2) < 0) {
cbff89ba 159 debugs(50, DBG_CRITICAL, "ERROR: listen(" << status() << ", " << (Squid_MaxFD >> 2) << "): " << xstrerror());
0ba55a12
AJ
160 errcode = errno;
161 return;
162 }
163
164 if (Config.accept_filter && strcmp(Config.accept_filter, "none") != 0) {
165#ifdef SO_ACCEPTFILTER
166 struct accept_filter_arg afa;
167 bzero(&afa, sizeof(afa));
5b67dfa4 168 debugs(5, DBG_IMPORTANT, "Installing accept filter '" << Config.accept_filter << "' on " << conn);
0ba55a12 169 xstrncpy(afa.af_name, Config.accept_filter, sizeof(afa.af_name));
a9870624 170 if (setsockopt(conn->fd, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa)) < 0)
5b67dfa4 171 debugs(5, DBG_CRITICAL, "WARNING: SO_ACCEPTFILTER '" << Config.accept_filter << "': '" << xstrerror());
0ba55a12
AJ
172#elif defined(TCP_DEFER_ACCEPT)
173 int seconds = 30;
174 if (strncmp(Config.accept_filter, "data=", 5) == 0)
175 seconds = atoi(Config.accept_filter + 5);
a9870624 176 if (setsockopt(conn->fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &seconds, sizeof(seconds)) < 0)
5b67dfa4 177 debugs(5, DBG_CRITICAL, "WARNING: TCP_DEFER_ACCEPT '" << Config.accept_filter << "': '" << xstrerror());
0ba55a12 178#else
5b67dfa4 179 debugs(5, DBG_CRITICAL, "WARNING: accept_filter not supported on your OS");
0ba55a12
AJ
180#endif
181 }
04f55905
AJ
182}
183
184/**
185 * This private callback is called whenever a filedescriptor is ready
186 * to dupe itself and fob off an accept()ed connection
187 *
188 * It will either do that accept operation. Or if there are not enough FD
189 * available to do the clone safely will push the listening FD into a list
190 * of deferred operations. The list gets kicked and the dupe/accept() actually
191 * done later when enough sockets become available.
192 */
193void
cbff89ba 194Comm::TcpAcceptor::doAccept(int fd, void *data)
04f55905 195{
5b67dfa4
AJ
196 try {
197 debugs(5, 2, HERE << "New connection on FD " << fd);
04f55905 198
5b67dfa4 199 Must(isOpen(fd));
cbff89ba 200 TcpAcceptor *afd = static_cast<TcpAcceptor*>(data);
04f55905 201
5b67dfa4
AJ
202 if (!okToAccept()) {
203 AcceptLimiter::Instance().defer(afd);
204 } else {
205 afd->acceptNext();
206 }
cbff89ba 207 SetSelect(fd, COMM_SELECT_READ, Comm::TcpAcceptor::doAccept, afd, 0);
5b67dfa4 208
db98b2bd 209 } catch (const std::exception &e) {
cbff89ba 210 fatalf("FATAL: error while accepting new client connection: %s\n", e.what());
db98b2bd 211 } catch (...) {
5b67dfa4 212 fatal("FATAL: error while accepting new client connection: [unkown]\n");
04f55905 213 }
04f55905
AJ
214}
215
216bool
cbff89ba 217Comm::TcpAcceptor::okToAccept()
04f55905
AJ
218{
219 static time_t last_warn = 0;
220
221 if (fdNFree() >= RESERVED_FD)
222 return true;
223
224 if (last_warn + 15 < squid_curtime) {
225 debugs(5, DBG_CRITICAL, "WARNING! Your cache is running out of filedescriptors");
226 last_warn = squid_curtime;
227 }
228
229 return false;
230}
231
971581ee 232void
cbff89ba 233Comm::TcpAcceptor::acceptOne()
04f55905
AJ
234{
235 /*
236 * We don't worry about running low on FDs here. Instead,
237 * doAccept() will use AcceptLimiter if we reach the limit
238 * there.
239 */
240
241 /* Accept a new connection */
5b67dfa4 242 ConnectionPointer newConnDetails = new Connection();
8bbb16e3 243 const comm_err_t flag = oldAccept(newConnDetails);
04f55905
AJ
244
245 /* Check for errors */
5b67dfa4 246 if (!newConnDetails->isOpen()) {
04f55905 247
cbff89ba 248 if (flag == COMM_NOMESSAGE) {
04f55905 249 /* register interest again */
6f8536c0 250 debugs(5, 5, HERE << "try later: " << conn << " handler Subscription: " << theCallSub);
8bbb16e3 251 SetSelect(conn->fd, COMM_SELECT_READ, doAccept, this, 0);
971581ee 252 return;
04f55905
AJ
253 }
254
255 // A non-recoverable error; notify the caller */
cbff89ba 256 debugs(5, 5, HERE << "non-recoverable error:" << status() << " handler Subscription: " << theCallSub);
8bbb16e3 257 notify(flag, newConnDetails);
5b67dfa4 258 mustStop("Listener socket closed");
971581ee 259 return;
04f55905
AJ
260 }
261
5b67dfa4
AJ
262 debugs(5, 5, HERE << "Listener: " << conn <<
263 " accepted new connection " << newConnDetails <<
6f8536c0 264 " handler Subscription: " << theCallSub);
8bbb16e3 265 notify(flag, newConnDetails);
04f55905
AJ
266}
267
268void
cbff89ba 269Comm::TcpAcceptor::acceptNext()
04f55905 270{
a9870624 271 Must(IsConnOpen(conn));
5b67dfa4 272 debugs(5, 2, HERE << "connection on " << conn);
971581ee 273 acceptOne();
04f55905
AJ
274}
275
276void
8bbb16e3 277Comm::TcpAcceptor::notify(const comm_err_t flag, const Comm::ConnectionPointer &newConnDetails) const
04f55905
AJ
278{
279 // listener socket handlers just abandon the port with COMM_ERR_CLOSING
280 // it should only happen when this object is deleted...
1fc32b95 281 if (flag == COMM_ERR_CLOSING) {
04f55905
AJ
282 return;
283 }
284
5b67dfa4
AJ
285 if (theCallSub != NULL) {
286 AsyncCall::Pointer call = theCallSub->callback();
287 CommAcceptCbParams &params = GetCommParams<CommAcceptCbParams>(call);
a9870624 288 params.fd = conn->fd;
5b67dfa4 289 params.conn = newConnDetails;
0ba55a12
AJ
290 params.flag = flag;
291 params.xerrno = errcode;
292 ScheduleCallHere(call);
0ba55a12 293 }
04f55905
AJ
294}
295
296/**
97b8ac39 297 * accept() and process
5b67dfa4
AJ
298 * Wait for an incoming connection on our listener socket.
299 *
300 * \retval COMM_OK success. details parameter filled.
301 * \retval COMM_NOMESSAGE attempted accept() but nothing useful came in.
302 * \retval COMM_ERROR an outright failure occured.
303 * Or if this client has too many connections already.
273f66c4 304 */
5b67dfa4 305comm_err_t
8bbb16e3 306Comm::TcpAcceptor::oldAccept(Comm::ConnectionPointer &details)
04f55905
AJ
307{
308 PROF_start(comm_accept);
e4f1fdae 309 ++statCounter.syscalls.sock.accepts;
04f55905
AJ
310 int sock;
311 struct addrinfo *gai = NULL;
5b67dfa4 312 details->local.InitAddrInfo(gai);
04f55905 313
1fc32b95 314 errcode = 0; // reset local errno copy.
a9870624 315 if ((sock = accept(conn->fd, gai->ai_addr, &gai->ai_addrlen)) < 0) {
1fc32b95 316 errcode = errno; // store last accept errno locally.
04f55905 317
5b67dfa4 318 details->local.FreeAddrInfo(gai);
04f55905
AJ
319
320 PROF_stop(comm_accept);
321
322 if (ignoreErrno(errno)) {
cbff89ba 323 debugs(50, 5, HERE << status() << ": " << xstrerror());
04f55905
AJ
324 return COMM_NOMESSAGE;
325 } else if (ENFILE == errno || EMFILE == errno) {
cbff89ba 326 debugs(50, 3, HERE << status() << ": " << xstrerror());
04f55905
AJ
327 return COMM_ERROR;
328 } else {
e0236918 329 debugs(50, DBG_IMPORTANT, HERE << status() << ": " << xstrerror());
04f55905
AJ
330 return COMM_ERROR;
331 }
332 }
333
a9870624 334 Must(sock >= 0);
5b67dfa4
AJ
335 details->fd = sock;
336 details->remote = *gai;
04f55905 337
5511c78a 338 if ( Config.client_ip_max_connections >= 0) {
5b67dfa4
AJ
339 if (clientdbEstablished(details->remote, 0) > Config.client_ip_max_connections) {
340 debugs(50, DBG_IMPORTANT, "WARNING: " << details->remote << " attempting more than " << Config.client_ip_max_connections << " connections.");
341 details->local.FreeAddrInfo(gai);
5511c78a
AJ
342 return COMM_ERROR;
343 }
344 }
345
903198a7 346 // lookup the local-end details of this new connection
5b67dfa4
AJ
347 details->local.InitAddrInfo(gai);
348 details->local.SetEmpty();
e42281f9
AJ
349 if (getsockname(sock, gai->ai_addr, &gai->ai_addrlen) != 0) {
350 debugs(50, DBG_IMPORTANT, "ERROR: getsockname() failed to locate local-IP on " << details << ": " << xstrerror());
351 details->local.FreeAddrInfo(gai);
352 return COMM_ERROR;
353 }
5b67dfa4
AJ
354 details->local = *gai;
355 details->local.FreeAddrInfo(gai);
04f55905
AJ
356
357 /* fdstat update */
5b67dfa4 358 // XXX : these are not all HTTP requests. use a note about type and ip:port details->
903198a7 359 // so we end up with a uniform "(HTTP|FTP-data|HTTPS|...) remote-ip:remote-port"
04f55905
AJ
360 fd_open(sock, FD_SOCKET, "HTTP Request");
361
362 fdd_table[sock].close_file = NULL;
363 fdd_table[sock].close_line = 0;
364
365 fde *F = &fd_table[sock];
5b67dfa4
AJ
366 details->remote.NtoA(F->ipaddr,MAX_IPSTRLEN);
367 F->remote_port = details->remote.GetPort();
368 F->local_addr = details->local;
369 F->sock_family = details->local.IsIPv6()?AF_INET6:AF_INET;
04f55905 370
903198a7
AJ
371 // set socket flags
372 commSetCloseOnExec(sock);
04f55905
AJ
373 commSetNonBlocking(sock);
374
375 /* IFF the socket is (tproxy) transparent, pass the flag down to allow spoofing */
40d34a62
AJ
376 F->flags.transparent = fd_table[conn->fd].flags.transparent; // XXX: can we remove this line yet?
377
378 // Perform NAT or TPROXY operations to retrieve the real client/dest IP addresses
379 if (conn->flags&(COMM_TRANSPARENT|COMM_INTERCEPTION) && !Ip::Interceptor.Lookup(details, conn)) {
380 // Failed.
381 return COMM_ERROR;
382 }
04f55905
AJ
383
384 PROF_stop(comm_accept);
5b67dfa4 385 return COMM_OK;
04f55905 386}