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