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"
40 #include "comm/AcceptLimiter.h"
41 #include "comm/comm_internal.h"
42 #include "comm/ListenStateData.h"
45 #include "ConnectionDetail.h"
48 #include "SquidTime.h"
49 #include "CommCalls.h"
50 #include "DescriptorSet.h"
51 #include "icmp/net_db.h"
52 #include "ip/IpAddress.h"
53 #include "ip/IpIntercept.h"
55 #if defined(_SQUID_CYGWIN_)
56 #include <sys/ioctl.h>
58 #ifdef HAVE_NETINET_TCP_H
59 #include <netinet/tcp.h>
63 * New C-like simple comm code. This stuff is a mess and doesn't really buy us anything.
72 static void commStopHalfClosedMonitor(int fd
);
73 static IOCB commHalfClosedReader
;
76 struct comm_io_callback_t
{
79 AsyncCall::Pointer callback
;
87 bool active() const { return callback
!= NULL
; }
92 comm_io_callback_t readcb
;
93 comm_io_callback_t writecb
;
95 typedef struct _comm_fd comm_fd_t
;
96 comm_fd_t
*commfd_table
;
98 // TODO: make this a comm_io_callback_t method?
100 commio_has_callback(int fd
, iocb_type type
, comm_io_callback_t
*ccb
)
102 assert(ccb
->fd
== fd
);
103 assert(ccb
->type
== type
);
104 return ccb
->active();
108 * Configure comm_io_callback_t for I/O
110 * @param fd filedescriptor
111 * @param ccb comm io callback
113 * @param cbdata callback data (must be cbdata'ed)
114 * @param buf buffer, if applicable
115 * @param freefunc freefunc, if applicable
116 * @param size buffer size
119 commio_set_callback(int fd
, iocb_type type
, comm_io_callback_t
*ccb
,
120 AsyncCall::Pointer
&cb
, char *buf
, FREE
*freefunc
, int size
)
122 assert(!ccb
->active());
123 assert(ccb
->type
== type
);
128 ccb
->freefunc
= freefunc
;
134 // Schedule the callback call and clear the callback
136 commio_finish_callback(int fd
, comm_io_callback_t
*ccb
, comm_err_t code
, int xerrno
)
138 debugs(5, 3, "commio_finish_callback: called for FD " << fd
<< " (" <<
139 code
<< ", " << xerrno
<< ")");
140 assert(ccb
->active());
141 assert(ccb
->fd
== fd
);
143 ccb
->xerrno
= xerrno
;
145 comm_io_callback_t cb
= *ccb
;
147 /* We've got a copy; blow away the real one */
148 /* XXX duplicate code from commio_cancel_callback! */
150 ccb
->callback
= NULL
; // cb has it
158 if (cb
.callback
!= NULL
) {
159 typedef CommIoCbParams Params
;
160 Params
¶ms
= GetCommParams
<Params
>(cb
.callback
);
163 params
.size
= cb
.offset
;
164 params
.flag
= cb
.errcode
;
165 params
.xerrno
= cb
.xerrno
;
166 ScheduleCallHere(cb
.callback
);
172 * Cancel the given callback
174 * Remember that the data is cbdataRef'ed.
176 // TODO: make this a comm_io_callback_t method
178 commio_cancel_callback(int fd
, comm_io_callback_t
*ccb
)
180 debugs(5, 3, "commio_cancel_callback: called for FD " << fd
);
181 assert(ccb
->fd
== fd
);
182 assert(ccb
->active());
185 ccb
->callback
= NULL
;
189 * Call the given comm callback; assumes the callback is valid.
191 * @param ccb io completion callback
194 commio_call_callback(comm_io_callback_t
*ccb
)
198 class ConnectStateData
202 void *operator new (size_t);
203 void operator delete (void *);
204 static void Connect (int fd
, void *me
);
206 void callCallback(comm_err_t status
, int xerrno
);
209 // defaults given by client
211 u_short default_port
;
212 Ip::Address default_addr
;
213 // NP: CANNOT store the default addr:port together as it gets set/reset differently.
215 DnsLookupDetails dns
; ///< host lookup details
217 AsyncCall::Pointer callback
;
226 int commRetryConnect();
227 CBDATA_CLASS(ConnectStateData
);
232 static DescriptorSet
*TheHalfClosed
= NULL
; /// the set of half-closed FDs
233 static bool WillCheckHalfClosed
= false; /// true if check is scheduled
234 static EVH commHalfClosedCheck
;
235 static void commPlanHalfClosedCheck();
237 static comm_err_t
commBind(int s
, struct addrinfo
&);
238 static void commSetReuseAddr(int);
239 static void commSetNoLinger(int);
241 static void commSetTcpNoDelay(int);
243 static void commSetTcpRcvbuf(int, int);
244 static PF commConnectFree
;
245 static PF commHandleWrite
;
246 static IPH commConnectDnsHandle
;
253 static MemAllocator
*conn_close_pool
= NULL
;
254 fd_debug_t
*fdd_table
= NULL
;
259 return fd_table
[fd
].flags
.open
!= 0;
265 * If the read attempt succeeds or fails, call the callback.
266 * Else, wait for another IO notification.
269 commHandleRead(int fd
, void *data
)
271 comm_io_callback_t
*ccb
= (comm_io_callback_t
*) data
;
273 assert(data
== COMMIO_FD_READCB(fd
));
274 assert(commio_has_callback(fd
, IOCB_READ
, ccb
));
276 statCounter
.syscalls
.sock
.reads
++;
279 retval
= FD_READ_METHOD(fd
, ccb
->buf
, ccb
->size
);
280 debugs(5, 3, "comm_read_try: FD " << fd
<< ", size " << ccb
->size
<< ", retval " << retval
<< ", errno " << errno
);
282 if (retval
< 0 && !ignoreErrno(errno
)) {
283 debugs(5, 3, "comm_read_try: scheduling COMM_ERROR");
285 commio_finish_callback(fd
, ccb
, COMM_ERROR
, errno
);
289 /* See if we read anything */
290 /* Note - read 0 == socket EOF, which is a valid read */
292 fd_bytes(fd
, retval
, FD_READ
);
293 ccb
->offset
= retval
;
294 commio_finish_callback(fd
, ccb
, COMM_OK
, errno
);
298 /* Nope, register for some more IO */
299 commSetSelect(fd
, COMM_SELECT_READ
, commHandleRead
, data
, 0);
303 * Queue a read. handler/handler_data are called when the read
304 * completes, on error, or on file descriptor close.
307 comm_read(int fd
, char *buf
, int size
, IOCB
*handler
, void *handler_data
)
309 AsyncCall::Pointer call
= commCbCall(5,4, "SomeCommReadHandler",
310 CommIoCbPtrFun(handler
, handler_data
));
311 comm_read(fd
, buf
, size
, call
);
315 comm_read(int fd
, char *buf
, int size
, AsyncCall::Pointer
&callback
)
317 debugs(5, 5, "comm_read, queueing read for FD " << fd
<< "; asynCall " << callback
);
319 /* Make sure we are open and not closing */
321 assert(!fd_table
[fd
].closing());
322 comm_io_callback_t
*ccb
= COMMIO_FD_READCB(fd
);
324 // Make sure we are either not reading or just passively monitoring.
325 // Active/passive conflicts are OK and simply cancel passive monitoring.
327 // if the assertion below fails, we have an active comm_read conflict
328 assert(fd_table
[fd
].halfClosedReader
!= NULL
);
329 commStopHalfClosedMonitor(fd
);
330 assert(!ccb
->active());
334 commio_set_callback(fd
, IOCB_READ
, ccb
, callback
, (char *)buf
, NULL
, size
);
335 commSetSelect(fd
, COMM_SELECT_READ
, commHandleRead
, ccb
, 0);
339 * Empty the read buffers
341 * This is a magical routine that empties the read buffers.
342 * Under some platforms (Linux) if a buffer has data in it before
343 * you call close(), the socket will hang and take quite a while
347 comm_empty_os_read_buffers(int fd
)
350 /* prevent those nasty RST packets */
351 char buf
[SQUID_TCP_SO_RCVBUF
];
353 if (fd_table
[fd
].flags
.nonblocking
== 1) {
354 while (FD_READ_METHOD(fd
, buf
, SQUID_TCP_SO_RCVBUF
) > 0) {};
361 * Return whether the FD has a pending completed callback.
364 comm_has_pending_read_callback(int fd
)
367 // XXX: We do not know whether there is a read callback scheduled.
368 // This is used for pconn management that should probably be more
369 // tightly integrated into comm to minimize the chance that a
370 // closing pconn socket will be used for a new transaction.
374 // Does comm check this fd for read readiness?
375 // Note that when comm is not monitoring, there can be a pending callback
376 // call, which may resume comm monitoring once fired.
378 comm_monitors_read(int fd
)
381 // Being active is usually the same as monitoring because we always
382 // start monitoring the FD when we configure comm_io_callback_t for I/O
383 // and we usually configure comm_io_callback_t for I/O when we starting
384 // monitoring a FD for reading. TODO: replace with commio_has_callback
385 return COMMIO_FD_READCB(fd
)->active();
389 * Cancel a pending read. Assert that we have the right parameters,
390 * and that there are no pending read events!
392 * XXX: We do not assert that there are no pending read events and
393 * with async calls it becomes even more difficult.
394 * The whole interface should be reworked to do callback->cancel()
395 * instead of searching for places where the callback may be stored and
396 * updating the state of those places.
398 * AHC Don't call the comm handlers?
401 comm_read_cancel(int fd
, IOCB
*callback
, void *data
)
404 debugs(5, 4, "comm_read_cancel fails: FD " << fd
<< " closed");
408 comm_io_callback_t
*cb
= COMMIO_FD_READCB(fd
);
409 // TODO: is "active" == "monitors FD"?
411 debugs(5, 4, "comm_read_cancel fails: FD " << fd
<< " inactive");
415 typedef CommCbFunPtrCallT
<CommIoCbPtrFun
> Call
;
416 Call
*call
= dynamic_cast<Call
*>(cb
->callback
.getRaw());
418 debugs(5, 4, "comm_read_cancel fails: FD " << fd
<< " lacks callback");
422 call
->cancel("old comm_read_cancel");
424 typedef CommIoCbParams Params
;
425 const Params
¶ms
= GetCommParams
<Params
>(cb
->callback
);
427 /* Ok, we can be reasonably sure we won't lose any data here! */
428 assert(call
->dialer
.handler
== callback
);
429 assert(params
.data
== data
);
431 /* Delete the callback */
432 commio_cancel_callback(fd
, cb
);
434 /* And the IO event */
435 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
439 comm_read_cancel(int fd
, AsyncCall::Pointer
&callback
)
441 callback
->cancel("comm_read_cancel");
444 debugs(5, 4, "comm_read_cancel fails: FD " << fd
<< " closed");
448 comm_io_callback_t
*cb
= COMMIO_FD_READCB(fd
);
451 debugs(5, 4, "comm_read_cancel fails: FD " << fd
<< " inactive");
455 AsyncCall::Pointer call
= cb
->callback
;
456 assert(call
!= NULL
); // XXX: should never fail (active() checks for callback==NULL)
458 /* Ok, we can be reasonably sure we won't lose any data here! */
459 assert(call
== callback
);
461 /* Delete the callback */
462 commio_cancel_callback(fd
, cb
);
464 /* And the IO event */
465 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
470 * synchronous wrapper around udp socket functions
473 comm_udp_recvfrom(int fd
, void *buf
, size_t len
, int flags
, Ip::Address
&from
)
475 statCounter
.syscalls
.sock
.recvfroms
++;
477 struct addrinfo
*AI
= NULL
;
479 debugs(5,8, "comm_udp_recvfrom: FD " << fd
<< " from " << from
);
481 assert( NULL
== AI
);
483 from
.InitAddrInfo(AI
);
485 x
= recvfrom(fd
, buf
, len
, flags
, AI
->ai_addr
, &AI
->ai_addrlen
);
489 from
.FreeAddrInfo(AI
);
495 comm_udp_recv(int fd
, void *buf
, size_t len
, int flags
)
498 return comm_udp_recvfrom(fd
, buf
, len
, flags
, nul
);
502 comm_udp_send(int s
, const void *buf
, size_t len
, int flags
)
504 return send(s
, buf
, len
, flags
);
509 comm_has_incomplete_write(int fd
)
512 return COMMIO_FD_WRITECB(fd
)->active();
516 * Queue a write. handler/handler_data are called when the write fully
517 * completes, on error, or on file descriptor close.
520 /* Return the local port associated with fd. */
522 comm_local_port(int fd
)
525 struct addrinfo
*addr
= NULL
;
526 fde
*F
= &fd_table
[fd
];
528 /* If the fd is closed already, just return */
530 if (!F
->flags
.open
) {
531 debugs(5, 0, "comm_local_port: FD " << fd
<< " has been closed.");
535 if (F
->local_addr
.GetPort())
536 return F
->local_addr
.GetPort();
539 if (F
->sock_family
== AF_INET
)
543 temp
.InitAddrInfo(addr
);
545 if (getsockname(fd
, addr
->ai_addr
, &(addr
->ai_addrlen
)) ) {
546 debugs(50, 1, "comm_local_port: Failed to retrieve TCP/UDP port number for socket: FD " << fd
<< ": " << xstrerror());
547 temp
.FreeAddrInfo(addr
);
552 temp
.FreeAddrInfo(addr
);
554 F
->local_addr
.SetPort(temp
.GetPort());
556 #if 0 // seems to undo comm_open actions on the FD ...
557 // grab default socket information for this address
558 temp
.GetAddrInfo(addr
);
560 F
->sock_family
= addr
->ai_family
;
562 temp
.FreeAddrInfo(addr
);
565 debugs(5, 6, "comm_local_port: FD " << fd
<< ": port " << F
->local_addr
.GetPort() << "(family=" << F
->sock_family
<< ")");
566 return F
->local_addr
.GetPort();
570 commBind(int s
, struct addrinfo
&inaddr
)
572 statCounter
.syscalls
.sock
.binds
++;
574 if (bind(s
, inaddr
.ai_addr
, inaddr
.ai_addrlen
) == 0) {
575 debugs(50, 6, "commBind: bind socket FD " << s
<< " to " << fd_table
[s
].local_addr
);
579 debugs(50, 0, "commBind: Cannot bind socket FD " << s
<< " to " << fd_table
[s
].local_addr
<< ": " << xstrerror());
585 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
586 * is OR of flags specified in comm.h. Defaults TOS
589 comm_open(int sock_type
,
595 return comm_openex(sock_type
, proto
, addr
, flags
, 0, note
);
599 comm_open_listener(int sock_type
,
607 /* all listener sockets require bind() */
608 flags
|= COMM_DOBIND
;
610 /* attempt native enabled port. */
611 sock
= comm_openex(sock_type
, proto
, addr
, flags
, 0, note
);
614 /* under IPv6 there is the possibility IPv6 is present but disabled. */
615 /* try again as IPv4-native */
616 if ( sock
< 0 && addr
.IsIPv6() && addr
.SetIPv4() ) {
617 /* attempt to open this IPv4-only. */
618 sock
= comm_openex(sock_type
, proto
, addr
, flags
, 0, note
);
619 debugs(50, 2, HERE
<< "attempt open " << note
<< " socket on: " << addr
);
627 limitError(int const anErrno
)
629 return anErrno
== ENFILE
|| anErrno
== EMFILE
;
633 comm_set_tos(int fd
, int tos
)
636 int x
= setsockopt(fd
, IPPROTO_IP
, IP_TOS
, (char *) &tos
, sizeof(int));
638 debugs(50, 1, "comm_set_tos: setsockopt(IP_TOS) on FD " << fd
<< ": " << xstrerror());
641 debugs(50, 0, "WARNING: setsockopt(IP_TOS) not supported on this platform");
647 comm_set_v6only(int fd
, int tos
)
650 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, (char *) &tos
, sizeof(int)) < 0) {
651 debugs(50, 1, "comm_open: setsockopt(IPV6_V6ONLY) " << (tos
?"ON":"OFF") << " for FD " << fd
<< ": " << xstrerror());
654 debugs(50, 0, "WARNING: comm_open: setsockopt(IPV6_V6ONLY) not supported on this platform");
659 * Set the socket IP_TRANSPARENT option for Linux TPROXY v4 support.
662 comm_set_transparent(int fd
)
664 #if defined(IP_TRANSPARENT)
666 if (setsockopt(fd
, SOL_IP
, IP_TRANSPARENT
, (char *) &tos
, sizeof(int)) < 0) {
667 debugs(50, DBG_IMPORTANT
, "comm_open: setsockopt(IP_TRANSPARENT) on FD " << fd
<< ": " << xstrerror());
669 /* mark the socket as having transparent options */
670 fd_table
[fd
].flags
.transparent
= 1;
673 debugs(50, DBG_CRITICAL
, "WARNING: comm_open: setsockopt(IP_TRANSPARENT) not supported on this platform");
678 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
679 * is OR of flags specified in defines.h:COMM_*
682 comm_openex(int sock_type
,
692 struct addrinfo
*AI
= NULL
;
694 PROF_start(comm_open
);
695 /* Create socket for accepting new connections. */
696 statCounter
.syscalls
.sock
.sockets
++;
698 /* Setup the socket addrinfo details for use */
699 addr
.GetAddrInfo(AI
);
700 AI
->ai_socktype
= sock_type
;
701 AI
->ai_protocol
= proto
;
703 debugs(50, 3, "comm_openex: Attempt open socket for: " << addr
);
705 if ((new_socket
= socket(AI
->ai_family
, AI
->ai_socktype
, AI
->ai_protocol
)) < 0) {
706 /* Increase the number of reserved fd's if calls to socket()
707 * are failing because the open file table is full. This
708 * limits the number of simultaneous clients */
710 if (limitError(errno
)) {
711 debugs(50, DBG_IMPORTANT
, "comm_open: socket failure: " << xstrerror());
714 debugs(50, DBG_CRITICAL
, "comm_open: socket failure: " << xstrerror());
717 addr
.FreeAddrInfo(AI
);
719 PROF_stop(comm_open
);
723 debugs(50, 3, "comm_openex: Opened socket FD " << new_socket
<< " : family=" << AI
->ai_family
<< ", type=" << AI
->ai_socktype
<< ", protocol=" << AI
->ai_protocol
);
725 /* set TOS if needed */
726 if (TOS
&& comm_set_tos(new_socket
, TOS
) ) {
730 #if IPV6_SPECIAL_SPLITSTACK
733 comm_set_v6only(new_socket
, 1);
737 #if IPV6_SPECIAL_V4MAPPED
739 /* Windows Vista supports Dual-Sockets. BUT defaults them to V6ONLY. Turn it OFF. */
740 /* Other OS may have this administratively disabled for general use. Same deal. */
742 comm_set_v6only(new_socket
, 0);
747 debugs(5, 5, "comm_open: FD " << new_socket
<< " is a new socket");
749 assert(!isOpen(new_socket
));
750 fd_open(new_socket
, FD_SOCKET
, note
);
752 fdd_table
[new_socket
].close_file
= NULL
;
754 fdd_table
[new_socket
].close_line
= 0;
756 F
= &fd_table
[new_socket
];
758 F
->local_addr
= addr
;
762 F
->sock_family
= AI
->ai_family
;
764 if (!(flags
& COMM_NOCLOEXEC
))
765 commSetCloseOnExec(new_socket
);
767 if ((flags
& COMM_REUSEADDR
))
768 commSetReuseAddr(new_socket
);
770 if (addr
.GetPort() > (u_short
) 0) {
772 if (sock_type
!= SOCK_DGRAM
)
774 commSetNoLinger(new_socket
);
777 commSetReuseAddr(new_socket
);
780 /* MUST be done before binding or face OS Error: "(99) Cannot assign requested address"... */
781 if ((flags
& COMM_TRANSPARENT
)) {
782 comm_set_transparent(new_socket
);
785 if ( (flags
& COMM_DOBIND
) || addr
.GetPort() > 0 || !addr
.IsAnyAddr() ) {
786 if ( !(flags
& COMM_DOBIND
) && addr
.IsAnyAddr() )
787 debugs(5,1,"WARNING: Squid is attempting to bind() port " << addr
<< " without being a listener.");
788 if ( addr
.IsNoAddr() )
789 debugs(5,0,"CRITICAL: Squid is attempting to bind() port " << addr
<< "!!");
791 if (commBind(new_socket
, *AI
) != COMM_OK
) {
792 comm_close(new_socket
);
793 addr
.FreeAddrInfo(AI
);
795 PROF_stop(comm_open
);
799 addr
.FreeAddrInfo(AI
);
801 if (flags
& COMM_NONBLOCKING
)
802 if (commSetNonBlocking(new_socket
) == COMM_ERROR
) {
804 PROF_stop(comm_open
);
808 if (sock_type
== SOCK_STREAM
)
809 commSetTcpNoDelay(new_socket
);
813 if (Config
.tcpRcvBufsz
> 0 && sock_type
== SOCK_STREAM
)
814 commSetTcpRcvbuf(new_socket
, Config
.tcpRcvBufsz
);
816 PROF_stop(comm_open
);
821 CBDATA_CLASS_INIT(ConnectStateData
);
824 ConnectStateData::operator new (size_t size
)
826 CBDATA_INIT_TYPE(ConnectStateData
);
827 return cbdataAlloc(ConnectStateData
);
831 ConnectStateData::operator delete (void *address
)
839 commConnectStart(int fd
, const char *host
, u_short port
, AsyncCall::Pointer
&cb
)
841 debugs(cb
->debugSection
, cb
->debugLevel
, "commConnectStart: FD " << fd
<<
842 ", cb " << cb
<< ", " << host
<< ":" << port
); // TODO: just print *cb
844 ConnectStateData
*cs
;
845 cs
= new ConnectStateData
;
847 cs
->host
= xstrdup(host
);
848 cs
->default_port
= port
;
851 comm_add_close_handler(fd
, commConnectFree
, cs
);
852 ipcache_nbgethostbyname(host
, commConnectDnsHandle
, cs
);
855 // TODO: Remove this and similar callback registration functions by replacing
856 // (callback,data) parameters with an AsyncCall so that we do not have to use
857 // a generic call name and debug level when creating an AsyncCall. This will
858 // also cut the number of callback registration routines in half.
860 commConnectStart(int fd
, const char *host
, u_short port
, CNCB
* callback
, void *data
)
862 debugs(5, 5, "commConnectStart: FD " << fd
<< ", data " << data
<< ", " << host
<< ":" << port
);
863 AsyncCall::Pointer call
= commCbCall(5,3,
864 "SomeCommConnectHandler", CommConnectCbPtrFun(callback
, data
));
865 commConnectStart(fd
, host
, port
, call
);
869 commConnectDnsHandle(const ipcache_addrs
*ia
, const DnsLookupDetails
&details
, void *data
)
871 ConnectStateData
*cs
= (ConnectStateData
*)data
;
875 debugs(5, 3, "commConnectDnsHandle: Unknown host: " << cs
->host
);
876 cs
->callCallback(COMM_ERR_DNS
, 0);
880 assert(ia
->cur
< ia
->count
);
882 cs
->default_addr
= ia
->in_addrs
[ia
->cur
];
884 if (Config
.onoff
.balance_on_multiple_ip
)
885 ipcacheCycleAddr(cs
->host
, NULL
);
887 cs
->addrcount
= ia
->count
;
889 cs
->connstart
= squid_curtime
;
895 ConnectStateData::callCallback(comm_err_t status
, int xerrno
)
897 debugs(5, 3, "commConnectCallback: FD " << fd
);
899 comm_remove_close_handler(fd
, commConnectFree
, this);
900 commSetTimeout(fd
, -1, NULL
, NULL
);
902 typedef CommConnectCbParams Params
;
903 Params
¶ms
= GetCommParams
<Params
>(callback
);
906 params
.flag
= status
;
907 params
.xerrno
= xerrno
;
908 ScheduleCallHere(callback
);
911 commConnectFree(fd
, this);
915 commConnectFree(int fd
, void *data
)
917 ConnectStateData
*cs
= (ConnectStateData
*)data
;
918 debugs(5, 3, "commConnectFree: FD " << fd
);
919 // delete cs->callback;
926 copyFDFlags(int to
, fde
*F
)
928 if (F
->flags
.close_on_exec
)
929 commSetCloseOnExec(to
);
931 if (F
->flags
.nonblocking
)
932 commSetNonBlocking(to
);
936 if (F
->flags
.nodelay
)
937 commSetTcpNoDelay(to
);
941 if (Config
.tcpRcvBufsz
> 0)
942 commSetTcpRcvbuf(to
, Config
.tcpRcvBufsz
);
945 /* Reset FD so that we can connect() again */
947 ConnectStateData::commResetFD()
949 struct addrinfo
*AI
= NULL
;
951 int new_family
= AF_UNSPEC
;
953 // XXX: do we have to check this?
955 // if (!cbdataReferenceValid(callback.data))
958 statCounter
.syscalls
.sock
.sockets
++;
960 /* setup a bare-bones addrinfo */
961 /* TODO INET6: for WinXP we may need to check the local_addr type and setup the family properly. */
963 new_family
= AI
->ai_family
;
965 int fd2
= socket(AI
->ai_family
, AI
->ai_socktype
, AI
->ai_protocol
);
967 nul
.FreeAddrInfo(AI
);
970 debugs(5, DBG_CRITICAL
, HERE
<< "WARNING: FD " << fd2
<< " socket failed to allocate: " << xstrerror());
972 if (ENFILE
== errno
|| EMFILE
== errno
)
980 /* On Windows dup2() can't work correctly on Sockets, the */
981 /* workaround is to close the destination Socket before call them. */
986 if (dup2(fd2
, fd
) < 0) {
987 debugs(5, DBG_CRITICAL
, HERE
<< "WARNING: dup2(FD " << fd2
<< ", FD " << fd
<< ") failed: " << xstrerror());
989 if (ENFILE
== errno
|| EMFILE
== errno
)
999 fde
*F
= &fd_table
[fd
];
1001 debugs(50, 3, "commResetFD: Reset socket FD " << fd
<< "->" << fd2
<< " : family=" << new_family
);
1003 /* INET6: copy the new sockets family type to the FDE table */
1004 fd_table
[fd
].sock_family
= new_family
;
1006 fd_table
[fd
].flags
.called_connect
= 0;
1008 * yuck, this has assumptions about comm_open() arguments for
1009 * the original socket
1012 /* MUST be done before binding or face OS Error: "(99) Cannot assign requested address"... */
1013 if ( F
->flags
.transparent
) {
1014 comm_set_transparent(fd
);
1018 F
->local_addr
.GetAddrInfo(AI
);
1020 if (commBind(fd
, *AI
) != COMM_OK
) {
1021 debugs(5, DBG_CRITICAL
, "WARNING: Reset of FD " << fd
<< " for " << F
->local_addr
<< " failed to bind: " << xstrerror());
1022 F
->local_addr
.FreeAddrInfo(AI
);
1025 F
->local_addr
.FreeAddrInfo(AI
);
1028 comm_set_tos(fd
, F
->tos
);
1030 #if IPV6_SPECIAL_SPLITSTACK
1031 if ( F
->local_addr
.IsIPv6() )
1032 comm_set_v6only(fd
, 1);
1041 ConnectStateData::commRetryConnect()
1043 assert(addrcount
> 0);
1045 if (addrcount
== 1) {
1046 if (tries
>= Config
.retry
.maxtries
)
1049 if (squid_curtime
- connstart
> Config
.Timeout
.connect
)
1052 if (tries
> addrcount
)
1056 return commResetFD();
1060 commReconnect(void *data
)
1062 ConnectStateData
*cs
= (ConnectStateData
*)data
;
1063 ipcache_nbgethostbyname(cs
->host
, commConnectDnsHandle
, cs
);
1066 /** Connect SOCK to specified DEST_PORT at DEST_HOST. */
1068 ConnectStateData::Connect(int fd
, void *me
)
1070 ConnectStateData
*cs
= (ConnectStateData
*)me
;
1071 assert (cs
->fd
== fd
);
1076 ConnectStateData::defaults()
1079 S
.SetPort(default_port
);
1083 ConnectStateData::connect()
1088 debugs(5,5, HERE
<< "to " << S
);
1090 switch (comm_connect_addr(fd
, S
) ) {
1092 case COMM_INPROGRESS
:
1093 debugs(5, 5, HERE
<< "FD " << fd
<< ": COMM_INPROGRESS");
1094 commSetSelect(fd
, COMM_SELECT_WRITE
, ConnectStateData::Connect
, this, 0);
1098 debugs(5, 5, HERE
<< "FD " << fd
<< ": COMM_OK - connected");
1099 ipcacheMarkGoodAddr(host
, S
);
1100 callCallback(COMM_OK
, 0);
1104 case COMM_ERR_PROTOCOL
:
1105 /* problem using the desired protocol over this socket.
1106 * count the connection attempt, reset the socket, and immediately try again */
1114 debugs(5, 5, HERE
"FD " << fd
<< ": * - try again");
1116 ipcacheMarkBadAddr(host
, S
);
1119 if (Config
.onoff
.test_reachability
)
1120 netdbDeleteAddrNetwork(S
);
1123 if (commRetryConnect()) {
1124 eventAdd("commReconnect", commReconnect
, this, this->addrcount
== 1 ? 0.05 : 0.0, 0);
1126 debugs(5, 5, HERE
<< "FD " << fd
<< ": * - ERR tried too many times already.");
1127 callCallback(COMM_ERR_CONNECT
, errno
);
1133 commSetTimeout_old(int fd, int timeout, PF * handler, void *data)
1135 debugs(5, 3, HERE << "FD " << fd << " timeout " << timeout);
1137 assert(fd < Squid_MaxFD);
1138 fde *F = &fd_table[fd];
1139 assert(F->flags.open);
1142 cbdataReferenceDone(F->timeout_data);
1143 F->timeout_handler = NULL;
1147 cbdataReferenceDone(F->timeout_data);
1148 F->timeout_handler = handler;
1149 F->timeout_data = cbdataReference(data);
1152 F->timeout = squid_curtime + (time_t) timeout;
1160 commSetTimeout(int fd
, int timeout
, PF
* handler
, void *data
)
1162 AsyncCall::Pointer call
;
1163 debugs(5, 3, HERE
<< "FD " << fd
<< " timeout " << timeout
);
1164 if (handler
!= NULL
)
1165 call
=commCbCall(5,4, "SomeTimeoutHandler", CommTimeoutCbPtrFun(handler
, data
));
1168 return commSetTimeout(fd
, timeout
, call
);
1172 int commSetTimeout(int fd
, int timeout
, AsyncCall::Pointer
&callback
)
1174 debugs(5, 3, HERE
<< "FD " << fd
<< " timeout " << timeout
);
1176 assert(fd
< Squid_MaxFD
);
1177 fde
*F
= &fd_table
[fd
];
1178 assert(F
->flags
.open
);
1181 F
->timeoutHandler
= NULL
;
1184 if (callback
!= NULL
) {
1185 typedef CommTimeoutCbParams Params
;
1186 Params
¶ms
= GetCommParams
<Params
>(callback
);
1188 F
->timeoutHandler
= callback
;
1191 F
->timeout
= squid_curtime
+ (time_t) timeout
;
1199 comm_connect_addr(int sock
, const Ip::Address
&address
)
1201 comm_err_t status
= COMM_OK
;
1202 fde
*F
= &fd_table
[sock
];
1206 struct addrinfo
*AI
= NULL
;
1207 PROF_start(comm_connect_addr
);
1209 assert(address
.GetPort() != 0);
1211 debugs(5, 9, "comm_connect_addr: connecting socket " << sock
<< " to " << address
<< " (want family: " << F
->sock_family
<< ")");
1213 /* BUG 2222 FIX: reset the FD when its found to be IPv4 in IPv6 mode */
1214 /* inverse case of IPv4 failing to connect on IPv6 socket is handeld post-connect.
1215 * this case must presently be handled here since the GetAddrInfo asserts on bad mappings.
1216 * eventually we want it to throw a Must() that gets handled there instead of this if.
1217 * NP: because commresetFD is private to ConnStateData we have to return an error and
1218 * trust its handled properly.
1221 if (F
->sock_family
== AF_INET
&& !address
.IsIPv4()) {
1222 return COMM_ERR_PROTOCOL
;
1226 address
.GetAddrInfo(AI
, F
->sock_family
);
1228 /* Establish connection. */
1231 if (!F
->flags
.called_connect
) {
1232 F
->flags
.called_connect
= 1;
1233 statCounter
.syscalls
.sock
.connects
++;
1235 x
= connect(sock
, AI
->ai_addr
, AI
->ai_addrlen
);
1237 // XXX: ICAP code refuses callbacks during a pending comm_ call
1238 // Async calls development will fix this.
1241 errno
= EINPROGRESS
;
1245 debugs(5,5, "comm_connect_addr: sock=" << sock
<< ", addrinfo( " <<
1246 " flags=" << AI
->ai_flags
<<
1247 ", family=" << AI
->ai_family
<<
1248 ", socktype=" << AI
->ai_socktype
<<
1249 ", protocol=" << AI
->ai_protocol
<<
1250 ", &addr=" << AI
->ai_addr
<<
1251 ", addrlen=" << AI
->ai_addrlen
<<
1253 debugs(5, 9, "connect FD " << sock
<< ": (" << x
<< ") " << xstrerror());
1254 debugs(14,9, "connecting to: " << address
);
1257 #if defined(_SQUID_NEWSOS6_)
1258 /* Makoto MATSUSHITA <matusita@ics.es.osaka-u.ac.jp> */
1260 connect(sock
, AI
->ai_addr
, AI
->ai_addrlen
);
1262 if (errno
== EINVAL
) {
1263 errlen
= sizeof(err
);
1264 x
= getsockopt(sock
, SOL_SOCKET
, SO_ERROR
, &err
, &errlen
);
1271 errlen
= sizeof(err
);
1273 x
= getsockopt(sock
, SOL_SOCKET
, SO_ERROR
, &err
, &errlen
);
1278 #if defined(_SQUID_SOLARIS_)
1280 * Solaris 2.4's socket emulation doesn't allow you
1281 * to determine the error from a failed non-blocking
1282 * connect and just returns EPIPE. Create a fake
1283 * error message for connect. -- fenner@parc.xerox.com
1285 if (x
< 0 && errno
== EPIPE
)
1293 /* Squid seems to be working fine without this code. With this code,
1294 * we leak memory on many connect requests because of EINPROGRESS.
1295 * If you find that this code is needed, please file a bug report. */
1297 #ifdef _SQUID_LINUX_
1299 * Linux Debian replaces our allocated AI pointer with garbage when
1300 * connect() fails. This leads to segmentation faults deallocating
1301 * the system-allocated memory when we go to clean up our pointer.
1302 * HACK: is to leak the memory returned since we can't deallocate.
1310 address
.FreeAddrInfo(AI
);
1312 PROF_stop(comm_connect_addr
);
1314 if (errno
== 0 || errno
== EISCONN
)
1316 else if (ignoreErrno(errno
))
1317 status
= COMM_INPROGRESS
;
1320 if ( F
->sock_family
== AF_INET6
&& address
.IsIPv4() ) {
1322 /* failover to trying IPv4-only link if an IPv6 one fails */
1323 /* to catch the edge case of apps listening on IPv4-localhost */
1324 F
->sock_family
= AF_INET
;
1325 int res
= comm_connect_addr(sock
, address
);
1327 /* if that fails too, undo our temporary socktype hack so the repeat works properly. */
1328 if (res
== COMM_ERROR
)
1329 F
->sock_family
= AF_INET6
;
1336 address
.NtoA(F
->ipaddr
, MAX_IPSTRLEN
);
1338 F
->remote_port
= address
.GetPort(); /* remote_port is HS */
1340 if (status
== COMM_OK
) {
1341 debugs(5, 10, "comm_connect_addr: FD " << sock
<< " connected to " << address
);
1342 } else if (status
== COMM_INPROGRESS
) {
1343 debugs(5, 10, "comm_connect_addr: FD " << sock
<< " connection pending");
1350 commCallCloseHandlers(int fd
)
1352 fde
*F
= &fd_table
[fd
];
1353 debugs(5, 5, "commCallCloseHandlers: FD " << fd
);
1355 while (F
->closeHandler
!= NULL
) {
1356 AsyncCall::Pointer call
= F
->closeHandler
;
1357 F
->closeHandler
= call
->Next();
1358 call
->setNext(NULL
);
1359 // If call is not canceled schedule it for execution else ignore it
1360 if (!call
->canceled()) {
1361 debugs(5, 5, "commCallCloseHandlers: ch->handler=" << call
);
1362 typedef CommCloseCbParams Params
;
1363 Params
¶ms
= GetCommParams
<Params
>(call
);
1365 ScheduleCallHere(call
);
1372 commLingerClose(int fd
, void *unused
)
1374 LOCAL_ARRAY(char, buf
, 1024);
1376 n
= FD_READ_METHOD(fd
, buf
, 1024);
1379 debugs(5, 3, "commLingerClose: FD " << fd
<< " read: " << xstrerror());
1385 commLingerTimeout(int fd
, void *unused
)
1387 debugs(5, 3, "commLingerTimeout: FD " << fd
);
1392 * Inspired by apache
1395 comm_lingering_close(int fd
)
1399 if (fd_table
[fd
].ssl
)
1400 ssl_shutdown_method(fd
);
1404 if (shutdown(fd
, 1) < 0) {
1409 fd_note(fd
, "lingering close");
1410 commSetTimeout(fd
, 10, commLingerTimeout
, NULL
);
1411 commSetSelect(fd
, COMM_SELECT_READ
, commLingerClose
, NULL
, 0);
1417 * enable linger with time of 0 so that when the socket is
1418 * closed, TCP generates a RESET
1421 comm_reset_close(int fd
)
1428 if (setsockopt(fd
, SOL_SOCKET
, SO_LINGER
, (char *) &L
, sizeof(L
)) < 0)
1429 debugs(50, DBG_CRITICAL
, "ERROR: Closing FD " << fd
<< " with TCP RST: " << xstrerror());
1435 comm_close_start(int fd
, void *data
)
1438 fde
*F
= &fd_table
[fd
];
1440 ssl_shutdown_method(fd
);
1447 comm_close_complete(int fd
, void *data
)
1450 fde
*F
= &fd_table
[fd
];
1458 fd_close(fd
); /* update fdstat */
1462 statCounter
.syscalls
.sock
.closes
++;
1464 /* When an fd closes, give accept() a chance, if need be */
1465 Comm::AcceptLimiter::Instance().kick();
1469 * Close the socket fd.
1471 * + call write handlers with ERR_CLOSING
1472 * + call read handlers with ERR_CLOSING
1473 * + call closing handlers
1475 * NOTE: COMM_ERR_CLOSING will NOT be called for CommReads' sitting in a
1476 * DeferredReadManager.
1479 _comm_close(int fd
, char const *file
, int line
)
1481 debugs(5, 3, "comm_close: start closing FD " << fd
);
1483 assert(fd
< Squid_MaxFD
);
1485 fde
*F
= &fd_table
[fd
];
1486 fdd_table
[fd
].close_file
= file
;
1487 fdd_table
[fd
].close_line
= line
;
1492 /* XXX: is this obsolete behind F->closing() ? */
1493 if ( (shutting_down
|| reconfiguring
) && (!F
->flags
.open
|| F
->type
== FD_FILE
))
1496 /* The following fails because ipc.c is doing calls to pipe() to create sockets! */
1499 assert(F
->type
!= FD_FILE
);
1501 PROF_start(comm_close
);
1503 F
->flags
.close_request
= 1;
1505 AsyncCall::Pointer startCall
=commCbCall(5,4, "comm_close_start",
1506 CommCloseCbPtrFun(comm_close_start
, NULL
));
1507 typedef CommCloseCbParams Params
;
1508 Params
&startParams
= GetCommParams
<Params
>(startCall
);
1509 startParams
.fd
= fd
;
1510 ScheduleCallHere(startCall
);
1512 // a half-closed fd may lack a reader, so we stop monitoring explicitly
1513 if (commHasHalfClosedMonitor(fd
))
1514 commStopHalfClosedMonitor(fd
);
1515 commSetTimeout(fd
, -1, NULL
, NULL
);
1517 // notify read/write handlers
1518 if (commio_has_callback(fd
, IOCB_WRITE
, COMMIO_FD_WRITECB(fd
))) {
1519 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), COMM_ERR_CLOSING
, errno
);
1521 if (commio_has_callback(fd
, IOCB_READ
, COMMIO_FD_READCB(fd
))) {
1522 commio_finish_callback(fd
, COMMIO_FD_READCB(fd
), COMM_ERR_CLOSING
, errno
);
1525 commCallCloseHandlers(fd
);
1528 F
->pconn
.pool
->count(F
->pconn
.uses
);
1530 comm_empty_os_read_buffers(fd
);
1533 AsyncCall::Pointer completeCall
=commCbCall(5,4, "comm_close_complete",
1534 CommCloseCbPtrFun(comm_close_complete
, NULL
));
1535 Params
&completeParams
= GetCommParams
<Params
>(completeCall
);
1536 completeParams
.fd
= fd
;
1537 // must use async call to wait for all callbacks
1538 // scheduled before comm_close() to finish
1539 ScheduleCallHere(completeCall
);
1541 PROF_stop(comm_close
);
1544 /* Send a udp datagram to specified TO_ADDR. */
1546 comm_udp_sendto(int fd
,
1547 const Ip::Address
&to_addr
,
1552 struct addrinfo
*AI
= NULL
;
1554 PROF_start(comm_udp_sendto
);
1555 statCounter
.syscalls
.sock
.sendtos
++;
1557 debugs(50, 3, "comm_udp_sendto: Attempt to send UDP packet to " << to_addr
<<
1558 " using FD " << fd
<< " using Port " << comm_local_port(fd
) );
1560 /* BUG: something in the above macro appears to occasionally be setting AI to garbage. */
1561 /* AYJ: 2007-08-27 : or was it because I wasn't then setting 'fd_table[fd].sock_family' to fill properly. */
1562 assert( NULL
== AI
);
1564 to_addr
.GetAddrInfo(AI
, fd_table
[fd
].sock_family
);
1566 x
= sendto(fd
, buf
, len
, 0, AI
->ai_addr
, AI
->ai_addrlen
);
1568 to_addr
.FreeAddrInfo(AI
);
1570 PROF_stop(comm_udp_sendto
);
1575 #ifdef _SQUID_LINUX_
1577 if (ECONNREFUSED
!= errno
)
1580 debugs(50, 1, "comm_udp_sendto: FD " << fd
<< ", (family=" << fd_table
[fd
].sock_family
<< ") " << to_addr
<< ": " << xstrerror());
1586 comm_add_close_handler(int fd
, PF
* handler
, void *data
)
1588 debugs(5, 5, "comm_add_close_handler: FD " << fd
<< ", handler=" <<
1589 handler
<< ", data=" << data
);
1591 AsyncCall::Pointer call
=commCbCall(5,4, "SomeCloseHandler",
1592 CommCloseCbPtrFun(handler
, data
));
1593 comm_add_close_handler(fd
, call
);
1597 comm_add_close_handler(int fd
, AsyncCall::Pointer
&call
)
1599 debugs(5, 5, "comm_add_close_handler: FD " << fd
<< ", AsyncCall=" << call
);
1601 /*TODO:Check for a similar scheduled AsyncCall*/
1602 // for (c = fd_table[fd].closeHandler; c; c = c->next)
1603 // assert(c->handler != handler || c->data != data);
1605 call
->setNext(fd_table
[fd
].closeHandler
);
1607 fd_table
[fd
].closeHandler
= call
;
1611 // remove function-based close handler
1613 comm_remove_close_handler(int fd
, PF
* handler
, void *data
)
1615 assert (isOpen(fd
));
1616 /* Find handler in list */
1617 debugs(5, 5, "comm_remove_close_handler: FD " << fd
<< ", handler=" <<
1618 handler
<< ", data=" << data
);
1620 AsyncCall::Pointer p
;
1621 for (p
= fd_table
[fd
].closeHandler
; p
!= NULL
; p
= p
->Next()) {
1622 typedef CommCbFunPtrCallT
<CommCloseCbPtrFun
> Call
;
1623 const Call
*call
= dynamic_cast<const Call
*>(p
.getRaw());
1624 if (!call
) // method callbacks have their own comm_remove_close_handler
1627 typedef CommCloseCbParams Params
;
1628 const Params
¶ms
= GetCommParams
<Params
>(p
);
1629 if (call
->dialer
.handler
== handler
&& params
.data
== data
)
1630 break; /* This is our handler */
1633 // comm_close removes all close handlers so our handler may be gone
1635 p
->cancel("comm_remove_close_handler");
1636 // TODO: should we remove the handler from the close handlers list?
1639 // remove method-based close handler
1641 comm_remove_close_handler(int fd
, AsyncCall::Pointer
&call
)
1643 assert (isOpen(fd
));
1644 debugs(5, 5, "comm_remove_close_handler: FD " << fd
<< ", AsyncCall=" << call
);
1646 // comm_close removes all close handlers so our handler may be gone
1647 // TODO: should we remove the handler from the close handlers list?
1649 // Check to see if really exist the given AsyncCall in comm_close handlers
1650 // TODO: optimize: this slow code is only needed for the assert() below
1651 AsyncCall::Pointer p
;
1652 for (p
= fd_table
[fd
].closeHandler
; p
!= NULL
&& p
!= call
; p
= p
->Next());
1656 call
->cancel("comm_remove_close_handler");
1660 commSetNoLinger(int fd
)
1664 L
.l_onoff
= 0; /* off */
1667 if (setsockopt(fd
, SOL_SOCKET
, SO_LINGER
, (char *) &L
, sizeof(L
)) < 0)
1668 debugs(50, 0, "commSetNoLinger: FD " << fd
<< ": " << xstrerror());
1670 fd_table
[fd
].flags
.nolinger
= 1;
1674 commSetReuseAddr(int fd
)
1678 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &on
, sizeof(on
)) < 0)
1679 debugs(50, 1, "commSetReuseAddr: FD " << fd
<< ": " << xstrerror());
1683 commSetTcpRcvbuf(int fd
, int size
)
1685 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, (char *) &size
, sizeof(size
)) < 0)
1686 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd
<< ", SIZE " << size
<< ": " << xstrerror());
1687 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, (char *) &size
, sizeof(size
)) < 0)
1688 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd
<< ", SIZE " << size
<< ": " << xstrerror());
1689 #ifdef TCP_WINDOW_CLAMP
1690 if (setsockopt(fd
, SOL_TCP
, TCP_WINDOW_CLAMP
, (char *) &size
, sizeof(size
)) < 0)
1691 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd
<< ", SIZE " << size
<< ": " << xstrerror());
1696 commSetNonBlocking(int fd
)
1698 #ifndef _SQUID_MSWIN_
1702 #ifdef _SQUID_WIN32_
1704 int nonblocking
= TRUE
;
1706 #ifdef _SQUID_CYGWIN_
1708 if (fd_table
[fd
].type
!= FD_PIPE
) {
1711 if (ioctl(fd
, FIONBIO
, &nonblocking
) < 0) {
1712 debugs(50, 0, "commSetNonBlocking: FD " << fd
<< ": " << xstrerror() << " " << fd_table
[fd
].type
);
1716 #ifdef _SQUID_CYGWIN_
1721 #ifndef _SQUID_MSWIN_
1723 if ((flags
= fcntl(fd
, F_GETFL
, dummy
)) < 0) {
1724 debugs(50, 0, "FD " << fd
<< ": fcntl F_GETFL: " << xstrerror());
1728 if (fcntl(fd
, F_SETFL
, flags
| SQUID_NONBLOCK
) < 0) {
1729 debugs(50, 0, "commSetNonBlocking: FD " << fd
<< ": " << xstrerror());
1734 #ifdef _SQUID_CYGWIN_
1739 fd_table
[fd
].flags
.nonblocking
= 1;
1745 commUnsetNonBlocking(int fd
)
1747 #ifdef _SQUID_MSWIN_
1748 int nonblocking
= FALSE
;
1750 if (ioctlsocket(fd
, FIONBIO
, (unsigned long *) &nonblocking
) < 0) {
1755 if ((flags
= fcntl(fd
, F_GETFL
, dummy
)) < 0) {
1756 debugs(50, 0, "FD " << fd
<< ": fcntl F_GETFL: " << xstrerror());
1760 if (fcntl(fd
, F_SETFL
, flags
& (~SQUID_NONBLOCK
)) < 0) {
1762 debugs(50, 0, "commUnsetNonBlocking: FD " << fd
<< ": " << xstrerror());
1766 fd_table
[fd
].flags
.nonblocking
= 0;
1771 commSetCloseOnExec(int fd
)
1777 if ((flags
= fcntl(fd
, F_GETFL
, dummy
)) < 0) {
1778 debugs(50, 0, "FD " << fd
<< ": fcntl F_GETFL: " << xstrerror());
1782 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) < 0)
1783 debugs(50, 0, "FD " << fd
<< ": set close-on-exec failed: " << xstrerror());
1785 fd_table
[fd
].flags
.close_on_exec
= 1;
1792 commSetTcpNoDelay(int fd
)
1796 if (setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *) &on
, sizeof(on
)) < 0)
1797 debugs(50, 1, "commSetTcpNoDelay: FD " << fd
<< ": " << xstrerror());
1799 fd_table
[fd
].flags
.nodelay
= 1;
1805 commSetTcpKeepalive(int fd
, int idle
, int interval
, int timeout
)
1809 if (timeout
&& interval
) {
1810 int count
= (timeout
+ interval
- 1) / interval
;
1811 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPCNT
, &count
, sizeof(on
)) < 0)
1812 debugs(5, 1, "commSetKeepalive: FD " << fd
<< ": " << xstrerror());
1817 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, &idle
, sizeof(on
)) < 0)
1818 debugs(5, 1, "commSetKeepalive: FD " << fd
<< ": " << xstrerror());
1821 #ifdef TCP_KEEPINTVL
1823 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, &interval
, sizeof(on
)) < 0)
1824 debugs(5, 1, "commSetKeepalive: FD " << fd
<< ": " << xstrerror());
1827 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &on
, sizeof(on
)) < 0)
1828 debugs(5, 1, "commSetKeepalive: FD " << fd
<< ": " << xstrerror());
1834 fd_table
=(fde
*) xcalloc(Squid_MaxFD
, sizeof(fde
));
1835 fdd_table
= (fd_debug_t
*)xcalloc(Squid_MaxFD
, sizeof(fd_debug_t
));
1837 /* make sure the accept() socket FIFO delay queue exists */
1838 Comm::AcceptLimiter::Instance();
1840 commfd_table
= (comm_fd_t
*) xcalloc(Squid_MaxFD
, sizeof(comm_fd_t
));
1841 for (int pos
= 0; pos
< Squid_MaxFD
; pos
++) {
1842 commfd_table
[pos
].fd
= pos
;
1843 commfd_table
[pos
].readcb
.fd
= pos
;
1844 commfd_table
[pos
].readcb
.type
= IOCB_READ
;
1845 commfd_table
[pos
].writecb
.fd
= pos
;
1846 commfd_table
[pos
].writecb
.type
= IOCB_WRITE
;
1849 /* XXX account fd_table */
1850 /* Keep a few file descriptors free so that we don't run out of FD's
1851 * after accepting a client but before it opens a socket or a file.
1852 * Since Squid_MaxFD can be as high as several thousand, don't waste them */
1853 RESERVED_FD
= min(100, Squid_MaxFD
/ 4);
1855 conn_close_pool
= memPoolCreate("close_handler", sizeof(close_handler
));
1857 TheHalfClosed
= new DescriptorSet
;
1863 delete TheHalfClosed
;
1864 TheHalfClosed
= NULL
;
1866 safe_free(fd_table
);
1867 safe_free(fdd_table
);
1868 safe_free(commfd_table
);
1873 commHandleWrite(int fd
, void *data
)
1875 comm_io_callback_t
*state
= (comm_io_callback_t
*)data
;
1879 assert(state
== COMMIO_FD_WRITECB(fd
));
1881 PROF_start(commHandleWrite
);
1882 debugs(5, 5, "commHandleWrite: FD " << fd
<< ": off " <<
1883 (long int) state
->offset
<< ", sz " << (long int) state
->size
<< ".");
1885 nleft
= state
->size
- state
->offset
;
1886 len
= FD_WRITE_METHOD(fd
, state
->buf
+ state
->offset
, nleft
);
1887 debugs(5, 5, "commHandleWrite: write() returns " << len
);
1888 fd_bytes(fd
, len
, FD_WRITE
);
1889 statCounter
.syscalls
.sock
.writes
++;
1890 // After each successful partial write,
1891 // reset fde::writeStart to the current time.
1892 fd_table
[fd
].writeStart
= squid_curtime
;
1895 /* Note we even call write if nleft == 0 */
1899 debugs(5, 1, "commHandleWrite: FD " << fd
<< ": write failure: connection closed with " << nleft
<< " bytes remaining.");
1901 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), nleft
? COMM_ERROR
: COMM_OK
, errno
);
1902 } else if (len
< 0) {
1905 if (fd_table
[fd
].flags
.socket_eof
) {
1906 debugs(50, 2, "commHandleWrite: FD " << fd
<< ": write failure: " << xstrerror() << ".");
1907 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), nleft
? COMM_ERROR
: COMM_OK
, errno
);
1908 } else if (ignoreErrno(errno
)) {
1909 debugs(50, 10, "commHandleWrite: FD " << fd
<< ": write failure: " << xstrerror() << ".");
1916 debugs(50, 2, "commHandleWrite: FD " << fd
<< ": write failure: " << xstrerror() << ".");
1917 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), nleft
? COMM_ERROR
: COMM_OK
, errno
);
1920 /* A successful write, continue */
1921 state
->offset
+= len
;
1923 if (state
->offset
< state
->size
) {
1924 /* Not done, reinstall the write handler and write some more */
1931 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), nleft
? COMM_OK
: COMM_ERROR
, errno
);
1935 PROF_stop(commHandleWrite
);
1939 * Queue a write. handler/handler_data are called when the write
1940 * completes, on error, or on file descriptor close.
1942 * free_func is used to free the passed buffer when the write has completed.
1945 comm_write(int fd
, const char *buf
, int size
, IOCB
* handler
, void *handler_data
, FREE
* free_func
)
1947 AsyncCall::Pointer call
= commCbCall(5,5, "SomeCommWriteHander",
1948 CommIoCbPtrFun(handler
, handler_data
));
1950 comm_write(fd
, buf
, size
, call
, free_func
);
1954 comm_write(int fd
, const char *buf
, int size
, AsyncCall::Pointer
&callback
, FREE
* free_func
)
1956 debugs(5, 5, "comm_write: FD " << fd
<< ": sz " << size
<< ": asynCall " << callback
);
1958 /* Make sure we are open, not closing, and not writing */
1960 assert(!fd_table
[fd
].closing());
1961 comm_io_callback_t
*ccb
= COMMIO_FD_WRITECB(fd
);
1962 assert(!ccb
->active());
1964 fd_table
[fd
].writeStart
= squid_curtime
;
1965 /* Queue the write */
1966 commio_set_callback(fd
, IOCB_WRITE
, ccb
, callback
,
1967 (char *)buf
, free_func
, size
);
1968 commSetSelect(fd
, COMM_SELECT_WRITE
, commHandleWrite
, ccb
, 0);
1972 /* a wrapper around comm_write to allow for MemBuf to be comm_written in a snap */
1974 comm_write_mbuf(int fd
, MemBuf
*mb
, IOCB
* handler
, void *handler_data
)
1976 comm_write(fd
, mb
->buf
, mb
->size
, handler
, handler_data
, mb
->freeFunc());
1980 comm_write_mbuf(int fd
, MemBuf
*mb
, AsyncCall::Pointer
&callback
)
1982 comm_write(fd
, mb
->buf
, mb
->size
, callback
, mb
->freeFunc());
1987 * hm, this might be too general-purpose for all the places we'd
1991 ignoreErrno(int ierrno
)
1998 #if EAGAIN != EWOULDBLOCK
2021 commCloseAllSockets(void)
2026 for (fd
= 0; fd
<= Biggest_FD
; fd
++) {
2032 if (F
->type
!= FD_SOCKET
)
2035 if (F
->flags
.ipc
) /* don't close inter-process sockets */
2038 if (F
->timeoutHandler
!= NULL
) {
2039 AsyncCall::Pointer callback
= F
->timeoutHandler
;
2040 F
->timeoutHandler
= NULL
;
2041 debugs(5, 5, "commCloseAllSockets: FD " << fd
<< ": Calling timeout handler");
2042 ScheduleCallHere(callback
);
2044 debugs(5, 5, "commCloseAllSockets: FD " << fd
<< ": calling comm_reset_close()");
2045 comm_reset_close(fd
);
2051 AlreadyTimedOut(fde
*F
)
2056 if (F
->timeout
== 0)
2059 if (F
->timeout
> squid_curtime
)
2066 writeTimedOut(int fd
)
2068 if (!commio_has_callback(fd
, IOCB_WRITE
, COMMIO_FD_WRITECB(fd
)))
2071 if ((squid_curtime
- fd_table
[fd
].writeStart
) < Config
.Timeout
.write
)
2082 AsyncCall::Pointer callback
;
2084 for (fd
= 0; fd
<= Biggest_FD
; fd
++) {
2087 if (writeTimedOut(fd
)) {
2088 // We have an active write callback and we are timed out
2089 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), COMM_ERROR
, ETIMEDOUT
);
2090 } else if (AlreadyTimedOut(F
))
2093 debugs(5, 5, "checkTimeouts: FD " << fd
<< " Expired");
2095 if (F
->timeoutHandler
!= NULL
) {
2096 debugs(5, 5, "checkTimeouts: FD " << fd
<< ": Call timeout handler");
2097 callback
= F
->timeoutHandler
;
2098 F
->timeoutHandler
= NULL
;
2099 ScheduleCallHere(callback
);
2101 debugs(5, 5, "checkTimeouts: FD " << fd
<< ": Forcing comm_close()");
2107 void CommIO::Initialise()
2109 /* Initialize done pipe signal */
2111 if (pipe(DonePipe
)) {}
2112 DoneFD
= DonePipe
[1];
2113 DoneReadFD
= DonePipe
[0];
2114 fd_open(DoneReadFD
, FD_PIPE
, "async-io completetion event: main");
2115 fd_open(DoneFD
, FD_PIPE
, "async-io completetion event: threads");
2116 commSetNonBlocking(DoneReadFD
);
2117 commSetNonBlocking(DoneFD
);
2118 commSetSelect(DoneReadFD
, COMM_SELECT_READ
, NULLFDHandler
, NULL
, 0);
2122 void CommIO::NotifyIOClose()
2124 /* Close done pipe signal */
2129 fd_close(DoneReadFD
);
2130 Initialised
= false;
2133 bool CommIO::Initialised
= false;
2134 bool CommIO::DoneSignalled
= false;
2135 int CommIO::DoneFD
= -1;
2136 int CommIO::DoneReadFD
= -1;
2142 FD_READ_METHOD(DoneReadFD
, buf
, sizeof(buf
));
2146 CommIO::NULLFDHandler(int fd
, void *data
)
2149 commSetSelect(fd
, COMM_SELECT_READ
, NULLFDHandler
, NULL
, 0);
2153 CommIO::ResetNotifications()
2155 if (DoneSignalled
) {
2157 DoneSignalled
= false;
2161 /// Start waiting for a possibly half-closed connection to close
2162 // by scheduling a read callback to a monitoring handler that
2163 // will close the connection on read errors.
2165 commStartHalfClosedMonitor(int fd
)
2167 debugs(5, 5, HERE
<< "adding FD " << fd
<< " to " << *TheHalfClosed
);
2169 assert(!commHasHalfClosedMonitor(fd
));
2170 (void)TheHalfClosed
->add(fd
); // could also assert the result
2171 commPlanHalfClosedCheck(); // may schedule check if we added the first FD
2176 commPlanHalfClosedCheck()
2178 if (!WillCheckHalfClosed
&& !TheHalfClosed
->empty()) {
2179 eventAdd("commHalfClosedCheck", &commHalfClosedCheck
, NULL
, 1.0, 1);
2180 WillCheckHalfClosed
= true;
2184 /// iterates over all descriptors that may need half-closed tests and
2185 /// calls comm_read for those that do; re-schedules the check if needed
2188 commHalfClosedCheck(void *)
2190 debugs(5, 5, HERE
<< "checking " << *TheHalfClosed
);
2192 typedef DescriptorSet::const_iterator DSCI
;
2193 const DSCI end
= TheHalfClosed
->end();
2194 for (DSCI i
= TheHalfClosed
->begin(); i
!= end
; ++i
) {
2196 if (!fd_table
[fd
].halfClosedReader
) { // not reading already
2197 AsyncCall::Pointer call
= commCbCall(5,4, "commHalfClosedReader",
2198 CommIoCbPtrFun(&commHalfClosedReader
, NULL
));
2199 comm_read(fd
, NULL
, 0, call
);
2200 fd_table
[fd
].halfClosedReader
= call
;
2204 WillCheckHalfClosed
= false; // as far as we know
2205 commPlanHalfClosedCheck(); // may need to check again
2208 /// checks whether we are waiting for possibly half-closed connection to close
2209 // We are monitoring if the read handler for the fd is the monitoring handler.
2211 commHasHalfClosedMonitor(int fd
)
2213 return TheHalfClosed
->has(fd
);
2216 /// stop waiting for possibly half-closed connection to close
2218 commStopHalfClosedMonitor(int const fd
)
2220 debugs(5, 5, HERE
<< "removing FD " << fd
<< " from " << *TheHalfClosed
);
2222 // cancel the read if one was scheduled
2223 AsyncCall::Pointer reader
= fd_table
[fd
].halfClosedReader
;
2225 comm_read_cancel(fd
, reader
);
2226 fd_table
[fd
].halfClosedReader
= NULL
;
2228 TheHalfClosed
->del(fd
);
2231 /// I/O handler for the possibly half-closed connection monitoring code
2233 commHalfClosedReader(int fd
, char *, size_t size
, comm_err_t flag
, int, void *)
2235 // there cannot be more data coming in on half-closed connections
2237 assert(commHasHalfClosedMonitor(fd
)); // or we would have canceled the read
2239 fd_table
[fd
].halfClosedReader
= NULL
; // done reading, for now
2241 // nothing to do if fd is being closed
2242 if (flag
== COMM_ERR_CLOSING
)
2245 // if read failed, close the connection
2246 if (flag
!= COMM_OK
) {
2247 debugs(5, 3, "commHalfClosedReader: closing FD " << fd
);
2252 // continue waiting for close or error
2253 commPlanHalfClosedCheck(); // make sure this fd will be checked again
2257 CommRead::CommRead() : fd(-1), buf(NULL
), len(0), callback(NULL
) {}
2259 CommRead::CommRead(int fd_
, char *buf_
, int len_
, AsyncCall::Pointer
&callback_
)
2260 : fd(fd_
), buf(buf_
), len(len_
), callback(callback_
) {}
2262 DeferredRead::DeferredRead () : theReader(NULL
), theContext(NULL
), theRead(), cancelled(false) {}
2264 DeferredRead::DeferredRead (DeferrableRead
*aReader
, void *data
, CommRead
const &aRead
) : theReader(aReader
), theContext (data
), theRead(aRead
), cancelled(false) {}
2266 DeferredReadManager::~DeferredReadManager()
2269 assert (deferredReads
.empty());
2272 /* explicit instantiation required for some systems */
2274 /// \cond AUTODOCS-IGNORE
2275 template cbdata_type CbDataList
<DeferredRead
>::CBDATA_CbDataList
;
2279 DeferredReadManager::delayRead(DeferredRead
const &aRead
)
2281 debugs(5, 3, "Adding deferred read on FD " << aRead
.theRead
.fd
);
2282 CbDataList
<DeferredRead
> *temp
= deferredReads
.push_back(aRead
);
2284 // We have to use a global function as a closer and point to temp
2285 // instead of "this" because DeferredReadManager is not a job and
2286 // is not even cbdata protected
2287 AsyncCall::Pointer closer
= commCbCall(5,4,
2288 "DeferredReadManager::CloseHandler",
2289 CommCloseCbPtrFun(&CloseHandler
, temp
));
2290 comm_add_close_handler(aRead
.theRead
.fd
, closer
);
2291 temp
->element
.closer
= closer
; // remeber so that we can cancel
2295 DeferredReadManager::CloseHandler(int fd
, void *thecbdata
)
2297 if (!cbdataReferenceValid (thecbdata
))
2300 CbDataList
<DeferredRead
> *temp
= (CbDataList
<DeferredRead
> *)thecbdata
;
2302 temp
->element
.closer
= NULL
;
2303 temp
->element
.markCancelled();
2307 DeferredReadManager::popHead(CbDataListContainer
<DeferredRead
> &deferredReads
)
2309 assert (!deferredReads
.empty());
2311 DeferredRead
&read
= deferredReads
.head
->element
;
2312 if (!read
.cancelled
) {
2313 comm_remove_close_handler(read
.theRead
.fd
, read
.closer
);
2317 DeferredRead result
= deferredReads
.pop_front();
2323 DeferredReadManager::kickReads(int const count
)
2325 /* if we had CbDataList::size() we could consolidate this and flushReads */
2332 size_t remaining
= count
;
2334 while (!deferredReads
.empty() && remaining
) {
2335 DeferredRead aRead
= popHead(deferredReads
);
2338 if (!aRead
.cancelled
)
2344 DeferredReadManager::flushReads()
2346 CbDataListContainer
<DeferredRead
> reads
;
2347 reads
= deferredReads
;
2348 deferredReads
= CbDataListContainer
<DeferredRead
>();
2350 // XXX: For fairness this SHOULD randomize the order
2351 while (!reads
.empty()) {
2352 DeferredRead aRead
= popHead(reads
);
2358 DeferredReadManager::kickARead(DeferredRead
const &aRead
)
2360 if (aRead
.cancelled
)
2363 if (aRead
.theRead
.fd
>=0 && fd_table
[aRead
.theRead
.fd
].closing())
2366 debugs(5, 3, "Kicking deferred read on FD " << aRead
.theRead
.fd
);
2368 aRead
.theReader(aRead
.theContext
, aRead
.theRead
);
2372 DeferredRead::markCancelled()
2377 ConnectionDetail::ConnectionDetail() : me(), peer()
2382 CommSelectEngine::checkEvents(int timeout
)
2384 static time_t last_timeout
= 0;
2386 /* No, this shouldn't be here. But it shouldn't be in each comm handler. -adrian */
2387 if (squid_curtime
> last_timeout
) {
2388 last_timeout
= squid_curtime
;
2392 switch (comm_select(timeout
)) {
2408 fatal_dump("comm.cc: Internal error -- this should never happen.");