]> git.ipfire.org Git - thirdparty/squid.git/blob - src/htcp.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / htcp.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 31 Hypertext Caching Protocol
6 * AUTHOR: Duane Wesssels
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
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.
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
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "squid-old.h"
37 #include "AccessLogEntry.h"
38 #include "acl/FilledChecklist.h"
39 #include "acl/Acl.h"
40 #include "comm.h"
41 #include "comm/Loops.h"
42 #include "htcp.h"
43 #include "http.h"
44 #include "HttpRequest.h"
45 #include "icmp/net_db.h"
46 #include "ipc/StartListening.h"
47 #include "ip/tools.h"
48 #include "MemBuf.h"
49 #include "SquidTime.h"
50 #include "StatCounters.h"
51 #include "Store.h"
52 #include "StoreClient.h"
53 #include "compat/xalloc.h"
54
55 /// dials htcpIncomingConnectionOpened call
56 class HtcpListeningStartedDialer: public CallDialer,
57 public Ipc::StartListeningCb
58 {
59 public:
60 typedef void (*Handler)(int errNo);
61 HtcpListeningStartedDialer(Handler aHandler): handler(aHandler) {}
62
63 virtual void print(std::ostream &os) const { startPrint(os) << ')'; }
64 virtual bool canDial(AsyncCall &) const { return true; }
65 virtual void dial(AsyncCall &) { (handler)(errNo); }
66
67 public:
68 Handler handler;
69 };
70
71 typedef struct _Countstr Countstr;
72
73 typedef struct _htcpHeader htcpHeader;
74
75 typedef struct _htcpDataHeader htcpDataHeader;
76
77 typedef struct _htcpDataHeaderSquid htcpDataHeaderSquid;
78
79 typedef struct _htcpAuthHeader htcpAuthHeader;
80
81 typedef struct _htcpStuff htcpStuff;
82
83 typedef struct _htcpDetail htcpDetail;
84
85 struct _Countstr {
86 uint16_t length;
87 char *text;
88 };
89
90 struct _htcpHeader {
91 uint16_t length;
92 u_char major;
93 u_char minor;
94 };
95
96 struct _htcpDataHeaderSquid {
97 uint16_t length;
98
99 #if !WORDS_BIGENDIAN
100 unsigned int opcode:4;
101 unsigned int response:4;
102 #else
103 unsigned int response:4;
104 unsigned int opcode:4;
105 #endif
106
107 #if !WORDS_BIGENDIAN
108 unsigned int reserved:6;
109 unsigned int F1:1;
110 unsigned int RR:1;
111 #else
112 unsigned int RR:1;
113 unsigned int F1:1;
114 unsigned int reserved:6;
115 #endif
116
117 uint32_t msg_id;
118 };
119
120 struct _htcpDataHeader {
121 uint16_t length;
122
123 #if WORDS_BIGENDIAN
124 uint8_t opcode:
125 4;
126 uint8_t response:
127 4;
128 #else
129 uint8_t response:
130 4;
131 uint8_t opcode:
132 4;
133 #endif
134
135 #if WORDS_BIGENDIAN
136 uint8_t reserved:
137 6;
138 uint8_t F1:
139 1;
140 uint8_t RR:
141 1;
142 #else
143 uint8_t RR:
144 1;
145 uint8_t F1:
146 1;
147 uint8_t reserved:
148 6;
149 #endif
150
151 uint32_t msg_id;
152 };
153
154 /* RR == 0 --> F1 = RESPONSE DESIRED FLAG */
155 /* RR == 1 --> F1 = MESSAGE OVERALL FLAG */
156 /* RR == 0 --> REQUEST */
157 /* RR == 1 --> RESPONSE */
158
159 struct _htcpAuthHeader {
160 uint16_t length;
161 time_t sig_time;
162 time_t sig_expire;
163 Countstr key_name;
164 Countstr signature;
165 };
166
167 class htcpSpecifier : public StoreClient
168 {
169
170 public:
171 MEMPROXY_CLASS(htcpSpecifier);
172
173 void created (StoreEntry *newEntry);
174 void checkHit();
175 void checkedHit(StoreEntry *e);
176
177 void setFrom(Ip::Address &from);
178 void setDataHeader(htcpDataHeader *);
179 char *method;
180 char *uri;
181 char *version;
182 char *req_hdrs;
183 HttpRequest *request;
184
185 private:
186 HttpRequest *checkHitRequest;
187
188 Ip::Address from; // was a ptr. return to such IFF needed. otherwise copy should do.
189 htcpDataHeader *dhdr;
190 };
191
192 MEMPROXY_CLASS_INLINE(htcpSpecifier);
193
194 struct _htcpDetail {
195 char *resp_hdrs;
196 char *entity_hdrs;
197 char *cache_hdrs;
198 };
199
200 struct _htcpStuff {
201 int op;
202 int rr;
203 int f1;
204 int response;
205 int reason;
206 uint32_t msg_id;
207 htcpSpecifier S;
208 htcpDetail D;
209 };
210
211 enum {
212 HTCP_NOP,
213 HTCP_TST,
214 HTCP_MON,
215 HTCP_SET,
216 HTCP_CLR,
217 HTCP_END
218 };
219
220 static const char *const htcpOpcodeStr[] = {
221 "HTCP_NOP",
222 "HTCP_TST",
223 "HTCP_MON",
224 "HTCP_SET",
225 "HTCP_CLR",
226 "HTCP_END"
227 };
228
229 /*
230 * values for htcpDataHeader->response
231 */
232 enum {
233 AUTH_REQUIRED,
234 AUTH_FAILURE,
235 OPCODE_UNIMPLEMENTED,
236 MAJOR_VERSION_UNSUPPORTED,
237 MINOR_VERSION_UNSUPPORTED,
238 INVALID_OPCODE
239 };
240
241 /*
242 * values for htcpDataHeader->RR
243 */
244 enum {
245 RR_REQUEST,
246 RR_RESPONSE
247 };
248
249 static void htcpIncomingConnectionOpened(int errNo);
250 static uint32_t msg_id_counter = 0;
251
252 static Comm::ConnectionPointer htcpOutgoingConn = NULL;
253 static Comm::ConnectionPointer htcpIncomingConn = NULL;
254 #define N_QUERIED_KEYS 8192
255 static uint32_t queried_id[N_QUERIED_KEYS];
256 static cache_key queried_keys[N_QUERIED_KEYS][SQUID_MD5_DIGEST_LENGTH];
257
258 static Ip::Address queried_addr[N_QUERIED_KEYS];
259 static MemAllocator *htcpDetailPool = NULL;
260
261 static int old_squid_format = 0;
262
263
264 static ssize_t htcpBuildPacket(char *buf, size_t buflen, htcpStuff * stuff);
265 static htcpSpecifier *htcpUnpackSpecifier(char *buf, int sz);
266 static htcpDetail *htcpUnpackDetail(char *buf, int sz);
267 static ssize_t htcpBuildAuth(char *buf, size_t buflen);
268 static ssize_t htcpBuildCountstr(char *buf, size_t buflen, const char *s);
269 static ssize_t htcpBuildData(char *buf, size_t buflen, htcpStuff * stuff);
270 static ssize_t htcpBuildDetail(char *buf, size_t buflen, htcpStuff * stuff);
271 static ssize_t htcpBuildOpData(char *buf, size_t buflen, htcpStuff * stuff);
272 static ssize_t htcpBuildSpecifier(char *buf, size_t buflen, htcpStuff * stuff);
273 static ssize_t htcpBuildTstOpData(char *buf, size_t buflen, htcpStuff * stuff);
274 static void htcpFreeSpecifier(htcpSpecifier * s);
275 static void htcpFreeDetail(htcpDetail * s);
276
277 static void htcpHandleMsg(char *buf, int sz, Ip::Address &from);
278
279 static void htcpLogHtcp(Ip::Address &, int, log_type, const char *);
280 static void htcpHandleMon(htcpDataHeader *, char *buf, int sz, Ip::Address &from);
281
282 static void htcpHandleNop(htcpDataHeader *, char *buf, int sz, Ip::Address &from);
283
284 static void htcpHandleSet(htcpDataHeader *, char *buf, int sz, Ip::Address &from);
285
286 static void htcpHandleTst(htcpDataHeader *, char *buf, int sz, Ip::Address &from);
287
288 static void htcpRecv(int fd, void *data);
289
290 static void htcpSend(const char *buf, int len, Ip::Address &to);
291
292 static void htcpTstReply(htcpDataHeader *, StoreEntry *, htcpSpecifier *, Ip::Address &);
293
294 static void htcpHandleTstRequest(htcpDataHeader *, char *buf, int sz, Ip::Address &from);
295
296 static void htcpHandleTstResponse(htcpDataHeader *, char *, int, Ip::Address &);
297
298 static void
299 htcpHexdump(const char *tag, const char *s, int sz)
300 {
301 #if USE_HEXDUMP
302 int i;
303 int k;
304 char hex[80];
305 debugs(31, 3, "htcpHexdump " << tag);
306 memset(hex, '\0', 80);
307
308 for (i = 0; i < sz; i++) {
309 k = i % 16;
310 snprintf(&hex[k * 3], 4, " %02x", (int) *(s + i));
311
312 if (k < 15 && i < (sz - 1))
313 continue;
314
315 debugs(31, 3, "\t" << hex);
316
317 memset(hex, '\0', 80);
318 }
319
320 #endif
321 }
322
323 /*
324 * STUFF FOR SENDING HTCP MESSAGES
325 */
326
327 static ssize_t
328 htcpBuildAuth(char *buf, size_t buflen)
329 {
330 htcpAuthHeader auth;
331 size_t copy_sz = 0;
332 assert(2 == sizeof(uint16_t));
333 auth.length = htons(2);
334 copy_sz += 2;
335 if (buflen < copy_sz)
336 return -1;
337 memcpy(buf, &auth, copy_sz);
338 return copy_sz;
339 }
340
341 static ssize_t
342 htcpBuildCountstr(char *buf, size_t buflen, const char *s)
343 {
344 uint16_t length;
345 size_t len;
346 int off = 0;
347
348 if (buflen - off < 2)
349 return -1;
350
351 if (s)
352 len = strlen(s);
353 else
354 len = 0;
355
356 debugs(31, 3, "htcpBuildCountstr: LENGTH = " << len);
357
358 debugs(31, 3, "htcpBuildCountstr: TEXT = {" << (s ? s : "<NULL>") << "}");
359
360 length = htons((uint16_t) len);
361
362 memcpy(buf + off, &length, 2);
363
364 off += 2;
365
366 if (buflen - off < len)
367 return -1;
368
369 if (len)
370 memcpy(buf + off, s, len);
371
372 off += len;
373
374 return off;
375 }
376
377 static ssize_t
378 htcpBuildSpecifier(char *buf, size_t buflen, htcpStuff * stuff)
379 {
380 ssize_t off = 0;
381 ssize_t s;
382 s = htcpBuildCountstr(buf + off, buflen - off, stuff->S.method);
383
384 if (s < 0)
385 return s;
386
387 off += s;
388
389 s = htcpBuildCountstr(buf + off, buflen - off, stuff->S.uri);
390
391 if (s < 0)
392 return s;
393
394 off += s;
395
396 s = htcpBuildCountstr(buf + off, buflen - off, stuff->S.version);
397
398 if (s < 0)
399 return s;
400
401 off += s;
402
403 s = htcpBuildCountstr(buf + off, buflen - off, stuff->S.req_hdrs);
404
405 if (s < 0)
406 return s;
407
408 off += s;
409
410 debugs(31, 3, "htcpBuildSpecifier: size " << off);
411
412 return off;
413 }
414
415 static ssize_t
416 htcpBuildDetail(char *buf, size_t buflen, htcpStuff * stuff)
417 {
418 ssize_t off = 0;
419 ssize_t s;
420 s = htcpBuildCountstr(buf + off, buflen - off, stuff->D.resp_hdrs);
421
422 if (s < 0)
423 return s;
424
425 off += s;
426
427 s = htcpBuildCountstr(buf + off, buflen - off, stuff->D.entity_hdrs);
428
429 if (s < 0)
430 return s;
431
432 off += s;
433
434 s = htcpBuildCountstr(buf + off, buflen - off, stuff->D.cache_hdrs);
435
436 if (s < 0)
437 return s;
438
439 off += s;
440
441 return off;
442 }
443
444 static ssize_t
445 htcpBuildTstOpData(char *buf, size_t buflen, htcpStuff * stuff)
446 {
447 switch (stuff->rr) {
448
449 case RR_REQUEST:
450 debugs(31, 3, "htcpBuildTstOpData: RR_REQUEST");
451 return htcpBuildSpecifier(buf, buflen, stuff);
452
453 case RR_RESPONSE:
454 debugs(31, 3, "htcpBuildTstOpData: RR_RESPONSE");
455 debugs(31, 3, "htcpBuildTstOpData: F1 = " << stuff->f1);
456
457 if (stuff->f1) /* cache miss */
458 return 0;
459 else /* cache hit */
460 return htcpBuildDetail(buf, buflen, stuff);
461
462 default:
463 fatal_dump("htcpBuildTstOpData: bad RR value");
464 }
465
466 return 0;
467 }
468
469 static ssize_t
470 htcpBuildClrOpData(char *buf, size_t buflen, htcpStuff * stuff)
471 {
472 unsigned short reason;
473
474 switch (stuff->rr) {
475 case RR_REQUEST:
476 debugs(31, 3, "htcpBuildClrOpData: RR_REQUEST");
477 reason = htons((unsigned short)stuff->reason);
478 memcpy(buf, &reason, 2);
479 return htcpBuildSpecifier(buf + 2, buflen - 2, stuff) + 2;
480 case RR_RESPONSE:
481 break;
482 default:
483 fatal_dump("htcpBuildClrOpData: bad RR value");
484 }
485
486 return 0;
487 }
488
489 static ssize_t
490 htcpBuildOpData(char *buf, size_t buflen, htcpStuff * stuff)
491 {
492 ssize_t off = 0;
493 debugs(31, 3, "htcpBuildOpData: opcode " << htcpOpcodeStr[stuff->op]);
494
495 switch (stuff->op) {
496
497 case HTCP_TST:
498 off = htcpBuildTstOpData(buf + off, buflen, stuff);
499 break;
500
501 case HTCP_CLR:
502 off = htcpBuildClrOpData(buf + off, buflen, stuff);
503 break;
504
505 default:
506 assert(0);
507 break;
508 }
509
510 return off;
511 }
512
513 static ssize_t
514 htcpBuildData(char *buf, size_t buflen, htcpStuff * stuff)
515 {
516 ssize_t off = 0;
517 ssize_t op_data_sz;
518 size_t hdr_sz = sizeof(htcpDataHeader);
519 htcpDataHeader hdr;
520
521 if (buflen < hdr_sz)
522 return -1;
523
524 off += hdr_sz; /* skip! */
525
526 op_data_sz = htcpBuildOpData(buf + off, buflen - off, stuff);
527
528 if (op_data_sz < 0)
529 return op_data_sz;
530
531 off += op_data_sz;
532
533 debugs(31, 3, "htcpBuildData: hdr.length = " << off);
534
535 hdr.length = (uint16_t) off;
536
537 hdr.opcode = stuff->op;
538
539 hdr.response = stuff->response;
540
541 hdr.RR = stuff->rr;
542
543 hdr.F1 = stuff->f1;
544
545 hdr.msg_id = stuff->msg_id;
546
547 /* convert multi-byte fields */
548 hdr.length = htons(hdr.length);
549
550 hdr.msg_id = htonl(hdr.msg_id);
551
552 if (!old_squid_format) {
553 memcpy(buf, &hdr, hdr_sz);
554 } else {
555 htcpDataHeaderSquid hdrSquid;
556 memset(&hdrSquid, 0, sizeof(hdrSquid));
557 hdrSquid.length = hdr.length;
558 hdrSquid.opcode = hdr.opcode;
559 hdrSquid.response = hdr.response;
560 hdrSquid.F1 = hdr.F1;
561 hdrSquid.RR = hdr.RR;
562 memcpy(buf, &hdrSquid, hdr_sz);
563 }
564
565 debugs(31, 3, "htcpBuildData: size " << off);
566
567 return off;
568 }
569
570 /*
571 * Build an HTCP packet into buf, maximum length buflen.
572 * Returns the packet length, or zero on failure.
573 */
574 static ssize_t
575 htcpBuildPacket(char *buf, size_t buflen, htcpStuff * stuff)
576 {
577 ssize_t s;
578 ssize_t off = 0;
579 size_t hdr_sz = sizeof(htcpHeader);
580 htcpHeader hdr;
581 /* skip the header -- we don't know the overall length */
582
583 if (buflen < hdr_sz) {
584 return 0;
585 }
586
587 off += hdr_sz;
588 s = htcpBuildData(buf + off, buflen - off, stuff);
589
590 if (s < 0) {
591 return 0;
592 }
593
594 off += s;
595 s = htcpBuildAuth(buf + off, buflen - off);
596
597 if (s < 0) {
598 return 0;
599 }
600
601 off += s;
602 hdr.length = htons((uint16_t) off);
603 hdr.major = 0;
604
605 if (old_squid_format)
606 hdr.minor = 0;
607 else
608 hdr.minor = 1;
609
610 memcpy(buf, &hdr, hdr_sz);
611
612 debugs(31, 3, "htcpBuildPacket: size " << off);
613
614 return off;
615 }
616
617 static void
618 htcpSend(const char *buf, int len, Ip::Address &to)
619 {
620 debugs(31, 3, HERE << to);
621 htcpHexdump("htcpSend", buf, len);
622
623 if (comm_udp_sendto(htcpOutgoingConn->fd, to, buf, len) < 0)
624 debugs(31, 3, HERE << htcpOutgoingConn << " sendto: " << xstrerror());
625 else
626 statCounter.htcp.pkts_sent++;
627 }
628
629 /*
630 * STUFF FOR RECEIVING HTCP MESSAGES
631 */
632
633 void
634 htcpSpecifier::setFrom(Ip::Address &aSocket)
635 {
636 from = aSocket;
637 }
638
639 void
640 htcpSpecifier::setDataHeader(htcpDataHeader *aDataHeader)
641 {
642 dhdr = aDataHeader;
643 }
644
645 static void
646 htcpFreeSpecifier(htcpSpecifier * s)
647 {
648 HTTPMSGUNLOCK(s->request);
649
650 delete s;
651 }
652
653 static void
654 htcpFreeDetail(htcpDetail * d)
655 {
656 htcpDetailPool->freeOne(d);
657 }
658
659 /*
660 * Unpack an HTCP SPECIFIER in place
661 * This will overwrite any following AUTH block
662 */
663 static htcpSpecifier *
664 htcpUnpackSpecifier(char *buf, int sz)
665 {
666 htcpSpecifier *s = new htcpSpecifier;
667 HttpRequestMethod method;
668
669 /* Find length of METHOD */
670 uint16_t l = ntohs(*(uint16_t *) buf);
671 sz -= 2;
672 buf += 2;
673
674 if (l > sz) {
675 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack METHOD");
676 htcpFreeSpecifier(s);
677 return NULL;
678 }
679
680 /* Set METHOD */
681 s->method = buf;
682 buf += l;
683 sz -= l;
684 debugs(31, 6, "htcpUnpackSpecifier: METHOD (" << l << "/" << sz << ") '" << s->method << "'");
685
686 /* Find length of URI */
687 l = ntohs(*(uint16_t *) buf);
688 sz -= 2;
689
690 if (l > sz) {
691 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack URI");
692 htcpFreeSpecifier(s);
693 return NULL;
694 }
695
696 /* Add terminating null to METHOD */
697 *buf = '\0';
698 buf += 2;
699
700 /* Set URI */
701 s->uri = buf;
702 buf += l;
703 sz -= l;
704 debugs(31, 6, "htcpUnpackSpecifier: URI (" << l << "/" << sz << ") '" << s->uri << "'");
705
706 /* Find length of VERSION */
707 l = ntohs(*(uint16_t *) buf);
708 sz -= 2;
709
710 if (l > sz) {
711 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack VERSION");
712 htcpFreeSpecifier(s);
713 return NULL;
714 }
715
716 /* Add terminating null to URI */
717 *buf = '\0';
718 buf += 2;
719
720 /* Set VERSION */
721 s->version = buf;
722 buf += l;
723 sz -= l;
724 debugs(31, 6, "htcpUnpackSpecifier: VERSION (" << l << "/" << sz << ") '" << s->version << "'");
725
726 /* Find length of REQ-HDRS */
727 l = ntohs(*(uint16_t *) buf);
728 sz -= 2;
729
730 if (l > sz) {
731 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack REQ-HDRS");
732 htcpFreeSpecifier(s);
733 return NULL;
734 }
735
736 /* Add terminating null to URI */
737 *buf = '\0';
738 buf += 2;
739
740 /* Set REQ-HDRS */
741 s->req_hdrs = buf;
742 buf += l;
743 sz -= l;
744 debugs(31, 6, "htcpUnpackSpecifier: REQ-HDRS (" << l << "/" << sz << ") '" << s->req_hdrs << "'");
745
746 debugs(31, 3, "htcpUnpackSpecifier: " << sz << " bytes left");
747
748 /*
749 * Add terminating null to REQ-HDRS. This is possible because we allocated
750 * an extra byte when we received the packet. This will overwrite any following
751 * AUTH block.
752 */
753 *buf = '\0';
754
755 /*
756 * Parse the request
757 */
758 method = HttpRequestMethod(s->method, NULL);
759
760 s->request = HttpRequest::CreateFromUrlAndMethod(s->uri, method == METHOD_NONE ? HttpRequestMethod(METHOD_GET) : method);
761
762 if (s->request)
763 HTTPMSGLOCK(s->request);
764
765 return s;
766 }
767
768 /*
769 * Unpack an HTCP DETAIL in place
770 * This will overwrite any following AUTH block
771 */
772 static htcpDetail *
773 htcpUnpackDetail(char *buf, int sz)
774 {
775 htcpDetail *d = static_cast<htcpDetail *>(htcpDetailPool->alloc());
776
777 /* Find length of RESP-HDRS */
778 uint16_t l = ntohs(*(uint16_t *) buf);
779 sz -= 2;
780 buf += 2;
781
782 if (l > sz) {
783 debugs(31, 3, "htcpUnpackDetail: failed to unpack RESP_HDRS");
784 htcpFreeDetail(d);
785 return NULL;
786 }
787
788 /* Set RESP-HDRS */
789 d->resp_hdrs = buf;
790
791 buf += l;
792
793 sz -= l;
794
795 /* Find length of ENTITY-HDRS */
796 l = ntohs(*(uint16_t *) buf);
797
798 sz -= 2;
799
800 if (l > sz) {
801 debugs(31, 3, "htcpUnpackDetail: failed to unpack ENTITY_HDRS");
802 htcpFreeDetail(d);
803 return NULL;
804 }
805
806 /* Add terminating null to RESP-HDRS */
807 *buf = '\0';
808
809 /* Set ENTITY-HDRS */
810 buf += 2;
811
812 d->entity_hdrs = buf;
813
814 buf += l;
815
816 sz -= l;
817
818 /* Find length of CACHE-HDRS */
819 l = ntohs(*(uint16_t *) buf);
820
821 sz -= 2;
822
823 if (l > sz) {
824 debugs(31, 3, "htcpUnpackDetail: failed to unpack CACHE_HDRS");
825 htcpFreeDetail(d);
826 return NULL;
827 }
828
829 /* Add terminating null to ENTITY-HDRS */
830 *buf = '\0';
831
832 /* Set CACHE-HDRS */
833 buf += 2;
834
835 d->cache_hdrs = buf;
836
837 buf += l;
838
839 sz -= l;
840
841 debugs(31, 3, "htcpUnpackDetail: " << sz << " bytes left");
842
843 /*
844 * Add terminating null to CACHE-HDRS. This is possible because we allocated
845 * an extra byte when we received the packet. This will overwrite any following
846 * AUTH block.
847 */
848 *buf = '\0';
849
850 return d;
851 }
852
853 static bool
854 htcpAccessAllowed(acl_access * acl, htcpSpecifier * s, Ip::Address &from)
855 {
856 /* default deny if no access list present */
857 if (!acl)
858 return false;
859
860 ACLFilledChecklist checklist(acl, s->request, NULL);
861 checklist.src_addr = from;
862 checklist.my_addr.SetNoAddr();
863 return (checklist.fastCheck() == ACCESS_ALLOWED);
864 }
865
866 static void
867 htcpTstReply(htcpDataHeader * dhdr, StoreEntry * e, htcpSpecifier * spec, Ip::Address &from)
868 {
869 htcpStuff stuff;
870 static char pkt[8192];
871 HttpHeader hdr(hoHtcpReply);
872 MemBuf mb;
873 Packer p;
874 ssize_t pktlen;
875 memset(&stuff, '\0', sizeof(stuff));
876 stuff.op = HTCP_TST;
877 stuff.rr = RR_RESPONSE;
878 stuff.f1 = 0;
879 stuff.response = e ? 0 : 1;
880 debugs(31, 3, "htcpTstReply: response = " << stuff.response);
881 stuff.msg_id = dhdr->msg_id;
882
883 if (spec) {
884 mb.init();
885 packerToMemInit(&p, &mb);
886 stuff.S.method = spec->method;
887 stuff.S.uri = spec->uri;
888 stuff.S.version = spec->version;
889 stuff.S.req_hdrs = spec->req_hdrs;
890 if (e)
891 hdr.putInt(HDR_AGE, (e->timestamp <= squid_curtime ? (squid_curtime - e->timestamp) : 0) );
892 else
893 hdr.putInt(HDR_AGE, 0);
894 hdr.packInto(&p);
895 stuff.D.resp_hdrs = xstrdup(mb.buf);
896 debugs(31, 3, "htcpTstReply: resp_hdrs = {" << stuff.D.resp_hdrs << "}");
897 mb.reset();
898 hdr.reset();
899
900 if (e && e->expires > -1)
901 hdr.putTime(HDR_EXPIRES, e->expires);
902
903 if (e && e->lastmod > -1)
904 hdr.putTime(HDR_LAST_MODIFIED, e->lastmod);
905
906 hdr.packInto(&p);
907
908 stuff.D.entity_hdrs = xstrdup(mb.buf);
909
910 debugs(31, 3, "htcpTstReply: entity_hdrs = {" << stuff.D.entity_hdrs << "}");
911
912 mb.reset();
913
914 hdr.reset();
915
916 #if USE_ICMP
917 if (char *host = urlHostname(spec->uri)) {
918 int rtt = 0;
919 int hops = 0;
920 int samp = 0;
921 netdbHostData(host, &samp, &rtt, &hops);
922
923 if (rtt || hops) {
924 char cto_buf[128];
925 snprintf(cto_buf, 128, "%s %d %f %d",
926 host, samp, 0.001 * rtt, hops);
927 hdr.putExt("Cache-to-Origin", cto_buf);
928 }
929 }
930 #endif /* USE_ICMP */
931
932 hdr.packInto(&p);
933 stuff.D.cache_hdrs = xstrdup(mb.buf);
934 debugs(31, 3, "htcpTstReply: cache_hdrs = {" << stuff.D.cache_hdrs << "}");
935 mb.clean();
936 hdr.clean();
937 packerClean(&p);
938 }
939
940 pktlen = htcpBuildPacket(pkt, sizeof(pkt), &stuff);
941
942 safe_free(stuff.D.resp_hdrs);
943 safe_free(stuff.D.entity_hdrs);
944 safe_free(stuff.D.cache_hdrs);
945
946 if (!pktlen) {
947 debugs(31, 3, "htcpTstReply: htcpBuildPacket() failed");
948 return;
949 }
950
951 htcpSend(pkt, (int) pktlen, from);
952 }
953
954 static void
955
956 htcpClrReply(htcpDataHeader * dhdr, int purgeSucceeded, Ip::Address &from)
957 {
958 htcpStuff stuff;
959 static char pkt[8192];
960 ssize_t pktlen;
961
962 /* If dhdr->F1 == 0, no response desired */
963
964 if (dhdr->F1 == 0)
965 return;
966
967 memset(&stuff, '\0', sizeof(stuff));
968
969 stuff.op = HTCP_CLR;
970
971 stuff.rr = RR_RESPONSE;
972
973 stuff.f1 = 0;
974
975 stuff.response = purgeSucceeded ? 0 : 2;
976
977 debugs(31, 3, "htcpClrReply: response = " << stuff.response);
978
979 stuff.msg_id = dhdr->msg_id;
980
981 pktlen = htcpBuildPacket(pkt, sizeof(pkt), &stuff);
982
983 if (pktlen == 0) {
984 debugs(31, 3, "htcpClrReply: htcpBuildPacket() failed");
985 return;
986 }
987
988 htcpSend(pkt, (int) pktlen, from);
989 }
990
991 static void
992
993 htcpHandleNop(htcpDataHeader * hdr, char *buf, int sz, Ip::Address &from)
994 {
995 debugs(31, 3, "htcpHandleNop: Unimplemented");
996 }
997
998 void
999 htcpSpecifier::checkHit()
1000 {
1001 char *blk_end;
1002 checkHitRequest = request;
1003
1004 if (NULL == checkHitRequest) {
1005 debugs(31, 3, "htcpCheckHit: NO; failed to parse URL");
1006 checkedHit(NullStoreEntry::getInstance());
1007 return;
1008 }
1009
1010 blk_end = req_hdrs + strlen(req_hdrs);
1011
1012 if (!checkHitRequest->header.parse(req_hdrs, blk_end)) {
1013 debugs(31, 3, "htcpCheckHit: NO; failed to parse request headers");
1014 delete checkHitRequest;
1015 checkHitRequest = NULL;
1016 checkedHit(NullStoreEntry::getInstance());
1017 return;
1018 }
1019
1020 StoreEntry::getPublicByRequest(this, checkHitRequest);
1021 }
1022
1023 void
1024 htcpSpecifier::created (StoreEntry *e)
1025 {
1026 StoreEntry *hit=NULL;
1027 assert (e);
1028
1029 if (e->isNull()) {
1030 debugs(31, 3, "htcpCheckHit: NO; public object not found");
1031 } else if (!e->validToSend()) {
1032 debugs(31, 3, "htcpCheckHit: NO; entry not valid to send" );
1033 } else if (refreshCheckHTCP(e, checkHitRequest)) {
1034 debugs(31, 3, "htcpCheckHit: NO; cached response is stale");
1035 } else {
1036 debugs(31, 3, "htcpCheckHit: YES!?");
1037 hit = e;
1038 }
1039
1040 checkedHit (hit);
1041 }
1042
1043 static void
1044 htcpClrStoreEntry(StoreEntry * e)
1045 {
1046 debugs(31, 4, "htcpClrStoreEntry: Clearing store for entry: " << e->url() );
1047 e->releaseRequest();
1048 }
1049
1050 static int
1051 htcpClrStore(const htcpSpecifier * s)
1052 {
1053 HttpRequest *request = s->request;
1054 char *blk_end;
1055 StoreEntry *e = NULL;
1056 int released = 0;
1057
1058 if (request == NULL) {
1059 debugs(31, 3, "htcpClrStore: failed to parse URL");
1060 return -1;
1061 }
1062
1063 /* Parse request headers */
1064 blk_end = s->req_hdrs + strlen(s->req_hdrs);
1065
1066 if (!request->header.parse(s->req_hdrs, blk_end)) {
1067 debugs(31, 2, "htcpClrStore: failed to parse request headers");
1068 return -1;
1069 }
1070
1071 /* Lookup matching entries. This matches both GET and HEAD */
1072 while ((e = storeGetPublicByRequest(request)) != NULL) {
1073 if (e != NULL) {
1074 htcpClrStoreEntry(e);
1075 released++;
1076 }
1077 }
1078
1079 if (released) {
1080 debugs(31, 4, "htcpClrStore: Cleared " << released << " matching entries");
1081 return 1;
1082 } else {
1083 debugs(31, 4, "htcpClrStore: No matching entry found");
1084 return 0;
1085 }
1086 }
1087
1088 static void
1089
1090 htcpHandleTst(htcpDataHeader * hdr, char *buf, int sz, Ip::Address &from)
1091 {
1092 debugs(31, 3, "htcpHandleTst: sz = " << sz);
1093
1094 if (hdr->RR == RR_REQUEST)
1095 htcpHandleTstRequest(hdr, buf, sz, from);
1096 else
1097 htcpHandleTstResponse(hdr, buf, sz, from);
1098 }
1099
1100 HtcpReplyData::HtcpReplyData() : hdr(hoHtcpReply)
1101 {}
1102
1103 static void
1104
1105 htcpHandleTstResponse(htcpDataHeader * hdr, char *buf, int sz, Ip::Address &from)
1106 {
1107 htcpReplyData htcpReply;
1108 cache_key *key = NULL;
1109
1110 Ip::Address *peer;
1111 htcpDetail *d = NULL;
1112 char *t;
1113
1114 if (queried_id[hdr->msg_id % N_QUERIED_KEYS] != hdr->msg_id) {
1115 debugs(31, 2, "htcpHandleTstResponse: No matching query id '" <<
1116 hdr->msg_id << "' (expected " <<
1117 queried_id[hdr->msg_id % N_QUERIED_KEYS] << ") from '" <<
1118 from << "'");
1119
1120 return;
1121 }
1122
1123 key = queried_keys[hdr->msg_id % N_QUERIED_KEYS];
1124
1125 if (!key) {
1126 debugs(31, 3, "htcpHandleTstResponse: No query key for response id '" << hdr->msg_id << "' from '" << from << "'");
1127 return;
1128 }
1129
1130 peer = &queried_addr[hdr->msg_id % N_QUERIED_KEYS];
1131
1132 if ( *peer != from || peer->GetPort() != from.GetPort() ) {
1133 debugs(31, 3, "htcpHandleTstResponse: Unexpected response source " << from );
1134 return;
1135 }
1136
1137 if (hdr->F1 == 1) {
1138 debugs(31, 2, "htcpHandleTstResponse: error condition, F1/MO == 1");
1139 return;
1140 }
1141
1142 htcpReply.msg_id = hdr->msg_id;
1143 debugs(31, 3, "htcpHandleTstResponse: msg_id = " << htcpReply.msg_id);
1144 htcpReply.hit = hdr->response ? 0 : 1;
1145
1146 if (hdr->F1) {
1147 debugs(31, 3, "htcpHandleTstResponse: MISS");
1148 } else {
1149 debugs(31, 3, "htcpHandleTstResponse: HIT");
1150 d = htcpUnpackDetail(buf, sz);
1151
1152 if (d == NULL) {
1153 debugs(31, 3, "htcpHandleTstResponse: bad DETAIL");
1154 return;
1155 }
1156
1157 if ((t = d->resp_hdrs))
1158 htcpReply.hdr.parse(t, t + strlen(t));
1159
1160 if ((t = d->entity_hdrs))
1161 htcpReply.hdr.parse(t, t + strlen(t));
1162
1163 if ((t = d->cache_hdrs))
1164 htcpReply.hdr.parse(t, t + strlen(t));
1165 }
1166
1167 debugs(31, 3, "htcpHandleTstResponse: key (" << key << ") " << storeKeyText(key));
1168 neighborsHtcpReply(key, &htcpReply, from);
1169 htcpReply.hdr.clean();
1170
1171 if (d)
1172 htcpFreeDetail(d);
1173 }
1174
1175 static void
1176 htcpHandleTstRequest(htcpDataHeader * dhdr, char *buf, int sz, Ip::Address &from)
1177 {
1178 /* buf should be a SPECIFIER */
1179 htcpSpecifier *s;
1180
1181 if (sz == 0) {
1182 debugs(31, 3, "htcpHandleTst: nothing to do");
1183 return;
1184 }
1185
1186 if (dhdr->F1 == 0)
1187 return;
1188
1189 /* s is a new object */
1190 s = htcpUnpackSpecifier(buf, sz);
1191
1192 s->setFrom(from);
1193
1194 s->setDataHeader(dhdr);
1195
1196 if (NULL == s) {
1197 debugs(31, 3, "htcpHandleTstRequest: htcpUnpackSpecifier failed");
1198 htcpLogHtcp(from, dhdr->opcode, LOG_UDP_INVALID, dash_str);
1199 return;
1200 }
1201
1202 if (!s->request) {
1203 debugs(31, 3, "htcpHandleTstRequest: failed to parse request");
1204 htcpLogHtcp(from, dhdr->opcode, LOG_UDP_INVALID, dash_str);
1205 htcpFreeSpecifier(s);
1206 return;
1207 }
1208
1209 if (!htcpAccessAllowed(Config.accessList.htcp, s, from)) {
1210 debugs(31, 3, "htcpHandleTstRequest: Access denied");
1211 htcpLogHtcp(from, dhdr->opcode, LOG_UDP_DENIED, s->uri);
1212 htcpFreeSpecifier(s);
1213 return;
1214 }
1215
1216 debugs(31, 2, "HTCP TST request: " << s->method << " " << s->uri << " " << s->version);
1217 debugs(31, 2, "HTCP TST headers: " << s->req_hdrs);
1218 s->checkHit();
1219 }
1220
1221 void
1222 htcpSpecifier::checkedHit(StoreEntry *e)
1223 {
1224 if (e) {
1225 htcpTstReply(dhdr, e, this, from); /* hit */
1226 htcpLogHtcp(from, dhdr->opcode, LOG_UDP_HIT, uri);
1227 } else {
1228 htcpTstReply(dhdr, NULL, NULL, from); /* cache miss */
1229 htcpLogHtcp(from, dhdr->opcode, LOG_UDP_MISS, uri);
1230 }
1231
1232 htcpFreeSpecifier(this);
1233 }
1234
1235 static void
1236
1237 htcpHandleMon(htcpDataHeader * hdr, char *buf, int sz, Ip::Address &from)
1238 {
1239 debugs(31, 3, "htcpHandleMon: Unimplemented");
1240 }
1241
1242 static void
1243
1244 htcpHandleSet(htcpDataHeader * hdr, char *buf, int sz, Ip::Address &from)
1245 {
1246 debugs(31, 3, "htcpHandleSet: Unimplemented");
1247 }
1248
1249 static void
1250 htcpHandleClr(htcpDataHeader * hdr, char *buf, int sz, Ip::Address &from)
1251 {
1252 htcpSpecifier *s;
1253 /* buf[0/1] is reserved and reason */
1254 int reason = buf[1] << 4;
1255 debugs(31, 2, "HTCP CLR reason: " << reason);
1256 buf += 2;
1257 sz -= 2;
1258
1259 /* buf should be a SPECIFIER */
1260
1261 if (sz == 0) {
1262 debugs(31, 4, "htcpHandleClr: nothing to do");
1263 htcpLogHtcp(from, hdr->opcode, LOG_UDP_INVALID, dash_str);
1264 return;
1265 }
1266
1267 s = htcpUnpackSpecifier(buf, sz);
1268
1269 if (NULL == s) {
1270 debugs(31, 3, "htcpHandleClr: htcpUnpackSpecifier failed");
1271 htcpLogHtcp(from, hdr->opcode, LOG_UDP_INVALID, dash_str);
1272 return;
1273 }
1274
1275 if (!s->request) {
1276 debugs(31, 3, "htcpHandleTstRequest: failed to parse request");
1277 htcpLogHtcp(from, hdr->opcode, LOG_UDP_INVALID, dash_str);
1278 htcpFreeSpecifier(s);
1279 return;
1280 }
1281
1282 if (!htcpAccessAllowed(Config.accessList.htcp_clr, s, from)) {
1283 debugs(31, 3, "htcpHandleClr: Access denied");
1284 htcpLogHtcp(from, hdr->opcode, LOG_UDP_DENIED, s->uri);
1285 htcpFreeSpecifier(s);
1286 return;
1287 }
1288
1289 debugs(31, 2, "HTCP CLR request: " << s->method << " " << s->uri << " " << s->version);
1290 debugs(31, 2, "HTCP CLR headers: " << s->req_hdrs);
1291
1292 /* Release objects from cache
1293 * analog to clientPurgeRequest in client_side.c
1294 */
1295
1296 switch (htcpClrStore(s)) {
1297
1298 case 1:
1299 htcpClrReply(hdr, 1, from); /* hit */
1300 htcpLogHtcp(from, hdr->opcode, LOG_UDP_HIT, s->uri);
1301 break;
1302
1303 case 0:
1304 htcpClrReply(hdr, 0, from); /* miss */
1305 htcpLogHtcp(from, hdr->opcode, LOG_UDP_MISS, s->uri);
1306 break;
1307
1308 default:
1309 break;
1310 }
1311
1312 htcpFreeSpecifier(s);
1313 }
1314
1315 /*
1316 * Forward a CLR request to all peers who have requested that CLRs be
1317 * forwarded to them.
1318 */
1319 static void
1320 htcpForwardClr(char *buf, int sz)
1321 {
1322 peer *p;
1323
1324 for (p = Config.peers; p; p = p->next) {
1325 if (!p->options.htcp) {
1326 continue;
1327 }
1328 if (!p->options.htcp_forward_clr) {
1329 continue;
1330 }
1331
1332 htcpSend(buf, sz, p->in_addr);
1333 }
1334 }
1335
1336 /*
1337 * Do the first pass of handling an HTCP message. This used to be two
1338 * separate functions, htcpHandle and htcpHandleData. They were merged to
1339 * allow for forwarding HTCP packets easily to other peers if desired.
1340 *
1341 * This function now works out what type of message we have received and then
1342 * hands it off to other functions to break apart message-specific data.
1343 */
1344 static void
1345 htcpHandleMsg(char *buf, int sz, Ip::Address &from)
1346 {
1347 htcpHeader htcpHdr;
1348 htcpDataHeader hdr;
1349 char *hbuf;
1350 int hsz;
1351
1352 if (sz < 0 || (size_t)sz < sizeof(htcpHeader)) {
1353 // These are highly likely to be attack packets. Should probably get a bigger warning.
1354 debugs(31, 2, "htcpHandle: msg size less than htcpHeader size from " << from);
1355 return;
1356 }
1357
1358 htcpHexdump("htcpHandle", buf, sz);
1359 memcpy(&htcpHdr, buf, sizeof(htcpHeader));
1360 htcpHdr.length = ntohs(htcpHdr.length);
1361
1362 if (htcpHdr.minor == 0)
1363 old_squid_format = 1;
1364 else
1365 old_squid_format = 0;
1366
1367 debugs(31, 3, "htcpHandle: htcpHdr.length = " << htcpHdr.length);
1368 debugs(31, 3, "htcpHandle: htcpHdr.major = " << htcpHdr.major);
1369 debugs(31, 3, "htcpHandle: htcpHdr.minor = " << htcpHdr.minor);
1370
1371 if (sz != htcpHdr.length) {
1372 debugs(31, 3, "htcpHandle: sz/" << sz << " != htcpHdr.length/" <<
1373 htcpHdr.length << " from " << from );
1374
1375 return;
1376 }
1377
1378 if (htcpHdr.major != 0) {
1379 debugs(31, 3, "htcpHandle: Unknown major version " << htcpHdr.major << " from " << from );
1380
1381 return;
1382 }
1383
1384 hbuf = buf + sizeof(htcpHeader);
1385 hsz = sz - sizeof(htcpHeader);
1386
1387 if ((size_t)hsz < sizeof(htcpDataHeader)) {
1388 debugs(31, 3, "htcpHandleData: msg size less than htcpDataHeader size");
1389 return;
1390 }
1391
1392 if (!old_squid_format) {
1393 memcpy(&hdr, hbuf, sizeof(hdr));
1394 } else {
1395 htcpDataHeaderSquid hdrSquid;
1396 memcpy(&hdrSquid, hbuf, sizeof(hdrSquid));
1397 hdr.length = hdrSquid.length;
1398 hdr.opcode = hdrSquid.opcode;
1399 hdr.response = hdrSquid.response;
1400 hdr.F1 = hdrSquid.F1;
1401 hdr.RR = hdrSquid.RR;
1402 hdr.reserved = 0;
1403 hdr.msg_id = hdrSquid.msg_id;
1404 }
1405
1406 hdr.length = ntohs(hdr.length);
1407 hdr.msg_id = ntohl(hdr.msg_id);
1408 debugs(31, 3, "htcpHandleData: hsz = " << hsz);
1409 debugs(31, 3, "htcpHandleData: length = " << hdr.length);
1410
1411 if (hdr.opcode >= HTCP_END) {
1412 debugs(31, 3, "htcpHandleData: client " << from << ", opcode " << hdr.opcode << " out of range");
1413 return;
1414 }
1415
1416 debugs(31, 3, "htcpHandleData: opcode = " << hdr.opcode << " " << htcpOpcodeStr[hdr.opcode]);
1417 debugs(31, 3, "htcpHandleData: response = " << hdr.response);
1418 debugs(31, 3, "htcpHandleData: F1 = " << hdr.F1);
1419 debugs(31, 3, "htcpHandleData: RR = " << hdr.RR);
1420 debugs(31, 3, "htcpHandleData: msg_id = " << hdr.msg_id);
1421
1422 if (hsz < hdr.length) {
1423 debugs(31, 3, "htcpHandleData: sz < hdr.length");
1424 return;
1425 }
1426
1427 /*
1428 * set sz = hdr.length so we ignore any AUTH fields following
1429 * the DATA.
1430 */
1431 hsz = (int) hdr.length;
1432 hbuf += sizeof(htcpDataHeader);
1433 hsz -= sizeof(htcpDataHeader);
1434 debugs(31, 3, "htcpHandleData: hsz = " << hsz);
1435
1436 htcpHexdump("htcpHandleData", hbuf, hsz);
1437
1438 switch (hdr.opcode) {
1439 case HTCP_NOP:
1440 htcpHandleNop(&hdr, hbuf, hsz, from);
1441 break;
1442 case HTCP_TST:
1443 htcpHandleTst(&hdr, hbuf, hsz, from);
1444 break;
1445 case HTCP_MON:
1446 htcpHandleMon(&hdr, hbuf, hsz, from);
1447 break;
1448 case HTCP_SET:
1449 htcpHandleSet(&hdr, hbuf, hsz, from);
1450 break;
1451 case HTCP_CLR:
1452 htcpHandleClr(&hdr, hbuf, hsz, from);
1453 htcpForwardClr(buf, sz);
1454 break;
1455 default:
1456 break;
1457 }
1458 }
1459
1460 static void
1461 htcpRecv(int fd, void *data)
1462 {
1463 static char buf[8192];
1464 int len;
1465 static Ip::Address from;
1466
1467 /* Receive up to 8191 bytes, leaving room for a null */
1468
1469 len = comm_udp_recvfrom(fd, buf, sizeof(buf) - 1, 0, from);
1470
1471 debugs(31, 3, "htcpRecv: FD " << fd << ", " << len << " bytes from " << from );
1472
1473 if (len)
1474 statCounter.htcp.pkts_recv++;
1475
1476 htcpHandleMsg(buf, len, from);
1477
1478 Comm::SetSelect(fd, COMM_SELECT_READ, htcpRecv, NULL, 0);
1479 }
1480
1481 /*
1482 * ======================================================================
1483 * PUBLIC FUNCTIONS
1484 * ======================================================================
1485 */
1486
1487 void
1488 htcpInit(void)
1489 {
1490 if (Config.Port.htcp <= 0) {
1491 debugs(31, DBG_IMPORTANT, "HTCP Disabled.");
1492 return;
1493 }
1494
1495 htcpIncomingConn = new Comm::Connection;
1496 htcpIncomingConn->local = Config.Addrs.udp_incoming;
1497 htcpIncomingConn->local.SetPort(Config.Port.htcp);
1498
1499 if (!Ip::EnableIpv6 && !htcpIncomingConn->local.SetIPv4()) {
1500 debugs(31, DBG_CRITICAL, "ERROR: IPv6 is disabled. " << htcpIncomingConn->local << " is not an IPv4 address.");
1501 fatal("HTCP port cannot be opened.");
1502 }
1503 /* split-stack for now requires default IPv4-only HTCP */
1504 if (Ip::EnableIpv6&IPV6_SPECIAL_SPLITSTACK && htcpIncomingConn->local.IsAnyAddr()) {
1505 htcpIncomingConn->local.SetIPv4();
1506 }
1507
1508 AsyncCall::Pointer call = asyncCall(31, 2,
1509 "htcpIncomingConnectionOpened",
1510 HtcpListeningStartedDialer(&htcpIncomingConnectionOpened));
1511
1512 Ipc::StartListening(SOCK_DGRAM,
1513 IPPROTO_UDP,
1514 htcpIncomingConn,
1515 Ipc::fdnInHtcpSocket, call);
1516
1517 if (!Config.Addrs.udp_outgoing.IsNoAddr()) {
1518 htcpOutgoingConn = new Comm::Connection;
1519 htcpOutgoingConn->local = Config.Addrs.udp_outgoing;
1520 htcpOutgoingConn->local.SetPort(Config.Port.htcp);
1521
1522 if (!Ip::EnableIpv6 && !htcpOutgoingConn->local.SetIPv4()) {
1523 debugs(31, DBG_CRITICAL, "ERROR: IPv6 is disabled. " << htcpOutgoingConn->local << " is not an IPv4 address.");
1524 fatal("HTCP port cannot be opened.");
1525 }
1526 /* split-stack for now requires default IPv4-only HTCP */
1527 if (Ip::EnableIpv6&IPV6_SPECIAL_SPLITSTACK && htcpOutgoingConn->local.IsAnyAddr()) {
1528 htcpOutgoingConn->local.SetIPv4();
1529 }
1530
1531 enter_suid();
1532 comm_open_listener(SOCK_DGRAM, IPPROTO_UDP, htcpOutgoingConn, "Outgoing HTCP Socket");
1533 leave_suid();
1534
1535 if (!Comm::IsConnOpen(htcpOutgoingConn))
1536 fatal("Cannot open Outgoing HTCP Socket");
1537
1538 Comm::SetSelect(htcpOutgoingConn->fd, COMM_SELECT_READ, htcpRecv, NULL, 0);
1539
1540 debugs(31, DBG_IMPORTANT, "Sending HTCP messages from " << htcpOutgoingConn->local);
1541 }
1542
1543 if (!htcpDetailPool) {
1544 htcpDetailPool = memPoolCreate("htcpDetail", sizeof(htcpDetail));
1545 }
1546 }
1547
1548 static void
1549 htcpIncomingConnectionOpened(int)
1550 {
1551 if (!Comm::IsConnOpen(htcpIncomingConn))
1552 fatal("Cannot open HTCP Socket");
1553
1554 Comm::SetSelect(htcpIncomingConn->fd, COMM_SELECT_READ, htcpRecv, NULL, 0);
1555
1556 debugs(31, DBG_CRITICAL, "Accepting HTCP messages on " << htcpIncomingConn->local);
1557
1558 if (Config.Addrs.udp_outgoing.IsNoAddr()) {
1559 htcpOutgoingConn = htcpIncomingConn;
1560 debugs(31, DBG_IMPORTANT, "Sending HTCP messages from " << htcpOutgoingConn->local);
1561 }
1562 }
1563
1564 int
1565 htcpQuery(StoreEntry * e, HttpRequest * req, peer * p)
1566 {
1567 cache_key *save_key;
1568 static char pkt[8192];
1569 ssize_t pktlen;
1570 char vbuf[32];
1571 htcpStuff stuff;
1572 HttpHeader hdr(hoRequest);
1573 Packer pa;
1574 MemBuf mb;
1575 http_state_flags flags;
1576
1577 if (!Comm::IsConnOpen(htcpIncomingConn))
1578 return 0;
1579
1580 old_squid_format = p->options.htcp_oldsquid;
1581 memset(&flags, '\0', sizeof(flags));
1582 snprintf(vbuf, sizeof(vbuf), "%d/%d",
1583 req->http_ver.major, req->http_ver.minor);
1584 stuff.op = HTCP_TST;
1585 stuff.rr = RR_REQUEST;
1586 stuff.f1 = 1;
1587 stuff.response = 0;
1588 stuff.msg_id = ++msg_id_counter;
1589 stuff.S.method = (char *) RequestMethodStr(req->method);
1590 stuff.S.uri = (char *) e->url();
1591 stuff.S.version = vbuf;
1592 HttpStateData::httpBuildRequestHeader(req, e, &hdr, flags);
1593 mb.init();
1594 packerToMemInit(&pa, &mb);
1595 hdr.packInto(&pa);
1596 hdr.clean();
1597 packerClean(&pa);
1598 stuff.S.req_hdrs = mb.buf;
1599 pktlen = htcpBuildPacket(pkt, sizeof(pkt), &stuff);
1600 mb.clean();
1601 if (!pktlen) {
1602 debugs(31, 3, "htcpQuery: htcpBuildPacket() failed");
1603 return -1;
1604 }
1605
1606 htcpSend(pkt, (int) pktlen, p->in_addr);
1607
1608 queried_id[stuff.msg_id % N_QUERIED_KEYS] = stuff.msg_id;
1609 save_key = queried_keys[stuff.msg_id % N_QUERIED_KEYS];
1610 storeKeyCopy(save_key, (const cache_key *)e->key);
1611 queried_addr[stuff.msg_id % N_QUERIED_KEYS] = p->in_addr;
1612 debugs(31, 3, "htcpQuery: key (" << save_key << ") " << storeKeyText(save_key));
1613
1614 return 1;
1615 }
1616
1617 /*
1618 * Send an HTCP CLR message for a specified item to a given peer.
1619 */
1620 void
1621 htcpClear(StoreEntry * e, const char *uri, HttpRequest * req, const HttpRequestMethod &method, peer * p, htcp_clr_reason reason)
1622 {
1623 static char pkt[8192];
1624 ssize_t pktlen;
1625 char vbuf[32];
1626 htcpStuff stuff;
1627 HttpHeader hdr(hoRequest);
1628 Packer pa;
1629 MemBuf mb;
1630 http_state_flags flags;
1631
1632 if (!Comm::IsConnOpen(htcpIncomingConn))
1633 return;
1634
1635 old_squid_format = p->options.htcp_oldsquid;
1636 memset(&flags, '\0', sizeof(flags));
1637 snprintf(vbuf, sizeof(vbuf), "%d/%d",
1638 req->http_ver.major, req->http_ver.minor);
1639 stuff.op = HTCP_CLR;
1640 stuff.rr = RR_REQUEST;
1641 stuff.f1 = 0;
1642 stuff.response = 0;
1643 stuff.msg_id = ++msg_id_counter;
1644 switch (reason) {
1645 case HTCP_CLR_INVALIDATION:
1646 stuff.reason = 1;
1647 break;
1648 default:
1649 stuff.reason = 0;
1650 break;
1651 }
1652 stuff.S.method = (char *) RequestMethodStr(req->method);
1653 if (e == NULL || e->mem_obj == NULL) {
1654 if (uri == NULL) {
1655 return;
1656 }
1657 stuff.S.uri = xstrdup(uri);
1658 } else {
1659 stuff.S.uri = (char *) e->url();
1660 }
1661 stuff.S.version = vbuf;
1662 if (reason != HTCP_CLR_INVALIDATION) {
1663 HttpStateData::httpBuildRequestHeader(req, e, &hdr, flags);
1664 mb.init();
1665 packerToMemInit(&pa, &mb);
1666 hdr.packInto(&pa);
1667 hdr.clean();
1668 packerClean(&pa);
1669 stuff.S.req_hdrs = mb.buf;
1670 } else {
1671 stuff.S.req_hdrs = NULL;
1672 }
1673 pktlen = htcpBuildPacket(pkt, sizeof(pkt), &stuff);
1674 if (reason != HTCP_CLR_INVALIDATION) {
1675 mb.clean();
1676 }
1677 if (e == NULL) {
1678 xfree(stuff.S.uri);
1679 }
1680 if (!pktlen) {
1681 debugs(31, 3, "htcpClear: htcpBuildPacket() failed");
1682 return;
1683 }
1684
1685 htcpSend(pkt, (int) pktlen, p->in_addr);
1686 }
1687
1688 /*
1689 * htcpSocketShutdown only closes the 'in' socket if it is
1690 * different than the 'out' socket.
1691 */
1692 void
1693 htcpSocketShutdown(void)
1694 {
1695 if (!Comm::IsConnOpen(htcpIncomingConn))
1696 return;
1697
1698 debugs(12, DBG_IMPORTANT, "Stop accepting HTCP on " << htcpIncomingConn->local);
1699 /*
1700 * Here we just unlink htcpIncomingConn because the HTCP 'in'
1701 * and 'out' sockets might be just one FD. This prevents this
1702 * function from executing repeatedly. When we are really ready to
1703 * exit or restart, main will comm_close the 'out' descriptor.
1704 */
1705 htcpIncomingConn = NULL;
1706
1707 /*
1708 * Normally we only write to the outgoing HTCP socket, but
1709 * we also have a read handler there to catch messages sent
1710 * to that specific interface. During shutdown, we must
1711 * disable reading on the outgoing socket.
1712 */
1713 /* XXX Don't we need this handler to read replies while shutting down?
1714 * I think there should be a separate hander for reading replies..
1715 */
1716 assert(Comm::IsConnOpen(htcpOutgoingConn));
1717
1718 Comm::SetSelect(htcpOutgoingConn->fd, COMM_SELECT_READ, NULL, NULL, 0);
1719 }
1720
1721 void
1722 htcpSocketClose(void)
1723 {
1724 htcpSocketShutdown();
1725
1726 if (htcpOutgoingConn != NULL) {
1727 debugs(12, DBG_IMPORTANT, "Stop sending HTCP from " << htcpOutgoingConn->local);
1728 htcpOutgoingConn = NULL;
1729 }
1730 }
1731
1732 static void
1733 htcpLogHtcp(Ip::Address &caddr, int opcode, log_type logcode, const char *url)
1734 {
1735 AccessLogEntry al;
1736 if (LOG_TAG_NONE == logcode)
1737 return;
1738 if (!Config.onoff.log_udp)
1739 return;
1740 al.htcp.opcode = htcpOpcodeStr[opcode];
1741 al.url = url;
1742 al.cache.caddr = caddr;
1743 al.cache.code = logcode;
1744 al.cache.msec = 0;
1745 accessLogLog(&al, NULL);
1746 }