2 * DEBUG: section 05 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/Address.h"
53 #include "ip/Intercept.h"
56 #if defined(_SQUID_CYGWIN_)
57 #include <sys/ioctl.h>
59 #ifdef HAVE_NETINET_TCP_H
60 #include <netinet/tcp.h>
64 * New C-like simple comm code. This stuff is a mess and doesn't really buy us anything.
73 static void commStopHalfClosedMonitor(int fd
);
74 static IOCB commHalfClosedReader
;
75 static void comm_init_opened(int new_socket
, Ip::Address
&addr
, unsigned char TOS
, const char *note
, struct addrinfo
*AI
);
76 static int comm_apply_flags(int new_socket
, Ip::Address
&addr
, int flags
, struct addrinfo
*AI
);
79 struct comm_io_callback_t
{
82 AsyncCall::Pointer callback
;
90 bool active() const { return callback
!= NULL
; }
95 comm_io_callback_t readcb
;
96 comm_io_callback_t writecb
;
98 typedef struct _comm_fd comm_fd_t
;
99 comm_fd_t
*commfd_table
;
101 // TODO: make this a comm_io_callback_t method?
103 commio_has_callback(int fd
, iocb_type type
, comm_io_callback_t
*ccb
)
105 assert(ccb
->fd
== fd
);
106 assert(ccb
->type
== type
);
107 return ccb
->active();
111 * Configure comm_io_callback_t for I/O
113 * @param fd filedescriptor
114 * @param ccb comm io callback
116 * @param cbdata callback data (must be cbdata'ed)
117 * @param buf buffer, if applicable
118 * @param freefunc freefunc, if applicable
119 * @param size buffer size
122 commio_set_callback(int fd
, iocb_type type
, comm_io_callback_t
*ccb
,
123 AsyncCall::Pointer
&cb
, char *buf
, FREE
*freefunc
, int size
)
125 assert(!ccb
->active());
126 assert(ccb
->type
== type
);
131 ccb
->freefunc
= freefunc
;
137 // Schedule the callback call and clear the callback
139 commio_finish_callback(int fd
, comm_io_callback_t
*ccb
, comm_err_t code
, int xerrno
)
141 debugs(5, 3, "commio_finish_callback: called for FD " << fd
<< " (" <<
142 code
<< ", " << xerrno
<< ")");
143 assert(ccb
->active());
144 assert(ccb
->fd
== fd
);
146 ccb
->xerrno
= xerrno
;
148 comm_io_callback_t cb
= *ccb
;
150 /* We've got a copy; blow away the real one */
151 /* XXX duplicate code from commio_cancel_callback! */
153 ccb
->callback
= NULL
; // cb has it
161 if (cb
.callback
!= NULL
) {
162 typedef CommIoCbParams Params
;
163 Params
¶ms
= GetCommParams
<Params
>(cb
.callback
);
166 params
.size
= cb
.offset
;
167 params
.flag
= cb
.errcode
;
168 params
.xerrno
= cb
.xerrno
;
169 ScheduleCallHere(cb
.callback
);
175 * Cancel the given callback
177 * Remember that the data is cbdataRef'ed.
179 // TODO: make this a comm_io_callback_t method
181 commio_cancel_callback(int fd
, comm_io_callback_t
*ccb
)
183 debugs(5, 3, "commio_cancel_callback: called for FD " << fd
);
184 assert(ccb
->fd
== fd
);
185 assert(ccb
->active());
188 ccb
->callback
= NULL
;
192 * Call the given comm callback; assumes the callback is valid.
194 * @param ccb io completion callback
197 commio_call_callback(comm_io_callback_t
*ccb
)
201 class ConnectStateData
205 void *operator new (size_t);
206 void operator delete (void *);
207 static void Connect (int fd
, void *me
);
209 void callCallback(comm_err_t status
, int xerrno
);
212 // defaults given by client
214 u_short default_port
;
215 Ip::Address default_addr
;
216 // NP: CANNOT store the default addr:port together as it gets set/reset differently.
218 DnsLookupDetails dns
; ///< host lookup details
220 AsyncCall::Pointer callback
;
229 int commRetryConnect();
230 CBDATA_CLASS(ConnectStateData
);
235 static DescriptorSet
*TheHalfClosed
= NULL
; /// the set of half-closed FDs
236 static bool WillCheckHalfClosed
= false; /// true if check is scheduled
237 static EVH commHalfClosedCheck
;
238 static void commPlanHalfClosedCheck();
240 static comm_err_t
commBind(int s
, struct addrinfo
&);
241 static void commSetReuseAddr(int);
242 static void commSetNoLinger(int);
244 static void commSetTcpNoDelay(int);
246 static void commSetTcpRcvbuf(int, int);
247 static PF commConnectFree
;
248 static PF commHandleWrite
;
249 static IPH commConnectDnsHandle
;
256 static MemAllocator
*conn_close_pool
= NULL
;
257 fd_debug_t
*fdd_table
= NULL
;
262 return fd_table
[fd
].flags
.open
!= 0;
268 * If the read attempt succeeds or fails, call the callback.
269 * Else, wait for another IO notification.
272 commHandleRead(int fd
, void *data
)
274 comm_io_callback_t
*ccb
= (comm_io_callback_t
*) data
;
276 assert(data
== COMMIO_FD_READCB(fd
));
277 assert(commio_has_callback(fd
, IOCB_READ
, ccb
));
279 statCounter
.syscalls
.sock
.reads
++;
282 retval
= FD_READ_METHOD(fd
, ccb
->buf
, ccb
->size
);
283 debugs(5, 3, "comm_read_try: FD " << fd
<< ", size " << ccb
->size
<< ", retval " << retval
<< ", errno " << errno
);
285 if (retval
< 0 && !ignoreErrno(errno
)) {
286 debugs(5, 3, "comm_read_try: scheduling COMM_ERROR");
288 commio_finish_callback(fd
, ccb
, COMM_ERROR
, errno
);
292 /* See if we read anything */
293 /* Note - read 0 == socket EOF, which is a valid read */
295 fd_bytes(fd
, retval
, FD_READ
);
296 ccb
->offset
= retval
;
297 commio_finish_callback(fd
, ccb
, COMM_OK
, errno
);
301 /* Nope, register for some more IO */
302 commSetSelect(fd
, COMM_SELECT_READ
, commHandleRead
, data
, 0);
306 * Queue a read. handler/handler_data are called when the read
307 * completes, on error, or on file descriptor close.
310 comm_read(int fd
, char *buf
, int size
, IOCB
*handler
, void *handler_data
)
312 AsyncCall::Pointer call
= commCbCall(5,4, "SomeCommReadHandler",
313 CommIoCbPtrFun(handler
, handler_data
));
314 comm_read(fd
, buf
, size
, call
);
318 comm_read(int fd
, char *buf
, int size
, AsyncCall::Pointer
&callback
)
320 debugs(5, 5, "comm_read, queueing read for FD " << fd
<< "; asynCall " << callback
);
322 /* Make sure we are open and not closing */
324 assert(!fd_table
[fd
].closing());
325 comm_io_callback_t
*ccb
= COMMIO_FD_READCB(fd
);
327 // Make sure we are either not reading or just passively monitoring.
328 // Active/passive conflicts are OK and simply cancel passive monitoring.
330 // if the assertion below fails, we have an active comm_read conflict
331 assert(fd_table
[fd
].halfClosedReader
!= NULL
);
332 commStopHalfClosedMonitor(fd
);
333 assert(!ccb
->active());
337 commio_set_callback(fd
, IOCB_READ
, ccb
, callback
, (char *)buf
, NULL
, size
);
338 commSetSelect(fd
, COMM_SELECT_READ
, commHandleRead
, ccb
, 0);
342 * Empty the read buffers
344 * This is a magical routine that empties the read buffers.
345 * Under some platforms (Linux) if a buffer has data in it before
346 * you call close(), the socket will hang and take quite a while
350 comm_empty_os_read_buffers(int fd
)
353 /* prevent those nasty RST packets */
354 char buf
[SQUID_TCP_SO_RCVBUF
];
356 if (fd_table
[fd
].flags
.nonblocking
== 1) {
357 while (FD_READ_METHOD(fd
, buf
, SQUID_TCP_SO_RCVBUF
) > 0) {};
364 * Return whether the FD has a pending completed callback.
367 comm_has_pending_read_callback(int fd
)
370 // XXX: We do not know whether there is a read callback scheduled.
371 // This is used for pconn management that should probably be more
372 // tightly integrated into comm to minimize the chance that a
373 // closing pconn socket will be used for a new transaction.
377 // Does comm check this fd for read readiness?
378 // Note that when comm is not monitoring, there can be a pending callback
379 // call, which may resume comm monitoring once fired.
381 comm_monitors_read(int fd
)
384 // Being active is usually the same as monitoring because we always
385 // start monitoring the FD when we configure comm_io_callback_t for I/O
386 // and we usually configure comm_io_callback_t for I/O when we starting
387 // monitoring a FD for reading. TODO: replace with commio_has_callback
388 return COMMIO_FD_READCB(fd
)->active();
392 * Cancel a pending read. Assert that we have the right parameters,
393 * and that there are no pending read events!
395 * XXX: We do not assert that there are no pending read events and
396 * with async calls it becomes even more difficult.
397 * The whole interface should be reworked to do callback->cancel()
398 * instead of searching for places where the callback may be stored and
399 * updating the state of those places.
401 * AHC Don't call the comm handlers?
404 comm_read_cancel(int fd
, IOCB
*callback
, void *data
)
407 debugs(5, 4, "comm_read_cancel fails: FD " << fd
<< " closed");
411 comm_io_callback_t
*cb
= COMMIO_FD_READCB(fd
);
412 // TODO: is "active" == "monitors FD"?
414 debugs(5, 4, "comm_read_cancel fails: FD " << fd
<< " inactive");
418 typedef CommCbFunPtrCallT
<CommIoCbPtrFun
> Call
;
419 Call
*call
= dynamic_cast<Call
*>(cb
->callback
.getRaw());
421 debugs(5, 4, "comm_read_cancel fails: FD " << fd
<< " lacks callback");
425 call
->cancel("old comm_read_cancel");
427 typedef CommIoCbParams Params
;
428 const Params
¶ms
= GetCommParams
<Params
>(cb
->callback
);
430 /* Ok, we can be reasonably sure we won't lose any data here! */
431 assert(call
->dialer
.handler
== callback
);
432 assert(params
.data
== data
);
434 /* Delete the callback */
435 commio_cancel_callback(fd
, cb
);
437 /* And the IO event */
438 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
442 comm_read_cancel(int fd
, AsyncCall::Pointer
&callback
)
444 callback
->cancel("comm_read_cancel");
447 debugs(5, 4, "comm_read_cancel fails: FD " << fd
<< " closed");
451 comm_io_callback_t
*cb
= COMMIO_FD_READCB(fd
);
454 debugs(5, 4, "comm_read_cancel fails: FD " << fd
<< " inactive");
458 AsyncCall::Pointer call
= cb
->callback
;
459 assert(call
!= NULL
); // XXX: should never fail (active() checks for callback==NULL)
461 /* Ok, we can be reasonably sure we won't lose any data here! */
462 assert(call
== callback
);
464 /* Delete the callback */
465 commio_cancel_callback(fd
, cb
);
467 /* And the IO event */
468 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
473 * synchronous wrapper around udp socket functions
476 comm_udp_recvfrom(int fd
, void *buf
, size_t len
, int flags
, Ip::Address
&from
)
478 statCounter
.syscalls
.sock
.recvfroms
++;
480 struct addrinfo
*AI
= NULL
;
482 debugs(5,8, "comm_udp_recvfrom: FD " << fd
<< " from " << from
);
484 assert( NULL
== AI
);
486 from
.InitAddrInfo(AI
);
488 x
= recvfrom(fd
, buf
, len
, flags
, AI
->ai_addr
, &AI
->ai_addrlen
);
492 from
.FreeAddrInfo(AI
);
498 comm_udp_recv(int fd
, void *buf
, size_t len
, int flags
)
501 return comm_udp_recvfrom(fd
, buf
, len
, flags
, nul
);
505 comm_udp_send(int s
, const void *buf
, size_t len
, int flags
)
507 return send(s
, buf
, len
, flags
);
512 comm_has_incomplete_write(int fd
)
515 return COMMIO_FD_WRITECB(fd
)->active();
519 * Queue a write. handler/handler_data are called when the write fully
520 * completes, on error, or on file descriptor close.
523 /* Return the local port associated with fd. */
525 comm_local_port(int fd
)
528 struct addrinfo
*addr
= NULL
;
529 fde
*F
= &fd_table
[fd
];
531 /* If the fd is closed already, just return */
533 if (!F
->flags
.open
) {
534 debugs(5, 0, "comm_local_port: FD " << fd
<< " has been closed.");
538 if (F
->local_addr
.GetPort())
539 return F
->local_addr
.GetPort();
541 if (F
->sock_family
== AF_INET
)
544 temp
.InitAddrInfo(addr
);
546 if (getsockname(fd
, addr
->ai_addr
, &(addr
->ai_addrlen
)) ) {
547 debugs(50, 1, "comm_local_port: Failed to retrieve TCP/UDP port number for socket: FD " << fd
<< ": " << xstrerror());
548 temp
.FreeAddrInfo(addr
);
553 temp
.FreeAddrInfo(addr
);
555 F
->local_addr
.SetPort(temp
.GetPort());
557 #if 0 // seems to undo comm_open actions on the FD ...
558 // grab default socket information for this address
559 temp
.GetAddrInfo(addr
);
561 F
->sock_family
= addr
->ai_family
;
563 temp
.FreeAddrInfo(addr
);
566 debugs(5, 6, "comm_local_port: FD " << fd
<< ": port " << F
->local_addr
.GetPort() << "(family=" << F
->sock_family
<< ")");
567 return F
->local_addr
.GetPort();
571 commBind(int s
, struct addrinfo
&inaddr
)
573 statCounter
.syscalls
.sock
.binds
++;
575 if (bind(s
, inaddr
.ai_addr
, inaddr
.ai_addrlen
) == 0) {
576 debugs(50, 6, "commBind: bind socket FD " << s
<< " to " << fd_table
[s
].local_addr
);
580 debugs(50, 0, "commBind: Cannot bind socket FD " << s
<< " to " << fd_table
[s
].local_addr
<< ": " << xstrerror());
586 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
587 * is OR of flags specified in comm.h. Defaults TOS
590 comm_open(int sock_type
,
596 return comm_openex(sock_type
, proto
, addr
, flags
, 0, note
);
600 comm_open_listener(int sock_type
,
608 /* all listener sockets require bind() */
609 flags
|= COMM_DOBIND
;
611 /* attempt native enabled port. */
612 sock
= comm_openex(sock_type
, proto
, addr
, flags
, 0, note
);
618 limitError(int const anErrno
)
620 return anErrno
== ENFILE
|| anErrno
== EMFILE
;
624 comm_set_tos(int fd
, int tos
)
627 int x
= setsockopt(fd
, IPPROTO_IP
, IP_TOS
, (char *) &tos
, sizeof(int));
629 debugs(50, 1, "comm_set_tos: setsockopt(IP_TOS) on FD " << fd
<< ": " << xstrerror());
632 debugs(50, 0, "WARNING: setsockopt(IP_TOS) not supported on this platform");
638 comm_set_v6only(int fd
, int tos
)
641 if (setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, (char *) &tos
, sizeof(int)) < 0) {
642 debugs(50, 1, "comm_open: setsockopt(IPV6_V6ONLY) " << (tos
?"ON":"OFF") << " for FD " << fd
<< ": " << xstrerror());
645 debugs(50, 0, "WARNING: comm_open: setsockopt(IPV6_V6ONLY) not supported on this platform");
650 * Set the socket IP_TRANSPARENT option for Linux TPROXY v4 support.
653 comm_set_transparent(int fd
)
655 #if defined(IP_TRANSPARENT)
657 if (setsockopt(fd
, SOL_IP
, IP_TRANSPARENT
, (char *) &tos
, sizeof(int)) < 0) {
658 debugs(50, DBG_IMPORTANT
, "comm_open: setsockopt(IP_TRANSPARENT) on FD " << fd
<< ": " << xstrerror());
660 /* mark the socket as having transparent options */
661 fd_table
[fd
].flags
.transparent
= 1;
664 debugs(50, DBG_CRITICAL
, "WARNING: comm_open: setsockopt(IP_TRANSPARENT) not supported on this platform");
669 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
670 * is OR of flags specified in defines.h:COMM_*
673 comm_openex(int sock_type
,
682 struct addrinfo
*AI
= NULL
;
684 PROF_start(comm_open
);
685 /* Create socket for accepting new connections. */
686 statCounter
.syscalls
.sock
.sockets
++;
688 /* Setup the socket addrinfo details for use */
689 addr
.GetAddrInfo(AI
);
690 AI
->ai_socktype
= sock_type
;
691 AI
->ai_protocol
= proto
;
693 debugs(50, 3, "comm_openex: Attempt open socket for: " << addr
);
695 new_socket
= socket(AI
->ai_family
, AI
->ai_socktype
, AI
->ai_protocol
);
697 /* under IPv6 there is the possibility IPv6 is present but disabled. */
698 /* try again as IPv4-native if possible */
699 if ( new_socket
< 0 && Ip::EnableIpv6
&& addr
.IsIPv6() && addr
.SetIPv4() ) {
700 /* attempt to open this IPv4-only. */
701 addr
.FreeAddrInfo(AI
);
702 /* Setup the socket addrinfo details for use */
703 addr
.GetAddrInfo(AI
);
704 AI
->ai_socktype
= sock_type
;
705 AI
->ai_protocol
= proto
;
706 debugs(50, 3, "comm_openex: Attempt fallback open socket for: " << addr
);
707 new_socket
= socket(AI
->ai_family
, AI
->ai_socktype
, AI
->ai_protocol
);
708 debugs(50, 2, HERE
<< "attempt open " << note
<< " socket on: " << addr
);
711 if (new_socket
< 0) {
712 /* Increase the number of reserved fd's if calls to socket()
713 * are failing because the open file table is full. This
714 * limits the number of simultaneous clients */
716 if (limitError(errno
)) {
717 debugs(50, DBG_IMPORTANT
, "comm_open: socket failure: " << xstrerror());
720 debugs(50, DBG_CRITICAL
, "comm_open: socket failure: " << xstrerror());
723 addr
.FreeAddrInfo(AI
);
725 PROF_stop(comm_open
);
729 debugs(50, 3, "comm_openex: Opened socket FD " << new_socket
<< " : family=" << AI
->ai_family
<< ", type=" << AI
->ai_socktype
<< ", protocol=" << AI
->ai_protocol
);
731 /* set TOS if needed */
732 if (TOS
&& comm_set_tos(new_socket
, TOS
) ) {
736 if ( Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& addr
.IsIPv6() )
737 comm_set_v6only(new_socket
, 1);
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. */
741 if ( Ip::EnableIpv6
&IPV6_SPECIAL_V4MAPPING
&& addr
.IsIPv6() )
742 comm_set_v6only(new_socket
, 0);
744 comm_init_opened(new_socket
, addr
, TOS
, note
, AI
);
745 new_socket
= comm_apply_flags(new_socket
, addr
, flags
, AI
);
747 addr
.FreeAddrInfo(AI
);
749 PROF_stop(comm_open
);
754 /// update FD tables after a local or remote (IPC) comm_openex();
756 comm_init_opened(int new_socket
,
762 assert(new_socket
>= 0);
768 debugs(5, 5, "comm_open: FD " << new_socket
<< " is a new socket");
770 assert(!isOpen(new_socket
));
771 fd_open(new_socket
, FD_SOCKET
, note
);
773 fdd_table
[new_socket
].close_file
= NULL
;
775 fdd_table
[new_socket
].close_line
= 0;
777 F
= &fd_table
[new_socket
];
779 F
->local_addr
= addr
;
783 F
->sock_family
= AI
->ai_family
;
786 /// apply flags after a local comm_open*() call;
787 /// returns new_socket or -1 on error
789 comm_apply_flags(int new_socket
,
794 assert(new_socket
>= 0);
796 const int sock_type
= AI
->ai_socktype
;
798 if (!(flags
& COMM_NOCLOEXEC
))
799 commSetCloseOnExec(new_socket
);
801 if ((flags
& COMM_REUSEADDR
))
802 commSetReuseAddr(new_socket
);
804 if (addr
.GetPort() > (u_short
) 0) {
806 if (sock_type
!= SOCK_DGRAM
)
808 commSetNoLinger(new_socket
);
811 commSetReuseAddr(new_socket
);
814 /* MUST be done before binding or face OS Error: "(99) Cannot assign requested address"... */
815 if ((flags
& COMM_TRANSPARENT
)) {
816 comm_set_transparent(new_socket
);
819 if ( (flags
& COMM_DOBIND
) || addr
.GetPort() > 0 || !addr
.IsAnyAddr() ) {
820 if ( !(flags
& COMM_DOBIND
) && addr
.IsAnyAddr() )
821 debugs(5,1,"WARNING: Squid is attempting to bind() port " << addr
<< " without being a listener.");
822 if ( addr
.IsNoAddr() )
823 debugs(5,0,"CRITICAL: Squid is attempting to bind() port " << addr
<< "!!");
825 if (commBind(new_socket
, *AI
) != COMM_OK
) {
826 comm_close(new_socket
);
831 if (flags
& COMM_NONBLOCKING
)
832 if (commSetNonBlocking(new_socket
) == COMM_ERROR
) {
833 comm_close(new_socket
);
838 if (sock_type
== SOCK_STREAM
)
839 commSetTcpNoDelay(new_socket
);
843 if (Config
.tcpRcvBufsz
> 0 && sock_type
== SOCK_STREAM
)
844 commSetTcpRcvbuf(new_socket
, Config
.tcpRcvBufsz
);
850 comm_import_opened(int fd
,
856 debugs(5, 2, HERE
<< " FD " << fd
<< " at " << addr
);
860 comm_init_opened(fd
, addr
, 0, note
, AI
);
862 if (!(flags
& COMM_NOCLOEXEC
))
863 fd_table
[fd
].flags
.close_on_exec
= 1;
865 if (addr
.GetPort() > (u_short
) 0) {
867 if (sock_type
!= SOCK_DGRAM
)
869 fd_table
[fd
].flags
.nolinger
= 1;
872 if ((flags
& COMM_TRANSPARENT
))
873 fd_table
[fd
].flags
.transparent
= 1;
875 if (flags
& COMM_NONBLOCKING
)
876 fd_table
[fd
].flags
.nonblocking
= 1;
879 if (AI
->ai_socktype
== SOCK_STREAM
)
880 fd_table
[fd
].flags
.nodelay
= 1;
883 /* no fd_table[fd].flags. updates needed for these conditions:
884 * if ((flags & COMM_REUSEADDR)) ...
885 * if ((flags & COMM_DOBIND) ...) ...
890 CBDATA_CLASS_INIT(ConnectStateData
);
893 ConnectStateData::operator new (size_t size
)
895 CBDATA_INIT_TYPE(ConnectStateData
);
896 return cbdataAlloc(ConnectStateData
);
900 ConnectStateData::operator delete (void *address
)
908 commConnectStart(int fd
, const char *host
, u_short port
, AsyncCall::Pointer
&cb
)
910 debugs(cb
->debugSection
, cb
->debugLevel
, "commConnectStart: FD " << fd
<<
911 ", cb " << cb
<< ", " << host
<< ":" << port
); // TODO: just print *cb
913 ConnectStateData
*cs
;
914 cs
= new ConnectStateData
;
916 cs
->host
= xstrdup(host
);
917 cs
->default_port
= port
;
920 comm_add_close_handler(fd
, commConnectFree
, cs
);
921 ipcache_nbgethostbyname(host
, commConnectDnsHandle
, cs
);
924 // TODO: Remove this and similar callback registration functions by replacing
925 // (callback,data) parameters with an AsyncCall so that we do not have to use
926 // a generic call name and debug level when creating an AsyncCall. This will
927 // also cut the number of callback registration routines in half.
929 commConnectStart(int fd
, const char *host
, u_short port
, CNCB
* callback
, void *data
)
931 debugs(5, 5, "commConnectStart: FD " << fd
<< ", data " << data
<< ", " << host
<< ":" << port
);
932 AsyncCall::Pointer call
= commCbCall(5,3,
933 "SomeCommConnectHandler", CommConnectCbPtrFun(callback
, data
));
934 commConnectStart(fd
, host
, port
, call
);
938 commConnectDnsHandle(const ipcache_addrs
*ia
, const DnsLookupDetails
&details
, void *data
)
940 ConnectStateData
*cs
= (ConnectStateData
*)data
;
944 debugs(5, 3, "commConnectDnsHandle: Unknown host: " << cs
->host
);
945 cs
->callCallback(COMM_ERR_DNS
, 0);
949 assert(ia
->cur
< ia
->count
);
951 cs
->default_addr
= ia
->in_addrs
[ia
->cur
];
953 if (Config
.onoff
.balance_on_multiple_ip
)
954 ipcacheCycleAddr(cs
->host
, NULL
);
956 cs
->addrcount
= ia
->count
;
958 cs
->connstart
= squid_curtime
;
964 ConnectStateData::callCallback(comm_err_t status
, int xerrno
)
966 debugs(5, 3, "commConnectCallback: FD " << fd
);
968 comm_remove_close_handler(fd
, commConnectFree
, this);
969 commSetTimeout(fd
, -1, NULL
, NULL
);
971 typedef CommConnectCbParams Params
;
972 Params
¶ms
= GetCommParams
<Params
>(callback
);
975 params
.flag
= status
;
976 params
.xerrno
= xerrno
;
977 ScheduleCallHere(callback
);
980 commConnectFree(fd
, this);
984 commConnectFree(int fd
, void *data
)
986 ConnectStateData
*cs
= (ConnectStateData
*)data
;
987 debugs(5, 3, "commConnectFree: FD " << fd
);
988 // delete cs->callback;
995 copyFDFlags(int to
, fde
*F
)
997 if (F
->flags
.close_on_exec
)
998 commSetCloseOnExec(to
);
1000 if (F
->flags
.nonblocking
)
1001 commSetNonBlocking(to
);
1005 if (F
->flags
.nodelay
)
1006 commSetTcpNoDelay(to
);
1010 if (Config
.tcpRcvBufsz
> 0)
1011 commSetTcpRcvbuf(to
, Config
.tcpRcvBufsz
);
1014 /* Reset FD so that we can connect() again */
1016 ConnectStateData::commResetFD()
1019 // XXX: do we have to check this?
1021 // if (!cbdataReferenceValid(callback.data))
1024 statCounter
.syscalls
.sock
.sockets
++;
1026 fde
*F
= &fd_table
[fd
];
1028 struct addrinfo
*AI
= NULL
;
1029 F
->local_addr
.GetAddrInfo(AI
);
1030 int new_family
= AI
->ai_family
;
1032 int fd2
= socket(new_family
, AI
->ai_socktype
, AI
->ai_protocol
);
1035 debugs(5, DBG_CRITICAL
, HERE
<< "WARNING: FD " << fd2
<< " socket failed to allocate: " << xstrerror());
1037 if (ENFILE
== errno
|| EMFILE
== errno
)
1040 F
->local_addr
.FreeAddrInfo(AI
);
1044 #ifdef _SQUID_MSWIN_
1046 /* On Windows dup2() can't work correctly on Sockets, the */
1047 /* workaround is to close the destination Socket before call them. */
1052 if (dup2(fd2
, fd
) < 0) {
1053 debugs(5, DBG_CRITICAL
, HERE
<< "WARNING: dup2(FD " << fd2
<< ", FD " << fd
<< ") failed: " << xstrerror());
1055 if (ENFILE
== errno
|| EMFILE
== errno
)
1060 F
->local_addr
.FreeAddrInfo(AI
);
1063 commResetSelect(fd
);
1067 debugs(50, 3, "commResetFD: Reset socket FD " << fd
<< "->" << fd2
<< " : family=" << new_family
);
1069 /* INET6: copy the new sockets family type to the FDE table */
1070 F
->sock_family
= new_family
;
1072 F
->flags
.called_connect
= 0;
1075 * yuck, this has assumptions about comm_open() arguments for
1076 * the original socket
1079 /* MUST be done before binding or face OS Error: "(99) Cannot assign requested address"... */
1080 if ( F
->flags
.transparent
) {
1081 comm_set_transparent(fd
);
1084 if (commBind(fd
, *AI
) != COMM_OK
) {
1085 debugs(5, DBG_CRITICAL
, "WARNING: Reset of FD " << fd
<< " for " << F
->local_addr
<< " failed to bind: " << xstrerror());
1086 F
->local_addr
.FreeAddrInfo(AI
);
1089 F
->local_addr
.FreeAddrInfo(AI
);
1092 comm_set_tos(fd
, F
->tos
);
1094 if ( Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& F
->local_addr
.IsIPv6() )
1095 comm_set_v6only(fd
, 1);
1103 ConnectStateData::commRetryConnect()
1105 assert(addrcount
> 0);
1107 if (addrcount
== 1) {
1108 if (tries
>= Config
.retry
.maxtries
)
1111 if (squid_curtime
- connstart
> Config
.Timeout
.connect
)
1114 if (tries
> addrcount
) {
1115 /* Flush bad address count in case we are
1116 * skipping over incompatible protocol
1118 ipcacheMarkAllGood(host
);
1123 return commResetFD();
1127 commReconnect(void *data
)
1129 ConnectStateData
*cs
= (ConnectStateData
*)data
;
1130 ipcache_nbgethostbyname(cs
->host
, commConnectDnsHandle
, cs
);
1133 /** Connect SOCK to specified DEST_PORT at DEST_HOST. */
1135 ConnectStateData::Connect(int fd
, void *me
)
1137 ConnectStateData
*cs
= (ConnectStateData
*)me
;
1138 assert (cs
->fd
== fd
);
1143 ConnectStateData::defaults()
1146 S
.SetPort(default_port
);
1150 ConnectStateData::connect()
1154 debugs(5,5, HERE
<< "to " << S
);
1156 switch (comm_connect_addr(fd
, S
) ) {
1158 case COMM_INPROGRESS
:
1159 debugs(5, 5, HERE
<< "FD " << fd
<< ": COMM_INPROGRESS");
1160 commSetSelect(fd
, COMM_SELECT_WRITE
, ConnectStateData::Connect
, this, 0);
1164 debugs(5, 5, HERE
<< "FD " << fd
<< ": COMM_OK - connected");
1165 ipcacheMarkGoodAddr(host
, S
);
1166 callCallback(COMM_OK
, 0);
1169 case COMM_ERR_PROTOCOL
:
1170 debugs(5, 5, HERE
"FD " << fd
<< ": COMM_ERR_PROTOCOL - try again");
1171 /* problem using the desired protocol over this socket.
1172 * skip to the next address and hope it's more compatible
1173 * but do not mark the current address as bad
1176 if (commRetryConnect()) {
1177 /* Force an addr cycle to move forward to the next possible address */
1178 ipcacheCycleAddr(host
, NULL
);
1179 eventAdd("commReconnect", commReconnect
, this, this->addrcount
== 1 ? 0.05 : 0.0, 0);
1181 debugs(5, 5, HERE
<< "FD " << fd
<< ": COMM_ERR_PROTOCOL - ERR tried too many times already.");
1182 callCallback(COMM_ERR_CONNECT
, errno
);
1187 debugs(5, 5, HERE
"FD " << fd
<< ": * - try again");
1189 ipcacheMarkBadAddr(host
, S
);
1192 if (Config
.onoff
.test_reachability
)
1193 netdbDeleteAddrNetwork(S
);
1196 if (commRetryConnect()) {
1197 eventAdd("commReconnect", commReconnect
, this, this->addrcount
== 1 ? 0.05 : 0.0, 0);
1199 debugs(5, 5, HERE
<< "FD " << fd
<< ": * - ERR tried too many times already.");
1200 callCallback(COMM_ERR_CONNECT
, errno
);
1206 commSetTimeout_old(int fd, int timeout, PF * handler, void *data)
1208 debugs(5, 3, HERE << "FD " << fd << " timeout " << timeout);
1210 assert(fd < Squid_MaxFD);
1211 fde *F = &fd_table[fd];
1212 assert(F->flags.open);
1215 cbdataReferenceDone(F->timeout_data);
1216 F->timeout_handler = NULL;
1220 cbdataReferenceDone(F->timeout_data);
1221 F->timeout_handler = handler;
1222 F->timeout_data = cbdataReference(data);
1225 F->timeout = squid_curtime + (time_t) timeout;
1233 commSetTimeout(int fd
, int timeout
, PF
* handler
, void *data
)
1235 AsyncCall::Pointer call
;
1236 debugs(5, 3, HERE
<< "FD " << fd
<< " timeout " << timeout
);
1237 if (handler
!= NULL
)
1238 call
=commCbCall(5,4, "SomeTimeoutHandler", CommTimeoutCbPtrFun(handler
, data
));
1241 return commSetTimeout(fd
, timeout
, call
);
1245 int commSetTimeout(int fd
, int timeout
, AsyncCall::Pointer
&callback
)
1247 debugs(5, 3, HERE
<< "FD " << fd
<< " timeout " << timeout
);
1249 assert(fd
< Squid_MaxFD
);
1250 fde
*F
= &fd_table
[fd
];
1251 assert(F
->flags
.open
);
1254 F
->timeoutHandler
= NULL
;
1257 if (callback
!= NULL
) {
1258 typedef CommTimeoutCbParams Params
;
1259 Params
¶ms
= GetCommParams
<Params
>(callback
);
1261 F
->timeoutHandler
= callback
;
1264 F
->timeout
= squid_curtime
+ (time_t) timeout
;
1272 comm_connect_addr(int sock
, const Ip::Address
&address
)
1274 comm_err_t status
= COMM_OK
;
1275 fde
*F
= &fd_table
[sock
];
1279 struct addrinfo
*AI
= NULL
;
1280 PROF_start(comm_connect_addr
);
1282 assert(address
.GetPort() != 0);
1284 debugs(5, 9, HERE
<< "connecting socket FD " << sock
<< " to " << address
<< " (want family: " << F
->sock_family
<< ")");
1286 /* Handle IPv6 over IPv4-only socket case.
1287 * this case must presently be handled here since the GetAddrInfo asserts on bad mappings.
1288 * NP: because commResetFD is private to ConnStateData we have to return an error and
1289 * trust its handled properly.
1291 if (F
->sock_family
== AF_INET
&& !address
.IsIPv4()) {
1292 errno
= ENETUNREACH
;
1293 return COMM_ERR_PROTOCOL
;
1296 /* Handle IPv4 over IPv6-only socket case.
1297 * This case is presently handled here as it's both a known case and it's
1298 * uncertain what error will be returned by the IPv6 stack in such case. It's
1299 * possible this will also be handled by the errno checks below after connect()
1300 * but needs carefull cross-platform verification, and verifying the address
1301 * condition here is simple.
1303 if (!F
->local_addr
.IsIPv4() && address
.IsIPv4()) {
1304 errno
= ENETUNREACH
;
1305 return COMM_ERR_PROTOCOL
;
1308 address
.GetAddrInfo(AI
, F
->sock_family
);
1310 /* Establish connection. */
1313 if (!F
->flags
.called_connect
) {
1314 F
->flags
.called_connect
= 1;
1315 statCounter
.syscalls
.sock
.connects
++;
1317 x
= connect(sock
, AI
->ai_addr
, AI
->ai_addrlen
);
1319 // XXX: ICAP code refuses callbacks during a pending comm_ call
1320 // Async calls development will fix this.
1323 errno
= EINPROGRESS
;
1327 debugs(5,5, "comm_connect_addr: sock=" << sock
<< ", addrinfo( " <<
1328 " flags=" << AI
->ai_flags
<<
1329 ", family=" << AI
->ai_family
<<
1330 ", socktype=" << AI
->ai_socktype
<<
1331 ", protocol=" << AI
->ai_protocol
<<
1332 ", &addr=" << AI
->ai_addr
<<
1333 ", addrlen=" << AI
->ai_addrlen
<<
1335 debugs(5, 9, "connect FD " << sock
<< ": (" << x
<< ") " << xstrerror());
1336 debugs(14,9, "connecting to: " << address
);
1339 #if defined(_SQUID_NEWSOS6_)
1340 /* Makoto MATSUSHITA <matusita@ics.es.osaka-u.ac.jp> */
1342 connect(sock
, AI
->ai_addr
, AI
->ai_addrlen
);
1344 if (errno
== EINVAL
) {
1345 errlen
= sizeof(err
);
1346 x
= getsockopt(sock
, SOL_SOCKET
, SO_ERROR
, &err
, &errlen
);
1353 errlen
= sizeof(err
);
1355 x
= getsockopt(sock
, SOL_SOCKET
, SO_ERROR
, &err
, &errlen
);
1360 #if defined(_SQUID_SOLARIS_)
1362 * Solaris 2.4's socket emulation doesn't allow you
1363 * to determine the error from a failed non-blocking
1364 * connect and just returns EPIPE. Create a fake
1365 * error message for connect. -- fenner@parc.xerox.com
1367 if (x
< 0 && errno
== EPIPE
)
1375 /* Squid seems to be working fine without this code. With this code,
1376 * we leak memory on many connect requests because of EINPROGRESS.
1377 * If you find that this code is needed, please file a bug report. */
1379 #ifdef _SQUID_LINUX_
1381 * Linux Debian replaces our allocated AI pointer with garbage when
1382 * connect() fails. This leads to segmentation faults deallocating
1383 * the system-allocated memory when we go to clean up our pointer.
1384 * HACK: is to leak the memory returned since we can't deallocate.
1392 address
.FreeAddrInfo(AI
);
1394 PROF_stop(comm_connect_addr
);
1396 if (errno
== 0 || errno
== EISCONN
)
1398 else if (ignoreErrno(errno
))
1399 status
= COMM_INPROGRESS
;
1400 else if (errno
== EAFNOSUPPORT
|| errno
== EINVAL
)
1401 return COMM_ERR_PROTOCOL
;
1405 address
.NtoA(F
->ipaddr
, MAX_IPSTRLEN
);
1407 F
->remote_port
= address
.GetPort(); /* remote_port is HS */
1409 if (status
== COMM_OK
) {
1410 debugs(5, 10, "comm_connect_addr: FD " << sock
<< " connected to " << address
);
1411 } else if (status
== COMM_INPROGRESS
) {
1412 debugs(5, 10, "comm_connect_addr: FD " << sock
<< " connection pending");
1419 commCallCloseHandlers(int fd
)
1421 fde
*F
= &fd_table
[fd
];
1422 debugs(5, 5, "commCallCloseHandlers: FD " << fd
);
1424 while (F
->closeHandler
!= NULL
) {
1425 AsyncCall::Pointer call
= F
->closeHandler
;
1426 F
->closeHandler
= call
->Next();
1427 call
->setNext(NULL
);
1428 // If call is not canceled schedule it for execution else ignore it
1429 if (!call
->canceled()) {
1430 debugs(5, 5, "commCallCloseHandlers: ch->handler=" << call
);
1431 typedef CommCloseCbParams Params
;
1432 Params
¶ms
= GetCommParams
<Params
>(call
);
1434 ScheduleCallHere(call
);
1441 commLingerClose(int fd
, void *unused
)
1443 LOCAL_ARRAY(char, buf
, 1024);
1445 n
= FD_READ_METHOD(fd
, buf
, 1024);
1448 debugs(5, 3, "commLingerClose: FD " << fd
<< " read: " << xstrerror());
1454 commLingerTimeout(int fd
, void *unused
)
1456 debugs(5, 3, "commLingerTimeout: FD " << fd
);
1461 * Inspired by apache
1464 comm_lingering_close(int fd
)
1468 if (fd_table
[fd
].ssl
)
1469 ssl_shutdown_method(fd
);
1473 if (shutdown(fd
, 1) < 0) {
1478 fd_note(fd
, "lingering close");
1479 commSetTimeout(fd
, 10, commLingerTimeout
, NULL
);
1480 commSetSelect(fd
, COMM_SELECT_READ
, commLingerClose
, NULL
, 0);
1486 * enable linger with time of 0 so that when the socket is
1487 * closed, TCP generates a RESET
1490 comm_reset_close(int fd
)
1497 if (setsockopt(fd
, SOL_SOCKET
, SO_LINGER
, (char *) &L
, sizeof(L
)) < 0)
1498 debugs(50, DBG_CRITICAL
, "ERROR: Closing FD " << fd
<< " with TCP RST: " << xstrerror());
1504 comm_close_start(int fd
, void *data
)
1507 fde
*F
= &fd_table
[fd
];
1509 ssl_shutdown_method(fd
);
1516 comm_close_complete(int fd
, void *data
)
1519 fde
*F
= &fd_table
[fd
];
1527 fd_close(fd
); /* update fdstat */
1531 statCounter
.syscalls
.sock
.closes
++;
1533 /* When an fd closes, give accept() a chance, if need be */
1534 Comm::AcceptLimiter::Instance().kick();
1538 * Close the socket fd.
1540 * + call write handlers with ERR_CLOSING
1541 * + call read handlers with ERR_CLOSING
1542 * + call closing handlers
1544 * NOTE: COMM_ERR_CLOSING will NOT be called for CommReads' sitting in a
1545 * DeferredReadManager.
1548 _comm_close(int fd
, char const *file
, int line
)
1550 debugs(5, 3, "comm_close: start closing FD " << fd
);
1552 assert(fd
< Squid_MaxFD
);
1554 fde
*F
= &fd_table
[fd
];
1555 fdd_table
[fd
].close_file
= file
;
1556 fdd_table
[fd
].close_line
= line
;
1561 /* XXX: is this obsolete behind F->closing() ? */
1562 if ( (shutting_down
|| reconfiguring
) && (!F
->flags
.open
|| F
->type
== FD_FILE
))
1565 /* The following fails because ipc.c is doing calls to pipe() to create sockets! */
1568 assert(F
->type
!= FD_FILE
);
1570 PROF_start(comm_close
);
1572 F
->flags
.close_request
= 1;
1574 AsyncCall::Pointer startCall
=commCbCall(5,4, "comm_close_start",
1575 CommCloseCbPtrFun(comm_close_start
, NULL
));
1576 typedef CommCloseCbParams Params
;
1577 Params
&startParams
= GetCommParams
<Params
>(startCall
);
1578 startParams
.fd
= fd
;
1579 ScheduleCallHere(startCall
);
1581 // a half-closed fd may lack a reader, so we stop monitoring explicitly
1582 if (commHasHalfClosedMonitor(fd
))
1583 commStopHalfClosedMonitor(fd
);
1584 commSetTimeout(fd
, -1, NULL
, NULL
);
1586 // notify read/write handlers after canceling select reservations, if any
1587 if (commio_has_callback(fd
, IOCB_WRITE
, COMMIO_FD_WRITECB(fd
))) {
1588 commSetSelect(fd
, COMM_SELECT_WRITE
, NULL
, NULL
, 0);
1589 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), COMM_ERR_CLOSING
, errno
);
1591 if (commio_has_callback(fd
, IOCB_READ
, COMMIO_FD_READCB(fd
))) {
1592 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
1593 commio_finish_callback(fd
, COMMIO_FD_READCB(fd
), COMM_ERR_CLOSING
, errno
);
1596 commCallCloseHandlers(fd
);
1599 F
->pconn
.pool
->count(F
->pconn
.uses
);
1601 comm_empty_os_read_buffers(fd
);
1604 AsyncCall::Pointer completeCall
=commCbCall(5,4, "comm_close_complete",
1605 CommCloseCbPtrFun(comm_close_complete
, NULL
));
1606 Params
&completeParams
= GetCommParams
<Params
>(completeCall
);
1607 completeParams
.fd
= fd
;
1608 // must use async call to wait for all callbacks
1609 // scheduled before comm_close() to finish
1610 ScheduleCallHere(completeCall
);
1612 PROF_stop(comm_close
);
1615 /* Send a udp datagram to specified TO_ADDR. */
1617 comm_udp_sendto(int fd
,
1618 const Ip::Address
&to_addr
,
1623 struct addrinfo
*AI
= NULL
;
1625 PROF_start(comm_udp_sendto
);
1626 statCounter
.syscalls
.sock
.sendtos
++;
1628 debugs(50, 3, "comm_udp_sendto: Attempt to send UDP packet to " << to_addr
<<
1629 " using FD " << fd
<< " using Port " << comm_local_port(fd
) );
1631 /* BUG: something in the above macro appears to occasionally be setting AI to garbage. */
1632 /* AYJ: 2007-08-27 : or was it because I wasn't then setting 'fd_table[fd].sock_family' to fill properly. */
1633 assert( NULL
== AI
);
1635 to_addr
.GetAddrInfo(AI
, fd_table
[fd
].sock_family
);
1637 x
= sendto(fd
, buf
, len
, 0, AI
->ai_addr
, AI
->ai_addrlen
);
1639 to_addr
.FreeAddrInfo(AI
);
1641 PROF_stop(comm_udp_sendto
);
1646 #ifdef _SQUID_LINUX_
1648 if (ECONNREFUSED
!= errno
)
1651 debugs(50, 1, "comm_udp_sendto: FD " << fd
<< ", (family=" << fd_table
[fd
].sock_family
<< ") " << to_addr
<< ": " << xstrerror());
1657 comm_add_close_handler(int fd
, PF
* handler
, void *data
)
1659 debugs(5, 5, "comm_add_close_handler: FD " << fd
<< ", handler=" <<
1660 handler
<< ", data=" << data
);
1662 AsyncCall::Pointer call
=commCbCall(5,4, "SomeCloseHandler",
1663 CommCloseCbPtrFun(handler
, data
));
1664 comm_add_close_handler(fd
, call
);
1668 comm_add_close_handler(int fd
, AsyncCall::Pointer
&call
)
1670 debugs(5, 5, "comm_add_close_handler: FD " << fd
<< ", AsyncCall=" << call
);
1672 /*TODO:Check for a similar scheduled AsyncCall*/
1673 // for (c = fd_table[fd].closeHandler; c; c = c->next)
1674 // assert(c->handler != handler || c->data != data);
1676 call
->setNext(fd_table
[fd
].closeHandler
);
1678 fd_table
[fd
].closeHandler
= call
;
1682 // remove function-based close handler
1684 comm_remove_close_handler(int fd
, PF
* handler
, void *data
)
1686 assert (isOpen(fd
));
1687 /* Find handler in list */
1688 debugs(5, 5, "comm_remove_close_handler: FD " << fd
<< ", handler=" <<
1689 handler
<< ", data=" << data
);
1691 AsyncCall::Pointer p
;
1692 for (p
= fd_table
[fd
].closeHandler
; p
!= NULL
; p
= p
->Next()) {
1693 typedef CommCbFunPtrCallT
<CommCloseCbPtrFun
> Call
;
1694 const Call
*call
= dynamic_cast<const Call
*>(p
.getRaw());
1695 if (!call
) // method callbacks have their own comm_remove_close_handler
1698 typedef CommCloseCbParams Params
;
1699 const Params
¶ms
= GetCommParams
<Params
>(p
);
1700 if (call
->dialer
.handler
== handler
&& params
.data
== data
)
1701 break; /* This is our handler */
1704 // comm_close removes all close handlers so our handler may be gone
1706 p
->cancel("comm_remove_close_handler");
1707 // TODO: should we remove the handler from the close handlers list?
1710 // remove method-based close handler
1712 comm_remove_close_handler(int fd
, AsyncCall::Pointer
&call
)
1714 assert (isOpen(fd
));
1715 debugs(5, 5, "comm_remove_close_handler: FD " << fd
<< ", AsyncCall=" << call
);
1717 // comm_close removes all close handlers so our handler may be gone
1718 // TODO: should we remove the handler from the close handlers list?
1720 // Check to see if really exist the given AsyncCall in comm_close handlers
1721 // TODO: optimize: this slow code is only needed for the assert() below
1722 AsyncCall::Pointer p
;
1723 for (p
= fd_table
[fd
].closeHandler
; p
!= NULL
&& p
!= call
; p
= p
->Next());
1727 call
->cancel("comm_remove_close_handler");
1731 commSetNoLinger(int fd
)
1735 L
.l_onoff
= 0; /* off */
1738 if (setsockopt(fd
, SOL_SOCKET
, SO_LINGER
, (char *) &L
, sizeof(L
)) < 0)
1739 debugs(50, 0, "commSetNoLinger: FD " << fd
<< ": " << xstrerror());
1741 fd_table
[fd
].flags
.nolinger
= 1;
1745 commSetReuseAddr(int fd
)
1749 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &on
, sizeof(on
)) < 0)
1750 debugs(50, 1, "commSetReuseAddr: FD " << fd
<< ": " << xstrerror());
1754 commSetTcpRcvbuf(int fd
, int size
)
1756 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, (char *) &size
, sizeof(size
)) < 0)
1757 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd
<< ", SIZE " << size
<< ": " << xstrerror());
1758 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, (char *) &size
, sizeof(size
)) < 0)
1759 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd
<< ", SIZE " << size
<< ": " << xstrerror());
1760 #ifdef TCP_WINDOW_CLAMP
1761 if (setsockopt(fd
, SOL_TCP
, TCP_WINDOW_CLAMP
, (char *) &size
, sizeof(size
)) < 0)
1762 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd
<< ", SIZE " << size
<< ": " << xstrerror());
1767 commSetNonBlocking(int fd
)
1769 #ifndef _SQUID_MSWIN_
1773 #ifdef _SQUID_WIN32_
1775 int nonblocking
= TRUE
;
1777 #ifdef _SQUID_CYGWIN_
1779 if (fd_table
[fd
].type
!= FD_PIPE
) {
1782 if (ioctl(fd
, FIONBIO
, &nonblocking
) < 0) {
1783 debugs(50, 0, "commSetNonBlocking: FD " << fd
<< ": " << xstrerror() << " " << fd_table
[fd
].type
);
1787 #ifdef _SQUID_CYGWIN_
1792 #ifndef _SQUID_MSWIN_
1794 if ((flags
= fcntl(fd
, F_GETFL
, dummy
)) < 0) {
1795 debugs(50, 0, "FD " << fd
<< ": fcntl F_GETFL: " << xstrerror());
1799 if (fcntl(fd
, F_SETFL
, flags
| SQUID_NONBLOCK
) < 0) {
1800 debugs(50, 0, "commSetNonBlocking: FD " << fd
<< ": " << xstrerror());
1805 #ifdef _SQUID_CYGWIN_
1810 fd_table
[fd
].flags
.nonblocking
= 1;
1816 commUnsetNonBlocking(int fd
)
1818 #ifdef _SQUID_MSWIN_
1819 int nonblocking
= FALSE
;
1821 if (ioctlsocket(fd
, FIONBIO
, (unsigned long *) &nonblocking
) < 0) {
1826 if ((flags
= fcntl(fd
, F_GETFL
, dummy
)) < 0) {
1827 debugs(50, 0, "FD " << fd
<< ": fcntl F_GETFL: " << xstrerror());
1831 if (fcntl(fd
, F_SETFL
, flags
& (~SQUID_NONBLOCK
)) < 0) {
1833 debugs(50, 0, "commUnsetNonBlocking: FD " << fd
<< ": " << xstrerror());
1837 fd_table
[fd
].flags
.nonblocking
= 0;
1842 commSetCloseOnExec(int fd
)
1848 if ((flags
= fcntl(fd
, F_GETFD
, dummy
)) < 0) {
1849 debugs(50, 0, "FD " << fd
<< ": fcntl F_GETFD: " << xstrerror());
1853 if (fcntl(fd
, F_SETFD
, flags
| FD_CLOEXEC
) < 0)
1854 debugs(50, 0, "FD " << fd
<< ": set close-on-exec failed: " << xstrerror());
1856 fd_table
[fd
].flags
.close_on_exec
= 1;
1863 commSetTcpNoDelay(int fd
)
1867 if (setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *) &on
, sizeof(on
)) < 0)
1868 debugs(50, 1, "commSetTcpNoDelay: FD " << fd
<< ": " << xstrerror());
1870 fd_table
[fd
].flags
.nodelay
= 1;
1876 commSetTcpKeepalive(int fd
, int idle
, int interval
, int timeout
)
1880 if (timeout
&& interval
) {
1881 int count
= (timeout
+ interval
- 1) / interval
;
1882 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPCNT
, &count
, sizeof(on
)) < 0)
1883 debugs(5, 1, "commSetKeepalive: FD " << fd
<< ": " << xstrerror());
1888 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPIDLE
, &idle
, sizeof(on
)) < 0)
1889 debugs(5, 1, "commSetKeepalive: FD " << fd
<< ": " << xstrerror());
1892 #ifdef TCP_KEEPINTVL
1894 if (setsockopt(fd
, IPPROTO_TCP
, TCP_KEEPINTVL
, &interval
, sizeof(on
)) < 0)
1895 debugs(5, 1, "commSetKeepalive: FD " << fd
<< ": " << xstrerror());
1898 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &on
, sizeof(on
)) < 0)
1899 debugs(5, 1, "commSetKeepalive: FD " << fd
<< ": " << xstrerror());
1905 fd_table
=(fde
*) xcalloc(Squid_MaxFD
, sizeof(fde
));
1906 fdd_table
= (fd_debug_t
*)xcalloc(Squid_MaxFD
, sizeof(fd_debug_t
));
1908 /* make sure the accept() socket FIFO delay queue exists */
1909 Comm::AcceptLimiter::Instance();
1911 commfd_table
= (comm_fd_t
*) xcalloc(Squid_MaxFD
, sizeof(comm_fd_t
));
1912 for (int pos
= 0; pos
< Squid_MaxFD
; pos
++) {
1913 commfd_table
[pos
].fd
= pos
;
1914 commfd_table
[pos
].readcb
.fd
= pos
;
1915 commfd_table
[pos
].readcb
.type
= IOCB_READ
;
1916 commfd_table
[pos
].writecb
.fd
= pos
;
1917 commfd_table
[pos
].writecb
.type
= IOCB_WRITE
;
1920 /* XXX account fd_table */
1921 /* Keep a few file descriptors free so that we don't run out of FD's
1922 * after accepting a client but before it opens a socket or a file.
1923 * Since Squid_MaxFD can be as high as several thousand, don't waste them */
1924 RESERVED_FD
= min(100, Squid_MaxFD
/ 4);
1926 conn_close_pool
= memPoolCreate("close_handler", sizeof(close_handler
));
1928 TheHalfClosed
= new DescriptorSet
;
1934 delete TheHalfClosed
;
1935 TheHalfClosed
= NULL
;
1937 safe_free(fd_table
);
1938 safe_free(fdd_table
);
1939 safe_free(commfd_table
);
1944 commHandleWrite(int fd
, void *data
)
1946 comm_io_callback_t
*state
= (comm_io_callback_t
*)data
;
1950 assert(state
== COMMIO_FD_WRITECB(fd
));
1952 PROF_start(commHandleWrite
);
1953 debugs(5, 5, "commHandleWrite: FD " << fd
<< ": off " <<
1954 (long int) state
->offset
<< ", sz " << (long int) state
->size
<< ".");
1956 nleft
= state
->size
- state
->offset
;
1957 len
= FD_WRITE_METHOD(fd
, state
->buf
+ state
->offset
, nleft
);
1958 debugs(5, 5, "commHandleWrite: write() returns " << len
);
1959 fd_bytes(fd
, len
, FD_WRITE
);
1960 statCounter
.syscalls
.sock
.writes
++;
1961 // After each successful partial write,
1962 // reset fde::writeStart to the current time.
1963 fd_table
[fd
].writeStart
= squid_curtime
;
1966 /* Note we even call write if nleft == 0 */
1970 debugs(5, 1, "commHandleWrite: FD " << fd
<< ": write failure: connection closed with " << nleft
<< " bytes remaining.");
1972 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), nleft
? COMM_ERROR
: COMM_OK
, errno
);
1973 } else if (len
< 0) {
1976 if (fd_table
[fd
].flags
.socket_eof
) {
1977 debugs(50, 2, "commHandleWrite: FD " << fd
<< ": write failure: " << xstrerror() << ".");
1978 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), nleft
? COMM_ERROR
: COMM_OK
, errno
);
1979 } else if (ignoreErrno(errno
)) {
1980 debugs(50, 10, "commHandleWrite: FD " << fd
<< ": write failure: " << xstrerror() << ".");
1987 debugs(50, 2, "commHandleWrite: FD " << fd
<< ": write failure: " << xstrerror() << ".");
1988 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), nleft
? COMM_ERROR
: COMM_OK
, errno
);
1991 /* A successful write, continue */
1992 state
->offset
+= len
;
1994 if (state
->offset
< state
->size
) {
1995 /* Not done, reinstall the write handler and write some more */
2002 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), nleft
? COMM_OK
: COMM_ERROR
, errno
);
2006 PROF_stop(commHandleWrite
);
2010 * Queue a write. handler/handler_data are called when the write
2011 * completes, on error, or on file descriptor close.
2013 * free_func is used to free the passed buffer when the write has completed.
2016 comm_write(int fd
, const char *buf
, int size
, IOCB
* handler
, void *handler_data
, FREE
* free_func
)
2018 AsyncCall::Pointer call
= commCbCall(5,5, "SomeCommWriteHander",
2019 CommIoCbPtrFun(handler
, handler_data
));
2021 comm_write(fd
, buf
, size
, call
, free_func
);
2025 comm_write(int fd
, const char *buf
, int size
, AsyncCall::Pointer
&callback
, FREE
* free_func
)
2027 debugs(5, 5, "comm_write: FD " << fd
<< ": sz " << size
<< ": asynCall " << callback
);
2029 /* Make sure we are open, not closing, and not writing */
2031 assert(!fd_table
[fd
].closing());
2032 comm_io_callback_t
*ccb
= COMMIO_FD_WRITECB(fd
);
2033 assert(!ccb
->active());
2035 fd_table
[fd
].writeStart
= squid_curtime
;
2036 /* Queue the write */
2037 commio_set_callback(fd
, IOCB_WRITE
, ccb
, callback
,
2038 (char *)buf
, free_func
, size
);
2039 commSetSelect(fd
, COMM_SELECT_WRITE
, commHandleWrite
, ccb
, 0);
2043 /* a wrapper around comm_write to allow for MemBuf to be comm_written in a snap */
2045 comm_write_mbuf(int fd
, MemBuf
*mb
, IOCB
* handler
, void *handler_data
)
2047 comm_write(fd
, mb
->buf
, mb
->size
, handler
, handler_data
, mb
->freeFunc());
2051 comm_write_mbuf(int fd
, MemBuf
*mb
, AsyncCall::Pointer
&callback
)
2053 comm_write(fd
, mb
->buf
, mb
->size
, callback
, mb
->freeFunc());
2058 * hm, this might be too general-purpose for all the places we'd
2062 ignoreErrno(int ierrno
)
2069 #if EAGAIN != EWOULDBLOCK
2092 commCloseAllSockets(void)
2097 for (fd
= 0; fd
<= Biggest_FD
; fd
++) {
2103 if (F
->type
!= FD_SOCKET
)
2106 if (F
->flags
.ipc
) /* don't close inter-process sockets */
2109 if (F
->timeoutHandler
!= NULL
) {
2110 AsyncCall::Pointer callback
= F
->timeoutHandler
;
2111 F
->timeoutHandler
= NULL
;
2112 debugs(5, 5, "commCloseAllSockets: FD " << fd
<< ": Calling timeout handler");
2113 ScheduleCallHere(callback
);
2115 debugs(5, 5, "commCloseAllSockets: FD " << fd
<< ": calling comm_reset_close()");
2116 comm_reset_close(fd
);
2122 AlreadyTimedOut(fde
*F
)
2127 if (F
->timeout
== 0)
2130 if (F
->timeout
> squid_curtime
)
2137 writeTimedOut(int fd
)
2139 if (!commio_has_callback(fd
, IOCB_WRITE
, COMMIO_FD_WRITECB(fd
)))
2142 if ((squid_curtime
- fd_table
[fd
].writeStart
) < Config
.Timeout
.write
)
2153 AsyncCall::Pointer callback
;
2155 for (fd
= 0; fd
<= Biggest_FD
; fd
++) {
2158 if (writeTimedOut(fd
)) {
2159 // We have an active write callback and we are timed out
2160 debugs(5, 5, "checkTimeouts: FD " << fd
<< " auto write timeout");
2161 commSetSelect(fd
, COMM_SELECT_WRITE
, NULL
, NULL
, 0);
2162 commio_finish_callback(fd
, COMMIO_FD_WRITECB(fd
), COMM_ERROR
, ETIMEDOUT
);
2163 } else if (AlreadyTimedOut(F
))
2166 debugs(5, 5, "checkTimeouts: FD " << fd
<< " Expired");
2168 if (F
->timeoutHandler
!= NULL
) {
2169 debugs(5, 5, "checkTimeouts: FD " << fd
<< ": Call timeout handler");
2170 callback
= F
->timeoutHandler
;
2171 F
->timeoutHandler
= NULL
;
2172 ScheduleCallHere(callback
);
2174 debugs(5, 5, "checkTimeouts: FD " << fd
<< ": Forcing comm_close()");
2180 void CommIO::Initialise()
2182 /* Initialize done pipe signal */
2184 if (pipe(DonePipe
)) {}
2185 DoneFD
= DonePipe
[1];
2186 DoneReadFD
= DonePipe
[0];
2187 fd_open(DoneReadFD
, FD_PIPE
, "async-io completetion event: main");
2188 fd_open(DoneFD
, FD_PIPE
, "async-io completetion event: threads");
2189 commSetNonBlocking(DoneReadFD
);
2190 commSetNonBlocking(DoneFD
);
2191 commSetSelect(DoneReadFD
, COMM_SELECT_READ
, NULLFDHandler
, NULL
, 0);
2195 void CommIO::NotifyIOClose()
2197 /* Close done pipe signal */
2202 fd_close(DoneReadFD
);
2203 Initialised
= false;
2206 bool CommIO::Initialised
= false;
2207 bool CommIO::DoneSignalled
= false;
2208 int CommIO::DoneFD
= -1;
2209 int CommIO::DoneReadFD
= -1;
2215 FD_READ_METHOD(DoneReadFD
, buf
, sizeof(buf
));
2219 CommIO::NULLFDHandler(int fd
, void *data
)
2222 commSetSelect(fd
, COMM_SELECT_READ
, NULLFDHandler
, NULL
, 0);
2226 CommIO::ResetNotifications()
2228 if (DoneSignalled
) {
2230 DoneSignalled
= false;
2234 /// Start waiting for a possibly half-closed connection to close
2235 // by scheduling a read callback to a monitoring handler that
2236 // will close the connection on read errors.
2238 commStartHalfClosedMonitor(int fd
)
2240 debugs(5, 5, HERE
<< "adding FD " << fd
<< " to " << *TheHalfClosed
);
2242 assert(!commHasHalfClosedMonitor(fd
));
2243 (void)TheHalfClosed
->add(fd
); // could also assert the result
2244 commPlanHalfClosedCheck(); // may schedule check if we added the first FD
2249 commPlanHalfClosedCheck()
2251 if (!WillCheckHalfClosed
&& !TheHalfClosed
->empty()) {
2252 eventAdd("commHalfClosedCheck", &commHalfClosedCheck
, NULL
, 1.0, 1);
2253 WillCheckHalfClosed
= true;
2257 /// iterates over all descriptors that may need half-closed tests and
2258 /// calls comm_read for those that do; re-schedules the check if needed
2261 commHalfClosedCheck(void *)
2263 debugs(5, 5, HERE
<< "checking " << *TheHalfClosed
);
2265 typedef DescriptorSet::const_iterator DSCI
;
2266 const DSCI end
= TheHalfClosed
->end();
2267 for (DSCI i
= TheHalfClosed
->begin(); i
!= end
; ++i
) {
2269 if (!fd_table
[fd
].halfClosedReader
) { // not reading already
2270 AsyncCall::Pointer call
= commCbCall(5,4, "commHalfClosedReader",
2271 CommIoCbPtrFun(&commHalfClosedReader
, NULL
));
2272 comm_read(fd
, NULL
, 0, call
);
2273 fd_table
[fd
].halfClosedReader
= call
;
2277 WillCheckHalfClosed
= false; // as far as we know
2278 commPlanHalfClosedCheck(); // may need to check again
2281 /// checks whether we are waiting for possibly half-closed connection to close
2282 // We are monitoring if the read handler for the fd is the monitoring handler.
2284 commHasHalfClosedMonitor(int fd
)
2286 return TheHalfClosed
->has(fd
);
2289 /// stop waiting for possibly half-closed connection to close
2291 commStopHalfClosedMonitor(int const fd
)
2293 debugs(5, 5, HERE
<< "removing FD " << fd
<< " from " << *TheHalfClosed
);
2295 // cancel the read if one was scheduled
2296 AsyncCall::Pointer reader
= fd_table
[fd
].halfClosedReader
;
2298 comm_read_cancel(fd
, reader
);
2299 fd_table
[fd
].halfClosedReader
= NULL
;
2301 TheHalfClosed
->del(fd
);
2304 /// I/O handler for the possibly half-closed connection monitoring code
2306 commHalfClosedReader(int fd
, char *, size_t size
, comm_err_t flag
, int, void *)
2308 // there cannot be more data coming in on half-closed connections
2310 assert(commHasHalfClosedMonitor(fd
)); // or we would have canceled the read
2312 fd_table
[fd
].halfClosedReader
= NULL
; // done reading, for now
2314 // nothing to do if fd is being closed
2315 if (flag
== COMM_ERR_CLOSING
)
2318 // if read failed, close the connection
2319 if (flag
!= COMM_OK
) {
2320 debugs(5, 3, "commHalfClosedReader: closing FD " << fd
);
2325 // continue waiting for close or error
2326 commPlanHalfClosedCheck(); // make sure this fd will be checked again
2330 CommRead::CommRead() : fd(-1), buf(NULL
), len(0), callback(NULL
) {}
2332 CommRead::CommRead(int fd_
, char *buf_
, int len_
, AsyncCall::Pointer
&callback_
)
2333 : fd(fd_
), buf(buf_
), len(len_
), callback(callback_
) {}
2335 DeferredRead::DeferredRead () : theReader(NULL
), theContext(NULL
), theRead(), cancelled(false) {}
2337 DeferredRead::DeferredRead (DeferrableRead
*aReader
, void *data
, CommRead
const &aRead
) : theReader(aReader
), theContext (data
), theRead(aRead
), cancelled(false) {}
2339 DeferredReadManager::~DeferredReadManager()
2342 assert (deferredReads
.empty());
2345 /* explicit instantiation required for some systems */
2347 /// \cond AUTODOCS-IGNORE
2348 template cbdata_type CbDataList
<DeferredRead
>::CBDATA_CbDataList
;
2352 DeferredReadManager::delayRead(DeferredRead
const &aRead
)
2354 debugs(5, 3, "Adding deferred read on FD " << aRead
.theRead
.fd
);
2355 CbDataList
<DeferredRead
> *temp
= deferredReads
.push_back(aRead
);
2357 // We have to use a global function as a closer and point to temp
2358 // instead of "this" because DeferredReadManager is not a job and
2359 // is not even cbdata protected
2360 AsyncCall::Pointer closer
= commCbCall(5,4,
2361 "DeferredReadManager::CloseHandler",
2362 CommCloseCbPtrFun(&CloseHandler
, temp
));
2363 comm_add_close_handler(aRead
.theRead
.fd
, closer
);
2364 temp
->element
.closer
= closer
; // remeber so that we can cancel
2368 DeferredReadManager::CloseHandler(int fd
, void *thecbdata
)
2370 if (!cbdataReferenceValid (thecbdata
))
2373 CbDataList
<DeferredRead
> *temp
= (CbDataList
<DeferredRead
> *)thecbdata
;
2375 temp
->element
.closer
= NULL
;
2376 temp
->element
.markCancelled();
2380 DeferredReadManager::popHead(CbDataListContainer
<DeferredRead
> &deferredReads
)
2382 assert (!deferredReads
.empty());
2384 DeferredRead
&read
= deferredReads
.head
->element
;
2385 if (!read
.cancelled
) {
2386 comm_remove_close_handler(read
.theRead
.fd
, read
.closer
);
2390 DeferredRead result
= deferredReads
.pop_front();
2396 DeferredReadManager::kickReads(int const count
)
2398 /* if we had CbDataList::size() we could consolidate this and flushReads */
2405 size_t remaining
= count
;
2407 while (!deferredReads
.empty() && remaining
) {
2408 DeferredRead aRead
= popHead(deferredReads
);
2411 if (!aRead
.cancelled
)
2417 DeferredReadManager::flushReads()
2419 CbDataListContainer
<DeferredRead
> reads
;
2420 reads
= deferredReads
;
2421 deferredReads
= CbDataListContainer
<DeferredRead
>();
2423 // XXX: For fairness this SHOULD randomize the order
2424 while (!reads
.empty()) {
2425 DeferredRead aRead
= popHead(reads
);
2431 DeferredReadManager::kickARead(DeferredRead
const &aRead
)
2433 if (aRead
.cancelled
)
2436 if (aRead
.theRead
.fd
>=0 && fd_table
[aRead
.theRead
.fd
].closing())
2439 debugs(5, 3, "Kicking deferred read on FD " << aRead
.theRead
.fd
);
2441 aRead
.theReader(aRead
.theContext
, aRead
.theRead
);
2445 DeferredRead::markCancelled()
2450 ConnectionDetail::ConnectionDetail() : me(), peer()
2455 CommSelectEngine::checkEvents(int timeout
)
2457 static time_t last_timeout
= 0;
2459 /* No, this shouldn't be here. But it shouldn't be in each comm handler. -adrian */
2460 if (squid_curtime
> last_timeout
) {
2461 last_timeout
= squid_curtime
;
2465 switch (comm_select(timeout
)) {
2481 fatal_dump("comm.cc: Internal error -- this should never happen.");
2486 /// Create a unix-domain socket (UDS) that only supports FD_MSGHDR I/O.
2488 comm_open_uds(int sock_type
,
2490 struct sockaddr_un
* addr
,
2493 // TODO: merge with comm_openex() when Ip::Address becomes NetAddress
2497 PROF_start(comm_open
);
2498 /* Create socket for accepting new connections. */
2499 statCounter
.syscalls
.sock
.sockets
++;
2501 /* Setup the socket addrinfo details for use */
2504 AI
.ai_family
= PF_UNIX
;
2505 AI
.ai_socktype
= sock_type
;
2506 AI
.ai_protocol
= proto
;
2507 AI
.ai_addrlen
= SUN_LEN(addr
);
2508 AI
.ai_addr
= (sockaddr
*)addr
;
2509 AI
.ai_canonname
= NULL
;
2512 debugs(50, 3, HERE
<< "Attempt open socket for: " << addr
->sun_path
);
2514 if ((new_socket
= socket(AI
.ai_family
, AI
.ai_socktype
, AI
.ai_protocol
)) < 0) {
2515 /* Increase the number of reserved fd's if calls to socket()
2516 * are failing because the open file table is full. This
2517 * limits the number of simultaneous clients */
2519 if (limitError(errno
)) {
2520 debugs(50, DBG_IMPORTANT
, HERE
<< "socket failure: " << xstrerror());
2523 debugs(50, DBG_CRITICAL
, HERE
<< "socket failure: " << xstrerror());
2526 PROF_stop(comm_open
);
2530 debugs(50, 3, HERE
"Opened UDS FD " << new_socket
<< " : family=" << AI
.ai_family
<< ", type=" << AI
.ai_socktype
<< ", protocol=" << AI
.ai_protocol
);
2533 debugs(50, 5, HERE
<< "FD " << new_socket
<< " is a new socket");
2535 assert(!isOpen(new_socket
));
2536 fd_open(new_socket
, FD_MSGHDR
, NULL
);
2538 fdd_table
[new_socket
].close_file
= NULL
;
2540 fdd_table
[new_socket
].close_line
= 0;
2542 fd_table
[new_socket
].sock_family
= AI
.ai_family
;
2544 if (!(flags
& COMM_NOCLOEXEC
))
2545 commSetCloseOnExec(new_socket
);
2547 if (flags
& COMM_REUSEADDR
)
2548 commSetReuseAddr(new_socket
);
2550 if (flags
& COMM_NONBLOCKING
) {
2551 if (commSetNonBlocking(new_socket
) != COMM_OK
) {
2552 comm_close(new_socket
);
2553 PROF_stop(comm_open
);
2558 if (flags
& COMM_DOBIND
) {
2559 if (commBind(new_socket
, AI
) != COMM_OK
) {
2560 comm_close(new_socket
);
2561 PROF_stop(comm_open
);
2567 if (sock_type
== SOCK_STREAM
)
2568 commSetTcpNoDelay(new_socket
);
2572 if (Config
.tcpRcvBufsz
> 0 && sock_type
== SOCK_STREAM
)
2573 commSetTcpRcvbuf(new_socket
, Config
.tcpRcvBufsz
);
2575 PROF_stop(comm_open
);