]> git.ipfire.org Git - thirdparty/squid.git/blame - src/comm.cc
Bug 5428: Warn if pkg-config is not found (#1902)
[thirdparty/squid.git] / src / comm.cc
CommitLineData
30a4f2a8 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
30a4f2a8 3 *
bbc27441
AJ
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.
30a4f2a8 7 */
090089c4 8
bbc27441
AJ
9/* DEBUG: section 05 Socket Functions */
10
582c2af2 11#include "squid.h"
2949dd02 12#include "base/AsyncFunCalls.h"
582c2af2 13#include "ClientInfo.h"
04f55905
AJ
14#include "comm/AcceptLimiter.h"
15#include "comm/comm_internal.h"
cfd66529 16#include "comm/Connection.h"
ec41b64c 17#include "comm/IoCallback.h"
d841c88d 18#include "comm/Loops.h"
7e66d5e2 19#include "comm/Read.h"
cbff89ba 20#include "comm/TcpAcceptor.h"
602d9612 21#include "comm/Write.h"
582c2af2 22#include "compat/cmsg.h"
74257126 23#include "DescriptorSet.h"
582c2af2 24#include "event.h"
c4ad1349 25#include "fd.h"
582c2af2
FC
26#include "fde.h"
27#include "globals.h"
9b5c4a9a 28#include "icmp/net_db.h"
96d89ea0 29#include "ip/Intercept.h"
425de4c8 30#include "ip/QosConfig.h"
055421ee 31#include "ip/tools.h"
582c2af2 32#include "pconn.h"
65e41a45 33#include "sbuf/SBuf.h"
7d17a6a6 34#include "sbuf/Stream.h"
4d5904f7 35#include "SquidConfig.h"
e4f1fdae 36#include "StatCounters.h"
582c2af2 37#include "StoreIOBuffer.h"
1125ea7b
MM
38#include "tools.h"
39
cb4f4424 40#if USE_OPENSSL
4db984be
CT
41#include "ssl/support.h"
42#endif
090089c4 43
074d6a40
AJ
44#include <cerrno>
45#include <cmath>
be266cb2 46#if _SQUID_CYGWIN_
b671cc68 47#include <sys/ioctl.h>
48#endif
30a4f2a8 49#ifdef HAVE_NETINET_TCP_H
50#include <netinet/tcp.h>
51#endif
582c2af2
FC
52#if HAVE_SYS_UN_H
53#include <sys/un.h>
54#endif
090089c4 55
2b663917 56/*
57 * New C-like simple comm code. This stuff is a mess and doesn't really buy us anything.
58 */
59
82ec8dfc 60static IOCB commHalfClosedReader;
74d7d196 61static int comm_openex(int sock_type, int proto, Ip::Address &, int flags, const char *note);
c6f168c1 62static void comm_init_opened(const Comm::ConnectionPointer &conn, const char *note, struct addrinfo *AI);
a67d2b2e 63static int comm_apply_flags(int new_socket, Ip::Address &addr, int flags, struct addrinfo *AI);
82ec8dfc 64
9a0a18de 65#if USE_DELAY_POOLS
b4cd430a
CT
66CBDATA_CLASS_INIT(CommQuotaQueue);
67
68static void commHandleWriteHelper(void * data);
69#endif
70
090089c4 71/* STATIC */
62e76326 72
aee3523a 73static DescriptorSet *TheHalfClosed = nullptr; /// the set of half-closed FDs
74257126
AR
74static bool WillCheckHalfClosed = false; /// true if check is scheduled
75static EVH commHalfClosedCheck;
76static void commPlanHalfClosedCheck();
77
c8407295 78static Comm::Flag commBind(int s, struct addrinfo &);
74d7d196 79static void commSetBindAddressNoPort(int);
f5b8bbc4 80static void commSetReuseAddr(int);
81static void commSetNoLinger(int);
30a4f2a8 82#ifdef TCP_NODELAY
f5b8bbc4 83static void commSetTcpNoDelay(int);
30a4f2a8 84#endif
f5b8bbc4 85static void commSetTcpRcvbuf(int, int);
723123a9 86
04f55905 87bool
b0469965 88isOpen(const int fd)
b300c36d 89{
9a5ac93d 90 return fd >= 0 && fd_table && fd_table[fd].flags.open != 0;
b300c36d 91}
92
e1a88700 93/**
c4b7a5a9 94 * Empty the read buffers
95 *
96 * This is a magical routine that empties the read buffers.
97 * Under some platforms (Linux) if a buffer has data in it before
98 * you call close(), the socket will hang and take quite a while
99 * to timeout.
100 */
101static void
102comm_empty_os_read_buffers(int fd)
103{
1191b93b 104#if _SQUID_LINUX_
b41a5a1b
AJ
105#if USE_OPENSSL
106 // Bug 4146: SSL-Bump BIO does not release sockets on close.
107 if (fd_table[fd].ssl)
108 return;
109#endif
110
c4b7a5a9 111 /* prevent those nasty RST packets */
112 char buf[SQUID_TCP_SO_RCVBUF];
e3c37fc2 113 if (fd_table[fd].flags.nonblocking && fd_table[fd].type != FD_MSGHDR) {
cc192b50 114 while (FD_READ_METHOD(fd, buf, SQUID_TCP_SO_RCVBUF) > 0) {};
115 }
2081cefa
FC
116#else
117 (void)fd;
c4b7a5a9 118#endif
119}
120
e1a88700 121/**
ce767c23 122 * synchronous wrapper around udp socket functions
123 */
ce767c23 124int
b7ac5457 125comm_udp_recvfrom(int fd, void *buf, size_t len, int flags, Ip::Address &from)
ce767c23 126{
95dc7ff4 127 ++ statCounter.syscalls.sock.recvfroms;
cc192b50 128 debugs(5,8, "comm_udp_recvfrom: FD " << fd << " from " << from);
aee3523a 129 struct addrinfo *AI = nullptr;
851614a8 130 Ip::Address::InitAddr(AI);
4dd643d5 131 int x = recvfrom(fd, buf, len, flags, AI->ai_addr, &AI->ai_addrlen);
cc192b50 132 from = *AI;
851614a8 133 Ip::Address::FreeAddr(AI);
cc192b50 134 return x;
ce767c23 135}
136
365f12a9 137int
7d21986b 138comm_udp_recv(int fd, void *buf, size_t len, int flags)
365f12a9 139{
b7ac5457 140 Ip::Address nul;
cc192b50 141 return comm_udp_recvfrom(fd, buf, len, flags, nul);
365f12a9 142}
143
f71da12c 144ssize_t
7d21986b 145comm_udp_send(int s, const void *buf, size_t len, int flags)
f71da12c 146{
62e76326 147 return send(s, buf, len, flags);
f71da12c 148}
ce767c23 149
545d554b 150bool
151comm_has_incomplete_write(int fd)
152{
aee3523a 153 assert(isOpen(fd) && COMMIO_FD_WRITECB(fd) != nullptr);
b0469965 154 return COMMIO_FD_WRITECB(fd)->active();
d4cb310b 155}
156
e1a88700 157/**
cf3c0ee3 158 * Queue a write. handler/handler_data are called when the write fully
159 * completes, on error, or on file descriptor close.
160 */
9864ee44 161
090089c4 162/* Return the local port associated with fd. */
f45dd259 163unsigned short
b8d8561b 164comm_local_port(int fd)
090089c4 165{
b7ac5457 166 Ip::Address temp;
aee3523a 167 struct addrinfo *addr = nullptr;
76f87348 168 fde *F = &fd_table[fd];
090089c4 169
090089c4 170 /* If the fd is closed already, just return */
62e76326 171
60c0b5a2 172 if (!F->flags.open) {
bf8fe701 173 debugs(5, 0, "comm_local_port: FD " << fd << " has been closed.");
62e76326 174 return 0;
090089c4 175 }
62e76326 176
4dd643d5
AJ
177 if (F->local_addr.port())
178 return F->local_addr.port();
62e76326 179
6084c0b6 180 if (F->sock_family == AF_INET)
4dd643d5 181 temp.setIPv4();
6084c0b6 182
851614a8 183 Ip::Address::InitAddr(addr);
62e76326 184
cc192b50 185 if (getsockname(fd, addr->ai_addr, &(addr->ai_addrlen)) ) {
b69e9ffa 186 int xerrno = errno;
d816f28d 187 debugs(50, DBG_IMPORTANT, "ERROR: " << MYNAME << "Failed to retrieve TCP/UDP port number for socket: FD " << fd << ": " << xstrerr(xerrno));
851614a8 188 Ip::Address::FreeAddr(addr);
62e76326 189 return 0;
090089c4 190 }
cc192b50 191 temp = *addr;
192
851614a8 193 Ip::Address::FreeAddr(addr);
cc192b50 194
4dd643d5 195 if (F->local_addr.isAnyAddr()) {
3d031c35
AJ
196 /* save the whole local address, not just the port. */
197 F->local_addr = temp;
198 } else {
4dd643d5 199 F->local_addr.port(temp.port());
3d031c35 200 }
62e76326 201
4dd643d5
AJ
202 debugs(5, 6, "comm_local_port: FD " << fd << ": port " << F->local_addr.port() << "(family=" << F->sock_family << ")");
203 return F->local_addr.port();
090089c4 204}
205
74d7d196 206/// sets the IP_BIND_ADDRESS_NO_PORT socket option to optimize ephemeral port
207/// reuse by outgoing TCP connections that must bind(2) to a source IP address
208static void
209commSetBindAddressNoPort(const int fd)
210{
211#if defined(IP_BIND_ADDRESS_NO_PORT)
212 int flag = 1;
213 if (setsockopt(fd, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, reinterpret_cast<char*>(&flag), sizeof(flag)) < 0) {
214 const auto savedErrno = errno;
215 debugs(50, DBG_IMPORTANT, "ERROR: setsockopt(IP_BIND_ADDRESS_NO_PORT) failure: " << xstrerr(savedErrno));
216 }
217#else
218 (void)fd;
219#endif
220}
221
c8407295 222static Comm::Flag
cc192b50 223commBind(int s, struct addrinfo &inaddr)
090089c4 224{
95dc7ff4 225 ++ statCounter.syscalls.sock.binds;
62e76326 226
ac760b5e 227 if (bind(s, inaddr.ai_addr, inaddr.ai_addrlen) == 0) {
b69e9ffa 228 debugs(50, 6, "bind socket FD " << s << " to " << fd_table[s].local_addr);
c8407295 229 return Comm::OK;
ac760b5e 230 }
b69e9ffa 231 int xerrno = errno;
d816f28d 232 debugs(50, DBG_CRITICAL, "ERROR: " << MYNAME << "Cannot bind socket FD " << s << " to " << fd_table[s].local_addr << ": " << xstrerr(xerrno));
62e76326 233
4ee57cbe 234 return Comm::COMM_ERROR;
090089c4 235}
236
e1a88700 237/**
238 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
239 * is OR of flags specified in comm.h. Defaults TOS
240 */
b8d8561b 241int
16b204c4 242comm_open(int sock_type,
62e76326 243 int proto,
b7ac5457 244 Ip::Address &addr,
62e76326 245 int flags,
246 const char *note)
d6827718 247{
74d7d196 248 // assume zero-port callers do not need to know the assigned port right away
249 if (sock_type == SOCK_STREAM && addr.port() == 0 && ((flags & COMM_DOBIND) || !addr.isAnyAddr()))
250 flags |= COMM_DOBIND_PORT_LATER;
251
c6f168c1 252 return comm_openex(sock_type, proto, addr, flags, note);
d6827718 253}
254
e0d28505
AJ
255void
256comm_open_listener(int sock_type,
257 int proto,
e02ed2e3 258 Comm::ConnectionPointer &conn,
e0d28505
AJ
259 const char *note)
260{
261 /* all listener sockets require bind() */
262 conn->flags |= COMM_DOBIND;
263
264 /* attempt native enabled port. */
c6f168c1 265 conn->fd = comm_openex(sock_type, proto, conn->local, conn->flags, note);
e0d28505
AJ
266}
267
31be869c
AJ
268int
269comm_open_listener(int sock_type,
04f7fd38 270 int proto,
b7ac5457 271 Ip::Address &addr,
04f7fd38
AJ
272 int flags,
273 const char *note)
31be869c 274{
e02ed2e3
AJ
275 int sock = -1;
276
ac760b5e
AJ
277 /* all listener sockets require bind() */
278 flags |= COMM_DOBIND;
279
e02ed2e3 280 /* attempt native enabled port. */
c6f168c1 281 sock = comm_openex(sock_type, proto, addr, flags, note);
e02ed2e3 282
31be869c
AJ
283 return sock;
284}
285
2d8c0b1a 286static bool
287limitError(int const anErrno)
288{
289 return anErrno == ENFILE || anErrno == EMFILE;
290}
d6827718 291
8b082ed9 292static void
cc192b50 293comm_set_v6only(int fd, int tos)
294{
295#ifdef IPV6_V6ONLY
296 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &tos, sizeof(int)) < 0) {
b69e9ffa
AJ
297 int xerrno = errno;
298 debugs(50, DBG_IMPORTANT, MYNAME << "setsockopt(IPV6_V6ONLY) " << (tos?"ON":"OFF") << " for FD " << fd << ": " << xstrerr(xerrno));
cc192b50 299 }
300#else
b69e9ffa 301 debugs(50, DBG_CRITICAL, MYNAME << "WARNING: setsockopt(IPV6_V6ONLY) not supported on this platform");
cc192b50 302#endif /* sockopt */
303}
057f5854 304
40d6264d 305/**
b2192042
AJ
306 * Set the socket option required for TPROXY spoofing for:
307 * - Linux TPROXY v4 support,
308 * - OpenBSD divert-to support,
309 * - FreeBSD IPFW TPROXY v4 support.
40d6264d 310 */
8b082ed9 311static void
e950e673 312comm_set_transparent(int fd)
f1e0717c 313{
b2192042
AJ
314#if _SQUID_LINUX_ && defined(IP_TRANSPARENT) // Linux
315# define soLevel SOL_IP
316# define soFlag IP_TRANSPARENT
d0e6f578 317 bool doneSuid = false;
b2192042
AJ
318
319#elif defined(SO_BINDANY) // OpenBSD 4.7+ and NetBSD with PF
320# define soLevel SOL_SOCKET
321# define soFlag SO_BINDANY
322 enter_suid();
d0e6f578 323 bool doneSuid = true;
1125ea7b 324
b2192042
AJ
325#elif defined(IP_BINDANY) // FreeBSD with IPFW
326# define soLevel IPPROTO_IP
327# define soFlag IP_BINDANY
1125ea7b 328 enter_suid();
d0e6f578 329 bool doneSuid = true;
b2192042
AJ
330
331#else
332 debugs(50, DBG_CRITICAL, "WARNING: comm_open: setsockopt(TPROXY) not supported on this platform");
c46e7feb 333 (void)fd;
b2192042
AJ
334#endif /* sockopt */
335
336#if defined(soLevel) && defined(soFlag)
337 int tos = 1;
338 if (setsockopt(fd, soLevel, soFlag, (char *) &tos, sizeof(int)) < 0) {
b69e9ffa
AJ
339 int xerrno = errno;
340 debugs(50, DBG_IMPORTANT, MYNAME << "setsockopt(TPROXY) on FD " << fd << ": " << xstrerr(xerrno));
1125ea7b
MM
341 } else {
342 /* mark the socket as having transparent options */
343 fd_table[fd].flags.transparent = true;
344 }
b2192042
AJ
345 if (doneSuid)
346 leave_suid();
347#endif
f1e0717c
AJ
348}
349
e1a88700 350/**
351 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
352 * is OR of flags specified in defines.h:COMM_*
353 */
74d7d196 354static int
d6827718 355comm_openex(int sock_type,
62e76326 356 int proto,
e02ed2e3 357 Ip::Address &addr,
62e76326 358 int flags,
62e76326 359 const char *note)
090089c4 360{
e02ed2e3 361 int new_socket;
aee3523a 362 struct addrinfo *AI = nullptr;
090089c4 363
364 /* Create socket for accepting new connections. */
95dc7ff4 365 ++ statCounter.syscalls.sock.sockets;
62e76326 366
97bbba61
AR
367 if (!Ip::EnableIpv6 && addr.isIPv6()) {
368 debugs(50, 2, "refusing to open an IPv6 socket when IPv6 support is disabled: " << addr);
369 errno = ENOTSUP;
370 return -1;
371 }
372
cc192b50 373 /* Setup the socket addrinfo details for use */
e02ed2e3 374 addr.getAddrInfo(AI);
cc192b50 375 AI->ai_socktype = sock_type;
376 AI->ai_protocol = proto;
cc192b50 377
e02ed2e3 378 debugs(50, 3, "comm_openex: Attempt open socket for: " << addr );
cc192b50 379
e02ed2e3 380 new_socket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
c3d6a21b 381 const auto firstErrNo = errno;
055421ee 382
0eb08770
HN
383 /* under IPv6 there is the possibility IPv6 is present but disabled. */
384 /* try again as IPv4-native if possible */
e02ed2e3 385 if ( new_socket < 0 && Ip::EnableIpv6 && addr.isIPv6() && addr.setIPv4() ) {
0eb08770 386 /* attempt to open this IPv4-only. */
851614a8 387 Ip::Address::FreeAddr(AI);
0eb08770 388 /* Setup the socket addrinfo details for use */
e02ed2e3 389 addr.getAddrInfo(AI);
0eb08770
HN
390 AI->ai_socktype = sock_type;
391 AI->ai_protocol = proto;
b69e9ffa 392 debugs(50, 3, "Attempt fallback open socket for: " << addr );
e02ed2e3 393 new_socket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
c3d6a21b
AR
394 // TODO: Report failures of this second socket() call.
395 // if both socket() calls fail, we use firstErrNo
b69e9ffa 396 debugs(50, 2, "attempt open " << note << " socket on: " << addr);
0eb08770 397 }
0eb08770 398
e02ed2e3 399 if (new_socket < 0) {
62e76326 400 /* Increase the number of reserved fd's if calls to socket()
401 * are failing because the open file table is full. This
402 * limits the number of simultaneous clients */
403
c3d6a21b
AR
404 if (limitError(firstErrNo)) {
405 debugs(50, DBG_IMPORTANT, MYNAME << "socket failure: " << xstrerr(firstErrNo));
62e76326 406 fdAdjustReserved();
2d8c0b1a 407 } else {
c3d6a21b 408 debugs(50, DBG_CRITICAL, MYNAME << "socket failure: " << xstrerr(firstErrNo));
62e76326 409 }
410
851614a8 411 Ip::Address::FreeAddr(AI);
cc192b50 412
c3d6a21b 413 errno = firstErrNo; // restore for caller
62e76326 414 return -1;
090089c4 415 }
62e76326 416
e02ed2e3
AJ
417 // XXX: temporary for the transition. comm_openex will eventually have a conn to play with.
418 Comm::ConnectionPointer conn = new Comm::Connection;
419 conn->local = addr;
420 conn->fd = new_socket;
421
422 debugs(50, 3, "comm_openex: Opened socket " << conn << " : family=" << AI->ai_family << ", type=" << AI->ai_socktype << ", protocol=" << AI->ai_protocol );
cc192b50 423
e02ed2e3 424 if ( Ip::EnableIpv6&IPV6_SPECIAL_SPLITSTACK && addr.isIPv6() )
b5523edc 425 comm_set_v6only(conn->fd, 1);
62e76326 426
cc192b50 427 /* Windows Vista supports Dual-Sockets. BUT defaults them to V6ONLY. Turn it OFF. */
9b1f7ee8 428 /* Other OS may have this administratively disabled for general use. Same deal. */
e02ed2e3 429 if ( Ip::EnableIpv6&IPV6_SPECIAL_V4MAPPING && addr.isIPv6() )
b5523edc 430 comm_set_v6only(conn->fd, 0);
cc192b50 431
c6f168c1 432 comm_init_opened(conn, note, AI);
e02ed2e3 433 new_socket = comm_apply_flags(conn->fd, addr, flags, AI);
38344a8e 434
851614a8 435 Ip::Address::FreeAddr(AI);
38344a8e 436
e02ed2e3
AJ
437 // XXX transition only. prevent conn from closing the new FD on function exit.
438 conn->fd = -1;
c3d6a21b
AR
439 // XXX: firstErrNo is not applicable here -- socket() calls succeeded above!
440 // TODO: Stop reporting error codes via errno.
441 errno = firstErrNo;
e02ed2e3 442 return new_socket;
38344a8e
AR
443}
444
445/// update FD tables after a local or remote (IPC) comm_openex();
446void
e0d28505 447comm_init_opened(const Comm::ConnectionPointer &conn,
5667a628
AR
448 const char *note,
449 struct addrinfo *AI)
38344a8e 450{
e0d28505 451 assert(Comm::IsConnOpen(conn));
38344a8e
AR
452 assert(AI);
453
090089c4 454 /* update fdstat */
bf95c10a 455 debugs(5, 5, conn << " is a new socket");
62e76326 456
e0d28505
AJ
457 assert(!isOpen(conn->fd)); // NP: global isOpen checks the fde entry for openness not the Comm::Connection
458 fd_open(conn->fd, FD_SOCKET, note);
62e76326 459
e0d28505
AJ
460 fde *F = &fd_table[conn->fd];
461 F->local_addr = conn->local;
cc192b50 462
463 F->sock_family = AI->ai_family;
38344a8e
AR
464}
465
466/// apply flags after a local comm_open*() call;
467/// returns new_socket or -1 on error
468static int
469comm_apply_flags(int new_socket,
a67d2b2e 470 Ip::Address &addr,
5667a628
AR
471 int flags,
472 struct addrinfo *AI)
38344a8e
AR
473{
474 assert(new_socket >= 0);
475 assert(AI);
476 const int sock_type = AI->ai_socktype;
62e76326 477
79a15e0a 478 if (!(flags & COMM_NOCLOEXEC))
62e76326 479 commSetCloseOnExec(new_socket);
480
cdc33f35 481 if ((flags & COMM_REUSEADDR))
62e76326 482 commSetReuseAddr(new_socket);
483
4dd643d5 484 if (addr.port() > (unsigned short) 0) {
7aa9bb3e 485#if _SQUID_WINDOWS_
a50bfe93 486 if (sock_type != SOCK_DGRAM)
487#endif
a50bfe93 488 commSetNoLinger(new_socket);
62e76326 489
490 if (opt_reuseaddr)
491 commSetReuseAddr(new_socket);
090089c4 492 }
62e76326 493
a35595cd 494 /* MUST be done before binding or face OS Error: "(99) Cannot assign requested address"... */
9e008dda 495 if ((flags & COMM_TRANSPARENT)) {
a35595cd
AJ
496 comm_set_transparent(new_socket);
497 }
a35595cd 498
4dd643d5
AJ
499 if ( (flags & COMM_DOBIND) || addr.port() > 0 || !addr.isAnyAddr() ) {
500 if ( !(flags & COMM_DOBIND) && addr.isAnyAddr() )
e0236918 501 debugs(5, DBG_IMPORTANT,"WARNING: Squid is attempting to bind() port " << addr << " without being a listener.");
4dd643d5 502 if ( addr.isNoAddr() )
d816f28d 503 debugs(5, DBG_CRITICAL, "ERROR: Squid is attempting to bind() port " << addr << "!!");
ac760b5e 504
7d17a6a6
EB
505#if defined(SO_REUSEPORT)
506 if (flags & COMM_REUSEPORT) {
507 int on = 1;
508 if (setsockopt(new_socket, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<char*>(&on), sizeof(on)) < 0) {
509 const auto savedErrno = errno;
510 const auto errorMessage = ToSBuf("cannot enable SO_REUSEPORT socket option when binding to ",
511 addr, ": ", xstrerr(savedErrno));
512 if (reconfiguring)
513 debugs(5, DBG_IMPORTANT, "ERROR: " << errorMessage);
514 else
515 throw TexcHere(errorMessage);
516 }
517 }
518#endif
74d7d196 519
520 if ((flags & COMM_DOBIND_PORT_LATER))
521 commSetBindAddressNoPort(new_socket);
522
c8407295 523 if (commBind(new_socket, *AI) != Comm::OK) {
62e76326 524 comm_close(new_socket);
525 return -1;
62e76326 526 }
23ff6968 527 }
62e76326 528
79a15e0a 529 if (flags & COMM_NONBLOCKING)
4ee57cbe 530 if (commSetNonBlocking(new_socket) == Comm::COMM_ERROR) {
38344a8e 531 comm_close(new_socket);
62e76326 532 return -1;
62e76326 533 }
534
30a4f2a8 535#ifdef TCP_NODELAY
536 if (sock_type == SOCK_STREAM)
62e76326 537 commSetTcpNoDelay(new_socket);
538
30a4f2a8 539#endif
62e76326 540
1241e63e 541 if (Config.tcpRcvBufsz > 0 && sock_type == SOCK_STREAM)
62e76326 542 commSetTcpRcvbuf(new_socket, Config.tcpRcvBufsz);
543
090089c4 544 return new_socket;
545}
546
a4c0f9c6 547void
e0d28505 548comm_import_opened(const Comm::ConnectionPointer &conn,
5667a628
AR
549 const char *note,
550 struct addrinfo *AI)
a4c0f9c6 551{
bf95c10a 552 debugs(5, 2, conn);
e0d28505 553 assert(Comm::IsConnOpen(conn));
a4c0f9c6
AR
554 assert(AI);
555
c6f168c1 556 comm_init_opened(conn, note, AI);
a4c0f9c6 557
4dd643d5 558 if (conn->local.port() > (unsigned short) 0) {
7aa9bb3e 559#if _SQUID_WINDOWS_
e0d28505 560 if (AI->ai_socktype != SOCK_DGRAM)
a4c0f9c6 561#endif
be4d35dc 562 fd_table[conn->fd].flags.nolinger = true;
a4c0f9c6
AR
563 }
564
e0d28505 565 if ((conn->flags & COMM_TRANSPARENT))
be4d35dc 566 fd_table[conn->fd].flags.transparent = true;
a4c0f9c6 567
e0d28505 568 if (conn->flags & COMM_NONBLOCKING)
be4d35dc 569 fd_table[conn->fd].flags.nonblocking = true;
a4c0f9c6
AR
570
571#ifdef TCP_NODELAY
572 if (AI->ai_socktype == SOCK_STREAM)
be4d35dc 573 fd_table[conn->fd].flags.nodelay = true;
a4c0f9c6
AR
574#endif
575
576 /* no fd_table[fd].flags. updates needed for these conditions:
577 * if ((flags & COMM_REUSEADDR)) ...
578 * if ((flags & COMM_DOBIND) ...) ...
579 */
580}
581
933dd095
AJ
582// XXX: now that raw-FD timeouts are only unset for pipes and files this SHOULD be a no-op.
583// With handler already unset. Leaving this present until that can be verified for all code paths.
584void
585commUnsetFdTimeout(int fd)
b0469965 586{
bf95c10a 587 debugs(5, 3, "Remove timeout for FD " << fd);
b0469965 588 assert(fd >= 0);
589 assert(fd < Squid_MaxFD);
590 fde *F = &fd_table[fd];
591 assert(F->flags.open);
592
aee3523a 593 F->timeoutHandler = nullptr;
933dd095 594 F->timeout = 0;
7957e704
AJ
595}
596
597int
4650a4fa 598commSetConnTimeout(const Comm::ConnectionPointer &conn, time_t timeout, AsyncCall::Pointer &callback)
7957e704 599{
bf95c10a 600 debugs(5, 3, conn << " timeout " << timeout);
7957e704
AJ
601 assert(Comm::IsConnOpen(conn));
602 assert(conn->fd < Squid_MaxFD);
603 fde *F = &fd_table[conn->fd];
604 assert(F->flags.open);
605
606 if (timeout < 0) {
aee3523a 607 F->timeoutHandler = nullptr;
7957e704
AJ
608 F->timeout = 0;
609 } else {
aee3523a 610 if (callback != nullptr) {
7957e704
AJ
611 typedef CommTimeoutCbParams Params;
612 Params &params = GetCommParams<Params>(callback);
613 params.conn = conn;
614 F->timeoutHandler = callback;
615 }
b0469965 616
7957e704
AJ
617 F->timeout = squid_curtime + (time_t) timeout;
618 }
619
620 return F->timeout;
b0469965 621}
090089c4 622
8d77a37c
AJ
623int
624commUnsetConnTimeout(const Comm::ConnectionPointer &conn)
625{
bf95c10a 626 debugs(5, 3, "Remove timeout for " << conn);
8d77a37c
AJ
627 AsyncCall::Pointer nil;
628 return commSetConnTimeout(conn, -1, nil);
629}
630
8630961c
AD
631/**
632 * Connect socket FD to given remote address.
633 * If return value is an error flag (COMM_ERROR, ERR_CONNECT, ERR_PROTOCOL, etc.),
634 * then error code will also be returned in errno.
635 */
b8d8561b 636int
b7ac5457 637comm_connect_addr(int sock, const Ip::Address &address)
090089c4 638{
c8407295 639 Comm::Flag status = Comm::OK;
76f87348 640 fde *F = &fd_table[sock];
cc192b50 641 int x = 0;
b5568a61 642 int err = 0;
9689d97c 643 socklen_t errlen;
aee3523a 644 struct addrinfo *AI = nullptr;
cc192b50 645
4dd643d5 646 assert(address.port() != 0);
cc192b50 647
bf95c10a 648 debugs(5, 9, "connecting socket FD " << sock << " to " << address << " (want family: " << F->sock_family << ")");
cc192b50 649
3d98ff81 650 /* Handle IPv6 over IPv4-only socket case.
4dd643d5 651 * this case must presently be handled here since the getAddrInfo asserts on bad mappings.
3d98ff81 652 * NP: because commResetFD is private to ConnStateData we have to return an error and
9d92af86
AJ
653 * trust its handled properly.
654 */
4dd643d5 655 if (F->sock_family == AF_INET && !address.isIPv4()) {
3d98ff81 656 errno = ENETUNREACH;
c8407295 657 return Comm::ERR_PROTOCOL;
3d98ff81
HN
658 }
659
660 /* Handle IPv4 over IPv6-only socket case.
661 * This case is presently handled here as it's both a known case and it's
662 * uncertain what error will be returned by the IPv6 stack in such case. It's
663 * possible this will also be handled by the errno checks below after connect()
2f8abb64 664 * but needs careful cross-platform verification, and verifying the address
3d98ff81
HN
665 * condition here is simple.
666 */
4dd643d5 667 if (!F->local_addr.isIPv4() && address.isIPv4()) {
3d98ff81 668 errno = ENETUNREACH;
c8407295 669 return Comm::ERR_PROTOCOL;
9d92af86 670 }
9d92af86 671
4dd643d5 672 address.getAddrInfo(AI, F->sock_family);
cc192b50 673
090089c4 674 /* Establish connection. */
8630961c 675 int xerrno = 0;
62e76326 676
9e008dda 677 if (!F->flags.called_connect) {
be4d35dc 678 F->flags.called_connect = true;
95dc7ff4 679 ++ statCounter.syscalls.sock.connects;
62e76326 680
07383e11
CT
681 errno = 0;
682 if ((x = connect(sock, AI->ai_addr, AI->ai_addrlen)) < 0) {
683 xerrno = errno;
684 debugs(5,5, "sock=" << sock << ", addrinfo(" <<
9e008dda
AJ
685 " flags=" << AI->ai_flags <<
686 ", family=" << AI->ai_family <<
687 ", socktype=" << AI->ai_socktype <<
688 ", protocol=" << AI->ai_protocol <<
689 ", &addr=" << AI->ai_addr <<
07383e11 690 ", addrlen=" << AI->ai_addrlen << " )");
8630961c 691 debugs(5, 9, "connect FD " << sock << ": (" << x << ") " << xstrerr(xerrno));
07383e11
CT
692 debugs(14,9, "connecting to: " << address);
693
694 } else if (x == 0) {
695 // XXX: ICAP code refuses callbacks during a pending comm_ call
696 // Async calls development will fix this.
697 x = -1;
698 xerrno = EINPROGRESS;
cc192b50 699 }
8630961c 700
9e008dda 701 } else {
8630961c 702 errno = 0;
8a09e810 703#if _SQUID_NEWSOS6_
62e76326 704 /* Makoto MATSUSHITA <matusita@ics.es.osaka-u.ac.jp> */
8630961c
AD
705 if (connect(sock, AI->ai_addr, AI->ai_addrlen) < 0)
706 xerrno = errno;
62e76326 707
8630961c 708 if (xerrno == EINVAL) {
62e76326 709 errlen = sizeof(err);
710 x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen);
62e76326 711 if (x >= 0)
8630961c 712 xerrno = x;
62e76326 713 }
33ac9442 714#else
62e76326 715 errlen = sizeof(err);
62e76326 716 x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen);
62e76326 717 if (x == 0)
8630961c 718 xerrno = err;
62e76326 719
8a09e810 720#if _SQUID_SOLARIS_
62e76326 721 /*
722 * Solaris 2.4's socket emulation doesn't allow you
723 * to determine the error from a failed non-blocking
724 * connect and just returns EPIPE. Create a fake
725 * error message for connect. -- fenner@parc.xerox.com
726 */
8630961c
AD
727 if (x < 0 && xerrno == EPIPE)
728 xerrno = ENOTCONN;
729 else
730 xerrno = errno;
33ac9442 731#endif
30a4f2a8 732#endif
e5f6c5c2 733 }
62e76326 734
851614a8 735 Ip::Address::FreeAddr(AI);
feca3b9a 736
8630961c
AD
737 errno = xerrno;
738 if (xerrno == 0 || xerrno == EISCONN)
c8407295 739 status = Comm::OK;
8630961c 740 else if (ignoreErrno(xerrno))
c8407295 741 status = Comm::INPROGRESS;
8630961c 742 else if (xerrno == EAFNOSUPPORT || xerrno == EINVAL)
c8407295 743 return Comm::ERR_PROTOCOL;
b5568a61 744 else
4ee57cbe 745 return Comm::COMM_ERROR;
62e76326 746
4dd643d5 747 address.toStr(F->ipaddr, MAX_IPSTRLEN);
62e76326 748
4dd643d5 749 F->remote_port = address.port(); /* remote_port is HS */
62e76326 750
c8407295 751 if (status == Comm::OK) {
e0236918 752 debugs(5, DBG_DATA, "comm_connect_addr: FD " << sock << " connected to " << address);
c8407295 753 } else if (status == Comm::INPROGRESS) {
e0236918 754 debugs(5, DBG_DATA, "comm_connect_addr: FD " << sock << " connection pending");
090089c4 755 }
62e76326 756
8630961c 757 errno = xerrno;
090089c4 758 return status;
759}
760
cb201b7e 761void
762commCallCloseHandlers(int fd)
763{
76f87348 764 fde *F = &fd_table[fd];
bf8fe701 765 debugs(5, 5, "commCallCloseHandlers: FD " << fd);
62e76326 766
aee3523a 767 while (F->closeHandler != nullptr) {
b0469965 768 AsyncCall::Pointer call = F->closeHandler;
9e008dda 769 F->closeHandler = call->Next();
aee3523a 770 call->setNext(nullptr);
9e008dda
AJ
771 // If call is not canceled schedule it for execution else ignore it
772 if (!call->canceled()) {
773 debugs(5, 5, "commCallCloseHandlers: ch->handler=" << call);
2b6b1bcb
AR
774 // XXX: Without the following code, callback fd may be -1.
775 // typedef CommCloseCbParams Params;
776 // auto &params = GetCommParams<Params>(call);
777 // params.fd = fd;
9e008dda
AJ
778 ScheduleCallHere(call);
779 }
cb201b7e 780 }
781}
782
27774cee 783/**
98264874 784 * enable linger with time of 0 so that when the socket is
785 * closed, TCP generates a RESET
786 */
787void
8ace824c 788comm_reset_close(const Comm::ConnectionPointer &conn)
98264874 789{
5c336a3b
AJ
790 struct linger L;
791 L.l_onoff = 1;
792 L.l_linger = 0;
62e76326 793
b69e9ffa
AJ
794 if (setsockopt(conn->fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0) {
795 int xerrno = errno;
796 debugs(50, DBG_CRITICAL, "ERROR: Closing " << conn << " with TCP RST: " << xstrerr(xerrno));
797 }
5c336a3b
AJ
798 conn->close();
799}
800
801// Legacy close function.
802void
803old_comm_reset_close(int fd)
804{
98264874 805 struct linger L;
806 L.l_onoff = 1;
807 L.l_linger = 0;
62e76326 808
b69e9ffa
AJ
809 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0) {
810 int xerrno = errno;
811 debugs(50, DBG_CRITICAL, "ERROR: Closing FD " << fd << " with TCP RST: " << xstrerr(xerrno));
812 }
98264874 813 comm_close(fd);
814}
815
8b082ed9 816static void
2949dd02 817commStartTlsClose(const int fd)
10b06767 818{
2949dd02 819 Security::SessionSendGoodbye(fd_table[fd].ssl);
10b06767
AJ
820}
821
8b082ed9 822static void
2949dd02 823comm_close_complete(const int fd)
2d8c0b1a 824{
2949dd02 825 auto F = &fd_table[fd];
1ca8bbfb 826 F->ssl.reset();
0476ec45 827 F->dynamicTlsContext.reset();
2949dd02
EB
828 fd_close(fd); /* update fdstat */
829 close(fd);
b0469965 830
95dc7ff4 831 ++ statCounter.syscalls.sock.closes;
b0469965 832
575d05c4 833 /* When one connection closes, give accept() a chance, if need be */
ccfbe8f4 834 CodeContext::Reset(); // exit FD-specific context
04f55905 835 Comm::AcceptLimiter::Instance().kick();
2d8c0b1a 836}
c4b7a5a9 837
838/*
839 * Close the socket fd.
840 *
841 * + call write handlers with ERR_CLOSING
842 * + call read handlers with ERR_CLOSING
843 * + call closing handlers
a46d2c0e 844 *
a928fdfd
EB
845 * A deferred reader has no Comm read handler mentioned above. To stay in sync,
846 * such a reader must register a Comm closing handler.
c4b7a5a9 847 */
b8d8561b 848void
43ae1d95 849_comm_close(int fd, char const *file, int line)
090089c4 850{
4a5d9ea5 851 debugs(5, 3, "start closing FD " << fd << " by " << file << ":" << line);
03eb2f01 852 assert(fd >= 0);
853 assert(fd < Squid_MaxFD);
82ec8dfc
AR
854
855 fde *F = &fd_table[fd];
1f7c9178 856
82ec8dfc 857 if (F->closing())
62e76326 858 return;
859
36afac26 860 /* XXX: is this obsolete behind F->closing() ? */
b8869bcf 861 if ( (shutting_down || reconfiguring) && (!F->flags.open || F->type == FD_FILE))
62e76326 862 return;
863
c4b7a5a9 864 /* The following fails because ipc.c is doing calls to pipe() to create sockets! */
c7e637d7 865 if (!isOpen(fd)) {
d816f28d 866 debugs(50, DBG_IMPORTANT, "ERROR: Squid BUG #3556: FD " << fd << " is not an open socket.");
c7e637d7
AJ
867 // XXX: do we need to run close(fd) or fd_close(fd) here?
868 return;
869 }
62e76326 870
76f87348 871 assert(F->type != FD_FILE);
62e76326 872
be4d35dc 873 F->flags.close_request = true;
62e76326 874
ccfbe8f4
AR
875 // We have caller's context and fde::codeContext. In the unlikely event they
876 // differ, it is not clear which context is more applicable to this closure.
877 // For simplicity sake, we remain in the caller's context while still
878 // allowing individual advanced callbacks to overwrite it.
879
575d05c4 880 if (F->ssl) {
2949dd02
EB
881 const auto startCall = asyncCall(5, 4, "commStartTlsClose",
882 callDialer(commStartTlsClose, fd));
575d05c4
AJ
883 ScheduleCallHere(startCall);
884 }
62e76326 885
74257126
AR
886 // a half-closed fd may lack a reader, so we stop monitoring explicitly
887 if (commHasHalfClosedMonitor(fd))
888 commStopHalfClosedMonitor(fd);
933dd095 889 commUnsetFdTimeout(fd);
62e76326 890
a6351f16 891 // notify read/write handlers after canceling select reservations, if any
ec41b64c 892 if (COMMIO_FD_WRITECB(fd)->active()) {
aee3523a 893 Comm::SetSelect(fd, COMM_SELECT_WRITE, nullptr, nullptr, 0);
c3d6a21b 894 COMMIO_FD_WRITECB(fd)->finish(Comm::ERR_CLOSING, 0);
2b663917 895 }
ec41b64c 896 if (COMMIO_FD_READCB(fd)->active()) {
aee3523a 897 Comm::SetSelect(fd, COMM_SELECT_READ, nullptr, nullptr, 0);
c3d6a21b 898 COMMIO_FD_READCB(fd)->finish(Comm::ERR_CLOSING, 0);
2b663917 899 }
2d8c0b1a 900
9a0a18de 901#if USE_DELAY_POOLS
b27668ec
EB
902 if (BandwidthBucket *bucket = BandwidthBucket::SelectBucket(F)) {
903 if (bucket->selectWaiting)
904 bucket->onFdClosed();
f33d34a8 905 }
b4cd430a
CT
906#endif
907
cb201b7e 908 commCallCloseHandlers(fd);
62e76326 909
a7ad6e4e 910 comm_empty_os_read_buffers(fd);
9e008dda 911
9e008dda 912 // must use async call to wait for all callbacks
82ec8dfc 913 // scheduled before comm_close() to finish
2949dd02
EB
914 const auto completeCall = asyncCall(5, 4, "comm_close_complete",
915 callDialer(comm_close_complete, fd));
10b06767 916 ScheduleCallHere(completeCall);
090089c4 917}
918
090089c4 919/* Send a udp datagram to specified TO_ADDR. */
b8d8561b 920int
5df61230 921comm_udp_sendto(int fd,
b7ac5457 922 const Ip::Address &to_addr,
62e76326 923 const void *buf,
924 int len)
090089c4 925{
95dc7ff4 926 ++ statCounter.syscalls.sock.sendtos;
62e76326 927
cc192b50 928 debugs(50, 3, "comm_udp_sendto: Attempt to send UDP packet to " << to_addr <<
9e008dda 929 " using FD " << fd << " using Port " << comm_local_port(fd) );
cc192b50 930
aee3523a 931 struct addrinfo *AI = nullptr;
4dd643d5
AJ
932 to_addr.getAddrInfo(AI, fd_table[fd].sock_family);
933 int x = sendto(fd, buf, len, 0, AI->ai_addr, AI->ai_addrlen);
b69e9ffa 934 int xerrno = errno;
851614a8 935 Ip::Address::FreeAddr(AI);
cc192b50 936
b69e9ffa
AJ
937 if (x >= 0) {
938 errno = xerrno; // restore for caller to use
2d8c0b1a 939 return x;
b69e9ffa 940 }
2d8c0b1a 941
1191b93b 942#if _SQUID_LINUX_
b69e9ffa 943 if (ECONNREFUSED != xerrno)
17d51783 944#endif
b69e9ffa 945 debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", (family=" << fd_table[fd].sock_family << ") " << to_addr << ": " << xstrerr(xerrno));
62e76326 946
b69e9ffa 947 errno = xerrno; // restore for caller to use
4ee57cbe 948 return Comm::COMM_ERROR;
090089c4 949}
950
398bc066 951AsyncCall::Pointer
575d05c4 952comm_add_close_handler(int fd, CLCB * handler, void *data)
30a4f2a8 953{
bf8fe701 954 debugs(5, 5, "comm_add_close_handler: FD " << fd << ", handler=" <<
955 handler << ", data=" << data);
62e76326 956
b0469965 957 AsyncCall::Pointer call=commCbCall(5,4, "SomeCloseHandler",
9e008dda 958 CommCloseCbPtrFun(handler, data));
b0469965 959 comm_add_close_handler(fd, call);
398bc066 960 return call;
b0469965 961}
62e76326 962
b0469965 963void
964comm_add_close_handler(int fd, AsyncCall::Pointer &call)
965{
966 debugs(5, 5, "comm_add_close_handler: FD " << fd << ", AsyncCall=" << call);
62e76326 967
b0469965 968 /*TODO:Check for a similar scheduled AsyncCall*/
969// for (c = fd_table[fd].closeHandler; c; c = c->next)
970// assert(c->handler != handler || c->data != data);
62e76326 971
a928fdfd
EB
972 // TODO: Consider enhancing AsyncCallList to support random-access close
973 // handlers, perhaps after upgrading the remaining legacy CLCB handlers.
b0469965 974 call->setNext(fd_table[fd].closeHandler);
62e76326 975
b0469965 976 fd_table[fd].closeHandler = call;
30a4f2a8 977}
978
b0469965 979// remove function-based close handler
b8d8561b 980void
575d05c4 981comm_remove_close_handler(int fd, CLCB * handler, void *data)
090089c4 982{
8ebef9e0 983 assert(isOpen(fd));
30a4f2a8 984 /* Find handler in list */
bf8fe701 985 debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", handler=" <<
986 handler << ", data=" << data);
62e76326 987
aee3523a
AR
988 AsyncCall::Pointer p, prev = nullptr;
989 for (p = fd_table[fd].closeHandler; p != nullptr; prev = p, p = p->Next()) {
b0469965 990 typedef CommCbFunPtrCallT<CommCloseCbPtrFun> Call;
991 const Call *call = dynamic_cast<const Call*>(p.getRaw());
992 if (!call) // method callbacks have their own comm_remove_close_handler
993 continue;
62e76326 994
b0469965 995 typedef CommCloseCbParams Params;
996 const Params &params = GetCommParams<Params>(p);
997 if (call->dialer.handler == handler && params.data == data)
f53969cc 998 break; /* This is our handler */
b0469965 999 }
7828df5b
CT
1000
1001 // comm_close removes all close handlers so our handler may be gone
aee3523a 1002 if (p != nullptr) {
37cba319 1003 p->dequeue(fd_table[fd].closeHandler, prev);
7828df5b 1004 p->cancel("comm_remove_close_handler");
37cba319 1005 }
b0469965 1006}
62e76326 1007
b0469965 1008// remove method-based close handler
1009void
1010comm_remove_close_handler(int fd, AsyncCall::Pointer &call)
1011{
8ebef9e0 1012 assert(isOpen(fd));
b0469965 1013 debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", AsyncCall=" << call);
62e76326 1014
7828df5b 1015 // comm_close removes all close handlers so our handler may be gone
aee3523a
AR
1016 AsyncCall::Pointer p, prev = nullptr;
1017 for (p = fd_table[fd].closeHandler; p != nullptr && p != call; prev = p, p = p->Next());
62e76326 1018
aee3523a 1019 if (p != nullptr)
37cba319 1020 p->dequeue(fd_table[fd].closeHandler, prev);
b0469965 1021 call->cancel("comm_remove_close_handler");
30a4f2a8 1022}
090089c4 1023
b8d8561b 1024static void
1025commSetNoLinger(int fd)
30a4f2a8 1026{
62e76326 1027
30a4f2a8 1028 struct linger L;
f53969cc 1029 L.l_onoff = 0; /* off */
090089c4 1030 L.l_linger = 0;
62e76326 1031
b69e9ffa
AJ
1032 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0) {
1033 int xerrno = errno;
1034 debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1035 }
be4d35dc 1036 fd_table[fd].flags.nolinger = true;
090089c4 1037}
1038
b8d8561b 1039static void
1040commSetReuseAddr(int fd)
090089c4 1041{
1042 int on = 1;
b69e9ffa
AJ
1043 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0) {
1044 int xerrno = errno;
1045 debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1046 }
090089c4 1047}
1048
b8d8561b 1049static void
1050commSetTcpRcvbuf(int fd, int size)
f868539a 1051{
b69e9ffa
AJ
1052 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size)) < 0) {
1053 int xerrno = errno;
1054 debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", SIZE " << size << ": " << xstrerr(xerrno));
1055 }
1056 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size)) < 0) {
1057 int xerrno = errno;
1058 debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", SIZE " << size << ": " << xstrerr(xerrno));
1059 }
8f0d53ef 1060#ifdef TCP_WINDOW_CLAMP
b69e9ffa
AJ
1061 if (setsockopt(fd, SOL_TCP, TCP_WINDOW_CLAMP, (char *) &size, sizeof(size)) < 0) {
1062 int xerrno = errno;
1063 debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", SIZE " << size << ": " << xstrerr(xerrno));
1064 }
8f0d53ef 1065#endif
f868539a 1066}
1067
b8d8561b 1068int
1069commSetNonBlocking(int fd)
30a4f2a8 1070{
be266cb2 1071#if _SQUID_WINDOWS_
b05490a8 1072 int nonblocking = TRUE;
62e76326 1073
a396d1f8 1074 if (ioctl(fd, FIONBIO, &nonblocking) < 0) {
b69e9ffa
AJ
1075 int xerrno = errno;
1076 debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": " << xstrerr(xerrno) << " " << fd_table[fd].type);
a396d1f8
EA
1077 return Comm::COMM_ERROR;
1078 }
62e76326 1079
a396d1f8
EA
1080#else
1081 int flags;
1082 int dummy = 0;
62e76326 1083
a396d1f8 1084 if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
b69e9ffa
AJ
1085 int xerrno = errno;
1086 debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": fcntl F_GETFL: " << xstrerr(xerrno));
a396d1f8
EA
1087 return Comm::COMM_ERROR;
1088 }
62e76326 1089
a396d1f8 1090 if (fcntl(fd, F_SETFL, flags | SQUID_NONBLOCK) < 0) {
b69e9ffa
AJ
1091 int xerrno = errno;
1092 debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
a396d1f8 1093 return Comm::COMM_ERROR;
090089c4 1094 }
7f6ffd15 1095#endif
62e76326 1096
a396d1f8 1097 fd_table[fd].flags.nonblocking = true;
090089c4 1098 return 0;
1099}
1100
7e3ce7b9 1101int
1102commUnsetNonBlocking(int fd)
1103{
7aa9bb3e 1104#if _SQUID_WINDOWS_
a50bfe93 1105 int nonblocking = FALSE;
1106
1107 if (ioctlsocket(fd, FIONBIO, (unsigned long *) &nonblocking) < 0) {
1108#else
7e3ce7b9 1109 int flags;
1110 int dummy = 0;
62e76326 1111
7e3ce7b9 1112 if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
b69e9ffa
AJ
1113 int xerrno = errno;
1114 debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": fcntl F_GETFL: " << xstrerr(xerrno));
4ee57cbe 1115 return Comm::COMM_ERROR;
7e3ce7b9 1116 }
62e76326 1117
7e3ce7b9 1118 if (fcntl(fd, F_SETFL, flags & (~SQUID_NONBLOCK)) < 0) {
a50bfe93 1119#endif
b69e9ffa
AJ
1120 int xerrno = errno;
1121 debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
4ee57cbe 1122 return Comm::COMM_ERROR;
7e3ce7b9 1123 }
62e76326 1124
be4d35dc 1125 fd_table[fd].flags.nonblocking = false;
7e3ce7b9 1126 return 0;
1127}
1128
b8d8561b 1129void
e1381638
AJ
1130commSetCloseOnExec(int fd)
1131{
3ca60c86 1132#ifdef FD_CLOEXEC
731e4d49 1133 int flags;
7a18b487 1134 int dummy = 0;
62e76326 1135
2209fe19 1136 if ((flags = fcntl(fd, F_GETFD, dummy)) < 0) {
b69e9ffa
AJ
1137 int xerrno = errno;
1138 debugs(50, DBG_CRITICAL, MYNAME << "FD " << fd << ": fcntl F_GETFD: " << xstrerr(xerrno));
62e76326 1139 return;
3ca60c86 1140 }
62e76326 1141
b69e9ffa
AJ
1142 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
1143 int xerrno = errno;
d816f28d 1144 debugs(50, DBG_CRITICAL, "ERROR: " << MYNAME << "FD " << fd << ": set close-on-exec failed: " << xstrerr(xerrno));
b69e9ffa 1145 }
3ca60c86 1146#endif
1147}
1148
e90100aa 1149#ifdef TCP_NODELAY
1150static void
e1381638
AJ
1151commSetTcpNoDelay(int fd)
1152{
e90100aa 1153 int on = 1;
62e76326 1154
b69e9ffa
AJ
1155 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) < 0) {
1156 int xerrno = errno;
1157 debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
1158 }
62e76326 1159
be4d35dc 1160 fd_table[fd].flags.nodelay = true;
e90100aa 1161}
62e76326 1162
e90100aa 1163#endif
1164
d86b3703 1165void
e1381638
AJ
1166comm_init(void)
1167{
d7ca82e6 1168 assert(fd_table);
2d8c0b1a 1169
04f55905
AJ
1170 /* make sure the accept() socket FIFO delay queue exists */
1171 Comm::AcceptLimiter::Instance();
b0469965 1172
ec41b64c
AJ
1173 // make sure the IO pending callback table exists
1174 Comm::CallbackTableInit();
2d8c0b1a 1175
59c4d35b 1176 /* XXX account fd_table */
090089c4 1177 /* Keep a few file descriptors free so that we don't run out of FD's
1178 * after accepting a client but before it opens a socket or a file.
e83892e9 1179 * Since Squid_MaxFD can be as high as several thousand, don't waste them */
d85c3078 1180 RESERVED_FD = min(100, Squid_MaxFD / 4);
2d8c0b1a 1181
74257126 1182 TheHalfClosed = new DescriptorSet;
d841c88d
AJ
1183
1184 /* setup the select loop module */
1185 Comm::SelectLoopInit();
090089c4 1186}
1187
236d1779 1188void
e1381638
AJ
1189comm_exit(void)
1190{
74257126 1191 delete TheHalfClosed;
aee3523a 1192 TheHalfClosed = nullptr;
74257126 1193
ec41b64c 1194 Comm::CallbackTableDestruct();
236d1779 1195}
1196
9a0a18de 1197#if USE_DELAY_POOLS
b4cd430a 1198// called when the queue is done waiting for the client bucket to fill
ec41b64c 1199void
b4cd430a
CT
1200commHandleWriteHelper(void * data)
1201{
1202 CommQuotaQueue *queue = static_cast<CommQuotaQueue*>(data);
1203 assert(queue);
1204
1205 ClientInfo *clientInfo = queue->clientInfo;
1206 // ClientInfo invalidates queue if freed, so if we got here through,
1207 // evenAdd cbdata protections, everything should be valid and consistent
f33d34a8 1208 assert(clientInfo);
b4cd430a
CT
1209 assert(clientInfo->hasQueue());
1210 assert(clientInfo->hasQueue(queue));
b4cd430a
CT
1211 assert(clientInfo->eventWaiting);
1212 clientInfo->eventWaiting = false;
1213
1214 do {
b90a3414
EB
1215 clientInfo->writeOrDequeue();
1216 if (clientInfo->selectWaiting)
1217 return;
1218 } while (clientInfo->hasQueue());
b4cd430a 1219
b90a3414
EB
1220 debugs(77, 3, "emptied queue");
1221}
1222
1223void
1224ClientInfo::writeOrDequeue()
1225{
1226 assert(!selectWaiting);
1227 const auto head = quotaPeekFd();
1228 const auto &headFde = fd_table[head];
1229 CallBack(headFde.codeContext, [&] {
1230 const auto ccb = COMMIO_FD_WRITECB(head);
1231 // check that the head descriptor is still relevant
1232 if (headFde.clientInfo == this &&
b6388dfd
AJ
1233 quotaPeekReserv() == ccb->quotaQueueReserv &&
1234 !headFde.closing()) {
b4cd430a
CT
1235
1236 // wait for the head descriptor to become ready for writing
d841c88d 1237 Comm::SetSelect(head, COMM_SELECT_WRITE, Comm::HandleWrite, ccb, 0);
b90a3414
EB
1238 selectWaiting = true;
1239 } else {
1240 quotaDequeue(); // remove the no longer relevant descriptor
f33d34a8 1241 }
b90a3414 1242 });
b4cd430a
CT
1243}
1244
1245bool
1246ClientInfo::hasQueue() const
1247{
1248 assert(quotaQueue);
1249 return !quotaQueue->empty();
1250}
1251
1252bool
1253ClientInfo::hasQueue(const CommQuotaQueue *q) const
1254{
1255 assert(quotaQueue);
1256 return quotaQueue == q;
1257}
1258
1259/// returns the first descriptor to be dequeued
1260int
1261ClientInfo::quotaPeekFd() const
1262{
1263 assert(quotaQueue);
1264 return quotaQueue->front();
1265}
1266
1267/// returns the reservation ID of the first descriptor to be dequeued
1268unsigned int
1269ClientInfo::quotaPeekReserv() const
1270{
1271 assert(quotaQueue);
1272 return quotaQueue->outs + 1;
1273}
1274
1275/// queues a given fd, creating the queue if necessary; returns reservation ID
1276unsigned int
1277ClientInfo::quotaEnqueue(int fd)
1278{
1279 assert(quotaQueue);
1280 return quotaQueue->enqueue(fd);
1281}
1282
1283/// removes queue head
1284void
1285ClientInfo::quotaDequeue()
1286{
1287 assert(quotaQueue);
1288 quotaQueue->dequeue();
1289}
1290
1291void
1292ClientInfo::kickQuotaQueue()
1293{
1294 if (!eventWaiting && !selectWaiting && hasQueue()) {
1295 // wait at least a second if the bucket is empty
b27668ec 1296 const double delay = (bucketLevel < 1.0) ? 1.0 : 0.0;
b4cd430a 1297 eventAdd("commHandleWriteHelper", &commHandleWriteHelper,
f33d34a8 1298 quotaQueue, delay, 0, true);
b4cd430a
CT
1299 eventWaiting = true;
1300 }
1301}
1302
1303/// calculates how much to write for a single dequeued client
1304int
b27668ec 1305ClientInfo::quota()
b4cd430a
CT
1306{
1307 /* If we have multiple clients and give full bucketSize to each client then
1308 * clt1 may often get a lot more because clt1->clt2 time distance in the
1309 * select(2) callback order may be a lot smaller than cltN->clt1 distance.
1310 * We divide quota evenly to be more fair. */
1311
1312 if (!rationedCount) {
1313 rationedCount = quotaQueue->size() + 1;
1314
1315 // The delay in ration recalculation _temporary_ deprives clients from
1316 // bytes that should have trickled in while rationedCount was positive.
1317 refillBucket();
1318
1319 // Rounding errors do not accumulate here, but we round down to avoid
1320 // negative bucket sizes after write with rationedCount=1.
b27668ec 1321 rationedQuota = static_cast<int>(floor(bucketLevel/rationedCount));
bf95c10a 1322 debugs(77,5, "new rationedQuota: " << rationedQuota <<
f33d34a8 1323 '*' << rationedCount);
b4cd430a
CT
1324 }
1325
1326 --rationedCount;
bf95c10a 1327 debugs(77,7, "rationedQuota: " << rationedQuota <<
f33d34a8 1328 " rations remaining: " << rationedCount);
b4cd430a
CT
1329
1330 // update 'last seen' time to prevent clientdb GC from dropping us
1331 last_seen = squid_curtime;
1332 return rationedQuota;
1333}
1334
b27668ec
EB
1335bool
1336ClientInfo::applyQuota(int &nleft, Comm::IoCallback *state)
1337{
1338 assert(hasQueue());
1339 assert(quotaPeekFd() == state->conn->fd);
1340 quotaDequeue(); // we will write or requeue below
1341 if (nleft > 0 && !BandwidthBucket::applyQuota(nleft, state)) {
1342 state->quotaQueueReserv = quotaEnqueue(state->conn->fd);
1343 kickQuotaQueue();
1344 return false;
1345 }
1346 return true;
1347}
1348
b4cd430a 1349void
b27668ec 1350ClientInfo::scheduleWrite(Comm::IoCallback *state)
b4cd430a 1351{
b27668ec
EB
1352 if (writeLimitingActive) {
1353 state->quotaQueueReserv = quotaEnqueue(state->conn->fd);
1354 kickQuotaQueue();
b4cd430a 1355 }
b27668ec 1356}
b4cd430a 1357
b27668ec
EB
1358void
1359ClientInfo::onFdClosed()
1360{
1361 BandwidthBucket::onFdClosed();
1362 // kick queue or it will get stuck as commWriteHandle is not called
1363 kickQuotaQueue();
1364}
b4cd430a 1365
b27668ec
EB
1366void
1367ClientInfo::reduceBucket(const int len)
1368{
1369 if (len > 0)
1370 BandwidthBucket::reduceBucket(len);
1371 // even if we wrote nothing, we were served; give others a chance
1372 kickQuotaQueue();
b4cd430a
CT
1373}
1374
f33d34a8 1375void
b4cd430a
CT
1376ClientInfo::setWriteLimiter(const int aWriteSpeedLimit, const double anInitialBurst, const double aHighWatermark)
1377{
b27668ec 1378 debugs(77,5, "Write limits for " << (const char*)key <<
f33d34a8
A
1379 " speed=" << aWriteSpeedLimit << " burst=" << anInitialBurst <<
1380 " highwatermark=" << aHighWatermark);
b4cd430a
CT
1381
1382 // set or possibly update traffic shaping parameters
1383 writeLimitingActive = true;
1384 writeSpeedLimit = aWriteSpeedLimit;
1385 bucketSizeLimit = aHighWatermark;
1386
1387 // but some members should only be set once for a newly activated bucket
1388 if (firstTimeConnection) {
1389 firstTimeConnection = false;
1390
1391 assert(!selectWaiting);
1392 assert(!quotaQueue);
e9dadd7d 1393 quotaQueue = new CommQuotaQueue(this);
b4cd430a 1394
b27668ec 1395 bucketLevel = anInitialBurst;
b4cd430a
CT
1396 prevTime = current_dtime;
1397 }
1398}
1399
1400CommQuotaQueue::CommQuotaQueue(ClientInfo *info): clientInfo(info),
f53969cc 1401 ins(0), outs(0)
b4cd430a
CT
1402{
1403 assert(clientInfo);
1404}
1405
1406CommQuotaQueue::~CommQuotaQueue()
1407{
1408 assert(!clientInfo); // ClientInfo should clear this before destroying us
1409}
1410
1411/// places the given fd at the end of the queue; returns reservation ID
1412unsigned int
1413CommQuotaQueue::enqueue(int fd)
1414{
b27668ec 1415 debugs(77,5, "clt" << (const char*)clientInfo->key <<
f33d34a8 1416 ": FD " << fd << " with qqid" << (ins+1) << ' ' << fds.size());
b4cd430a 1417 fds.push_back(fd);
b90a3414 1418 fd_table[fd].codeContext = CodeContext::Current();
b4cd430a
CT
1419 return ++ins;
1420}
1421
1422/// removes queue head
1423void
1424CommQuotaQueue::dequeue()
1425{
1426 assert(!fds.empty());
b27668ec 1427 debugs(77,5, "clt" << (const char*)clientInfo->key <<
f33d34a8
A
1428 ": FD " << fds.front() << " with qqid" << (outs+1) << ' ' <<
1429 fds.size());
b4cd430a
CT
1430 fds.pop_front();
1431 ++outs;
1432}
b27668ec 1433#endif /* USE_DELAY_POOLS */
b4cd430a 1434
89924214 1435/*
1436 * hm, this might be too general-purpose for all the places we'd
1437 * like to use it.
1438 */
b224ea98 1439int
e1381638
AJ
1440ignoreErrno(int ierrno)
1441{
603500e7 1442 switch (ierrno) {
62e76326 1443
89924214 1444 case EINPROGRESS:
62e76326 1445
603500e7 1446 case EWOULDBLOCK:
26a880e2 1447#if EAGAIN != EWOULDBLOCK
62e76326 1448
603500e7 1449 case EAGAIN:
26a880e2 1450#endif
62e76326 1451
603500e7 1452 case EALREADY:
62e76326 1453
603500e7 1454 case EINTR:
db494ab8 1455#ifdef ERESTART
62e76326 1456
db494ab8 1457 case ERESTART:
1458#endif
62e76326 1459
1460 return 1;
1461
603500e7 1462 default:
62e76326 1463 return 0;
603500e7 1464 }
62e76326 1465
603500e7 1466 /* NOTREACHED */
26a880e2 1467}
d723bf6b 1468
1469void
e1381638
AJ
1470commCloseAllSockets(void)
1471{
d723bf6b 1472 int fd;
aee3523a 1473 fde *F = nullptr;
62e76326 1474
95dc7ff4 1475 for (fd = 0; fd <= Biggest_FD; ++fd) {
62e76326 1476 F = &fd_table[fd];
1477
1478 if (!F->flags.open)
1479 continue;
1480
1481 if (F->type != FD_SOCKET)
1482 continue;
1483
f53969cc 1484 if (F->flags.ipc) /* don't close inter-process sockets */
62e76326 1485 continue;
1486
aee3523a 1487 if (F->timeoutHandler != nullptr) {
b0469965 1488 AsyncCall::Pointer callback = F->timeoutHandler;
aee3523a 1489 F->timeoutHandler = nullptr;
bf8fe701 1490 debugs(5, 5, "commCloseAllSockets: FD " << fd << ": Calling timeout handler");
9e008dda 1491 ScheduleCallHere(callback);
62e76326 1492 } else {
468fe1b5 1493 debugs(5, 5, "commCloseAllSockets: FD " << fd << ": calling comm_reset_close()");
5c336a3b 1494 old_comm_reset_close(fd);
62e76326 1495 }
d723bf6b 1496 }
1497}
1b3db6d9 1498
2d8c0b1a 1499static bool
e1381638
AJ
1500AlreadyTimedOut(fde *F)
1501{
2d8c0b1a 1502 if (!F->flags.open)
1503 return true;
1504
1505 if (F->timeout == 0)
1506 return true;
1507
1508 if (F->timeout > squid_curtime)
1509 return true;
1510
1511 return false;
1512}
1513
5ef5e5cc
AJ
1514static bool
1515writeTimedOut(int fd)
1516{
ec41b64c 1517 if (!COMMIO_FD_WRITECB(fd)->active())
5ef5e5cc
AJ
1518 return false;
1519
1520 if ((squid_curtime - fd_table[fd].writeStart) < Config.Timeout.write)
1521 return false;
1522
1523 return true;
1524}
1525
1b3db6d9 1526void
e1381638
AJ
1527checkTimeouts(void)
1528{
1b3db6d9 1529 int fd;
aee3523a 1530 fde *F = nullptr;
b0469965 1531 AsyncCall::Pointer callback;
62e76326 1532
95dc7ff4 1533 for (fd = 0; fd <= Biggest_FD; ++fd) {
62e76326 1534 F = &fd_table[fd];
1535
5ef5e5cc
AJ
1536 if (writeTimedOut(fd)) {
1537 // We have an active write callback and we are timed out
ccfbe8f4 1538 CodeContext::Reset(F->codeContext);
52f6ea9e 1539 debugs(5, 5, "checkTimeouts: FD " << fd << " auto write timeout");
aee3523a 1540 Comm::SetSelect(fd, COMM_SELECT_WRITE, nullptr, nullptr, 0);
4ee57cbe 1541 COMMIO_FD_WRITECB(fd)->finish(Comm::COMM_ERROR, ETIMEDOUT);
ccfbe8f4 1542 CodeContext::Reset();
db43f179 1543 continue;
b27668ec
EB
1544#if USE_DELAY_POOLS
1545 } else if (F->writeQuotaHandler != nullptr && COMMIO_FD_WRITECB(fd)->conn != nullptr) {
ccfbe8f4 1546 // TODO: Move and extract quota() call to place it inside F->codeContext.
b27668ec 1547 if (!F->writeQuotaHandler->selectWaiting && F->writeQuotaHandler->quota() && !F->closing()) {
ccfbe8f4 1548 CodeContext::Reset(F->codeContext);
b27668ec
EB
1549 F->writeQuotaHandler->selectWaiting = true;
1550 Comm::SetSelect(fd, COMM_SELECT_WRITE, Comm::HandleWrite, COMMIO_FD_WRITECB(fd), 0);
ccfbe8f4 1551 CodeContext::Reset();
b27668ec
EB
1552 }
1553 continue;
1554#endif
1555 }
1556 else if (AlreadyTimedOut(F))
62e76326 1557 continue;
1558
ccfbe8f4 1559 CodeContext::Reset(F->codeContext);
9e008dda 1560 debugs(5, 5, "checkTimeouts: FD " << fd << " Expired");
62e76326 1561
aee3523a 1562 if (F->timeoutHandler != nullptr) {
bf8fe701 1563 debugs(5, 5, "checkTimeouts: FD " << fd << ": Call timeout handler");
b0469965 1564 callback = F->timeoutHandler;
aee3523a 1565 F->timeoutHandler = nullptr;
9e008dda 1566 ScheduleCallHere(callback);
62e76326 1567 } else {
bf8fe701 1568 debugs(5, 5, "checkTimeouts: FD " << fd << ": Forcing comm_close()");
62e76326 1569 comm_close(fd);
1570 }
ccfbe8f4
AR
1571
1572 CodeContext::Reset();
b5443c04 1573 }
1574}
1575
9e008dda
AJ
1576/// Start waiting for a possibly half-closed connection to close
1577// by scheduling a read callback to a monitoring handler that
82ec8dfc 1578// will close the connection on read errors.
a46d2c0e 1579void
e1381638
AJ
1580commStartHalfClosedMonitor(int fd)
1581{
bf95c10a 1582 debugs(5, 5, "adding FD " << fd << " to " << *TheHalfClosed);
8ebef9e0 1583 assert(isOpen(fd) && !commHasHalfClosedMonitor(fd));
74257126 1584 (void)TheHalfClosed->add(fd); // could also assert the result
1f9077c2 1585 fd_table[fd].codeContext = CodeContext::Current();
74257126
AR
1586 commPlanHalfClosedCheck(); // may schedule check if we added the first FD
1587}
1588
1589static
1590void
e1381638
AJ
1591commPlanHalfClosedCheck()
1592{
74257126 1593 if (!WillCheckHalfClosed && !TheHalfClosed->empty()) {
aee3523a 1594 eventAdd("commHalfClosedCheck", &commHalfClosedCheck, nullptr, 1.0, 1);
74257126
AR
1595 WillCheckHalfClosed = true;
1596 }
1597}
1598
1599/// iterates over all descriptors that may need half-closed tests and
1600/// calls comm_read for those that do; re-schedules the check if needed
1601static
1602void
e1381638
AJ
1603commHalfClosedCheck(void *)
1604{
bf95c10a 1605 debugs(5, 5, "checking " << *TheHalfClosed);
74257126
AR
1606
1607 typedef DescriptorSet::const_iterator DSCI;
1608 const DSCI end = TheHalfClosed->end();
1609 for (DSCI i = TheHalfClosed->begin(); i != end; ++i) {
ec20038e
AJ
1610 Comm::ConnectionPointer c = new Comm::Connection; // XXX: temporary. make HalfClosed a list of these.
1611 c->fd = *i;
1612 if (!fd_table[c->fd].halfClosedReader) { // not reading already
1f9077c2
EB
1613 CallBack(fd_table[c->fd].codeContext, [&c] {
1614 AsyncCall::Pointer call = commCbCall(5,4, "commHalfClosedReader",
b6388dfd 1615 CommIoCbPtrFun(&commHalfClosedReader, nullptr));
1f9077c2
EB
1616 Comm::Read(c, call);
1617 fd_table[c->fd].halfClosedReader = call;
1618 });
ec20038e
AJ
1619 } else
1620 c->fd = -1; // XXX: temporary. prevent c replacement erase closing listed FD
74257126 1621 }
f900210a 1622
74257126
AR
1623 WillCheckHalfClosed = false; // as far as we know
1624 commPlanHalfClosedCheck(); // may need to check again
f900210a 1625}
1626
82ec8dfc
AR
1627/// checks whether we are waiting for possibly half-closed connection to close
1628// We are monitoring if the read handler for the fd is the monitoring handler.
1629bool
e1381638
AJ
1630commHasHalfClosedMonitor(int fd)
1631{
74257126 1632 return TheHalfClosed->has(fd);
a46d2c0e 1633}
1634
82ec8dfc 1635/// stop waiting for possibly half-closed connection to close
7e66d5e2 1636void
e1381638
AJ
1637commStopHalfClosedMonitor(int const fd)
1638{
bf95c10a 1639 debugs(5, 5, "removing FD " << fd << " from " << *TheHalfClosed);
74257126
AR
1640
1641 // cancel the read if one was scheduled
1642 AsyncCall::Pointer reader = fd_table[fd].halfClosedReader;
aee3523a 1643 if (reader != nullptr)
0d4e382b 1644 Comm::ReadCancel(fd, reader);
aee3523a 1645 fd_table[fd].halfClosedReader = nullptr;
74257126
AR
1646
1647 TheHalfClosed->del(fd);
a46d2c0e 1648}
1649
82ec8dfc
AR
1650/// I/O handler for the possibly half-closed connection monitoring code
1651static void
c8407295 1652commHalfClosedReader(const Comm::ConnectionPointer &conn, char *, size_t size, Comm::Flag flag, int, void *)
e1381638 1653{
82ec8dfc 1654 // there cannot be more data coming in on half-closed connections
9e008dda 1655 assert(size == 0);
aee3523a 1656 assert(conn != nullptr);
e0d28505 1657 assert(commHasHalfClosedMonitor(conn->fd)); // or we would have canceled the read
74257126 1658
aee3523a 1659 fd_table[conn->fd].halfClosedReader = nullptr; // done reading, for now
a46d2c0e 1660
82ec8dfc 1661 // nothing to do if fd is being closed
c8407295 1662 if (flag == Comm::ERR_CLOSING)
82ec8dfc 1663 return;
a46d2c0e 1664
82ec8dfc 1665 // if read failed, close the connection
c8407295 1666 if (flag != Comm::OK) {
bf95c10a 1667 debugs(5, 3, "closing " << conn);
80463bb4 1668 conn->close();
82ec8dfc
AR
1669 return;
1670 }
a46d2c0e 1671
82ec8dfc 1672 // continue waiting for close or error
74257126 1673 commPlanHalfClosedCheck(); // make sure this fd will be checked again
a46d2c0e 1674}
a46d2c0e 1675
8ff3fa2e 1676int
e1381638
AJ
1677CommSelectEngine::checkEvents(int timeout)
1678{
fa3f745b 1679 static time_t last_timeout = 0;
1680
1681 /* No, this shouldn't be here. But it shouldn't be in each comm handler. -adrian */
1682 if (squid_curtime > last_timeout) {
1683 last_timeout = squid_curtime;
1684 checkTimeouts();
1685 }
1686
d841c88d 1687 switch (Comm::DoSelect(timeout)) {
8ff3fa2e 1688
c8407295 1689 case Comm::OK:
8ff3fa2e 1690
c8407295 1691 case Comm::TIMEOUT:
8ff3fa2e 1692 return 0;
1693
c8407295 1694 case Comm::IDLE:
8ff3fa2e 1695
c8407295 1696 case Comm::SHUTDOWN:
8ff3fa2e 1697 return EVENT_IDLE;
1698
4ee57cbe 1699 case Comm::COMM_ERROR:
8ff3fa2e 1700 return EVENT_ERROR;
1701
1702 default:
1703 fatal_dump("comm.cc: Internal error -- this should never happen.");
1704 return EVENT_ERROR;
1705 };
1706}
10cefb7b 1707
0ffda73c 1708/// Create a unix-domain socket (UDS) that only supports FD_MSGHDR I/O.
10cefb7b 1709int
1710comm_open_uds(int sock_type,
1711 int proto,
1712 struct sockaddr_un* addr,
1713 int flags)
1714{
a67d2b2e 1715 // TODO: merge with comm_openex() when Ip::Address becomes NetAddress
ba568924 1716
10cefb7b 1717 int new_socket;
10cefb7b 1718
10cefb7b 1719 /* Create socket for accepting new connections. */
95dc7ff4 1720 ++ statCounter.syscalls.sock.sockets;
10cefb7b 1721
1722 /* Setup the socket addrinfo details for use */
ba568924 1723 struct addrinfo AI;
10cefb7b 1724 AI.ai_flags = 0;
1725 AI.ai_family = PF_UNIX;
1726 AI.ai_socktype = sock_type;
1727 AI.ai_protocol = proto;
1728 AI.ai_addrlen = SUN_LEN(addr);
1729 AI.ai_addr = (sockaddr*)addr;
aee3523a
AR
1730 AI.ai_canonname = nullptr;
1731 AI.ai_next = nullptr;
10cefb7b 1732
bf95c10a 1733 debugs(50, 3, "Attempt open socket for: " << addr->sun_path);
10cefb7b 1734
1735 if ((new_socket = socket(AI.ai_family, AI.ai_socktype, AI.ai_protocol)) < 0) {
b69e9ffa 1736 int xerrno = errno;
10cefb7b 1737 /* Increase the number of reserved fd's if calls to socket()
1738 * are failing because the open file table is full. This
1739 * limits the number of simultaneous clients */
1740
b69e9ffa
AJ
1741 if (limitError(xerrno)) {
1742 debugs(50, DBG_IMPORTANT, MYNAME << "socket failure: " << xstrerr(xerrno));
10cefb7b 1743 fdAdjustReserved();
1744 } else {
b69e9ffa 1745 debugs(50, DBG_CRITICAL, MYNAME << "socket failure: " << xstrerr(xerrno));
10cefb7b 1746 }
10cefb7b 1747 return -1;
1748 }
1749
fc9d2eb0 1750 debugs(50, 3, "Opened UDS FD " << new_socket << " : family=" << AI.ai_family << ", type=" << AI.ai_socktype << ", protocol=" << AI.ai_protocol);
10cefb7b 1751
1752 /* update fdstat */
bf95c10a 1753 debugs(50, 5, "FD " << new_socket << " is a new socket");
10cefb7b 1754
1755 assert(!isOpen(new_socket));
04efe9dd 1756 fd_open(new_socket, FD_MSGHDR, addr->sun_path);
10cefb7b 1757
ba568924
AR
1758 fd_table[new_socket].sock_family = AI.ai_family;
1759
10cefb7b 1760 if (!(flags & COMM_NOCLOEXEC))
1761 commSetCloseOnExec(new_socket);
1762
1763 if (flags & COMM_REUSEADDR)
1764 commSetReuseAddr(new_socket);
1765
1766 if (flags & COMM_NONBLOCKING) {
c8407295 1767 if (commSetNonBlocking(new_socket) != Comm::OK) {
10cefb7b 1768 comm_close(new_socket);
10cefb7b 1769 return -1;
1770 }
1771 }
1772
1773 if (flags & COMM_DOBIND) {
c8407295 1774 if (commBind(new_socket, AI) != Comm::OK) {
10cefb7b 1775 comm_close(new_socket);
10cefb7b 1776 return -1;
1777 }
1778 }
1779
1780#ifdef TCP_NODELAY
1781 if (sock_type == SOCK_STREAM)
1782 commSetTcpNoDelay(new_socket);
1783
1784#endif
1785
1786 if (Config.tcpRcvBufsz > 0 && sock_type == SOCK_STREAM)
1787 commSetTcpRcvbuf(new_socket, Config.tcpRcvBufsz);
1788
10cefb7b 1789 return new_socket;
1790}
f53969cc 1791