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