2 * DEBUG: section 5 Socket Functions
3 * AUTHOR: Harvest Derived
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
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.
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.
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.
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
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
36 #include "StoreIOBuffer.h"
42 #include "ConnectionDetail.h"
45 #include "SquidTime.h"
46 #include "CommCalls.h"
47 #include "DescriptorSet.h"
48 #include "icmp/net_db.h"
49 #include "ip/IpAddress.h"
50 #include "ip/IpIntercept.h"
53 #if defined(_SQUID_CYGWIN_)
54 #include <sys/ioctl.h>
56 #if HAVE_NETINET_TCP_H
57 #include <netinet/tcp.h>
61 * New C-like simple comm code. This stuff is a mess and doesn't really buy us anything.
70 static void commStopHalfClosedMonitor(int fd
);
71 static IOCB commHalfClosedReader
;
74 struct comm_io_callback_t
{
77 AsyncCall::Pointer callback
;
85 bool active() const { return callback
!= NULL
; }
90 comm_io_callback_t readcb
;
91 comm_io_callback_t writecb
;
93 typedef struct _comm_fd comm_fd_t
;
94 comm_fd_t
*commfd_table
;
96 // TODO: make this a comm_io_callback_t method?
98 commio_has_callback(int fd
, iocb_type type
, comm_io_callback_t
*ccb
)
100 assert(ccb
->fd
== fd
);
101 assert(ccb
->type
== type
);
102 return ccb
->active();
106 * Configure comm_io_callback_t for I/O
108 * @param fd filedescriptor
109 * @param ccb comm io callback
111 * @param cbdata callback data (must be cbdata'ed)
112 * @param buf buffer, if applicable
113 * @param freefunc freefunc, if applicable
114 * @param size buffer size
117 commio_set_callback(int fd
, iocb_type type
, comm_io_callback_t
*ccb
,
118 AsyncCall::Pointer
&cb
, char *buf
, FREE
*freefunc
, int size
)
120 assert(!ccb
->active());
121 assert(ccb
->type
== type
);
126 ccb
->freefunc
= freefunc
;
132 // Schedule the callback call and clear the callback
134 commio_finish_callback(int fd
, comm_io_callback_t
*ccb
, comm_err_t code
, int xerrno
)
136 debugs(5, 3, "commio_finish_callback: called for FD " << fd
<< " (" <<
137 code
<< ", " << xerrno
<< ")");
138 assert(ccb
->active());
139 assert(ccb
->fd
== fd
);
141 ccb
->xerrno
= xerrno
;
143 comm_io_callback_t cb
= *ccb
;
145 /* We've got a copy; blow away the real one */
146 /* XXX duplicate code from commio_cancel_callback! */
148 ccb
->callback
= NULL
; // cb has it
156 if (cb
.callback
!= NULL
) {
157 typedef CommIoCbParams Params
;
158 Params
¶ms
= GetCommParams
<Params
>(cb
.callback
);
161 params
.size
= cb
.offset
;
162 params
.flag
= cb
.errcode
;
163 params
.xerrno
= cb
.xerrno
;
164 ScheduleCallHere(cb
.callback
);
170 * Cancel the given callback
172 * Remember that the data is cbdataRef'ed.
174 // TODO: make this a comm_io_callback_t method
176 commio_cancel_callback(int fd
, comm_io_callback_t
*ccb
)
178 debugs(5, 3, "commio_cancel_callback: called for FD " << fd
);
179 assert(ccb
->fd
== fd
);
180 assert(ccb
->active());
183 ccb
->callback
= NULL
;
187 * Call the given comm callback; assumes the callback is valid.
189 * @param ccb io completion callback
192 commio_call_callback(comm_io_callback_t
*ccb
)
196 class ConnectStateData
200 void *operator new (size_t);
201 void operator delete (void *);
202 static void Connect (int fd
, void *me
);
204 void callCallback(comm_err_t status
, int xerrno
);
207 // defaults given by client
209 u_short default_port
;
210 IpAddress default_addr
;
211 // NP: CANNOT store the default addr:port together as it gets set/reset differently.
213 DnsLookupDetails dns
; ///< host lookup details
215 AsyncCall::Pointer callback
;
224 int commRetryConnect();
225 CBDATA_CLASS(ConnectStateData
);
230 static DescriptorSet
*TheHalfClosed
= NULL
; /// the set of half-closed FDs
231 static bool WillCheckHalfClosed
= false; /// true if check is scheduled
232 static EVH commHalfClosedCheck
;
233 static void commPlanHalfClosedCheck();
235 static comm_err_t
commBind(int s
, struct addrinfo
&);
236 static void commSetReuseAddr(int);
237 static void commSetNoLinger(int);
239 static void commSetTcpNoDelay(int);
241 static void commSetTcpRcvbuf(int, int);
242 static PF commConnectFree
;
243 static PF commHandleWrite
;
244 static IPH commConnectDnsHandle
;
246 static PF comm_accept_try
;
252 AcceptFD(int aFd
= -1): fd(aFd
), theCallback(0), mayAcceptMore(false) {}
254 void subscribe(AsyncCall::Pointer
&call
);
256 void notify(int newfd
, comm_err_t
, int xerrno
, const ConnectionDetail
&);
263 AsyncCall::Pointer theCallback
;
273 char const *close_file
;
277 typedef struct _fd_debug_t fd_debug_t
;
279 static MemAllocator
*conn_close_pool
= NULL
;
280 AcceptFD
*fdc_table
= NULL
; // TODO: rename. And use Vector<>?
281 fd_debug_t
*fdd_table
= NULL
;
286 return fd_table
[fd
].flags
.open
!= 0;
292 * If the read attempt succeeds or fails, call the callback.
293 * Else, wait for another IO notification.
296 commHandleRead(int fd
, void *data
)
298 comm_io_callback_t
*ccb
= (comm_io_callback_t
*) data
;
300 assert(data
== COMMIO_FD_READCB(fd
));
301 assert(commio_has_callback(fd
, IOCB_READ
, ccb
));
303 statCounter
.syscalls
.sock
.reads
++;
306 retval
= FD_READ_METHOD(fd
, ccb
->buf
, ccb
->size
);
307 debugs(5, 3, "comm_read_try: FD " << fd
<< ", size " << ccb
->size
<< ", retval " << retval
<< ", errno " << errno
);
309 if (retval
< 0 && !ignoreErrno(errno
)) {
310 debugs(5, 3, "comm_read_try: scheduling COMM_ERROR");
312 commio_finish_callback(fd
, ccb
, COMM_ERROR
, errno
);
316 /* See if we read anything */
317 /* Note - read 0 == socket EOF, which is a valid read */
319 fd_bytes(fd
, retval
, FD_READ
);
320 ccb
->offset
= retval
;
321 commio_finish_callback(fd
, ccb
, COMM_OK
, errno
);
325 /* Nope, register for some more IO */
326 commSetSelect(fd
, COMM_SELECT_READ
, commHandleRead
, data
, 0);
330 * Queue a read. handler/handler_data are called when the read
331 * completes, on error, or on file descriptor close.
334 comm_read(int fd
, char *buf
, int size
, IOCB
*handler
, void *handler_data
)
336 AsyncCall::Pointer call
= commCbCall(5,4, "SomeCommReadHandler",
337 CommIoCbPtrFun(handler
, handler_data
));
338 comm_read(fd
, buf
, size
, call
);
342 comm_read(int fd
, char *buf
, int size
, AsyncCall::Pointer
&callback
)
344 debugs(5, 5, "comm_read, queueing read for FD " << fd
<< "; asynCall " << callback
);
346 /* Make sure we are open and not closing */
348 assert(!fd_table
[fd
].closing());
349 comm_io_callback_t
*ccb
= COMMIO_FD_READCB(fd
);
351 // Make sure we are either not reading or just passively monitoring.
352 // Active/passive conflicts are OK and simply cancel passive monitoring.
354 // if the assertion below fails, we have an active comm_read conflict
355 assert(fd_table
[fd
].halfClosedReader
!= NULL
);
356 commStopHalfClosedMonitor(fd
);
357 assert(!ccb
->active());
361 commio_set_callback(fd
, IOCB_READ
, ccb
, callback
, (char *)buf
, NULL
, size
);
362 commSetSelect(fd
, COMM_SELECT_READ
, commHandleRead
, ccb
, 0);
366 * Empty the read buffers
368 * This is a magical routine that empties the read buffers.
369 * Under some platforms (Linux) if a buffer has data in it before
370 * you call close(), the socket will hang and take quite a while
374 comm_empty_os_read_buffers(int fd
)
377 /* prevent those nasty RST packets */
378 char buf
[SQUID_TCP_SO_RCVBUF
];
380 if (fd_table
[fd
].flags
.nonblocking
== 1) {
381 while (FD_READ_METHOD(fd
, buf
, SQUID_TCP_SO_RCVBUF
) > 0) {};
388 * Return whether the FD has a pending completed callback.
391 comm_has_pending_read_callback(int fd
)
394 // XXX: We do not know whether there is a read callback scheduled.
395 // This is used for pconn management that should probably be more
396 // tightly integrated into comm to minimize the chance that a
397 // closing pconn socket will be used for a new transaction.
401 // Does comm check this fd for read readiness?
402 // Note that when comm is not monitoring, there can be a pending callback
403 // call, which may resume comm monitoring once fired.
405 comm_monitors_read(int fd
)
408 // Being active is usually the same as monitoring because we always
409 // start monitoring the FD when we configure comm_io_callback_t for I/O
410 // and we usually configure comm_io_callback_t for I/O when we starting
411 // monitoring a FD for reading. TODO: replace with commio_has_callback
412 return COMMIO_FD_READCB(fd
)->active();
416 * Cancel a pending read. Assert that we have the right parameters,
417 * and that there are no pending read events!
419 * XXX: We do not assert that there are no pending read events and
420 * with async calls it becomes even more difficult.
421 * The whole interface should be reworked to do callback->cancel()
422 * instead of searching for places where the callback may be stored and
423 * updating the state of those places.
425 * AHC Don't call the comm handlers?
428 comm_read_cancel(int fd
, IOCB
*callback
, void *data
)
431 debugs(5, 4, "comm_read_cancel fails: FD " << fd
<< " closed");
435 comm_io_callback_t
*cb
= COMMIO_FD_READCB(fd
);
436 // TODO: is "active" == "monitors FD"?
438 debugs(5, 4, "comm_read_cancel fails: FD " << fd
<< " inactive");
442 typedef CommCbFunPtrCallT
<CommIoCbPtrFun
> Call
;
443 Call
*call
= dynamic_cast<Call
*>(cb
->callback
.getRaw());
445 debugs(5, 4, "comm_read_cancel fails: FD " << fd
<< " lacks callback");
449 call
->cancel("old comm_read_cancel");
451 typedef CommIoCbParams Params
;
452 const Params
¶ms
= GetCommParams
<Params
>(cb
->callback
);
454 /* Ok, we can be reasonably sure we won't lose any data here! */
455 assert(call
->dialer
.handler
== callback
);
456 assert(params
.data
== data
);
458 /* Delete the callback */
459 commio_cancel_callback(fd
, cb
);
461 /* And the IO event */
462 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
466 comm_read_cancel(int fd
, AsyncCall::Pointer
&callback
)
468 callback
->cancel("comm_read_cancel");
471 debugs(5, 4, "comm_read_cancel fails: FD " << fd
<< " closed");
475 comm_io_callback_t
*cb
= COMMIO_FD_READCB(fd
);
478 debugs(5, 4, "comm_read_cancel fails: FD " << fd
<< " inactive");
482 AsyncCall::Pointer call
= cb
->callback
;
483 assert(call
!= NULL
); // XXX: should never fail (active() checks for callback==NULL)
485 /* Ok, we can be reasonably sure we won't lose any data here! */
486 assert(call
== callback
);
488 /* Delete the callback */
489 commio_cancel_callback(fd
, cb
);
491 /* And the IO event */
492 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
497 * synchronous wrapper around udp socket functions
500 comm_udp_recvfrom(int fd
, void *buf
, size_t len
, int flags
, IpAddress
&from
)
502 statCounter
.syscalls
.sock
.recvfroms
++;
504 struct addrinfo
*AI
= NULL
;
506 debugs(5,8, "comm_udp_recvfrom: FD " << fd
<< " from " << from
);
508 assert( NULL
== AI
);
510 from
.InitAddrInfo(AI
);
512 x
= recvfrom(fd
, buf
, len
, flags
, AI
->ai_addr
, &AI
->ai_addrlen
);
516 from
.FreeAddrInfo(AI
);
522 comm_udp_recv(int fd
, void *buf
, size_t len
, int flags
)
525 return comm_udp_recvfrom(fd
, buf
, len
, flags
, nul
);
529 comm_udp_send(int s
, const void *buf
, size_t len
, int flags
)
531 return send(s
, buf
, len
, flags
);
536 comm_has_incomplete_write(int fd
)
539 return COMMIO_FD_WRITECB(fd
)->active();
543 * Queue a write. handler/handler_data are called when the write fully
544 * completes, on error, or on file descriptor close.
547 /* Return the local port associated with fd. */
549 comm_local_port(int fd
)
552 struct addrinfo
*addr
= NULL
;
553 fde
*F
= &fd_table
[fd
];
555 /* If the fd is closed already, just return */
557 if (!F
->flags
.open
) {
558 debugs(5, 0, "comm_local_port: FD " << fd
<< " has been closed.");
562 if (F
->local_addr
.GetPort())
563 return F
->local_addr
.GetPort();
566 if (F
->sock_family
== AF_INET
)
570 temp
.InitAddrInfo(addr
);
572 if (getsockname(fd
, addr
->ai_addr
, &(addr
->ai_addrlen
)) ) {
573 debugs(50, 1, "comm_local_port: Failed to retrieve TCP/UDP port number for socket: FD " << fd
<< ": " << xstrerror());
574 temp
.FreeAddrInfo(addr
);
579 temp
.FreeAddrInfo(addr
);
581 F
->local_addr
.SetPort(temp
.GetPort());
583 #if 0 // seems to undo comm_open actions on the FD ...
584 // grab default socket information for this address
585 temp
.GetAddrInfo(addr
);
587 F
->sock_family
= addr
->ai_family
;
589 temp
.FreeAddrInfo(addr
);
592 debugs(5, 6, "comm_local_port: FD " << fd
<< ": port " << F
->local_addr
.GetPort() << "(family=" << F
->sock_family
<< ")");
593 return F
->local_addr
.GetPort();
597 commBind(int s
, struct addrinfo
&inaddr
)
599 statCounter
.syscalls
.sock
.binds
++;
601 if (bind(s
, inaddr
.ai_addr
, inaddr
.ai_addrlen
) == 0) {
602 debugs(50, 6, "commBind: bind socket FD " << s
<< " to " << fd_table
[s
].local_addr
);
606 debugs(50, 0, "commBind: Cannot bind socket FD " << s
<< " to " << fd_table
[s
].local_addr
<< ": " << xstrerror());
612 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
613 * is OR of flags specified in comm.h. Defaults TOS
616 comm_open(int sock_type
,
622 return comm_openex(sock_type
, proto
, addr
, flags
, 0, note
);
626 comm_open_listener(int sock_type
,
634 /* all listener sockets require bind() */
635 flags
|= COMM_DOBIND
;
637 /* attempt native enabled port. */
638 sock
= comm_openex(sock_type
, proto
, addr
, flags
, 0, note
);
641 /* under IPv6 there is the possibility IPv6 is present but disabled. */
642 /* try again as IPv4-native */
643 if ( sock
< 0 && addr
.IsIPv6() && addr
.SetIPv4() ) {
644 /* attempt to open this IPv4-only. */
645 sock
= comm_openex(sock_type
, proto
, addr
, flags
, 0, note
);
646 debugs(50, 2, HERE
<< "attempt open " << note
<< " socket on: " << addr
);
654 limitError(int const anErrno
)
656 return anErrno
== ENFILE
|| anErrno
== EMFILE
;
660 comm_set_tos(int fd
, int tos
)
663 int x
= setsockopt(fd
, IPPROTO_IP
, IP_TOS
, (char *) &tos
, sizeof(int));
665 debugs(50, 1, "comm_set_tos: setsockopt(IP_TOS) on FD " << fd
<< ": " << xstrerror());
668 debugs(50, 0, "WARNING: setsockopt(IP_TOS) not supported on this platform");
674 comm_set_v6only(int fd
, int tos
)
677 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, (char *) &tos
, sizeof(int)) < 0) {
678 debugs(50, 1, "comm_open: setsockopt(IPV6_V6ONLY) " << (tos
?"ON":"OFF") << " for FD " << fd
<< ": " << xstrerror());
681 debugs(50, 0, "WARNING: comm_open: setsockopt(IPV6_V6ONLY) not supported on this platform");
686 * Set the socket IP_TRANSPARENT option for Linux TPROXY v4 support.
689 comm_set_transparent(int fd
)
691 #if defined(IP_TRANSPARENT)
693 if (setsockopt(fd
, SOL_IP
, IP_TRANSPARENT
, (char *) &tos
, sizeof(int)) < 0) {
694 debugs(50, DBG_IMPORTANT
, "comm_open: setsockopt(IP_TRANSPARENT) on FD " << fd
<< ": " << xstrerror());
696 /* mark the socket as having transparent options */
697 fd_table
[fd
].flags
.transparent
= 1;
700 debugs(50, DBG_CRITICAL
, "WARNING: comm_open: setsockopt(IP_TRANSPARENT) not supported on this platform");
705 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
706 * is OR of flags specified in defines.h:COMM_*
709 comm_openex(int sock_type
,
719 struct addrinfo
*AI
= NULL
;
721 PROF_start(comm_open
);
722 /* Create socket for accepting new connections. */
723 statCounter
.syscalls
.sock
.sockets
++;
725 /* Setup the socket addrinfo details for use */
726 addr
.GetAddrInfo(AI
);
727 AI
->ai_socktype
= sock_type
;
728 AI
->ai_protocol
= proto
;
730 debugs(50, 3, "comm_openex: Attempt open socket for: " << addr
);
732 if ((new_socket
= socket(AI
->ai_family
, AI
->ai_socktype
, AI
->ai_protocol
)) < 0) {
733 /* Increase the number of reserved fd's if calls to socket()
734 * are failing because the open file table is full. This
735 * limits the number of simultaneous clients */
737 if (limitError(errno
)) {
738 debugs(50, DBG_IMPORTANT
, "comm_open: socket failure: " << xstrerror());
741 debugs(50, DBG_CRITICAL
, "comm_open: socket failure: " << xstrerror());
744 addr
.FreeAddrInfo(AI
);
746 PROF_stop(comm_open
);
750 debugs(50, 3, "comm_openex: Opened socket FD " << new_socket
<< " : family=" << AI
->ai_family
<< ", type=" << AI
->ai_socktype
<< ", protocol=" << AI
->ai_protocol
);
752 /* set TOS if needed */
753 if (TOS
&& comm_set_tos(new_socket
, TOS
) ) {
757 #if IPV6_SPECIAL_SPLITSTACK
760 comm_set_v6only(new_socket
, 1);
764 #if IPV6_SPECIAL_V4MAPPED
766 /* Windows Vista supports Dual-Sockets. BUT defaults them to V6ONLY. Turn it OFF. */
767 /* Other OS may have this administratively disabled for general use. Same deal. */
769 comm_set_v6only(new_socket
, 0);
774 debugs(5, 5, "comm_open: FD " << new_socket
<< " is a new socket");
776 assert(!isOpen(new_socket
));
777 fd_open(new_socket
, FD_SOCKET
, note
);
779 fdd_table
[new_socket
].close_file
= NULL
;
781 fdd_table
[new_socket
].close_line
= 0;
783 F
= &fd_table
[new_socket
];
785 F
->local_addr
= addr
;
789 F
->sock_family
= AI
->ai_family
;
791 if (!(flags
& COMM_NOCLOEXEC
))
792 commSetCloseOnExec(new_socket
);
794 if ((flags
& COMM_REUSEADDR
))
795 commSetReuseAddr(new_socket
);
797 if (addr
.GetPort() > (u_short
) 0) {
799 if (sock_type
!= SOCK_DGRAM
)
801 commSetNoLinger(new_socket
);
804 commSetReuseAddr(new_socket
);
807 /* MUST be done before binding or face OS Error: "(99) Cannot assign requested address"... */
808 if ((flags
& COMM_TRANSPARENT
)) {
809 comm_set_transparent(new_socket
);
812 if ( (flags
& COMM_DOBIND
) || addr
.GetPort() > 0 || !addr
.IsAnyAddr() ) {
813 if ( !(flags
& COMM_DOBIND
) && addr
.IsAnyAddr() )
814 debugs(5,1,"WARNING: Squid is attempting to bind() port " << addr
<< " without being a listener.");
815 if ( addr
.IsNoAddr() )
816 debugs(5,0,"CRITICAL: Squid is attempting to bind() port " << addr
<< "!!");
818 if (commBind(new_socket
, *AI
) != COMM_OK
) {
819 comm_close(new_socket
);
820 addr
.FreeAddrInfo(AI
);
822 PROF_stop(comm_open
);
826 addr
.FreeAddrInfo(AI
);
828 if (flags
& COMM_NONBLOCKING
)
829 if (commSetNonBlocking(new_socket
) == COMM_ERROR
) {
831 PROF_stop(comm_open
);
835 if (sock_type
== SOCK_STREAM
)
836 commSetTcpNoDelay(new_socket
);
840 if (Config
.tcpRcvBufsz
> 0 && sock_type
== SOCK_STREAM
)
841 commSetTcpRcvbuf(new_socket
, Config
.tcpRcvBufsz
);
843 PROF_stop(comm_open
);
848 CBDATA_CLASS_INIT(ConnectStateData
);
851 ConnectStateData::operator new (size_t size
)
853 CBDATA_INIT_TYPE(ConnectStateData
);
854 return cbdataAlloc(ConnectStateData
);
858 ConnectStateData::operator delete (void *address
)
866 commConnectStart(int fd
, const char *host
, u_short port
, AsyncCall::Pointer
&cb
)
868 debugs(cb
->debugSection
, cb
->debugLevel
, "commConnectStart: FD " << fd
<<
869 ", cb " << cb
<< ", " << host
<< ":" << port
); // TODO: just print *cb
871 ConnectStateData
*cs
;
872 cs
= new ConnectStateData
;
874 cs
->host
= xstrdup(host
);
875 cs
->default_port
= port
;
878 comm_add_close_handler(fd
, commConnectFree
, cs
);
879 ipcache_nbgethostbyname(host
, commConnectDnsHandle
, cs
);
882 // TODO: Remove this and similar callback registration functions by replacing
883 // (callback,data) parameters with an AsyncCall so that we do not have to use
884 // a generic call name and debug level when creating an AsyncCall. This will
885 // also cut the number of callback registration routines in half.
887 commConnectStart(int fd
, const char *host
, u_short port
, CNCB
* callback
, void *data
)
889 debugs(5, 5, "commConnectStart: FD " << fd
<< ", data " << data
<< ", " << host
<< ":" << port
);
890 AsyncCall::Pointer call
= commCbCall(5,3,
891 "SomeCommConnectHandler", CommConnectCbPtrFun(callback
, data
));
892 commConnectStart(fd
, host
, port
, call
);
896 commConnectDnsHandle(const ipcache_addrs
*ia
, const DnsLookupDetails
&details
, void *data
)
898 ConnectStateData
*cs
= (ConnectStateData
*)data
;
902 debugs(5, 3, "commConnectDnsHandle: Unknown host: " << cs
->host
);
903 cs
->callCallback(COMM_ERR_DNS
, 0);
907 assert(ia
->cur
< ia
->count
);
909 cs
->default_addr
= ia
->in_addrs
[ia
->cur
];
911 if (Config
.onoff
.balance_on_multiple_ip
)
912 ipcacheCycleAddr(cs
->host
, NULL
);
914 cs
->addrcount
= ia
->count
;
916 cs
->connstart
= squid_curtime
;
922 ConnectStateData::callCallback(comm_err_t status
, int xerrno
)
924 debugs(5, 3, "commConnectCallback: FD " << fd
);
926 comm_remove_close_handler(fd
, commConnectFree
, this);
927 commSetTimeout(fd
, -1, NULL
, NULL
);
929 typedef CommConnectCbParams Params
;
930 Params
¶ms
= GetCommParams
<Params
>(callback
);
933 params
.flag
= status
;
934 params
.xerrno
= xerrno
;
935 ScheduleCallHere(callback
);
938 commConnectFree(fd
, this);
942 commConnectFree(int fd
, void *data
)
944 ConnectStateData
*cs
= (ConnectStateData
*)data
;
945 debugs(5, 3, "commConnectFree: FD " << fd
);
946 // delete cs->callback;
953 copyFDFlags(int to
, fde
*F
)
955 if (F
->flags
.close_on_exec
)
956 commSetCloseOnExec(to
);
958 if (F
->flags
.nonblocking
)
959 commSetNonBlocking(to
);
963 if (F
->flags
.nodelay
)
964 commSetTcpNoDelay(to
);
968 if (Config
.tcpRcvBufsz
> 0)
969 commSetTcpRcvbuf(to
, Config
.tcpRcvBufsz
);
972 /* Reset FD so that we can connect() again */
974 ConnectStateData::commResetFD()
976 struct addrinfo
*AI
= NULL
;
978 int new_family
= AF_UNSPEC
;
980 // XXX: do we have to check this?
982 // if (!cbdataReferenceValid(callback.data))
985 statCounter
.syscalls
.sock
.sockets
++;
987 /* setup a bare-bones addrinfo */
988 /* TODO INET6: for WinXP we may need to check the local_addr type and setup the family properly. */
990 new_family
= AI
->ai_family
;
992 int fd2
= socket(AI
->ai_family
, AI
->ai_socktype
, AI
->ai_protocol
);
994 nul
.FreeAddrInfo(AI
);
997 debugs(5, DBG_CRITICAL
, HERE
<< "WARNING: FD " << fd2
<< " socket failed to allocate: " << xstrerror());
999 if (ENFILE
== errno
|| EMFILE
== errno
)
1005 #ifdef _SQUID_MSWIN_
1007 /* On Windows dup2() can't work correctly on Sockets, the */
1008 /* workaround is to close the destination Socket before call them. */
1013 if (dup2(fd2
, fd
) < 0) {
1014 debugs(5, DBG_CRITICAL
, HERE
<< "WARNING: dup2(FD " << fd2
<< ", FD " << fd
<< ") failed: " << xstrerror());
1016 if (ENFILE
== errno
|| EMFILE
== errno
)
1023 commResetSelect(fd
);
1026 fde
*F
= &fd_table
[fd
];
1028 debugs(50, 3, "commResetFD: Reset socket FD " << fd
<< "->" << fd2
<< " : family=" << new_family
);
1030 /* INET6: copy the new sockets family type to the FDE table */
1031 fd_table
[fd
].sock_family
= new_family
;
1033 fd_table
[fd
].flags
.called_connect
= 0;
1035 * yuck, this has assumptions about comm_open() arguments for
1036 * the original socket
1039 /* MUST be done before binding or face OS Error: "(99) Cannot assign requested address"... */
1040 if ( F
->flags
.transparent
) {
1041 comm_set_transparent(fd
);
1045 F
->local_addr
.GetAddrInfo(AI
);
1047 if (commBind(fd
, *AI
) != COMM_OK
) {
1048 debugs(5, DBG_CRITICAL
, "WARNING: Reset of FD " << fd
<< " for " << F
->local_addr
<< " failed to bind: " << xstrerror());
1049 F
->local_addr
.FreeAddrInfo(AI
);
1052 F
->local_addr
.FreeAddrInfo(AI
);
1055 comm_set_tos(fd
, F
->tos
);
1057 #if IPV6_SPECIAL_SPLITSTACK
1058 if ( F
->local_addr
.IsIPv6() )
1059 comm_set_v6only(fd
, 1);
1068 ConnectStateData::commRetryConnect()
1070 assert(addrcount
> 0);
1072 if (addrcount
== 1) {
1073 if (tries
>= Config
.retry
.maxtries
)
1076 if (squid_curtime
- connstart
> Config
.Timeout
.connect
)
1079 if (tries
> addrcount
)
1083 return commResetFD();
1087 commReconnect(void *data
)
1089 ConnectStateData
*cs
= (ConnectStateData
*)data
;
1090 ipcache_nbgethostbyname(cs
->host
, commConnectDnsHandle
, cs
);
1093 /** Connect SOCK to specified DEST_PORT at DEST_HOST. */
1095 ConnectStateData::Connect(int fd
, void *me
)
1097 ConnectStateData
*cs
= (ConnectStateData
*)me
;
1098 assert (cs
->fd
== fd
);
1103 ConnectStateData::defaults()
1106 S
.SetPort(default_port
);
1110 ConnectStateData::connect()
1115 debugs(5,5, HERE
<< "to " << S
);
1117 switch (comm_connect_addr(fd
, S
) ) {
1119 case COMM_INPROGRESS
:
1120 debugs(5, 5, HERE
<< "FD " << fd
<< ": COMM_INPROGRESS");
1121 commSetSelect(fd
, COMM_SELECT_WRITE
, ConnectStateData::Connect
, this, 0);
1125 debugs(5, 5, HERE
<< "FD " << fd
<< ": COMM_OK - connected");
1126 ipcacheMarkGoodAddr(host
, S
);
1127 callCallback(COMM_OK
, 0);
1131 case COMM_ERR_PROTOCOL
:
1132 /* problem using the desired protocol over this socket.
1133 * count the connection attempt, reset the socket, and immediately try again */
1141 debugs(5, 5, HERE
"FD " << fd
<< ": * - try again");
1143 ipcacheMarkBadAddr(host
, S
);
1146 if (Config
.onoff
.test_reachability
)
1147 netdbDeleteAddrNetwork(S
);
1150 if (commRetryConnect()) {
1151 eventAdd("commReconnect", commReconnect
, this, this->addrcount
== 1 ? 0.05 : 0.0, 0);
1153 debugs(5, 5, HERE
<< "FD " << fd
<< ": * - ERR tried too many times already.");
1154 callCallback(COMM_ERR_CONNECT
, errno
);
1160 commSetTimeout_old(int fd, int timeout, PF * handler, void *data)
1162 debugs(5, 3, HERE << "FD " << fd << " timeout " << timeout);
1164 assert(fd < Squid_MaxFD);
1165 fde *F = &fd_table[fd];
1166 assert(F->flags.open);
1169 cbdataReferenceDone(F->timeout_data);
1170 F->timeout_handler = NULL;
1174 cbdataReferenceDone(F->timeout_data);
1175 F->timeout_handler = handler;
1176 F->timeout_data = cbdataReference(data);
1179 F->timeout = squid_curtime + (time_t) timeout;
1187 commSetTimeout(int fd
, int timeout
, PF
* handler
, void *data
)
1189 AsyncCall::Pointer call
;
1190 debugs(5, 3, HERE
<< "FD " << fd
<< " timeout " << timeout
);
1191 if (handler
!= NULL
)
1192 call
=commCbCall(5,4, "SomeTimeoutHandler", CommTimeoutCbPtrFun(handler
, data
));
1195 return commSetTimeout(fd
, timeout
, call
);
1199 int commSetTimeout(int fd
, int timeout
, AsyncCall::Pointer
&callback
)
1201 debugs(5, 3, HERE
<< "FD " << fd
<< " timeout " << timeout
);
1203 assert(fd
< Squid_MaxFD
);
1204 fde
*F
= &fd_table
[fd
];
1205 assert(F
->flags
.open
);
1208 F
->timeoutHandler
= NULL
;
1211 if (callback
!= NULL
) {
1212 typedef CommTimeoutCbParams Params
;
1213 Params
¶ms
= GetCommParams
<Params
>(callback
);
1215 F
->timeoutHandler
= callback
;
1218 F
->timeout
= squid_curtime
+ (time_t) timeout
;
1226 comm_connect_addr(int sock
, const IpAddress
&address
)
1228 comm_err_t status
= COMM_OK
;
1229 fde
*F
= &fd_table
[sock
];
1233 struct addrinfo
*AI
= NULL
;
1234 PROF_start(comm_connect_addr
);
1236 assert(address
.GetPort() != 0);
1238 debugs(5, 9, "comm_connect_addr: connecting socket " << sock
<< " to " << address
<< " (want family: " << F
->sock_family
<< ")");
1240 /* BUG 2222 FIX: reset the FD when its found to be IPv4 in IPv6 mode */
1241 /* inverse case of IPv4 failing to connect on IPv6 socket is handeld post-connect.
1242 * this case must presently be handled here since the GetAddrInfo asserts on bad mappings.
1243 * eventually we want it to throw a Must() that gets handled there instead of this if.
1244 * NP: because commresetFD is private to ConnStateData we have to return an error and
1245 * trust its handled properly.
1248 if (F
->sock_family
== AF_INET
&& !address
.IsIPv4()) {
1249 return COMM_ERR_PROTOCOL
;
1253 address
.GetAddrInfo(AI
, F
->sock_family
);
1255 /* Establish connection. */
1258 if (!F
->flags
.called_connect
) {
1259 F
->flags
.called_connect
= 1;
1260 statCounter
.syscalls
.sock
.connects
++;
1262 x
= connect(sock
, AI
->ai_addr
, AI
->ai_addrlen
);
1264 // XXX: ICAP code refuses callbacks during a pending comm_ call
1265 // Async calls development will fix this.
1268 errno
= EINPROGRESS
;
1272 debugs(5,5, "comm_connect_addr: sock=" << sock
<< ", addrinfo( " <<
1273 " flags=" << AI
->ai_flags
<<
1274 ", family=" << AI
->ai_family
<<
1275 ", socktype=" << AI
->ai_socktype
<<
1276 ", protocol=" << AI
->ai_protocol
<<
1277 ", &addr=" << AI
->ai_addr
<<
1278 ", addrlen=" << AI
->ai_addrlen
<<
1280 debugs(5, 9, "connect FD " << sock
<< ": (" << x
<< ") " << xstrerror());
1281 debugs(14,9, "connecting to: " << address
);
1284 #if defined(_SQUID_NEWSOS6_)
1285 /* Makoto MATSUSHITA <matusita@ics.es.osaka-u.ac.jp> */
1287 connect(sock
, AI
->ai_addr
, AI
->ai_addrlen
);
1289 if (errno
== EINVAL
) {
1290 errlen
= sizeof(err
);
1291 x
= getsockopt(sock
, SOL_SOCKET
, SO_ERROR
, &err
, &errlen
);
1298 errlen
= sizeof(err
);
1300 x
= getsockopt(sock
, SOL_SOCKET
, SO_ERROR
, &err
, &errlen
);
1305 #if defined(_SQUID_SOLARIS_)
1307 * Solaris 2.4's socket emulation doesn't allow you
1308 * to determine the error from a failed non-blocking
1309 * connect and just returns EPIPE. Create a fake
1310 * error message for connect. -- fenner@parc.xerox.com
1312 if (x
< 0 && errno
== EPIPE
)
1320 /* Squid seems to be working fine without this code. With this code,
1321 * we leak memory on many connect requests because of EINPROGRESS.
1322 * If you find that this code is needed, please file a bug report. */
1324 #ifdef _SQUID_LINUX_
1326 * Linux Debian replaces our allocated AI pointer with garbage when
1327 * connect() fails. This leads to segmentation faults deallocating
1328 * the system-allocated memory when we go to clean up our pointer.
1329 * HACK: is to leak the memory returned since we can't deallocate.
1337 address
.FreeAddrInfo(AI
);
1339 PROF_stop(comm_connect_addr
);
1341 if (errno
== 0 || errno
== EISCONN
)
1343 else if (ignoreErrno(errno
))
1344 status
= COMM_INPROGRESS
;
1347 if ( F
->sock_family
== AF_INET6
&& address
.IsIPv4() ) {
1349 /* failover to trying IPv4-only link if an IPv6 one fails */
1350 /* to catch the edge case of apps listening on IPv4-localhost */
1351 F
->sock_family
= AF_INET
;
1352 int res
= comm_connect_addr(sock
, address
);
1354 /* if that fails too, undo our temporary socktype hack so the repeat works properly. */
1355 if (res
== COMM_ERROR
)
1356 F
->sock_family
= AF_INET6
;
1363 address
.NtoA(F
->ipaddr
, MAX_IPSTRLEN
);
1365 F
->remote_port
= address
.GetPort(); /* remote_port is HS */
1367 if (status
== COMM_OK
) {
1368 debugs(5, 10, "comm_connect_addr: FD " << sock
<< " connected to " << address
);
1369 } else if (status
== COMM_INPROGRESS
) {
1370 debugs(5, 10, "comm_connect_addr: FD " << sock
<< " connection pending");
1376 /* Wait for an incoming connection on FD. FD should be a socket returned
1377 * from comm_listen. */
1379 comm_old_accept(int fd
, ConnectionDetail
&details
)
1381 PROF_start(comm_accept
);
1382 statCounter
.syscalls
.sock
.accepts
++;
1384 struct addrinfo
*gai
= NULL
;
1385 details
.me
.InitAddrInfo(gai
);
1387 if ((sock
= accept(fd
, gai
->ai_addr
, &gai
->ai_addrlen
)) < 0) {
1389 details
.me
.FreeAddrInfo(gai
);
1391 PROF_stop(comm_accept
);
1393 if (ignoreErrno(errno
)) {
1394 debugs(50, 5, "comm_old_accept: FD " << fd
<< ": " << xstrerror());
1395 return COMM_NOMESSAGE
;
1396 } else if (ENFILE
== errno
|| EMFILE
== errno
) {
1397 debugs(50, 3, "comm_old_accept: FD " << fd
<< ": " << xstrerror());
1400 debugs(50, 1, "comm_old_accept: FD " << fd
<< ": " << xstrerror());
1405 details
.peer
= *gai
;
1407 if ( Config
.client_ip_max_connections
>= 0) {
1408 if (clientdbEstablished(details
.peer
, 0) > Config
.client_ip_max_connections
) {
1409 debugs(50, DBG_IMPORTANT
, "WARNING: " << details
.peer
<< " attempting more than " << Config
.client_ip_max_connections
<< " connections.");
1410 details
.me
.FreeAddrInfo(gai
);
1415 details
.me
.InitAddrInfo(gai
);
1417 details
.me
.SetEmpty();
1418 getsockname(sock
, gai
->ai_addr
, &gai
->ai_addrlen
);
1421 commSetCloseOnExec(sock
);
1424 fd_open(sock
, FD_SOCKET
, "HTTP Request");
1425 fdd_table
[sock
].close_file
= NULL
;
1426 fdd_table
[sock
].close_line
= 0;
1427 fde
*F
= &fd_table
[sock
];
1428 details
.peer
.NtoA(F
->ipaddr
,MAX_IPSTRLEN
);
1429 F
->remote_port
= details
.peer
.GetPort();
1430 F
->local_addr
.SetPort(details
.me
.GetPort());
1432 F
->sock_family
= AF_INET
;
1434 F
->sock_family
= details
.me
.IsIPv4()?AF_INET
:AF_INET6
;
1436 details
.me
.FreeAddrInfo(gai
);
1438 commSetNonBlocking(sock
);
1440 /* IFF the socket is (tproxy) transparent, pass the flag down to allow spoofing */
1441 F
->flags
.transparent
= fd_table
[fd
].flags
.transparent
;
1443 PROF_stop(comm_accept
);
1448 commCallCloseHandlers(int fd
)
1450 fde
*F
= &fd_table
[fd
];
1451 debugs(5, 5, "commCallCloseHandlers: FD " << fd
);
1453 while (F
->closeHandler
!= NULL
) {
1454 AsyncCall::Pointer call
= F
->closeHandler
;
1455 F
->closeHandler
= call
->Next();
1456 call
->setNext(NULL
);
1457 // If call is not canceled schedule it for execution else ignore it
1458 if (!call
->canceled()) {
1459 debugs(5, 5, "commCallCloseHandlers: ch->handler=" << call
);
1460 typedef CommCloseCbParams Params
;
1461 Params
¶ms
= GetCommParams
<Params
>(call
);
1463 ScheduleCallHere(call
);
1470 commLingerClose(int fd
, void *unused
)
1472 LOCAL_ARRAY(char, buf
, 1024);
1474 n
= FD_READ_METHOD(fd
, buf
, 1024);
1477 debugs(5, 3, "commLingerClose: FD " << fd
<< " read: " << xstrerror());
1483 commLingerTimeout(int fd
, void *unused
)
1485 debugs(5, 3, "commLingerTimeout: FD " << fd
);
1490 * Inspired by apache
1493 comm_lingering_close(int fd
)
1497 if (fd_table
[fd
].ssl
)
1498 ssl_shutdown_method(fd
);
1502 if (shutdown(fd
, 1) < 0) {
1507 fd_note(fd
, "lingering close");
1508 commSetTimeout(fd
, 10, commLingerTimeout
, NULL
);
1509 commSetSelect(fd
, COMM_SELECT_READ
, commLingerClose
, NULL
, 0);
1515 * enable linger with time of 0 so that when the socket is
1516 * closed, TCP generates a RESET
1519 comm_reset_close(int fd
)
1526 if (setsockopt(fd
, SOL_SOCKET
, SO_LINGER
, (char *) &L
, sizeof(L
)) < 0)
1527 debugs(50, 0, "commResetTCPClose: FD " << fd
<< ": " << xstrerror());
1533 comm_close_start(int fd
, void *data
)
1536 fde
*F
= &fd_table
[fd
];
1538 ssl_shutdown_method(fd
);
1546 comm_close_complete(int fd
, void *data
)
1549 fde
*F
= &fd_table
[fd
];
1557 fd_close(fd
); /* update fdstat */
1561 fdc_table
[fd
] = AcceptFD(fd
);
1563 statCounter
.syscalls
.sock
.closes
++;
1565 /* When an fd closes, give accept() a chance, if need be */
1567 if (fdNFree() >= RESERVED_FD
)
1568 AcceptLimiter::Instance().kick();
1573 * Close the socket fd.
1575 * + call write handlers with ERR_CLOSING
1576 * + call read handlers with ERR_CLOSING
1577 * + call closing handlers
1579 * NOTE: COMM_ERR_CLOSING will NOT be called for CommReads' sitting in a
1580 * DeferredReadManager.
1583 _comm_close(int fd
, char const *file
, int line
)
1585 debugs(5, 3, "comm_close: start closing FD " << fd
);
1587 assert(fd
< Squid_MaxFD
);
1589 fde
*F
= &fd_table
[fd
];
1590 fdd_table
[fd
].close_file
= file
;
1591 fdd_table
[fd
].close_line
= line
;
1596 /* XXX: is this obsolete behind F->closing() ? */
1597 if ( (shutting_down
|| reconfiguring
) && (!F
->flags
.open
|| F
->type
== FD_FILE
))
1600 /* The following fails because ipc.c is doing calls to pipe() to create sockets! */
1603 assert(F
->type
!= FD_FILE
);
1605 PROF_start(comm_close
);
1607 F
->flags
.close_request
= 1;
1609 AsyncCall::Pointer startCall
=commCbCall(5,4, "comm_close_start",
1610 CommCloseCbPtrFun(comm_close_start
, NULL
));
1611 typedef CommCloseCbParams Params
;
1612 Params
&startParams
= GetCommParams
<Params
>(startCall
);
1613 startParams
.fd
= fd
;
1614 ScheduleCallHere(startCall
);
1616 // a half-closed fd may lack a reader, so we stop monitoring explicitly
1617 if (commHasHalfClosedMonitor(fd
))
1618 commStopHalfClosedMonitor(fd
);
1619 commSetTimeout(fd
, -1, NULL
, NULL
);
1621 // notify read/write handlers
1622 if (commio_has_callback(fd
, IOCB_WRITE
, COMMIO_FD_WRITECB(fd
))) {
1623 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), COMM_ERR_CLOSING
, errno
);
1625 if (commio_has_callback(fd
, IOCB_READ
, COMMIO_FD_READCB(fd
))) {
1626 commio_finish_callback(fd
, COMMIO_FD_READCB(fd
), COMM_ERR_CLOSING
, errno
);
1629 // notify accept handlers
1630 fdc_table
[fd
].notify(-1, COMM_ERR_CLOSING
, 0, ConnectionDetail());
1632 commCallCloseHandlers(fd
);
1635 F
->pconn
.pool
->count(F
->pconn
.uses
);
1637 comm_empty_os_read_buffers(fd
);
1640 AsyncCall::Pointer completeCall
=commCbCall(5,4, "comm_close_complete",
1641 CommCloseCbPtrFun(comm_close_complete
, NULL
));
1642 Params
&completeParams
= GetCommParams
<Params
>(completeCall
);
1643 completeParams
.fd
= fd
;
1644 // must use async call to wait for all callbacks
1645 // scheduled before comm_close() to finish
1646 ScheduleCallHere(completeCall
);
1648 PROF_stop(comm_close
);
1651 /* Send a udp datagram to specified TO_ADDR. */
1653 comm_udp_sendto(int fd
,
1654 const IpAddress
&to_addr
,
1659 struct addrinfo
*AI
= NULL
;
1661 PROF_start(comm_udp_sendto
);
1662 statCounter
.syscalls
.sock
.sendtos
++;
1664 debugs(50, 3, "comm_udp_sendto: Attempt to send UDP packet to " << to_addr
<<
1665 " using FD " << fd
<< " using Port " << comm_local_port(fd
) );
1667 /* BUG: something in the above macro appears to occasionally be setting AI to garbage. */
1668 /* AYJ: 2007-08-27 : or was it because I wasn't then setting 'fd_table[fd].sock_family' to fill properly. */
1669 assert( NULL
== AI
);
1671 to_addr
.GetAddrInfo(AI
, fd_table
[fd
].sock_family
);
1673 x
= sendto(fd
, buf
, len
, 0, AI
->ai_addr
, AI
->ai_addrlen
);
1675 to_addr
.FreeAddrInfo(AI
);
1677 PROF_stop(comm_udp_sendto
);
1682 #ifdef _SQUID_LINUX_
1684 if (ECONNREFUSED
!= errno
)
1687 debugs(50, 1, "comm_udp_sendto: FD " << fd
<< ", (family=" << fd_table
[fd
].sock_family
<< ") " << to_addr
<< ": " << xstrerror());
1693 comm_add_close_handler(int fd
, PF
* handler
, void *data
)
1695 debugs(5, 5, "comm_add_close_handler: FD " << fd
<< ", handler=" <<
1696 handler
<< ", data=" << data
);
1698 AsyncCall::Pointer call
=commCbCall(5,4, "SomeCloseHandler",
1699 CommCloseCbPtrFun(handler
, data
));
1700 comm_add_close_handler(fd
, call
);
1704 comm_add_close_handler(int fd
, AsyncCall::Pointer
&call
)
1706 debugs(5, 5, "comm_add_close_handler: FD " << fd
<< ", AsyncCall=" << call
);
1708 /*TODO:Check for a similar scheduled AsyncCall*/
1709 // for (c = fd_table[fd].closeHandler; c; c = c->next)
1710 // assert(c->handler != handler || c->data != data);
1712 call
->setNext(fd_table
[fd
].closeHandler
);
1714 fd_table
[fd
].closeHandler
= call
;
1718 // remove function-based close handler
1720 comm_remove_close_handler(int fd
, PF
* handler
, void *data
)
1722 assert (isOpen(fd
));
1723 /* Find handler in list */
1724 debugs(5, 5, "comm_remove_close_handler: FD " << fd
<< ", handler=" <<
1725 handler
<< ", data=" << data
);
1727 AsyncCall::Pointer p
;
1728 for (p
= fd_table
[fd
].closeHandler
; p
!= NULL
; p
= p
->Next()) {
1729 typedef CommCbFunPtrCallT
<CommCloseCbPtrFun
> Call
;
1730 const Call
*call
= dynamic_cast<const Call
*>(p
.getRaw());
1731 if (!call
) // method callbacks have their own comm_remove_close_handler
1734 typedef CommCloseCbParams Params
;
1735 const Params
¶ms
= GetCommParams
<Params
>(p
);
1736 if (call
->dialer
.handler
== handler
&& params
.data
== data
)
1737 break; /* This is our handler */
1740 // comm_close removes all close handlers so our handler may be gone
1742 p
->cancel("comm_remove_close_handler");
1743 // TODO: should we remove the handler from the close handlers list?
1746 // remove method-based close handler
1748 comm_remove_close_handler(int fd
, AsyncCall::Pointer
&call
)
1750 assert (isOpen(fd
));
1751 debugs(5, 5, "comm_remove_close_handler: FD " << fd
<< ", AsyncCall=" << call
);
1753 // comm_close removes all close handlers so our handler may be gone
1754 // TODO: should we remove the handler from the close handlers list?
1756 // Check to see if really exist the given AsyncCall in comm_close handlers
1757 // TODO: optimize: this slow code is only needed for the assert() below
1758 AsyncCall::Pointer p
;
1759 for (p
= fd_table
[fd
].closeHandler
; p
!= NULL
&& p
!= call
; p
= p
->Next());
1763 call
->cancel("comm_remove_close_handler");
1767 commSetNoLinger(int fd
)
1771 L
.l_onoff
= 0; /* off */
1774 if (setsockopt(fd
, SOL_SOCKET
, SO_LINGER
, (char *) &L
, sizeof(L
)) < 0)
1775 debugs(50, 0, "commSetNoLinger: FD " << fd
<< ": " << xstrerror());
1777 fd_table
[fd
].flags
.nolinger
= 1;
1781 commSetReuseAddr(int fd
)
1785 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &on
, sizeof(on
)) < 0)
1786 debugs(50, 1, "commSetReuseAddr: FD " << fd
<< ": " << xstrerror());
1790 commSetTcpRcvbuf(int fd
, int size
)
1792 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, (char *) &size
, sizeof(size
)) < 0)
1793 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd
<< ", SIZE " << size
<< ": " << xstrerror());
1794 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, (char *) &size
, sizeof(size
)) < 0)
1795 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd
<< ", SIZE " << size
<< ": " << xstrerror());
1796 #if TCP_WINDOW_CLAMP
1797 if (setsockopt(fd
, SOL_TCP
, TCP_WINDOW_CLAMP
, (char *) &size
, sizeof(size
)) < 0)
1798 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd
<< ", SIZE " << size
<< ": " << xstrerror());
1803 commSetNonBlocking(int fd
)
1805 #ifndef _SQUID_MSWIN_
1809 #ifdef _SQUID_WIN32_
1811 int nonblocking
= TRUE
;
1813 #ifdef _SQUID_CYGWIN_
1815 if (fd_table
[fd
].type
!= FD_PIPE
) {
1818 if (ioctl(fd
, FIONBIO
, &nonblocking
) < 0) {
1819 debugs(50, 0, "commSetNonBlocking: FD " << fd
<< ": " << xstrerror() << " " << fd_table
[fd
].type
);
1823 #ifdef _SQUID_CYGWIN_
1828 #ifndef _SQUID_MSWIN_
1830 if ((flags
= fcntl(fd
, F_GETFL
, dummy
)) < 0) {
1831 debugs(50, 0, "FD " << fd
<< ": fcntl F_GETFL: " << xstrerror());
1835 if (fcntl(fd
, F_SETFL
, flags
| SQUID_NONBLOCK
) < 0) {
1836 debugs(50, 0, "commSetNonBlocking: FD " << fd
<< ": " << xstrerror());
1841 #ifdef _SQUID_CYGWIN_
1846 fd_table
[fd
].flags
.nonblocking
= 1;
1852 commUnsetNonBlocking(int fd
)
1854 #ifdef _SQUID_MSWIN_
1855 int nonblocking
= FALSE
;
1857 if (ioctlsocket(fd
, FIONBIO
, (unsigned long *) &nonblocking
) < 0) {
1862 if ((flags
= fcntl(fd
, F_GETFL
, dummy
)) < 0) {
1863 debugs(50, 0, "FD " << fd
<< ": fcntl F_GETFL: " << xstrerror());
1867 if (fcntl(fd
, F_SETFL
, flags
& (~SQUID_NONBLOCK
)) < 0) {
1869 debugs(50, 0, "commUnsetNonBlocking: FD " << fd
<< ": " << xstrerror());
1873 fd_table
[fd
].flags
.nonblocking
= 0;
1878 commSetCloseOnExec(int fd
)
1884 if ((flags
= fcntl(fd
, F_GETFL
, dummy
)) < 0) {
1885 debugs(50, 0, "FD " << fd
<< ": fcntl F_GETFL: " << xstrerror());
1889 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) < 0)
1890 debugs(50, 0, "FD " << fd
<< ": set close-on-exec failed: " << xstrerror());
1892 fd_table
[fd
].flags
.close_on_exec
= 1;
1899 commSetTcpNoDelay(int fd
)
1903 if (setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *) &on
, sizeof(on
)) < 0)
1904 debugs(50, 1, "commSetTcpNoDelay: FD " << fd
<< ": " << xstrerror());
1906 fd_table
[fd
].flags
.nodelay
= 1;
1912 commSetTcpKeepalive(int fd
, int idle
, int interval
, int timeout
)
1916 if (timeout
&& interval
) {
1917 int count
= (timeout
+ interval
- 1) / interval
;
1918 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPCNT
, &count
, sizeof(on
)) < 0)
1919 debugs(5, 1, "commSetKeepalive: FD " << fd
<< ": " << xstrerror());
1924 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, &idle
, sizeof(on
)) < 0)
1925 debugs(5, 1, "commSetKeepalive: FD " << fd
<< ": " << xstrerror());
1930 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, &interval
, sizeof(on
)) < 0)
1931 debugs(5, 1, "commSetKeepalive: FD " << fd
<< ": " << xstrerror());
1934 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &on
, sizeof(on
)) < 0)
1935 debugs(5, 1, "commSetKeepalive: FD " << fd
<< ": " << xstrerror());
1941 fd_table
=(fde
*) xcalloc(Squid_MaxFD
, sizeof(fde
));
1942 fdd_table
= (fd_debug_t
*)xcalloc(Squid_MaxFD
, sizeof(fd_debug_t
));
1944 fdc_table
= new AcceptFD
[Squid_MaxFD
];
1945 for (int pos
= 0; pos
< Squid_MaxFD
; ++pos
) {
1946 fdc_table
[pos
] = AcceptFD(pos
);
1949 commfd_table
= (comm_fd_t
*) xcalloc(Squid_MaxFD
, sizeof(comm_fd_t
));
1950 for (int pos
= 0; pos
< Squid_MaxFD
; pos
++) {
1951 commfd_table
[pos
].fd
= pos
;
1952 commfd_table
[pos
].readcb
.fd
= pos
;
1953 commfd_table
[pos
].readcb
.type
= IOCB_READ
;
1954 commfd_table
[pos
].writecb
.fd
= pos
;
1955 commfd_table
[pos
].writecb
.type
= IOCB_WRITE
;
1958 /* XXX account fd_table */
1959 /* Keep a few file descriptors free so that we don't run out of FD's
1960 * after accepting a client but before it opens a socket or a file.
1961 * Since Squid_MaxFD can be as high as several thousand, don't waste them */
1962 RESERVED_FD
= min(100, Squid_MaxFD
/ 4);
1964 conn_close_pool
= memPoolCreate("close_handler", sizeof(close_handler
));
1966 TheHalfClosed
= new DescriptorSet
;
1972 delete TheHalfClosed
;
1973 TheHalfClosed
= NULL
;
1975 safe_free(fd_table
);
1976 safe_free(fdd_table
);
1981 safe_free(commfd_table
);
1986 commHandleWrite(int fd
, void *data
)
1988 comm_io_callback_t
*state
= (comm_io_callback_t
*)data
;
1992 assert(state
== COMMIO_FD_WRITECB(fd
));
1994 PROF_start(commHandleWrite
);
1995 debugs(5, 5, "commHandleWrite: FD " << fd
<< ": off " <<
1996 (long int) state
->offset
<< ", sz " << (long int) state
->size
<< ".");
1998 nleft
= state
->size
- state
->offset
;
1999 len
= FD_WRITE_METHOD(fd
, state
->buf
+ state
->offset
, nleft
);
2000 debugs(5, 5, "commHandleWrite: write() returns " << len
);
2001 fd_bytes(fd
, len
, FD_WRITE
);
2002 statCounter
.syscalls
.sock
.writes
++;
2003 // After each successful partial write,
2004 // reset fde::writeStart to the current time.
2005 fd_table
[fd
].writeStart
= squid_curtime
;
2008 /* Note we even call write if nleft == 0 */
2012 debugs(5, 1, "commHandleWrite: FD " << fd
<< ": write failure: connection closed with " << nleft
<< " bytes remaining.");
2014 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), nleft
? COMM_ERROR
: COMM_OK
, errno
);
2015 } else if (len
< 0) {
2018 if (fd_table
[fd
].flags
.socket_eof
) {
2019 debugs(50, 2, "commHandleWrite: FD " << fd
<< ": write failure: " << xstrerror() << ".");
2020 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), nleft
? COMM_ERROR
: COMM_OK
, errno
);
2021 } else if (ignoreErrno(errno
)) {
2022 debugs(50, 10, "commHandleWrite: FD " << fd
<< ": write failure: " << xstrerror() << ".");
2029 debugs(50, 2, "commHandleWrite: FD " << fd
<< ": write failure: " << xstrerror() << ".");
2030 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), nleft
? COMM_ERROR
: COMM_OK
, errno
);
2033 /* A successful write, continue */
2034 state
->offset
+= len
;
2036 if (state
->offset
< state
->size
) {
2037 /* Not done, reinstall the write handler and write some more */
2044 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), nleft
? COMM_OK
: COMM_ERROR
, errno
);
2048 PROF_stop(commHandleWrite
);
2052 * Queue a write. handler/handler_data are called when the write
2053 * completes, on error, or on file descriptor close.
2055 * free_func is used to free the passed buffer when the write has completed.
2058 comm_write(int fd
, const char *buf
, int size
, IOCB
* handler
, void *handler_data
, FREE
* free_func
)
2060 AsyncCall::Pointer call
= commCbCall(5,5, "SomeCommWriteHander",
2061 CommIoCbPtrFun(handler
, handler_data
));
2063 comm_write(fd
, buf
, size
, call
, free_func
);
2067 comm_write(int fd
, const char *buf
, int size
, AsyncCall::Pointer
&callback
, FREE
* free_func
)
2069 debugs(5, 5, "comm_write: FD " << fd
<< ": sz " << size
<< ": asynCall " << callback
);
2071 /* Make sure we are open, not closing, and not writing */
2073 assert(!fd_table
[fd
].closing());
2074 comm_io_callback_t
*ccb
= COMMIO_FD_WRITECB(fd
);
2075 assert(!ccb
->active());
2077 fd_table
[fd
].writeStart
= squid_curtime
;
2078 /* Queue the write */
2079 commio_set_callback(fd
, IOCB_WRITE
, ccb
, callback
,
2080 (char *)buf
, free_func
, size
);
2081 commSetSelect(fd
, COMM_SELECT_WRITE
, commHandleWrite
, ccb
, 0);
2085 /* a wrapper around comm_write to allow for MemBuf to be comm_written in a snap */
2087 comm_write_mbuf(int fd
, MemBuf
*mb
, IOCB
* handler
, void *handler_data
)
2089 comm_write(fd
, mb
->buf
, mb
->size
, handler
, handler_data
, mb
->freeFunc());
2093 comm_write_mbuf(int fd
, MemBuf
*mb
, AsyncCall::Pointer
&callback
)
2095 comm_write(fd
, mb
->buf
, mb
->size
, callback
, mb
->freeFunc());
2100 * hm, this might be too general-purpose for all the places we'd
2104 ignoreErrno(int ierrno
)
2111 #if EAGAIN != EWOULDBLOCK
2134 commCloseAllSockets(void)
2139 for (fd
= 0; fd
<= Biggest_FD
; fd
++) {
2145 if (F
->type
!= FD_SOCKET
)
2148 if (F
->flags
.ipc
) /* don't close inter-process sockets */
2151 if (F
->timeoutHandler
!= NULL
) {
2152 AsyncCall::Pointer callback
= F
->timeoutHandler
;
2153 F
->timeoutHandler
= NULL
;
2154 debugs(5, 5, "commCloseAllSockets: FD " << fd
<< ": Calling timeout handler");
2155 ScheduleCallHere(callback
);
2157 debugs(5, 5, "commCloseAllSockets: FD " << fd
<< ": calling comm_close()");
2164 AlreadyTimedOut(fde
*F
)
2169 if (F
->timeout
== 0)
2172 if (F
->timeout
> squid_curtime
)
2179 writeTimedOut(int fd
)
2181 if (!commio_has_callback(fd
, IOCB_WRITE
, COMMIO_FD_WRITECB(fd
)))
2184 if ((squid_curtime
- fd_table
[fd
].writeStart
) < Config
.Timeout
.write
)
2195 AsyncCall::Pointer callback
;
2197 for (fd
= 0; fd
<= Biggest_FD
; fd
++) {
2200 if (writeTimedOut(fd
)) {
2201 // We have an active write callback and we are timed out
2202 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), COMM_ERROR
, ETIMEDOUT
);
2203 } else if (AlreadyTimedOut(F
))
2206 debugs(5, 5, "checkTimeouts: FD " << fd
<< " Expired");
2208 if (F
->timeoutHandler
!= NULL
) {
2209 debugs(5, 5, "checkTimeouts: FD " << fd
<< ": Call timeout handler");
2210 callback
= F
->timeoutHandler
;
2211 F
->timeoutHandler
= NULL
;
2212 ScheduleCallHere(callback
);
2214 debugs(5, 5, "checkTimeouts: FD " << fd
<< ": Forcing comm_close()");
2221 * New-style listen and accept routines
2223 * Listen simply registers our interest in an FD for listening,
2224 * and accept takes a callback to call when an FD has been
2228 comm_listen(int sock
)
2232 if ((x
= listen(sock
, Squid_MaxFD
>> 2)) < 0) {
2233 debugs(50, 0, "comm_listen: listen(" << (Squid_MaxFD
>> 2) << ", " << sock
<< "): " << xstrerror());
2237 if (Config
.accept_filter
&& strcmp(Config
.accept_filter
, "none") != 0) {
2239 struct accept_filter_arg afa
;
2240 bzero(&afa
, sizeof(afa
));
2241 debugs(5, DBG_CRITICAL
, "Installing accept filter '" << Config
.accept_filter
<< "' on FD " << sock
);
2242 xstrncpy(afa
.af_name
, Config
.accept_filter
, sizeof(afa
.af_name
));
2243 x
= setsockopt(sock
, SOL_SOCKET
, SO_ACCEPTFILTER
, &afa
, sizeof(afa
));
2245 debugs(5, 0, "SO_ACCEPTFILTER '" << Config
.accept_filter
<< "': '" << xstrerror());
2246 #elif TCP_DEFER_ACCEPT
2248 if (strncmp(Config
.accept_filter
, "data=", 5) == 0)
2249 seconds
= atoi(Config
.accept_filter
+ 5);
2250 x
= setsockopt(sock
, IPPROTO_TCP
, TCP_DEFER_ACCEPT
, &seconds
, sizeof(seconds
));
2252 debugs(5, 0, "TCP_DEFER_ACCEPT '" << Config
.accept_filter
<< "': '" << xstrerror());
2254 debugs(5, 0, "accept_filter not supported on your OS");
2262 comm_accept(int fd
, IOACB
*handler
, void *handler_data
)
2264 debugs(5, 5, "comm_accept: FD " << fd
<< " handler: " << (void*)handler
);
2267 AsyncCall::Pointer call
= commCbCall(5,5, "SomeCommAcceptHandler",
2268 CommAcceptCbPtrFun(handler
, handler_data
));
2269 fdc_table
[fd
].subscribe(call
);
2273 comm_accept(int fd
, AsyncCall::Pointer
&call
)
2275 debugs(5, 5, "comm_accept: FD " << fd
<< " AsyncCall: " << call
);
2278 fdc_table
[fd
].subscribe(call
);
2281 // Called when somebody wants to be notified when our socket accepts new
2282 // connection. We do not probe the FD until there is such interest.
2284 AcceptFD::subscribe(AsyncCall::Pointer
&call
)
2286 /* make sure we're not pending! */
2287 assert(!theCallback
);
2291 mayAcceptMore
= true; // even if we failed to accept last time
2297 commSetSelect(fd
, COMM_SELECT_READ
, comm_accept_try
, NULL
, 0);
2301 AcceptFD::acceptOne()
2303 // If there is no callback and we accept, we will leak the accepted FD.
2304 // When we are running out of FDs, there is often no callback.
2306 debugs(5, 5, "AcceptFD::acceptOne orphaned: FD " << fd
);
2307 // XXX: can we remove this and similar "just in case" calls and
2308 // either listen always or listen only when there is a callback?
2309 if (!AcceptLimiter::Instance().deferring())
2310 commSetSelect(fd
, COMM_SELECT_READ
, comm_accept_try
, NULL
, 0);
2315 * We don't worry about running low on FDs here. Instead,
2316 * httpAccept() will use AcceptLimiter if we reach the limit
2320 /* Accept a new connection */
2321 ConnectionDetail connDetails
;
2322 int newfd
= comm_old_accept(fd
, connDetails
);
2324 /* Check for errors */
2327 assert(theCallback
!= NULL
);
2329 if (newfd
== COMM_NOMESSAGE
) {
2330 /* register interest again */
2331 debugs(5, 5, HERE
<< "try later: FD " << fd
<<
2332 " handler: " << *theCallback
);
2333 commSetSelect(fd
, COMM_SELECT_READ
, comm_accept_try
, NULL
, 0);
2337 // A non-recoverable error; notify the caller */
2338 notify(-1, COMM_ERROR
, errno
, connDetails
);
2342 assert(theCallback
!= NULL
);
2343 debugs(5, 5, "AcceptFD::acceptOne accepted: FD " << fd
<<
2344 " newfd: " << newfd
<< " from: " << connDetails
.peer
<<
2345 " handler: " << *theCallback
);
2346 notify(newfd
, COMM_OK
, 0, connDetails
);
2351 AcceptFD::acceptNext()
2353 mayAcceptMore
= acceptOne();
2357 AcceptFD::notify(int newfd
, comm_err_t errcode
, int xerrno
, const ConnectionDetail
&connDetails
)
2359 if (theCallback
!= NULL
) {
2360 typedef CommAcceptCbParams Params
;
2361 Params
¶ms
= GetCommParams
<Params
>(theCallback
);
2364 params
.details
= connDetails
;
2365 params
.flag
= errcode
;
2366 params
.xerrno
= xerrno
;
2367 ScheduleCallHere(theCallback
);
2373 * This callback is called whenever a filedescriptor is ready
2374 * to dupe itself and fob off an accept()ed connection
2377 comm_accept_try(int fd
, void *)
2380 fdc_table
[fd
].acceptNext();
2383 void CommIO::Initialise()
2385 /* Initialize done pipe signal */
2387 if (pipe(DonePipe
)) {}
2388 DoneFD
= DonePipe
[1];
2389 DoneReadFD
= DonePipe
[0];
2390 fd_open(DoneReadFD
, FD_PIPE
, "async-io completetion event: main");
2391 fd_open(DoneFD
, FD_PIPE
, "async-io completetion event: threads");
2392 commSetNonBlocking(DoneReadFD
);
2393 commSetNonBlocking(DoneFD
);
2394 commSetSelect(DoneReadFD
, COMM_SELECT_READ
, NULLFDHandler
, NULL
, 0);
2398 void CommIO::NotifyIOClose()
2400 /* Close done pipe signal */
2405 fd_close(DoneReadFD
);
2406 Initialised
= false;
2409 bool CommIO::Initialised
= false;
2410 bool CommIO::DoneSignalled
= false;
2411 int CommIO::DoneFD
= -1;
2412 int CommIO::DoneReadFD
= -1;
2418 FD_READ_METHOD(DoneReadFD
, buf
, sizeof(buf
));
2422 CommIO::NULLFDHandler(int fd
, void *data
)
2425 commSetSelect(fd
, COMM_SELECT_READ
, NULLFDHandler
, NULL
, 0);
2429 CommIO::ResetNotifications()
2431 if (DoneSignalled
) {
2433 DoneSignalled
= false;
2437 AcceptLimiter
AcceptLimiter::Instance_
;
2439 AcceptLimiter
&AcceptLimiter::Instance()
2445 AcceptLimiter::deferring() const
2447 return deferred
.size() > 0;
2451 AcceptLimiter::defer (int fd
, Acceptor::AcceptorFunction
*aFunc
, void *data
)
2453 debugs(5, 5, "AcceptLimiter::defer: FD " << fd
<< " handler: " << (void*)aFunc
);
2455 temp
.theFunction
= aFunc
;
2457 temp
.theData
= data
;
2458 deferred
.push_back(temp
);
2462 AcceptLimiter::kick()
2467 /* Yes, this means the first on is the last off....
2468 * If the list container was a little more friendly, we could sensibly us it.
2470 Acceptor temp
= deferred
.pop_back();
2472 comm_accept (temp
.acceptFD
, temp
.theFunction
, temp
.theData
);
2475 /// Start waiting for a possibly half-closed connection to close
2476 // by scheduling a read callback to a monitoring handler that
2477 // will close the connection on read errors.
2479 commStartHalfClosedMonitor(int fd
)
2481 debugs(5, 5, HERE
<< "adding FD " << fd
<< " to " << *TheHalfClosed
);
2483 assert(!commHasHalfClosedMonitor(fd
));
2484 (void)TheHalfClosed
->add(fd
); // could also assert the result
2485 commPlanHalfClosedCheck(); // may schedule check if we added the first FD
2490 commPlanHalfClosedCheck()
2492 if (!WillCheckHalfClosed
&& !TheHalfClosed
->empty()) {
2493 eventAdd("commHalfClosedCheck", &commHalfClosedCheck
, NULL
, 1.0, 1);
2494 WillCheckHalfClosed
= true;
2498 /// iterates over all descriptors that may need half-closed tests and
2499 /// calls comm_read for those that do; re-schedules the check if needed
2502 commHalfClosedCheck(void *)
2504 debugs(5, 5, HERE
<< "checking " << *TheHalfClosed
);
2506 typedef DescriptorSet::const_iterator DSCI
;
2507 const DSCI end
= TheHalfClosed
->end();
2508 for (DSCI i
= TheHalfClosed
->begin(); i
!= end
; ++i
) {
2510 if (!fd_table
[fd
].halfClosedReader
) { // not reading already
2511 AsyncCall::Pointer call
= commCbCall(5,4, "commHalfClosedReader",
2512 CommIoCbPtrFun(&commHalfClosedReader
, NULL
));
2513 comm_read(fd
, NULL
, 0, call
);
2514 fd_table
[fd
].halfClosedReader
= call
;
2518 WillCheckHalfClosed
= false; // as far as we know
2519 commPlanHalfClosedCheck(); // may need to check again
2522 /// checks whether we are waiting for possibly half-closed connection to close
2523 // We are monitoring if the read handler for the fd is the monitoring handler.
2525 commHasHalfClosedMonitor(int fd
)
2527 return TheHalfClosed
->has(fd
);
2530 /// stop waiting for possibly half-closed connection to close
2532 commStopHalfClosedMonitor(int const fd
)
2534 debugs(5, 5, HERE
<< "removing FD " << fd
<< " from " << *TheHalfClosed
);
2536 // cancel the read if one was scheduled
2537 AsyncCall::Pointer reader
= fd_table
[fd
].halfClosedReader
;
2539 comm_read_cancel(fd
, reader
);
2540 fd_table
[fd
].halfClosedReader
= NULL
;
2542 TheHalfClosed
->del(fd
);
2545 /// I/O handler for the possibly half-closed connection monitoring code
2547 commHalfClosedReader(int fd
, char *, size_t size
, comm_err_t flag
, int, void *)
2549 // there cannot be more data coming in on half-closed connections
2551 assert(commHasHalfClosedMonitor(fd
)); // or we would have canceled the read
2553 fd_table
[fd
].halfClosedReader
= NULL
; // done reading, for now
2555 // nothing to do if fd is being closed
2556 if (flag
== COMM_ERR_CLOSING
)
2559 // if read failed, close the connection
2560 if (flag
!= COMM_OK
) {
2561 debugs(5, 3, "commHalfClosedReader: closing FD " << fd
);
2566 // continue waiting for close or error
2567 commPlanHalfClosedCheck(); // make sure this fd will be checked again
2571 CommRead::CommRead() : fd(-1), buf(NULL
), len(0), callback(NULL
) {}
2573 CommRead::CommRead(int fd_
, char *buf_
, int len_
, AsyncCall::Pointer
&callback_
)
2574 : fd(fd_
), buf(buf_
), len(len_
), callback(callback_
) {}
2576 DeferredRead::DeferredRead () : theReader(NULL
), theContext(NULL
), theRead(), cancelled(false) {}
2578 DeferredRead::DeferredRead (DeferrableRead
*aReader
, void *data
, CommRead
const &aRead
) : theReader(aReader
), theContext (data
), theRead(aRead
), cancelled(false) {}
2580 DeferredReadManager::~DeferredReadManager()
2583 assert (deferredReads
.empty());
2586 /* explicit instantiation required for some systems */
2588 /// \cond AUTODOCS-IGNORE
2589 template cbdata_type CbDataList
<DeferredRead
>::CBDATA_CbDataList
;
2593 DeferredReadManager::delayRead(DeferredRead
const &aRead
)
2595 debugs(5, 3, "Adding deferred read on FD " << aRead
.theRead
.fd
);
2596 CbDataList
<DeferredRead
> *temp
= deferredReads
.push_back(aRead
);
2598 // We have to use a global function as a closer and point to temp
2599 // instead of "this" because DeferredReadManager is not a job and
2600 // is not even cbdata protected
2601 AsyncCall::Pointer closer
= commCbCall(5,4,
2602 "DeferredReadManager::CloseHandler",
2603 CommCloseCbPtrFun(&CloseHandler
, temp
));
2604 comm_add_close_handler(aRead
.theRead
.fd
, closer
);
2605 temp
->element
.closer
= closer
; // remeber so that we can cancel
2609 DeferredReadManager::CloseHandler(int fd
, void *thecbdata
)
2611 if (!cbdataReferenceValid (thecbdata
))
2614 CbDataList
<DeferredRead
> *temp
= (CbDataList
<DeferredRead
> *)thecbdata
;
2616 temp
->element
.closer
= NULL
;
2617 temp
->element
.markCancelled();
2621 DeferredReadManager::popHead(CbDataListContainer
<DeferredRead
> &deferredReads
)
2623 assert (!deferredReads
.empty());
2625 DeferredRead
&read
= deferredReads
.head
->element
;
2626 if (!read
.cancelled
) {
2627 comm_remove_close_handler(read
.theRead
.fd
, read
.closer
);
2631 DeferredRead result
= deferredReads
.pop_front();
2637 DeferredReadManager::kickReads(int const count
)
2639 /* if we had CbDataList::size() we could consolidate this and flushReads */
2646 size_t remaining
= count
;
2648 while (!deferredReads
.empty() && remaining
) {
2649 DeferredRead aRead
= popHead(deferredReads
);
2652 if (!aRead
.cancelled
)
2658 DeferredReadManager::flushReads()
2660 CbDataListContainer
<DeferredRead
> reads
;
2661 reads
= deferredReads
;
2662 deferredReads
= CbDataListContainer
<DeferredRead
>();
2664 // XXX: For fairness this SHOULD randomize the order
2665 while (!reads
.empty()) {
2666 DeferredRead aRead
= popHead(reads
);
2672 DeferredReadManager::kickARead(DeferredRead
const &aRead
)
2674 if (aRead
.cancelled
)
2677 if (aRead
.theRead
.fd
>=0 && fd_table
[aRead
.theRead
.fd
].closing())
2680 debugs(5, 3, "Kicking deferred read on FD " << aRead
.theRead
.fd
);
2682 aRead
.theReader(aRead
.theContext
, aRead
.theRead
);
2686 DeferredRead::markCancelled()
2691 ConnectionDetail::ConnectionDetail() : me(), peer()
2696 CommSelectEngine::checkEvents(int timeout
)
2698 static time_t last_timeout
= 0;
2700 /* No, this shouldn't be here. But it shouldn't be in each comm handler. -adrian */
2701 if (squid_curtime
> last_timeout
) {
2702 last_timeout
= squid_curtime
;
2706 switch (comm_select(timeout
)) {
2722 fatal_dump("comm.cc: Internal error -- this should never happen.");