comm.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2020 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 
751 // XXX: This code has been broken, unused, and untested since 933dd09. Remove.
752 #if LINGERING_CLOSE
753 static void
754 commLingerClose(int fd, void *unused)
755 {
756  LOCAL_ARRAY(char, buf, 1024);
757  int n = FD_READ_METHOD(fd, buf, 1024);
758  if (n < 0) {
759  int xerrno = errno;
760  debugs(5, 3, "FD " << fd << " read: " << xstrerr(xerrno));
761  }
762  comm_close(fd);
763 }
764 
765 static void
766 commLingerTimeout(const FdeCbParams &params)
767 {
768  debugs(5, 3, "commLingerTimeout: FD " << params.fd);
769  comm_close(params.fd);
770 }
771 
772 /*
773  * Inspired by apache
774  */
775 void
776 comm_lingering_close(int fd)
777 {
779 
780  if (shutdown(fd, 1) < 0) {
781  comm_close(fd);
782  return;
783  }
784 
785  fd_note(fd, "lingering close");
786  AsyncCall::Pointer call = commCbCall(5,4, "commLingerTimeout", FdeCbPtrFun(commLingerTimeout, NULL));
787 
788  debugs(5, 3, HERE << "FD " << fd << " timeout " << timeout);
789  assert(fd_table[fd].flags.open);
790  if (callback != NULL) {
791  typedef FdeCbParams Params;
792  Params &params = GetCommParams<Params>(callback);
793  params.fd = fd;
794  fd_table[fd].timeoutHandler = callback;
795  fd_table[fd].timeout = squid_curtime + static_cast<time_t>(10);
796  }
797 
798  Comm::SetSelect(fd, COMM_SELECT_READ, commLingerClose, NULL, 0);
799 }
800 
801 #endif
802 
807 void
809 {
810  struct linger L;
811  L.l_onoff = 1;
812  L.l_linger = 0;
813 
814  if (setsockopt(conn->fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0) {
815  int xerrno = errno;
816  debugs(50, DBG_CRITICAL, "ERROR: Closing " << conn << " with TCP RST: " << xstrerr(xerrno));
817  }
818  conn->close();
819 }
820 
821 // Legacy close function.
822 void
824 {
825  struct linger L;
826  L.l_onoff = 1;
827  L.l_linger = 0;
828 
829  if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0) {
830  int xerrno = errno;
831  debugs(50, DBG_CRITICAL, "ERROR: Closing FD " << fd << " with TCP RST: " << xstrerr(xerrno));
832  }
833  comm_close(fd);
834 }
835 
836 void
838 {
840 }
841 
842 void
844 {
845  fde *F = &fd_table[params.fd];
846  F->ssl.reset();
847  F->dynamicTlsContext.reset();
848  fd_close(params.fd); /* update fdstat */
849  close(params.fd);
850 
851  ++ statCounter.syscalls.sock.closes;
852 
853  /* When one connection closes, give accept() a chance, if need be */
854  CodeContext::Reset(); // exit FD-specific context
856 }
857 
858 /*
859  * Close the socket fd.
860  *
861  * + call write handlers with ERR_CLOSING
862  * + call read handlers with ERR_CLOSING
863  * + call closing handlers
864  *
865  * NOTE: Comm::ERR_CLOSING will NOT be called for CommReads' sitting in a
866  * DeferredReadManager.
867  */
868 void
869 _comm_close(int fd, char const *file, int line)
870 {
871  debugs(5, 3, "start closing FD " << fd << " by " << file << ":" << line);
872  assert(fd >= 0);
873  assert(fd < Squid_MaxFD);
874 
875  fde *F = &fd_table[fd];
876 
877  if (F->closing())
878  return;
879 
880  /* XXX: is this obsolete behind F->closing() ? */
881  if ( (shutting_down || reconfiguring) && (!F->flags.open || F->type == FD_FILE))
882  return;
883 
884  /* The following fails because ipc.c is doing calls to pipe() to create sockets! */
885  if (!isOpen(fd)) {
886  debugs(50, DBG_IMPORTANT, HERE << "BUG 3556: FD " << fd << " is not an open socket.");
887  // XXX: do we need to run close(fd) or fd_close(fd) here?
888  return;
889  }
890 
891  assert(F->type != FD_FILE);
892 
894 
895  F->flags.close_request = true;
896 
897  // We have caller's context and fde::codeContext. In the unlikely event they
898  // differ, it is not clear which context is more applicable to this closure.
899  // For simplicity sake, we remain in the caller's context while still
900  // allowing individual advanced callbacks to overwrite it.
901 
902  if (F->ssl) {
903  AsyncCall::Pointer startCall=commCbCall(5,4, "commStartTlsClose",
904  FdeCbPtrFun(commStartTlsClose, nullptr));
905  FdeCbParams &startParams = GetCommParams<FdeCbParams>(startCall);
906  startParams.fd = fd;
907  ScheduleCallHere(startCall);
908  }
909 
910  // a half-closed fd may lack a reader, so we stop monitoring explicitly
911  if (commHasHalfClosedMonitor(fd))
913  commUnsetFdTimeout(fd);
914 
915  // notify read/write handlers after canceling select reservations, if any
916  if (COMMIO_FD_WRITECB(fd)->active()) {
918  COMMIO_FD_WRITECB(fd)->finish(Comm::ERR_CLOSING, errno);
919  }
920  if (COMMIO_FD_READCB(fd)->active()) {
922  COMMIO_FD_READCB(fd)->finish(Comm::ERR_CLOSING, errno);
923  }
924 
925 #if USE_DELAY_POOLS
927  if (bucket->selectWaiting)
928  bucket->onFdClosed();
929  }
930 #endif
931 
933 
935 
936  AsyncCall::Pointer completeCall=commCbCall(5,4, "comm_close_complete",
938  FdeCbParams &completeParams = GetCommParams<FdeCbParams>(completeCall);
939  completeParams.fd = fd;
940  // must use async call to wait for all callbacks
941  // scheduled before comm_close() to finish
942  ScheduleCallHere(completeCall);
943 
945 }
946 
947 /* Send a udp datagram to specified TO_ADDR. */
948 int
950  const Ip::Address &to_addr,
951  const void *buf,
952  int len)
953 {
955  ++ statCounter.syscalls.sock.sendtos;
956 
957  debugs(50, 3, "comm_udp_sendto: Attempt to send UDP packet to " << to_addr <<
958  " using FD " << fd << " using Port " << comm_local_port(fd) );
959 
960  struct addrinfo *AI = NULL;
961  to_addr.getAddrInfo(AI, fd_table[fd].sock_family);
962  int x = sendto(fd, buf, len, 0, AI->ai_addr, AI->ai_addrlen);
963  int xerrno = errno;
965 
967 
968  if (x >= 0) {
969  errno = xerrno; // restore for caller to use
970  return x;
971  }
972 
973 #if _SQUID_LINUX_
974  if (ECONNREFUSED != xerrno)
975 #endif
976  debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", (family=" << fd_table[fd].sock_family << ") " << to_addr << ": " << xstrerr(xerrno));
977 
978  errno = xerrno; // restore for caller to use
979  return Comm::COMM_ERROR;
980 }
981 
984 {
985  debugs(5, 5, "comm_add_close_handler: FD " << fd << ", handler=" <<
986  handler << ", data=" << data);
987 
988  AsyncCall::Pointer call=commCbCall(5,4, "SomeCloseHandler",
990  comm_add_close_handler(fd, call);
991  return call;
992 }
993 
994 void
996 {
997  debugs(5, 5, "comm_add_close_handler: FD " << fd << ", AsyncCall=" << call);
998 
999  /*TODO:Check for a similar scheduled AsyncCall*/
1000 // for (c = fd_table[fd].closeHandler; c; c = c->next)
1001 // assert(c->handler != handler || c->data != data);
1002 
1003  call->setNext(fd_table[fd].closeHandler);
1004 
1005  fd_table[fd].closeHandler = call;
1006 }
1007 
1008 // remove function-based close handler
1009 void
1011 {
1012  assert(isOpen(fd));
1013  /* Find handler in list */
1014  debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", handler=" <<
1015  handler << ", data=" << data);
1016 
1017  AsyncCall::Pointer p, prev = NULL;
1018  for (p = fd_table[fd].closeHandler; p != NULL; prev = p, p = p->Next()) {
1020  const Call *call = dynamic_cast<const Call*>(p.getRaw());
1021  if (!call) // method callbacks have their own comm_remove_close_handler
1022  continue;
1023 
1024  typedef CommCloseCbParams Params;
1025  const Params &params = GetCommParams<Params>(p);
1026  if (call->dialer.handler == handler && params.data == data)
1027  break; /* This is our handler */
1028  }
1029 
1030  // comm_close removes all close handlers so our handler may be gone
1031  if (p != NULL) {
1032  p->dequeue(fd_table[fd].closeHandler, prev);
1033  p->cancel("comm_remove_close_handler");
1034  }
1035 }
1036 
1037 // remove method-based close handler
1038 void
1040 {
1041  assert(isOpen(fd));
1042  debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", AsyncCall=" << call);
1043 
1044  // comm_close removes all close handlers so our handler may be gone
1045  AsyncCall::Pointer p, prev = NULL;
1046  for (p = fd_table[fd].closeHandler; p != NULL && p != call; prev = p, p = p->Next());
1047 
1048  if (p != NULL)
1049  p->dequeue(fd_table[fd].closeHandler, prev);
1050  call->cancel("comm_remove_close_handler");
1051 }
1052 
1053 static void
1055 {
1056 
1057  struct linger L;
1058  L.l_onoff = 0; /* off */
1059  L.l_linger = 0;
1060 
1061  if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0) {
1062  int xerrno = errno;
1063  debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1064  }
1065  fd_table[fd].flags.nolinger = true;
1066 }
1067 
1068 static void
1070 {
1071  int on = 1;
1072  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0) {
1073  int xerrno = errno;
1074  debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1075  }
1076 }
1077 
1078 static void
1080 {
1081  if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size)) < 0) {
1082  int xerrno = errno;
1083  debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", SIZE " << size << ": " << xstrerr(xerrno));
1084  }
1085  if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size)) < 0) {
1086  int xerrno = errno;
1087  debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", SIZE " << size << ": " << xstrerr(xerrno));
1088  }
1089 #ifdef TCP_WINDOW_CLAMP
1090  if (setsockopt(fd, SOL_TCP, TCP_WINDOW_CLAMP, (char *) &size, sizeof(size)) < 0) {
1091  int xerrno = errno;
1092  debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", SIZE " << size << ": " << xstrerr(xerrno));
1093  }
1094 #endif
1095 }
1096 
1097 int
1099 {
1100 #if _SQUID_WINDOWS_
1101  int nonblocking = TRUE;
1102 
1103  if (ioctl(fd, FIONBIO, &nonblocking) < 0) {
1104  int xerrno = errno;
1105  debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": " << xstrerr(xerrno) << " " << fd_table[fd].type);
1106  return Comm::COMM_ERROR;
1107  }
1108 
1109 #else
1110  int flags;
1111  int dummy = 0;
1112 
1113  if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
1114  int xerrno = errno;
1115  debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": fcntl F_GETFL: " << xstrerr(xerrno));
1116  return Comm::COMM_ERROR;
1117  }
1118 
1119  if (fcntl(fd, F_SETFL, flags | SQUID_NONBLOCK) < 0) {
1120  int xerrno = errno;
1121  debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1122  return Comm::COMM_ERROR;
1123  }
1124 #endif
1125 
1126  fd_table[fd].flags.nonblocking = true;
1127  return 0;
1128 }
1129 
1130 int
1132 {
1133 #if _SQUID_WINDOWS_
1134  int nonblocking = FALSE;
1135 
1136  if (ioctlsocket(fd, FIONBIO, (unsigned long *) &nonblocking) < 0) {
1137 #else
1138  int flags;
1139  int dummy = 0;
1140 
1141  if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
1142  int xerrno = errno;
1143  debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": fcntl F_GETFL: " << xstrerr(xerrno));
1144  return Comm::COMM_ERROR;
1145  }
1146 
1147  if (fcntl(fd, F_SETFL, flags & (~SQUID_NONBLOCK)) < 0) {
1148 #endif
1149  int xerrno = errno;
1150  debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1151  return Comm::COMM_ERROR;
1152  }
1153 
1154  fd_table[fd].flags.nonblocking = false;
1155  return 0;
1156 }
1157 
1158 void
1160 {
1161 #ifdef FD_CLOEXEC
1162  int flags;
1163  int dummy = 0;
1164 
1165  if ((flags = fcntl(fd, F_GETFD, dummy)) < 0) {
1166  int xerrno = errno;
1167  debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": fcntl F_GETFD: " << xstrerr(xerrno));
1168  return;
1169  }
1170 
1171  if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
1172  int xerrno = errno;
1173  debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": set close-on-exec failed: " << xstrerr(xerrno));
1174  }
1175 
1176  fd_table[fd].flags.close_on_exec = true;
1177 
1178 #endif
1179 }
1180 
1181 #ifdef TCP_NODELAY
1182 static void
1183 commSetTcpNoDelay(int fd)
1184 {
1185  int on = 1;
1186 
1187  if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) < 0) {
1188  int xerrno = errno;
1189  debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1190  }
1191 
1192  fd_table[fd].flags.nodelay = true;
1193 }
1194 
1195 #endif
1196 
1197 void
1198 commSetTcpKeepalive(int fd, int idle, int interval, int timeout)
1199 {
1200  int on = 1;
1201 #ifdef TCP_KEEPCNT
1202  if (timeout && interval) {
1203  int count = (timeout + interval - 1) / interval;
1204  if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &count, sizeof(on)) < 0) {
1205  int xerrno = errno;
1206  debugs(5, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1207  }
1208  }
1209 #endif
1210 #ifdef TCP_KEEPIDLE
1211  if (idle) {
1212  if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(on)) < 0) {
1213  int xerrno = errno;
1214  debugs(5, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1215  }
1216  }
1217 #endif
1218 #ifdef TCP_KEEPINTVL
1219  if (interval) {
1220  if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(on)) < 0) {
1221  int xerrno = errno;
1222  debugs(5, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1223  }
1224  }
1225 #endif
1226  if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &on, sizeof(on)) < 0) {
1227  int xerrno = errno;
1228  debugs(5, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1229  }
1230 }
1231 
1232 void
1234 {
1235  assert(fd_table);
1236 
1237  /* make sure the accept() socket FIFO delay queue exists */
1239 
1240  // make sure the IO pending callback table exists
1242 
1243  /* XXX account fd_table */
1244  /* Keep a few file descriptors free so that we don't run out of FD's
1245  * after accepting a client but before it opens a socket or a file.
1246  * Since Squid_MaxFD can be as high as several thousand, don't waste them */
1247  RESERVED_FD = min(100, Squid_MaxFD / 4);
1248 
1250 
1251  /* setup the select loop module */
1253 }
1254 
1255 void
1257 {
1258  delete TheHalfClosed;
1259  TheHalfClosed = NULL;
1260 
1262 }
1263 
1264 #if USE_DELAY_POOLS
1265 // called when the queue is done waiting for the client bucket to fill
1266 void
1268 {
1269  CommQuotaQueue *queue = static_cast<CommQuotaQueue*>(data);
1270  assert(queue);
1271 
1272  ClientInfo *clientInfo = queue->clientInfo;
1273  // ClientInfo invalidates queue if freed, so if we got here through,
1274  // evenAdd cbdata protections, everything should be valid and consistent
1275  assert(clientInfo);
1276  assert(clientInfo->hasQueue());
1277  assert(clientInfo->hasQueue(queue));
1278  assert(clientInfo->eventWaiting);
1279  clientInfo->eventWaiting = false;
1280 
1281  do {
1282  clientInfo->writeOrDequeue();
1283  if (clientInfo->selectWaiting)
1284  return;
1285  } while (clientInfo->hasQueue());
1286 
1287  debugs(77, 3, "emptied queue");
1288 }
1289 
1290 void
1292 {
1294  const auto head = quotaPeekFd();
1295  const auto &headFde = fd_table[head];
1296  CallBack(headFde.codeContext, [&] {
1297  const auto ccb = COMMIO_FD_WRITECB(head);
1298  // check that the head descriptor is still relevant
1299  if (headFde.clientInfo == this &&
1300  quotaPeekReserv() == ccb->quotaQueueReserv &&
1301  !headFde.closing()) {
1302 
1303  // wait for the head descriptor to become ready for writing
1304  Comm::SetSelect(head, COMM_SELECT_WRITE, Comm::HandleWrite, ccb, 0);
1305  selectWaiting = true;
1306  } else {
1307  quotaDequeue(); // remove the no longer relevant descriptor
1308  }
1309  });
1310 }
1311 
1312 bool
1314 {
1315  assert(quotaQueue);
1316  return !quotaQueue->empty();
1317 }
1318 
1319 bool
1321 {
1322  assert(quotaQueue);
1323  return quotaQueue == q;
1324 }
1325 
1327 int
1329 {
1330  assert(quotaQueue);
1331  return quotaQueue->front();
1332 }
1333 
1335 unsigned int
1337 {
1338  assert(quotaQueue);
1339  return quotaQueue->outs + 1;
1340 }
1341 
1343 unsigned int
1345 {
1346  assert(quotaQueue);
1347  return quotaQueue->enqueue(fd);
1348 }
1349 
1351 void
1353 {
1354  assert(quotaQueue);
1355  quotaQueue->dequeue();
1356 }
1357 
1358 void
1360 {
1361  if (!eventWaiting && !selectWaiting && hasQueue()) {
1362  // wait at least a second if the bucket is empty
1363  const double delay = (bucketLevel < 1.0) ? 1.0 : 0.0;
1364  eventAdd("commHandleWriteHelper", &commHandleWriteHelper,
1365  quotaQueue, delay, 0, true);
1366  eventWaiting = true;
1367  }
1368 }
1369 
1371 int
1373 {
1374  /* If we have multiple clients and give full bucketSize to each client then
1375  * clt1 may often get a lot more because clt1->clt2 time distance in the
1376  * select(2) callback order may be a lot smaller than cltN->clt1 distance.
1377  * We divide quota evenly to be more fair. */
1378 
1379  if (!rationedCount) {
1380  rationedCount = quotaQueue->size() + 1;
1381 
1382  // The delay in ration recalculation _temporary_ deprives clients from
1383  // bytes that should have trickled in while rationedCount was positive.
1384  refillBucket();
1385 
1386  // Rounding errors do not accumulate here, but we round down to avoid
1387  // negative bucket sizes after write with rationedCount=1.
1388  rationedQuota = static_cast<int>(floor(bucketLevel/rationedCount));
1389  debugs(77,5, HERE << "new rationedQuota: " << rationedQuota <<
1390  '*' << rationedCount);
1391  }
1392 
1393  --rationedCount;
1394  debugs(77,7, HERE << "rationedQuota: " << rationedQuota <<
1395  " rations remaining: " << rationedCount);
1396 
1397  // update 'last seen' time to prevent clientdb GC from dropping us
1399  return rationedQuota;
1400 }
1401 
1402 bool
1404 {
1405  assert(hasQueue());
1406  assert(quotaPeekFd() == state->conn->fd);
1407  quotaDequeue(); // we will write or requeue below
1408  if (nleft > 0 && !BandwidthBucket::applyQuota(nleft, state)) {
1409  state->quotaQueueReserv = quotaEnqueue(state->conn->fd);
1410  kickQuotaQueue();
1411  return false;
1412  }
1413  return true;
1414 }
1415 
1416 void
1418 {
1419  if (writeLimitingActive) {
1420  state->quotaQueueReserv = quotaEnqueue(state->conn->fd);
1421  kickQuotaQueue();
1422  }
1423 }
1424 
1425 void
1427 {
1429  // kick queue or it will get stuck as commWriteHandle is not called
1430  kickQuotaQueue();
1431 }
1432 
1433 void
1435 {
1436  if (len > 0)
1438  // even if we wrote nothing, we were served; give others a chance
1439  kickQuotaQueue();
1440 }
1441 
1442 void
1443 ClientInfo::setWriteLimiter(const int aWriteSpeedLimit, const double anInitialBurst, const double aHighWatermark)
1444 {
1445  debugs(77,5, "Write limits for " << (const char*)key <<
1446  " speed=" << aWriteSpeedLimit << " burst=" << anInitialBurst <<
1447  " highwatermark=" << aHighWatermark);
1448 
1449  // set or possibly update traffic shaping parameters
1450  writeLimitingActive = true;
1451  writeSpeedLimit = aWriteSpeedLimit;
1452  bucketSizeLimit = aHighWatermark;
1453 
1454  // but some members should only be set once for a newly activated bucket
1455  if (firstTimeConnection) {
1456  firstTimeConnection = false;
1457 
1459  assert(!quotaQueue);
1460  quotaQueue = new CommQuotaQueue(this);
1461 
1462  bucketLevel = anInitialBurst;
1464  }
1465 }
1466 
1468  ins(0), outs(0)
1469 {
1470  assert(clientInfo);
1471 }
1472 
1474 {
1475  assert(!clientInfo); // ClientInfo should clear this before destroying us
1476 }
1477 
1479 unsigned int
1481 {
1482  debugs(77,5, "clt" << (const char*)clientInfo->key <<
1483  ": FD " << fd << " with qqid" << (ins+1) << ' ' << fds.size());
1484  fds.push_back(fd);
1485  fd_table[fd].codeContext = CodeContext::Current();
1486  return ++ins;
1487 }
1488 
1490 void
1492 {
1493  assert(!fds.empty());
1494  debugs(77,5, "clt" << (const char*)clientInfo->key <<
1495  ": FD " << fds.front() << " with qqid" << (outs+1) << ' ' <<
1496  fds.size());
1497  fds.pop_front();
1498  ++outs;
1499 }
1500 #endif /* USE_DELAY_POOLS */
1501 
1502 /*
1503  * hm, this might be too general-purpose for all the places we'd
1504  * like to use it.
1505  */
1506 int
1507 ignoreErrno(int ierrno)
1508 {
1509  switch (ierrno) {
1510 
1511  case EINPROGRESS:
1512 
1513  case EWOULDBLOCK:
1514 #if EAGAIN != EWOULDBLOCK
1515 
1516  case EAGAIN:
1517 #endif
1518 
1519  case EALREADY:
1520 
1521  case EINTR:
1522 #ifdef ERESTART
1523 
1524  case ERESTART:
1525 #endif
1526 
1527  return 1;
1528 
1529  default:
1530  return 0;
1531  }
1532 
1533  /* NOTREACHED */
1534 }
1535 
1536 void
1538 {
1539  int fd;
1540  fde *F = NULL;
1541 
1542  for (fd = 0; fd <= Biggest_FD; ++fd) {
1543  F = &fd_table[fd];
1544 
1545  if (!F->flags.open)
1546  continue;
1547 
1548  if (F->type != FD_SOCKET)
1549  continue;
1550 
1551  if (F->flags.ipc) /* don't close inter-process sockets */
1552  continue;
1553 
1554  if (F->timeoutHandler != NULL) {
1555  AsyncCall::Pointer callback = F->timeoutHandler;
1556  F->timeoutHandler = NULL;
1557  debugs(5, 5, "commCloseAllSockets: FD " << fd << ": Calling timeout handler");
1559  } else {
1560  debugs(5, 5, "commCloseAllSockets: FD " << fd << ": calling comm_reset_close()");
1562  }
1563  }
1564 }
1565 
1566 static bool
1568 {
1569  if (!F->flags.open)
1570  return true;
1571 
1572  if (F->timeout == 0)
1573  return true;
1574 
1575  if (F->timeout > squid_curtime)
1576  return true;
1577 
1578  return false;
1579 }
1580 
1581 static bool
1583 {
1584  if (!COMMIO_FD_WRITECB(fd)->active())
1585  return false;
1586 
1587  if ((squid_curtime - fd_table[fd].writeStart) < Config.Timeout.write)
1588  return false;
1589 
1590  return true;
1591 }
1592 
1593 void
1595 {
1596  int fd;
1597  fde *F = NULL;
1599 
1600  for (fd = 0; fd <= Biggest_FD; ++fd) {
1601  F = &fd_table[fd];
1602 
1603  if (writeTimedOut(fd)) {
1604  // We have an active write callback and we are timed out
1605  CodeContext::Reset(F->codeContext);
1606  debugs(5, 5, "checkTimeouts: FD " << fd << " auto write timeout");
1608  COMMIO_FD_WRITECB(fd)->finish(Comm::COMM_ERROR, ETIMEDOUT);
1610 #if USE_DELAY_POOLS
1611  } else if (F->writeQuotaHandler != nullptr && COMMIO_FD_WRITECB(fd)->conn != nullptr) {
1612  // TODO: Move and extract quota() call to place it inside F->codeContext.
1613  if (!F->writeQuotaHandler->selectWaiting && F->writeQuotaHandler->quota() && !F->closing()) {
1614  CodeContext::Reset(F->codeContext);
1615  F->writeQuotaHandler->selectWaiting = true;
1618  }
1619  continue;
1620 #endif
1621  }
1622  else if (AlreadyTimedOut(F))
1623  continue;
1624 
1625  CodeContext::Reset(F->codeContext);
1626  debugs(5, 5, "checkTimeouts: FD " << fd << " Expired");
1627 
1628  if (F->timeoutHandler != NULL) {
1629  debugs(5, 5, "checkTimeouts: FD " << fd << ": Call timeout handler");
1630  callback = F->timeoutHandler;
1631  F->timeoutHandler = NULL;
1633  } else {
1634  debugs(5, 5, "checkTimeouts: FD " << fd << ": Forcing comm_close()");
1635  comm_close(fd);
1636  }
1637 
1639  }
1640 }
1641 
1643 // by scheduling a read callback to a monitoring handler that
1644 // will close the connection on read errors.
1645 void
1647 {
1648  debugs(5, 5, HERE << "adding FD " << fd << " to " << *TheHalfClosed);
1649  assert(isOpen(fd) && !commHasHalfClosedMonitor(fd));
1650  (void)TheHalfClosed->add(fd); // could also assert the result
1651  fd_table[fd].codeContext = CodeContext::Current();
1652  commPlanHalfClosedCheck(); // may schedule check if we added the first FD
1653 }
1654 
1655 static
1656 void
1658 {
1659  if (!WillCheckHalfClosed && !TheHalfClosed->empty()) {
1660  eventAdd("commHalfClosedCheck", &commHalfClosedCheck, NULL, 1.0, 1);
1661  WillCheckHalfClosed = true;
1662  }
1663 }
1664 
1667 static
1668 void
1670 {
1671  debugs(5, 5, HERE << "checking " << *TheHalfClosed);
1672 
1673  typedef DescriptorSet::const_iterator DSCI;
1674  const DSCI end = TheHalfClosed->end();
1675  for (DSCI i = TheHalfClosed->begin(); i != end; ++i) {
1676  Comm::ConnectionPointer c = new Comm::Connection; // XXX: temporary. make HalfClosed a list of these.
1677  c->fd = *i;
1678  if (!fd_table[c->fd].halfClosedReader) { // not reading already
1679  CallBack(fd_table[c->fd].codeContext, [&c] {
1680  AsyncCall::Pointer call = commCbCall(5,4, "commHalfClosedReader",
1681  CommIoCbPtrFun(&commHalfClosedReader, nullptr));
1682  Comm::Read(c, call);
1683  fd_table[c->fd].halfClosedReader = call;
1684  });
1685  } else
1686  c->fd = -1; // XXX: temporary. prevent c replacement erase closing listed FD
1687  }
1688 
1689  WillCheckHalfClosed = false; // as far as we know
1690  commPlanHalfClosedCheck(); // may need to check again
1691 }
1692 
1694 // We are monitoring if the read handler for the fd is the monitoring handler.
1695 bool
1697 {
1698  return TheHalfClosed->has(fd);
1699 }
1700 
1702 void
1704 {
1705  debugs(5, 5, HERE << "removing FD " << fd << " from " << *TheHalfClosed);
1706 
1707  // cancel the read if one was scheduled
1708  AsyncCall::Pointer reader = fd_table[fd].halfClosedReader;
1709  if (reader != NULL)
1710  Comm::ReadCancel(fd, reader);
1711  fd_table[fd].halfClosedReader = NULL;
1712 
1713  TheHalfClosed->del(fd);
1714 }
1715 
1717 static void
1718 commHalfClosedReader(const Comm::ConnectionPointer &conn, char *, size_t size, Comm::Flag flag, int, void *)
1719 {
1720  // there cannot be more data coming in on half-closed connections
1721  assert(size == 0);
1722  assert(conn != NULL);
1723  assert(commHasHalfClosedMonitor(conn->fd)); // or we would have canceled the read
1724 
1725  fd_table[conn->fd].halfClosedReader = NULL; // done reading, for now
1726 
1727  // nothing to do if fd is being closed
1728  if (flag == Comm::ERR_CLOSING)
1729  return;
1730 
1731  // if read failed, close the connection
1732  if (flag != Comm::OK) {
1733  debugs(5, 3, HERE << "closing " << conn);
1734  conn->close();
1735  return;
1736  }
1737 
1738  // continue waiting for close or error
1739  commPlanHalfClosedCheck(); // make sure this fd will be checked again
1740 }
1741 
1743 
1744 CommRead::CommRead(const Comm::ConnectionPointer &c, char *buf_, int len_, AsyncCall::Pointer &callback_)
1745  : conn(c), buf(buf_), len(len_), callback(callback_) {}
1746 
1747 DeferredRead::DeferredRead () : theReader(NULL), theContext(NULL), theRead(), cancelled(false) {}
1748 
1749 DeferredRead::DeferredRead (DeferrableRead *aReader, void *data, CommRead const &aRead) : theReader(aReader), theContext (data), theRead(aRead), cancelled(false) {}
1750 
1752 {
1753  flushReads();
1755 }
1756 
1757 /* explicit instantiation required for some systems */
1758 
1762 
1763 void
1765 {
1766  debugs(5, 3, "Adding deferred read on " << aRead.theRead.conn);
1768 
1769  // We have to use a global function as a closer and point to temp
1770  // instead of "this" because DeferredReadManager is not a job and
1771  // is not even cbdata protected
1772  // XXX: and yet we use cbdata protection functions on it??
1773  AsyncCall::Pointer closer = commCbCall(5,4,
1774  "DeferredReadManager::CloseHandler",
1776  comm_add_close_handler(aRead.theRead.conn->fd, closer);
1777  temp->element.closer = closer; // remember so that we can cancel
1778 }
1779 
1780 void
1782 {
1783  if (!cbdataReferenceValid(params.data))
1784  return;
1785 
1787 
1788  temp->element.closer = NULL;
1789  temp->element.markCancelled();
1790 }
1791 
1794 {
1795  assert (!deferredReads.empty());
1796 
1798 
1799  // NOTE: at this point the connection has been paused/stalled for an unknown
1800  // amount of time. We must re-validate that it is active and usable.
1801 
1802  // If the connection has been closed already. Cancel this read.
1803  if (!fd_table || !Comm::IsConnOpen(read.theRead.conn)) {
1804  if (read.closer != NULL) {
1805  read.closer->cancel("Connection closed before.");
1806  read.closer = NULL;
1807  }
1808  read.markCancelled();
1809  }
1810 
1811  if (!read.cancelled) {
1813  read.closer = NULL;
1814  }
1815 
1817 
1818  return result;
1819 }
1820 
1821 void
1823 {
1824  /* if we had CbDataList::size() we could consolidate this and flushReads */
1825 
1826  if (count < 1) {
1827  flushReads();
1828  return;
1829  }
1830 
1831  size_t remaining = count;
1832 
1833  while (!deferredReads.empty() && remaining) {
1835  kickARead(aRead);
1836 
1837  if (!aRead.cancelled)
1838  --remaining;
1839  }
1840 }
1841 
1842 void
1844 {
1846  reads = deferredReads;
1848 
1849  // XXX: For fairness this SHOULD randomize the order
1850  while (!reads.empty()) {
1851  DeferredRead aRead = popHead(reads);
1852  kickARead(aRead);
1853  }
1854 }
1855 
1856 void
1858 {
1859  if (aRead.cancelled)
1860  return;
1861 
1862  if (Comm::IsConnOpen(aRead.theRead.conn) && fd_table[aRead.theRead.conn->fd].closing())
1863  return;
1864 
1865  debugs(5, 3, "Kicking deferred read on " << aRead.theRead.conn);
1866 
1867  aRead.theReader(aRead.theContext, aRead.theRead);
1868 }
1869 
1870 void
1872 {
1873  cancelled = true;
1874 }
1875 
1876 int
1878 {
1879  static time_t last_timeout = 0;
1880 
1881  /* No, this shouldn't be here. But it shouldn't be in each comm handler. -adrian */
1882  if (squid_curtime > last_timeout) {
1883  last_timeout = squid_curtime;
1884  checkTimeouts();
1885  }
1886 
1887  switch (Comm::DoSelect(timeout)) {
1888 
1889  case Comm::OK:
1890 
1891  case Comm::TIMEOUT:
1892  return 0;
1893 
1894  case Comm::IDLE:
1895 
1896  case Comm::SHUTDOWN:
1897  return EVENT_IDLE;
1898 
1899  case Comm::COMM_ERROR:
1900  return EVENT_ERROR;
1901 
1902  default:
1903  fatal_dump("comm.cc: Internal error -- this should never happen.");
1904  return EVENT_ERROR;
1905  };
1906 }
1907 
1909 int
1910 comm_open_uds(int sock_type,
1911  int proto,
1912  struct sockaddr_un* addr,
1913  int flags)
1914 {
1915  // TODO: merge with comm_openex() when Ip::Address becomes NetAddress
1916 
1917  int new_socket;
1918 
1920  /* Create socket for accepting new connections. */
1921  ++ statCounter.syscalls.sock.sockets;
1922 
1923  /* Setup the socket addrinfo details for use */
1924  struct addrinfo AI;
1925  AI.ai_flags = 0;
1926  AI.ai_family = PF_UNIX;
1927  AI.ai_socktype = sock_type;
1928  AI.ai_protocol = proto;
1929  AI.ai_addrlen = SUN_LEN(addr);
1930  AI.ai_addr = (sockaddr*)addr;
1931  AI.ai_canonname = NULL;
1932  AI.ai_next = NULL;
1933 
1934  debugs(50, 3, HERE << "Attempt open socket for: " << addr->sun_path);
1935 
1936  if ((new_socket = socket(AI.ai_family, AI.ai_socktype, AI.ai_protocol)) < 0) {
1937  int xerrno = errno;
1938  /* Increase the number of reserved fd's if calls to socket()
1939  * are failing because the open file table is full. This
1940  * limits the number of simultaneous clients */
1941 
1942  if (limitError(xerrno)) {
1943  debugs(50, DBG_IMPORTANT, MYNAME << "socket failure: " << xstrerr(xerrno));
1944  fdAdjustReserved();
1945  } else {
1946  debugs(50, DBG_CRITICAL, MYNAME << "socket failure: " << xstrerr(xerrno));
1947  }
1948 
1950  return -1;
1951  }
1952 
1953  debugs(50, 3, "Opened UDS FD " << new_socket << " : family=" << AI.ai_family << ", type=" << AI.ai_socktype << ", protocol=" << AI.ai_protocol);
1954 
1955  /* update fdstat */
1956  debugs(50, 5, HERE << "FD " << new_socket << " is a new socket");
1957 
1958  assert(!isOpen(new_socket));
1959  fd_open(new_socket, FD_MSGHDR, addr->sun_path);
1960 
1961  fd_table[new_socket].sock_family = AI.ai_family;
1962 
1963  if (!(flags & COMM_NOCLOEXEC))
1964  commSetCloseOnExec(new_socket);
1965 
1966  if (flags & COMM_REUSEADDR)
1967  commSetReuseAddr(new_socket);
1968 
1969  if (flags & COMM_NONBLOCKING) {
1970  if (commSetNonBlocking(new_socket) != Comm::OK) {
1971  comm_close(new_socket);
1973  return -1;
1974  }
1975  }
1976 
1977  if (flags & COMM_DOBIND) {
1978  if (commBind(new_socket, AI) != Comm::OK) {
1979  comm_close(new_socket);
1981  return -1;
1982  }
1983  }
1984 
1985 #ifdef TCP_NODELAY
1986  if (sock_type == SOCK_STREAM)
1987  commSetTcpNoDelay(new_socket);
1988 
1989 #endif
1990 
1991  if (Config.tcpRcvBufsz > 0 && sock_type == SOCK_STREAM)
1992  commSetTcpRcvbuf(new_socket, Config.tcpRcvBufsz);
1993 
1995 
1996  return new_socket;
1997 }
1998 
static void commSetTcpRcvbuf(int, int)
Definition: comm.cc:1079
bool cancel(const char *reason)
Definition: AsyncCall.cc:56
void comm_exit(void)
Definition: comm.cc:1256
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:1403
CommQuotaQueue(ClientInfo *info)
Definition: comm.cc:1467
AsyncCall::Pointer comm_add_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:983
#define MYNAME
Definition: Debug.h:170
void quotaDequeue()
pops queue head from queue
Definition: comm.cc:1352
void _comm_close(int fd, char const *file, int line)
Definition: comm.cc:869
struct StatCounters::@137::@142 sock
unsigned int enqueue(int fd)
places the given fd at the end of the queue; returns reservation ID
Definition: comm.cc:1480
void flushReads()
Definition: comm.cc:1843
void delayRead(DeferredRead const &)
Definition: comm.cc:1764
#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
void setNext(AsyncCall::Pointer aNext)
Definition: AsyncCall.h:63
#define LOCAL_ARRAY(type, name, size)
Definition: squid.h:75
void fd_note(int fd, const char *s)
Definition: fd.cc:246
int front() const
Definition: ClientInfo.h:119
virtual void reduceBucket(int len) override
Decreases the bucket level.
Definition: comm.cc:1434
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:949
void commCallCloseHandlers(int fd)
Definition: comm.cc:734
void setWriteLimiter(const int aWriteSpeedLimit, const double anInitialBurst, const double aHighWatermark)
Definition: comm.cc:1443
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:1703
@ TIMEOUT
Definition: Flag.h:19
int commSetNonBlocking(int fd)
Definition: comm.cc:1098
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:1267
DeferredRead()
Definition: comm.cc:1747
int type
Definition: errorpage.cc:152
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:412
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:1696
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:18
Definition: fde.h:51
int socklen_t
Definition: types.h:158
void commStartTlsClose(const FdeCbParams &params)
Definition: comm.cc:837
socklen_t ai_addrlen
static bool writeTimedOut(int fd)
Definition: comm.cc:1582
#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:55
void comm_set_v6only(int fd, int tos)
Definition: comm.cc:270
void kickARead(DeferredRead const &)
Definition: comm.cc:1857
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:200
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:1426
void commCloseAllSockets(void)
Definition: comm.cc:1537
void ReadCancel(int fd, AsyncCall::Pointer &callback)
Cancel the read pending on FD. No action if none pending.
Definition: Read.cc:217
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:1131
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:1233
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:1742
size_t tcpRcvBufsz
Definition: SquidConfig.h:247
#define NULL
Definition: types.h:166
static bool WillCheckHalfClosed
the set of half-closed FDs
Definition: comm.cc:74
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:128
Details about a particular Comm IO callback event.
Definition: IoCallback.h:29
virtual int quota() override
allocate quota for a just dequeued client
Definition: comm.cc:1372
struct StatCounters::@137 syscalls
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:1793
virtual bool applyQuota(int &nleft, Comm::IoCallback *state)
int quotaPeekFd() const
returns the next fd reservation
Definition: comm.cc:1328
void checkTimeouts(void)
Definition: comm.cc:1594
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:1417
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:1822
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:808
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
virtual void onFdClosed()
Performs cleanup when the related file descriptor becomes closed.
static bool AlreadyTimedOut(fde *F)
Definition: comm.cc:1567
#define IPV6_SPECIAL_SPLITSTACK
Definition: tools.h:22
#define SQUID_NONBLOCK
bool setIPv4()
Definition: Address.cc:224
void commSetCloseOnExec(int fd)
Definition: comm.cc:1159
@ 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:1344
static BandwidthBucket * SelectBucket(fde *f)
int ins
number of enqueue calls, used to generate a "reservation" ID
Definition: ClientInfo.h:126
@ COMM_ERROR
Definition: Flag.h:17
void dequeue()
removes queue head
Definition: comm.cc:1491
#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:1291
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:1877
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:1313
int ignoreErrno(int ierrno)
Definition: comm.cc:1507
#define TRUE
Definition: std-includes.h:55
FD event (FDECB) dialer.
Definition: CommCalls.h:291
#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:1198
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.
const typedef int * const_iterator
Definition: DescriptorSet.h:22
~CommQuotaQueue()
Definition: comm.cc:1473
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:1359
struct addrinfo * ai_next
double bucketLevel
how much can be written now
unsigned int quotaPeekReserv() const
returns the next reserv. to pop
Definition: comm.cc:1336
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:1871
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
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:1069
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:1657
struct SquidConfig::@98 Timeout
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:1054
AsyncCall::Pointer & Next()
Definition: AsyncCall.h:67
void commStartHalfClosedMonitor(int fd)
Start waiting for a possibly half-closed connection to close.
Definition: comm.cc:1646
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:1910
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
bool firstTimeConnection
is this first time connection for this client
Definition: ClientInfo.h:66
const A & min(A const &lhs, A const &rhs)
#define COMM_SELECT_WRITE
Definition: defines.h:37
void old_comm_reset_close(int fd)
Definition: comm.cc:823
@ FD_FILE
Definition: enums.h:15
void comm_remove_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:1010
#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:843
#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
bool canceled()
Definition: AsyncCall.h:54
@ SHUTDOWN
Definition: Flag.h:20

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors