]> git.ipfire.org Git - thirdparty/squid.git/blame - src/comm.cc
Make CNCB use const conn
[thirdparty/squid.git] / src / comm.cc
CommitLineData
30a4f2a8 1/*
b510f3a1 2 * DEBUG: section 05 Socket Functions
30a4f2a8 3 * AUTHOR: Harvest Derived
4 *
2b6662ba 5 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 6 * ----------------------------------------------------------
30a4f2a8 7 *
2b6662ba 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.
30a4f2a8 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.
9e008dda 21 *
30a4f2a8 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.
9e008dda 26 *
30a4f2a8 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
cbdec147 29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 30 *
2d8c0b1a 31 *
32 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
30a4f2a8 33 */
090089c4 34
44a47c6e 35#include "squid.h"
cfd66529 36#include "base/AsyncCall.h"
c4b7a5a9 37#include "StoreIOBuffer.h"
38#include "comm.h"
a553a5a3 39#include "event.h"
528b2c61 40#include "fde.h"
04f55905
AJ
41#include "comm/AcceptLimiter.h"
42#include "comm/comm_internal.h"
cfd66529 43#include "comm/Connection.h"
56410c89 44#include "CommIO.h"
a553a5a3 45#include "CommRead.h"
0eb49b6d 46#include "MemBuf.h"
781ce8ff 47#include "pconn.h"
985c86bc 48#include "SquidTime.h"
b0469965 49#include "CommCalls.h"
74257126 50#include "DescriptorSet.h"
9b5c4a9a 51#include "icmp/net_db.h"
96d89ea0
HN
52#include "ip/Address.h"
53#include "ip/Intercept.h"
055421ee 54#include "ip/tools.h"
090089c4 55
b671cc68 56#if defined(_SQUID_CYGWIN_)
57#include <sys/ioctl.h>
58#endif
30a4f2a8 59#ifdef HAVE_NETINET_TCP_H
60#include <netinet/tcp.h>
61#endif
090089c4 62
2b663917 63/*
64 * New C-like simple comm code. This stuff is a mess and doesn't really buy us anything.
65 */
66
67typedef enum {
9e008dda
AJ
68 IOCB_NONE,
69 IOCB_READ,
70 IOCB_WRITE
2b663917 71} iocb_type;
72
82ec8dfc
AR
73static void commStopHalfClosedMonitor(int fd);
74static IOCB commHalfClosedReader;
a67d2b2e
AR
75static void comm_init_opened(int new_socket, Ip::Address &addr, unsigned char TOS, const char *note, struct addrinfo *AI);
76static int comm_apply_flags(int new_socket, Ip::Address &addr, int flags, struct addrinfo *AI);
82ec8dfc
AR
77
78
b0469965 79struct comm_io_callback_t {
9e008dda
AJ
80 iocb_type type;
81 int fd;
82 AsyncCall::Pointer callback;
83 char *buf;
84 FREE *freefunc;
85 int size;
86 int offset;
87 comm_err_t errcode;
88 int xerrno;
89
90 bool active() const { return callback != NULL; }
2b663917 91};
2b663917 92
93struct _comm_fd {
9e008dda
AJ
94 int fd;
95 comm_io_callback_t readcb;
96 comm_io_callback_t writecb;
2b663917 97};
98typedef struct _comm_fd comm_fd_t;
99comm_fd_t *commfd_table;
100
b0469965 101// TODO: make this a comm_io_callback_t method?
2b663917 102bool
103commio_has_callback(int fd, iocb_type type, comm_io_callback_t *ccb)
104{
9e008dda
AJ
105 assert(ccb->fd == fd);
106 assert(ccb->type == type);
107 return ccb->active();
2b663917 108}
109
110/*
b0469965 111 * Configure comm_io_callback_t for I/O
2b663917 112 *
113 * @param fd filedescriptor
114 * @param ccb comm io callback
115 * @param cb callback
116 * @param cbdata callback data (must be cbdata'ed)
117 * @param buf buffer, if applicable
118 * @param freefunc freefunc, if applicable
119 * @param size buffer size
120 */
b0469965 121static void
122commio_set_callback(int fd, iocb_type type, comm_io_callback_t *ccb,
9e008dda 123 AsyncCall::Pointer &cb, char *buf, FREE *freefunc, int size)
2b663917 124{
9e008dda
AJ
125 assert(!ccb->active());
126 assert(ccb->type == type);
127 assert(cb != NULL);
128 ccb->fd = fd;
129 ccb->callback = cb;
130 ccb->buf = buf;
131 ccb->freefunc = freefunc;
132 ccb->size = size;
133 ccb->offset = 0;
2b663917 134}
135
136
b0469965 137// Schedule the callback call and clear the callback
138static void
139commio_finish_callback(int fd, comm_io_callback_t *ccb, comm_err_t code, int xerrno)
2b663917 140{
b0469965 141 debugs(5, 3, "commio_finish_callback: called for FD " << fd << " (" <<
9e008dda
AJ
142 code << ", " << xerrno << ")");
143 assert(ccb->active());
144 assert(ccb->fd == fd);
145 ccb->errcode = code;
146 ccb->xerrno = xerrno;
147
148 comm_io_callback_t cb = *ccb;
149
150 /* We've got a copy; blow away the real one */
151 /* XXX duplicate code from commio_cancel_callback! */
152 ccb->xerrno = 0;
153 ccb->callback = NULL; // cb has it
154
155 /* free data */
156 if (cb.freefunc) {
157 cb.freefunc(cb.buf);
158 cb.buf = NULL;
159 }
160
161 if (cb.callback != NULL) {
b0469965 162 typedef CommIoCbParams Params;
163 Params &params = GetCommParams<Params>(cb.callback);
164 params.fd = cb.fd;
165 params.buf = cb.buf;
166 params.size = cb.offset;
167 params.flag = cb.errcode;
168 params.xerrno = cb.xerrno;
169 ScheduleCallHere(cb.callback);
9e008dda 170 }
2b663917 171}
172
173
174/*
175 * Cancel the given callback
176 *
177 * Remember that the data is cbdataRef'ed.
178 */
b0469965 179// TODO: make this a comm_io_callback_t method
180static void
2b663917 181commio_cancel_callback(int fd, comm_io_callback_t *ccb)
182{
b0469965 183 debugs(5, 3, "commio_cancel_callback: called for FD " << fd);
9e008dda
AJ
184 assert(ccb->fd == fd);
185 assert(ccb->active());
2b663917 186
9e008dda
AJ
187 ccb->xerrno = 0;
188 ccb->callback = NULL;
2b663917 189}
190
191/*
192 * Call the given comm callback; assumes the callback is valid.
9e008dda 193 *
2b663917 194 * @param ccb io completion callback
195 */
196void
197commio_call_callback(comm_io_callback_t *ccb)
198{
2b663917 199}
200
090089c4 201/* STATIC */
62e76326 202
74257126
AR
203static DescriptorSet *TheHalfClosed = NULL; /// the set of half-closed FDs
204static bool WillCheckHalfClosed = false; /// true if check is scheduled
205static EVH commHalfClosedCheck;
206static void commPlanHalfClosedCheck();
207
cc192b50 208static comm_err_t commBind(int s, struct addrinfo &);
f5b8bbc4 209static void commSetReuseAddr(int);
210static void commSetNoLinger(int);
30a4f2a8 211#ifdef TCP_NODELAY
f5b8bbc4 212static void commSetTcpNoDelay(int);
30a4f2a8 213#endif
f5b8bbc4 214static void commSetTcpRcvbuf(int, int);
03a1ee42 215static PF commHandleWrite;
723123a9 216
c4b7a5a9 217typedef enum {
62e76326 218 COMM_CB_READ = 1,
e4ae841b 219 COMM_CB_DERIVED
c4b7a5a9 220} comm_callback_t;
221
b001e822 222static MemAllocator *conn_close_pool = NULL;
c4b7a5a9 223fd_debug_t *fdd_table = NULL;
62e76326 224
04f55905 225bool
b0469965 226isOpen(const int fd)
b300c36d 227{
9e008dda 228 return fd_table[fd].flags.open != 0;
b300c36d 229}
230
e1a88700 231/**
c4b7a5a9 232 * Attempt a read
233 *
234 * If the read attempt succeeds or fails, call the callback.
235 * Else, wait for another IO notification.
236 */
2d8c0b1a 237void
2b663917 238commHandleRead(int fd, void *data)
2d8c0b1a 239{
2b663917 240 comm_io_callback_t *ccb = (comm_io_callback_t *) data;
9e008dda 241
2b663917 242 assert(data == COMMIO_FD_READCB(fd));
243 assert(commio_has_callback(fd, IOCB_READ, ccb));
62e76326 244 /* Attempt a read */
245 statCounter.syscalls.sock.reads++;
246 errno = 0;
2d8c0b1a 247 int retval;
2b663917 248 retval = FD_READ_METHOD(fd, ccb->buf, ccb->size);
bf8fe701 249 debugs(5, 3, "comm_read_try: FD " << fd << ", size " << ccb->size << ", retval " << retval << ", errno " << errno);
62e76326 250
251 if (retval < 0 && !ignoreErrno(errno)) {
bf8fe701 252 debugs(5, 3, "comm_read_try: scheduling COMM_ERROR");
9e008dda
AJ
253 ccb->offset = 0;
254 commio_finish_callback(fd, ccb, COMM_ERROR, errno);
62e76326 255 return;
256 };
257
258 /* See if we read anything */
259 /* Note - read 0 == socket EOF, which is a valid read */
260 if (retval >= 0) {
261 fd_bytes(fd, retval, FD_READ);
9e008dda
AJ
262 ccb->offset = retval;
263 commio_finish_callback(fd, ccb, COMM_OK, errno);
62e76326 264 return;
265 }
c4b7a5a9 266
62e76326 267 /* Nope, register for some more IO */
2b663917 268 commSetSelect(fd, COMM_SELECT_READ, commHandleRead, data, 0);
c4b7a5a9 269}
270
e1a88700 271/**
c4b7a5a9 272 * Queue a read. handler/handler_data are called when the read
273 * completes, on error, or on file descriptor close.
274 */
275void
276comm_read(int fd, char *buf, int size, IOCB *handler, void *handler_data)
b0469965 277{
278 AsyncCall::Pointer call = commCbCall(5,4, "SomeCommReadHandler",
9e008dda 279 CommIoCbPtrFun(handler, handler_data));
b0469965 280 comm_read(fd, buf, size, call);
281}
282
283void
284comm_read(int fd, char *buf, int size, AsyncCall::Pointer &callback)
c4b7a5a9 285{
82ec8dfc 286 debugs(5, 5, "comm_read, queueing read for FD " << fd << "; asynCall " << callback);
c4b7a5a9 287
82ec8dfc
AR
288 /* Make sure we are open and not closing */
289 assert(isOpen(fd));
290 assert(!fd_table[fd].closing());
291 comm_io_callback_t *ccb = COMMIO_FD_READCB(fd);
292
293 // Make sure we are either not reading or just passively monitoring.
294 // Active/passive conflicts are OK and simply cancel passive monitoring.
295 if (ccb->active()) {
296 // if the assertion below fails, we have an active comm_read conflict
74257126 297 assert(fd_table[fd].halfClosedReader != NULL);
82ec8dfc
AR
298 commStopHalfClosedMonitor(fd);
299 assert(!ccb->active());
300 }
528b2c61 301
2b663917 302 /* Queue the read */
82ec8dfc
AR
303 commio_set_callback(fd, IOCB_READ, ccb, callback, (char *)buf, NULL, size);
304 commSetSelect(fd, COMM_SELECT_READ, commHandleRead, ccb, 0);
c4b7a5a9 305}
306
e1a88700 307/**
c4b7a5a9 308 * Empty the read buffers
309 *
310 * This is a magical routine that empties the read buffers.
311 * Under some platforms (Linux) if a buffer has data in it before
312 * you call close(), the socket will hang and take quite a while
313 * to timeout.
314 */
315static void
316comm_empty_os_read_buffers(int fd)
317{
a42d5c25 318#ifdef _SQUID_LINUX_
c4b7a5a9 319 /* prevent those nasty RST packets */
320 char buf[SQUID_TCP_SO_RCVBUF];
62e76326 321
cc192b50 322 if (fd_table[fd].flags.nonblocking == 1) {
323 while (FD_READ_METHOD(fd, buf, SQUID_TCP_SO_RCVBUF) > 0) {};
324 }
c4b7a5a9 325#endif
326}
327
328
e1a88700 329/**
2b663917 330 * Return whether the FD has a pending completed callback.
c4b7a5a9 331 */
332int
333comm_has_pending_read_callback(int fd)
334{
b0469965 335 assert(isOpen(fd));
336 // XXX: We do not know whether there is a read callback scheduled.
337 // This is used for pconn management that should probably be more
338 // tightly integrated into comm to minimize the chance that a
339 // closing pconn socket will be used for a new transaction.
545d554b 340 return false;
c4b7a5a9 341}
342
b0469965 343// Does comm check this fd for read readiness?
344// Note that when comm is not monitoring, there can be a pending callback
345// call, which may resume comm monitoring once fired.
528b2c61 346bool
b0469965 347comm_monitors_read(int fd)
c4b7a5a9 348{
b0469965 349 assert(isOpen(fd));
350 // Being active is usually the same as monitoring because we always
351 // start monitoring the FD when we configure comm_io_callback_t for I/O
352 // and we usually configure comm_io_callback_t for I/O when we starting
353 // monitoring a FD for reading. TODO: replace with commio_has_callback
354 return COMMIO_FD_READCB(fd)->active();
c4b7a5a9 355}
356
e1a88700 357/**
c4b7a5a9 358 * Cancel a pending read. Assert that we have the right parameters,
359 * and that there are no pending read events!
2b663917 360 *
b0469965 361 * XXX: We do not assert that there are no pending read events and
362 * with async calls it becomes even more difficult.
363 * The whole interface should be reworked to do callback->cancel()
364 * instead of searching for places where the callback may be stored and
365 * updating the state of those places.
366 *
2b663917 367 * AHC Don't call the comm handlers?
c4b7a5a9 368 */
369void
370comm_read_cancel(int fd, IOCB *callback, void *data)
371{
b0469965 372 if (!isOpen(fd)) {
373 debugs(5, 4, "comm_read_cancel fails: FD " << fd << " closed");
374 return;
9e008dda 375 }
b0469965 376
377 comm_io_callback_t *cb = COMMIO_FD_READCB(fd);
378 // TODO: is "active" == "monitors FD"?
379 if (!cb->active()) {
380 debugs(5, 4, "comm_read_cancel fails: FD " << fd << " inactive");
381 return;
9e008dda 382 }
b0469965 383
384 typedef CommCbFunPtrCallT<CommIoCbPtrFun> Call;
385 Call *call = dynamic_cast<Call*>(cb->callback.getRaw());
386 if (!call) {
387 debugs(5, 4, "comm_read_cancel fails: FD " << fd << " lacks callback");
388 return;
9e008dda 389 }
b0469965 390
82ec8dfc
AR
391 call->cancel("old comm_read_cancel");
392
b0469965 393 typedef CommIoCbParams Params;
394 const Params &params = GetCommParams<Params>(cb->callback);
c4b7a5a9 395
c4b7a5a9 396 /* Ok, we can be reasonably sure we won't lose any data here! */
b0469965 397 assert(call->dialer.handler == callback);
398 assert(params.data == data);
c4b7a5a9 399
400 /* Delete the callback */
b0469965 401 commio_cancel_callback(fd, cb);
420f2ac8 402
403 /* And the IO event */
62e76326 404 commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
c4b7a5a9 405}
406
c4b7a5a9 407void
b0469965 408comm_read_cancel(int fd, AsyncCall::Pointer &callback)
c4b7a5a9 409{
b0469965 410 callback->cancel("comm_read_cancel");
9e008dda 411
b0469965 412 if (!isOpen(fd)) {
413 debugs(5, 4, "comm_read_cancel fails: FD " << fd << " closed");
414 return;
415 }
416
417 comm_io_callback_t *cb = COMMIO_FD_READCB(fd);
418
419 if (!cb->active()) {
420 debugs(5, 4, "comm_read_cancel fails: FD " << fd << " inactive");
421 return;
422 }
423
424 AsyncCall::Pointer call = cb->callback;
82ec8dfc 425 assert(call != NULL); // XXX: should never fail (active() checks for callback==NULL)
9e008dda 426
b0469965 427 /* Ok, we can be reasonably sure we won't lose any data here! */
428 assert(call == callback);
c4b7a5a9 429
b0469965 430 /* Delete the callback */
431 commio_cancel_callback(fd, cb);
432
433 /* And the IO event */
434 commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
c4b7a5a9 435}
436
437
e1a88700 438/**
ce767c23 439 * synchronous wrapper around udp socket functions
440 */
ce767c23 441int
b7ac5457 442comm_udp_recvfrom(int fd, void *buf, size_t len, int flags, Ip::Address &from)
ce767c23 443{
62e76326 444 statCounter.syscalls.sock.recvfroms++;
cc192b50 445 int x = 0;
446 struct addrinfo *AI = NULL;
447
448 debugs(5,8, "comm_udp_recvfrom: FD " << fd << " from " << from);
449
450 assert( NULL == AI );
451
452 from.InitAddrInfo(AI);
453
454 x = recvfrom(fd, buf, len, flags, AI->ai_addr, &AI->ai_addrlen);
455
456 from = *AI;
457
458 from.FreeAddrInfo(AI);
459
460 return x;
ce767c23 461}
462
365f12a9 463int
7d21986b 464comm_udp_recv(int fd, void *buf, size_t len, int flags)
365f12a9 465{
b7ac5457 466 Ip::Address nul;
cc192b50 467 return comm_udp_recvfrom(fd, buf, len, flags, nul);
365f12a9 468}
469
f71da12c 470ssize_t
7d21986b 471comm_udp_send(int s, const void *buf, size_t len, int flags)
f71da12c 472{
62e76326 473 return send(s, buf, len, flags);
f71da12c 474}
ce767c23 475
476
545d554b 477bool
478comm_has_incomplete_write(int fd)
479{
b0469965 480 assert(isOpen(fd));
481 return COMMIO_FD_WRITECB(fd)->active();
d4cb310b 482}
483
e1a88700 484/**
cf3c0ee3 485 * Queue a write. handler/handler_data are called when the write fully
486 * completes, on error, or on file descriptor close.
487 */
9864ee44 488
090089c4 489/* Return the local port associated with fd. */
b8d8561b 490u_short
491comm_local_port(int fd)
090089c4 492{
b7ac5457 493 Ip::Address temp;
cc192b50 494 struct addrinfo *addr = NULL;
76f87348 495 fde *F = &fd_table[fd];
090089c4 496
090089c4 497 /* If the fd is closed already, just return */
62e76326 498
60c0b5a2 499 if (!F->flags.open) {
bf8fe701 500 debugs(5, 0, "comm_local_port: FD " << fd << " has been closed.");
62e76326 501 return 0;
090089c4 502 }
62e76326 503
cc192b50 504 if (F->local_addr.GetPort())
505 return F->local_addr.GetPort();
62e76326 506
6084c0b6
AJ
507 if (F->sock_family == AF_INET)
508 temp.SetIPv4();
6084c0b6 509
cc192b50 510 temp.InitAddrInfo(addr);
62e76326 511
cc192b50 512 if (getsockname(fd, addr->ai_addr, &(addr->ai_addrlen)) ) {
bf8fe701 513 debugs(50, 1, "comm_local_port: Failed to retrieve TCP/UDP port number for socket: FD " << fd << ": " << xstrerror());
cc192b50 514 temp.FreeAddrInfo(addr);
62e76326 515 return 0;
090089c4 516 }
cc192b50 517 temp = *addr;
518
519 temp.FreeAddrInfo(addr);
520
3d031c35
AJ
521 if (F->local_addr.IsAnyAddr()) {
522 /* save the whole local address, not just the port. */
523 F->local_addr = temp;
524 } else {
525 F->local_addr.SetPort(temp.GetPort());
526 }
62e76326 527
6084c0b6 528 debugs(5, 6, "comm_local_port: FD " << fd << ": port " << F->local_addr.GetPort() << "(family=" << F->sock_family << ")");
cc192b50 529 return F->local_addr.GetPort();
090089c4 530}
531
3d7e9d7c 532static comm_err_t
cc192b50 533commBind(int s, struct addrinfo &inaddr)
090089c4 534{
83704487 535 statCounter.syscalls.sock.binds++;
62e76326 536
ac760b5e
AJ
537 if (bind(s, inaddr.ai_addr, inaddr.ai_addrlen) == 0) {
538 debugs(50, 6, "commBind: bind socket FD " << s << " to " << fd_table[s].local_addr);
62e76326 539 return COMM_OK;
ac760b5e 540 }
62e76326 541
cc192b50 542 debugs(50, 0, "commBind: Cannot bind socket FD " << s << " to " << fd_table[s].local_addr << ": " << xstrerror());
62e76326 543
090089c4 544 return COMM_ERROR;
545}
546
e1a88700 547/**
548 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
549 * is OR of flags specified in comm.h. Defaults TOS
550 */
b8d8561b 551int
16b204c4 552comm_open(int sock_type,
62e76326 553 int proto,
b7ac5457 554 Ip::Address &addr,
62e76326 555 int flags,
556 const char *note)
d6827718 557{
cc192b50 558 return comm_openex(sock_type, proto, addr, flags, 0, note);
d6827718 559}
560
31be869c
AJ
561int
562comm_open_listener(int sock_type,
04f7fd38 563 int proto,
b7ac5457 564 Ip::Address &addr,
04f7fd38
AJ
565 int flags,
566 const char *note)
31be869c
AJ
567{
568 int sock = -1;
569
ac760b5e
AJ
570 /* all listener sockets require bind() */
571 flags |= COMM_DOBIND;
572
31be869c
AJ
573 /* attempt native enabled port. */
574 sock = comm_openex(sock_type, proto, addr, flags, 0, note);
575
31be869c
AJ
576 return sock;
577}
578
2d8c0b1a 579static bool
580limitError(int const anErrno)
581{
582 return anErrno == ENFILE || anErrno == EMFILE;
583}
d6827718 584
057f5854 585int
586comm_set_tos(int fd, int tos)
587{
588#ifdef IP_TOS
9e008dda
AJ
589 int x = setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &tos, sizeof(int));
590 if (x < 0)
591 debugs(50, 1, "comm_set_tos: setsockopt(IP_TOS) on FD " << fd << ": " << xstrerror());
592 return x;
057f5854 593#else
9e008dda
AJ
594 debugs(50, 0, "WARNING: setsockopt(IP_TOS) not supported on this platform");
595 return -1;
057f5854 596#endif
597}
598
cc192b50 599void
600comm_set_v6only(int fd, int tos)
601{
602#ifdef IPV6_V6ONLY
603 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &tos, sizeof(int)) < 0) {
7e07ced1 604 debugs(50, 1, "comm_open: setsockopt(IPV6_V6ONLY) " << (tos?"ON":"OFF") << " for FD " << fd << ": " << xstrerror());
cc192b50 605 }
606#else
607 debugs(50, 0, "WARNING: comm_open: setsockopt(IPV6_V6ONLY) not supported on this platform");
608#endif /* sockopt */
609}
057f5854 610
40d6264d
AJ
611/**
612 * Set the socket IP_TRANSPARENT option for Linux TPROXY v4 support.
613 */
f1e0717c 614void
e950e673 615comm_set_transparent(int fd)
f1e0717c 616{
2ad20b4f 617#if defined(IP_TRANSPARENT)
e950e673 618 int tos = 1;
ef88b51d
AJ
619 if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &tos, sizeof(int)) < 0) {
620 debugs(50, DBG_IMPORTANT, "comm_open: setsockopt(IP_TRANSPARENT) on FD " << fd << ": " << xstrerror());
9e008dda 621 } else {
3949d8b7
AJ
622 /* mark the socket as having transparent options */
623 fd_table[fd].flags.transparent = 1;
624 }
f1e0717c 625#else
ef88b51d 626 debugs(50, DBG_CRITICAL, "WARNING: comm_open: setsockopt(IP_TRANSPARENT) not supported on this platform");
f1e0717c
AJ
627#endif /* sockopt */
628}
629
e1a88700 630/**
631 * Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
632 * is OR of flags specified in defines.h:COMM_*
633 */
d6827718 634int
635comm_openex(int sock_type,
62e76326 636 int proto,
b7ac5457 637 Ip::Address &addr,
62e76326 638 int flags,
639 unsigned char TOS,
640 const char *note)
090089c4 641{
642 int new_socket;
cc192b50 643 int tos = 0;
644 struct addrinfo *AI = NULL;
090089c4 645
88bfe092 646 PROF_start(comm_open);
090089c4 647 /* Create socket for accepting new connections. */
83704487 648 statCounter.syscalls.sock.sockets++;
62e76326 649
cc192b50 650 /* Setup the socket addrinfo details for use */
651 addr.GetAddrInfo(AI);
652 AI->ai_socktype = sock_type;
653 AI->ai_protocol = proto;
cc192b50 654
655 debugs(50, 3, "comm_openex: Attempt open socket for: " << addr );
656
0eb08770 657 new_socket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
055421ee 658
0eb08770
HN
659 /* under IPv6 there is the possibility IPv6 is present but disabled. */
660 /* try again as IPv4-native if possible */
055421ee 661 if ( new_socket < 0 && Ip::EnableIpv6 && addr.IsIPv6() && addr.SetIPv4() ) {
0eb08770
HN
662 /* attempt to open this IPv4-only. */
663 addr.FreeAddrInfo(AI);
664 /* Setup the socket addrinfo details for use */
665 addr.GetAddrInfo(AI);
666 AI->ai_socktype = sock_type;
667 AI->ai_protocol = proto;
668 debugs(50, 3, "comm_openex: Attempt fallback open socket for: " << addr );
ec505200 669 new_socket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
0eb08770
HN
670 debugs(50, 2, HERE << "attempt open " << note << " socket on: " << addr);
671 }
0eb08770
HN
672
673 if (new_socket < 0) {
62e76326 674 /* Increase the number of reserved fd's if calls to socket()
675 * are failing because the open file table is full. This
676 * limits the number of simultaneous clients */
677
2d8c0b1a 678 if (limitError(errno)) {
dd4e714f 679 debugs(50, DBG_IMPORTANT, "comm_open: socket failure: " << xstrerror());
62e76326 680 fdAdjustReserved();
2d8c0b1a 681 } else {
8ca8bd92 682 debugs(50, DBG_CRITICAL, "comm_open: socket failure: " << xstrerror());
62e76326 683 }
684
cc192b50 685 addr.FreeAddrInfo(AI);
686
62e76326 687 PROF_stop(comm_open);
688 return -1;
090089c4 689 }
62e76326 690
cc192b50 691 debugs(50, 3, "comm_openex: Opened socket FD " << new_socket << " : family=" << AI->ai_family << ", type=" << AI->ai_socktype << ", protocol=" << AI->ai_protocol );
692
d6827718 693 /* set TOS if needed */
cc192b50 694 if (TOS && comm_set_tos(new_socket, TOS) ) {
62e76326 695 tos = TOS;
cc192b50 696 }
62e76326 697
055421ee 698 if ( Ip::EnableIpv6&IPV6_SPECIAL_SPLITSTACK && addr.IsIPv6() )
7e07ced1 699 comm_set_v6only(new_socket, 1);
62e76326 700
cc192b50 701 /* Windows Vista supports Dual-Sockets. BUT defaults them to V6ONLY. Turn it OFF. */
9b1f7ee8 702 /* Other OS may have this administratively disabled for general use. Same deal. */
055421ee 703 if ( Ip::EnableIpv6&IPV6_SPECIAL_V4MAPPING && addr.IsIPv6() )
cc192b50 704 comm_set_v6only(new_socket, 0);
705
38344a8e
AR
706 comm_init_opened(new_socket, addr, TOS, note, AI);
707 new_socket = comm_apply_flags(new_socket, addr, flags, AI);
708
709 addr.FreeAddrInfo(AI);
710
711 PROF_stop(comm_open);
712
713 return new_socket;
714}
715
716/// update FD tables after a local or remote (IPC) comm_openex();
717void
718comm_init_opened(int new_socket,
a67d2b2e 719 Ip::Address &addr,
5667a628
AR
720 unsigned char TOS,
721 const char *note,
722 struct addrinfo *AI)
38344a8e
AR
723{
724 assert(new_socket >= 0);
725 assert(AI);
726
727 fde *F = NULL;
728
090089c4 729 /* update fdstat */
bf8fe701 730 debugs(5, 5, "comm_open: FD " << new_socket << " is a new socket");
62e76326 731
b0469965 732 assert(!isOpen(new_socket));
5c5783a2 733 fd_open(new_socket, FD_SOCKET, note);
62e76326 734
c4b7a5a9 735 fdd_table[new_socket].close_file = NULL;
62e76326 736
c4b7a5a9 737 fdd_table[new_socket].close_line = 0;
62e76326 738
76f87348 739 F = &fd_table[new_socket];
62e76326 740
d6827718 741 F->local_addr = addr;
62e76326 742
cc192b50 743 F->tos = TOS;
744
745 F->sock_family = AI->ai_family;
38344a8e
AR
746}
747
748/// apply flags after a local comm_open*() call;
749/// returns new_socket or -1 on error
750static int
751comm_apply_flags(int new_socket,
a67d2b2e 752 Ip::Address &addr,
5667a628
AR
753 int flags,
754 struct addrinfo *AI)
38344a8e
AR
755{
756 assert(new_socket >= 0);
757 assert(AI);
758 const int sock_type = AI->ai_socktype;
62e76326 759
79a15e0a 760 if (!(flags & COMM_NOCLOEXEC))
62e76326 761 commSetCloseOnExec(new_socket);
762
cdc33f35 763 if ((flags & COMM_REUSEADDR))
62e76326 764 commSetReuseAddr(new_socket);
765
9e008dda 766 if (addr.GetPort() > (u_short) 0) {
a50bfe93 767#ifdef _SQUID_MSWIN_
a50bfe93 768 if (sock_type != SOCK_DGRAM)
769#endif
a50bfe93 770 commSetNoLinger(new_socket);
62e76326 771
772 if (opt_reuseaddr)
773 commSetReuseAddr(new_socket);
090089c4 774 }
62e76326 775
a35595cd 776 /* MUST be done before binding or face OS Error: "(99) Cannot assign requested address"... */
9e008dda 777 if ((flags & COMM_TRANSPARENT)) {
a35595cd
AJ
778 comm_set_transparent(new_socket);
779 }
a35595cd 780
ac760b5e
AJ
781 if ( (flags & COMM_DOBIND) || addr.GetPort() > 0 || !addr.IsAnyAddr() ) {
782 if ( !(flags & COMM_DOBIND) && addr.IsAnyAddr() )
783 debugs(5,1,"WARNING: Squid is attempting to bind() port " << addr << " without being a listener.");
784 if ( addr.IsNoAddr() )
785 debugs(5,0,"CRITICAL: Squid is attempting to bind() port " << addr << "!!");
786
cc192b50 787 if (commBind(new_socket, *AI) != COMM_OK) {
62e76326 788 comm_close(new_socket);
789 return -1;
62e76326 790 }
23ff6968 791 }
62e76326 792
79a15e0a 793 if (flags & COMM_NONBLOCKING)
9e008dda 794 if (commSetNonBlocking(new_socket) == COMM_ERROR) {
38344a8e 795 comm_close(new_socket);
62e76326 796 return -1;
62e76326 797 }
798
30a4f2a8 799#ifdef TCP_NODELAY
800 if (sock_type == SOCK_STREAM)
62e76326 801 commSetTcpNoDelay(new_socket);
802
30a4f2a8 803#endif
62e76326 804
1241e63e 805 if (Config.tcpRcvBufsz > 0 && sock_type == SOCK_STREAM)
62e76326 806 commSetTcpRcvbuf(new_socket, Config.tcpRcvBufsz);
807
090089c4 808 return new_socket;
809}
810
a4c0f9c6
AR
811void
812comm_import_opened(int fd,
a67d2b2e 813 Ip::Address &addr,
5667a628
AR
814 int flags,
815 const char *note,
816 struct addrinfo *AI)
a4c0f9c6
AR
817{
818 debugs(5, 2, HERE << " FD " << fd << " at " << addr);
819 assert(fd >= 0);
820 assert(AI);
821
822 comm_init_opened(fd, addr, 0, note, AI);
823
824 if (!(flags & COMM_NOCLOEXEC))
825 fd_table[fd].flags.close_on_exec = 1;
826
827 if (addr.GetPort() > (u_short) 0) {
828#ifdef _SQUID_MSWIN_
829 if (sock_type != SOCK_DGRAM)
830#endif
831 fd_table[fd].flags.nolinger = 1;
832 }
833
834 if ((flags & COMM_TRANSPARENT))
835 fd_table[fd].flags.transparent = 1;
836
837 if (flags & COMM_NONBLOCKING)
838 fd_table[fd].flags.nonblocking = 1;
839
840#ifdef TCP_NODELAY
841 if (AI->ai_socktype == SOCK_STREAM)
842 fd_table[fd].flags.nodelay = 1;
843#endif
844
845 /* no fd_table[fd].flags. updates needed for these conditions:
846 * if ((flags & COMM_REUSEADDR)) ...
847 * if ((flags & COMM_DOBIND) ...) ...
848 */
849}
850
5ae21d99 851#if 0
b8d8561b 852int
b0469965 853commSetTimeout_old(int fd, int timeout, PF * handler, void *data)
090089c4 854{
f3767a6a 855 debugs(5, 3, HERE << "FD " << fd << " timeout " << timeout);
03eb2f01 856 assert(fd >= 0);
857 assert(fd < Squid_MaxFD);
2d8c0b1a 858 fde *F = &fd_table[fd];
60c0b5a2 859 assert(F->flags.open);
62e76326 860
5c5783a2 861 if (timeout < 0) {
62e76326 862 cbdataReferenceDone(F->timeout_data);
863 F->timeout_handler = NULL;
864 F->timeout = 0;
5849612f 865 } else {
62e76326 866 if (handler) {
867 cbdataReferenceDone(F->timeout_data);
868 F->timeout_handler = handler;
869 F->timeout_data = cbdataReference(data);
870 }
871
872 F->timeout = squid_curtime + (time_t) timeout;
30a4f2a8 873 }
62e76326 874
a3fa14bf 875 return F->timeout;
090089c4 876}
5ae21d99 877#endif
b0469965 878
879int
880commSetTimeout(int fd, int timeout, PF * handler, void *data)
881{
882 AsyncCall::Pointer call;
f3767a6a 883 debugs(5, 3, HERE << "FD " << fd << " timeout " << timeout);
9e008dda
AJ
884 if (handler != NULL)
885 call=commCbCall(5,4, "SomeTimeoutHandler", CommTimeoutCbPtrFun(handler, data));
b0469965 886 else
9e008dda 887 call = NULL;
b0469965 888 return commSetTimeout(fd, timeout, call);
889}
890
891
cfd66529
AJ
892int
893commSetTimeout(int fd, int timeout, AsyncCall::Pointer &callback)
b0469965 894{
f3767a6a 895 debugs(5, 3, HERE << "FD " << fd << " timeout " << timeout);
b0469965 896 assert(fd >= 0);
897 assert(fd < Squid_MaxFD);
898 fde *F = &fd_table[fd];
899 assert(F->flags.open);
900
901 if (timeout < 0) {
902 F->timeoutHandler = NULL;
903 F->timeout = 0;
904 } else {
905 if (callback != NULL) {
9e008dda
AJ
906 typedef CommTimeoutCbParams Params;
907 Params &params = GetCommParams<Params>(callback);
908 params.fd = fd;
b0469965 909 F->timeoutHandler = callback;
910 }
911
912 F->timeout = squid_curtime + (time_t) timeout;
913 }
914
915 return F->timeout;
916
917}
090089c4 918
b8d8561b 919int
b7ac5457 920comm_connect_addr(int sock, const Ip::Address &address)
090089c4 921{
3d7e9d7c 922 comm_err_t status = COMM_OK;
76f87348 923 fde *F = &fd_table[sock];
cc192b50 924 int x = 0;
b5568a61 925 int err = 0;
9689d97c 926 socklen_t errlen;
feca3b9a 927 struct addrinfo *AI = NULL;
88bfe092 928 PROF_start(comm_connect_addr);
cc192b50 929
930 assert(address.GetPort() != 0);
931
fa6a39d7 932 debugs(5, 9, HERE << "connecting socket FD " << sock << " to " << address << " (want family: " << F->sock_family << ")");
cc192b50 933
3d98ff81 934 /* Handle IPv6 over IPv4-only socket case.
9d92af86 935 * this case must presently be handled here since the GetAddrInfo asserts on bad mappings.
3d98ff81 936 * NP: because commResetFD is private to ConnStateData we have to return an error and
9d92af86
AJ
937 * trust its handled properly.
938 */
9e008dda 939 if (F->sock_family == AF_INET && !address.IsIPv4()) {
3d98ff81
HN
940 errno = ENETUNREACH;
941 return COMM_ERR_PROTOCOL;
942 }
943
944 /* Handle IPv4 over IPv6-only socket case.
945 * This case is presently handled here as it's both a known case and it's
946 * uncertain what error will be returned by the IPv6 stack in such case. It's
947 * possible this will also be handled by the errno checks below after connect()
948 * but needs carefull cross-platform verification, and verifying the address
949 * condition here is simple.
950 */
951 if (!F->local_addr.IsIPv4() && address.IsIPv4()) {
952 errno = ENETUNREACH;
9d92af86
AJ
953 return COMM_ERR_PROTOCOL;
954 }
9d92af86 955
feca3b9a 956 address.GetAddrInfo(AI, F->sock_family);
cc192b50 957
090089c4 958 /* Establish connection. */
b5568a61 959 errno = 0;
62e76326 960
9e008dda 961 if (!F->flags.called_connect) {
62e76326 962 F->flags.called_connect = 1;
963 statCounter.syscalls.sock.connects++;
964
feca3b9a 965 x = connect(sock, AI->ai_addr, AI->ai_addrlen);
62e76326 966
5a33a66a 967 // XXX: ICAP code refuses callbacks during a pending comm_ call
968 // Async calls development will fix this.
969 if (x == 0) {
970 x = -1;
971 errno = EINPROGRESS;
972 }
973
9e008dda 974 if (x < 0) {
cc192b50 975 debugs(5,5, "comm_connect_addr: sock=" << sock << ", addrinfo( " <<
9e008dda
AJ
976 " flags=" << AI->ai_flags <<
977 ", family=" << AI->ai_family <<
978 ", socktype=" << AI->ai_socktype <<
979 ", protocol=" << AI->ai_protocol <<
980 ", &addr=" << AI->ai_addr <<
981 ", addrlen=" << AI->ai_addrlen <<
982 " )" );
cc192b50 983 debugs(5, 9, "connect FD " << sock << ": (" << x << ") " << xstrerror());
984 debugs(14,9, "connecting to: " << address );
985 }
9e008dda 986 } else {
140e2c0b 987#if defined(_SQUID_NEWSOS6_)
62e76326 988 /* Makoto MATSUSHITA <matusita@ics.es.osaka-u.ac.jp> */
989
feca3b9a 990 connect(sock, AI->ai_addr, AI->ai_addrlen);
62e76326 991
992 if (errno == EINVAL) {
993 errlen = sizeof(err);
994 x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen);
995
996 if (x >= 0)
997 errno = x;
998 }
999
33ac9442 1000#else
62e76326 1001 errlen = sizeof(err);
1002
1003 x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen);
1004
1005 if (x == 0)
1006 errno = err;
1007
b5568a61 1008#if defined(_SQUID_SOLARIS_)
62e76326 1009 /*
1010 * Solaris 2.4's socket emulation doesn't allow you
1011 * to determine the error from a failed non-blocking
1012 * connect and just returns EPIPE. Create a fake
1013 * error message for connect. -- fenner@parc.xerox.com
1014 */
1015 if (x < 0 && errno == EPIPE)
1016 errno = ENOTCONN;
1017
33ac9442 1018#endif
30a4f2a8 1019#endif
62e76326 1020
e5f6c5c2 1021 }
62e76326 1022
9e008dda
AJ
1023 /* Squid seems to be working fine without this code. With this code,
1024 * we leak memory on many connect requests because of EINPROGRESS.
1025 * If you find that this code is needed, please file a bug report. */
82ec8dfc 1026#if 0
feca3b9a
AJ
1027#ifdef _SQUID_LINUX_
1028 /* 2007-11-27:
9e008dda 1029 * Linux Debian replaces our allocated AI pointer with garbage when
feca3b9a
AJ
1030 * connect() fails. This leads to segmentation faults deallocating
1031 * the system-allocated memory when we go to clean up our pointer.
1032 * HACK: is to leak the memory returned since we can't deallocate.
1033 */
9e008dda 1034 if (errno != 0) {
feca3b9a
AJ
1035 AI = NULL;
1036 }
82ec8dfc 1037#endif
feca3b9a
AJ
1038#endif
1039
1040 address.FreeAddrInfo(AI);
1041
88bfe092 1042 PROF_stop(comm_connect_addr);
62e76326 1043
b5568a61 1044 if (errno == 0 || errno == EISCONN)
62e76326 1045 status = COMM_OK;
b5568a61 1046 else if (ignoreErrno(errno))
62e76326 1047 status = COMM_INPROGRESS;
3d98ff81
HN
1048 else if (errno == EAFNOSUPPORT || errno == EINVAL)
1049 return COMM_ERR_PROTOCOL;
b5568a61 1050 else
3d98ff81 1051 return COMM_ERROR;
62e76326 1052
cc192b50 1053 address.NtoA(F->ipaddr, MAX_IPSTRLEN);
62e76326 1054
cc192b50 1055 F->remote_port = address.GetPort(); /* remote_port is HS */
62e76326 1056
9e008dda 1057 if (status == COMM_OK) {
cc192b50 1058 debugs(5, 10, "comm_connect_addr: FD " << sock << " connected to " << address);
9e008dda 1059 } else if (status == COMM_INPROGRESS) {
bf8fe701 1060 debugs(5, 10, "comm_connect_addr: FD " << sock << " connection pending");
090089c4 1061 }
62e76326 1062
090089c4 1063 return status;
1064}
1065
cb201b7e 1066void
1067commCallCloseHandlers(int fd)
1068{
76f87348 1069 fde *F = &fd_table[fd];
bf8fe701 1070 debugs(5, 5, "commCallCloseHandlers: FD " << fd);
62e76326 1071
8000a965 1072 while (F->closeHandler != NULL) {
b0469965 1073 AsyncCall::Pointer call = F->closeHandler;
9e008dda
AJ
1074 F->closeHandler = call->Next();
1075 call->setNext(NULL);
1076 // If call is not canceled schedule it for execution else ignore it
1077 if (!call->canceled()) {
1078 debugs(5, 5, "commCallCloseHandlers: ch->handler=" << call);
1079 typedef CommCloseCbParams Params;
1080 Params &params = GetCommParams<Params>(call);
1081 params.fd = fd;
1082 ScheduleCallHere(call);
1083 }
cb201b7e 1084 }
1085}
1086
5492ad1d 1087#if LINGERING_CLOSE
1088static void
1089commLingerClose(int fd, void *unused)
1090{
1091 LOCAL_ARRAY(char, buf, 1024);
1092 int n;
1f7c9178 1093 n = FD_READ_METHOD(fd, buf, 1024);
62e76326 1094
5492ad1d 1095 if (n < 0)
bf8fe701 1096 debugs(5, 3, "commLingerClose: FD " << fd << " read: " << xstrerror());
62e76326 1097
5492ad1d 1098 comm_close(fd);
1099}
1100
1101static void
1102commLingerTimeout(int fd, void *unused)
1103{
bf8fe701 1104 debugs(5, 3, "commLingerTimeout: FD " << fd);
5492ad1d 1105 comm_close(fd);
1106}
1107
1108/*
1109 * Inspired by apache
1110 */
1111void
1112comm_lingering_close(int fd)
1113{
d4c19b39 1114#if USE_SSL
62e76326 1115
d4c19b39 1116 if (fd_table[fd].ssl)
62e76326 1117 ssl_shutdown_method(fd);
1118
d4c19b39 1119#endif
62e76326 1120
5492ad1d 1121 if (shutdown(fd, 1) < 0) {
62e76326 1122 comm_close(fd);
1123 return;
5492ad1d 1124 }
62e76326 1125
5492ad1d 1126 fd_note(fd, "lingering close");
1127 commSetTimeout(fd, 10, commLingerTimeout, NULL);
1128 commSetSelect(fd, COMM_SELECT_READ, commLingerClose, NULL, 0);
1129}
62e76326 1130
5492ad1d 1131#endif
1132
5c336a3b 1133/**
98264874 1134 * enable linger with time of 0 so that when the socket is
1135 * closed, TCP generates a RESET
1136 */
1137void
5c336a3b 1138comm_reset_close(Comm::ConnectionPointer &conn)
98264874 1139{
5c336a3b
AJ
1140 struct linger L;
1141 L.l_onoff = 1;
1142 L.l_linger = 0;
62e76326 1143
5c336a3b
AJ
1144 if (setsockopt(conn->fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0)
1145 debugs(50, DBG_CRITICAL, "ERROR: Closing FD " << conn->fd << " with TCP RST: " << xstrerror());
1146
1147 conn->close();
1148}
1149
1150// Legacy close function.
1151void
1152old_comm_reset_close(int fd)
1153{
98264874 1154 struct linger L;
1155 L.l_onoff = 1;
1156 L.l_linger = 0;
62e76326 1157
98264874 1158 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0)
468fe1b5 1159 debugs(50, DBG_CRITICAL, "ERROR: Closing FD " << fd << " with TCP RST: " << xstrerror());
62e76326 1160
98264874 1161 comm_close(fd);
1162}
1163
9e008dda 1164void
10b06767
AJ
1165comm_close_start(int fd, void *data)
1166{
10b06767 1167#if USE_SSL
86dedcc1 1168 fde *F = &fd_table[fd];
10b06767
AJ
1169 if (F->ssl)
1170 ssl_shutdown_method(fd);
1171
1172#endif
1173
1174}
1175
9e008dda 1176void
b0469965 1177comm_close_complete(int fd, void *data)
2d8c0b1a 1178{
b0469965 1179#if USE_SSL
1180 fde *F = &fd_table[fd];
2d8c0b1a 1181
b0469965 1182 if (F->ssl) {
1183 SSL_free(F->ssl);
1184 F->ssl = NULL;
1185 }
2d8c0b1a 1186
b0469965 1187#endif
1188 fd_close(fd); /* update fdstat */
1189
1190 close(fd);
1191
b0469965 1192 statCounter.syscalls.sock.closes++;
1193
1194 /* When an fd closes, give accept() a chance, if need be */
04f55905 1195 Comm::AcceptLimiter::Instance().kick();
2d8c0b1a 1196}
c4b7a5a9 1197
1198/*
1199 * Close the socket fd.
1200 *
1201 * + call write handlers with ERR_CLOSING
1202 * + call read handlers with ERR_CLOSING
1203 * + call closing handlers
a46d2c0e 1204 *
9e008dda 1205 * NOTE: COMM_ERR_CLOSING will NOT be called for CommReads' sitting in a
a46d2c0e 1206 * DeferredReadManager.
c4b7a5a9 1207 */
b8d8561b 1208void
43ae1d95 1209_comm_close(int fd, char const *file, int line)
090089c4 1210{
82ec8dfc 1211 debugs(5, 3, "comm_close: start closing FD " << fd);
03eb2f01 1212 assert(fd >= 0);
1213 assert(fd < Squid_MaxFD);
82ec8dfc
AR
1214
1215 fde *F = &fd_table[fd];
c4b7a5a9 1216 fdd_table[fd].close_file = file;
1217 fdd_table[fd].close_line = line;
1f7c9178 1218
82ec8dfc 1219 if (F->closing())
62e76326 1220 return;
1221
36afac26 1222 /* XXX: is this obsolete behind F->closing() ? */
b8869bcf 1223 if ( (shutting_down || reconfiguring) && (!F->flags.open || F->type == FD_FILE))
62e76326 1224 return;
1225
c4b7a5a9 1226 /* The following fails because ipc.c is doing calls to pipe() to create sockets! */
b0469965 1227 assert(isOpen(fd));
62e76326 1228
76f87348 1229 assert(F->type != FD_FILE);
62e76326 1230
88bfe092 1231 PROF_start(comm_close);
62e76326 1232
10b06767 1233 F->flags.close_request = 1;
62e76326 1234
10b06767 1235 AsyncCall::Pointer startCall=commCbCall(5,4, "comm_close_start",
9e008dda 1236 CommCloseCbPtrFun(comm_close_start, NULL));
10b06767
AJ
1237 typedef CommCloseCbParams Params;
1238 Params &startParams = GetCommParams<Params>(startCall);
1239 startParams.fd = fd;
1240 ScheduleCallHere(startCall);
62e76326 1241
74257126
AR
1242 // a half-closed fd may lack a reader, so we stop monitoring explicitly
1243 if (commHasHalfClosedMonitor(fd))
1244 commStopHalfClosedMonitor(fd);
fa80a8ef 1245 commSetTimeout(fd, -1, NULL, NULL);
62e76326 1246
82ec8dfc 1247 // notify read/write handlers
2b663917 1248 if (commio_has_callback(fd, IOCB_WRITE, COMMIO_FD_WRITECB(fd))) {
b0469965 1249 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), COMM_ERR_CLOSING, errno);
2b663917 1250 }
1251 if (commio_has_callback(fd, IOCB_READ, COMMIO_FD_READCB(fd))) {
b0469965 1252 commio_finish_callback(fd, COMMIO_FD_READCB(fd), COMM_ERR_CLOSING, errno);
2b663917 1253 }
2d8c0b1a 1254
cb201b7e 1255 commCallCloseHandlers(fd);
62e76326 1256
781ce8ff 1257 if (F->pconn.uses)
1258 F->pconn.pool->count(F->pconn.uses);
62e76326 1259
a7ad6e4e 1260 comm_empty_os_read_buffers(fd);
9e008dda 1261
62e76326 1262
10b06767 1263 AsyncCall::Pointer completeCall=commCbCall(5,4, "comm_close_complete",
9e008dda 1264 CommCloseCbPtrFun(comm_close_complete, NULL));
10b06767
AJ
1265 Params &completeParams = GetCommParams<Params>(completeCall);
1266 completeParams.fd = fd;
9e008dda 1267 // must use async call to wait for all callbacks
82ec8dfc 1268 // scheduled before comm_close() to finish
10b06767 1269 ScheduleCallHere(completeCall);
62e76326 1270
88bfe092 1271 PROF_stop(comm_close);
090089c4 1272}
1273
090089c4 1274/* Send a udp datagram to specified TO_ADDR. */
b8d8561b 1275int
5df61230 1276comm_udp_sendto(int fd,
b7ac5457 1277 const Ip::Address &to_addr,
62e76326 1278 const void *buf,
1279 int len)
090089c4 1280{
cc192b50 1281 int x = 0;
1282 struct addrinfo *AI = NULL;
1283
88bfe092 1284 PROF_start(comm_udp_sendto);
83704487 1285 statCounter.syscalls.sock.sendtos++;
62e76326 1286
cc192b50 1287 debugs(50, 3, "comm_udp_sendto: Attempt to send UDP packet to " << to_addr <<
9e008dda 1288 " using FD " << fd << " using Port " << comm_local_port(fd) );
cc192b50 1289
1290 /* BUG: something in the above macro appears to occasionally be setting AI to garbage. */
1291 /* AYJ: 2007-08-27 : or was it because I wasn't then setting 'fd_table[fd].sock_family' to fill properly. */
1292 assert( NULL == AI );
1293
1294 to_addr.GetAddrInfo(AI, fd_table[fd].sock_family);
1295
1296 x = sendto(fd, buf, len, 0, AI->ai_addr, AI->ai_addrlen);
1297
1298 to_addr.FreeAddrInfo(AI);
1299
88bfe092 1300 PROF_stop(comm_udp_sendto);
62e76326 1301
2d8c0b1a 1302 if (x >= 0)
1303 return x;
1304
17d51783 1305#ifdef _SQUID_LINUX_
62e76326 1306
2d8c0b1a 1307 if (ECONNREFUSED != errno)
17d51783 1308#endif
62e76326 1309
cc192b50 1310 debugs(50, 1, "comm_udp_sendto: FD " << fd << ", (family=" << fd_table[fd].sock_family << ") " << to_addr << ": " << xstrerror());
62e76326 1311
2d8c0b1a 1312 return COMM_ERROR;
090089c4 1313}
1314
b8d8561b 1315void
582b6456 1316comm_add_close_handler(int fd, PF * handler, void *data)
30a4f2a8 1317{
bf8fe701 1318 debugs(5, 5, "comm_add_close_handler: FD " << fd << ", handler=" <<
1319 handler << ", data=" << data);
62e76326 1320
b0469965 1321 AsyncCall::Pointer call=commCbCall(5,4, "SomeCloseHandler",
9e008dda 1322 CommCloseCbPtrFun(handler, data));
b0469965 1323 comm_add_close_handler(fd, call);
1324}
62e76326 1325
b0469965 1326void
1327comm_add_close_handler(int fd, AsyncCall::Pointer &call)
1328{
1329 debugs(5, 5, "comm_add_close_handler: FD " << fd << ", AsyncCall=" << call);
62e76326 1330
b0469965 1331 /*TODO:Check for a similar scheduled AsyncCall*/
1332// for (c = fd_table[fd].closeHandler; c; c = c->next)
1333// assert(c->handler != handler || c->data != data);
62e76326 1334
b0469965 1335 call->setNext(fd_table[fd].closeHandler);
62e76326 1336
b0469965 1337 fd_table[fd].closeHandler = call;
30a4f2a8 1338}
1339
b0469965 1340
1341// remove function-based close handler
b8d8561b 1342void
582b6456 1343comm_remove_close_handler(int fd, PF * handler, void *data)
090089c4 1344{
b0469965 1345 assert (isOpen(fd));
30a4f2a8 1346 /* Find handler in list */
bf8fe701 1347 debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", handler=" <<
1348 handler << ", data=" << data);
62e76326 1349
b0469965 1350 AsyncCall::Pointer p;
9e008dda 1351 for (p = fd_table[fd].closeHandler; p != NULL; p = p->Next()) {
b0469965 1352 typedef CommCbFunPtrCallT<CommCloseCbPtrFun> Call;
1353 const Call *call = dynamic_cast<const Call*>(p.getRaw());
1354 if (!call) // method callbacks have their own comm_remove_close_handler
1355 continue;
62e76326 1356
b0469965 1357 typedef CommCloseCbParams Params;
1358 const Params &params = GetCommParams<Params>(p);
1359 if (call->dialer.handler == handler && params.data == data)
1360 break; /* This is our handler */
1361 }
7828df5b
CT
1362
1363 // comm_close removes all close handlers so our handler may be gone
1364 if (p != NULL)
1365 p->cancel("comm_remove_close_handler");
1366 // TODO: should we remove the handler from the close handlers list?
b0469965 1367}
62e76326 1368
b0469965 1369// remove method-based close handler
1370void
1371comm_remove_close_handler(int fd, AsyncCall::Pointer &call)
1372{
1373 assert (isOpen(fd));
b0469965 1374 debugs(5, 5, "comm_remove_close_handler: FD " << fd << ", AsyncCall=" << call);
62e76326 1375
7828df5b
CT
1376 // comm_close removes all close handlers so our handler may be gone
1377 // TODO: should we remove the handler from the close handlers list?
1378#if 0
b0469965 1379 // Check to see if really exist the given AsyncCall in comm_close handlers
1380 // TODO: optimize: this slow code is only needed for the assert() below
1381 AsyncCall::Pointer p;
1382 for (p = fd_table[fd].closeHandler; p != NULL && p != call; p = p->Next());
1383 assert(p == call);
7828df5b 1384#endif
62e76326 1385
b0469965 1386 call->cancel("comm_remove_close_handler");
30a4f2a8 1387}
090089c4 1388
b8d8561b 1389static void
1390commSetNoLinger(int fd)
30a4f2a8 1391{
62e76326 1392
30a4f2a8 1393 struct linger L;
090089c4 1394 L.l_onoff = 0; /* off */
1395 L.l_linger = 0;
62e76326 1396
30a4f2a8 1397 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0)
bf8fe701 1398 debugs(50, 0, "commSetNoLinger: FD " << fd << ": " << xstrerror());
62e76326 1399
58a6c186 1400 fd_table[fd].flags.nolinger = 1;
090089c4 1401}
1402
b8d8561b 1403static void
1404commSetReuseAddr(int fd)
090089c4 1405{
1406 int on = 1;
62e76326 1407
30a4f2a8 1408 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0)
bf8fe701 1409 debugs(50, 1, "commSetReuseAddr: FD " << fd << ": " << xstrerror());
090089c4 1410}
1411
b8d8561b 1412static void
1413commSetTcpRcvbuf(int fd, int size)
f868539a 1414{
1415 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size)) < 0)
bf8fe701 1416 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd << ", SIZE " << size << ": " << xstrerror());
8f0d53ef 1417 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size)) < 0)
1418 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd << ", SIZE " << size << ": " << xstrerror());
1419#ifdef TCP_WINDOW_CLAMP
1420 if (setsockopt(fd, SOL_TCP, TCP_WINDOW_CLAMP, (char *) &size, sizeof(size)) < 0)
1421 debugs(50, 1, "commSetTcpRcvbuf: FD " << fd << ", SIZE " << size << ": " << xstrerror());
1422#endif
f868539a 1423}
1424
b8d8561b 1425int
1426commSetNonBlocking(int fd)
30a4f2a8 1427{
a50bfe93 1428#ifndef _SQUID_MSWIN_
731e4d49 1429 int flags;
9e205701 1430 int dummy = 0;
a50bfe93 1431#endif
ec4daaa5 1432#ifdef _SQUID_WIN32_
62e76326 1433
b05490a8 1434 int nonblocking = TRUE;
62e76326 1435
629b5f75 1436#ifdef _SQUID_CYGWIN_
1437
7f6ffd15 1438 if (fd_table[fd].type != FD_PIPE) {
629b5f75 1439#endif
1440
62e76326 1441 if (ioctl(fd, FIONBIO, &nonblocking) < 0) {
bf8fe701 1442 debugs(50, 0, "commSetNonBlocking: FD " << fd << ": " << xstrerror() << " " << fd_table[fd].type);
62e76326 1443 return COMM_ERROR;
1444 }
629b5f75 1445
1446#ifdef _SQUID_CYGWIN_
1447
7f6ffd15 1448 } else {
1449#endif
629b5f75 1450#endif
a50bfe93 1451#ifndef _SQUID_MSWIN_
62e76326 1452
1453 if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
bf8fe701 1454 debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror());
62e76326 1455 return COMM_ERROR;
1456 }
1457
1458 if (fcntl(fd, F_SETFL, flags | SQUID_NONBLOCK) < 0) {
bf8fe701 1459 debugs(50, 0, "commSetNonBlocking: FD " << fd << ": " << xstrerror());
62e76326 1460 return COMM_ERROR;
1461 }
1462
a50bfe93 1463#endif
629b5f75 1464#ifdef _SQUID_CYGWIN_
62e76326 1465
090089c4 1466 }
62e76326 1467
7f6ffd15 1468#endif
58a6c186 1469 fd_table[fd].flags.nonblocking = 1;
62e76326 1470
090089c4 1471 return 0;
1472}
1473
7e3ce7b9 1474int
1475commUnsetNonBlocking(int fd)
1476{
a50bfe93 1477#ifdef _SQUID_MSWIN_
1478 int nonblocking = FALSE;
1479
1480 if (ioctlsocket(fd, FIONBIO, (unsigned long *) &nonblocking) < 0) {
1481#else
7e3ce7b9 1482 int flags;
1483 int dummy = 0;
62e76326 1484
7e3ce7b9 1485 if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
bf8fe701 1486 debugs(50, 0, "FD " << fd << ": fcntl F_GETFL: " << xstrerror());
62e76326 1487 return COMM_ERROR;
7e3ce7b9 1488 }
62e76326 1489
7e3ce7b9 1490 if (fcntl(fd, F_SETFL, flags & (~SQUID_NONBLOCK)) < 0) {
a50bfe93 1491#endif
bf8fe701 1492 debugs(50, 0, "commUnsetNonBlocking: FD " << fd << ": " << xstrerror());
62e76326 1493 return COMM_ERROR;
7e3ce7b9 1494 }
62e76326 1495
7e3ce7b9 1496 fd_table[fd].flags.nonblocking = 0;
1497 return 0;
1498}
1499
b8d8561b 1500void
e1381638
AJ
1501commSetCloseOnExec(int fd)
1502{
3ca60c86 1503#ifdef FD_CLOEXEC
731e4d49 1504 int flags;
7a18b487 1505 int dummy = 0;
62e76326 1506
2209fe19
AJ
1507 if ((flags = fcntl(fd, F_GETFD, dummy)) < 0) {
1508 debugs(50, 0, "FD " << fd << ": fcntl F_GETFD: " << xstrerror());
62e76326 1509 return;
3ca60c86 1510 }
62e76326 1511
24382924 1512 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
bf8fe701 1513 debugs(50, 0, "FD " << fd << ": set close-on-exec failed: " << xstrerror());
62e76326 1514
d6827718 1515 fd_table[fd].flags.close_on_exec = 1;
62e76326 1516
3ca60c86 1517#endif
1518}
1519
e90100aa 1520#ifdef TCP_NODELAY
1521static void
e1381638
AJ
1522commSetTcpNoDelay(int fd)
1523{
e90100aa 1524 int on = 1;
62e76326 1525
e90100aa 1526 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) < 0)
bf8fe701 1527 debugs(50, 1, "commSetTcpNoDelay: FD " << fd << ": " << xstrerror());
62e76326 1528
d6827718 1529 fd_table[fd].flags.nodelay = 1;
e90100aa 1530}
62e76326 1531
e90100aa 1532#endif
1533
b2130d58 1534void
e1381638
AJ
1535commSetTcpKeepalive(int fd, int idle, int interval, int timeout)
1536{
b2130d58 1537 int on = 1;
1538#ifdef TCP_KEEPCNT
1539 if (timeout && interval) {
9e008dda
AJ
1540 int count = (timeout + interval - 1) / interval;
1541 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &count, sizeof(on)) < 0)
1542 debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror());
b2130d58 1543 }
1544#endif
1545#ifdef TCP_KEEPIDLE
1546 if (idle) {
9e008dda
AJ
1547 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(on)) < 0)
1548 debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror());
b2130d58 1549 }
1550#endif
1551#ifdef TCP_KEEPINTVL
1552 if (interval) {
9e008dda
AJ
1553 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(on)) < 0)
1554 debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror());
b2130d58 1555 }
1556#endif
1557 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &on, sizeof(on)) < 0)
9e008dda 1558 debugs(5, 1, "commSetKeepalive: FD " << fd << ": " << xstrerror());
b2130d58 1559}
6a988308 1560
d86b3703 1561void
e1381638
AJ
1562comm_init(void)
1563{
c4b7a5a9 1564 fd_table =(fde *) xcalloc(Squid_MaxFD, sizeof(fde));
1565 fdd_table = (fd_debug_t *)xcalloc(Squid_MaxFD, sizeof(fd_debug_t));
2d8c0b1a 1566
04f55905
AJ
1567 /* make sure the accept() socket FIFO delay queue exists */
1568 Comm::AcceptLimiter::Instance();
b0469965 1569
1570 commfd_table = (comm_fd_t *) xcalloc(Squid_MaxFD, sizeof(comm_fd_t));
2b663917 1571 for (int pos = 0; pos < Squid_MaxFD; pos++) {
9e008dda
AJ
1572 commfd_table[pos].fd = pos;
1573 commfd_table[pos].readcb.fd = pos;
1574 commfd_table[pos].readcb.type = IOCB_READ;
1575 commfd_table[pos].writecb.fd = pos;
1576 commfd_table[pos].writecb.type = IOCB_WRITE;
2b663917 1577 }
2d8c0b1a 1578
59c4d35b 1579 /* XXX account fd_table */
090089c4 1580 /* Keep a few file descriptors free so that we don't run out of FD's
1581 * after accepting a client but before it opens a socket or a file.
e83892e9 1582 * Since Squid_MaxFD can be as high as several thousand, don't waste them */
d85c3078 1583 RESERVED_FD = min(100, Squid_MaxFD / 4);
2d8c0b1a 1584
04eb0689 1585 conn_close_pool = memPoolCreate("close_handler", sizeof(close_handler));
74257126
AR
1586
1587 TheHalfClosed = new DescriptorSet;
090089c4 1588}
1589
236d1779 1590void
e1381638
AJ
1591comm_exit(void)
1592{
74257126
AR
1593 delete TheHalfClosed;
1594 TheHalfClosed = NULL;
1595
236d1779 1596 safe_free(fd_table);
1597 safe_free(fdd_table);
236d1779 1598 safe_free(commfd_table);
1599}
1600
30a4f2a8 1601/* Write to FD. */
b8d8561b 1602static void
e1381638
AJ
1603commHandleWrite(int fd, void *data)
1604{
2b663917 1605 comm_io_callback_t *state = (comm_io_callback_t *)data;
30a4f2a8 1606 int len = 0;
1607 int nleft;
1608
2b663917 1609 assert(state == COMMIO_FD_WRITECB(fd));
1610
88bfe092 1611 PROF_start(commHandleWrite);
bf8fe701 1612 debugs(5, 5, "commHandleWrite: FD " << fd << ": off " <<
1613 (long int) state->offset << ", sz " << (long int) state->size << ".");
30a4f2a8 1614
1615 nleft = state->size - state->offset;
1f7c9178 1616 len = FD_WRITE_METHOD(fd, state->buf + state->offset, nleft);
bf8fe701 1617 debugs(5, 5, "commHandleWrite: write() returns " << len);
b69f7771 1618 fd_bytes(fd, len, FD_WRITE);
83704487 1619 statCounter.syscalls.sock.writes++;
307b696e
A
1620 // After each successful partial write,
1621 // reset fde::writeStart to the current time.
5ef5e5cc 1622 fd_table[fd].writeStart = squid_curtime;
30a4f2a8 1623
1624 if (len == 0) {
62e76326 1625 /* Note we even call write if nleft == 0 */
1626 /* We're done */
1627
1628 if (nleft != 0)
bf8fe701 1629 debugs(5, 1, "commHandleWrite: FD " << fd << ": write failure: connection closed with " << nleft << " bytes remaining.");
62e76326 1630
b0469965 1631 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_ERROR : COMM_OK, errno);
30a4f2a8 1632 } else if (len < 0) {
62e76326 1633 /* An error */
1634
1635 if (fd_table[fd].flags.socket_eof) {
bf8fe701 1636 debugs(50, 2, "commHandleWrite: FD " << fd << ": write failure: " << xstrerror() << ".");
b0469965 1637 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_ERROR : COMM_OK, errno);
62e76326 1638 } else if (ignoreErrno(errno)) {
bf8fe701 1639 debugs(50, 10, "commHandleWrite: FD " << fd << ": write failure: " << xstrerror() << ".");
62e76326 1640 commSetSelect(fd,
1641 COMM_SELECT_WRITE,
1642 commHandleWrite,
1643 state,
1644 0);
1645 } else {
bf8fe701 1646 debugs(50, 2, "commHandleWrite: FD " << fd << ": write failure: " << xstrerror() << ".");
b0469965 1647 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_ERROR : COMM_OK, errno);
62e76326 1648 }
30a4f2a8 1649 } else {
62e76326 1650 /* A successful write, continue */
1651 state->offset += len;
1652
57d55dfa 1653 if (state->offset < state->size) {
62e76326 1654 /* Not done, reinstall the write handler and write some more */
1655 commSetSelect(fd,
1656 COMM_SELECT_WRITE,
1657 commHandleWrite,
1658 state,
1659 0);
1660 } else {
b0469965 1661 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), nleft ? COMM_OK : COMM_ERROR, errno);
62e76326 1662 }
30a4f2a8 1663 }
62e76326 1664
88bfe092 1665 PROF_stop(commHandleWrite);
30a4f2a8 1666}
1667
7cd8c414 1668/*
1669 * Queue a write. handler/handler_data are called when the write
1670 * completes, on error, or on file descriptor close.
1671 *
1672 * free_func is used to free the passed buffer when the write has completed.
1673 */
b8d8561b 1674void
e1381638
AJ
1675comm_write(int fd, const char *buf, int size, IOCB * handler, void *handler_data, FREE * free_func)
1676{
b0469965 1677 AsyncCall::Pointer call = commCbCall(5,5, "SomeCommWriteHander",
9e008dda 1678 CommIoCbPtrFun(handler, handler_data));
b0469965 1679
1680 comm_write(fd, buf, size, call, free_func);
1681}
1682
1683void
e1381638
AJ
1684comm_write(int fd, const char *buf, int size, AsyncCall::Pointer &callback, FREE * free_func)
1685{
82ec8dfc 1686 debugs(5, 5, "comm_write: FD " << fd << ": sz " << size << ": asynCall " << callback);
62e76326 1687
82ec8dfc
AR
1688 /* Make sure we are open, not closing, and not writing */
1689 assert(isOpen(fd));
1690 assert(!fd_table[fd].closing());
1691 comm_io_callback_t *ccb = COMMIO_FD_WRITECB(fd);
1692 assert(!ccb->active());
b0469965 1693
5ef5e5cc 1694 fd_table[fd].writeStart = squid_curtime;
82ec8dfc
AR
1695 /* Queue the write */
1696 commio_set_callback(fd, IOCB_WRITE, ccb, callback,
9e008dda 1697 (char *)buf, free_func, size);
82ec8dfc 1698 commSetSelect(fd, COMM_SELECT_WRITE, commHandleWrite, ccb, 0);
30a4f2a8 1699}
26a880e2 1700
b0469965 1701
137ee196 1702/* a wrapper around comm_write to allow for MemBuf to be comm_written in a snap */
cb69b4c7 1703void
e1381638
AJ
1704comm_write_mbuf(int fd, MemBuf *mb, IOCB * handler, void *handler_data)
1705{
2b663917 1706 comm_write(fd, mb->buf, mb->size, handler, handler_data, mb->freeFunc());
cb69b4c7 1707}
1708
b0469965 1709void
e1381638
AJ
1710comm_write_mbuf(int fd, MemBuf *mb, AsyncCall::Pointer &callback)
1711{
b0469965 1712 comm_write(fd, mb->buf, mb->size, callback, mb->freeFunc());
1713}
1714
c4b7a5a9 1715
89924214 1716/*
1717 * hm, this might be too general-purpose for all the places we'd
1718 * like to use it.
1719 */
b224ea98 1720int
e1381638
AJ
1721ignoreErrno(int ierrno)
1722{
603500e7 1723 switch (ierrno) {
62e76326 1724
89924214 1725 case EINPROGRESS:
62e76326 1726
603500e7 1727 case EWOULDBLOCK:
26a880e2 1728#if EAGAIN != EWOULDBLOCK
62e76326 1729
603500e7 1730 case EAGAIN:
26a880e2 1731#endif
62e76326 1732
603500e7 1733 case EALREADY:
62e76326 1734
603500e7 1735 case EINTR:
db494ab8 1736#ifdef ERESTART
62e76326 1737
db494ab8 1738 case ERESTART:
1739#endif
62e76326 1740
1741 return 1;
1742
603500e7 1743 default:
62e76326 1744 return 0;
603500e7 1745 }
62e76326 1746
603500e7 1747 /* NOTREACHED */
26a880e2 1748}
d723bf6b 1749
1750void
e1381638
AJ
1751commCloseAllSockets(void)
1752{
d723bf6b 1753 int fd;
1754 fde *F = NULL;
62e76326 1755
d723bf6b 1756 for (fd = 0; fd <= Biggest_FD; fd++) {
62e76326 1757 F = &fd_table[fd];
1758
1759 if (!F->flags.open)
1760 continue;
1761
1762 if (F->type != FD_SOCKET)
1763 continue;
1764
1765 if (F->flags.ipc) /* don't close inter-process sockets */
1766 continue;
1767
b0469965 1768 if (F->timeoutHandler != NULL) {
1769 AsyncCall::Pointer callback = F->timeoutHandler;
1770 F->timeoutHandler = NULL;
bf8fe701 1771 debugs(5, 5, "commCloseAllSockets: FD " << fd << ": Calling timeout handler");
9e008dda 1772 ScheduleCallHere(callback);
62e76326 1773 } else {
468fe1b5 1774 debugs(5, 5, "commCloseAllSockets: FD " << fd << ": calling comm_reset_close()");
5c336a3b 1775 old_comm_reset_close(fd);
62e76326 1776 }
d723bf6b 1777 }
1778}
1b3db6d9 1779
2d8c0b1a 1780static bool
e1381638
AJ
1781AlreadyTimedOut(fde *F)
1782{
2d8c0b1a 1783 if (!F->flags.open)
1784 return true;
1785
1786 if (F->timeout == 0)
1787 return true;
1788
1789 if (F->timeout > squid_curtime)
1790 return true;
1791
1792 return false;
1793}
1794
5ef5e5cc
AJ
1795static bool
1796writeTimedOut(int fd)
1797{
1798 if (!commio_has_callback(fd, IOCB_WRITE, COMMIO_FD_WRITECB(fd)))
1799 return false;
1800
1801 if ((squid_curtime - fd_table[fd].writeStart) < Config.Timeout.write)
1802 return false;
1803
1804 return true;
1805}
1806
1b3db6d9 1807void
e1381638
AJ
1808checkTimeouts(void)
1809{
1b3db6d9 1810 int fd;
1811 fde *F = NULL;
b0469965 1812 AsyncCall::Pointer callback;
62e76326 1813
1b3db6d9 1814 for (fd = 0; fd <= Biggest_FD; fd++) {
62e76326 1815 F = &fd_table[fd];
1816
5ef5e5cc
AJ
1817 if (writeTimedOut(fd)) {
1818 // We have an active write callback and we are timed out
52f6ea9e
AR
1819 debugs(5, 5, "checkTimeouts: FD " << fd << " auto write timeout");
1820 commSetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0);
5ef5e5cc 1821 commio_finish_callback(fd, COMMIO_FD_WRITECB(fd), COMM_ERROR, ETIMEDOUT);
307b696e 1822 } else if (AlreadyTimedOut(F))
62e76326 1823 continue;
1824
9e008dda 1825 debugs(5, 5, "checkTimeouts: FD " << fd << " Expired");
62e76326 1826
b0469965 1827 if (F->timeoutHandler != NULL) {
bf8fe701 1828 debugs(5, 5, "checkTimeouts: FD " << fd << ": Call timeout handler");
b0469965 1829 callback = F->timeoutHandler;
1830 F->timeoutHandler = NULL;
9e008dda 1831 ScheduleCallHere(callback);
62e76326 1832 } else {
bf8fe701 1833 debugs(5, 5, "checkTimeouts: FD " << fd << ": Forcing comm_close()");
62e76326 1834 comm_close(fd);
1835 }
b5443c04 1836 }
1837}
1838
e1381638
AJ
1839void CommIO::Initialise()
1840{
6cce2334 1841 /* Initialize done pipe signal */
1842 int DonePipe[2];
9e008dda 1843 if (pipe(DonePipe)) {}
6cce2334 1844 DoneFD = DonePipe[1];
1845 DoneReadFD = DonePipe[0];
d06925a4 1846 fd_open(DoneReadFD, FD_PIPE, "async-io completetion event: main");
1847 fd_open(DoneFD, FD_PIPE, "async-io completetion event: threads");
1848 commSetNonBlocking(DoneReadFD);
1849 commSetNonBlocking(DoneFD);
1850 commSetSelect(DoneReadFD, COMM_SELECT_READ, NULLFDHandler, NULL, 0);
6cce2334 1851 Initialised = true;
1852}
1853
e1381638
AJ
1854void CommIO::NotifyIOClose()
1855{
d06925a4 1856 /* Close done pipe signal */
1857 FlushPipe();
1858 close(DoneFD);
1859 close(DoneReadFD);
1860 fd_close(DoneFD);
1861 fd_close(DoneReadFD);
1862 Initialised = false;
1863}
1864
6cce2334 1865bool CommIO::Initialised = false;
1866bool CommIO::DoneSignalled = false;
1867int CommIO::DoneFD = -1;
1868int CommIO::DoneReadFD = -1;
1869
1870void
e1381638
AJ
1871CommIO::FlushPipe()
1872{
6cce2334 1873 char buf[256];
56410c89 1874 FD_READ_METHOD(DoneReadFD, buf, sizeof(buf));
6cce2334 1875}
1876
1877void
e1381638
AJ
1878CommIO::NULLFDHandler(int fd, void *data)
1879{
6cce2334 1880 FlushPipe();
1881 commSetSelect(fd, COMM_SELECT_READ, NULLFDHandler, NULL, 0);
1882}
1883
1884void
e1381638
AJ
1885CommIO::ResetNotifications()
1886{
6cce2334 1887 if (DoneSignalled) {
62e76326 1888 FlushPipe();
1889 DoneSignalled = false;
6cce2334 1890 }
1891}
a46d2c0e 1892
9e008dda
AJ
1893/// Start waiting for a possibly half-closed connection to close
1894// by scheduling a read callback to a monitoring handler that
82ec8dfc 1895// will close the connection on read errors.
a46d2c0e 1896void
e1381638
AJ
1897commStartHalfClosedMonitor(int fd)
1898{
74257126 1899 debugs(5, 5, HERE << "adding FD " << fd << " to " << *TheHalfClosed);
82ec8dfc
AR
1900 assert(isOpen(fd));
1901 assert(!commHasHalfClosedMonitor(fd));
74257126
AR
1902 (void)TheHalfClosed->add(fd); // could also assert the result
1903 commPlanHalfClosedCheck(); // may schedule check if we added the first FD
1904}
1905
1906static
1907void
e1381638
AJ
1908commPlanHalfClosedCheck()
1909{
74257126
AR
1910 if (!WillCheckHalfClosed && !TheHalfClosed->empty()) {
1911 eventAdd("commHalfClosedCheck", &commHalfClosedCheck, NULL, 1.0, 1);
1912 WillCheckHalfClosed = true;
1913 }
1914}
1915
1916/// iterates over all descriptors that may need half-closed tests and
1917/// calls comm_read for those that do; re-schedules the check if needed
1918static
1919void
e1381638
AJ
1920commHalfClosedCheck(void *)
1921{
74257126
AR
1922 debugs(5, 5, HERE << "checking " << *TheHalfClosed);
1923
1924 typedef DescriptorSet::const_iterator DSCI;
1925 const DSCI end = TheHalfClosed->end();
1926 for (DSCI i = TheHalfClosed->begin(); i != end; ++i) {
1927 const int fd = *i;
1928 if (!fd_table[fd].halfClosedReader) { // not reading already
1929 AsyncCall::Pointer call = commCbCall(5,4, "commHalfClosedReader",
9e008dda 1930 CommIoCbPtrFun(&commHalfClosedReader, NULL));
74257126
AR
1931 comm_read(fd, NULL, 0, call);
1932 fd_table[fd].halfClosedReader = call;
1933 }
1934 }
f900210a 1935
74257126
AR
1936 WillCheckHalfClosed = false; // as far as we know
1937 commPlanHalfClosedCheck(); // may need to check again
f900210a 1938}
1939
82ec8dfc
AR
1940/// checks whether we are waiting for possibly half-closed connection to close
1941// We are monitoring if the read handler for the fd is the monitoring handler.
1942bool
e1381638
AJ
1943commHasHalfClosedMonitor(int fd)
1944{
74257126 1945 return TheHalfClosed->has(fd);
a46d2c0e 1946}
1947
82ec8dfc
AR
1948/// stop waiting for possibly half-closed connection to close
1949static void
e1381638
AJ
1950commStopHalfClosedMonitor(int const fd)
1951{
74257126
AR
1952 debugs(5, 5, HERE << "removing FD " << fd << " from " << *TheHalfClosed);
1953
1954 // cancel the read if one was scheduled
1955 AsyncCall::Pointer reader = fd_table[fd].halfClosedReader;
1956 if (reader != NULL)
1957 comm_read_cancel(fd, reader);
1958 fd_table[fd].halfClosedReader = NULL;
1959
1960 TheHalfClosed->del(fd);
a46d2c0e 1961}
1962
82ec8dfc
AR
1963/// I/O handler for the possibly half-closed connection monitoring code
1964static void
e1381638
AJ
1965commHalfClosedReader(int fd, char *, size_t size, comm_err_t flag, int, void *)
1966{
82ec8dfc 1967 // there cannot be more data coming in on half-closed connections
9e008dda 1968 assert(size == 0);
74257126
AR
1969 assert(commHasHalfClosedMonitor(fd)); // or we would have canceled the read
1970
1971 fd_table[fd].halfClosedReader = NULL; // done reading, for now
a46d2c0e 1972
82ec8dfc
AR
1973 // nothing to do if fd is being closed
1974 if (flag == COMM_ERR_CLOSING)
1975 return;
a46d2c0e 1976
82ec8dfc
AR
1977 // if read failed, close the connection
1978 if (flag != COMM_OK) {
1979 debugs(5, 3, "commHalfClosedReader: closing FD " << fd);
1980 comm_close(fd);
1981 return;
1982 }
a46d2c0e 1983
82ec8dfc 1984 // continue waiting for close or error
74257126 1985 commPlanHalfClosedCheck(); // make sure this fd will be checked again
a46d2c0e 1986}
1987
a46d2c0e 1988
b0469965 1989CommRead::CommRead() : fd(-1), buf(NULL), len(0), callback(NULL) {}
a46d2c0e 1990
b0469965 1991CommRead::CommRead(int fd_, char *buf_, int len_, AsyncCall::Pointer &callback_)
1992 : fd(fd_), buf(buf_), len(len_), callback(callback_) {}
a46d2c0e 1993
a50bfe93 1994DeferredRead::DeferredRead () : theReader(NULL), theContext(NULL), theRead(), cancelled(false) {}
a46d2c0e 1995
a50bfe93 1996DeferredRead::DeferredRead (DeferrableRead *aReader, void *data, CommRead const &aRead) : theReader(aReader), theContext (data), theRead(aRead), cancelled(false) {}
a46d2c0e 1997
e1381638
AJ
1998DeferredReadManager::~DeferredReadManager()
1999{
a46d2c0e 2000 flushReads();
2001 assert (deferredReads.empty());
2002}
2003
97427e90 2004/* explicit instantiation required for some systems */
2005
63be0a78 2006/// \cond AUTODOCS-IGNORE
2236466c 2007template cbdata_type CbDataList<DeferredRead>::CBDATA_CbDataList;
63be0a78 2008/// \endcond
97427e90 2009
a46d2c0e 2010void
e1381638
AJ
2011DeferredReadManager::delayRead(DeferredRead const &aRead)
2012{
bf8fe701 2013 debugs(5, 3, "Adding deferred read on FD " << aRead.theRead.fd);
2236466c 2014 CbDataList<DeferredRead> *temp = deferredReads.push_back(aRead);
2796c0d7 2015
9e008dda 2016 // We have to use a global function as a closer and point to temp
2796c0d7
AR
2017 // instead of "this" because DeferredReadManager is not a job and
2018 // is not even cbdata protected
2019 AsyncCall::Pointer closer = commCbCall(5,4,
9e008dda
AJ
2020 "DeferredReadManager::CloseHandler",
2021 CommCloseCbPtrFun(&CloseHandler, temp));
2796c0d7
AR
2022 comm_add_close_handler(aRead.theRead.fd, closer);
2023 temp->element.closer = closer; // remeber so that we can cancel
a46d2c0e 2024}
2025
2026void
e1381638
AJ
2027DeferredReadManager::CloseHandler(int fd, void *thecbdata)
2028{
a46d2c0e 2029 if (!cbdataReferenceValid (thecbdata))
2030 return;
2031
2236466c 2032 CbDataList<DeferredRead> *temp = (CbDataList<DeferredRead> *)thecbdata;
a46d2c0e 2033
2796c0d7 2034 temp->element.closer = NULL;
a46d2c0e 2035 temp->element.markCancelled();
2036}
2037
2038DeferredRead
e1381638
AJ
2039DeferredReadManager::popHead(CbDataListContainer<DeferredRead> &deferredReads)
2040{
a46d2c0e 2041 assert (!deferredReads.empty());
2042
2796c0d7
AR
2043 DeferredRead &read = deferredReads.head->element;
2044 if (!read.cancelled) {
2045 comm_remove_close_handler(read.theRead.fd, read.closer);
2046 read.closer = NULL;
2047 }
a46d2c0e 2048
2049 DeferredRead result = deferredReads.pop_front();
2050
2051 return result;
2052}
2053
2054void
e1381638
AJ
2055DeferredReadManager::kickReads(int const count)
2056{
2236466c 2057 /* if we had CbDataList::size() we could consolidate this and flushReads */
a46d2c0e 2058
33cea91c 2059 if (count < 1) {
a46d2c0e 2060 flushReads();
33cea91c 2061 return;
2062 }
a46d2c0e 2063
2064 size_t remaining = count;
2065
2066 while (!deferredReads.empty() && remaining) {
2067 DeferredRead aRead = popHead(deferredReads);
2068 kickARead(aRead);
2069
2070 if (!aRead.cancelled)
2071 --remaining;
2072 }
2073}
2074
2075void
e1381638
AJ
2076DeferredReadManager::flushReads()
2077{
2236466c 2078 CbDataListContainer<DeferredRead> reads;
a46d2c0e 2079 reads = deferredReads;
2236466c 2080 deferredReads = CbDataListContainer<DeferredRead>();
a46d2c0e 2081
27b24462 2082 // XXX: For fairness this SHOULD randomize the order
a46d2c0e 2083 while (!reads.empty()) {
2084 DeferredRead aRead = popHead(reads);
2085 kickARead(aRead);
2086 }
2087}
2088
2089void
e1381638
AJ
2090DeferredReadManager::kickARead(DeferredRead const &aRead)
2091{
a46d2c0e 2092 if (aRead.cancelled)
2093 return;
2094
af6a12ee
AJ
2095 if (aRead.theRead.fd>=0 && fd_table[aRead.theRead.fd].closing())
2096 return;
ed2c738b 2097
bf8fe701 2098 debugs(5, 3, "Kicking deferred read on FD " << aRead.theRead.fd);
a46d2c0e 2099
2100 aRead.theReader(aRead.theContext, aRead.theRead);
2101}
2102
2103void
e1381638
AJ
2104DeferredRead::markCancelled()
2105{
a46d2c0e 2106 cancelled = true;
2107}
2d8c0b1a 2108
8ff3fa2e 2109int
e1381638
AJ
2110CommSelectEngine::checkEvents(int timeout)
2111{
fa3f745b 2112 static time_t last_timeout = 0;
2113
2114 /* No, this shouldn't be here. But it shouldn't be in each comm handler. -adrian */
2115 if (squid_curtime > last_timeout) {
2116 last_timeout = squid_curtime;
2117 checkTimeouts();
2118 }
2119
8ff3fa2e 2120 switch (comm_select(timeout)) {
2121
2122 case COMM_OK:
2123
2124 case COMM_TIMEOUT:
2125 return 0;
2126
2127 case COMM_IDLE:
2128
2129 case COMM_SHUTDOWN:
2130 return EVENT_IDLE;
2131
2132 case COMM_ERROR:
2133 return EVENT_ERROR;
2134
2135 default:
2136 fatal_dump("comm.cc: Internal error -- this should never happen.");
2137 return EVENT_ERROR;
2138 };
2139}
10cefb7b 2140
0ffda73c 2141/// Create a unix-domain socket (UDS) that only supports FD_MSGHDR I/O.
10cefb7b 2142int
2143comm_open_uds(int sock_type,
2144 int proto,
2145 struct sockaddr_un* addr,
2146 int flags)
2147{
a67d2b2e 2148 // TODO: merge with comm_openex() when Ip::Address becomes NetAddress
ba568924 2149
10cefb7b 2150 int new_socket;
10cefb7b 2151
2152 PROF_start(comm_open);
10cefb7b 2153 /* Create socket for accepting new connections. */
2154 statCounter.syscalls.sock.sockets++;
2155
2156 /* Setup the socket addrinfo details for use */
ba568924 2157 struct addrinfo AI;
10cefb7b 2158 AI.ai_flags = 0;
2159 AI.ai_family = PF_UNIX;
2160 AI.ai_socktype = sock_type;
2161 AI.ai_protocol = proto;
2162 AI.ai_addrlen = SUN_LEN(addr);
2163 AI.ai_addr = (sockaddr*)addr;
2164 AI.ai_canonname = NULL;
2165 AI.ai_next = NULL;
2166
ba568924 2167 debugs(50, 3, HERE << "Attempt open socket for: " << addr->sun_path);
10cefb7b 2168
2169 if ((new_socket = socket(AI.ai_family, AI.ai_socktype, AI.ai_protocol)) < 0) {
2170 /* Increase the number of reserved fd's if calls to socket()
2171 * are failing because the open file table is full. This
2172 * limits the number of simultaneous clients */
2173
2174 if (limitError(errno)) {
ba568924 2175 debugs(50, DBG_IMPORTANT, HERE << "socket failure: " << xstrerror());
10cefb7b 2176 fdAdjustReserved();
2177 } else {
ba568924 2178 debugs(50, DBG_CRITICAL, HERE << "socket failure: " << xstrerror());
10cefb7b 2179 }
2180
2181 PROF_stop(comm_open);
2182 return -1;
2183 }
2184
ba568924 2185 debugs(50, 3, HERE "Opened UDS FD " << new_socket << " : family=" << AI.ai_family << ", type=" << AI.ai_socktype << ", protocol=" << AI.ai_protocol);
10cefb7b 2186
2187 /* update fdstat */
ba568924 2188 debugs(50, 5, HERE << "FD " << new_socket << " is a new socket");
10cefb7b 2189
2190 assert(!isOpen(new_socket));
1bac0258 2191 fd_open(new_socket, FD_MSGHDR, NULL);
10cefb7b 2192
2193 fdd_table[new_socket].close_file = NULL;
ba568924 2194
10cefb7b 2195 fdd_table[new_socket].close_line = 0;
2196
ba568924
AR
2197 fd_table[new_socket].sock_family = AI.ai_family;
2198
10cefb7b 2199 if (!(flags & COMM_NOCLOEXEC))
2200 commSetCloseOnExec(new_socket);
2201
2202 if (flags & COMM_REUSEADDR)
2203 commSetReuseAddr(new_socket);
2204
2205 if (flags & COMM_NONBLOCKING) {
2206 if (commSetNonBlocking(new_socket) != COMM_OK) {
2207 comm_close(new_socket);
2208 PROF_stop(comm_open);
2209 return -1;
2210 }
2211 }
2212
2213 if (flags & COMM_DOBIND) {
2214 if (commBind(new_socket, AI) != COMM_OK) {
2215 comm_close(new_socket);
2216 PROF_stop(comm_open);
2217 return -1;
2218 }
2219 }
2220
2221#ifdef TCP_NODELAY
2222 if (sock_type == SOCK_STREAM)
2223 commSetTcpNoDelay(new_socket);
2224
2225#endif
2226
2227 if (Config.tcpRcvBufsz > 0 && sock_type == SOCK_STREAM)
2228 commSetTcpRcvbuf(new_socket, Config.tcpRcvBufsz);
2229
2230 PROF_stop(comm_open);
2231
2232 return new_socket;
2233}