]>
Commit | Line | Data |
---|---|---|
cc192b50 | 1 | |
30a4f2a8 | 2 | /* |
63be0a78 | 3 | * $Id: comm.cc,v 1.447 2008/02/26 21:49:34 amosjeffries Exp $ |
30a4f2a8 | 4 | * |
5 | * DEBUG: section 5 Socket Functions | |
6 | * AUTHOR: Harvest Derived | |
7 | * | |
2b6662ba | 8 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
e25c139f | 9 | * ---------------------------------------------------------- |
30a4f2a8 | 10 | * |
2b6662ba | 11 | * Squid is the result of efforts by numerous individuals from |
12 | * the Internet community; see the CONTRIBUTORS file for full | |
13 | * details. Many organizations have provided support for Squid's | |
14 | * development; see the SPONSORS file for full details. Squid is | |
15 | * Copyrighted (C) 2001 by the Regents of the University of | |
16 | * California; see the COPYRIGHT file for full details. Squid | |
17 | * incorporates software developed and/or copyrighted by other | |
18 | * sources; see the CREDITS file for full details. | |
30a4f2a8 | 19 | * |
20 | * This program is free software; you can redistribute it and/or modify | |
21 | * it under the terms of the GNU General Public License as published by | |
22 | * the Free Software Foundation; either version 2 of the License, or | |
23 | * (at your option) any later version. | |
24 | * | |
25 | * This program is distributed in the hope that it will be useful, | |
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
28 | * GNU General Public License for more details. | |
29 | * | |
30 | * You should have received a copy of the GNU General Public License | |
31 | * along with this program; if not, write to the Free Software | |
cbdec147 | 32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
e25c139f | 33 | * |
2d8c0b1a | 34 | * |
35 | * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org> | |
30a4f2a8 | 36 | */ |
090089c4 | 37 | |
44a47c6e | 38 | #include "squid.h" |
c4b7a5a9 | 39 | #include "StoreIOBuffer.h" |
40 | #include "comm.h" | |
a553a5a3 | 41 | #include "event.h" |
528b2c61 | 42 | #include "fde.h" |
56410c89 | 43 | #include "CommIO.h" |
a553a5a3 | 44 | #include "CommRead.h" |
ee0989f2 | 45 | #include "ConnectionDetail.h" |
0eb49b6d | 46 | #include "MemBuf.h" |
781ce8ff | 47 | #include "pconn.h" |
985c86bc | 48 | #include "SquidTime.h" |
b0469965 | 49 | #include "CommCalls.h" |
cc192b50 | 50 | #include "IPAddress.h" |
3949d8b7 | 51 | #include "IPInterception.h" |
74257126 | 52 | #include "DescriptorSet.h" |
090089c4 | 53 | |
b671cc68 | 54 | #if defined(_SQUID_CYGWIN_) |
55 | #include <sys/ioctl.h> | |
56 | #endif | |
30a4f2a8 | 57 | #ifdef HAVE_NETINET_TCP_H |
58 | #include <netinet/tcp.h> | |
59 | #endif | |
090089c4 | 60 | |
2b663917 | 61 | /* |
62 | * New C-like simple comm code. This stuff is a mess and doesn't really buy us anything. | |
63 | */ | |
64 | ||
65 | typedef enum { | |
66 | IOCB_NONE, | |
67 | IOCB_READ, | |
68 | IOCB_WRITE | |
69 | } iocb_type; | |
70 | ||
82ec8dfc AR |
71 | static void commStopHalfClosedMonitor(int fd); |
72 | static IOCB commHalfClosedReader; | |
73 | ||
74 | ||
b0469965 | 75 | struct comm_io_callback_t { |
2b663917 | 76 | iocb_type type; |
77 | int fd; | |
b0469965 | 78 | AsyncCall::Pointer callback; |
2b663917 | 79 | char *buf; |
80 | FREE *freefunc; | |
81 | int size; | |
82 | int offset; | |
2b663917 | 83 | comm_err_t errcode; |
84 | int xerrno; | |
b0469965 | 85 | |
86 | bool active() const { return callback != NULL; } | |
2b663917 | 87 | }; |
2b663917 | 88 | |
89 | struct _comm_fd { | |
90 | int fd; | |
91 | comm_io_callback_t readcb; | |
92 | comm_io_callback_t writecb; | |
93 | }; | |
94 | typedef struct _comm_fd comm_fd_t; | |
95 | comm_fd_t *commfd_table; | |
96 | ||
b0469965 | 97 | // TODO: make this a comm_io_callback_t method? |
2b663917 | 98 | bool |
99 | commio_has_callback(int fd, iocb_type type, comm_io_callback_t *ccb) | |
100 | { | |
101 | assert(ccb->fd == fd); | |
102 | assert(ccb->type == type); | |
b0469965 | 103 | return ccb->active(); |
2b663917 | 104 | } |
105 | ||
106 | /* | |
b0469965 | 107 | * Configure comm_io_callback_t for I/O |
2b663917 | 108 | * |
109 | * @param fd filedescriptor | |
110 | * @param ccb comm io callback | |
111 | * @param cb callback | |
112 | * @param cbdata callback data (must be cbdata'ed) | |
113 | * @param buf buffer, if applicable | |
114 | * @param freefunc freefunc, if applicable | |
115 | * @param size buffer size | |
116 | */ | |
b0469965 | 117 | static void |
118 | commio_set_callback(int fd, iocb_type type, comm_io_callback_t *ccb, | |
119 | AsyncCall::Pointer &cb, char *buf, FREE *freefunc, int size) | |
2b663917 | 120 | { |
b0469965 | 121 | assert(!ccb->active()); |
2b663917 | 122 | assert(ccb->type == type); |
b0469965 | 123 | assert(cb != NULL); |
2b663917 | 124 | ccb->fd = fd; |
125 | ccb->callback = cb; | |
2b663917 | 126 | ccb->buf = buf; |
127 | ccb->freefunc = freefunc; | |
128 | ccb->size = size; | |
2b663917 | 129 | ccb->offset = 0; |
130 | } | |
131 | ||
132 | ||
b0469965 | 133 | // Schedule the callback call and clear the callback |
134 | static void | |
135 | commio_finish_callback(int fd, comm_io_callback_t *ccb, comm_err_t code, int xerrno) | |
2b663917 | 136 | { |
b0469965 | 137 | debugs(5, 3, "commio_finish_callback: called for FD " << fd << " (" << |
138 | code << ", " << xerrno << ")"); | |
139 | assert(ccb->active()); | |
2b663917 | 140 | assert(ccb->fd == fd); |
141 | ccb->errcode = code; | |
142 | ccb->xerrno = xerrno; | |
b0469965 | 143 | |
144 | comm_io_callback_t cb = *ccb; | |
145 | ||
146 | /* We've got a copy; blow away the real one */ | |
147 | /* XXX duplicate code from commio_cancel_callback! */ | |
148 | ccb->xerrno = 0; | |
149 | ccb->callback = NULL; // cb has it | |
150 | ||
151 | /* free data */ | |
152 | if (cb.freefunc) { | |
153 | cb.freefunc(cb.buf); | |
154 | cb.buf = NULL; | |
155 | } | |
156 | ||
157 | if (cb.callback != NULL) { | |
158 | typedef CommIoCbParams Params; | |
159 | Params ¶ms = GetCommParams<Params>(cb.callback); | |
160 | params.fd = cb.fd; | |
161 | params.buf = cb.buf; | |
162 | params.size = cb.offset; | |
163 | params.flag = cb.errcode; | |
164 | params.xerrno = cb.xerrno; | |
165 | ScheduleCallHere(cb.callback); | |
166 | } | |
2b663917 | 167 | } |
168 | ||
169 | ||
170 | /* | |
171 | * Cancel the given callback | |
172 | * | |
173 | * Remember that the data is cbdataRef'ed. | |
174 | */ | |
b0469965 | 175 | // TODO: make this a comm_io_callback_t method |
176 | static void | |
2b663917 | 177 | commio_cancel_callback(int fd, comm_io_callback_t *ccb) |
178 | { | |
b0469965 | 179 | debugs(5, 3, "commio_cancel_callback: called for FD " << fd); |
2b663917 | 180 | assert(ccb->fd == fd); |
b0469965 | 181 | assert(ccb->active()); |
2b663917 | 182 | |
183 | ccb->xerrno = 0; | |
2b663917 | 184 | ccb->callback = NULL; |
2b663917 | 185 | } |
186 | ||
187 | /* | |
188 | * Call the given comm callback; assumes the callback is valid. | |
189 | * | |
190 | * @param ccb io completion callback | |
191 | */ | |
192 | void | |
193 | commio_call_callback(comm_io_callback_t *ccb) | |
194 | { | |
2b663917 | 195 | } |
196 | ||
2d8c0b1a | 197 | class ConnectStateData |
62e76326 | 198 | { |
2d8c0b1a | 199 | |
200 | public: | |
d2d59a68 | 201 | void *operator new (size_t); |
202 | void operator delete (void *); | |
2d8c0b1a | 203 | static void Connect (int fd, void *me); |
204 | void connect(); | |
205 | void callCallback(comm_err_t status, int xerrno); | |
206 | void defaults(); | |
cc192b50 | 207 | |
208 | // defaults given by client | |
f88211e8 | 209 | char *host; |
cc192b50 | 210 | u_short default_port; |
211 | IPAddress default_addr; | |
212 | // NP: CANNOT store the default addr:port together as it gets set/reset differently. | |
62e76326 | 213 | |
cc192b50 | 214 | IPAddress S; |
b0469965 | 215 | AsyncCall::Pointer callback; |
62e76326 | 216 | |
03a1ee42 | 217 | int fd; |
22c653cd | 218 | int tries; |
219 | int addrcount; | |
220 | int connstart; | |
d2d59a68 | 221 | |
222 | private: | |
0b77ecd8 | 223 | int commResetFD(); |
224 | int commRetryConnect(); | |
d2d59a68 | 225 | CBDATA_CLASS(ConnectStateData); |
2d8c0b1a | 226 | }; |
f88211e8 | 227 | |
090089c4 | 228 | /* STATIC */ |
62e76326 | 229 | |
74257126 AR |
230 | static DescriptorSet *TheHalfClosed = NULL; /// the set of half-closed FDs |
231 | static bool WillCheckHalfClosed = false; /// true if check is scheduled | |
232 | static EVH commHalfClosedCheck; | |
233 | static void commPlanHalfClosedCheck(); | |
234 | ||
cc192b50 | 235 | static comm_err_t commBind(int s, struct addrinfo &); |
f5b8bbc4 | 236 | static void commSetReuseAddr(int); |
237 | static void commSetNoLinger(int); | |
30a4f2a8 | 238 | #ifdef TCP_NODELAY |
f5b8bbc4 | 239 | static void commSetTcpNoDelay(int); |
30a4f2a8 | 240 | #endif |
f5b8bbc4 | 241 | static void commSetTcpRcvbuf(int, int); |
f88211e8 | 242 | static PF commConnectFree; |
03a1ee42 | 243 | static PF commHandleWrite; |
edeb28fd | 244 | static IPH commConnectDnsHandle; |
723123a9 | 245 | |
3c1a197f | 246 | static PF comm_accept_try; |
c4b7a5a9 | 247 | |
62e76326 | 248 | class AcceptFD |
249 | { | |
250 | ||
251 | public: | |
b0469965 | 252 | AcceptFD(int aFd = -1): fd(aFd), theCallback(0), mayAcceptMore(false) {} |
62e76326 | 253 | |
b0469965 | 254 | void subscribe(AsyncCall::Pointer &call); |
255 | void acceptNext(); | |
256 | void notify(int newfd, comm_err_t, int xerrno, const ConnectionDetail &); | |
62e76326 | 257 | |
2d8c0b1a | 258 | int fd; |
545d554b | 259 | |
b0469965 | 260 | private: |
261 | bool acceptOne(); | |
62e76326 | 262 | |
b0469965 | 263 | AsyncCall::Pointer theCallback; |
264 | bool mayAcceptMore; | |
c4b7a5a9 | 265 | }; |
62e76326 | 266 | |
c4b7a5a9 | 267 | typedef enum { |
62e76326 | 268 | COMM_CB_READ = 1, |
2d8c0b1a | 269 | COMM_CB_DERIVED, |
c4b7a5a9 | 270 | } comm_callback_t; |
271 | ||
62e76326 | 272 | struct _fd_debug_t |
273 | { | |
43ae1d95 | 274 | char const *close_file; |
62e76326 | 275 | int close_line; |
c4b7a5a9 | 276 | }; |
62e76326 | 277 | |
c4b7a5a9 | 278 | typedef struct _fd_debug_t fd_debug_t; |
279 | ||
b001e822 | 280 | static MemAllocator *conn_close_pool = NULL; |
b0469965 | 281 | AcceptFD *fdc_table = NULL; // TODO: rename. And use Vector<>? |
c4b7a5a9 | 282 | fd_debug_t *fdd_table = NULL; |
62e76326 | 283 | |
b0469965 | 284 | static bool |
285 | isOpen(const int fd) | |
b300c36d | 286 | { |
b0469965 | 287 | return fd_table[fd].flags.open != 0; |
b300c36d | 288 | } |
289 | ||
e1a88700 | 290 | /** |
c4b7a5a9 | 291 | * Attempt a read |
292 | * | |
293 | * If the read attempt succeeds or fails, call the callback. | |
294 | * Else, wait for another IO notification. | |
295 | */ | |
2d8c0b1a | 296 | void |
2b663917 | 297 | commHandleRead(int fd, void *data) |
2d8c0b1a | 298 | { |
2b663917 | 299 | comm_io_callback_t *ccb = (comm_io_callback_t *) data; |
300 | ||
301 | assert(data == COMMIO_FD_READCB(fd)); | |
302 | assert(commio_has_callback(fd, IOCB_READ, ccb)); | |
62e76326 | 303 | /* Attempt a read */ |
304 | statCounter.syscalls.sock.reads++; | |
305 | errno = 0; | |
2d8c0b1a | 306 | int retval; |
2b663917 | 307 | retval = FD_READ_METHOD(fd, ccb->buf, ccb->size); |
bf8fe701 | 308 | debugs(5, 3, "comm_read_try: FD " << fd << ", size " << ccb->size << ", retval " << retval << ", errno " << errno); |
62e76326 | 309 | |
310 | if (retval < 0 && !ignoreErrno(errno)) { | |
bf8fe701 | 311 | debugs(5, 3, "comm_read_try: scheduling COMM_ERROR"); |
2b663917 | 312 | ccb->offset = 0; |
b0469965 | 313 | commio_finish_callback(fd, ccb, COMM_ERROR, errno); |
62e76326 | 314 | return; |
315 | }; | |
316 | ||
317 | /* See if we read anything */ | |
318 | /* Note - read 0 == socket EOF, which is a valid read */ | |
319 | if (retval >= 0) { | |
320 | fd_bytes(fd, retval, FD_READ); | |
b0469965 | 321 | ccb->offset = retval; |
322 | commio_finish_callback(fd, ccb, COMM_OK, errno); | |
62e76326 | 323 | return; |
324 | } | |
c4b7a5a9 | 325 | |
62e76326 | 326 | /* Nope, register for some more IO */ |
2b663917 | 327 | commSetSelect(fd, COMM_SELECT_READ, commHandleRead, data, 0); |
c4b7a5a9 | 328 | } |
329 | ||
e1a88700 | 330 | /** |
c4b7a5a9 | 331 | * Queue a read. handler/handler_data are called when the read |
332 | * completes, on error, or on file descriptor close. | |
333 | */ | |
334 | void | |
335 | comm_read(int fd, char *buf, int size, IOCB *handler, void *handler_data) | |
b0469965 | 336 | { |
337 | AsyncCall::Pointer call = commCbCall(5,4, "SomeCommReadHandler", | |
338 | CommIoCbPtrFun(handler, handler_data)); | |
339 | comm_read(fd, buf, size, call); | |
340 | } | |
341 | ||
342 | void | |
343 | comm_read(int fd, char *buf, int size, AsyncCall::Pointer &callback) | |
c4b7a5a9 | 344 | { |
82ec8dfc | 345 | debugs(5, 5, "comm_read, queueing read for FD " << fd << "; asynCall " << callback); |
c4b7a5a9 | 346 | |
82ec8dfc AR |
347 | /* Make sure we are open and not closing */ |
348 | assert(isOpen(fd)); | |
349 | assert(!fd_table[fd].closing()); | |
350 | comm_io_callback_t *ccb = COMMIO_FD_READCB(fd); | |
351 | ||
352 | // Make sure we are either not reading or just passively monitoring. | |
353 | // Active/passive conflicts are OK and simply cancel passive monitoring. | |
354 | if (ccb->active()) { | |
355 | // if the assertion below fails, we have an active comm_read conflict | |
74257126 | 356 | assert(fd_table[fd].halfClosedReader != NULL); |
82ec8dfc AR |
357 | commStopHalfClosedMonitor(fd); |
358 | assert(!ccb->active()); | |
359 | } | |
528b2c61 | 360 | |
2b663917 | 361 | /* Queue the read */ |
82ec8dfc AR |
362 | commio_set_callback(fd, IOCB_READ, ccb, callback, (char *)buf, NULL, size); |
363 | commSetSelect(fd, COMM_SELECT_READ, commHandleRead, ccb, 0); | |
c4b7a5a9 | 364 | } |
365 | ||
e1a88700 | 366 | /** |
c4b7a5a9 | 367 | * Empty the read buffers |
368 | * | |
369 | * This is a magical routine that empties the read buffers. | |
370 | * Under some platforms (Linux) if a buffer has data in it before | |
371 | * you call close(), the socket will hang and take quite a while | |
372 | * to timeout. | |
373 | */ | |
374 | static void | |
375 | comm_empty_os_read_buffers(int fd) | |
376 | { | |
a42d5c25 | 377 | #ifdef _SQUID_LINUX_ |
c4b7a5a9 | 378 | /* prevent those nasty RST packets */ |
379 | char buf[SQUID_TCP_SO_RCVBUF]; | |
62e76326 | 380 | |
cc192b50 | 381 | if (fd_table[fd].flags.nonblocking == 1) { |
382 | while (FD_READ_METHOD(fd, buf, SQUID_TCP_SO_RCVBUF) > 0) {}; | |
383 | } | |
c4b7a5a9 | 384 | #endif |
385 | } | |
386 | ||
387 | ||
e1a88700 | 388 | /** |
2b663917 | 389 | * Return whether the FD has a pending completed callback. |
c4b7a5a9 | 390 | */ |
391 | int | |
392 | comm_has_pending_read_callback(int fd) | |
393 | { | |
b0469965 | 394 | assert(isOpen(fd)); |
395 | // XXX: We do not know whether there is a read callback scheduled. | |
396 | // This is used for pconn management that should probably be more | |
397 | // tightly integrated into comm to minimize the chance that a | |
398 | // closing pconn socket will be used for a new transaction. | |
545d554b | 399 | return false; |
c4b7a5a9 | 400 | } |
401 | ||
b0469965 | 402 | // Does comm check this fd for read readiness? |
403 | // Note that when comm is not monitoring, there can be a pending callback | |
404 | // call, which may resume comm monitoring once fired. | |
528b2c61 | 405 | bool |
b0469965 | 406 | comm_monitors_read(int fd) |
c4b7a5a9 | 407 | { |
b0469965 | 408 | assert(isOpen(fd)); |
409 | // Being active is usually the same as monitoring because we always | |
410 | // start monitoring the FD when we configure comm_io_callback_t for I/O | |
411 | // and we usually configure comm_io_callback_t for I/O when we starting | |
412 | // monitoring a FD for reading. TODO: replace with commio_has_callback | |
413 | return COMMIO_FD_READCB(fd)->active(); | |
c4b7a5a9 | 414 | } |
415 | ||
e1a88700 | 416 | /** |
c4b7a5a9 | 417 | * Cancel a pending read. Assert that we have the right parameters, |
418 | * and that there are no pending read events! | |
2b663917 | 419 | * |
b0469965 | 420 | * XXX: We do not assert that there are no pending read events and |
421 | * with async calls it becomes even more difficult. | |
422 | * The whole interface should be reworked to do callback->cancel() | |
423 | * instead of searching for places where the callback may be stored and | |
424 | * updating the state of those places. | |
425 | * | |
2b663917 | 426 | * AHC Don't call the comm handlers? |
c4b7a5a9 | 427 | */ |
428 | void | |
429 | comm_read_cancel(int fd, IOCB *callback, void *data) | |
430 | { | |
b0469965 | 431 | if (!isOpen(fd)) { |
432 | debugs(5, 4, "comm_read_cancel fails: FD " << fd << " closed"); | |
433 | return; | |
434 | } | |
435 | ||
436 | comm_io_callback_t *cb = COMMIO_FD_READCB(fd); | |
437 | // TODO: is "active" == "monitors FD"? | |
438 | if (!cb->active()) { | |
439 | debugs(5, 4, "comm_read_cancel fails: FD " << fd << " inactive"); | |
440 | return; | |
441 | } | |
442 | ||
443 | typedef CommCbFunPtrCallT<CommIoCbPtrFun> Call; | |
444 | Call *call = dynamic_cast<Call*>(cb->callback.getRaw()); | |
445 | if (!call) { | |
446 | debugs(5, 4, "comm_read_cancel fails: FD " << fd << " lacks callback"); | |
447 | return; | |
448 | } | |
449 | ||
82ec8dfc AR |
450 | call->cancel("old comm_read_cancel"); |
451 | ||
b0469965 | 452 | typedef CommIoCbParams Params; |
453 | const Params ¶ms = GetCommParams<Params>(cb->callback); | |
c4b7a5a9 | 454 | |
c4b7a5a9 | 455 | /* Ok, we can be reasonably sure we won't lose any data here! */ |
b0469965 | 456 | assert(call->dialer.handler == callback); |
457 | assert(params.data == data); | |
c4b7a5a9 | 458 | |
459 | /* Delete the callback */ | |
b0469965 | 460 | commio_cancel_callback(fd, cb); |
420f2ac8 | 461 | |
462 | /* And the IO event */ | |
62e76326 | 463 | commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0); |
c4b7a5a9 | 464 | } |
465 | ||
c4b7a5a9 | 466 | void |
b0469965 | 467 | comm_read_cancel(int fd, AsyncCall::Pointer &callback) |
c4b7a5a9 | 468 | { |
b0469965 | 469 | callback->cancel("comm_read_cancel"); |
470 | ||
471 | if (!isOpen(fd)) { | |
472 | debugs(5, 4, "comm_read_cancel fails: FD " << fd << " closed"); | |
473 | return; | |
474 | } | |
475 | ||
476 | comm_io_callback_t *cb = COMMIO_FD_READCB(fd); | |
477 | ||
478 | if (!cb->active()) { | |
479 | debugs(5, 4, "comm_read_cancel fails: FD " << fd << " inactive"); | |
480 | return; | |
481 | } | |
482 | ||
483 | AsyncCall::Pointer call = cb->callback; | |
82ec8dfc | 484 | assert(call != NULL); // XXX: should never fail (active() checks for callback==NULL) |
b0469965 | 485 | |
486 | /* Ok, we can be reasonably sure we won't lose any data here! */ | |
487 | assert(call == callback); | |
c4b7a5a9 | 488 | |
b0469965 | 489 | /* Delete the callback */ |
490 | commio_cancel_callback(fd, cb); | |
491 | ||
492 | /* And the IO event */ | |
493 | commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0); | |
c4b7a5a9 | 494 | } |
495 | ||
496 | ||
e1a88700 | 497 | /** |
ce767c23 | 498 | * synchronous wrapper around udp socket functions |
499 | */ | |
ce767c23 | 500 | int |
cc192b50 | 501 | comm_udp_recvfrom(int fd, void *buf, size_t len, int flags, IPAddress &from) |
ce767c23 | 502 | { |
62e76326 | 503 | statCounter.syscalls.sock.recvfroms++; |
cc192b50 | 504 | int x = 0; |
505 | struct addrinfo *AI = NULL; | |
506 | ||
507 | debugs(5,8, "comm_udp_recvfrom: FD " << fd << " from " << from); | |
508 | ||
509 | assert( NULL == AI ); | |
510 | ||
511 | from.InitAddrInfo(AI); | |
512 | ||
513 | x = recvfrom(fd, buf, len, flags, AI->ai_addr, &AI->ai_addrlen); | |
514 | ||
515 | from = *AI; | |
516 | ||
517 | from.FreeAddrInfo(AI); | |
518 | ||
519 | return x; | |
ce767c23 | 520 | } |
521 | ||
365f12a9 | 522 | int |
7d21986b | 523 | comm_udp_recv(int fd, void *buf, size_t len, int flags) |
365f12a9 | 524 | { |
cc192b50 | 525 | IPAddress nul; |
526 | return comm_udp_recvfrom(fd, buf, len, flags, nul); | |
365f12a9 | 527 | } |
528 | ||
f71da12c | 529 | ssize_t |
7d21986b | 530 | comm_udp_send(int s, const void *buf, size_t len, int flags) |
f71da12c | 531 | { |
62e76326 | 532 | return send(s, buf, len, flags); |
f71da12c | 533 | } |
ce767c23 | 534 | |
535 | ||
545d554b | 536 | bool |
537 | comm_has_incomplete_write(int fd) | |
538 | { | |
b0469965 | 539 | assert(isOpen(fd)); |
540 | return COMMIO_FD_WRITECB(fd)->active(); | |
d4cb310b | 541 | } |
542 | ||
e1a88700 | 543 | /** |
cf3c0ee3 | 544 | * Queue a write. handler/handler_data are called when the write fully |
545 | * completes, on error, or on file descriptor close. | |
546 | */ | |
9864ee44 | 547 | |
090089c4 | 548 | /* Return the local port associated with fd. */ |
b8d8561b | 549 | u_short |
550 | comm_local_port(int fd) | |
090089c4 | 551 | { |
cc192b50 | 552 | IPAddress temp; |
553 | struct addrinfo *addr = NULL; | |
76f87348 | 554 | fde *F = &fd_table[fd]; |
090089c4 | 555 | |
090089c4 | 556 | /* If the fd is closed already, just return */ |
62e76326 | 557 | |
60c0b5a2 | 558 | if (!F->flags.open) { |
bf8fe701 | 559 | debugs(5, 0, "comm_local_port: FD " << fd << " has been closed."); |
62e76326 | 560 | return 0; |
090089c4 | 561 | } |
62e76326 | 562 | |
cc192b50 | 563 | if (F->local_addr.GetPort()) |
564 | return F->local_addr.GetPort(); | |
62e76326 | 565 | |
cc192b50 | 566 | temp.InitAddrInfo(addr); |
62e76326 | 567 | |
cc192b50 | 568 | if (getsockname(fd, addr->ai_addr, &(addr->ai_addrlen)) ) { |
bf8fe701 | 569 | debugs(50, 1, "comm_local_port: Failed to retrieve TCP/UDP port number for socket: FD " << fd << ": " << xstrerror()); |
cc192b50 | 570 | temp.FreeAddrInfo(addr); |
62e76326 | 571 | return 0; |
090089c4 | 572 | } |
cc192b50 | 573 | temp = *addr; |
574 | ||
575 | temp.FreeAddrInfo(addr); | |
576 | ||
577 | F->local_addr.SetPort(temp.GetPort()); | |
578 | ||
579 | // grab default socket information for this address | |
580 | temp.GetAddrInfo(addr); | |
581 | ||
582 | F->sock_family = addr->ai_family; | |
583 | ||
584 | temp.FreeAddrInfo(addr); | |
62e76326 | 585 | |
cc192b50 | 586 | debugs(5, 6, "comm_local_port: FD " << fd << ": port " << F->local_addr.GetPort()); |
587 | return F->local_addr.GetPort(); | |
090089c4 | 588 | } |
589 | ||
3d7e9d7c | 590 | static comm_err_t |
cc192b50 | 591 | commBind(int s, struct addrinfo &inaddr) |
090089c4 | 592 | { |
83704487 | 593 | statCounter.syscalls.sock.binds++; |
62e76326 | 594 | |
cc192b50 | 595 | if (bind(s, inaddr.ai_addr, inaddr.ai_addrlen) == 0) |
62e76326 | 596 | return COMM_OK; |
597 | ||
cc192b50 | 598 | debugs(50, 0, "commBind: Cannot bind socket FD " << s << " to " << fd_table[s].local_addr << ": " << xstrerror()); |
62e76326 | 599 | |
090089c4 | 600 | return COMM_ERROR; |
601 | } | |
602 | ||
e1a88700 | 603 | /** |
604 | * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE | |
605 | * is OR of flags specified in comm.h. Defaults TOS | |
606 | */ | |
b8d8561b | 607 | int |
16b204c4 | 608 | comm_open(int sock_type, |
62e76326 | 609 | int proto, |
cc192b50 | 610 | IPAddress &addr, |
62e76326 | 611 | int flags, |
612 | const char *note) | |
d6827718 | 613 | { |
cc192b50 | 614 | return comm_openex(sock_type, proto, addr, flags, 0, note); |
d6827718 | 615 | } |
616 | ||
2d8c0b1a | 617 | static bool |
618 | limitError(int const anErrno) | |
619 | { | |
620 | return anErrno == ENFILE || anErrno == EMFILE; | |
621 | } | |
d6827718 | 622 | |
057f5854 | 623 | int |
624 | comm_set_tos(int fd, int tos) | |
625 | { | |
626 | #ifdef IP_TOS | |
627 | int x = setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &tos, sizeof(int)); | |
628 | if (x < 0) | |
629 | debugs(50, 1, "comm_set_tos: setsockopt(IP_TOS) on FD " << fd << ": " << xstrerror()); | |
630 | return x; | |
631 | #else | |
e1a88700 | 632 | debugs(50, 0, "WARNING: setsockopt(IP_TOS) not supported on this platform"); |
e343a6ce | 633 | return -1; |
057f5854 | 634 | #endif |
635 | } | |
636 | ||
cc192b50 | 637 | void |
638 | comm_set_v6only(int fd, int tos) | |
639 | { | |
640 | #ifdef IPV6_V6ONLY | |
641 | if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &tos, sizeof(int)) < 0) { | |
642 | debugs(50, 1, "comm_open: setsockopt(IPV6_V6ONLY) on FD " << fd << ": " << xstrerror()); | |
643 | } | |
644 | #else | |
645 | debugs(50, 0, "WARNING: comm_open: setsockopt(IPV6_V6ONLY) not supported on this platform"); | |
646 | #endif /* sockopt */ | |
647 | } | |
057f5854 | 648 | |
40d6264d AJ |
649 | /** |
650 | * Set the socket IP_TRANSPARENT option for Linux TPROXY v4 support. | |
651 | */ | |
f1e0717c | 652 | void |
e950e673 | 653 | comm_set_transparent(int fd) |
f1e0717c | 654 | { |
2ad20b4f | 655 | #if defined(IP_TRANSPARENT) |
e950e673 | 656 | int tos = 1; |
ef88b51d AJ |
657 | if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &tos, sizeof(int)) < 0) { |
658 | debugs(50, DBG_IMPORTANT, "comm_open: setsockopt(IP_TRANSPARENT) on FD " << fd << ": " << xstrerror()); | |
f1e0717c | 659 | } |
3949d8b7 AJ |
660 | else { |
661 | /* mark the socket as having transparent options */ | |
662 | fd_table[fd].flags.transparent = 1; | |
663 | } | |
f1e0717c | 664 | #else |
ef88b51d | 665 | debugs(50, DBG_CRITICAL, "WARNING: comm_open: setsockopt(IP_TRANSPARENT) not supported on this platform"); |
f1e0717c AJ |
666 | #endif /* sockopt */ |
667 | } | |
668 | ||
e1a88700 | 669 | /** |
670 | * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE | |
671 | * is OR of flags specified in defines.h:COMM_* | |
672 | */ | |
d6827718 | 673 | int |
674 | comm_openex(int sock_type, | |
62e76326 | 675 | int proto, |
cc192b50 | 676 | IPAddress &addr, |
62e76326 | 677 | int flags, |
678 | unsigned char TOS, | |
679 | const char *note) | |
090089c4 | 680 | { |
681 | int new_socket; | |
76f87348 | 682 | fde *F = NULL; |
cc192b50 | 683 | int tos = 0; |
684 | struct addrinfo *AI = NULL; | |
090089c4 | 685 | |
88bfe092 | 686 | PROF_start(comm_open); |
090089c4 | 687 | /* Create socket for accepting new connections. */ |
83704487 | 688 | statCounter.syscalls.sock.sockets++; |
62e76326 | 689 | |
cc192b50 | 690 | /* Setup the socket addrinfo details for use */ |
691 | addr.GetAddrInfo(AI); | |
692 | AI->ai_socktype = sock_type; | |
693 | AI->ai_protocol = proto; | |
cc192b50 | 694 | |
695 | debugs(50, 3, "comm_openex: Attempt open socket for: " << addr ); | |
696 | ||
697 | if ((new_socket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol)) < 0) | |
62e76326 | 698 | { |
699 | /* Increase the number of reserved fd's if calls to socket() | |
700 | * are failing because the open file table is full. This | |
701 | * limits the number of simultaneous clients */ | |
702 | ||
2d8c0b1a | 703 | if (limitError(errno)) { |
bf8fe701 | 704 | debugs(50, 1, "comm_open: socket failure: " << xstrerror()); |
62e76326 | 705 | fdAdjustReserved(); |
2d8c0b1a | 706 | } else { |
bf8fe701 | 707 | debugs(50, 0, "comm_open: socket failure: " << xstrerror()); |
62e76326 | 708 | } |
709 | ||
cc192b50 | 710 | addr.FreeAddrInfo(AI); |
711 | ||
62e76326 | 712 | PROF_stop(comm_open); |
713 | return -1; | |
090089c4 | 714 | } |
62e76326 | 715 | |
cc192b50 | 716 | debugs(50, 3, "comm_openex: Opened socket FD " << new_socket << " : family=" << AI->ai_family << ", type=" << AI->ai_socktype << ", protocol=" << AI->ai_protocol ); |
717 | ||
d6827718 | 718 | /* set TOS if needed */ |
cc192b50 | 719 | if (TOS && comm_set_tos(new_socket, TOS) ) { |
62e76326 | 720 | tos = TOS; |
cc192b50 | 721 | } |
62e76326 | 722 | |
cc192b50 | 723 | #if IPV6_SPECIAL_SPLITSTACK |
62e76326 | 724 | |
cc192b50 | 725 | if( addr.IsIPv6() ) |
726 | comm_set_v6only(new_socket, tos); | |
62e76326 | 727 | |
d6827718 | 728 | #endif |
62e76326 | 729 | |
9b1f7ee8 | 730 | #if IPV6_SPECIAL_V4MAPPED |
cc192b50 | 731 | |
732 | /* Windows Vista supports Dual-Sockets. BUT defaults them to V6ONLY. Turn it OFF. */ | |
9b1f7ee8 | 733 | /* Other OS may have this administratively disabled for general use. Same deal. */ |
cc192b50 | 734 | if( addr.IsIPv6() ) |
735 | comm_set_v6only(new_socket, 0); | |
736 | ||
737 | #endif | |
62e76326 | 738 | |
090089c4 | 739 | /* update fdstat */ |
bf8fe701 | 740 | debugs(5, 5, "comm_open: FD " << new_socket << " is a new socket"); |
62e76326 | 741 | |
b0469965 | 742 | assert(!isOpen(new_socket)); |
5c5783a2 | 743 | fd_open(new_socket, FD_SOCKET, note); |
62e76326 | 744 | |
c4b7a5a9 | 745 | fdd_table[new_socket].close_file = NULL; |
62e76326 | 746 | |
c4b7a5a9 | 747 | fdd_table[new_socket].close_line = 0; |
62e76326 | 748 | |
76f87348 | 749 | F = &fd_table[new_socket]; |
62e76326 | 750 | |
d6827718 | 751 | F->local_addr = addr; |
62e76326 | 752 | |
cc192b50 | 753 | F->tos = TOS; |
754 | ||
755 | F->sock_family = AI->ai_family; | |
62e76326 | 756 | |
79a15e0a | 757 | if (!(flags & COMM_NOCLOEXEC)) |
62e76326 | 758 | commSetCloseOnExec(new_socket); |
759 | ||
cdc33f35 | 760 | if ((flags & COMM_REUSEADDR)) |
62e76326 | 761 | commSetReuseAddr(new_socket); |
762 | ||
cc192b50 | 763 | if (addr.GetPort() > (u_short) 0) |
62e76326 | 764 | { |
a50bfe93 | 765 | #ifdef _SQUID_MSWIN_ |
766 | ||
767 | if (sock_type != SOCK_DGRAM) | |
768 | #endif | |
769 | ||
770 | commSetNoLinger(new_socket); | |
62e76326 | 771 | |
772 | if (opt_reuseaddr) | |
773 | commSetReuseAddr(new_socket); | |
090089c4 | 774 | } |
62e76326 | 775 | |
a35595cd AJ |
776 | /* MUST be done before binding or face OS Error: "(99) Cannot assign requested address"... */ |
777 | if((flags & COMM_TRANSPARENT)) { | |
778 | comm_set_transparent(new_socket); | |
779 | } | |
a35595cd | 780 | |
cc192b50 | 781 | if (!addr.IsNoAddr()) |
62e76326 | 782 | { |
cc192b50 | 783 | if (commBind(new_socket, *AI) != COMM_OK) { |
62e76326 | 784 | comm_close(new_socket); |
cc192b50 | 785 | addr.FreeAddrInfo(AI); |
62e76326 | 786 | return -1; |
787 | PROF_stop(comm_open); | |
788 | } | |
23ff6968 | 789 | } |
62e76326 | 790 | |
cc192b50 | 791 | addr.FreeAddrInfo(AI); |
090089c4 | 792 | |
79a15e0a | 793 | if (flags & COMM_NONBLOCKING) |
62e76326 | 794 | if (commSetNonBlocking(new_socket) == COMM_ERROR) |
795 | { | |
796 | return -1; | |
797 | PROF_stop(comm_open); | |
798 | } | |
799 | ||
30a4f2a8 | 800 | #ifdef TCP_NODELAY |
801 | if (sock_type == SOCK_STREAM) | |
62e76326 | 802 | commSetTcpNoDelay(new_socket); |
803 | ||
30a4f2a8 | 804 | #endif |
62e76326 | 805 | |
1241e63e | 806 | if (Config.tcpRcvBufsz > 0 && sock_type == SOCK_STREAM) |
62e76326 | 807 | commSetTcpRcvbuf(new_socket, Config.tcpRcvBufsz); |
808 | ||
88bfe092 | 809 | PROF_stop(comm_open); |
62e76326 | 810 | |
090089c4 | 811 | return new_socket; |
812 | } | |
813 | ||
d2d59a68 | 814 | CBDATA_CLASS_INIT(ConnectStateData); |
815 | ||
816 | void * | |
817 | ConnectStateData::operator new (size_t size) | |
818 | { | |
819 | CBDATA_INIT_TYPE(ConnectStateData); | |
820 | return cbdataAlloc(ConnectStateData); | |
821 | } | |
822 | ||
823 | void | |
824 | ConnectStateData::operator delete (void *address) | |
825 | { | |
826 | cbdataFree(address); | |
827 | } | |
828 | ||
b0469965 | 829 | |
830 | ||
e5f6c5c2 | 831 | void |
b0469965 | 832 | commConnectStart(int fd, const char *host, u_short port, AsyncCall::Pointer &cb) |
e924600d | 833 | { |
b0469965 | 834 | debugs(cb->debugSection, cb->debugLevel, "commConnectStart: FD " << fd << |
835 | ", cb " << cb << ", " << host << ":" << port); // TODO: just print *cb | |
836 | ||
28c60158 | 837 | ConnectStateData *cs; |
d2d59a68 | 838 | cs = new ConnectStateData; |
03a1ee42 | 839 | cs->fd = fd; |
e924600d | 840 | cs->host = xstrdup(host); |
cc192b50 | 841 | cs->default_port = port; |
b0469965 | 842 | cs->callback = cb; |
843 | ||
e924600d | 844 | comm_add_close_handler(fd, commConnectFree, cs); |
8407afee | 845 | ipcache_nbgethostbyname(host, commConnectDnsHandle, cs); |
edeb28fd | 846 | } |
847 | ||
b0469965 | 848 | // TODO: Remove this and similar callback registration functions by replacing |
849 | // (callback,data) parameters with an AsyncCall so that we do not have to use | |
850 | // a generic call name and debug level when creating an AsyncCall. This will | |
851 | // also cut the number of callback registration routines in half. | |
852 | void | |
853 | commConnectStart(int fd, const char *host, u_short port, CNCB * callback, void *data) | |
854 | { | |
855 | debugs(5, 5, "commConnectStart: FD " << fd << ", data " << data << ", " << host << ":" << port); | |
856 | AsyncCall::Pointer call = commCbCall(5,3, | |
857 | "SomeCommConnectHandler", CommConnectCbPtrFun(callback, data)); | |
858 | commConnectStart(fd, host, port, call); | |
859 | } | |
860 | ||
edeb28fd | 861 | static void |
03a1ee42 | 862 | commConnectDnsHandle(const ipcache_addrs * ia, void *data) |
edeb28fd | 863 | { |
e6ccf245 | 864 | ConnectStateData *cs = (ConnectStateData *)data; |
62e76326 | 865 | |
edeb28fd | 866 | if (ia == NULL) { |
bf8fe701 | 867 | debugs(5, 3, "commConnectDnsHandle: Unknown host: " << cs->host); |
62e76326 | 868 | |
869 | if (!dns_error_message) { | |
870 | dns_error_message = "Unknown DNS error"; | |
bf8fe701 | 871 | debugs(5, 1, "commConnectDnsHandle: Bad dns_error_message"); |
62e76326 | 872 | } |
873 | ||
874 | assert(dns_error_message != NULL); | |
2d8c0b1a | 875 | cs->callCallback(COMM_ERR_DNS, 0); |
62e76326 | 876 | return; |
edeb28fd | 877 | } |
62e76326 | 878 | |
f076b37b | 879 | assert(ia->cur < ia->count); |
cc192b50 | 880 | |
881 | cs->default_addr = ia->in_addrs[ia->cur]; | |
a12a049a | 882 | |
883 | if (Config.onoff.balance_on_multiple_ip) | |
884 | ipcacheCycleAddr(cs->host, NULL); | |
885 | ||
22c653cd | 886 | cs->addrcount = ia->count; |
a12a049a | 887 | |
22c653cd | 888 | cs->connstart = squid_curtime; |
a12a049a | 889 | |
2d8c0b1a | 890 | cs->connect(); |
e924600d | 891 | } |
892 | ||
2d8c0b1a | 893 | void |
894 | ConnectStateData::callCallback(comm_err_t status, int xerrno) | |
895 | { | |
b0469965 | 896 | debugs(5, 3, "commConnectCallback: FD " << fd); |
bf8fe701 | 897 | |
2d8c0b1a | 898 | comm_remove_close_handler(fd, commConnectFree, this); |
e1b16349 | 899 | commSetTimeout(fd, -1, NULL, NULL); |
62e76326 | 900 | |
b0469965 | 901 | typedef CommConnectCbParams Params; |
902 | Params ¶ms = GetCommParams<Params>(callback); | |
903 | params.fd = fd; | |
904 | params.flag = status; | |
905 | params.xerrno = xerrno; | |
906 | ScheduleCallHere(callback); | |
907 | callback = NULL; | |
62e76326 | 908 | |
744c68f5 | 909 | commConnectFree(fd, this); |
f88211e8 | 910 | } |
911 | ||
e924600d | 912 | static void |
9daca08e | 913 | commConnectFree(int fd, void *data) |
e924600d | 914 | { |
e6ccf245 | 915 | ConnectStateData *cs = (ConnectStateData *)data; |
bf8fe701 | 916 | debugs(5, 3, "commConnectFree: FD " << fd); |
b0469965 | 917 | // delete cs->callback; |
918 | cs->callback = NULL; | |
8407afee | 919 | safe_free(cs->host); |
00d77d6b | 920 | delete cs; |
e924600d | 921 | } |
922 | ||
2d8c0b1a | 923 | static void |
924 | copyFDFlags(int to, fde *F) | |
925 | { | |
926 | if (F->flags.close_on_exec) | |
927 | commSetCloseOnExec(to); | |
928 | ||
929 | if (F->flags.nonblocking) | |
930 | commSetNonBlocking(to); | |
931 | ||
932 | #ifdef TCP_NODELAY | |
933 | ||
934 | if (F->flags.nodelay) | |
935 | commSetTcpNoDelay(to); | |
936 | ||
937 | #endif | |
938 | ||
939 | if (Config.tcpRcvBufsz > 0) | |
940 | commSetTcpRcvbuf(to, Config.tcpRcvBufsz); | |
941 | } | |
942 | ||
22c653cd | 943 | /* Reset FD so that we can connect() again */ |
0b77ecd8 | 944 | int |
945 | ConnectStateData::commResetFD() | |
edeb28fd | 946 | { |
cc192b50 | 947 | struct addrinfo *AI = NULL; |
948 | IPAddress nul; | |
9d92af86 | 949 | int new_family = AF_UNSPEC; |
cc192b50 | 950 | |
b0469965 | 951 | // XXX: do we have to check this? |
952 | // | |
953 | // if (!cbdataReferenceValid(callback.data)) | |
954 | // return 0; | |
62e76326 | 955 | |
83704487 | 956 | statCounter.syscalls.sock.sockets++; |
62e76326 | 957 | |
cc192b50 | 958 | /* setup a bare-bones addrinfo */ |
9d92af86 | 959 | /* TODO INET6: for WinXP we may need to check the local_addr type and setup the family properly. */ |
cc192b50 | 960 | nul.GetAddrInfo(AI); |
9d92af86 | 961 | new_family = AI->ai_family; |
cc192b50 | 962 | |
963 | int fd2 = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol); | |
964 | ||
965 | nul.FreeAddrInfo(AI); | |
62e76326 | 966 | |
edeb28fd | 967 | if (fd2 < 0) { |
f3767a6a | 968 | debugs(5, 0, HERE << "socket: " << xstrerror()); |
62e76326 | 969 | |
970 | if (ENFILE == errno || EMFILE == errno) | |
971 | fdAdjustReserved(); | |
972 | ||
973 | return 0; | |
edeb28fd | 974 | } |
62e76326 | 975 | |
68aa4272 | 976 | #ifdef _SQUID_MSWIN_ |
977 | ||
978 | /* On Windows dup2() can't work correctly on Sockets, the */ | |
979 | /* workaround is to close the destination Socket before call them. */ | |
0b77ecd8 | 980 | close(fd); |
68aa4272 | 981 | |
982 | #endif | |
983 | ||
0b77ecd8 | 984 | if (dup2(fd2, fd) < 0) { |
f3767a6a | 985 | debugs(5, 0, HERE << "dup2: " << xstrerror()); |
62e76326 | 986 | |
987 | if (ENFILE == errno || EMFILE == errno) | |
988 | fdAdjustReserved(); | |
989 | ||
990 | close(fd2); | |
991 | ||
992 | return 0; | |
edeb28fd | 993 | } |
3a5a4930 | 994 | commResetSelect(fd); |
62e76326 | 995 | |
edeb28fd | 996 | close(fd2); |
0b77ecd8 | 997 | fde *F = &fd_table[fd]; |
9d92af86 AJ |
998 | |
999 | /* INET6: copy the new sockets family type to the FDE table */ | |
1000 | fd_table[fd].sock_family = new_family; | |
1001 | ||
0b77ecd8 | 1002 | fd_table[fd].flags.called_connect = 0; |
09544acc | 1003 | /* |
1004 | * yuck, this has assumptions about comm_open() arguments for | |
1005 | * the original socket | |
1006 | */ | |
62e76326 | 1007 | |
e17b4929 AJ |
1008 | /* MUST be done before binding or face OS Error: "(99) Cannot assign requested address"... */ |
1009 | if( F->flags.transparent ) { | |
1010 | comm_set_transparent(fd); | |
1011 | } | |
1012 | ||
cc192b50 | 1013 | AI = NULL; |
1014 | F->local_addr.GetAddrInfo(AI); | |
1015 | ||
1016 | if (commBind(fd, *AI) != COMM_OK) { | |
68d57d84 | 1017 | debugs(5, DBG_CRITICAL, "WARNING: Reset of FD " << fd << " for " << F->local_addr << " failed to bind: " << xstrerror()); |
cc192b50 | 1018 | F->local_addr.FreeAddrInfo(AI); |
62e76326 | 1019 | return 0; |
09544acc | 1020 | } |
cc192b50 | 1021 | F->local_addr.FreeAddrInfo(AI); |
62e76326 | 1022 | |
cc192b50 | 1023 | if (F->tos) |
1024 | comm_set_tos(fd, F->tos); | |
1025 | ||
1026 | #if IPV6_SPECIAL_SPLITSTACK | |
1027 | ||
1028 | if( F->local_addr.IsIPv6() ) | |
1029 | comm_set_v6only(fd, F->tos); | |
62e76326 | 1030 | |
d6827718 | 1031 | #endif |
cc192b50 | 1032 | |
f3767a6a | 1033 | copyFDFlags(fd, F); |
62e76326 | 1034 | |
edeb28fd | 1035 | return 1; |
1036 | } | |
1037 | ||
0b77ecd8 | 1038 | int |
1039 | ConnectStateData::commRetryConnect() | |
22c653cd | 1040 | { |
0b77ecd8 | 1041 | assert(addrcount > 0); |
62e76326 | 1042 | |
0b77ecd8 | 1043 | if (addrcount == 1) { |
1044 | if (tries >= Config.retry.maxtries) | |
62e76326 | 1045 | return 0; |
1046 | ||
0b77ecd8 | 1047 | if (squid_curtime - connstart > Config.Timeout.connect) |
62e76326 | 1048 | return 0; |
22c653cd | 1049 | } else { |
0b77ecd8 | 1050 | if (tries > addrcount) |
62e76326 | 1051 | return 0; |
22c653cd | 1052 | } |
62e76326 | 1053 | |
0b77ecd8 | 1054 | return commResetFD(); |
22c653cd | 1055 | } |
1056 | ||
4ed0e075 | 1057 | static void |
1058 | commReconnect(void *data) | |
1059 | { | |
1060 | ConnectStateData *cs = (ConnectStateData *)data; | |
1061 | ipcache_nbgethostbyname(cs->host, commConnectDnsHandle, cs); | |
1062 | } | |
1063 | ||
f3767a6a | 1064 | /** Connect SOCK to specified DEST_PORT at DEST_HOST. */ |
2d8c0b1a | 1065 | void |
f3767a6a | 1066 | ConnectStateData::Connect(int fd, void *me) |
090089c4 | 1067 | { |
2d8c0b1a | 1068 | ConnectStateData *cs = (ConnectStateData *)me; |
1069 | assert (cs->fd == fd); | |
1070 | cs->connect(); | |
1071 | } | |
1072 | ||
1073 | void | |
1074 | ConnectStateData::defaults() | |
1075 | { | |
cc192b50 | 1076 | S = default_addr; |
1077 | S.SetPort(default_port); | |
2d8c0b1a | 1078 | } |
62e76326 | 1079 | |
2d8c0b1a | 1080 | void |
1081 | ConnectStateData::connect() | |
1082 | { | |
cc192b50 | 1083 | if (S.IsAnyAddr()) |
2d8c0b1a | 1084 | defaults(); |
62e76326 | 1085 | |
f3767a6a | 1086 | debugs(5,5, HERE << "to " << S); |
cc192b50 | 1087 | |
1088 | switch (comm_connect_addr(fd, S) ) { | |
62e76326 | 1089 | |
e5f6c5c2 | 1090 | case COMM_INPROGRESS: |
f3767a6a | 1091 | debugs(5, 5, HERE << "FD " << fd << ": COMM_INPROGRESS"); |
2d8c0b1a | 1092 | commSetSelect(fd, COMM_SELECT_WRITE, ConnectStateData::Connect, this, 0); |
62e76326 | 1093 | break; |
1094 | ||
e5f6c5c2 | 1095 | case COMM_OK: |
f3767a6a | 1096 | debugs(5, 5, HERE << "FD " << fd << ": COMM_OK - connected"); |
cc192b50 | 1097 | ipcacheMarkGoodAddr(host, S); |
2d8c0b1a | 1098 | callCallback(COMM_OK, 0); |
62e76326 | 1099 | break; |
1100 | ||
9d92af86 AJ |
1101 | #if USE_IPV6 |
1102 | case COMM_ERR_PROTOCOL: | |
1103 | /* problem using the desired protocol over this socket. | |
1104 | * count the connection attempt, reset the socket, and immediately try again */ | |
1105 | tries++; | |
1106 | commResetFD(); | |
1107 | connect(); | |
1108 | break; | |
1109 | #endif | |
1110 | ||
e5f6c5c2 | 1111 | default: |
f3767a6a | 1112 | debugs(5, 5, HERE "FD " << fd << ": * - try again"); |
2d8c0b1a | 1113 | tries++; |
cc192b50 | 1114 | ipcacheMarkBadAddr(host, S); |
62e76326 | 1115 | |
1116 | if (Config.onoff.test_reachability) | |
cc192b50 | 1117 | netdbDeleteAddrNetwork(S); |
62e76326 | 1118 | |
0b77ecd8 | 1119 | if (commRetryConnect()) { |
4ed0e075 | 1120 | eventAdd("commReconnect", commReconnect, this, this->addrcount == 1 ? 0.05 : 0.0, 0); |
62e76326 | 1121 | } else { |
f3767a6a | 1122 | debugs(5, 5, HERE << "FD " << fd << ": * - ERR tried too many times already."); |
2d8c0b1a | 1123 | callCallback(COMM_ERR_CONNECT, errno); |
62e76326 | 1124 | } |
090089c4 | 1125 | } |
090089c4 | 1126 | } |
b0469965 | 1127 | /* |
b8d8561b | 1128 | int |
b0469965 | 1129 | commSetTimeout_old(int fd, int timeout, PF * handler, void *data) |
090089c4 | 1130 | { |
f3767a6a | 1131 | debugs(5, 3, HERE << "FD " << fd << " timeout " << timeout); |
03eb2f01 | 1132 | assert(fd >= 0); |
1133 | assert(fd < Squid_MaxFD); | |
2d8c0b1a | 1134 | fde *F = &fd_table[fd]; |
60c0b5a2 | 1135 | assert(F->flags.open); |
62e76326 | 1136 | |
5c5783a2 | 1137 | if (timeout < 0) { |
62e76326 | 1138 | cbdataReferenceDone(F->timeout_data); |
1139 | F->timeout_handler = NULL; | |
1140 | F->timeout = 0; | |
5849612f | 1141 | } else { |
62e76326 | 1142 | if (handler) { |
1143 | cbdataReferenceDone(F->timeout_data); | |
1144 | F->timeout_handler = handler; | |
1145 | F->timeout_data = cbdataReference(data); | |
1146 | } | |
1147 | ||
1148 | F->timeout = squid_curtime + (time_t) timeout; | |
30a4f2a8 | 1149 | } |
62e76326 | 1150 | |
a3fa14bf | 1151 | return F->timeout; |
090089c4 | 1152 | } |
b0469965 | 1153 | */ |
1154 | ||
1155 | int | |
1156 | commSetTimeout(int fd, int timeout, PF * handler, void *data) | |
1157 | { | |
1158 | AsyncCall::Pointer call; | |
f3767a6a | 1159 | debugs(5, 3, HERE << "FD " << fd << " timeout " << timeout); |
b0469965 | 1160 | if(handler != NULL) |
1161 | call=commCbCall(5,4, "SomeTimeoutHandler", CommTimeoutCbPtrFun(handler, data)); | |
1162 | else | |
1163 | call = NULL; | |
1164 | return commSetTimeout(fd, timeout, call); | |
1165 | } | |
1166 | ||
1167 | ||
1168 | int commSetTimeout(int fd, int timeout, AsyncCall::Pointer &callback) | |
1169 | { | |
f3767a6a | 1170 | debugs(5, 3, HERE << "FD " << fd << " timeout " << timeout); |
b0469965 | 1171 | assert(fd >= 0); |
1172 | assert(fd < Squid_MaxFD); | |
1173 | fde *F = &fd_table[fd]; | |
1174 | assert(F->flags.open); | |
1175 | ||
1176 | if (timeout < 0) { | |
1177 | F->timeoutHandler = NULL; | |
1178 | F->timeout = 0; | |
1179 | } else { | |
1180 | if (callback != NULL) { | |
1181 | typedef CommTimeoutCbParams Params; | |
1182 | Params ¶ms = GetCommParams<Params>(callback); | |
1183 | params.fd = fd; | |
1184 | F->timeoutHandler = callback; | |
1185 | } | |
1186 | ||
1187 | F->timeout = squid_curtime + (time_t) timeout; | |
1188 | } | |
1189 | ||
1190 | return F->timeout; | |
1191 | ||
1192 | } | |
090089c4 | 1193 | |
b8d8561b | 1194 | int |
cc192b50 | 1195 | comm_connect_addr(int sock, const IPAddress &address) |
090089c4 | 1196 | { |
3d7e9d7c | 1197 | comm_err_t status = COMM_OK; |
76f87348 | 1198 | fde *F = &fd_table[sock]; |
cc192b50 | 1199 | int x = 0; |
b5568a61 | 1200 | int err = 0; |
9689d97c | 1201 | socklen_t errlen; |
feca3b9a | 1202 | struct addrinfo *AI = NULL; |
88bfe092 | 1203 | PROF_start(comm_connect_addr); |
cc192b50 | 1204 | |
1205 | assert(address.GetPort() != 0); | |
1206 | ||
b0469965 | 1207 | debugs(5, 9, "comm_connect_addr: connecting socket " << sock << " to " << address << " (want family: " << F->sock_family << ")"); |
cc192b50 | 1208 | |
9d92af86 AJ |
1209 | /* BUG 2222 FIX: reset the FD when its found to be IPv4 in IPv6 mode */ |
1210 | /* inverse case of IPv4 failing to connect on IPv6 socket is handeld post-connect. | |
1211 | * this case must presently be handled here since the GetAddrInfo asserts on bad mappings. | |
1212 | * eventually we want it to throw a Must() that gets handled there instead of this if. | |
1213 | * NP: because commresetFD is private to ConnStateData we have to return an error and | |
1214 | * trust its handled properly. | |
1215 | */ | |
1216 | #if USE_IPV6 | |
1217 | if(F->sock_family == AF_INET && !address.IsIPv4()) { | |
1218 | return COMM_ERR_PROTOCOL; | |
1219 | } | |
1220 | #endif | |
1221 | ||
feca3b9a | 1222 | address.GetAddrInfo(AI, F->sock_family); |
cc192b50 | 1223 | |
090089c4 | 1224 | /* Establish connection. */ |
b5568a61 | 1225 | errno = 0; |
62e76326 | 1226 | |
1227 | if (!F->flags.called_connect) | |
1228 | { | |
1229 | F->flags.called_connect = 1; | |
1230 | statCounter.syscalls.sock.connects++; | |
1231 | ||
feca3b9a | 1232 | x = connect(sock, AI->ai_addr, AI->ai_addrlen); |
62e76326 | 1233 | |
5a33a66a | 1234 | // XXX: ICAP code refuses callbacks during a pending comm_ call |
1235 | // Async calls development will fix this. | |
1236 | if (x == 0) { | |
1237 | x = -1; | |
1238 | errno = EINPROGRESS; | |
1239 | } | |
1240 | ||
62e76326 | 1241 | if (x < 0) |
cc192b50 | 1242 | { |
1243 | debugs(5,5, "comm_connect_addr: sock=" << sock << ", addrinfo( " << | |
feca3b9a AJ |
1244 | " flags=" << AI->ai_flags << |
1245 | ", family=" << AI->ai_family << | |
1246 | ", socktype=" << AI->ai_socktype << | |
1247 | ", protocol=" << AI->ai_protocol << | |
1248 | ", &addr=" << AI->ai_addr << | |
1249 | ", addrlen=" << AI->ai_addrlen << | |
cc192b50 | 1250 | " )" ); |
1251 | debugs(5, 9, "connect FD " << sock << ": (" << x << ") " << xstrerror()); | |
1252 | debugs(14,9, "connecting to: " << address ); | |
1253 | } | |
62e76326 | 1254 | } else |
1255 | { | |
140e2c0b | 1256 | #if defined(_SQUID_NEWSOS6_) |
62e76326 | 1257 | /* Makoto MATSUSHITA <matusita@ics.es.osaka-u.ac.jp> */ |
1258 | ||
feca3b9a | 1259 | connect(sock, AI->ai_addr, AI->ai_addrlen); |
62e76326 | 1260 | |
1261 | if (errno == EINVAL) { | |
1262 | errlen = sizeof(err); | |
1263 | x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen); | |
1264 | ||
1265 | if (x >= 0) | |
1266 | errno = x; | |
1267 | } | |
1268 | ||
33ac9442 | 1269 | #else |
62e76326 | 1270 | errlen = sizeof(err); |
1271 | ||
1272 | x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen); | |
1273 | ||
1274 | if (x == 0) | |
1275 | errno = err; | |
1276 | ||
b5568a61 | 1277 | #if defined(_SQUID_SOLARIS_) |
62e76326 | 1278 | /* |
1279 | * Solaris 2.4's socket emulation doesn't allow you | |
1280 | * to determine the error from a failed non-blocking | |
1281 | * connect and just returns EPIPE. Create a fake | |
1282 | * error message for connect. -- fenner@parc.xerox.com | |
1283 | */ | |
1284 | if (x < 0 && errno == EPIPE) | |
1285 | errno = ENOTCONN; | |
1286 | ||
33ac9442 | 1287 | #endif |
30a4f2a8 | 1288 | #endif |
62e76326 | 1289 | |
e5f6c5c2 | 1290 | } |
62e76326 | 1291 | |
82ec8dfc AR |
1292 | /* Squid seems to be working fine without this code. With this code, |
1293 | * we leak memory on many connect requests because of EINPROGRESS. | |
1294 | * If you find that this code is needed, please file a bug report. */ | |
1295 | #if 0 | |
feca3b9a AJ |
1296 | #ifdef _SQUID_LINUX_ |
1297 | /* 2007-11-27: | |
1298 | * Linux Debian replaces our allocated AI pointer with garbage when | |
1299 | * connect() fails. This leads to segmentation faults deallocating | |
1300 | * the system-allocated memory when we go to clean up our pointer. | |
1301 | * HACK: is to leak the memory returned since we can't deallocate. | |
1302 | */ | |
1303 | if(errno != 0) { | |
1304 | AI = NULL; | |
1305 | } | |
82ec8dfc | 1306 | #endif |
feca3b9a AJ |
1307 | #endif |
1308 | ||
1309 | address.FreeAddrInfo(AI); | |
1310 | ||
88bfe092 | 1311 | PROF_stop(comm_connect_addr); |
62e76326 | 1312 | |
b5568a61 | 1313 | if (errno == 0 || errno == EISCONN) |
62e76326 | 1314 | status = COMM_OK; |
b5568a61 | 1315 | else if (ignoreErrno(errno)) |
62e76326 | 1316 | status = COMM_INPROGRESS; |
b5568a61 | 1317 | else |
cc192b50 | 1318 | #if USE_IPV6 |
1319 | if( address.IsIPv4() && F->sock_family == AF_INET6 ) { | |
1320 | ||
1321 | /* failover to trying IPv4-only link if an IPv6 one fails */ | |
1322 | /* to catch the edge case of apps listening on IPv4-localhost */ | |
1323 | F->sock_family = AF_INET; | |
1324 | int res = comm_connect_addr(sock, address); | |
1325 | ||
1326 | /* if that fails too, undo our temporary socktype hack so the repeat works properly. */ | |
1327 | if(res == COMM_ERROR) | |
1328 | F->sock_family = AF_INET6; | |
1329 | ||
1330 | return res; | |
1331 | } | |
1332 | else | |
1333 | #endif | |
62e76326 | 1334 | return COMM_ERROR; |
1335 | ||
cc192b50 | 1336 | address.NtoA(F->ipaddr, MAX_IPSTRLEN); |
62e76326 | 1337 | |
cc192b50 | 1338 | F->remote_port = address.GetPort(); /* remote_port is HS */ |
62e76326 | 1339 | |
1340 | if (status == COMM_OK) | |
1341 | { | |
cc192b50 | 1342 | debugs(5, 10, "comm_connect_addr: FD " << sock << " connected to " << address); |
62e76326 | 1343 | } else if (status == COMM_INPROGRESS) |
1344 | { | |
bf8fe701 | 1345 | debugs(5, 10, "comm_connect_addr: FD " << sock << " connection pending"); |
090089c4 | 1346 | } |
62e76326 | 1347 | |
090089c4 | 1348 | return status; |
1349 | } | |
1350 | ||
1351 | /* Wait for an incoming connection on FD. FD should be a socket returned | |
1352 | * from comm_listen. */ | |
ee0989f2 | 1353 | static int |
1354 | comm_old_accept(int fd, ConnectionDetail &details) | |
090089c4 | 1355 | { |
88bfe092 | 1356 | PROF_start(comm_accept); |
ee0989f2 | 1357 | statCounter.syscalls.sock.accepts++; |
1358 | int sock; | |
cc192b50 | 1359 | struct addrinfo *gai = NULL; |
1360 | details.me.InitAddrInfo(gai); | |
1361 | ||
1362 | if ((sock = accept(fd, gai->ai_addr, &gai->ai_addrlen)) < 0) { | |
1363 | ||
1364 | details.me.FreeAddrInfo(gai); | |
62e76326 | 1365 | |
62e76326 | 1366 | PROF_stop(comm_accept); |
1367 | ||
1368 | if (ignoreErrno(errno)) | |
1369 | { | |
bf8fe701 | 1370 | debugs(50, 5, "comm_old_accept: FD " << fd << ": " << xstrerror()); |
62e76326 | 1371 | return COMM_NOMESSAGE; |
1372 | } else if (ENFILE == errno || EMFILE == errno) | |
1373 | { | |
bf8fe701 | 1374 | debugs(50, 3, "comm_old_accept: FD " << fd << ": " << xstrerror()); |
62e76326 | 1375 | return COMM_ERROR; |
1376 | } else | |
1377 | { | |
bf8fe701 | 1378 | debugs(50, 1, "comm_old_accept: FD " << fd << ": " << xstrerror()); |
62e76326 | 1379 | return COMM_ERROR; |
1380 | } | |
090089c4 | 1381 | } |
62e76326 | 1382 | |
cc192b50 | 1383 | details.peer = *gai; |
1384 | ||
3be4d5d1 | 1385 | details.me.InitAddrInfo(gai); |
1386 | ||
cc192b50 | 1387 | details.me.SetEmpty(); |
1388 | getsockname(sock, gai->ai_addr, &gai->ai_addrlen); | |
1389 | details.me = *gai; | |
62e76326 | 1390 | |
3ca60c86 | 1391 | commSetCloseOnExec(sock); |
cc192b50 | 1392 | |
090089c4 | 1393 | /* fdstat update */ |
5c5783a2 | 1394 | fd_open(sock, FD_SOCKET, "HTTP Request"); |
c4b7a5a9 | 1395 | fdd_table[sock].close_file = NULL; |
1396 | fdd_table[sock].close_line = 0; | |
ee0989f2 | 1397 | fde *F = &fd_table[sock]; |
cc192b50 | 1398 | details.peer.NtoA(F->ipaddr,MAX_IPSTRLEN); |
1399 | F->remote_port = details.peer.GetPort(); | |
1400 | F->local_addr.SetPort(details.me.GetPort()); | |
3be4d5d1 | 1401 | #if USE_IPV6 |
1402 | F->sock_family = AF_INET; | |
1403 | #else | |
1404 | F->sock_family = details.me.IsIPv4()?AF_INET:AF_INET6; | |
1405 | #endif | |
1406 | details.me.FreeAddrInfo(gai); | |
cc192b50 | 1407 | |
090089c4 | 1408 | commSetNonBlocking(sock); |
cc192b50 | 1409 | |
acaa7194 AJ |
1410 | /* IFF the socket is (tproxy) transparent, pass the flag down to allow spoofing */ |
1411 | F->flags.transparent = fd_table[fd].flags.transparent; | |
72d57dea | 1412 | |
88bfe092 | 1413 | PROF_stop(comm_accept); |
090089c4 | 1414 | return sock; |
1415 | } | |
1416 | ||
cb201b7e | 1417 | void |
1418 | commCallCloseHandlers(int fd) | |
1419 | { | |
76f87348 | 1420 | fde *F = &fd_table[fd]; |
bf8fe701 | 1421 | debugs(5, 5, "commCallCloseHandlers: FD " << fd); |
62e76326 | 1422 | |
8000a965 | 1423 | while (F->closeHandler != NULL) { |
b0469965 | 1424 | AsyncCall::Pointer call = F->closeHandler; |
1425 | F->closeHandler = call->Next(); | |
1426 | call->setNext(NULL); | |
1427 | // If call is not canceled schedule it for execution else ignore it | |
1428 | if(!call->canceled()){ | |
1429 | debugs(5, 5, "commCallCloseHandlers: ch->handler=" << call); | |
1430 | typedef CommCloseCbParams Params; | |
1431 | Params ¶ms = GetCommParams<Params>(call); | |
1432 | params.fd = fd; | |
1433 | ScheduleCallHere(call); | |
1434 | } | |
cb201b7e | 1435 | } |
1436 | } | |
1437 | ||
5492ad1d | 1438 | #if LINGERING_CLOSE |
1439 | static void | |
1440 | commLingerClose(int fd, void *unused) | |
1441 | { | |
1442 | LOCAL_ARRAY(char, buf, 1024); | |
1443 | int n; | |
1f7c9178 | 1444 | n = FD_READ_METHOD(fd, buf, 1024); |
62e76326 | 1445 | |
5492ad1d | 1446 | if (n < 0) |
bf8fe701 | 1447 | debugs(5, 3, "commLingerClose: FD " << fd << " read: " << xstrerror()); |
62e76326 | 1448 | |
5492ad1d | 1449 | comm_close(fd); |
1450 | } | |
1451 | ||
1452 | static void | |
1453 | commLingerTimeout(int fd, void *unused) | |
1454 | { | |
bf8fe701 | 1455 | debugs(5, 3, "commLingerTimeout: FD " << fd); |
5492ad1d | 1456 | comm_close(fd); |
1457 | } | |
1458 | ||
1459 | /* | |
1460 | * Inspired by apache | |
1461 | */ | |
1462 | void | |
1463 | comm_lingering_close(int fd) | |
1464 | { | |
d4c19b39 | 1465 | #if USE_SSL |
62e76326 | 1466 | |
d4c19b39 | 1467 | if (fd_table[fd].ssl) |
62e76326 | 1468 | ssl_shutdown_method(fd); |
1469 | ||
d4c19b39 | 1470 | #endif |
62e76326 | 1471 | |
5492ad1d | 1472 | if (shutdown(fd, 1) < 0) { |
62e76326 | 1473 | comm_close(fd); |
1474 | return; | |
5492ad1d | 1475 | } |
62e76326 | 1476 | |
5492ad1d | 1477 | fd_note(fd, "lingering close"); |
1478 | commSetTimeout(fd, 10, commLingerTimeout, NULL); | |
1479 | commSetSelect(fd, COMM_SELECT_READ, commLingerClose, NULL, 0); | |
1480 | } | |
62e76326 | 1481 | |
5492ad1d | 1482 | #endif |
1483 | ||
98264874 | 1484 | /* |
1485 | * enable linger with time of 0 so that when the socket is | |
1486 | * closed, TCP generates a RESET | |
1487 | */ | |
1488 | void | |
1489 | comm_reset_close(int fd) | |
1490 | { | |
62e76326 | 1491 | |
98264874 | 1492 | struct linger L; |
1493 | L.l_onoff = 1; | |
1494 | L.l_linger = 0; | |
62e76326 | 1495 | |
98264874 | 1496 | if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0) |
bf8fe701 | 1497 | debugs(50, 0, "commResetTCPClose: FD " << fd << ": " << xstrerror()); |
62e76326 | 1498 | |
98264874 | 1499 | comm_close(fd); |
1500 | } | |
1501 | ||
10b06767 AJ |
1502 | void |
1503 | comm_close_start(int fd, void *data) | |
1504 | { | |
10b06767 | 1505 | #if USE_SSL |
86dedcc1 | 1506 | fde *F = &fd_table[fd]; |
10b06767 AJ |
1507 | if (F->ssl) |
1508 | ssl_shutdown_method(fd); | |
1509 | ||
1510 | #endif | |
1511 | ||
1512 | } | |
1513 | ||
1514 | ||
b0469965 | 1515 | void |
1516 | comm_close_complete(int fd, void *data) | |
2d8c0b1a | 1517 | { |
b0469965 | 1518 | #if USE_SSL |
1519 | fde *F = &fd_table[fd]; | |
2d8c0b1a | 1520 | |
b0469965 | 1521 | if (F->ssl) { |
1522 | SSL_free(F->ssl); | |
1523 | F->ssl = NULL; | |
1524 | } | |
2d8c0b1a | 1525 | |
b0469965 | 1526 | #endif |
1527 | fd_close(fd); /* update fdstat */ | |
1528 | ||
1529 | close(fd); | |
1530 | ||
b0469965 | 1531 | fdc_table[fd] = AcceptFD(fd); |
1532 | ||
1533 | statCounter.syscalls.sock.closes++; | |
1534 | ||
1535 | /* When an fd closes, give accept() a chance, if need be */ | |
1536 | ||
1537 | if (fdNFree() >= RESERVED_FD) | |
1538 | AcceptLimiter::Instance().kick(); | |
2d8c0b1a | 1539 | |
2d8c0b1a | 1540 | } |
c4b7a5a9 | 1541 | |
1542 | /* | |
1543 | * Close the socket fd. | |
1544 | * | |
1545 | * + call write handlers with ERR_CLOSING | |
1546 | * + call read handlers with ERR_CLOSING | |
1547 | * + call closing handlers | |
a46d2c0e | 1548 | * |
1549 | * NOTE: COMM_ERR_CLOSING will NOT be called for CommReads' sitting in a | |
1550 | * DeferredReadManager. | |
c4b7a5a9 | 1551 | */ |
b8d8561b | 1552 | void |
43ae1d95 | 1553 | _comm_close(int fd, char const *file, int line) |
090089c4 | 1554 | { |
82ec8dfc | 1555 | debugs(5, 3, "comm_close: start closing FD " << fd); |
03eb2f01 | 1556 | assert(fd >= 0); |
1557 | assert(fd < Squid_MaxFD); | |
82ec8dfc AR |
1558 | |
1559 | fde *F = &fd_table[fd]; | |
c4b7a5a9 | 1560 | fdd_table[fd].close_file = file; |
1561 | fdd_table[fd].close_line = line; | |
1f7c9178 | 1562 | |
82ec8dfc | 1563 | if (F->closing()) |
62e76326 | 1564 | return; |
1565 | ||
60c0b5a2 | 1566 | if (shutting_down && (!F->flags.open || F->type == FD_FILE)) |
62e76326 | 1567 | return; |
1568 | ||
c4b7a5a9 | 1569 | /* The following fails because ipc.c is doing calls to pipe() to create sockets! */ |
b0469965 | 1570 | assert(isOpen(fd)); |
62e76326 | 1571 | |
76f87348 | 1572 | assert(F->type != FD_FILE); |
62e76326 | 1573 | |
88bfe092 | 1574 | PROF_start(comm_close); |
62e76326 | 1575 | |
10b06767 | 1576 | F->flags.close_request = 1; |
62e76326 | 1577 | |
10b06767 AJ |
1578 | AsyncCall::Pointer startCall=commCbCall(5,4, "comm_close_start", |
1579 | CommCloseCbPtrFun(comm_close_start, NULL)); | |
1580 | typedef CommCloseCbParams Params; | |
1581 | Params &startParams = GetCommParams<Params>(startCall); | |
1582 | startParams.fd = fd; | |
1583 | ScheduleCallHere(startCall); | |
62e76326 | 1584 | |
74257126 AR |
1585 | // a half-closed fd may lack a reader, so we stop monitoring explicitly |
1586 | if (commHasHalfClosedMonitor(fd)) | |
1587 | commStopHalfClosedMonitor(fd); | |
fa80a8ef | 1588 | commSetTimeout(fd, -1, NULL, NULL); |
62e76326 | 1589 | |
82ec8dfc | 1590 | // notify read/write handlers |
2b663917 | 1591 | if (commio_has_callback(fd, IOCB_WRITE, COMMIO_FD_WRITECB(fd))) { |
b0469965 | 1592 | commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), COMM_ERR_CLOSING, errno); |
2b663917 | 1593 | } |
1594 | if (commio_has_callback(fd, IOCB_READ, COMMIO_FD_READCB(fd))) { | |
b0469965 | 1595 | commio_finish_callback(fd, COMMIO_FD_READCB(fd), COMM_ERR_CLOSING, errno); |
2b663917 | 1596 | } |
2d8c0b1a | 1597 | |
82ec8dfc | 1598 | // notify accept handlers |
b0469965 | 1599 | fdc_table[fd].notify(-1, COMM_ERR_CLOSING, 0, ConnectionDetail()); |
c4b7a5a9 | 1600 | |
cb201b7e | 1601 | commCallCloseHandlers(fd); |
62e76326 | 1602 | |
781ce8ff | 1603 | if (F->pconn.uses) |
1604 | F->pconn.pool->count(F->pconn.uses); | |
62e76326 | 1605 | |
a7ad6e4e | 1606 | comm_empty_os_read_buffers(fd); |
b0469965 | 1607 | |
62e76326 | 1608 | |
10b06767 | 1609 | AsyncCall::Pointer completeCall=commCbCall(5,4, "comm_close_complete", |
b0469965 | 1610 | CommCloseCbPtrFun(comm_close_complete, NULL)); |
10b06767 AJ |
1611 | Params &completeParams = GetCommParams<Params>(completeCall); |
1612 | completeParams.fd = fd; | |
82ec8dfc AR |
1613 | // must use async call to wait for all callbacks |
1614 | // scheduled before comm_close() to finish | |
10b06767 | 1615 | ScheduleCallHere(completeCall); |
62e76326 | 1616 | |
88bfe092 | 1617 | PROF_stop(comm_close); |
090089c4 | 1618 | } |
1619 | ||
090089c4 | 1620 | /* Send a udp datagram to specified TO_ADDR. */ |
b8d8561b | 1621 | int |
5df61230 | 1622 | comm_udp_sendto(int fd, |
cc192b50 | 1623 | const IPAddress &to_addr, |
62e76326 | 1624 | const void *buf, |
1625 | int len) | |
090089c4 | 1626 | { |
cc192b50 | 1627 | int x = 0; |
1628 | struct addrinfo *AI = NULL; | |
1629 | ||
88bfe092 | 1630 | PROF_start(comm_udp_sendto); |
83704487 | 1631 | statCounter.syscalls.sock.sendtos++; |
62e76326 | 1632 | |
cc192b50 | 1633 | debugs(50, 3, "comm_udp_sendto: Attempt to send UDP packet to " << to_addr << |
1634 | " using FD " << fd << " using Port " << comm_local_port(fd) ); | |
1635 | ||
1636 | /* BUG: something in the above macro appears to occasionally be setting AI to garbage. */ | |
1637 | /* AYJ: 2007-08-27 : or was it because I wasn't then setting 'fd_table[fd].sock_family' to fill properly. */ | |
1638 | assert( NULL == AI ); | |
1639 | ||
1640 | to_addr.GetAddrInfo(AI, fd_table[fd].sock_family); | |
1641 | ||
1642 | x = sendto(fd, buf, len, 0, AI->ai_addr, AI->ai_addrlen); | |
1643 | ||
1644 | to_addr.FreeAddrInfo(AI); | |
1645 | ||
88bfe092 | 1646 | PROF_stop(comm_udp_sendto); |
62e76326 | 1647 | |
2d8c0b1a | 1648 | if (x >= 0) |
1649 | return x; | |
1650 | ||
17d51783 | 1651 | #ifdef _SQUID_LINUX_ |
62e76326 | 1652 | |
2d8c0b1a | 1653 | if (ECONNREFUSED != errno) |
17d51783 | 1654 | #endif |
62e76326 | 1655 | |
cc192b50 | 1656 | debugs(50, 1, "comm_udp_sendto: FD " << fd << ", (family=" << fd_table[fd].sock_family << ") " << to_addr << ": " << xstrerror()); |
62e76326 | 1657 | |
2d8c0b1a | 1658 | return COMM_ERROR; |
090089c4 | 1659 | } |
1660 | ||
b8d8561b | 1661 | void |
582b6456 | 1662 | comm_add_close_handler(int fd, PF * handler, void *data) |
30a4f2a8 | 1663 | { |
bf8fe701 | 1664 | debugs(5, 5, "comm_add_close_handler: FD " << fd << ", handler=" << |
1665 | handler << ", data=" << data); | |
62e76326 | 1666 | |
b0469965 | 1667 | AsyncCall::Pointer call=commCbCall(5,4, "SomeCloseHandler", |
1668 | CommCloseCbPtrFun(handler, data)); | |
1669 | comm_add_close_handler(fd, call); | |
1670 | } | |
62e76326 | 1671 | |
b0469965 | 1672 | void |
1673 | comm_add_close_handler(int fd, AsyncCall::Pointer &call) | |
1674 | { | |
1675 | debugs(5, 5, "comm_add_close_handler: FD " << fd << ", AsyncCall=" << call); | |
62e76326 | 1676 | |
b0469965 | 1677 | /*TODO:Check for a similar scheduled AsyncCall*/ |
1678 | // for (c = fd_table[fd].closeHandler; c; c = c->next) | |
1679 | // assert(c->handler != handler || c->data != data); | |
62e76326 | 1680 | |
b0469965 | 1681 | call->setNext(fd_table[fd].closeHandler); |
62e76326 | 1682 | |
b0469965 | 1683 | fd_table[fd].closeHandler = call; |
30a4f2a8 | 1684 | } |
1685 | ||
b0469965 | 1686 | |
1687 | // remove function-based close handler | |
b8d8561b | 1688 | void |
582b6456 | 1689 | comm_remove_close_handler(int fd, PF * handler, void *data) |
090089c4 | 1690 | { |
b0469965 | 1691 | assert (isOpen(fd)); |
30a4f2a8 | 1692 | /* Find handler in list */ |
bf8fe701 | 1693 | debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", handler=" << |
1694 | handler << ", data=" << data); | |
62e76326 | 1695 | |
b0469965 | 1696 | AsyncCall::Pointer p; |
1697 | for (p = fd_table[fd].closeHandler; p != NULL; p = p->Next()){ | |
1698 | typedef CommCbFunPtrCallT<CommCloseCbPtrFun> Call; | |
1699 | const Call *call = dynamic_cast<const Call*>(p.getRaw()); | |
1700 | if (!call) // method callbacks have their own comm_remove_close_handler | |
1701 | continue; | |
62e76326 | 1702 | |
b0469965 | 1703 | typedef CommCloseCbParams Params; |
1704 | const Params ¶ms = GetCommParams<Params>(p); | |
1705 | if (call->dialer.handler == handler && params.data == data) | |
1706 | break; /* This is our handler */ | |
1707 | } | |
f88211e8 | 1708 | assert(p != NULL); |
b0469965 | 1709 | p->cancel("comm_remove_close_handler"); |
1710 | } | |
62e76326 | 1711 | |
b0469965 | 1712 | // remove method-based close handler |
1713 | void | |
1714 | comm_remove_close_handler(int fd, AsyncCall::Pointer &call) | |
1715 | { | |
1716 | assert (isOpen(fd)); | |
1717 | /* Find handler in list */ | |
1718 | debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", AsyncCall=" << call); | |
62e76326 | 1719 | |
b0469965 | 1720 | // Check to see if really exist the given AsyncCall in comm_close handlers |
1721 | // TODO: optimize: this slow code is only needed for the assert() below | |
1722 | AsyncCall::Pointer p; | |
1723 | for (p = fd_table[fd].closeHandler; p != NULL && p != call; p = p->Next()); | |
1724 | assert(p == call); | |
62e76326 | 1725 | |
b0469965 | 1726 | call->cancel("comm_remove_close_handler"); |
30a4f2a8 | 1727 | } |
090089c4 | 1728 | |
b8d8561b | 1729 | static void |
1730 | commSetNoLinger(int fd) | |
30a4f2a8 | 1731 | { |
62e76326 | 1732 | |
30a4f2a8 | 1733 | struct linger L; |
090089c4 | 1734 | L.l_onoff = 0; /* off */ |
1735 | L.l_linger = 0; | |
62e76326 | 1736 | |
30a4f2a8 | 1737 | if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0) |
bf8fe701 | 1738 | debugs(50, 0, "commSetNoLinger: FD " << fd << ": " << xstrerror()); |
62e76326 | 1739 | |
58a6c186 | 1740 | fd_table[fd].flags.nolinger = 1; |
090089c4 | 1741 | } |
1742 | ||
b8d8561b | 1743 | static void |
1744 | commSetReuseAddr(int fd) | |
090089c4 | 1745 | { |
1746 | int on = 1; | |
62e76326 | 1747 | |
30a4f2a8 | 1748 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0) |
bf8fe701 | 1749 | debugs(50, 1, "commSetReuseAddr: FD " << fd << ": " << xstrerror()); |
090089c4 | 1750 | } |
1751 | ||
b8d8561b | 1752 | static void |
1753 | commSetTcpRcvbuf(int fd, int size) | |
f868539a | 1754 | { |
1755 | if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size)) < 0) | |
bf8fe701 | 1756 | debugs(50, 1, "commSetTcpRcvbuf: FD " << fd << ", SIZE " << size << ": " << xstrerror()); |
8f0d53ef | 1757 | if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size)) < 0) |
1758 | debugs(50, 1, "commSetTcpRcvbuf: FD " << fd << ", SIZE " << size << ": " << xstrerror()); | |
1759 | #ifdef TCP_WINDOW_CLAMP | |
1760 | if (setsockopt(fd, SOL_TCP, TCP_WINDOW_CLAMP, (char *) &size, sizeof(size)) < 0) | |
1761 | debugs(50, 1, "commSetTcpRcvbuf: FD " << fd << ", SIZE " << size << ": " << xstrerror()); | |
1762 | #endif | |
f868539a | 1763 | } |
1764 | ||
b8d8561b | 1765 | int |
1766 | commSetNonBlocking(int fd) | |
30a4f2a8 | 1767 | { |
a50bfe93 | 1768 | #ifndef _SQUID_MSWIN_ |
731e4d49 | 1769 | int flags; |
9e205701 | 1770 | int dummy = 0; |
a50bfe93 | 1771 | #endif |
ec4daaa5 | 1772 | #ifdef _SQUID_WIN32_ |
62e76326 | 1773 | |
b05490a8 | 1774 | int nonblocking = TRUE; |
62e76326 | 1775 | |
629b5f75 | 1776 | #ifdef _SQUID_CYGWIN_ |
1777 | ||
7f6ffd15 | 1778 | if (fd_table[fd].type != FD_PIPE) { |
629b5f75 | 1779 | #endif |
1780 | ||
62e76326 | 1781 | if (ioctl(fd, FIONBIO, &nonblocking) < 0) { |
bf8fe701 | 1782 | debugs(50, 0, "commSetNonBlocking: FD " << fd << ": " << xstrerror() << " " << fd_table[fd].type); |
62e76326 | 1783 | return COMM_ERROR; |
1784 | } | |
629b5f75 | 1785 | |
1786 | #ifdef _SQUID_CYGWIN_ | |
1787 | ||
7f6ffd15 | 1788 | } else { |
1789 | #endif | |
629b5f75 | 1790 | #endif |
a50bfe93 | 1791 | #ifndef _SQUID_MSWIN_ |
62e76326 | 1792 | |
1793 | if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) { | |
bf8fe701 | 1794 | debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror()); |
62e76326 | 1795 | return COMM_ERROR; |
1796 | } | |
1797 | ||
1798 | if (fcntl(fd, F_SETFL, flags | SQUID_NONBLOCK) < 0) { | |
bf8fe701 | 1799 | debugs(50, 0, "commSetNonBlocking: FD " << fd << ": " << xstrerror()); |
62e76326 | 1800 | return COMM_ERROR; |
1801 | } | |
1802 | ||
a50bfe93 | 1803 | #endif |
629b5f75 | 1804 | #ifdef _SQUID_CYGWIN_ |
62e76326 | 1805 | |
090089c4 | 1806 | } |
62e76326 | 1807 | |
7f6ffd15 | 1808 | #endif |
58a6c186 | 1809 | fd_table[fd].flags.nonblocking = 1; |
62e76326 | 1810 | |
090089c4 | 1811 | return 0; |
1812 | } | |
1813 | ||
7e3ce7b9 | 1814 | int |
1815 | commUnsetNonBlocking(int fd) | |
1816 | { | |
a50bfe93 | 1817 | #ifdef _SQUID_MSWIN_ |
1818 | int nonblocking = FALSE; | |
1819 | ||
1820 | if (ioctlsocket(fd, FIONBIO, (unsigned long *) &nonblocking) < 0) { | |
1821 | #else | |
7e3ce7b9 | 1822 | int flags; |
1823 | int dummy = 0; | |
62e76326 | 1824 | |
7e3ce7b9 | 1825 | if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) { |
bf8fe701 | 1826 | debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror()); |
62e76326 | 1827 | return COMM_ERROR; |
7e3ce7b9 | 1828 | } |
62e76326 | 1829 | |
7e3ce7b9 | 1830 | if (fcntl(fd, F_SETFL, flags & (~SQUID_NONBLOCK)) < 0) { |
a50bfe93 | 1831 | #endif |
bf8fe701 | 1832 | debugs(50, 0, "commUnsetNonBlocking: FD " << fd << ": " << xstrerror()); |
62e76326 | 1833 | return COMM_ERROR; |
7e3ce7b9 | 1834 | } |
62e76326 | 1835 | |
7e3ce7b9 | 1836 | fd_table[fd].flags.nonblocking = 0; |
1837 | return 0; | |
1838 | } | |
1839 | ||
b8d8561b | 1840 | void |
a50bfe93 | 1841 | commSetCloseOnExec(int fd) { |
3ca60c86 | 1842 | #ifdef FD_CLOEXEC |
731e4d49 | 1843 | int flags; |
7a18b487 | 1844 | int dummy = 0; |
62e76326 | 1845 | |
c7989865 | 1846 | if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) { |
bf8fe701 | 1847 | debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror()); |
62e76326 | 1848 | return; |
3ca60c86 | 1849 | } |
62e76326 | 1850 | |
24382924 | 1851 | if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) |
bf8fe701 | 1852 | debugs(50, 0, "FD " << fd << ": set close-on-exec failed: " << xstrerror()); |
62e76326 | 1853 | |
d6827718 | 1854 | fd_table[fd].flags.close_on_exec = 1; |
62e76326 | 1855 | |
3ca60c86 | 1856 | #endif |
1857 | } | |
1858 | ||
e90100aa | 1859 | #ifdef TCP_NODELAY |
1860 | static void | |
a50bfe93 | 1861 | commSetTcpNoDelay(int fd) { |
e90100aa | 1862 | int on = 1; |
62e76326 | 1863 | |
e90100aa | 1864 | if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) < 0) |
bf8fe701 | 1865 | debugs(50, 1, "commSetTcpNoDelay: FD " << fd << ": " << xstrerror()); |
62e76326 | 1866 | |
d6827718 | 1867 | fd_table[fd].flags.nodelay = 1; |
e90100aa | 1868 | } |
62e76326 | 1869 | |
e90100aa | 1870 | #endif |
1871 | ||
b2130d58 | 1872 | void |
1873 | commSetTcpKeepalive(int fd, int idle, int interval, int timeout) | |
1874 | { | |
1875 | int on = 1; | |
1876 | #ifdef TCP_KEEPCNT | |
1877 | if (timeout && interval) { | |
1878 | int count = (timeout + interval - 1) / interval; | |
1879 | if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &count, sizeof(on)) < 0) | |
e680134c | 1880 | debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror()); |
b2130d58 | 1881 | } |
1882 | #endif | |
1883 | #ifdef TCP_KEEPIDLE | |
1884 | if (idle) { | |
1885 | if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(on)) < 0) | |
e680134c | 1886 | debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror()); |
b2130d58 | 1887 | } |
1888 | #endif | |
1889 | #ifdef TCP_KEEPINTVL | |
1890 | if (interval) { | |
1891 | if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(on)) < 0) | |
e680134c | 1892 | debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror()); |
b2130d58 | 1893 | } |
1894 | #endif | |
1895 | if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &on, sizeof(on)) < 0) | |
e680134c | 1896 | debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror()); |
b2130d58 | 1897 | } |
6a988308 | 1898 | |
d86b3703 | 1899 | void |
a50bfe93 | 1900 | comm_init(void) { |
c4b7a5a9 | 1901 | fd_table =(fde *) xcalloc(Squid_MaxFD, sizeof(fde)); |
1902 | fdd_table = (fd_debug_t *)xcalloc(Squid_MaxFD, sizeof(fd_debug_t)); | |
2d8c0b1a | 1903 | |
b0469965 | 1904 | fdc_table = new AcceptFD[Squid_MaxFD]; |
2b663917 | 1905 | for (int pos = 0; pos < Squid_MaxFD; ++pos) { |
b0469965 | 1906 | fdc_table[pos] = AcceptFD(pos); |
2b663917 | 1907 | } |
b0469965 | 1908 | |
1909 | commfd_table = (comm_fd_t *) xcalloc(Squid_MaxFD, sizeof(comm_fd_t)); | |
2b663917 | 1910 | for (int pos = 0; pos < Squid_MaxFD; pos++) { |
1911 | commfd_table[pos].fd = pos; | |
1912 | commfd_table[pos].readcb.fd = pos; | |
1913 | commfd_table[pos].readcb.type = IOCB_READ; | |
1914 | commfd_table[pos].writecb.fd = pos; | |
1915 | commfd_table[pos].writecb.type = IOCB_WRITE; | |
1916 | } | |
2d8c0b1a | 1917 | |
59c4d35b | 1918 | /* XXX account fd_table */ |
090089c4 | 1919 | /* Keep a few file descriptors free so that we don't run out of FD's |
1920 | * after accepting a client but before it opens a socket or a file. | |
e83892e9 | 1921 | * Since Squid_MaxFD can be as high as several thousand, don't waste them */ |
0254ee29 | 1922 | RESERVED_FD = XMIN(100, Squid_MaxFD / 4); |
2d8c0b1a | 1923 | |
04eb0689 | 1924 | conn_close_pool = memPoolCreate("close_handler", sizeof(close_handler)); |
74257126 AR |
1925 | |
1926 | TheHalfClosed = new DescriptorSet; | |
090089c4 | 1927 | } |
1928 | ||
236d1779 | 1929 | void |
1930 | comm_exit(void) { | |
74257126 AR |
1931 | delete TheHalfClosed; |
1932 | TheHalfClosed = NULL; | |
1933 | ||
236d1779 | 1934 | safe_free(fd_table); |
1935 | safe_free(fdd_table); | |
1936 | if (fdc_table) { | |
1937 | delete[] fdc_table; | |
1938 | fdc_table = NULL; | |
1939 | } | |
1940 | safe_free(commfd_table); | |
1941 | } | |
1942 | ||
30a4f2a8 | 1943 | /* Write to FD. */ |
b8d8561b | 1944 | static void |
a50bfe93 | 1945 | commHandleWrite(int fd, void *data) { |
2b663917 | 1946 | comm_io_callback_t *state = (comm_io_callback_t *)data; |
30a4f2a8 | 1947 | int len = 0; |
1948 | int nleft; | |
1949 | ||
2b663917 | 1950 | assert(state == COMMIO_FD_WRITECB(fd)); |
1951 | ||
88bfe092 | 1952 | PROF_start(commHandleWrite); |
bf8fe701 | 1953 | debugs(5, 5, "commHandleWrite: FD " << fd << ": off " << |
1954 | (long int) state->offset << ", sz " << (long int) state->size << "."); | |
30a4f2a8 | 1955 | |
1956 | nleft = state->size - state->offset; | |
1f7c9178 | 1957 | len = FD_WRITE_METHOD(fd, state->buf + state->offset, nleft); |
bf8fe701 | 1958 | debugs(5, 5, "commHandleWrite: write() returns " << len); |
b69f7771 | 1959 | fd_bytes(fd, len, FD_WRITE); |
83704487 | 1960 | statCounter.syscalls.sock.writes++; |
30a4f2a8 | 1961 | |
1962 | if (len == 0) { | |
62e76326 | 1963 | /* Note we even call write if nleft == 0 */ |
1964 | /* We're done */ | |
1965 | ||
1966 | if (nleft != 0) | |
bf8fe701 | 1967 | debugs(5, 1, "commHandleWrite: FD " << fd << ": write failure: connection closed with " << nleft << " bytes remaining."); |
62e76326 | 1968 | |
b0469965 | 1969 | commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_ERROR : COMM_OK, errno); |
30a4f2a8 | 1970 | } else if (len < 0) { |
62e76326 | 1971 | /* An error */ |
1972 | ||
1973 | if (fd_table[fd].flags.socket_eof) { | |
bf8fe701 | 1974 | debugs(50, 2, "commHandleWrite: FD " << fd << ": write failure: " << xstrerror() << "."); |
b0469965 | 1975 | commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_ERROR : COMM_OK, errno); |
62e76326 | 1976 | } else if (ignoreErrno(errno)) { |
bf8fe701 | 1977 | debugs(50, 10, "commHandleWrite: FD " << fd << ": write failure: " << xstrerror() << "."); |
62e76326 | 1978 | commSetSelect(fd, |
1979 | COMM_SELECT_WRITE, | |
1980 | commHandleWrite, | |
1981 | state, | |
1982 | 0); | |
1983 | } else { | |
bf8fe701 | 1984 | debugs(50, 2, "commHandleWrite: FD " << fd << ": write failure: " << xstrerror() << "."); |
b0469965 | 1985 | commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_ERROR : COMM_OK, errno); |
62e76326 | 1986 | } |
30a4f2a8 | 1987 | } else { |
62e76326 | 1988 | /* A successful write, continue */ |
1989 | state->offset += len; | |
1990 | ||
57d55dfa | 1991 | if (state->offset < state->size) { |
62e76326 | 1992 | /* Not done, reinstall the write handler and write some more */ |
1993 | commSetSelect(fd, | |
1994 | COMM_SELECT_WRITE, | |
1995 | commHandleWrite, | |
1996 | state, | |
1997 | 0); | |
1998 | } else { | |
b0469965 | 1999 | commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_OK : COMM_ERROR, errno); |
62e76326 | 2000 | } |
30a4f2a8 | 2001 | } |
62e76326 | 2002 | |
88bfe092 | 2003 | PROF_stop(commHandleWrite); |
30a4f2a8 | 2004 | } |
2005 | ||
7cd8c414 | 2006 | /* |
2007 | * Queue a write. handler/handler_data are called when the write | |
2008 | * completes, on error, or on file descriptor close. | |
2009 | * | |
2010 | * free_func is used to free the passed buffer when the write has completed. | |
2011 | */ | |
b8d8561b | 2012 | void |
2b663917 | 2013 | comm_write(int fd, const char *buf, int size, IOCB * handler, void *handler_data, FREE * free_func) |
b0469965 | 2014 | { |
2015 | AsyncCall::Pointer call = commCbCall(5,5, "SomeCommWriteHander", | |
2016 | CommIoCbPtrFun(handler, handler_data)); | |
2017 | ||
2018 | comm_write(fd, buf, size, call, free_func); | |
2019 | } | |
2020 | ||
2021 | void | |
2022 | comm_write(int fd, const char *buf, int size, AsyncCall::Pointer &callback, FREE * free_func) | |
2b663917 | 2023 | { |
82ec8dfc | 2024 | debugs(5, 5, "comm_write: FD " << fd << ": sz " << size << ": asynCall " << callback); |
62e76326 | 2025 | |
82ec8dfc AR |
2026 | /* Make sure we are open, not closing, and not writing */ |
2027 | assert(isOpen(fd)); | |
2028 | assert(!fd_table[fd].closing()); | |
2029 | comm_io_callback_t *ccb = COMMIO_FD_WRITECB(fd); | |
2030 | assert(!ccb->active()); | |
b0469965 | 2031 | |
82ec8dfc AR |
2032 | /* Queue the write */ |
2033 | commio_set_callback(fd, IOCB_WRITE, ccb, callback, | |
2034 | (char *)buf, free_func, size); | |
2035 | commSetSelect(fd, COMM_SELECT_WRITE, commHandleWrite, ccb, 0); | |
30a4f2a8 | 2036 | } |
26a880e2 | 2037 | |
b0469965 | 2038 | |
137ee196 | 2039 | /* a wrapper around comm_write to allow for MemBuf to be comm_written in a snap */ |
cb69b4c7 | 2040 | void |
2b663917 | 2041 | comm_write_mbuf(int fd, MemBuf *mb, IOCB * handler, void *handler_data) { |
2042 | comm_write(fd, mb->buf, mb->size, handler, handler_data, mb->freeFunc()); | |
cb69b4c7 | 2043 | } |
2044 | ||
b0469965 | 2045 | void |
2046 | comm_write_mbuf(int fd, MemBuf *mb, AsyncCall::Pointer &callback) { | |
2047 | comm_write(fd, mb->buf, mb->size, callback, mb->freeFunc()); | |
2048 | } | |
2049 | ||
c4b7a5a9 | 2050 | |
89924214 | 2051 | /* |
2052 | * hm, this might be too general-purpose for all the places we'd | |
2053 | * like to use it. | |
2054 | */ | |
b224ea98 | 2055 | int |
a50bfe93 | 2056 | ignoreErrno(int ierrno) { |
603500e7 | 2057 | switch (ierrno) { |
62e76326 | 2058 | |
89924214 | 2059 | case EINPROGRESS: |
62e76326 | 2060 | |
603500e7 | 2061 | case EWOULDBLOCK: |
26a880e2 | 2062 | #if EAGAIN != EWOULDBLOCK |
62e76326 | 2063 | |
603500e7 | 2064 | case EAGAIN: |
26a880e2 | 2065 | #endif |
62e76326 | 2066 | |
603500e7 | 2067 | case EALREADY: |
62e76326 | 2068 | |
603500e7 | 2069 | case EINTR: |
db494ab8 | 2070 | #ifdef ERESTART |
62e76326 | 2071 | |
db494ab8 | 2072 | case ERESTART: |
2073 | #endif | |
62e76326 | 2074 | |
2075 | return 1; | |
2076 | ||
603500e7 | 2077 | default: |
62e76326 | 2078 | return 0; |
603500e7 | 2079 | } |
62e76326 | 2080 | |
603500e7 | 2081 | /* NOTREACHED */ |
26a880e2 | 2082 | } |
d723bf6b | 2083 | |
2084 | void | |
a50bfe93 | 2085 | commCloseAllSockets(void) { |
d723bf6b | 2086 | int fd; |
2087 | fde *F = NULL; | |
62e76326 | 2088 | |
d723bf6b | 2089 | for (fd = 0; fd <= Biggest_FD; fd++) { |
62e76326 | 2090 | F = &fd_table[fd]; |
2091 | ||
2092 | if (!F->flags.open) | |
2093 | continue; | |
2094 | ||
2095 | if (F->type != FD_SOCKET) | |
2096 | continue; | |
2097 | ||
2098 | if (F->flags.ipc) /* don't close inter-process sockets */ | |
2099 | continue; | |
2100 | ||
b0469965 | 2101 | if (F->timeoutHandler != NULL) { |
2102 | AsyncCall::Pointer callback = F->timeoutHandler; | |
2103 | F->timeoutHandler = NULL; | |
bf8fe701 | 2104 | debugs(5, 5, "commCloseAllSockets: FD " << fd << ": Calling timeout handler"); |
b0469965 | 2105 | ScheduleCallHere(callback); |
62e76326 | 2106 | } else { |
bf8fe701 | 2107 | debugs(5, 5, "commCloseAllSockets: FD " << fd << ": calling comm_close()"); |
62e76326 | 2108 | comm_close(fd); |
2109 | } | |
d723bf6b | 2110 | } |
2111 | } | |
1b3db6d9 | 2112 | |
2d8c0b1a | 2113 | static bool |
a50bfe93 | 2114 | AlreadyTimedOut(fde *F) { |
2d8c0b1a | 2115 | if (!F->flags.open) |
2116 | return true; | |
2117 | ||
2118 | if (F->timeout == 0) | |
2119 | return true; | |
2120 | ||
2121 | if (F->timeout > squid_curtime) | |
2122 | return true; | |
2123 | ||
2124 | return false; | |
2125 | } | |
2126 | ||
1b3db6d9 | 2127 | void |
a50bfe93 | 2128 | checkTimeouts(void) { |
1b3db6d9 | 2129 | int fd; |
2130 | fde *F = NULL; | |
b0469965 | 2131 | AsyncCall::Pointer callback; |
62e76326 | 2132 | |
1b3db6d9 | 2133 | for (fd = 0; fd <= Biggest_FD; fd++) { |
62e76326 | 2134 | F = &fd_table[fd]; |
2135 | ||
2d8c0b1a | 2136 | if (AlreadyTimedOut(F)) |
62e76326 | 2137 | continue; |
2138 | ||
bf8fe701 | 2139 | debugs(5, 5, "checkTimeouts: FD " << fd << " Expired"); |
62e76326 | 2140 | |
b0469965 | 2141 | if (F->timeoutHandler != NULL) { |
bf8fe701 | 2142 | debugs(5, 5, "checkTimeouts: FD " << fd << ": Call timeout handler"); |
b0469965 | 2143 | callback = F->timeoutHandler; |
2144 | F->timeoutHandler = NULL; | |
2145 | ScheduleCallHere(callback); | |
62e76326 | 2146 | } else { |
bf8fe701 | 2147 | debugs(5, 5, "checkTimeouts: FD " << fd << ": Forcing comm_close()"); |
62e76326 | 2148 | comm_close(fd); |
2149 | } | |
b5443c04 | 2150 | } |
2151 | } | |
2152 | ||
c4b7a5a9 | 2153 | /* |
2154 | * New-style listen and accept routines | |
2155 | * | |
2156 | * Listen simply registers our interest in an FD for listening, | |
2157 | * and accept takes a callback to call when an FD has been | |
2158 | * accept()ed. | |
2159 | */ | |
2160 | int | |
a50bfe93 | 2161 | comm_listen(int sock) { |
c4b7a5a9 | 2162 | int x; |
62e76326 | 2163 | |
c4b7a5a9 | 2164 | if ((x = listen(sock, Squid_MaxFD >> 2)) < 0) { |
bf8fe701 | 2165 | debugs(50, 0, "comm_listen: listen(" << (Squid_MaxFD >> 2) << ", " << sock << "): " << xstrerror()); |
62e76326 | 2166 | return x; |
c4b7a5a9 | 2167 | } |
62e76326 | 2168 | |
0b4d4be5 | 2169 | if (Config.accept_filter && strcmp(Config.accept_filter, "none") != 0) { |
cc9f92d4 | 2170 | #ifdef SO_ACCEPTFILTER |
cc9f92d4 | 2171 | struct accept_filter_arg afa; |
2172 | bzero(&afa, sizeof(afa)); | |
e680134c | 2173 | debugs(5, DBG_CRITICAL, "Installing accept filter '" << Config.accept_filter << "' on FD " << sock); |
cc9f92d4 | 2174 | xstrncpy(afa.af_name, Config.accept_filter, sizeof(afa.af_name)); |
2175 | x = setsockopt(sock, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa)); | |
2176 | if (x < 0) | |
0b4d4be5 | 2177 | debugs(5, 0, "SO_ACCEPTFILTER '" << Config.accept_filter << "': '" << xstrerror()); |
2178 | #elif defined(TCP_DEFER_ACCEPT) | |
2179 | int seconds = 30; | |
2180 | if (strncmp(Config.accept_filter, "data=", 5) == 0) | |
2181 | seconds = atoi(Config.accept_filter + 5); | |
2182 | x = setsockopt(sock, IPPROTO_TCP, TCP_DEFER_ACCEPT, &seconds, sizeof(seconds)); | |
2183 | if (x < 0) | |
2184 | debugs(5, 0, "TCP_DEFER_ACCEPT '" << Config.accept_filter << "': '" << xstrerror()); | |
2185 | #else | |
2186 | debugs(5, 0, "accept_filter not supported on your OS"); | |
cc9f92d4 | 2187 | #endif |
0b4d4be5 | 2188 | } |
cc9f92d4 | 2189 | |
c4b7a5a9 | 2190 | return sock; |
2191 | } | |
2192 | ||
2d8c0b1a | 2193 | void |
b0469965 | 2194 | comm_accept(int fd, IOACB *handler, void *handler_data) { |
2195 | debugs(5, 5, "comm_accept: FD " << fd << " handler: " << (void*)handler); | |
2196 | assert(isOpen(fd)); | |
2197 | ||
2198 | AsyncCall::Pointer call = commCbCall(5,5, "SomeCommAcceptHandler", | |
2199 | CommAcceptCbPtrFun(handler, handler_data)); | |
2200 | fdc_table[fd].subscribe(call); | |
2d8c0b1a | 2201 | } |
c4b7a5a9 | 2202 | |
b0469965 | 2203 | void |
2204 | comm_accept(int fd, AsyncCall::Pointer &call) { | |
2205 | debugs(5, 5, "comm_accept: FD " << fd << " AsyncCall: " << call); | |
2206 | assert(isOpen(fd)); | |
2207 | ||
2208 | fdc_table[fd].subscribe(call); | |
2d8c0b1a | 2209 | } |
62e76326 | 2210 | |
b0469965 | 2211 | // Called when somebody wants to be notified when our socket accepts new |
2212 | // connection. We do not probe the FD until there is such interest. | |
2d8c0b1a | 2213 | void |
b0469965 | 2214 | AcceptFD::subscribe(AsyncCall::Pointer &call) { |
2215 | /* make sure we're not pending! */ | |
2216 | assert(!theCallback); | |
2217 | theCallback = call; | |
2218 | ||
2219 | #if OPTIMISTIC_IO | |
2220 | mayAcceptMore = true; // even if we failed to accept last time | |
2221 | #endif | |
2222 | ||
2223 | if (mayAcceptMore) | |
2224 | acceptNext(); | |
2225 | else | |
2226 | commSetSelect(fd, COMM_SELECT_READ, comm_accept_try, NULL, 0); | |
2227 | } | |
2228 | ||
2229 | bool | |
2230 | AcceptFD::acceptOne() { | |
c99de607 | 2231 | // If there is no callback and we accept, we will leak the accepted FD. |
2232 | // When we are running out of FDs, there is often no callback. | |
b0469965 | 2233 | if (!theCallback) { |
2234 | debugs(5, 5, "AcceptFD::acceptOne orphaned: FD " << fd); | |
c99de607 | 2235 | // XXX: can we remove this and similar "just in case" calls and |
2236 | // either listen always or listen only when there is a callback? | |
2237 | if (!AcceptLimiter::Instance().deferring()) | |
2238 | commSetSelect(fd, COMM_SELECT_READ, comm_accept_try, NULL, 0); | |
b0469965 | 2239 | return false; |
c99de607 | 2240 | } |
2241 | ||
bdd8c442 | 2242 | /* |
2243 | * We don't worry about running low on FDs here. Instead, | |
2244 | * httpAccept() will use AcceptLimiter if we reach the limit | |
2245 | * there. | |
2246 | */ | |
62e76326 | 2247 | |
2d8c0b1a | 2248 | /* Accept a new connection */ |
b0469965 | 2249 | ConnectionDetail connDetails; |
2250 | int newfd = comm_old_accept(fd, connDetails); | |
62e76326 | 2251 | |
2d8c0b1a | 2252 | /* Check for errors */ |
bdd8c442 | 2253 | |
2d8c0b1a | 2254 | if (newfd < 0) { |
b0469965 | 2255 | assert(theCallback != NULL); |
2256 | ||
2d8c0b1a | 2257 | if (newfd == COMM_NOMESSAGE) { |
2258 | /* register interest again */ | |
10b06767 | 2259 | debugs(5, 5, HERE << "try later: FD " << fd << |
b0469965 | 2260 | " handler: " << *theCallback); |
2d8c0b1a | 2261 | commSetSelect(fd, COMM_SELECT_READ, comm_accept_try, NULL, 0); |
b0469965 | 2262 | return false; |
62e76326 | 2263 | } |
2264 | ||
b0469965 | 2265 | // A non-recoverable error; notify the caller */ |
2266 | notify(-1, COMM_ERROR, errno, connDetails); | |
2267 | return false; | |
2d8c0b1a | 2268 | } |
62e76326 | 2269 | |
b0469965 | 2270 | assert(theCallback != NULL); |
2271 | debugs(5, 5, "AcceptFD::acceptOne accepted: FD " << fd << | |
2272 | " newfd: " << newfd << " from: " << connDetails.peer << | |
2273 | " handler: " << *theCallback); | |
2274 | notify(newfd, COMM_OK, 0, connDetails); | |
2275 | return true; | |
2d8c0b1a | 2276 | } |
62e76326 | 2277 | |
2d8c0b1a | 2278 | void |
b0469965 | 2279 | AcceptFD::acceptNext() { |
2280 | mayAcceptMore = acceptOne(); | |
2d8c0b1a | 2281 | } |
62e76326 | 2282 | |
b0469965 | 2283 | void |
2284 | AcceptFD::notify(int newfd, comm_err_t errcode, int xerrno, const ConnectionDetail &connDetails) | |
2285 | { | |
2286 | if (theCallback != NULL) { | |
2287 | typedef CommAcceptCbParams Params; | |
2288 | Params ¶ms = GetCommParams<Params>(theCallback); | |
2289 | params.fd = fd; | |
2290 | params.nfd = newfd; | |
2291 | params.details = connDetails; | |
2292 | params.flag = errcode; | |
2293 | params.xerrno = xerrno; | |
2294 | ScheduleCallHere(theCallback); | |
2295 | theCallback = NULL; | |
2296 | } | |
c4b7a5a9 | 2297 | } |
2298 | ||
2d8c0b1a | 2299 | /* |
2300 | * This callback is called whenever a filedescriptor is ready | |
2301 | * to dupe itself and fob off an accept()ed connection | |
2302 | */ | |
2303 | static void | |
b0469965 | 2304 | comm_accept_try(int fd, void *) { |
2305 | assert(isOpen(fd)); | |
2306 | fdc_table[fd].acceptNext(); | |
c4b7a5a9 | 2307 | } |
6cce2334 | 2308 | |
a50bfe93 | 2309 | void CommIO::Initialise() { |
6cce2334 | 2310 | /* Initialize done pipe signal */ |
2311 | int DonePipe[2]; | |
1d5da050 | 2312 | if(pipe(DonePipe)) {} |
6cce2334 | 2313 | DoneFD = DonePipe[1]; |
2314 | DoneReadFD = DonePipe[0]; | |
d06925a4 | 2315 | fd_open(DoneReadFD, FD_PIPE, "async-io completetion event: main"); |
2316 | fd_open(DoneFD, FD_PIPE, "async-io completetion event: threads"); | |
2317 | commSetNonBlocking(DoneReadFD); | |
2318 | commSetNonBlocking(DoneFD); | |
2319 | commSetSelect(DoneReadFD, COMM_SELECT_READ, NULLFDHandler, NULL, 0); | |
6cce2334 | 2320 | Initialised = true; |
2321 | } | |
2322 | ||
d06925a4 | 2323 | void CommIO::NotifyIOClose() { |
2324 | /* Close done pipe signal */ | |
2325 | FlushPipe(); | |
2326 | close(DoneFD); | |
2327 | close(DoneReadFD); | |
2328 | fd_close(DoneFD); | |
2329 | fd_close(DoneReadFD); | |
2330 | Initialised = false; | |
2331 | } | |
2332 | ||
6cce2334 | 2333 | bool CommIO::Initialised = false; |
2334 | bool CommIO::DoneSignalled = false; | |
2335 | int CommIO::DoneFD = -1; | |
2336 | int CommIO::DoneReadFD = -1; | |
2337 | ||
2338 | void | |
a50bfe93 | 2339 | CommIO::FlushPipe() { |
6cce2334 | 2340 | char buf[256]; |
56410c89 | 2341 | FD_READ_METHOD(DoneReadFD, buf, sizeof(buf)); |
6cce2334 | 2342 | } |
2343 | ||
2344 | void | |
a50bfe93 | 2345 | CommIO::NULLFDHandler(int fd, void *data) { |
6cce2334 | 2346 | FlushPipe(); |
2347 | commSetSelect(fd, COMM_SELECT_READ, NULLFDHandler, NULL, 0); | |
2348 | } | |
2349 | ||
2350 | void | |
a50bfe93 | 2351 | CommIO::ResetNotifications() { |
6cce2334 | 2352 | if (DoneSignalled) { |
62e76326 | 2353 | FlushPipe(); |
2354 | DoneSignalled = false; | |
6cce2334 | 2355 | } |
2356 | } | |
a46d2c0e | 2357 | |
2358 | AcceptLimiter AcceptLimiter::Instance_; | |
2359 | ||
a50bfe93 | 2360 | AcceptLimiter &AcceptLimiter::Instance() { |
a46d2c0e | 2361 | return Instance_; |
2362 | } | |
2363 | ||
c99de607 | 2364 | bool |
2365 | AcceptLimiter::deferring() const { | |
2366 | return deferred.size() > 0; | |
2367 | } | |
2368 | ||
a46d2c0e | 2369 | void |
a50bfe93 | 2370 | AcceptLimiter::defer (int fd, Acceptor::AcceptorFunction *aFunc, void *data) { |
bf8fe701 | 2371 | debugs(5, 5, "AcceptLimiter::defer: FD " << fd << " handler: " << (void*)aFunc); |
a46d2c0e | 2372 | Acceptor temp; |
2373 | temp.theFunction = aFunc; | |
2374 | temp.acceptFD = fd; | |
2375 | temp.theData = data; | |
2376 | deferred.push_back(temp); | |
2377 | } | |
2378 | ||
2379 | void | |
a50bfe93 | 2380 | AcceptLimiter::kick() { |
c99de607 | 2381 | if (!deferring()) |
a46d2c0e | 2382 | return; |
2383 | ||
2384 | /* Yes, this means the first on is the last off.... | |
2385 | * If the list container was a little more friendly, we could sensibly us it. | |
2386 | */ | |
2387 | Acceptor temp = deferred.pop_back(); | |
2388 | ||
2389 | comm_accept (temp.acceptFD, temp.theFunction, temp.theData); | |
2390 | } | |
2391 | ||
82ec8dfc AR |
2392 | /// Start waiting for a possibly half-closed connection to close |
2393 | // by scheduling a read callback to a monitoring handler that | |
2394 | // will close the connection on read errors. | |
a46d2c0e | 2395 | void |
82ec8dfc | 2396 | commStartHalfClosedMonitor(int fd) { |
74257126 | 2397 | debugs(5, 5, HERE << "adding FD " << fd << " to " << *TheHalfClosed); |
82ec8dfc AR |
2398 | assert(isOpen(fd)); |
2399 | assert(!commHasHalfClosedMonitor(fd)); | |
74257126 AR |
2400 | (void)TheHalfClosed->add(fd); // could also assert the result |
2401 | commPlanHalfClosedCheck(); // may schedule check if we added the first FD | |
2402 | } | |
2403 | ||
2404 | static | |
2405 | void | |
2406 | commPlanHalfClosedCheck() | |
2407 | { | |
2408 | if (!WillCheckHalfClosed && !TheHalfClosed->empty()) { | |
2409 | eventAdd("commHalfClosedCheck", &commHalfClosedCheck, NULL, 1.0, 1); | |
2410 | WillCheckHalfClosed = true; | |
2411 | } | |
2412 | } | |
2413 | ||
2414 | /// iterates over all descriptors that may need half-closed tests and | |
2415 | /// calls comm_read for those that do; re-schedules the check if needed | |
2416 | static | |
2417 | void | |
2418 | commHalfClosedCheck(void *) { | |
2419 | debugs(5, 5, HERE << "checking " << *TheHalfClosed); | |
2420 | ||
2421 | typedef DescriptorSet::const_iterator DSCI; | |
2422 | const DSCI end = TheHalfClosed->end(); | |
2423 | for (DSCI i = TheHalfClosed->begin(); i != end; ++i) { | |
2424 | const int fd = *i; | |
2425 | if (!fd_table[fd].halfClosedReader) { // not reading already | |
2426 | AsyncCall::Pointer call = commCbCall(5,4, "commHalfClosedReader", | |
2427 | CommIoCbPtrFun(&commHalfClosedReader, NULL)); | |
2428 | comm_read(fd, NULL, 0, call); | |
2429 | fd_table[fd].halfClosedReader = call; | |
2430 | } | |
2431 | } | |
f900210a | 2432 | |
74257126 AR |
2433 | WillCheckHalfClosed = false; // as far as we know |
2434 | commPlanHalfClosedCheck(); // may need to check again | |
f900210a | 2435 | } |
2436 | ||
82ec8dfc AR |
2437 | /// checks whether we are waiting for possibly half-closed connection to close |
2438 | // We are monitoring if the read handler for the fd is the monitoring handler. | |
2439 | bool | |
2440 | commHasHalfClosedMonitor(int fd) { | |
74257126 | 2441 | return TheHalfClosed->has(fd); |
a46d2c0e | 2442 | } |
2443 | ||
82ec8dfc AR |
2444 | /// stop waiting for possibly half-closed connection to close |
2445 | static void | |
2446 | commStopHalfClosedMonitor(int const fd) { | |
74257126 AR |
2447 | debugs(5, 5, HERE << "removing FD " << fd << " from " << *TheHalfClosed); |
2448 | ||
2449 | // cancel the read if one was scheduled | |
2450 | AsyncCall::Pointer reader = fd_table[fd].halfClosedReader; | |
2451 | if (reader != NULL) | |
2452 | comm_read_cancel(fd, reader); | |
2453 | fd_table[fd].halfClosedReader = NULL; | |
2454 | ||
2455 | TheHalfClosed->del(fd); | |
a46d2c0e | 2456 | } |
2457 | ||
82ec8dfc AR |
2458 | /// I/O handler for the possibly half-closed connection monitoring code |
2459 | static void | |
2460 | commHalfClosedReader(int fd, char *, size_t size, comm_err_t flag, int, void *) { | |
2461 | // there cannot be more data coming in on half-closed connections | |
2462 | assert(size == 0); | |
74257126 AR |
2463 | assert(commHasHalfClosedMonitor(fd)); // or we would have canceled the read |
2464 | ||
2465 | fd_table[fd].halfClosedReader = NULL; // done reading, for now | |
a46d2c0e | 2466 | |
82ec8dfc AR |
2467 | // nothing to do if fd is being closed |
2468 | if (flag == COMM_ERR_CLOSING) | |
2469 | return; | |
a46d2c0e | 2470 | |
82ec8dfc AR |
2471 | // if read failed, close the connection |
2472 | if (flag != COMM_OK) { | |
2473 | debugs(5, 3, "commHalfClosedReader: closing FD " << fd); | |
2474 | comm_close(fd); | |
2475 | return; | |
2476 | } | |
a46d2c0e | 2477 | |
82ec8dfc | 2478 | // continue waiting for close or error |
74257126 | 2479 | commPlanHalfClosedCheck(); // make sure this fd will be checked again |
a46d2c0e | 2480 | } |
2481 | ||
a46d2c0e | 2482 | |
b0469965 | 2483 | CommRead::CommRead() : fd(-1), buf(NULL), len(0), callback(NULL) {} |
a46d2c0e | 2484 | |
b0469965 | 2485 | CommRead::CommRead(int fd_, char *buf_, int len_, AsyncCall::Pointer &callback_) |
2486 | : fd(fd_), buf(buf_), len(len_), callback(callback_) {} | |
a46d2c0e | 2487 | |
a50bfe93 | 2488 | DeferredRead::DeferredRead () : theReader(NULL), theContext(NULL), theRead(), cancelled(false) {} |
a46d2c0e | 2489 | |
a50bfe93 | 2490 | DeferredRead::DeferredRead (DeferrableRead *aReader, void *data, CommRead const &aRead) : theReader(aReader), theContext (data), theRead(aRead), cancelled(false) {} |
a46d2c0e | 2491 | |
a50bfe93 | 2492 | DeferredReadManager::~DeferredReadManager() { |
a46d2c0e | 2493 | flushReads(); |
2494 | assert (deferredReads.empty()); | |
2495 | } | |
2496 | ||
97427e90 | 2497 | /* explicit instantiation required for some systems */ |
2498 | ||
63be0a78 | 2499 | /// \cond AUTODOCS-IGNORE |
2236466c | 2500 | template cbdata_type CbDataList<DeferredRead>::CBDATA_CbDataList; |
63be0a78 | 2501 | /// \endcond |
97427e90 | 2502 | |
a46d2c0e | 2503 | void |
a50bfe93 | 2504 | DeferredReadManager::delayRead(DeferredRead const &aRead) { |
bf8fe701 | 2505 | debugs(5, 3, "Adding deferred read on FD " << aRead.theRead.fd); |
2236466c | 2506 | CbDataList<DeferredRead> *temp = deferredReads.push_back(aRead); |
2796c0d7 AR |
2507 | |
2508 | // We have to use a global function as a closer and point to temp | |
2509 | // instead of "this" because DeferredReadManager is not a job and | |
2510 | // is not even cbdata protected | |
2511 | AsyncCall::Pointer closer = commCbCall(5,4, | |
2512 | "DeferredReadManager::CloseHandler", | |
2513 | CommCloseCbPtrFun(&CloseHandler, temp)); | |
2514 | comm_add_close_handler(aRead.theRead.fd, closer); | |
2515 | temp->element.closer = closer; // remeber so that we can cancel | |
a46d2c0e | 2516 | } |
2517 | ||
2518 | void | |
a50bfe93 | 2519 | DeferredReadManager::CloseHandler(int fd, void *thecbdata) { |
a46d2c0e | 2520 | if (!cbdataReferenceValid (thecbdata)) |
2521 | return; | |
2522 | ||
2236466c | 2523 | CbDataList<DeferredRead> *temp = (CbDataList<DeferredRead> *)thecbdata; |
a46d2c0e | 2524 | |
2796c0d7 | 2525 | temp->element.closer = NULL; |
a46d2c0e | 2526 | temp->element.markCancelled(); |
2527 | } | |
2528 | ||
2529 | DeferredRead | |
2236466c | 2530 | DeferredReadManager::popHead(CbDataListContainer<DeferredRead> &deferredReads) { |
a46d2c0e | 2531 | assert (!deferredReads.empty()); |
2532 | ||
2796c0d7 AR |
2533 | DeferredRead &read = deferredReads.head->element; |
2534 | if (!read.cancelled) { | |
2535 | comm_remove_close_handler(read.theRead.fd, read.closer); | |
2536 | read.closer = NULL; | |
2537 | } | |
a46d2c0e | 2538 | |
2539 | DeferredRead result = deferredReads.pop_front(); | |
2540 | ||
2541 | return result; | |
2542 | } | |
2543 | ||
2544 | void | |
a50bfe93 | 2545 | DeferredReadManager::kickReads(int const count) { |
2236466c | 2546 | /* if we had CbDataList::size() we could consolidate this and flushReads */ |
a46d2c0e | 2547 | |
33cea91c | 2548 | if (count < 1) { |
a46d2c0e | 2549 | flushReads(); |
33cea91c | 2550 | return; |
2551 | } | |
a46d2c0e | 2552 | |
2553 | size_t remaining = count; | |
2554 | ||
2555 | while (!deferredReads.empty() && remaining) { | |
2556 | DeferredRead aRead = popHead(deferredReads); | |
2557 | kickARead(aRead); | |
2558 | ||
2559 | if (!aRead.cancelled) | |
2560 | --remaining; | |
2561 | } | |
2562 | } | |
2563 | ||
2564 | void | |
a50bfe93 | 2565 | DeferredReadManager::flushReads() { |
2236466c | 2566 | CbDataListContainer<DeferredRead> reads; |
a46d2c0e | 2567 | reads = deferredReads; |
2236466c | 2568 | deferredReads = CbDataListContainer<DeferredRead>(); |
a46d2c0e | 2569 | |
2570 | while (!reads.empty()) { | |
2571 | DeferredRead aRead = popHead(reads); | |
2572 | kickARead(aRead); | |
2573 | } | |
2574 | } | |
2575 | ||
2576 | void | |
a50bfe93 | 2577 | DeferredReadManager::kickARead(DeferredRead const &aRead) { |
a46d2c0e | 2578 | if (aRead.cancelled) |
2579 | return; | |
2580 | ||
bf8fe701 | 2581 | debugs(5, 3, "Kicking deferred read on FD " << aRead.theRead.fd); |
a46d2c0e | 2582 | |
2583 | aRead.theReader(aRead.theContext, aRead.theRead); | |
2584 | } | |
2585 | ||
2586 | void | |
a50bfe93 | 2587 | DeferredRead::markCancelled() { |
a46d2c0e | 2588 | cancelled = true; |
2589 | } | |
2d8c0b1a | 2590 | |
cc192b50 | 2591 | ConnectionDetail::ConnectionDetail() : me(), peer() { |
2d8c0b1a | 2592 | } |
8ff3fa2e | 2593 | |
8ff3fa2e | 2594 | int |
2595 | CommSelectEngine::checkEvents(int timeout) { | |
fa3f745b | 2596 | static time_t last_timeout = 0; |
2597 | ||
2598 | /* No, this shouldn't be here. But it shouldn't be in each comm handler. -adrian */ | |
2599 | if (squid_curtime > last_timeout) { | |
2600 | last_timeout = squid_curtime; | |
2601 | checkTimeouts(); | |
2602 | } | |
2603 | ||
8ff3fa2e | 2604 | switch (comm_select(timeout)) { |
2605 | ||
2606 | case COMM_OK: | |
2607 | ||
2608 | case COMM_TIMEOUT: | |
2609 | return 0; | |
2610 | ||
2611 | case COMM_IDLE: | |
2612 | ||
2613 | case COMM_SHUTDOWN: | |
2614 | return EVENT_IDLE; | |
2615 | ||
2616 | case COMM_ERROR: | |
2617 | return EVENT_ERROR; | |
2618 | ||
2619 | default: | |
2620 | fatal_dump("comm.cc: Internal error -- this should never happen."); | |
2621 | return EVENT_ERROR; | |
2622 | }; | |
2623 | } |