2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 31 Hypertext Caching Protocol */
12 #include "AccessLogEntry.h"
14 #include "acl/FilledChecklist.h"
15 #include "base/AsyncCallbacks.h"
16 #include "CachePeer.h"
17 #include "CachePeers.h"
19 #include "comm/Connection.h"
20 #include "comm/Loops.h"
21 #include "compat/xalloc.h"
22 #include "debug/Messages.h"
26 #include "http/ContentLengthInterpreter.h"
27 #include "HttpRequest.h"
28 #include "icmp/net_db.h"
30 #include "ipc/StartListening.h"
32 #include "mem/forward.h"
35 #include "SquidConfig.h"
36 #include "StatCounters.h"
38 #include "store_key_md5.h"
39 #include "StoreClient.h"
42 typedef struct _Countstr Countstr
;
44 typedef struct _htcpHeader htcpHeader
;
46 typedef struct _htcpDataHeader htcpDataHeader
;
48 typedef struct _htcpDataHeaderSquid htcpDataHeaderSquid
;
50 typedef struct _htcpAuthHeader htcpAuthHeader
;
63 struct _htcpDataHeaderSquid
{
67 unsigned int opcode
:4;
68 unsigned int response
:4;
70 unsigned int response
:4;
71 unsigned int opcode
:4;
75 unsigned int reserved
:6;
81 unsigned int reserved
:6;
87 struct _htcpDataHeader
{
111 /* RR == 0 --> F1 = RESPONSE DESIRED FLAG */
112 /* RR == 1 --> F1 = MESSAGE OVERALL FLAG */
113 /* RR == 0 --> REQUEST */
114 /* RR == 1 --> RESPONSE */
116 struct _htcpAuthHeader
{
124 class htcpSpecifier
: public CodeContext
, public StoreClient
126 MEMPROXY_CLASS(htcpSpecifier
);
129 typedef RefCount
<htcpSpecifier
> Pointer
;
132 void checkedHit(StoreEntry
*);
134 void setFrom(Ip::Address
&anIp
) { from
= anIp
; }
135 void setDataHeader(htcpDataHeader
*aDataHeader
) {
139 /* CodeContext API */
140 ScopedId
codeContextGist() const override
; // override
141 std::ostream
&detailCodeContext(std::ostream
&os
) const override
; // override
143 /* StoreClient API */
144 LogTags
*loggingTags() const override
;
145 void fillChecklist(ACLFilledChecklist
&) const override
;
148 const char *method
= nullptr;
149 const char *uri
= nullptr;
150 char *version
= nullptr;
151 char *req_hdrs
= nullptr;
152 size_t reqHdrsSz
= 0; ///< size of the req_hdrs content
153 HttpRequest::Pointer request
;
155 /// optimization: nil until needed
156 mutable AccessLogEntryPointer al
;
159 HttpRequest::Pointer checkHitRequest
;
162 htcpDataHeader
*dhdr
= nullptr;
167 MEMPROXY_CLASS(htcpDetail
);
169 char *resp_hdrs
= nullptr;
170 size_t respHdrsSz
= 0;
172 char *entity_hdrs
= nullptr;
173 size_t entityHdrsSz
= 0;
175 char *cache_hdrs
= nullptr;
176 size_t cacheHdrsSz
= 0;
182 htcpStuff(uint32_t id
, int o
, int r
, int f
) :
208 static const char *const htcpOpcodeStr
[] = {
218 * values for htcpDataHeader->response
223 OPCODE_UNIMPLEMENTED
,
224 MAJOR_VERSION_UNSUPPORTED
,
225 MINOR_VERSION_UNSUPPORTED
,
230 * values for htcpDataHeader->RR
237 static void htcpIncomingConnectionOpened(Ipc::StartListeningAnswer
&);
238 static uint32_t msg_id_counter
= 0;
240 static Comm::ConnectionPointer htcpOutgoingConn
= nullptr;
241 static Comm::ConnectionPointer htcpIncomingConn
= nullptr;
242 #define N_QUERIED_KEYS 8192
243 static uint32_t queried_id
[N_QUERIED_KEYS
];
244 static cache_key queried_keys
[N_QUERIED_KEYS
][SQUID_MD5_DIGEST_LENGTH
];
246 static Ip::Address queried_addr
[N_QUERIED_KEYS
];
248 static int old_squid_format
= 0;
250 static ssize_t
htcpBuildPacket(char *buf
, size_t buflen
, htcpStuff
* stuff
);
251 static htcpDetail
*htcpUnpackDetail(char *buf
, int sz
);
252 static ssize_t
htcpBuildAuth(char *buf
, size_t buflen
);
253 static ssize_t
htcpBuildCountstr(char *buf
, size_t buflen
, const char *s
, size_t len
);
254 static ssize_t
htcpBuildData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
255 static ssize_t
htcpBuildDetail(char *buf
, size_t buflen
, htcpStuff
* stuff
);
256 static ssize_t
htcpBuildOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
257 static ssize_t
htcpBuildSpecifier(char *buf
, size_t buflen
, htcpStuff
* stuff
);
258 static ssize_t
htcpBuildTstOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
260 static void htcpHandleMsg(char *buf
, int sz
, Ip::Address
&from
);
262 static void htcpLogHtcp(Ip::Address
&, const int, const LogTags_ot
, const char *, AccessLogEntryPointer
);
263 static void htcpHandleTst(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
265 static void htcpRecv(int fd
, void *data
);
267 static void htcpSend(const char *buf
, int len
, Ip::Address
&to
);
269 static void htcpTstReply(htcpDataHeader
*, StoreEntry
*, htcpSpecifier
*, Ip::Address
&);
271 static void htcpHandleTstRequest(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
273 static void htcpHandleTstResponse(htcpDataHeader
*, char *, int, Ip::Address
&);
276 htcpSyncAle(AccessLogEntryPointer
&al
, const Ip::Address
&caddr
, const int opcode
, const char *url
)
279 al
= new AccessLogEntry();
280 al
->cache
.caddr
= caddr
;
281 al
->htcp
.opcode
= htcpOpcodeStr
[opcode
];
283 al
->setVirginUrlForMissingRequest(al
->url
);
284 // HTCP transactions do not wait
285 al
->cache
.start_time
= current_time
;
286 al
->cache
.trTime
.tv_sec
= 0;
287 al
->cache
.trTime
.tv_usec
= 0;
291 htcpHexdump(const char *tag
, const char *s
, int sz
)
295 debugs(31, 3, "htcpHexdump " << tag
);
296 memset(hex
, '\0', sizeof(hex
));
298 for (int i
= 0; i
< sz
; ++i
) {
300 snprintf(&hex
[k
* 3], 4, " %02x", (int) *(s
+ i
));
302 if (k
< 15 && i
< (sz
- 1))
305 debugs(31, 3, "\t" << hex
);
307 memset(hex
, '\0', sizeof(hex
));
317 * STUFF FOR SENDING HTCP MESSAGES
321 htcpBuildAuth(char *buf
, size_t buflen
)
325 assert(2 == sizeof(uint16_t));
326 auth
.length
= htons(2);
328 if (buflen
< copy_sz
)
330 memcpy(buf
, &auth
, copy_sz
);
335 htcpBuildCountstr(char *buf
, size_t buflen
, const char *s
, size_t len
)
339 if (buflen
- off
< 2)
342 debugs(31, 3, "htcpBuildCountstr: LENGTH = " << len
);
344 debugs(31, 3, "htcpBuildCountstr: TEXT = {" << (s
? s
: "<NULL>") << "}");
346 uint16_t length
= htons((uint16_t) len
);
348 memcpy(buf
+ off
, &length
, 2);
352 if (buflen
- off
< len
)
356 memcpy(buf
+ off
, s
, len
);
364 htcpBuildSpecifier(char *buf
, size_t buflen
, htcpStuff
* stuff
)
368 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.method
, (stuff
->S
.method
?strlen(stuff
->S
.method
):0));
375 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.uri
, (stuff
->S
.uri
?strlen(stuff
->S
.uri
):0));
382 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.version
, (stuff
->S
.version
?strlen(stuff
->S
.version
):0));
389 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.req_hdrs
, stuff
->S
.reqHdrsSz
);
396 debugs(31, 3, "htcpBuildSpecifier: size " << off
);
402 htcpBuildDetail(char *buf
, size_t buflen
, htcpStuff
* stuff
)
406 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.resp_hdrs
, stuff
->D
.respHdrsSz
);
413 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.entity_hdrs
, stuff
->D
.entityHdrsSz
);
420 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.cache_hdrs
, stuff
->D
.cacheHdrsSz
);
431 htcpBuildTstOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
436 debugs(31, 3, "htcpBuildTstOpData: RR_REQUEST");
437 return htcpBuildSpecifier(buf
, buflen
, stuff
);
440 debugs(31, 3, "htcpBuildTstOpData: RR_RESPONSE");
441 debugs(31, 3, "htcpBuildTstOpData: F1 = " << stuff
->f1
);
443 if (stuff
->f1
) /* cache miss */
446 return htcpBuildDetail(buf
, buflen
, stuff
);
449 fatal_dump("htcpBuildTstOpData: bad RR value");
456 htcpBuildClrOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
458 unsigned short reason
;
462 debugs(31, 3, "htcpBuildClrOpData: RR_REQUEST");
463 reason
= htons((unsigned short)stuff
->reason
);
464 memcpy(buf
, &reason
, 2);
465 return htcpBuildSpecifier(buf
+ 2, buflen
- 2, stuff
) + 2;
469 fatal_dump("htcpBuildClrOpData: bad RR value");
476 htcpBuildOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
479 debugs(31, 3, "htcpBuildOpData: opcode " << htcpOpcodeStr
[stuff
->op
]);
484 off
= htcpBuildTstOpData(buf
+ off
, buflen
, stuff
);
488 off
= htcpBuildClrOpData(buf
+ off
, buflen
, stuff
);
500 htcpBuildData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
504 size_t hdr_sz
= sizeof(htcpDataHeader
);
509 off
+= hdr_sz
; /* skip! */
511 op_data_sz
= htcpBuildOpData(buf
+ off
, buflen
- off
, stuff
);
518 debugs(31, 3, "htcpBuildData: hdr.length = " << off
);
520 if (!old_squid_format
) {
522 memset(&hdr
, 0, sizeof(hdr
));
523 /* convert multi-byte fields */
524 hdr
.msg_id
= htonl(stuff
->msg_id
);
525 hdr
.length
= htons(static_cast<uint16_t>(off
));
526 hdr
.opcode
= stuff
->op
;
527 hdr
.response
= stuff
->response
;
530 memcpy(buf
, &hdr
, hdr_sz
);
532 htcpDataHeaderSquid hdrSquid
;
533 memset(&hdrSquid
, 0, sizeof(hdrSquid
));
534 hdrSquid
.length
= htons(static_cast<uint16_t>(off
));
535 hdrSquid
.opcode
= stuff
->op
;
536 hdrSquid
.response
= stuff
->response
;
537 hdrSquid
.F1
= stuff
->f1
;
538 hdrSquid
.RR
= stuff
->rr
;
539 memcpy(buf
, &hdrSquid
, hdr_sz
);
542 debugs(31, 3, "htcpBuildData: size " << off
);
548 * Build an HTCP packet into buf, maximum length buflen.
549 * Returns the packet length, or zero on failure.
552 htcpBuildPacket(char *buf
, size_t buflen
, htcpStuff
* stuff
)
556 size_t hdr_sz
= sizeof(htcpHeader
);
558 /* skip the header -- we don't know the overall length */
560 if (buflen
< hdr_sz
) {
565 s
= htcpBuildData(buf
+ off
, buflen
- off
, stuff
);
572 s
= htcpBuildAuth(buf
+ off
, buflen
- off
);
579 hdr
.length
= htons((uint16_t) off
);
582 if (old_squid_format
)
587 memcpy(buf
, &hdr
, hdr_sz
);
589 debugs(31, 3, "htcpBuildPacket: size " << off
);
595 htcpSend(const char *buf
, int len
, Ip::Address
&to
)
598 htcpHexdump("htcpSend", buf
, len
);
600 if (comm_udp_sendto(htcpOutgoingConn
->fd
, to
, buf
, len
) < 0) {
602 debugs(31, 3, htcpOutgoingConn
<< " sendto: " << xstrerr(xerrno
));
604 ++statCounter
.htcp
.pkts_sent
;
608 * Unpack an HTCP SPECIFIER in place
609 * This will overwrite any following AUTH block
611 // XXX: this needs to be turned into an Htcp1::Parser inheriting from Http1::RequestParser
612 // but with different first-line and block unpacking logic.
613 static htcpSpecifier::Pointer
614 htcpUnpackSpecifier(char *buf
, int sz
)
616 static const htcpSpecifier::Pointer nil
;
617 htcpSpecifier::Pointer
s(new htcpSpecifier
);
618 HttpRequestMethod method
;
620 /* Find length of METHOD */
621 uint16_t l
= ntohs(*(uint16_t *) buf
);
626 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack METHOD");
634 debugs(31, 6, "htcpUnpackSpecifier: METHOD (" << l
<< "/" << sz
<< ") '" << s
->method
<< "'");
636 /* Find length of URI */
637 l
= ntohs(*(uint16_t *) buf
);
641 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack URI");
645 /* Add terminating null to METHOD */
653 debugs(31, 6, "htcpUnpackSpecifier: URI (" << l
<< "/" << sz
<< ") '" << s
->uri
<< "'");
655 /* Find length of VERSION */
656 l
= ntohs(*(uint16_t *) buf
);
660 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack VERSION");
664 /* Add terminating null to URI */
672 debugs(31, 6, "htcpUnpackSpecifier: VERSION (" << l
<< "/" << sz
<< ") '" << s
->version
<< "'");
674 /* Find length of REQ-HDRS */
675 l
= ntohs(*(uint16_t *) buf
);
679 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack REQ-HDRS");
683 /* Add terminating null to URI */
692 debugs(31, 6, "htcpUnpackSpecifier: REQ-HDRS (" << l
<< "/" << sz
<< ") '" << s
->req_hdrs
<< "'");
694 debugs(31, 3, "htcpUnpackSpecifier: " << sz
<< " bytes left");
697 * Add terminating null to REQ-HDRS. This is possible because we allocated
698 * an extra byte when we received the packet. This will overwrite any following
704 method
.HttpRequestMethodXXX(s
->method
);
706 const auto mx
= MasterXaction::MakePortless
<XactionInitiator::initHtcp
>();
707 s
->request
= HttpRequest::FromUrlXXX(s
->uri
, mx
, method
== Http::METHOD_NONE
? HttpRequestMethod(Http::METHOD_GET
) : method
);
709 debugs(31, 3, "failed to create request. Invalid URI?");
717 * Unpack an HTCP DETAIL in place
718 * This will overwrite any following AUTH block
721 htcpUnpackDetail(char *buf
, int sz
)
723 htcpDetail
*d
= new htcpDetail
;
725 /* Find length of RESP-HDRS */
726 uint16_t l
= ntohs(*(uint16_t *) buf
);
731 debugs(31, 3, "htcpUnpackDetail: failed to unpack RESP_HDRS");
742 /* Find length of ENTITY-HDRS */
743 l
= ntohs(*(uint16_t *) buf
);
748 debugs(31, 3, "htcpUnpackDetail: failed to unpack ENTITY_HDRS");
753 /* Add terminating null to RESP-HDRS */
756 /* Set ENTITY-HDRS */
759 d
->entity_hdrs
= buf
;
764 /* Find length of CACHE-HDRS */
765 l
= ntohs(*(uint16_t *) buf
);
770 debugs(31, 3, "htcpUnpackDetail: failed to unpack CACHE_HDRS");
775 /* Add terminating null to ENTITY-HDRS */
786 debugs(31, 3, "htcpUnpackDetail: " << sz
<< " bytes left");
789 * Add terminating null to CACHE-HDRS. This is possible because we allocated
790 * an extra byte when we received the packet. This will overwrite any following
799 htcpAccessAllowed(acl_access
* acl
, const htcpSpecifier::Pointer
&s
, Ip::Address
&from
)
801 /* default deny if no access list present */
805 ACLFilledChecklist
checklist(acl
, s
->request
.getRaw());
806 checklist
.src_addr
= from
;
807 checklist
.my_addr
.setNoAddr();
808 return checklist
.fastCheck().allowed();
812 htcpTstReply(htcpDataHeader
* dhdr
, StoreEntry
* e
, htcpSpecifier
* spec
, Ip::Address
&from
)
814 static char pkt
[8192];
815 HttpHeader
hdr(hoHtcpReply
);
818 htcpStuff
stuff(dhdr
->msg_id
, HTCP_TST
, RR_RESPONSE
, 0);
819 stuff
.response
= e
? 0 : 1;
820 debugs(31, 3, "htcpTstReply: response = " << stuff
.response
);
823 stuff
.S
.method
= spec
->method
;
824 stuff
.S
.request
= spec
->request
;
825 stuff
.S
.uri
= spec
->uri
;
826 stuff
.S
.version
= spec
->version
;
827 stuff
.S
.req_hdrs
= spec
->req_hdrs
;
828 stuff
.S
.reqHdrsSz
= spec
->reqHdrsSz
;
830 hdr
.putInt(Http::HdrType::AGE
, (e
->timestamp
<= squid_curtime
? (squid_curtime
- e
->timestamp
) : 0) );
832 hdr
.putInt(Http::HdrType::AGE
, 0);
836 stuff
.D
.resp_hdrs
= xstrdup(mb
.buf
);
837 stuff
.D
.respHdrsSz
= mb
.contentSize();
838 debugs(31, 3, "htcpTstReply: resp_hdrs = {" << stuff
.D
.resp_hdrs
<< "}");
842 if (e
&& e
->expires
> -1)
843 hdr
.putTime(Http::HdrType::EXPIRES
, e
->expires
);
845 if (e
&& e
->lastModified() > -1)
846 hdr
.putTime(Http::HdrType::LAST_MODIFIED
, e
->lastModified());
850 stuff
.D
.entity_hdrs
= xstrdup(mb
.buf
);
851 stuff
.D
.entityHdrsSz
= mb
.contentSize();
853 debugs(31, 3, "htcpTstReply: entity_hdrs = {" << stuff
.D
.entity_hdrs
<< "}");
859 if (const char *host
= spec
->request
->url
.host()) {
863 netdbHostData(host
, &samp
, &rtt
, &hops
);
866 char cto_buf
[SQUIDHOSTNAMELEN
+128];
867 snprintf(cto_buf
, sizeof(cto_buf
), "%s %d %f %d",
868 host
, samp
, 0.001 * rtt
, hops
);
869 hdr
.putExt("Cache-to-Origin", cto_buf
);
872 #endif /* USE_ICMP */
875 stuff
.D
.cache_hdrs
= xstrdup(mb
.buf
);
876 stuff
.D
.cacheHdrsSz
= mb
.contentSize();
877 debugs(31, 3, "htcpTstReply: cache_hdrs = {" << stuff
.D
.cache_hdrs
<< "}");
882 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
884 safe_free(stuff
.D
.resp_hdrs
);
885 stuff
.D
.respHdrsSz
= 0;
886 safe_free(stuff
.D
.entity_hdrs
);
887 stuff
.D
.entityHdrsSz
= 0;
888 safe_free(stuff
.D
.cache_hdrs
);
889 stuff
.D
.cacheHdrsSz
= 0;
892 debugs(31, 3, "htcpTstReply: htcpBuildPacket() failed");
896 htcpSend(pkt
, (int) pktlen
, from
);
901 htcpClrReply(htcpDataHeader
* dhdr
, int purgeSucceeded
, Ip::Address
&from
)
903 static char pkt
[8192];
906 /* If dhdr->F1 == 0, no response desired */
911 htcpStuff
stuff(dhdr
->msg_id
, HTCP_CLR
, RR_RESPONSE
, 0);
913 stuff
.response
= purgeSucceeded
? 0 : 2;
915 debugs(31, 3, "htcpClrReply: response = " << stuff
.response
);
917 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
920 debugs(31, 3, "htcpClrReply: htcpBuildPacket() failed");
924 htcpSend(pkt
, (int) pktlen
, from
);
928 htcpSpecifier::codeContextGist() const
931 const auto gist
= al
->codeContextGist();
937 if (const auto &mx
= request
->masterXaction
)
938 return mx
->id
.detach();
941 return ScopedId("HTCP w/o master");
945 htcpSpecifier::detailCodeContext(std::ostream
&os
) const
948 return al
->detailCodeContext(os
);
951 if (const auto &mx
= request
->masterXaction
)
952 return os
<< Debug::Extra
<< "current master transaction: " << mx
->id
;
955 // TODO: Report method, uri, and version if they have been set
960 htcpSpecifier::checkHit()
962 checkHitRequest
= request
;
964 if (!checkHitRequest
) {
965 debugs(31, 3, "htcpCheckHit: NO; failed to parse URL");
970 if (!checkHitRequest
->parseHeader(req_hdrs
, reqHdrsSz
)) {
971 debugs(31, 3, "htcpCheckHit: NO; failed to parse request headers");
972 checkHitRequest
= nullptr;
977 const auto e
= storeGetPublicByRequest(checkHitRequest
.getRaw());
978 StoreEntry
*hit
= nullptr;
981 debugs(31, 3, "NO; public object not found");
982 } else if (!e
->validToSend()) {
983 debugs(31, 3, "NO; entry not valid to send" );
984 } else if (e
->hittingRequiresCollapsing() && !startCollapsingOn(*e
, false)) {
985 debugs(31, 3, "NO; prohibited CF hit: " << *e
);
986 } else if (!didCollapse
&& refreshCheckHTCP(e
, checkHitRequest
.getRaw())) {
987 debugs(31, 3, "NO; cached response is stale");
989 debugs(31, 3, "YES!?");
995 // TODO: StoreClients must either store/lock or abandon found entries.
1001 htcpSpecifier::loggingTags() const
1003 // calling htcpSyncAle() here would not change cache.code
1005 al
= new AccessLogEntry();
1006 return &al
->cache
.code
;
1010 htcpSpecifier::fillChecklist(ACLFilledChecklist
&checklist
) const
1012 checklist
.setRequest(request
.getRaw());
1013 htcpSyncAle(al
, from
, dhdr
->opcode
, uri
);
1018 htcpClrStoreEntry(StoreEntry
* e
)
1020 debugs(31, 4, "htcpClrStoreEntry: Clearing store for entry: " << e
->url() );
1021 e
->releaseRequest();
1025 htcpClrStore(const htcpSpecifier::Pointer
&s
)
1027 HttpRequestPointer
request(s
->request
);
1029 debugs(31, 3, "htcpClrStore: failed to parse URL");
1033 /* Parse request headers */
1034 if (!request
->parseHeader(s
->req_hdrs
, s
->reqHdrsSz
)) {
1035 debugs(31, 2, "htcpClrStore: failed to parse request headers");
1039 StoreEntry
*e
= nullptr;
1041 /* Lookup matching entries. This matches both GET and HEAD */
1042 while ((e
= storeGetPublicByRequest(request
.getRaw()))) {
1043 htcpClrStoreEntry(e
);
1048 debugs(31, 4, "htcpClrStore: Cleared " << released
<< " matching entries");
1051 debugs(31, 4, "htcpClrStore: No matching entry found");
1058 htcpHandleTst(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1060 debugs(31, 3, "htcpHandleTst: sz = " << sz
);
1062 if (hdr
->RR
== RR_REQUEST
)
1063 htcpHandleTstRequest(hdr
, buf
, sz
, from
);
1065 htcpHandleTstResponse(hdr
, buf
, sz
, from
);
1068 HtcpReplyData::HtcpReplyData() :
1069 hit(0), hdr(hoHtcpReply
), msg_id(0), version(0.0)
1071 memset(&cto
, 0, sizeof(cto
));
1075 HtcpReplyData::parseHeader(const char *buffer
, const size_t size
)
1077 Http::ContentLengthInterpreter interpreter
;
1078 // no applyStatusCodeRules() -- HTCP replies lack cached HTTP status code
1079 return hdr
.parse(buffer
, size
, interpreter
);
1084 htcpHandleTstResponse(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1086 HtcpReplyData htcpReply
;
1087 cache_key
*key
= nullptr;
1090 htcpDetail
*d
= nullptr;
1093 if (queried_id
[hdr
->msg_id
% N_QUERIED_KEYS
] != hdr
->msg_id
) {
1094 debugs(31, 2, "htcpHandleTstResponse: No matching query id '" <<
1095 hdr
->msg_id
<< "' (expected " <<
1096 queried_id
[hdr
->msg_id
% N_QUERIED_KEYS
] << ") from '" <<
1102 key
= queried_keys
[hdr
->msg_id
% N_QUERIED_KEYS
];
1105 debugs(31, 3, "htcpHandleTstResponse: No query key for response id '" << hdr
->msg_id
<< "' from '" << from
<< "'");
1109 peer
= &queried_addr
[hdr
->msg_id
% N_QUERIED_KEYS
];
1111 if ( *peer
!= from
|| peer
->port() != from
.port() ) {
1112 debugs(31, 3, "htcpHandleTstResponse: Unexpected response source " << from
);
1117 debugs(31, 2, "htcpHandleTstResponse: error condition, F1/MO == 1");
1121 htcpReply
.msg_id
= hdr
->msg_id
;
1122 debugs(31, 3, "htcpHandleTstResponse: msg_id = " << htcpReply
.msg_id
);
1123 htcpReply
.hit
= hdr
->response
? 0 : 1;
1126 debugs(31, 3, "htcpHandleTstResponse: MISS");
1128 debugs(31, 3, "htcpHandleTstResponse: HIT");
1129 d
= htcpUnpackDetail(buf
, sz
);
1132 debugs(31, 3, "htcpHandleTstResponse: bad DETAIL");
1136 if ((t
= d
->resp_hdrs
))
1137 htcpReply
.parseHeader(t
, d
->respHdrsSz
);
1139 if ((t
= d
->entity_hdrs
))
1140 htcpReply
.parseHeader(t
, d
->entityHdrsSz
);
1142 if ((t
= d
->cache_hdrs
))
1143 htcpReply
.parseHeader(t
, d
->cacheHdrsSz
);
1146 debugs(31, 3, "htcpHandleTstResponse: key (" << key
<< ") " << storeKeyText(key
));
1147 neighborsHtcpReply(key
, &htcpReply
, from
);
1148 htcpReply
.hdr
.clean();
1154 htcpHandleTstRequest(htcpDataHeader
* dhdr
, char *buf
, int sz
, Ip::Address
&from
)
1157 debugs(31, 3, "htcpHandleTst: nothing to do");
1164 /* buf should be a SPECIFIER */
1165 htcpSpecifier::Pointer
s(htcpUnpackSpecifier(buf
, sz
));
1168 debugs(31, 3, "htcpHandleTstRequest: htcpUnpackSpecifier failed");
1169 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_INVALID
, dash_str
, nullptr);
1173 s
->setDataHeader(dhdr
);
1177 debugs(31, 3, "htcpHandleTstRequest: failed to parse request");
1178 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_INVALID
, dash_str
, s
->al
);
1182 if (!htcpAccessAllowed(Config
.accessList
.htcp
, s
, from
)) {
1183 debugs(31, 3, "htcpHandleTstRequest: Access denied");
1184 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_DENIED
, s
->uri
, s
->al
);
1188 debugs(31, 2, "HTCP TST request: " << s
->method
<< " " << s
->uri
<< " " << s
->version
);
1189 debugs(31, 2, "HTCP TST headers: " << s
->req_hdrs
);
1194 htcpSpecifier::checkedHit(StoreEntry
*e
)
1197 htcpTstReply(dhdr
, e
, this, from
); /* hit */
1198 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_HIT
, uri
, al
);
1200 htcpTstReply(dhdr
, nullptr, nullptr, from
); /* cache miss */
1201 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_MISS
, uri
, al
);
1206 htcpHandleClr(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1208 /* buf[0/1] is reserved and reason */
1209 int reason
= buf
[1] << 4;
1210 debugs(31, 2, "HTCP CLR reason: " << reason
);
1214 /* buf should be a SPECIFIER */
1217 debugs(31, 4, "htcpHandleClr: nothing to do");
1218 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
, nullptr);
1222 htcpSpecifier::Pointer
s(htcpUnpackSpecifier(buf
, sz
));
1225 debugs(31, 3, "htcpHandleClr: htcpUnpackSpecifier failed");
1226 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
, nullptr);
1230 s
->setDataHeader(hdr
);
1234 debugs(31, 3, "htcpHandleTstRequest: failed to parse request");
1235 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
, s
->al
);
1239 if (!htcpAccessAllowed(Config
.accessList
.htcp_clr
, s
, from
)) {
1240 debugs(31, 3, "htcpHandleClr: Access denied");
1241 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_DENIED
, s
->uri
, s
->al
);
1245 debugs(31, 2, "HTCP CLR request: " << s
->method
<< " " << s
->uri
<< " " << s
->version
);
1246 debugs(31, 2, "HTCP CLR headers: " << s
->req_hdrs
);
1248 /* Release objects from cache
1249 * analog to clientPurgeRequest in client_side.c
1252 switch (htcpClrStore(s
)) {
1255 htcpClrReply(hdr
, 1, from
); /* hit */
1256 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_HIT
, s
->uri
, s
->al
);
1260 htcpClrReply(hdr
, 0, from
); /* miss */
1261 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_MISS
, s
->uri
, s
->al
);
1270 * Forward a CLR request to all peers who have requested that CLRs be
1271 * forwarded to them.
1274 htcpForwardClr(char *buf
, int sz
)
1276 for (const auto &p
: CurrentCachePeers()) {
1277 if (!p
->options
.htcp
) {
1280 if (!p
->options
.htcp_forward_clr
) {
1284 htcpSend(buf
, sz
, p
->in_addr
);
1289 * Do the first pass of handling an HTCP message. This used to be two
1290 * separate functions, htcpHandle and htcpHandleData. They were merged to
1291 * allow for forwarding HTCP packets easily to other peers if desired.
1293 * This function now works out what type of message we have received and then
1294 * hands it off to other functions to break apart message-specific data.
1297 htcpHandleMsg(char *buf
, int sz
, Ip::Address
&from
)
1299 // TODO: function-scoped CodeContext::Reset(...("HTCP message from", from))
1306 if (sz
< 0 || (size_t)sz
< sizeof(htcpHeader
)) {
1307 // These are highly likely to be attack packets. Should probably get a bigger warning.
1308 debugs(31, 2, "htcpHandle: msg size less than htcpHeader size from " << from
);
1312 htcpHexdump("htcpHandle", buf
, sz
);
1313 memcpy(&htcpHdr
, buf
, sizeof(htcpHeader
));
1314 htcpHdr
.length
= ntohs(htcpHdr
.length
);
1316 if (htcpHdr
.minor
== 0)
1317 old_squid_format
= 1;
1319 old_squid_format
= 0;
1321 debugs(31, 3, "htcpHandle: htcpHdr.length = " << htcpHdr
.length
);
1322 debugs(31, 3, "htcpHandle: htcpHdr.major = " << htcpHdr
.major
);
1323 debugs(31, 3, "htcpHandle: htcpHdr.minor = " << htcpHdr
.minor
);
1325 if (sz
!= htcpHdr
.length
) {
1326 debugs(31, 3, "htcpHandle: sz/" << sz
<< " != htcpHdr.length/" <<
1327 htcpHdr
.length
<< " from " << from
);
1332 if (htcpHdr
.major
!= 0) {
1333 debugs(31, 3, "htcpHandle: Unknown major version " << htcpHdr
.major
<< " from " << from
);
1338 hbuf
= buf
+ sizeof(htcpHeader
);
1339 hsz
= sz
- sizeof(htcpHeader
);
1341 if ((size_t)hsz
< sizeof(htcpDataHeader
)) {
1342 debugs(31, 3, "htcpHandleData: msg size less than htcpDataHeader size");
1346 if (!old_squid_format
) {
1347 memcpy(&hdr
, hbuf
, sizeof(hdr
));
1349 htcpDataHeaderSquid hdrSquid
;
1350 memcpy(&hdrSquid
, hbuf
, sizeof(hdrSquid
));
1351 hdr
.length
= hdrSquid
.length
;
1352 hdr
.opcode
= hdrSquid
.opcode
;
1353 hdr
.response
= hdrSquid
.response
;
1354 hdr
.F1
= hdrSquid
.F1
;
1355 hdr
.RR
= hdrSquid
.RR
;
1357 hdr
.msg_id
= hdrSquid
.msg_id
;
1360 hdr
.length
= ntohs(hdr
.length
);
1361 hdr
.msg_id
= ntohl(hdr
.msg_id
);
1362 debugs(31, 3, "htcpHandleData: hsz = " << hsz
);
1363 debugs(31, 3, "htcpHandleData: length = " << hdr
.length
);
1365 if (hdr
.opcode
>= HTCP_END
) {
1366 debugs(31, 3, "htcpHandleData: client " << from
<< ", opcode " << hdr
.opcode
<< " out of range");
1370 debugs(31, 3, "htcpHandleData: opcode = " << hdr
.opcode
<< " " << htcpOpcodeStr
[hdr
.opcode
]);
1371 debugs(31, 3, "htcpHandleData: response = " << hdr
.response
);
1372 debugs(31, 3, "htcpHandleData: F1 = " << hdr
.F1
);
1373 debugs(31, 3, "htcpHandleData: RR = " << hdr
.RR
);
1374 debugs(31, 3, "htcpHandleData: msg_id = " << hdr
.msg_id
);
1376 if (hsz
< hdr
.length
) {
1377 debugs(31, 3, "htcpHandleData: sz < hdr.length");
1382 * set sz = hdr.length so we ignore any AUTH fields following
1385 hsz
= (int) hdr
.length
;
1386 hbuf
+= sizeof(htcpDataHeader
);
1387 hsz
-= sizeof(htcpDataHeader
);
1388 debugs(31, 3, "htcpHandleData: hsz = " << hsz
);
1390 htcpHexdump("htcpHandleData", hbuf
, hsz
);
1392 switch (hdr
.opcode
) {
1394 debugs(31, 3, "HTCP NOP not implemented");
1397 htcpHandleTst(&hdr
, hbuf
, hsz
, from
);
1400 debugs(31, 3, "HTCP MON not implemented");
1403 debugs(31, 3, "HTCP SET not implemented");
1406 htcpHandleClr(&hdr
, hbuf
, hsz
, from
);
1407 htcpForwardClr(buf
, sz
);
1415 htcpRecv(int fd
, void *)
1417 static char buf
[8192];
1419 static Ip::Address from
;
1421 /* Receive up to 8191 bytes, leaving room for a null */
1423 len
= comm_udp_recvfrom(fd
, buf
, sizeof(buf
) - 1, 0, from
);
1425 debugs(31, 3, "htcpRecv: FD " << fd
<< ", " << len
<< " bytes from " << from
);
1428 ++statCounter
.htcp
.pkts_recv
;
1430 htcpHandleMsg(buf
, len
, from
);
1432 Comm::SetSelect(fd
, COMM_SELECT_READ
, htcpRecv
, nullptr, 0);
1436 * ======================================================================
1438 * ======================================================================
1444 if (Config
.Port
.htcp
<= 0) {
1445 debugs(31, Important(21), "HTCP Disabled.");
1449 htcpIncomingConn
= new Comm::Connection
;
1450 htcpIncomingConn
->local
= Config
.Addrs
.udp_incoming
;
1451 htcpIncomingConn
->local
.port(Config
.Port
.htcp
);
1453 if (!Ip::EnableIpv6
&& !htcpIncomingConn
->local
.setIPv4()) {
1454 debugs(31, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << htcpIncomingConn
->local
<< " is not an IPv4 address.");
1455 fatal("HTCP port cannot be opened.");
1457 /* split-stack for now requires default IPv4-only HTCP */
1458 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& htcpIncomingConn
->local
.isAnyAddr()) {
1459 htcpIncomingConn
->local
.setIPv4();
1462 auto call
= asyncCallbackFun(31, 2, htcpIncomingConnectionOpened
);
1463 Ipc::StartListening(SOCK_DGRAM
,
1466 Ipc::fdnInHtcpSocket
, call
);
1468 if (!Config
.Addrs
.udp_outgoing
.isNoAddr()) {
1469 htcpOutgoingConn
= new Comm::Connection
;
1470 htcpOutgoingConn
->local
= Config
.Addrs
.udp_outgoing
;
1471 htcpOutgoingConn
->local
.port(Config
.Port
.htcp
);
1473 if (!Ip::EnableIpv6
&& !htcpOutgoingConn
->local
.setIPv4()) {
1474 debugs(31, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << htcpOutgoingConn
->local
<< " is not an IPv4 address.");
1475 fatal("HTCP port cannot be opened.");
1477 /* split-stack for now requires default IPv4-only HTCP */
1478 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& htcpOutgoingConn
->local
.isAnyAddr()) {
1479 htcpOutgoingConn
->local
.setIPv4();
1483 comm_open_listener(SOCK_DGRAM
, IPPROTO_UDP
, htcpOutgoingConn
, "Outgoing HTCP Socket");
1486 if (!Comm::IsConnOpen(htcpOutgoingConn
))
1487 fatal("Cannot open Outgoing HTCP Socket");
1489 Comm::SetSelect(htcpOutgoingConn
->fd
, COMM_SELECT_READ
, htcpRecv
, nullptr, 0);
1491 debugs(31, DBG_IMPORTANT
, "Sending HTCP messages from " << htcpOutgoingConn
->local
);
1497 htcpIncomingConnectionOpened(Ipc::StartListeningAnswer
&answer
)
1499 const auto &conn
= answer
.conn
;
1501 if (!Comm::IsConnOpen(conn
))
1502 fatal("Cannot open HTCP Socket");
1504 Comm::SetSelect(conn
->fd
, COMM_SELECT_READ
, htcpRecv
, nullptr, 0);
1506 debugs(31, DBG_CRITICAL
, "Accepting HTCP messages on " << conn
->local
);
1508 if (Config
.Addrs
.udp_outgoing
.isNoAddr()) {
1509 htcpOutgoingConn
= conn
;
1510 debugs(31, DBG_IMPORTANT
, "Sending HTCP messages from " << htcpOutgoingConn
->local
);
1515 htcpQuery(StoreEntry
* e
, HttpRequest
* req
, CachePeer
* p
)
1517 cache_key
*save_key
;
1518 static char pkt
[8192];
1521 HttpHeader
hdr(hoRequest
);
1522 Http::StateFlags flags
;
1524 if (!Comm::IsConnOpen(htcpIncomingConn
))
1527 old_squid_format
= p
->options
.htcp_oldsquid
;
1528 snprintf(vbuf
, sizeof(vbuf
), "%d/%d",
1529 req
->http_ver
.major
, req
->http_ver
.minor
);
1531 htcpStuff
stuff(++msg_id_counter
, HTCP_TST
, RR_REQUEST
, 1);
1532 SBuf sb
= req
->method
.image();
1533 stuff
.S
.method
= sb
.c_str();
1534 stuff
.S
.uri
= (char *) e
->url();
1535 stuff
.S
.version
= vbuf
;
1536 HttpStateData::httpBuildRequestHeader(req
, e
, nullptr, &hdr
, p
, flags
);
1541 stuff
.S
.req_hdrs
= mb
.buf
;
1542 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
1545 debugs(31, 3, "htcpQuery: htcpBuildPacket() failed");
1549 htcpSend(pkt
, (int) pktlen
, p
->in_addr
);
1551 queried_id
[stuff
.msg_id
% N_QUERIED_KEYS
] = stuff
.msg_id
;
1552 save_key
= queried_keys
[stuff
.msg_id
% N_QUERIED_KEYS
];
1553 storeKeyCopy(save_key
, (const cache_key
*)e
->key
);
1554 queried_addr
[stuff
.msg_id
% N_QUERIED_KEYS
] = p
->in_addr
;
1555 debugs(31, 3, "htcpQuery: key (" << save_key
<< ") " << storeKeyText(save_key
));
1561 * Send an HTCP CLR message for a specified item to a given CachePeer.
1564 htcpClear(StoreEntry
* e
, HttpRequest
* req
, const HttpRequestMethod
&, CachePeer
* p
, htcp_clr_reason reason
)
1566 static char pkt
[8192];
1569 HttpHeader
hdr(hoRequest
);
1571 Http::StateFlags flags
;
1573 if (!Comm::IsConnOpen(htcpIncomingConn
))
1576 old_squid_format
= p
->options
.htcp_oldsquid
;
1577 snprintf(vbuf
, sizeof(vbuf
), "%d/%d",
1578 req
->http_ver
.major
, req
->http_ver
.minor
);
1580 htcpStuff
stuff(++msg_id_counter
, HTCP_CLR
, RR_REQUEST
, 0);
1581 if (reason
== HTCP_CLR_INVALIDATION
)
1584 SBuf sb
= req
->method
.image();
1585 stuff
.S
.method
= sb
.c_str();
1586 stuff
.S
.request
= req
;
1587 SBuf uri
= req
->effectiveRequestUri();
1588 stuff
.S
.uri
= uri
.c_str();
1589 stuff
.S
.version
= vbuf
;
1590 if (reason
!= HTCP_CLR_INVALIDATION
) {
1591 HttpStateData::httpBuildRequestHeader(req
, e
, nullptr, &hdr
, p
, flags
);
1595 stuff
.S
.req_hdrs
= mb
.buf
;
1597 stuff
.S
.req_hdrs
= nullptr;
1599 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
1600 if (reason
!= HTCP_CLR_INVALIDATION
) {
1604 debugs(31, 3, "htcpClear: htcpBuildPacket() failed");
1608 htcpSend(pkt
, (int) pktlen
, p
->in_addr
);
1612 * htcpSocketShutdown only closes the 'in' socket if it is
1613 * different than the 'out' socket.
1616 htcpSocketShutdown(void)
1618 if (!Comm::IsConnOpen(htcpIncomingConn
))
1621 debugs(12, DBG_IMPORTANT
, "Stop accepting HTCP on " << htcpIncomingConn
->local
);
1623 * Here we just unlink htcpIncomingConn because the HTCP 'in'
1624 * and 'out' sockets might be just one FD. This prevents this
1625 * function from executing repeatedly. When we are really ready to
1626 * exit or restart, main will comm_close the 'out' descriptor.
1628 htcpIncomingConn
= nullptr;
1631 * Normally we only write to the outgoing HTCP socket, but
1632 * we also have a read handler there to catch messages sent
1633 * to that specific interface. During shutdown, we must
1634 * disable reading on the outgoing socket.
1636 /* XXX Don't we need this handler to read replies while shutting down?
1637 * I think there should be a separate handler for reading replies..
1639 assert(Comm::IsConnOpen(htcpOutgoingConn
));
1641 Comm::SetSelect(htcpOutgoingConn
->fd
, COMM_SELECT_READ
, nullptr, nullptr, 0);
1645 htcpClosePorts(void)
1647 htcpSocketShutdown();
1649 if (htcpOutgoingConn
!= nullptr) {
1650 debugs(12, DBG_IMPORTANT
, "Stop sending HTCP from " << htcpOutgoingConn
->local
);
1651 htcpOutgoingConn
= nullptr;
1656 htcpLogHtcp(Ip::Address
&caddr
, const int opcode
, const LogTags_ot logcode
, const char *url
, AccessLogEntryPointer al
)
1658 if (!Config
.onoff
.log_udp
)
1661 htcpSyncAle(al
, caddr
, opcode
, url
);
1663 assert(logcode
!= LOG_TAG_NONE
);
1664 al
->cache
.code
.update(logcode
);
1666 accessLogLog(al
, nullptr);