]> git.ipfire.org Git - thirdparty/squid.git/blame - src/comm.cc
adding
[thirdparty/squid.git] / src / comm.cc
CommitLineData
52e1d7e2 1
30a4f2a8 2/*
f52826a1 3 * $Id: comm.cc,v 1.170 1997/06/20 05:26:08 wessels Exp $
30a4f2a8 4 *
5 * DEBUG: section 5 Socket Functions
6 * AUTHOR: Harvest Derived
7 *
42c04c16 8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
30a4f2a8 9 * --------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by
14 * the National Science Foundation.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 */
d1f14731 31
30a4f2a8 32/*
33 * Copyright (c) 1994, 1995. All rights reserved.
34 *
35 * The Harvest software was developed by the Internet Research Task
36 * Force Research Group on Resource Discovery (IRTF-RD):
37 *
38 * Mic Bowman of Transarc Corporation.
39 * Peter Danzig of the University of Southern California.
40 * Darren R. Hardy of the University of Colorado at Boulder.
41 * Udi Manber of the University of Arizona.
42 * Michael F. Schwartz of the University of Colorado at Boulder.
43 * Duane Wessels of the University of Colorado at Boulder.
44 *
45 * This copyright notice applies to software in the Harvest
46 * ``src/'' directory only. Users should consult the individual
47 * copyright notices in the ``components/'' subdirectories for
48 * copyright information about other software bundled with the
49 * Harvest source code distribution.
50 *
51 * TERMS OF USE
52 *
53 * The Harvest software may be used and re-distributed without
54 * charge, provided that the software origin and research team are
55 * cited in any use of the system. Most commonly this is
56 * accomplished by including a link to the Harvest Home Page
57 * (http://harvest.cs.colorado.edu/) from the query page of any
58 * Broker you deploy, as well as in the query result pages. These
59 * links are generated automatically by the standard Broker
60 * software distribution.
61 *
62 * The Harvest software is provided ``as is'', without express or
63 * implied warranty, and with no support nor obligation to assist
64 * in its use, correction, modification or enhancement. We assume
65 * no liability with respect to the infringement of copyrights,
66 * trade secrets, or any patents, and are not responsible for
67 * consequential damages. Proper use of the Harvest software is
68 * entirely the responsibility of the user.
69 *
70 * DERIVATIVE WORKS
71 *
72 * Users may make derivative works from the Harvest software, subject
73 * to the following constraints:
74 *
75 * - You must include the above copyright notice and these
76 * accompanying paragraphs in all forms of derivative works,
77 * and any documentation and other materials related to such
78 * distribution and use acknowledge that the software was
79 * developed at the above institutions.
80 *
81 * - You must notify IRTF-RD regarding your distribution of
82 * the derivative work.
83 *
84 * - You must clearly notify users that your are distributing
85 * a modified version and not the original Harvest software.
86 *
87 * - Any derivative product is also subject to these copyright
88 * and use restrictions.
89 *
90 * Note that the Harvest software is NOT in the public domain. We
91 * retain copyright, as specified above.
92 *
93 * HISTORY OF FREE SOFTWARE STATUS
94 *
95 * Originally we required sites to license the software in cases
96 * where they were going to build commercial products/services
97 * around Harvest. In June 1995 we changed this policy. We now
98 * allow people to use the core Harvest software (the code found in
99 * the Harvest ``src/'' directory) for free. We made this change
100 * in the interest of encouraging the widest possible deployment of
101 * the technology. The Harvest software is really a reference
102 * implementation of a set of protocols and formats, some of which
103 * we intend to standardize. We encourage commercial
104 * re-implementations of code complying to this set of standards.
105 */
090089c4 106
44a47c6e 107#include "squid.h"
0a0bf5db 108#include <errno.h>
090089c4 109
30a4f2a8 110#ifdef HAVE_NETINET_TCP_H
111#include <netinet/tcp.h>
112#endif
090089c4 113
114/* Block processing new client requests (accepts on ascii port) when we start
115 * running shy of free file descriptors. For example, under SunOS, we'll keep
116 * 64 file descriptors free for disk-i/o and connections to remote servers */
117
da22ac20 118int RESERVED_FD = 64;
97c03d3c 119int polledinc = 0;
090089c4 120
121#define min(x,y) ((x)<(y)? (x) : (y))
122#define max(a,b) ((a)>(b)? (a) : (b))
123
f17936ab 124struct _cwstate {
30a4f2a8 125 char *buf;
126 long size;
127 long offset;
f17936ab 128 CWCB *handler;
30a4f2a8 129 void *handler_data;
4a63c85f 130 void (*free) (void *);
f17936ab 131};
090089c4 132
f88211e8 133typedef struct {
134 char *host;
135 u_short port;
136 struct sockaddr_in S;
137 CNCB *callback;
138 void *data;
139 int tries;
140 struct in_addr in_addr;
141 int locks;
03a1ee42 142 int fd;
f88211e8 143} ConnectStateData;
144
090089c4 145/* GLOBAL */
090089c4 146FD_ENTRY *fd_table = NULL; /* also used in disk.c */
147
148/* STATIC */
24382924 149static int commBind _PARAMS((int s, struct in_addr, u_short port));
f88211e8 150#if !HAVE_POLL
5742d7c9 151static int examine_select _PARAMS((fd_set *, fd_set *));
dcfe6390 152#endif
67508012 153static void checkTimeouts _PARAMS((void));
67508012 154static void commSetReuseAddr _PARAMS((int));
67508012 155static void commSetNoLinger _PARAMS((int));
812ed90c 156#if HAVE_POLL
157static void comm_poll_incoming _PARAMS((void));
158#else
67508012 159static void comm_select_incoming _PARAMS((void));
812ed90c 160#endif
f17936ab 161static void CommWriteStateCallbackAndFree _PARAMS((int fd, int code));
30a4f2a8 162#ifdef TCP_NODELAY
67508012 163static void commSetTcpNoDelay _PARAMS((int));
30a4f2a8 164#endif
67508012 165static void commSetTcpRcvbuf _PARAMS((int, int));
f88211e8 166static PF commConnectFree;
03a1ee42 167static PF commConnectHandle;
168static PF commHandleWrite;
812ed90c 169static int fdIsHttpOrIcp _PARAMS((int fd));
edeb28fd 170static IPH commConnectDnsHandle;
03a1ee42 171static void commConnectCallback _PARAMS((ConnectStateData * cs, int status));
30a4f2a8 172
30a4f2a8 173static struct timeval zero_tv;
090089c4 174
81f754fa 175void
f17936ab 176commCancelWriteHandler(int fd)
81f754fa 177{
f17936ab 178 CommWriteStateData *CommWriteState = fd_table[fd].rwstate;
179 if (CommWriteState) {
180 CommWriteState->handler = NULL;
181 CommWriteState->handler_data = NULL;
81f754fa 182 }
183}
184
b8d8561b 185static void
f17936ab 186CommWriteStateCallbackAndFree(int fd, int code)
9864ee44 187{
f17936ab 188 CommWriteStateData *CommWriteState = fd_table[fd].rwstate;
189 CWCB *callback = NULL;
1a8f5ed6 190 void *data;
a56a3abe 191 fd_table[fd].rwstate = NULL;
f17936ab 192 if (CommWriteState == NULL)
9864ee44 193 return;
f17936ab 194 if (CommWriteState->free) {
195 CommWriteState->free(CommWriteState->buf);
196 CommWriteState->buf = NULL;
9864ee44 197 }
f17936ab 198 callback = CommWriteState->handler;
1a8f5ed6 199 data = CommWriteState->handler_data;
f17936ab 200 CommWriteState->handler = NULL;
1a8f5ed6 201 if (callback && cbdataValid(data))
202 callback(fd, CommWriteState->buf, CommWriteState->offset, code, data);
203 cbdataUnlock(data);
f17936ab 204 safe_free(CommWriteState);
9864ee44 205}
206
090089c4 207/* Return the local port associated with fd. */
b8d8561b 208u_short
209comm_local_port(int fd)
090089c4 210{
211 struct sockaddr_in addr;
212 int addr_len = 0;
9864ee44 213 FD_ENTRY *fde = &fd_table[fd];
090089c4 214
090089c4 215 /* If the fd is closed already, just return */
95d15928 216 if (!fde->open) {
a3d5953d 217 debug(5, 0) ("comm_local_port: FD %d has been closed.\n", fd);
30a4f2a8 218 return 0;
090089c4 219 }
9864ee44 220 if (fde->local_port)
221 return fde->local_port;
090089c4 222 addr_len = sizeof(addr);
223 if (getsockname(fd, (struct sockaddr *) &addr, &addr_len)) {
a3d5953d 224 debug(50, 1) ("comm_local_port: Failed to retrieve TCP/UDP port number for socket: FD %d: %s\n", fd, xstrerror());
30a4f2a8 225 return 0;
090089c4 226 }
a3d5953d 227 debug(5, 6) ("comm_local_port: FD %d: sockaddr %u.\n", fd, addr.sin_addr.s_addr);
9864ee44 228 fde->local_port = ntohs(addr.sin_port);
229 return fde->local_port;
090089c4 230}
231
b8d8561b 232static int
233commBind(int s, struct in_addr in_addr, u_short port)
090089c4 234{
235 struct sockaddr_in S;
090089c4 236
090089c4 237 memset(&S, '\0', sizeof(S));
238 S.sin_family = AF_INET;
239 S.sin_port = htons(port);
30a4f2a8 240 S.sin_addr = in_addr;
090089c4 241 if (bind(s, (struct sockaddr *) &S, sizeof(S)) == 0)
242 return COMM_OK;
a3d5953d 243 debug(50, 0) ("commBind: Cannot bind socket FD %d to %s:%d: %s\n",
090089c4 244 s,
30a4f2a8 245 S.sin_addr.s_addr == INADDR_ANY ? "*" : inet_ntoa(S.sin_addr),
44a62238 246 (int) port,
247 xstrerror());
090089c4 248 return COMM_ERROR;
249}
250
251/* Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE
252 * is OR of flags specified in comm.h. */
b8d8561b 253int
16b204c4 254comm_open(int sock_type,
cc6a9d2e 255 int proto,
256 struct in_addr addr,
257 u_short port,
258 int flags,
0ee4272b 259 const char *note)
090089c4 260{
261 int new_socket;
95d15928 262 FD_ENTRY *fde = NULL;
b6f794d6 263 int tcp_rcv_bufsz = Config.tcpRcvBufsz;
090089c4 264
265 /* Create socket for accepting new connections. */
16b204c4 266 if ((new_socket = socket(AF_INET, sock_type, proto)) < 0) {
090089c4 267 /* Increase the number of reserved fd's if calls to socket()
268 * are failing because the open file table is full. This
269 * limits the number of simultaneous clients */
270 switch (errno) {
271 case ENFILE:
272 case EMFILE:
a3d5953d 273 debug(50, 1) ("comm_open: socket failure: %s\n", xstrerror());
090089c4 274 break;
275 default:
a3d5953d 276 debug(50, 0) ("comm_open: socket failure: %s\n", xstrerror());
090089c4 277 }
278 return (COMM_ERROR);
279 }
280 /* update fdstat */
489b22c1 281 debug(5,5)("comm_open: FD %d is a new socket\n", new_socket);
5c5783a2 282 fd_open(new_socket, FD_SOCKET, note);
95d15928 283 fde = &fd_table[new_socket];
16b204c4 284 if (!BIT_TEST(flags, COMM_NOCLOEXEC))
3ca60c86 285 commSetCloseOnExec(new_socket);
7690e8eb 286 if (port > (u_short) 0) {
30a4f2a8 287 commSetNoLinger(new_socket);
288 if (do_reuse)
090089c4 289 commSetReuseAddr(new_socket);
090089c4 290 }
429fdbec 291 if (addr.s_addr != no_addr.s_addr)
30a4f2a8 292 if (commBind(new_socket, addr, port) != COMM_OK)
293 return COMM_ERROR;
95d15928 294 fde->local_port = port;
090089c4 295
16b204c4 296 if (BIT_TEST(flags, COMM_NONBLOCKING))
30a4f2a8 297 if (commSetNonBlocking(new_socket) == COMM_ERROR)
298 return COMM_ERROR;
299#ifdef TCP_NODELAY
300 if (sock_type == SOCK_STREAM)
301 commSetTcpNoDelay(new_socket);
302#endif
f868539a 303 if (tcp_rcv_bufsz > 0 && sock_type == SOCK_STREAM)
304 commSetTcpRcvbuf(new_socket, tcp_rcv_bufsz);
090089c4 305 return new_socket;
306}
307
308 /*
e83892e9 309 * NOTE: set the listen queue to Squid_MaxFD/4 and rely on the kernel to
090089c4 310 * impose an upper limit. Solaris' listen(3n) page says it has
311 * no limit on this parameter, but sys/socket.h sets SOMAXCONN
312 * to 5. HP-UX currently has a limit of 20. SunOS is 5 and
313 * OSF 3.0 is 8.
314 */
b8d8561b 315int
316comm_listen(int sock)
090089c4 317{
318 int x;
e83892e9 319 if ((x = listen(sock, Squid_MaxFD >> 2)) < 0) {
a3d5953d 320 debug(50, 0) ("comm_listen: listen(%d, %d): %s\n",
e83892e9 321 Squid_MaxFD >> 2,
090089c4 322 sock, xstrerror());
323 return x;
324 }
325 return sock;
326}
327
e5f6c5c2 328void
4f92c80c 329commConnectStart(int fd, const char *host, u_short port, CNCB * callback, void *data)
e924600d 330{
331 ConnectStateData *cs = xcalloc(1, sizeof(ConnectStateData));
8407afee 332 cbdataAdd(cs);
03a1ee42 333 cs->fd = fd;
e924600d 334 cs->host = xstrdup(host);
335 cs->port = port;
336 cs->callback = callback;
337 cs->data = data;
8407afee 338 cbdataLock(data);
e924600d 339 comm_add_close_handler(fd, commConnectFree, cs);
f88211e8 340 cs->locks++;
8407afee 341 ipcache_nbgethostbyname(host, commConnectDnsHandle, cs);
edeb28fd 342}
343
344static void
03a1ee42 345commConnectDnsHandle(const ipcache_addrs * ia, void *data)
edeb28fd 346{
347 ConnectStateData *cs = data;
f88211e8 348 assert(cs->locks == 1);
349 cs->locks--;
edeb28fd 350 if (ia == NULL) {
a3d5953d 351 debug(5, 3) ("commConnectDnsHandle: Unknown host: %s\n", cs->host);
03a1ee42 352 commConnectCallback(cs, COMM_ERR_DNS);
edeb28fd 353 return;
354 }
355 cs->in_addr = ia->in_addrs[ia->cur];
03a1ee42 356 commConnectHandle(cs->fd, cs);
e924600d 357}
358
f88211e8 359static void
03a1ee42 360commConnectCallback(ConnectStateData * cs, int status)
f88211e8 361{
a3d5953d 362 CNCB *callback = cs->callback;
363 void *data = cs->data;
03a1ee42 364 int fd = cs->fd;
a3d5953d 365 comm_remove_close_handler(fd, commConnectFree, cs);
366 commConnectFree(fd, cs);
8407afee 367 if (cbdataValid(data))
368 callback(fd, status, data);
369 cbdataUnlock(data);
f88211e8 370}
371
e924600d 372static void
03a1ee42 373commConnectFree(int fdunused, void *data)
e924600d 374{
375 ConnectStateData *cs = data;
8407afee 376 if (cs->locks)
377 ipcacheUnregister(cs->host, cs);
378 safe_free(cs->host);
379 cbdataFree(cs);
e924600d 380}
381
edeb28fd 382static int
f88211e8 383commRetryConnect(int fd, ConnectStateData * cs)
edeb28fd 384{
385 int fd2;
f88211e8 386 if (++cs->tries == 4)
edeb28fd 387 return 0;
7dd44885 388 if (!cbdataValid(cs->data))
389 return 0;
edeb28fd 390 fd2 = socket(AF_INET, SOCK_STREAM, 0);
391 if (fd2 < 0) {
a3d5953d 392 debug(5, 0) ("commRetryConnect: socket: %s\n", xstrerror());
edeb28fd 393 return 0;
394 }
395 if (dup2(fd2, fd) < 0) {
a3d5953d 396 debug(5, 0) ("commRetryConnect: dup2: %s\n", xstrerror());
edeb28fd 397 return 0;
398 }
399 commSetNonBlocking(fd);
400 close(fd2);
401 return 1;
402}
403
e924600d 404/* Connect SOCK to specified DEST_PORT at DEST_HOST. */
405static void
406commConnectHandle(int fd, void *data)
090089c4 407{
f88211e8 408 ConnectStateData *cs = data;
409 if (cs->S.sin_addr.s_addr == 0) {
410 cs->S.sin_family = AF_INET;
411 cs->S.sin_addr = cs->in_addr;
412 cs->S.sin_port = htons(cs->port);
e5f6c5c2 413 if (Config.Log.log_fqdn)
f88211e8 414 fqdncache_gethostbyaddr(cs->S.sin_addr, FQDN_LOOKUP_IF_MISS);
e5f6c5c2 415 }
f88211e8 416 switch (comm_connect_addr(fd, &cs->S)) {
e5f6c5c2 417 case COMM_INPROGRESS:
489b22c1 418 debug(5, 5) ("FD %d: COMM_INPROGRESS\n", fd);
f88211e8 419 commSetSelect(fd, COMM_SELECT_WRITE, commConnectHandle, cs, 0);
e5f6c5c2 420 break;
421 case COMM_OK:
e924600d 422 if (vizSock > -1)
f88211e8 423 vizHackSendPkt(&cs->S, 2);
424 ipcacheCycleAddr(cs->host);
03a1ee42 425 commConnectCallback(cs, COMM_OK);
e5f6c5c2 426 break;
427 default:
f88211e8 428 if (commRetryConnect(fd, cs)) {
a3d5953d 429 debug(5, 1) ("Retrying connection to %s: %s\n",
f88211e8 430 cs->host, xstrerror());
431 cs->S.sin_addr.s_addr = 0;
432 ipcacheCycleAddr(cs->host);
433 cs->locks++;
8407afee 434 ipcache_nbgethostbyname(cs->host, commConnectDnsHandle, cs);
edeb28fd 435 } else {
f88211e8 436 ipcacheRemoveBadAddr(cs->host, cs->S.sin_addr);
03a1ee42 437 commConnectCallback(cs, COMM_ERR_CONNECT);
edeb28fd 438 }
e5f6c5c2 439 break;
090089c4 440 }
090089c4 441}
b8d8561b 442int
4f92c80c 443commSetTimeout(int fd, int timeout, PF * handler, void *data)
090089c4 444{
5c5783a2 445 FD_ENTRY *fde;
a3d5953d 446 debug(5, 3) ("commSetTimeout: FD %d timeout %d\n", fd, timeout);
03eb2f01 447 assert(fd >= 0);
448 assert(fd < Squid_MaxFD);
5c5783a2 449 fde = &fd_table[fd];
450 if (timeout < 0) {
4f92c80c 451 fde->timeout_handler = NULL;
452 fde->timeout_data = NULL;
453 return fde->timeout = 0;
5c5783a2 454 }
bbdb774b 455 if (shutdown_pending || reconfigure_pending) {
4f92c80c 456 /* don't increase the timeout if something pending */
457 if (fde->timeout > 0 && (int) (fde->timeout - squid_curtime) < timeout)
458 return fde->timeout;
5c5783a2 459 }
03eb2f01 460 assert(handler || fde->timeout_handler);
5c5783a2 461 if (handler || data) {
4f92c80c 462 fde->timeout_handler = handler;
463 fde->timeout_data = data;
30a4f2a8 464 }
5c5783a2 465 return fde->timeout = squid_curtime + (time_t) timeout;
090089c4 466}
467
b8d8561b 468int
0ee4272b 469comm_connect_addr(int sock, const struct sockaddr_in *address)
090089c4 470{
471 int status = COMM_OK;
95d15928 472 FD_ENTRY *fde = &fd_table[sock];
090089c4 473 int len;
474 int x;
489b22c1 475 assert(ntohs(address->sin_port) != 0);
090089c4 476 /* Establish connection. */
086bce16 477 if (connect(sock, (struct sockaddr *) address, sizeof(struct sockaddr_in)) < 0) {
489b22c1 478 debug(5,9)("connect FD %d: %s\n", sock, xstrerror());
090089c4 479 switch (errno) {
480 case EALREADY:
30a4f2a8 481#if EAGAIN != EWOULDBLOCK
482 case EAGAIN:
483#endif
0a0bf5db 484 case EINTR:
30a4f2a8 485 case EWOULDBLOCK:
090089c4 486 case EINPROGRESS:
e5f6c5c2 487 status = COMM_INPROGRESS;
090089c4 488 break;
489 case EISCONN:
490 status = COMM_OK;
491 break;
492 case EINVAL:
493 len = sizeof(x);
494 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *) &x, &len) >= 0)
495 errno = x;
496 default:
a3d5953d 497 debug(50, 2) ("connect: %s:%d: %s.\n",
28ab0c0a 498 fqdnFromAddr(address->sin_addr),
090089c4 499 ntohs(address->sin_port),
500 xstrerror());
501 return COMM_ERROR;
502 }
e5f6c5c2 503 }
95d15928 504 xstrncpy(fde->ipaddr, inet_ntoa(address->sin_addr), 16);
505 fde->remote_port = ntohs(address->sin_port);
090089c4 506 if (status == COMM_OK) {
a3d5953d 507 debug(5, 10) ("comm_connect_addr: FD %d connected to %s:%d\n",
5c5783a2 508 sock, fde->ipaddr, fde->remote_port);
f21cd581 509 } else if (status == COMM_INPROGRESS) {
a3d5953d 510 debug(5, 10) ("comm_connect_addr: FD %d connection pending\n", sock);
090089c4 511 }
512 /* Add new socket to list of open sockets. */
090089c4 513 return status;
514}
515
516/* Wait for an incoming connection on FD. FD should be a socket returned
517 * from comm_listen. */
b8d8561b 518int
519comm_accept(int fd, struct sockaddr_in *peer, struct sockaddr_in *me)
090089c4 520{
521 int sock;
1f9afe33 522 struct sockaddr_in P;
523 struct sockaddr_in M;
090089c4 524 int Slen;
95d15928 525 FD_ENTRY *fde = NULL;
090089c4 526
1f9afe33 527 Slen = sizeof(P);
528 while ((sock = accept(fd, (struct sockaddr *) &P, &Slen)) < 0) {
090089c4 529 switch (errno) {
530#if EAGAIN != EWOULDBLOCK
531 case EAGAIN:
532#endif
533 case EWOULDBLOCK:
090089c4 534 case EINTR:
0a0bf5db 535 return COMM_NOMESSAGE;
090089c4 536 case ENFILE:
537 case EMFILE:
090089c4 538 return COMM_ERROR;
539 default:
a3d5953d 540 debug(50, 1) ("comm_accept: FD %d: accept failure: %s\n",
090089c4 541 fd, xstrerror());
542 return COMM_ERROR;
543 }
544 }
545
546 if (peer)
1f9afe33 547 *peer = P;
4053a845 548 Slen = sizeof(M);
549 memset(&M, '\0', Slen);
550 getsockname(sock, (struct sockaddr *) &M, &Slen);
551 if (me)
1f9afe33 552 *me = M;
3ca60c86 553 commSetCloseOnExec(sock);
090089c4 554 /* fdstat update */
5c5783a2 555 fd_open(sock, FD_SOCKET, "HTTP Request");
95d15928 556 fde = &fd_table[sock];
95d15928 557 strcpy(fde->ipaddr, inet_ntoa(P.sin_addr));
558 fde->remote_port = htons(P.sin_port);
559 fde->local_port = htons(M.sin_port);
090089c4 560 commSetNonBlocking(sock);
090089c4 561 return sock;
562}
563
cb201b7e 564void
565commCallCloseHandlers(int fd)
566{
95d15928 567 FD_ENTRY *fde = &fd_table[fd];
cb201b7e 568 struct close_handler *ch;
a3d5953d 569 debug(5, 5) ("commCallCloseHandlers: FD %d\n", fd);
95d15928 570 while ((ch = fde->close_handler) != NULL) {
571 fde->close_handler = ch->next;
cb201b7e 572 ch->handler(fd, ch->data);
573 safe_free(ch);
574 }
575}
576
b8d8561b 577void
578comm_close(int fd)
090089c4 579{
95d15928 580 FD_ENTRY *fde = NULL;
a3d5953d 581 debug(5, 5) ("comm_close: FD %d\n", fd);
03eb2f01 582 assert(fd >= 0);
583 assert(fd < Squid_MaxFD);
95d15928 584 fde = &fd_table[fd];
585 if (!fde->open)
9864ee44 586 return;
f52826a1 587 assert(fde->type != FD_FILE);
95d15928 588 fde->open = 0;
f17936ab 589 CommWriteStateCallbackAndFree(fd, COMM_ERROR);
cb201b7e 590 commCallCloseHandlers(fd);
5c5783a2 591 fd_close(fd); /* update fdstat */
0a0bf5db 592#if USE_ASYNC_IO
593 aioClose(fd);
594#else
9864ee44 595 close(fd);
0a0bf5db 596#endif
090089c4 597}
598
090089c4 599
600/* Send a udp datagram to specified PORT at HOST. */
b8d8561b 601int
0ee4272b 602comm_udp_send(int fd, const char *host, u_short port, const char *buf, int len)
090089c4 603{
0ee4272b 604 const ipcache_addrs *ia = NULL;
090089c4 605 static struct sockaddr_in to_addr;
606 int bytes_sent;
607
608 /* Set up the destination socket address for message to send to. */
609 to_addr.sin_family = AF_INET;
610
e5f6c5c2 611 if ((ia = ipcache_gethostbyname(host, IP_BLOCKING_LOOKUP)) == 0) {
a3d5953d 612 debug(50, 1) ("comm_udp_send: gethostbyname failure: %s: %s\n",
090089c4 613 host, xstrerror());
614 return (COMM_ERROR);
615 }
e5f6c5c2 616 to_addr.sin_addr = ia->in_addrs[ia->cur];
090089c4 617 to_addr.sin_port = htons(port);
618 if ((bytes_sent = sendto(fd, buf, len, 0, (struct sockaddr *) &to_addr,
619 sizeof(to_addr))) < 0) {
a3d5953d 620 debug(50, 1) ("comm_udp_send: sendto failure: FD %d: %s\n",
090089c4 621 fd, xstrerror());
622 return COMM_ERROR;
623 }
624 return bytes_sent;
625}
626
627/* Send a udp datagram to specified TO_ADDR. */
b8d8561b 628int
5df61230 629comm_udp_sendto(int fd,
630 const struct sockaddr_in *to_addr,
631 int addr_len,
632 const char *buf,
633 int len)
090089c4 634{
5df61230 635 int x;
636 x = sendto(fd, buf, len, 0, (struct sockaddr *) to_addr, addr_len);
637 if (x < 0) {
a3d5953d 638 debug(50, 1) ("comm_udp_sendto: FD %d, %s, port %d: %s\n",
5df61230 639 fd,
640 inet_ntoa(to_addr->sin_addr),
641 (int) htons(to_addr->sin_port),
642 xstrerror());
090089c4 643 return COMM_ERROR;
644 }
5df61230 645 return x;
090089c4 646}
647
b8d8561b 648void
649comm_set_stall(int fd, int delta)
4883993a 650{
651 if (fd < 0)
652 return;
b8de7ebe 653 fd_table[fd].stall_until = squid_curtime + delta;
4883993a 654}
655
dcfe6390 656
f88211e8 657#if HAVE_POLL
dcfe6390 658
659/* poll() version by:
660 * Stewart Forster <slf@connect.com.au>, and
661 * Anthony Baxter <arb@connect.com.au> */
662
663static void
812ed90c 664comm_poll_incoming(void)
dcfe6390 665{
429fdbec 666 int fd;
996a0a51 667 int fds[4];
0b2421ea 668 struct pollfd pfds[3 + MAXHTTPPORTS];
996a0a51 669 unsigned long N = 0;
429fdbec 670 unsigned long i, nfds;
812ed90c 671 int j;
582b6456 672 PF *hdl = NULL;
97c03d3c 673 polledinc = 0;
dcfe6390 674 if (theInIcpConnection >= 0)
675 fds[N++] = theInIcpConnection;
933c6d93 676 if (theInIcpConnection != theOutIcpConnection)
1793867a 677 if (theOutIcpConnection >= 0)
933c6d93 678 fds[N++] = theOutIcpConnection;
0b2421ea 679 for (j = 0; j < NHttpSockets; j++) {
812ed90c 680 if (HttpSockets[j] < 0)
0b2421ea 681 continue;
812ed90c 682 if (fd_table[HttpSockets[j]].stall_until > squid_curtime)
0b2421ea 683 continue;
812ed90c 684 fds[N++] = HttpSockets[j];
685 }
429fdbec 686 for (i = nfds = 0; i < N; i++) {
687 int events;
dcfe6390 688 fd = fds[i];
429fdbec 689 events = 0;
690 if (fd_table[fd].read_handler)
691 events |= POLLRDNORM;
692 if (fd_table[fd].write_handler)
693 events |= POLLWRNORM;
694 if (events) {
695 pfds[nfds].fd = fd;
696 pfds[nfds].events = events;
697 pfds[nfds].revents = 0;
698 nfds++;
dcfe6390 699 }
dcfe6390 700 }
429fdbec 701 if (!nfds)
702 return;
97c03d3c 703 polledinc = poll(pfds, nfds, 0);
704 if (polledinc < 1) {
705 polledinc = 0;
996a0a51 706 return;
97c03d3c 707 }
429fdbec 708 for (i = 0; i < nfds; i++) {
709 int revents;
710 if (((revents = pfds[i].revents) == 0) || ((fd = pfds[i].fd) == -1))
dcfe6390 711 continue;
429fdbec 712 if (revents & (POLLRDNORM | POLLIN | POLLHUP | POLLERR)) {
713 hdl = fd_table[fd].read_handler;
714 fd_table[fd].read_handler = 0;
715 hdl(fd, fd_table[fd].read_data);
dcfe6390 716 }
429fdbec 717 if (revents & (POLLWRNORM | POLLOUT | POLLHUP | POLLERR)) {
718 hdl = fd_table[fd].write_handler;
719 fd_table[fd].write_handler = 0;
720 hdl(fd, fd_table[fd].write_data);
dcfe6390 721 }
722 }
723 /* TO FIX: repoll ICP connection here */
724}
725
ca98227c 726#else
dcfe6390 727
b8d8561b 728static void
0673c0ba 729comm_select_incoming(void)
055f4d4d 730{
731 fd_set read_mask;
732 fd_set write_mask;
733 int maxfd = 0;
734 int fd = 0;
0b2421ea 735 int fds[3 + MAXHTTPPORTS];
055f4d4d 736 int N = 0;
737 int i = 0;
582b6456 738 PF *hdl = NULL;
97c03d3c 739 polledinc = 0;
055f4d4d 740 FD_ZERO(&read_mask);
741 FD_ZERO(&write_mask);
0b2421ea 742 for (i = 0; i < NHttpSockets; i++) {
812ed90c 743 if (HttpSockets[i] < 0)
0b2421ea 744 continue;
812ed90c 745 if (fd_table[HttpSockets[i]].stall_until > squid_curtime)
0b2421ea 746 continue;
812ed90c 747 fds[N++] = HttpSockets[i];
748 }
30a4f2a8 749 if (theInIcpConnection >= 0)
750 fds[N++] = theInIcpConnection;
933c6d93 751 if (theInIcpConnection != theOutIcpConnection)
752 if (theOutIcpConnection >= 0)
753 fds[N++] = theOutIcpConnection;
055f4d4d 754 fds[N++] = 0;
055f4d4d 755 for (i = 0; i < N; i++) {
756 fd = fds[i];
757 if (fd_table[fd].read_handler) {
758 FD_SET(fd, &read_mask);
759 if (fd > maxfd)
760 maxfd = fd;
761 }
762 if (fd_table[fd].write_handler) {
763 FD_SET(fd, &write_mask);
764 if (fd > maxfd)
765 maxfd = fd;
766 }
767 }
055f4d4d 768 if (maxfd++ == 0)
769 return;
97c03d3c 770 polledinc = select(maxfd, &read_mask, &write_mask, NULL, &zero_tv);
771 if (polledinc < 1) {
772 polledinc = 0;
dcfe6390 773 return;
97c03d3c 774 }
dcfe6390 775 for (i = 0; i < N; i++) {
776 fd = fds[i];
777 if (FD_ISSET(fd, &read_mask)) {
778 hdl = fd_table[fd].read_handler;
779 fd_table[fd].read_handler = 0;
780 hdl(fd, fd_table[fd].read_data);
781 }
782 if (FD_ISSET(fd, &write_mask)) {
783 hdl = fd_table[fd].write_handler;
784 fd_table[fd].write_handler = 0;
785 hdl(fd, fd_table[fd].write_data);
786 }
787 }
788}
789#endif
790
812ed90c 791static int
792fdIsHttpOrIcp(int fd)
793{
794 int j;
795 if (fd == theInIcpConnection)
796 return 1;
797 if (fd == theOutIcpConnection)
798 return 1;
799 for (j = 0; j < NHttpSockets; j++) {
800 if (fd == HttpSockets[j])
801 return 1;
802 }
803 return 0;
804}
805
f88211e8 806#if HAVE_POLL
dcfe6390 807/* poll all sockets; call handlers for those that are ready. */
808int
812ed90c 809comm_poll(time_t sec)
dcfe6390 810{
0a0bf5db 811 struct pollfd pfds[SQUID_MAXFD];
582b6456 812 PF *hdl = NULL;
dcfe6390 813 int fd;
814 int i;
815 int maxfd;
996a0a51 816 unsigned long nfds;
dcfe6390 817 int num;
dcfe6390 818 static time_t last_timeout = 0;
97c03d3c 819 static int lastinc = 0;
429fdbec 820 int poll_time;
812ed90c 821 static int incoming_counter = 0;
dcfe6390 822 time_t timeout;
dcfe6390 823 /* assume all process are very fast (less than 1 second). Call
824 * time() only once */
dcfe6390 825 /* use only 1 second granularity */
826 timeout = squid_curtime + sec;
827 do {
bbdb774b 828 if (shutdown_pending || reconfigure_pending) {
dcfe6390 829 serverConnectionsClose();
dcfe6390 830 dnsShutdownServers();
831 redirectShutdownServers();
429fdbec 832 /* shutdown_pending will be set to
833 * +1 for SIGTERM
834 * -1 for SIGINT */
bbdb774b 835 /* reconfigure_pending always == 1 when SIGHUP received */
836 if (shutdown_pending > 0 || reconfigure_pending > 0)
5c5783a2 837 setSocketShutdownLifetimes(Config.shutdownLifetime);
dcfe6390 838 else
9e4ad609 839 setSocketShutdownLifetimes(1);
dcfe6390 840 }
429fdbec 841 nfds = 0;
842 maxfd = Biggest_FD + 1;
429fdbec 843 for (i = 0; i < maxfd; i++) {
844 int events;
845 events = 0;
dcfe6390 846 /* Check each open socket for a handler. */
429fdbec 847 if (fd_table[i].read_handler && fd_table[i].stall_until <= squid_curtime)
848 events |= POLLRDNORM;
849 if (fd_table[i].write_handler)
850 events |= POLLWRNORM;
851 if (events) {
429fdbec 852 pfds[nfds].fd = i;
853 pfds[nfds].events = events;
854 pfds[nfds].revents = 0;
855 nfds++;
055f4d4d 856 }
0b2421ea 857 }
bbdb774b 858 if (shutdown_pending || reconfigure_pending)
a3d5953d 859 debug(5, 2) ("comm_poll: Still waiting on %d FDs\n", nfds);
dcfe6390 860 if (nfds == 0)
861 return COMM_SHUTDOWN;
0a0bf5db 862 poll_time = sec > 0 ? 100 : 0;
863#if USE_ASYNC_IO
864 aioCheckCallbacks();
865#endif
dcfe6390 866 for (;;) {
429fdbec 867 poll_time = sec > 0 ? 1000 : 0;
868 num = poll(pfds, nfds, poll_time);
869 select_loops++;
dcfe6390 870 if (num >= 0)
871 break;
872 if (errno == EINTR)
0a0bf5db 873 continue;
a3d5953d 874 debug(5, 0) ("comm_poll: poll failure: %s\n", xstrerror());
03eb2f01 875 assert(errno != EINVAL);
dcfe6390 876 return COMM_ERROR;
877 /* NOTREACHED */
878 }
a3d5953d 879 debug(5, num ? 5 : 8) ("comm_poll: %d sockets ready\n", num);
5c5783a2 880 /* Check timeout handlers ONCE each second. */
dcfe6390 881 if (squid_curtime > last_timeout) {
882 last_timeout = squid_curtime;
883 checkTimeouts();
dcfe6390 884 }
885 if (num == 0)
886 continue;
887 /* scan each socket but the accept socket. Poll this
2c5294ce 888 * more frequently to minimize losses due to the 5 connect
dcfe6390 889 * limit in SunOS */
429fdbec 890 for (i = 0; i < nfds; i++) {
891 int revents;
892 if (((revents = pfds[i].revents) == 0) || ((fd = pfds[i].fd) == -1))
dcfe6390 893 continue;
97c03d3c 894 if ((incoming_counter++ & (lastinc > 0 ? 1 : 7)) == 0)
812ed90c 895 comm_poll_incoming();
896 if (fdIsHttpOrIcp(fd))
996a0a51 897 continue;
429fdbec 898 if (revents & (POLLRDNORM | POLLIN | POLLHUP | POLLERR)) {
a3d5953d 899 debug(5, 6) ("comm_poll: FD %d ready for reading\n", fd);
0b2421ea 900 if ((hdl = fd_table[fd].read_handler)) {
901 fd_table[fd].read_handler = 0;
902 hdl(fd, fd_table[fd].read_data);
903 }
dcfe6390 904 }
429fdbec 905 if (revents & (POLLWRNORM | POLLOUT | POLLHUP | POLLERR)) {
a3d5953d 906 debug(5, 5) ("comm_poll: FD %d ready for writing\n", fd);
0b2421ea 907 if ((hdl = fd_table[fd].write_handler)) {
908 fd_table[fd].write_handler = 0;
909 hdl(fd, fd_table[fd].write_data);
910 }
dcfe6390 911 }
429fdbec 912 if (revents & POLLNVAL) {
913 struct close_handler *ch;
914 struct close_handler *next;
5c5783a2 915 FD_ENTRY *fde = &fd_table[fd];
a3d5953d 916 debug(5, 0) ("WARNING: FD %d has handlers, but it's invalid.\n", fd);
917 debug(5, 0) ("FD %d is a %s\n", fd, fdstatTypeStr[fd_table[fd].type]);
918 debug(5, 0) ("--> %s\n", fd_table[fd].desc);
919 debug(5, 0) ("tmout:%p read:%p write:%p\n",
5c5783a2 920 fde->timeout_handler,
921 fde->read_handler,
922 fde->write_handler);
923 for (ch = fde->close_handler; ch; ch = ch->next)
a3d5953d 924 debug(5, 0) (" close handler: %p\n", ch->handler);
5c5783a2 925 if (fde->close_handler) {
926 for (ch = fde->close_handler; ch; ch = next) {
dcfe6390 927 next = ch->next;
928 ch->handler(fd, ch->data);
929 safe_free(ch);
930 }
5c5783a2 931 } else if (fde->timeout_handler) {
a3d5953d 932 debug(5, 0) ("comm_poll: Calling Timeout Handler\n");
5c5783a2 933 fde->timeout_handler(fd, fde->timeout_data);
dcfe6390 934 }
5c5783a2 935 fde->close_handler = NULL;
936 fde->timeout_handler = NULL;
937 fde->read_handler = NULL;
938 fde->write_handler = NULL;
dcfe6390 939 }
97c03d3c 940 lastinc = polledinc;
dcfe6390 941 }
942 return COMM_OK;
97c03d3c 943 } while (timeout > squid_curtime);
a3d5953d 944 debug(5, 8) ("comm_poll: time out: %d.\n", squid_curtime);
dcfe6390 945 return COMM_TIMEOUT;
055f4d4d 946}
090089c4 947
dcfe6390 948#else
090089c4 949
950/* Select on all sockets; call handlers for those that are ready. */
b8d8561b 951int
952comm_select(time_t sec)
090089c4 953{
090089c4 954 fd_set readfds;
955 fd_set writefds;
582b6456 956 PF *hdl = NULL;
7d49daab 957 int fd;
958 int i;
959 int maxfd;
960 int nfds;
090089c4 961 int num;
97c03d3c 962 static int incoming_counter = 0;
090089c4 963 static time_t last_timeout = 0;
964 struct timeval poll_time;
97c03d3c 965 static int lastinc;
7d49daab 966 time_t timeout;
090089c4 967
968 /* assume all process are very fast (less than 1 second). Call
969 * time() only once */
090089c4 970 /* use only 1 second granularity */
b8de7ebe 971 timeout = squid_curtime + sec;
090089c4 972
f7361640 973 do {
090089c4 974 FD_ZERO(&readfds);
975 FD_ZERO(&writefds);
bbdb774b 976 if (shutdown_pending || reconfigure_pending) {
30a4f2a8 977 serverConnectionsClose();
f88bb09c 978 dnsShutdownServers();
d2af9477 979 redirectShutdownServers();
429fdbec 980 /* shutdown_pending will be set to
981 * +1 for SIGTERM
982 * -1 for SIGINT */
bbdb774b 983 /* reconfigure_pending always == 1 when SIGHUP received */
984 if (shutdown_pending > 0 || reconfigure_pending > 0)
5c5783a2 985 setSocketShutdownLifetimes(Config.shutdownLifetime);
f3753518 986 else
987 setSocketShutdownLifetimes(0);
30a4f2a8 988 }
4d64d74a 989 nfds = 0;
429fdbec 990 maxfd = Biggest_FD + 1;
4d64d74a 991 for (i = 0; i < maxfd; i++) {
090089c4 992 /* Check each open socket for a handler. */
ab1afadb 993 if (fd_table[i].stall_until > squid_curtime)
994 continue;
995 if (fd_table[i].read_handler) {
4d64d74a 996 nfds++;
090089c4 997 FD_SET(i, &readfds);
4d64d74a 998 }
999 if (fd_table[i].write_handler) {
1000 nfds++;
090089c4 1001 FD_SET(i, &writefds);
4d64d74a 1002 }
090089c4 1003 }
bbdb774b 1004 if (shutdown_pending || reconfigure_pending)
a3d5953d 1005 debug(5, 2) ("comm_select: Still waiting on %d FDs\n", nfds);
4d64d74a 1006 if (nfds == 0)
1007 return COMM_SHUTDOWN;
0a0bf5db 1008#if USE_ASYNC_IO
1009 aioCheckCallbacks();
1010#endif
7690e8eb 1011 for (;;) {
89fb2544 1012 poll_time.tv_sec = sec > 0 ? 1 : 0;
090089c4 1013 poll_time.tv_usec = 0;
d0217c9b 1014 num = select(maxfd, &readfds, &writefds, NULL, &poll_time);
429fdbec 1015 select_loops++;
090089c4 1016 if (num >= 0)
1017 break;
4d64d74a 1018 if (errno == EINTR)
1019 break;
a3d5953d 1020 debug(50, 0) ("comm_select: select failure: %s\n",
30a4f2a8 1021 xstrerror());
d0217c9b 1022 examine_select(&readfds, &writefds);
bf9f8f2b 1023 return COMM_ERROR;
30a4f2a8 1024 /* NOTREACHED */
090089c4 1025 }
4d64d74a 1026 if (num < 0)
1027 continue;
a3d5953d 1028 debug(5, num ? 5 : 8) ("comm_select: %d sockets ready at %d\n",
30a4f2a8 1029 num, (int) squid_curtime);
090089c4 1030
1031 /* Check lifetime and timeout handlers ONCE each second.
1032 * Replaces brain-dead check every time through the loop! */
b8de7ebe 1033 if (squid_curtime > last_timeout) {
1034 last_timeout = squid_curtime;
090089c4 1035 checkTimeouts();
090089c4 1036 }
7d49daab 1037 if (num == 0)
1038 continue;
1039
090089c4 1040 /* scan each socket but the accept socket. Poll this
2c5294ce 1041 * more frequently to minimize losses due to the 5 connect
090089c4 1042 * limit in SunOS */
1043
5742d7c9 1044 for (fd = 0; fd < maxfd; fd++) {
d0217c9b 1045 if (!FD_ISSET(fd, &readfds) && !FD_ISSET(fd, &writefds))
7d49daab 1046 continue;
cb2f803a 1047 if ((incoming_counter++ & (lastinc > 0 ? 1 : 7)) == 0)
1048 comm_select_incoming();
812ed90c 1049 if (fdIsHttpOrIcp(fd))
7d49daab 1050 continue;
7d49daab 1051 if (FD_ISSET(fd, &readfds)) {
a3d5953d 1052 debug(5, 6) ("comm_select: FD %d ready for reading\n", fd);
7d49daab 1053 if (fd_table[fd].read_handler) {
ff8d0ea6 1054 hdl = fd_table[fd].read_handler;
7d49daab 1055 fd_table[fd].read_handler = 0;
ff8d0ea6 1056 hdl(fd, fd_table[fd].read_data);
090089c4 1057 }
7d49daab 1058 }
1059 if (FD_ISSET(fd, &writefds)) {
a3d5953d 1060 debug(5, 5) ("comm_select: FD %d ready for writing\n", fd);
7d49daab 1061 if (fd_table[fd].write_handler) {
ff8d0ea6 1062 hdl = fd_table[fd].write_handler;
7d49daab 1063 fd_table[fd].write_handler = 0;
ff8d0ea6 1064 hdl(fd, fd_table[fd].write_data);
090089c4 1065 }
7d49daab 1066 }
97c03d3c 1067 lastinc = polledinc;
090089c4 1068 }
7d49daab 1069 return COMM_OK;
97c03d3c 1070 } while (timeout > squid_curtime);
a3d5953d 1071 debug(5, 8) ("comm_select: time out: %d.\n", squid_curtime);
090089c4 1072 return COMM_TIMEOUT;
1073}
dcfe6390 1074#endif
090089c4 1075
b8d8561b 1076void
582b6456 1077commSetSelect(int fd, unsigned int type, PF * handler, void *client_data, time_t timeout)
090089c4 1078{
5c5783a2 1079 FD_ENTRY *fde;
489b22c1 1080 assert(fd >= 0);
5c5783a2 1081 fde = &fd_table[fd];
489b22c1 1082 debug(5,5)("commSetSelect: FD %d, handler=%p, data=%p\n", fd, handler, client_data);
090089c4 1083 if (type & COMM_SELECT_READ) {
4f92c80c 1084 fde->read_handler = handler;
1085 fde->read_data = client_data;
090089c4 1086 }
1087 if (type & COMM_SELECT_WRITE) {
4f92c80c 1088 fde->write_handler = handler;
1089 fde->write_data = client_data;
090089c4 1090 }
5c5783a2 1091 if (timeout)
4f92c80c 1092 fde->timeout = squid_curtime + timeout;
090089c4 1093}
1094
b8d8561b 1095void
582b6456 1096comm_add_close_handler(int fd, PF * handler, void *data)
30a4f2a8 1097{
1098 struct close_handler *new = xmalloc(sizeof(*new));
a3d5953d 1099 debug(5, 5) ("comm_add_close_handler: FD %d, handler=%p, data=%p\n",
e0c42e90 1100 fd, handler, data);
30a4f2a8 1101 new->handler = handler;
1102 new->data = data;
1103 new->next = fd_table[fd].close_handler;
1104 fd_table[fd].close_handler = new;
1105}
1106
b8d8561b 1107void
582b6456 1108comm_remove_close_handler(int fd, PF * handler, void *data)
090089c4 1109{
f88211e8 1110 struct close_handler *p;
1111 struct close_handler *last = NULL;
30a4f2a8 1112 /* Find handler in list */
1113 for (p = fd_table[fd].close_handler; p != NULL; last = p, p = p->next)
1114 if (p->handler == handler && p->data == data)
1115 break; /* This is our handler */
f88211e8 1116 assert(p != NULL);
30a4f2a8 1117 /* Remove list entry */
1118 if (last)
1119 last->next = p->next;
1120 else
1121 fd_table[fd].close_handler = p->next;
1122 safe_free(p);
1123}
090089c4 1124
b8d8561b 1125static void
1126commSetNoLinger(int fd)
30a4f2a8 1127{
1128 struct linger L;
090089c4 1129 L.l_onoff = 0; /* off */
1130 L.l_linger = 0;
30a4f2a8 1131 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &L, sizeof(L)) < 0)
a3d5953d 1132 debug(50, 0) ("commSetNoLinger: FD %d: %s\n", fd, xstrerror());
090089c4 1133}
1134
b8d8561b 1135static void
1136commSetReuseAddr(int fd)
090089c4 1137{
1138 int on = 1;
30a4f2a8 1139 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0)
a3d5953d 1140 debug(50, 1) ("commSetReuseAddr: FD %d: %s\n", fd, xstrerror());
090089c4 1141}
1142
b8d8561b 1143static void
1144commSetTcpRcvbuf(int fd, int size)
f868539a 1145{
1146 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size)) < 0)
a3d5953d 1147 debug(50, 1) ("commSetTcpRcvbuf: FD %d, SIZE %d: %s\n",
b6f794d6 1148 fd, size, xstrerror());
f868539a 1149}
1150
b8d8561b 1151int
1152commSetNonBlocking(int fd)
30a4f2a8 1153{
731e4d49 1154 int flags;
9e205701 1155 int dummy = 0;
95cf2361 1156 if ((flags = fcntl(fd, F_GETFL, dummy)) < 0) {
a3d5953d 1157 debug(50, 0) ("FD %d: fcntl F_GETFL: %s\n", fd, xstrerror());
731e4d49 1158 return COMM_ERROR;
1159 }
4f92c80c 1160 if (fcntl(fd, F_SETFL, flags | SQUID_NONBLOCK) < 0) {
a3d5953d 1161 debug(50, 0) ("commSetNonBlocking: FD %d: %s\n", fd, xstrerror());
30a4f2a8 1162 return COMM_ERROR;
090089c4 1163 }
090089c4 1164 return 0;
1165}
1166
b8d8561b 1167void
1168commSetCloseOnExec(int fd)
3ca60c86 1169{
1170#ifdef FD_CLOEXEC
731e4d49 1171 int flags;
1172 if ((flags = fcntl(fd, F_GETFL)) < 0) {
a3d5953d 1173 debug(50, 0) ("FD %d: fcntl F_GETFL: %s\n", fd, xstrerror());
24382924 1174 return;
3ca60c86 1175 }
24382924 1176 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
a3d5953d 1177 debug(50, 0) ("FD %d: set close-on-exec failed: %s\n", fd, xstrerror());
3ca60c86 1178#endif
1179}
1180
e90100aa 1181#ifdef TCP_NODELAY
1182static void
1183commSetTcpNoDelay(int fd)
1184{
1185 int on = 1;
1186 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) < 0)
a3d5953d 1187 debug(50, 1) ("commSetTcpNoDelay: FD %d: %s\n", fd, xstrerror());
e90100aa 1188}
1189#endif
1190
b8d8561b 1191int
0673c0ba 1192comm_init(void)
090089c4 1193{
e83892e9 1194 fd_table = xcalloc(Squid_MaxFD, sizeof(FD_ENTRY));
1195 meta_data.misc += Squid_MaxFD * sizeof(FD_ENTRY);
090089c4 1196 /* Keep a few file descriptors free so that we don't run out of FD's
1197 * after accepting a client but before it opens a socket or a file.
e83892e9 1198 * Since Squid_MaxFD can be as high as several thousand, don't waste them */
1199 RESERVED_FD = min(100, Squid_MaxFD / 4);
090089c4 1200 /* hardwired lifetimes */
e83892e9 1201 meta_data.misc += Squid_MaxFD * sizeof(int);
055f4d4d 1202 zero_tv.tv_sec = 0;
1203 zero_tv.tv_usec = 0;
090089c4 1204 return 0;
1205}
1206
1207
f88211e8 1208#if !HAVE_POLL
090089c4 1209/*
1210 * examine_select - debug routine.
1211 *
1212 * I spend the day chasing this core dump that occurs when both the client
1213 * and the server side of a cache fetch simultaneoulsy abort the
1214 * connection. While I haven't really studied the code to figure out how
1215 * it happens, the snippet below may prevent the cache from exitting:
1216 *
1217 * Call this from where the select loop fails.
1218 */
b8d8561b 1219static int
5742d7c9 1220examine_select(fd_set * readfds, fd_set * writefds)
090089c4 1221{
1222 int fd = 0;
bbc5ea8f 1223 fd_set read_x;
1224 fd_set write_x;
090089c4 1225 int num;
1226 struct timeval tv;
30a4f2a8 1227 struct close_handler *ch = NULL;
1228 struct close_handler *next = NULL;
5c5783a2 1229 FD_ENTRY *fde = NULL;
090089c4 1230
a3d5953d 1231 debug(5, 0) ("examine_select: Examining open file descriptors...\n");
e83892e9 1232 for (fd = 0; fd < Squid_MaxFD; fd++) {
090089c4 1233 FD_ZERO(&read_x);
1234 FD_ZERO(&write_x);
090089c4 1235 tv.tv_sec = tv.tv_usec = 0;
af00901c 1236 if (FD_ISSET(fd, readfds))
090089c4 1237 FD_SET(fd, &read_x);
af00901c 1238 else if (FD_ISSET(fd, writefds))
1239 FD_SET(fd, &write_x);
af00901c 1240 else
1241 continue;
e83892e9 1242 num = select(Squid_MaxFD, &read_x, &write_x, NULL, &tv);
af00901c 1243 if (num > -1) {
a3d5953d 1244 debug(5, 5) ("FD %d is valid.\n", fd);
af00901c 1245 continue;
1246 }
5c5783a2 1247 fde = &fd_table[fd];
a3d5953d 1248 debug(5, 0) ("FD %d: %s\n", fd, xstrerror());
1249 debug(5, 0) ("WARNING: FD %d has handlers, but it's invalid.\n", fd);
1250 debug(5, 0) ("FD %d is a %s called '%s'\n",
ca98227c 1251 fd,
95d15928 1252 fdstatTypeStr[fd_table[fd].type],
5c5783a2 1253 fde->desc);
a3d5953d 1254 debug(5, 0) ("tmout:%p read:%p write:%p\n",
5c5783a2 1255 fde->timeout_handler,
1256 fde->read_handler,
1257 fde->write_handler);
1258 for (ch = fde->close_handler; ch; ch = ch->next)
a3d5953d 1259 debug(5, 0) (" close handler: %p\n", ch->handler);
5c5783a2 1260 if (fde->close_handler) {
1261 for (ch = fde->close_handler; ch; ch = next) {
af00901c 1262 next = ch->next;
1263 ch->handler(fd, ch->data);
1264 safe_free(ch);
090089c4 1265 }
5c5783a2 1266 } else if (fde->timeout_handler) {
a3d5953d 1267 debug(5, 0) ("examine_select: Calling Timeout Handler\n");
5c5783a2 1268 fde->timeout_handler(fd, fde->timeout_data);
090089c4 1269 }
5c5783a2 1270 fde->close_handler = NULL;
1271 fde->timeout_handler = NULL;
1272 fde->read_handler = NULL;
1273 fde->write_handler = NULL;
af00901c 1274 FD_CLR(fd, readfds);
1275 FD_CLR(fd, writefds);
090089c4 1276 }
090089c4 1277 return 0;
1278}
dcfe6390 1279#endif
090089c4 1280
b8d8561b 1281static void
0673c0ba 1282checkTimeouts(void)
090089c4 1283{
1284 int fd;
9864ee44 1285 FD_ENTRY *fde = NULL;
5c5783a2 1286 PF *callback;
429fdbec 1287 for (fd = 0; fd <= Biggest_FD; fd++) {
1288 fde = &fd_table[fd];
5c5783a2 1289 if (fde->open != FD_OPEN)
429fdbec 1290 continue;
5c5783a2 1291 if (fde->timeout == 0)
30a4f2a8 1292 continue;
5c5783a2 1293 if (fde->timeout > squid_curtime)
30a4f2a8 1294 continue;
a3d5953d 1295 debug(5, 5) ("checkTimeouts: FD %d Expired\n", fd);
5c5783a2 1296 if (fde->timeout_handler) {
a3d5953d 1297 debug(5, 5) ("checkTimeouts: FD %d: Call timeout handler\n", fd);
5c5783a2 1298 callback = fde->timeout_handler;
1299 fde->timeout_handler = NULL;
1300 callback(fd, fde->timeout_data);
30a4f2a8 1301 } else {
a3d5953d 1302 debug(5, 5) ("checkTimeouts: FD %d: Forcing comm_close()\n", fd);
30a4f2a8 1303 comm_close(fd);
090089c4 1304 }
1305 }
1306}
1307
30a4f2a8 1308/* Write to FD. */
b8d8561b 1309static void
582b6456 1310commHandleWrite(int fd, void *data)
30a4f2a8 1311{
f17936ab 1312 CommWriteStateData *state = data;
30a4f2a8 1313 int len = 0;
1314 int nleft;
1315
a3d5953d 1316 debug(5, 5) ("commHandleWrite: FD %d: state=%p, off %d, sz %d.\n",
30a4f2a8 1317 fd, state, state->offset, state->size);
1318
1319 nleft = state->size - state->offset;
1320 len = write(fd, state->buf + state->offset, nleft);
b69f7771 1321 fd_bytes(fd, len, FD_WRITE);
30a4f2a8 1322
1323 if (len == 0) {
1324 /* Note we even call write if nleft == 0 */
1325 /* We're done */
1326 if (nleft != 0)
02be0294 1327 debug(5, 1) ("commHandleWrite: FD %d: write failure: connection closed with %d bytes remaining.\n", fd, nleft);
f17936ab 1328 CommWriteStateCallbackAndFree(fd, nleft ? COMM_ERROR : COMM_OK);
30a4f2a8 1329 } else if (len < 0) {
1330 /* An error */
0a0bf5db 1331 if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR) {
a3d5953d 1332 debug(50, 10) ("commHandleWrite: FD %d: write failure: %s.\n",
30a4f2a8 1333 fd, xstrerror());
b177367b 1334 commSetSelect(fd,
30a4f2a8 1335 COMM_SELECT_WRITE,
cd1fb0eb 1336 commHandleWrite,
b177367b 1337 state,
85d7ea98 1338 0);
9864ee44 1339 } else {
a3d5953d 1340 debug(50, 2) ("commHandleWrite: FD %d: write failure: %s.\n",
9864ee44 1341 fd, xstrerror());
f17936ab 1342 CommWriteStateCallbackAndFree(fd, COMM_ERROR);
30a4f2a8 1343 }
30a4f2a8 1344 } else {
1345 /* A successful write, continue */
1346 state->offset += len;
1347 if (state->offset < state->size) {
1348 /* Not done, reinstall the write handler and write some more */
b177367b 1349 commSetSelect(fd,
30a4f2a8 1350 COMM_SELECT_WRITE,
cd1fb0eb 1351 commHandleWrite,
b177367b 1352 state,
85d7ea98 1353 0);
9864ee44 1354 } else {
f17936ab 1355 CommWriteStateCallbackAndFree(fd, COMM_OK);
30a4f2a8 1356 }
30a4f2a8 1357 }
1358}
1359
1360
1361
1362/* Select for Writing on FD, until SIZE bytes are sent. Call
1363 * * HANDLER when complete. */
b8d8561b 1364void
9e4ad609 1365comm_write(int fd, char *buf, int size, CWCB * handler, void *handler_data, FREE * free_func)
30a4f2a8 1366{
f17936ab 1367 CommWriteStateData *state = NULL;
a3d5953d 1368 debug(5, 5) ("comm_write: FD %d: sz %d: hndl %p: data %p.\n",
787869c5 1369 fd, size, handler, handler_data);
03eb2f01 1370 assert(fd_table[fd].rwstate == NULL);
f17936ab 1371 state = xcalloc(1, sizeof(CommWriteStateData));
30a4f2a8 1372 state->buf = buf;
1373 state->size = size;
1374 state->offset = 0;
1375 state->handler = handler;
30a4f2a8 1376 state->handler_data = handler_data;
86ee2017 1377 state->free = free_func;
a56a3abe 1378 fd_table[fd].rwstate = state;
1a8f5ed6 1379 cbdataLock(handler_data);
b177367b 1380 commSetSelect(fd,
30a4f2a8 1381 COMM_SELECT_WRITE,
cd1fb0eb 1382 commHandleWrite,
b177367b 1383 fd_table[fd].rwstate,
1384 0);
30a4f2a8 1385}