]> git.ipfire.org Git - thirdparty/squid.git/blame - src/comm.cc
Adjust refresh_pattern min-age to make 0 mean 0, not 1 second
[thirdparty/squid.git] / src / comm.cc
CommitLineData
da2b3a17 1
30a4f2a8 2/*
4a7a3d56 3 * $Id: comm.cc,v 1.430 2007/04/30 16:56:09 wessels Exp $
30a4f2a8 4 *
5 * DEBUG: section 5 Socket Functions
6 * AUTHOR: Harvest Derived
7 *
2b6662ba 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 9 * ----------------------------------------------------------
30a4f2a8 10 *
2b6662ba 11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
30a4f2a8 19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
cbdec147 32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 33 *
2d8c0b1a 34 *
35 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
30a4f2a8 36 */
090089c4 37
44a47c6e 38#include "squid.h"
c4b7a5a9 39#include "StoreIOBuffer.h"
40#include "comm.h"
a553a5a3 41#include "event.h"
528b2c61 42#include "fde.h"
56410c89 43#include "CommIO.h"
a553a5a3 44#include "CommRead.h"
ee0989f2 45#include "ConnectionDetail.h"
0eb49b6d 46#include "MemBuf.h"
781ce8ff 47#include "pconn.h"
985c86bc 48#include "SquidTime.h"
090089c4 49
b671cc68 50#if defined(_SQUID_CYGWIN_)
51#include <sys/ioctl.h>
52#endif
30a4f2a8 53#ifdef HAVE_NETINET_TCP_H
54#include <netinet/tcp.h>
55#endif
090089c4 56
2b663917 57/*
58 * New C-like simple comm code. This stuff is a mess and doesn't really buy us anything.
59 */
60
61typedef enum {
62 IOCB_NONE,
63 IOCB_READ,
64 IOCB_WRITE
65} iocb_type;
66
67struct _comm_io_callback {
68 iocb_type type;
69 int fd;
70 IOCB *callback;
71 void *callback_data;
72 char *buf;
73 FREE *freefunc;
74 int size;
75 int offset;
76 bool active;
77 bool completed;
78 comm_err_t errcode;
79 int xerrno;
80 dlink_node node;
81};
82typedef struct _comm_io_callback comm_io_callback_t;
83
84struct _comm_fd {
85 int fd;
86 comm_io_callback_t readcb;
87 comm_io_callback_t writecb;
88};
89typedef struct _comm_fd comm_fd_t;
90comm_fd_t *commfd_table;
91
92dlink_list commfd_completed_events;
93
94bool
95commio_has_callback(int fd, iocb_type type, comm_io_callback_t *ccb)
96{
97 assert(ccb->fd == fd);
98 assert(ccb->type == type);
99 return ccb->active == true;
100}
101
102/*
103 * Set the given handler and mark active
104 *
105 * @param fd filedescriptor
106 * @param ccb comm io callback
107 * @param cb callback
108 * @param cbdata callback data (must be cbdata'ed)
109 * @param buf buffer, if applicable
110 * @param freefunc freefunc, if applicable
111 * @param size buffer size
112 */
113void
114commio_set_callback(int fd, iocb_type type, comm_io_callback_t *ccb, IOCB *cb, void *cbdata, char *buf, FREE *freefunc, int size)
115{
116 assert(ccb->active == false);
117 assert(ccb->type == type);
118 ccb->fd = fd;
119 ccb->callback = cb;
b7c66413 120 ccb->callback_data = cbdataReference(cbdata);
2b663917 121 ccb->buf = buf;
122 ccb->freefunc = freefunc;
123 ccb->size = size;
124 ccb->active = true;
125 ccb->completed = false;
126 ccb->offset = 0;
127}
128
129
130/*
131 * Complete the callback
132 *
133 * Someone may have already called this function once on a non-completed callback.
134 * This happens in the comm_close() routine - the IO may have completed
135 * but comm_close() is called bfeore teh callback has been called.
136 * In this case, leave the details the same (offset, for example) but just update
137 * the error codes.
138 */
139void
140commio_complete_callback(int fd, comm_io_callback_t *ccb, comm_err_t code, int xerrno)
141{
bf8fe701 142 debugs(5, 3, "commio_complete_callback: called for " << fd << " (" << code << ", " << xerrno << ")");
2b663917 143 assert(ccb->active == true);
144 assert(ccb->fd == fd);
145 ccb->errcode = code;
146 ccb->xerrno = xerrno;
147 if (! ccb->completed)
148 dlinkAddTail(ccb, &ccb->node, &commfd_completed_events);
149 ccb->completed = true;
150}
151
152
153/*
154 * Cancel the given callback
155 *
156 * Remember that the data is cbdataRef'ed.
157 */
158void
159commio_cancel_callback(int fd, comm_io_callback_t *ccb)
160{
bf8fe701 161 debugs(5, 3, "commio_cancel_callback: called for " << fd);
2b663917 162 assert(ccb->fd == fd);
163 assert(ccb->active == true);
164
165 if (ccb->completed == true) {
166 dlinkDelete(&ccb->node, &commfd_completed_events);
167 }
168 if (ccb->callback_data)
169 cbdataReferenceDone(ccb->callback_data);
170
171 ccb->xerrno = 0;
172 ccb->active = false;
173 ccb->completed = false;
174 ccb->callback = NULL;
175 ccb->callback_data = NULL;
176}
177
178/*
179 * Call the given comm callback; assumes the callback is valid.
180 *
181 * @param ccb io completion callback
182 */
183void
184commio_call_callback(comm_io_callback_t *ccb)
185{
186 comm_io_callback_t cb = *ccb;
187 void *cbdata;
188 assert(cb.active == true);
189 assert(cb.completed == true);
bf8fe701 190 debugs(5, 3, "commio_call_callback: called for " << ccb->fd);
2b663917 191
192 /* We've got a copy; blow away the real one */
193 /* XXX duplicate code from commio_cancel_callback! */
194 dlinkDelete(&ccb->node, &commfd_completed_events);
195 ccb->xerrno = 0;
196 ccb->active = false;
197 ccb->completed = false;
198 ccb->callback = NULL;
199 ccb->callback_data = NULL;
200
201 /* free data */
202 if (cb.freefunc) {
203 cb.freefunc(cb.buf);
204 cb.buf = NULL;
205 }
206 if (cb.callback && cbdataReferenceValidDone(cb.callback_data, &cbdata)) {
207 /* XXX truely ugly for now! */
208 cb.callback(cb.fd, cb.buf, cb.offset, cb.errcode, cb.xerrno, cbdata);
209 }
210}
211
212void
213commio_call_callbacks(void)
214{
215 comm_io_callback_t *ccb;
216 while (commfd_completed_events.head != NULL) {
217 ccb = (comm_io_callback_t *) commfd_completed_events.head->data;
218 commio_call_callback(ccb);
219 }
220}
221
f49a8979 222
2d8c0b1a 223class ConnectStateData
62e76326 224{
2d8c0b1a 225
226public:
d2d59a68 227 void *operator new (size_t);
228 void operator delete (void *);
2d8c0b1a 229 static void Connect (int fd, void *me);
230 void connect();
231 void callCallback(comm_err_t status, int xerrno);
232 void defaults();
f88211e8 233 char *host;
234 u_short port;
62e76326 235
f88211e8 236 struct sockaddr_in S;
2d8c0b1a 237 CallBack<CNCB> callback;
62e76326 238
ddfcbc22 239 struct IN_ADDR in_addr;
03a1ee42 240 int fd;
22c653cd 241 int tries;
242 int addrcount;
243 int connstart;
d2d59a68 244
245private:
0b77ecd8 246 int commResetFD();
247 int commRetryConnect();
d2d59a68 248 CBDATA_CLASS(ConnectStateData);
2d8c0b1a 249};
f88211e8 250
090089c4 251/* STATIC */
62e76326 252
ddfcbc22 253static comm_err_t commBind(int s, struct IN_ADDR, u_short port);
f5b8bbc4 254static void commSetReuseAddr(int);
255static void commSetNoLinger(int);
30a4f2a8 256#ifdef TCP_NODELAY
f5b8bbc4 257static void commSetTcpNoDelay(int);
30a4f2a8 258#endif
f5b8bbc4 259static void commSetTcpRcvbuf(int, int);
f88211e8 260static PF commConnectFree;
03a1ee42 261static PF commHandleWrite;
edeb28fd 262static IPH commConnectDnsHandle;
05589f93 263static void requireOpenAndActive(int const fd);
723123a9 264
3c1a197f 265static PF comm_accept_try;
c4b7a5a9 266
62e76326 267class AcceptFD
268{
269
270public:
bdd8c442 271 AcceptFD() : count(0), finished_(false){}
2d8c0b1a 272
273 void doCallback(int fd, int newfd, comm_err_t errcode, int xerrno, ConnectionDetail *);
274 void nullCallback();
275 void beginAccepting() {count = 0; finished(false);}
276
277 size_t acceptCount() const { return count;}
278
279 bool finishedAccepting() const;
2d8c0b1a 280 CallBack<IOACB> callback;
281 bool finished() const;
282 void finished(bool);
283
284private:
c4c6da01 285 static size_t const MAX_ACCEPT_PER_LOOP;
2d8c0b1a 286 size_t count;
287 bool finished_;
288};
289
c4c6da01 290size_t const AcceptFD::MAX_ACCEPT_PER_LOOP(10);
291
a46d2c0e 292class fdc_t
62e76326 293{
a46d2c0e 294
295public:
2d8c0b1a 296 void acceptOne(int fd);
297 void beginAccepting();
298 int acceptCount() const;
2b663917 299 fdc_t() : active(0), fd(-1), half_closed (false){CommCallbackList.head = NULL;CommCallbackList.tail = NULL; }
62e76326 300
2d8c0b1a 301 fdc_t(int anFD) : active(0), fd(anFD), half_closed(false)
62e76326 302 {
2d8c0b1a 303 CommCallbackList.head = NULL;
304 CommCallbackList.tail = NULL;
62e76326 305 }
306
2d8c0b1a 307 int active;
308 int fd;
309 dlink_list CommCallbackList;
62e76326 310
545d554b 311 template<class P>
312 bool findCallback(P predicate);
313
5447bba9 314 class Accept
62e76326 315 {
5447bba9 316
317 public:
62e76326 318 AcceptFD accept;
319 ConnectionDetail connDetails;
5447bba9 320 };
62e76326 321
5447bba9 322 Accept accept;
62e76326 323
a46d2c0e 324 bool half_closed;
c4b7a5a9 325};
62e76326 326
c4b7a5a9 327typedef enum {
62e76326 328 COMM_CB_READ = 1,
2d8c0b1a 329 COMM_CB_DERIVED,
c4b7a5a9 330} comm_callback_t;
331
2d8c0b1a 332static int CommCallbackSeqnum = 1;
333
334class CommCommonCallback
62e76326 335{
2d8c0b1a 336
337public:
338 CommCommonCallback() : fd (-1), errcode (COMM_OK), xerrno(0), seqnum (CommCallbackSeqnum){}
339
340 CommCommonCallback(int anFD, comm_err_t errcode, int anErrno) : fd (anFD), errcode (errcode), xerrno(anErrno), seqnum (CommCallbackSeqnum){}
341
62e76326 342 int fd;
62e76326 343 comm_err_t errcode;
344 int xerrno;
345 int seqnum;
2d8c0b1a 346};
347
348class CommCallbackData
349{
350
351public:
b001e822 352 MEMPROXY_CLASS(CommCallbackData);
2d8c0b1a 353 CommCallbackData(CommCommonCallback const &);
010ffcf0 354 virtual ~CommCallbackData() {}
355
2d8c0b1a 356 virtual comm_callback_t getType() const { return COMM_CB_DERIVED; }
357
358 void callACallback();
359 void fdClosing();
360 virtual void callCallback() = 0;
361 void registerSelf();
362 void deRegisterSelf();
363 char *buf;
62e76326 364 StoreIOBuffer sb;
2d8c0b1a 365
366protected:
367 CommCommonCallback result;
43ae1d95 368 friend void _comm_close(int fd, char const *file, int line);
2d8c0b1a 369 friend void comm_calliocallback(void);
370
371private:
2d8c0b1a 372 dlink_node fd_node;
373 dlink_node h_node;
c4b7a5a9 374};
62e76326 375
b001e822 376MEMPROXY_CLASS_INLINE(CommCallbackData)
377
2d8c0b1a 378class CommAcceptCallbackData : public CommCallbackData
379{
380
381public:
b001e822 382 MEMPROXY_CLASS(CommAcceptCallbackData);
2d8c0b1a 383 CommAcceptCallbackData(int const anFd, CallBack<IOACB>, comm_err_t, int, int, ConnectionDetail const &);
384 virtual void callCallback();
385
386private:
2d8c0b1a 387 CallBack<IOACB> callback;
388 int newfd;
389 ConnectionDetail details;
390};
391
b001e822 392MEMPROXY_CLASS_INLINE(CommAcceptCallbackData)
393
2d8c0b1a 394class CommFillCallbackData : public CommCallbackData
395{
396
397public:
b001e822 398 MEMPROXY_CLASS(CommFillCallbackData);
2d8c0b1a 399 CommFillCallbackData(int const anFd, CallBack<IOFCB> aCallback, comm_err_t, int);
400 virtual void callCallback();
401
402private:
2d8c0b1a 403 CallBack<IOFCB> callback;
404};
405
b001e822 406MEMPROXY_CLASS_INLINE(CommFillCallbackData)
407
62e76326 408struct _fd_debug_t
409{
43ae1d95 410 char const *close_file;
62e76326 411 int close_line;
c4b7a5a9 412};
62e76326 413
c4b7a5a9 414typedef struct _fd_debug_t fd_debug_t;
415
b001e822 416static MemAllocator *conn_close_pool = NULL;
c4b7a5a9 417fdc_t *fdc_table = NULL;
418fd_debug_t *fdd_table = NULL;
419dlink_list CommCallbackList;
c4b7a5a9 420
421
422/* New and improved stuff */
423
2d8c0b1a 424CommCallbackData::CommCallbackData(CommCommonCallback const &newResults) : result (newResults)
425{
426 assert(fdc_table[result.fd].active == 1);
427 registerSelf();
428}
429
2d8c0b1a 430CommAcceptCallbackData::CommAcceptCallbackData(int const anFd, CallBack<IOACB> aCallback, comm_err_t anErrcode, int anErrno, int aNewFD, ConnectionDetail const &newDetails) :CommCallbackData(CommCommonCallback(anFd, anErrcode, anErrno)), callback (aCallback), newfd(aNewFD), details(newDetails)
431{}
432
2d8c0b1a 433void
434CommCallbackData::registerSelf()
435{
62e76326 436 /* Add it to the end of the list */
2d8c0b1a 437 dlinkAddTail(this, &h_node, &CommCallbackList);
c4b7a5a9 438
62e76326 439 /* and add it to the end of the fd list */
2d8c0b1a 440 dlinkAddTail(this, &fd_node, &(fdc_table[result.fd].CommCallbackList));
441}
c4b7a5a9 442
2d8c0b1a 443void
444CommCallbackData::deRegisterSelf()
445{
446 dlinkDelete(&h_node, &CommCallbackList);
447 dlinkDelete(&fd_node, &(fdc_table[result.fd].CommCallbackList));
c4b7a5a9 448}
449
2d8c0b1a 450/*
451 * add an IO callback
452 *
453 * IO callbacks are added when we want to notify someone that some IO
454 * has finished but we don't want to risk re-entering a non-reentrant
455 * code block.
456 */
2d8c0b1a 457void
458CommAcceptCallbackData::callCallback()
459{
1d5161bd 460 PROF_start(CommAcceptCallbackData_callCallback);
2d8c0b1a 461 callback.handler(result.fd, newfd, &details, result.errcode, result.xerrno, callback.data);
1d5161bd 462 PROF_stop(CommAcceptCallbackData_callCallback);
2d8c0b1a 463}
62e76326 464
2d8c0b1a 465void
466CommCallbackData::fdClosing()
467{
468 result.errcode = COMM_ERR_CLOSING;
c4b7a5a9 469}
470
2d8c0b1a 471void
472CommCallbackData::callACallback()
473{
474 assert(fdc_table[result.fd].active == 1);
475 deRegisterSelf();
476 callCallback();
477}
c4b7a5a9 478
479/*
480 * call the IO callbacks
481 *
482 * This should be called before comm_select() so code can attempt to
483 * initiate some IO.
484 *
485 * When io callbacks are added, they are added with the current
486 * sequence number. The sequence number is incremented in this routine -
487 * since callbacks are added to the _tail_ of the list, when we hit a
488 * callback with a seqnum _not_ what it was when we entered this routine,
489 * we can stop.
490 */
491void
492comm_calliocallback(void)
493{
62e76326 494 CommCallbackData *cio;
010ffcf0 495 int oldseqnum = CommCallbackSeqnum++;
62e76326 496
497 /* Call our callbacks until we hit NULL or the seqnum changes */
498
1d5161bd 499 /* This will likely rap other counts - again, thats ok (for now)
500 * What we should see is the total of the various callback subclasses
501 * equaling this counter.
502 * If they don't, someone has added a class but not profiled it.
503 */
504 PROF_start(comm_calliocallback);
2d8c0b1a 505
bf8fe701 506 debugs(5, 7, "comm_calliocallback: " << CommCallbackList.head);
b300c36d 507
1d5161bd 508 while (CommCallbackList.head != NULL && oldseqnum != ((CommCallbackData *)CommCallbackList.head->data)->result.seqnum) {
509 dlink_node *node = (dlink_node *)CommCallbackList.head;
62e76326 510 cio = (CommCallbackData *)node->data;
2d8c0b1a 511 cio->callACallback();
00d77d6b 512 delete cio;
2d8c0b1a 513 }
1d5161bd 514
515 PROF_stop(comm_calliocallback);
2d8c0b1a 516}
62e76326 517
b300c36d 518bool
519comm_iocallbackpending(void)
520{
bf8fe701 521 debugs(5, 7, "comm_iocallbackpending: " << CommCallbackList.head);
5a1d3fec 522 return (CommCallbackList.head != NULL) || (commfd_completed_events.head != NULL);
b300c36d 523}
524
c4b7a5a9 525/*
526 * Attempt a read
527 *
528 * If the read attempt succeeds or fails, call the callback.
529 * Else, wait for another IO notification.
530 */
2d8c0b1a 531void
2b663917 532commHandleRead(int fd, void *data)
2d8c0b1a 533{
2b663917 534 comm_io_callback_t *ccb = (comm_io_callback_t *) data;
535
536 assert(data == COMMIO_FD_READCB(fd));
537 assert(commio_has_callback(fd, IOCB_READ, ccb));
62e76326 538 /* Attempt a read */
539 statCounter.syscalls.sock.reads++;
540 errno = 0;
2d8c0b1a 541 int retval;
2b663917 542 retval = FD_READ_METHOD(fd, ccb->buf, ccb->size);
bf8fe701 543 debugs(5, 3, "comm_read_try: FD " << fd << ", size " << ccb->size << ", retval " << retval << ", errno " << errno);
62e76326 544
545 if (retval < 0 && !ignoreErrno(errno)) {
bf8fe701 546 debugs(5, 3, "comm_read_try: scheduling COMM_ERROR");
2b663917 547 ccb->offset = 0;
548 commio_complete_callback(fd, ccb, COMM_ERROR, errno);
62e76326 549 return;
550 };
551
552 /* See if we read anything */
553 /* Note - read 0 == socket EOF, which is a valid read */
554 if (retval >= 0) {
555 fd_bytes(fd, retval, FD_READ);
2b663917 556 ccb->offset = retval;
557 commio_complete_callback(fd, ccb, COMM_OK, errno);
62e76326 558 return;
559 }
c4b7a5a9 560
62e76326 561 /* Nope, register for some more IO */
2b663917 562 commSetSelect(fd, COMM_SELECT_READ, commHandleRead, data, 0);
c4b7a5a9 563}
564
565/*
566 * Queue a read. handler/handler_data are called when the read
567 * completes, on error, or on file descriptor close.
568 */
569void
570comm_read(int fd, char *buf, int size, IOCB *handler, void *handler_data)
571{
62e76326 572 /* Make sure we're not reading anything and we're not closing */
573 assert(fdc_table[fd].active == 1);
62e76326 574 assert(!fd_table[fd].flags.closing);
c4b7a5a9 575
bf8fe701 576 debugs(5, 4, "comm_read, queueing read for FD " << fd);
528b2c61 577
2b663917 578 /* Queue the read */
579 /* XXX ugly */
580 commio_set_callback(fd, IOCB_READ, COMMIO_FD_READCB(fd), handler, handler_data, (char *)buf, NULL, size);
581 commSetSelect(fd, COMM_SELECT_READ, commHandleRead, COMMIO_FD_READCB(fd), 0);
c4b7a5a9 582}
583
c4b7a5a9 584/*
585 * Empty the read buffers
586 *
587 * This is a magical routine that empties the read buffers.
588 * Under some platforms (Linux) if a buffer has data in it before
589 * you call close(), the socket will hang and take quite a while
590 * to timeout.
591 */
592static void
593comm_empty_os_read_buffers(int fd)
594{
a42d5c25 595#ifdef _SQUID_LINUX_
c4b7a5a9 596 /* prevent those nasty RST packets */
597 char buf[SQUID_TCP_SO_RCVBUF];
62e76326 598
c4b7a5a9 599 if (fd_table[fd].flags.nonblocking == 1)
62e76326 600 while (FD_READ_METHOD(fd, buf, SQUID_TCP_SO_RCVBUF) > 0)
601
602 ;
c4b7a5a9 603#endif
604}
605
81679554 606static void
05589f93 607requireOpenAndActive(int const fd)
608{
609 assert(fd_table[fd].flags.open == 1);
610 assert(fdc_table[fd].active == 1);
611}
c4b7a5a9 612
613/*
2b663917 614 * Return whether the FD has a pending completed callback.
c4b7a5a9 615 */
616int
617comm_has_pending_read_callback(int fd)
618{
05589f93 619 requireOpenAndActive(fd);
2b663917 620 return COMMIO_FD_READCB(fd)->active && COMMIO_FD_READCB(fd)->completed;
545d554b 621}
622
623template <class P>
624bool
625fdc_t::findCallback(P predicate)
626{
c4b7a5a9 627 /*
628 * XXX I don't like having to walk the list!
629 * Instead, if this routine is called often enough, we should
630 * also maintain a linked list of _read_ events - we can just
631 * check if the list head a HEAD..
632 * - adrian
633 */
545d554b 634 dlink_node *node = CommCallbackList.head;
62e76326 635
c4b7a5a9 636 while (node != NULL) {
545d554b 637 if (predicate((CommCallbackData *)node->data))
638 return true;
62e76326 639
640 node = node->next;
c4b7a5a9 641 }
642
643 /* Not found */
545d554b 644 return false;
c4b7a5a9 645}
646
647/*
648 * return whether a file descriptor has a read handler
649 *
650 * Assumptions: the fd is open
528b2c61 651 * the fd is a comm fd.
2b663917 652 *
653 * Again - is this "pending read", or "pending completed event", or what?
654 * I'll assume its pending read, not pending completed.
655 *
656 * This makes no sense though - if this is called to check whether there's
657 * a pending read -before- submitting a read then it won't matter whether
658 * its completed or not! Ie:
659 *
660 * + if there's no read and you want to schedule one; fine.
661 * + if a read has completed then the callback block has been deactivated before
662 * the callback is called - if something decides to register for a read
663 * callback once again it should find !active and !completed.
664 * + scheduling a read event when the fd is ! active -and- completed, thats
665 * a bug
666 * + like, afaict, anything else is.
c4b7a5a9 667 */
528b2c61 668bool
c4b7a5a9 669comm_has_pending_read(int fd)
670{
05589f93 671 requireOpenAndActive(fd);
2b663917 672 return COMMIO_FD_READCB(fd)->active && (! COMMIO_FD_READCB(fd)->completed);
c4b7a5a9 673}
674
675/*
676 * Cancel a pending read. Assert that we have the right parameters,
677 * and that there are no pending read events!
2b663917 678 *
679 * AHC Don't call the comm handlers?
c4b7a5a9 680 */
681void
682comm_read_cancel(int fd, IOCB *callback, void *data)
683{
05589f93 684 requireOpenAndActive(fd);
c4b7a5a9 685
c4b7a5a9 686 /* Ok, we can be reasonably sure we won't lose any data here! */
2b663917 687 assert(COMMIO_FD_READCB(fd)->callback == callback);
688 assert(COMMIO_FD_READCB(fd)->callback_data == data);
c4b7a5a9 689
690 /* Delete the callback */
2b663917 691 commio_cancel_callback(fd, COMMIO_FD_READCB(fd));
420f2ac8 692
693 /* And the IO event */
62e76326 694 commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
c4b7a5a9 695}
696
697
d846c273 698/*
699 * Open a filedescriptor, set some sane defaults
bdd8c442 700 * XXX DPW 2006-05-30 what is the point of this?
d846c273 701 */
c4b7a5a9 702void
43ae1d95 703fdc_open(int fd, unsigned int type, char const *desc)
c4b7a5a9 704{
62e76326 705 assert(fdc_table[fd].active == 0);
c4b7a5a9 706
62e76326 707 fdc_table[fd].active = 1;
708 fdc_table[fd].fd = fd;
62e76326 709 fd_open(fd, type, desc);
c4b7a5a9 710}
711
712
ce767c23 713/*
714 * synchronous wrapper around udp socket functions
715 */
716
717int
7d21986b 718comm_udp_recvfrom(int fd, void *buf, size_t len, int flags,
62e76326 719
720 struct sockaddr *from, socklen_t *fromlen)
ce767c23 721{
62e76326 722 statCounter.syscalls.sock.recvfroms++;
723 return recvfrom(fd, buf, len, flags, from, fromlen);
ce767c23 724}
725
365f12a9 726int
7d21986b 727comm_udp_recv(int fd, void *buf, size_t len, int flags)
365f12a9 728{
62e76326 729 return comm_udp_recvfrom(fd, buf, len, flags, NULL, 0);
365f12a9 730}
731
f71da12c 732ssize_t
7d21986b 733comm_udp_send(int s, const void *buf, size_t len, int flags)
f71da12c 734{
62e76326 735 return send(s, buf, len, flags);
f71da12c 736}
ce767c23 737
738
545d554b 739bool
740comm_has_incomplete_write(int fd)
741{
742 requireOpenAndActive(fd);
2b663917 743 return COMMIO_FD_WRITECB(fd)->active;
d4cb310b 744}
745
cf3c0ee3 746/*
747 * Queue a write. handler/handler_data are called when the write fully
748 * completes, on error, or on file descriptor close.
749 */
9864ee44 750
090089c4 751/* Return the local port associated with fd. */
b8d8561b 752u_short
753comm_local_port(int fd)
090089c4 754{
62e76326 755
090089c4 756 struct sockaddr_in addr;
6637e3a5 757 socklen_t addr_len = 0;
76f87348 758 fde *F = &fd_table[fd];
090089c4 759
090089c4 760 /* If the fd is closed already, just return */
62e76326 761
60c0b5a2 762 if (!F->flags.open) {
bf8fe701 763 debugs(5, 0, "comm_local_port: FD " << fd << " has been closed.");
62e76326 764 return 0;
090089c4 765 }
62e76326 766
76f87348 767 if (F->local_port)
62e76326 768 return F->local_port;
769
090089c4 770 addr_len = sizeof(addr);
62e76326 771
090089c4 772 if (getsockname(fd, (struct sockaddr *) &addr, &addr_len)) {
bf8fe701 773 debugs(50, 1, "comm_local_port: Failed to retrieve TCP/UDP port number for socket: FD " << fd << ": " << xstrerror());
62e76326 774 return 0;
090089c4 775 }
62e76326 776
76f87348 777 F->local_port = ntohs(addr.sin_port);
4a7a3d56 778 debugs(5, 6, "comm_local_port: FD " << fd << ": port " << F->local_port);
76f87348 779 return F->local_port;
090089c4 780}
781
3d7e9d7c 782static comm_err_t
62e76326 783
ddfcbc22 784commBind(int s, struct IN_ADDR in_addr, u_short port)
090089c4 785{
62e76326 786
090089c4 787 struct sockaddr_in S;
090089c4 788
090089c4 789 memset(&S, '\0', sizeof(S));
790 S.sin_family = AF_INET;
791 S.sin_port = htons(port);
30a4f2a8 792 S.sin_addr = in_addr;
83704487 793 statCounter.syscalls.sock.binds++;
62e76326 794
090089c4 795 if (bind(s, (struct sockaddr *) &S, sizeof(S)) == 0)
62e76326 796 return COMM_OK;
797
bf8fe701 798 debugs(50, 0, "commBind: Cannot bind socket FD " << s << " to " <<
799 (S.sin_addr.s_addr == INADDR_ANY ? "*" : inet_ntoa(S.sin_addr)) <<
800 ":" << (int) port << ": " << xstrerror());
62e76326 801
090089c4 802 return COMM_ERROR;
803}
804
805/* Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
d6827718 806 * is OR of flags specified in comm.h. Defaults TOS */
b8d8561b 807int
16b204c4 808comm_open(int sock_type,
62e76326 809 int proto,
810
ddfcbc22 811 struct IN_ADDR addr,
62e76326 812 u_short port,
813 int flags,
814 const char *note)
d6827718 815{
816 return comm_openex(sock_type, proto, addr, port, flags, 0, note);
817}
818
2d8c0b1a 819static bool
820limitError(int const anErrno)
821{
822 return anErrno == ENFILE || anErrno == EMFILE;
823}
d6827718 824
825/* Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
826 * is OR of flags specified in defines.h:COMM_* */
827int
828comm_openex(int sock_type,
62e76326 829 int proto,
830
ddfcbc22 831 struct IN_ADDR addr,
62e76326 832 u_short port,
833 int flags,
834 unsigned char TOS,
835 const char *note)
090089c4 836{
837 int new_socket;
9056f553 838 int tos = 0;
76f87348 839 fde *F = NULL;
090089c4 840
88bfe092 841 PROF_start(comm_open);
090089c4 842 /* Create socket for accepting new connections. */
83704487 843 statCounter.syscalls.sock.sockets++;
62e76326 844
845 if ((new_socket = socket(AF_INET, sock_type, proto)) < 0)
846 {
847 /* Increase the number of reserved fd's if calls to socket()
848 * are failing because the open file table is full. This
849 * limits the number of simultaneous clients */
850
2d8c0b1a 851 if (limitError(errno)) {
bf8fe701 852 debugs(50, 1, "comm_open: socket failure: " << xstrerror());
62e76326 853 fdAdjustReserved();
2d8c0b1a 854 } else {
bf8fe701 855 debugs(50, 0, "comm_open: socket failure: " << xstrerror());
62e76326 856 }
857
858 PROF_stop(comm_open);
859 return -1;
090089c4 860 }
62e76326 861
d6827718 862 /* set TOS if needed */
62e76326 863 if (TOS)
864 {
d6827718 865#ifdef IP_TOS
62e76326 866 tos = TOS;
867
bf8fe701 868 if (setsockopt(new_socket, IPPROTO_IP, IP_TOS, (char *) &tos, sizeof(int)) < 0) {
869 debugs(50, 1, "comm_open: setsockopt(IP_TOS) on FD " << new_socket << ": " << xstrerror());
870 }
62e76326 871
d6827718 872#else
62e76326 873
bf8fe701 874 debugs(50, 0, "comm_open: setsockopt(IP_TOS) not supported on this platform");
62e76326 875
d6827718 876#endif
62e76326 877
d6827718 878 }
62e76326 879
090089c4 880 /* update fdstat */
bf8fe701 881 debugs(5, 5, "comm_open: FD " << new_socket << " is a new socket");
62e76326 882
5c5783a2 883 fd_open(new_socket, FD_SOCKET, note);
62e76326 884
c4b7a5a9 885 fdd_table[new_socket].close_file = NULL;
62e76326 886
c4b7a5a9 887 fdd_table[new_socket].close_line = 0;
62e76326 888
c4b7a5a9 889 assert(fdc_table[new_socket].active == 0);
62e76326 890
c4b7a5a9 891 fdc_table[new_socket].active = 1;
62e76326 892
76f87348 893 F = &fd_table[new_socket];
62e76326 894
d6827718 895 F->local_addr = addr;
62e76326 896
d6827718 897 F->tos = tos;
62e76326 898
79a15e0a 899 if (!(flags & COMM_NOCLOEXEC))
62e76326 900 commSetCloseOnExec(new_socket);
901
cdc33f35 902 if ((flags & COMM_REUSEADDR))
62e76326 903 commSetReuseAddr(new_socket);
904
905 if (port > (u_short) 0)
906 {
a50bfe93 907#ifdef _SQUID_MSWIN_
908
909 if (sock_type != SOCK_DGRAM)
910#endif
911
912 commSetNoLinger(new_socket);
62e76326 913
914 if (opt_reuseaddr)
915 commSetReuseAddr(new_socket);
090089c4 916 }
62e76326 917
918 if (addr.s_addr != no_addr.s_addr)
919 {
920 if (commBind(new_socket, addr, port) != COMM_OK) {
921 comm_close(new_socket);
922 return -1;
923 PROF_stop(comm_open);
924 }
23ff6968 925 }
62e76326 926
76f87348 927 F->local_port = port;
090089c4 928
79a15e0a 929 if (flags & COMM_NONBLOCKING)
62e76326 930 if (commSetNonBlocking(new_socket) == COMM_ERROR)
931 {
932 return -1;
933 PROF_stop(comm_open);
934 }
935
30a4f2a8 936#ifdef TCP_NODELAY
937 if (sock_type == SOCK_STREAM)
62e76326 938 commSetTcpNoDelay(new_socket);
939
30a4f2a8 940#endif
62e76326 941
1241e63e 942 if (Config.tcpRcvBufsz > 0 && sock_type == SOCK_STREAM)
62e76326 943 commSetTcpRcvbuf(new_socket, Config.tcpRcvBufsz);
944
88bfe092 945 PROF_stop(comm_open);
62e76326 946
090089c4 947 return new_socket;
948}
949
d2d59a68 950CBDATA_CLASS_INIT(ConnectStateData);
951
952void *
953ConnectStateData::operator new (size_t size)
954{
955 CBDATA_INIT_TYPE(ConnectStateData);
956 return cbdataAlloc(ConnectStateData);
957}
958
959void
960ConnectStateData::operator delete (void *address)
961{
962 cbdataFree(address);
963}
964
e5f6c5c2 965void
4f92c80c 966commConnectStart(int fd, const char *host, u_short port, CNCB * callback, void *data)
e924600d 967{
28c60158 968 ConnectStateData *cs;
4a7a3d56 969 debugs(5, 3, "commConnectStart: FD " << fd << ", data " << data << ", " << host << ":" << port);
d2d59a68 970 cs = new ConnectStateData;
03a1ee42 971 cs->fd = fd;
e924600d 972 cs->host = xstrdup(host);
973 cs->port = port;
85a56f15 974 cs->callback = CallBack<CNCB>(callback, data);
e924600d 975 comm_add_close_handler(fd, commConnectFree, cs);
8407afee 976 ipcache_nbgethostbyname(host, commConnectDnsHandle, cs);
edeb28fd 977}
978
979static void
03a1ee42 980commConnectDnsHandle(const ipcache_addrs * ia, void *data)
edeb28fd 981{
e6ccf245 982 ConnectStateData *cs = (ConnectStateData *)data;
62e76326 983
edeb28fd 984 if (ia == NULL) {
bf8fe701 985 debugs(5, 3, "commConnectDnsHandle: Unknown host: " << cs->host);
62e76326 986
987 if (!dns_error_message) {
988 dns_error_message = "Unknown DNS error";
bf8fe701 989 debugs(5, 1, "commConnectDnsHandle: Bad dns_error_message");
62e76326 990 }
991
992 assert(dns_error_message != NULL);
2d8c0b1a 993 cs->callCallback(COMM_ERR_DNS, 0);
62e76326 994 return;
edeb28fd 995 }
62e76326 996
f076b37b 997 assert(ia->cur < ia->count);
edeb28fd 998 cs->in_addr = ia->in_addrs[ia->cur];
a12a049a 999
1000 if (Config.onoff.balance_on_multiple_ip)
1001 ipcacheCycleAddr(cs->host, NULL);
1002
22c653cd 1003 cs->addrcount = ia->count;
a12a049a 1004
22c653cd 1005 cs->connstart = squid_curtime;
a12a049a 1006
2d8c0b1a 1007 cs->connect();
e924600d 1008}
1009
2d8c0b1a 1010void
1011ConnectStateData::callCallback(comm_err_t status, int xerrno)
1012{
bf8fe701 1013 debugs(5, 3, "commConnectCallback: FD " << fd << ", data " << callback.data);
1014
2d8c0b1a 1015 comm_remove_close_handler(fd, commConnectFree, this);
1016 CallBack<CNCB> aCallback = callback;
1017 callback = CallBack<CNCB>();
e1b16349 1018 commSetTimeout(fd, -1, NULL, NULL);
62e76326 1019
85a56f15 1020 if (aCallback.dataValid())
2d8c0b1a 1021 aCallback.handler(fd, status, xerrno, aCallback.data);
62e76326 1022
744c68f5 1023 commConnectFree(fd, this);
f88211e8 1024}
1025
e924600d 1026static void
9daca08e 1027commConnectFree(int fd, void *data)
e924600d 1028{
e6ccf245 1029 ConnectStateData *cs = (ConnectStateData *)data;
bf8fe701 1030 debugs(5, 3, "commConnectFree: FD " << fd);
85a56f15 1031 cs->callback = CallBack<CNCB>();
8407afee 1032 safe_free(cs->host);
00d77d6b 1033 delete cs;
e924600d 1034}
1035
2d8c0b1a 1036static void
1037copyFDFlags(int to, fde *F)
1038{
1039 if (F->flags.close_on_exec)
1040 commSetCloseOnExec(to);
1041
1042 if (F->flags.nonblocking)
1043 commSetNonBlocking(to);
1044
1045#ifdef TCP_NODELAY
1046
1047 if (F->flags.nodelay)
1048 commSetTcpNoDelay(to);
1049
1050#endif
1051
1052 if (Config.tcpRcvBufsz > 0)
1053 commSetTcpRcvbuf(to, Config.tcpRcvBufsz);
1054}
1055
22c653cd 1056/* Reset FD so that we can connect() again */
0b77ecd8 1057int
1058ConnectStateData::commResetFD()
edeb28fd 1059{
0b77ecd8 1060 if (!cbdataReferenceValid(callback.data))
62e76326 1061 return 0;
1062
83704487 1063 statCounter.syscalls.sock.sockets++;
62e76326 1064
2d8c0b1a 1065 int fd2 = socket(AF_INET, SOCK_STREAM, 0);
62e76326 1066
edeb28fd 1067 if (fd2 < 0) {
bf8fe701 1068 debugs(5, 0, "commResetFD: socket: " << xstrerror());
62e76326 1069
1070 if (ENFILE == errno || EMFILE == errno)
1071 fdAdjustReserved();
1072
1073 return 0;
edeb28fd 1074 }
62e76326 1075
68aa4272 1076#ifdef _SQUID_MSWIN_
1077
1078 /* On Windows dup2() can't work correctly on Sockets, the */
1079 /* workaround is to close the destination Socket before call them. */
0b77ecd8 1080 close(fd);
68aa4272 1081
1082#endif
1083
0b77ecd8 1084 if (dup2(fd2, fd) < 0) {
bf8fe701 1085 debugs(5, 0, "commResetFD: dup2: " << xstrerror());
62e76326 1086
1087 if (ENFILE == errno || EMFILE == errno)
1088 fdAdjustReserved();
1089
1090 close(fd2);
1091
1092 return 0;
edeb28fd 1093 }
62e76326 1094
edeb28fd 1095 close(fd2);
0b77ecd8 1096 fde *F = &fd_table[fd];
1097 fd_table[fd].flags.called_connect = 0;
09544acc 1098 /*
1099 * yuck, this has assumptions about comm_open() arguments for
1100 * the original socket
1101 */
62e76326 1102
0b77ecd8 1103 if (commBind(fd, F->local_addr, F->local_port) != COMM_OK) {
bf8fe701 1104 debugs(5, 0, "commResetFD: bind: " << xstrerror());
62e76326 1105 return 0;
09544acc 1106 }
62e76326 1107
d6827718 1108#ifdef IP_TOS
1109 if (F->tos) {
0b77ecd8 1110 if (setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &F->tos, sizeof(int)) < 0)
bf8fe701 1111 debugs(50, 1, "commResetFD: setsockopt(IP_TOS) on FD " << fd << ": " << xstrerror());
d6827718 1112 }
62e76326 1113
d6827718 1114#endif
0b77ecd8 1115 copyFDFlags (fd, F);
62e76326 1116
edeb28fd 1117 return 1;
1118}
1119
0b77ecd8 1120int
1121ConnectStateData::commRetryConnect()
22c653cd 1122{
0b77ecd8 1123 assert(addrcount > 0);
62e76326 1124
0b77ecd8 1125 if (addrcount == 1) {
1126 if (tries >= Config.retry.maxtries)
62e76326 1127 return 0;
1128
0b77ecd8 1129 if (squid_curtime - connstart > Config.Timeout.connect)
62e76326 1130 return 0;
22c653cd 1131 } else {
0b77ecd8 1132 if (tries > addrcount)
62e76326 1133 return 0;
22c653cd 1134 }
62e76326 1135
0b77ecd8 1136 return commResetFD();
22c653cd 1137}
1138
4ed0e075 1139static void
1140commReconnect(void *data)
1141{
1142 ConnectStateData *cs = (ConnectStateData *)data;
1143 ipcache_nbgethostbyname(cs->host, commConnectDnsHandle, cs);
1144}
1145
e924600d 1146/* Connect SOCK to specified DEST_PORT at DEST_HOST. */
2d8c0b1a 1147void
1148ConnectStateData::Connect (int fd, void *me)
090089c4 1149{
2d8c0b1a 1150 ConnectStateData *cs = (ConnectStateData *)me;
1151 assert (cs->fd == fd);
1152 cs->connect();
1153}
1154
1155void
1156ConnectStateData::defaults()
1157{
1158 S.sin_family = AF_INET;
1159 S.sin_addr = in_addr;
1160 S.sin_port = htons(port);
2d8c0b1a 1161}
62e76326 1162
2d8c0b1a 1163void
1164ConnectStateData::connect()
1165{
1166 if (S.sin_addr.s_addr == 0)
1167 defaults();
62e76326 1168
2d8c0b1a 1169 switch (comm_connect_addr(fd, &S)) {
62e76326 1170
e5f6c5c2 1171 case COMM_INPROGRESS:
bf8fe701 1172 debugs(5, 5, "ConnectStateData::connect: FD " << fd << ": COMM_INPROGRESS");
2d8c0b1a 1173 commSetSelect(fd, COMM_SELECT_WRITE, ConnectStateData::Connect, this, 0);
62e76326 1174 break;
1175
e5f6c5c2 1176 case COMM_OK:
2d8c0b1a 1177 ipcacheMarkGoodAddr(host, S.sin_addr);
1178 callCallback(COMM_OK, 0);
62e76326 1179 break;
1180
e5f6c5c2 1181 default:
2d8c0b1a 1182 tries++;
1183 ipcacheMarkBadAddr(host, S.sin_addr);
62e76326 1184
1185 if (Config.onoff.test_reachability)
2d8c0b1a 1186 netdbDeleteAddrNetwork(S.sin_addr);
62e76326 1187
0b77ecd8 1188 if (commRetryConnect()) {
4ed0e075 1189 eventAdd("commReconnect", commReconnect, this, this->addrcount == 1 ? 0.05 : 0.0, 0);
62e76326 1190 } else {
2d8c0b1a 1191 callCallback(COMM_ERR_CONNECT, errno);
62e76326 1192 }
090089c4 1193 }
090089c4 1194}
22c653cd 1195
b8d8561b 1196int
4f92c80c 1197commSetTimeout(int fd, int timeout, PF * handler, void *data)
090089c4 1198{
bf8fe701 1199 debugs(5, 3, "commSetTimeout: FD " << fd << " timeout " << timeout);
03eb2f01 1200 assert(fd >= 0);
1201 assert(fd < Squid_MaxFD);
2d8c0b1a 1202 fde *F = &fd_table[fd];
60c0b5a2 1203 assert(F->flags.open);
62e76326 1204
5c5783a2 1205 if (timeout < 0) {
62e76326 1206 cbdataReferenceDone(F->timeout_data);
1207 F->timeout_handler = NULL;
1208 F->timeout = 0;
5849612f 1209 } else {
62e76326 1210 if (handler) {
1211 cbdataReferenceDone(F->timeout_data);
1212 F->timeout_handler = handler;
1213 F->timeout_data = cbdataReference(data);
1214 }
1215
1216 F->timeout = squid_curtime + (time_t) timeout;
30a4f2a8 1217 }
62e76326 1218
a3fa14bf 1219 return F->timeout;
090089c4 1220}
1221
b8d8561b 1222int
62e76326 1223
0ee4272b 1224comm_connect_addr(int sock, const struct sockaddr_in *address)
090089c4 1225{
3d7e9d7c 1226 comm_err_t status = COMM_OK;
76f87348 1227 fde *F = &fd_table[sock];
090089c4 1228 int x;
b5568a61 1229 int err = 0;
9689d97c 1230 socklen_t errlen;
489b22c1 1231 assert(ntohs(address->sin_port) != 0);
88bfe092 1232 PROF_start(comm_connect_addr);
090089c4 1233 /* Establish connection. */
b5568a61 1234 errno = 0;
62e76326 1235
1236 if (!F->flags.called_connect)
1237 {
1238 F->flags.called_connect = 1;
1239 statCounter.syscalls.sock.connects++;
1240
1241 x = connect(sock, (struct sockaddr *) address, sizeof(*address));
1242
1243 if (x < 0)
bf8fe701 1244 debugs(5, 9, "connect FD " << sock << ": " << xstrerror());
62e76326 1245 } else
1246 {
140e2c0b 1247#if defined(_SQUID_NEWSOS6_)
62e76326 1248 /* Makoto MATSUSHITA <matusita@ics.es.osaka-u.ac.jp> */
1249
1250 connect(sock, (struct sockaddr *) address, sizeof(*address));
1251
1252 if (errno == EINVAL) {
1253 errlen = sizeof(err);
1254 x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen);
1255
1256 if (x >= 0)
1257 errno = x;
1258 }
1259
33ac9442 1260#else
62e76326 1261 errlen = sizeof(err);
1262
1263 x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen);
1264
1265 if (x == 0)
1266 errno = err;
1267
b5568a61 1268#if defined(_SQUID_SOLARIS_)
62e76326 1269 /*
1270 * Solaris 2.4's socket emulation doesn't allow you
1271 * to determine the error from a failed non-blocking
1272 * connect and just returns EPIPE. Create a fake
1273 * error message for connect. -- fenner@parc.xerox.com
1274 */
1275 if (x < 0 && errno == EPIPE)
1276 errno = ENOTCONN;
1277
33ac9442 1278#endif
30a4f2a8 1279#endif
62e76326 1280
e5f6c5c2 1281 }
62e76326 1282
88bfe092 1283 PROF_stop(comm_connect_addr);
62e76326 1284
b5568a61 1285 if (errno == 0 || errno == EISCONN)
62e76326 1286 status = COMM_OK;
b5568a61 1287 else if (ignoreErrno(errno))
62e76326 1288 status = COMM_INPROGRESS;
b5568a61 1289 else
62e76326 1290 return COMM_ERROR;
1291
76f87348 1292 xstrncpy(F->ipaddr, inet_ntoa(address->sin_addr), 16);
62e76326 1293
76f87348 1294 F->remote_port = ntohs(address->sin_port);
62e76326 1295
1296 if (status == COMM_OK)
1297 {
bf8fe701 1298 debugs(5, 10, "comm_connect_addr: FD " << sock << " connected to " << F->ipaddr << ":" << F->remote_port);
62e76326 1299 } else if (status == COMM_INPROGRESS)
1300 {
bf8fe701 1301 debugs(5, 10, "comm_connect_addr: FD " << sock << " connection pending");
090089c4 1302 }
62e76326 1303
090089c4 1304 return status;
1305}
1306
1307/* Wait for an incoming connection on FD. FD should be a socket returned
1308 * from comm_listen. */
ee0989f2 1309static int
1310comm_old_accept(int fd, ConnectionDetail &details)
090089c4 1311{
88bfe092 1312 PROF_start(comm_accept);
ee0989f2 1313 statCounter.syscalls.sock.accepts++;
1314 int sock;
1315 socklen_t Slen = sizeof(details.peer);
62e76326 1316
ee0989f2 1317 if ((sock = accept(fd, (struct sockaddr *) &details.peer, &Slen)) < 0) {
62e76326 1318 PROF_stop(comm_accept);
1319
1320 if (ignoreErrno(errno))
1321 {
bf8fe701 1322 debugs(50, 5, "comm_old_accept: FD " << fd << ": " << xstrerror());
62e76326 1323 return COMM_NOMESSAGE;
1324 } else if (ENFILE == errno || EMFILE == errno)
1325 {
bf8fe701 1326 debugs(50, 3, "comm_old_accept: FD " << fd << ": " << xstrerror());
62e76326 1327 return COMM_ERROR;
1328 } else
1329 {
bf8fe701 1330 debugs(50, 1, "comm_old_accept: FD " << fd << ": " << xstrerror());
62e76326 1331 return COMM_ERROR;
1332 }
090089c4 1333 }
62e76326 1334
ee0989f2 1335 Slen = sizeof(details.me);
1336 memset(&details.me, '\0', Slen);
62e76326 1337
ee0989f2 1338 getsockname(sock, (struct sockaddr *) &details.me, &Slen);
3ca60c86 1339 commSetCloseOnExec(sock);
090089c4 1340 /* fdstat update */
5c5783a2 1341 fd_open(sock, FD_SOCKET, "HTTP Request");
c4b7a5a9 1342 fdd_table[sock].close_file = NULL;
1343 fdd_table[sock].close_line = 0;
1344 fdc_table[sock].active = 1;
ee0989f2 1345 fde *F = &fd_table[sock];
1346 xstrncpy(F->ipaddr, inet_ntoa(details.peer.sin_addr), 16);
1347 F->remote_port = htons(details.peer.sin_port);
1348 F->local_port = htons(details.me.sin_port);
090089c4 1349 commSetNonBlocking(sock);
88bfe092 1350 PROF_stop(comm_accept);
090089c4 1351 return sock;
1352}
1353
cb201b7e 1354void
1355commCallCloseHandlers(int fd)
1356{
76f87348 1357 fde *F = &fd_table[fd];
bf8fe701 1358 debugs(5, 5, "commCallCloseHandlers: FD " << fd);
62e76326 1359
8000a965 1360 while (F->closeHandler != NULL) {
62e76326 1361 close_handler ch = *F->closeHandler;
b001e822 1362 conn_close_pool->free(F->closeHandler); /* AAA */
62e76326 1363 F->closeHandler = ch.next;
1364 ch.next = NULL;
bf8fe701 1365 debugs(5, 5, "commCallCloseHandlers: ch->handler=" << ch.handler << " data=" << ch.data);
62e76326 1366
1367 if (cbdataReferenceValid(ch.data))
1368 ch.handler(fd, ch.data);
1369
1370 cbdataReferenceDone(ch.data);
cb201b7e 1371 }
1372}
1373
5492ad1d 1374#if LINGERING_CLOSE
1375static void
1376commLingerClose(int fd, void *unused)
1377{
1378 LOCAL_ARRAY(char, buf, 1024);
1379 int n;
1f7c9178 1380 n = FD_READ_METHOD(fd, buf, 1024);
62e76326 1381
5492ad1d 1382 if (n < 0)
bf8fe701 1383 debugs(5, 3, "commLingerClose: FD " << fd << " read: " << xstrerror());
62e76326 1384
5492ad1d 1385 comm_close(fd);
1386}
1387
1388static void
1389commLingerTimeout(int fd, void *unused)
1390{
bf8fe701 1391 debugs(5, 3, "commLingerTimeout: FD " << fd);
5492ad1d 1392 comm_close(fd);
1393}
1394
1395/*
1396 * Inspired by apache
1397 */
1398void
1399comm_lingering_close(int fd)
1400{
d4c19b39 1401#if USE_SSL
62e76326 1402
d4c19b39 1403 if (fd_table[fd].ssl)
62e76326 1404 ssl_shutdown_method(fd);
1405
d4c19b39 1406#endif
62e76326 1407
5492ad1d 1408 if (shutdown(fd, 1) < 0) {
62e76326 1409 comm_close(fd);
1410 return;
5492ad1d 1411 }
62e76326 1412
5492ad1d 1413 fd_note(fd, "lingering close");
1414 commSetTimeout(fd, 10, commLingerTimeout, NULL);
1415 commSetSelect(fd, COMM_SELECT_READ, commLingerClose, NULL, 0);
1416}
62e76326 1417
5492ad1d 1418#endif
1419
98264874 1420/*
1421 * enable linger with time of 0 so that when the socket is
1422 * closed, TCP generates a RESET
1423 */
1424void
1425comm_reset_close(int fd)
1426{
62e76326 1427
98264874 1428 struct linger L;
1429 L.l_onoff = 1;
1430 L.l_linger = 0;
62e76326 1431
98264874 1432 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0)
bf8fe701 1433 debugs(50, 0, "commResetTCPClose: FD " << fd << ": " << xstrerror());
62e76326 1434
98264874 1435 comm_close(fd);
1436}
1437
2d8c0b1a 1438void
1439CommRead::nullCallback()
1440{
1441 callback = CallBack<IOCB>();
1442}
1443
1444void
1445AcceptFD::nullCallback()
1446{
1447 callback = CallBack<IOACB>();
1448}
1449
1450void
1451CommRead::doCallback(comm_err_t errcode, int xerrno)
1452{
1453 if (callback.handler)
1454 callback.handler(fd, buf, 0, errcode, xerrno, callback.data);
1455
1456 nullCallback();
1457}
1458
1459void
1460AcceptFD::doCallback(int fd, int newfd, comm_err_t errcode, int xerrno, ConnectionDetail *connDetails)
1461{
1462 if (callback.handler) {
1463 CallBack<IOACB> aCallback = callback;
1464 nullCallback();
1465 aCallback.handler(fd, newfd, connDetails, errcode, xerrno, aCallback.data);
1466 }
1467}
c4b7a5a9 1468
1469/*
1470 * Close the socket fd.
1471 *
1472 * + call write handlers with ERR_CLOSING
1473 * + call read handlers with ERR_CLOSING
1474 * + call closing handlers
a46d2c0e 1475 *
1476 * NOTE: COMM_ERR_CLOSING will NOT be called for CommReads' sitting in a
1477 * DeferredReadManager.
c4b7a5a9 1478 */
b8d8561b 1479void
43ae1d95 1480_comm_close(int fd, char const *file, int line)
090089c4 1481{
76f87348 1482 fde *F = NULL;
c4b7a5a9 1483 dlink_node *node;
1484 CommCallbackData *cio;
1f7c9178 1485
bf8fe701 1486 debugs(5, 5, "comm_close: FD " << fd);
03eb2f01 1487 assert(fd >= 0);
1488 assert(fd < Squid_MaxFD);
76f87348 1489 F = &fd_table[fd];
c4b7a5a9 1490 fdd_table[fd].close_file = file;
1491 fdd_table[fd].close_line = line;
1f7c9178 1492
58a6c186 1493 if (F->flags.closing)
62e76326 1494 return;
1495
60c0b5a2 1496 if (shutting_down && (!F->flags.open || F->type == FD_FILE))
62e76326 1497 return;
1498
60c0b5a2 1499 assert(F->flags.open);
62e76326 1500
c4b7a5a9 1501 /* The following fails because ipc.c is doing calls to pipe() to create sockets! */
62e76326 1502 assert(fdc_table[fd].active == 1);
1503
76f87348 1504 assert(F->type != FD_FILE);
62e76326 1505
88bfe092 1506 PROF_start(comm_close);
62e76326 1507
58a6c186 1508 F->flags.closing = 1;
62e76326 1509
d4c19b39 1510#if USE_SSL
62e76326 1511
d4c19b39 1512 if (F->ssl)
62e76326 1513 ssl_shutdown_method(fd);
1514
d4c19b39 1515#endif
62e76326 1516
fa80a8ef 1517 commSetTimeout(fd, -1, NULL, NULL);
62e76326 1518
2b663917 1519 /* new-style read/write handler stuff */
1520 if (commio_has_callback(fd, IOCB_WRITE, COMMIO_FD_WRITECB(fd))) {
1521 commio_complete_callback(fd, COMMIO_FD_WRITECB(fd), COMM_ERR_CLOSING, errno);
1522 commio_call_callback(COMMIO_FD_WRITECB(fd));
1523 }
1524 if (commio_has_callback(fd, IOCB_READ, COMMIO_FD_READCB(fd))) {
1525 commio_complete_callback(fd, COMMIO_FD_READCB(fd), COMM_ERR_CLOSING, errno);
1526 commio_call_callback(COMMIO_FD_READCB(fd));
1527 }
2d8c0b1a 1528
2b663917 1529 /* Do callbacks for read/accept routines, if any */
2d8c0b1a 1530 fdc_table[fd].accept.accept.doCallback(fd, -1, COMM_ERR_CLOSING, 0, NULL);
62e76326 1531
511e2383 1532 /* Complete (w/ COMM_ERR_CLOSING!) any pending io callbacks */
c4b7a5a9 1533 while (fdc_table[fd].CommCallbackList.head != NULL) {
62e76326 1534 node = fdc_table[fd].CommCallbackList.head;
1535 cio = (CommCallbackData *)node->data;
2d8c0b1a 1536 assert(fd == cio->result.fd); /* just paranoid */
62e76326 1537 /* We're closing! */
2d8c0b1a 1538 cio->fdClosing();
1539 cio->callACallback();
00d77d6b 1540 delete cio;
c4b7a5a9 1541 }
1542
cb201b7e 1543 commCallCloseHandlers(fd);
62e76326 1544
781ce8ff 1545 if (F->pconn.uses)
1546 F->pconn.pool->count(F->pconn.uses);
62e76326 1547
a7ad6e4e 1548 comm_empty_os_read_buffers(fd);
62e76326 1549
d4c19b39 1550#if USE_SSL
62e76326 1551
d4c19b39 1552 if (F->ssl) {
62e76326 1553 SSL_free(F->ssl);
1554 F->ssl = NULL;
d4c19b39 1555 }
62e76326 1556
d4c19b39 1557#endif
5c5783a2 1558 fd_close(fd); /* update fdstat */
62e76326 1559
5874bf28 1560 close(fd);
62e76326 1561
c4b7a5a9 1562 fdc_table[fd].active = 0;
62e76326 1563
a46d2c0e 1564 if (fdc_table[fd].half_closed) {
1565 AbortChecker::Instance().stopMonitoring(fd);
1566 fdc_table[fd].half_closed = false;
1567 }
1568
2d8c0b1a 1569 fdc_table[fd] = fdc_t(fd);
62e76326 1570
83704487 1571 statCounter.syscalls.sock.closes++;
62e76326 1572
88bfe092 1573 PROF_stop(comm_close);
a46d2c0e 1574 /* When an fd closes, give accept() a chance, if need be */
1575
1576 if (fdNFree() >= RESERVED_FD)
1577 AcceptLimiter::Instance().kick();
090089c4 1578}
1579
090089c4 1580/* Send a udp datagram to specified TO_ADDR. */
b8d8561b 1581int
5df61230 1582comm_udp_sendto(int fd,
62e76326 1583
1584 const struct sockaddr_in *to_addr,
1585 int addr_len,
1586 const void *buf,
1587 int len)
090089c4 1588{
5df61230 1589 int x;
88bfe092 1590 PROF_start(comm_udp_sendto);
83704487 1591 statCounter.syscalls.sock.sendtos++;
62e76326 1592
5df61230 1593 x = sendto(fd, buf, len, 0, (struct sockaddr *) to_addr, addr_len);
88bfe092 1594 PROF_stop(comm_udp_sendto);
62e76326 1595
2d8c0b1a 1596 if (x >= 0)
1597 return x;
1598
17d51783 1599#ifdef _SQUID_LINUX_
62e76326 1600
2d8c0b1a 1601 if (ECONNREFUSED != errno)
17d51783 1602#endif
62e76326 1603
bf8fe701 1604 debugs(50, 1, "comm_udp_sendto: FD " << fd << ", " <<
1605 inet_ntoa(to_addr->sin_addr) << ", port " <<
1606 (int) htons(to_addr->sin_port) << ": " << xstrerror());
62e76326 1607
2d8c0b1a 1608 return COMM_ERROR;
090089c4 1609}
1610
b8d8561b 1611void
582b6456 1612comm_add_close_handler(int fd, PF * handler, void *data)
30a4f2a8 1613{
b001e822 1614 close_handler *newHandler = (close_handler *)conn_close_pool->alloc(); /* AAA */
cddc721b 1615 close_handler *c;
bf8fe701 1616 debugs(5, 5, "comm_add_close_handler: FD " << fd << ", handler=" <<
1617 handler << ", data=" << data);
62e76326 1618
29b8d8d6 1619 for (c = fd_table[fd].closeHandler; c; c = c->next)
62e76326 1620 assert(c->handler != handler || c->data != data);
1621
e6ccf245 1622 newHandler->handler = handler;
62e76326 1623
e6ccf245 1624 newHandler->data = cbdataReference(data);
62e76326 1625
e6ccf245 1626 newHandler->next = fd_table[fd].closeHandler;
62e76326 1627
e6ccf245 1628 fd_table[fd].closeHandler = newHandler;
30a4f2a8 1629}
1630
b8d8561b 1631void
582b6456 1632comm_remove_close_handler(int fd, PF * handler, void *data)
090089c4 1633{
2f0fda5b 1634 assert (fdc_table[fd].active);
f1dc9b30 1635 close_handler *p;
1636 close_handler *last = NULL;
30a4f2a8 1637 /* Find handler in list */
bf8fe701 1638 debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", handler=" <<
1639 handler << ", data=" << data);
62e76326 1640
29b8d8d6 1641 for (p = fd_table[fd].closeHandler; p != NULL; last = p, p = p->next)
62e76326 1642 if (p->handler == handler && p->data == data)
1643 break; /* This is our handler */
1644
f88211e8 1645 assert(p != NULL);
62e76326 1646
30a4f2a8 1647 /* Remove list entry */
1648 if (last)
62e76326 1649 last->next = p->next;
30a4f2a8 1650 else
62e76326 1651 fd_table[fd].closeHandler = p->next;
1652
fa80a8ef 1653 cbdataReferenceDone(p->data);
62e76326 1654
b001e822 1655 conn_close_pool->free(p);
30a4f2a8 1656}
090089c4 1657
b8d8561b 1658static void
1659commSetNoLinger(int fd)
30a4f2a8 1660{
62e76326 1661
30a4f2a8 1662 struct linger L;
090089c4 1663 L.l_onoff = 0; /* off */
1664 L.l_linger = 0;
62e76326 1665
30a4f2a8 1666 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0)
bf8fe701 1667 debugs(50, 0, "commSetNoLinger: FD " << fd << ": " << xstrerror());
62e76326 1668
58a6c186 1669 fd_table[fd].flags.nolinger = 1;
090089c4 1670}
1671
b8d8561b 1672static void
1673commSetReuseAddr(int fd)
090089c4 1674{
1675 int on = 1;
62e76326 1676
30a4f2a8 1677 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0)
bf8fe701 1678 debugs(50, 1, "commSetReuseAddr: FD " << fd << ": " << xstrerror());
090089c4 1679}
1680
b8d8561b 1681static void
1682commSetTcpRcvbuf(int fd, int size)
f868539a 1683{
1684 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size)) < 0)
bf8fe701 1685 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd << ", SIZE " << size << ": " << xstrerror());
f868539a 1686}
1687
b8d8561b 1688int
1689commSetNonBlocking(int fd)
30a4f2a8 1690{
a50bfe93 1691#ifndef _SQUID_MSWIN_
731e4d49 1692 int flags;
9e205701 1693 int dummy = 0;
a50bfe93 1694#endif
ec4daaa5 1695#ifdef _SQUID_WIN32_
62e76326 1696
b05490a8 1697 int nonblocking = TRUE;
62e76326 1698
629b5f75 1699#ifdef _SQUID_CYGWIN_
1700
7f6ffd15 1701 if (fd_table[fd].type != FD_PIPE) {
629b5f75 1702#endif
1703
62e76326 1704 if (ioctl(fd, FIONBIO, &nonblocking) < 0) {
bf8fe701 1705 debugs(50, 0, "commSetNonBlocking: FD " << fd << ": " << xstrerror() << " " << fd_table[fd].type);
62e76326 1706 return COMM_ERROR;
1707 }
629b5f75 1708
1709#ifdef _SQUID_CYGWIN_
1710
7f6ffd15 1711 } else {
1712#endif
629b5f75 1713#endif
a50bfe93 1714#ifndef _SQUID_MSWIN_
62e76326 1715
1716 if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
bf8fe701 1717 debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror());
62e76326 1718 return COMM_ERROR;
1719 }
1720
1721 if (fcntl(fd, F_SETFL, flags | SQUID_NONBLOCK) < 0) {
bf8fe701 1722 debugs(50, 0, "commSetNonBlocking: FD " << fd << ": " << xstrerror());
62e76326 1723 return COMM_ERROR;
1724 }
1725
a50bfe93 1726#endif
629b5f75 1727#ifdef _SQUID_CYGWIN_
62e76326 1728
090089c4 1729 }
62e76326 1730
7f6ffd15 1731#endif
58a6c186 1732 fd_table[fd].flags.nonblocking = 1;
62e76326 1733
090089c4 1734 return 0;
1735}
1736
7e3ce7b9 1737int
1738commUnsetNonBlocking(int fd)
1739{
a50bfe93 1740#ifdef _SQUID_MSWIN_
1741 int nonblocking = FALSE;
1742
1743 if (ioctlsocket(fd, FIONBIO, (unsigned long *) &nonblocking) < 0) {
1744#else
7e3ce7b9 1745 int flags;
1746 int dummy = 0;
62e76326 1747
7e3ce7b9 1748 if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
bf8fe701 1749 debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror());
62e76326 1750 return COMM_ERROR;
7e3ce7b9 1751 }
62e76326 1752
7e3ce7b9 1753 if (fcntl(fd, F_SETFL, flags & (~SQUID_NONBLOCK)) < 0) {
a50bfe93 1754#endif
bf8fe701 1755 debugs(50, 0, "commUnsetNonBlocking: FD " << fd << ": " << xstrerror());
62e76326 1756 return COMM_ERROR;
7e3ce7b9 1757 }
62e76326 1758
7e3ce7b9 1759 fd_table[fd].flags.nonblocking = 0;
1760 return 0;
1761}
1762
b8d8561b 1763void
a50bfe93 1764commSetCloseOnExec(int fd) {
3ca60c86 1765#ifdef FD_CLOEXEC
731e4d49 1766 int flags;
7a18b487 1767 int dummy = 0;
62e76326 1768
c7989865 1769 if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
bf8fe701 1770 debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror());
62e76326 1771 return;
3ca60c86 1772 }
62e76326 1773
24382924 1774 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
bf8fe701 1775 debugs(50, 0, "FD " << fd << ": set close-on-exec failed: " << xstrerror());
62e76326 1776
d6827718 1777 fd_table[fd].flags.close_on_exec = 1;
62e76326 1778
3ca60c86 1779#endif
1780}
1781
e90100aa 1782#ifdef TCP_NODELAY
1783static void
a50bfe93 1784commSetTcpNoDelay(int fd) {
e90100aa 1785 int on = 1;
62e76326 1786
e90100aa 1787 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) < 0)
bf8fe701 1788 debugs(50, 1, "commSetTcpNoDelay: FD " << fd << ": " << xstrerror());
62e76326 1789
d6827718 1790 fd_table[fd].flags.nodelay = 1;
e90100aa 1791}
62e76326 1792
e90100aa 1793#endif
1794
6a988308 1795
d86b3703 1796void
a50bfe93 1797comm_init(void) {
c4b7a5a9 1798 fd_table =(fde *) xcalloc(Squid_MaxFD, sizeof(fde));
1799 fdd_table = (fd_debug_t *)xcalloc(Squid_MaxFD, sizeof(fd_debug_t));
2d8c0b1a 1800 fdc_table = new fdc_t[Squid_MaxFD];
2b663917 1801 commfd_table = (comm_fd_t *) xcalloc(Squid_MaxFD, sizeof(comm_fd_t));
2d8c0b1a 1802
2b663917 1803 for (int pos = 0; pos < Squid_MaxFD; ++pos) {
2d8c0b1a 1804 fdc_table[pos] = fdc_t(pos);
2b663917 1805 }
1806 for (int pos = 0; pos < Squid_MaxFD; pos++) {
1807 commfd_table[pos].fd = pos;
1808 commfd_table[pos].readcb.fd = pos;
1809 commfd_table[pos].readcb.type = IOCB_READ;
1810 commfd_table[pos].writecb.fd = pos;
1811 commfd_table[pos].writecb.type = IOCB_WRITE;
1812 }
2d8c0b1a 1813
59c4d35b 1814 /* XXX account fd_table */
090089c4 1815 /* Keep a few file descriptors free so that we don't run out of FD's
1816 * after accepting a client but before it opens a socket or a file.
e83892e9 1817 * Since Squid_MaxFD can be as high as several thousand, don't waste them */
0254ee29 1818 RESERVED_FD = XMIN(100, Squid_MaxFD / 4);
2d8c0b1a 1819
04eb0689 1820 conn_close_pool = memPoolCreate("close_handler", sizeof(close_handler));
090089c4 1821}
1822
30a4f2a8 1823/* Write to FD. */
b8d8561b 1824static void
a50bfe93 1825commHandleWrite(int fd, void *data) {
2b663917 1826 comm_io_callback_t *state = (comm_io_callback_t *)data;
30a4f2a8 1827 int len = 0;
1828 int nleft;
1829
2b663917 1830 assert(state == COMMIO_FD_WRITECB(fd));
1831
88bfe092 1832 PROF_start(commHandleWrite);
bf8fe701 1833 debugs(5, 5, "commHandleWrite: FD " << fd << ": off " <<
1834 (long int) state->offset << ", sz " << (long int) state->size << ".");
30a4f2a8 1835
1836 nleft = state->size - state->offset;
1f7c9178 1837 len = FD_WRITE_METHOD(fd, state->buf + state->offset, nleft);
bf8fe701 1838 debugs(5, 5, "commHandleWrite: write() returns " << len);
b69f7771 1839 fd_bytes(fd, len, FD_WRITE);
83704487 1840 statCounter.syscalls.sock.writes++;
30a4f2a8 1841
1842 if (len == 0) {
62e76326 1843 /* Note we even call write if nleft == 0 */
1844 /* We're done */
1845
1846 if (nleft != 0)
bf8fe701 1847 debugs(5, 1, "commHandleWrite: FD " << fd << ": write failure: connection closed with " << nleft << " bytes remaining.");
62e76326 1848
2b663917 1849 commio_complete_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_ERROR : COMM_OK, errno);
30a4f2a8 1850 } else if (len < 0) {
62e76326 1851 /* An error */
1852
1853 if (fd_table[fd].flags.socket_eof) {
bf8fe701 1854 debugs(50, 2, "commHandleWrite: FD " << fd << ": write failure: " << xstrerror() << ".");
2b663917 1855 commio_complete_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_ERROR : COMM_OK, errno);
62e76326 1856 } else if (ignoreErrno(errno)) {
bf8fe701 1857 debugs(50, 10, "commHandleWrite: FD " << fd << ": write failure: " << xstrerror() << ".");
62e76326 1858 commSetSelect(fd,
1859 COMM_SELECT_WRITE,
1860 commHandleWrite,
1861 state,
1862 0);
1863 } else {
bf8fe701 1864 debugs(50, 2, "commHandleWrite: FD " << fd << ": write failure: " << xstrerror() << ".");
2b663917 1865 commio_complete_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_ERROR : COMM_OK, errno);
62e76326 1866 }
30a4f2a8 1867 } else {
62e76326 1868 /* A successful write, continue */
1869 state->offset += len;
1870
1871 if (state->offset < (off_t)state->size) {
1872 /* Not done, reinstall the write handler and write some more */
1873 commSetSelect(fd,
1874 COMM_SELECT_WRITE,
1875 commHandleWrite,
1876 state,
1877 0);
1878 } else {
2b663917 1879 commio_complete_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_OK : COMM_ERROR, errno);
62e76326 1880 }
30a4f2a8 1881 }
62e76326 1882
88bfe092 1883 PROF_stop(commHandleWrite);
30a4f2a8 1884}
1885
7cd8c414 1886/*
1887 * Queue a write. handler/handler_data are called when the write
1888 * completes, on error, or on file descriptor close.
1889 *
1890 * free_func is used to free the passed buffer when the write has completed.
1891 */
b8d8561b 1892void
2b663917 1893comm_write(int fd, const char *buf, int size, IOCB * handler, void *handler_data, FREE * free_func)
1894{
c4b7a5a9 1895 assert(!fd_table[fd].flags.closing);
1896
bf8fe701 1897 debugs(5, 5, "comm_write: FD " << fd << ": sz " << size << ": hndl " << handler << ": data " << handler_data << ".");
62e76326 1898
2b663917 1899 if (commio_has_callback(fd, IOCB_WRITE, COMMIO_FD_WRITECB(fd))) {
dec5db5d 1900 /* This means that the write has been scheduled, but has not
1901 * triggered yet
1902 */
2b663917 1903 fatalf ("comm_write: fd %d: pending callback!\n", fd);
6cf028ab 1904 }
2b663917 1905 /* XXX ugly */
1906 commio_set_callback(fd, IOCB_WRITE, COMMIO_FD_WRITECB(fd), handler, handler_data, (char *)buf, free_func, size);
1907 commSetSelect(fd, COMM_SELECT_WRITE, commHandleWrite, COMMIO_FD_WRITECB(fd), 0);
30a4f2a8 1908}
26a880e2 1909
137ee196 1910/* a wrapper around comm_write to allow for MemBuf to be comm_written in a snap */
cb69b4c7 1911void
2b663917 1912comm_write_mbuf(int fd, MemBuf *mb, IOCB * handler, void *handler_data) {
1913 comm_write(fd, mb->buf, mb->size, handler, handler_data, mb->freeFunc());
cb69b4c7 1914}
1915
c4b7a5a9 1916
89924214 1917/*
1918 * hm, this might be too general-purpose for all the places we'd
1919 * like to use it.
1920 */
b224ea98 1921int
a50bfe93 1922ignoreErrno(int ierrno) {
603500e7 1923 switch (ierrno) {
62e76326 1924
89924214 1925 case EINPROGRESS:
62e76326 1926
603500e7 1927 case EWOULDBLOCK:
26a880e2 1928#if EAGAIN != EWOULDBLOCK
62e76326 1929
603500e7 1930 case EAGAIN:
26a880e2 1931#endif
62e76326 1932
603500e7 1933 case EALREADY:
62e76326 1934
603500e7 1935 case EINTR:
db494ab8 1936#ifdef ERESTART
62e76326 1937
db494ab8 1938 case ERESTART:
1939#endif
62e76326 1940
1941 return 1;
1942
603500e7 1943 default:
62e76326 1944 return 0;
603500e7 1945 }
62e76326 1946
603500e7 1947 /* NOTREACHED */
26a880e2 1948}
d723bf6b 1949
1950void
a50bfe93 1951commCloseAllSockets(void) {
d723bf6b 1952 int fd;
1953 fde *F = NULL;
62e76326 1954
d723bf6b 1955 for (fd = 0; fd <= Biggest_FD; fd++) {
62e76326 1956 F = &fd_table[fd];
1957
1958 if (!F->flags.open)
1959 continue;
1960
1961 if (F->type != FD_SOCKET)
1962 continue;
1963
1964 if (F->flags.ipc) /* don't close inter-process sockets */
1965 continue;
1966
1967 if (F->timeout_handler) {
1968 PF *callback = F->timeout_handler;
1969 void *cbdata = NULL;
1970 F->timeout_handler = NULL;
bf8fe701 1971 debugs(5, 5, "commCloseAllSockets: FD " << fd << ": Calling timeout handler");
62e76326 1972
1973 if (cbdataReferenceValidDone(F->timeout_data, &cbdata))
1974 callback(fd, cbdata);
1975 } else {
bf8fe701 1976 debugs(5, 5, "commCloseAllSockets: FD " << fd << ": calling comm_close()");
62e76326 1977 comm_close(fd);
1978 }
d723bf6b 1979 }
1980}
1b3db6d9 1981
2d8c0b1a 1982static bool
a50bfe93 1983AlreadyTimedOut(fde *F) {
2d8c0b1a 1984 if (!F->flags.open)
1985 return true;
1986
1987 if (F->timeout == 0)
1988 return true;
1989
1990 if (F->timeout > squid_curtime)
1991 return true;
1992
1993 return false;
1994}
1995
1b3db6d9 1996void
a50bfe93 1997checkTimeouts(void) {
1b3db6d9 1998 int fd;
1999 fde *F = NULL;
2000 PF *callback;
62e76326 2001
1b3db6d9 2002 for (fd = 0; fd <= Biggest_FD; fd++) {
62e76326 2003 F = &fd_table[fd];
2004
2d8c0b1a 2005 if (AlreadyTimedOut(F))
62e76326 2006 continue;
2007
bf8fe701 2008 debugs(5, 5, "checkTimeouts: FD " << fd << " Expired");
62e76326 2009
2010 if (F->timeout_handler) {
bf8fe701 2011 debugs(5, 5, "checkTimeouts: FD " << fd << ": Call timeout handler");
62e76326 2012 callback = F->timeout_handler;
2013 F->timeout_handler = NULL;
2014 callback(fd, F->timeout_data);
2015 } else {
bf8fe701 2016 debugs(5, 5, "checkTimeouts: FD " << fd << ": Forcing comm_close()");
62e76326 2017 comm_close(fd);
2018 }
b5443c04 2019 }
2020}
2021
c4b7a5a9 2022/*
2023 * New-style listen and accept routines
2024 *
2025 * Listen simply registers our interest in an FD for listening,
2026 * and accept takes a callback to call when an FD has been
2027 * accept()ed.
2028 */
2029int
a50bfe93 2030comm_listen(int sock) {
c4b7a5a9 2031 int x;
62e76326 2032
c4b7a5a9 2033 if ((x = listen(sock, Squid_MaxFD >> 2)) < 0) {
bf8fe701 2034 debugs(50, 0, "comm_listen: listen(" << (Squid_MaxFD >> 2) << ", " << sock << "): " << xstrerror());
62e76326 2035 return x;
c4b7a5a9 2036 }
62e76326 2037
c4b7a5a9 2038 return sock;
2039}
2040
2d8c0b1a 2041void
a50bfe93 2042fdc_t::beginAccepting() {
2d8c0b1a 2043 accept.accept.beginAccepting();
2044}
c4b7a5a9 2045
2d8c0b1a 2046int
a50bfe93 2047fdc_t::acceptCount() const {
2d8c0b1a 2048 return accept.accept.acceptCount();
2049}
62e76326 2050
2d8c0b1a 2051void
a50bfe93 2052fdc_t::acceptOne(int fd) {
c99de607 2053 // If there is no callback and we accept, we will leak the accepted FD.
2054 // When we are running out of FDs, there is often no callback.
2055 if (!accept.accept.callback.handler) {
bf8fe701 2056 debugs(5, 5, "fdc_t::acceptOne orphaned: FD " << fd);
c99de607 2057 // XXX: can we remove this and similar "just in case" calls and
2058 // either listen always or listen only when there is a callback?
2059 if (!AcceptLimiter::Instance().deferring())
2060 commSetSelect(fd, COMM_SELECT_READ, comm_accept_try, NULL, 0);
2061 accept.accept.finished(true);
2062 return;
2063 }
2064
bdd8c442 2065 /*
2066 * We don't worry about running low on FDs here. Instead,
2067 * httpAccept() will use AcceptLimiter if we reach the limit
2068 * there.
2069 */
62e76326 2070
2d8c0b1a 2071 /* Accept a new connection */
2072 int newfd = comm_old_accept(fd, accept.connDetails);
62e76326 2073
2d8c0b1a 2074 /* Check for errors */
bdd8c442 2075
2d8c0b1a 2076 if (newfd < 0) {
2077 if (newfd == COMM_NOMESSAGE) {
2078 /* register interest again */
bf8fe701 2079 debugs(5, 5, "fdc_t::acceptOne eof: FD " << fd << " handler: " << (void*)accept.accept.callback.handler);
2d8c0b1a 2080 commSetSelect(fd, COMM_SELECT_READ, comm_accept_try, NULL, 0);
2081 accept.accept.finished(true);
62e76326 2082 return;
2083 }
2084
2d8c0b1a 2085 /* A non-recoverable error - register an error callback */
2086 new CommAcceptCallbackData(fd, accept.accept.callback, COMM_ERROR, errno, -1, accept.connDetails);
62e76326 2087
2d8c0b1a 2088 accept.accept.callback = CallBack<IOACB>();
62e76326 2089
2d8c0b1a 2090 accept.accept.finished(true);
62e76326 2091
2d8c0b1a 2092 return;
2093 }
62e76326 2094
bf8fe701 2095 debugs(5, 5, "fdc_t::acceptOne accepted: FD " << fd << " handler: " << (void*)accept.accept.callback.handler << " newfd: " << newfd);
c99de607 2096
2097 assert(accept.accept.callback.handler);
2d8c0b1a 2098 accept.accept.doCallback(fd, newfd, COMM_OK, 0, &accept.connDetails);
62e76326 2099
2d8c0b1a 2100 /* If we weren't re-registed, don't bother trying again! */
62e76326 2101
2d8c0b1a 2102 if (accept.accept.callback.handler == NULL)
2103 accept.accept.finished(true);
2104}
62e76326 2105
2d8c0b1a 2106bool
a50bfe93 2107AcceptFD::finished() const {
2d8c0b1a 2108 return finished_;
2109}
62e76326 2110
2d8c0b1a 2111void
a50bfe93 2112AcceptFD::finished(bool newValue) {
2d8c0b1a 2113 finished_ = newValue;
2114}
62e76326 2115
2d8c0b1a 2116bool
a50bfe93 2117AcceptFD::finishedAccepting() const {
2d8c0b1a 2118 return acceptCount() >= MAX_ACCEPT_PER_LOOP || finished();
c4b7a5a9 2119}
2120
2d8c0b1a 2121/*
2122 * This callback is called whenever a filedescriptor is ready
2123 * to dupe itself and fob off an accept()ed connection
2124 */
2125static void
a50bfe93 2126comm_accept_try(int fd, void *data) {
2d8c0b1a 2127 assert(fdc_table[fd].active == 1);
2128
2129 fdc_table[fd].beginAccepting();
2130
2131 while (!fdc_table[fd].accept.accept.finishedAccepting())
2132 fdc_table[fd].acceptOne(fd);
2133}
c4b7a5a9 2134
2135/*
2136 * Notes:
2137 * + the current interface will queue _one_ accept per io loop.
2138 * this isn't very optimal and should be revisited at a later date.
2139 */
2140void
a50bfe93 2141comm_accept(int fd, IOACB *handler, void *handler_data) {
bf8fe701 2142 debugs(5, 5, "comm_accept: FD " << fd << " handler: " << (void*)handler);
05589f93 2143 requireOpenAndActive(fd);
c4b7a5a9 2144
62e76326 2145 /* make sure we're not pending! */
2d8c0b1a 2146 assert(fdc_table[fd].accept.accept.callback.handler == NULL);
c4b7a5a9 2147
62e76326 2148 /* Record our details */
d242ad41 2149 fdc_table[fd].accept.accept.callback = CallBack<IOACB> (handler, handler_data);
c4b7a5a9 2150
62e76326 2151 /* Kick off the accept */
c4b7a5a9 2152#if OPTIMISTIC_IO
62e76326 2153
2154 comm_accept_try(fd, NULL);
c4b7a5a9 2155#else
62e76326 2156
2157 commSetSelect(fd, COMM_SELECT_READ, comm_accept_try, NULL, 0);
c4b7a5a9 2158#endif
2159}
6cce2334 2160
a50bfe93 2161void CommIO::Initialise() {
6cce2334 2162 /* Initialize done pipe signal */
2163 int DonePipe[2];
2164 pipe(DonePipe);
2165 DoneFD = DonePipe[1];
2166 DoneReadFD = DonePipe[0];
d06925a4 2167 fd_open(DoneReadFD, FD_PIPE, "async-io completetion event: main");
2168 fd_open(DoneFD, FD_PIPE, "async-io completetion event: threads");
2169 commSetNonBlocking(DoneReadFD);
2170 commSetNonBlocking(DoneFD);
2171 commSetSelect(DoneReadFD, COMM_SELECT_READ, NULLFDHandler, NULL, 0);
6cce2334 2172 Initialised = true;
2173}
2174
d06925a4 2175void CommIO::NotifyIOClose() {
2176 /* Close done pipe signal */
2177 FlushPipe();
2178 close(DoneFD);
2179 close(DoneReadFD);
2180 fd_close(DoneFD);
2181 fd_close(DoneReadFD);
2182 Initialised = false;
2183}
2184
6cce2334 2185bool CommIO::Initialised = false;
2186bool CommIO::DoneSignalled = false;
2187int CommIO::DoneFD = -1;
2188int CommIO::DoneReadFD = -1;
2189
2190void
a50bfe93 2191CommIO::FlushPipe() {
6cce2334 2192 char buf[256];
56410c89 2193 FD_READ_METHOD(DoneReadFD, buf, sizeof(buf));
6cce2334 2194}
2195
2196void
a50bfe93 2197CommIO::NULLFDHandler(int fd, void *data) {
6cce2334 2198 FlushPipe();
2199 commSetSelect(fd, COMM_SELECT_READ, NULLFDHandler, NULL, 0);
2200}
2201
2202void
a50bfe93 2203CommIO::ResetNotifications() {
6cce2334 2204 if (DoneSignalled) {
62e76326 2205 FlushPipe();
2206 DoneSignalled = false;
6cce2334 2207 }
2208}
a46d2c0e 2209
2210AcceptLimiter AcceptLimiter::Instance_;
2211
a50bfe93 2212AcceptLimiter &AcceptLimiter::Instance() {
a46d2c0e 2213 return Instance_;
2214}
2215
c99de607 2216bool
2217AcceptLimiter::deferring() const {
2218 return deferred.size() > 0;
2219}
2220
a46d2c0e 2221void
a50bfe93 2222AcceptLimiter::defer (int fd, Acceptor::AcceptorFunction *aFunc, void *data) {
bf8fe701 2223 debugs(5, 5, "AcceptLimiter::defer: FD " << fd << " handler: " << (void*)aFunc);
a46d2c0e 2224 Acceptor temp;
2225 temp.theFunction = aFunc;
2226 temp.acceptFD = fd;
2227 temp.theData = data;
2228 deferred.push_back(temp);
2229}
2230
2231void
a50bfe93 2232AcceptLimiter::kick() {
c99de607 2233 if (!deferring())
a46d2c0e 2234 return;
2235
2236 /* Yes, this means the first on is the last off....
2237 * If the list container was a little more friendly, we could sensibly us it.
2238 */
2239 Acceptor temp = deferred.pop_back();
2240
2241 comm_accept (temp.acceptFD, temp.theFunction, temp.theData);
2242}
2243
2244void
a50bfe93 2245commMarkHalfClosed(int fd) {
a46d2c0e 2246 assert (fdc_table[fd].active && !fdc_table[fd].half_closed);
2247 AbortChecker::Instance().monitor(fd);
2248 fdc_table[fd].half_closed = true;
2249}
2250
f900210a 2251int commIsHalfClosed(int fd) {
e85e41a2 2252 assert (fdc_table[fd].active);
f900210a 2253
2254 return fdc_table[fd].half_closed;
2255}
2256
2257void
2258commCheckHalfClosed(void *data) {
2259 AbortChecker::Instance().doIOLoop();
2260 eventAdd("commCheckHalfClosed", commCheckHalfClosed, NULL, 1.0, false);
2261}
2262
a46d2c0e 2263AbortChecker &AbortChecker::Instance() {return Instance_;}
2264
2265AbortChecker AbortChecker::Instance_;
2266
2267void
a50bfe93 2268AbortChecker::AbortCheckReader(int fd, char *, size_t size, comm_err_t flag, int xerrno, void *data) {
a46d2c0e 2269 assert (size == 0);
2270 /* sketch:
2271 * if the read is ok and 0, the conn is still open.
2272 * if the read is a fail, close the conn
2273 */
2274
2275 if (flag != COMM_OK && flag != COMM_ERR_CLOSING) {
bf8fe701 2276 debugs(5, 3, "AbortChecker::AbortCheckReader: FD " << fd << " aborted");
a46d2c0e 2277 comm_close(fd);
2278 }
2279}
2280
2281void
a50bfe93 2282AbortChecker::monitor(int fd) {
a46d2c0e 2283 assert (!contains(fd));
2284
2285 add
2286 (fd);
2287
bf8fe701 2288 debugs(5, 3, "AbortChecker::monitor: monitoring half closed FD " << fd << " for aborts");
a46d2c0e 2289}
2290
2291void
a50bfe93 2292AbortChecker::stopMonitoring (int fd) {
a46d2c0e 2293 assert (contains (fd));
2294
2295 remove
2296 (fd);
2297
bf8fe701 2298 debugs(5, 3, "AbortChecker::stopMonitoring: stopped monitoring half closed FD " << fd << " for aborts");
a46d2c0e 2299}
2300
2301#include "splay.h"
2302void
a50bfe93 2303AbortChecker::doIOLoop() {
f900210a 2304 fds->walk(RemoveCheck, this);
a46d2c0e 2305 fds->walk(AddCheck, this);
a46d2c0e 2306}
2307
2308void
a50bfe93 2309AbortChecker::AddCheck (int const &fd, void *data) {
a46d2c0e 2310 AbortChecker *me = (AbortChecker *)data;
2311 me->addCheck(fd);
2312}
2313
2314void
a50bfe93 2315AbortChecker::RemoveCheck (int const &fd, void *data) {
a46d2c0e 2316 AbortChecker *me = (AbortChecker *)data;
2317 me->removeCheck(fd);
2318}
2319
2320
2321int
a50bfe93 2322AbortChecker::IntCompare (int const &lhs, int const &rhs) {
a46d2c0e 2323 return lhs - rhs;
2324}
2325
2326bool
a50bfe93 2327AbortChecker::contains (int const fd) const {
a46d2c0e 2328 fds = fds->splay(fd, IntCompare);
2329
2330 if (splayLastResult != 0)
2331 return false;
2332
2333 return true;
2334}
2335
2336void
2337
2338AbortChecker::remove
a50bfe93 2339 (int const fd) {
a46d2c0e 2340
2341 fds = fds->remove
2342 (fd, IntCompare);
2343}
2344
2345void
2346
2347AbortChecker::add
a50bfe93 2348 (int const fd) {
a46d2c0e 2349 fds = fds->insert (fd, IntCompare);
2350}
2351
2352void
a50bfe93 2353AbortChecker::addCheck (int const fd) {
a46d2c0e 2354 /* assert comm_is_open (fd); */
2355 comm_read(fd, NULL, 0, AbortCheckReader, NULL);
2356}
2357
2358void
a50bfe93 2359AbortChecker::removeCheck (int const fd) {
a46d2c0e 2360 /*
2361 comm_read_cancel(fd, AbortCheckReader, NULL);
2362 */
2363}
2364
a50bfe93 2365CommRead::CommRead() : fd(-1), buf(NULL), len(0) {}
a46d2c0e 2366
2367CommRead::CommRead(int fd_, char *buf_, int len_, IOCB *handler_, void *data_)
a50bfe93 2368 : fd(fd_), buf(buf_), len(len_), callback(handler_, data_) {}
a46d2c0e 2369
a50bfe93 2370DeferredRead::DeferredRead () : theReader(NULL), theContext(NULL), theRead(), cancelled(false) {}
a46d2c0e 2371
a50bfe93 2372DeferredRead::DeferredRead (DeferrableRead *aReader, void *data, CommRead const &aRead) : theReader(aReader), theContext (data), theRead(aRead), cancelled(false) {}
a46d2c0e 2373
a50bfe93 2374DeferredReadManager::~DeferredReadManager() {
a46d2c0e 2375 flushReads();
2376 assert (deferredReads.empty());
2377}
2378
97427e90 2379/* explicit instantiation required for some systems */
2380
2381template cbdata_type List<DeferredRead>
2382::CBDATA_List;
2383
a46d2c0e 2384void
a50bfe93 2385DeferredReadManager::delayRead(DeferredRead const &aRead) {
bf8fe701 2386 debugs(5, 3, "Adding deferred read on FD " << aRead.theRead.fd);
a46d2c0e 2387 List<DeferredRead> *temp = deferredReads.push_back(aRead);
2388 comm_add_close_handler (aRead.theRead.fd, CloseHandler, temp);
2389}
2390
2391void
a50bfe93 2392DeferredReadManager::CloseHandler(int fd, void *thecbdata) {
a46d2c0e 2393 if (!cbdataReferenceValid (thecbdata))
2394 return;
2395
2396 List<DeferredRead> *temp = (List<DeferredRead> *)thecbdata;
2397
2398 temp->element.markCancelled();
2399}
2400
2401DeferredRead
a50bfe93 2402DeferredReadManager::popHead(ListContainer<DeferredRead> &deferredReads) {
a46d2c0e 2403 assert (!deferredReads.empty());
2404
2405 if (!deferredReads.head->element.cancelled)
2406 comm_remove_close_handler(deferredReads.head->element.theRead.fd, CloseHandler, deferredReads.head);
2407
2408 DeferredRead result = deferredReads.pop_front();
2409
2410 return result;
2411}
2412
2413void
a50bfe93 2414DeferredReadManager::kickReads(int const count) {
a46d2c0e 2415 /* if we had List::size() we could consolidate this and flushReads */
2416
33cea91c 2417 if (count < 1) {
a46d2c0e 2418 flushReads();
33cea91c 2419 return;
2420 }
a46d2c0e 2421
2422 size_t remaining = count;
2423
2424 while (!deferredReads.empty() && remaining) {
2425 DeferredRead aRead = popHead(deferredReads);
2426 kickARead(aRead);
2427
2428 if (!aRead.cancelled)
2429 --remaining;
2430 }
2431}
2432
2433void
a50bfe93 2434DeferredReadManager::flushReads() {
a46d2c0e 2435 ListContainer<DeferredRead> reads;
2436 reads = deferredReads;
2437 deferredReads = ListContainer<DeferredRead>();
2438
2439 while (!reads.empty()) {
2440 DeferredRead aRead = popHead(reads);
2441 kickARead(aRead);
2442 }
2443}
2444
2445void
a50bfe93 2446DeferredReadManager::kickARead(DeferredRead const &aRead) {
a46d2c0e 2447 if (aRead.cancelled)
2448 return;
2449
bf8fe701 2450 debugs(5, 3, "Kicking deferred read on FD " << aRead.theRead.fd);
a46d2c0e 2451
2452 aRead.theReader(aRead.theContext, aRead.theRead);
2453}
2454
2455void
a50bfe93 2456DeferredRead::markCancelled() {
a46d2c0e 2457 cancelled = true;
2458}
2d8c0b1a 2459
a50bfe93 2460ConnectionDetail::ConnectionDetail() {
04830959 2461 memset(&me, 0, sizeof(me));
2462 memset(&peer, 0, sizeof(peer));
2d8c0b1a 2463}
8ff3fa2e 2464
2465bool
2466CommDispatcher::dispatch() {
2467 bool result = comm_iocallbackpending();
2468 comm_calliocallback();
2469 /* and again to deal with indirectly queued events
2470 * resulting from the first call. These are usually
2471 * callbacks and should be dealt with immediately.
2472 */
2473 comm_calliocallback();
2b663917 2474
2475 /* Adrian's *new* stuff */
2476 commio_call_callbacks();
8ff3fa2e 2477 return result;
2478}
2479
2480int
2481CommSelectEngine::checkEvents(int timeout) {
fa3f745b 2482 static time_t last_timeout = 0;
2483
2484 /* No, this shouldn't be here. But it shouldn't be in each comm handler. -adrian */
2485 if (squid_curtime > last_timeout) {
2486 last_timeout = squid_curtime;
2487 checkTimeouts();
2488 }
2489
8ff3fa2e 2490 switch (comm_select(timeout)) {
2491
2492 case COMM_OK:
2493
2494 case COMM_TIMEOUT:
2495 return 0;
2496
2497 case COMM_IDLE:
2498
2499 case COMM_SHUTDOWN:
2500 return EVENT_IDLE;
2501
2502 case COMM_ERROR:
2503 return EVENT_ERROR;
2504
2505 default:
2506 fatal_dump("comm.cc: Internal error -- this should never happen.");
2507 return EVENT_ERROR;
2508 };
2509}