]> git.ipfire.org Git - thirdparty/squid.git/blob - src/icp_v2.cc
Completd and fixed some class constructors, removed redundant htcpReplyData class.
[thirdparty/squid.git] / src / icp_v2.cc
1 /*
2 * DEBUG: section 12 Internet Cache Protocol (ICP)
3 * AUTHOR: Duane Wessels
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32
33 /**
34 \defgroup ServerProtocolICPInternal2 ICPv2 Internals
35 \ingroup ServerProtocolICPAPI
36 */
37
38 #include "squid.h"
39 #include "AccessLogEntry.h"
40 #include "acl/Acl.h"
41 #include "acl/FilledChecklist.h"
42 #include "client_db.h"
43 #include "comm.h"
44 #include "comm/Connection.h"
45 #include "comm/Loops.h"
46 #include "comm/UdpOpenDialer.h"
47 #include "fd.h"
48 #include "HttpRequest.h"
49 #include "icmp/net_db.h"
50 #include "ICP.h"
51 #include "ip/Address.h"
52 #include "ip/tools.h"
53 #include "ipcache.h"
54 #include "md5.h"
55 #include "multicast.h"
56 #include "neighbors.h"
57 #include "refresh.h"
58 #include "rfc1738.h"
59 #include "SquidConfig.h"
60 #include "SquidTime.h"
61 #include "StatCounters.h"
62 #include "Store.h"
63 #include "store_key_md5.h"
64 #include "SwapDir.h"
65 #include "tools.h"
66 #include "wordlist.h"
67
68 #if HAVE_ERRNO_H
69 #include <errno.h>
70 #endif
71
72 static void icpIncomingConnectionOpened(const Comm::ConnectionPointer &conn, int errNo);
73
74 /// \ingroup ServerProtocolICPInternal2
75 static void icpLogIcp(const Ip::Address &, log_type, int, const char *, int);
76
77 /// \ingroup ServerProtocolICPInternal2
78 static void icpHandleIcpV2(int, Ip::Address &, char *, int);
79
80 /// \ingroup ServerProtocolICPInternal2
81 static void icpCount(void *, int, size_t, int);
82
83 /**
84 \ingroup ServerProtocolICPInternal2
85 * IcpQueueHead is global so comm_incoming() knows whether or not
86 * to call icpUdpSendQueue.
87 */
88 static icpUdpData *IcpQueueHead = NULL;
89 /// \ingroup ServerProtocolICPInternal2
90 static icpUdpData *IcpQueueTail = NULL;
91
92 /// \ingroup ServerProtocolICPInternal2
93 Comm::ConnectionPointer icpIncomingConn = NULL;
94 /// \ingroup ServerProtocolICPInternal2
95 Comm::ConnectionPointer icpOutgoingConn = NULL;
96
97 /* icp_common_t */
98 _icp_common_t::_icp_common_t() :
99 opcode(ICP_INVALID), version(0), length(0), reqnum(0),
100 flags(0), pad(0), shostid(0)
101 {}
102
103 _icp_common_t::_icp_common_t(char *buf, unsigned int len) :
104 opcode(ICP_INVALID), version(0), reqnum(0), flags(0), pad(0), shostid(0)
105 {
106 if (len < sizeof(_icp_common_t)) {
107 /* mark as invalid */
108 length = len + 1;
109 return;
110 }
111
112 memcpy(this, buf, sizeof(icp_common_t));
113 /*
114 * Convert network order sensitive fields
115 */
116 length = ntohs(length);
117 reqnum = ntohl(reqnum);
118 flags = ntohl(flags);
119 pad = ntohl(pad);
120 }
121
122 icp_opcode
123 _icp_common_t::getOpCode() const
124 {
125 if (opcode > (char)ICP_END)
126 return ICP_INVALID;
127
128 return (icp_opcode)opcode;
129 }
130
131 /* ICPState */
132
133 ICPState::ICPState(icp_common_t &aHeader, HttpRequest *aRequest):
134 header(aHeader),
135 request(HTTPMSGLOCK(aRequest)),
136 fd(-1),
137 url(NULL)
138 {}
139
140 ICPState::~ICPState()
141 {
142 safe_free(url);
143 HTTPMSGUNLOCK(request);
144 }
145
146 /* End ICPState */
147
148 /* ICP2State */
149
150 /// \ingroup ServerProtocolICPInternal2
151 class ICP2State : public ICPState, public StoreClient
152 {
153
154 public:
155 ICP2State(icp_common_t & aHeader, HttpRequest *aRequest):
156 ICPState(aHeader, aRequest),rtt(0),src_rtt(0),flags(0) {}
157
158 ~ICP2State();
159 void created(StoreEntry * newEntry);
160
161 int rtt;
162 int src_rtt;
163 uint32_t flags;
164 };
165
166 ICP2State::~ICP2State()
167 {}
168
169 void
170 ICP2State::created(StoreEntry *newEntry)
171 {
172 StoreEntry *entry = newEntry->isNull () ? NULL : newEntry;
173 debugs(12, 5, "icpHandleIcpV2: OPCODE " << icp_opcode_str[header.opcode]);
174 icp_opcode codeToSend;
175
176 if (icpCheckUdpHit(entry, request)) {
177 codeToSend = ICP_HIT;
178 } else {
179 #if USE_ICMP
180 if (Config.onoff.test_reachability && rtt == 0) {
181 if ((rtt = netdbHostRtt(request->GetHost())) == 0)
182 netdbPingSite(request->GetHost());
183 }
184 #endif /* USE_ICMP */
185
186 if (icpGetCommonOpcode() != ICP_ERR)
187 codeToSend = icpGetCommonOpcode();
188 else if (Config.onoff.test_reachability && rtt == 0)
189 codeToSend = ICP_MISS_NOFETCH;
190 else
191 codeToSend = ICP_MISS;
192 }
193
194 icpCreateAndSend(codeToSend, flags, url, header.reqnum, src_rtt, fd, from);
195 delete this;
196 }
197
198 /* End ICP2State */
199
200 /// \ingroup ServerProtocolICPInternal2
201 static void
202 icpLogIcp(const Ip::Address &caddr, log_type logcode, int len, const char *url, int delay)
203 {
204 AccessLogEntry::Pointer al = new AccessLogEntry();
205
206 if (LOG_TAG_NONE == logcode)
207 return;
208
209 if (LOG_ICP_QUERY == logcode)
210 return;
211
212 clientdbUpdate(caddr, logcode, AnyP::PROTO_ICP, len);
213
214 if (!Config.onoff.log_udp)
215 return;
216
217 al->icp.opcode = ICP_QUERY;
218
219 al->url = url;
220
221 al->cache.caddr = caddr;
222
223 al->cache.replySize = len;
224
225 al->cache.code = logcode;
226
227 al->cache.msec = delay;
228
229 accessLogLog(al, NULL);
230 }
231
232 /// \ingroup ServerProtocolICPInternal2
233 void
234 icpUdpSendQueue(int fd, void *unused)
235 {
236 icpUdpData *q;
237
238 while ((q = IcpQueueHead) != NULL) {
239 int delay = tvSubUsec(q->queue_time, current_time);
240 /* increment delay to prevent looping */
241 const int x = icpUdpSend(fd, q->address, (icp_common_t *) q->msg, q->logcode, ++delay);
242 IcpQueueHead = q->next;
243 xfree(q);
244
245 if (x < 0)
246 break;
247 }
248 }
249
250 _icp_common_t *
251 _icp_common_t::createMessage(
252 icp_opcode opcode,
253 int flags,
254 const char *url,
255 int reqnum,
256 int pad)
257 {
258 char *buf = NULL;
259 icp_common_t *headerp = NULL;
260 char *urloffset = NULL;
261 int buf_len;
262 buf_len = sizeof(icp_common_t) + strlen(url) + 1;
263
264 if (opcode == ICP_QUERY)
265 buf_len += sizeof(uint32_t);
266
267 buf = (char *) xcalloc(buf_len, 1);
268
269 headerp = (icp_common_t *) (void *) buf;
270
271 headerp->opcode = (char) opcode;
272
273 headerp->version = ICP_VERSION_CURRENT;
274
275 headerp->length = (uint16_t) htons(buf_len);
276
277 headerp->reqnum = htonl(reqnum);
278
279 headerp->flags = htonl(flags);
280
281 headerp->pad = htonl(pad);
282
283 headerp->shostid = 0;
284
285 urloffset = buf + sizeof(icp_common_t);
286
287 if (opcode == ICP_QUERY)
288 urloffset += sizeof(uint32_t);
289
290 memcpy(urloffset, url, strlen(url));
291
292 return (icp_common_t *)buf;
293 }
294
295 int
296 icpUdpSend(int fd,
297 const Ip::Address &to,
298 icp_common_t * msg,
299 log_type logcode,
300 int delay)
301 {
302 icpUdpData *queue;
303 int x;
304 int len;
305 len = (int) ntohs(msg->length);
306 debugs(12, 5, "icpUdpSend: FD " << fd << " sending " <<
307 icp_opcode_str[msg->opcode] << ", " << len << " bytes to " << to);
308
309 x = comm_udp_sendto(fd, to, msg, len);
310
311 if (x >= 0) {
312 /* successfully written */
313 icpLogIcp(to, logcode, len, (char *) (msg + 1), delay);
314 icpCount(msg, SENT, (size_t) len, delay);
315 safe_free(msg);
316 } else if (0 == delay) {
317 /* send failed, but queue it */
318 queue = (icpUdpData *) xcalloc(1, sizeof(icpUdpData));
319 queue->address = to;
320 queue->msg = msg;
321 queue->len = (int) ntohs(msg->length);
322 queue->queue_time = current_time;
323 queue->logcode = logcode;
324
325 if (IcpQueueHead == NULL) {
326 IcpQueueHead = queue;
327 IcpQueueTail = queue;
328 } else if (IcpQueueTail == IcpQueueHead) {
329 IcpQueueTail = queue;
330 IcpQueueHead->next = queue;
331 } else {
332 IcpQueueTail->next = queue;
333 IcpQueueTail = queue;
334 }
335
336 Comm::SetSelect(fd, COMM_SELECT_WRITE, icpUdpSendQueue, NULL, 0);
337 ++statCounter.icp.replies_queued;
338 } else {
339 /* don't queue it */
340 ++statCounter.icp.replies_dropped;
341 }
342
343 return x;
344 }
345
346 int
347 icpCheckUdpHit(StoreEntry * e, HttpRequest * request)
348 {
349 if (e == NULL)
350 return 0;
351
352 if (!e->validToSend())
353 return 0;
354
355 if (Config.onoff.icp_hit_stale)
356 return 1;
357
358 if (refreshCheckICP(e, request))
359 return 0;
360
361 return 1;
362 }
363
364 /**
365 * This routine selects an ICP opcode for ICP misses.
366 *
367 \retval ICP_ERR no opcode selected here
368 \retval ICP_MISS_NOFETCH store is rebuilding, no fetch is possible yet
369 */
370 icp_opcode
371 icpGetCommonOpcode()
372 {
373 /* if store is rebuilding, return a UDP_MISS_NOFETCH */
374
375 if ((StoreController::store_dirs_rebuilding && opt_reload_hit_only) ||
376 hit_only_mode_until > squid_curtime) {
377 return ICP_MISS_NOFETCH;
378 }
379
380 return ICP_ERR;
381 }
382
383 log_type
384 icpLogFromICPCode(icp_opcode opcode)
385 {
386 if (opcode == ICP_ERR)
387 return LOG_UDP_INVALID;
388
389 if (opcode == ICP_DENIED)
390 return LOG_UDP_DENIED;
391
392 if (opcode == ICP_HIT)
393 return LOG_UDP_HIT;
394
395 if (opcode == ICP_MISS)
396 return LOG_UDP_MISS;
397
398 if (opcode == ICP_MISS_NOFETCH)
399 return LOG_UDP_MISS_NOFETCH;
400
401 fatal("expected ICP opcode\n");
402
403 return LOG_UDP_INVALID;
404 }
405
406 void
407 icpCreateAndSend(icp_opcode opcode, int flags, char const *url, int reqnum, int pad, int fd, const Ip::Address &from)
408 {
409 icp_common_t *reply = _icp_common_t::createMessage(opcode, flags, url, reqnum, pad);
410 icpUdpSend(fd, from, reply, icpLogFromICPCode(opcode), 0);
411 }
412
413 void
414 icpDenyAccess(Ip::Address &from, char *url, int reqnum, int fd)
415 {
416 debugs(12, 2, "icpDenyAccess: Access Denied for " << from << " by " << AclMatchedName << ".");
417
418 if (clientdbCutoffDenied(from)) {
419 /*
420 * count this DENIED query in the clientdb, even though
421 * we're not sending an ICP reply...
422 */
423 clientdbUpdate(from, LOG_UDP_DENIED, AnyP::PROTO_ICP, 0);
424 } else {
425 icpCreateAndSend(ICP_DENIED, 0, url, reqnum, 0, fd, from);
426 }
427 }
428
429 bool
430 icpAccessAllowed(Ip::Address &from, HttpRequest * icp_request)
431 {
432 /* absent an explicit allow, we deny all */
433 if (!Config.accessList.icp)
434 return true;
435
436 ACLFilledChecklist checklist(Config.accessList.icp, icp_request, NULL);
437 checklist.src_addr = from;
438 checklist.my_addr.SetNoAddr();
439 return (checklist.fastCheck() == ACCESS_ALLOWED);
440 }
441
442 char const *
443 icpGetUrlToSend(char *url)
444 {
445 if (strpbrk(url, w_space))
446 return rfc1738_escape(url);
447 else
448 return url;
449 }
450
451 HttpRequest *
452 icpGetRequest(char *url, int reqnum, int fd, Ip::Address &from)
453 {
454 if (strpbrk(url, w_space)) {
455 url = rfc1738_escape(url);
456 icpCreateAndSend(ICP_ERR, 0, rfc1738_escape(url), reqnum, 0, fd, from);
457 return NULL;
458 }
459
460 HttpRequest *result;
461
462 if ((result = HttpRequest::CreateFromUrl(url)) == NULL)
463 icpCreateAndSend(ICP_ERR, 0, url, reqnum, 0, fd, from);
464
465 return result;
466
467 }
468
469 static void
470 doV2Query(int fd, Ip::Address &from, char *buf, icp_common_t header)
471 {
472 int rtt = 0;
473 int src_rtt = 0;
474 uint32_t flags = 0;
475 /* We have a valid packet */
476 char *url = buf + sizeof(icp_common_t) + sizeof(uint32_t);
477 HttpRequest *icp_request = icpGetRequest(url, header.reqnum, fd, from);
478
479 if (!icp_request)
480 return;
481
482 HTTPMSGLOCK(icp_request);
483
484 if (!icpAccessAllowed(from, icp_request)) {
485 icpDenyAccess(from, url, header.reqnum, fd);
486 HTTPMSGUNLOCK(icp_request);
487 return;
488 }
489 #if USE_ICMP
490 if (header.flags & ICP_FLAG_SRC_RTT) {
491 rtt = netdbHostRtt(icp_request->GetHost());
492 int hops = netdbHostHops(icp_request->GetHost());
493 src_rtt = ((hops & 0xFFFF) << 16) | (rtt & 0xFFFF);
494
495 if (rtt)
496 flags |= ICP_FLAG_SRC_RTT;
497 }
498 #endif /* USE_ICMP */
499
500 /* The peer is allowed to use this cache */
501 ICP2State *state = new ICP2State (header, icp_request);
502
503 state->fd = fd;
504
505 state->from = from;
506
507 state->url = xstrdup (url);
508
509 state->flags = flags;
510
511 state->rtt = rtt;
512
513 state->src_rtt = src_rtt;
514
515 StoreEntry::getPublic (state, url, Http::METHOD_GET);
516
517 HTTPMSGUNLOCK(icp_request);
518 }
519
520 void
521 _icp_common_t::handleReply(char *buf, Ip::Address &from)
522 {
523 if (neighbors_do_private_keys && reqnum == 0) {
524 debugs(12, DBG_CRITICAL, "icpHandleIcpV2: Neighbor " << from << " returned reqnum = 0");
525 debugs(12, DBG_CRITICAL, "icpHandleIcpV2: Disabling use of private keys");
526 neighbors_do_private_keys = 0;
527 }
528
529 char *url = buf + sizeof(icp_common_t);
530 debugs(12, 3, "icpHandleIcpV2: " << icp_opcode_str[opcode] << " from " << from << " for '" << url << "'");
531
532 const cache_key *key = icpGetCacheKey(url, (int) reqnum);
533 /* call neighborsUdpAck even if ping_status != PING_WAITING */
534 neighborsUdpAck(key, this, from);
535 }
536
537 static void
538 icpHandleIcpV2(int fd, Ip::Address &from, char *buf, int len)
539 {
540 if (len <= 0) {
541 debugs(12, 3, "icpHandleIcpV2: ICP message is too small");
542 return;
543 }
544
545 icp_common_t header(buf, len);
546 /*
547 * Length field should match the number of bytes read
548 */
549
550 if (len != header.length) {
551 debugs(12, 3, "icpHandleIcpV2: ICP message is too small");
552 return;
553 }
554
555 switch (header.opcode) {
556
557 case ICP_QUERY:
558 /* We have a valid packet */
559 doV2Query(fd, from, buf, header);
560 break;
561
562 case ICP_HIT:
563
564 case ICP_DECHO:
565
566 case ICP_MISS:
567
568 case ICP_DENIED:
569
570 case ICP_MISS_NOFETCH:
571 header.handleReply(buf, from);
572 break;
573
574 case ICP_INVALID:
575
576 case ICP_ERR:
577 break;
578
579 default:
580 debugs(12, DBG_CRITICAL, "icpHandleIcpV2: UNKNOWN OPCODE: " << header.opcode << " from " << from);
581
582 break;
583 }
584 }
585
586 #ifdef ICP_PKT_DUMP
587 static void
588 icpPktDump(icp_common_t * pkt)
589 {
590 Ip::Address a;
591
592 debugs(12, 9, "opcode: " << std::setw(3) << pkt->opcode << " " << icp_opcode_str[pkt->opcode]);
593 debugs(12, 9, "version: "<< std::left << std::setw(8) << pkt->version);
594 debugs(12, 9, "length: "<< std::left << std::setw(8) << ntohs(pkt->length));
595 debugs(12, 9, "reqnum: "<< std::left << std::setw(8) << ntohl(pkt->reqnum));
596 debugs(12, 9, "flags: "<< std::left << std::hex << std::setw(8) << ntohl(pkt->flags));
597 a = (struct in_addr)pkt->shostid;
598 debugs(12, 9, "shostid: " << a );
599 debugs(12, 9, "payload: " << (char *) pkt + sizeof(icp_common_t));
600 }
601
602 #endif
603
604 void
605 icpHandleUdp(int sock, void *data)
606 {
607 int *N = &incoming_sockets_accepted;
608
609 Ip::Address from;
610 LOCAL_ARRAY(char, buf, SQUID_UDP_SO_RCVBUF);
611 int len;
612 int icp_version;
613 int max = INCOMING_UDP_MAX;
614 Comm::SetSelect(sock, COMM_SELECT_READ, icpHandleUdp, NULL, 0);
615
616 while (max) {
617 --max;
618 len = comm_udp_recvfrom(sock,
619 buf,
620 SQUID_UDP_SO_RCVBUF - 1,
621 0,
622 from);
623
624 if (len == 0)
625 break;
626
627 if (len < 0) {
628 if (ignoreErrno(errno))
629 break;
630
631 #if _SQUID_LINUX_
632 /* Some Linux systems seem to set the FD for reading and then
633 * return ECONNREFUSED when sendto() fails and generates an ICMP
634 * port unreachable message. */
635 /* or maybe an EHOSTUNREACH "No route to host" message */
636 if (errno != ECONNREFUSED && errno != EHOSTUNREACH)
637 #endif
638
639 debugs(50, DBG_IMPORTANT, "icpHandleUdp: FD " << sock << " recvfrom: " << xstrerror());
640
641 break;
642 }
643
644 ++(*N);
645 icpCount(buf, RECV, (size_t) len, 0);
646 buf[len] = '\0';
647 debugs(12, 4, "icpHandleUdp: FD " << sock << ": received " <<
648 (unsigned long int)len << " bytes from " << from);
649
650 #ifdef ICP_PACKET_DUMP
651
652 icpPktDump(buf);
653 #endif
654
655 if ((size_t) len < sizeof(icp_common_t)) {
656 debugs(12, 4, "icpHandleUdp: Ignoring too-small UDP packet");
657 break;
658 }
659
660 icp_version = (int) buf[1]; /* cheat! */
661
662 if (icpOutgoingConn->local == from)
663 // ignore ICP packets which loop back (multicast usually)
664 debugs(12, 4, "icpHandleUdp: Ignoring UDP packet sent by myself");
665 else if (icp_version == ICP_VERSION_2)
666 icpHandleIcpV2(sock, from, buf, len);
667 else if (icp_version == ICP_VERSION_3)
668 icpHandleIcpV3(sock, from, buf, len);
669 else
670 debugs(12, DBG_IMPORTANT, "WARNING: Unused ICP version " << icp_version <<
671 " received from " << from);
672 }
673 }
674
675 void
676 icpOpenPorts(void)
677 {
678 uint16_t port;
679
680 if ((port = Config.Port.icp) <= 0)
681 return;
682
683 icpIncomingConn = new Comm::Connection;
684 icpIncomingConn->local = Config.Addrs.udp_incoming;
685 icpIncomingConn->local.SetPort(port);
686
687 if (!Ip::EnableIpv6 && !icpIncomingConn->local.SetIPv4()) {
688 debugs(12, DBG_CRITICAL, "ERROR: IPv6 is disabled. " << icpIncomingConn->local << " is not an IPv4 address.");
689 fatal("ICP port cannot be opened.");
690 }
691 /* split-stack for now requires default IPv4-only ICP */
692 if (Ip::EnableIpv6&IPV6_SPECIAL_SPLITSTACK && icpIncomingConn->local.IsAnyAddr()) {
693 icpIncomingConn->local.SetIPv4();
694 }
695
696 AsyncCall::Pointer call = asyncCall(12, 2,
697 "icpIncomingConnectionOpened",
698 Comm::UdpOpenDialer(&icpIncomingConnectionOpened));
699
700 Ipc::StartListening(SOCK_DGRAM,
701 IPPROTO_UDP,
702 icpIncomingConn,
703 Ipc::fdnInIcpSocket, call);
704
705 if ( !Config.Addrs.udp_outgoing.IsNoAddr() ) {
706 icpOutgoingConn = new Comm::Connection;
707 icpOutgoingConn->local = Config.Addrs.udp_outgoing;
708 icpOutgoingConn->local.SetPort(port);
709
710 if (!Ip::EnableIpv6 && !icpOutgoingConn->local.SetIPv4()) {
711 debugs(49, DBG_CRITICAL, "ERROR: IPv6 is disabled. " << icpOutgoingConn->local << " is not an IPv4 address.");
712 fatal("ICP port cannot be opened.");
713 }
714 /* split-stack for now requires default IPv4-only ICP */
715 if (Ip::EnableIpv6&IPV6_SPECIAL_SPLITSTACK && icpOutgoingConn->local.IsAnyAddr()) {
716 icpOutgoingConn->local.SetIPv4();
717 }
718
719 enter_suid();
720 comm_open_listener(SOCK_DGRAM, IPPROTO_UDP, icpOutgoingConn, "Outgoing ICP Port");
721 leave_suid();
722
723 if (!Comm::IsConnOpen(icpOutgoingConn))
724 fatal("Cannot open Outgoing ICP Port");
725
726 debugs(12, DBG_CRITICAL, "Sending ICP messages from " << icpOutgoingConn->local);
727
728 Comm::SetSelect(icpOutgoingConn->fd, COMM_SELECT_READ, icpHandleUdp, NULL, 0);
729 fd_note(icpOutgoingConn->fd, "Outgoing ICP socket");
730 }
731 }
732
733 static void
734 icpIncomingConnectionOpened(const Comm::ConnectionPointer &conn, int errNo)
735 {
736 if (!Comm::IsConnOpen(conn))
737 fatal("Cannot open ICP Port");
738
739 Comm::SetSelect(conn->fd, COMM_SELECT_READ, icpHandleUdp, NULL, 0);
740
741 for (const wordlist *s = Config.mcast_group_list; s; s = s->next)
742 ipcache_nbgethostbyname(s->key, mcastJoinGroups, NULL); // XXX: pass the conn for mcastJoinGroups usage.
743
744 debugs(12, DBG_IMPORTANT, "Accepting ICP messages on " << conn->local);
745
746 fd_note(conn->fd, "Incoming ICP port");
747
748 if (Config.Addrs.udp_outgoing.IsNoAddr()) {
749 icpOutgoingConn = conn;
750 debugs(12, DBG_IMPORTANT, "Sending ICP messages from " << icpOutgoingConn->local);
751 }
752 }
753
754 /**
755 * icpConnectionShutdown only closes the 'in' socket if it is
756 * different than the 'out' socket.
757 */
758 void
759 icpConnectionShutdown(void)
760 {
761 if (!Comm::IsConnOpen(icpIncomingConn))
762 return;
763
764 debugs(12, DBG_IMPORTANT, "Stop receiving ICP on " << icpIncomingConn->local);
765
766 /** Release the 'in' socket for lazy closure.
767 * in and out sockets may be sharing one same FD.
768 * This prevents this function from executing repeatedly.
769 */
770 icpIncomingConn = NULL;
771
772 /**
773 * Normally we only write to the outgoing ICP socket, but
774 * we also have a read handler there to catch messages sent
775 * to that specific interface. During shutdown, we must
776 * disable reading on the outgoing socket.
777 */
778 assert(Comm::IsConnOpen(icpOutgoingConn));
779
780 Comm::SetSelect(icpOutgoingConn->fd, COMM_SELECT_READ, NULL, NULL, 0);
781 }
782
783 void
784 icpClosePorts(void)
785 {
786 icpConnectionShutdown();
787
788 if (icpOutgoingConn != NULL) {
789 debugs(12, DBG_IMPORTANT, "Stop sending ICP from " << icpOutgoingConn->local);
790 icpOutgoingConn = NULL;
791 }
792 }
793
794 static void
795 icpCount(void *buf, int which, size_t len, int delay)
796 {
797 icp_common_t *icp = (icp_common_t *) buf;
798
799 if (len < sizeof(*icp))
800 return;
801
802 if (SENT == which) {
803 ++statCounter.icp.pkts_sent;
804 kb_incr(&statCounter.icp.kbytes_sent, len);
805
806 if (ICP_QUERY == icp->opcode) {
807 ++statCounter.icp.queries_sent;
808 kb_incr(&statCounter.icp.q_kbytes_sent, len);
809 } else {
810 ++statCounter.icp.replies_sent;
811 kb_incr(&statCounter.icp.r_kbytes_sent, len);
812 /* this is the sent-reply service time */
813 statCounter.icp.replySvcTime.count(delay);
814 }
815
816 if (ICP_HIT == icp->opcode)
817 ++statCounter.icp.hits_sent;
818 } else if (RECV == which) {
819 ++statCounter.icp.pkts_recv;
820 kb_incr(&statCounter.icp.kbytes_recv, len);
821
822 if (ICP_QUERY == icp->opcode) {
823 ++statCounter.icp.queries_recv;
824 kb_incr(&statCounter.icp.q_kbytes_recv, len);
825 } else {
826 ++statCounter.icp.replies_recv;
827 kb_incr(&statCounter.icp.r_kbytes_recv, len);
828 /* statCounter.icp.querySvcTime set in clientUpdateCounters */
829 }
830
831 if (ICP_HIT == icp->opcode)
832 ++statCounter.icp.hits_recv;
833 }
834 }
835
836 #define N_QUERIED_KEYS 8192
837 #define N_QUERIED_KEYS_MASK 8191
838 static cache_key queried_keys[N_QUERIED_KEYS][SQUID_MD5_DIGEST_LENGTH];
839
840 int
841 icpSetCacheKey(const cache_key * key)
842 {
843 static int reqnum = 0;
844
845 if (++reqnum < 0)
846 reqnum = 1;
847
848 storeKeyCopy(queried_keys[reqnum & N_QUERIED_KEYS_MASK], key);
849
850 return reqnum;
851 }
852
853 const cache_key *
854 icpGetCacheKey(const char *url, int reqnum)
855 {
856 if (neighbors_do_private_keys && reqnum)
857 return queried_keys[reqnum & N_QUERIED_KEYS_MASK];
858
859 return storeKeyPublic(url, Http::METHOD_GET);
860 }