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