]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm.cc
Bug #3048 fix: "commio_has_callback(fd, IOCB_READ, ccb)" assertion;
[thirdparty/squid.git] / src / comm.cc
1 /*
2 * DEBUG: section 05 Socket Functions
3 * AUTHOR: Harvest Derived
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
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.
16 *
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.
21 *
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.
26 *
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.
30 *
31 *
32 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
33 */
34
35 #include "squid.h"
36 #include "StoreIOBuffer.h"
37 #include "comm.h"
38 #include "event.h"
39 #include "fde.h"
40 #include "comm/AcceptLimiter.h"
41 #include "comm/comm_internal.h"
42 #include "comm/ListenStateData.h"
43 #include "CommIO.h"
44 #include "CommRead.h"
45 #include "ConnectionDetail.h"
46 #include "MemBuf.h"
47 #include "pconn.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"
54 #include "ip/tools.h"
55
56 #if defined(_SQUID_CYGWIN_)
57 #include <sys/ioctl.h>
58 #endif
59 #ifdef HAVE_NETINET_TCP_H
60 #include <netinet/tcp.h>
61 #endif
62
63 /*
64 * New C-like simple comm code. This stuff is a mess and doesn't really buy us anything.
65 */
66
67 typedef enum {
68 IOCB_NONE,
69 IOCB_READ,
70 IOCB_WRITE
71 } iocb_type;
72
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);
77
78
79 struct comm_io_callback_t {
80 iocb_type type;
81 int fd;
82 AsyncCall::Pointer callback;
83 char *buf;
84 FREE *freefunc;
85 int size;
86 int offset;
87 comm_err_t errcode;
88 int xerrno;
89
90 bool active() const { return callback != NULL; }
91 };
92
93 struct _comm_fd {
94 int fd;
95 comm_io_callback_t readcb;
96 comm_io_callback_t writecb;
97 };
98 typedef struct _comm_fd comm_fd_t;
99 comm_fd_t *commfd_table;
100
101 // TODO: make this a comm_io_callback_t method?
102 bool
103 commio_has_callback(int fd, iocb_type type, comm_io_callback_t *ccb)
104 {
105 assert(ccb->fd == fd);
106 assert(ccb->type == type);
107 return ccb->active();
108 }
109
110 /*
111 * Configure comm_io_callback_t for I/O
112 *
113 * @param fd filedescriptor
114 * @param ccb comm io callback
115 * @param cb 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
120 */
121 static void
122 commio_set_callback(int fd, iocb_type type, comm_io_callback_t *ccb,
123 AsyncCall::Pointer &cb, char *buf, FREE *freefunc, int size)
124 {
125 assert(!ccb->active());
126 assert(ccb->type == type);
127 assert(cb != NULL);
128 ccb->fd = fd;
129 ccb->callback = cb;
130 ccb->buf = buf;
131 ccb->freefunc = freefunc;
132 ccb->size = size;
133 ccb->offset = 0;
134 }
135
136
137 // Schedule the callback call and clear the callback
138 static void
139 commio_finish_callback(int fd, comm_io_callback_t *ccb, comm_err_t code, int xerrno)
140 {
141 debugs(5, 3, "commio_finish_callback: called for FD " << fd << " (" <<
142 code << ", " << xerrno << ")");
143 assert(ccb->active());
144 assert(ccb->fd == fd);
145 ccb->errcode = code;
146 ccb->xerrno = xerrno;
147
148 comm_io_callback_t cb = *ccb;
149
150 /* We've got a copy; blow away the real one */
151 /* XXX duplicate code from commio_cancel_callback! */
152 ccb->xerrno = 0;
153 ccb->callback = NULL; // cb has it
154
155 /* free data */
156 if (cb.freefunc) {
157 cb.freefunc(cb.buf);
158 cb.buf = NULL;
159 }
160
161 if (cb.callback != NULL) {
162 typedef CommIoCbParams Params;
163 Params &params = GetCommParams<Params>(cb.callback);
164 params.fd = cb.fd;
165 params.buf = cb.buf;
166 params.size = cb.offset;
167 params.flag = cb.errcode;
168 params.xerrno = cb.xerrno;
169 ScheduleCallHere(cb.callback);
170 }
171 }
172
173
174 /*
175 * Cancel the given callback
176 *
177 * Remember that the data is cbdataRef'ed.
178 */
179 // TODO: make this a comm_io_callback_t method
180 static void
181 commio_cancel_callback(int fd, comm_io_callback_t *ccb)
182 {
183 debugs(5, 3, "commio_cancel_callback: called for FD " << fd);
184 assert(ccb->fd == fd);
185 assert(ccb->active());
186
187 ccb->xerrno = 0;
188 ccb->callback = NULL;
189 }
190
191 /*
192 * Call the given comm callback; assumes the callback is valid.
193 *
194 * @param ccb io completion callback
195 */
196 void
197 commio_call_callback(comm_io_callback_t *ccb)
198 {
199 }
200
201 class ConnectStateData
202 {
203
204 public:
205 void *operator new (size_t);
206 void operator delete (void *);
207 static void Connect (int fd, void *me);
208 void connect();
209 void callCallback(comm_err_t status, int xerrno);
210 void defaults();
211
212 // defaults given by client
213 char *host;
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.
217
218 DnsLookupDetails dns; ///< host lookup details
219 Ip::Address S;
220 AsyncCall::Pointer callback;
221
222 int fd;
223 int tries;
224 int addrcount;
225 int connstart;
226
227 private:
228 int commResetFD();
229 int commRetryConnect();
230 CBDATA_CLASS(ConnectStateData);
231 };
232
233 /* STATIC */
234
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();
239
240 static comm_err_t commBind(int s, struct addrinfo &);
241 static void commSetReuseAddr(int);
242 static void commSetNoLinger(int);
243 #ifdef TCP_NODELAY
244 static void commSetTcpNoDelay(int);
245 #endif
246 static void commSetTcpRcvbuf(int, int);
247 static PF commConnectFree;
248 static PF commHandleWrite;
249 static IPH commConnectDnsHandle;
250
251 typedef enum {
252 COMM_CB_READ = 1,
253 COMM_CB_DERIVED
254 } comm_callback_t;
255
256 static MemAllocator *conn_close_pool = NULL;
257 fd_debug_t *fdd_table = NULL;
258
259 bool
260 isOpen(const int fd)
261 {
262 return fd_table[fd].flags.open != 0;
263 }
264
265 /**
266 * Attempt a read
267 *
268 * If the read attempt succeeds or fails, call the callback.
269 * Else, wait for another IO notification.
270 */
271 void
272 commHandleRead(int fd, void *data)
273 {
274 comm_io_callback_t *ccb = (comm_io_callback_t *) data;
275
276 assert(data == COMMIO_FD_READCB(fd));
277 assert(commio_has_callback(fd, IOCB_READ, ccb));
278 /* Attempt a read */
279 statCounter.syscalls.sock.reads++;
280 errno = 0;
281 int retval;
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);
284
285 if (retval < 0 && !ignoreErrno(errno)) {
286 debugs(5, 3, "comm_read_try: scheduling COMM_ERROR");
287 ccb->offset = 0;
288 commio_finish_callback(fd, ccb, COMM_ERROR, errno);
289 return;
290 };
291
292 /* See if we read anything */
293 /* Note - read 0 == socket EOF, which is a valid read */
294 if (retval >= 0) {
295 fd_bytes(fd, retval, FD_READ);
296 ccb->offset = retval;
297 commio_finish_callback(fd, ccb, COMM_OK, errno);
298 return;
299 }
300
301 /* Nope, register for some more IO */
302 commSetSelect(fd, COMM_SELECT_READ, commHandleRead, data, 0);
303 }
304
305 /**
306 * Queue a read. handler/handler_data are called when the read
307 * completes, on error, or on file descriptor close.
308 */
309 void
310 comm_read(int fd, char *buf, int size, IOCB *handler, void *handler_data)
311 {
312 AsyncCall::Pointer call = commCbCall(5,4, "SomeCommReadHandler",
313 CommIoCbPtrFun(handler, handler_data));
314 comm_read(fd, buf, size, call);
315 }
316
317 void
318 comm_read(int fd, char *buf, int size, AsyncCall::Pointer &callback)
319 {
320 debugs(5, 5, "comm_read, queueing read for FD " << fd << "; asynCall " << callback);
321
322 /* Make sure we are open and not closing */
323 assert(isOpen(fd));
324 assert(!fd_table[fd].closing());
325 comm_io_callback_t *ccb = COMMIO_FD_READCB(fd);
326
327 // Make sure we are either not reading or just passively monitoring.
328 // Active/passive conflicts are OK and simply cancel passive monitoring.
329 if (ccb->active()) {
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());
334 }
335
336 /* Queue the read */
337 commio_set_callback(fd, IOCB_READ, ccb, callback, (char *)buf, NULL, size);
338 commSetSelect(fd, COMM_SELECT_READ, commHandleRead, ccb, 0);
339 }
340
341 /**
342 * Empty the read buffers
343 *
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
347 * to timeout.
348 */
349 static void
350 comm_empty_os_read_buffers(int fd)
351 {
352 #ifdef _SQUID_LINUX_
353 /* prevent those nasty RST packets */
354 char buf[SQUID_TCP_SO_RCVBUF];
355
356 if (fd_table[fd].flags.nonblocking == 1) {
357 while (FD_READ_METHOD(fd, buf, SQUID_TCP_SO_RCVBUF) > 0) {};
358 }
359 #endif
360 }
361
362
363 /**
364 * Return whether the FD has a pending completed callback.
365 */
366 int
367 comm_has_pending_read_callback(int fd)
368 {
369 assert(isOpen(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.
374 return false;
375 }
376
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.
380 bool
381 comm_monitors_read(int fd)
382 {
383 assert(isOpen(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();
389 }
390
391 /**
392 * Cancel a pending read. Assert that we have the right parameters,
393 * and that there are no pending read events!
394 *
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.
400 *
401 * AHC Don't call the comm handlers?
402 */
403 void
404 comm_read_cancel(int fd, IOCB *callback, void *data)
405 {
406 if (!isOpen(fd)) {
407 debugs(5, 4, "comm_read_cancel fails: FD " << fd << " closed");
408 return;
409 }
410
411 comm_io_callback_t *cb = COMMIO_FD_READCB(fd);
412 // TODO: is "active" == "monitors FD"?
413 if (!cb->active()) {
414 debugs(5, 4, "comm_read_cancel fails: FD " << fd << " inactive");
415 return;
416 }
417
418 typedef CommCbFunPtrCallT<CommIoCbPtrFun> Call;
419 Call *call = dynamic_cast<Call*>(cb->callback.getRaw());
420 if (!call) {
421 debugs(5, 4, "comm_read_cancel fails: FD " << fd << " lacks callback");
422 return;
423 }
424
425 call->cancel("old comm_read_cancel");
426
427 typedef CommIoCbParams Params;
428 const Params &params = GetCommParams<Params>(cb->callback);
429
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);
433
434 /* Delete the callback */
435 commio_cancel_callback(fd, cb);
436
437 /* And the IO event */
438 commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
439 }
440
441 void
442 comm_read_cancel(int fd, AsyncCall::Pointer &callback)
443 {
444 callback->cancel("comm_read_cancel");
445
446 if (!isOpen(fd)) {
447 debugs(5, 4, "comm_read_cancel fails: FD " << fd << " closed");
448 return;
449 }
450
451 comm_io_callback_t *cb = COMMIO_FD_READCB(fd);
452
453 if (!cb->active()) {
454 debugs(5, 4, "comm_read_cancel fails: FD " << fd << " inactive");
455 return;
456 }
457
458 AsyncCall::Pointer call = cb->callback;
459 assert(call != NULL); // XXX: should never fail (active() checks for callback==NULL)
460
461 /* Ok, we can be reasonably sure we won't lose any data here! */
462 assert(call == callback);
463
464 /* Delete the callback */
465 commio_cancel_callback(fd, cb);
466
467 /* And the IO event */
468 commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
469 }
470
471
472 /**
473 * synchronous wrapper around udp socket functions
474 */
475 int
476 comm_udp_recvfrom(int fd, void *buf, size_t len, int flags, Ip::Address &from)
477 {
478 statCounter.syscalls.sock.recvfroms++;
479 int x = 0;
480 struct addrinfo *AI = NULL;
481
482 debugs(5,8, "comm_udp_recvfrom: FD " << fd << " from " << from);
483
484 assert( NULL == AI );
485
486 from.InitAddrInfo(AI);
487
488 x = recvfrom(fd, buf, len, flags, AI->ai_addr, &AI->ai_addrlen);
489
490 from = *AI;
491
492 from.FreeAddrInfo(AI);
493
494 return x;
495 }
496
497 int
498 comm_udp_recv(int fd, void *buf, size_t len, int flags)
499 {
500 Ip::Address nul;
501 return comm_udp_recvfrom(fd, buf, len, flags, nul);
502 }
503
504 ssize_t
505 comm_udp_send(int s, const void *buf, size_t len, int flags)
506 {
507 return send(s, buf, len, flags);
508 }
509
510
511 bool
512 comm_has_incomplete_write(int fd)
513 {
514 assert(isOpen(fd));
515 return COMMIO_FD_WRITECB(fd)->active();
516 }
517
518 /**
519 * Queue a write. handler/handler_data are called when the write fully
520 * completes, on error, or on file descriptor close.
521 */
522
523 /* Return the local port associated with fd. */
524 u_short
525 comm_local_port(int fd)
526 {
527 Ip::Address temp;
528 struct addrinfo *addr = NULL;
529 fde *F = &fd_table[fd];
530
531 /* If the fd is closed already, just return */
532
533 if (!F->flags.open) {
534 debugs(5, 0, "comm_local_port: FD " << fd << " has been closed.");
535 return 0;
536 }
537
538 if (F->local_addr.GetPort())
539 return F->local_addr.GetPort();
540
541 if (F->sock_family == AF_INET)
542 temp.SetIPv4();
543
544 temp.InitAddrInfo(addr);
545
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);
549 return 0;
550 }
551 temp = *addr;
552
553 temp.FreeAddrInfo(addr);
554
555 F->local_addr.SetPort(temp.GetPort());
556
557 #if 0 // seems to undo comm_open actions on the FD ...
558 // grab default socket information for this address
559 temp.GetAddrInfo(addr);
560
561 F->sock_family = addr->ai_family;
562
563 temp.FreeAddrInfo(addr);
564 #endif
565
566 debugs(5, 6, "comm_local_port: FD " << fd << ": port " << F->local_addr.GetPort() << "(family=" << F->sock_family << ")");
567 return F->local_addr.GetPort();
568 }
569
570 static comm_err_t
571 commBind(int s, struct addrinfo &inaddr)
572 {
573 statCounter.syscalls.sock.binds++;
574
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);
577 return COMM_OK;
578 }
579
580 debugs(50, 0, "commBind: Cannot bind socket FD " << s << " to " << fd_table[s].local_addr << ": " << xstrerror());
581
582 return COMM_ERROR;
583 }
584
585 /**
586 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
587 * is OR of flags specified in comm.h. Defaults TOS
588 */
589 int
590 comm_open(int sock_type,
591 int proto,
592 Ip::Address &addr,
593 int flags,
594 const char *note)
595 {
596 return comm_openex(sock_type, proto, addr, flags, 0, note);
597 }
598
599 int
600 comm_open_listener(int sock_type,
601 int proto,
602 Ip::Address &addr,
603 int flags,
604 const char *note)
605 {
606 int sock = -1;
607
608 /* all listener sockets require bind() */
609 flags |= COMM_DOBIND;
610
611 /* attempt native enabled port. */
612 sock = comm_openex(sock_type, proto, addr, flags, 0, note);
613
614 return sock;
615 }
616
617 static bool
618 limitError(int const anErrno)
619 {
620 return anErrno == ENFILE || anErrno == EMFILE;
621 }
622
623 int
624 comm_set_tos(int fd, int tos)
625 {
626 #ifdef IP_TOS
627 int x = setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &tos, sizeof(int));
628 if (x < 0)
629 debugs(50, 1, "comm_set_tos: setsockopt(IP_TOS) on FD " << fd << ": " << xstrerror());
630 return x;
631 #else
632 debugs(50, 0, "WARNING: setsockopt(IP_TOS) not supported on this platform");
633 return -1;
634 #endif
635 }
636
637 void
638 comm_set_v6only(int fd, int tos)
639 {
640 #ifdef IPV6_V6ONLY
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());
643 }
644 #else
645 debugs(50, 0, "WARNING: comm_open: setsockopt(IPV6_V6ONLY) not supported on this platform");
646 #endif /* sockopt */
647 }
648
649 /**
650 * Set the socket IP_TRANSPARENT option for Linux TPROXY v4 support.
651 */
652 void
653 comm_set_transparent(int fd)
654 {
655 #if defined(IP_TRANSPARENT)
656 int tos = 1;
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());
659 } else {
660 /* mark the socket as having transparent options */
661 fd_table[fd].flags.transparent = 1;
662 }
663 #else
664 debugs(50, DBG_CRITICAL, "WARNING: comm_open: setsockopt(IP_TRANSPARENT) not supported on this platform");
665 #endif /* sockopt */
666 }
667
668 /**
669 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
670 * is OR of flags specified in defines.h:COMM_*
671 */
672 int
673 comm_openex(int sock_type,
674 int proto,
675 Ip::Address &addr,
676 int flags,
677 unsigned char TOS,
678 const char *note)
679 {
680 int new_socket;
681 int tos = 0;
682 struct addrinfo *AI = NULL;
683
684 PROF_start(comm_open);
685 /* Create socket for accepting new connections. */
686 statCounter.syscalls.sock.sockets++;
687
688 /* Setup the socket addrinfo details for use */
689 addr.GetAddrInfo(AI);
690 AI->ai_socktype = sock_type;
691 AI->ai_protocol = proto;
692
693 debugs(50, 3, "comm_openex: Attempt open socket for: " << addr );
694
695 new_socket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
696
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);
709 }
710
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 */
715
716 if (limitError(errno)) {
717 debugs(50, DBG_IMPORTANT, "comm_open: socket failure: " << xstrerror());
718 fdAdjustReserved();
719 } else {
720 debugs(50, DBG_CRITICAL, "comm_open: socket failure: " << xstrerror());
721 }
722
723 addr.FreeAddrInfo(AI);
724
725 PROF_stop(comm_open);
726 return -1;
727 }
728
729 debugs(50, 3, "comm_openex: Opened socket FD " << new_socket << " : family=" << AI->ai_family << ", type=" << AI->ai_socktype << ", protocol=" << AI->ai_protocol );
730
731 /* set TOS if needed */
732 if (TOS && comm_set_tos(new_socket, TOS) ) {
733 tos = TOS;
734 }
735
736 if ( Ip::EnableIpv6&IPV6_SPECIAL_SPLITSTACK && addr.IsIPv6() )
737 comm_set_v6only(new_socket, 1);
738
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);
743
744 comm_init_opened(new_socket, addr, TOS, note, AI);
745 new_socket = comm_apply_flags(new_socket, addr, flags, AI);
746
747 addr.FreeAddrInfo(AI);
748
749 PROF_stop(comm_open);
750
751 return new_socket;
752 }
753
754 /// update FD tables after a local or remote (IPC) comm_openex();
755 void
756 comm_init_opened(int new_socket,
757 Ip::Address &addr,
758 unsigned char TOS,
759 const char *note,
760 struct addrinfo *AI)
761 {
762 assert(new_socket >= 0);
763 assert(AI);
764
765 fde *F = NULL;
766
767 /* update fdstat */
768 debugs(5, 5, "comm_open: FD " << new_socket << " is a new socket");
769
770 assert(!isOpen(new_socket));
771 fd_open(new_socket, FD_SOCKET, note);
772
773 fdd_table[new_socket].close_file = NULL;
774
775 fdd_table[new_socket].close_line = 0;
776
777 F = &fd_table[new_socket];
778
779 F->local_addr = addr;
780
781 F->tos = TOS;
782
783 F->sock_family = AI->ai_family;
784 }
785
786 /// apply flags after a local comm_open*() call;
787 /// returns new_socket or -1 on error
788 static int
789 comm_apply_flags(int new_socket,
790 Ip::Address &addr,
791 int flags,
792 struct addrinfo *AI)
793 {
794 assert(new_socket >= 0);
795 assert(AI);
796 const int sock_type = AI->ai_socktype;
797
798 if (!(flags & COMM_NOCLOEXEC))
799 commSetCloseOnExec(new_socket);
800
801 if ((flags & COMM_REUSEADDR))
802 commSetReuseAddr(new_socket);
803
804 if (addr.GetPort() > (u_short) 0) {
805 #ifdef _SQUID_MSWIN_
806 if (sock_type != SOCK_DGRAM)
807 #endif
808 commSetNoLinger(new_socket);
809
810 if (opt_reuseaddr)
811 commSetReuseAddr(new_socket);
812 }
813
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);
817 }
818
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 << "!!");
824
825 if (commBind(new_socket, *AI) != COMM_OK) {
826 comm_close(new_socket);
827 return -1;
828 }
829 }
830
831 if (flags & COMM_NONBLOCKING)
832 if (commSetNonBlocking(new_socket) == COMM_ERROR) {
833 comm_close(new_socket);
834 return -1;
835 }
836
837 #ifdef TCP_NODELAY
838 if (sock_type == SOCK_STREAM)
839 commSetTcpNoDelay(new_socket);
840
841 #endif
842
843 if (Config.tcpRcvBufsz > 0 && sock_type == SOCK_STREAM)
844 commSetTcpRcvbuf(new_socket, Config.tcpRcvBufsz);
845
846 return new_socket;
847 }
848
849 void
850 comm_import_opened(int fd,
851 Ip::Address &addr,
852 int flags,
853 const char *note,
854 struct addrinfo *AI)
855 {
856 debugs(5, 2, HERE << " FD " << fd << " at " << addr);
857 assert(fd >= 0);
858 assert(AI);
859
860 comm_init_opened(fd, addr, 0, note, AI);
861
862 if (!(flags & COMM_NOCLOEXEC))
863 fd_table[fd].flags.close_on_exec = 1;
864
865 if (addr.GetPort() > (u_short) 0) {
866 #ifdef _SQUID_MSWIN_
867 if (sock_type != SOCK_DGRAM)
868 #endif
869 fd_table[fd].flags.nolinger = 1;
870 }
871
872 if ((flags & COMM_TRANSPARENT))
873 fd_table[fd].flags.transparent = 1;
874
875 if (flags & COMM_NONBLOCKING)
876 fd_table[fd].flags.nonblocking = 1;
877
878 #ifdef TCP_NODELAY
879 if (AI->ai_socktype == SOCK_STREAM)
880 fd_table[fd].flags.nodelay = 1;
881 #endif
882
883 /* no fd_table[fd].flags. updates needed for these conditions:
884 * if ((flags & COMM_REUSEADDR)) ...
885 * if ((flags & COMM_DOBIND) ...) ...
886 */
887 }
888
889
890 CBDATA_CLASS_INIT(ConnectStateData);
891
892 void *
893 ConnectStateData::operator new (size_t size)
894 {
895 CBDATA_INIT_TYPE(ConnectStateData);
896 return cbdataAlloc(ConnectStateData);
897 }
898
899 void
900 ConnectStateData::operator delete (void *address)
901 {
902 cbdataFree(address);
903 }
904
905
906
907 void
908 commConnectStart(int fd, const char *host, u_short port, AsyncCall::Pointer &cb)
909 {
910 debugs(cb->debugSection, cb->debugLevel, "commConnectStart: FD " << fd <<
911 ", cb " << cb << ", " << host << ":" << port); // TODO: just print *cb
912
913 ConnectStateData *cs;
914 cs = new ConnectStateData;
915 cs->fd = fd;
916 cs->host = xstrdup(host);
917 cs->default_port = port;
918 cs->callback = cb;
919
920 comm_add_close_handler(fd, commConnectFree, cs);
921 ipcache_nbgethostbyname(host, commConnectDnsHandle, cs);
922 }
923
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.
928 void
929 commConnectStart(int fd, const char *host, u_short port, CNCB * callback, void *data)
930 {
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);
935 }
936
937 static void
938 commConnectDnsHandle(const ipcache_addrs *ia, const DnsLookupDetails &details, void *data)
939 {
940 ConnectStateData *cs = (ConnectStateData *)data;
941 cs->dns = details;
942
943 if (ia == NULL) {
944 debugs(5, 3, "commConnectDnsHandle: Unknown host: " << cs->host);
945 cs->callCallback(COMM_ERR_DNS, 0);
946 return;
947 }
948
949 assert(ia->cur < ia->count);
950
951 cs->default_addr = ia->in_addrs[ia->cur];
952
953 if (Config.onoff.balance_on_multiple_ip)
954 ipcacheCycleAddr(cs->host, NULL);
955
956 cs->addrcount = ia->count;
957
958 cs->connstart = squid_curtime;
959
960 cs->connect();
961 }
962
963 void
964 ConnectStateData::callCallback(comm_err_t status, int xerrno)
965 {
966 debugs(5, 3, "commConnectCallback: FD " << fd);
967
968 comm_remove_close_handler(fd, commConnectFree, this);
969 commSetTimeout(fd, -1, NULL, NULL);
970
971 typedef CommConnectCbParams Params;
972 Params &params = GetCommParams<Params>(callback);
973 params.fd = fd;
974 params.dns = dns;
975 params.flag = status;
976 params.xerrno = xerrno;
977 ScheduleCallHere(callback);
978 callback = NULL;
979
980 commConnectFree(fd, this);
981 }
982
983 static void
984 commConnectFree(int fd, void *data)
985 {
986 ConnectStateData *cs = (ConnectStateData *)data;
987 debugs(5, 3, "commConnectFree: FD " << fd);
988 // delete cs->callback;
989 cs->callback = NULL;
990 safe_free(cs->host);
991 delete cs;
992 }
993
994 static void
995 copyFDFlags(int to, fde *F)
996 {
997 if (F->flags.close_on_exec)
998 commSetCloseOnExec(to);
999
1000 if (F->flags.nonblocking)
1001 commSetNonBlocking(to);
1002
1003 #ifdef TCP_NODELAY
1004
1005 if (F->flags.nodelay)
1006 commSetTcpNoDelay(to);
1007
1008 #endif
1009
1010 if (Config.tcpRcvBufsz > 0)
1011 commSetTcpRcvbuf(to, Config.tcpRcvBufsz);
1012 }
1013
1014 /* Reset FD so that we can connect() again */
1015 int
1016 ConnectStateData::commResetFD()
1017 {
1018
1019 // XXX: do we have to check this?
1020 //
1021 // if (!cbdataReferenceValid(callback.data))
1022 // return 0;
1023
1024 statCounter.syscalls.sock.sockets++;
1025
1026 fde *F = &fd_table[fd];
1027
1028 struct addrinfo *AI = NULL;
1029 F->local_addr.GetAddrInfo(AI);
1030 int new_family = AI->ai_family;
1031
1032 int fd2 = socket(new_family, AI->ai_socktype, AI->ai_protocol);
1033
1034 if (fd2 < 0) {
1035 debugs(5, DBG_CRITICAL, HERE << "WARNING: FD " << fd2 << " socket failed to allocate: " << xstrerror());
1036
1037 if (ENFILE == errno || EMFILE == errno)
1038 fdAdjustReserved();
1039
1040 F->local_addr.FreeAddrInfo(AI);
1041 return 0;
1042 }
1043
1044 #ifdef _SQUID_MSWIN_
1045
1046 /* On Windows dup2() can't work correctly on Sockets, the */
1047 /* workaround is to close the destination Socket before call them. */
1048 close(fd);
1049
1050 #endif
1051
1052 if (dup2(fd2, fd) < 0) {
1053 debugs(5, DBG_CRITICAL, HERE << "WARNING: dup2(FD " << fd2 << ", FD " << fd << ") failed: " << xstrerror());
1054
1055 if (ENFILE == errno || EMFILE == errno)
1056 fdAdjustReserved();
1057
1058 close(fd2);
1059
1060 F->local_addr.FreeAddrInfo(AI);
1061 return 0;
1062 }
1063 commResetSelect(fd);
1064
1065 close(fd2);
1066
1067 debugs(50, 3, "commResetFD: Reset socket FD " << fd << "->" << fd2 << " : family=" << new_family );
1068
1069 /* INET6: copy the new sockets family type to the FDE table */
1070 F->sock_family = new_family;
1071
1072 F->flags.called_connect = 0;
1073
1074 /*
1075 * yuck, this has assumptions about comm_open() arguments for
1076 * the original socket
1077 */
1078
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);
1082 }
1083
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);
1087 return 0;
1088 }
1089 F->local_addr.FreeAddrInfo(AI);
1090
1091 if (F->tos)
1092 comm_set_tos(fd, F->tos);
1093
1094 if ( Ip::EnableIpv6&IPV6_SPECIAL_SPLITSTACK && F->local_addr.IsIPv6() )
1095 comm_set_v6only(fd, 1);
1096
1097 copyFDFlags(fd, F);
1098
1099 return 1;
1100 }
1101
1102 int
1103 ConnectStateData::commRetryConnect()
1104 {
1105 assert(addrcount > 0);
1106
1107 if (addrcount == 1) {
1108 if (tries >= Config.retry.maxtries)
1109 return 0;
1110
1111 if (squid_curtime - connstart > Config.Timeout.connect)
1112 return 0;
1113 } else {
1114 if (tries > addrcount) {
1115 /* Flush bad address count in case we are
1116 * skipping over incompatible protocol
1117 */
1118 ipcacheMarkAllGood(host);
1119 return 0;
1120 }
1121 }
1122
1123 return commResetFD();
1124 }
1125
1126 static void
1127 commReconnect(void *data)
1128 {
1129 ConnectStateData *cs = (ConnectStateData *)data;
1130 ipcache_nbgethostbyname(cs->host, commConnectDnsHandle, cs);
1131 }
1132
1133 /** Connect SOCK to specified DEST_PORT at DEST_HOST. */
1134 void
1135 ConnectStateData::Connect(int fd, void *me)
1136 {
1137 ConnectStateData *cs = (ConnectStateData *)me;
1138 assert (cs->fd == fd);
1139 cs->connect();
1140 }
1141
1142 void
1143 ConnectStateData::defaults()
1144 {
1145 S = default_addr;
1146 S.SetPort(default_port);
1147 }
1148
1149 void
1150 ConnectStateData::connect()
1151 {
1152 defaults();
1153
1154 debugs(5,5, HERE << "to " << S);
1155
1156 switch (comm_connect_addr(fd, S) ) {
1157
1158 case COMM_INPROGRESS:
1159 debugs(5, 5, HERE << "FD " << fd << ": COMM_INPROGRESS");
1160 commSetSelect(fd, COMM_SELECT_WRITE, ConnectStateData::Connect, this, 0);
1161 break;
1162
1163 case COMM_OK:
1164 debugs(5, 5, HERE << "FD " << fd << ": COMM_OK - connected");
1165 ipcacheMarkGoodAddr(host, S);
1166 callCallback(COMM_OK, 0);
1167 break;
1168
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
1174 */
1175 tries++;
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);
1180 } else {
1181 debugs(5, 5, HERE << "FD " << fd << ": COMM_ERR_PROTOCOL - ERR tried too many times already.");
1182 callCallback(COMM_ERR_CONNECT, errno);
1183 }
1184 break;
1185
1186 default:
1187 debugs(5, 5, HERE "FD " << fd << ": * - try again");
1188 tries++;
1189 ipcacheMarkBadAddr(host, S);
1190
1191 #if USE_ICMP
1192 if (Config.onoff.test_reachability)
1193 netdbDeleteAddrNetwork(S);
1194 #endif
1195
1196 if (commRetryConnect()) {
1197 eventAdd("commReconnect", commReconnect, this, this->addrcount == 1 ? 0.05 : 0.0, 0);
1198 } else {
1199 debugs(5, 5, HERE << "FD " << fd << ": * - ERR tried too many times already.");
1200 callCallback(COMM_ERR_CONNECT, errno);
1201 }
1202 }
1203 }
1204 /*
1205 int
1206 commSetTimeout_old(int fd, int timeout, PF * handler, void *data)
1207 {
1208 debugs(5, 3, HERE << "FD " << fd << " timeout " << timeout);
1209 assert(fd >= 0);
1210 assert(fd < Squid_MaxFD);
1211 fde *F = &fd_table[fd];
1212 assert(F->flags.open);
1213
1214 if (timeout < 0) {
1215 cbdataReferenceDone(F->timeout_data);
1216 F->timeout_handler = NULL;
1217 F->timeout = 0;
1218 } else {
1219 if (handler) {
1220 cbdataReferenceDone(F->timeout_data);
1221 F->timeout_handler = handler;
1222 F->timeout_data = cbdataReference(data);
1223 }
1224
1225 F->timeout = squid_curtime + (time_t) timeout;
1226 }
1227
1228 return F->timeout;
1229 }
1230 */
1231
1232 int
1233 commSetTimeout(int fd, int timeout, PF * handler, void *data)
1234 {
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));
1239 else
1240 call = NULL;
1241 return commSetTimeout(fd, timeout, call);
1242 }
1243
1244
1245 int commSetTimeout(int fd, int timeout, AsyncCall::Pointer &callback)
1246 {
1247 debugs(5, 3, HERE << "FD " << fd << " timeout " << timeout);
1248 assert(fd >= 0);
1249 assert(fd < Squid_MaxFD);
1250 fde *F = &fd_table[fd];
1251 assert(F->flags.open);
1252
1253 if (timeout < 0) {
1254 F->timeoutHandler = NULL;
1255 F->timeout = 0;
1256 } else {
1257 if (callback != NULL) {
1258 typedef CommTimeoutCbParams Params;
1259 Params &params = GetCommParams<Params>(callback);
1260 params.fd = fd;
1261 F->timeoutHandler = callback;
1262 }
1263
1264 F->timeout = squid_curtime + (time_t) timeout;
1265 }
1266
1267 return F->timeout;
1268
1269 }
1270
1271 int
1272 comm_connect_addr(int sock, const Ip::Address &address)
1273 {
1274 comm_err_t status = COMM_OK;
1275 fde *F = &fd_table[sock];
1276 int x = 0;
1277 int err = 0;
1278 socklen_t errlen;
1279 struct addrinfo *AI = NULL;
1280 PROF_start(comm_connect_addr);
1281
1282 assert(address.GetPort() != 0);
1283
1284 debugs(5, 9, HERE << "connecting socket FD " << sock << " to " << address << " (want family: " << F->sock_family << ")");
1285
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.
1290 */
1291 if (F->sock_family == AF_INET && !address.IsIPv4()) {
1292 errno = ENETUNREACH;
1293 return COMM_ERR_PROTOCOL;
1294 }
1295
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.
1302 */
1303 if (!F->local_addr.IsIPv4() && address.IsIPv4()) {
1304 errno = ENETUNREACH;
1305 return COMM_ERR_PROTOCOL;
1306 }
1307
1308 address.GetAddrInfo(AI, F->sock_family);
1309
1310 /* Establish connection. */
1311 errno = 0;
1312
1313 if (!F->flags.called_connect) {
1314 F->flags.called_connect = 1;
1315 statCounter.syscalls.sock.connects++;
1316
1317 x = connect(sock, AI->ai_addr, AI->ai_addrlen);
1318
1319 // XXX: ICAP code refuses callbacks during a pending comm_ call
1320 // Async calls development will fix this.
1321 if (x == 0) {
1322 x = -1;
1323 errno = EINPROGRESS;
1324 }
1325
1326 if (x < 0) {
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 <<
1334 " )" );
1335 debugs(5, 9, "connect FD " << sock << ": (" << x << ") " << xstrerror());
1336 debugs(14,9, "connecting to: " << address );
1337 }
1338 } else {
1339 #if defined(_SQUID_NEWSOS6_)
1340 /* Makoto MATSUSHITA <matusita@ics.es.osaka-u.ac.jp> */
1341
1342 connect(sock, AI->ai_addr, AI->ai_addrlen);
1343
1344 if (errno == EINVAL) {
1345 errlen = sizeof(err);
1346 x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen);
1347
1348 if (x >= 0)
1349 errno = x;
1350 }
1351
1352 #else
1353 errlen = sizeof(err);
1354
1355 x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen);
1356
1357 if (x == 0)
1358 errno = err;
1359
1360 #if defined(_SQUID_SOLARIS_)
1361 /*
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
1366 */
1367 if (x < 0 && errno == EPIPE)
1368 errno = ENOTCONN;
1369
1370 #endif
1371 #endif
1372
1373 }
1374
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. */
1378 #if 0
1379 #ifdef _SQUID_LINUX_
1380 /* 2007-11-27:
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.
1385 */
1386 if (errno != 0) {
1387 AI = NULL;
1388 }
1389 #endif
1390 #endif
1391
1392 address.FreeAddrInfo(AI);
1393
1394 PROF_stop(comm_connect_addr);
1395
1396 if (errno == 0 || errno == EISCONN)
1397 status = COMM_OK;
1398 else if (ignoreErrno(errno))
1399 status = COMM_INPROGRESS;
1400 else if (errno == EAFNOSUPPORT || errno == EINVAL)
1401 return COMM_ERR_PROTOCOL;
1402 else
1403 return COMM_ERROR;
1404
1405 address.NtoA(F->ipaddr, MAX_IPSTRLEN);
1406
1407 F->remote_port = address.GetPort(); /* remote_port is HS */
1408
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");
1413 }
1414
1415 return status;
1416 }
1417
1418 void
1419 commCallCloseHandlers(int fd)
1420 {
1421 fde *F = &fd_table[fd];
1422 debugs(5, 5, "commCallCloseHandlers: FD " << fd);
1423
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 &params = GetCommParams<Params>(call);
1433 params.fd = fd;
1434 ScheduleCallHere(call);
1435 }
1436 }
1437 }
1438
1439 #if LINGERING_CLOSE
1440 static void
1441 commLingerClose(int fd, void *unused)
1442 {
1443 LOCAL_ARRAY(char, buf, 1024);
1444 int n;
1445 n = FD_READ_METHOD(fd, buf, 1024);
1446
1447 if (n < 0)
1448 debugs(5, 3, "commLingerClose: FD " << fd << " read: " << xstrerror());
1449
1450 comm_close(fd);
1451 }
1452
1453 static void
1454 commLingerTimeout(int fd, void *unused)
1455 {
1456 debugs(5, 3, "commLingerTimeout: FD " << fd);
1457 comm_close(fd);
1458 }
1459
1460 /*
1461 * Inspired by apache
1462 */
1463 void
1464 comm_lingering_close(int fd)
1465 {
1466 #if USE_SSL
1467
1468 if (fd_table[fd].ssl)
1469 ssl_shutdown_method(fd);
1470
1471 #endif
1472
1473 if (shutdown(fd, 1) < 0) {
1474 comm_close(fd);
1475 return;
1476 }
1477
1478 fd_note(fd, "lingering close");
1479 commSetTimeout(fd, 10, commLingerTimeout, NULL);
1480 commSetSelect(fd, COMM_SELECT_READ, commLingerClose, NULL, 0);
1481 }
1482
1483 #endif
1484
1485 /*
1486 * enable linger with time of 0 so that when the socket is
1487 * closed, TCP generates a RESET
1488 */
1489 void
1490 comm_reset_close(int fd)
1491 {
1492
1493 struct linger L;
1494 L.l_onoff = 1;
1495 L.l_linger = 0;
1496
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());
1499
1500 comm_close(fd);
1501 }
1502
1503 void
1504 comm_close_start(int fd, void *data)
1505 {
1506 #if USE_SSL
1507 fde *F = &fd_table[fd];
1508 if (F->ssl)
1509 ssl_shutdown_method(fd);
1510
1511 #endif
1512
1513 }
1514
1515 void
1516 comm_close_complete(int fd, void *data)
1517 {
1518 #if USE_SSL
1519 fde *F = &fd_table[fd];
1520
1521 if (F->ssl) {
1522 SSL_free(F->ssl);
1523 F->ssl = NULL;
1524 }
1525
1526 #endif
1527 fd_close(fd); /* update fdstat */
1528
1529 close(fd);
1530
1531 statCounter.syscalls.sock.closes++;
1532
1533 /* When an fd closes, give accept() a chance, if need be */
1534 Comm::AcceptLimiter::Instance().kick();
1535 }
1536
1537 /*
1538 * Close the socket fd.
1539 *
1540 * + call write handlers with ERR_CLOSING
1541 * + call read handlers with ERR_CLOSING
1542 * + call closing handlers
1543 *
1544 * NOTE: COMM_ERR_CLOSING will NOT be called for CommReads' sitting in a
1545 * DeferredReadManager.
1546 */
1547 void
1548 _comm_close(int fd, char const *file, int line)
1549 {
1550 debugs(5, 3, "comm_close: start closing FD " << fd);
1551 assert(fd >= 0);
1552 assert(fd < Squid_MaxFD);
1553
1554 fde *F = &fd_table[fd];
1555 fdd_table[fd].close_file = file;
1556 fdd_table[fd].close_line = line;
1557
1558 if (F->closing())
1559 return;
1560
1561 /* XXX: is this obsolete behind F->closing() ? */
1562 if ( (shutting_down || reconfiguring) && (!F->flags.open || F->type == FD_FILE))
1563 return;
1564
1565 /* The following fails because ipc.c is doing calls to pipe() to create sockets! */
1566 assert(isOpen(fd));
1567
1568 assert(F->type != FD_FILE);
1569
1570 PROF_start(comm_close);
1571
1572 F->flags.close_request = 1;
1573
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);
1580
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);
1585
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);
1590 }
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);
1594 }
1595
1596 commCallCloseHandlers(fd);
1597
1598 if (F->pconn.uses)
1599 F->pconn.pool->count(F->pconn.uses);
1600
1601 comm_empty_os_read_buffers(fd);
1602
1603
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);
1611
1612 PROF_stop(comm_close);
1613 }
1614
1615 /* Send a udp datagram to specified TO_ADDR. */
1616 int
1617 comm_udp_sendto(int fd,
1618 const Ip::Address &to_addr,
1619 const void *buf,
1620 int len)
1621 {
1622 int x = 0;
1623 struct addrinfo *AI = NULL;
1624
1625 PROF_start(comm_udp_sendto);
1626 statCounter.syscalls.sock.sendtos++;
1627
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) );
1630
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 );
1634
1635 to_addr.GetAddrInfo(AI, fd_table[fd].sock_family);
1636
1637 x = sendto(fd, buf, len, 0, AI->ai_addr, AI->ai_addrlen);
1638
1639 to_addr.FreeAddrInfo(AI);
1640
1641 PROF_stop(comm_udp_sendto);
1642
1643 if (x >= 0)
1644 return x;
1645
1646 #ifdef _SQUID_LINUX_
1647
1648 if (ECONNREFUSED != errno)
1649 #endif
1650
1651 debugs(50, 1, "comm_udp_sendto: FD " << fd << ", (family=" << fd_table[fd].sock_family << ") " << to_addr << ": " << xstrerror());
1652
1653 return COMM_ERROR;
1654 }
1655
1656 void
1657 comm_add_close_handler(int fd, PF * handler, void *data)
1658 {
1659 debugs(5, 5, "comm_add_close_handler: FD " << fd << ", handler=" <<
1660 handler << ", data=" << data);
1661
1662 AsyncCall::Pointer call=commCbCall(5,4, "SomeCloseHandler",
1663 CommCloseCbPtrFun(handler, data));
1664 comm_add_close_handler(fd, call);
1665 }
1666
1667 void
1668 comm_add_close_handler(int fd, AsyncCall::Pointer &call)
1669 {
1670 debugs(5, 5, "comm_add_close_handler: FD " << fd << ", AsyncCall=" << call);
1671
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);
1675
1676 call->setNext(fd_table[fd].closeHandler);
1677
1678 fd_table[fd].closeHandler = call;
1679 }
1680
1681
1682 // remove function-based close handler
1683 void
1684 comm_remove_close_handler(int fd, PF * handler, void *data)
1685 {
1686 assert (isOpen(fd));
1687 /* Find handler in list */
1688 debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", handler=" <<
1689 handler << ", data=" << data);
1690
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
1696 continue;
1697
1698 typedef CommCloseCbParams Params;
1699 const Params &params = GetCommParams<Params>(p);
1700 if (call->dialer.handler == handler && params.data == data)
1701 break; /* This is our handler */
1702 }
1703
1704 // comm_close removes all close handlers so our handler may be gone
1705 if (p != NULL)
1706 p->cancel("comm_remove_close_handler");
1707 // TODO: should we remove the handler from the close handlers list?
1708 }
1709
1710 // remove method-based close handler
1711 void
1712 comm_remove_close_handler(int fd, AsyncCall::Pointer &call)
1713 {
1714 assert (isOpen(fd));
1715 debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", AsyncCall=" << call);
1716
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?
1719 #if 0
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());
1724 assert(p == call);
1725 #endif
1726
1727 call->cancel("comm_remove_close_handler");
1728 }
1729
1730 static void
1731 commSetNoLinger(int fd)
1732 {
1733
1734 struct linger L;
1735 L.l_onoff = 0; /* off */
1736 L.l_linger = 0;
1737
1738 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0)
1739 debugs(50, 0, "commSetNoLinger: FD " << fd << ": " << xstrerror());
1740
1741 fd_table[fd].flags.nolinger = 1;
1742 }
1743
1744 static void
1745 commSetReuseAddr(int fd)
1746 {
1747 int on = 1;
1748
1749 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0)
1750 debugs(50, 1, "commSetReuseAddr: FD " << fd << ": " << xstrerror());
1751 }
1752
1753 static void
1754 commSetTcpRcvbuf(int fd, int size)
1755 {
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());
1763 #endif
1764 }
1765
1766 int
1767 commSetNonBlocking(int fd)
1768 {
1769 #ifndef _SQUID_MSWIN_
1770 int flags;
1771 int dummy = 0;
1772 #endif
1773 #ifdef _SQUID_WIN32_
1774
1775 int nonblocking = TRUE;
1776
1777 #ifdef _SQUID_CYGWIN_
1778
1779 if (fd_table[fd].type != FD_PIPE) {
1780 #endif
1781
1782 if (ioctl(fd, FIONBIO, &nonblocking) < 0) {
1783 debugs(50, 0, "commSetNonBlocking: FD " << fd << ": " << xstrerror() << " " << fd_table[fd].type);
1784 return COMM_ERROR;
1785 }
1786
1787 #ifdef _SQUID_CYGWIN_
1788
1789 } else {
1790 #endif
1791 #endif
1792 #ifndef _SQUID_MSWIN_
1793
1794 if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
1795 debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror());
1796 return COMM_ERROR;
1797 }
1798
1799 if (fcntl(fd, F_SETFL, flags | SQUID_NONBLOCK) < 0) {
1800 debugs(50, 0, "commSetNonBlocking: FD " << fd << ": " << xstrerror());
1801 return COMM_ERROR;
1802 }
1803
1804 #endif
1805 #ifdef _SQUID_CYGWIN_
1806
1807 }
1808
1809 #endif
1810 fd_table[fd].flags.nonblocking = 1;
1811
1812 return 0;
1813 }
1814
1815 int
1816 commUnsetNonBlocking(int fd)
1817 {
1818 #ifdef _SQUID_MSWIN_
1819 int nonblocking = FALSE;
1820
1821 if (ioctlsocket(fd, FIONBIO, (unsigned long *) &nonblocking) < 0) {
1822 #else
1823 int flags;
1824 int dummy = 0;
1825
1826 if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
1827 debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror());
1828 return COMM_ERROR;
1829 }
1830
1831 if (fcntl(fd, F_SETFL, flags & (~SQUID_NONBLOCK)) < 0) {
1832 #endif
1833 debugs(50, 0, "commUnsetNonBlocking: FD " << fd << ": " << xstrerror());
1834 return COMM_ERROR;
1835 }
1836
1837 fd_table[fd].flags.nonblocking = 0;
1838 return 0;
1839 }
1840
1841 void
1842 commSetCloseOnExec(int fd)
1843 {
1844 #ifdef FD_CLOEXEC
1845 int flags;
1846 int dummy = 0;
1847
1848 if ((flags = fcntl(fd, F_GETFD, dummy)) < 0) {
1849 debugs(50, 0, "FD " << fd << ": fcntl F_GETFD: " << xstrerror());
1850 return;
1851 }
1852
1853 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
1854 debugs(50, 0, "FD " << fd << ": set close-on-exec failed: " << xstrerror());
1855
1856 fd_table[fd].flags.close_on_exec = 1;
1857
1858 #endif
1859 }
1860
1861 #ifdef TCP_NODELAY
1862 static void
1863 commSetTcpNoDelay(int fd)
1864 {
1865 int on = 1;
1866
1867 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) < 0)
1868 debugs(50, 1, "commSetTcpNoDelay: FD " << fd << ": " << xstrerror());
1869
1870 fd_table[fd].flags.nodelay = 1;
1871 }
1872
1873 #endif
1874
1875 void
1876 commSetTcpKeepalive(int fd, int idle, int interval, int timeout)
1877 {
1878 int on = 1;
1879 #ifdef TCP_KEEPCNT
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());
1884 }
1885 #endif
1886 #ifdef TCP_KEEPIDLE
1887 if (idle) {
1888 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(on)) < 0)
1889 debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror());
1890 }
1891 #endif
1892 #ifdef TCP_KEEPINTVL
1893 if (interval) {
1894 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(on)) < 0)
1895 debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror());
1896 }
1897 #endif
1898 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &on, sizeof(on)) < 0)
1899 debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror());
1900 }
1901
1902 void
1903 comm_init(void)
1904 {
1905 fd_table =(fde *) xcalloc(Squid_MaxFD, sizeof(fde));
1906 fdd_table = (fd_debug_t *)xcalloc(Squid_MaxFD, sizeof(fd_debug_t));
1907
1908 /* make sure the accept() socket FIFO delay queue exists */
1909 Comm::AcceptLimiter::Instance();
1910
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;
1918 }
1919
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);
1925
1926 conn_close_pool = memPoolCreate("close_handler", sizeof(close_handler));
1927
1928 TheHalfClosed = new DescriptorSet;
1929 }
1930
1931 void
1932 comm_exit(void)
1933 {
1934 delete TheHalfClosed;
1935 TheHalfClosed = NULL;
1936
1937 safe_free(fd_table);
1938 safe_free(fdd_table);
1939 safe_free(commfd_table);
1940 }
1941
1942 /* Write to FD. */
1943 static void
1944 commHandleWrite(int fd, void *data)
1945 {
1946 comm_io_callback_t *state = (comm_io_callback_t *)data;
1947 int len = 0;
1948 int nleft;
1949
1950 assert(state == COMMIO_FD_WRITECB(fd));
1951
1952 PROF_start(commHandleWrite);
1953 debugs(5, 5, "commHandleWrite: FD " << fd << ": off " <<
1954 (long int) state->offset << ", sz " << (long int) state->size << ".");
1955
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;
1964
1965 if (len == 0) {
1966 /* Note we even call write if nleft == 0 */
1967 /* We're done */
1968
1969 if (nleft != 0)
1970 debugs(5, 1, "commHandleWrite: FD " << fd << ": write failure: connection closed with " << nleft << " bytes remaining.");
1971
1972 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_ERROR : COMM_OK, errno);
1973 } else if (len < 0) {
1974 /* An error */
1975
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() << ".");
1981 commSetSelect(fd,
1982 COMM_SELECT_WRITE,
1983 commHandleWrite,
1984 state,
1985 0);
1986 } else {
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);
1989 }
1990 } else {
1991 /* A successful write, continue */
1992 state->offset += len;
1993
1994 if (state->offset < state->size) {
1995 /* Not done, reinstall the write handler and write some more */
1996 commSetSelect(fd,
1997 COMM_SELECT_WRITE,
1998 commHandleWrite,
1999 state,
2000 0);
2001 } else {
2002 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_OK : COMM_ERROR, errno);
2003 }
2004 }
2005
2006 PROF_stop(commHandleWrite);
2007 }
2008
2009 /*
2010 * Queue a write. handler/handler_data are called when the write
2011 * completes, on error, or on file descriptor close.
2012 *
2013 * free_func is used to free the passed buffer when the write has completed.
2014 */
2015 void
2016 comm_write(int fd, const char *buf, int size, IOCB * handler, void *handler_data, FREE * free_func)
2017 {
2018 AsyncCall::Pointer call = commCbCall(5,5, "SomeCommWriteHander",
2019 CommIoCbPtrFun(handler, handler_data));
2020
2021 comm_write(fd, buf, size, call, free_func);
2022 }
2023
2024 void
2025 comm_write(int fd, const char *buf, int size, AsyncCall::Pointer &callback, FREE * free_func)
2026 {
2027 debugs(5, 5, "comm_write: FD " << fd << ": sz " << size << ": asynCall " << callback);
2028
2029 /* Make sure we are open, not closing, and not writing */
2030 assert(isOpen(fd));
2031 assert(!fd_table[fd].closing());
2032 comm_io_callback_t *ccb = COMMIO_FD_WRITECB(fd);
2033 assert(!ccb->active());
2034
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);
2040 }
2041
2042
2043 /* a wrapper around comm_write to allow for MemBuf to be comm_written in a snap */
2044 void
2045 comm_write_mbuf(int fd, MemBuf *mb, IOCB * handler, void *handler_data)
2046 {
2047 comm_write(fd, mb->buf, mb->size, handler, handler_data, mb->freeFunc());
2048 }
2049
2050 void
2051 comm_write_mbuf(int fd, MemBuf *mb, AsyncCall::Pointer &callback)
2052 {
2053 comm_write(fd, mb->buf, mb->size, callback, mb->freeFunc());
2054 }
2055
2056
2057 /*
2058 * hm, this might be too general-purpose for all the places we'd
2059 * like to use it.
2060 */
2061 int
2062 ignoreErrno(int ierrno)
2063 {
2064 switch (ierrno) {
2065
2066 case EINPROGRESS:
2067
2068 case EWOULDBLOCK:
2069 #if EAGAIN != EWOULDBLOCK
2070
2071 case EAGAIN:
2072 #endif
2073
2074 case EALREADY:
2075
2076 case EINTR:
2077 #ifdef ERESTART
2078
2079 case ERESTART:
2080 #endif
2081
2082 return 1;
2083
2084 default:
2085 return 0;
2086 }
2087
2088 /* NOTREACHED */
2089 }
2090
2091 void
2092 commCloseAllSockets(void)
2093 {
2094 int fd;
2095 fde *F = NULL;
2096
2097 for (fd = 0; fd <= Biggest_FD; fd++) {
2098 F = &fd_table[fd];
2099
2100 if (!F->flags.open)
2101 continue;
2102
2103 if (F->type != FD_SOCKET)
2104 continue;
2105
2106 if (F->flags.ipc) /* don't close inter-process sockets */
2107 continue;
2108
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);
2114 } else {
2115 debugs(5, 5, "commCloseAllSockets: FD " << fd << ": calling comm_reset_close()");
2116 comm_reset_close(fd);
2117 }
2118 }
2119 }
2120
2121 static bool
2122 AlreadyTimedOut(fde *F)
2123 {
2124 if (!F->flags.open)
2125 return true;
2126
2127 if (F->timeout == 0)
2128 return true;
2129
2130 if (F->timeout > squid_curtime)
2131 return true;
2132
2133 return false;
2134 }
2135
2136 static bool
2137 writeTimedOut(int fd)
2138 {
2139 if (!commio_has_callback(fd, IOCB_WRITE, COMMIO_FD_WRITECB(fd)))
2140 return false;
2141
2142 if ((squid_curtime - fd_table[fd].writeStart) < Config.Timeout.write)
2143 return false;
2144
2145 return true;
2146 }
2147
2148 void
2149 checkTimeouts(void)
2150 {
2151 int fd;
2152 fde *F = NULL;
2153 AsyncCall::Pointer callback;
2154
2155 for (fd = 0; fd <= Biggest_FD; fd++) {
2156 F = &fd_table[fd];
2157
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))
2164 continue;
2165
2166 debugs(5, 5, "checkTimeouts: FD " << fd << " Expired");
2167
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);
2173 } else {
2174 debugs(5, 5, "checkTimeouts: FD " << fd << ": Forcing comm_close()");
2175 comm_close(fd);
2176 }
2177 }
2178 }
2179
2180 void CommIO::Initialise()
2181 {
2182 /* Initialize done pipe signal */
2183 int DonePipe[2];
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);
2192 Initialised = true;
2193 }
2194
2195 void CommIO::NotifyIOClose()
2196 {
2197 /* Close done pipe signal */
2198 FlushPipe();
2199 close(DoneFD);
2200 close(DoneReadFD);
2201 fd_close(DoneFD);
2202 fd_close(DoneReadFD);
2203 Initialised = false;
2204 }
2205
2206 bool CommIO::Initialised = false;
2207 bool CommIO::DoneSignalled = false;
2208 int CommIO::DoneFD = -1;
2209 int CommIO::DoneReadFD = -1;
2210
2211 void
2212 CommIO::FlushPipe()
2213 {
2214 char buf[256];
2215 FD_READ_METHOD(DoneReadFD, buf, sizeof(buf));
2216 }
2217
2218 void
2219 CommIO::NULLFDHandler(int fd, void *data)
2220 {
2221 FlushPipe();
2222 commSetSelect(fd, COMM_SELECT_READ, NULLFDHandler, NULL, 0);
2223 }
2224
2225 void
2226 CommIO::ResetNotifications()
2227 {
2228 if (DoneSignalled) {
2229 FlushPipe();
2230 DoneSignalled = false;
2231 }
2232 }
2233
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.
2237 void
2238 commStartHalfClosedMonitor(int fd)
2239 {
2240 debugs(5, 5, HERE << "adding FD " << fd << " to " << *TheHalfClosed);
2241 assert(isOpen(fd));
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
2245 }
2246
2247 static
2248 void
2249 commPlanHalfClosedCheck()
2250 {
2251 if (!WillCheckHalfClosed && !TheHalfClosed->empty()) {
2252 eventAdd("commHalfClosedCheck", &commHalfClosedCheck, NULL, 1.0, 1);
2253 WillCheckHalfClosed = true;
2254 }
2255 }
2256
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
2259 static
2260 void
2261 commHalfClosedCheck(void *)
2262 {
2263 debugs(5, 5, HERE << "checking " << *TheHalfClosed);
2264
2265 typedef DescriptorSet::const_iterator DSCI;
2266 const DSCI end = TheHalfClosed->end();
2267 for (DSCI i = TheHalfClosed->begin(); i != end; ++i) {
2268 const int fd = *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;
2274 }
2275 }
2276
2277 WillCheckHalfClosed = false; // as far as we know
2278 commPlanHalfClosedCheck(); // may need to check again
2279 }
2280
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.
2283 bool
2284 commHasHalfClosedMonitor(int fd)
2285 {
2286 return TheHalfClosed->has(fd);
2287 }
2288
2289 /// stop waiting for possibly half-closed connection to close
2290 static void
2291 commStopHalfClosedMonitor(int const fd)
2292 {
2293 debugs(5, 5, HERE << "removing FD " << fd << " from " << *TheHalfClosed);
2294
2295 // cancel the read if one was scheduled
2296 AsyncCall::Pointer reader = fd_table[fd].halfClosedReader;
2297 if (reader != NULL)
2298 comm_read_cancel(fd, reader);
2299 fd_table[fd].halfClosedReader = NULL;
2300
2301 TheHalfClosed->del(fd);
2302 }
2303
2304 /// I/O handler for the possibly half-closed connection monitoring code
2305 static void
2306 commHalfClosedReader(int fd, char *, size_t size, comm_err_t flag, int, void *)
2307 {
2308 // there cannot be more data coming in on half-closed connections
2309 assert(size == 0);
2310 assert(commHasHalfClosedMonitor(fd)); // or we would have canceled the read
2311
2312 fd_table[fd].halfClosedReader = NULL; // done reading, for now
2313
2314 // nothing to do if fd is being closed
2315 if (flag == COMM_ERR_CLOSING)
2316 return;
2317
2318 // if read failed, close the connection
2319 if (flag != COMM_OK) {
2320 debugs(5, 3, "commHalfClosedReader: closing FD " << fd);
2321 comm_close(fd);
2322 return;
2323 }
2324
2325 // continue waiting for close or error
2326 commPlanHalfClosedCheck(); // make sure this fd will be checked again
2327 }
2328
2329
2330 CommRead::CommRead() : fd(-1), buf(NULL), len(0), callback(NULL) {}
2331
2332 CommRead::CommRead(int fd_, char *buf_, int len_, AsyncCall::Pointer &callback_)
2333 : fd(fd_), buf(buf_), len(len_), callback(callback_) {}
2334
2335 DeferredRead::DeferredRead () : theReader(NULL), theContext(NULL), theRead(), cancelled(false) {}
2336
2337 DeferredRead::DeferredRead (DeferrableRead *aReader, void *data, CommRead const &aRead) : theReader(aReader), theContext (data), theRead(aRead), cancelled(false) {}
2338
2339 DeferredReadManager::~DeferredReadManager()
2340 {
2341 flushReads();
2342 assert (deferredReads.empty());
2343 }
2344
2345 /* explicit instantiation required for some systems */
2346
2347 /// \cond AUTODOCS-IGNORE
2348 template cbdata_type CbDataList<DeferredRead>::CBDATA_CbDataList;
2349 /// \endcond
2350
2351 void
2352 DeferredReadManager::delayRead(DeferredRead const &aRead)
2353 {
2354 debugs(5, 3, "Adding deferred read on FD " << aRead.theRead.fd);
2355 CbDataList<DeferredRead> *temp = deferredReads.push_back(aRead);
2356
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
2365 }
2366
2367 void
2368 DeferredReadManager::CloseHandler(int fd, void *thecbdata)
2369 {
2370 if (!cbdataReferenceValid (thecbdata))
2371 return;
2372
2373 CbDataList<DeferredRead> *temp = (CbDataList<DeferredRead> *)thecbdata;
2374
2375 temp->element.closer = NULL;
2376 temp->element.markCancelled();
2377 }
2378
2379 DeferredRead
2380 DeferredReadManager::popHead(CbDataListContainer<DeferredRead> &deferredReads)
2381 {
2382 assert (!deferredReads.empty());
2383
2384 DeferredRead &read = deferredReads.head->element;
2385 if (!read.cancelled) {
2386 comm_remove_close_handler(read.theRead.fd, read.closer);
2387 read.closer = NULL;
2388 }
2389
2390 DeferredRead result = deferredReads.pop_front();
2391
2392 return result;
2393 }
2394
2395 void
2396 DeferredReadManager::kickReads(int const count)
2397 {
2398 /* if we had CbDataList::size() we could consolidate this and flushReads */
2399
2400 if (count < 1) {
2401 flushReads();
2402 return;
2403 }
2404
2405 size_t remaining = count;
2406
2407 while (!deferredReads.empty() && remaining) {
2408 DeferredRead aRead = popHead(deferredReads);
2409 kickARead(aRead);
2410
2411 if (!aRead.cancelled)
2412 --remaining;
2413 }
2414 }
2415
2416 void
2417 DeferredReadManager::flushReads()
2418 {
2419 CbDataListContainer<DeferredRead> reads;
2420 reads = deferredReads;
2421 deferredReads = CbDataListContainer<DeferredRead>();
2422
2423 // XXX: For fairness this SHOULD randomize the order
2424 while (!reads.empty()) {
2425 DeferredRead aRead = popHead(reads);
2426 kickARead(aRead);
2427 }
2428 }
2429
2430 void
2431 DeferredReadManager::kickARead(DeferredRead const &aRead)
2432 {
2433 if (aRead.cancelled)
2434 return;
2435
2436 if (aRead.theRead.fd>=0 && fd_table[aRead.theRead.fd].closing())
2437 return;
2438
2439 debugs(5, 3, "Kicking deferred read on FD " << aRead.theRead.fd);
2440
2441 aRead.theReader(aRead.theContext, aRead.theRead);
2442 }
2443
2444 void
2445 DeferredRead::markCancelled()
2446 {
2447 cancelled = true;
2448 }
2449
2450 ConnectionDetail::ConnectionDetail() : me(), peer()
2451 {
2452 }
2453
2454 int
2455 CommSelectEngine::checkEvents(int timeout)
2456 {
2457 static time_t last_timeout = 0;
2458
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;
2462 checkTimeouts();
2463 }
2464
2465 switch (comm_select(timeout)) {
2466
2467 case COMM_OK:
2468
2469 case COMM_TIMEOUT:
2470 return 0;
2471
2472 case COMM_IDLE:
2473
2474 case COMM_SHUTDOWN:
2475 return EVENT_IDLE;
2476
2477 case COMM_ERROR:
2478 return EVENT_ERROR;
2479
2480 default:
2481 fatal_dump("comm.cc: Internal error -- this should never happen.");
2482 return EVENT_ERROR;
2483 };
2484 }
2485
2486 /// Create a unix-domain socket (UDS) that only supports FD_MSGHDR I/O.
2487 int
2488 comm_open_uds(int sock_type,
2489 int proto,
2490 struct sockaddr_un* addr,
2491 int flags)
2492 {
2493 // TODO: merge with comm_openex() when Ip::Address becomes NetAddress
2494
2495 int new_socket;
2496
2497 PROF_start(comm_open);
2498 /* Create socket for accepting new connections. */
2499 statCounter.syscalls.sock.sockets++;
2500
2501 /* Setup the socket addrinfo details for use */
2502 struct addrinfo AI;
2503 AI.ai_flags = 0;
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;
2510 AI.ai_next = NULL;
2511
2512 debugs(50, 3, HERE << "Attempt open socket for: " << addr->sun_path);
2513
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 */
2518
2519 if (limitError(errno)) {
2520 debugs(50, DBG_IMPORTANT, HERE << "socket failure: " << xstrerror());
2521 fdAdjustReserved();
2522 } else {
2523 debugs(50, DBG_CRITICAL, HERE << "socket failure: " << xstrerror());
2524 }
2525
2526 PROF_stop(comm_open);
2527 return -1;
2528 }
2529
2530 debugs(50, 3, HERE "Opened UDS FD " << new_socket << " : family=" << AI.ai_family << ", type=" << AI.ai_socktype << ", protocol=" << AI.ai_protocol);
2531
2532 /* update fdstat */
2533 debugs(50, 5, HERE << "FD " << new_socket << " is a new socket");
2534
2535 assert(!isOpen(new_socket));
2536 fd_open(new_socket, FD_MSGHDR, NULL);
2537
2538 fdd_table[new_socket].close_file = NULL;
2539
2540 fdd_table[new_socket].close_line = 0;
2541
2542 fd_table[new_socket].sock_family = AI.ai_family;
2543
2544 if (!(flags & COMM_NOCLOEXEC))
2545 commSetCloseOnExec(new_socket);
2546
2547 if (flags & COMM_REUSEADDR)
2548 commSetReuseAddr(new_socket);
2549
2550 if (flags & COMM_NONBLOCKING) {
2551 if (commSetNonBlocking(new_socket) != COMM_OK) {
2552 comm_close(new_socket);
2553 PROF_stop(comm_open);
2554 return -1;
2555 }
2556 }
2557
2558 if (flags & COMM_DOBIND) {
2559 if (commBind(new_socket, AI) != COMM_OK) {
2560 comm_close(new_socket);
2561 PROF_stop(comm_open);
2562 return -1;
2563 }
2564 }
2565
2566 #ifdef TCP_NODELAY
2567 if (sock_type == SOCK_STREAM)
2568 commSetTcpNoDelay(new_socket);
2569
2570 #endif
2571
2572 if (Config.tcpRcvBufsz > 0 && sock_type == SOCK_STREAM)
2573 commSetTcpRcvbuf(new_socket, Config.tcpRcvBufsz);
2574
2575 PROF_stop(comm_open);
2576
2577 return new_socket;
2578 }