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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors