]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm.cc
Merged from trunk
[thirdparty/squid.git] / src / comm.cc
1 /*
2 * DEBUG: section 5 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 "CommIO.h"
41 #include "CommRead.h"
42 #include "ConnectionDetail.h"
43 #include "MemBuf.h"
44 #include "pconn.h"
45 #include "SquidTime.h"
46 #include "CommCalls.h"
47 #include "DescriptorSet.h"
48 #include "icmp/net_db.h"
49 #include "ip/IpAddress.h"
50 #include "ip/IpIntercept.h"
51
52 #if defined(_SQUID_CYGWIN_)
53 #include <sys/ioctl.h>
54 #endif
55 #ifdef HAVE_NETINET_TCP_H
56 #include <netinet/tcp.h>
57 #endif
58
59 /*
60 * New C-like simple comm code. This stuff is a mess and doesn't really buy us anything.
61 */
62
63 typedef enum {
64 IOCB_NONE,
65 IOCB_READ,
66 IOCB_WRITE
67 } iocb_type;
68
69 static void commStopHalfClosedMonitor(int fd);
70 static IOCB commHalfClosedReader;
71
72
73 struct comm_io_callback_t {
74 iocb_type type;
75 int fd;
76 AsyncCall::Pointer callback;
77 char *buf;
78 FREE *freefunc;
79 int size;
80 int offset;
81 comm_err_t errcode;
82 int xerrno;
83
84 bool active() const { return callback != NULL; }
85 };
86
87 struct _comm_fd {
88 int fd;
89 comm_io_callback_t readcb;
90 comm_io_callback_t writecb;
91 };
92 typedef struct _comm_fd comm_fd_t;
93 comm_fd_t *commfd_table;
94
95 // TODO: make this a comm_io_callback_t method?
96 bool
97 commio_has_callback(int fd, iocb_type type, comm_io_callback_t *ccb)
98 {
99 assert(ccb->fd == fd);
100 assert(ccb->type == type);
101 return ccb->active();
102 }
103
104 /*
105 * Configure comm_io_callback_t for I/O
106 *
107 * @param fd filedescriptor
108 * @param ccb comm io callback
109 * @param cb callback
110 * @param cbdata callback data (must be cbdata'ed)
111 * @param buf buffer, if applicable
112 * @param freefunc freefunc, if applicable
113 * @param size buffer size
114 */
115 static void
116 commio_set_callback(int fd, iocb_type type, comm_io_callback_t *ccb,
117 AsyncCall::Pointer &cb, char *buf, FREE *freefunc, int size)
118 {
119 assert(!ccb->active());
120 assert(ccb->type == type);
121 assert(cb != NULL);
122 ccb->fd = fd;
123 ccb->callback = cb;
124 ccb->buf = buf;
125 ccb->freefunc = freefunc;
126 ccb->size = size;
127 ccb->offset = 0;
128 }
129
130
131 // Schedule the callback call and clear the callback
132 static void
133 commio_finish_callback(int fd, comm_io_callback_t *ccb, comm_err_t code, int xerrno)
134 {
135 debugs(5, 3, "commio_finish_callback: called for FD " << fd << " (" <<
136 code << ", " << xerrno << ")");
137 assert(ccb->active());
138 assert(ccb->fd == fd);
139 ccb->errcode = code;
140 ccb->xerrno = xerrno;
141
142 comm_io_callback_t cb = *ccb;
143
144 /* We've got a copy; blow away the real one */
145 /* XXX duplicate code from commio_cancel_callback! */
146 ccb->xerrno = 0;
147 ccb->callback = NULL; // cb has it
148
149 /* free data */
150 if (cb.freefunc) {
151 cb.freefunc(cb.buf);
152 cb.buf = NULL;
153 }
154
155 if (cb.callback != NULL) {
156 typedef CommIoCbParams Params;
157 Params &params = GetCommParams<Params>(cb.callback);
158 params.fd = cb.fd;
159 params.buf = cb.buf;
160 params.size = cb.offset;
161 params.flag = cb.errcode;
162 params.xerrno = cb.xerrno;
163 ScheduleCallHere(cb.callback);
164 }
165 }
166
167
168 /*
169 * Cancel the given callback
170 *
171 * Remember that the data is cbdataRef'ed.
172 */
173 // TODO: make this a comm_io_callback_t method
174 static void
175 commio_cancel_callback(int fd, comm_io_callback_t *ccb)
176 {
177 debugs(5, 3, "commio_cancel_callback: called for FD " << fd);
178 assert(ccb->fd == fd);
179 assert(ccb->active());
180
181 ccb->xerrno = 0;
182 ccb->callback = NULL;
183 }
184
185 /*
186 * Call the given comm callback; assumes the callback is valid.
187 *
188 * @param ccb io completion callback
189 */
190 void
191 commio_call_callback(comm_io_callback_t *ccb)
192 {
193 }
194
195 class ConnectStateData
196 {
197
198 public:
199 void *operator new (size_t);
200 void operator delete (void *);
201 static void Connect (int fd, void *me);
202 void connect();
203 void callCallback(comm_err_t status, int xerrno);
204 void defaults();
205
206 // defaults given by client
207 char *host;
208 u_short default_port;
209 IpAddress default_addr;
210 // NP: CANNOT store the default addr:port together as it gets set/reset differently.
211
212 DnsLookupDetails dns; ///< host lookup details
213 IpAddress S;
214 AsyncCall::Pointer callback;
215
216 int fd;
217 int tries;
218 int addrcount;
219 int connstart;
220
221 private:
222 int commResetFD();
223 int commRetryConnect();
224 CBDATA_CLASS(ConnectStateData);
225 };
226
227 /* STATIC */
228
229 static DescriptorSet *TheHalfClosed = NULL; /// the set of half-closed FDs
230 static bool WillCheckHalfClosed = false; /// true if check is scheduled
231 static EVH commHalfClosedCheck;
232 static void commPlanHalfClosedCheck();
233
234 static comm_err_t commBind(int s, struct addrinfo &);
235 static void commSetReuseAddr(int);
236 static void commSetNoLinger(int);
237 #ifdef TCP_NODELAY
238 static void commSetTcpNoDelay(int);
239 #endif
240 static void commSetTcpRcvbuf(int, int);
241 static PF commConnectFree;
242 static PF commHandleWrite;
243 static IPH commConnectDnsHandle;
244
245 static PF comm_accept_try;
246
247 class AcceptFD
248 {
249
250 public:
251 AcceptFD(int aFd = -1): fd(aFd), theCallback(0), mayAcceptMore(false) {}
252
253 void subscribe(AsyncCall::Pointer &call);
254 void acceptNext();
255 void notify(int newfd, comm_err_t, int xerrno, const ConnectionDetail &);
256
257 int fd;
258
259 private:
260 bool acceptOne();
261
262 AsyncCall::Pointer theCallback;
263 bool mayAcceptMore;
264 };
265
266 typedef enum {
267 COMM_CB_READ = 1,
268 COMM_CB_DERIVED
269 } comm_callback_t;
270
271 struct _fd_debug_t {
272 char const *close_file;
273 int close_line;
274 };
275
276 typedef struct _fd_debug_t fd_debug_t;
277
278 static MemAllocator *conn_close_pool = NULL;
279 AcceptFD *fdc_table = NULL; // TODO: rename. And use Vector<>?
280 fd_debug_t *fdd_table = NULL;
281
282 static bool
283 isOpen(const int fd)
284 {
285 return fd_table[fd].flags.open != 0;
286 }
287
288 /**
289 * Attempt a read
290 *
291 * If the read attempt succeeds or fails, call the callback.
292 * Else, wait for another IO notification.
293 */
294 void
295 commHandleRead(int fd, void *data)
296 {
297 comm_io_callback_t *ccb = (comm_io_callback_t *) data;
298
299 assert(data == COMMIO_FD_READCB(fd));
300 assert(commio_has_callback(fd, IOCB_READ, ccb));
301 /* Attempt a read */
302 statCounter.syscalls.sock.reads++;
303 errno = 0;
304 int retval;
305 retval = FD_READ_METHOD(fd, ccb->buf, ccb->size);
306 debugs(5, 3, "comm_read_try: FD " << fd << ", size " << ccb->size << ", retval " << retval << ", errno " << errno);
307
308 if (retval < 0 && !ignoreErrno(errno)) {
309 debugs(5, 3, "comm_read_try: scheduling COMM_ERROR");
310 ccb->offset = 0;
311 commio_finish_callback(fd, ccb, COMM_ERROR, errno);
312 return;
313 };
314
315 /* See if we read anything */
316 /* Note - read 0 == socket EOF, which is a valid read */
317 if (retval >= 0) {
318 fd_bytes(fd, retval, FD_READ);
319 ccb->offset = retval;
320 commio_finish_callback(fd, ccb, COMM_OK, errno);
321 return;
322 }
323
324 /* Nope, register for some more IO */
325 commSetSelect(fd, COMM_SELECT_READ, commHandleRead, data, 0);
326 }
327
328 /**
329 * Queue a read. handler/handler_data are called when the read
330 * completes, on error, or on file descriptor close.
331 */
332 void
333 comm_read(int fd, char *buf, int size, IOCB *handler, void *handler_data)
334 {
335 AsyncCall::Pointer call = commCbCall(5,4, "SomeCommReadHandler",
336 CommIoCbPtrFun(handler, handler_data));
337 comm_read(fd, buf, size, call);
338 }
339
340 void
341 comm_read(int fd, char *buf, int size, AsyncCall::Pointer &callback)
342 {
343 debugs(5, 5, "comm_read, queueing read for FD " << fd << "; asynCall " << callback);
344
345 /* Make sure we are open and not closing */
346 assert(isOpen(fd));
347 assert(!fd_table[fd].closing());
348 comm_io_callback_t *ccb = COMMIO_FD_READCB(fd);
349
350 // Make sure we are either not reading or just passively monitoring.
351 // Active/passive conflicts are OK and simply cancel passive monitoring.
352 if (ccb->active()) {
353 // if the assertion below fails, we have an active comm_read conflict
354 assert(fd_table[fd].halfClosedReader != NULL);
355 commStopHalfClosedMonitor(fd);
356 assert(!ccb->active());
357 }
358
359 /* Queue the read */
360 commio_set_callback(fd, IOCB_READ, ccb, callback, (char *)buf, NULL, size);
361 commSetSelect(fd, COMM_SELECT_READ, commHandleRead, ccb, 0);
362 }
363
364 /**
365 * Empty the read buffers
366 *
367 * This is a magical routine that empties the read buffers.
368 * Under some platforms (Linux) if a buffer has data in it before
369 * you call close(), the socket will hang and take quite a while
370 * to timeout.
371 */
372 static void
373 comm_empty_os_read_buffers(int fd)
374 {
375 #ifdef _SQUID_LINUX_
376 /* prevent those nasty RST packets */
377 char buf[SQUID_TCP_SO_RCVBUF];
378
379 if (fd_table[fd].flags.nonblocking == 1) {
380 while (FD_READ_METHOD(fd, buf, SQUID_TCP_SO_RCVBUF) > 0) {};
381 }
382 #endif
383 }
384
385
386 /**
387 * Return whether the FD has a pending completed callback.
388 */
389 int
390 comm_has_pending_read_callback(int fd)
391 {
392 assert(isOpen(fd));
393 // XXX: We do not know whether there is a read callback scheduled.
394 // This is used for pconn management that should probably be more
395 // tightly integrated into comm to minimize the chance that a
396 // closing pconn socket will be used for a new transaction.
397 return false;
398 }
399
400 // Does comm check this fd for read readiness?
401 // Note that when comm is not monitoring, there can be a pending callback
402 // call, which may resume comm monitoring once fired.
403 bool
404 comm_monitors_read(int fd)
405 {
406 assert(isOpen(fd));
407 // Being active is usually the same as monitoring because we always
408 // start monitoring the FD when we configure comm_io_callback_t for I/O
409 // and we usually configure comm_io_callback_t for I/O when we starting
410 // monitoring a FD for reading. TODO: replace with commio_has_callback
411 return COMMIO_FD_READCB(fd)->active();
412 }
413
414 /**
415 * Cancel a pending read. Assert that we have the right parameters,
416 * and that there are no pending read events!
417 *
418 * XXX: We do not assert that there are no pending read events and
419 * with async calls it becomes even more difficult.
420 * The whole interface should be reworked to do callback->cancel()
421 * instead of searching for places where the callback may be stored and
422 * updating the state of those places.
423 *
424 * AHC Don't call the comm handlers?
425 */
426 void
427 comm_read_cancel(int fd, IOCB *callback, void *data)
428 {
429 if (!isOpen(fd)) {
430 debugs(5, 4, "comm_read_cancel fails: FD " << fd << " closed");
431 return;
432 }
433
434 comm_io_callback_t *cb = COMMIO_FD_READCB(fd);
435 // TODO: is "active" == "monitors FD"?
436 if (!cb->active()) {
437 debugs(5, 4, "comm_read_cancel fails: FD " << fd << " inactive");
438 return;
439 }
440
441 typedef CommCbFunPtrCallT<CommIoCbPtrFun> Call;
442 Call *call = dynamic_cast<Call*>(cb->callback.getRaw());
443 if (!call) {
444 debugs(5, 4, "comm_read_cancel fails: FD " << fd << " lacks callback");
445 return;
446 }
447
448 call->cancel("old comm_read_cancel");
449
450 typedef CommIoCbParams Params;
451 const Params &params = GetCommParams<Params>(cb->callback);
452
453 /* Ok, we can be reasonably sure we won't lose any data here! */
454 assert(call->dialer.handler == callback);
455 assert(params.data == data);
456
457 /* Delete the callback */
458 commio_cancel_callback(fd, cb);
459
460 /* And the IO event */
461 commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
462 }
463
464 void
465 comm_read_cancel(int fd, AsyncCall::Pointer &callback)
466 {
467 callback->cancel("comm_read_cancel");
468
469 if (!isOpen(fd)) {
470 debugs(5, 4, "comm_read_cancel fails: FD " << fd << " closed");
471 return;
472 }
473
474 comm_io_callback_t *cb = COMMIO_FD_READCB(fd);
475
476 if (!cb->active()) {
477 debugs(5, 4, "comm_read_cancel fails: FD " << fd << " inactive");
478 return;
479 }
480
481 AsyncCall::Pointer call = cb->callback;
482 assert(call != NULL); // XXX: should never fail (active() checks for callback==NULL)
483
484 /* Ok, we can be reasonably sure we won't lose any data here! */
485 assert(call == callback);
486
487 /* Delete the callback */
488 commio_cancel_callback(fd, cb);
489
490 /* And the IO event */
491 commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
492 }
493
494
495 /**
496 * synchronous wrapper around udp socket functions
497 */
498 int
499 comm_udp_recvfrom(int fd, void *buf, size_t len, int flags, IpAddress &from)
500 {
501 statCounter.syscalls.sock.recvfroms++;
502 int x = 0;
503 struct addrinfo *AI = NULL;
504
505 debugs(5,8, "comm_udp_recvfrom: FD " << fd << " from " << from);
506
507 assert( NULL == AI );
508
509 from.InitAddrInfo(AI);
510
511 x = recvfrom(fd, buf, len, flags, AI->ai_addr, &AI->ai_addrlen);
512
513 from = *AI;
514
515 from.FreeAddrInfo(AI);
516
517 return x;
518 }
519
520 int
521 comm_udp_recv(int fd, void *buf, size_t len, int flags)
522 {
523 IpAddress nul;
524 return comm_udp_recvfrom(fd, buf, len, flags, nul);
525 }
526
527 ssize_t
528 comm_udp_send(int s, const void *buf, size_t len, int flags)
529 {
530 return send(s, buf, len, flags);
531 }
532
533
534 bool
535 comm_has_incomplete_write(int fd)
536 {
537 assert(isOpen(fd));
538 return COMMIO_FD_WRITECB(fd)->active();
539 }
540
541 /**
542 * Queue a write. handler/handler_data are called when the write fully
543 * completes, on error, or on file descriptor close.
544 */
545
546 /* Return the local port associated with fd. */
547 u_short
548 comm_local_port(int fd)
549 {
550 IpAddress temp;
551 struct addrinfo *addr = NULL;
552 fde *F = &fd_table[fd];
553
554 /* If the fd is closed already, just return */
555
556 if (!F->flags.open) {
557 debugs(5, 0, "comm_local_port: FD " << fd << " has been closed.");
558 return 0;
559 }
560
561 if (F->local_addr.GetPort())
562 return F->local_addr.GetPort();
563
564 #if USE_IPV6
565 if (F->sock_family == AF_INET)
566 temp.SetIPv4();
567 #endif
568
569 temp.InitAddrInfo(addr);
570
571 if (getsockname(fd, addr->ai_addr, &(addr->ai_addrlen)) ) {
572 debugs(50, 1, "comm_local_port: Failed to retrieve TCP/UDP port number for socket: FD " << fd << ": " << xstrerror());
573 temp.FreeAddrInfo(addr);
574 return 0;
575 }
576 temp = *addr;
577
578 temp.FreeAddrInfo(addr);
579
580 F->local_addr.SetPort(temp.GetPort());
581
582 #if 0 // seems to undo comm_open actions on the FD ...
583 // grab default socket information for this address
584 temp.GetAddrInfo(addr);
585
586 F->sock_family = addr->ai_family;
587
588 temp.FreeAddrInfo(addr);
589 #endif
590
591 debugs(5, 6, "comm_local_port: FD " << fd << ": port " << F->local_addr.GetPort() << "(family=" << F->sock_family << ")");
592 return F->local_addr.GetPort();
593 }
594
595 static comm_err_t
596 commBind(int s, struct addrinfo &inaddr)
597 {
598 statCounter.syscalls.sock.binds++;
599
600 if (bind(s, inaddr.ai_addr, inaddr.ai_addrlen) == 0) {
601 debugs(50, 6, "commBind: bind socket FD " << s << " to " << fd_table[s].local_addr);
602 return COMM_OK;
603 }
604
605 debugs(50, 0, "commBind: Cannot bind socket FD " << s << " to " << fd_table[s].local_addr << ": " << xstrerror());
606
607 return COMM_ERROR;
608 }
609
610 /**
611 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
612 * is OR of flags specified in comm.h. Defaults TOS
613 */
614 int
615 comm_open(int sock_type,
616 int proto,
617 IpAddress &addr,
618 int flags,
619 const char *note)
620 {
621 return comm_openex(sock_type, proto, addr, flags, 0, note);
622 }
623
624 int
625 comm_open_listener(int sock_type,
626 int proto,
627 IpAddress &addr,
628 int flags,
629 const char *note)
630 {
631 int sock = -1;
632
633 /* all listener sockets require bind() */
634 flags |= COMM_DOBIND;
635
636 /* attempt native enabled port. */
637 sock = comm_openex(sock_type, proto, addr, flags, 0, note);
638
639 #if USE_IPV6
640 /* under IPv6 there is the possibility IPv6 is present but disabled. */
641 /* try again as IPv4-native */
642 if ( sock < 0 && addr.IsIPv6() && addr.SetIPv4() ) {
643 /* attempt to open this IPv4-only. */
644 sock = comm_openex(sock_type, proto, addr, flags, 0, note);
645 debugs(50, 2, HERE << "attempt open " << note << " socket on: " << addr);
646 }
647 #endif
648
649 return sock;
650 }
651
652 static bool
653 limitError(int const anErrno)
654 {
655 return anErrno == ENFILE || anErrno == EMFILE;
656 }
657
658 int
659 comm_set_tos(int fd, int tos)
660 {
661 #ifdef IP_TOS
662 int x = setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &tos, sizeof(int));
663 if (x < 0)
664 debugs(50, 1, "comm_set_tos: setsockopt(IP_TOS) on FD " << fd << ": " << xstrerror());
665 return x;
666 #else
667 debugs(50, 0, "WARNING: setsockopt(IP_TOS) not supported on this platform");
668 return -1;
669 #endif
670 }
671
672 void
673 comm_set_v6only(int fd, int tos)
674 {
675 #ifdef IPV6_V6ONLY
676 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &tos, sizeof(int)) < 0) {
677 debugs(50, 1, "comm_open: setsockopt(IPV6_V6ONLY) " << (tos?"ON":"OFF") << " for FD " << fd << ": " << xstrerror());
678 }
679 #else
680 debugs(50, 0, "WARNING: comm_open: setsockopt(IPV6_V6ONLY) not supported on this platform");
681 #endif /* sockopt */
682 }
683
684 /**
685 * Set the socket IP_TRANSPARENT option for Linux TPROXY v4 support.
686 */
687 void
688 comm_set_transparent(int fd)
689 {
690 #if defined(IP_TRANSPARENT)
691 int tos = 1;
692 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &tos, sizeof(int)) < 0) {
693 debugs(50, DBG_IMPORTANT, "comm_open: setsockopt(IP_TRANSPARENT) on FD " << fd << ": " << xstrerror());
694 } else {
695 /* mark the socket as having transparent options */
696 fd_table[fd].flags.transparent = 1;
697 }
698 #else
699 debugs(50, DBG_CRITICAL, "WARNING: comm_open: setsockopt(IP_TRANSPARENT) not supported on this platform");
700 #endif /* sockopt */
701 }
702
703 /**
704 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
705 * is OR of flags specified in defines.h:COMM_*
706 */
707 int
708 comm_openex(int sock_type,
709 int proto,
710 IpAddress &addr,
711 int flags,
712 unsigned char TOS,
713 const char *note)
714 {
715 int new_socket;
716 fde *F = NULL;
717 int tos = 0;
718 struct addrinfo *AI = NULL;
719
720 PROF_start(comm_open);
721 /* Create socket for accepting new connections. */
722 statCounter.syscalls.sock.sockets++;
723
724 /* Setup the socket addrinfo details for use */
725 addr.GetAddrInfo(AI);
726 AI->ai_socktype = sock_type;
727 AI->ai_protocol = proto;
728
729 debugs(50, 3, "comm_openex: Attempt open socket for: " << addr );
730
731 if ((new_socket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol)) < 0) {
732 /* Increase the number of reserved fd's if calls to socket()
733 * are failing because the open file table is full. This
734 * limits the number of simultaneous clients */
735
736 if (limitError(errno)) {
737 debugs(50, DBG_IMPORTANT, "comm_open: socket failure: " << xstrerror());
738 fdAdjustReserved();
739 } else {
740 debugs(50, DBG_CRITICAL, "comm_open: socket failure: " << xstrerror());
741 }
742
743 addr.FreeAddrInfo(AI);
744
745 PROF_stop(comm_open);
746 return -1;
747 }
748
749 debugs(50, 3, "comm_openex: Opened socket FD " << new_socket << " : family=" << AI->ai_family << ", type=" << AI->ai_socktype << ", protocol=" << AI->ai_protocol );
750
751 /* set TOS if needed */
752 if (TOS && comm_set_tos(new_socket, TOS) ) {
753 tos = TOS;
754 }
755
756 #if IPV6_SPECIAL_SPLITSTACK
757
758 if ( addr.IsIPv6() )
759 comm_set_v6only(new_socket, 1);
760
761 #endif
762
763 #if IPV6_SPECIAL_V4MAPPED
764
765 /* Windows Vista supports Dual-Sockets. BUT defaults them to V6ONLY. Turn it OFF. */
766 /* Other OS may have this administratively disabled for general use. Same deal. */
767 if ( addr.IsIPv6() )
768 comm_set_v6only(new_socket, 0);
769
770 #endif
771
772 /* update fdstat */
773 debugs(5, 5, "comm_open: FD " << new_socket << " is a new socket");
774
775 assert(!isOpen(new_socket));
776 fd_open(new_socket, FD_SOCKET, note);
777
778 fdd_table[new_socket].close_file = NULL;
779
780 fdd_table[new_socket].close_line = 0;
781
782 F = &fd_table[new_socket];
783
784 F->local_addr = addr;
785
786 F->tos = TOS;
787
788 F->sock_family = AI->ai_family;
789
790 if (!(flags & COMM_NOCLOEXEC))
791 commSetCloseOnExec(new_socket);
792
793 if ((flags & COMM_REUSEADDR))
794 commSetReuseAddr(new_socket);
795
796 if (addr.GetPort() > (u_short) 0) {
797 #ifdef _SQUID_MSWIN_
798 if (sock_type != SOCK_DGRAM)
799 #endif
800 commSetNoLinger(new_socket);
801
802 if (opt_reuseaddr)
803 commSetReuseAddr(new_socket);
804 }
805
806 /* MUST be done before binding or face OS Error: "(99) Cannot assign requested address"... */
807 if ((flags & COMM_TRANSPARENT)) {
808 comm_set_transparent(new_socket);
809 }
810
811 if ( (flags & COMM_DOBIND) || addr.GetPort() > 0 || !addr.IsAnyAddr() ) {
812 if ( !(flags & COMM_DOBIND) && addr.IsAnyAddr() )
813 debugs(5,1,"WARNING: Squid is attempting to bind() port " << addr << " without being a listener.");
814 if ( addr.IsNoAddr() )
815 debugs(5,0,"CRITICAL: Squid is attempting to bind() port " << addr << "!!");
816
817 if (commBind(new_socket, *AI) != COMM_OK) {
818 comm_close(new_socket);
819 addr.FreeAddrInfo(AI);
820 return -1;
821 PROF_stop(comm_open);
822 }
823 }
824
825 addr.FreeAddrInfo(AI);
826
827 if (flags & COMM_NONBLOCKING)
828 if (commSetNonBlocking(new_socket) == COMM_ERROR) {
829 return -1;
830 PROF_stop(comm_open);
831 }
832
833 #ifdef TCP_NODELAY
834 if (sock_type == SOCK_STREAM)
835 commSetTcpNoDelay(new_socket);
836
837 #endif
838
839 if (Config.tcpRcvBufsz > 0 && sock_type == SOCK_STREAM)
840 commSetTcpRcvbuf(new_socket, Config.tcpRcvBufsz);
841
842 PROF_stop(comm_open);
843
844 return new_socket;
845 }
846
847 CBDATA_CLASS_INIT(ConnectStateData);
848
849 void *
850 ConnectStateData::operator new (size_t size)
851 {
852 CBDATA_INIT_TYPE(ConnectStateData);
853 return cbdataAlloc(ConnectStateData);
854 }
855
856 void
857 ConnectStateData::operator delete (void *address)
858 {
859 cbdataFree(address);
860 }
861
862
863
864 void
865 commConnectStart(int fd, const char *host, u_short port, AsyncCall::Pointer &cb)
866 {
867 debugs(cb->debugSection, cb->debugLevel, "commConnectStart: FD " << fd <<
868 ", cb " << cb << ", " << host << ":" << port); // TODO: just print *cb
869
870 ConnectStateData *cs;
871 cs = new ConnectStateData;
872 cs->fd = fd;
873 cs->host = xstrdup(host);
874 cs->default_port = port;
875 cs->callback = cb;
876
877 comm_add_close_handler(fd, commConnectFree, cs);
878 ipcache_nbgethostbyname(host, commConnectDnsHandle, cs);
879 }
880
881 // TODO: Remove this and similar callback registration functions by replacing
882 // (callback,data) parameters with an AsyncCall so that we do not have to use
883 // a generic call name and debug level when creating an AsyncCall. This will
884 // also cut the number of callback registration routines in half.
885 void
886 commConnectStart(int fd, const char *host, u_short port, CNCB * callback, void *data)
887 {
888 debugs(5, 5, "commConnectStart: FD " << fd << ", data " << data << ", " << host << ":" << port);
889 AsyncCall::Pointer call = commCbCall(5,3,
890 "SomeCommConnectHandler", CommConnectCbPtrFun(callback, data));
891 commConnectStart(fd, host, port, call);
892 }
893
894 static void
895 commConnectDnsHandle(const ipcache_addrs *ia, const DnsLookupDetails &details, void *data)
896 {
897 ConnectStateData *cs = (ConnectStateData *)data;
898 cs->dns = details;
899
900 if (ia == NULL) {
901 debugs(5, 3, "commConnectDnsHandle: Unknown host: " << cs->host);
902 cs->callCallback(COMM_ERR_DNS, 0);
903 return;
904 }
905
906 assert(ia->cur < ia->count);
907
908 cs->default_addr = ia->in_addrs[ia->cur];
909
910 if (Config.onoff.balance_on_multiple_ip)
911 ipcacheCycleAddr(cs->host, NULL);
912
913 cs->addrcount = ia->count;
914
915 cs->connstart = squid_curtime;
916
917 cs->connect();
918 }
919
920 void
921 ConnectStateData::callCallback(comm_err_t status, int xerrno)
922 {
923 debugs(5, 3, "commConnectCallback: FD " << fd);
924
925 comm_remove_close_handler(fd, commConnectFree, this);
926 commSetTimeout(fd, -1, NULL, NULL);
927
928 typedef CommConnectCbParams Params;
929 Params &params = GetCommParams<Params>(callback);
930 params.fd = fd;
931 params.dns = dns;
932 params.flag = status;
933 params.xerrno = xerrno;
934 ScheduleCallHere(callback);
935 callback = NULL;
936
937 commConnectFree(fd, this);
938 }
939
940 static void
941 commConnectFree(int fd, void *data)
942 {
943 ConnectStateData *cs = (ConnectStateData *)data;
944 debugs(5, 3, "commConnectFree: FD " << fd);
945 // delete cs->callback;
946 cs->callback = NULL;
947 safe_free(cs->host);
948 delete cs;
949 }
950
951 static void
952 copyFDFlags(int to, fde *F)
953 {
954 if (F->flags.close_on_exec)
955 commSetCloseOnExec(to);
956
957 if (F->flags.nonblocking)
958 commSetNonBlocking(to);
959
960 #ifdef TCP_NODELAY
961
962 if (F->flags.nodelay)
963 commSetTcpNoDelay(to);
964
965 #endif
966
967 if (Config.tcpRcvBufsz > 0)
968 commSetTcpRcvbuf(to, Config.tcpRcvBufsz);
969 }
970
971 /* Reset FD so that we can connect() again */
972 int
973 ConnectStateData::commResetFD()
974 {
975 struct addrinfo *AI = NULL;
976 IpAddress nul;
977 int new_family = AF_UNSPEC;
978
979 // XXX: do we have to check this?
980 //
981 // if (!cbdataReferenceValid(callback.data))
982 // return 0;
983
984 statCounter.syscalls.sock.sockets++;
985
986 /* setup a bare-bones addrinfo */
987 /* TODO INET6: for WinXP we may need to check the local_addr type and setup the family properly. */
988 nul.GetAddrInfo(AI);
989 new_family = AI->ai_family;
990
991 int fd2 = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
992
993 nul.FreeAddrInfo(AI);
994
995 if (fd2 < 0) {
996 debugs(5, DBG_CRITICAL, HERE << "WARNING: FD " << fd2 << " socket failed to allocate: " << xstrerror());
997
998 if (ENFILE == errno || EMFILE == errno)
999 fdAdjustReserved();
1000
1001 return 0;
1002 }
1003
1004 #ifdef _SQUID_MSWIN_
1005
1006 /* On Windows dup2() can't work correctly on Sockets, the */
1007 /* workaround is to close the destination Socket before call them. */
1008 close(fd);
1009
1010 #endif
1011
1012 if (dup2(fd2, fd) < 0) {
1013 debugs(5, DBG_CRITICAL, HERE << "WARNING: dup2(FD " << fd2 << ", FD " << fd << ") failed: " << xstrerror());
1014
1015 if (ENFILE == errno || EMFILE == errno)
1016 fdAdjustReserved();
1017
1018 close(fd2);
1019
1020 return 0;
1021 }
1022 commResetSelect(fd);
1023
1024 close(fd2);
1025 fde *F = &fd_table[fd];
1026
1027 debugs(50, 3, "commResetFD: Reset socket FD " << fd << "->" << fd2 << " : family=" << new_family );
1028
1029 /* INET6: copy the new sockets family type to the FDE table */
1030 fd_table[fd].sock_family = new_family;
1031
1032 fd_table[fd].flags.called_connect = 0;
1033 /*
1034 * yuck, this has assumptions about comm_open() arguments for
1035 * the original socket
1036 */
1037
1038 /* MUST be done before binding or face OS Error: "(99) Cannot assign requested address"... */
1039 if ( F->flags.transparent ) {
1040 comm_set_transparent(fd);
1041 }
1042
1043 AI = NULL;
1044 F->local_addr.GetAddrInfo(AI);
1045
1046 if (commBind(fd, *AI) != COMM_OK) {
1047 debugs(5, DBG_CRITICAL, "WARNING: Reset of FD " << fd << " for " << F->local_addr << " failed to bind: " << xstrerror());
1048 F->local_addr.FreeAddrInfo(AI);
1049 return 0;
1050 }
1051 F->local_addr.FreeAddrInfo(AI);
1052
1053 if (F->tos)
1054 comm_set_tos(fd, F->tos);
1055
1056 #if IPV6_SPECIAL_SPLITSTACK
1057 if ( F->local_addr.IsIPv6() )
1058 comm_set_v6only(fd, 1);
1059 #endif
1060
1061 copyFDFlags(fd, F);
1062
1063 return 1;
1064 }
1065
1066 int
1067 ConnectStateData::commRetryConnect()
1068 {
1069 assert(addrcount > 0);
1070
1071 if (addrcount == 1) {
1072 if (tries >= Config.retry.maxtries)
1073 return 0;
1074
1075 if (squid_curtime - connstart > Config.Timeout.connect)
1076 return 0;
1077 } else {
1078 if (tries > addrcount)
1079 return 0;
1080 }
1081
1082 return commResetFD();
1083 }
1084
1085 static void
1086 commReconnect(void *data)
1087 {
1088 ConnectStateData *cs = (ConnectStateData *)data;
1089 ipcache_nbgethostbyname(cs->host, commConnectDnsHandle, cs);
1090 }
1091
1092 /** Connect SOCK to specified DEST_PORT at DEST_HOST. */
1093 void
1094 ConnectStateData::Connect(int fd, void *me)
1095 {
1096 ConnectStateData *cs = (ConnectStateData *)me;
1097 assert (cs->fd == fd);
1098 cs->connect();
1099 }
1100
1101 void
1102 ConnectStateData::defaults()
1103 {
1104 S = default_addr;
1105 S.SetPort(default_port);
1106 }
1107
1108 void
1109 ConnectStateData::connect()
1110 {
1111 if (S.IsAnyAddr())
1112 defaults();
1113
1114 debugs(5,5, HERE << "to " << S);
1115
1116 switch (comm_connect_addr(fd, S) ) {
1117
1118 case COMM_INPROGRESS:
1119 debugs(5, 5, HERE << "FD " << fd << ": COMM_INPROGRESS");
1120 commSetSelect(fd, COMM_SELECT_WRITE, ConnectStateData::Connect, this, 0);
1121 break;
1122
1123 case COMM_OK:
1124 debugs(5, 5, HERE << "FD " << fd << ": COMM_OK - connected");
1125 ipcacheMarkGoodAddr(host, S);
1126 callCallback(COMM_OK, 0);
1127 break;
1128
1129 #if USE_IPV6
1130 case COMM_ERR_PROTOCOL:
1131 /* problem using the desired protocol over this socket.
1132 * count the connection attempt, reset the socket, and immediately try again */
1133 tries++;
1134 commResetFD();
1135 connect();
1136 break;
1137 #endif
1138
1139 default:
1140 debugs(5, 5, HERE "FD " << fd << ": * - try again");
1141 tries++;
1142 ipcacheMarkBadAddr(host, S);
1143
1144 #if USE_ICMP
1145 if (Config.onoff.test_reachability)
1146 netdbDeleteAddrNetwork(S);
1147 #endif
1148
1149 if (commRetryConnect()) {
1150 eventAdd("commReconnect", commReconnect, this, this->addrcount == 1 ? 0.05 : 0.0, 0);
1151 } else {
1152 debugs(5, 5, HERE << "FD " << fd << ": * - ERR tried too many times already.");
1153 callCallback(COMM_ERR_CONNECT, errno);
1154 }
1155 }
1156 }
1157 /*
1158 int
1159 commSetTimeout_old(int fd, int timeout, PF * handler, void *data)
1160 {
1161 debugs(5, 3, HERE << "FD " << fd << " timeout " << timeout);
1162 assert(fd >= 0);
1163 assert(fd < Squid_MaxFD);
1164 fde *F = &fd_table[fd];
1165 assert(F->flags.open);
1166
1167 if (timeout < 0) {
1168 cbdataReferenceDone(F->timeout_data);
1169 F->timeout_handler = NULL;
1170 F->timeout = 0;
1171 } else {
1172 if (handler) {
1173 cbdataReferenceDone(F->timeout_data);
1174 F->timeout_handler = handler;
1175 F->timeout_data = cbdataReference(data);
1176 }
1177
1178 F->timeout = squid_curtime + (time_t) timeout;
1179 }
1180
1181 return F->timeout;
1182 }
1183 */
1184
1185 int
1186 commSetTimeout(int fd, int timeout, PF * handler, void *data)
1187 {
1188 AsyncCall::Pointer call;
1189 debugs(5, 3, HERE << "FD " << fd << " timeout " << timeout);
1190 if (handler != NULL)
1191 call=commCbCall(5,4, "SomeTimeoutHandler", CommTimeoutCbPtrFun(handler, data));
1192 else
1193 call = NULL;
1194 return commSetTimeout(fd, timeout, call);
1195 }
1196
1197
1198 int commSetTimeout(int fd, int timeout, AsyncCall::Pointer &callback)
1199 {
1200 debugs(5, 3, HERE << "FD " << fd << " timeout " << timeout);
1201 assert(fd >= 0);
1202 assert(fd < Squid_MaxFD);
1203 fde *F = &fd_table[fd];
1204 assert(F->flags.open);
1205
1206 if (timeout < 0) {
1207 F->timeoutHandler = NULL;
1208 F->timeout = 0;
1209 } else {
1210 if (callback != NULL) {
1211 typedef CommTimeoutCbParams Params;
1212 Params &params = GetCommParams<Params>(callback);
1213 params.fd = fd;
1214 F->timeoutHandler = callback;
1215 }
1216
1217 F->timeout = squid_curtime + (time_t) timeout;
1218 }
1219
1220 return F->timeout;
1221
1222 }
1223
1224 int
1225 comm_connect_addr(int sock, const IpAddress &address)
1226 {
1227 comm_err_t status = COMM_OK;
1228 fde *F = &fd_table[sock];
1229 int x = 0;
1230 int err = 0;
1231 socklen_t errlen;
1232 struct addrinfo *AI = NULL;
1233 PROF_start(comm_connect_addr);
1234
1235 assert(address.GetPort() != 0);
1236
1237 debugs(5, 9, "comm_connect_addr: connecting socket " << sock << " to " << address << " (want family: " << F->sock_family << ")");
1238
1239 /* BUG 2222 FIX: reset the FD when its found to be IPv4 in IPv6 mode */
1240 /* inverse case of IPv4 failing to connect on IPv6 socket is handeld post-connect.
1241 * this case must presently be handled here since the GetAddrInfo asserts on bad mappings.
1242 * eventually we want it to throw a Must() that gets handled there instead of this if.
1243 * NP: because commresetFD is private to ConnStateData we have to return an error and
1244 * trust its handled properly.
1245 */
1246 #if USE_IPV6
1247 if (F->sock_family == AF_INET && !address.IsIPv4()) {
1248 return COMM_ERR_PROTOCOL;
1249 }
1250 #endif
1251
1252 address.GetAddrInfo(AI, F->sock_family);
1253
1254 /* Establish connection. */
1255 errno = 0;
1256
1257 if (!F->flags.called_connect) {
1258 F->flags.called_connect = 1;
1259 statCounter.syscalls.sock.connects++;
1260
1261 x = connect(sock, AI->ai_addr, AI->ai_addrlen);
1262
1263 // XXX: ICAP code refuses callbacks during a pending comm_ call
1264 // Async calls development will fix this.
1265 if (x == 0) {
1266 x = -1;
1267 errno = EINPROGRESS;
1268 }
1269
1270 if (x < 0) {
1271 debugs(5,5, "comm_connect_addr: sock=" << sock << ", addrinfo( " <<
1272 " flags=" << AI->ai_flags <<
1273 ", family=" << AI->ai_family <<
1274 ", socktype=" << AI->ai_socktype <<
1275 ", protocol=" << AI->ai_protocol <<
1276 ", &addr=" << AI->ai_addr <<
1277 ", addrlen=" << AI->ai_addrlen <<
1278 " )" );
1279 debugs(5, 9, "connect FD " << sock << ": (" << x << ") " << xstrerror());
1280 debugs(14,9, "connecting to: " << address );
1281 }
1282 } else {
1283 #if defined(_SQUID_NEWSOS6_)
1284 /* Makoto MATSUSHITA <matusita@ics.es.osaka-u.ac.jp> */
1285
1286 connect(sock, AI->ai_addr, AI->ai_addrlen);
1287
1288 if (errno == EINVAL) {
1289 errlen = sizeof(err);
1290 x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen);
1291
1292 if (x >= 0)
1293 errno = x;
1294 }
1295
1296 #else
1297 errlen = sizeof(err);
1298
1299 x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen);
1300
1301 if (x == 0)
1302 errno = err;
1303
1304 #if defined(_SQUID_SOLARIS_)
1305 /*
1306 * Solaris 2.4's socket emulation doesn't allow you
1307 * to determine the error from a failed non-blocking
1308 * connect and just returns EPIPE. Create a fake
1309 * error message for connect. -- fenner@parc.xerox.com
1310 */
1311 if (x < 0 && errno == EPIPE)
1312 errno = ENOTCONN;
1313
1314 #endif
1315 #endif
1316
1317 }
1318
1319 /* Squid seems to be working fine without this code. With this code,
1320 * we leak memory on many connect requests because of EINPROGRESS.
1321 * If you find that this code is needed, please file a bug report. */
1322 #if 0
1323 #ifdef _SQUID_LINUX_
1324 /* 2007-11-27:
1325 * Linux Debian replaces our allocated AI pointer with garbage when
1326 * connect() fails. This leads to segmentation faults deallocating
1327 * the system-allocated memory when we go to clean up our pointer.
1328 * HACK: is to leak the memory returned since we can't deallocate.
1329 */
1330 if (errno != 0) {
1331 AI = NULL;
1332 }
1333 #endif
1334 #endif
1335
1336 address.FreeAddrInfo(AI);
1337
1338 PROF_stop(comm_connect_addr);
1339
1340 if (errno == 0 || errno == EISCONN)
1341 status = COMM_OK;
1342 else if (ignoreErrno(errno))
1343 status = COMM_INPROGRESS;
1344 else
1345 #if USE_IPV6
1346 if ( F->sock_family == AF_INET6 && address.IsIPv4() ) {
1347
1348 /* failover to trying IPv4-only link if an IPv6 one fails */
1349 /* to catch the edge case of apps listening on IPv4-localhost */
1350 F->sock_family = AF_INET;
1351 int res = comm_connect_addr(sock, address);
1352
1353 /* if that fails too, undo our temporary socktype hack so the repeat works properly. */
1354 if (res == COMM_ERROR)
1355 F->sock_family = AF_INET6;
1356
1357 return res;
1358 } else
1359 #endif
1360 return COMM_ERROR;
1361
1362 address.NtoA(F->ipaddr, MAX_IPSTRLEN);
1363
1364 F->remote_port = address.GetPort(); /* remote_port is HS */
1365
1366 if (status == COMM_OK) {
1367 debugs(5, 10, "comm_connect_addr: FD " << sock << " connected to " << address);
1368 } else if (status == COMM_INPROGRESS) {
1369 debugs(5, 10, "comm_connect_addr: FD " << sock << " connection pending");
1370 }
1371
1372 return status;
1373 }
1374
1375 /* Wait for an incoming connection on FD. FD should be a socket returned
1376 * from comm_listen. */
1377 static int
1378 comm_old_accept(int fd, ConnectionDetail &details)
1379 {
1380 PROF_start(comm_accept);
1381 statCounter.syscalls.sock.accepts++;
1382 int sock;
1383 struct addrinfo *gai = NULL;
1384 details.me.InitAddrInfo(gai);
1385
1386 if ((sock = accept(fd, gai->ai_addr, &gai->ai_addrlen)) < 0) {
1387
1388 details.me.FreeAddrInfo(gai);
1389
1390 PROF_stop(comm_accept);
1391
1392 if (ignoreErrno(errno)) {
1393 debugs(50, 5, "comm_old_accept: FD " << fd << ": " << xstrerror());
1394 return COMM_NOMESSAGE;
1395 } else if (ENFILE == errno || EMFILE == errno) {
1396 debugs(50, 3, "comm_old_accept: FD " << fd << ": " << xstrerror());
1397 return COMM_ERROR;
1398 } else {
1399 debugs(50, 1, "comm_old_accept: FD " << fd << ": " << xstrerror());
1400 return COMM_ERROR;
1401 }
1402 }
1403
1404 details.peer = *gai;
1405
1406 details.me.InitAddrInfo(gai);
1407
1408 details.me.SetEmpty();
1409 getsockname(sock, gai->ai_addr, &gai->ai_addrlen);
1410 details.me = *gai;
1411
1412 commSetCloseOnExec(sock);
1413
1414 /* fdstat update */
1415 fd_open(sock, FD_SOCKET, "HTTP Request");
1416 fdd_table[sock].close_file = NULL;
1417 fdd_table[sock].close_line = 0;
1418 fde *F = &fd_table[sock];
1419 details.peer.NtoA(F->ipaddr,MAX_IPSTRLEN);
1420 F->remote_port = details.peer.GetPort();
1421 F->local_addr.SetPort(details.me.GetPort());
1422 #if USE_IPV6
1423 F->sock_family = AF_INET;
1424 #else
1425 F->sock_family = details.me.IsIPv4()?AF_INET:AF_INET6;
1426 #endif
1427 details.me.FreeAddrInfo(gai);
1428
1429 commSetNonBlocking(sock);
1430
1431 /* IFF the socket is (tproxy) transparent, pass the flag down to allow spoofing */
1432 F->flags.transparent = fd_table[fd].flags.transparent;
1433
1434 PROF_stop(comm_accept);
1435 return sock;
1436 }
1437
1438 void
1439 commCallCloseHandlers(int fd)
1440 {
1441 fde *F = &fd_table[fd];
1442 debugs(5, 5, "commCallCloseHandlers: FD " << fd);
1443
1444 while (F->closeHandler != NULL) {
1445 AsyncCall::Pointer call = F->closeHandler;
1446 F->closeHandler = call->Next();
1447 call->setNext(NULL);
1448 // If call is not canceled schedule it for execution else ignore it
1449 if (!call->canceled()) {
1450 debugs(5, 5, "commCallCloseHandlers: ch->handler=" << call);
1451 typedef CommCloseCbParams Params;
1452 Params &params = GetCommParams<Params>(call);
1453 params.fd = fd;
1454 ScheduleCallHere(call);
1455 }
1456 }
1457 }
1458
1459 #if LINGERING_CLOSE
1460 static void
1461 commLingerClose(int fd, void *unused)
1462 {
1463 LOCAL_ARRAY(char, buf, 1024);
1464 int n;
1465 n = FD_READ_METHOD(fd, buf, 1024);
1466
1467 if (n < 0)
1468 debugs(5, 3, "commLingerClose: FD " << fd << " read: " << xstrerror());
1469
1470 comm_close(fd);
1471 }
1472
1473 static void
1474 commLingerTimeout(int fd, void *unused)
1475 {
1476 debugs(5, 3, "commLingerTimeout: FD " << fd);
1477 comm_close(fd);
1478 }
1479
1480 /*
1481 * Inspired by apache
1482 */
1483 void
1484 comm_lingering_close(int fd)
1485 {
1486 #if USE_SSL
1487
1488 if (fd_table[fd].ssl)
1489 ssl_shutdown_method(fd);
1490
1491 #endif
1492
1493 if (shutdown(fd, 1) < 0) {
1494 comm_close(fd);
1495 return;
1496 }
1497
1498 fd_note(fd, "lingering close");
1499 commSetTimeout(fd, 10, commLingerTimeout, NULL);
1500 commSetSelect(fd, COMM_SELECT_READ, commLingerClose, NULL, 0);
1501 }
1502
1503 #endif
1504
1505 /*
1506 * enable linger with time of 0 so that when the socket is
1507 * closed, TCP generates a RESET
1508 */
1509 void
1510 comm_reset_close(int fd)
1511 {
1512
1513 struct linger L;
1514 L.l_onoff = 1;
1515 L.l_linger = 0;
1516
1517 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0)
1518 debugs(50, 0, "commResetTCPClose: FD " << fd << ": " << xstrerror());
1519
1520 comm_close(fd);
1521 }
1522
1523 void
1524 comm_close_start(int fd, void *data)
1525 {
1526 #if USE_SSL
1527 fde *F = &fd_table[fd];
1528 if (F->ssl)
1529 ssl_shutdown_method(fd);
1530
1531 #endif
1532
1533 }
1534
1535
1536 void
1537 comm_close_complete(int fd, void *data)
1538 {
1539 #if USE_SSL
1540 fde *F = &fd_table[fd];
1541
1542 if (F->ssl) {
1543 SSL_free(F->ssl);
1544 F->ssl = NULL;
1545 }
1546
1547 #endif
1548 fd_close(fd); /* update fdstat */
1549
1550 close(fd);
1551
1552 fdc_table[fd] = AcceptFD(fd);
1553
1554 statCounter.syscalls.sock.closes++;
1555
1556 /* When an fd closes, give accept() a chance, if need be */
1557
1558 if (fdNFree() >= RESERVED_FD)
1559 AcceptLimiter::Instance().kick();
1560
1561 }
1562
1563 /*
1564 * Close the socket fd.
1565 *
1566 * + call write handlers with ERR_CLOSING
1567 * + call read handlers with ERR_CLOSING
1568 * + call closing handlers
1569 *
1570 * NOTE: COMM_ERR_CLOSING will NOT be called for CommReads' sitting in a
1571 * DeferredReadManager.
1572 */
1573 void
1574 _comm_close(int fd, char const *file, int line)
1575 {
1576 debugs(5, 3, "comm_close: start closing FD " << fd);
1577 assert(fd >= 0);
1578 assert(fd < Squid_MaxFD);
1579
1580 fde *F = &fd_table[fd];
1581 fdd_table[fd].close_file = file;
1582 fdd_table[fd].close_line = line;
1583
1584 if (F->closing())
1585 return;
1586
1587 /* XXX: is this obsolete behind F->closing() ? */
1588 if ( (shutting_down || reconfiguring) && (!F->flags.open || F->type == FD_FILE))
1589 return;
1590
1591 /* The following fails because ipc.c is doing calls to pipe() to create sockets! */
1592 assert(isOpen(fd));
1593
1594 assert(F->type != FD_FILE);
1595
1596 PROF_start(comm_close);
1597
1598 F->flags.close_request = 1;
1599
1600 AsyncCall::Pointer startCall=commCbCall(5,4, "comm_close_start",
1601 CommCloseCbPtrFun(comm_close_start, NULL));
1602 typedef CommCloseCbParams Params;
1603 Params &startParams = GetCommParams<Params>(startCall);
1604 startParams.fd = fd;
1605 ScheduleCallHere(startCall);
1606
1607 // a half-closed fd may lack a reader, so we stop monitoring explicitly
1608 if (commHasHalfClosedMonitor(fd))
1609 commStopHalfClosedMonitor(fd);
1610 commSetTimeout(fd, -1, NULL, NULL);
1611
1612 // notify read/write handlers
1613 if (commio_has_callback(fd, IOCB_WRITE, COMMIO_FD_WRITECB(fd))) {
1614 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), COMM_ERR_CLOSING, errno);
1615 }
1616 if (commio_has_callback(fd, IOCB_READ, COMMIO_FD_READCB(fd))) {
1617 commio_finish_callback(fd, COMMIO_FD_READCB(fd), COMM_ERR_CLOSING, errno);
1618 }
1619
1620 // notify accept handlers
1621 fdc_table[fd].notify(-1, COMM_ERR_CLOSING, 0, ConnectionDetail());
1622
1623 commCallCloseHandlers(fd);
1624
1625 if (F->pconn.uses)
1626 F->pconn.pool->count(F->pconn.uses);
1627
1628 comm_empty_os_read_buffers(fd);
1629
1630
1631 AsyncCall::Pointer completeCall=commCbCall(5,4, "comm_close_complete",
1632 CommCloseCbPtrFun(comm_close_complete, NULL));
1633 Params &completeParams = GetCommParams<Params>(completeCall);
1634 completeParams.fd = fd;
1635 // must use async call to wait for all callbacks
1636 // scheduled before comm_close() to finish
1637 ScheduleCallHere(completeCall);
1638
1639 PROF_stop(comm_close);
1640 }
1641
1642 /* Send a udp datagram to specified TO_ADDR. */
1643 int
1644 comm_udp_sendto(int fd,
1645 const IpAddress &to_addr,
1646 const void *buf,
1647 int len)
1648 {
1649 int x = 0;
1650 struct addrinfo *AI = NULL;
1651
1652 PROF_start(comm_udp_sendto);
1653 statCounter.syscalls.sock.sendtos++;
1654
1655 debugs(50, 3, "comm_udp_sendto: Attempt to send UDP packet to " << to_addr <<
1656 " using FD " << fd << " using Port " << comm_local_port(fd) );
1657
1658 /* BUG: something in the above macro appears to occasionally be setting AI to garbage. */
1659 /* AYJ: 2007-08-27 : or was it because I wasn't then setting 'fd_table[fd].sock_family' to fill properly. */
1660 assert( NULL == AI );
1661
1662 to_addr.GetAddrInfo(AI, fd_table[fd].sock_family);
1663
1664 x = sendto(fd, buf, len, 0, AI->ai_addr, AI->ai_addrlen);
1665
1666 to_addr.FreeAddrInfo(AI);
1667
1668 PROF_stop(comm_udp_sendto);
1669
1670 if (x >= 0)
1671 return x;
1672
1673 #ifdef _SQUID_LINUX_
1674
1675 if (ECONNREFUSED != errno)
1676 #endif
1677
1678 debugs(50, 1, "comm_udp_sendto: FD " << fd << ", (family=" << fd_table[fd].sock_family << ") " << to_addr << ": " << xstrerror());
1679
1680 return COMM_ERROR;
1681 }
1682
1683 void
1684 comm_add_close_handler(int fd, PF * handler, void *data)
1685 {
1686 debugs(5, 5, "comm_add_close_handler: FD " << fd << ", handler=" <<
1687 handler << ", data=" << data);
1688
1689 AsyncCall::Pointer call=commCbCall(5,4, "SomeCloseHandler",
1690 CommCloseCbPtrFun(handler, data));
1691 comm_add_close_handler(fd, call);
1692 }
1693
1694 void
1695 comm_add_close_handler(int fd, AsyncCall::Pointer &call)
1696 {
1697 debugs(5, 5, "comm_add_close_handler: FD " << fd << ", AsyncCall=" << call);
1698
1699 /*TODO:Check for a similar scheduled AsyncCall*/
1700 // for (c = fd_table[fd].closeHandler; c; c = c->next)
1701 // assert(c->handler != handler || c->data != data);
1702
1703 call->setNext(fd_table[fd].closeHandler);
1704
1705 fd_table[fd].closeHandler = call;
1706 }
1707
1708
1709 // remove function-based close handler
1710 void
1711 comm_remove_close_handler(int fd, PF * handler, void *data)
1712 {
1713 assert (isOpen(fd));
1714 /* Find handler in list */
1715 debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", handler=" <<
1716 handler << ", data=" << data);
1717
1718 AsyncCall::Pointer p;
1719 for (p = fd_table[fd].closeHandler; p != NULL; p = p->Next()) {
1720 typedef CommCbFunPtrCallT<CommCloseCbPtrFun> Call;
1721 const Call *call = dynamic_cast<const Call*>(p.getRaw());
1722 if (!call) // method callbacks have their own comm_remove_close_handler
1723 continue;
1724
1725 typedef CommCloseCbParams Params;
1726 const Params &params = GetCommParams<Params>(p);
1727 if (call->dialer.handler == handler && params.data == data)
1728 break; /* This is our handler */
1729 }
1730
1731 // comm_close removes all close handlers so our handler may be gone
1732 if (p != NULL)
1733 p->cancel("comm_remove_close_handler");
1734 // TODO: should we remove the handler from the close handlers list?
1735 }
1736
1737 // remove method-based close handler
1738 void
1739 comm_remove_close_handler(int fd, AsyncCall::Pointer &call)
1740 {
1741 assert (isOpen(fd));
1742 debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", AsyncCall=" << call);
1743
1744 // comm_close removes all close handlers so our handler may be gone
1745 // TODO: should we remove the handler from the close handlers list?
1746 #if 0
1747 // Check to see if really exist the given AsyncCall in comm_close handlers
1748 // TODO: optimize: this slow code is only needed for the assert() below
1749 AsyncCall::Pointer p;
1750 for (p = fd_table[fd].closeHandler; p != NULL && p != call; p = p->Next());
1751 assert(p == call);
1752 #endif
1753
1754 call->cancel("comm_remove_close_handler");
1755 }
1756
1757 static void
1758 commSetNoLinger(int fd)
1759 {
1760
1761 struct linger L;
1762 L.l_onoff = 0; /* off */
1763 L.l_linger = 0;
1764
1765 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0)
1766 debugs(50, 0, "commSetNoLinger: FD " << fd << ": " << xstrerror());
1767
1768 fd_table[fd].flags.nolinger = 1;
1769 }
1770
1771 static void
1772 commSetReuseAddr(int fd)
1773 {
1774 int on = 1;
1775
1776 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0)
1777 debugs(50, 1, "commSetReuseAddr: FD " << fd << ": " << xstrerror());
1778 }
1779
1780 static void
1781 commSetTcpRcvbuf(int fd, int size)
1782 {
1783 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size)) < 0)
1784 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd << ", SIZE " << size << ": " << xstrerror());
1785 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size)) < 0)
1786 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd << ", SIZE " << size << ": " << xstrerror());
1787 #ifdef TCP_WINDOW_CLAMP
1788 if (setsockopt(fd, SOL_TCP, TCP_WINDOW_CLAMP, (char *) &size, sizeof(size)) < 0)
1789 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd << ", SIZE " << size << ": " << xstrerror());
1790 #endif
1791 }
1792
1793 int
1794 commSetNonBlocking(int fd)
1795 {
1796 #ifndef _SQUID_MSWIN_
1797 int flags;
1798 int dummy = 0;
1799 #endif
1800 #ifdef _SQUID_WIN32_
1801
1802 int nonblocking = TRUE;
1803
1804 #ifdef _SQUID_CYGWIN_
1805
1806 if (fd_table[fd].type != FD_PIPE) {
1807 #endif
1808
1809 if (ioctl(fd, FIONBIO, &nonblocking) < 0) {
1810 debugs(50, 0, "commSetNonBlocking: FD " << fd << ": " << xstrerror() << " " << fd_table[fd].type);
1811 return COMM_ERROR;
1812 }
1813
1814 #ifdef _SQUID_CYGWIN_
1815
1816 } else {
1817 #endif
1818 #endif
1819 #ifndef _SQUID_MSWIN_
1820
1821 if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
1822 debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror());
1823 return COMM_ERROR;
1824 }
1825
1826 if (fcntl(fd, F_SETFL, flags | SQUID_NONBLOCK) < 0) {
1827 debugs(50, 0, "commSetNonBlocking: FD " << fd << ": " << xstrerror());
1828 return COMM_ERROR;
1829 }
1830
1831 #endif
1832 #ifdef _SQUID_CYGWIN_
1833
1834 }
1835
1836 #endif
1837 fd_table[fd].flags.nonblocking = 1;
1838
1839 return 0;
1840 }
1841
1842 int
1843 commUnsetNonBlocking(int fd)
1844 {
1845 #ifdef _SQUID_MSWIN_
1846 int nonblocking = FALSE;
1847
1848 if (ioctlsocket(fd, FIONBIO, (unsigned long *) &nonblocking) < 0) {
1849 #else
1850 int flags;
1851 int dummy = 0;
1852
1853 if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
1854 debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror());
1855 return COMM_ERROR;
1856 }
1857
1858 if (fcntl(fd, F_SETFL, flags & (~SQUID_NONBLOCK)) < 0) {
1859 #endif
1860 debugs(50, 0, "commUnsetNonBlocking: FD " << fd << ": " << xstrerror());
1861 return COMM_ERROR;
1862 }
1863
1864 fd_table[fd].flags.nonblocking = 0;
1865 return 0;
1866 }
1867
1868 void
1869 commSetCloseOnExec(int fd)
1870 {
1871 #ifdef FD_CLOEXEC
1872 int flags;
1873 int dummy = 0;
1874
1875 if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
1876 debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror());
1877 return;
1878 }
1879
1880 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
1881 debugs(50, 0, "FD " << fd << ": set close-on-exec failed: " << xstrerror());
1882
1883 fd_table[fd].flags.close_on_exec = 1;
1884
1885 #endif
1886 }
1887
1888 #ifdef TCP_NODELAY
1889 static void
1890 commSetTcpNoDelay(int fd)
1891 {
1892 int on = 1;
1893
1894 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) < 0)
1895 debugs(50, 1, "commSetTcpNoDelay: FD " << fd << ": " << xstrerror());
1896
1897 fd_table[fd].flags.nodelay = 1;
1898 }
1899
1900 #endif
1901
1902 void
1903 commSetTcpKeepalive(int fd, int idle, int interval, int timeout)
1904 {
1905 int on = 1;
1906 #ifdef TCP_KEEPCNT
1907 if (timeout && interval) {
1908 int count = (timeout + interval - 1) / interval;
1909 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &count, sizeof(on)) < 0)
1910 debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror());
1911 }
1912 #endif
1913 #ifdef TCP_KEEPIDLE
1914 if (idle) {
1915 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(on)) < 0)
1916 debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror());
1917 }
1918 #endif
1919 #ifdef TCP_KEEPINTVL
1920 if (interval) {
1921 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(on)) < 0)
1922 debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror());
1923 }
1924 #endif
1925 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &on, sizeof(on)) < 0)
1926 debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror());
1927 }
1928
1929 void
1930 comm_init(void)
1931 {
1932 fd_table =(fde *) xcalloc(Squid_MaxFD, sizeof(fde));
1933 fdd_table = (fd_debug_t *)xcalloc(Squid_MaxFD, sizeof(fd_debug_t));
1934
1935 fdc_table = new AcceptFD[Squid_MaxFD];
1936 for (int pos = 0; pos < Squid_MaxFD; ++pos) {
1937 fdc_table[pos] = AcceptFD(pos);
1938 }
1939
1940 commfd_table = (comm_fd_t *) xcalloc(Squid_MaxFD, sizeof(comm_fd_t));
1941 for (int pos = 0; pos < Squid_MaxFD; pos++) {
1942 commfd_table[pos].fd = pos;
1943 commfd_table[pos].readcb.fd = pos;
1944 commfd_table[pos].readcb.type = IOCB_READ;
1945 commfd_table[pos].writecb.fd = pos;
1946 commfd_table[pos].writecb.type = IOCB_WRITE;
1947 }
1948
1949 /* XXX account fd_table */
1950 /* Keep a few file descriptors free so that we don't run out of FD's
1951 * after accepting a client but before it opens a socket or a file.
1952 * Since Squid_MaxFD can be as high as several thousand, don't waste them */
1953 RESERVED_FD = min(100, Squid_MaxFD / 4);
1954
1955 conn_close_pool = memPoolCreate("close_handler", sizeof(close_handler));
1956
1957 TheHalfClosed = new DescriptorSet;
1958 }
1959
1960 void
1961 comm_exit(void)
1962 {
1963 delete TheHalfClosed;
1964 TheHalfClosed = NULL;
1965
1966 safe_free(fd_table);
1967 safe_free(fdd_table);
1968 if (fdc_table) {
1969 delete[] fdc_table;
1970 fdc_table = NULL;
1971 }
1972 safe_free(commfd_table);
1973 }
1974
1975 /* Write to FD. */
1976 static void
1977 commHandleWrite(int fd, void *data)
1978 {
1979 comm_io_callback_t *state = (comm_io_callback_t *)data;
1980 int len = 0;
1981 int nleft;
1982
1983 assert(state == COMMIO_FD_WRITECB(fd));
1984
1985 PROF_start(commHandleWrite);
1986 debugs(5, 5, "commHandleWrite: FD " << fd << ": off " <<
1987 (long int) state->offset << ", sz " << (long int) state->size << ".");
1988
1989 nleft = state->size - state->offset;
1990 len = FD_WRITE_METHOD(fd, state->buf + state->offset, nleft);
1991 debugs(5, 5, "commHandleWrite: write() returns " << len);
1992 fd_bytes(fd, len, FD_WRITE);
1993 statCounter.syscalls.sock.writes++;
1994 // After each successful partial write,
1995 // reset fde::writeStart to the current time.
1996 fd_table[fd].writeStart = squid_curtime;
1997
1998 if (len == 0) {
1999 /* Note we even call write if nleft == 0 */
2000 /* We're done */
2001
2002 if (nleft != 0)
2003 debugs(5, 1, "commHandleWrite: FD " << fd << ": write failure: connection closed with " << nleft << " bytes remaining.");
2004
2005 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_ERROR : COMM_OK, errno);
2006 } else if (len < 0) {
2007 /* An error */
2008
2009 if (fd_table[fd].flags.socket_eof) {
2010 debugs(50, 2, "commHandleWrite: FD " << fd << ": write failure: " << xstrerror() << ".");
2011 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_ERROR : COMM_OK, errno);
2012 } else if (ignoreErrno(errno)) {
2013 debugs(50, 10, "commHandleWrite: FD " << fd << ": write failure: " << xstrerror() << ".");
2014 commSetSelect(fd,
2015 COMM_SELECT_WRITE,
2016 commHandleWrite,
2017 state,
2018 0);
2019 } else {
2020 debugs(50, 2, "commHandleWrite: FD " << fd << ": write failure: " << xstrerror() << ".");
2021 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_ERROR : COMM_OK, errno);
2022 }
2023 } else {
2024 /* A successful write, continue */
2025 state->offset += len;
2026
2027 if (state->offset < state->size) {
2028 /* Not done, reinstall the write handler and write some more */
2029 commSetSelect(fd,
2030 COMM_SELECT_WRITE,
2031 commHandleWrite,
2032 state,
2033 0);
2034 } else {
2035 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_OK : COMM_ERROR, errno);
2036 }
2037 }
2038
2039 PROF_stop(commHandleWrite);
2040 }
2041
2042 /*
2043 * Queue a write. handler/handler_data are called when the write
2044 * completes, on error, or on file descriptor close.
2045 *
2046 * free_func is used to free the passed buffer when the write has completed.
2047 */
2048 void
2049 comm_write(int fd, const char *buf, int size, IOCB * handler, void *handler_data, FREE * free_func)
2050 {
2051 AsyncCall::Pointer call = commCbCall(5,5, "SomeCommWriteHander",
2052 CommIoCbPtrFun(handler, handler_data));
2053
2054 comm_write(fd, buf, size, call, free_func);
2055 }
2056
2057 void
2058 comm_write(int fd, const char *buf, int size, AsyncCall::Pointer &callback, FREE * free_func)
2059 {
2060 debugs(5, 5, "comm_write: FD " << fd << ": sz " << size << ": asynCall " << callback);
2061
2062 /* Make sure we are open, not closing, and not writing */
2063 assert(isOpen(fd));
2064 assert(!fd_table[fd].closing());
2065 comm_io_callback_t *ccb = COMMIO_FD_WRITECB(fd);
2066 assert(!ccb->active());
2067
2068 fd_table[fd].writeStart = squid_curtime;
2069 /* Queue the write */
2070 commio_set_callback(fd, IOCB_WRITE, ccb, callback,
2071 (char *)buf, free_func, size);
2072 commSetSelect(fd, COMM_SELECT_WRITE, commHandleWrite, ccb, 0);
2073 }
2074
2075
2076 /* a wrapper around comm_write to allow for MemBuf to be comm_written in a snap */
2077 void
2078 comm_write_mbuf(int fd, MemBuf *mb, IOCB * handler, void *handler_data)
2079 {
2080 comm_write(fd, mb->buf, mb->size, handler, handler_data, mb->freeFunc());
2081 }
2082
2083 void
2084 comm_write_mbuf(int fd, MemBuf *mb, AsyncCall::Pointer &callback)
2085 {
2086 comm_write(fd, mb->buf, mb->size, callback, mb->freeFunc());
2087 }
2088
2089
2090 /*
2091 * hm, this might be too general-purpose for all the places we'd
2092 * like to use it.
2093 */
2094 int
2095 ignoreErrno(int ierrno)
2096 {
2097 switch (ierrno) {
2098
2099 case EINPROGRESS:
2100
2101 case EWOULDBLOCK:
2102 #if EAGAIN != EWOULDBLOCK
2103
2104 case EAGAIN:
2105 #endif
2106
2107 case EALREADY:
2108
2109 case EINTR:
2110 #ifdef ERESTART
2111
2112 case ERESTART:
2113 #endif
2114
2115 return 1;
2116
2117 default:
2118 return 0;
2119 }
2120
2121 /* NOTREACHED */
2122 }
2123
2124 void
2125 commCloseAllSockets(void)
2126 {
2127 int fd;
2128 fde *F = NULL;
2129
2130 for (fd = 0; fd <= Biggest_FD; fd++) {
2131 F = &fd_table[fd];
2132
2133 if (!F->flags.open)
2134 continue;
2135
2136 if (F->type != FD_SOCKET)
2137 continue;
2138
2139 if (F->flags.ipc) /* don't close inter-process sockets */
2140 continue;
2141
2142 if (F->timeoutHandler != NULL) {
2143 AsyncCall::Pointer callback = F->timeoutHandler;
2144 F->timeoutHandler = NULL;
2145 debugs(5, 5, "commCloseAllSockets: FD " << fd << ": Calling timeout handler");
2146 ScheduleCallHere(callback);
2147 } else {
2148 debugs(5, 5, "commCloseAllSockets: FD " << fd << ": calling comm_close()");
2149 comm_close(fd);
2150 }
2151 }
2152 }
2153
2154 static bool
2155 AlreadyTimedOut(fde *F)
2156 {
2157 if (!F->flags.open)
2158 return true;
2159
2160 if (F->timeout == 0)
2161 return true;
2162
2163 if (F->timeout > squid_curtime)
2164 return true;
2165
2166 return false;
2167 }
2168
2169 static bool
2170 writeTimedOut(int fd)
2171 {
2172 if (!commio_has_callback(fd, IOCB_WRITE, COMMIO_FD_WRITECB(fd)))
2173 return false;
2174
2175 if ((squid_curtime - fd_table[fd].writeStart) < Config.Timeout.write)
2176 return false;
2177
2178 return true;
2179 }
2180
2181 void
2182 checkTimeouts(void)
2183 {
2184 int fd;
2185 fde *F = NULL;
2186 AsyncCall::Pointer callback;
2187
2188 for (fd = 0; fd <= Biggest_FD; fd++) {
2189 F = &fd_table[fd];
2190
2191 if (writeTimedOut(fd)) {
2192 // We have an active write callback and we are timed out
2193 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), COMM_ERROR, ETIMEDOUT);
2194 } else if (AlreadyTimedOut(F))
2195 continue;
2196
2197 debugs(5, 5, "checkTimeouts: FD " << fd << " Expired");
2198
2199 if (F->timeoutHandler != NULL) {
2200 debugs(5, 5, "checkTimeouts: FD " << fd << ": Call timeout handler");
2201 callback = F->timeoutHandler;
2202 F->timeoutHandler = NULL;
2203 ScheduleCallHere(callback);
2204 } else {
2205 debugs(5, 5, "checkTimeouts: FD " << fd << ": Forcing comm_close()");
2206 comm_close(fd);
2207 }
2208 }
2209 }
2210
2211 /*
2212 * New-style listen and accept routines
2213 *
2214 * Listen simply registers our interest in an FD for listening,
2215 * and accept takes a callback to call when an FD has been
2216 * accept()ed.
2217 */
2218 int
2219 comm_listen(int sock)
2220 {
2221 int x;
2222
2223 if ((x = listen(sock, Squid_MaxFD >> 2)) < 0) {
2224 debugs(50, 0, "comm_listen: listen(" << (Squid_MaxFD >> 2) << ", " << sock << "): " << xstrerror());
2225 return x;
2226 }
2227
2228 if (Config.accept_filter && strcmp(Config.accept_filter, "none") != 0) {
2229 #ifdef SO_ACCEPTFILTER
2230 struct accept_filter_arg afa;
2231 bzero(&afa, sizeof(afa));
2232 debugs(5, DBG_CRITICAL, "Installing accept filter '" << Config.accept_filter << "' on FD " << sock);
2233 xstrncpy(afa.af_name, Config.accept_filter, sizeof(afa.af_name));
2234 x = setsockopt(sock, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa));
2235 if (x < 0)
2236 debugs(5, 0, "SO_ACCEPTFILTER '" << Config.accept_filter << "': '" << xstrerror());
2237 #elif defined(TCP_DEFER_ACCEPT)
2238 int seconds = 30;
2239 if (strncmp(Config.accept_filter, "data=", 5) == 0)
2240 seconds = atoi(Config.accept_filter + 5);
2241 x = setsockopt(sock, IPPROTO_TCP, TCP_DEFER_ACCEPT, &seconds, sizeof(seconds));
2242 if (x < 0)
2243 debugs(5, 0, "TCP_DEFER_ACCEPT '" << Config.accept_filter << "': '" << xstrerror());
2244 #else
2245 debugs(5, 0, "accept_filter not supported on your OS");
2246 #endif
2247 }
2248
2249 return sock;
2250 }
2251
2252 void
2253 comm_accept(int fd, IOACB *handler, void *handler_data)
2254 {
2255 debugs(5, 5, "comm_accept: FD " << fd << " handler: " << (void*)handler);
2256 assert(isOpen(fd));
2257
2258 AsyncCall::Pointer call = commCbCall(5,5, "SomeCommAcceptHandler",
2259 CommAcceptCbPtrFun(handler, handler_data));
2260 fdc_table[fd].subscribe(call);
2261 }
2262
2263 void
2264 comm_accept(int fd, AsyncCall::Pointer &call)
2265 {
2266 debugs(5, 5, "comm_accept: FD " << fd << " AsyncCall: " << call);
2267 assert(isOpen(fd));
2268
2269 fdc_table[fd].subscribe(call);
2270 }
2271
2272 // Called when somebody wants to be notified when our socket accepts new
2273 // connection. We do not probe the FD until there is such interest.
2274 void
2275 AcceptFD::subscribe(AsyncCall::Pointer &call)
2276 {
2277 /* make sure we're not pending! */
2278 assert(!theCallback);
2279 theCallback = call;
2280
2281 #if OPTIMISTIC_IO
2282 mayAcceptMore = true; // even if we failed to accept last time
2283 #endif
2284
2285 if (mayAcceptMore)
2286 acceptNext();
2287 else
2288 commSetSelect(fd, COMM_SELECT_READ, comm_accept_try, NULL, 0);
2289 }
2290
2291 bool
2292 AcceptFD::acceptOne()
2293 {
2294 // If there is no callback and we accept, we will leak the accepted FD.
2295 // When we are running out of FDs, there is often no callback.
2296 if (!theCallback) {
2297 debugs(5, 5, "AcceptFD::acceptOne orphaned: FD " << fd);
2298 // XXX: can we remove this and similar "just in case" calls and
2299 // either listen always or listen only when there is a callback?
2300 if (!AcceptLimiter::Instance().deferring())
2301 commSetSelect(fd, COMM_SELECT_READ, comm_accept_try, NULL, 0);
2302 return false;
2303 }
2304
2305 /*
2306 * We don't worry about running low on FDs here. Instead,
2307 * httpAccept() will use AcceptLimiter if we reach the limit
2308 * there.
2309 */
2310
2311 /* Accept a new connection */
2312 ConnectionDetail connDetails;
2313 int newfd = comm_old_accept(fd, connDetails);
2314
2315 /* Check for errors */
2316
2317 if (newfd < 0) {
2318 assert(theCallback != NULL);
2319
2320 if (newfd == COMM_NOMESSAGE) {
2321 /* register interest again */
2322 debugs(5, 5, HERE << "try later: FD " << fd <<
2323 " handler: " << *theCallback);
2324 commSetSelect(fd, COMM_SELECT_READ, comm_accept_try, NULL, 0);
2325 return false;
2326 }
2327
2328 // A non-recoverable error; notify the caller */
2329 notify(-1, COMM_ERROR, errno, connDetails);
2330 return false;
2331 }
2332
2333 assert(theCallback != NULL);
2334 debugs(5, 5, "AcceptFD::acceptOne accepted: FD " << fd <<
2335 " newfd: " << newfd << " from: " << connDetails.peer <<
2336 " handler: " << *theCallback);
2337 notify(newfd, COMM_OK, 0, connDetails);
2338 return true;
2339 }
2340
2341 void
2342 AcceptFD::acceptNext()
2343 {
2344 mayAcceptMore = acceptOne();
2345 }
2346
2347 void
2348 AcceptFD::notify(int newfd, comm_err_t errcode, int xerrno, const ConnectionDetail &connDetails)
2349 {
2350 if (theCallback != NULL) {
2351 typedef CommAcceptCbParams Params;
2352 Params &params = GetCommParams<Params>(theCallback);
2353 params.fd = fd;
2354 params.nfd = newfd;
2355 params.details = connDetails;
2356 params.flag = errcode;
2357 params.xerrno = xerrno;
2358 ScheduleCallHere(theCallback);
2359 theCallback = NULL;
2360 }
2361 }
2362
2363 /*
2364 * This callback is called whenever a filedescriptor is ready
2365 * to dupe itself and fob off an accept()ed connection
2366 */
2367 static void
2368 comm_accept_try(int fd, void *)
2369 {
2370 assert(isOpen(fd));
2371 fdc_table[fd].acceptNext();
2372 }
2373
2374 void CommIO::Initialise()
2375 {
2376 /* Initialize done pipe signal */
2377 int DonePipe[2];
2378 if (pipe(DonePipe)) {}
2379 DoneFD = DonePipe[1];
2380 DoneReadFD = DonePipe[0];
2381 fd_open(DoneReadFD, FD_PIPE, "async-io completetion event: main");
2382 fd_open(DoneFD, FD_PIPE, "async-io completetion event: threads");
2383 commSetNonBlocking(DoneReadFD);
2384 commSetNonBlocking(DoneFD);
2385 commSetSelect(DoneReadFD, COMM_SELECT_READ, NULLFDHandler, NULL, 0);
2386 Initialised = true;
2387 }
2388
2389 void CommIO::NotifyIOClose()
2390 {
2391 /* Close done pipe signal */
2392 FlushPipe();
2393 close(DoneFD);
2394 close(DoneReadFD);
2395 fd_close(DoneFD);
2396 fd_close(DoneReadFD);
2397 Initialised = false;
2398 }
2399
2400 bool CommIO::Initialised = false;
2401 bool CommIO::DoneSignalled = false;
2402 int CommIO::DoneFD = -1;
2403 int CommIO::DoneReadFD = -1;
2404
2405 void
2406 CommIO::FlushPipe()
2407 {
2408 char buf[256];
2409 FD_READ_METHOD(DoneReadFD, buf, sizeof(buf));
2410 }
2411
2412 void
2413 CommIO::NULLFDHandler(int fd, void *data)
2414 {
2415 FlushPipe();
2416 commSetSelect(fd, COMM_SELECT_READ, NULLFDHandler, NULL, 0);
2417 }
2418
2419 void
2420 CommIO::ResetNotifications()
2421 {
2422 if (DoneSignalled) {
2423 FlushPipe();
2424 DoneSignalled = false;
2425 }
2426 }
2427
2428 AcceptLimiter AcceptLimiter::Instance_;
2429
2430 AcceptLimiter &AcceptLimiter::Instance()
2431 {
2432 return Instance_;
2433 }
2434
2435 bool
2436 AcceptLimiter::deferring() const
2437 {
2438 return deferred.size() > 0;
2439 }
2440
2441 void
2442 AcceptLimiter::defer (int fd, Acceptor::AcceptorFunction *aFunc, void *data)
2443 {
2444 debugs(5, 5, "AcceptLimiter::defer: FD " << fd << " handler: " << (void*)aFunc);
2445 Acceptor temp;
2446 temp.theFunction = aFunc;
2447 temp.acceptFD = fd;
2448 temp.theData = data;
2449 deferred.push_back(temp);
2450 }
2451
2452 void
2453 AcceptLimiter::kick()
2454 {
2455 if (!deferring())
2456 return;
2457
2458 /* Yes, this means the first on is the last off....
2459 * If the list container was a little more friendly, we could sensibly us it.
2460 */
2461 Acceptor temp = deferred.pop_back();
2462
2463 comm_accept (temp.acceptFD, temp.theFunction, temp.theData);
2464 }
2465
2466 /// Start waiting for a possibly half-closed connection to close
2467 // by scheduling a read callback to a monitoring handler that
2468 // will close the connection on read errors.
2469 void
2470 commStartHalfClosedMonitor(int fd)
2471 {
2472 debugs(5, 5, HERE << "adding FD " << fd << " to " << *TheHalfClosed);
2473 assert(isOpen(fd));
2474 assert(!commHasHalfClosedMonitor(fd));
2475 (void)TheHalfClosed->add(fd); // could also assert the result
2476 commPlanHalfClosedCheck(); // may schedule check if we added the first FD
2477 }
2478
2479 static
2480 void
2481 commPlanHalfClosedCheck()
2482 {
2483 if (!WillCheckHalfClosed && !TheHalfClosed->empty()) {
2484 eventAdd("commHalfClosedCheck", &commHalfClosedCheck, NULL, 1.0, 1);
2485 WillCheckHalfClosed = true;
2486 }
2487 }
2488
2489 /// iterates over all descriptors that may need half-closed tests and
2490 /// calls comm_read for those that do; re-schedules the check if needed
2491 static
2492 void
2493 commHalfClosedCheck(void *)
2494 {
2495 debugs(5, 5, HERE << "checking " << *TheHalfClosed);
2496
2497 typedef DescriptorSet::const_iterator DSCI;
2498 const DSCI end = TheHalfClosed->end();
2499 for (DSCI i = TheHalfClosed->begin(); i != end; ++i) {
2500 const int fd = *i;
2501 if (!fd_table[fd].halfClosedReader) { // not reading already
2502 AsyncCall::Pointer call = commCbCall(5,4, "commHalfClosedReader",
2503 CommIoCbPtrFun(&commHalfClosedReader, NULL));
2504 comm_read(fd, NULL, 0, call);
2505 fd_table[fd].halfClosedReader = call;
2506 }
2507 }
2508
2509 WillCheckHalfClosed = false; // as far as we know
2510 commPlanHalfClosedCheck(); // may need to check again
2511 }
2512
2513 /// checks whether we are waiting for possibly half-closed connection to close
2514 // We are monitoring if the read handler for the fd is the monitoring handler.
2515 bool
2516 commHasHalfClosedMonitor(int fd)
2517 {
2518 return TheHalfClosed->has(fd);
2519 }
2520
2521 /// stop waiting for possibly half-closed connection to close
2522 static void
2523 commStopHalfClosedMonitor(int const fd)
2524 {
2525 debugs(5, 5, HERE << "removing FD " << fd << " from " << *TheHalfClosed);
2526
2527 // cancel the read if one was scheduled
2528 AsyncCall::Pointer reader = fd_table[fd].halfClosedReader;
2529 if (reader != NULL)
2530 comm_read_cancel(fd, reader);
2531 fd_table[fd].halfClosedReader = NULL;
2532
2533 TheHalfClosed->del(fd);
2534 }
2535
2536 /// I/O handler for the possibly half-closed connection monitoring code
2537 static void
2538 commHalfClosedReader(int fd, char *, size_t size, comm_err_t flag, int, void *)
2539 {
2540 // there cannot be more data coming in on half-closed connections
2541 assert(size == 0);
2542 assert(commHasHalfClosedMonitor(fd)); // or we would have canceled the read
2543
2544 fd_table[fd].halfClosedReader = NULL; // done reading, for now
2545
2546 // nothing to do if fd is being closed
2547 if (flag == COMM_ERR_CLOSING)
2548 return;
2549
2550 // if read failed, close the connection
2551 if (flag != COMM_OK) {
2552 debugs(5, 3, "commHalfClosedReader: closing FD " << fd);
2553 comm_close(fd);
2554 return;
2555 }
2556
2557 // continue waiting for close or error
2558 commPlanHalfClosedCheck(); // make sure this fd will be checked again
2559 }
2560
2561
2562 CommRead::CommRead() : fd(-1), buf(NULL), len(0), callback(NULL) {}
2563
2564 CommRead::CommRead(int fd_, char *buf_, int len_, AsyncCall::Pointer &callback_)
2565 : fd(fd_), buf(buf_), len(len_), callback(callback_) {}
2566
2567 DeferredRead::DeferredRead () : theReader(NULL), theContext(NULL), theRead(), cancelled(false) {}
2568
2569 DeferredRead::DeferredRead (DeferrableRead *aReader, void *data, CommRead const &aRead) : theReader(aReader), theContext (data), theRead(aRead), cancelled(false) {}
2570
2571 DeferredReadManager::~DeferredReadManager()
2572 {
2573 flushReads();
2574 assert (deferredReads.empty());
2575 }
2576
2577 /* explicit instantiation required for some systems */
2578
2579 /// \cond AUTODOCS-IGNORE
2580 template cbdata_type CbDataList<DeferredRead>::CBDATA_CbDataList;
2581 /// \endcond
2582
2583 void
2584 DeferredReadManager::delayRead(DeferredRead const &aRead)
2585 {
2586 debugs(5, 3, "Adding deferred read on FD " << aRead.theRead.fd);
2587 CbDataList<DeferredRead> *temp = deferredReads.push_back(aRead);
2588
2589 // We have to use a global function as a closer and point to temp
2590 // instead of "this" because DeferredReadManager is not a job and
2591 // is not even cbdata protected
2592 AsyncCall::Pointer closer = commCbCall(5,4,
2593 "DeferredReadManager::CloseHandler",
2594 CommCloseCbPtrFun(&CloseHandler, temp));
2595 comm_add_close_handler(aRead.theRead.fd, closer);
2596 temp->element.closer = closer; // remeber so that we can cancel
2597 }
2598
2599 void
2600 DeferredReadManager::CloseHandler(int fd, void *thecbdata)
2601 {
2602 if (!cbdataReferenceValid (thecbdata))
2603 return;
2604
2605 CbDataList<DeferredRead> *temp = (CbDataList<DeferredRead> *)thecbdata;
2606
2607 temp->element.closer = NULL;
2608 temp->element.markCancelled();
2609 }
2610
2611 DeferredRead
2612 DeferredReadManager::popHead(CbDataListContainer<DeferredRead> &deferredReads)
2613 {
2614 assert (!deferredReads.empty());
2615
2616 DeferredRead &read = deferredReads.head->element;
2617 if (!read.cancelled) {
2618 comm_remove_close_handler(read.theRead.fd, read.closer);
2619 read.closer = NULL;
2620 }
2621
2622 DeferredRead result = deferredReads.pop_front();
2623
2624 return result;
2625 }
2626
2627 void
2628 DeferredReadManager::kickReads(int const count)
2629 {
2630 /* if we had CbDataList::size() we could consolidate this and flushReads */
2631
2632 if (count < 1) {
2633 flushReads();
2634 return;
2635 }
2636
2637 size_t remaining = count;
2638
2639 while (!deferredReads.empty() && remaining) {
2640 DeferredRead aRead = popHead(deferredReads);
2641 kickARead(aRead);
2642
2643 if (!aRead.cancelled)
2644 --remaining;
2645 }
2646 }
2647
2648 void
2649 DeferredReadManager::flushReads()
2650 {
2651 CbDataListContainer<DeferredRead> reads;
2652 reads = deferredReads;
2653 deferredReads = CbDataListContainer<DeferredRead>();
2654
2655 // XXX: For fairness this SHOULD randomize the order
2656 while (!reads.empty()) {
2657 DeferredRead aRead = popHead(reads);
2658 kickARead(aRead);
2659 }
2660 }
2661
2662 void
2663 DeferredReadManager::kickARead(DeferredRead const &aRead)
2664 {
2665 if (aRead.cancelled)
2666 return;
2667
2668 if (aRead.theRead.fd>=0 && fd_table[aRead.theRead.fd].closing())
2669 return;
2670
2671 debugs(5, 3, "Kicking deferred read on FD " << aRead.theRead.fd);
2672
2673 aRead.theReader(aRead.theContext, aRead.theRead);
2674 }
2675
2676 void
2677 DeferredRead::markCancelled()
2678 {
2679 cancelled = true;
2680 }
2681
2682 ConnectionDetail::ConnectionDetail() : me(), peer()
2683 {
2684 }
2685
2686 int
2687 CommSelectEngine::checkEvents(int timeout)
2688 {
2689 static time_t last_timeout = 0;
2690
2691 /* No, this shouldn't be here. But it shouldn't be in each comm handler. -adrian */
2692 if (squid_curtime > last_timeout) {
2693 last_timeout = squid_curtime;
2694 checkTimeouts();
2695 }
2696
2697 switch (comm_select(timeout)) {
2698
2699 case COMM_OK:
2700
2701 case COMM_TIMEOUT:
2702 return 0;
2703
2704 case COMM_IDLE:
2705
2706 case COMM_SHUTDOWN:
2707 return EVENT_IDLE;
2708
2709 case COMM_ERROR:
2710 return EVENT_ERROR;
2711
2712 default:
2713 fatal_dump("comm.cc: Internal error -- this should never happen.");
2714 return EVENT_ERROR;
2715 };
2716 }