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