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