]>
Commit | Line | Data |
---|---|---|
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" |
04f55905 | 49 | #include "SquidTime.h" |
e4f1fdae | 50 | #include "StatCounters.h" |
04f55905 | 51 | |
21d845b1 FC |
52 | #if HAVE_ERRNO_H |
53 | #include <errno.h> | |
54 | #endif | |
55 | ||
db98b2bd A |
56 | namespace Comm |
57 | { | |
58 | CBDATA_CLASS_INIT(TcpAcceptor); | |
a9870624 AJ |
59 | }; |
60 | ||
8bbb16e3 | 61 | Comm::TcpAcceptor::TcpAcceptor(const Comm::ConnectionPointer &newConn, const char *note, const Subscription::Pointer &aSub) : |
cbff89ba | 62 | AsyncJob("Comm::TcpAcceptor"), |
0ba55a12 AJ |
63 | errcode(0), |
64 | isLimited(0), | |
5b67dfa4 AJ |
65 | theCallSub(aSub), |
66 | conn(newConn) | |
cbff89ba | 67 | {} |
0ba55a12 AJ |
68 | |
69 | void | |
cbff89ba | 70 | Comm::TcpAcceptor::subscribe(const Subscription::Pointer &aSub) |
0ba55a12 | 71 | { |
cbff89ba | 72 | debugs(5, 5, HERE << status() << " AsyncCall Subscription: " << aSub); |
5b67dfa4 AJ |
73 | unsubscribe("subscription change"); |
74 | theCallSub = aSub; | |
4c5518e5 AJ |
75 | } |
76 | ||
0ba55a12 | 77 | void |
cbff89ba | 78 | Comm::TcpAcceptor::unsubscribe(const char *reason) |
0ba55a12 | 79 | { |
cbff89ba | 80 | debugs(5, 5, HERE << status() << " AsyncCall Subscription " << theCallSub << " removed: " << reason); |
5b67dfa4 | 81 | theCallSub = NULL; |
0ba55a12 AJ |
82 | } |
83 | ||
a9870624 | 84 | void |
cbff89ba | 85 | Comm::TcpAcceptor::start() |
a9870624 | 86 | { |
cbff89ba | 87 | debugs(5, 5, HERE << status() << " AsyncCall Subscription: " << theCallSub); |
a9870624 AJ |
88 | |
89 | Must(IsConnOpen(conn)); | |
90 | ||
91 | setListen(); | |
92 | ||
93 | // if no error so far start accepting connections. | |
94 | if (errcode == 0) | |
8bbb16e3 | 95 | SetSelect(conn->fd, COMM_SELECT_READ, doAccept, this, 0); |
a9870624 AJ |
96 | } |
97 | ||
98 | bool | |
cbff89ba | 99 | Comm::TcpAcceptor::doneAll() const |
a9870624 | 100 | { |
6f8536c0 | 101 | // stop when FD is closed |
a9870624 | 102 | if (!IsConnOpen(conn)) { |
a9870624 AJ |
103 | return AsyncJob::doneAll(); |
104 | } | |
105 | ||
5b67dfa4 AJ |
106 | // stop when handlers are gone |
107 | if (theCallSub == NULL) { | |
a9870624 AJ |
108 | return AsyncJob::doneAll(); |
109 | } | |
110 | ||
5b67dfa4 | 111 | // open FD with handlers...keep accepting. |
a9870624 AJ |
112 | return false; |
113 | } | |
114 | ||
115 | void | |
cbff89ba | 116 | Comm::TcpAcceptor::swanSong() |
a9870624 AJ |
117 | { |
118 | debugs(5,5, HERE); | |
5b67dfa4 | 119 | unsubscribe("swanSong"); |
a9870624 | 120 | conn = NULL; |
5b67dfa4 | 121 | AcceptLimiter::Instance().removeDead(this); |
a9870624 AJ |
122 | AsyncJob::swanSong(); |
123 | } | |
124 | ||
cbff89ba AJ |
125 | const char * |
126 | Comm::TcpAcceptor::status() const | |
127 | { | |
8bbb16e3 AJ |
128 | if (conn == NULL) |
129 | return "[nil connection]"; | |
130 | ||
cbff89ba AJ |
131 | static char ipbuf[MAX_IPSTRLEN] = {'\0'}; |
132 | if (ipbuf[0] == '\0') | |
8bbb16e3 | 133 | conn->local.ToHostname(ipbuf, MAX_IPSTRLEN); |
cbff89ba AJ |
134 | |
135 | static MemBuf buf; | |
136 | buf.reset(); | |
8bbb16e3 | 137 | buf.Printf(" FD %d, %s",conn->fd, ipbuf); |
cbff89ba AJ |
138 | |
139 | const char *jobStatus = AsyncJob::status(); | |
140 | buf.append(jobStatus, strlen(jobStatus)); | |
141 | ||
142 | return buf.content(); | |
143 | } | |
144 | ||
0ba55a12 AJ |
145 | /** |
146 | * New-style listen and accept routines | |
147 | * | |
148 | * setListen simply registers our interest in an FD for listening. | |
149 | * The constructor takes a callback to call when an FD has been | |
150 | * accept()ed some time later. | |
151 | */ | |
152 | void | |
cbff89ba | 153 | Comm::TcpAcceptor::setListen() |
0ba55a12 AJ |
154 | { |
155 | errcode = 0; // reset local errno copy. | |
a9870624 | 156 | if (listen(conn->fd, Squid_MaxFD >> 2) < 0) { |
cbff89ba | 157 | debugs(50, DBG_CRITICAL, "ERROR: listen(" << status() << ", " << (Squid_MaxFD >> 2) << "): " << xstrerror()); |
0ba55a12 AJ |
158 | errcode = errno; |
159 | return; | |
160 | } | |
161 | ||
162 | if (Config.accept_filter && strcmp(Config.accept_filter, "none") != 0) { | |
163 | #ifdef SO_ACCEPTFILTER | |
164 | struct accept_filter_arg afa; | |
165 | bzero(&afa, sizeof(afa)); | |
5b67dfa4 | 166 | debugs(5, DBG_IMPORTANT, "Installing accept filter '" << Config.accept_filter << "' on " << conn); |
0ba55a12 | 167 | xstrncpy(afa.af_name, Config.accept_filter, sizeof(afa.af_name)); |
a9870624 | 168 | if (setsockopt(conn->fd, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa)) < 0) |
5b67dfa4 | 169 | debugs(5, DBG_CRITICAL, "WARNING: SO_ACCEPTFILTER '" << Config.accept_filter << "': '" << xstrerror()); |
0ba55a12 AJ |
170 | #elif defined(TCP_DEFER_ACCEPT) |
171 | int seconds = 30; | |
172 | if (strncmp(Config.accept_filter, "data=", 5) == 0) | |
173 | seconds = atoi(Config.accept_filter + 5); | |
a9870624 | 174 | if (setsockopt(conn->fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &seconds, sizeof(seconds)) < 0) |
5b67dfa4 | 175 | debugs(5, DBG_CRITICAL, "WARNING: TCP_DEFER_ACCEPT '" << Config.accept_filter << "': '" << xstrerror()); |
0ba55a12 | 176 | #else |
5b67dfa4 | 177 | debugs(5, DBG_CRITICAL, "WARNING: accept_filter not supported on your OS"); |
0ba55a12 AJ |
178 | #endif |
179 | } | |
04f55905 AJ |
180 | } |
181 | ||
182 | /** | |
183 | * This private callback is called whenever a filedescriptor is ready | |
184 | * to dupe itself and fob off an accept()ed connection | |
185 | * | |
186 | * It will either do that accept operation. Or if there are not enough FD | |
187 | * available to do the clone safely will push the listening FD into a list | |
188 | * of deferred operations. The list gets kicked and the dupe/accept() actually | |
189 | * done later when enough sockets become available. | |
190 | */ | |
191 | void | |
cbff89ba | 192 | Comm::TcpAcceptor::doAccept(int fd, void *data) |
04f55905 | 193 | { |
5b67dfa4 AJ |
194 | try { |
195 | debugs(5, 2, HERE << "New connection on FD " << fd); | |
04f55905 | 196 | |
5b67dfa4 | 197 | Must(isOpen(fd)); |
cbff89ba | 198 | TcpAcceptor *afd = static_cast<TcpAcceptor*>(data); |
04f55905 | 199 | |
5b67dfa4 AJ |
200 | if (!okToAccept()) { |
201 | AcceptLimiter::Instance().defer(afd); | |
202 | } else { | |
203 | afd->acceptNext(); | |
204 | } | |
cbff89ba | 205 | SetSelect(fd, COMM_SELECT_READ, Comm::TcpAcceptor::doAccept, afd, 0); |
5b67dfa4 | 206 | |
db98b2bd | 207 | } catch (const std::exception &e) { |
cbff89ba | 208 | fatalf("FATAL: error while accepting new client connection: %s\n", e.what()); |
db98b2bd | 209 | } catch (...) { |
5b67dfa4 | 210 | fatal("FATAL: error while accepting new client connection: [unkown]\n"); |
04f55905 | 211 | } |
04f55905 AJ |
212 | } |
213 | ||
214 | bool | |
cbff89ba | 215 | Comm::TcpAcceptor::okToAccept() |
04f55905 AJ |
216 | { |
217 | static time_t last_warn = 0; | |
218 | ||
219 | if (fdNFree() >= RESERVED_FD) | |
220 | return true; | |
221 | ||
222 | if (last_warn + 15 < squid_curtime) { | |
223 | debugs(5, DBG_CRITICAL, "WARNING! Your cache is running out of filedescriptors"); | |
224 | last_warn = squid_curtime; | |
225 | } | |
226 | ||
227 | return false; | |
228 | } | |
229 | ||
971581ee | 230 | void |
cbff89ba | 231 | Comm::TcpAcceptor::acceptOne() |
04f55905 AJ |
232 | { |
233 | /* | |
234 | * We don't worry about running low on FDs here. Instead, | |
235 | * doAccept() will use AcceptLimiter if we reach the limit | |
236 | * there. | |
237 | */ | |
238 | ||
239 | /* Accept a new connection */ | |
5b67dfa4 | 240 | ConnectionPointer newConnDetails = new Connection(); |
8bbb16e3 | 241 | const comm_err_t flag = oldAccept(newConnDetails); |
04f55905 AJ |
242 | |
243 | /* Check for errors */ | |
5b67dfa4 | 244 | if (!newConnDetails->isOpen()) { |
04f55905 | 245 | |
cbff89ba | 246 | if (flag == COMM_NOMESSAGE) { |
04f55905 | 247 | /* register interest again */ |
6f8536c0 | 248 | debugs(5, 5, HERE << "try later: " << conn << " handler Subscription: " << theCallSub); |
8bbb16e3 | 249 | SetSelect(conn->fd, COMM_SELECT_READ, doAccept, this, 0); |
971581ee | 250 | return; |
04f55905 AJ |
251 | } |
252 | ||
253 | // A non-recoverable error; notify the caller */ | |
cbff89ba | 254 | debugs(5, 5, HERE << "non-recoverable error:" << status() << " handler Subscription: " << theCallSub); |
8bbb16e3 | 255 | notify(flag, newConnDetails); |
5b67dfa4 | 256 | mustStop("Listener socket closed"); |
971581ee | 257 | return; |
04f55905 AJ |
258 | } |
259 | ||
5b67dfa4 AJ |
260 | debugs(5, 5, HERE << "Listener: " << conn << |
261 | " accepted new connection " << newConnDetails << | |
6f8536c0 | 262 | " handler Subscription: " << theCallSub); |
8bbb16e3 | 263 | notify(flag, newConnDetails); |
04f55905 AJ |
264 | } |
265 | ||
266 | void | |
cbff89ba | 267 | Comm::TcpAcceptor::acceptNext() |
04f55905 | 268 | { |
a9870624 | 269 | Must(IsConnOpen(conn)); |
5b67dfa4 | 270 | debugs(5, 2, HERE << "connection on " << conn); |
971581ee | 271 | acceptOne(); |
04f55905 AJ |
272 | } |
273 | ||
274 | void | |
8bbb16e3 | 275 | Comm::TcpAcceptor::notify(const comm_err_t flag, const Comm::ConnectionPointer &newConnDetails) const |
04f55905 AJ |
276 | { |
277 | // listener socket handlers just abandon the port with COMM_ERR_CLOSING | |
278 | // it should only happen when this object is deleted... | |
1fc32b95 | 279 | if (flag == COMM_ERR_CLOSING) { |
04f55905 AJ |
280 | return; |
281 | } | |
282 | ||
5b67dfa4 AJ |
283 | if (theCallSub != NULL) { |
284 | AsyncCall::Pointer call = theCallSub->callback(); | |
285 | CommAcceptCbParams ¶ms = GetCommParams<CommAcceptCbParams>(call); | |
a9870624 | 286 | params.fd = conn->fd; |
5b67dfa4 | 287 | params.conn = newConnDetails; |
0ba55a12 AJ |
288 | params.flag = flag; |
289 | params.xerrno = errcode; | |
290 | ScheduleCallHere(call); | |
0ba55a12 | 291 | } |
04f55905 AJ |
292 | } |
293 | ||
294 | /** | |
97b8ac39 | 295 | * accept() and process |
5b67dfa4 AJ |
296 | * Wait for an incoming connection on our listener socket. |
297 | * | |
298 | * \retval COMM_OK success. details parameter filled. | |
299 | * \retval COMM_NOMESSAGE attempted accept() but nothing useful came in. | |
300 | * \retval COMM_ERROR an outright failure occured. | |
301 | * Or if this client has too many connections already. | |
273f66c4 | 302 | */ |
5b67dfa4 | 303 | comm_err_t |
8bbb16e3 | 304 | Comm::TcpAcceptor::oldAccept(Comm::ConnectionPointer &details) |
04f55905 AJ |
305 | { |
306 | PROF_start(comm_accept); | |
e4f1fdae | 307 | ++statCounter.syscalls.sock.accepts; |
04f55905 AJ |
308 | int sock; |
309 | struct addrinfo *gai = NULL; | |
5b67dfa4 | 310 | details->local.InitAddrInfo(gai); |
04f55905 | 311 | |
1fc32b95 | 312 | errcode = 0; // reset local errno copy. |
a9870624 | 313 | if ((sock = accept(conn->fd, gai->ai_addr, &gai->ai_addrlen)) < 0) { |
1fc32b95 | 314 | errcode = errno; // store last accept errno locally. |
04f55905 | 315 | |
5b67dfa4 | 316 | details->local.FreeAddrInfo(gai); |
04f55905 AJ |
317 | |
318 | PROF_stop(comm_accept); | |
319 | ||
320 | if (ignoreErrno(errno)) { | |
cbff89ba | 321 | debugs(50, 5, HERE << status() << ": " << xstrerror()); |
04f55905 AJ |
322 | return COMM_NOMESSAGE; |
323 | } else if (ENFILE == errno || EMFILE == errno) { | |
cbff89ba | 324 | debugs(50, 3, HERE << status() << ": " << xstrerror()); |
04f55905 AJ |
325 | return COMM_ERROR; |
326 | } else { | |
e0236918 | 327 | debugs(50, DBG_IMPORTANT, HERE << status() << ": " << xstrerror()); |
04f55905 AJ |
328 | return COMM_ERROR; |
329 | } | |
330 | } | |
331 | ||
a9870624 | 332 | Must(sock >= 0); |
5b67dfa4 AJ |
333 | details->fd = sock; |
334 | details->remote = *gai; | |
04f55905 | 335 | |
5511c78a | 336 | if ( Config.client_ip_max_connections >= 0) { |
5b67dfa4 AJ |
337 | if (clientdbEstablished(details->remote, 0) > Config.client_ip_max_connections) { |
338 | debugs(50, DBG_IMPORTANT, "WARNING: " << details->remote << " attempting more than " << Config.client_ip_max_connections << " connections."); | |
339 | details->local.FreeAddrInfo(gai); | |
5511c78a AJ |
340 | return COMM_ERROR; |
341 | } | |
342 | } | |
343 | ||
903198a7 | 344 | // lookup the local-end details of this new connection |
5b67dfa4 AJ |
345 | details->local.InitAddrInfo(gai); |
346 | details->local.SetEmpty(); | |
04f55905 | 347 | getsockname(sock, gai->ai_addr, &gai->ai_addrlen); |
5b67dfa4 AJ |
348 | details->local = *gai; |
349 | details->local.FreeAddrInfo(gai); | |
04f55905 AJ |
350 | |
351 | /* fdstat update */ | |
5b67dfa4 | 352 | // XXX : these are not all HTTP requests. use a note about type and ip:port details-> |
903198a7 | 353 | // so we end up with a uniform "(HTTP|FTP-data|HTTPS|...) remote-ip:remote-port" |
04f55905 AJ |
354 | fd_open(sock, FD_SOCKET, "HTTP Request"); |
355 | ||
356 | fdd_table[sock].close_file = NULL; | |
357 | fdd_table[sock].close_line = 0; | |
358 | ||
359 | fde *F = &fd_table[sock]; | |
5b67dfa4 AJ |
360 | details->remote.NtoA(F->ipaddr,MAX_IPSTRLEN); |
361 | F->remote_port = details->remote.GetPort(); | |
362 | F->local_addr = details->local; | |
363 | F->sock_family = details->local.IsIPv6()?AF_INET6:AF_INET; | |
04f55905 | 364 | |
903198a7 AJ |
365 | // set socket flags |
366 | commSetCloseOnExec(sock); | |
04f55905 AJ |
367 | commSetNonBlocking(sock); |
368 | ||
369 | /* IFF the socket is (tproxy) transparent, pass the flag down to allow spoofing */ | |
40d34a62 AJ |
370 | F->flags.transparent = fd_table[conn->fd].flags.transparent; // XXX: can we remove this line yet? |
371 | ||
372 | // Perform NAT or TPROXY operations to retrieve the real client/dest IP addresses | |
373 | if (conn->flags&(COMM_TRANSPARENT|COMM_INTERCEPTION) && !Ip::Interceptor.Lookup(details, conn)) { | |
374 | // Failed. | |
375 | return COMM_ERROR; | |
376 | } | |
04f55905 AJ |
377 | |
378 | PROF_stop(comm_accept); | |
5b67dfa4 | 379 | return COMM_OK; |
04f55905 | 380 | } |