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