]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm.cc
Potential bug #3015 fix: assertion failed: comm.cc:143: "ccb->active()"
[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
1587 if (commio_has_callback(fd, IOCB_WRITE, COMMIO_FD_WRITECB(fd))) {
1588 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), COMM_ERR_CLOSING, errno);
1589 }
1590 if (commio_has_callback(fd, IOCB_READ, COMMIO_FD_READCB(fd))) {
1591 commio_finish_callback(fd, COMMIO_FD_READCB(fd), COMM_ERR_CLOSING, errno);
1592 }
1593
1594 commCallCloseHandlers(fd);
1595
1596 if (F->pconn.uses)
1597 F->pconn.pool->count(F->pconn.uses);
1598
1599 comm_empty_os_read_buffers(fd);
1600
1601
1602 AsyncCall::Pointer completeCall=commCbCall(5,4, "comm_close_complete",
1603 CommCloseCbPtrFun(comm_close_complete, NULL));
1604 Params &completeParams = GetCommParams<Params>(completeCall);
1605 completeParams.fd = fd;
1606 // must use async call to wait for all callbacks
1607 // scheduled before comm_close() to finish
1608 ScheduleCallHere(completeCall);
1609
1610 PROF_stop(comm_close);
1611 }
1612
1613 /* Send a udp datagram to specified TO_ADDR. */
1614 int
1615 comm_udp_sendto(int fd,
1616 const Ip::Address &to_addr,
1617 const void *buf,
1618 int len)
1619 {
1620 int x = 0;
1621 struct addrinfo *AI = NULL;
1622
1623 PROF_start(comm_udp_sendto);
1624 statCounter.syscalls.sock.sendtos++;
1625
1626 debugs(50, 3, "comm_udp_sendto: Attempt to send UDP packet to " << to_addr <<
1627 " using FD " << fd << " using Port " << comm_local_port(fd) );
1628
1629 /* BUG: something in the above macro appears to occasionally be setting AI to garbage. */
1630 /* AYJ: 2007-08-27 : or was it because I wasn't then setting 'fd_table[fd].sock_family' to fill properly. */
1631 assert( NULL == AI );
1632
1633 to_addr.GetAddrInfo(AI, fd_table[fd].sock_family);
1634
1635 x = sendto(fd, buf, len, 0, AI->ai_addr, AI->ai_addrlen);
1636
1637 to_addr.FreeAddrInfo(AI);
1638
1639 PROF_stop(comm_udp_sendto);
1640
1641 if (x >= 0)
1642 return x;
1643
1644 #ifdef _SQUID_LINUX_
1645
1646 if (ECONNREFUSED != errno)
1647 #endif
1648
1649 debugs(50, 1, "comm_udp_sendto: FD " << fd << ", (family=" << fd_table[fd].sock_family << ") " << to_addr << ": " << xstrerror());
1650
1651 return COMM_ERROR;
1652 }
1653
1654 void
1655 comm_add_close_handler(int fd, PF * handler, void *data)
1656 {
1657 debugs(5, 5, "comm_add_close_handler: FD " << fd << ", handler=" <<
1658 handler << ", data=" << data);
1659
1660 AsyncCall::Pointer call=commCbCall(5,4, "SomeCloseHandler",
1661 CommCloseCbPtrFun(handler, data));
1662 comm_add_close_handler(fd, call);
1663 }
1664
1665 void
1666 comm_add_close_handler(int fd, AsyncCall::Pointer &call)
1667 {
1668 debugs(5, 5, "comm_add_close_handler: FD " << fd << ", AsyncCall=" << call);
1669
1670 /*TODO:Check for a similar scheduled AsyncCall*/
1671 // for (c = fd_table[fd].closeHandler; c; c = c->next)
1672 // assert(c->handler != handler || c->data != data);
1673
1674 call->setNext(fd_table[fd].closeHandler);
1675
1676 fd_table[fd].closeHandler = call;
1677 }
1678
1679
1680 // remove function-based close handler
1681 void
1682 comm_remove_close_handler(int fd, PF * handler, void *data)
1683 {
1684 assert (isOpen(fd));
1685 /* Find handler in list */
1686 debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", handler=" <<
1687 handler << ", data=" << data);
1688
1689 AsyncCall::Pointer p;
1690 for (p = fd_table[fd].closeHandler; p != NULL; p = p->Next()) {
1691 typedef CommCbFunPtrCallT<CommCloseCbPtrFun> Call;
1692 const Call *call = dynamic_cast<const Call*>(p.getRaw());
1693 if (!call) // method callbacks have their own comm_remove_close_handler
1694 continue;
1695
1696 typedef CommCloseCbParams Params;
1697 const Params &params = GetCommParams<Params>(p);
1698 if (call->dialer.handler == handler && params.data == data)
1699 break; /* This is our handler */
1700 }
1701
1702 // comm_close removes all close handlers so our handler may be gone
1703 if (p != NULL)
1704 p->cancel("comm_remove_close_handler");
1705 // TODO: should we remove the handler from the close handlers list?
1706 }
1707
1708 // remove method-based close handler
1709 void
1710 comm_remove_close_handler(int fd, AsyncCall::Pointer &call)
1711 {
1712 assert (isOpen(fd));
1713 debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", AsyncCall=" << call);
1714
1715 // comm_close removes all close handlers so our handler may be gone
1716 // TODO: should we remove the handler from the close handlers list?
1717 #if 0
1718 // Check to see if really exist the given AsyncCall in comm_close handlers
1719 // TODO: optimize: this slow code is only needed for the assert() below
1720 AsyncCall::Pointer p;
1721 for (p = fd_table[fd].closeHandler; p != NULL && p != call; p = p->Next());
1722 assert(p == call);
1723 #endif
1724
1725 call->cancel("comm_remove_close_handler");
1726 }
1727
1728 static void
1729 commSetNoLinger(int fd)
1730 {
1731
1732 struct linger L;
1733 L.l_onoff = 0; /* off */
1734 L.l_linger = 0;
1735
1736 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0)
1737 debugs(50, 0, "commSetNoLinger: FD " << fd << ": " << xstrerror());
1738
1739 fd_table[fd].flags.nolinger = 1;
1740 }
1741
1742 static void
1743 commSetReuseAddr(int fd)
1744 {
1745 int on = 1;
1746
1747 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0)
1748 debugs(50, 1, "commSetReuseAddr: FD " << fd << ": " << xstrerror());
1749 }
1750
1751 static void
1752 commSetTcpRcvbuf(int fd, int size)
1753 {
1754 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size)) < 0)
1755 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd << ", SIZE " << size << ": " << xstrerror());
1756 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size)) < 0)
1757 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd << ", SIZE " << size << ": " << xstrerror());
1758 #ifdef TCP_WINDOW_CLAMP
1759 if (setsockopt(fd, SOL_TCP, TCP_WINDOW_CLAMP, (char *) &size, sizeof(size)) < 0)
1760 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd << ", SIZE " << size << ": " << xstrerror());
1761 #endif
1762 }
1763
1764 int
1765 commSetNonBlocking(int fd)
1766 {
1767 #ifndef _SQUID_MSWIN_
1768 int flags;
1769 int dummy = 0;
1770 #endif
1771 #ifdef _SQUID_WIN32_
1772
1773 int nonblocking = TRUE;
1774
1775 #ifdef _SQUID_CYGWIN_
1776
1777 if (fd_table[fd].type != FD_PIPE) {
1778 #endif
1779
1780 if (ioctl(fd, FIONBIO, &nonblocking) < 0) {
1781 debugs(50, 0, "commSetNonBlocking: FD " << fd << ": " << xstrerror() << " " << fd_table[fd].type);
1782 return COMM_ERROR;
1783 }
1784
1785 #ifdef _SQUID_CYGWIN_
1786
1787 } else {
1788 #endif
1789 #endif
1790 #ifndef _SQUID_MSWIN_
1791
1792 if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
1793 debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror());
1794 return COMM_ERROR;
1795 }
1796
1797 if (fcntl(fd, F_SETFL, flags | SQUID_NONBLOCK) < 0) {
1798 debugs(50, 0, "commSetNonBlocking: FD " << fd << ": " << xstrerror());
1799 return COMM_ERROR;
1800 }
1801
1802 #endif
1803 #ifdef _SQUID_CYGWIN_
1804
1805 }
1806
1807 #endif
1808 fd_table[fd].flags.nonblocking = 1;
1809
1810 return 0;
1811 }
1812
1813 int
1814 commUnsetNonBlocking(int fd)
1815 {
1816 #ifdef _SQUID_MSWIN_
1817 int nonblocking = FALSE;
1818
1819 if (ioctlsocket(fd, FIONBIO, (unsigned long *) &nonblocking) < 0) {
1820 #else
1821 int flags;
1822 int dummy = 0;
1823
1824 if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
1825 debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror());
1826 return COMM_ERROR;
1827 }
1828
1829 if (fcntl(fd, F_SETFL, flags & (~SQUID_NONBLOCK)) < 0) {
1830 #endif
1831 debugs(50, 0, "commUnsetNonBlocking: FD " << fd << ": " << xstrerror());
1832 return COMM_ERROR;
1833 }
1834
1835 fd_table[fd].flags.nonblocking = 0;
1836 return 0;
1837 }
1838
1839 void
1840 commSetCloseOnExec(int fd)
1841 {
1842 #ifdef FD_CLOEXEC
1843 int flags;
1844 int dummy = 0;
1845
1846 if ((flags = fcntl(fd, F_GETFD, dummy)) < 0) {
1847 debugs(50, 0, "FD " << fd << ": fcntl F_GETFD: " << xstrerror());
1848 return;
1849 }
1850
1851 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
1852 debugs(50, 0, "FD " << fd << ": set close-on-exec failed: " << xstrerror());
1853
1854 fd_table[fd].flags.close_on_exec = 1;
1855
1856 #endif
1857 }
1858
1859 #ifdef TCP_NODELAY
1860 static void
1861 commSetTcpNoDelay(int fd)
1862 {
1863 int on = 1;
1864
1865 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) < 0)
1866 debugs(50, 1, "commSetTcpNoDelay: FD " << fd << ": " << xstrerror());
1867
1868 fd_table[fd].flags.nodelay = 1;
1869 }
1870
1871 #endif
1872
1873 void
1874 commSetTcpKeepalive(int fd, int idle, int interval, int timeout)
1875 {
1876 int on = 1;
1877 #ifdef TCP_KEEPCNT
1878 if (timeout && interval) {
1879 int count = (timeout + interval - 1) / interval;
1880 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &count, sizeof(on)) < 0)
1881 debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror());
1882 }
1883 #endif
1884 #ifdef TCP_KEEPIDLE
1885 if (idle) {
1886 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(on)) < 0)
1887 debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror());
1888 }
1889 #endif
1890 #ifdef TCP_KEEPINTVL
1891 if (interval) {
1892 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(on)) < 0)
1893 debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror());
1894 }
1895 #endif
1896 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &on, sizeof(on)) < 0)
1897 debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror());
1898 }
1899
1900 void
1901 comm_init(void)
1902 {
1903 fd_table =(fde *) xcalloc(Squid_MaxFD, sizeof(fde));
1904 fdd_table = (fd_debug_t *)xcalloc(Squid_MaxFD, sizeof(fd_debug_t));
1905
1906 /* make sure the accept() socket FIFO delay queue exists */
1907 Comm::AcceptLimiter::Instance();
1908
1909 commfd_table = (comm_fd_t *) xcalloc(Squid_MaxFD, sizeof(comm_fd_t));
1910 for (int pos = 0; pos < Squid_MaxFD; pos++) {
1911 commfd_table[pos].fd = pos;
1912 commfd_table[pos].readcb.fd = pos;
1913 commfd_table[pos].readcb.type = IOCB_READ;
1914 commfd_table[pos].writecb.fd = pos;
1915 commfd_table[pos].writecb.type = IOCB_WRITE;
1916 }
1917
1918 /* XXX account fd_table */
1919 /* Keep a few file descriptors free so that we don't run out of FD's
1920 * after accepting a client but before it opens a socket or a file.
1921 * Since Squid_MaxFD can be as high as several thousand, don't waste them */
1922 RESERVED_FD = min(100, Squid_MaxFD / 4);
1923
1924 conn_close_pool = memPoolCreate("close_handler", sizeof(close_handler));
1925
1926 TheHalfClosed = new DescriptorSet;
1927 }
1928
1929 void
1930 comm_exit(void)
1931 {
1932 delete TheHalfClosed;
1933 TheHalfClosed = NULL;
1934
1935 safe_free(fd_table);
1936 safe_free(fdd_table);
1937 safe_free(commfd_table);
1938 }
1939
1940 /* Write to FD. */
1941 static void
1942 commHandleWrite(int fd, void *data)
1943 {
1944 comm_io_callback_t *state = (comm_io_callback_t *)data;
1945 int len = 0;
1946 int nleft;
1947
1948 assert(state == COMMIO_FD_WRITECB(fd));
1949
1950 PROF_start(commHandleWrite);
1951 debugs(5, 5, "commHandleWrite: FD " << fd << ": off " <<
1952 (long int) state->offset << ", sz " << (long int) state->size << ".");
1953
1954 nleft = state->size - state->offset;
1955 len = FD_WRITE_METHOD(fd, state->buf + state->offset, nleft);
1956 debugs(5, 5, "commHandleWrite: write() returns " << len);
1957 fd_bytes(fd, len, FD_WRITE);
1958 statCounter.syscalls.sock.writes++;
1959 // After each successful partial write,
1960 // reset fde::writeStart to the current time.
1961 fd_table[fd].writeStart = squid_curtime;
1962
1963 if (len == 0) {
1964 /* Note we even call write if nleft == 0 */
1965 /* We're done */
1966
1967 if (nleft != 0)
1968 debugs(5, 1, "commHandleWrite: FD " << fd << ": write failure: connection closed with " << nleft << " bytes remaining.");
1969
1970 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_ERROR : COMM_OK, errno);
1971 } else if (len < 0) {
1972 /* An error */
1973
1974 if (fd_table[fd].flags.socket_eof) {
1975 debugs(50, 2, "commHandleWrite: FD " << fd << ": write failure: " << xstrerror() << ".");
1976 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_ERROR : COMM_OK, errno);
1977 } else if (ignoreErrno(errno)) {
1978 debugs(50, 10, "commHandleWrite: FD " << fd << ": write failure: " << xstrerror() << ".");
1979 commSetSelect(fd,
1980 COMM_SELECT_WRITE,
1981 commHandleWrite,
1982 state,
1983 0);
1984 } else {
1985 debugs(50, 2, "commHandleWrite: FD " << fd << ": write failure: " << xstrerror() << ".");
1986 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_ERROR : COMM_OK, errno);
1987 }
1988 } else {
1989 /* A successful write, continue */
1990 state->offset += len;
1991
1992 if (state->offset < state->size) {
1993 /* Not done, reinstall the write handler and write some more */
1994 commSetSelect(fd,
1995 COMM_SELECT_WRITE,
1996 commHandleWrite,
1997 state,
1998 0);
1999 } else {
2000 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_OK : COMM_ERROR, errno);
2001 }
2002 }
2003
2004 PROF_stop(commHandleWrite);
2005 }
2006
2007 /*
2008 * Queue a write. handler/handler_data are called when the write
2009 * completes, on error, or on file descriptor close.
2010 *
2011 * free_func is used to free the passed buffer when the write has completed.
2012 */
2013 void
2014 comm_write(int fd, const char *buf, int size, IOCB * handler, void *handler_data, FREE * free_func)
2015 {
2016 AsyncCall::Pointer call = commCbCall(5,5, "SomeCommWriteHander",
2017 CommIoCbPtrFun(handler, handler_data));
2018
2019 comm_write(fd, buf, size, call, free_func);
2020 }
2021
2022 void
2023 comm_write(int fd, const char *buf, int size, AsyncCall::Pointer &callback, FREE * free_func)
2024 {
2025 debugs(5, 5, "comm_write: FD " << fd << ": sz " << size << ": asynCall " << callback);
2026
2027 /* Make sure we are open, not closing, and not writing */
2028 assert(isOpen(fd));
2029 assert(!fd_table[fd].closing());
2030 comm_io_callback_t *ccb = COMMIO_FD_WRITECB(fd);
2031 assert(!ccb->active());
2032
2033 fd_table[fd].writeStart = squid_curtime;
2034 /* Queue the write */
2035 commio_set_callback(fd, IOCB_WRITE, ccb, callback,
2036 (char *)buf, free_func, size);
2037 commSetSelect(fd, COMM_SELECT_WRITE, commHandleWrite, ccb, 0);
2038 }
2039
2040
2041 /* a wrapper around comm_write to allow for MemBuf to be comm_written in a snap */
2042 void
2043 comm_write_mbuf(int fd, MemBuf *mb, IOCB * handler, void *handler_data)
2044 {
2045 comm_write(fd, mb->buf, mb->size, handler, handler_data, mb->freeFunc());
2046 }
2047
2048 void
2049 comm_write_mbuf(int fd, MemBuf *mb, AsyncCall::Pointer &callback)
2050 {
2051 comm_write(fd, mb->buf, mb->size, callback, mb->freeFunc());
2052 }
2053
2054
2055 /*
2056 * hm, this might be too general-purpose for all the places we'd
2057 * like to use it.
2058 */
2059 int
2060 ignoreErrno(int ierrno)
2061 {
2062 switch (ierrno) {
2063
2064 case EINPROGRESS:
2065
2066 case EWOULDBLOCK:
2067 #if EAGAIN != EWOULDBLOCK
2068
2069 case EAGAIN:
2070 #endif
2071
2072 case EALREADY:
2073
2074 case EINTR:
2075 #ifdef ERESTART
2076
2077 case ERESTART:
2078 #endif
2079
2080 return 1;
2081
2082 default:
2083 return 0;
2084 }
2085
2086 /* NOTREACHED */
2087 }
2088
2089 void
2090 commCloseAllSockets(void)
2091 {
2092 int fd;
2093 fde *F = NULL;
2094
2095 for (fd = 0; fd <= Biggest_FD; fd++) {
2096 F = &fd_table[fd];
2097
2098 if (!F->flags.open)
2099 continue;
2100
2101 if (F->type != FD_SOCKET)
2102 continue;
2103
2104 if (F->flags.ipc) /* don't close inter-process sockets */
2105 continue;
2106
2107 if (F->timeoutHandler != NULL) {
2108 AsyncCall::Pointer callback = F->timeoutHandler;
2109 F->timeoutHandler = NULL;
2110 debugs(5, 5, "commCloseAllSockets: FD " << fd << ": Calling timeout handler");
2111 ScheduleCallHere(callback);
2112 } else {
2113 debugs(5, 5, "commCloseAllSockets: FD " << fd << ": calling comm_reset_close()");
2114 comm_reset_close(fd);
2115 }
2116 }
2117 }
2118
2119 static bool
2120 AlreadyTimedOut(fde *F)
2121 {
2122 if (!F->flags.open)
2123 return true;
2124
2125 if (F->timeout == 0)
2126 return true;
2127
2128 if (F->timeout > squid_curtime)
2129 return true;
2130
2131 return false;
2132 }
2133
2134 static bool
2135 writeTimedOut(int fd)
2136 {
2137 if (!commio_has_callback(fd, IOCB_WRITE, COMMIO_FD_WRITECB(fd)))
2138 return false;
2139
2140 if ((squid_curtime - fd_table[fd].writeStart) < Config.Timeout.write)
2141 return false;
2142
2143 return true;
2144 }
2145
2146 void
2147 checkTimeouts(void)
2148 {
2149 int fd;
2150 fde *F = NULL;
2151 AsyncCall::Pointer callback;
2152
2153 for (fd = 0; fd <= Biggest_FD; fd++) {
2154 F = &fd_table[fd];
2155
2156 if (writeTimedOut(fd)) {
2157 // We have an active write callback and we are timed out
2158 debugs(5, 5, "checkTimeouts: FD " << fd << " auto write timeout");
2159 commSetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0);
2160 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), COMM_ERROR, ETIMEDOUT);
2161 } else if (AlreadyTimedOut(F))
2162 continue;
2163
2164 debugs(5, 5, "checkTimeouts: FD " << fd << " Expired");
2165
2166 if (F->timeoutHandler != NULL) {
2167 debugs(5, 5, "checkTimeouts: FD " << fd << ": Call timeout handler");
2168 callback = F->timeoutHandler;
2169 F->timeoutHandler = NULL;
2170 ScheduleCallHere(callback);
2171 } else {
2172 debugs(5, 5, "checkTimeouts: FD " << fd << ": Forcing comm_close()");
2173 comm_close(fd);
2174 }
2175 }
2176 }
2177
2178 void CommIO::Initialise()
2179 {
2180 /* Initialize done pipe signal */
2181 int DonePipe[2];
2182 if (pipe(DonePipe)) {}
2183 DoneFD = DonePipe[1];
2184 DoneReadFD = DonePipe[0];
2185 fd_open(DoneReadFD, FD_PIPE, "async-io completetion event: main");
2186 fd_open(DoneFD, FD_PIPE, "async-io completetion event: threads");
2187 commSetNonBlocking(DoneReadFD);
2188 commSetNonBlocking(DoneFD);
2189 commSetSelect(DoneReadFD, COMM_SELECT_READ, NULLFDHandler, NULL, 0);
2190 Initialised = true;
2191 }
2192
2193 void CommIO::NotifyIOClose()
2194 {
2195 /* Close done pipe signal */
2196 FlushPipe();
2197 close(DoneFD);
2198 close(DoneReadFD);
2199 fd_close(DoneFD);
2200 fd_close(DoneReadFD);
2201 Initialised = false;
2202 }
2203
2204 bool CommIO::Initialised = false;
2205 bool CommIO::DoneSignalled = false;
2206 int CommIO::DoneFD = -1;
2207 int CommIO::DoneReadFD = -1;
2208
2209 void
2210 CommIO::FlushPipe()
2211 {
2212 char buf[256];
2213 FD_READ_METHOD(DoneReadFD, buf, sizeof(buf));
2214 }
2215
2216 void
2217 CommIO::NULLFDHandler(int fd, void *data)
2218 {
2219 FlushPipe();
2220 commSetSelect(fd, COMM_SELECT_READ, NULLFDHandler, NULL, 0);
2221 }
2222
2223 void
2224 CommIO::ResetNotifications()
2225 {
2226 if (DoneSignalled) {
2227 FlushPipe();
2228 DoneSignalled = false;
2229 }
2230 }
2231
2232 /// Start waiting for a possibly half-closed connection to close
2233 // by scheduling a read callback to a monitoring handler that
2234 // will close the connection on read errors.
2235 void
2236 commStartHalfClosedMonitor(int fd)
2237 {
2238 debugs(5, 5, HERE << "adding FD " << fd << " to " << *TheHalfClosed);
2239 assert(isOpen(fd));
2240 assert(!commHasHalfClosedMonitor(fd));
2241 (void)TheHalfClosed->add(fd); // could also assert the result
2242 commPlanHalfClosedCheck(); // may schedule check if we added the first FD
2243 }
2244
2245 static
2246 void
2247 commPlanHalfClosedCheck()
2248 {
2249 if (!WillCheckHalfClosed && !TheHalfClosed->empty()) {
2250 eventAdd("commHalfClosedCheck", &commHalfClosedCheck, NULL, 1.0, 1);
2251 WillCheckHalfClosed = true;
2252 }
2253 }
2254
2255 /// iterates over all descriptors that may need half-closed tests and
2256 /// calls comm_read for those that do; re-schedules the check if needed
2257 static
2258 void
2259 commHalfClosedCheck(void *)
2260 {
2261 debugs(5, 5, HERE << "checking " << *TheHalfClosed);
2262
2263 typedef DescriptorSet::const_iterator DSCI;
2264 const DSCI end = TheHalfClosed->end();
2265 for (DSCI i = TheHalfClosed->begin(); i != end; ++i) {
2266 const int fd = *i;
2267 if (!fd_table[fd].halfClosedReader) { // not reading already
2268 AsyncCall::Pointer call = commCbCall(5,4, "commHalfClosedReader",
2269 CommIoCbPtrFun(&commHalfClosedReader, NULL));
2270 comm_read(fd, NULL, 0, call);
2271 fd_table[fd].halfClosedReader = call;
2272 }
2273 }
2274
2275 WillCheckHalfClosed = false; // as far as we know
2276 commPlanHalfClosedCheck(); // may need to check again
2277 }
2278
2279 /// checks whether we are waiting for possibly half-closed connection to close
2280 // We are monitoring if the read handler for the fd is the monitoring handler.
2281 bool
2282 commHasHalfClosedMonitor(int fd)
2283 {
2284 return TheHalfClosed->has(fd);
2285 }
2286
2287 /// stop waiting for possibly half-closed connection to close
2288 static void
2289 commStopHalfClosedMonitor(int const fd)
2290 {
2291 debugs(5, 5, HERE << "removing FD " << fd << " from " << *TheHalfClosed);
2292
2293 // cancel the read if one was scheduled
2294 AsyncCall::Pointer reader = fd_table[fd].halfClosedReader;
2295 if (reader != NULL)
2296 comm_read_cancel(fd, reader);
2297 fd_table[fd].halfClosedReader = NULL;
2298
2299 TheHalfClosed->del(fd);
2300 }
2301
2302 /// I/O handler for the possibly half-closed connection monitoring code
2303 static void
2304 commHalfClosedReader(int fd, char *, size_t size, comm_err_t flag, int, void *)
2305 {
2306 // there cannot be more data coming in on half-closed connections
2307 assert(size == 0);
2308 assert(commHasHalfClosedMonitor(fd)); // or we would have canceled the read
2309
2310 fd_table[fd].halfClosedReader = NULL; // done reading, for now
2311
2312 // nothing to do if fd is being closed
2313 if (flag == COMM_ERR_CLOSING)
2314 return;
2315
2316 // if read failed, close the connection
2317 if (flag != COMM_OK) {
2318 debugs(5, 3, "commHalfClosedReader: closing FD " << fd);
2319 comm_close(fd);
2320 return;
2321 }
2322
2323 // continue waiting for close or error
2324 commPlanHalfClosedCheck(); // make sure this fd will be checked again
2325 }
2326
2327
2328 CommRead::CommRead() : fd(-1), buf(NULL), len(0), callback(NULL) {}
2329
2330 CommRead::CommRead(int fd_, char *buf_, int len_, AsyncCall::Pointer &callback_)
2331 : fd(fd_), buf(buf_), len(len_), callback(callback_) {}
2332
2333 DeferredRead::DeferredRead () : theReader(NULL), theContext(NULL), theRead(), cancelled(false) {}
2334
2335 DeferredRead::DeferredRead (DeferrableRead *aReader, void *data, CommRead const &aRead) : theReader(aReader), theContext (data), theRead(aRead), cancelled(false) {}
2336
2337 DeferredReadManager::~DeferredReadManager()
2338 {
2339 flushReads();
2340 assert (deferredReads.empty());
2341 }
2342
2343 /* explicit instantiation required for some systems */
2344
2345 /// \cond AUTODOCS-IGNORE
2346 template cbdata_type CbDataList<DeferredRead>::CBDATA_CbDataList;
2347 /// \endcond
2348
2349 void
2350 DeferredReadManager::delayRead(DeferredRead const &aRead)
2351 {
2352 debugs(5, 3, "Adding deferred read on FD " << aRead.theRead.fd);
2353 CbDataList<DeferredRead> *temp = deferredReads.push_back(aRead);
2354
2355 // We have to use a global function as a closer and point to temp
2356 // instead of "this" because DeferredReadManager is not a job and
2357 // is not even cbdata protected
2358 AsyncCall::Pointer closer = commCbCall(5,4,
2359 "DeferredReadManager::CloseHandler",
2360 CommCloseCbPtrFun(&CloseHandler, temp));
2361 comm_add_close_handler(aRead.theRead.fd, closer);
2362 temp->element.closer = closer; // remeber so that we can cancel
2363 }
2364
2365 void
2366 DeferredReadManager::CloseHandler(int fd, void *thecbdata)
2367 {
2368 if (!cbdataReferenceValid (thecbdata))
2369 return;
2370
2371 CbDataList<DeferredRead> *temp = (CbDataList<DeferredRead> *)thecbdata;
2372
2373 temp->element.closer = NULL;
2374 temp->element.markCancelled();
2375 }
2376
2377 DeferredRead
2378 DeferredReadManager::popHead(CbDataListContainer<DeferredRead> &deferredReads)
2379 {
2380 assert (!deferredReads.empty());
2381
2382 DeferredRead &read = deferredReads.head->element;
2383 if (!read.cancelled) {
2384 comm_remove_close_handler(read.theRead.fd, read.closer);
2385 read.closer = NULL;
2386 }
2387
2388 DeferredRead result = deferredReads.pop_front();
2389
2390 return result;
2391 }
2392
2393 void
2394 DeferredReadManager::kickReads(int const count)
2395 {
2396 /* if we had CbDataList::size() we could consolidate this and flushReads */
2397
2398 if (count < 1) {
2399 flushReads();
2400 return;
2401 }
2402
2403 size_t remaining = count;
2404
2405 while (!deferredReads.empty() && remaining) {
2406 DeferredRead aRead = popHead(deferredReads);
2407 kickARead(aRead);
2408
2409 if (!aRead.cancelled)
2410 --remaining;
2411 }
2412 }
2413
2414 void
2415 DeferredReadManager::flushReads()
2416 {
2417 CbDataListContainer<DeferredRead> reads;
2418 reads = deferredReads;
2419 deferredReads = CbDataListContainer<DeferredRead>();
2420
2421 // XXX: For fairness this SHOULD randomize the order
2422 while (!reads.empty()) {
2423 DeferredRead aRead = popHead(reads);
2424 kickARead(aRead);
2425 }
2426 }
2427
2428 void
2429 DeferredReadManager::kickARead(DeferredRead const &aRead)
2430 {
2431 if (aRead.cancelled)
2432 return;
2433
2434 if (aRead.theRead.fd>=0 && fd_table[aRead.theRead.fd].closing())
2435 return;
2436
2437 debugs(5, 3, "Kicking deferred read on FD " << aRead.theRead.fd);
2438
2439 aRead.theReader(aRead.theContext, aRead.theRead);
2440 }
2441
2442 void
2443 DeferredRead::markCancelled()
2444 {
2445 cancelled = true;
2446 }
2447
2448 ConnectionDetail::ConnectionDetail() : me(), peer()
2449 {
2450 }
2451
2452 int
2453 CommSelectEngine::checkEvents(int timeout)
2454 {
2455 static time_t last_timeout = 0;
2456
2457 /* No, this shouldn't be here. But it shouldn't be in each comm handler. -adrian */
2458 if (squid_curtime > last_timeout) {
2459 last_timeout = squid_curtime;
2460 checkTimeouts();
2461 }
2462
2463 switch (comm_select(timeout)) {
2464
2465 case COMM_OK:
2466
2467 case COMM_TIMEOUT:
2468 return 0;
2469
2470 case COMM_IDLE:
2471
2472 case COMM_SHUTDOWN:
2473 return EVENT_IDLE;
2474
2475 case COMM_ERROR:
2476 return EVENT_ERROR;
2477
2478 default:
2479 fatal_dump("comm.cc: Internal error -- this should never happen.");
2480 return EVENT_ERROR;
2481 };
2482 }
2483
2484 /// Create a unix-domain socket (UDS) that only supports FD_MSGHDR I/O.
2485 int
2486 comm_open_uds(int sock_type,
2487 int proto,
2488 struct sockaddr_un* addr,
2489 int flags)
2490 {
2491 // TODO: merge with comm_openex() when Ip::Address becomes NetAddress
2492
2493 int new_socket;
2494
2495 PROF_start(comm_open);
2496 /* Create socket for accepting new connections. */
2497 statCounter.syscalls.sock.sockets++;
2498
2499 /* Setup the socket addrinfo details for use */
2500 struct addrinfo AI;
2501 AI.ai_flags = 0;
2502 AI.ai_family = PF_UNIX;
2503 AI.ai_socktype = sock_type;
2504 AI.ai_protocol = proto;
2505 AI.ai_addrlen = SUN_LEN(addr);
2506 AI.ai_addr = (sockaddr*)addr;
2507 AI.ai_canonname = NULL;
2508 AI.ai_next = NULL;
2509
2510 debugs(50, 3, HERE << "Attempt open socket for: " << addr->sun_path);
2511
2512 if ((new_socket = socket(AI.ai_family, AI.ai_socktype, AI.ai_protocol)) < 0) {
2513 /* Increase the number of reserved fd's if calls to socket()
2514 * are failing because the open file table is full. This
2515 * limits the number of simultaneous clients */
2516
2517 if (limitError(errno)) {
2518 debugs(50, DBG_IMPORTANT, HERE << "socket failure: " << xstrerror());
2519 fdAdjustReserved();
2520 } else {
2521 debugs(50, DBG_CRITICAL, HERE << "socket failure: " << xstrerror());
2522 }
2523
2524 PROF_stop(comm_open);
2525 return -1;
2526 }
2527
2528 debugs(50, 3, HERE "Opened UDS FD " << new_socket << " : family=" << AI.ai_family << ", type=" << AI.ai_socktype << ", protocol=" << AI.ai_protocol);
2529
2530 /* update fdstat */
2531 debugs(50, 5, HERE << "FD " << new_socket << " is a new socket");
2532
2533 assert(!isOpen(new_socket));
2534 fd_open(new_socket, FD_MSGHDR, NULL);
2535
2536 fdd_table[new_socket].close_file = NULL;
2537
2538 fdd_table[new_socket].close_line = 0;
2539
2540 fd_table[new_socket].sock_family = AI.ai_family;
2541
2542 if (!(flags & COMM_NOCLOEXEC))
2543 commSetCloseOnExec(new_socket);
2544
2545 if (flags & COMM_REUSEADDR)
2546 commSetReuseAddr(new_socket);
2547
2548 if (flags & COMM_NONBLOCKING) {
2549 if (commSetNonBlocking(new_socket) != COMM_OK) {
2550 comm_close(new_socket);
2551 PROF_stop(comm_open);
2552 return -1;
2553 }
2554 }
2555
2556 if (flags & COMM_DOBIND) {
2557 if (commBind(new_socket, AI) != COMM_OK) {
2558 comm_close(new_socket);
2559 PROF_stop(comm_open);
2560 return -1;
2561 }
2562 }
2563
2564 #ifdef TCP_NODELAY
2565 if (sock_type == SOCK_STREAM)
2566 commSetTcpNoDelay(new_socket);
2567
2568 #endif
2569
2570 if (Config.tcpRcvBufsz > 0 && sock_type == SOCK_STREAM)
2571 commSetTcpRcvbuf(new_socket, Config.tcpRcvBufsz);
2572
2573 PROF_stop(comm_open);
2574
2575 return new_socket;
2576 }