]> git.ipfire.org Git - thirdparty/squid.git/blame - src/neighbors.cc
fix some of the header detection issues on FreeBSD
[thirdparty/squid.git] / src / neighbors.cc
CommitLineData
22f3fd98 1
30a4f2a8 2/*
3d7e9d7c 3 * $Id: neighbors.cc,v 1.305 2002/09/15 06:23:29 adrian Exp $
30a4f2a8 4 *
5 * DEBUG: section 15 Neighbor Routines
6 * AUTHOR: Harvest Derived
7 *
2b6662ba 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 9 * ----------------------------------------------------------
30a4f2a8 10 *
2b6662ba 11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
30a4f2a8 19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
cbdec147 32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 33 *
019dd986 34 */
090089c4 35
44a47c6e 36#include "squid.h"
090089c4 37
429fdbec 38/* count mcast group peers every 15 minutes */
39#define MCAST_COUNT_RATE 900
40
f5b8bbc4 41static int peerAllowedToUse(const peer *, request_t *);
f5b8bbc4 42static int peerWouldBePinged(const peer *, request_t *);
43static void neighborRemove(peer *);
f5b8bbc4 44static void neighborAlive(peer *, const MemObject *, const icp_common_t *);
399cabec 45#if USE_HTCP
46static void neighborAliveHtcp(peer *, const MemObject *, const htcpReplyData *);
47#endif
56b62ecc 48static void neighborCountIgnored(peer *);
f5b8bbc4 49static void peerRefreshDNS(void *);
b69f7771 50static IPH peerDNSConfigure;
eb406bb7 51static void peerProbeConnect(peer *);
52static IPH peerProbeConnect2;
53static CNCB peerProbeConnectDone;
f5b8bbc4 54static void peerCountMcastPeersDone(void *data);
55static void peerCountMcastPeersStart(void *data);
56static void peerCountMcastPeersSchedule(peer * p, time_t when);
b3264694 57static IRCB peerCountHandleIcpReply;
f5b8bbc4 58static void neighborIgnoreNonPeer(const struct sockaddr_in *, icp_opcode);
ed7f5615 59static OBJH neighborDumpPeers;
60static OBJH neighborDumpNonPeers;
61static void dump_peers(StoreEntry * sentry, peer * peers);
090089c4 62
090089c4 63static icp_common_t echo_hdr;
30a4f2a8 64static u_short echo_port;
30a4f2a8 65
c7a3724d 66static int NLateReplies = 0;
40a1495e 67static peer *first_ping = NULL;
28070024 68
a2c963ae 69const char *
b69f7771 70neighborTypeStr(const peer * p)
69a71bd7 71{
e102ebda 72 if (p->type == PEER_NONE)
73 return "Non-Peer";
b69f7771 74 if (p->type == PEER_SIBLING)
69a71bd7 75 return "Sibling";
b69f7771 76 if (p->type == PEER_MULTICAST)
7b3bd12c 77 return "Multicast Group";
69a71bd7 78 return "Parent";
79}
80
090089c4 81
d9f9d78b 82peer *
0cdcddb9 83whichPeer(const struct sockaddr_in * from)
090089c4 84{
22e4fa85 85 int j;
4eda6afe 86 u_short port = ntohs(from->sin_port);
87 struct in_addr ip = from->sin_addr;
b69f7771 88 peer *p = NULL;
a3d5953d 89 debug(15, 3) ("whichPeer: from %s port %d\n", inet_ntoa(ip), port);
40a1495e 90 for (p = Config.peers; p; p = p->next) {
b69f7771 91 for (j = 0; j < p->n_addresses; j++) {
399cabec 92 if (ip.s_addr == p->addresses[j].s_addr && port == p->icp.port) {
b69f7771 93 return p;
090089c4 94 }
95 }
96 }
4eda6afe 97 return NULL;
090089c4 98}
99
701a8572 100peer_t
b69f7771 101neighborType(const peer * p, const request_t * request)
24a1003d 102{
b012353a 103 const struct _domain_type *d = NULL;
b69f7771 104 for (d = p->typelist; d; d = d->next) {
7e3ce7b9 105 if (0 == matchDomainName(request->host, d->domain))
deb79f06 106 if (d->type != PEER_NONE)
b012353a 107 return d->type;
24a1003d 108 }
b69f7771 109 return p->type;
24a1003d 110}
111
7b3bd12c 112/*
62fd6124 113 * peerAllowedToUse
7b3bd12c 114 *
dd4bd44e 115 * this function figures out if it is appropriate to fetch REQUEST
62fd6124 116 * from PEER.
7b3bd12c 117 */
b8d8561b 118static int
b69f7771 119peerAllowedToUse(const peer * p, request_t * request)
090089c4 120{
b012353a 121 const struct _domain_ping *d = NULL;
090089c4 122 int do_ping = 1;
f88bb09c 123 aclCheck_t checklist;
ce66013b 124 assert(request != NULL);
9689d97c 125 if (neighborType(p, request) == PEER_SIBLING) {
126 if (request->flags.nocache)
24a1003d 127 return 0;
9689d97c 128 if (request->flags.refresh)
d950cec8 129 return 0;
9689d97c 130 if (request->flags.loopdetect)
131 return 0;
132 if (request->flags.need_validation)
133 return 0;
134 }
b6a2f15e 135 if (p->peer_domain == NULL && p->access == NULL)
090089c4 136 return do_ping;
090089c4 137 do_ping = 0;
b6a2f15e 138 for (d = p->peer_domain; d; d = d->next) {
7e3ce7b9 139 if (0 == matchDomainName(request->host, d->domain)) {
130faaed 140 do_ping = d->do_ping;
141 break;
142 }
30a4f2a8 143 do_ping = !d->do_ping;
144 }
b6a2f15e 145 if (p->peer_domain && 0 == do_ping)
130faaed 146 return do_ping;
505e35db 147 if (p->access == NULL)
148 return do_ping;
d20b1cd0 149 memset(&checklist, '\0', sizeof(checklist));
8ae8bb05 150 checklist.src_addr = request->client_addr;
3c11d1f5 151 checklist.my_addr = request->my_addr;
7e3ce7b9 152 checklist.my_port = request->my_port;
f88bb09c 153 checklist.request = request;
4749aee5 154#if 0 && USE_IDENT
155 /*
156 * this is currently broken because 'request->user_ident' has been
157 * moved to conn->rfc931 and we don't have access to the parent
158 * ConnStateData here.
159 */
94e7025e 160 if (request->user_ident[0])
4749aee5 161 xstrncpy(checklist.rfc931, request->user_ident, USER_IDENT_SZ);
94e7025e 162#endif
505e35db 163 return aclCheckFast(p->access, &checklist);
090089c4 164}
165
62fd6124 166/* Return TRUE if it is okay to send an ICP request to this peer. */
167static int
b69f7771 168peerWouldBePinged(const peer * p, request_t * request)
62fd6124 169{
b69f7771 170 if (!peerAllowedToUse(p, request))
62fd6124 171 return 0;
cd196bc8 172 if (p->options.no_query)
62fd6124 173 return 0;
d1b63fc8 174 if (p->options.background_ping && (squid_curtime - p->stats.last_query < Config.backgroundPingRate))
175 return 0;
cd196bc8 176 if (p->options.mcast_responder)
62fd6124 177 return 0;
83b381d5 178 if (p->n_addresses == 0)
179 return 0;
0e614a06 180 if (p->icp.port == 0)
181 return 0;
62fd6124 182 /* the case below seems strange, but can happen if the
183 * URL host is on the other side of a firewall */
b69f7771 184 if (p->type == PEER_SIBLING)
92695e5e 185 if (!request->flags.hierarchical)
62fd6124 186 return 0;
83b381d5 187 /* Ping dead peers every timeout interval */
188 if (squid_curtime - p->stats.last_query > Config.Timeout.deadPeer)
189 return 1;
d1b63fc8 190 if (p->icp.port == echo_port)
191 if (!neighborUp(p))
192 return 0;
62fd6124 193 return 1;
194}
195
196/* Return TRUE if it is okay to send an HTTP request to this peer. */
8ff4505b 197int
b69f7771 198peerHTTPOkay(const peer * p, request_t * request)
62fd6124 199{
b69f7771 200 if (!peerAllowedToUse(p, request))
62fd6124 201 return 0;
b69f7771 202 if (!neighborUp(p))
62fd6124 203 return 0;
c7f9eb6d 204 if (p->max_conn)
205 if (p->stats.conn_open >= p->max_conn)
206 return 0;
62fd6124 207 return 1;
208}
209
3c6e634f 210int
211neighborsCount(request_t * request)
090089c4 212{
b69f7771 213 peer *p = NULL;
090089c4 214 int count = 0;
40a1495e 215 for (p = Config.peers; p; p = p->next)
b69f7771 216 if (peerWouldBePinged(p, request))
3c6e634f 217 count++;
a3d5953d 218 debug(15, 3) ("neighborsCount: %d\n", count);
3c6e634f 219 return count;
220}
090089c4 221
002245f2 222#if UNUSED_CODE
deb79f06 223peer *
3c6e634f 224getSingleParent(request_t * request)
225{
deb79f06 226 peer *p = NULL;
b69f7771 227 peer *q = NULL;
40a1495e 228 for (q = Config.peers; q; q = q->next) {
b69f7771 229 if (!peerHTTPOkay(q, request))
caebbe00 230 continue;
b69f7771 231 if (neighborType(q, request) != PEER_PARENT)
3c6e634f 232 return NULL; /* oops, found SIBLING */
233 if (p)
234 return NULL; /* oops, found second parent */
b69f7771 235 p = q;
090089c4 236 }
cd196bc8 237 if (p != NULL && !p->options.no_query)
a369131d 238 return NULL;
a3d5953d 239 debug(15, 3) ("getSingleParent: returning %s\n", p ? p->host : "NULL");
3c6e634f 240 return p;
090089c4 241}
002245f2 242#endif
090089c4 243
deb79f06 244peer *
b8d8561b 245getFirstUpParent(request_t * request)
090089c4 246{
b69f7771 247 peer *p = NULL;
40a1495e 248 for (p = Config.peers; p; p = p->next) {
b69f7771 249 if (!neighborUp(p))
30a4f2a8 250 continue;
b69f7771 251 if (neighborType(p, request) != PEER_PARENT)
090089c4 252 continue;
b69f7771 253 if (!peerHTTPOkay(p, request))
e924600d 254 continue;
255 break;
090089c4 256 }
a3d5953d 257 debug(15, 3) ("getFirstUpParent: returning %s\n", p ? p->host : "NULL");
b69f7771 258 return p;
090089c4 259}
260
deb79f06 261peer *
48b38d01 262getRoundRobinParent(request_t * request)
263{
b69f7771 264 peer *p;
265 peer *q = NULL;
40a1495e 266 for (p = Config.peers; p; p = p->next) {
cd196bc8 267 if (!p->options.roundrobin)
0c8177e6 268 continue;
b69f7771 269 if (neighborType(p, request) != PEER_PARENT)
0c8177e6 270 continue;
b69f7771 271 if (!peerHTTPOkay(p, request))
48b38d01 272 continue;
b69f7771 273 if (q && q->rr_count < p->rr_count)
48b38d01 274 continue;
b69f7771 275 q = p;
48b38d01 276 }
b69f7771 277 if (q)
278 q->rr_count++;
a3d5953d 279 debug(15, 3) ("getRoundRobinParent: returning %s\n", q ? q->host : "NULL");
b69f7771 280 return q;
48b38d01 281}
282
d1b63fc8 283peer *
284getWeightedRoundRobinParent(request_t * request)
285{
286 peer *p;
287 peer *q = NULL;
288 int weighted_rtt;
289 for (p = Config.peers; p; p = p->next) {
290 if (!p->options.weighted_roundrobin)
291 continue;
292 if (neighborType(p, request) != PEER_PARENT)
293 continue;
294 if (!peerHTTPOkay(p, request))
295 continue;
296 if (q && q->rr_count < p->rr_count)
297 continue;
298 q = p;
299 }
300 if (q && q->rr_count > 1000000)
301 for (p = Config.peers; p; p = p->next) {
302 if (!p->options.weighted_roundrobin)
303 continue;
304 if (neighborType(p, request) != PEER_PARENT)
305 continue;
306 p->rr_count = 0;
307 }
308 if (q) {
309 weighted_rtt = (q->stats.rtt - q->basetime) / q->weight;
310
311 if (weighted_rtt < 1)
312 weighted_rtt = 1;
313 q->rr_count += weighted_rtt;
314 debug(15, 3) ("getWeightedRoundRobinParent: weighted_rtt %d\n", (int) weighted_rtt);
315 }
316 debug(15, 3) ("getWeightedRoundRobinParent: returning %s\n", q ? q->host : "NULL");
317 return q;
318}
319
82056f1e 320/* This gets called every 5 minutes to clear the round-robin counter. */
321void
322peerClearRR(void *data)
323{
324 peer *p = data;
325 p->rr_count -= p->rr_lastcount;
326 if (p->rr_count < 0)
327 p->rr_count = 0;
328 p->rr_lastcount = p->rr_count;
44a5f25e 329 eventAdd("peerClearRR", peerClearRR, p, 5 * 60.0, 0);
82056f1e 330}
331
deb79f06 332peer *
5269d0bd 333getDefaultParent(request_t * request)
334{
b69f7771 335 peer *p = NULL;
40a1495e 336 for (p = Config.peers; p; p = p->next) {
b69f7771 337 if (neighborType(p, request) != PEER_PARENT)
5269d0bd 338 continue;
cd196bc8 339 if (!p->options.default_parent)
5269d0bd 340 continue;
b69f7771 341 if (!peerHTTPOkay(p, request))
5269d0bd 342 continue;
a3d5953d 343 debug(15, 3) ("getDefaultParent: returning %s\n", p->host);
b69f7771 344 return p;
5269d0bd 345 }
669fefd4 346 debug(15, 3) ("getDefaultParent: returning NULL\n");
5269d0bd 347 return NULL;
348}
349
3c50f9bd 350/*
351 * XXX DW thinks this function is equivalent to/redundant with
352 * getFirstUpParent(). peerHTTPOkay() only returns true if the
353 * peer is UP anyway, so this function would not return a
354 * DOWN parent.
355 */
6a655df0 356peer *
357getAnyParent(request_t * request)
358{
359 peer *p = NULL;
360 for (p = Config.peers; p; p = p->next) {
361 if (neighborType(p, request) != PEER_PARENT)
362 continue;
363 if (!peerHTTPOkay(p, request))
364 continue;
365 debug(15, 3) ("getAnyParent: returning %s\n", p->host);
366 return p;
367 }
368 debug(15, 3) ("getAnyParent: returning NULL\n");
369 return NULL;
370}
371
deb79f06 372peer *
b69f7771 373getNextPeer(peer * p)
090089c4 374{
b69f7771 375 return p->next;
090089c4 376}
377
deb79f06 378peer *
379getFirstPeer(void)
090089c4 380{
40a1495e 381 return Config.peers;
090089c4 382}
383
b8d8561b 384static void
deb79f06 385neighborRemove(peer * target)
30a4f2a8 386{
b69f7771 387 peer *p = NULL;
388 peer **P = NULL;
40a1495e 389 p = Config.peers;
390 P = &Config.peers;
b69f7771 391 while (p) {
392 if (target == p)
30a4f2a8 393 break;
b69f7771 394 P = &p->next;
395 p = p->next;
396 }
397 if (p) {
398 *P = p->next;
b6a2f15e 399 cbdataFree(p);
40a1495e 400 Config.npeers--;
4d64d74a 401 }
40a1495e 402 first_ping = Config.peers;
4d64d74a 403}
404
b8d8561b 405void
406neighbors_open(int fd)
090089c4 407{
30a4f2a8 408 struct sockaddr_in name;
6637e3a5 409 socklen_t len = sizeof(struct sockaddr_in);
30a4f2a8 410 struct servent *sep = NULL;
b263bd3f 411 const char *me = getMyHostname();
412 peer *this;
413 peer *next;
30a4f2a8 414 memset(&name, '\0', sizeof(struct sockaddr_in));
415 if (getsockname(fd, (struct sockaddr *) &name, &len) < 0)
a3d5953d 416 debug(15, 1) ("getsockname(%d,%p,%p) failed.\n", fd, &name, &len);
b263bd3f 417 for (this = Config.peers; this; this = next) {
418 sockaddr_in_list *s;
419 next = this->next;
420 if (0 != strcmp(this->host, me))
421 continue;
422 for (s = Config.Sockaddr.http; s; s = s->next) {
423 if (this->http_port != ntohs(s->s.sin_port))
424 continue;
425 debug(15, 1) ("WARNING: Peer looks like this host\n");
426 debug(15, 1) (" Ignoring %s %s/%d/%d\n",
427 neighborTypeStr(this), this->host, this->http_port,
428 this->icp.port);
429 neighborRemove(this);
430 }
431 }
432
5999b776 433 peerRefreshDNS((void *) 1);
30a4f2a8 434 if (0 == echo_hdr.opcode) {
27cd7235 435 echo_hdr.opcode = ICP_SECHO;
30a4f2a8 436 echo_hdr.version = ICP_VERSION_CURRENT;
437 echo_hdr.length = 0;
438 echo_hdr.reqnum = 0;
439 echo_hdr.flags = 0;
440 echo_hdr.pad = 0;
30a4f2a8 441 echo_hdr.shostid = name.sin_addr.s_addr;
442 sep = getservbyname("echo", "udp");
443 echo_port = sep ? ntohs((u_short) sep->s_port) : 7;
090089c4 444 }
c68e4e90 445 first_ping = Config.peers;
ed7f5615 446 cachemgrRegister("server_list",
447 "Peer Cache Statistics",
1da3b90b 448 neighborDumpPeers, 0, 1);
22f3fd98 449 cachemgrRegister("non_peers",
450 "List of Unknown sites sending ICP messages",
1da3b90b 451 neighborDumpNonPeers, 0, 1);
090089c4 452}
453
b8d8561b 454int
b6c0e933 455neighborsUdpPing(request_t * request,
641941c0 456 StoreEntry * entry,
582b6456 457 IRCB * callback,
641941c0 458 void *callback_data,
52040193 459 int *exprep,
465dc415 460 int *timeout)
090089c4 461{
9fb13bb6 462 const char *url = storeUrl(entry);
b6c0e933 463 MemObject *mem = entry->mem_obj;
b69f7771 464 peer *p = NULL;
090089c4 465 int i;
1061b406 466 int reqnum = 0;
6d2296d4 467 int flags;
9dee2904 468 icp_common_t *query;
429fdbec 469 int queries_sent = 0;
0a0bf5db 470 int peers_pinged = 0;
a8c926ff 471 int parent_timeout = 0, parent_exprep = 0;
472 int sibling_timeout = 0, sibling_exprep = 0;
090089c4 473
40a1495e 474 if (Config.peers == NULL)
090089c4 475 return 0;
ce66013b 476 if (theOutIcpConnection < 0)
477 fatal("neighborsUdpPing: There is no ICP socket!");
9fb13bb6 478 assert(entry->swap_status == SWAPOUT_NONE);
b6c0e933 479 mem->start_ping = current_time;
b4e7f82d 480 mem->ping_reply_callback = callback;
b6c0e933 481 mem->ircb_data = callback_data;
186477c1 482 reqnum = icpSetCacheKey(entry->hash.key);
40a1495e 483 for (i = 0, p = first_ping; i++ < Config.npeers; p = p->next) {
b69f7771 484 if (p == NULL)
40a1495e 485 p = Config.peers;
a3d5953d 486 debug(15, 5) ("neighborsUdpPing: Peer %s\n", p->host);
b69f7771 487 if (!peerWouldBePinged(p, request))
deb79f06 488 continue; /* next peer */
0a0bf5db 489 peers_pinged++;
a3d5953d 490 debug(15, 4) ("neighborsUdpPing: pinging peer %s for '%s'\n",
b69f7771 491 p->host, url);
492 if (p->type == PEER_MULTICAST)
03a1ee42 493 mcastSetTtl(theOutIcpConnection, p->mcast.ttl);
186477c1 494 debug(15, 3) ("neighborsUdpPing: key = '%s'\n", storeKeyText(entry->hash.key));
a3d5953d 495 debug(15, 3) ("neighborsUdpPing: reqnum = %d\n", reqnum);
090089c4 496
dc9d133b 497#if USE_HTCP
cd196bc8 498 if (p->options.htcp) {
1994bb24 499 debug(15, 3) ("neighborsUdpPing: sending HTCP query\n");
176ae152 500 htcpQuery(entry, request, p);
dc9d133b 501 } else
502#endif
399cabec 503 if (p->icp.port == echo_port) {
a3d5953d 504 debug(15, 4) ("neighborsUdpPing: Looks like a dumb cache, send DECHO ping\n");
1061b406 505 echo_hdr.reqnum = reqnum;
27cd7235 506 query = icpCreateMessage(ICP_DECHO, 0, url, reqnum, 0);
af00901c 507 icpUdpSend(theOutIcpConnection,
b69f7771 508 &p->in_addr,
9dee2904 509 query,
721b3dd8 510 LOG_ICP_QUERY,
aee4e794 511 0);
090089c4 512 } else {
6d2296d4 513 flags = 0;
17a0a4ee 514 if (Config.onoff.query_icmp)
399cabec 515 if (p->icp.version == ICP_VERSION_2)
429fdbec 516 flags |= ICP_FLAG_SRC_RTT;
27cd7235 517 query = icpCreateMessage(ICP_QUERY, flags, url, reqnum, 0);
af00901c 518 icpUdpSend(theOutIcpConnection,
b69f7771 519 &p->in_addr,
9dee2904 520 query,
721b3dd8 521 LOG_ICP_QUERY,
aee4e794 522 0);
090089c4 523 }
429fdbec 524 queries_sent++;
090089c4 525
b69f7771 526 p->stats.pings_sent++;
b69f7771 527 if (p->type == PEER_MULTICAST) {
dc835977 528 /*
529 * set a bogus last_reply time so neighborUp() never
530 * says a multicast peer is dead.
531 */
532 p->stats.last_reply = squid_curtime;
b69f7771 533 (*exprep) += p->mcast.n_replies_expected;
47b780e1 534 } else if (neighborUp(p)) {
535 /* its alive, expect a reply from it */
a8c926ff 536 if (neighborType(p, request) == PEER_PARENT) {
537 parent_exprep++;
538 parent_timeout += p->stats.rtt;
539 } else {
540 sibling_exprep++;
541 sibling_timeout += p->stats.rtt;
542 }
090089c4 543 } else {
1061b406 544 /* Neighbor is dead; ping it anyway, but don't expect a reply */
090089c4 545 /* log it once at the threshold */
dc835977 546 if (p->stats.logged_state == PEER_ALIVE) {
ab4b78e9 547 debug(15, 1) ("Detected DEAD %s: %s/%d/%d\n",
b69f7771 548 neighborTypeStr(p),
399cabec 549 p->host, p->http_port, p->icp.port);
dc835977 550 p->stats.logged_state = PEER_DEAD;
090089c4 551 }
552 }
dc835977 553 p->stats.last_query = squid_curtime;
83b381d5 554 if (p->stats.probe_start == 0)
555 p->stats.probe_start = squid_curtime;
090089c4 556 }
40a1495e 557 if ((first_ping = first_ping->next) == NULL)
558 first_ping = Config.peers;
090089c4 559
0ab965da 560#if ALLOW_SOURCE_PING
090089c4 561 /* only do source_ping if we have neighbors */
40a1495e 562 if (Config.npeers) {
a369131d 563 const ipcache_addrs *ia = NULL;
564 struct sockaddr_in to_addr;
565 char *host = request->host;
17a0a4ee 566 if (!Config.onoff.source_ping) {
a3d5953d 567 debug(15, 6) ("neighborsUdpPing: Source Ping is disabled.\n");
0a0bf5db 568 } else if ((ia = ipcache_gethostbyname(host, 0))) {
a3d5953d 569 debug(15, 6) ("neighborsUdpPing: Source Ping: to %s for '%s'\n",
1061b406 570 host, url);
1061b406 571 echo_hdr.reqnum = reqnum;
16b204c4 572 if (icmp_sock != -1) {
e5f6c5c2 573 icmpSourcePing(ia->in_addrs[ia->cur], &echo_hdr, url);
16b204c4 574 } else {
575 to_addr.sin_family = AF_INET;
e5f6c5c2 576 to_addr.sin_addr = ia->in_addrs[ia->cur];
16b204c4 577 to_addr.sin_port = htons(echo_port);
27cd7235 578 query = icpCreateMessage(ICP_SECHO, 0, url, reqnum, 0);
16b204c4 579 icpUdpSend(theOutIcpConnection,
16b204c4 580 &to_addr,
9dee2904 581 query,
721b3dd8 582 LOG_ICP_QUERY,
aee4e794 583 0);
16b204c4 584 }
090089c4 585 } else {
a3d5953d 586 debug(15, 6) ("neighborsUdpPing: Source Ping: unknown host: %s\n",
1061b406 587 host);
090089c4 588 }
589 }
429fdbec 590#endif
a8c926ff 591 /*
592 * How many replies to expect?
593 */
594 *exprep = parent_exprep + sibling_exprep;
595
52040193 596 /*
465dc415 597 * If there is a configured timeout, use it
52040193 598 */
465dc415 599 if (Config.Timeout.icp_query)
bb337ad7 600 *timeout = Config.Timeout.icp_query;
28993292 601 else {
a8c926ff 602 if (*exprep > 0) {
603 if (parent_exprep)
604 *timeout = 2 * parent_timeout / parent_exprep;
605 else
606 *timeout = 2 * sibling_timeout / sibling_exprep;
607 } else
28993292 608 *timeout = 2000; /* 2 seconds */
609 if (Config.Timeout.icp_query_max)
610 if (*timeout > Config.Timeout.icp_query_max)
611 *timeout = Config.Timeout.icp_query_max;
43af4d27 612 if (*timeout < Config.Timeout.icp_query_min)
613 *timeout = Config.Timeout.icp_query_min;
28993292 614 }
0a0bf5db 615 return peers_pinged;
090089c4 616}
617
26b164ac 618/* lookup the digest of a given peer */
619lookup_t
f66a9ef4 620peerDigestLookup(peer * p, request_t * request)
26b164ac 621{
6cfa8966 622#if USE_CACHE_DIGESTS
f66a9ef4 623 const cache_key *key = request ? storeKeyPublicByRequest(request) : NULL;
26b164ac 624 assert(p);
0c511722 625 assert(request);
69c95dd3 626 debug(15, 5) ("peerDigestLookup: peer %s\n", p->host);
4b4cd312 627 /* does the peeer have a valid digest? */
e13ee7ad 628 if (!p->digest) {
629 debug(15, 5) ("peerDigestLookup: gone!\n");
26b164ac 630 return LOOKUP_NONE;
9689d97c 631 } else if (!peerHTTPOkay(p, request)) {
e13ee7ad 632 debug(15, 5) ("peerDigestLookup: !peerHTTPOkay\n");
26b164ac 633 return LOOKUP_NONE;
e13ee7ad 634 } else if (p->digest->flags.usable) {
635 debug(15, 5) ("peerDigestLookup: usable\n");
4b4cd312 636 /* fall through; put here to have common case on top */ ;
e13ee7ad 637 } else if (!p->digest->flags.needed) {
638 debug(15, 5) ("peerDigestLookup: note need\n");
639 peerDigestNeeded(p->digest);
26b164ac 640 return LOOKUP_NONE;
641 } else {
e13ee7ad 642 debug(15, 5) ("peerDigestLookup: !ready && %srequested\n",
643 p->digest->flags.requested ? "" : "!");
26b164ac 644 return LOOKUP_NONE;
645 }
69c95dd3 646 debug(15, 5) ("peerDigestLookup: OK to lookup peer %s\n", p->host);
e13ee7ad 647 assert(p->digest->cd);
26b164ac 648 /* does digest predict a hit? */
e13ee7ad 649 if (!cacheDigestTest(p->digest->cd, key))
26b164ac 650 return LOOKUP_MISS;
69c95dd3 651 debug(15, 5) ("peerDigestLookup: peer %s says HIT!\n", p->host);
26b164ac 652 return LOOKUP_HIT;
653#endif
654 return LOOKUP_NONE;
655}
656
657/* select best peer based on cache digests */
a54d3f8e 658peer *
f66a9ef4 659neighborsDigestSelect(request_t * request)
a54d3f8e 660{
661 peer *best_p = NULL;
6cfa8966 662#if USE_CACHE_DIGESTS
a54d3f8e 663 const cache_key *key;
664 int best_rtt = 0;
665 int choice_count = 0;
666 int ichoice_count = 0;
667 peer *p;
668 int p_rtt;
669 int i;
03058802 670 if (!request->flags.hierarchical)
671 return NULL;
f66a9ef4 672 key = storeKeyPublicByRequest(request);
a54d3f8e 673 for (i = 0, p = first_ping; i++ < Config.npeers; p = p->next) {
26b164ac 674 lookup_t lookup;
a54d3f8e 675 if (!p)
676 p = Config.peers;
677 if (i == 1)
678 first_ping = p;
f66a9ef4 679 lookup = peerDigestLookup(p, request);
26b164ac 680 if (lookup == LOOKUP_NONE)
a54d3f8e 681 continue;
a54d3f8e 682 choice_count++;
26b164ac 683 if (lookup == LOOKUP_MISS)
a54d3f8e 684 continue;
26b164ac 685 p_rtt = netdbHostRtt(p->host);
686 debug(15, 5) ("neighborsDigestSelect: peer %s rtt: %d\n",
a54d3f8e 687 p->host, p_rtt);
688 /* is this peer better than others in terms of rtt ? */
689 if (!best_p || (p_rtt && p_rtt < best_rtt)) {
690 best_p = p;
691 best_rtt = p_rtt;
4b4cd312 692 if (p_rtt) /* informative choice (aka educated guess) */
a54d3f8e 693 ichoice_count++;
694 debug(15, 4) ("neighborsDigestSelect: peer %s leads with rtt %d\n",
695 p->host, best_rtt);
696 }
697 }
26b164ac 698 debug(15, 4) ("neighborsDigestSelect: choices: %d (%d)\n",
699 choice_count, ichoice_count);
700 peerNoteDigestLookup(request, best_p,
701 best_p ? LOOKUP_HIT : (choice_count ? LOOKUP_MISS : LOOKUP_NONE));
a54d3f8e 702 request->hier.n_choices = choice_count;
703 request->hier.n_ichoices = ichoice_count;
704#endif
705 return best_p;
706}
707
26b164ac 708void
4b4cd312 709peerNoteDigestLookup(request_t * request, peer * p, lookup_t lookup)
26b164ac 710{
6cfa8966 711#if USE_CACHE_DIGESTS
26b164ac 712 if (p)
713 strncpy(request->hier.cd_host, p->host, sizeof(request->hier.cd_host));
714 else
715 *request->hier.cd_host = '\0';
716 request->hier.cd_lookup = lookup;
717 debug(15, 4) ("peerNoteDigestLookup: peer %s, lookup: %s\n",
718 p ? p->host : "<none>", lookup_t_str[lookup]);
719#endif
720}
721
d73844ca 722static void
b69f7771 723neighborAlive(peer * p, const MemObject * mem, const icp_common_t * header)
4eda6afe 724{
39ac34a9 725 if (p->stats.logged_state == PEER_DEAD && p->tcp_up) {
ab4b78e9 726 debug(15, 1) ("Detected REVIVED %s: %s/%d/%d\n",
b69f7771 727 neighborTypeStr(p),
399cabec 728 p->host, p->http_port, p->icp.port);
dc835977 729 p->stats.logged_state = PEER_ALIVE;
4eda6afe 730 }
dc835977 731 p->stats.last_reply = squid_curtime;
83b381d5 732 p->stats.probe_start = 0;
44f2f2e4 733 p->stats.pings_acked++;
27cd7235 734 if ((icp_opcode) header->opcode <= ICP_END)
399cabec 735 p->icp.counts[header->opcode]++;
44f2f2e4 736 p->icp.version = (int) header->version;
737}
738
739static void
740neighborUpdateRtt(peer * p, MemObject * mem)
741{
d1b63fc8 742 int rtt, rtt_av_factor;
d2cd6935 743 if (!mem)
744 return;
44f2f2e4 745 if (!mem->start_ping.tv_sec)
746 return;
747 rtt = tvSubMsec(mem->start_ping, current_time);
1b6ef2d2 748 if (rtt < 1 || rtt > 10000)
749 return;
d1b63fc8 750 rtt_av_factor = RTT_AV_FACTOR;
751 if (p->options.weighted_roundrobin)
752 rtt_av_factor = RTT_BACKGROUND_AV_FACTOR;
1b6ef2d2 753 p->stats.rtt = intAverage(p->stats.rtt, rtt,
d1b63fc8 754 p->stats.pings_acked, rtt_av_factor);
4eda6afe 755}
090089c4 756
399cabec 757#if USE_HTCP
758static void
759neighborAliveHtcp(peer * p, const MemObject * mem, const htcpReplyData * htcp)
760{
399cabec 761 if (p->stats.logged_state == PEER_DEAD && p->tcp_up) {
762 debug(15, 1) ("Detected REVIVED %s: %s/%d/%d\n",
763 neighborTypeStr(p),
764 p->host, p->http_port, p->icp.port);
765 p->stats.logged_state = PEER_ALIVE;
766 }
767 p->stats.last_reply = squid_curtime;
83b381d5 768 p->stats.probe_start = 0;
44f2f2e4 769 p->stats.pings_acked++;
399cabec 770 p->htcp.counts[htcp->hit ? 1 : 0]++;
44f2f2e4 771 p->htcp.version = htcp->version;
399cabec 772}
773#endif
774
38792624 775static void
56b62ecc 776neighborCountIgnored(peer * p)
a7e59001 777{
b69f7771 778 if (p == NULL)
a7e59001 779 return;
b69f7771 780 p->stats.ignored_replies++;
c7a3724d 781 NLateReplies++;
a7e59001 782}
783
e102ebda 784static peer *non_peers = NULL;
785
786static void
787neighborIgnoreNonPeer(const struct sockaddr_in *from, icp_opcode opcode)
788{
789 peer *np;
e102ebda 790 for (np = non_peers; np; np = np->next) {
791 if (np->in_addr.sin_addr.s_addr != from->sin_addr.s_addr)
792 continue;
793 if (np->in_addr.sin_port != from->sin_port)
794 continue;
795 break;
796 }
797 if (np == NULL) {
798 np = xcalloc(1, sizeof(peer));
799 np->in_addr.sin_addr = from->sin_addr;
800 np->in_addr.sin_port = from->sin_port;
399cabec 801 np->icp.port = ntohl(from->sin_port);
e102ebda 802 np->type = PEER_NONE;
803 np->host = xstrdup(inet_ntoa(from->sin_addr));
804 np->next = non_peers;
805 non_peers = np;
806 }
399cabec 807 np->icp.counts[opcode]++;
e4cc2fdf 808 if (isPowTen(++np->stats.ignored_replies))
809 debug(15, 1) ("WARNING: Ignored %d replies from non-peer %s\n",
810 np->stats.ignored_replies, np->host);
e102ebda 811}
812
429fdbec 813/* ignoreMulticastReply
814 *
d6827718 815 * * We want to ignore replies from multicast peers if the
816 * * cache_host_domain rules would normally prevent the peer
817 * * from being used
429fdbec 818 */
819static int
b69f7771 820ignoreMulticastReply(peer * p, MemObject * mem)
429fdbec 821{
b69f7771 822 if (p == NULL)
429fdbec 823 return 0;
cd196bc8 824 if (!p->options.mcast_responder)
429fdbec 825 return 0;
b69f7771 826 if (peerHTTPOkay(p, mem->request))
429fdbec 827 return 0;
828 return 1;
829}
830
831/* I should attach these records to the entry. We take the first
832 * hit we get our wait until everyone misses. The timeout handler
833 * call needs to nip this shopping list or call one of the misses.
834 *
835 * If a hit process is already started, then sobeit
836 */
b8d8561b 837void
5ad33356 838neighborsUdpAck(const cache_key * key, icp_common_t * header, const struct sockaddr_in *from)
090089c4 839{
b69f7771 840 peer *p = NULL;
5ad33356 841 StoreEntry *entry;
842 MemObject *mem = NULL;
b6c0e933 843 peer_t ntype = PEER_NONE;
5e5126cc 844 char *opcode_d;
a7e59001 845 icp_opcode opcode = (icp_opcode) header->opcode;
090089c4 846
5ad33356 847 debug(15, 6) ("neighborsUdpAck: opcode %d '%s'\n",
848 (int) opcode, storeKeyText(key));
849 if (NULL != (entry = storeGet(key)))
850 mem = entry->mem_obj;
b69f7771 851 if ((p = whichPeer(from)))
852 neighborAlive(p, mem, header);
27cd7235 853 if (opcode > ICP_END)
d2af9477 854 return;
27cd7235 855 opcode_d = icp_opcode_str[opcode];
41587298 856 if (p)
857 neighborUpdateRtt(p, mem);
5ad33356 858 /* Does the entry exist? */
859 if (NULL == entry) {
860 debug(12, 3) ("neighborsUdpAck: Cache key '%s' not found\n",
861 storeKeyText(key));
56b62ecc 862 neighborCountIgnored(p);
5ad33356 863 return;
864 }
2d1c6a4f 865 /* check if someone is already fetching it */
d46a87a8 866 if (EBIT_TEST(entry->flags, ENTRY_DISPATCHED)) {
5ad33356 867 debug(15, 3) ("neighborsUdpAck: '%s' already being fetched.\n",
868 storeKeyText(key));
56b62ecc 869 neighborCountIgnored(p);
d2af9477 870 return;
871 }
2d1c6a4f 872 if (mem == NULL) {
5ad33356 873 debug(15, 2) ("Ignoring %s for missing mem_obj: %s\n",
874 opcode_d, storeKeyText(key));
56b62ecc 875 neighborCountIgnored(p);
8de2f7ad 876 return;
877 }
878 if (entry->ping_status != PING_WAITING) {
674ac814 879 debug(15, 2) ("neighborsUdpAck: Late %s for %s\n",
5ad33356 880 opcode_d, storeKeyText(key));
56b62ecc 881 neighborCountIgnored(p);
8de2f7ad 882 return;
883 }
4eda6afe 884 if (entry->lock_count == 0) {
5ad33356 885 debug(12, 1) ("neighborsUdpAck: '%s' has no locks\n",
886 storeKeyText(key));
56b62ecc 887 neighborCountIgnored(p);
4eda6afe 888 return;
090089c4 889 }
a3d5953d 890 debug(15, 3) ("neighborsUdpAck: %s for '%s' from %s \n",
5ad33356 891 opcode_d, storeKeyText(key), p ? p->host : "source");
44f2f2e4 892 if (p) {
b69f7771 893 ntype = neighborType(p, mem->request);
44f2f2e4 894 }
b69f7771 895 if (ignoreMulticastReply(p, mem)) {
56b62ecc 896 neighborCountIgnored(p);
27cd7235 897 } else if (opcode == ICP_MISS) {
b69f7771 898 if (p == NULL) {
e102ebda 899 neighborIgnoreNonPeer(from, opcode);
d2af9477 900 } else {
b4e7f82d 901 mem->ping_reply_callback(p, ntype, PROTO_ICP, header, mem->ircb_data);
30a4f2a8 902 }
a7c05555 903 } else if (opcode == ICP_HIT) {
b69f7771 904 if (p == NULL) {
e102ebda 905 neighborIgnoreNonPeer(from, opcode);
d2af9477 906 } else {
27cd7235 907 header->opcode = ICP_HIT;
b4e7f82d 908 mem->ping_reply_callback(p, ntype, PROTO_ICP, header, mem->ircb_data);
090089c4 909 }
27cd7235 910 } else if (opcode == ICP_DECHO) {
b69f7771 911 if (p == NULL) {
e102ebda 912 neighborIgnoreNonPeer(from, opcode);
deb79f06 913 } else if (ntype == PEER_SIBLING) {
5e604a0e 914 debug_trap("neighborsUdpAck: Found non-ICP cache as SIBLING\n");
915 debug_trap("neighborsUdpAck: non-ICP neighbors must be a PARENT\n");
090089c4 916 } else {
b4e7f82d 917 mem->ping_reply_callback(p, ntype, PROTO_ICP, header, mem->ircb_data);
090089c4 918 }
27cd7235 919 } else if (opcode == ICP_SECHO) {
b69f7771 920 if (p) {
a3d5953d 921 debug(15, 1) ("Ignoring SECHO from neighbor %s\n", p->host);
56b62ecc 922 neighborCountIgnored(p);
0ab965da 923#if ALLOW_SOURCE_PING
924 } else if (Config.onoff.source_ping) {
b4e7f82d 925 mem->ping_reply_callback(NULL, ntype, PROTO_ICP, header, mem->ircb_data);
0ab965da 926#endif
927 } else {
928 debug(15, 1) ("Unsolicited SECHO from %s\n", inet_ntoa(from->sin_addr));
090089c4 929 }
27cd7235 930 } else if (opcode == ICP_DENIED) {
b69f7771 931 if (p == NULL) {
e102ebda 932 neighborIgnoreNonPeer(from, opcode);
b69f7771 933 } else if (p->stats.pings_acked > 100) {
399cabec 934 if (100 * p->icp.counts[ICP_DENIED] / p->stats.pings_acked > 95) {
a3d5953d 935 debug(15, 0) ("95%% of replies from '%s' are UDP_DENIED\n", p->host);
936 debug(15, 0) ("Disabling '%s', please check your configuration.\n", p->host);
b69f7771 937 neighborRemove(p);
938 p = NULL;
e006d372 939 } else {
56b62ecc 940 neighborCountIgnored(p);
30a4f2a8 941 }
942 }
27cd7235 943 } else if (opcode == ICP_MISS_NOFETCH) {
b4e7f82d 944 mem->ping_reply_callback(p, ntype, PROTO_ICP, header, mem->ircb_data);
090089c4 945 } else {
a3d5953d 946 debug(15, 0) ("neighborsUdpAck: Unexpected ICP reply: %s\n", opcode_d);
d2af9477 947 }
090089c4 948}
949
deb79f06 950peer *
40a1495e 951peerFindByName(const char *name)
98ffb7e4 952{
b69f7771 953 peer *p = NULL;
40a1495e 954 for (p = Config.peers; p; p = p->next) {
b69f7771 955 if (!strcasecmp(name, p->host))
98ffb7e4 956 break;
957 }
b69f7771 958 return p;
98ffb7e4 959}
b012353a 960
db1cd23c 961peer *
962peerFindByNameAndPort(const char *name, unsigned short port)
963{
964 peer *p = NULL;
965 for (p = Config.peers; p; p = p->next) {
966 if (strcasecmp(name, p->host))
967 continue;
968 if (port != p->http_port)
969 continue;
970 break;
971 }
972 return p;
973}
974
5269d0bd 975int
b69f7771 976neighborUp(const peer * p)
5269d0bd 977{
eb406bb7 978 if (!p->tcp_up) {
a4b8110e 979 peerProbeConnect((peer *) p);
03b82057 980 return 0;
eb406bb7 981 }
83b381d5 982 if (p->options.no_query)
03b82057 983 return 1;
83b381d5 984 if (p->stats.probe_start != 0 &&
a4b8110e 985 squid_curtime - p->stats.probe_start > Config.Timeout.deadPeer)
03b82057 986 return 0;
64b8103b 987 /*
988 * The peer can not be UP if we don't have any IP addresses
186477c1 989 * for it.
64b8103b 990 */
991 if (0 == p->n_addresses)
992 return 0;
03b82057 993 return 1;
5269d0bd 994}
995
e6e3b09b 996void
28c60158 997peerDestroy(void *data)
e6e3b09b 998{
db1cd23c 999 peer *p = data;
ee4a1f5d 1000 struct _domain_ping *l = NULL;
1001 struct _domain_ping *nl = NULL;
b69f7771 1002 if (p == NULL)
3c6e634f 1003 return;
b6a2f15e 1004 for (l = p->peer_domain; l; l = nl) {
ee4a1f5d 1005 nl = l->next;
1006 safe_free(l->domain);
1007 safe_free(l);
1008 }
b69f7771 1009 safe_free(p->host);
e13ee7ad 1010#if USE_CACHE_DIGESTS
fa80a8ef 1011 cbdataReferenceDone(p->digest);
e13ee7ad 1012#endif
e6e3b09b 1013}
c7a3724d 1014
e13ee7ad 1015void
e16d4250 1016peerNoteDigestGone(peer * p)
e13ee7ad 1017{
1018#if USE_CACHE_DIGESTS
fa80a8ef 1019 cbdataReferenceDone(p->digest);
e13ee7ad 1020#endif
1021}
1022
dd4bd44e 1023static void
03a1ee42 1024peerDNSConfigure(const ipcache_addrs * ia, void *data)
dd4bd44e 1025{
b69f7771 1026 peer *p = data;
dd4bd44e 1027 struct sockaddr_in *ap;
1028 int j;
b69f7771 1029 if (p->n_addresses == 0) {
a3d5953d 1030 debug(15, 1) ("Configuring %s %s/%d/%d\n", neighborTypeStr(p),
399cabec 1031 p->host, p->http_port, p->icp.port);
b69f7771 1032 if (p->type == PEER_MULTICAST)
a3d5953d 1033 debug(15, 1) (" Multicast TTL = %d\n", p->mcast.ttl);
b69f7771 1034 }
1035 p->n_addresses = 0;
dd4bd44e 1036 if (ia == NULL) {
a3d5953d 1037 debug(0, 0) ("WARNING: DNS lookup for '%s' failed!\n", p->host);
dd4bd44e 1038 return;
1039 }
c04fe656 1040 if ((int) ia->count < 1) {
a3d5953d 1041 debug(0, 0) ("WARNING: No IP address found for '%s'!\n", p->host);
dd4bd44e 1042 return;
1043 }
c04fe656 1044 for (j = 0; j < (int) ia->count && j < PEER_MAX_ADDRESSES; j++) {
b69f7771 1045 p->addresses[j] = ia->in_addrs[j];
a3d5953d 1046 debug(15, 2) ("--> IP address #%d: %s\n", j, inet_ntoa(p->addresses[j]));
b69f7771 1047 p->n_addresses++;
dd4bd44e 1048 }
b69f7771 1049 ap = &p->in_addr;
dd4bd44e 1050 memset(ap, '\0', sizeof(struct sockaddr_in));
1051 ap->sin_family = AF_INET;
b69f7771 1052 ap->sin_addr = p->addresses[0];
399cabec 1053 ap->sin_port = htons(p->icp.port);
b69f7771 1054 if (p->type == PEER_MULTICAST)
1055 peerCountMcastPeersSchedule(p, 10);
1a827bc0 1056 if (p->type != PEER_MULTICAST)
cd196bc8 1057 if (!p->options.no_netdb_exchange)
223213df 1058 eventAddIsh("netdbExchangeStart", netdbExchangeStart, p, 30.0, 1);
dd4bd44e 1059}
1060
1061static void
51242273 1062peerRefreshDNS(void *data)
dd4bd44e 1063{
b69f7771 1064 peer *p = NULL;
46ca5fc6 1065 if (eventFind(peerRefreshDNS, NULL))
73cb7e3a 1066 eventDelete(peerRefreshDNS, NULL);
51242273 1067 if (!data && 0 == stat5minClientRequests()) {
1f3c4622 1068 /* no recent client traffic, wait a bit */
5999b776 1069 eventAddIsh("peerRefreshDNS", peerRefreshDNS, NULL, 180.0, 1);
1f3c4622 1070 return;
dd4bd44e 1071 }
1f3c4622 1072 for (p = Config.peers; p; p = p->next)
1073 ipcache_nbgethostbyname(p->host, peerDNSConfigure, p);
dd4bd44e 1074 /* Reconfigure the peers every hour */
52040193 1075 eventAddIsh("peerRefreshDNS", peerRefreshDNS, NULL, 3600.0, 1);
dd4bd44e 1076}
e924600d 1077
eb406bb7 1078void
a4b8110e 1079peerConnectFailed(peer * p)
eb406bb7 1080{
1081 p->stats.last_connect_failure = squid_curtime;
1082 if (!p->tcp_up) {
1083 debug(15, 2) ("TCP connection to %s/%d dead\n", p->host, p->http_port);
1084 return;
1085 }
1086 debug(15, 1) ("TCP connection to %s/%d failed\n", p->host, p->http_port);
1087 p->tcp_up--;
1088 if (!p->tcp_up) {
1089 debug(15, 1) ("Detected DEAD %s: %s/%d/%d\n",
1090 neighborTypeStr(p),
1091 p->host, p->http_port, p->icp.port);
1092 p->stats.logged_state = PEER_DEAD;
1093 }
1094}
1095
1096void
a4b8110e 1097peerConnectSucceded(peer * p)
eb406bb7 1098{
1099 if (!p->tcp_up) {
1100 debug(15, 2) ("TCP connection to %s/%d succeded\n", p->host, p->http_port);
1101 debug(15, 1) ("Detected REVIVED %s: %s/%d/%d\n",
1102 neighborTypeStr(p),
1103 p->host, p->http_port, p->icp.port);
1104 p->stats.logged_state = PEER_ALIVE;
1105 }
1106 p->tcp_up = PEER_TCP_MAGIC_COUNT;
1107}
1108
0e7b6b06 1109/*
eb406bb7 1110 * peerProbeConnect will be called on dead peers by neighborUp
0e7b6b06 1111 */
e924600d 1112static void
a4b8110e 1113peerProbeConnect(peer * p)
e924600d 1114{
e924600d 1115 int fd;
eb406bb7 1116 if (p->test_fd != -1)
a4b8110e 1117 return; /* probe already running */
eb406bb7 1118 if (squid_curtime - p->stats.last_connect_probe < Config.Timeout.connect)
a4b8110e 1119 return; /* don't probe to often */
d6827718 1120 fd = comm_open(SOCK_STREAM, 0, getOutgoingAddr(NULL),
e924600d 1121 0, COMM_NONBLOCKING, p->host);
1122 if (fd < 0)
1123 return;
03a1ee42 1124 p->test_fd = fd;
eb406bb7 1125 p->stats.last_connect_probe = squid_curtime;
1126 ipcache_nbgethostbyname(p->host, peerProbeConnect2, p);
e924600d 1127}
1128
1129static void
eb406bb7 1130peerProbeConnect2(const ipcache_addrs * ianotused, void *data)
e924600d 1131{
1132 peer *p = data;
03a1ee42 1133 commConnectStart(p->test_fd,
e924600d 1134 p->host,
1135 p->http_port,
eb406bb7 1136 peerProbeConnectDone,
e924600d 1137 p);
1138}
1139
1140static void
3d7e9d7c 1141peerProbeConnectDone(int fd, comm_err_t status, void *data)
e924600d 1142{
1143 peer *p = data;
98829f69 1144 if (status == COMM_OK) {
eb406bb7 1145 peerConnectSucceded(p);
e924600d 1146 } else {
eb406bb7 1147 peerConnectFailed(p);
e924600d 1148 }
1149 comm_close(fd);
eb406bb7 1150 p->test_fd = -1;
e924600d 1151 return;
1152}
1153
429fdbec 1154static void
1155peerCountMcastPeersSchedule(peer * p, time_t when)
1156{
b515fc11 1157 if (p->mcast.flags.count_event_pending)
429fdbec 1158 return;
1159 eventAdd("peerCountMcastPeersStart",
1160 peerCountMcastPeersStart,
1161 p,
52040193 1162 (double) when, 1);
b515fc11 1163 p->mcast.flags.count_event_pending = 1;
429fdbec 1164}
1165
1166static void
1167peerCountMcastPeersStart(void *data)
1168{
1169 peer *p = data;
28c60158 1170 ps_state *psstate;
429fdbec 1171 StoreEntry *fake;
1172 MemObject *mem;
1173 icp_common_t *query;
007b8be4 1174 int reqnum;
429fdbec 1175 LOCAL_ARRAY(char, url, MAX_URL);
ce66013b 1176 assert(p->type == PEER_MULTICAST);
b515fc11 1177 p->mcast.flags.count_event_pending = 0;
042461c3 1178 snprintf(url, MAX_URL, "http://%s/", inet_ntoa(p->in_addr.sin_addr));
92695e5e 1179 fake = storeCreateEntry(url, url, null_request_flags, METHOD_GET);
72711e31 1180 psstate = cbdataAlloc(ps_state);
429fdbec 1181 psstate->request = requestLink(urlParse(METHOD_GET, url));
1182 psstate->entry = fake;
1183 psstate->callback = NULL;
429fdbec 1184 psstate->callback_data = p;
b4e7f82d 1185 psstate->ping.start = current_time;
429fdbec 1186 mem = fake->mem_obj;
2db609de 1187 mem->request = requestLink(psstate->request);
429fdbec 1188 mem->start_ping = current_time;
b4e7f82d 1189 mem->ping_reply_callback = peerCountHandleIcpReply;
429fdbec 1190 mem->ircb_data = psstate;
03a1ee42 1191 mcastSetTtl(theOutIcpConnection, p->mcast.ttl);
007b8be4 1192 p->mcast.id = mem->id;
186477c1 1193 reqnum = icpSetCacheKey(fake->hash.key);
007b8be4 1194 query = icpCreateMessage(ICP_QUERY, 0, url, reqnum, 0);
429fdbec 1195 icpUdpSend(theOutIcpConnection,
1196 &p->in_addr,
1197 query,
721b3dd8 1198 LOG_ICP_QUERY,
aee4e794 1199 0);
429fdbec 1200 fake->ping_status = PING_WAITING;
1201 eventAdd("peerCountMcastPeersDone",
1202 peerCountMcastPeersDone,
2db609de 1203 psstate,
465dc415 1204 (double) Config.Timeout.mcast_icp_query, 1);
b515fc11 1205 p->mcast.flags.counting = 1;
429fdbec 1206 peerCountMcastPeersSchedule(p, MCAST_COUNT_RATE);
1207}
1208
1209static void
1210peerCountMcastPeersDone(void *data)
1211{
1212 ps_state *psstate = data;
1213 peer *p = psstate->callback_data;
1214 StoreEntry *fake = psstate->entry;
b515fc11 1215 p->mcast.flags.counting = 0;
9e4ad609 1216 p->mcast.avg_n_members = doubleAverage(p->mcast.avg_n_members,
b4e7f82d 1217 (double) psstate->ping.n_recv,
9e4ad609 1218 ++p->mcast.n_times_counted,
1219 10);
1a827bc0 1220 debug(15, 1) ("Group %s: %d replies, %4.1f average, RTT %d\n",
429fdbec 1221 p->host,
b4e7f82d 1222 psstate->ping.n_recv,
1a827bc0 1223 p->mcast.avg_n_members,
1224 p->stats.rtt);
429fdbec 1225 p->mcast.n_replies_expected = (int) p->mcast.avg_n_members;
b7fe0ab0 1226 EBIT_SET(fake->flags, ENTRY_ABORTED);
e7a22b88 1227 requestUnlink(fake->mem_obj->request);
f0d5043e 1228 fake->mem_obj->request = NULL;
429fdbec 1229 storeReleaseRequest(fake);
1230 storeUnlockObject(fake);
e7a22b88 1231 requestUnlink(psstate->request);
1a827bc0 1232 cbdataFree(psstate);
429fdbec 1233}
1234
1235static void
5942e8d4 1236peerCountHandleIcpReply(peer * p, peer_t type, protocol_t proto, void *hdrnotused, void *data)
429fdbec 1237{
d1b63fc8 1238 int rtt_av_factor;
1239
429fdbec 1240 ps_state *psstate = data;
1a827bc0 1241 StoreEntry *fake = psstate->entry;
1242 MemObject *mem = fake->mem_obj;
1243 int rtt = tvSubMsec(mem->start_ping, current_time);
86aebcda 1244 assert(proto == PROTO_ICP);
1a827bc0 1245 assert(fake);
1246 assert(mem);
b4e7f82d 1247 psstate->ping.n_recv++;
d1b63fc8 1248 rtt_av_factor = RTT_AV_FACTOR;
1249 if (p->options.weighted_roundrobin)
1250 rtt_av_factor = RTT_BACKGROUND_AV_FACTOR;
1251 p->stats.rtt = intAverage(p->stats.rtt, rtt, psstate->ping.n_recv, rtt_av_factor);
429fdbec 1252}
ed7f5615 1253
1254static void
1255neighborDumpPeers(StoreEntry * sentry)
1256{
1257 dump_peers(sentry, Config.peers);
1258}
1259
1260static void
1261neighborDumpNonPeers(StoreEntry * sentry)
1262{
1263 dump_peers(sentry, non_peers);
1264}
1265
a369131d 1266void
1267dump_peer_options(StoreEntry * sentry, peer * p)
1268{
cd196bc8 1269 if (p->options.proxy_only)
a369131d 1270 storeAppendPrintf(sentry, " proxy-only");
cd196bc8 1271 if (p->options.no_query)
a369131d 1272 storeAppendPrintf(sentry, " no-query");
d1b63fc8 1273 if (p->options.background_ping)
1274 storeAppendPrintf(sentry, " background-ping");
cd196bc8 1275 if (p->options.no_digest)
8638fc66 1276 storeAppendPrintf(sentry, " no-digest");
cd196bc8 1277 if (p->options.default_parent)
a369131d 1278 storeAppendPrintf(sentry, " default");
cd196bc8 1279 if (p->options.roundrobin)
a369131d 1280 storeAppendPrintf(sentry, " round-robin");
d1b63fc8 1281 if (p->options.weighted_roundrobin)
1282 storeAppendPrintf(sentry, " weighted-round-robin");
cd196bc8 1283 if (p->options.mcast_responder)
a369131d 1284 storeAppendPrintf(sentry, " multicast-responder");
cd196bc8 1285 if (p->options.closest_only)
a369131d 1286 storeAppendPrintf(sentry, " closest-only");
dc9d133b 1287#if USE_HTCP
cd196bc8 1288 if (p->options.htcp)
e8d185d2 1289 storeAppendPrintf(sentry, " htcp");
dc9d133b 1290#endif
cd196bc8 1291 if (p->options.no_netdb_exchange)
1a5dae67 1292 storeAppendPrintf(sentry, " no-netdb-exchange");
95e36d02 1293#if DELAY_POOLS
cd196bc8 1294 if (p->options.no_delay)
95e36d02 1295 storeAppendPrintf(sentry, " no-delay");
1296#endif
c68e9c6b 1297 if (p->login)
1298 storeAppendPrintf(sentry, " login=%s", p->login);
a369131d 1299 if (p->mcast.ttl > 0)
1300 storeAppendPrintf(sentry, " ttl=%d", p->mcast.ttl);
1301 storeAppendPrintf(sentry, "\n");
1302}
1303
ed7f5615 1304static void
1305dump_peers(StoreEntry * sentry, peer * peers)
1306{
1307 peer *e = NULL;
1308 struct _domain_ping *d = NULL;
1309 icp_opcode op;
06e6d12a 1310 int i;
ed7f5615 1311 if (peers == NULL)
1312 storeAppendPrintf(sentry, "There are no neighbors installed.\n");
1313 for (e = peers; e; e = e->next) {
1314 assert(e->host != NULL);
1315 storeAppendPrintf(sentry, "\n%-11.11s: %s/%d/%d\n",
1316 neighborTypeStr(e),
1317 e->host,
1318 e->http_port,
399cabec 1319 e->icp.port);
a369131d 1320 storeAppendPrintf(sentry, "Flags :");
1321 dump_peer_options(sentry, e);
06e6d12a 1322 for (i = 0; i < e->n_addresses; i++) {
1323 storeAppendPrintf(sentry, "Address[%d] : %s\n", i,
1324 inet_ntoa(e->addresses[i]));
1325 }
ed7f5615 1326 storeAppendPrintf(sentry, "Status : %s\n",
1327 neighborUp(e) ? "Up" : "Down");
1328 storeAppendPrintf(sentry, "AVG RTT : %d msec\n", e->stats.rtt);
c7f9eb6d 1329 storeAppendPrintf(sentry, "OPEN CONNS : %d\n", e->stats.conn_open);
ed7f5615 1330 storeAppendPrintf(sentry, "LAST QUERY : %8d seconds ago\n",
1331 (int) (squid_curtime - e->stats.last_query));
1332 storeAppendPrintf(sentry, "LAST REPLY : %8d seconds ago\n",
1333 (int) (squid_curtime - e->stats.last_reply));
1334 storeAppendPrintf(sentry, "PINGS SENT : %8d\n", e->stats.pings_sent);
1335 storeAppendPrintf(sentry, "PINGS ACKED: %8d %3d%%\n",
1336 e->stats.pings_acked,
1337 percent(e->stats.pings_acked, e->stats.pings_sent));
1338 storeAppendPrintf(sentry, "FETCHES : %8d %3d%%\n",
1339 e->stats.fetches,
1340 percent(e->stats.fetches, e->stats.pings_acked));
1341 storeAppendPrintf(sentry, "IGNORED : %8d %3d%%\n",
1342 e->stats.ignored_replies,
1343 percent(e->stats.ignored_replies, e->stats.pings_acked));
1344 storeAppendPrintf(sentry, "Histogram of PINGS ACKED:\n");
1994bb24 1345#if USE_HTCP
cd196bc8 1346 if (e->options.htcp) {
1994bb24 1347 storeAppendPrintf(sentry, "\tMisses\t%8d %3d%%\n",
1348 e->htcp.counts[0],
1349 percent(e->htcp.counts[0], e->stats.pings_acked));
1350 storeAppendPrintf(sentry, "\tHits\t%8d %3d%%\n",
1351 e->htcp.counts[1],
1352 percent(e->htcp.counts[1], e->stats.pings_acked));
1353 } else {
1354#endif
5942e8d4 1355 for (op = ICP_INVALID; op < ICP_END; op++) {
1356 if (e->icp.counts[op] == 0)
1357 continue;
1358 storeAppendPrintf(sentry, " %12.12s : %8d %3d%%\n",
1359 icp_opcode_str[op],
1360 e->icp.counts[op],
1361 percent(e->icp.counts[op], e->stats.pings_acked));
1362 }
1994bb24 1363#if USE_HTCP
5942e8d4 1364 }
1994bb24 1365#endif
eb406bb7 1366 if (e->stats.last_connect_failure) {
ed7f5615 1367 storeAppendPrintf(sentry, "Last failed connect() at: %s\n",
eb406bb7 1368 mkhttpdlogtime(&(e->stats.last_connect_failure)));
ed7f5615 1369 }
b6a2f15e 1370 if (e->peer_domain != NULL) {
ed7f5615 1371 storeAppendPrintf(sentry, "DOMAIN LIST: ");
b6a2f15e 1372 for (d = e->peer_domain; d; d = d->next) {
5942e8d4 1373 storeAppendPrintf(sentry, "%s%s ",
1374 d->do_ping ? null_string : "!", d->domain);
1994bb24 1375 }
1376 storeAppendPrintf(sentry, "\n");
ed7f5615 1377 }
99edd1c3 1378 storeAppendPrintf(sentry, "keep-alive ratio: %d%%\n",
ed7f5615 1379 percent(e->stats.n_keepalives_recv, e->stats.n_keepalives_sent));
1380 }
1381}
86aebcda 1382
1383#if USE_HTCP
1384void
1385neighborsHtcpReply(const cache_key * key, htcpReplyData * htcp, const struct sockaddr_in *from)
1386{
b4e7f82d 1387 StoreEntry *e = storeGet(key);
399cabec 1388 MemObject *mem = NULL;
1389 peer *p;
1390 peer_t ntype = PEER_NONE;
399cabec 1391 debug(15, 6) ("neighborsHtcpReply: %s %s\n",
1392 htcp->hit ? "HIT" : "MISS", storeKeyText(key));
1393 if (NULL != (e = storeGet(key)))
1394 mem = e->mem_obj;
1395 if ((p = whichPeer(from)))
1396 neighborAliveHtcp(p, mem, htcp);
1397 /* Does the entry exist? */
1398 if (NULL == e) {
1399 debug(12, 3) ("neighyborsHtcpReply: Cache key '%s' not found\n",
1400 storeKeyText(key));
1401 neighborCountIgnored(p);
1402 return;
1403 }
1404 /* check if someone is already fetching it */
d46a87a8 1405 if (EBIT_TEST(e->flags, ENTRY_DISPATCHED)) {
399cabec 1406 debug(15, 3) ("neighborsUdpAck: '%s' already being fetched.\n",
1407 storeKeyText(key));
1408 neighborCountIgnored(p);
1409 return;
1410 }
1411 if (mem == NULL) {
1412 debug(15, 2) ("Ignoring reply for missing mem_obj: %s\n",
1413 storeKeyText(key));
1414 neighborCountIgnored(p);
1415 return;
1416 }
1417 if (e->ping_status != PING_WAITING) {
1418 debug(15, 2) ("neighborsUdpAck: Entry %s is not PING_WAITING\n",
1419 storeKeyText(key));
1420 neighborCountIgnored(p);
1421 return;
1422 }
1423 if (e->lock_count == 0) {
1424 debug(12, 1) ("neighborsUdpAck: '%s' has no locks\n",
1425 storeKeyText(key));
1426 neighborCountIgnored(p);
1427 return;
1428 }
44f2f2e4 1429 if (p) {
399cabec 1430 ntype = neighborType(p, mem->request);
44f2f2e4 1431 neighborUpdateRtt(p, mem);
1432 }
399cabec 1433 if (ignoreMulticastReply(p, mem)) {
1434 neighborCountIgnored(p);
1435 return;
1436 }
9bc73deb 1437 debug(15, 3) ("neighborsHtcpReply: e = %p\n", e);
b4e7f82d 1438 mem->ping_reply_callback(p, ntype, PROTO_HTCP, htcp, mem->ircb_data);
86aebcda 1439}
1440#endif