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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors