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