]>
Commit | Line | Data |
---|---|---|
30a4f2a8 | 1 | /* |
b510f3a1 | 2 | * DEBUG: section 05 Socket Functions |
30a4f2a8 | 3 | * AUTHOR: Harvest Derived |
4 | * | |
2b6662ba | 5 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
e25c139f | 6 | * ---------------------------------------------------------- |
30a4f2a8 | 7 | * |
2b6662ba | 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. | |
30a4f2a8 | 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. | |
9e008dda | 21 | * |
30a4f2a8 | 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. | |
9e008dda | 26 | * |
30a4f2a8 | 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 | |
cbdec147 | 29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
e25c139f | 30 | * |
2d8c0b1a | 31 | * |
32 | * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org> | |
30a4f2a8 | 33 | */ |
090089c4 | 34 | |
582c2af2 | 35 | #include "squid.h" |
cfd66529 | 36 | #include "base/AsyncCall.h" |
c4b7a5a9 | 37 | #include "comm.h" |
582c2af2 FC |
38 | #include "ClientInfo.h" |
39 | #include "CommCalls.h" | |
04f55905 AJ |
40 | #include "comm/AcceptLimiter.h" |
41 | #include "comm/comm_internal.h" | |
cfd66529 | 42 | #include "comm/Connection.h" |
ec41b64c | 43 | #include "comm/IoCallback.h" |
d841c88d | 44 | #include "comm/Loops.h" |
ec41b64c | 45 | #include "comm/Write.h" |
cbff89ba | 46 | #include "comm/TcpAcceptor.h" |
a553a5a3 | 47 | #include "CommRead.h" |
582c2af2 | 48 | #include "compat/cmsg.h" |
74257126 | 49 | #include "DescriptorSet.h" |
582c2af2 FC |
50 | #include "event.h" |
51 | #include "fde.h" | |
52 | #include "globals.h" | |
9b5c4a9a | 53 | #include "icmp/net_db.h" |
96d89ea0 HN |
54 | #include "ip/Address.h" |
55 | #include "ip/Intercept.h" | |
425de4c8 | 56 | #include "ip/QosConfig.h" |
055421ee | 57 | #include "ip/tools.h" |
582c2af2 FC |
58 | #include "MemBuf.h" |
59 | #include "pconn.h" | |
60 | #include "protos.h" | |
61 | #include "profiler/Profiler.h" | |
62 | #include "SquidTime.h" | |
e4f1fdae | 63 | #include "StatCounters.h" |
582c2af2 | 64 | #include "StoreIOBuffer.h" |
4db984be CT |
65 | #if USE_SSL |
66 | #include "ssl/support.h" | |
67 | #endif | |
090089c4 | 68 | |
b4cd430a | 69 | #include "cbdata.h" |
be266cb2 | 70 | #if _SQUID_CYGWIN_ |
b671cc68 | 71 | #include <sys/ioctl.h> |
72 | #endif | |
30a4f2a8 | 73 | #ifdef HAVE_NETINET_TCP_H |
74 | #include <netinet/tcp.h> | |
75 | #endif | |
582c2af2 FC |
76 | #if HAVE_SYS_UN_H |
77 | #include <sys/un.h> | |
78 | #endif | |
79 | #if HAVE_MATH_H | |
80 | #include <math.h> | |
81 | #endif | |
21d845b1 FC |
82 | #if HAVE_ERRNO_H |
83 | #include <errno.h> | |
84 | #endif | |
090089c4 | 85 | |
2b663917 | 86 | /* |
87 | * New C-like simple comm code. This stuff is a mess and doesn't really buy us anything. | |
88 | */ | |
89 | ||
82ec8dfc AR |
90 | static void commStopHalfClosedMonitor(int fd); |
91 | static IOCB commHalfClosedReader; | |
9c8a6c3b | 92 | static void comm_init_opened(const Comm::ConnectionPointer &conn, tos_t tos, nfmark_t nfmark, const char *note, struct addrinfo *AI); |
a67d2b2e | 93 | static int comm_apply_flags(int new_socket, Ip::Address &addr, int flags, struct addrinfo *AI); |
82ec8dfc | 94 | |
9a0a18de | 95 | #if USE_DELAY_POOLS |
b4cd430a CT |
96 | CBDATA_CLASS_INIT(CommQuotaQueue); |
97 | ||
98 | static void commHandleWriteHelper(void * data); | |
99 | #endif | |
100 | ||
090089c4 | 101 | /* STATIC */ |
62e76326 | 102 | |
74257126 AR |
103 | static DescriptorSet *TheHalfClosed = NULL; /// the set of half-closed FDs |
104 | static bool WillCheckHalfClosed = false; /// true if check is scheduled | |
105 | static EVH commHalfClosedCheck; | |
106 | static void commPlanHalfClosedCheck(); | |
107 | ||
cc192b50 | 108 | static comm_err_t commBind(int s, struct addrinfo &); |
f5b8bbc4 | 109 | static void commSetReuseAddr(int); |
110 | static void commSetNoLinger(int); | |
30a4f2a8 | 111 | #ifdef TCP_NODELAY |
f5b8bbc4 | 112 | static void commSetTcpNoDelay(int); |
30a4f2a8 | 113 | #endif |
f5b8bbc4 | 114 | static void commSetTcpRcvbuf(int, int); |
723123a9 | 115 | |
b001e822 | 116 | static MemAllocator *conn_close_pool = NULL; |
c4b7a5a9 | 117 | fd_debug_t *fdd_table = NULL; |
62e76326 | 118 | |
04f55905 | 119 | bool |
b0469965 | 120 | isOpen(const int fd) |
b300c36d | 121 | { |
9a5ac93d | 122 | return fd >= 0 && fd_table && fd_table[fd].flags.open != 0; |
b300c36d | 123 | } |
124 | ||
e1a88700 | 125 | /** |
c4b7a5a9 | 126 | * Attempt a read |
127 | * | |
128 | * If the read attempt succeeds or fails, call the callback. | |
129 | * Else, wait for another IO notification. | |
130 | */ | |
2d8c0b1a | 131 | void |
2b663917 | 132 | commHandleRead(int fd, void *data) |
2d8c0b1a | 133 | { |
ec41b64c | 134 | Comm::IoCallback *ccb = (Comm::IoCallback *) data; |
9e008dda | 135 | |
2b663917 | 136 | assert(data == COMMIO_FD_READCB(fd)); |
ec41b64c | 137 | assert(ccb->active()); |
62e76326 | 138 | /* Attempt a read */ |
95dc7ff4 | 139 | ++ statCounter.syscalls.sock.reads; |
62e76326 | 140 | errno = 0; |
2d8c0b1a | 141 | int retval; |
2b663917 | 142 | retval = FD_READ_METHOD(fd, ccb->buf, ccb->size); |
bf8fe701 | 143 | debugs(5, 3, "comm_read_try: FD " << fd << ", size " << ccb->size << ", retval " << retval << ", errno " << errno); |
62e76326 | 144 | |
145 | if (retval < 0 && !ignoreErrno(errno)) { | |
bf8fe701 | 146 | debugs(5, 3, "comm_read_try: scheduling COMM_ERROR"); |
9e008dda | 147 | ccb->offset = 0; |
ec41b64c | 148 | ccb->finish(COMM_ERROR, errno); |
62e76326 | 149 | return; |
150 | }; | |
151 | ||
152 | /* See if we read anything */ | |
153 | /* Note - read 0 == socket EOF, which is a valid read */ | |
154 | if (retval >= 0) { | |
155 | fd_bytes(fd, retval, FD_READ); | |
9e008dda | 156 | ccb->offset = retval; |
ec41b64c | 157 | ccb->finish(COMM_OK, errno); |
62e76326 | 158 | return; |
159 | } | |
c4b7a5a9 | 160 | |
62e76326 | 161 | /* Nope, register for some more IO */ |
d841c88d | 162 | Comm::SetSelect(fd, COMM_SELECT_READ, commHandleRead, data, 0); |
c4b7a5a9 | 163 | } |
164 | ||
abd8f140 AJ |
165 | /** |
166 | * Queue a read. handler/handler_data are called when the read | |
167 | * completes, on error, or on file descriptor close. | |
168 | */ | |
b0469965 | 169 | void |
ec20038e | 170 | comm_read(const Comm::ConnectionPointer &conn, char *buf, int size, AsyncCall::Pointer &callback) |
c4b7a5a9 | 171 | { |
ec20038e | 172 | debugs(5, 5, "comm_read, queueing read for " << conn << "; asynCall " << callback); |
c4b7a5a9 | 173 | |
82ec8dfc | 174 | /* Make sure we are open and not closing */ |
ec20038e AJ |
175 | assert(Comm::IsConnOpen(conn)); |
176 | assert(!fd_table[conn->fd].closing()); | |
b0388924 | 177 | Comm::IoCallback *ccb = COMMIO_FD_READCB(conn->fd); |
82ec8dfc AR |
178 | |
179 | // Make sure we are either not reading or just passively monitoring. | |
180 | // Active/passive conflicts are OK and simply cancel passive monitoring. | |
181 | if (ccb->active()) { | |
182 | // if the assertion below fails, we have an active comm_read conflict | |
ec20038e AJ |
183 | assert(fd_table[conn->fd].halfClosedReader != NULL); |
184 | commStopHalfClosedMonitor(conn->fd); | |
82ec8dfc AR |
185 | assert(!ccb->active()); |
186 | } | |
b0388924 | 187 | ccb->conn = conn; |
528b2c61 | 188 | |
2b663917 | 189 | /* Queue the read */ |
ec41b64c | 190 | ccb->setCallback(Comm::IOCB_READ, callback, (char *)buf, NULL, size); |
8bbb16e3 | 191 | Comm::SetSelect(conn->fd, COMM_SELECT_READ, commHandleRead, ccb, 0); |
c4b7a5a9 | 192 | } |
193 | ||
e1a88700 | 194 | /** |
c4b7a5a9 | 195 | * Empty the read buffers |
196 | * | |
197 | * This is a magical routine that empties the read buffers. | |
198 | * Under some platforms (Linux) if a buffer has data in it before | |
199 | * you call close(), the socket will hang and take quite a while | |
200 | * to timeout. | |
201 | */ | |
202 | static void | |
203 | comm_empty_os_read_buffers(int fd) | |
204 | { | |
1191b93b | 205 | #if _SQUID_LINUX_ |
c4b7a5a9 | 206 | /* prevent those nasty RST packets */ |
207 | char buf[SQUID_TCP_SO_RCVBUF]; | |
62e76326 | 208 | |
cc192b50 | 209 | if (fd_table[fd].flags.nonblocking == 1) { |
210 | while (FD_READ_METHOD(fd, buf, SQUID_TCP_SO_RCVBUF) > 0) {}; | |
211 | } | |
c4b7a5a9 | 212 | #endif |
213 | } | |
214 | ||
e1a88700 | 215 | /** |
2b663917 | 216 | * Return whether the FD has a pending completed callback. |
e884bbde | 217 | * NP: does not work. |
c4b7a5a9 | 218 | */ |
219 | int | |
220 | comm_has_pending_read_callback(int fd) | |
221 | { | |
b0469965 | 222 | assert(isOpen(fd)); |
223 | // XXX: We do not know whether there is a read callback scheduled. | |
224 | // This is used for pconn management that should probably be more | |
225 | // tightly integrated into comm to minimize the chance that a | |
226 | // closing pconn socket will be used for a new transaction. | |
545d554b | 227 | return false; |
c4b7a5a9 | 228 | } |
229 | ||
b0469965 | 230 | // Does comm check this fd for read readiness? |
231 | // Note that when comm is not monitoring, there can be a pending callback | |
232 | // call, which may resume comm monitoring once fired. | |
528b2c61 | 233 | bool |
b0469965 | 234 | comm_monitors_read(int fd) |
c4b7a5a9 | 235 | { |
b0469965 | 236 | assert(isOpen(fd)); |
237 | // Being active is usually the same as monitoring because we always | |
ec41b64c AJ |
238 | // start monitoring the FD when we configure Comm::IoCallback for I/O |
239 | // and we usually configure Comm::IoCallback for I/O when we starting | |
240 | // monitoring a FD for reading. | |
b0469965 | 241 | return COMMIO_FD_READCB(fd)->active(); |
c4b7a5a9 | 242 | } |
243 | ||
e1a88700 | 244 | /** |
c4b7a5a9 | 245 | * Cancel a pending read. Assert that we have the right parameters, |
246 | * and that there are no pending read events! | |
2b663917 | 247 | * |
b0469965 | 248 | * XXX: We do not assert that there are no pending read events and |
249 | * with async calls it becomes even more difficult. | |
250 | * The whole interface should be reworked to do callback->cancel() | |
251 | * instead of searching for places where the callback may be stored and | |
252 | * updating the state of those places. | |
253 | * | |
2b663917 | 254 | * AHC Don't call the comm handlers? |
c4b7a5a9 | 255 | */ |
256 | void | |
257 | comm_read_cancel(int fd, IOCB *callback, void *data) | |
258 | { | |
b0469965 | 259 | if (!isOpen(fd)) { |
260 | debugs(5, 4, "comm_read_cancel fails: FD " << fd << " closed"); | |
261 | return; | |
9e008dda | 262 | } |
b0469965 | 263 | |
ec41b64c | 264 | Comm::IoCallback *cb = COMMIO_FD_READCB(fd); |
b0469965 | 265 | // TODO: is "active" == "monitors FD"? |
266 | if (!cb->active()) { | |
267 | debugs(5, 4, "comm_read_cancel fails: FD " << fd << " inactive"); | |
268 | return; | |
9e008dda | 269 | } |
b0469965 | 270 | |
271 | typedef CommCbFunPtrCallT<CommIoCbPtrFun> Call; | |
272 | Call *call = dynamic_cast<Call*>(cb->callback.getRaw()); | |
273 | if (!call) { | |
274 | debugs(5, 4, "comm_read_cancel fails: FD " << fd << " lacks callback"); | |
275 | return; | |
9e008dda | 276 | } |
b0469965 | 277 | |
82ec8dfc AR |
278 | call->cancel("old comm_read_cancel"); |
279 | ||
b0469965 | 280 | typedef CommIoCbParams Params; |
281 | const Params ¶ms = GetCommParams<Params>(cb->callback); | |
c4b7a5a9 | 282 | |
c4b7a5a9 | 283 | /* Ok, we can be reasonably sure we won't lose any data here! */ |
b0469965 | 284 | assert(call->dialer.handler == callback); |
285 | assert(params.data == data); | |
c4b7a5a9 | 286 | |
287 | /* Delete the callback */ | |
ec41b64c | 288 | cb->cancel("old comm_read_cancel"); |
420f2ac8 | 289 | |
290 | /* And the IO event */ | |
d841c88d | 291 | Comm::SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0); |
c4b7a5a9 | 292 | } |
293 | ||
c4b7a5a9 | 294 | void |
b0469965 | 295 | comm_read_cancel(int fd, AsyncCall::Pointer &callback) |
c4b7a5a9 | 296 | { |
b0469965 | 297 | callback->cancel("comm_read_cancel"); |
9e008dda | 298 | |
b0469965 | 299 | if (!isOpen(fd)) { |
300 | debugs(5, 4, "comm_read_cancel fails: FD " << fd << " closed"); | |
301 | return; | |
302 | } | |
303 | ||
ec41b64c | 304 | Comm::IoCallback *cb = COMMIO_FD_READCB(fd); |
b0469965 | 305 | |
306 | if (!cb->active()) { | |
307 | debugs(5, 4, "comm_read_cancel fails: FD " << fd << " inactive"); | |
308 | return; | |
309 | } | |
310 | ||
311 | AsyncCall::Pointer call = cb->callback; | |
82ec8dfc | 312 | assert(call != NULL); // XXX: should never fail (active() checks for callback==NULL) |
9e008dda | 313 | |
b0469965 | 314 | /* Ok, we can be reasonably sure we won't lose any data here! */ |
315 | assert(call == callback); | |
c4b7a5a9 | 316 | |
b0469965 | 317 | /* Delete the callback */ |
ec41b64c | 318 | cb->cancel("comm_read_cancel"); |
b0469965 | 319 | |
320 | /* And the IO event */ | |
d841c88d | 321 | Comm::SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0); |
c4b7a5a9 | 322 | } |
323 | ||
e1a88700 | 324 | /** |
ce767c23 | 325 | * synchronous wrapper around udp socket functions |
326 | */ | |
ce767c23 | 327 | int |
b7ac5457 | 328 | comm_udp_recvfrom(int fd, void *buf, size_t len, int flags, Ip::Address &from) |
ce767c23 | 329 | { |
95dc7ff4 | 330 | ++ statCounter.syscalls.sock.recvfroms; |
cc192b50 | 331 | int x = 0; |
332 | struct addrinfo *AI = NULL; | |
333 | ||
334 | debugs(5,8, "comm_udp_recvfrom: FD " << fd << " from " << from); | |
335 | ||
336 | assert( NULL == AI ); | |
337 | ||
338 | from.InitAddrInfo(AI); | |
339 | ||
340 | x = recvfrom(fd, buf, len, flags, AI->ai_addr, &AI->ai_addrlen); | |
341 | ||
342 | from = *AI; | |
343 | ||
344 | from.FreeAddrInfo(AI); | |
345 | ||
346 | return x; | |
ce767c23 | 347 | } |
348 | ||
365f12a9 | 349 | int |
7d21986b | 350 | comm_udp_recv(int fd, void *buf, size_t len, int flags) |
365f12a9 | 351 | { |
b7ac5457 | 352 | Ip::Address nul; |
cc192b50 | 353 | return comm_udp_recvfrom(fd, buf, len, flags, nul); |
365f12a9 | 354 | } |
355 | ||
f71da12c | 356 | ssize_t |
7d21986b | 357 | comm_udp_send(int s, const void *buf, size_t len, int flags) |
f71da12c | 358 | { |
62e76326 | 359 | return send(s, buf, len, flags); |
f71da12c | 360 | } |
ce767c23 | 361 | |
545d554b | 362 | bool |
363 | comm_has_incomplete_write(int fd) | |
364 | { | |
b0469965 | 365 | assert(isOpen(fd)); |
366 | return COMMIO_FD_WRITECB(fd)->active(); | |
d4cb310b | 367 | } |
368 | ||
e1a88700 | 369 | /** |
cf3c0ee3 | 370 | * Queue a write. handler/handler_data are called when the write fully |
371 | * completes, on error, or on file descriptor close. | |
372 | */ | |
9864ee44 | 373 | |
090089c4 | 374 | /* Return the local port associated with fd. */ |
f45dd259 | 375 | unsigned short |
b8d8561b | 376 | comm_local_port(int fd) |
090089c4 | 377 | { |
b7ac5457 | 378 | Ip::Address temp; |
cc192b50 | 379 | struct addrinfo *addr = NULL; |
76f87348 | 380 | fde *F = &fd_table[fd]; |
090089c4 | 381 | |
090089c4 | 382 | /* If the fd is closed already, just return */ |
62e76326 | 383 | |
60c0b5a2 | 384 | if (!F->flags.open) { |
bf8fe701 | 385 | debugs(5, 0, "comm_local_port: FD " << fd << " has been closed."); |
62e76326 | 386 | return 0; |
090089c4 | 387 | } |
62e76326 | 388 | |
cc192b50 | 389 | if (F->local_addr.GetPort()) |
390 | return F->local_addr.GetPort(); | |
62e76326 | 391 | |
6084c0b6 AJ |
392 | if (F->sock_family == AF_INET) |
393 | temp.SetIPv4(); | |
6084c0b6 | 394 | |
cc192b50 | 395 | temp.InitAddrInfo(addr); |
62e76326 | 396 | |
cc192b50 | 397 | if (getsockname(fd, addr->ai_addr, &(addr->ai_addrlen)) ) { |
e0236918 | 398 | debugs(50, DBG_IMPORTANT, "comm_local_port: Failed to retrieve TCP/UDP port number for socket: FD " << fd << ": " << xstrerror()); |
cc192b50 | 399 | temp.FreeAddrInfo(addr); |
62e76326 | 400 | return 0; |
090089c4 | 401 | } |
cc192b50 | 402 | temp = *addr; |
403 | ||
404 | temp.FreeAddrInfo(addr); | |
405 | ||
3d031c35 AJ |
406 | if (F->local_addr.IsAnyAddr()) { |
407 | /* save the whole local address, not just the port. */ | |
408 | F->local_addr = temp; | |
409 | } else { | |
410 | F->local_addr.SetPort(temp.GetPort()); | |
411 | } | |
62e76326 | 412 | |
6084c0b6 | 413 | debugs(5, 6, "comm_local_port: FD " << fd << ": port " << F->local_addr.GetPort() << "(family=" << F->sock_family << ")"); |
cc192b50 | 414 | return F->local_addr.GetPort(); |
090089c4 | 415 | } |
416 | ||
3d7e9d7c | 417 | static comm_err_t |
cc192b50 | 418 | commBind(int s, struct addrinfo &inaddr) |
090089c4 | 419 | { |
95dc7ff4 | 420 | ++ statCounter.syscalls.sock.binds; |
62e76326 | 421 | |
ac760b5e AJ |
422 | if (bind(s, inaddr.ai_addr, inaddr.ai_addrlen) == 0) { |
423 | debugs(50, 6, "commBind: bind socket FD " << s << " to " << fd_table[s].local_addr); | |
62e76326 | 424 | return COMM_OK; |
ac760b5e | 425 | } |
62e76326 | 426 | |
cc192b50 | 427 | debugs(50, 0, "commBind: Cannot bind socket FD " << s << " to " << fd_table[s].local_addr << ": " << xstrerror()); |
62e76326 | 428 | |
090089c4 | 429 | return COMM_ERROR; |
430 | } | |
431 | ||
e1a88700 | 432 | /** |
433 | * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE | |
434 | * is OR of flags specified in comm.h. Defaults TOS | |
435 | */ | |
b8d8561b | 436 | int |
16b204c4 | 437 | comm_open(int sock_type, |
62e76326 | 438 | int proto, |
b7ac5457 | 439 | Ip::Address &addr, |
62e76326 | 440 | int flags, |
441 | const char *note) | |
d6827718 | 442 | { |
425de4c8 | 443 | return comm_openex(sock_type, proto, addr, flags, 0, 0, note); |
d6827718 | 444 | } |
445 | ||
e0d28505 AJ |
446 | void |
447 | comm_open_listener(int sock_type, | |
448 | int proto, | |
449 | Comm::ConnectionPointer &conn, | |
450 | const char *note) | |
451 | { | |
452 | /* all listener sockets require bind() */ | |
453 | conn->flags |= COMM_DOBIND; | |
454 | ||
455 | /* attempt native enabled port. */ | |
b5523edc | 456 | conn->fd = comm_openex(sock_type, proto, conn->local, conn->flags, 0, 0, note); |
e0d28505 AJ |
457 | } |
458 | ||
31be869c AJ |
459 | int |
460 | comm_open_listener(int sock_type, | |
04f7fd38 | 461 | int proto, |
b7ac5457 | 462 | Ip::Address &addr, |
04f7fd38 AJ |
463 | int flags, |
464 | const char *note) | |
31be869c AJ |
465 | { |
466 | int sock = -1; | |
467 | ||
ac760b5e AJ |
468 | /* all listener sockets require bind() */ |
469 | flags |= COMM_DOBIND; | |
470 | ||
31be869c | 471 | /* attempt native enabled port. */ |
425de4c8 | 472 | sock = comm_openex(sock_type, proto, addr, flags, 0, 0, note); |
31be869c | 473 | |
31be869c AJ |
474 | return sock; |
475 | } | |
476 | ||
2d8c0b1a | 477 | static bool |
478 | limitError(int const anErrno) | |
479 | { | |
480 | return anErrno == ENFILE || anErrno == EMFILE; | |
481 | } | |
d6827718 | 482 | |
cc192b50 | 483 | void |
484 | comm_set_v6only(int fd, int tos) | |
485 | { | |
486 | #ifdef IPV6_V6ONLY | |
487 | if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &tos, sizeof(int)) < 0) { | |
e0236918 | 488 | debugs(50, DBG_IMPORTANT, "comm_open: setsockopt(IPV6_V6ONLY) " << (tos?"ON":"OFF") << " for FD " << fd << ": " << xstrerror()); |
cc192b50 | 489 | } |
490 | #else | |
491 | debugs(50, 0, "WARNING: comm_open: setsockopt(IPV6_V6ONLY) not supported on this platform"); | |
492 | #endif /* sockopt */ | |
493 | } | |
057f5854 | 494 | |
40d6264d AJ |
495 | /** |
496 | * Set the socket IP_TRANSPARENT option for Linux TPROXY v4 support. | |
497 | */ | |
f1e0717c | 498 | void |
e950e673 | 499 | comm_set_transparent(int fd) |
f1e0717c | 500 | { |
2ad20b4f | 501 | #if defined(IP_TRANSPARENT) |
e950e673 | 502 | int tos = 1; |
ef88b51d AJ |
503 | if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &tos, sizeof(int)) < 0) { |
504 | debugs(50, DBG_IMPORTANT, "comm_open: setsockopt(IP_TRANSPARENT) on FD " << fd << ": " << xstrerror()); | |
9e008dda | 505 | } else { |
3949d8b7 AJ |
506 | /* mark the socket as having transparent options */ |
507 | fd_table[fd].flags.transparent = 1; | |
508 | } | |
f1e0717c | 509 | #else |
ef88b51d | 510 | debugs(50, DBG_CRITICAL, "WARNING: comm_open: setsockopt(IP_TRANSPARENT) not supported on this platform"); |
f1e0717c AJ |
511 | #endif /* sockopt */ |
512 | } | |
513 | ||
e1a88700 | 514 | /** |
515 | * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE | |
516 | * is OR of flags specified in defines.h:COMM_* | |
517 | */ | |
d6827718 | 518 | int |
519 | comm_openex(int sock_type, | |
62e76326 | 520 | int proto, |
b7ac5457 | 521 | Ip::Address &addr, |
62e76326 | 522 | int flags, |
425de4c8 AJ |
523 | tos_t tos, |
524 | nfmark_t nfmark, | |
62e76326 | 525 | const char *note) |
090089c4 | 526 | { |
527 | int new_socket; | |
cc192b50 | 528 | struct addrinfo *AI = NULL; |
090089c4 | 529 | |
88bfe092 | 530 | PROF_start(comm_open); |
090089c4 | 531 | /* Create socket for accepting new connections. */ |
95dc7ff4 | 532 | ++ statCounter.syscalls.sock.sockets; |
62e76326 | 533 | |
cc192b50 | 534 | /* Setup the socket addrinfo details for use */ |
535 | addr.GetAddrInfo(AI); | |
536 | AI->ai_socktype = sock_type; | |
537 | AI->ai_protocol = proto; | |
cc192b50 | 538 | |
e884bbde | 539 | debugs(50, 3, "comm_openex: Attempt open socket for: " << addr ); |
cc192b50 | 540 | |
0eb08770 | 541 | new_socket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol); |
055421ee | 542 | |
0eb08770 HN |
543 | /* under IPv6 there is the possibility IPv6 is present but disabled. */ |
544 | /* try again as IPv4-native if possible */ | |
055421ee | 545 | if ( new_socket < 0 && Ip::EnableIpv6 && addr.IsIPv6() && addr.SetIPv4() ) { |
0eb08770 HN |
546 | /* attempt to open this IPv4-only. */ |
547 | addr.FreeAddrInfo(AI); | |
548 | /* Setup the socket addrinfo details for use */ | |
549 | addr.GetAddrInfo(AI); | |
550 | AI->ai_socktype = sock_type; | |
551 | AI->ai_protocol = proto; | |
552 | debugs(50, 3, "comm_openex: Attempt fallback open socket for: " << addr ); | |
ec505200 | 553 | new_socket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol); |
0eb08770 HN |
554 | debugs(50, 2, HERE << "attempt open " << note << " socket on: " << addr); |
555 | } | |
0eb08770 HN |
556 | |
557 | if (new_socket < 0) { | |
62e76326 | 558 | /* Increase the number of reserved fd's if calls to socket() |
559 | * are failing because the open file table is full. This | |
560 | * limits the number of simultaneous clients */ | |
561 | ||
2d8c0b1a | 562 | if (limitError(errno)) { |
dd4e714f | 563 | debugs(50, DBG_IMPORTANT, "comm_open: socket failure: " << xstrerror()); |
62e76326 | 564 | fdAdjustReserved(); |
2d8c0b1a | 565 | } else { |
8ca8bd92 | 566 | debugs(50, DBG_CRITICAL, "comm_open: socket failure: " << xstrerror()); |
62e76326 | 567 | } |
568 | ||
cc192b50 | 569 | addr.FreeAddrInfo(AI); |
570 | ||
62e76326 | 571 | PROF_stop(comm_open); |
572 | return -1; | |
090089c4 | 573 | } |
62e76326 | 574 | |
1b76e6c1 | 575 | // XXX: temporary for the transition. comm_openex will eventually have a conn to play with. |
e884bbde AJ |
576 | Comm::ConnectionPointer conn = new Comm::Connection; |
577 | conn->local = addr; | |
b5523edc AJ |
578 | conn->fd = new_socket; |
579 | ||
580 | debugs(50, 3, "comm_openex: Opened socket " << conn << " : family=" << AI->ai_family << ", type=" << AI->ai_socktype << ", protocol=" << AI->ai_protocol ); | |
cc192b50 | 581 | |
d6827718 | 582 | /* set TOS if needed */ |
425de4c8 | 583 | if (tos) |
b5523edc | 584 | Ip::Qos::setSockTos(conn, tos); |
425de4c8 AJ |
585 | |
586 | /* set netfilter mark if needed */ | |
587 | if (nfmark) | |
b5523edc | 588 | Ip::Qos::setSockNfmark(conn, nfmark); |
62e76326 | 589 | |
055421ee | 590 | if ( Ip::EnableIpv6&IPV6_SPECIAL_SPLITSTACK && addr.IsIPv6() ) |
b5523edc | 591 | comm_set_v6only(conn->fd, 1); |
62e76326 | 592 | |
cc192b50 | 593 | /* Windows Vista supports Dual-Sockets. BUT defaults them to V6ONLY. Turn it OFF. */ |
9b1f7ee8 | 594 | /* Other OS may have this administratively disabled for general use. Same deal. */ |
055421ee | 595 | if ( Ip::EnableIpv6&IPV6_SPECIAL_V4MAPPING && addr.IsIPv6() ) |
b5523edc | 596 | comm_set_v6only(conn->fd, 0); |
cc192b50 | 597 | |
b5523edc AJ |
598 | comm_init_opened(conn, tos, nfmark, note, AI); |
599 | new_socket = comm_apply_flags(conn->fd, addr, flags, AI); | |
38344a8e AR |
600 | |
601 | addr.FreeAddrInfo(AI); | |
602 | ||
603 | PROF_stop(comm_open); | |
604 | ||
1b76e6c1 | 605 | // XXX transition only. prevent conn from closing the new FD on function exit. |
b5523edc | 606 | conn->fd = -1; |
38344a8e AR |
607 | return new_socket; |
608 | } | |
609 | ||
610 | /// update FD tables after a local or remote (IPC) comm_openex(); | |
611 | void | |
e0d28505 | 612 | comm_init_opened(const Comm::ConnectionPointer &conn, |
425de4c8 AJ |
613 | tos_t tos, |
614 | nfmark_t nfmark, | |
5667a628 AR |
615 | const char *note, |
616 | struct addrinfo *AI) | |
38344a8e | 617 | { |
e0d28505 | 618 | assert(Comm::IsConnOpen(conn)); |
38344a8e AR |
619 | assert(AI); |
620 | ||
090089c4 | 621 | /* update fdstat */ |
e0d28505 | 622 | debugs(5, 5, HERE << conn << " is a new socket"); |
62e76326 | 623 | |
e0d28505 AJ |
624 | assert(!isOpen(conn->fd)); // NP: global isOpen checks the fde entry for openness not the Comm::Connection |
625 | fd_open(conn->fd, FD_SOCKET, note); | |
62e76326 | 626 | |
e0d28505 AJ |
627 | fdd_table[conn->fd].close_file = NULL; |
628 | fdd_table[conn->fd].close_line = 0; | |
62e76326 | 629 | |
e0d28505 AJ |
630 | fde *F = &fd_table[conn->fd]; |
631 | F->local_addr = conn->local; | |
425de4c8 AJ |
632 | F->tosToServer = tos; |
633 | ||
634 | F->nfmarkToServer = nfmark; | |
cc192b50 | 635 | |
636 | F->sock_family = AI->ai_family; | |
38344a8e AR |
637 | } |
638 | ||
639 | /// apply flags after a local comm_open*() call; | |
640 | /// returns new_socket or -1 on error | |
641 | static int | |
642 | comm_apply_flags(int new_socket, | |
a67d2b2e | 643 | Ip::Address &addr, |
5667a628 AR |
644 | int flags, |
645 | struct addrinfo *AI) | |
38344a8e AR |
646 | { |
647 | assert(new_socket >= 0); | |
648 | assert(AI); | |
649 | const int sock_type = AI->ai_socktype; | |
62e76326 | 650 | |
79a15e0a | 651 | if (!(flags & COMM_NOCLOEXEC)) |
62e76326 | 652 | commSetCloseOnExec(new_socket); |
653 | ||
cdc33f35 | 654 | if ((flags & COMM_REUSEADDR)) |
62e76326 | 655 | commSetReuseAddr(new_socket); |
656 | ||
f45dd259 | 657 | if (addr.GetPort() > (unsigned short) 0) { |
1191b93b | 658 | #if _SQUID_MSWIN_ |
a50bfe93 | 659 | if (sock_type != SOCK_DGRAM) |
660 | #endif | |
a50bfe93 | 661 | commSetNoLinger(new_socket); |
62e76326 | 662 | |
663 | if (opt_reuseaddr) | |
664 | commSetReuseAddr(new_socket); | |
090089c4 | 665 | } |
62e76326 | 666 | |
a35595cd | 667 | /* MUST be done before binding or face OS Error: "(99) Cannot assign requested address"... */ |
9e008dda | 668 | if ((flags & COMM_TRANSPARENT)) { |
a35595cd AJ |
669 | comm_set_transparent(new_socket); |
670 | } | |
a35595cd | 671 | |
ac760b5e AJ |
672 | if ( (flags & COMM_DOBIND) || addr.GetPort() > 0 || !addr.IsAnyAddr() ) { |
673 | if ( !(flags & COMM_DOBIND) && addr.IsAnyAddr() ) | |
e0236918 | 674 | debugs(5, DBG_IMPORTANT,"WARNING: Squid is attempting to bind() port " << addr << " without being a listener."); |
ac760b5e AJ |
675 | if ( addr.IsNoAddr() ) |
676 | debugs(5,0,"CRITICAL: Squid is attempting to bind() port " << addr << "!!"); | |
677 | ||
cc192b50 | 678 | if (commBind(new_socket, *AI) != COMM_OK) { |
62e76326 | 679 | comm_close(new_socket); |
680 | return -1; | |
62e76326 | 681 | } |
23ff6968 | 682 | } |
62e76326 | 683 | |
79a15e0a | 684 | if (flags & COMM_NONBLOCKING) |
9e008dda | 685 | if (commSetNonBlocking(new_socket) == COMM_ERROR) { |
38344a8e | 686 | comm_close(new_socket); |
62e76326 | 687 | return -1; |
62e76326 | 688 | } |
689 | ||
30a4f2a8 | 690 | #ifdef TCP_NODELAY |
691 | if (sock_type == SOCK_STREAM) | |
62e76326 | 692 | commSetTcpNoDelay(new_socket); |
693 | ||
30a4f2a8 | 694 | #endif |
62e76326 | 695 | |
1241e63e | 696 | if (Config.tcpRcvBufsz > 0 && sock_type == SOCK_STREAM) |
62e76326 | 697 | commSetTcpRcvbuf(new_socket, Config.tcpRcvBufsz); |
698 | ||
090089c4 | 699 | return new_socket; |
700 | } | |
701 | ||
a4c0f9c6 | 702 | void |
e0d28505 | 703 | comm_import_opened(const Comm::ConnectionPointer &conn, |
5667a628 AR |
704 | const char *note, |
705 | struct addrinfo *AI) | |
a4c0f9c6 | 706 | { |
e0d28505 AJ |
707 | debugs(5, 2, HERE << conn); |
708 | assert(Comm::IsConnOpen(conn)); | |
a4c0f9c6 AR |
709 | assert(AI); |
710 | ||
9c8a6c3b | 711 | comm_init_opened(conn, 0, 0, note, AI); |
a4c0f9c6 | 712 | |
e0d28505 AJ |
713 | if (!(conn->flags & COMM_NOCLOEXEC)) |
714 | fd_table[conn->fd].flags.close_on_exec = 1; | |
a4c0f9c6 | 715 | |
f45dd259 | 716 | if (conn->local.GetPort() > (unsigned short) 0) { |
1191b93b | 717 | #if _SQUID_MSWIN_ |
e0d28505 | 718 | if (AI->ai_socktype != SOCK_DGRAM) |
a4c0f9c6 | 719 | #endif |
e0d28505 | 720 | fd_table[conn->fd].flags.nolinger = 1; |
a4c0f9c6 AR |
721 | } |
722 | ||
e0d28505 AJ |
723 | if ((conn->flags & COMM_TRANSPARENT)) |
724 | fd_table[conn->fd].flags.transparent = 1; | |
a4c0f9c6 | 725 | |
e0d28505 AJ |
726 | if (conn->flags & COMM_NONBLOCKING) |
727 | fd_table[conn->fd].flags.nonblocking = 1; | |
a4c0f9c6 AR |
728 | |
729 | #ifdef TCP_NODELAY | |
730 | if (AI->ai_socktype == SOCK_STREAM) | |
e0d28505 | 731 | fd_table[conn->fd].flags.nodelay = 1; |
a4c0f9c6 AR |
732 | #endif |
733 | ||
734 | /* no fd_table[fd].flags. updates needed for these conditions: | |
735 | * if ((flags & COMM_REUSEADDR)) ... | |
736 | * if ((flags & COMM_DOBIND) ...) ... | |
737 | */ | |
738 | } | |
739 | ||
933dd095 AJ |
740 | // XXX: now that raw-FD timeouts are only unset for pipes and files this SHOULD be a no-op. |
741 | // With handler already unset. Leaving this present until that can be verified for all code paths. | |
742 | void | |
743 | commUnsetFdTimeout(int fd) | |
b0469965 | 744 | { |
933dd095 | 745 | debugs(5, 3, HERE << "Remove timeout for FD " << fd); |
b0469965 | 746 | assert(fd >= 0); |
747 | assert(fd < Squid_MaxFD); | |
748 | fde *F = &fd_table[fd]; | |
749 | assert(F->flags.open); | |
750 | ||
933dd095 AJ |
751 | F->timeoutHandler = NULL; |
752 | F->timeout = 0; | |
7957e704 AJ |
753 | } |
754 | ||
755 | int | |
756 | commSetConnTimeout(const Comm::ConnectionPointer &conn, int timeout, AsyncCall::Pointer &callback) | |
757 | { | |
758 | debugs(5, 3, HERE << conn << " timeout " << timeout); | |
759 | assert(Comm::IsConnOpen(conn)); | |
760 | assert(conn->fd < Squid_MaxFD); | |
761 | fde *F = &fd_table[conn->fd]; | |
762 | assert(F->flags.open); | |
763 | ||
764 | if (timeout < 0) { | |
765 | F->timeoutHandler = NULL; | |
766 | F->timeout = 0; | |
767 | } else { | |
768 | if (callback != NULL) { | |
769 | typedef CommTimeoutCbParams Params; | |
770 | Params ¶ms = GetCommParams<Params>(callback); | |
771 | params.conn = conn; | |
772 | F->timeoutHandler = callback; | |
773 | } | |
b0469965 | 774 | |
7957e704 AJ |
775 | F->timeout = squid_curtime + (time_t) timeout; |
776 | } | |
777 | ||
778 | return F->timeout; | |
b0469965 | 779 | } |
090089c4 | 780 | |
8d77a37c AJ |
781 | int |
782 | commUnsetConnTimeout(const Comm::ConnectionPointer &conn) | |
783 | { | |
784 | debugs(5, 3, HERE << "Remove timeout for " << conn); | |
785 | AsyncCall::Pointer nil; | |
786 | return commSetConnTimeout(conn, -1, nil); | |
787 | } | |
788 | ||
b8d8561b | 789 | int |
b7ac5457 | 790 | comm_connect_addr(int sock, const Ip::Address &address) |
090089c4 | 791 | { |
3d7e9d7c | 792 | comm_err_t status = COMM_OK; |
76f87348 | 793 | fde *F = &fd_table[sock]; |
cc192b50 | 794 | int x = 0; |
b5568a61 | 795 | int err = 0; |
9689d97c | 796 | socklen_t errlen; |
feca3b9a | 797 | struct addrinfo *AI = NULL; |
88bfe092 | 798 | PROF_start(comm_connect_addr); |
cc192b50 | 799 | |
800 | assert(address.GetPort() != 0); | |
801 | ||
fa6a39d7 | 802 | debugs(5, 9, HERE << "connecting socket FD " << sock << " to " << address << " (want family: " << F->sock_family << ")"); |
cc192b50 | 803 | |
3d98ff81 | 804 | /* Handle IPv6 over IPv4-only socket case. |
9d92af86 | 805 | * this case must presently be handled here since the GetAddrInfo asserts on bad mappings. |
3d98ff81 | 806 | * NP: because commResetFD is private to ConnStateData we have to return an error and |
9d92af86 AJ |
807 | * trust its handled properly. |
808 | */ | |
9e008dda | 809 | if (F->sock_family == AF_INET && !address.IsIPv4()) { |
3d98ff81 HN |
810 | errno = ENETUNREACH; |
811 | return COMM_ERR_PROTOCOL; | |
812 | } | |
813 | ||
814 | /* Handle IPv4 over IPv6-only socket case. | |
815 | * This case is presently handled here as it's both a known case and it's | |
816 | * uncertain what error will be returned by the IPv6 stack in such case. It's | |
817 | * possible this will also be handled by the errno checks below after connect() | |
818 | * but needs carefull cross-platform verification, and verifying the address | |
819 | * condition here is simple. | |
820 | */ | |
821 | if (!F->local_addr.IsIPv4() && address.IsIPv4()) { | |
822 | errno = ENETUNREACH; | |
9d92af86 AJ |
823 | return COMM_ERR_PROTOCOL; |
824 | } | |
9d92af86 | 825 | |
feca3b9a | 826 | address.GetAddrInfo(AI, F->sock_family); |
cc192b50 | 827 | |
090089c4 | 828 | /* Establish connection. */ |
b5568a61 | 829 | errno = 0; |
62e76326 | 830 | |
9e008dda | 831 | if (!F->flags.called_connect) { |
62e76326 | 832 | F->flags.called_connect = 1; |
95dc7ff4 | 833 | ++ statCounter.syscalls.sock.connects; |
62e76326 | 834 | |
feca3b9a | 835 | x = connect(sock, AI->ai_addr, AI->ai_addrlen); |
62e76326 | 836 | |
5a33a66a | 837 | // XXX: ICAP code refuses callbacks during a pending comm_ call |
838 | // Async calls development will fix this. | |
839 | if (x == 0) { | |
840 | x = -1; | |
841 | errno = EINPROGRESS; | |
842 | } | |
843 | ||
9e008dda | 844 | if (x < 0) { |
cc192b50 | 845 | debugs(5,5, "comm_connect_addr: sock=" << sock << ", addrinfo( " << |
9e008dda AJ |
846 | " flags=" << AI->ai_flags << |
847 | ", family=" << AI->ai_family << | |
848 | ", socktype=" << AI->ai_socktype << | |
849 | ", protocol=" << AI->ai_protocol << | |
850 | ", &addr=" << AI->ai_addr << | |
851 | ", addrlen=" << AI->ai_addrlen << | |
852 | " )" ); | |
cc192b50 | 853 | debugs(5, 9, "connect FD " << sock << ": (" << x << ") " << xstrerror()); |
854 | debugs(14,9, "connecting to: " << address ); | |
855 | } | |
9e008dda | 856 | } else { |
8a09e810 | 857 | #if _SQUID_NEWSOS6_ |
62e76326 | 858 | /* Makoto MATSUSHITA <matusita@ics.es.osaka-u.ac.jp> */ |
859 | ||
feca3b9a | 860 | connect(sock, AI->ai_addr, AI->ai_addrlen); |
62e76326 | 861 | |
862 | if (errno == EINVAL) { | |
863 | errlen = sizeof(err); | |
864 | x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen); | |
865 | ||
866 | if (x >= 0) | |
867 | errno = x; | |
868 | } | |
869 | ||
33ac9442 | 870 | #else |
62e76326 | 871 | errlen = sizeof(err); |
872 | ||
873 | x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen); | |
874 | ||
875 | if (x == 0) | |
876 | errno = err; | |
877 | ||
8a09e810 | 878 | #if _SQUID_SOLARIS_ |
62e76326 | 879 | /* |
880 | * Solaris 2.4's socket emulation doesn't allow you | |
881 | * to determine the error from a failed non-blocking | |
882 | * connect and just returns EPIPE. Create a fake | |
883 | * error message for connect. -- fenner@parc.xerox.com | |
884 | */ | |
885 | if (x < 0 && errno == EPIPE) | |
886 | errno = ENOTCONN; | |
887 | ||
33ac9442 | 888 | #endif |
30a4f2a8 | 889 | #endif |
62e76326 | 890 | |
e5f6c5c2 | 891 | } |
62e76326 | 892 | |
9e008dda AJ |
893 | /* Squid seems to be working fine without this code. With this code, |
894 | * we leak memory on many connect requests because of EINPROGRESS. | |
895 | * If you find that this code is needed, please file a bug report. */ | |
82ec8dfc | 896 | #if 0 |
1191b93b | 897 | #if _SQUID_LINUX_ |
feca3b9a | 898 | /* 2007-11-27: |
9e008dda | 899 | * Linux Debian replaces our allocated AI pointer with garbage when |
feca3b9a AJ |
900 | * connect() fails. This leads to segmentation faults deallocating |
901 | * the system-allocated memory when we go to clean up our pointer. | |
902 | * HACK: is to leak the memory returned since we can't deallocate. | |
903 | */ | |
9e008dda | 904 | if (errno != 0) { |
feca3b9a AJ |
905 | AI = NULL; |
906 | } | |
82ec8dfc | 907 | #endif |
feca3b9a AJ |
908 | #endif |
909 | ||
910 | address.FreeAddrInfo(AI); | |
911 | ||
88bfe092 | 912 | PROF_stop(comm_connect_addr); |
62e76326 | 913 | |
b5568a61 | 914 | if (errno == 0 || errno == EISCONN) |
62e76326 | 915 | status = COMM_OK; |
b5568a61 | 916 | else if (ignoreErrno(errno)) |
62e76326 | 917 | status = COMM_INPROGRESS; |
3d98ff81 HN |
918 | else if (errno == EAFNOSUPPORT || errno == EINVAL) |
919 | return COMM_ERR_PROTOCOL; | |
b5568a61 | 920 | else |
3d98ff81 | 921 | return COMM_ERROR; |
62e76326 | 922 | |
cc192b50 | 923 | address.NtoA(F->ipaddr, MAX_IPSTRLEN); |
62e76326 | 924 | |
cc192b50 | 925 | F->remote_port = address.GetPort(); /* remote_port is HS */ |
62e76326 | 926 | |
9e008dda | 927 | if (status == COMM_OK) { |
e0236918 | 928 | debugs(5, DBG_DATA, "comm_connect_addr: FD " << sock << " connected to " << address); |
9e008dda | 929 | } else if (status == COMM_INPROGRESS) { |
e0236918 | 930 | debugs(5, DBG_DATA, "comm_connect_addr: FD " << sock << " connection pending"); |
090089c4 | 931 | } |
62e76326 | 932 | |
090089c4 | 933 | return status; |
934 | } | |
935 | ||
cb201b7e | 936 | void |
937 | commCallCloseHandlers(int fd) | |
938 | { | |
76f87348 | 939 | fde *F = &fd_table[fd]; |
bf8fe701 | 940 | debugs(5, 5, "commCallCloseHandlers: FD " << fd); |
62e76326 | 941 | |
8000a965 | 942 | while (F->closeHandler != NULL) { |
b0469965 | 943 | AsyncCall::Pointer call = F->closeHandler; |
9e008dda AJ |
944 | F->closeHandler = call->Next(); |
945 | call->setNext(NULL); | |
946 | // If call is not canceled schedule it for execution else ignore it | |
947 | if (!call->canceled()) { | |
948 | debugs(5, 5, "commCallCloseHandlers: ch->handler=" << call); | |
9e008dda AJ |
949 | ScheduleCallHere(call); |
950 | } | |
cb201b7e | 951 | } |
952 | } | |
953 | ||
5492ad1d | 954 | #if LINGERING_CLOSE |
955 | static void | |
956 | commLingerClose(int fd, void *unused) | |
957 | { | |
958 | LOCAL_ARRAY(char, buf, 1024); | |
959 | int n; | |
1f7c9178 | 960 | n = FD_READ_METHOD(fd, buf, 1024); |
62e76326 | 961 | |
5492ad1d | 962 | if (n < 0) |
bf8fe701 | 963 | debugs(5, 3, "commLingerClose: FD " << fd << " read: " << xstrerror()); |
62e76326 | 964 | |
5492ad1d | 965 | comm_close(fd); |
966 | } | |
967 | ||
968 | static void | |
933dd095 | 969 | commLingerTimeout(const FdeCbParams ¶ms) |
5492ad1d | 970 | { |
933dd095 AJ |
971 | debugs(5, 3, "commLingerTimeout: FD " << params.fd); |
972 | comm_close(params.fd); | |
5492ad1d | 973 | } |
974 | ||
975 | /* | |
976 | * Inspired by apache | |
977 | */ | |
978 | void | |
979 | comm_lingering_close(int fd) | |
980 | { | |
d4c19b39 | 981 | #if USE_SSL |
982 | if (fd_table[fd].ssl) | |
575d05c4 | 983 | ssl_shutdown_method(fd_table[fd].ssl); |
d4c19b39 | 984 | #endif |
62e76326 | 985 | |
5492ad1d | 986 | if (shutdown(fd, 1) < 0) { |
62e76326 | 987 | comm_close(fd); |
988 | return; | |
5492ad1d | 989 | } |
62e76326 | 990 | |
5492ad1d | 991 | fd_note(fd, "lingering close"); |
933dd095 AJ |
992 | AsyncCall::Pointer call = commCbCall(5,4, "commLingerTimeout", FdeCbPtrFun(commLingerTimeout, NULL)); |
993 | ||
994 | debugs(5, 3, HERE << "FD " << fd << " timeout " << timeout); | |
995 | assert(fd_table[fd].flags.open); | |
996 | if (callback != NULL) { | |
997 | typedef FdeCbParams Params; | |
998 | Params ¶ms = GetCommParams<Params>(callback); | |
999 | params.fd = fd; | |
1000 | fd_table[fd].timeoutHandler = callback; | |
1001 | fd_table[fd].timeout = squid_curtime + static_cast<time_t>(10); | |
1002 | } | |
1003 | ||
d841c88d | 1004 | Comm::SetSelect(fd, COMM_SELECT_READ, commLingerClose, NULL, 0); |
5492ad1d | 1005 | } |
62e76326 | 1006 | |
5492ad1d | 1007 | #endif |
1008 | ||
27774cee | 1009 | /** |
98264874 | 1010 | * enable linger with time of 0 so that when the socket is |
1011 | * closed, TCP generates a RESET | |
1012 | */ | |
1013 | void | |
8ace824c | 1014 | comm_reset_close(const Comm::ConnectionPointer &conn) |
98264874 | 1015 | { |
5c336a3b AJ |
1016 | struct linger L; |
1017 | L.l_onoff = 1; | |
1018 | L.l_linger = 0; | |
62e76326 | 1019 | |
5c336a3b | 1020 | if (setsockopt(conn->fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0) |
1b76e6c1 | 1021 | debugs(50, DBG_CRITICAL, "ERROR: Closing " << conn << " with TCP RST: " << xstrerror()); |
5c336a3b AJ |
1022 | |
1023 | conn->close(); | |
1024 | } | |
1025 | ||
1026 | // Legacy close function. | |
1027 | void | |
1028 | old_comm_reset_close(int fd) | |
1029 | { | |
98264874 | 1030 | struct linger L; |
1031 | L.l_onoff = 1; | |
1032 | L.l_linger = 0; | |
62e76326 | 1033 | |
98264874 | 1034 | if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0) |
468fe1b5 | 1035 | debugs(50, DBG_CRITICAL, "ERROR: Closing FD " << fd << " with TCP RST: " << xstrerror()); |
62e76326 | 1036 | |
98264874 | 1037 | comm_close(fd); |
1038 | } | |
1039 | ||
575d05c4 | 1040 | #if USE_SSL |
9e008dda | 1041 | void |
a17bf806 | 1042 | commStartSslClose(const FdeCbParams ¶ms) |
10b06767 | 1043 | { |
575d05c4 AJ |
1044 | assert(&fd_table[params.fd].ssl); |
1045 | ssl_shutdown_method(fd_table[params.fd].ssl); | |
10b06767 | 1046 | } |
575d05c4 | 1047 | #endif |
10b06767 | 1048 | |
9e008dda | 1049 | void |
a17bf806 | 1050 | comm_close_complete(const FdeCbParams ¶ms) |
2d8c0b1a | 1051 | { |
b0469965 | 1052 | #if USE_SSL |
575d05c4 | 1053 | fde *F = &fd_table[params.fd]; |
2d8c0b1a | 1054 | |
b0469965 | 1055 | if (F->ssl) { |
1056 | SSL_free(F->ssl); | |
1057 | F->ssl = NULL; | |
1058 | } | |
2d8c0b1a | 1059 | |
95d2589c CT |
1060 | if (F->dynamicSslContext) { |
1061 | SSL_CTX_free(F->dynamicSslContext); | |
1062 | F->dynamicSslContext = NULL; | |
1063 | } | |
b0469965 | 1064 | #endif |
575d05c4 AJ |
1065 | fd_close(params.fd); /* update fdstat */ |
1066 | close(params.fd); | |
b0469965 | 1067 | |
95dc7ff4 | 1068 | ++ statCounter.syscalls.sock.closes; |
b0469965 | 1069 | |
575d05c4 | 1070 | /* When one connection closes, give accept() a chance, if need be */ |
04f55905 | 1071 | Comm::AcceptLimiter::Instance().kick(); |
2d8c0b1a | 1072 | } |
c4b7a5a9 | 1073 | |
1074 | /* | |
1075 | * Close the socket fd. | |
1076 | * | |
1077 | * + call write handlers with ERR_CLOSING | |
1078 | * + call read handlers with ERR_CLOSING | |
1079 | * + call closing handlers | |
a46d2c0e | 1080 | * |
9e008dda | 1081 | * NOTE: COMM_ERR_CLOSING will NOT be called for CommReads' sitting in a |
a46d2c0e | 1082 | * DeferredReadManager. |
c4b7a5a9 | 1083 | */ |
b8d8561b | 1084 | void |
43ae1d95 | 1085 | _comm_close(int fd, char const *file, int line) |
090089c4 | 1086 | { |
82ec8dfc | 1087 | debugs(5, 3, "comm_close: start closing FD " << fd); |
03eb2f01 | 1088 | assert(fd >= 0); |
1089 | assert(fd < Squid_MaxFD); | |
82ec8dfc AR |
1090 | |
1091 | fde *F = &fd_table[fd]; | |
c4b7a5a9 | 1092 | fdd_table[fd].close_file = file; |
1093 | fdd_table[fd].close_line = line; | |
1f7c9178 | 1094 | |
82ec8dfc | 1095 | if (F->closing()) |
62e76326 | 1096 | return; |
1097 | ||
36afac26 | 1098 | /* XXX: is this obsolete behind F->closing() ? */ |
b8869bcf | 1099 | if ( (shutting_down || reconfiguring) && (!F->flags.open || F->type == FD_FILE)) |
62e76326 | 1100 | return; |
1101 | ||
c4b7a5a9 | 1102 | /* The following fails because ipc.c is doing calls to pipe() to create sockets! */ |
c7e637d7 AJ |
1103 | if (!isOpen(fd)) { |
1104 | debugs(50, DBG_IMPORTANT, HERE << "BUG 3556: FD " << fd << " is not an open socket."); | |
1105 | // XXX: do we need to run close(fd) or fd_close(fd) here? | |
1106 | return; | |
1107 | } | |
62e76326 | 1108 | |
76f87348 | 1109 | assert(F->type != FD_FILE); |
62e76326 | 1110 | |
88bfe092 | 1111 | PROF_start(comm_close); |
62e76326 | 1112 | |
10b06767 | 1113 | F->flags.close_request = 1; |
62e76326 | 1114 | |
575d05c4 AJ |
1115 | #if USE_SSL |
1116 | if (F->ssl) { | |
575d05c4 | 1117 | AsyncCall::Pointer startCall=commCbCall(5,4, "commStartSslClose", |
a17bf806 AJ |
1118 | FdeCbPtrFun(commStartSslClose, NULL)); |
1119 | FdeCbParams &startParams = GetCommParams<FdeCbParams>(startCall); | |
575d05c4 AJ |
1120 | startParams.fd = fd; |
1121 | ScheduleCallHere(startCall); | |
1122 | } | |
1123 | #endif | |
62e76326 | 1124 | |
74257126 AR |
1125 | // a half-closed fd may lack a reader, so we stop monitoring explicitly |
1126 | if (commHasHalfClosedMonitor(fd)) | |
1127 | commStopHalfClosedMonitor(fd); | |
933dd095 | 1128 | commUnsetFdTimeout(fd); |
62e76326 | 1129 | |
a6351f16 | 1130 | // notify read/write handlers after canceling select reservations, if any |
ec41b64c | 1131 | if (COMMIO_FD_WRITECB(fd)->active()) { |
d841c88d | 1132 | Comm::SetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0); |
ec41b64c | 1133 | COMMIO_FD_WRITECB(fd)->finish(COMM_ERR_CLOSING, errno); |
2b663917 | 1134 | } |
ec41b64c | 1135 | if (COMMIO_FD_READCB(fd)->active()) { |
d841c88d | 1136 | Comm::SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0); |
ec41b64c | 1137 | COMMIO_FD_READCB(fd)->finish(COMM_ERR_CLOSING, errno); |
2b663917 | 1138 | } |
2d8c0b1a | 1139 | |
9a0a18de | 1140 | #if USE_DELAY_POOLS |
b4cd430a CT |
1141 | if (ClientInfo *clientInfo = F->clientInfo) { |
1142 | if (clientInfo->selectWaiting) { | |
1143 | clientInfo->selectWaiting = false; | |
1144 | // kick queue or it will get stuck as commWriteHandle is not called | |
1145 | clientInfo->kickQuotaQueue(); | |
1146 | } | |
f33d34a8 | 1147 | } |
b4cd430a CT |
1148 | #endif |
1149 | ||
cb201b7e | 1150 | commCallCloseHandlers(fd); |
62e76326 | 1151 | |
fb505fa1 | 1152 | if (F->pconn.uses && F->pconn.pool) |
2dba5b8e | 1153 | F->pconn.pool->noteUses(F->pconn.uses); |
62e76326 | 1154 | |
a7ad6e4e | 1155 | comm_empty_os_read_buffers(fd); |
9e008dda | 1156 | |
10b06767 | 1157 | AsyncCall::Pointer completeCall=commCbCall(5,4, "comm_close_complete", |
a17bf806 AJ |
1158 | FdeCbPtrFun(comm_close_complete, NULL)); |
1159 | FdeCbParams &completeParams = GetCommParams<FdeCbParams>(completeCall); | |
10b06767 | 1160 | completeParams.fd = fd; |
9e008dda | 1161 | // must use async call to wait for all callbacks |
82ec8dfc | 1162 | // scheduled before comm_close() to finish |
10b06767 | 1163 | ScheduleCallHere(completeCall); |
62e76326 | 1164 | |
88bfe092 | 1165 | PROF_stop(comm_close); |
090089c4 | 1166 | } |
1167 | ||
090089c4 | 1168 | /* Send a udp datagram to specified TO_ADDR. */ |
b8d8561b | 1169 | int |
5df61230 | 1170 | comm_udp_sendto(int fd, |
b7ac5457 | 1171 | const Ip::Address &to_addr, |
62e76326 | 1172 | const void *buf, |
1173 | int len) | |
090089c4 | 1174 | { |
cc192b50 | 1175 | int x = 0; |
1176 | struct addrinfo *AI = NULL; | |
1177 | ||
88bfe092 | 1178 | PROF_start(comm_udp_sendto); |
95dc7ff4 | 1179 | ++ statCounter.syscalls.sock.sendtos; |
62e76326 | 1180 | |
cc192b50 | 1181 | debugs(50, 3, "comm_udp_sendto: Attempt to send UDP packet to " << to_addr << |
9e008dda | 1182 | " using FD " << fd << " using Port " << comm_local_port(fd) ); |
cc192b50 | 1183 | |
1184 | /* BUG: something in the above macro appears to occasionally be setting AI to garbage. */ | |
1185 | /* AYJ: 2007-08-27 : or was it because I wasn't then setting 'fd_table[fd].sock_family' to fill properly. */ | |
1186 | assert( NULL == AI ); | |
1187 | ||
1188 | to_addr.GetAddrInfo(AI, fd_table[fd].sock_family); | |
1189 | ||
1190 | x = sendto(fd, buf, len, 0, AI->ai_addr, AI->ai_addrlen); | |
1191 | ||
1192 | to_addr.FreeAddrInfo(AI); | |
1193 | ||
88bfe092 | 1194 | PROF_stop(comm_udp_sendto); |
62e76326 | 1195 | |
2d8c0b1a | 1196 | if (x >= 0) |
1197 | return x; | |
1198 | ||
1191b93b | 1199 | #if _SQUID_LINUX_ |
62e76326 | 1200 | |
2d8c0b1a | 1201 | if (ECONNREFUSED != errno) |
17d51783 | 1202 | #endif |
62e76326 | 1203 | |
e0236918 | 1204 | debugs(50, DBG_IMPORTANT, "comm_udp_sendto: FD " << fd << ", (family=" << fd_table[fd].sock_family << ") " << to_addr << ": " << xstrerror()); |
62e76326 | 1205 | |
2d8c0b1a | 1206 | return COMM_ERROR; |
090089c4 | 1207 | } |
1208 | ||
b8d8561b | 1209 | void |
575d05c4 | 1210 | comm_add_close_handler(int fd, CLCB * handler, void *data) |
30a4f2a8 | 1211 | { |
bf8fe701 | 1212 | debugs(5, 5, "comm_add_close_handler: FD " << fd << ", handler=" << |
1213 | handler << ", data=" << data); | |
62e76326 | 1214 | |
b0469965 | 1215 | AsyncCall::Pointer call=commCbCall(5,4, "SomeCloseHandler", |
9e008dda | 1216 | CommCloseCbPtrFun(handler, data)); |
b0469965 | 1217 | comm_add_close_handler(fd, call); |
1218 | } | |
62e76326 | 1219 | |
b0469965 | 1220 | void |
1221 | comm_add_close_handler(int fd, AsyncCall::Pointer &call) | |
1222 | { | |
1223 | debugs(5, 5, "comm_add_close_handler: FD " << fd << ", AsyncCall=" << call); | |
62e76326 | 1224 | |
b0469965 | 1225 | /*TODO:Check for a similar scheduled AsyncCall*/ |
1226 | // for (c = fd_table[fd].closeHandler; c; c = c->next) | |
1227 | // assert(c->handler != handler || c->data != data); | |
62e76326 | 1228 | |
b0469965 | 1229 | call->setNext(fd_table[fd].closeHandler); |
62e76326 | 1230 | |
b0469965 | 1231 | fd_table[fd].closeHandler = call; |
30a4f2a8 | 1232 | } |
1233 | ||
b0469965 | 1234 | // remove function-based close handler |
b8d8561b | 1235 | void |
575d05c4 | 1236 | comm_remove_close_handler(int fd, CLCB * handler, void *data) |
090089c4 | 1237 | { |
b0469965 | 1238 | assert (isOpen(fd)); |
30a4f2a8 | 1239 | /* Find handler in list */ |
bf8fe701 | 1240 | debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", handler=" << |
1241 | handler << ", data=" << data); | |
62e76326 | 1242 | |
37cba319 AR |
1243 | AsyncCall::Pointer p, prev = NULL; |
1244 | for (p = fd_table[fd].closeHandler; p != NULL; prev = p, p = p->Next()) { | |
b0469965 | 1245 | typedef CommCbFunPtrCallT<CommCloseCbPtrFun> Call; |
1246 | const Call *call = dynamic_cast<const Call*>(p.getRaw()); | |
1247 | if (!call) // method callbacks have their own comm_remove_close_handler | |
1248 | continue; | |
62e76326 | 1249 | |
b0469965 | 1250 | typedef CommCloseCbParams Params; |
1251 | const Params ¶ms = GetCommParams<Params>(p); | |
1252 | if (call->dialer.handler == handler && params.data == data) | |
1253 | break; /* This is our handler */ | |
1254 | } | |
7828df5b CT |
1255 | |
1256 | // comm_close removes all close handlers so our handler may be gone | |
37cba319 AR |
1257 | if (p != NULL) { |
1258 | p->dequeue(fd_table[fd].closeHandler, prev); | |
7828df5b | 1259 | p->cancel("comm_remove_close_handler"); |
37cba319 | 1260 | } |
b0469965 | 1261 | } |
62e76326 | 1262 | |
b0469965 | 1263 | // remove method-based close handler |
1264 | void | |
1265 | comm_remove_close_handler(int fd, AsyncCall::Pointer &call) | |
1266 | { | |
1267 | assert (isOpen(fd)); | |
b0469965 | 1268 | debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", AsyncCall=" << call); |
62e76326 | 1269 | |
7828df5b | 1270 | // comm_close removes all close handlers so our handler may be gone |
37cba319 AR |
1271 | AsyncCall::Pointer p, prev = NULL; |
1272 | for (p = fd_table[fd].closeHandler; p != NULL && p != call; prev = p, p = p->Next()); | |
62e76326 | 1273 | |
37cba319 AR |
1274 | if (p != NULL) |
1275 | p->dequeue(fd_table[fd].closeHandler, prev); | |
b0469965 | 1276 | call->cancel("comm_remove_close_handler"); |
30a4f2a8 | 1277 | } |
090089c4 | 1278 | |
b8d8561b | 1279 | static void |
1280 | commSetNoLinger(int fd) | |
30a4f2a8 | 1281 | { |
62e76326 | 1282 | |
30a4f2a8 | 1283 | struct linger L; |
090089c4 | 1284 | L.l_onoff = 0; /* off */ |
1285 | L.l_linger = 0; | |
62e76326 | 1286 | |
30a4f2a8 | 1287 | if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0) |
bf8fe701 | 1288 | debugs(50, 0, "commSetNoLinger: FD " << fd << ": " << xstrerror()); |
62e76326 | 1289 | |
58a6c186 | 1290 | fd_table[fd].flags.nolinger = 1; |
090089c4 | 1291 | } |
1292 | ||
b8d8561b | 1293 | static void |
1294 | commSetReuseAddr(int fd) | |
090089c4 | 1295 | { |
1296 | int on = 1; | |
62e76326 | 1297 | |
30a4f2a8 | 1298 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0) |
e0236918 | 1299 | debugs(50, DBG_IMPORTANT, "commSetReuseAddr: FD " << fd << ": " << xstrerror()); |
090089c4 | 1300 | } |
1301 | ||
b8d8561b | 1302 | static void |
1303 | commSetTcpRcvbuf(int fd, int size) | |
f868539a | 1304 | { |
1305 | if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size)) < 0) | |
e0236918 | 1306 | debugs(50, DBG_IMPORTANT, "commSetTcpRcvbuf: FD " << fd << ", SIZE " << size << ": " << xstrerror()); |
8f0d53ef | 1307 | if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size)) < 0) |
e0236918 | 1308 | debugs(50, DBG_IMPORTANT, "commSetTcpRcvbuf: FD " << fd << ", SIZE " << size << ": " << xstrerror()); |
8f0d53ef | 1309 | #ifdef TCP_WINDOW_CLAMP |
1310 | if (setsockopt(fd, SOL_TCP, TCP_WINDOW_CLAMP, (char *) &size, sizeof(size)) < 0) | |
e0236918 | 1311 | debugs(50, DBG_IMPORTANT, "commSetTcpRcvbuf: FD " << fd << ", SIZE " << size << ": " << xstrerror()); |
8f0d53ef | 1312 | #endif |
f868539a | 1313 | } |
1314 | ||
b8d8561b | 1315 | int |
1316 | commSetNonBlocking(int fd) | |
30a4f2a8 | 1317 | { |
1191b93b | 1318 | #if !_SQUID_MSWIN_ |
731e4d49 | 1319 | int flags; |
9e205701 | 1320 | int dummy = 0; |
a50bfe93 | 1321 | #endif |
be266cb2 | 1322 | #if _SQUID_WINDOWS_ |
b05490a8 | 1323 | int nonblocking = TRUE; |
62e76326 | 1324 | |
be266cb2 | 1325 | #if _SQUID_CYGWIN_ |
7f6ffd15 | 1326 | if (fd_table[fd].type != FD_PIPE) { |
629b5f75 | 1327 | #endif |
1328 | ||
62e76326 | 1329 | if (ioctl(fd, FIONBIO, &nonblocking) < 0) { |
bf8fe701 | 1330 | debugs(50, 0, "commSetNonBlocking: FD " << fd << ": " << xstrerror() << " " << fd_table[fd].type); |
62e76326 | 1331 | return COMM_ERROR; |
1332 | } | |
629b5f75 | 1333 | |
be266cb2 | 1334 | #if _SQUID_CYGWIN_ |
7f6ffd15 | 1335 | } else { |
1336 | #endif | |
629b5f75 | 1337 | #endif |
1191b93b | 1338 | #if !_SQUID_MSWIN_ |
62e76326 | 1339 | |
1340 | if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) { | |
bf8fe701 | 1341 | debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror()); |
62e76326 | 1342 | return COMM_ERROR; |
1343 | } | |
1344 | ||
1345 | if (fcntl(fd, F_SETFL, flags | SQUID_NONBLOCK) < 0) { | |
bf8fe701 | 1346 | debugs(50, 0, "commSetNonBlocking: FD " << fd << ": " << xstrerror()); |
62e76326 | 1347 | return COMM_ERROR; |
1348 | } | |
1349 | ||
a50bfe93 | 1350 | #endif |
be266cb2 | 1351 | #if _SQUID_CYGWIN_ |
090089c4 | 1352 | } |
7f6ffd15 | 1353 | #endif |
58a6c186 | 1354 | fd_table[fd].flags.nonblocking = 1; |
62e76326 | 1355 | |
090089c4 | 1356 | return 0; |
1357 | } | |
1358 | ||
7e3ce7b9 | 1359 | int |
1360 | commUnsetNonBlocking(int fd) | |
1361 | { | |
1191b93b | 1362 | #if _SQUID_MSWIN_ |
a50bfe93 | 1363 | int nonblocking = FALSE; |
1364 | ||
1365 | if (ioctlsocket(fd, FIONBIO, (unsigned long *) &nonblocking) < 0) { | |
1366 | #else | |
7e3ce7b9 | 1367 | int flags; |
1368 | int dummy = 0; | |
62e76326 | 1369 | |
7e3ce7b9 | 1370 | if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) { |
bf8fe701 | 1371 | debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror()); |
62e76326 | 1372 | return COMM_ERROR; |
7e3ce7b9 | 1373 | } |
62e76326 | 1374 | |
7e3ce7b9 | 1375 | if (fcntl(fd, F_SETFL, flags & (~SQUID_NONBLOCK)) < 0) { |
a50bfe93 | 1376 | #endif |
bf8fe701 | 1377 | debugs(50, 0, "commUnsetNonBlocking: FD " << fd << ": " << xstrerror()); |
62e76326 | 1378 | return COMM_ERROR; |
7e3ce7b9 | 1379 | } |
62e76326 | 1380 | |
7e3ce7b9 | 1381 | fd_table[fd].flags.nonblocking = 0; |
1382 | return 0; | |
1383 | } | |
1384 | ||
b8d8561b | 1385 | void |
e1381638 AJ |
1386 | commSetCloseOnExec(int fd) |
1387 | { | |
3ca60c86 | 1388 | #ifdef FD_CLOEXEC |
731e4d49 | 1389 | int flags; |
7a18b487 | 1390 | int dummy = 0; |
62e76326 | 1391 | |
2209fe19 AJ |
1392 | if ((flags = fcntl(fd, F_GETFD, dummy)) < 0) { |
1393 | debugs(50, 0, "FD " << fd << ": fcntl F_GETFD: " << xstrerror()); | |
62e76326 | 1394 | return; |
3ca60c86 | 1395 | } |
62e76326 | 1396 | |
24382924 | 1397 | if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) |
bf8fe701 | 1398 | debugs(50, 0, "FD " << fd << ": set close-on-exec failed: " << xstrerror()); |
62e76326 | 1399 | |
d6827718 | 1400 | fd_table[fd].flags.close_on_exec = 1; |
62e76326 | 1401 | |
3ca60c86 | 1402 | #endif |
1403 | } | |
1404 | ||
e90100aa | 1405 | #ifdef TCP_NODELAY |
1406 | static void | |
e1381638 AJ |
1407 | commSetTcpNoDelay(int fd) |
1408 | { | |
e90100aa | 1409 | int on = 1; |
62e76326 | 1410 | |
e90100aa | 1411 | if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) < 0) |
e0236918 | 1412 | debugs(50, DBG_IMPORTANT, "commSetTcpNoDelay: FD " << fd << ": " << xstrerror()); |
62e76326 | 1413 | |
d6827718 | 1414 | fd_table[fd].flags.nodelay = 1; |
e90100aa | 1415 | } |
62e76326 | 1416 | |
e90100aa | 1417 | #endif |
1418 | ||
b2130d58 | 1419 | void |
e1381638 AJ |
1420 | commSetTcpKeepalive(int fd, int idle, int interval, int timeout) |
1421 | { | |
b2130d58 | 1422 | int on = 1; |
1423 | #ifdef TCP_KEEPCNT | |
1424 | if (timeout && interval) { | |
9e008dda AJ |
1425 | int count = (timeout + interval - 1) / interval; |
1426 | if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &count, sizeof(on)) < 0) | |
e0236918 | 1427 | debugs(5, DBG_IMPORTANT, "commSetKeepalive: FD " << fd << ": " << xstrerror()); |
b2130d58 | 1428 | } |
1429 | #endif | |
1430 | #ifdef TCP_KEEPIDLE | |
1431 | if (idle) { | |
9e008dda | 1432 | if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(on)) < 0) |
e0236918 | 1433 | debugs(5, DBG_IMPORTANT, "commSetKeepalive: FD " << fd << ": " << xstrerror()); |
b2130d58 | 1434 | } |
1435 | #endif | |
1436 | #ifdef TCP_KEEPINTVL | |
1437 | if (interval) { | |
9e008dda | 1438 | if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(on)) < 0) |
e0236918 | 1439 | debugs(5, DBG_IMPORTANT, "commSetKeepalive: FD " << fd << ": " << xstrerror()); |
b2130d58 | 1440 | } |
1441 | #endif | |
1442 | if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &on, sizeof(on)) < 0) | |
e0236918 | 1443 | debugs(5, DBG_IMPORTANT, "commSetKeepalive: FD " << fd << ": " << xstrerror()); |
b2130d58 | 1444 | } |
6a988308 | 1445 | |
d86b3703 | 1446 | void |
e1381638 AJ |
1447 | comm_init(void) |
1448 | { | |
c4b7a5a9 | 1449 | fd_table =(fde *) xcalloc(Squid_MaxFD, sizeof(fde)); |
1450 | fdd_table = (fd_debug_t *)xcalloc(Squid_MaxFD, sizeof(fd_debug_t)); | |
2d8c0b1a | 1451 | |
04f55905 AJ |
1452 | /* make sure the accept() socket FIFO delay queue exists */ |
1453 | Comm::AcceptLimiter::Instance(); | |
b0469965 | 1454 | |
ec41b64c AJ |
1455 | // make sure the IO pending callback table exists |
1456 | Comm::CallbackTableInit(); | |
2d8c0b1a | 1457 | |
59c4d35b | 1458 | /* XXX account fd_table */ |
090089c4 | 1459 | /* Keep a few file descriptors free so that we don't run out of FD's |
1460 | * after accepting a client but before it opens a socket or a file. | |
e83892e9 | 1461 | * Since Squid_MaxFD can be as high as several thousand, don't waste them */ |
d85c3078 | 1462 | RESERVED_FD = min(100, Squid_MaxFD / 4); |
2d8c0b1a | 1463 | |
04eb0689 | 1464 | conn_close_pool = memPoolCreate("close_handler", sizeof(close_handler)); |
74257126 AR |
1465 | |
1466 | TheHalfClosed = new DescriptorSet; | |
d841c88d AJ |
1467 | |
1468 | /* setup the select loop module */ | |
1469 | Comm::SelectLoopInit(); | |
090089c4 | 1470 | } |
1471 | ||
236d1779 | 1472 | void |
e1381638 AJ |
1473 | comm_exit(void) |
1474 | { | |
74257126 AR |
1475 | delete TheHalfClosed; |
1476 | TheHalfClosed = NULL; | |
1477 | ||
236d1779 | 1478 | safe_free(fd_table); |
1479 | safe_free(fdd_table); | |
ec41b64c | 1480 | Comm::CallbackTableDestruct(); |
236d1779 | 1481 | } |
1482 | ||
9a0a18de | 1483 | #if USE_DELAY_POOLS |
b4cd430a | 1484 | // called when the queue is done waiting for the client bucket to fill |
ec41b64c | 1485 | void |
b4cd430a CT |
1486 | commHandleWriteHelper(void * data) |
1487 | { | |
1488 | CommQuotaQueue *queue = static_cast<CommQuotaQueue*>(data); | |
1489 | assert(queue); | |
1490 | ||
1491 | ClientInfo *clientInfo = queue->clientInfo; | |
1492 | // ClientInfo invalidates queue if freed, so if we got here through, | |
1493 | // evenAdd cbdata protections, everything should be valid and consistent | |
f33d34a8 | 1494 | assert(clientInfo); |
b4cd430a CT |
1495 | assert(clientInfo->hasQueue()); |
1496 | assert(clientInfo->hasQueue(queue)); | |
1497 | assert(!clientInfo->selectWaiting); | |
1498 | assert(clientInfo->eventWaiting); | |
1499 | clientInfo->eventWaiting = false; | |
1500 | ||
1501 | do { | |
1502 | // check that the head descriptor is still relevant | |
1503 | const int head = clientInfo->quotaPeekFd(); | |
ec41b64c | 1504 | Comm::IoCallback *ccb = COMMIO_FD_WRITECB(head); |
b4cd430a CT |
1505 | |
1506 | if (fd_table[head].clientInfo == clientInfo && | |
f33d34a8 A |
1507 | clientInfo->quotaPeekReserv() == ccb->quotaQueueReserv && |
1508 | !fd_table[head].closing()) { | |
b4cd430a CT |
1509 | |
1510 | // wait for the head descriptor to become ready for writing | |
d841c88d | 1511 | Comm::SetSelect(head, COMM_SELECT_WRITE, Comm::HandleWrite, ccb, 0); |
b4cd430a CT |
1512 | clientInfo->selectWaiting = true; |
1513 | return; | |
f33d34a8 | 1514 | } |
b4cd430a | 1515 | |
f33d34a8 A |
1516 | clientInfo->quotaDequeue(); // remove the no longer relevant descriptor |
1517 | // and continue looking for a relevant one | |
b4cd430a CT |
1518 | } while (clientInfo->hasQueue()); |
1519 | ||
1520 | debugs(77,3, HERE << "emptied queue"); | |
1521 | } | |
1522 | ||
1523 | bool | |
1524 | ClientInfo::hasQueue() const | |
1525 | { | |
1526 | assert(quotaQueue); | |
1527 | return !quotaQueue->empty(); | |
1528 | } | |
1529 | ||
1530 | bool | |
1531 | ClientInfo::hasQueue(const CommQuotaQueue *q) const | |
1532 | { | |
1533 | assert(quotaQueue); | |
1534 | return quotaQueue == q; | |
1535 | } | |
1536 | ||
1537 | /// returns the first descriptor to be dequeued | |
1538 | int | |
1539 | ClientInfo::quotaPeekFd() const | |
1540 | { | |
1541 | assert(quotaQueue); | |
1542 | return quotaQueue->front(); | |
1543 | } | |
1544 | ||
1545 | /// returns the reservation ID of the first descriptor to be dequeued | |
1546 | unsigned int | |
1547 | ClientInfo::quotaPeekReserv() const | |
1548 | { | |
1549 | assert(quotaQueue); | |
1550 | return quotaQueue->outs + 1; | |
1551 | } | |
1552 | ||
1553 | /// queues a given fd, creating the queue if necessary; returns reservation ID | |
1554 | unsigned int | |
1555 | ClientInfo::quotaEnqueue(int fd) | |
1556 | { | |
1557 | assert(quotaQueue); | |
1558 | return quotaQueue->enqueue(fd); | |
1559 | } | |
1560 | ||
1561 | /// removes queue head | |
1562 | void | |
1563 | ClientInfo::quotaDequeue() | |
1564 | { | |
1565 | assert(quotaQueue); | |
1566 | quotaQueue->dequeue(); | |
1567 | } | |
1568 | ||
1569 | void | |
1570 | ClientInfo::kickQuotaQueue() | |
1571 | { | |
1572 | if (!eventWaiting && !selectWaiting && hasQueue()) { | |
1573 | // wait at least a second if the bucket is empty | |
1574 | const double delay = (bucketSize < 1.0) ? 1.0 : 0.0; | |
1575 | eventAdd("commHandleWriteHelper", &commHandleWriteHelper, | |
f33d34a8 | 1576 | quotaQueue, delay, 0, true); |
b4cd430a CT |
1577 | eventWaiting = true; |
1578 | } | |
1579 | } | |
1580 | ||
1581 | /// calculates how much to write for a single dequeued client | |
1582 | int | |
1583 | ClientInfo::quotaForDequed() | |
1584 | { | |
1585 | /* If we have multiple clients and give full bucketSize to each client then | |
1586 | * clt1 may often get a lot more because clt1->clt2 time distance in the | |
1587 | * select(2) callback order may be a lot smaller than cltN->clt1 distance. | |
1588 | * We divide quota evenly to be more fair. */ | |
1589 | ||
1590 | if (!rationedCount) { | |
1591 | rationedCount = quotaQueue->size() + 1; | |
1592 | ||
1593 | // The delay in ration recalculation _temporary_ deprives clients from | |
1594 | // bytes that should have trickled in while rationedCount was positive. | |
1595 | refillBucket(); | |
1596 | ||
1597 | // Rounding errors do not accumulate here, but we round down to avoid | |
1598 | // negative bucket sizes after write with rationedCount=1. | |
1599 | rationedQuota = static_cast<int>(floor(bucketSize/rationedCount)); | |
1600 | debugs(77,5, HERE << "new rationedQuota: " << rationedQuota << | |
f33d34a8 | 1601 | '*' << rationedCount); |
b4cd430a CT |
1602 | } |
1603 | ||
1604 | --rationedCount; | |
1605 | debugs(77,7, HERE << "rationedQuota: " << rationedQuota << | |
f33d34a8 | 1606 | " rations remaining: " << rationedCount); |
b4cd430a CT |
1607 | |
1608 | // update 'last seen' time to prevent clientdb GC from dropping us | |
1609 | last_seen = squid_curtime; | |
1610 | return rationedQuota; | |
1611 | } | |
1612 | ||
1613 | ///< adds bytes to the quota bucket based on the rate and passed time | |
1614 | void | |
1615 | ClientInfo::refillBucket() | |
1616 | { | |
1617 | // all these times are in seconds, with double precision | |
1618 | const double currTime = current_dtime; | |
1619 | const double timePassed = currTime - prevTime; | |
1620 | ||
f33d34a8 | 1621 | // Calculate allowance for the time passed. Use double to avoid |
b4cd430a CT |
1622 | // accumulating rounding errors for small intervals. For example, always |
1623 | // adding 1 byte instead of 1.4 results in 29% bandwidth allocation error. | |
1624 | const double gain = timePassed * writeSpeedLimit; | |
1625 | ||
1626 | debugs(77,5, HERE << currTime << " clt" << (const char*)hash.key << ": " << | |
f33d34a8 A |
1627 | bucketSize << " + (" << timePassed << " * " << writeSpeedLimit << |
1628 | " = " << gain << ')'); | |
b4cd430a CT |
1629 | |
1630 | // to further combat error accumulation during micro updates, | |
1631 | // quit before updating time if we cannot add at least one byte | |
1632 | if (gain < 1.0) | |
f33d34a8 | 1633 | return; |
b4cd430a CT |
1634 | |
1635 | prevTime = currTime; | |
1636 | ||
1637 | // for "first" connections, drain initial fat before refilling but keep | |
1638 | // updating prevTime to avoid bursts after the fat is gone | |
1639 | if (bucketSize > bucketSizeLimit) { | |
f33d34a8 A |
1640 | debugs(77,4, HERE << "not refilling while draining initial fat"); |
1641 | return; | |
b4cd430a CT |
1642 | } |
1643 | ||
1644 | bucketSize += gain; | |
1645 | ||
1646 | // obey quota limits | |
1647 | if (bucketSize > bucketSizeLimit) | |
1648 | bucketSize = bucketSizeLimit; | |
1649 | } | |
1650 | ||
f33d34a8 | 1651 | void |
b4cd430a CT |
1652 | ClientInfo::setWriteLimiter(const int aWriteSpeedLimit, const double anInitialBurst, const double aHighWatermark) |
1653 | { | |
f33d34a8 A |
1654 | debugs(77,5, HERE << "Write limits for " << (const char*)hash.key << |
1655 | " speed=" << aWriteSpeedLimit << " burst=" << anInitialBurst << | |
1656 | " highwatermark=" << aHighWatermark); | |
b4cd430a CT |
1657 | |
1658 | // set or possibly update traffic shaping parameters | |
1659 | writeLimitingActive = true; | |
1660 | writeSpeedLimit = aWriteSpeedLimit; | |
1661 | bucketSizeLimit = aHighWatermark; | |
1662 | ||
1663 | // but some members should only be set once for a newly activated bucket | |
1664 | if (firstTimeConnection) { | |
1665 | firstTimeConnection = false; | |
1666 | ||
1667 | assert(!selectWaiting); | |
1668 | assert(!quotaQueue); | |
e9dadd7d | 1669 | quotaQueue = new CommQuotaQueue(this); |
b4cd430a CT |
1670 | |
1671 | bucketSize = anInitialBurst; | |
1672 | prevTime = current_dtime; | |
1673 | } | |
1674 | } | |
1675 | ||
1676 | CommQuotaQueue::CommQuotaQueue(ClientInfo *info): clientInfo(info), | |
f33d34a8 | 1677 | ins(0), outs(0) |
b4cd430a CT |
1678 | { |
1679 | assert(clientInfo); | |
1680 | } | |
1681 | ||
1682 | CommQuotaQueue::~CommQuotaQueue() | |
1683 | { | |
1684 | assert(!clientInfo); // ClientInfo should clear this before destroying us | |
1685 | } | |
1686 | ||
1687 | /// places the given fd at the end of the queue; returns reservation ID | |
1688 | unsigned int | |
1689 | CommQuotaQueue::enqueue(int fd) | |
1690 | { | |
1691 | debugs(77,5, HERE << "clt" << (const char*)clientInfo->hash.key << | |
f33d34a8 | 1692 | ": FD " << fd << " with qqid" << (ins+1) << ' ' << fds.size()); |
b4cd430a CT |
1693 | fds.push_back(fd); |
1694 | return ++ins; | |
1695 | } | |
1696 | ||
1697 | /// removes queue head | |
1698 | void | |
1699 | CommQuotaQueue::dequeue() | |
1700 | { | |
1701 | assert(!fds.empty()); | |
1702 | debugs(77,5, HERE << "clt" << (const char*)clientInfo->hash.key << | |
f33d34a8 A |
1703 | ": FD " << fds.front() << " with qqid" << (outs+1) << ' ' << |
1704 | fds.size()); | |
b4cd430a CT |
1705 | fds.pop_front(); |
1706 | ++outs; | |
1707 | } | |
b4cd430a CT |
1708 | #endif |
1709 | ||
89924214 | 1710 | /* |
1711 | * hm, this might be too general-purpose for all the places we'd | |
1712 | * like to use it. | |
1713 | */ | |
b224ea98 | 1714 | int |
e1381638 AJ |
1715 | ignoreErrno(int ierrno) |
1716 | { | |
603500e7 | 1717 | switch (ierrno) { |
62e76326 | 1718 | |
89924214 | 1719 | case EINPROGRESS: |
62e76326 | 1720 | |
603500e7 | 1721 | case EWOULDBLOCK: |
26a880e2 | 1722 | #if EAGAIN != EWOULDBLOCK |
62e76326 | 1723 | |
603500e7 | 1724 | case EAGAIN: |
26a880e2 | 1725 | #endif |
62e76326 | 1726 | |
603500e7 | 1727 | case EALREADY: |
62e76326 | 1728 | |
603500e7 | 1729 | case EINTR: |
db494ab8 | 1730 | #ifdef ERESTART |
62e76326 | 1731 | |
db494ab8 | 1732 | case ERESTART: |
1733 | #endif | |
62e76326 | 1734 | |
1735 | return 1; | |
1736 | ||
603500e7 | 1737 | default: |
62e76326 | 1738 | return 0; |
603500e7 | 1739 | } |
62e76326 | 1740 | |
603500e7 | 1741 | /* NOTREACHED */ |
26a880e2 | 1742 | } |
d723bf6b | 1743 | |
1744 | void | |
e1381638 AJ |
1745 | commCloseAllSockets(void) |
1746 | { | |
d723bf6b | 1747 | int fd; |
1748 | fde *F = NULL; | |
62e76326 | 1749 | |
95dc7ff4 | 1750 | for (fd = 0; fd <= Biggest_FD; ++fd) { |
62e76326 | 1751 | F = &fd_table[fd]; |
1752 | ||
1753 | if (!F->flags.open) | |
1754 | continue; | |
1755 | ||
1756 | if (F->type != FD_SOCKET) | |
1757 | continue; | |
1758 | ||
1759 | if (F->flags.ipc) /* don't close inter-process sockets */ | |
1760 | continue; | |
1761 | ||
b0469965 | 1762 | if (F->timeoutHandler != NULL) { |
1763 | AsyncCall::Pointer callback = F->timeoutHandler; | |
1764 | F->timeoutHandler = NULL; | |
bf8fe701 | 1765 | debugs(5, 5, "commCloseAllSockets: FD " << fd << ": Calling timeout handler"); |
9e008dda | 1766 | ScheduleCallHere(callback); |
62e76326 | 1767 | } else { |
468fe1b5 | 1768 | debugs(5, 5, "commCloseAllSockets: FD " << fd << ": calling comm_reset_close()"); |
5c336a3b | 1769 | old_comm_reset_close(fd); |
62e76326 | 1770 | } |
d723bf6b | 1771 | } |
1772 | } | |
1b3db6d9 | 1773 | |
2d8c0b1a | 1774 | static bool |
e1381638 AJ |
1775 | AlreadyTimedOut(fde *F) |
1776 | { | |
2d8c0b1a | 1777 | if (!F->flags.open) |
1778 | return true; | |
1779 | ||
1780 | if (F->timeout == 0) | |
1781 | return true; | |
1782 | ||
1783 | if (F->timeout > squid_curtime) | |
1784 | return true; | |
1785 | ||
1786 | return false; | |
1787 | } | |
1788 | ||
5ef5e5cc AJ |
1789 | static bool |
1790 | writeTimedOut(int fd) | |
1791 | { | |
ec41b64c | 1792 | if (!COMMIO_FD_WRITECB(fd)->active()) |
5ef5e5cc AJ |
1793 | return false; |
1794 | ||
1795 | if ((squid_curtime - fd_table[fd].writeStart) < Config.Timeout.write) | |
1796 | return false; | |
1797 | ||
1798 | return true; | |
1799 | } | |
1800 | ||
1b3db6d9 | 1801 | void |
e1381638 AJ |
1802 | checkTimeouts(void) |
1803 | { | |
1b3db6d9 | 1804 | int fd; |
1805 | fde *F = NULL; | |
b0469965 | 1806 | AsyncCall::Pointer callback; |
62e76326 | 1807 | |
95dc7ff4 | 1808 | for (fd = 0; fd <= Biggest_FD; ++fd) { |
62e76326 | 1809 | F = &fd_table[fd]; |
1810 | ||
5ef5e5cc AJ |
1811 | if (writeTimedOut(fd)) { |
1812 | // We have an active write callback and we are timed out | |
52f6ea9e | 1813 | debugs(5, 5, "checkTimeouts: FD " << fd << " auto write timeout"); |
d841c88d | 1814 | Comm::SetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0); |
ec41b64c | 1815 | COMMIO_FD_WRITECB(fd)->finish(COMM_ERROR, ETIMEDOUT); |
307b696e | 1816 | } else if (AlreadyTimedOut(F)) |
62e76326 | 1817 | continue; |
1818 | ||
9e008dda | 1819 | debugs(5, 5, "checkTimeouts: FD " << fd << " Expired"); |
62e76326 | 1820 | |
b0469965 | 1821 | if (F->timeoutHandler != NULL) { |
bf8fe701 | 1822 | debugs(5, 5, "checkTimeouts: FD " << fd << ": Call timeout handler"); |
b0469965 | 1823 | callback = F->timeoutHandler; |
1824 | F->timeoutHandler = NULL; | |
9e008dda | 1825 | ScheduleCallHere(callback); |
62e76326 | 1826 | } else { |
bf8fe701 | 1827 | debugs(5, 5, "checkTimeouts: FD " << fd << ": Forcing comm_close()"); |
62e76326 | 1828 | comm_close(fd); |
1829 | } | |
b5443c04 | 1830 | } |
1831 | } | |
1832 | ||
9e008dda AJ |
1833 | /// Start waiting for a possibly half-closed connection to close |
1834 | // by scheduling a read callback to a monitoring handler that | |
82ec8dfc | 1835 | // will close the connection on read errors. |
a46d2c0e | 1836 | void |
e1381638 AJ |
1837 | commStartHalfClosedMonitor(int fd) |
1838 | { | |
74257126 | 1839 | debugs(5, 5, HERE << "adding FD " << fd << " to " << *TheHalfClosed); |
82ec8dfc AR |
1840 | assert(isOpen(fd)); |
1841 | assert(!commHasHalfClosedMonitor(fd)); | |
74257126 AR |
1842 | (void)TheHalfClosed->add(fd); // could also assert the result |
1843 | commPlanHalfClosedCheck(); // may schedule check if we added the first FD | |
1844 | } | |
1845 | ||
1846 | static | |
1847 | void | |
e1381638 AJ |
1848 | commPlanHalfClosedCheck() |
1849 | { | |
74257126 AR |
1850 | if (!WillCheckHalfClosed && !TheHalfClosed->empty()) { |
1851 | eventAdd("commHalfClosedCheck", &commHalfClosedCheck, NULL, 1.0, 1); | |
1852 | WillCheckHalfClosed = true; | |
1853 | } | |
1854 | } | |
1855 | ||
1856 | /// iterates over all descriptors that may need half-closed tests and | |
1857 | /// calls comm_read for those that do; re-schedules the check if needed | |
1858 | static | |
1859 | void | |
e1381638 AJ |
1860 | commHalfClosedCheck(void *) |
1861 | { | |
74257126 AR |
1862 | debugs(5, 5, HERE << "checking " << *TheHalfClosed); |
1863 | ||
1864 | typedef DescriptorSet::const_iterator DSCI; | |
1865 | const DSCI end = TheHalfClosed->end(); | |
1866 | for (DSCI i = TheHalfClosed->begin(); i != end; ++i) { | |
ec20038e AJ |
1867 | Comm::ConnectionPointer c = new Comm::Connection; // XXX: temporary. make HalfClosed a list of these. |
1868 | c->fd = *i; | |
1869 | if (!fd_table[c->fd].halfClosedReader) { // not reading already | |
74257126 | 1870 | AsyncCall::Pointer call = commCbCall(5,4, "commHalfClosedReader", |
9e008dda | 1871 | CommIoCbPtrFun(&commHalfClosedReader, NULL)); |
ec20038e AJ |
1872 | comm_read(c, NULL, 0, call); |
1873 | fd_table[c->fd].halfClosedReader = call; | |
1874 | } else | |
1875 | c->fd = -1; // XXX: temporary. prevent c replacement erase closing listed FD | |
74257126 | 1876 | } |
f900210a | 1877 | |
74257126 AR |
1878 | WillCheckHalfClosed = false; // as far as we know |
1879 | commPlanHalfClosedCheck(); // may need to check again | |
f900210a | 1880 | } |
1881 | ||
82ec8dfc AR |
1882 | /// checks whether we are waiting for possibly half-closed connection to close |
1883 | // We are monitoring if the read handler for the fd is the monitoring handler. | |
1884 | bool | |
e1381638 AJ |
1885 | commHasHalfClosedMonitor(int fd) |
1886 | { | |
74257126 | 1887 | return TheHalfClosed->has(fd); |
a46d2c0e | 1888 | } |
1889 | ||
82ec8dfc AR |
1890 | /// stop waiting for possibly half-closed connection to close |
1891 | static void | |
e1381638 AJ |
1892 | commStopHalfClosedMonitor(int const fd) |
1893 | { | |
74257126 AR |
1894 | debugs(5, 5, HERE << "removing FD " << fd << " from " << *TheHalfClosed); |
1895 | ||
1896 | // cancel the read if one was scheduled | |
1897 | AsyncCall::Pointer reader = fd_table[fd].halfClosedReader; | |
1898 | if (reader != NULL) | |
1899 | comm_read_cancel(fd, reader); | |
1900 | fd_table[fd].halfClosedReader = NULL; | |
1901 | ||
1902 | TheHalfClosed->del(fd); | |
a46d2c0e | 1903 | } |
1904 | ||
82ec8dfc AR |
1905 | /// I/O handler for the possibly half-closed connection monitoring code |
1906 | static void | |
e0d28505 | 1907 | commHalfClosedReader(const Comm::ConnectionPointer &conn, char *, size_t size, comm_err_t flag, int, void *) |
e1381638 | 1908 | { |
82ec8dfc | 1909 | // there cannot be more data coming in on half-closed connections |
9e008dda | 1910 | assert(size == 0); |
e0d28505 AJ |
1911 | assert(conn != NULL); |
1912 | assert(commHasHalfClosedMonitor(conn->fd)); // or we would have canceled the read | |
74257126 | 1913 | |
e0d28505 | 1914 | fd_table[conn->fd].halfClosedReader = NULL; // done reading, for now |
a46d2c0e | 1915 | |
82ec8dfc AR |
1916 | // nothing to do if fd is being closed |
1917 | if (flag == COMM_ERR_CLOSING) | |
1918 | return; | |
a46d2c0e | 1919 | |
82ec8dfc AR |
1920 | // if read failed, close the connection |
1921 | if (flag != COMM_OK) { | |
e0d28505 | 1922 | debugs(5, 3, HERE << "closing " << conn); |
80463bb4 | 1923 | conn->close(); |
82ec8dfc AR |
1924 | return; |
1925 | } | |
a46d2c0e | 1926 | |
82ec8dfc | 1927 | // continue waiting for close or error |
74257126 | 1928 | commPlanHalfClosedCheck(); // make sure this fd will be checked again |
a46d2c0e | 1929 | } |
a46d2c0e | 1930 | |
3e4bebf8 | 1931 | CommRead::CommRead() : conn(NULL), buf(NULL), len(0), callback(NULL) {} |
a46d2c0e | 1932 | |
3e4bebf8 AJ |
1933 | CommRead::CommRead(const Comm::ConnectionPointer &c, char *buf_, int len_, AsyncCall::Pointer &callback_) |
1934 | : conn(c), buf(buf_), len(len_), callback(callback_) {} | |
a46d2c0e | 1935 | |
a50bfe93 | 1936 | DeferredRead::DeferredRead () : theReader(NULL), theContext(NULL), theRead(), cancelled(false) {} |
a46d2c0e | 1937 | |
a50bfe93 | 1938 | DeferredRead::DeferredRead (DeferrableRead *aReader, void *data, CommRead const &aRead) : theReader(aReader), theContext (data), theRead(aRead), cancelled(false) {} |
a46d2c0e | 1939 | |
e1381638 AJ |
1940 | DeferredReadManager::~DeferredReadManager() |
1941 | { | |
a46d2c0e | 1942 | flushReads(); |
1943 | assert (deferredReads.empty()); | |
1944 | } | |
1945 | ||
97427e90 | 1946 | /* explicit instantiation required for some systems */ |
1947 | ||
63be0a78 | 1948 | /// \cond AUTODOCS-IGNORE |
2236466c | 1949 | template cbdata_type CbDataList<DeferredRead>::CBDATA_CbDataList; |
63be0a78 | 1950 | /// \endcond |
97427e90 | 1951 | |
a46d2c0e | 1952 | void |
e1381638 AJ |
1953 | DeferredReadManager::delayRead(DeferredRead const &aRead) |
1954 | { | |
3e4bebf8 | 1955 | debugs(5, 3, "Adding deferred read on " << aRead.theRead.conn); |
2236466c | 1956 | CbDataList<DeferredRead> *temp = deferredReads.push_back(aRead); |
2796c0d7 | 1957 | |
9e008dda | 1958 | // We have to use a global function as a closer and point to temp |
2796c0d7 AR |
1959 | // instead of "this" because DeferredReadManager is not a job and |
1960 | // is not even cbdata protected | |
575d05c4 | 1961 | // XXX: and yet we use cbdata protection functions on it?? |
2796c0d7 | 1962 | AsyncCall::Pointer closer = commCbCall(5,4, |
9e008dda AJ |
1963 | "DeferredReadManager::CloseHandler", |
1964 | CommCloseCbPtrFun(&CloseHandler, temp)); | |
3e4bebf8 | 1965 | comm_add_close_handler(aRead.theRead.conn->fd, closer); |
2796c0d7 | 1966 | temp->element.closer = closer; // remeber so that we can cancel |
a46d2c0e | 1967 | } |
1968 | ||
1969 | void | |
575d05c4 | 1970 | DeferredReadManager::CloseHandler(const CommCloseCbParams ¶ms) |
e1381638 | 1971 | { |
575d05c4 | 1972 | if (!cbdataReferenceValid(params.data)) |
a46d2c0e | 1973 | return; |
1974 | ||
575d05c4 | 1975 | CbDataList<DeferredRead> *temp = (CbDataList<DeferredRead> *)params.data; |
a46d2c0e | 1976 | |
2796c0d7 | 1977 | temp->element.closer = NULL; |
a46d2c0e | 1978 | temp->element.markCancelled(); |
1979 | } | |
1980 | ||
1981 | DeferredRead | |
e1381638 AJ |
1982 | DeferredReadManager::popHead(CbDataListContainer<DeferredRead> &deferredReads) |
1983 | { | |
a46d2c0e | 1984 | assert (!deferredReads.empty()); |
1985 | ||
2796c0d7 | 1986 | DeferredRead &read = deferredReads.head->element; |
e7e8ebdb AJ |
1987 | |
1988 | // NOTE: at this point the connection has been paused/stalled for an unknown | |
1989 | // amount of time. We must re-validate that it is active and usable. | |
1990 | ||
1991 | // If the connection has been closed already. Cancel this read. | |
1992 | if (!Comm::IsConnOpen(read.theRead.conn)) { | |
1993 | if (read.closer != NULL) { | |
1994 | read.closer->cancel("Connection closed before."); | |
1995 | read.closer = NULL; | |
1996 | } | |
1997 | read.markCancelled(); | |
1998 | } | |
1999 | ||
2796c0d7 | 2000 | if (!read.cancelled) { |
3e4bebf8 | 2001 | comm_remove_close_handler(read.theRead.conn->fd, read.closer); |
2796c0d7 AR |
2002 | read.closer = NULL; |
2003 | } | |
a46d2c0e | 2004 | |
2005 | DeferredRead result = deferredReads.pop_front(); | |
2006 | ||
2007 | return result; | |
2008 | } | |
2009 | ||
2010 | void | |
e1381638 AJ |
2011 | DeferredReadManager::kickReads(int const count) |
2012 | { | |
2236466c | 2013 | /* if we had CbDataList::size() we could consolidate this and flushReads */ |
a46d2c0e | 2014 | |
33cea91c | 2015 | if (count < 1) { |
a46d2c0e | 2016 | flushReads(); |
33cea91c | 2017 | return; |
2018 | } | |
a46d2c0e | 2019 | |
2020 | size_t remaining = count; | |
2021 | ||
2022 | while (!deferredReads.empty() && remaining) { | |
2023 | DeferredRead aRead = popHead(deferredReads); | |
2024 | kickARead(aRead); | |
2025 | ||
2026 | if (!aRead.cancelled) | |
2027 | --remaining; | |
2028 | } | |
2029 | } | |
2030 | ||
2031 | void | |
e1381638 AJ |
2032 | DeferredReadManager::flushReads() |
2033 | { | |
2236466c | 2034 | CbDataListContainer<DeferredRead> reads; |
a46d2c0e | 2035 | reads = deferredReads; |
2236466c | 2036 | deferredReads = CbDataListContainer<DeferredRead>(); |
a46d2c0e | 2037 | |
27b24462 | 2038 | // XXX: For fairness this SHOULD randomize the order |
a46d2c0e | 2039 | while (!reads.empty()) { |
2040 | DeferredRead aRead = popHead(reads); | |
2041 | kickARead(aRead); | |
2042 | } | |
2043 | } | |
2044 | ||
2045 | void | |
e1381638 AJ |
2046 | DeferredReadManager::kickARead(DeferredRead const &aRead) |
2047 | { | |
a46d2c0e | 2048 | if (aRead.cancelled) |
2049 | return; | |
2050 | ||
3e4bebf8 | 2051 | if (Comm::IsConnOpen(aRead.theRead.conn) && fd_table[aRead.theRead.conn->fd].closing()) |
af6a12ee | 2052 | return; |
ed2c738b | 2053 | |
3e4bebf8 | 2054 | debugs(5, 3, "Kicking deferred read on " << aRead.theRead.conn); |
a46d2c0e | 2055 | |
2056 | aRead.theReader(aRead.theContext, aRead.theRead); | |
2057 | } | |
2058 | ||
2059 | void | |
e1381638 AJ |
2060 | DeferredRead::markCancelled() |
2061 | { | |
a46d2c0e | 2062 | cancelled = true; |
2063 | } | |
2d8c0b1a | 2064 | |
8ff3fa2e | 2065 | int |
e1381638 AJ |
2066 | CommSelectEngine::checkEvents(int timeout) |
2067 | { | |
fa3f745b | 2068 | static time_t last_timeout = 0; |
2069 | ||
2070 | /* No, this shouldn't be here. But it shouldn't be in each comm handler. -adrian */ | |
2071 | if (squid_curtime > last_timeout) { | |
2072 | last_timeout = squid_curtime; | |
2073 | checkTimeouts(); | |
2074 | } | |
2075 | ||
d841c88d | 2076 | switch (Comm::DoSelect(timeout)) { |
8ff3fa2e | 2077 | |
2078 | case COMM_OK: | |
2079 | ||
2080 | case COMM_TIMEOUT: | |
2081 | return 0; | |
2082 | ||
2083 | case COMM_IDLE: | |
2084 | ||
2085 | case COMM_SHUTDOWN: | |
2086 | return EVENT_IDLE; | |
2087 | ||
2088 | case COMM_ERROR: | |
2089 | return EVENT_ERROR; | |
2090 | ||
2091 | default: | |
2092 | fatal_dump("comm.cc: Internal error -- this should never happen."); | |
2093 | return EVENT_ERROR; | |
2094 | }; | |
2095 | } | |
10cefb7b | 2096 | |
0ffda73c | 2097 | /// Create a unix-domain socket (UDS) that only supports FD_MSGHDR I/O. |
10cefb7b | 2098 | int |
2099 | comm_open_uds(int sock_type, | |
2100 | int proto, | |
2101 | struct sockaddr_un* addr, | |
2102 | int flags) | |
2103 | { | |
a67d2b2e | 2104 | // TODO: merge with comm_openex() when Ip::Address becomes NetAddress |
ba568924 | 2105 | |
10cefb7b | 2106 | int new_socket; |
10cefb7b | 2107 | |
2108 | PROF_start(comm_open); | |
10cefb7b | 2109 | /* Create socket for accepting new connections. */ |
95dc7ff4 | 2110 | ++ statCounter.syscalls.sock.sockets; |
10cefb7b | 2111 | |
2112 | /* Setup the socket addrinfo details for use */ | |
ba568924 | 2113 | struct addrinfo AI; |
10cefb7b | 2114 | AI.ai_flags = 0; |
2115 | AI.ai_family = PF_UNIX; | |
2116 | AI.ai_socktype = sock_type; | |
2117 | AI.ai_protocol = proto; | |
2118 | AI.ai_addrlen = SUN_LEN(addr); | |
2119 | AI.ai_addr = (sockaddr*)addr; | |
2120 | AI.ai_canonname = NULL; | |
2121 | AI.ai_next = NULL; | |
2122 | ||
ba568924 | 2123 | debugs(50, 3, HERE << "Attempt open socket for: " << addr->sun_path); |
10cefb7b | 2124 | |
2125 | if ((new_socket = socket(AI.ai_family, AI.ai_socktype, AI.ai_protocol)) < 0) { | |
2126 | /* Increase the number of reserved fd's if calls to socket() | |
2127 | * are failing because the open file table is full. This | |
2128 | * limits the number of simultaneous clients */ | |
2129 | ||
2130 | if (limitError(errno)) { | |
ba568924 | 2131 | debugs(50, DBG_IMPORTANT, HERE << "socket failure: " << xstrerror()); |
10cefb7b | 2132 | fdAdjustReserved(); |
2133 | } else { | |
ba568924 | 2134 | debugs(50, DBG_CRITICAL, HERE << "socket failure: " << xstrerror()); |
10cefb7b | 2135 | } |
2136 | ||
2137 | PROF_stop(comm_open); | |
2138 | return -1; | |
2139 | } | |
2140 | ||
ba568924 | 2141 | debugs(50, 3, HERE "Opened UDS FD " << new_socket << " : family=" << AI.ai_family << ", type=" << AI.ai_socktype << ", protocol=" << AI.ai_protocol); |
10cefb7b | 2142 | |
2143 | /* update fdstat */ | |
ba568924 | 2144 | debugs(50, 5, HERE << "FD " << new_socket << " is a new socket"); |
10cefb7b | 2145 | |
2146 | assert(!isOpen(new_socket)); | |
1bac0258 | 2147 | fd_open(new_socket, FD_MSGHDR, NULL); |
10cefb7b | 2148 | |
2149 | fdd_table[new_socket].close_file = NULL; | |
ba568924 | 2150 | |
10cefb7b | 2151 | fdd_table[new_socket].close_line = 0; |
2152 | ||
ba568924 AR |
2153 | fd_table[new_socket].sock_family = AI.ai_family; |
2154 | ||
10cefb7b | 2155 | if (!(flags & COMM_NOCLOEXEC)) |
2156 | commSetCloseOnExec(new_socket); | |
2157 | ||
2158 | if (flags & COMM_REUSEADDR) | |
2159 | commSetReuseAddr(new_socket); | |
2160 | ||
2161 | if (flags & COMM_NONBLOCKING) { | |
2162 | if (commSetNonBlocking(new_socket) != COMM_OK) { | |
2163 | comm_close(new_socket); | |
2164 | PROF_stop(comm_open); | |
2165 | return -1; | |
2166 | } | |
2167 | } | |
2168 | ||
2169 | if (flags & COMM_DOBIND) { | |
2170 | if (commBind(new_socket, AI) != COMM_OK) { | |
2171 | comm_close(new_socket); | |
2172 | PROF_stop(comm_open); | |
2173 | return -1; | |
2174 | } | |
2175 | } | |
2176 | ||
2177 | #ifdef TCP_NODELAY | |
2178 | if (sock_type == SOCK_STREAM) | |
2179 | commSetTcpNoDelay(new_socket); | |
2180 | ||
2181 | #endif | |
2182 | ||
2183 | if (Config.tcpRcvBufsz > 0 && sock_type == SOCK_STREAM) | |
2184 | commSetTcpRcvbuf(new_socket, Config.tcpRcvBufsz); | |
2185 | ||
2186 | PROF_stop(comm_open); | |
2187 | ||
2188 | return new_socket; | |
2189 | } |