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