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