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