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