3 * $Id: comm.cc,v 1.439 2007/12/14 23:11:46 amosjeffries Exp $
5 * DEBUG: section 5 Socket Functions
6 * AUTHOR: Harvest Derived
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
39 #include "StoreIOBuffer.h"
45 #include "ConnectionDetail.h"
48 #include "SquidTime.h"
49 #include "IPAddress.h"
51 #if defined(_SQUID_CYGWIN_)
52 #include <sys/ioctl.h>
54 #ifdef HAVE_NETINET_TCP_H
55 #include <netinet/tcp.h>
59 * New C-like simple comm code. This stuff is a mess and doesn't really buy us anything.
68 struct _comm_io_callback
{
83 typedef struct _comm_io_callback comm_io_callback_t
;
87 comm_io_callback_t readcb
;
88 comm_io_callback_t writecb
;
90 typedef struct _comm_fd comm_fd_t
;
91 comm_fd_t
*commfd_table
;
93 dlink_list commfd_completed_events
;
96 commio_has_callback(int fd
, iocb_type type
, comm_io_callback_t
*ccb
)
98 assert(ccb
->fd
== fd
);
99 assert(ccb
->type
== type
);
100 return ccb
->active
== true;
104 * Set the given handler and mark active
106 * @param fd filedescriptor
107 * @param ccb comm io callback
109 * @param cbdata callback data (must be cbdata'ed)
110 * @param buf buffer, if applicable
111 * @param freefunc freefunc, if applicable
112 * @param size buffer size
115 commio_set_callback(int fd
, iocb_type type
, comm_io_callback_t
*ccb
, IOCB
*cb
, void *cbdata
, char *buf
, FREE
*freefunc
, int size
)
117 assert(ccb
->active
== false);
118 assert(ccb
->type
== type
);
121 ccb
->callback_data
= cbdataReference(cbdata
);
123 ccb
->freefunc
= freefunc
;
126 ccb
->completed
= false;
132 * Complete the callback
134 * Someone may have already called this function once on a non-completed callback.
135 * This happens in the comm_close() routine - the IO may have completed
136 * but comm_close() is called bfeore teh callback has been called.
137 * In this case, leave the details the same (offset, for example) but just update
141 commio_complete_callback(int fd
, comm_io_callback_t
*ccb
, comm_err_t code
, int xerrno
)
143 debugs(5, 3, "commio_complete_callback: called for " << fd
<< " (" << code
<< ", " << xerrno
<< ")");
144 assert(ccb
->active
== true);
145 assert(ccb
->fd
== fd
);
147 ccb
->xerrno
= xerrno
;
148 if (! ccb
->completed
)
149 dlinkAddTail(ccb
, &ccb
->node
, &commfd_completed_events
);
150 ccb
->completed
= true;
155 * Cancel the given callback
157 * Remember that the data is cbdataRef'ed.
160 commio_cancel_callback(int fd
, comm_io_callback_t
*ccb
)
162 debugs(5, 3, "commio_cancel_callback: called for " << fd
);
163 assert(ccb
->fd
== fd
);
164 assert(ccb
->active
== true);
166 if (ccb
->completed
== true) {
167 dlinkDelete(&ccb
->node
, &commfd_completed_events
);
169 if (ccb
->callback_data
)
170 cbdataReferenceDone(ccb
->callback_data
);
174 ccb
->completed
= false;
175 ccb
->callback
= NULL
;
176 ccb
->callback_data
= NULL
;
180 * Call the given comm callback; assumes the callback is valid.
182 * @param ccb io completion callback
185 commio_call_callback(comm_io_callback_t
*ccb
)
187 comm_io_callback_t cb
= *ccb
;
189 assert(cb
.active
== true);
190 assert(cb
.completed
== true);
191 debugs(5, 3, "commio_call_callback: called for " << ccb
->fd
);
193 /* We've got a copy; blow away the real one */
194 /* XXX duplicate code from commio_cancel_callback! */
195 dlinkDelete(&ccb
->node
, &commfd_completed_events
);
198 ccb
->completed
= false;
199 ccb
->callback
= NULL
;
200 ccb
->callback_data
= NULL
;
207 if (cb
.callback
&& cbdataReferenceValidDone(cb
.callback_data
, &cbdata
)) {
208 /* XXX truely ugly for now! */
209 cb
.callback(cb
.fd
, cb
.buf
, cb
.offset
, cb
.errcode
, cb
.xerrno
, cbdata
);
214 commio_call_callbacks(void)
216 comm_io_callback_t
*ccb
;
217 while (commfd_completed_events
.head
!= NULL
) {
218 ccb
= (comm_io_callback_t
*) commfd_completed_events
.head
->data
;
219 commio_call_callback(ccb
);
224 class ConnectStateData
228 void *operator new (size_t);
229 void operator delete (void *);
230 static void Connect (int fd
, void *me
);
232 void callCallback(comm_err_t status
, int xerrno
);
235 // defaults given by client
237 u_short default_port
;
238 IPAddress default_addr
;
239 // NP: CANNOT store the default addr:port together as it gets set/reset differently.
242 CallBack
<CNCB
> callback
;
251 int commRetryConnect();
252 CBDATA_CLASS(ConnectStateData
);
257 static comm_err_t
commBind(int s
, struct addrinfo
&);
258 static void commSetReuseAddr(int);
259 static void commSetNoLinger(int);
261 static void commSetTcpNoDelay(int);
263 static void commSetTcpRcvbuf(int, int);
264 static PF commConnectFree
;
265 static PF commHandleWrite
;
266 static IPH commConnectDnsHandle
;
267 static void requireOpenAndActive(int const fd
);
269 static PF comm_accept_try
;
275 AcceptFD() : count(0), finished_(false){}
277 void doCallback(int fd
, int newfd
, comm_err_t errcode
, int xerrno
, ConnectionDetail
*);
279 void beginAccepting() {count
= 0; finished(false);}
281 size_t acceptCount() const { return count
;}
283 bool finishedAccepting() const;
284 CallBack
<IOACB
> callback
;
285 bool finished() const;
289 static size_t const MAX_ACCEPT_PER_LOOP
;
294 size_t const AcceptFD::MAX_ACCEPT_PER_LOOP(10);
300 void acceptOne(int fd
);
301 void beginAccepting();
302 int acceptCount() const;
303 fdc_t() : active(0), fd(-1), half_closed (false){CommCallbackList
.head
= NULL
;CommCallbackList
.tail
= NULL
; }
305 fdc_t(int anFD
) : active(0), fd(anFD
), half_closed(false)
307 CommCallbackList
.head
= NULL
;
308 CommCallbackList
.tail
= NULL
;
313 dlink_list CommCallbackList
;
316 bool findCallback(P predicate
);
323 ConnectionDetail connDetails
;
336 static int CommCallbackSeqnum
= 1;
338 class CommCommonCallback
342 CommCommonCallback() : fd (-1), errcode (COMM_OK
), xerrno(0), seqnum (CommCallbackSeqnum
){}
344 CommCommonCallback(int anFD
, comm_err_t errcode
, int anErrno
) : fd (anFD
), errcode (errcode
), xerrno(anErrno
), seqnum (CommCallbackSeqnum
){}
352 class CommCallbackData
356 MEMPROXY_CLASS(CommCallbackData
);
357 CommCallbackData(CommCommonCallback
const &);
358 virtual ~CommCallbackData() {}
360 virtual comm_callback_t
getType() const { return COMM_CB_DERIVED
; }
362 void callACallback();
364 virtual void callCallback() = 0;
366 void deRegisterSelf();
371 CommCommonCallback result
;
372 friend void _comm_close(int fd
, char const *file
, int line
);
373 friend void comm_calliocallback(void);
380 MEMPROXY_CLASS_INLINE(CommCallbackData
)
382 class CommAcceptCallbackData
: public CommCallbackData
386 MEMPROXY_CLASS(CommAcceptCallbackData
);
387 CommAcceptCallbackData(int const anFd
, CallBack
<IOACB
>, comm_err_t
, int, int, ConnectionDetail
const &);
388 virtual void callCallback();
391 CallBack
<IOACB
> callback
;
393 ConnectionDetail details
;
396 MEMPROXY_CLASS_INLINE(CommAcceptCallbackData
)
398 class CommFillCallbackData
: public CommCallbackData
402 MEMPROXY_CLASS(CommFillCallbackData
);
403 CommFillCallbackData(int const anFd
, CallBack
<IOFCB
> aCallback
, comm_err_t
, int);
404 virtual void callCallback();
407 CallBack
<IOFCB
> callback
;
410 MEMPROXY_CLASS_INLINE(CommFillCallbackData
)
414 char const *close_file
;
418 typedef struct _fd_debug_t fd_debug_t
;
420 static MemAllocator
*conn_close_pool
= NULL
;
421 fdc_t
*fdc_table
= NULL
;
422 fd_debug_t
*fdd_table
= NULL
;
423 dlink_list CommCallbackList
;
426 /* New and improved stuff */
428 CommCallbackData::CommCallbackData(CommCommonCallback
const &newResults
) : result (newResults
)
430 assert(fdc_table
[result
.fd
].active
== 1);
434 CommAcceptCallbackData::CommAcceptCallbackData(int const anFd
, CallBack
<IOACB
> aCallback
, comm_err_t anErrcode
, int anErrno
, int aNewFD
, ConnectionDetail
const &newDetails
) :CommCallbackData(CommCommonCallback(anFd
, anErrcode
, anErrno
)), callback (aCallback
), newfd(aNewFD
), details(newDetails
)
438 CommCallbackData::registerSelf()
440 /* Add it to the end of the list */
441 dlinkAddTail(this, &h_node
, &CommCallbackList
);
443 /* and add it to the end of the fd list */
444 dlinkAddTail(this, &fd_node
, &(fdc_table
[result
.fd
].CommCallbackList
));
448 CommCallbackData::deRegisterSelf()
450 dlinkDelete(&h_node
, &CommCallbackList
);
451 dlinkDelete(&fd_node
, &(fdc_table
[result
.fd
].CommCallbackList
));
457 * IO callbacks are added when we want to notify someone that some IO
458 * has finished but we don't want to risk re-entering a non-reentrant
462 CommAcceptCallbackData::callCallback()
464 PROF_start(CommAcceptCallbackData_callCallback
);
465 callback
.handler(result
.fd
, newfd
, &details
, result
.errcode
, result
.xerrno
, callback
.data
);
466 PROF_stop(CommAcceptCallbackData_callCallback
);
470 CommCallbackData::fdClosing()
472 result
.errcode
= COMM_ERR_CLOSING
;
476 CommCallbackData::callACallback()
478 assert(fdc_table
[result
.fd
].active
== 1);
484 * call the IO callbacks
486 * This should be called before comm_select() so code can attempt to
489 * When io callbacks are added, they are added with the current
490 * sequence number. The sequence number is incremented in this routine -
491 * since callbacks are added to the _tail_ of the list, when we hit a
492 * callback with a seqnum _not_ what it was when we entered this routine,
496 comm_calliocallback(void)
498 CommCallbackData
*cio
;
499 int oldseqnum
= CommCallbackSeqnum
++;
501 /* Call our callbacks until we hit NULL or the seqnum changes */
503 /* This will likely rap other counts - again, thats ok (for now)
504 * What we should see is the total of the various callback subclasses
505 * equaling this counter.
506 * If they don't, someone has added a class but not profiled it.
508 PROF_start(comm_calliocallback
);
510 debugs(5, 7, "comm_calliocallback: " << CommCallbackList
.head
);
512 while (CommCallbackList
.head
!= NULL
&& oldseqnum
!= ((CommCallbackData
*)CommCallbackList
.head
->data
)->result
.seqnum
) {
513 dlink_node
*node
= (dlink_node
*)CommCallbackList
.head
;
514 cio
= (CommCallbackData
*)node
->data
;
515 cio
->callACallback();
519 PROF_stop(comm_calliocallback
);
523 comm_iocallbackpending(void)
525 debugs(5, 7, "comm_iocallbackpending: " << CommCallbackList
.head
);
526 return (CommCallbackList
.head
!= NULL
) || (commfd_completed_events
.head
!= NULL
);
532 * If the read attempt succeeds or fails, call the callback.
533 * Else, wait for another IO notification.
536 commHandleRead(int fd
, void *data
)
538 comm_io_callback_t
*ccb
= (comm_io_callback_t
*) data
;
540 assert(data
== COMMIO_FD_READCB(fd
));
541 assert(commio_has_callback(fd
, IOCB_READ
, ccb
));
543 statCounter
.syscalls
.sock
.reads
++;
546 retval
= FD_READ_METHOD(fd
, ccb
->buf
, ccb
->size
);
547 debugs(5, 3, "comm_read_try: FD " << fd
<< ", size " << ccb
->size
<< ", retval " << retval
<< ", errno " << errno
);
549 if (retval
< 0 && !ignoreErrno(errno
)) {
550 debugs(5, 3, "comm_read_try: scheduling COMM_ERROR");
552 commio_complete_callback(fd
, ccb
, COMM_ERROR
, errno
);
556 /* See if we read anything */
557 /* Note - read 0 == socket EOF, which is a valid read */
559 fd_bytes(fd
, retval
, FD_READ
);
560 ccb
->offset
= retval
;
561 commio_complete_callback(fd
, ccb
, COMM_OK
, errno
);
565 /* Nope, register for some more IO */
566 commSetSelect(fd
, COMM_SELECT_READ
, commHandleRead
, data
, 0);
570 * Queue a read. handler/handler_data are called when the read
571 * completes, on error, or on file descriptor close.
574 comm_read(int fd
, char *buf
, int size
, IOCB
*handler
, void *handler_data
)
576 /* Make sure we're not reading anything and we're not closing */
577 assert(fdc_table
[fd
].active
== 1);
578 assert(!fd_table
[fd
].flags
.closing
);
580 debugs(5, 4, "comm_read, queueing read for FD " << fd
);
584 commio_set_callback(fd
, IOCB_READ
, COMMIO_FD_READCB(fd
), handler
, handler_data
, (char *)buf
, NULL
, size
);
585 commSetSelect(fd
, COMM_SELECT_READ
, commHandleRead
, COMMIO_FD_READCB(fd
), 0);
589 * Empty the read buffers
591 * This is a magical routine that empties the read buffers.
592 * Under some platforms (Linux) if a buffer has data in it before
593 * you call close(), the socket will hang and take quite a while
597 comm_empty_os_read_buffers(int fd
)
600 /* prevent those nasty RST packets */
601 char buf
[SQUID_TCP_SO_RCVBUF
];
603 if (fd_table
[fd
].flags
.nonblocking
== 1) {
604 while (FD_READ_METHOD(fd
, buf
, SQUID_TCP_SO_RCVBUF
) > 0) {};
610 requireOpenAndActive(int const fd
)
612 assert(fd_table
[fd
].flags
.open
== 1);
613 assert(fdc_table
[fd
].active
== 1);
617 * Return whether the FD has a pending completed callback.
620 comm_has_pending_read_callback(int fd
)
622 requireOpenAndActive(fd
);
623 return COMMIO_FD_READCB(fd
)->active
&& COMMIO_FD_READCB(fd
)->completed
;
628 fdc_t::findCallback(P predicate
)
631 * XXX I don't like having to walk the list!
632 * Instead, if this routine is called often enough, we should
633 * also maintain a linked list of _read_ events - we can just
634 * check if the list head a HEAD..
637 dlink_node
*node
= CommCallbackList
.head
;
639 while (node
!= NULL
) {
640 if (predicate((CommCallbackData
*)node
->data
))
651 * return whether a file descriptor has a read handler
653 * Assumptions: the fd is open
654 * the fd is a comm fd.
656 * Again - is this "pending read", or "pending completed event", or what?
657 * I'll assume its pending read, not pending completed.
659 * This makes no sense though - if this is called to check whether there's
660 * a pending read -before- submitting a read then it won't matter whether
661 * its completed or not! Ie:
663 * + if there's no read and you want to schedule one; fine.
664 * + if a read has completed then the callback block has been deactivated before
665 * the callback is called - if something decides to register for a read
666 * callback once again it should find !active and !completed.
667 * + scheduling a read event when the fd is ! active -and- completed, thats
669 * + like, afaict, anything else is.
672 comm_has_pending_read(int fd
)
674 requireOpenAndActive(fd
);
675 return COMMIO_FD_READCB(fd
)->active
&& (! COMMIO_FD_READCB(fd
)->completed
);
679 * Cancel a pending read. Assert that we have the right parameters,
680 * and that there are no pending read events!
682 * AHC Don't call the comm handlers?
685 comm_read_cancel(int fd
, IOCB
*callback
, void *data
)
687 requireOpenAndActive(fd
);
689 /* Ok, we can be reasonably sure we won't lose any data here! */
690 assert(COMMIO_FD_READCB(fd
)->callback
== callback
);
691 assert(COMMIO_FD_READCB(fd
)->callback_data
== data
);
693 /* Delete the callback */
694 commio_cancel_callback(fd
, COMMIO_FD_READCB(fd
));
696 /* And the IO event */
697 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
702 * Open a filedescriptor, set some sane defaults
703 * XXX DPW 2006-05-30 what is the point of this?
706 fdc_open(int fd
, unsigned int type
, char const *desc
)
708 assert(fdc_table
[fd
].active
== 0);
710 fdc_table
[fd
].active
= 1;
711 fdc_table
[fd
].fd
= fd
;
712 fd_open(fd
, type
, desc
);
717 * synchronous wrapper around udp socket functions
720 comm_udp_recvfrom(int fd
, void *buf
, size_t len
, int flags
, IPAddress
&from
)
722 statCounter
.syscalls
.sock
.recvfroms
++;
724 struct addrinfo
*AI
= NULL
;
726 debugs(5,8, "comm_udp_recvfrom: FD " << fd
<< " from " << from
);
728 assert( NULL
== AI
);
730 from
.InitAddrInfo(AI
);
732 x
= recvfrom(fd
, buf
, len
, flags
, AI
->ai_addr
, &AI
->ai_addrlen
);
736 from
.FreeAddrInfo(AI
);
742 comm_udp_recv(int fd
, void *buf
, size_t len
, int flags
)
745 return comm_udp_recvfrom(fd
, buf
, len
, flags
, nul
);
749 comm_udp_send(int s
, const void *buf
, size_t len
, int flags
)
751 return send(s
, buf
, len
, flags
);
756 comm_has_incomplete_write(int fd
)
758 requireOpenAndActive(fd
);
759 return COMMIO_FD_WRITECB(fd
)->active
;
763 * Queue a write. handler/handler_data are called when the write fully
764 * completes, on error, or on file descriptor close.
767 /* Return the local port associated with fd. */
769 comm_local_port(int fd
)
772 struct addrinfo
*addr
= NULL
;
773 fde
*F
= &fd_table
[fd
];
775 /* If the fd is closed already, just return */
777 if (!F
->flags
.open
) {
778 debugs(5, 0, "comm_local_port: FD " << fd
<< " has been closed.");
782 if (F
->local_addr
.GetPort())
783 return F
->local_addr
.GetPort();
785 temp
.InitAddrInfo(addr
);
787 if (getsockname(fd
, addr
->ai_addr
, &(addr
->ai_addrlen
)) ) {
788 debugs(50, 1, "comm_local_port: Failed to retrieve TCP/UDP port number for socket: FD " << fd
<< ": " << xstrerror());
789 temp
.FreeAddrInfo(addr
);
794 temp
.FreeAddrInfo(addr
);
796 F
->local_addr
.SetPort(temp
.GetPort());
798 // grab default socket information for this address
799 temp
.GetAddrInfo(addr
);
801 F
->sock_family
= addr
->ai_family
;
803 temp
.FreeAddrInfo(addr
);
805 debugs(5, 6, "comm_local_port: FD " << fd
<< ": port " << F
->local_addr
.GetPort());
806 return F
->local_addr
.GetPort();
810 commBind(int s
, struct addrinfo
&inaddr
)
812 statCounter
.syscalls
.sock
.binds
++;
814 if (bind(s
, inaddr
.ai_addr
, inaddr
.ai_addrlen
) == 0)
817 debugs(50, 0, "commBind: Cannot bind socket FD " << s
<< " to " << fd_table
[s
].local_addr
<< ": " << xstrerror());
823 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
824 * is OR of flags specified in comm.h. Defaults TOS
827 comm_open(int sock_type
,
833 return comm_openex(sock_type
, proto
, addr
, flags
, 0, note
);
837 limitError(int const anErrno
)
839 return anErrno
== ENFILE
|| anErrno
== EMFILE
;
843 comm_set_tos(int fd
, int tos
)
846 int x
= setsockopt(fd
, IPPROTO_IP
, IP_TOS
, (char *) &tos
, sizeof(int));
848 debugs(50, 1, "comm_set_tos: setsockopt(IP_TOS) on FD " << fd
<< ": " << xstrerror());
851 debugs(50, 0, "WARNING: setsockopt(IP_TOS) not supported on this platform");
857 comm_set_v6only(int fd
, int tos
)
860 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, (char *) &tos
, sizeof(int)) < 0) {
861 debugs(50, 1, "comm_open: setsockopt(IPV6_V6ONLY) on FD " << fd
<< ": " << xstrerror());
864 debugs(50, 0, "WARNING: comm_open: setsockopt(IPV6_V6ONLY) not supported on this platform");
869 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
870 * is OR of flags specified in defines.h:COMM_*
873 comm_openex(int sock_type
,
883 struct addrinfo
*AI
= NULL
;
885 PROF_start(comm_open
);
886 /* Create socket for accepting new connections. */
887 statCounter
.syscalls
.sock
.sockets
++;
889 /* Setup the socket addrinfo details for use */
890 addr
.GetAddrInfo(AI
);
891 AI
->ai_socktype
= sock_type
;
892 AI
->ai_protocol
= proto
;
893 AI
->ai_flags
= flags
;
895 debugs(50, 3, "comm_openex: Attempt open socket for: " << addr
);
897 if ((new_socket
= socket(AI
->ai_family
, AI
->ai_socktype
, AI
->ai_protocol
)) < 0)
899 /* Increase the number of reserved fd's if calls to socket()
900 * are failing because the open file table is full. This
901 * limits the number of simultaneous clients */
903 if (limitError(errno
)) {
904 debugs(50, 1, "comm_open: socket failure: " << xstrerror());
907 debugs(50, 0, "comm_open: socket failure: " << xstrerror());
910 addr
.FreeAddrInfo(AI
);
912 PROF_stop(comm_open
);
916 debugs(50, 3, "comm_openex: Opened socket FD " << new_socket
<< " : family=" << AI
->ai_family
<< ", type=" << AI
->ai_socktype
<< ", protocol=" << AI
->ai_protocol
);
918 /* set TOS if needed */
919 if (TOS
&& comm_set_tos(new_socket
, TOS
) ) {
923 #if IPV6_SPECIAL_SPLITSTACK
926 comm_set_v6only(new_socket
, tos
);
930 #if IPV6_SPECIAL_V4MAPPED && defined(_SQUID_MSWIN_)
932 /* Windows Vista supports Dual-Sockets. BUT defaults them to V6ONLY. Turn it OFF. */
934 comm_set_v6only(new_socket
, 0);
939 debugs(5, 5, "comm_open: FD " << new_socket
<< " is a new socket");
941 fd_open(new_socket
, FD_SOCKET
, note
);
943 fdd_table
[new_socket
].close_file
= NULL
;
945 fdd_table
[new_socket
].close_line
= 0;
947 assert(fdc_table
[new_socket
].active
== 0);
949 fdc_table
[new_socket
].active
= 1;
951 F
= &fd_table
[new_socket
];
953 F
->local_addr
= addr
;
957 F
->sock_family
= AI
->ai_family
;
959 if (!(flags
& COMM_NOCLOEXEC
))
960 commSetCloseOnExec(new_socket
);
962 if ((flags
& COMM_REUSEADDR
))
963 commSetReuseAddr(new_socket
);
965 if (addr
.GetPort() > (u_short
) 0)
969 if (sock_type
!= SOCK_DGRAM
)
972 commSetNoLinger(new_socket
);
975 commSetReuseAddr(new_socket
);
978 if (!addr
.IsNoAddr())
980 if (commBind(new_socket
, *AI
) != COMM_OK
) {
981 comm_close(new_socket
);
982 addr
.FreeAddrInfo(AI
);
984 PROF_stop(comm_open
);
988 addr
.FreeAddrInfo(AI
);
990 if (flags
& COMM_NONBLOCKING
)
991 if (commSetNonBlocking(new_socket
) == COMM_ERROR
)
994 PROF_stop(comm_open
);
998 if (sock_type
== SOCK_STREAM
)
999 commSetTcpNoDelay(new_socket
);
1003 if (Config
.tcpRcvBufsz
> 0 && sock_type
== SOCK_STREAM
)
1004 commSetTcpRcvbuf(new_socket
, Config
.tcpRcvBufsz
);
1006 PROF_stop(comm_open
);
1011 CBDATA_CLASS_INIT(ConnectStateData
);
1014 ConnectStateData::operator new (size_t size
)
1016 CBDATA_INIT_TYPE(ConnectStateData
);
1017 return cbdataAlloc(ConnectStateData
);
1021 ConnectStateData::operator delete (void *address
)
1023 cbdataFree(address
);
1027 commConnectStart(int fd
, const char *host
, u_short port
, CNCB
* callback
, void *data
)
1029 ConnectStateData
*cs
;
1030 debugs(5, 3, "commConnectStart: FD " << fd
<< ", data " << data
<< ", " << host
<< ":" << port
);
1031 cs
= new ConnectStateData
;
1033 cs
->host
= xstrdup(host
);
1034 cs
->default_port
= port
;
1035 cs
->callback
= CallBack
<CNCB
>(callback
, data
);
1036 comm_add_close_handler(fd
, commConnectFree
, cs
);
1037 ipcache_nbgethostbyname(host
, commConnectDnsHandle
, cs
);
1041 commConnectDnsHandle(const ipcache_addrs
* ia
, void *data
)
1043 ConnectStateData
*cs
= (ConnectStateData
*)data
;
1046 debugs(5, 3, "commConnectDnsHandle: Unknown host: " << cs
->host
);
1048 if (!dns_error_message
) {
1049 dns_error_message
= "Unknown DNS error";
1050 debugs(5, 1, "commConnectDnsHandle: Bad dns_error_message");
1053 assert(dns_error_message
!= NULL
);
1054 cs
->callCallback(COMM_ERR_DNS
, 0);
1058 assert(ia
->cur
< ia
->count
);
1060 cs
->default_addr
= ia
->in_addrs
[ia
->cur
];
1062 if (Config
.onoff
.balance_on_multiple_ip
)
1063 ipcacheCycleAddr(cs
->host
, NULL
);
1065 cs
->addrcount
= ia
->count
;
1067 cs
->connstart
= squid_curtime
;
1073 ConnectStateData::callCallback(comm_err_t status
, int xerrno
)
1075 debugs(5, 3, "commConnectCallback: FD " << fd
<< ", data " << callback
.data
<< ", status " << status
);
1077 comm_remove_close_handler(fd
, commConnectFree
, this);
1078 CallBack
<CNCB
> aCallback
= callback
;
1079 callback
= CallBack
<CNCB
>();
1080 commSetTimeout(fd
, -1, NULL
, NULL
);
1082 if (aCallback
.dataValid())
1083 aCallback
.handler(fd
, status
, xerrno
, aCallback
.data
);
1085 commConnectFree(fd
, this);
1089 commConnectFree(int fd
, void *data
)
1091 ConnectStateData
*cs
= (ConnectStateData
*)data
;
1092 debugs(5, 3, "commConnectFree: FD " << fd
);
1093 cs
->callback
= CallBack
<CNCB
>();
1094 safe_free(cs
->host
);
1099 copyFDFlags(int to
, fde
*F
)
1101 if (F
->flags
.close_on_exec
)
1102 commSetCloseOnExec(to
);
1104 if (F
->flags
.nonblocking
)
1105 commSetNonBlocking(to
);
1109 if (F
->flags
.nodelay
)
1110 commSetTcpNoDelay(to
);
1114 if (Config
.tcpRcvBufsz
> 0)
1115 commSetTcpRcvbuf(to
, Config
.tcpRcvBufsz
);
1118 /* Reset FD so that we can connect() again */
1120 ConnectStateData::commResetFD()
1122 struct addrinfo
*AI
= NULL
;
1125 if (!cbdataReferenceValid(callback
.data
))
1128 statCounter
.syscalls
.sock
.sockets
++;
1130 /* setup a bare-bones addrinfo */
1131 nul
.GetAddrInfo(AI
);
1133 int fd2
= socket(AI
->ai_family
, AI
->ai_socktype
, AI
->ai_protocol
);
1135 nul
.FreeAddrInfo(AI
);
1138 debugs(5, 0, "commResetFD: socket: " << xstrerror());
1140 if (ENFILE
== errno
|| EMFILE
== errno
)
1146 #ifdef _SQUID_MSWIN_
1148 /* On Windows dup2() can't work correctly on Sockets, the */
1149 /* workaround is to close the destination Socket before call them. */
1154 if (dup2(fd2
, fd
) < 0) {
1155 debugs(5, 0, "commResetFD: dup2: " << xstrerror());
1157 if (ENFILE
== errno
|| EMFILE
== errno
)
1166 fde
*F
= &fd_table
[fd
];
1167 fd_table
[fd
].flags
.called_connect
= 0;
1169 * yuck, this has assumptions about comm_open() arguments for
1170 * the original socket
1174 F
->local_addr
.GetAddrInfo(AI
);
1176 if (commBind(fd
, *AI
) != COMM_OK
) {
1177 debugs(5, 0, "commResetFD: bind: " << xstrerror());
1178 F
->local_addr
.FreeAddrInfo(AI
);
1181 F
->local_addr
.FreeAddrInfo(AI
);
1184 comm_set_tos(fd
, F
->tos
);
1186 #if IPV6_SPECIAL_SPLITSTACK
1188 if( F
->local_addr
.IsIPv6() )
1189 comm_set_v6only(fd
, F
->tos
);
1193 copyFDFlags (fd
, F
);
1199 ConnectStateData::commRetryConnect()
1201 assert(addrcount
> 0);
1203 if (addrcount
== 1) {
1204 if (tries
>= Config
.retry
.maxtries
)
1207 if (squid_curtime
- connstart
> Config
.Timeout
.connect
)
1210 if (tries
> addrcount
)
1214 return commResetFD();
1218 commReconnect(void *data
)
1220 ConnectStateData
*cs
= (ConnectStateData
*)data
;
1221 ipcache_nbgethostbyname(cs
->host
, commConnectDnsHandle
, cs
);
1224 /* Connect SOCK to specified DEST_PORT at DEST_HOST. */
1226 ConnectStateData::Connect (int fd
, void *me
)
1228 ConnectStateData
*cs
= (ConnectStateData
*)me
;
1229 assert (cs
->fd
== fd
);
1234 ConnectStateData::defaults()
1237 S
.SetPort(default_port
);
1241 ConnectStateData::connect()
1246 debugs(5,5, "ConnectSateData::connect: to " << S
);
1248 switch (comm_connect_addr(fd
, S
) ) {
1250 case COMM_INPROGRESS
:
1251 debugs(5, 5, "ConnectStateData::connect: FD " << fd
<< ": COMM_INPROGRESS");
1252 commSetSelect(fd
, COMM_SELECT_WRITE
, ConnectStateData::Connect
, this, 0);
1256 debugs(5, 5, "ConnectStateData::connect: FD " << fd
<< ": COMM_OK - connected");
1257 ipcacheMarkGoodAddr(host
, S
);
1258 callCallback(COMM_OK
, 0);
1262 debugs(5, 5, "ConnectStateData::connect: FD " << fd
<< ": * - try again");
1264 ipcacheMarkBadAddr(host
, S
);
1266 if (Config
.onoff
.test_reachability
)
1267 netdbDeleteAddrNetwork(S
);
1269 if (commRetryConnect()) {
1270 eventAdd("commReconnect", commReconnect
, this, this->addrcount
== 1 ? 0.05 : 0.0, 0);
1272 debugs(5, 5, "ConnectStateData::connect: FD " << fd
<< ": * - ERR tried too many times already.");
1273 callCallback(COMM_ERR_CONNECT
, errno
);
1279 commSetTimeout(int fd
, int timeout
, PF
* handler
, void *data
)
1281 debugs(5, 3, "commSetTimeout: FD " << fd
<< " timeout " << timeout
);
1283 assert(fd
< Squid_MaxFD
);
1284 fde
*F
= &fd_table
[fd
];
1285 assert(F
->flags
.open
);
1288 cbdataReferenceDone(F
->timeout_data
);
1289 F
->timeout_handler
= NULL
;
1293 cbdataReferenceDone(F
->timeout_data
);
1294 F
->timeout_handler
= handler
;
1295 F
->timeout_data
= cbdataReference(data
);
1298 F
->timeout
= squid_curtime
+ (time_t) timeout
;
1305 comm_connect_addr(int sock
, const IPAddress
&address
)
1307 comm_err_t status
= COMM_OK
;
1308 fde
*F
= &fd_table
[sock
];
1312 struct addrinfo
*AI
= NULL
;
1313 PROF_start(comm_connect_addr
);
1315 assert(address
.GetPort() != 0);
1317 debugs(5, 9, "comm_connect_addr: connecting socket " << sock
<< " to " << address
<< " (want family: " << F
->sock_family
<<
1318 ") Old-State=" << fdc_table
[sock
].active
);
1320 address
.GetAddrInfo(AI
, F
->sock_family
);
1322 /* Establish connection. */
1325 if (!F
->flags
.called_connect
)
1327 F
->flags
.called_connect
= 1;
1328 statCounter
.syscalls
.sock
.connects
++;
1330 x
= connect(sock
, AI
->ai_addr
, AI
->ai_addrlen
);
1334 debugs(5,5, "comm_connect_addr: sock=" << sock
<< ", addrinfo( " <<
1335 " flags=" << AI
->ai_flags
<<
1336 ", family=" << AI
->ai_family
<<
1337 ", socktype=" << AI
->ai_socktype
<<
1338 ", protocol=" << AI
->ai_protocol
<<
1339 ", &addr=" << AI
->ai_addr
<<
1340 ", addrlen=" << AI
->ai_addrlen
<<
1342 debugs(5, 9, "connect FD " << sock
<< ": (" << x
<< ") " << xstrerror());
1343 debugs(14,9, "connecting to: " << address
);
1347 #if defined(_SQUID_NEWSOS6_)
1348 /* Makoto MATSUSHITA <matusita@ics.es.osaka-u.ac.jp> */
1350 connect(sock
, AI
->ai_addr
, AI
->ai_addrlen
);
1352 if (errno
== EINVAL
) {
1353 errlen
= sizeof(err
);
1354 x
= getsockopt(sock
, SOL_SOCKET
, SO_ERROR
, &err
, &errlen
);
1361 errlen
= sizeof(err
);
1363 x
= getsockopt(sock
, SOL_SOCKET
, SO_ERROR
, &err
, &errlen
);
1368 #if defined(_SQUID_SOLARIS_)
1370 * Solaris 2.4's socket emulation doesn't allow you
1371 * to determine the error from a failed non-blocking
1372 * connect and just returns EPIPE. Create a fake
1373 * error message for connect. -- fenner@parc.xerox.com
1375 if (x
< 0 && errno
== EPIPE
)
1383 #ifdef _SQUID_LINUX_
1385 * Linux Debian replaces our allocated AI pointer with garbage when
1386 * connect() fails. This leads to segmentation faults deallocating
1387 * the system-allocated memory when we go to clean up our pointer.
1388 * HACK: is to leak the memory returned since we can't deallocate.
1395 address
.FreeAddrInfo(AI
);
1397 PROF_stop(comm_connect_addr
);
1399 if (errno
== 0 || errno
== EISCONN
)
1401 else if (ignoreErrno(errno
))
1402 status
= COMM_INPROGRESS
;
1405 if( address
.IsIPv4() && F
->sock_family
== AF_INET6
) {
1407 /* failover to trying IPv4-only link if an IPv6 one fails */
1408 /* to catch the edge case of apps listening on IPv4-localhost */
1409 F
->sock_family
= AF_INET
;
1410 int res
= comm_connect_addr(sock
, address
);
1412 /* if that fails too, undo our temporary socktype hack so the repeat works properly. */
1413 if(res
== COMM_ERROR
)
1414 F
->sock_family
= AF_INET6
;
1422 address
.NtoA(F
->ipaddr
, MAX_IPSTRLEN
);
1424 F
->remote_port
= address
.GetPort(); /* remote_port is HS */
1426 if (status
== COMM_OK
)
1428 debugs(5, 10, "comm_connect_addr: FD " << sock
<< " connected to " << address
);
1429 } else if (status
== COMM_INPROGRESS
)
1431 debugs(5, 10, "comm_connect_addr: FD " << sock
<< " connection pending");
1437 /* Wait for an incoming connection on FD. FD should be a socket returned
1438 * from comm_listen. */
1440 comm_old_accept(int fd
, ConnectionDetail
&details
)
1442 PROF_start(comm_accept
);
1443 statCounter
.syscalls
.sock
.accepts
++;
1445 struct addrinfo
*gai
= NULL
;
1446 details
.me
.InitAddrInfo(gai
);
1448 if ((sock
= accept(fd
, gai
->ai_addr
, &gai
->ai_addrlen
)) < 0) {
1450 details
.me
.FreeAddrInfo(gai
);
1452 PROF_stop(comm_accept
);
1454 if (ignoreErrno(errno
))
1456 debugs(50, 5, "comm_old_accept: FD " << fd
<< ": " << xstrerror());
1457 return COMM_NOMESSAGE
;
1458 } else if (ENFILE
== errno
|| EMFILE
== errno
)
1460 debugs(50, 3, "comm_old_accept: FD " << fd
<< ": " << xstrerror());
1464 debugs(50, 1, "comm_old_accept: FD " << fd
<< ": " << xstrerror());
1469 details
.peer
= *gai
;
1471 details
.me
.SetEmpty();
1472 getsockname(sock
, gai
->ai_addr
, &gai
->ai_addrlen
);
1475 commSetCloseOnExec(sock
);
1478 fd_open(sock
, FD_SOCKET
, "HTTP Request");
1479 fdd_table
[sock
].close_file
= NULL
;
1480 fdd_table
[sock
].close_line
= 0;
1481 fdc_table
[sock
].active
= 1;
1482 fde
*F
= &fd_table
[sock
];
1483 details
.peer
.NtoA(F
->ipaddr
,MAX_IPSTRLEN
);
1484 F
->remote_port
= details
.peer
.GetPort();
1485 F
->local_addr
.SetPort(details
.me
.GetPort());
1486 F
->sock_family
= gai
->ai_family
;
1488 commSetNonBlocking(sock
);
1490 details
.me
.FreeAddrInfo(gai
);
1492 PROF_stop(comm_accept
);
1497 commCallCloseHandlers(int fd
)
1499 fde
*F
= &fd_table
[fd
];
1500 debugs(5, 5, "commCallCloseHandlers: FD " << fd
);
1502 while (F
->closeHandler
!= NULL
) {
1503 close_handler ch
= *F
->closeHandler
;
1504 conn_close_pool
->free(F
->closeHandler
); /* AAA */
1505 F
->closeHandler
= ch
.next
;
1507 debugs(5, 5, "commCallCloseHandlers: ch->handler=" << ch
.handler
<< " data=" << ch
.data
);
1509 if (cbdataReferenceValid(ch
.data
))
1510 ch
.handler(fd
, ch
.data
);
1512 cbdataReferenceDone(ch
.data
);
1518 commLingerClose(int fd
, void *unused
)
1520 LOCAL_ARRAY(char, buf
, 1024);
1522 n
= FD_READ_METHOD(fd
, buf
, 1024);
1525 debugs(5, 3, "commLingerClose: FD " << fd
<< " read: " << xstrerror());
1531 commLingerTimeout(int fd
, void *unused
)
1533 debugs(5, 3, "commLingerTimeout: FD " << fd
);
1538 * Inspired by apache
1541 comm_lingering_close(int fd
)
1545 if (fd_table
[fd
].ssl
)
1546 ssl_shutdown_method(fd
);
1550 if (shutdown(fd
, 1) < 0) {
1555 fd_note(fd
, "lingering close");
1556 commSetTimeout(fd
, 10, commLingerTimeout
, NULL
);
1557 commSetSelect(fd
, COMM_SELECT_READ
, commLingerClose
, NULL
, 0);
1563 * enable linger with time of 0 so that when the socket is
1564 * closed, TCP generates a RESET
1567 comm_reset_close(int fd
)
1574 if (setsockopt(fd
, SOL_SOCKET
, SO_LINGER
, (char *) &L
, sizeof(L
)) < 0)
1575 debugs(50, 0, "commResetTCPClose: FD " << fd
<< ": " << xstrerror());
1581 CommRead::nullCallback()
1583 callback
= CallBack
<IOCB
>();
1587 AcceptFD::nullCallback()
1589 callback
= CallBack
<IOACB
>();
1593 CommRead::doCallback(comm_err_t errcode
, int xerrno
)
1595 if (callback
.handler
)
1596 callback
.handler(fd
, buf
, 0, errcode
, xerrno
, callback
.data
);
1602 AcceptFD::doCallback(int fd
, int newfd
, comm_err_t errcode
, int xerrno
, ConnectionDetail
*connDetails
)
1604 if (callback
.handler
) {
1605 CallBack
<IOACB
> aCallback
= callback
;
1607 aCallback
.handler(fd
, newfd
, connDetails
, errcode
, xerrno
, aCallback
.data
);
1612 * Close the socket fd.
1614 * + call write handlers with ERR_CLOSING
1615 * + call read handlers with ERR_CLOSING
1616 * + call closing handlers
1618 * NOTE: COMM_ERR_CLOSING will NOT be called for CommReads' sitting in a
1619 * DeferredReadManager.
1622 _comm_close(int fd
, char const *file
, int line
)
1626 CommCallbackData
*cio
;
1628 debugs(5, 5, "comm_close: FD " << fd
);
1630 assert(fd
< Squid_MaxFD
);
1632 fdd_table
[fd
].close_file
= file
;
1633 fdd_table
[fd
].close_line
= line
;
1635 if (F
->flags
.closing
)
1638 if (shutting_down
&& (!F
->flags
.open
|| F
->type
== FD_FILE
))
1641 assert(F
->flags
.open
);
1643 /* The following fails because ipc.c is doing calls to pipe() to create sockets! */
1644 assert(fdc_table
[fd
].active
== 1);
1646 assert(F
->type
!= FD_FILE
);
1648 PROF_start(comm_close
);
1650 F
->flags
.closing
= 1;
1655 ssl_shutdown_method(fd
);
1659 commSetTimeout(fd
, -1, NULL
, NULL
);
1661 /* new-style read/write handler stuff */
1662 if (commio_has_callback(fd
, IOCB_WRITE
, COMMIO_FD_WRITECB(fd
))) {
1663 commio_complete_callback(fd
, COMMIO_FD_WRITECB(fd
), COMM_ERR_CLOSING
, errno
);
1664 commio_call_callback(COMMIO_FD_WRITECB(fd
));
1666 if (commio_has_callback(fd
, IOCB_READ
, COMMIO_FD_READCB(fd
))) {
1667 commio_complete_callback(fd
, COMMIO_FD_READCB(fd
), COMM_ERR_CLOSING
, errno
);
1668 commio_call_callback(COMMIO_FD_READCB(fd
));
1671 /* Do callbacks for read/accept routines, if any */
1672 fdc_table
[fd
].accept
.accept
.doCallback(fd
, -1, COMM_ERR_CLOSING
, 0, NULL
);
1674 /* Complete (w/ COMM_ERR_CLOSING!) any pending io callbacks */
1675 while (fdc_table
[fd
].CommCallbackList
.head
!= NULL
) {
1676 node
= fdc_table
[fd
].CommCallbackList
.head
;
1677 cio
= (CommCallbackData
*)node
->data
;
1678 assert(fd
== cio
->result
.fd
); /* just paranoid */
1679 /* We're closing! */
1681 cio
->callACallback();
1685 commCallCloseHandlers(fd
);
1688 F
->pconn
.pool
->count(F
->pconn
.uses
);
1690 comm_empty_os_read_buffers(fd
);
1700 fd_close(fd
); /* update fdstat */
1704 fdc_table
[fd
].active
= 0;
1706 if (fdc_table
[fd
].half_closed
) {
1707 AbortChecker::Instance().stopMonitoring(fd
);
1708 fdc_table
[fd
].half_closed
= false;
1711 fdc_table
[fd
] = fdc_t(fd
);
1713 statCounter
.syscalls
.sock
.closes
++;
1715 PROF_stop(comm_close
);
1716 /* When an fd closes, give accept() a chance, if need be */
1718 if (fdNFree() >= RESERVED_FD
)
1719 AcceptLimiter::Instance().kick();
1722 /* Send a udp datagram to specified TO_ADDR. */
1724 comm_udp_sendto(int fd
,
1725 const IPAddress
&to_addr
,
1730 struct addrinfo
*AI
= NULL
;
1732 PROF_start(comm_udp_sendto
);
1733 statCounter
.syscalls
.sock
.sendtos
++;
1735 debugs(50, 3, "comm_udp_sendto: Attempt to send UDP packet to " << to_addr
<<
1736 " using FD " << fd
<< " using Port " << comm_local_port(fd
) );
1738 /* BUG: something in the above macro appears to occasionally be setting AI to garbage. */
1739 /* AYJ: 2007-08-27 : or was it because I wasn't then setting 'fd_table[fd].sock_family' to fill properly. */
1740 assert( NULL
== AI
);
1742 to_addr
.GetAddrInfo(AI
, fd_table
[fd
].sock_family
);
1744 x
= sendto(fd
, buf
, len
, 0, AI
->ai_addr
, AI
->ai_addrlen
);
1746 to_addr
.FreeAddrInfo(AI
);
1748 PROF_stop(comm_udp_sendto
);
1753 #ifdef _SQUID_LINUX_
1755 if (ECONNREFUSED
!= errno
)
1758 debugs(50, 1, "comm_udp_sendto: FD " << fd
<< ", (family=" << fd_table
[fd
].sock_family
<< ") " << to_addr
<< ": " << xstrerror());
1764 comm_add_close_handler(int fd
, PF
* handler
, void *data
)
1766 close_handler
*newHandler
= (close_handler
*)conn_close_pool
->alloc(); /* AAA */
1768 debugs(5, 5, "comm_add_close_handler: FD " << fd
<< ", handler=" <<
1769 handler
<< ", data=" << data
);
1771 for (c
= fd_table
[fd
].closeHandler
; c
; c
= c
->next
)
1772 assert(c
->handler
!= handler
|| c
->data
!= data
);
1774 newHandler
->handler
= handler
;
1776 newHandler
->data
= cbdataReference(data
);
1778 newHandler
->next
= fd_table
[fd
].closeHandler
;
1780 fd_table
[fd
].closeHandler
= newHandler
;
1784 comm_remove_close_handler(int fd
, PF
* handler
, void *data
)
1786 assert (fdc_table
[fd
].active
);
1787 close_handler
*p
= NULL
;
1788 close_handler
*last
= NULL
;
1789 /* Find handler in list */
1790 debugs(5, 5, "comm_remove_close_handler: FD " << fd
<< ", handler=" <<
1791 handler
<< ", data=" << data
);
1793 for (p
= fd_table
[fd
].closeHandler
; p
!= NULL
; last
= p
, p
= p
->next
)
1794 if (p
->handler
== handler
&& p
->data
== data
)
1795 break; /* This is our handler */
1799 /* Remove list entry */
1801 last
->next
= p
->next
;
1803 fd_table
[fd
].closeHandler
= p
->next
;
1805 cbdataReferenceDone(p
->data
);
1807 conn_close_pool
->free(p
);
1811 commSetNoLinger(int fd
)
1815 L
.l_onoff
= 0; /* off */
1818 if (setsockopt(fd
, SOL_SOCKET
, SO_LINGER
, (char *) &L
, sizeof(L
)) < 0)
1819 debugs(50, 0, "commSetNoLinger: FD " << fd
<< ": " << xstrerror());
1821 fd_table
[fd
].flags
.nolinger
= 1;
1825 commSetReuseAddr(int fd
)
1829 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &on
, sizeof(on
)) < 0)
1830 debugs(50, 1, "commSetReuseAddr: FD " << fd
<< ": " << xstrerror());
1834 commSetTcpRcvbuf(int fd
, int size
)
1836 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, (char *) &size
, sizeof(size
)) < 0)
1837 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd
<< ", SIZE " << size
<< ": " << xstrerror());
1838 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, (char *) &size
, sizeof(size
)) < 0)
1839 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd
<< ", SIZE " << size
<< ": " << xstrerror());
1840 #ifdef TCP_WINDOW_CLAMP
1841 if (setsockopt(fd
, SOL_TCP
, TCP_WINDOW_CLAMP
, (char *) &size
, sizeof(size
)) < 0)
1842 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd
<< ", SIZE " << size
<< ": " << xstrerror());
1847 commSetNonBlocking(int fd
)
1849 #ifndef _SQUID_MSWIN_
1853 #ifdef _SQUID_WIN32_
1855 int nonblocking
= TRUE
;
1857 #ifdef _SQUID_CYGWIN_
1859 if (fd_table
[fd
].type
!= FD_PIPE
) {
1862 if (ioctl(fd
, FIONBIO
, &nonblocking
) < 0) {
1863 debugs(50, 0, "commSetNonBlocking: FD " << fd
<< ": " << xstrerror() << " " << fd_table
[fd
].type
);
1867 #ifdef _SQUID_CYGWIN_
1872 #ifndef _SQUID_MSWIN_
1874 if ((flags
= fcntl(fd
, F_GETFL
, dummy
)) < 0) {
1875 debugs(50, 0, "FD " << fd
<< ": fcntl F_GETFL: " << xstrerror());
1879 if (fcntl(fd
, F_SETFL
, flags
| SQUID_NONBLOCK
) < 0) {
1880 debugs(50, 0, "commSetNonBlocking: FD " << fd
<< ": " << xstrerror());
1885 #ifdef _SQUID_CYGWIN_
1890 fd_table
[fd
].flags
.nonblocking
= 1;
1896 commUnsetNonBlocking(int fd
)
1898 #ifdef _SQUID_MSWIN_
1899 int nonblocking
= FALSE
;
1901 if (ioctlsocket(fd
, FIONBIO
, (unsigned long *) &nonblocking
) < 0) {
1906 if ((flags
= fcntl(fd
, F_GETFL
, dummy
)) < 0) {
1907 debugs(50, 0, "FD " << fd
<< ": fcntl F_GETFL: " << xstrerror());
1911 if (fcntl(fd
, F_SETFL
, flags
& (~SQUID_NONBLOCK
)) < 0) {
1913 debugs(50, 0, "commUnsetNonBlocking: FD " << fd
<< ": " << xstrerror());
1917 fd_table
[fd
].flags
.nonblocking
= 0;
1922 commSetCloseOnExec(int fd
) {
1927 if ((flags
= fcntl(fd
, F_GETFL
, dummy
)) < 0) {
1928 debugs(50, 0, "FD " << fd
<< ": fcntl F_GETFL: " << xstrerror());
1932 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) < 0)
1933 debugs(50, 0, "FD " << fd
<< ": set close-on-exec failed: " << xstrerror());
1935 fd_table
[fd
].flags
.close_on_exec
= 1;
1942 commSetTcpNoDelay(int fd
) {
1945 if (setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *) &on
, sizeof(on
)) < 0)
1946 debugs(50, 1, "commSetTcpNoDelay: FD " << fd
<< ": " << xstrerror());
1948 fd_table
[fd
].flags
.nodelay
= 1;
1956 fd_table
=(fde
*) xcalloc(Squid_MaxFD
, sizeof(fde
));
1957 fdd_table
= (fd_debug_t
*)xcalloc(Squid_MaxFD
, sizeof(fd_debug_t
));
1958 fdc_table
= new fdc_t
[Squid_MaxFD
];
1959 commfd_table
= (comm_fd_t
*) xcalloc(Squid_MaxFD
, sizeof(comm_fd_t
));
1961 for (int pos
= 0; pos
< Squid_MaxFD
; ++pos
) {
1962 fdc_table
[pos
] = fdc_t(pos
);
1964 for (int pos
= 0; pos
< Squid_MaxFD
; pos
++) {
1965 commfd_table
[pos
].fd
= pos
;
1966 commfd_table
[pos
].readcb
.fd
= pos
;
1967 commfd_table
[pos
].readcb
.type
= IOCB_READ
;
1968 commfd_table
[pos
].writecb
.fd
= pos
;
1969 commfd_table
[pos
].writecb
.type
= IOCB_WRITE
;
1972 /* XXX account fd_table */
1973 /* Keep a few file descriptors free so that we don't run out of FD's
1974 * after accepting a client but before it opens a socket or a file.
1975 * Since Squid_MaxFD can be as high as several thousand, don't waste them */
1976 RESERVED_FD
= XMIN(100, Squid_MaxFD
/ 4);
1978 conn_close_pool
= memPoolCreate("close_handler", sizeof(close_handler
));
1983 safe_free(fd_table
);
1984 safe_free(fdd_table
);
1989 safe_free(commfd_table
);
1994 commHandleWrite(int fd
, void *data
) {
1995 comm_io_callback_t
*state
= (comm_io_callback_t
*)data
;
1999 assert(state
== COMMIO_FD_WRITECB(fd
));
2001 PROF_start(commHandleWrite
);
2002 debugs(5, 5, "commHandleWrite: FD " << fd
<< ": off " <<
2003 (long int) state
->offset
<< ", sz " << (long int) state
->size
<< ".");
2005 nleft
= state
->size
- state
->offset
;
2006 len
= FD_WRITE_METHOD(fd
, state
->buf
+ state
->offset
, nleft
);
2007 debugs(5, 5, "commHandleWrite: write() returns " << len
);
2008 fd_bytes(fd
, len
, FD_WRITE
);
2009 statCounter
.syscalls
.sock
.writes
++;
2012 /* Note we even call write if nleft == 0 */
2016 debugs(5, 1, "commHandleWrite: FD " << fd
<< ": write failure: connection closed with " << nleft
<< " bytes remaining.");
2018 commio_complete_callback(fd
, COMMIO_FD_WRITECB(fd
), nleft
? COMM_ERROR
: COMM_OK
, errno
);
2019 } else if (len
< 0) {
2022 if (fd_table
[fd
].flags
.socket_eof
) {
2023 debugs(50, 2, "commHandleWrite: FD " << fd
<< ": write failure: " << xstrerror() << ".");
2024 commio_complete_callback(fd
, COMMIO_FD_WRITECB(fd
), nleft
? COMM_ERROR
: COMM_OK
, errno
);
2025 } else if (ignoreErrno(errno
)) {
2026 debugs(50, 10, "commHandleWrite: FD " << fd
<< ": write failure: " << xstrerror() << ".");
2033 debugs(50, 2, "commHandleWrite: FD " << fd
<< ": write failure: " << xstrerror() << ".");
2034 commio_complete_callback(fd
, COMMIO_FD_WRITECB(fd
), nleft
? COMM_ERROR
: COMM_OK
, errno
);
2037 /* A successful write, continue */
2038 state
->offset
+= len
;
2040 if (state
->offset
< (off_t
)state
->size
) {
2041 /* Not done, reinstall the write handler and write some more */
2048 commio_complete_callback(fd
, COMMIO_FD_WRITECB(fd
), nleft
? COMM_OK
: COMM_ERROR
, errno
);
2052 PROF_stop(commHandleWrite
);
2056 * Queue a write. handler/handler_data are called when the write
2057 * completes, on error, or on file descriptor close.
2059 * free_func is used to free the passed buffer when the write has completed.
2062 comm_write(int fd
, const char *buf
, int size
, IOCB
* handler
, void *handler_data
, FREE
* free_func
)
2064 assert(!fd_table
[fd
].flags
.closing
);
2066 debugs(5, 5, "comm_write: FD " << fd
<< ": sz " << size
<< ": hndl " << handler
<< ": data " << handler_data
<< ".");
2068 if (commio_has_callback(fd
, IOCB_WRITE
, COMMIO_FD_WRITECB(fd
))) {
2069 /* This means that the write has been scheduled, but has not
2072 fatalf ("comm_write: fd %d: pending callback!\n", fd
);
2075 commio_set_callback(fd
, IOCB_WRITE
, COMMIO_FD_WRITECB(fd
), handler
, handler_data
, (char *)buf
, free_func
, size
);
2076 commSetSelect(fd
, COMM_SELECT_WRITE
, commHandleWrite
, COMMIO_FD_WRITECB(fd
), 0);
2079 /* a wrapper around comm_write to allow for MemBuf to be comm_written in a snap */
2081 comm_write_mbuf(int fd
, MemBuf
*mb
, IOCB
* handler
, void *handler_data
) {
2082 comm_write(fd
, mb
->buf
, mb
->size
, handler
, handler_data
, mb
->freeFunc());
2087 * hm, this might be too general-purpose for all the places we'd
2091 ignoreErrno(int ierrno
) {
2097 #if EAGAIN != EWOULDBLOCK
2120 commCloseAllSockets(void) {
2124 for (fd
= 0; fd
<= Biggest_FD
; fd
++) {
2130 if (F
->type
!= FD_SOCKET
)
2133 if (F
->flags
.ipc
) /* don't close inter-process sockets */
2136 if (F
->timeout_handler
) {
2137 PF
*callback
= F
->timeout_handler
;
2138 void *cbdata
= NULL
;
2139 F
->timeout_handler
= NULL
;
2140 debugs(5, 5, "commCloseAllSockets: FD " << fd
<< ": Calling timeout handler");
2142 if (cbdataReferenceValidDone(F
->timeout_data
, &cbdata
))
2143 callback(fd
, cbdata
);
2145 debugs(5, 5, "commCloseAllSockets: FD " << fd
<< ": calling comm_close()");
2152 AlreadyTimedOut(fde
*F
) {
2156 if (F
->timeout
== 0)
2159 if (F
->timeout
> squid_curtime
)
2166 checkTimeouts(void) {
2171 for (fd
= 0; fd
<= Biggest_FD
; fd
++) {
2174 if (AlreadyTimedOut(F
))
2177 debugs(5, 5, "checkTimeouts: FD " << fd
<< " Expired");
2179 if (F
->timeout_handler
) {
2180 debugs(5, 5, "checkTimeouts: FD " << fd
<< ": Call timeout handler");
2181 callback
= F
->timeout_handler
;
2182 F
->timeout_handler
= NULL
;
2183 callback(fd
, F
->timeout_data
);
2185 debugs(5, 5, "checkTimeouts: FD " << fd
<< ": Forcing comm_close()");
2192 * New-style listen and accept routines
2194 * Listen simply registers our interest in an FD for listening,
2195 * and accept takes a callback to call when an FD has been
2199 comm_listen(int sock
) {
2202 if ((x
= listen(sock
, Squid_MaxFD
>> 2)) < 0) {
2203 debugs(50, 0, "comm_listen: listen(" << (Squid_MaxFD
>> 2) << ", " << sock
<< "): " << xstrerror());
2207 if (Config
.accept_filter
&& strcmp(Config
.accept_filter
, "none") != 0) {
2208 #ifdef SO_ACCEPTFILTER
2209 struct accept_filter_arg afa
;
2210 bzero(&afa
, sizeof(afa
));
2211 debug(5, 0) ("Installing accept filter '%s' on FD %d\n",
2212 Config
.accept_filter
, sock
);
2213 xstrncpy(afa
.af_name
, Config
.accept_filter
, sizeof(afa
.af_name
));
2214 x
= setsockopt(sock
, SOL_SOCKET
, SO_ACCEPTFILTER
, &afa
, sizeof(afa
));
2216 debugs(5, 0, "SO_ACCEPTFILTER '" << Config
.accept_filter
<< "': '" << xstrerror());
2217 #elif defined(TCP_DEFER_ACCEPT)
2219 if (strncmp(Config
.accept_filter
, "data=", 5) == 0)
2220 seconds
= atoi(Config
.accept_filter
+ 5);
2221 x
= setsockopt(sock
, IPPROTO_TCP
, TCP_DEFER_ACCEPT
, &seconds
, sizeof(seconds
));
2223 debugs(5, 0, "TCP_DEFER_ACCEPT '" << Config
.accept_filter
<< "': '" << xstrerror());
2225 debugs(5, 0, "accept_filter not supported on your OS");
2233 fdc_t::beginAccepting() {
2234 accept
.accept
.beginAccepting();
2238 fdc_t::acceptCount() const {
2239 return accept
.accept
.acceptCount();
2243 fdc_t::acceptOne(int fd
) {
2244 // If there is no callback and we accept, we will leak the accepted FD.
2245 // When we are running out of FDs, there is often no callback.
2246 if (!accept
.accept
.callback
.handler
) {
2247 debugs(5, 5, "fdc_t::acceptOne orphaned: FD " << fd
);
2248 // XXX: can we remove this and similar "just in case" calls and
2249 // either listen always or listen only when there is a callback?
2250 if (!AcceptLimiter::Instance().deferring())
2251 commSetSelect(fd
, COMM_SELECT_READ
, comm_accept_try
, NULL
, 0);
2252 accept
.accept
.finished(true);
2257 * We don't worry about running low on FDs here. Instead,
2258 * httpAccept() will use AcceptLimiter if we reach the limit
2262 /* Accept a new connection */
2263 int newfd
= comm_old_accept(fd
, accept
.connDetails
);
2265 /* Check for errors */
2268 if (newfd
== COMM_NOMESSAGE
) {
2269 /* register interest again */
2270 debugs(5, 5, "fdc_t::acceptOne eof: FD " << fd
<< " handler: " << (void*)accept
.accept
.callback
.handler
);
2271 commSetSelect(fd
, COMM_SELECT_READ
, comm_accept_try
, NULL
, 0);
2272 accept
.accept
.finished(true);
2276 /* A non-recoverable error - register an error callback */
2277 new CommAcceptCallbackData(fd
, accept
.accept
.callback
, COMM_ERROR
, errno
, -1, accept
.connDetails
);
2279 accept
.accept
.callback
= CallBack
<IOACB
>();
2281 accept
.accept
.finished(true);
2286 debugs(5, 5, "fdc_t::acceptOne accepted: FD " << fd
<< " handler: " << (void*)accept
.accept
.callback
.handler
<< " newfd: " << newfd
);
2288 assert(accept
.accept
.callback
.handler
);
2289 accept
.accept
.doCallback(fd
, newfd
, COMM_OK
, 0, &accept
.connDetails
);
2291 /* If we weren't re-registed, don't bother trying again! */
2293 if (accept
.accept
.callback
.handler
== NULL
)
2294 accept
.accept
.finished(true);
2298 AcceptFD::finished() const {
2303 AcceptFD::finished(bool newValue
) {
2304 finished_
= newValue
;
2308 AcceptFD::finishedAccepting() const {
2309 return acceptCount() >= MAX_ACCEPT_PER_LOOP
|| finished();
2313 * This callback is called whenever a filedescriptor is ready
2314 * to dupe itself and fob off an accept()ed connection
2317 comm_accept_try(int fd
, void *data
) {
2318 assert(fdc_table
[fd
].active
== 1);
2320 fdc_table
[fd
].beginAccepting();
2322 while (!fdc_table
[fd
].accept
.accept
.finishedAccepting())
2323 fdc_table
[fd
].acceptOne(fd
);
2328 * + the current interface will queue _one_ accept per io loop.
2329 * this isn't very optimal and should be revisited at a later date.
2332 comm_accept(int fd
, IOACB
*handler
, void *handler_data
) {
2333 debugs(5, 5, "comm_accept: FD " << fd
<< " handler: " << (void*)handler
);
2334 requireOpenAndActive(fd
);
2336 /* make sure we're not pending! */
2337 assert(fdc_table
[fd
].accept
.accept
.callback
.handler
== NULL
);
2339 /* Record our details */
2340 fdc_table
[fd
].accept
.accept
.callback
= CallBack
<IOACB
> (handler
, handler_data
);
2342 /* Kick off the accept */
2345 comm_accept_try(fd
, NULL
);
2348 commSetSelect(fd
, COMM_SELECT_READ
, comm_accept_try
, NULL
, 0);
2352 void CommIO::Initialise() {
2353 /* Initialize done pipe signal */
2356 DoneFD
= DonePipe
[1];
2357 DoneReadFD
= DonePipe
[0];
2358 fd_open(DoneReadFD
, FD_PIPE
, "async-io completetion event: main");
2359 fd_open(DoneFD
, FD_PIPE
, "async-io completetion event: threads");
2360 commSetNonBlocking(DoneReadFD
);
2361 commSetNonBlocking(DoneFD
);
2362 commSetSelect(DoneReadFD
, COMM_SELECT_READ
, NULLFDHandler
, NULL
, 0);
2366 void CommIO::NotifyIOClose() {
2367 /* Close done pipe signal */
2372 fd_close(DoneReadFD
);
2373 Initialised
= false;
2376 bool CommIO::Initialised
= false;
2377 bool CommIO::DoneSignalled
= false;
2378 int CommIO::DoneFD
= -1;
2379 int CommIO::DoneReadFD
= -1;
2382 CommIO::FlushPipe() {
2384 FD_READ_METHOD(DoneReadFD
, buf
, sizeof(buf
));
2388 CommIO::NULLFDHandler(int fd
, void *data
) {
2390 commSetSelect(fd
, COMM_SELECT_READ
, NULLFDHandler
, NULL
, 0);
2394 CommIO::ResetNotifications() {
2395 if (DoneSignalled
) {
2397 DoneSignalled
= false;
2401 AcceptLimiter
AcceptLimiter::Instance_
;
2403 AcceptLimiter
&AcceptLimiter::Instance() {
2408 AcceptLimiter::deferring() const {
2409 return deferred
.size() > 0;
2413 AcceptLimiter::defer (int fd
, Acceptor::AcceptorFunction
*aFunc
, void *data
) {
2414 debugs(5, 5, "AcceptLimiter::defer: FD " << fd
<< " handler: " << (void*)aFunc
);
2416 temp
.theFunction
= aFunc
;
2418 temp
.theData
= data
;
2419 deferred
.push_back(temp
);
2423 AcceptLimiter::kick() {
2427 /* Yes, this means the first on is the last off....
2428 * If the list container was a little more friendly, we could sensibly us it.
2430 Acceptor temp
= deferred
.pop_back();
2432 comm_accept (temp
.acceptFD
, temp
.theFunction
, temp
.theData
);
2436 commMarkHalfClosed(int fd
) {
2437 assert (fdc_table
[fd
].active
&& !fdc_table
[fd
].half_closed
);
2438 AbortChecker::Instance().monitor(fd
);
2439 fdc_table
[fd
].half_closed
= true;
2442 int commIsHalfClosed(int fd
) {
2443 assert (fdc_table
[fd
].active
);
2445 return fdc_table
[fd
].half_closed
;
2449 commCheckHalfClosed(void *data
) {
2450 AbortChecker::Instance().doIOLoop();
2451 eventAdd("commCheckHalfClosed", commCheckHalfClosed
, NULL
, 1.0, false);
2454 AbortChecker
&AbortChecker::Instance() {return Instance_
;}
2456 AbortChecker
AbortChecker::Instance_
;
2459 AbortChecker::AbortCheckReader(int fd
, char *, size_t size
, comm_err_t flag
, int xerrno
, void *data
) {
2462 * if the read is ok and 0, the conn is still open.
2463 * if the read is a fail, close the conn
2466 if (flag
!= COMM_OK
&& flag
!= COMM_ERR_CLOSING
) {
2467 debugs(5, 3, "AbortChecker::AbortCheckReader: FD " << fd
<< " aborted");
2473 AbortChecker::monitor(int fd
) {
2474 assert (!contains(fd
));
2479 debugs(5, 3, "AbortChecker::monitor: monitoring half closed FD " << fd
<< " for aborts");
2483 AbortChecker::stopMonitoring (int fd
) {
2484 assert (contains (fd
));
2489 debugs(5, 3, "AbortChecker::stopMonitoring: stopped monitoring half closed FD " << fd
<< " for aborts");
2494 AbortChecker::doIOLoop() {
2495 fds
->walk(RemoveCheck
, this);
2496 fds
->walk(AddCheck
, this);
2500 AbortChecker::AddCheck (int const &fd
, void *data
) {
2501 AbortChecker
*me
= (AbortChecker
*)data
;
2506 AbortChecker::RemoveCheck (int const &fd
, void *data
) {
2507 AbortChecker
*me
= (AbortChecker
*)data
;
2508 me
->removeCheck(fd
);
2513 AbortChecker::IntCompare (int const &lhs
, int const &rhs
) {
2518 AbortChecker::contains (int const fd
) const {
2519 fds
= fds
->splay(fd
, IntCompare
);
2521 if (splayLastResult
!= 0)
2529 AbortChecker::remove
2540 fds
= fds
->insert (fd
, IntCompare
);
2544 AbortChecker::addCheck (int const fd
) {
2545 /* assert comm_is_open (fd); */
2546 comm_read(fd
, NULL
, 0, AbortCheckReader
, NULL
);
2550 AbortChecker::removeCheck (int const fd
) {
2552 comm_read_cancel(fd, AbortCheckReader, NULL);
2556 CommRead::CommRead() : fd(-1), buf(NULL
), len(0) {}
2558 CommRead::CommRead(int fd_
, char *buf_
, int len_
, IOCB
*handler_
, void *data_
)
2559 : fd(fd_
), buf(buf_
), len(len_
), callback(handler_
, data_
) {}
2561 DeferredRead::DeferredRead () : theReader(NULL
), theContext(NULL
), theRead(), cancelled(false) {}
2563 DeferredRead::DeferredRead (DeferrableRead
*aReader
, void *data
, CommRead
const &aRead
) : theReader(aReader
), theContext (data
), theRead(aRead
), cancelled(false) {}
2565 DeferredReadManager::~DeferredReadManager() {
2567 assert (deferredReads
.empty());
2570 /* explicit instantiation required for some systems */
2572 template cbdata_type List
<DeferredRead
>
2576 DeferredReadManager::delayRead(DeferredRead
const &aRead
) {
2577 debugs(5, 3, "Adding deferred read on FD " << aRead
.theRead
.fd
);
2578 List
<DeferredRead
> *temp
= deferredReads
.push_back(aRead
);
2579 comm_add_close_handler (aRead
.theRead
.fd
, CloseHandler
, temp
);
2583 DeferredReadManager::CloseHandler(int fd
, void *thecbdata
) {
2584 if (!cbdataReferenceValid (thecbdata
))
2587 List
<DeferredRead
> *temp
= (List
<DeferredRead
> *)thecbdata
;
2589 temp
->element
.markCancelled();
2593 DeferredReadManager::popHead(ListContainer
<DeferredRead
> &deferredReads
) {
2594 assert (!deferredReads
.empty());
2596 if (!deferredReads
.head
->element
.cancelled
)
2597 comm_remove_close_handler(deferredReads
.head
->element
.theRead
.fd
, CloseHandler
, deferredReads
.head
);
2599 DeferredRead result
= deferredReads
.pop_front();
2605 DeferredReadManager::kickReads(int const count
) {
2606 /* if we had List::size() we could consolidate this and flushReads */
2613 size_t remaining
= count
;
2615 while (!deferredReads
.empty() && remaining
) {
2616 DeferredRead aRead
= popHead(deferredReads
);
2619 if (!aRead
.cancelled
)
2625 DeferredReadManager::flushReads() {
2626 ListContainer
<DeferredRead
> reads
;
2627 reads
= deferredReads
;
2628 deferredReads
= ListContainer
<DeferredRead
>();
2630 while (!reads
.empty()) {
2631 DeferredRead aRead
= popHead(reads
);
2637 DeferredReadManager::kickARead(DeferredRead
const &aRead
) {
2638 if (aRead
.cancelled
)
2641 debugs(5, 3, "Kicking deferred read on FD " << aRead
.theRead
.fd
);
2643 aRead
.theReader(aRead
.theContext
, aRead
.theRead
);
2647 DeferredRead::markCancelled() {
2651 ConnectionDetail::ConnectionDetail() : me(), peer() {
2655 CommDispatcher::dispatch() {
2656 bool result
= comm_iocallbackpending();
2657 comm_calliocallback();
2658 /* and again to deal with indirectly queued events
2659 * resulting from the first call. These are usually
2660 * callbacks and should be dealt with immediately.
2662 comm_calliocallback();
2664 /* Adrian's *new* stuff */
2665 commio_call_callbacks();
2670 CommSelectEngine::checkEvents(int timeout
) {
2671 static time_t last_timeout
= 0;
2673 /* No, this shouldn't be here. But it shouldn't be in each comm handler. -adrian */
2674 if (squid_curtime
> last_timeout
) {
2675 last_timeout
= squid_curtime
;
2679 switch (comm_select(timeout
)) {
2695 fatal_dump("comm.cc: Internal error -- this should never happen.");