comm.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 05 Socket Functions */
10 
11 #include "squid.h"
12 #include "ClientInfo.h"
13 #include "comm/AcceptLimiter.h"
14 #include "comm/comm_internal.h"
15 #include "comm/Connection.h"
16 #include "comm/IoCallback.h"
17 #include "comm/Loops.h"
18 #include "comm/Read.h"
19 #include "comm/TcpAcceptor.h"
20 #include "comm/Write.h"
21 #include "CommRead.h"
22 #include "compat/cmsg.h"
23 #include "DescriptorSet.h"
24 #include "event.h"
25 #include "fd.h"
26 #include "fde.h"
27 #include "globals.h"
28 #include "icmp/net_db.h"
29 #include "ip/Intercept.h"
30 #include "ip/QosConfig.h"
31 #include "ip/tools.h"
32 #include "pconn.h"
33 #include "profiler/Profiler.h"
34 #include "sbuf/SBuf.h"
35 #include "sbuf/Stream.h"
36 #include "SquidConfig.h"
37 #include "StatCounters.h"
38 #include "StoreIOBuffer.h"
39 #include "tools.h"
40 
41 #if USE_OPENSSL
42 #include "ssl/support.h"
43 #endif
44 
45 #include <cerrno>
46 #include <cmath>
47 #if _SQUID_CYGWIN_
48 #include <sys/ioctl.h>
49 #endif
50 #ifdef HAVE_NETINET_TCP_H
51 #include <netinet/tcp.h>
52 #endif
53 #if HAVE_SYS_UN_H
54 #include <sys/un.h>
55 #endif
56 
57 /*
58  * New C-like simple comm code. This stuff is a mess and doesn't really buy us anything.
59  */
60 
62 static void comm_init_opened(const Comm::ConnectionPointer &conn, const char *note, struct addrinfo *AI);
63 static int comm_apply_flags(int new_socket, Ip::Address &addr, int flags, struct addrinfo *AI);
64 
65 #if USE_DELAY_POOLS
67 
68 static void commHandleWriteHelper(void * data);
69 #endif
70 
71 /* STATIC */
72 
74 static bool WillCheckHalfClosed = false;
76 static void commPlanHalfClosedCheck();
77 
78 static Comm::Flag commBind(int s, struct addrinfo &);
79 static void commSetReuseAddr(int);
80 static void commSetNoLinger(int);
81 #ifdef TCP_NODELAY
82 static void commSetTcpNoDelay(int);
83 #endif
84 static void commSetTcpRcvbuf(int, int);
85 
86 bool
87 isOpen(const int fd)
88 {
89  return fd >= 0 && fd_table && fd_table[fd].flags.open != 0;
90 }
91 
100 static void
102 {
103 #if _SQUID_LINUX_
104 #if USE_OPENSSL
105  // Bug 4146: SSL-Bump BIO does not release sockets on close.
106  if (fd_table[fd].ssl)
107  return;
108 #endif
109 
110  /* prevent those nasty RST packets */
111  char buf[SQUID_TCP_SO_RCVBUF];
112  if (fd_table[fd].flags.nonblocking && fd_table[fd].type != FD_MSGHDR) {
113  while (FD_READ_METHOD(fd, buf, SQUID_TCP_SO_RCVBUF) > 0) {};
114  }
115 #endif
116 }
117 
121 int
122 comm_udp_recvfrom(int fd, void *buf, size_t len, int flags, Ip::Address &from)
123 {
124  ++ statCounter.syscalls.sock.recvfroms;
125  debugs(5,8, "comm_udp_recvfrom: FD " << fd << " from " << from);
126  struct addrinfo *AI = NULL;
128  int x = recvfrom(fd, buf, len, flags, AI->ai_addr, &AI->ai_addrlen);
129  from = *AI;
131  return x;
132 }
133 
134 int
135 comm_udp_recv(int fd, void *buf, size_t len, int flags)
136 {
137  Ip::Address nul;
138  return comm_udp_recvfrom(fd, buf, len, flags, nul);
139 }
140 
141 ssize_t
142 comm_udp_send(int s, const void *buf, size_t len, int flags)
143 {
144  return send(s, buf, len, flags);
145 }
146 
147 bool
149 {
150  assert(isOpen(fd) && COMMIO_FD_WRITECB(fd) != NULL);
151  return COMMIO_FD_WRITECB(fd)->active();
152 }
153 
159 /* Return the local port associated with fd. */
160 unsigned short
162 {
163  Ip::Address temp;
164  struct addrinfo *addr = NULL;
165  fde *F = &fd_table[fd];
166 
167  /* If the fd is closed already, just return */
168 
169  if (!F->flags.open) {
170  debugs(5, 0, "comm_local_port: FD " << fd << " has been closed.");
171  return 0;
172  }
173 
174  if (F->local_addr.port())
175  return F->local_addr.port();
176 
177  if (F->sock_family == AF_INET)
178  temp.setIPv4();
179 
180  Ip::Address::InitAddr(addr);
181 
182  if (getsockname(fd, addr->ai_addr, &(addr->ai_addrlen)) ) {
183  int xerrno = errno;
184  debugs(50, DBG_IMPORTANT, MYNAME << "Failed to retrieve TCP/UDP port number for socket: FD " << fd << ": " << xstrerr(xerrno));
185  Ip::Address::FreeAddr(addr);
186  return 0;
187  }
188  temp = *addr;
189 
190  Ip::Address::FreeAddr(addr);
191 
192  if (F->local_addr.isAnyAddr()) {
193  /* save the whole local address, not just the port. */
194  F->local_addr = temp;
195  } else {
196  F->local_addr.port(temp.port());
197  }
198 
199  debugs(5, 6, "comm_local_port: FD " << fd << ": port " << F->local_addr.port() << "(family=" << F->sock_family << ")");
200  return F->local_addr.port();
201 }
202 
203 static Comm::Flag
204 commBind(int s, struct addrinfo &inaddr)
205 {
206  ++ statCounter.syscalls.sock.binds;
207 
208  if (bind(s, inaddr.ai_addr, inaddr.ai_addrlen) == 0) {
209  debugs(50, 6, "bind socket FD " << s << " to " << fd_table[s].local_addr);
210  return Comm::OK;
211  }
212  int xerrno = errno;
213  debugs(50, DBG_CRITICAL, MYNAME << "Cannot bind socket FD " << s << " to " << fd_table[s].local_addr << ": " << xstrerr(xerrno));
214 
215  return Comm::COMM_ERROR;
216 }
217 
222 int
223 comm_open(int sock_type,
224  int proto,
225  Ip::Address &addr,
226  int flags,
227  const char *note)
228 {
229  return comm_openex(sock_type, proto, addr, flags, note);
230 }
231 
232 void
233 comm_open_listener(int sock_type,
234  int proto,
236  const char *note)
237 {
238  /* all listener sockets require bind() */
239  conn->flags |= COMM_DOBIND;
240 
241  /* attempt native enabled port. */
242  conn->fd = comm_openex(sock_type, proto, conn->local, conn->flags, note);
243 }
244 
245 int
246 comm_open_listener(int sock_type,
247  int proto,
248  Ip::Address &addr,
249  int flags,
250  const char *note)
251 {
252  int sock = -1;
253 
254  /* all listener sockets require bind() */
255  flags |= COMM_DOBIND;
256 
257  /* attempt native enabled port. */
258  sock = comm_openex(sock_type, proto, addr, flags, note);
259 
260  return sock;
261 }
262 
263 static bool
264 limitError(int const anErrno)
265 {
266  return anErrno == ENFILE || anErrno == EMFILE;
267 }
268 
269 void
270 comm_set_v6only(int fd, int tos)
271 {
272 #ifdef IPV6_V6ONLY
273  if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &tos, sizeof(int)) < 0) {
274  int xerrno = errno;
275  debugs(50, DBG_IMPORTANT, MYNAME << "setsockopt(IPV6_V6ONLY) " << (tos?"ON":"OFF") << " for FD " << fd << ": " << xstrerr(xerrno));
276  }
277 #else
278  debugs(50, DBG_CRITICAL, MYNAME << "WARNING: setsockopt(IPV6_V6ONLY) not supported on this platform");
279 #endif /* sockopt */
280 }
281 
288 void
290 {
291 #if _SQUID_LINUX_ && defined(IP_TRANSPARENT) // Linux
292 # define soLevel SOL_IP
293 # define soFlag IP_TRANSPARENT
294  bool doneSuid = false;
295 
296 #elif defined(SO_BINDANY) // OpenBSD 4.7+ and NetBSD with PF
297 # define soLevel SOL_SOCKET
298 # define soFlag SO_BINDANY
299  enter_suid();
300  bool doneSuid = true;
301 
302 #elif defined(IP_BINDANY) // FreeBSD with IPFW
303 # define soLevel IPPROTO_IP
304 # define soFlag IP_BINDANY
305  enter_suid();
306  bool doneSuid = true;
307 
308 #else
309  debugs(50, DBG_CRITICAL, "WARNING: comm_open: setsockopt(TPROXY) not supported on this platform");
310 #endif /* sockopt */
311 
312 #if defined(soLevel) && defined(soFlag)
313  int tos = 1;
314  if (setsockopt(fd, soLevel, soFlag, (char *) &tos, sizeof(int)) < 0) {
315  int xerrno = errno;
316  debugs(50, DBG_IMPORTANT, MYNAME << "setsockopt(TPROXY) on FD " << fd << ": " << xstrerr(xerrno));
317  } else {
318  /* mark the socket as having transparent options */
319  fd_table[fd].flags.transparent = true;
320  }
321  if (doneSuid)
322  leave_suid();
323 #endif
324 }
325 
330 int
331 comm_openex(int sock_type,
332  int proto,
333  Ip::Address &addr,
334  int flags,
335  const char *note)
336 {
337  int new_socket;
338  struct addrinfo *AI = NULL;
339 
341  /* Create socket for accepting new connections. */
342  ++ statCounter.syscalls.sock.sockets;
343 
344  /* Setup the socket addrinfo details for use */
345  addr.getAddrInfo(AI);
346  AI->ai_socktype = sock_type;
347  AI->ai_protocol = proto;
348 
349  debugs(50, 3, "comm_openex: Attempt open socket for: " << addr );
350 
351  new_socket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
352  int xerrno = errno;
353 
354  /* under IPv6 there is the possibility IPv6 is present but disabled. */
355  /* try again as IPv4-native if possible */
356  if ( new_socket < 0 && Ip::EnableIpv6 && addr.isIPv6() && addr.setIPv4() ) {
357  /* attempt to open this IPv4-only. */
359  /* Setup the socket addrinfo details for use */
360  addr.getAddrInfo(AI);
361  AI->ai_socktype = sock_type;
362  AI->ai_protocol = proto;
363  debugs(50, 3, "Attempt fallback open socket for: " << addr );
364  new_socket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
365  debugs(50, 2, "attempt open " << note << " socket on: " << addr);
366  }
367 
368  if (new_socket < 0) {
369  /* Increase the number of reserved fd's if calls to socket()
370  * are failing because the open file table is full. This
371  * limits the number of simultaneous clients */
372 
373  if (limitError(errno)) {
374  debugs(50, DBG_IMPORTANT, MYNAME << "socket failure: " << xstrerr(xerrno));
376  } else {
377  debugs(50, DBG_CRITICAL, MYNAME << "socket failure: " << xstrerr(xerrno));
378  }
379 
381 
383  errno = xerrno; // restore for caller
384  return -1;
385  }
386 
387  // XXX: temporary for the transition. comm_openex will eventually have a conn to play with.
389  conn->local = addr;
390  conn->fd = new_socket;
391 
392  debugs(50, 3, "comm_openex: Opened socket " << conn << " : family=" << AI->ai_family << ", type=" << AI->ai_socktype << ", protocol=" << AI->ai_protocol );
393 
395  comm_set_v6only(conn->fd, 1);
396 
397  /* Windows Vista supports Dual-Sockets. BUT defaults them to V6ONLY. Turn it OFF. */
398  /* Other OS may have this administratively disabled for general use. Same deal. */
400  comm_set_v6only(conn->fd, 0);
401 
402  comm_init_opened(conn, note, AI);
403  new_socket = comm_apply_flags(conn->fd, addr, flags, AI);
404 
406 
408 
409  // XXX transition only. prevent conn from closing the new FD on function exit.
410  conn->fd = -1;
411  errno = xerrno; // restore for caller
412  return new_socket;
413 }
414 
416 void
418  const char *note,
419  struct addrinfo *AI)
420 {
422  assert(AI);
423 
424  /* update fdstat */
425  debugs(5, 5, HERE << conn << " is a new socket");
426 
427  assert(!isOpen(conn->fd)); // NP: global isOpen checks the fde entry for openness not the Comm::Connection
428  fd_open(conn->fd, FD_SOCKET, note);
429 
430  fde *F = &fd_table[conn->fd];
431  F->local_addr = conn->local;
432 
433  F->sock_family = AI->ai_family;
434 }
435 
438 static int
439 comm_apply_flags(int new_socket,
440  Ip::Address &addr,
441  int flags,
442  struct addrinfo *AI)
443 {
444  assert(new_socket >= 0);
445  assert(AI);
446  const int sock_type = AI->ai_socktype;
447 
448  if (!(flags & COMM_NOCLOEXEC))
449  commSetCloseOnExec(new_socket);
450 
451  if ((flags & COMM_REUSEADDR))
452  commSetReuseAddr(new_socket);
453 
454  if (addr.port() > (unsigned short) 0) {
455 #if _SQUID_WINDOWS_
456  if (sock_type != SOCK_DGRAM)
457 #endif
458  commSetNoLinger(new_socket);
459 
460  if (opt_reuseaddr)
461  commSetReuseAddr(new_socket);
462  }
463 
464  /* MUST be done before binding or face OS Error: "(99) Cannot assign requested address"... */
465  if ((flags & COMM_TRANSPARENT)) {
466  comm_set_transparent(new_socket);
467  }
468 
469  if ( (flags & COMM_DOBIND) || addr.port() > 0 || !addr.isAnyAddr() ) {
470  if ( !(flags & COMM_DOBIND) && addr.isAnyAddr() )
471  debugs(5, DBG_IMPORTANT,"WARNING: Squid is attempting to bind() port " << addr << " without being a listener.");
472  if ( addr.isNoAddr() )
473  debugs(5,0,"CRITICAL: Squid is attempting to bind() port " << addr << "!!");
474 
475 #if defined(SO_REUSEPORT)
476  if (flags & COMM_REUSEPORT) {
477  int on = 1;
478  if (setsockopt(new_socket, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<char*>(&on), sizeof(on)) < 0) {
479  const auto savedErrno = errno;
480  const auto errorMessage = ToSBuf("cannot enable SO_REUSEPORT socket option when binding to ",
481  addr, ": ", xstrerr(savedErrno));
482  if (reconfiguring)
483  debugs(5, DBG_IMPORTANT, "ERROR: " << errorMessage);
484  else
485  throw TexcHere(errorMessage);
486  }
487  }
488 #endif
489  if (commBind(new_socket, *AI) != Comm::OK) {
490  comm_close(new_socket);
491  return -1;
492  }
493  }
494 
495  if (flags & COMM_NONBLOCKING)
496  if (commSetNonBlocking(new_socket) == Comm::COMM_ERROR) {
497  comm_close(new_socket);
498  return -1;
499  }
500 
501 #ifdef TCP_NODELAY
502  if (sock_type == SOCK_STREAM)
503  commSetTcpNoDelay(new_socket);
504 
505 #endif
506 
507  if (Config.tcpRcvBufsz > 0 && sock_type == SOCK_STREAM)
508  commSetTcpRcvbuf(new_socket, Config.tcpRcvBufsz);
509 
510  return new_socket;
511 }
512 
513 void
515  const char *note,
516  struct addrinfo *AI)
517 {
518  debugs(5, 2, HERE << conn);
520  assert(AI);
521 
522  comm_init_opened(conn, note, AI);
523 
524  if (!(conn->flags & COMM_NOCLOEXEC))
525  fd_table[conn->fd].flags.close_on_exec = true;
526 
527  if (conn->local.port() > (unsigned short) 0) {
528 #if _SQUID_WINDOWS_
529  if (AI->ai_socktype != SOCK_DGRAM)
530 #endif
531  fd_table[conn->fd].flags.nolinger = true;
532  }
533 
534  if ((conn->flags & COMM_TRANSPARENT))
535  fd_table[conn->fd].flags.transparent = true;
536 
537  if (conn->flags & COMM_NONBLOCKING)
538  fd_table[conn->fd].flags.nonblocking = true;
539 
540 #ifdef TCP_NODELAY
541  if (AI->ai_socktype == SOCK_STREAM)
542  fd_table[conn->fd].flags.nodelay = true;
543 #endif
544 
545  /* no fd_table[fd].flags. updates needed for these conditions:
546  * if ((flags & COMM_REUSEADDR)) ...
547  * if ((flags & COMM_DOBIND) ...) ...
548  */
549 }
550 
551 // XXX: now that raw-FD timeouts are only unset for pipes and files this SHOULD be a no-op.
552 // With handler already unset. Leaving this present until that can be verified for all code paths.
553 void
555 {
556  debugs(5, 3, HERE << "Remove timeout for FD " << fd);
557  assert(fd >= 0);
558  assert(fd < Squid_MaxFD);
559  fde *F = &fd_table[fd];
560  assert(F->flags.open);
561 
562  F->timeoutHandler = NULL;
563  F->timeout = 0;
564 }
565 
566 int
568 {
569  debugs(5, 3, HERE << conn << " timeout " << timeout);
571  assert(conn->fd < Squid_MaxFD);
572  fde *F = &fd_table[conn->fd];
573  assert(F->flags.open);
574 
575  if (timeout < 0) {
576  F->timeoutHandler = NULL;
577  F->timeout = 0;
578  } else {
579  if (callback != NULL) {
580  typedef CommTimeoutCbParams Params;
581  Params &params = GetCommParams<Params>(callback);
582  params.conn = conn;
583  F->timeoutHandler = callback;
584  }
585 
586  F->timeout = squid_curtime + (time_t) timeout;
587  }
588 
589  return F->timeout;
590 }
591 
592 int
594 {
595  debugs(5, 3, HERE << "Remove timeout for " << conn);
597  return commSetConnTimeout(conn, -1, nil);
598 }
599 
605 int
606 comm_connect_addr(int sock, const Ip::Address &address)
607 {
608  Comm::Flag status = Comm::OK;
609  fde *F = &fd_table[sock];
610  int x = 0;
611  int err = 0;
612  socklen_t errlen;
613  struct addrinfo *AI = NULL;
615 
616  assert(address.port() != 0);
617 
618  debugs(5, 9, HERE << "connecting socket FD " << sock << " to " << address << " (want family: " << F->sock_family << ")");
619 
620  /* Handle IPv6 over IPv4-only socket case.
621  * this case must presently be handled here since the getAddrInfo asserts on bad mappings.
622  * NP: because commResetFD is private to ConnStateData we have to return an error and
623  * trust its handled properly.
624  */
625  if (F->sock_family == AF_INET && !address.isIPv4()) {
626  errno = ENETUNREACH;
627  return Comm::ERR_PROTOCOL;
628  }
629 
630  /* Handle IPv4 over IPv6-only socket case.
631  * This case is presently handled here as it's both a known case and it's
632  * uncertain what error will be returned by the IPv6 stack in such case. It's
633  * possible this will also be handled by the errno checks below after connect()
634  * but needs careful cross-platform verification, and verifying the address
635  * condition here is simple.
636  */
637  if (!F->local_addr.isIPv4() && address.isIPv4()) {
638  errno = ENETUNREACH;
639  return Comm::ERR_PROTOCOL;
640  }
641 
642  address.getAddrInfo(AI, F->sock_family);
643 
644  /* Establish connection. */
645  int xerrno = 0;
646 
647  if (!F->flags.called_connect) {
648  F->flags.called_connect = true;
649  ++ statCounter.syscalls.sock.connects;
650 
651  errno = 0;
652  if ((x = connect(sock, AI->ai_addr, AI->ai_addrlen)) < 0) {
653  xerrno = errno;
654  debugs(5,5, "sock=" << sock << ", addrinfo(" <<
655  " flags=" << AI->ai_flags <<
656  ", family=" << AI->ai_family <<
657  ", socktype=" << AI->ai_socktype <<
658  ", protocol=" << AI->ai_protocol <<
659  ", &addr=" << AI->ai_addr <<
660  ", addrlen=" << AI->ai_addrlen << " )");
661  debugs(5, 9, "connect FD " << sock << ": (" << x << ") " << xstrerr(xerrno));
662  debugs(14,9, "connecting to: " << address);
663 
664  } else if (x == 0) {
665  // XXX: ICAP code refuses callbacks during a pending comm_ call
666  // Async calls development will fix this.
667  x = -1;
668  xerrno = EINPROGRESS;
669  }
670 
671  } else {
672  errno = 0;
673 #if _SQUID_NEWSOS6_
674  /* Makoto MATSUSHITA <matusita@ics.es.osaka-u.ac.jp> */
675  if (connect(sock, AI->ai_addr, AI->ai_addrlen) < 0)
676  xerrno = errno;
677 
678  if (xerrno == EINVAL) {
679  errlen = sizeof(err);
680  x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen);
681  if (x >= 0)
682  xerrno = x;
683  }
684 #else
685  errlen = sizeof(err);
686  x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen);
687  if (x == 0)
688  xerrno = err;
689 
690 #if _SQUID_SOLARIS_
691  /*
692  * Solaris 2.4's socket emulation doesn't allow you
693  * to determine the error from a failed non-blocking
694  * connect and just returns EPIPE. Create a fake
695  * error message for connect. -- fenner@parc.xerox.com
696  */
697  if (x < 0 && xerrno == EPIPE)
698  xerrno = ENOTCONN;
699  else
700  xerrno = errno;
701 #endif
702 #endif
703  }
704 
706 
708 
709  errno = xerrno;
710  if (xerrno == 0 || xerrno == EISCONN)
711  status = Comm::OK;
712  else if (ignoreErrno(xerrno))
713  status = Comm::INPROGRESS;
714  else if (xerrno == EAFNOSUPPORT || xerrno == EINVAL)
715  return Comm::ERR_PROTOCOL;
716  else
717  return Comm::COMM_ERROR;
718 
719  address.toStr(F->ipaddr, MAX_IPSTRLEN);
720 
721  F->remote_port = address.port(); /* remote_port is HS */
722 
723  if (status == Comm::OK) {
724  debugs(5, DBG_DATA, "comm_connect_addr: FD " << sock << " connected to " << address);
725  } else if (status == Comm::INPROGRESS) {
726  debugs(5, DBG_DATA, "comm_connect_addr: FD " << sock << " connection pending");
727  }
728 
729  errno = xerrno;
730  return status;
731 }
732 
733 void
735 {
736  fde *F = &fd_table[fd];
737  debugs(5, 5, "commCallCloseHandlers: FD " << fd);
738 
739  while (F->closeHandler != NULL) {
740  AsyncCall::Pointer call = F->closeHandler;
741  F->closeHandler = call->Next();
742  call->setNext(NULL);
743  // If call is not canceled schedule it for execution else ignore it
744  if (!call->canceled()) {
745  debugs(5, 5, "commCallCloseHandlers: ch->handler=" << call);
746  ScheduleCallHere(call);
747  }
748  }
749 }
750 
755 void
757 {
758  struct linger L;
759  L.l_onoff = 1;
760  L.l_linger = 0;
761 
762  if (setsockopt(conn->fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0) {
763  int xerrno = errno;
764  debugs(50, DBG_CRITICAL, "ERROR: Closing " << conn << " with TCP RST: " << xstrerr(xerrno));
765  }
766  conn->close();
767 }
768 
769 // Legacy close function.
770 void
772 {
773  struct linger L;
774  L.l_onoff = 1;
775  L.l_linger = 0;
776 
777  if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0) {
778  int xerrno = errno;
779  debugs(50, DBG_CRITICAL, "ERROR: Closing FD " << fd << " with TCP RST: " << xstrerr(xerrno));
780  }
781  comm_close(fd);
782 }
783 
784 void
786 {
788 }
789 
790 void
792 {
793  fde *F = &fd_table[params.fd];
794  F->ssl.reset();
795  F->dynamicTlsContext.reset();
796  fd_close(params.fd); /* update fdstat */
797  close(params.fd);
798 
799  ++ statCounter.syscalls.sock.closes;
800 
801  /* When one connection closes, give accept() a chance, if need be */
802  CodeContext::Reset(); // exit FD-specific context
804 }
805 
806 /*
807  * Close the socket fd.
808  *
809  * + call write handlers with ERR_CLOSING
810  * + call read handlers with ERR_CLOSING
811  * + call closing handlers
812  *
813  * NOTE: Comm::ERR_CLOSING will NOT be called for CommReads' sitting in a
814  * DeferredReadManager.
815  */
816 void
817 _comm_close(int fd, char const *file, int line)
818 {
819  debugs(5, 3, "start closing FD " << fd << " by " << file << ":" << line);
820  assert(fd >= 0);
821  assert(fd < Squid_MaxFD);
822 
823  fde *F = &fd_table[fd];
824 
825  if (F->closing())
826  return;
827 
828  /* XXX: is this obsolete behind F->closing() ? */
829  if ( (shutting_down || reconfiguring) && (!F->flags.open || F->type == FD_FILE))
830  return;
831 
832  /* The following fails because ipc.c is doing calls to pipe() to create sockets! */
833  if (!isOpen(fd)) {
834  debugs(50, DBG_IMPORTANT, HERE << "BUG 3556: FD " << fd << " is not an open socket.");
835  // XXX: do we need to run close(fd) or fd_close(fd) here?
836  return;
837  }
838 
839  assert(F->type != FD_FILE);
840 
842 
843  F->flags.close_request = true;
844 
845  // We have caller's context and fde::codeContext. In the unlikely event they
846  // differ, it is not clear which context is more applicable to this closure.
847  // For simplicity sake, we remain in the caller's context while still
848  // allowing individual advanced callbacks to overwrite it.
849 
850  if (F->ssl) {
851  AsyncCall::Pointer startCall=commCbCall(5,4, "commStartTlsClose",
852  FdeCbPtrFun(commStartTlsClose, nullptr));
853  FdeCbParams &startParams = GetCommParams<FdeCbParams>(startCall);
854  startParams.fd = fd;
855  ScheduleCallHere(startCall);
856  }
857 
858  // a half-closed fd may lack a reader, so we stop monitoring explicitly
859  if (commHasHalfClosedMonitor(fd))
861  commUnsetFdTimeout(fd);
862 
863  // notify read/write handlers after canceling select reservations, if any
864  if (COMMIO_FD_WRITECB(fd)->active()) {
866  COMMIO_FD_WRITECB(fd)->finish(Comm::ERR_CLOSING, errno);
867  }
868  if (COMMIO_FD_READCB(fd)->active()) {
870  COMMIO_FD_READCB(fd)->finish(Comm::ERR_CLOSING, errno);
871  }
872 
873 #if USE_DELAY_POOLS
875  if (bucket->selectWaiting)
876  bucket->onFdClosed();
877  }
878 #endif
879 
881 
883 
884  AsyncCall::Pointer completeCall=commCbCall(5,4, "comm_close_complete",
886  FdeCbParams &completeParams = GetCommParams<FdeCbParams>(completeCall);
887  completeParams.fd = fd;
888  // must use async call to wait for all callbacks
889  // scheduled before comm_close() to finish
890  ScheduleCallHere(completeCall);
891 
893 }
894 
895 /* Send a udp datagram to specified TO_ADDR. */
896 int
898  const Ip::Address &to_addr,
899  const void *buf,
900  int len)
901 {
903  ++ statCounter.syscalls.sock.sendtos;
904 
905  debugs(50, 3, "comm_udp_sendto: Attempt to send UDP packet to " << to_addr <<
906  " using FD " << fd << " using Port " << comm_local_port(fd) );
907 
908  struct addrinfo *AI = NULL;
909  to_addr.getAddrInfo(AI, fd_table[fd].sock_family);
910  int x = sendto(fd, buf, len, 0, AI->ai_addr, AI->ai_addrlen);
911  int xerrno = errno;
913 
915 
916  if (x >= 0) {
917  errno = xerrno; // restore for caller to use
918  return x;
919  }
920 
921 #if _SQUID_LINUX_
922  if (ECONNREFUSED != xerrno)
923 #endif
924  debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", (family=" << fd_table[fd].sock_family << ") " << to_addr << ": " << xstrerr(xerrno));
925 
926  errno = xerrno; // restore for caller to use
927  return Comm::COMM_ERROR;
928 }
929 
932 {
933  debugs(5, 5, "comm_add_close_handler: FD " << fd << ", handler=" <<
934  handler << ", data=" << data);
935 
936  AsyncCall::Pointer call=commCbCall(5,4, "SomeCloseHandler",
938  comm_add_close_handler(fd, call);
939  return call;
940 }
941 
942 void
944 {
945  debugs(5, 5, "comm_add_close_handler: FD " << fd << ", AsyncCall=" << call);
946 
947  /*TODO:Check for a similar scheduled AsyncCall*/
948 // for (c = fd_table[fd].closeHandler; c; c = c->next)
949 // assert(c->handler != handler || c->data != data);
950 
951  call->setNext(fd_table[fd].closeHandler);
952 
953  fd_table[fd].closeHandler = call;
954 }
955 
956 // remove function-based close handler
957 void
959 {
960  assert(isOpen(fd));
961  /* Find handler in list */
962  debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", handler=" <<
963  handler << ", data=" << data);
964 
965  AsyncCall::Pointer p, prev = NULL;
966  for (p = fd_table[fd].closeHandler; p != NULL; prev = p, p = p->Next()) {
968  const Call *call = dynamic_cast<const Call*>(p.getRaw());
969  if (!call) // method callbacks have their own comm_remove_close_handler
970  continue;
971 
972  typedef CommCloseCbParams Params;
973  const Params &params = GetCommParams<Params>(p);
974  if (call->dialer.handler == handler && params.data == data)
975  break; /* This is our handler */
976  }
977 
978  // comm_close removes all close handlers so our handler may be gone
979  if (p != NULL) {
980  p->dequeue(fd_table[fd].closeHandler, prev);
981  p->cancel("comm_remove_close_handler");
982  }
983 }
984 
985 // remove method-based close handler
986 void
988 {
989  assert(isOpen(fd));
990  debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", AsyncCall=" << call);
991 
992  // comm_close removes all close handlers so our handler may be gone
993  AsyncCall::Pointer p, prev = NULL;
994  for (p = fd_table[fd].closeHandler; p != NULL && p != call; prev = p, p = p->Next());
995 
996  if (p != NULL)
997  p->dequeue(fd_table[fd].closeHandler, prev);
998  call->cancel("comm_remove_close_handler");
999 }
1000 
1001 static void
1003 {
1004 
1005  struct linger L;
1006  L.l_onoff = 0; /* off */
1007  L.l_linger = 0;
1008 
1009  if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0) {
1010  int xerrno = errno;
1011  debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1012  }
1013  fd_table[fd].flags.nolinger = true;
1014 }
1015 
1016 static void
1018 {
1019  int on = 1;
1020  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0) {
1021  int xerrno = errno;
1022  debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1023  }
1024 }
1025 
1026 static void
1028 {
1029  if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size)) < 0) {
1030  int xerrno = errno;
1031  debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", SIZE " << size << ": " << xstrerr(xerrno));
1032  }
1033  if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size)) < 0) {
1034  int xerrno = errno;
1035  debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", SIZE " << size << ": " << xstrerr(xerrno));
1036  }
1037 #ifdef TCP_WINDOW_CLAMP
1038  if (setsockopt(fd, SOL_TCP, TCP_WINDOW_CLAMP, (char *) &size, sizeof(size)) < 0) {
1039  int xerrno = errno;
1040  debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", SIZE " << size << ": " << xstrerr(xerrno));
1041  }
1042 #endif
1043 }
1044 
1045 int
1047 {
1048 #if _SQUID_WINDOWS_
1049  int nonblocking = TRUE;
1050 
1051  if (ioctl(fd, FIONBIO, &nonblocking) < 0) {
1052  int xerrno = errno;
1053  debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": " << xstrerr(xerrno) << " " << fd_table[fd].type);
1054  return Comm::COMM_ERROR;
1055  }
1056 
1057 #else
1058  int flags;
1059  int dummy = 0;
1060 
1061  if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
1062  int xerrno = errno;
1063  debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": fcntl F_GETFL: " << xstrerr(xerrno));
1064  return Comm::COMM_ERROR;
1065  }
1066 
1067  if (fcntl(fd, F_SETFL, flags | SQUID_NONBLOCK) < 0) {
1068  int xerrno = errno;
1069  debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1070  return Comm::COMM_ERROR;
1071  }
1072 #endif
1073 
1074  fd_table[fd].flags.nonblocking = true;
1075  return 0;
1076 }
1077 
1078 int
1080 {
1081 #if _SQUID_WINDOWS_
1082  int nonblocking = FALSE;
1083 
1084  if (ioctlsocket(fd, FIONBIO, (unsigned long *) &nonblocking) < 0) {
1085 #else
1086  int flags;
1087  int dummy = 0;
1088 
1089  if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
1090  int xerrno = errno;
1091  debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": fcntl F_GETFL: " << xstrerr(xerrno));
1092  return Comm::COMM_ERROR;
1093  }
1094 
1095  if (fcntl(fd, F_SETFL, flags & (~SQUID_NONBLOCK)) < 0) {
1096 #endif
1097  int xerrno = errno;
1098  debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1099  return Comm::COMM_ERROR;
1100  }
1101 
1102  fd_table[fd].flags.nonblocking = false;
1103  return 0;
1104 }
1105 
1106 void
1108 {
1109 #ifdef FD_CLOEXEC
1110  int flags;
1111  int dummy = 0;
1112 
1113  if ((flags = fcntl(fd, F_GETFD, dummy)) < 0) {
1114  int xerrno = errno;
1115  debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": fcntl F_GETFD: " << xstrerr(xerrno));
1116  return;
1117  }
1118 
1119  if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
1120  int xerrno = errno;
1121  debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": set close-on-exec failed: " << xstrerr(xerrno));
1122  }
1123 
1124  fd_table[fd].flags.close_on_exec = true;
1125 
1126 #endif
1127 }
1128 
1129 #ifdef TCP_NODELAY
1130 static void
1131 commSetTcpNoDelay(int fd)
1132 {
1133  int on = 1;
1134 
1135  if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) < 0) {
1136  int xerrno = errno;
1137  debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1138  }
1139 
1140  fd_table[fd].flags.nodelay = true;
1141 }
1142 
1143 #endif
1144 
1145 void
1146 commSetTcpKeepalive(int fd, int idle, int interval, int timeout)
1147 {
1148  int on = 1;
1149 #ifdef TCP_KEEPCNT
1150  if (timeout && interval) {
1151  int count = (timeout + interval - 1) / interval;
1152  if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &count, sizeof(on)) < 0) {
1153  int xerrno = errno;
1154  debugs(5, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1155  }
1156  }
1157 #endif
1158 #ifdef TCP_KEEPIDLE
1159  if (idle) {
1160  if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(on)) < 0) {
1161  int xerrno = errno;
1162  debugs(5, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1163  }
1164  }
1165 #endif
1166 #ifdef TCP_KEEPINTVL
1167  if (interval) {
1168  if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(on)) < 0) {
1169  int xerrno = errno;
1170  debugs(5, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1171  }
1172  }
1173 #endif
1174  if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &on, sizeof(on)) < 0) {
1175  int xerrno = errno;
1176  debugs(5, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1177  }
1178 }
1179 
1180 void
1182 {
1183  assert(fd_table);
1184 
1185  /* make sure the accept() socket FIFO delay queue exists */
1187 
1188  // make sure the IO pending callback table exists
1190 
1191  /* XXX account fd_table */
1192  /* Keep a few file descriptors free so that we don't run out of FD's
1193  * after accepting a client but before it opens a socket or a file.
1194  * Since Squid_MaxFD can be as high as several thousand, don't waste them */
1195  RESERVED_FD = min(100, Squid_MaxFD / 4);
1196 
1198 
1199  /* setup the select loop module */
1201 }
1202 
1203 void
1205 {
1206  delete TheHalfClosed;
1207  TheHalfClosed = NULL;
1208 
1210 }
1211 
1212 #if USE_DELAY_POOLS
1213 // called when the queue is done waiting for the client bucket to fill
1214 void
1216 {
1217  CommQuotaQueue *queue = static_cast<CommQuotaQueue*>(data);
1218  assert(queue);
1219 
1220  ClientInfo *clientInfo = queue->clientInfo;
1221  // ClientInfo invalidates queue if freed, so if we got here through,
1222  // evenAdd cbdata protections, everything should be valid and consistent
1223  assert(clientInfo);
1224  assert(clientInfo->hasQueue());
1225  assert(clientInfo->hasQueue(queue));
1226  assert(clientInfo->eventWaiting);
1227  clientInfo->eventWaiting = false;
1228 
1229  do {
1230  clientInfo->writeOrDequeue();
1231  if (clientInfo->selectWaiting)
1232  return;
1233  } while (clientInfo->hasQueue());
1234 
1235  debugs(77, 3, "emptied queue");
1236 }
1237 
1238 void
1240 {
1242  const auto head = quotaPeekFd();
1243  const auto &headFde = fd_table[head];
1244  CallBack(headFde.codeContext, [&] {
1245  const auto ccb = COMMIO_FD_WRITECB(head);
1246  // check that the head descriptor is still relevant
1247  if (headFde.clientInfo == this &&
1248  quotaPeekReserv() == ccb->quotaQueueReserv &&
1249  !headFde.closing()) {
1250 
1251  // wait for the head descriptor to become ready for writing
1252  Comm::SetSelect(head, COMM_SELECT_WRITE, Comm::HandleWrite, ccb, 0);
1253  selectWaiting = true;
1254  } else {
1255  quotaDequeue(); // remove the no longer relevant descriptor
1256  }
1257  });
1258 }
1259 
1260 bool
1262 {
1263  assert(quotaQueue);
1264  return !quotaQueue->empty();
1265 }
1266 
1267 bool
1269 {
1270  assert(quotaQueue);
1271  return quotaQueue == q;
1272 }
1273 
1275 int
1277 {
1278  assert(quotaQueue);
1279  return quotaQueue->front();
1280 }
1281 
1283 unsigned int
1285 {
1286  assert(quotaQueue);
1287  return quotaQueue->outs + 1;
1288 }
1289 
1291 unsigned int
1293 {
1294  assert(quotaQueue);
1295  return quotaQueue->enqueue(fd);
1296 }
1297 
1299 void
1301 {
1302  assert(quotaQueue);
1303  quotaQueue->dequeue();
1304 }
1305 
1306 void
1308 {
1309  if (!eventWaiting && !selectWaiting && hasQueue()) {
1310  // wait at least a second if the bucket is empty
1311  const double delay = (bucketLevel < 1.0) ? 1.0 : 0.0;
1312  eventAdd("commHandleWriteHelper", &commHandleWriteHelper,
1313  quotaQueue, delay, 0, true);
1314  eventWaiting = true;
1315  }
1316 }
1317 
1319 int
1321 {
1322  /* If we have multiple clients and give full bucketSize to each client then
1323  * clt1 may often get a lot more because clt1->clt2 time distance in the
1324  * select(2) callback order may be a lot smaller than cltN->clt1 distance.
1325  * We divide quota evenly to be more fair. */
1326 
1327  if (!rationedCount) {
1328  rationedCount = quotaQueue->size() + 1;
1329 
1330  // The delay in ration recalculation _temporary_ deprives clients from
1331  // bytes that should have trickled in while rationedCount was positive.
1332  refillBucket();
1333 
1334  // Rounding errors do not accumulate here, but we round down to avoid
1335  // negative bucket sizes after write with rationedCount=1.
1336  rationedQuota = static_cast<int>(floor(bucketLevel/rationedCount));
1337  debugs(77,5, HERE << "new rationedQuota: " << rationedQuota <<
1338  '*' << rationedCount);
1339  }
1340 
1341  --rationedCount;
1342  debugs(77,7, HERE << "rationedQuota: " << rationedQuota <<
1343  " rations remaining: " << rationedCount);
1344 
1345  // update 'last seen' time to prevent clientdb GC from dropping us
1347  return rationedQuota;
1348 }
1349 
1350 bool
1352 {
1353  assert(hasQueue());
1354  assert(quotaPeekFd() == state->conn->fd);
1355  quotaDequeue(); // we will write or requeue below
1356  if (nleft > 0 && !BandwidthBucket::applyQuota(nleft, state)) {
1357  state->quotaQueueReserv = quotaEnqueue(state->conn->fd);
1358  kickQuotaQueue();
1359  return false;
1360  }
1361  return true;
1362 }
1363 
1364 void
1366 {
1367  if (writeLimitingActive) {
1368  state->quotaQueueReserv = quotaEnqueue(state->conn->fd);
1369  kickQuotaQueue();
1370  }
1371 }
1372 
1373 void
1375 {
1377  // kick queue or it will get stuck as commWriteHandle is not called
1378  kickQuotaQueue();
1379 }
1380 
1381 void
1383 {
1384  if (len > 0)
1386  // even if we wrote nothing, we were served; give others a chance
1387  kickQuotaQueue();
1388 }
1389 
1390 void
1391 ClientInfo::setWriteLimiter(const int aWriteSpeedLimit, const double anInitialBurst, const double aHighWatermark)
1392 {
1393  debugs(77,5, "Write limits for " << (const char*)key <<
1394  " speed=" << aWriteSpeedLimit << " burst=" << anInitialBurst <<
1395  " highwatermark=" << aHighWatermark);
1396 
1397  // set or possibly update traffic shaping parameters
1398  writeLimitingActive = true;
1399  writeSpeedLimit = aWriteSpeedLimit;
1400  bucketSizeLimit = aHighWatermark;
1401 
1402  // but some members should only be set once for a newly activated bucket
1403  if (firstTimeConnection) {
1404  firstTimeConnection = false;
1405 
1407  assert(!quotaQueue);
1408  quotaQueue = new CommQuotaQueue(this);
1409 
1410  bucketLevel = anInitialBurst;
1412  }
1413 }
1414 
1416  ins(0), outs(0)
1417 {
1418  assert(clientInfo);
1419 }
1420 
1422 {
1423  assert(!clientInfo); // ClientInfo should clear this before destroying us
1424 }
1425 
1427 unsigned int
1429 {
1430  debugs(77,5, "clt" << (const char*)clientInfo->key <<
1431  ": FD " << fd << " with qqid" << (ins+1) << ' ' << fds.size());
1432  fds.push_back(fd);
1433  fd_table[fd].codeContext = CodeContext::Current();
1434  return ++ins;
1435 }
1436 
1438 void
1440 {
1441  assert(!fds.empty());
1442  debugs(77,5, "clt" << (const char*)clientInfo->key <<
1443  ": FD " << fds.front() << " with qqid" << (outs+1) << ' ' <<
1444  fds.size());
1445  fds.pop_front();
1446  ++outs;
1447 }
1448 #endif /* USE_DELAY_POOLS */
1449 
1450 /*
1451  * hm, this might be too general-purpose for all the places we'd
1452  * like to use it.
1453  */
1454 int
1455 ignoreErrno(int ierrno)
1456 {
1457  switch (ierrno) {
1458 
1459  case EINPROGRESS:
1460 
1461  case EWOULDBLOCK:
1462 #if EAGAIN != EWOULDBLOCK
1463 
1464  case EAGAIN:
1465 #endif
1466 
1467  case EALREADY:
1468 
1469  case EINTR:
1470 #ifdef ERESTART
1471 
1472  case ERESTART:
1473 #endif
1474 
1475  return 1;
1476 
1477  default:
1478  return 0;
1479  }
1480 
1481  /* NOTREACHED */
1482 }
1483 
1484 void
1486 {
1487  int fd;
1488  fde *F = NULL;
1489 
1490  for (fd = 0; fd <= Biggest_FD; ++fd) {
1491  F = &fd_table[fd];
1492 
1493  if (!F->flags.open)
1494  continue;
1495 
1496  if (F->type != FD_SOCKET)
1497  continue;
1498 
1499  if (F->flags.ipc) /* don't close inter-process sockets */
1500  continue;
1501 
1502  if (F->timeoutHandler != NULL) {
1503  AsyncCall::Pointer callback = F->timeoutHandler;
1504  F->timeoutHandler = NULL;
1505  debugs(5, 5, "commCloseAllSockets: FD " << fd << ": Calling timeout handler");
1507  } else {
1508  debugs(5, 5, "commCloseAllSockets: FD " << fd << ": calling comm_reset_close()");
1510  }
1511  }
1512 }
1513 
1514 static bool
1516 {
1517  if (!F->flags.open)
1518  return true;
1519 
1520  if (F->timeout == 0)
1521  return true;
1522 
1523  if (F->timeout > squid_curtime)
1524  return true;
1525 
1526  return false;
1527 }
1528 
1529 static bool
1531 {
1532  if (!COMMIO_FD_WRITECB(fd)->active())
1533  return false;
1534 
1535  if ((squid_curtime - fd_table[fd].writeStart) < Config.Timeout.write)
1536  return false;
1537 
1538  return true;
1539 }
1540 
1541 void
1543 {
1544  int fd;
1545  fde *F = NULL;
1547 
1548  for (fd = 0; fd <= Biggest_FD; ++fd) {
1549  F = &fd_table[fd];
1550 
1551  if (writeTimedOut(fd)) {
1552  // We have an active write callback and we are timed out
1553  CodeContext::Reset(F->codeContext);
1554  debugs(5, 5, "checkTimeouts: FD " << fd << " auto write timeout");
1556  COMMIO_FD_WRITECB(fd)->finish(Comm::COMM_ERROR, ETIMEDOUT);
1558  continue;
1559 #if USE_DELAY_POOLS
1560  } else if (F->writeQuotaHandler != nullptr && COMMIO_FD_WRITECB(fd)->conn != nullptr) {
1561  // TODO: Move and extract quota() call to place it inside F->codeContext.
1562  if (!F->writeQuotaHandler->selectWaiting && F->writeQuotaHandler->quota() && !F->closing()) {
1563  CodeContext::Reset(F->codeContext);
1564  F->writeQuotaHandler->selectWaiting = true;
1567  }
1568  continue;
1569 #endif
1570  }
1571  else if (AlreadyTimedOut(F))
1572  continue;
1573 
1574  CodeContext::Reset(F->codeContext);
1575  debugs(5, 5, "checkTimeouts: FD " << fd << " Expired");
1576 
1577  if (F->timeoutHandler != NULL) {
1578  debugs(5, 5, "checkTimeouts: FD " << fd << ": Call timeout handler");
1579  callback = F->timeoutHandler;
1580  F->timeoutHandler = NULL;
1582  } else {
1583  debugs(5, 5, "checkTimeouts: FD " << fd << ": Forcing comm_close()");
1584  comm_close(fd);
1585  }
1586 
1588  }
1589 }
1590 
1592 // by scheduling a read callback to a monitoring handler that
1593 // will close the connection on read errors.
1594 void
1596 {
1597  debugs(5, 5, HERE << "adding FD " << fd << " to " << *TheHalfClosed);
1598  assert(isOpen(fd) && !commHasHalfClosedMonitor(fd));
1599  (void)TheHalfClosed->add(fd); // could also assert the result
1600  fd_table[fd].codeContext = CodeContext::Current();
1601  commPlanHalfClosedCheck(); // may schedule check if we added the first FD
1602 }
1603 
1604 static
1605 void
1607 {
1608  if (!WillCheckHalfClosed && !TheHalfClosed->empty()) {
1609  eventAdd("commHalfClosedCheck", &commHalfClosedCheck, NULL, 1.0, 1);
1610  WillCheckHalfClosed = true;
1611  }
1612 }
1613 
1616 static
1617 void
1619 {
1620  debugs(5, 5, HERE << "checking " << *TheHalfClosed);
1621 
1622  typedef DescriptorSet::const_iterator DSCI;
1623  const DSCI end = TheHalfClosed->end();
1624  for (DSCI i = TheHalfClosed->begin(); i != end; ++i) {
1625  Comm::ConnectionPointer c = new Comm::Connection; // XXX: temporary. make HalfClosed a list of these.
1626  c->fd = *i;
1627  if (!fd_table[c->fd].halfClosedReader) { // not reading already
1628  CallBack(fd_table[c->fd].codeContext, [&c] {
1629  AsyncCall::Pointer call = commCbCall(5,4, "commHalfClosedReader",
1630  CommIoCbPtrFun(&commHalfClosedReader, nullptr));
1631  Comm::Read(c, call);
1632  fd_table[c->fd].halfClosedReader = call;
1633  });
1634  } else
1635  c->fd = -1; // XXX: temporary. prevent c replacement erase closing listed FD
1636  }
1637 
1638  WillCheckHalfClosed = false; // as far as we know
1639  commPlanHalfClosedCheck(); // may need to check again
1640 }
1641 
1643 // We are monitoring if the read handler for the fd is the monitoring handler.
1644 bool
1646 {
1647  return TheHalfClosed->has(fd);
1648 }
1649 
1651 void
1653 {
1654  debugs(5, 5, HERE << "removing FD " << fd << " from " << *TheHalfClosed);
1655 
1656  // cancel the read if one was scheduled
1657  AsyncCall::Pointer reader = fd_table[fd].halfClosedReader;
1658  if (reader != NULL)
1659  Comm::ReadCancel(fd, reader);
1660  fd_table[fd].halfClosedReader = NULL;
1661 
1662  TheHalfClosed->del(fd);
1663 }
1664 
1666 static void
1667 commHalfClosedReader(const Comm::ConnectionPointer &conn, char *, size_t size, Comm::Flag flag, int, void *)
1668 {
1669  // there cannot be more data coming in on half-closed connections
1670  assert(size == 0);
1671  assert(conn != NULL);
1672  assert(commHasHalfClosedMonitor(conn->fd)); // or we would have canceled the read
1673 
1674  fd_table[conn->fd].halfClosedReader = NULL; // done reading, for now
1675 
1676  // nothing to do if fd is being closed
1677  if (flag == Comm::ERR_CLOSING)
1678  return;
1679 
1680  // if read failed, close the connection
1681  if (flag != Comm::OK) {
1682  debugs(5, 3, HERE << "closing " << conn);
1683  conn->close();
1684  return;
1685  }
1686 
1687  // continue waiting for close or error
1688  commPlanHalfClosedCheck(); // make sure this fd will be checked again
1689 }
1690 
1692 
1693 CommRead::CommRead(const Comm::ConnectionPointer &c, char *buf_, int len_, AsyncCall::Pointer &callback_)
1694  : conn(c), buf(buf_), len(len_), callback(callback_) {}
1695 
1696 DeferredRead::DeferredRead () : theReader(NULL), theContext(NULL), theRead(), cancelled(false) {}
1697 
1698 DeferredRead::DeferredRead (DeferrableRead *aReader, void *data, CommRead const &aRead) : theReader(aReader), theContext (data), theRead(aRead), cancelled(false) {}
1699 
1701 {
1702  flushReads();
1704 }
1705 
1706 /* explicit instantiation required for some systems */
1707 
1711 
1712 void
1714 {
1715  debugs(5, 3, "Adding deferred read on " << aRead.theRead.conn);
1717 
1718  // We have to use a global function as a closer and point to temp
1719  // instead of "this" because DeferredReadManager is not a job and
1720  // is not even cbdata protected
1721  // XXX: and yet we use cbdata protection functions on it??
1722  AsyncCall::Pointer closer = commCbCall(5,4,
1723  "DeferredReadManager::CloseHandler",
1725  comm_add_close_handler(aRead.theRead.conn->fd, closer);
1726  temp->element.closer = closer; // remember so that we can cancel
1727 }
1728 
1729 void
1731 {
1732  if (!cbdataReferenceValid(params.data))
1733  return;
1734 
1736 
1737  temp->element.closer = NULL;
1738  temp->element.markCancelled();
1739 }
1740 
1743 {
1744  assert (!deferredReads.empty());
1745 
1747 
1748  // NOTE: at this point the connection has been paused/stalled for an unknown
1749  // amount of time. We must re-validate that it is active and usable.
1750 
1751  // If the connection has been closed already. Cancel this read.
1752  if (!fd_table || !Comm::IsConnOpen(read.theRead.conn)) {
1753  if (read.closer != NULL) {
1754  read.closer->cancel("Connection closed before.");
1755  read.closer = NULL;
1756  }
1757  read.markCancelled();
1758  }
1759 
1760  if (!read.cancelled) {
1762  read.closer = NULL;
1763  }
1764 
1766 
1767  return result;
1768 }
1769 
1770 void
1772 {
1773  /* if we had CbDataList::size() we could consolidate this and flushReads */
1774 
1775  if (count < 1) {
1776  flushReads();
1777  return;
1778  }
1779 
1780  size_t remaining = count;
1781 
1782  while (!deferredReads.empty() && remaining) {
1784  kickARead(aRead);
1785 
1786  if (!aRead.cancelled)
1787  --remaining;
1788  }
1789 }
1790 
1791 void
1793 {
1795  reads = deferredReads;
1797 
1798  // XXX: For fairness this SHOULD randomize the order
1799  while (!reads.empty()) {
1800  DeferredRead aRead = popHead(reads);
1801  kickARead(aRead);
1802  }
1803 }
1804 
1805 void
1807 {
1808  if (aRead.cancelled)
1809  return;
1810 
1811  if (Comm::IsConnOpen(aRead.theRead.conn) && fd_table[aRead.theRead.conn->fd].closing())
1812  return;
1813 
1814  debugs(5, 3, "Kicking deferred read on " << aRead.theRead.conn);
1815 
1816  aRead.theReader(aRead.theContext, aRead.theRead);
1817 }
1818 
1819 void
1821 {
1822  cancelled = true;
1823 }
1824 
1825 int
1827 {
1828  static time_t last_timeout = 0;
1829 
1830  /* No, this shouldn't be here. But it shouldn't be in each comm handler. -adrian */
1831  if (squid_curtime > last_timeout) {
1832  last_timeout = squid_curtime;
1833  checkTimeouts();
1834  }
1835 
1836  switch (Comm::DoSelect(timeout)) {
1837 
1838  case Comm::OK:
1839 
1840  case Comm::TIMEOUT:
1841  return 0;
1842 
1843  case Comm::IDLE:
1844 
1845  case Comm::SHUTDOWN:
1846  return EVENT_IDLE;
1847 
1848  case Comm::COMM_ERROR:
1849  return EVENT_ERROR;
1850 
1851  default:
1852  fatal_dump("comm.cc: Internal error -- this should never happen.");
1853  return EVENT_ERROR;
1854  };
1855 }
1856 
1858 int
1859 comm_open_uds(int sock_type,
1860  int proto,
1861  struct sockaddr_un* addr,
1862  int flags)
1863 {
1864  // TODO: merge with comm_openex() when Ip::Address becomes NetAddress
1865 
1866  int new_socket;
1867 
1869  /* Create socket for accepting new connections. */
1870  ++ statCounter.syscalls.sock.sockets;
1871 
1872  /* Setup the socket addrinfo details for use */
1873  struct addrinfo AI;
1874  AI.ai_flags = 0;
1875  AI.ai_family = PF_UNIX;
1876  AI.ai_socktype = sock_type;
1877  AI.ai_protocol = proto;
1878  AI.ai_addrlen = SUN_LEN(addr);
1879  AI.ai_addr = (sockaddr*)addr;
1880  AI.ai_canonname = NULL;
1881  AI.ai_next = NULL;
1882 
1883  debugs(50, 3, HERE << "Attempt open socket for: " << addr->sun_path);
1884 
1885  if ((new_socket = socket(AI.ai_family, AI.ai_socktype, AI.ai_protocol)) < 0) {
1886  int xerrno = errno;
1887  /* Increase the number of reserved fd's if calls to socket()
1888  * are failing because the open file table is full. This
1889  * limits the number of simultaneous clients */
1890 
1891  if (limitError(xerrno)) {
1892  debugs(50, DBG_IMPORTANT, MYNAME << "socket failure: " << xstrerr(xerrno));
1893  fdAdjustReserved();
1894  } else {
1895  debugs(50, DBG_CRITICAL, MYNAME << "socket failure: " << xstrerr(xerrno));
1896  }
1897 
1899  return -1;
1900  }
1901 
1902  debugs(50, 3, "Opened UDS FD " << new_socket << " : family=" << AI.ai_family << ", type=" << AI.ai_socktype << ", protocol=" << AI.ai_protocol);
1903 
1904  /* update fdstat */
1905  debugs(50, 5, HERE << "FD " << new_socket << " is a new socket");
1906 
1907  assert(!isOpen(new_socket));
1908  fd_open(new_socket, FD_MSGHDR, addr->sun_path);
1909 
1910  fd_table[new_socket].sock_family = AI.ai_family;
1911 
1912  if (!(flags & COMM_NOCLOEXEC))
1913  commSetCloseOnExec(new_socket);
1914 
1915  if (flags & COMM_REUSEADDR)
1916  commSetReuseAddr(new_socket);
1917 
1918  if (flags & COMM_NONBLOCKING) {
1919  if (commSetNonBlocking(new_socket) != Comm::OK) {
1920  comm_close(new_socket);
1922  return -1;
1923  }
1924  }
1925 
1926  if (flags & COMM_DOBIND) {
1927  if (commBind(new_socket, AI) != Comm::OK) {
1928  comm_close(new_socket);
1930  return -1;
1931  }
1932  }
1933 
1934 #ifdef TCP_NODELAY
1935  if (sock_type == SOCK_STREAM)
1936  commSetTcpNoDelay(new_socket);
1937 
1938 #endif
1939 
1940  if (Config.tcpRcvBufsz > 0 && sock_type == SOCK_STREAM)
1941  commSetTcpRcvbuf(new_socket, Config.tcpRcvBufsz);
1942 
1944 
1945  return new_socket;
1946 }
1947 
static void commSetTcpRcvbuf(int, int)
Definition: comm.cc:1027
bool cancel(const char *reason)
Definition: AsyncCall.cc:56
void comm_exit(void)
Definition: comm.cc:1204
const char * xstrerr(int error)
Definition: xstrerror.cc:83
#define SUN_LEN(ptr)
Definition: cmsg.h:113
virtual bool applyQuota(int &nleft, Comm::IoCallback *state) override
Definition: comm.cc:1351
CommQuotaQueue(ClientInfo *info)
Definition: comm.cc:1415
AsyncCall::Pointer comm_add_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:931
#define MYNAME
Definition: Debug.h:170
void quotaDequeue()
pops queue head from queue
Definition: comm.cc:1300
void _comm_close(int fd, char const *file, int line)
Definition: comm.cc:817
unsigned int enqueue(int fd)
places the given fd at the end of the queue; returns reservation ID
Definition: comm.cc:1428
void flushReads()
Definition: comm.cc:1792
void delayRead(DeferredRead const &)
Definition: comm.cc:1713
#define FALSE
Definition: std-includes.h:56
void * theContext
Definition: CommRead.h:40
void comm_set_transparent(int fd)
Definition: comm.cc:289
static Comm::Flag commBind(int s, struct addrinfo &)
Definition: comm.cc:204
DeferrableRead * theReader
Definition: CommRead.h:39
int front() const
Definition: ClientInfo.h:119
virtual void reduceBucket(int len) override
Decreases the bucket level.
Definition: comm.cc:1382
bool isAnyAddr() const
Definition: Address.cc:170
int comm_connect_addr(int sock, const Ip::Address &address)
Definition: comm.cc:606
int outs
number of dequeue calls, used to check the "reservation" ID
Definition: ClientInfo.h:127
#define COMMIO_FD_READCB(fd)
Definition: IoCallback.h:78
#define ScheduleCallHere(call)
Definition: AsyncCall.h:166
int comm_udp_sendto(int fd, const Ip::Address &to_addr, const void *buf, int len)
Definition: comm.cc:897
void commCallCloseHandlers(int fd)
Definition: comm.cc:734
void setWriteLimiter(const int aWriteSpeedLimit, const double anInitialBurst, const double aHighWatermark)
Definition: comm.cc:1391
Comm::Flag DoSelect(int)
Do poll and trigger callback functions as appropriate.
Definition: ModDevPoll.cc:313
@ INPROGRESS
Definition: Flag.h:22
@ FD_SOCKET
Definition: enums.h:16
int commSetConnTimeout(const Comm::ConnectionPointer &conn, int timeout, AsyncCall::Pointer &callback)
Definition: comm.cc:567
static uint32 F(uint32 X, uint32 Y, uint32 Z)
Definition: md4.c:46
ssize_t comm_udp_send(int s, const void *buf, size_t len, int flags)
Definition: comm.cc:142
int comm_udp_recv(int fd, void *buf, size_t len, int flags)
Definition: comm.cc:135
void commStopHalfClosedMonitor(int const fd)
stop waiting for possibly half-closed connection to close
Definition: comm.cc:1652
@ TIMEOUT
Definition: Flag.h:19
int commSetNonBlocking(int fd)
Definition: comm.cc:1046
static void FreeAddr(struct addrinfo *&ai)
Definition: Address.cc:686
int fd
FD which the call was about. Set by the async call creator.
Definition: CommCalls.h:90
#define comm_close(x)
Definition: comm.h:28
static void commHandleWriteHelper(void *data)
Definition: comm.cc:1215
DeferredRead()
Definition: comm.cc:1696
int type
Definition: errorpage.cc:153
void comm_import_opened(const Comm::ConnectionPointer &conn, const char *note, struct addrinfo *AI)
update Comm state after getting a comm_open() FD from another process
Definition: comm.cc:514
int cbdataReferenceValid(const void *p)
Definition: cbdata.cc:394
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:26
@ OK
Definition: Flag.h:16
CbDataList< C > * push_back(C const &)
Definition: CbDataList.h:160
#define DBG_CRITICAL
Definition: Debug.h:45
bool isIPv4() const
Definition: Address.cc:158
void CallBack(const CodeContext::Pointer &callbackContext, Fun &&callback)
Definition: CodeContext.h:81
void CLCB(const CommCloseCbParams &params)
Definition: CommCalls.h:42
@ ERR_CLOSING
Definition: Flag.h:25
#define COMMIO_FD_WRITECB(fd)
Definition: IoCallback.h:79
bool commHasHalfClosedMonitor(int fd)
checks whether we are waiting for possibly half-closed connection to close
Definition: comm.cc:1645
static IOCB commHalfClosedReader
Definition: comm.cc:61
#define PROF_stop(probename)
Definition: Profiler.h:63
bool comm_has_incomplete_write(int fd)
Definition: comm.cc:148
#define DBG_IMPORTANT
Definition: Debug.h:46
An unordered collection of unique descriptors with O(1) add/del/has ops.
Definition: DescriptorSet.h:19
Definition: fde.h:52
int socklen_t
Definition: types.h:158
void commStartTlsClose(const FdeCbParams &params)
Definition: comm.cc:785
socklen_t ai_addrlen
static bool writeTimedOut(int fd)
Definition: comm.cc:1530
#define COMM_NONBLOCKING
Definition: Connection.h:46
char * toStr(char *buf, const unsigned int blen, int force=AF_UNSPEC) const
Definition: Address.cc:802
double writeSpeedLimit
Write speed limit in bytes per second.
#define TexcHere(msg)
legacy convenience macro; it is not difficult to type Here() now
Definition: TextException.h:58
void comm_set_v6only(int fd, int tos)
Definition: comm.cc:270
void kickARead(DeferredRead const &)
Definition: comm.cc:1806
Comm::ConnectionPointer conn
Definition: IoCallback.h:33
#define MAX_IPSTRLEN
Length of buffer that needs to be allocated to old a null-terminated IP-string.
Definition: forward.h:23
static int comm_apply_flags(int new_socket, Ip::Address &addr, int flags, struct addrinfo *AI)
Definition: comm.cc:439
static void Reset()
forgets the current context, setting it to nil/unknown
Definition: CodeContext.cc:75
void SessionSendGoodbye(const Security::SessionPointer &)
send the shutdown/bye notice for an active TLS session.
Definition: Session.cc:193
bool add(int fd)
adds if unique; returns true if added
char sun_path[256]
Definition: cmsg.h:108
virtual void onFdClosed() override
Performs cleanup when the related file descriptor becomes closed.
Definition: comm.cc:1374
void commCloseAllSockets(void)
Definition: comm.cc:1485
void ReadCancel(int fd, AsyncCall::Pointer &callback)
Cancel the read pending on FD. No action if none pending.
Definition: Read.cc:219
struct sockaddr * ai_addr
void comm_open_listener(int sock_type, int proto, Comm::ConnectionPointer &conn, const char *note)
Definition: comm.cc:233
int commUnsetNonBlocking(int fd)
Definition: comm.cc:1079
bool has(const int fd) const
checks whether fd is in the set
Definition: DescriptorSet.h:28
static EVH commHalfClosedCheck
true if check is scheduled
Definition: comm.cc:75
#define COMM_TRANSPARENT
Definition: Connection.h:50
static DescriptorSet * TheHalfClosed
Definition: comm.cc:73
bool cancelled
Definition: CommRead.h:42
void leave_suid(void)
Definition: tools.cc:504
void comm_init(void)
Definition: comm.cc:1181
bool eventWaiting
waiting for commHandleWriteHelper event to fire
Definition: ClientInfo.h:71
CommQuotaQueue * quotaQueue
clients waiting for more write quota
Definition: ClientInfo.h:68
int size
Definition: ModDevPoll.cc:77
void commUnsetFdTimeout(int fd)
clear a timeout handler by FD number
Definition: comm.cc:554
CommRead()
Definition: comm.cc:1691
size_t tcpRcvBufsz
Definition: SquidConfig.h:248
#define NULL
Definition: types.h:166
static bool WillCheckHalfClosed
the set of half-closed FDs
Definition: comm.cc:74
struct StatCounters::@138 syscalls
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:128
Details about a particular Comm IO callback event.
Definition: IoCallback.h:30
virtual int quota() override
allocate quota for a just dequeued client
Definition: comm.cc:1320
const int * const_iterator
Definition: DescriptorSet.h:22
virtual void reduceBucket(const int len)
Decreases the bucket level.
#define COMM_REUSEADDR
Definition: Connection.h:48
#define IPV6_SPECIAL_V4MAPPING
Definition: tools.h:21
static CLCB CloseHandler
Definition: CommRead.h:57
Store fds
descriptor queue
Definition: ClientInfo.h:132
int comm_openex(int sock_type, int proto, Ip::Address &addr, int flags, const char *note)
Definition: comm.cc:331
static DeferredRead popHead(CbDataListContainer< DeferredRead > &deferredReads)
Definition: comm.cc:1742
virtual bool applyQuota(int &nleft, Comm::IoCallback *state)
int quotaPeekFd() const
returns the next fd reservation
Definition: comm.cc:1276
void checkTimeouts(void)
Definition: comm.cc:1542
static void comm_empty_os_read_buffers(int fd)
Definition: comm.cc:101
unsigned short port() const
Definition: Address.cc:778
bool isIPv6() const
Definition: Address.cc:164
CommRead theRead
Definition: CommRead.h:41
bool writeLimitingActive
Is write limiter active.
Definition: ClientInfo.h:65
int cbdata_type
Definition: cbdata.h:195
const_iterator begin() const
begin iterator a la STL; may become invalid if the object is modified
Definition: DescriptorSet.h:40
virtual void scheduleWrite(Comm::IoCallback *state) override
Will plan another write call.
Definition: comm.cc:1365
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:157
unsigned short comm_local_port(int fd)
Definition: comm.cc:161
void const char HLPCB void * data
Definition: stub_helper.cc:16
void IOCB(const Comm::ConnectionPointer &conn, char *, size_t size, Comm::Flag flag, int xerrno, void *data)
Definition: CommCalls.h:36
int reconfiguring
void kickReads(int const count)
Definition: comm.cc:1771
time_t last_seen
Definition: ClientInfo.h:63
int rationedQuota
precomputed quota preserving fairness among clients
Definition: ClientInfo.h:69
Base class for Squid-to-client bandwidth limiting.
CommCbFunPtrCallT< Dialer > * commCbCall(int debugSection, int debugLevel, const char *callName, const Dialer &dialer)
Definition: CommCalls.h:342
double bucketSizeLimit
maximum bucket size
void comm_reset_close(const Comm::ConnectionPointer &conn)
Definition: comm.cc:756
int conn
the current server connection FD
Definition: Transport.cc:26
#define assert(EX)
Definition: assert.h:19
#define COMM_REUSEPORT
Definition: Connection.h:52
SSL Connection
Definition: Session.h:45
virtual void onFdClosed()
Performs cleanup when the related file descriptor becomes closed.
static bool AlreadyTimedOut(fde *F)
Definition: comm.cc:1515
#define IPV6_SPECIAL_SPLITSTACK
Definition: tools.h:22
#define SQUID_NONBLOCK
bool setIPv4()
Definition: Address.cc:224
void commSetCloseOnExec(int fd)
Definition: comm.cc:1107
@ IDLE
Definition: Flag.h:21
size_t size() const
Definition: ClientInfo.h:118
unsigned int quotaEnqueue(int fd)
client starts waiting in queue; create the queue if necessary
Definition: comm.cc:1292
static BandwidthBucket * SelectBucket(fde *f)
int ins
number of enqueue calls, used to generate a "reservation" ID
Definition: ClientInfo.h:126
struct SquidConfig::@99 Timeout
@ COMM_ERROR
Definition: Flag.h:17
void dequeue()
removes queue head
Definition: comm.cc:1439
#define COMM_SELECT_READ
Definition: defines.h:36
int opt_reuseaddr
int comm_open(int sock_type, int proto, Ip::Address &addr, int flags, const char *note)
Definition: comm.cc:223
int commUnsetConnTimeout(const Comm::ConnectionPointer &conn)
Definition: comm.cc:593
bool empty() const
Definition: ClientInfo.h:117
#define CBDATA_CLASS_INIT(type)
Definition: cbdata.h:318
void fatal_dump(const char *message)
Definition: fatal.cc:78
void writeOrDequeue()
either selects the head descriptor for writing or calls quotaDequeue()
Definition: comm.cc:1239
time_t squid_curtime
Definition: stub_time.cc:17
void fdAdjustReserved(void)
Definition: fd.cc:316
int Squid_MaxFD
bool isNoAddr() const
Definition: Address.cc:284
static AcceptLimiter & Instance()
void SelectLoopInit(void)
Initialize the module on Squid startup.
Definition: ModDevPoll.cc:178
double prevTime
previous time when we checked
virtual int checkEvents(int timeout)
Definition: comm.cc:1826
void EVH(void *)
Definition: event.h:19
ClientInfo * clientInfo
bucket responsible for quota maintenance
Definition: ClientInfo.h:123
Flag
Definition: Flag.h:15
int FD_READ_METHOD(int fd, char *buf, int len)
Definition: fde.h:194
bool hasQueue() const
whether any clients are waiting for write quota
Definition: comm.cc:1261
int ignoreErrno(int ierrno)
Definition: comm.cc:1455
#define TRUE
Definition: std-includes.h:55
FD event (FDECB) dialer.
Definition: CommCalls.h:293
#define fd_table
Definition: fde.h:189
unsigned int quotaQueueReserv
reservation ID from CommQuotaQueue
Definition: IoCallback.h:42
static bool limitError(int const anErrno)
Definition: comm.cc:264
#define COMM_DOBIND
Definition: Connection.h:49
@ ERR_PROTOCOL
Definition: Flag.h:26
void commSetTcpKeepalive(int fd, int idle, int interval, int timeout)
Definition: comm.cc:1146
void getAddrInfo(struct addrinfo *&ai, int force=AF_UNSPEC) const
Definition: Address.cc:599
Comm::AcceptLimiter dummy
Definition: stub_libcomm.cc:16
bool empty() const
Definition: CbDataList.h:193
void refillBucket()
Increases the bucket level with the writeSpeedLimit speed.
~CommQuotaQueue()
Definition: comm.cc:1421
static const Pointer & Current()
Definition: CodeContext.cc:33
squidaio_request_t * head
Definition: aiops.cc:127
bool del(int fd)
deletes if there; returns true if deleted
void SetSelect(int, unsigned int, PF *, void *, time_t)
Mark an FD to be watched for its IO status.
Definition: ModDevPoll.cc:225
CbDataList< C > * head
Definition: CbDataList.h:47
AsyncCall::Pointer closer
internal close handler used by Comm
Definition: CommRead.h:43
time_t write
Definition: SquidConfig.h:110
void kickQuotaQueue()
Definition: comm.cc:1307
struct addrinfo * ai_next
double bucketLevel
how much can be written now
bool empty() const
number of descriptors in the set
Definition: DescriptorSet.h:37
unsigned int quotaPeekReserv() const
returns the next reserv. to pop
Definition: comm.cc:1284
SBuf ToSBuf(Args &&... args)
slowly stream-prints all arguments into a freshly allocated SBuf
Definition: Stream.h:124
int RESERVED_FD
void fd_open(int fd, unsigned int type, const char *desc)
Definition: fd.cc:186
void markCancelled()
Definition: comm.cc:1820
void enter_suid(void)
Definition: tools.cc:575
void IPH * handler
Definition: stub_ipcache.cc:16
void const char HLPCB * callback
Definition: stub_helper.cc:16
void Controller::create() STUB void Controller Controller nil
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
struct StatCounters::@138::@143 sock
Comm::ConnectionPointer conn
Definition: CommRead.h:25
const_iterator end() const
end iterator a la STL; may become invalid if the object is modified
Definition: DescriptorSet.h:42
void CallbackTableDestruct()
Definition: IoCallback.cc:34
PF HandleWrite
Definition: forward.h:32
@ FD_MSGHDR
Definition: enums.h:18
bool selectWaiting
is between commSetSelect and commHandleWrite
int shutting_down
static void commSetReuseAddr(int)
Definition: comm.cc:1017
int comm_udp_recvfrom(int fd, void *buf, size_t len, int flags, Ip::Address &from)
Definition: comm.cc:122
static void commPlanHalfClosedCheck()
Definition: comm.cc:1606
int rationedCount
number of clients that will receive rationedQuota
Definition: ClientInfo.h:70
void fd_close(int fd)
Definition: fd.cc:82
static void commSetNoLinger(int)
Definition: comm.cc:1002
void commStartHalfClosedMonitor(int fd)
Start waiting for a possibly half-closed connection to close.
Definition: comm.cc:1595
int comm_open_uds(int sock_type, int proto, struct sockaddr_un *addr, int flags)
Create a unix-domain socket (UDS) that only supports FD_MSGHDR I/O.
Definition: comm.cc:1859
int EnableIpv6
Whether IPv6 is supported and type of support.
Definition: tools.h:25
#define false
Definition: GnuRegex.c:233
bool isOpen(const int fd)
Definition: comm.cc:87
void CallbackTableInit()
Definition: IoCallback.cc:22
A const & min(A const &lhs, A const &rhs)
bool firstTimeConnection
is this first time connection for this client
Definition: ClientInfo.h:66
#define COMM_SELECT_WRITE
Definition: defines.h:37
void old_comm_reset_close(int fd)
Definition: comm.cc:771
@ FD_FILE
Definition: enums.h:15
void comm_remove_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:958
#define COMM_NOCLOEXEC
Definition: Connection.h:47
void const char * buf
Definition: stub_helper.cc:16
double current_dtime
Definition: stub_time.cc:16
void eventAdd(const char *name, EVH *func, void *arg, double when, int weight, bool cbdata)
Definition: event.cc:109
int Biggest_FD
void comm_close_complete(const FdeCbParams &params)
Definition: comm.cc:791
#define PROF_start(probename)
Definition: Profiler.h:62
static void InitAddr(struct addrinfo *&ai)
Definition: Address.cc:668
class SquidConfig Config
Definition: SquidConfig.cc:12
StatCounters statCounter
Definition: StatCounters.cc:12
static void comm_init_opened(const Comm::ConnectionPointer &conn, const char *note, struct addrinfo *AI)
update FD tables after a local or remote (IPC) comm_openex();
Definition: comm.cc:417
#define DBG_DATA
Definition: Debug.h:48
CbDataListContainer< DeferredRead > deferredReads
Definition: CommRead.h:61
@ SHUTDOWN
Definition: Flag.h:20

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors