2 * Copyright (C) 1996-2023 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"
18 #include "comm/Connection.h"
19 #include "comm/Loops.h"
20 #include "compat/xalloc.h"
21 #include "debug/Messages.h"
25 #include "http/ContentLengthInterpreter.h"
26 #include "HttpRequest.h"
27 #include "icmp/net_db.h"
29 #include "ipc/StartListening.h"
31 #include "mem/forward.h"
34 #include "SquidConfig.h"
35 #include "StatCounters.h"
37 #include "store_key_md5.h"
38 #include "StoreClient.h"
41 typedef struct _Countstr Countstr
;
43 typedef struct _htcpHeader htcpHeader
;
45 typedef struct _htcpDataHeader htcpDataHeader
;
47 typedef struct _htcpDataHeaderSquid htcpDataHeaderSquid
;
49 typedef struct _htcpAuthHeader htcpAuthHeader
;
62 struct _htcpDataHeaderSquid
{
66 unsigned int opcode
:4;
67 unsigned int response
:4;
69 unsigned int response
:4;
70 unsigned int opcode
:4;
74 unsigned int reserved
:6;
80 unsigned int reserved
:6;
86 struct _htcpDataHeader
{
110 /* RR == 0 --> F1 = RESPONSE DESIRED FLAG */
111 /* RR == 1 --> F1 = MESSAGE OVERALL FLAG */
112 /* RR == 0 --> REQUEST */
113 /* RR == 1 --> RESPONSE */
115 struct _htcpAuthHeader
{
123 class htcpSpecifier
: public CodeContext
, public StoreClient
125 MEMPROXY_CLASS(htcpSpecifier
);
128 typedef RefCount
<htcpSpecifier
> Pointer
;
131 void checkedHit(StoreEntry
*);
133 void setFrom(Ip::Address
&anIp
) { from
= anIp
; }
134 void setDataHeader(htcpDataHeader
*aDataHeader
) {
138 /* CodeContext API */
139 ScopedId
codeContextGist() const override
; // override
140 std::ostream
&detailCodeContext(std::ostream
&os
) const override
; // override
142 /* StoreClient API */
143 LogTags
*loggingTags() const override
;
144 void fillChecklist(ACLFilledChecklist
&) const override
;
147 const char *method
= nullptr;
148 const char *uri
= nullptr;
149 char *version
= nullptr;
150 char *req_hdrs
= nullptr;
151 size_t reqHdrsSz
= 0; ///< size of the req_hdrs content
152 HttpRequest::Pointer request
;
154 /// optimization: nil until needed
155 mutable AccessLogEntryPointer al
;
158 HttpRequest::Pointer checkHitRequest
;
161 htcpDataHeader
*dhdr
= nullptr;
166 MEMPROXY_CLASS(htcpDetail
);
168 char *resp_hdrs
= nullptr;
169 size_t respHdrsSz
= 0;
171 char *entity_hdrs
= nullptr;
172 size_t entityHdrsSz
= 0;
174 char *cache_hdrs
= nullptr;
175 size_t cacheHdrsSz
= 0;
181 htcpStuff(uint32_t id
, int o
, int r
, int f
) :
207 static const char *const htcpOpcodeStr
[] = {
217 * values for htcpDataHeader->response
222 OPCODE_UNIMPLEMENTED
,
223 MAJOR_VERSION_UNSUPPORTED
,
224 MINOR_VERSION_UNSUPPORTED
,
229 * values for htcpDataHeader->RR
236 static void htcpIncomingConnectionOpened(Ipc::StartListeningAnswer
&);
237 static uint32_t msg_id_counter
= 0;
239 static Comm::ConnectionPointer htcpOutgoingConn
= nullptr;
240 static Comm::ConnectionPointer htcpIncomingConn
= nullptr;
241 #define N_QUERIED_KEYS 8192
242 static uint32_t queried_id
[N_QUERIED_KEYS
];
243 static cache_key queried_keys
[N_QUERIED_KEYS
][SQUID_MD5_DIGEST_LENGTH
];
245 static Ip::Address queried_addr
[N_QUERIED_KEYS
];
247 static int old_squid_format
= 0;
249 static ssize_t
htcpBuildPacket(char *buf
, size_t buflen
, htcpStuff
* stuff
);
250 static htcpDetail
*htcpUnpackDetail(char *buf
, int sz
);
251 static ssize_t
htcpBuildAuth(char *buf
, size_t buflen
);
252 static ssize_t
htcpBuildCountstr(char *buf
, size_t buflen
, const char *s
, size_t len
);
253 static ssize_t
htcpBuildData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
254 static ssize_t
htcpBuildDetail(char *buf
, size_t buflen
, htcpStuff
* stuff
);
255 static ssize_t
htcpBuildOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
256 static ssize_t
htcpBuildSpecifier(char *buf
, size_t buflen
, htcpStuff
* stuff
);
257 static ssize_t
htcpBuildTstOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
259 static void htcpHandleMsg(char *buf
, int sz
, Ip::Address
&from
);
261 static void htcpLogHtcp(Ip::Address
&, const int, const LogTags_ot
, const char *, AccessLogEntryPointer
);
262 static void htcpHandleTst(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
264 static void htcpRecv(int fd
, void *data
);
266 static void htcpSend(const char *buf
, int len
, Ip::Address
&to
);
268 static void htcpTstReply(htcpDataHeader
*, StoreEntry
*, htcpSpecifier
*, Ip::Address
&);
270 static void htcpHandleTstRequest(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
272 static void htcpHandleTstResponse(htcpDataHeader
*, char *, int, Ip::Address
&);
275 htcpSyncAle(AccessLogEntryPointer
&al
, const Ip::Address
&caddr
, const int opcode
, const char *url
)
278 al
= new AccessLogEntry();
279 al
->cache
.caddr
= caddr
;
280 al
->htcp
.opcode
= htcpOpcodeStr
[opcode
];
282 al
->setVirginUrlForMissingRequest(al
->url
);
283 // HTCP transactions do not wait
284 al
->cache
.start_time
= current_time
;
285 al
->cache
.trTime
.tv_sec
= 0;
286 al
->cache
.trTime
.tv_usec
= 0;
290 htcpHexdump(const char *tag
, const char *s
, int sz
)
294 debugs(31, 3, "htcpHexdump " << tag
);
295 memset(hex
, '\0', sizeof(hex
));
297 for (int i
= 0; i
< sz
; ++i
) {
299 snprintf(&hex
[k
* 3], 4, " %02x", (int) *(s
+ i
));
301 if (k
< 15 && i
< (sz
- 1))
304 debugs(31, 3, "\t" << hex
);
306 memset(hex
, '\0', sizeof(hex
));
316 * STUFF FOR SENDING HTCP MESSAGES
320 htcpBuildAuth(char *buf
, size_t buflen
)
324 assert(2 == sizeof(uint16_t));
325 auth
.length
= htons(2);
327 if (buflen
< copy_sz
)
329 memcpy(buf
, &auth
, copy_sz
);
334 htcpBuildCountstr(char *buf
, size_t buflen
, const char *s
, size_t len
)
338 if (buflen
- off
< 2)
341 debugs(31, 3, "htcpBuildCountstr: LENGTH = " << len
);
343 debugs(31, 3, "htcpBuildCountstr: TEXT = {" << (s
? s
: "<NULL>") << "}");
345 uint16_t length
= htons((uint16_t) len
);
347 memcpy(buf
+ off
, &length
, 2);
351 if (buflen
- off
< len
)
355 memcpy(buf
+ off
, s
, len
);
363 htcpBuildSpecifier(char *buf
, size_t buflen
, htcpStuff
* stuff
)
367 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.method
, (stuff
->S
.method
?strlen(stuff
->S
.method
):0));
374 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.uri
, (stuff
->S
.uri
?strlen(stuff
->S
.uri
):0));
381 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.version
, (stuff
->S
.version
?strlen(stuff
->S
.version
):0));
388 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.req_hdrs
, stuff
->S
.reqHdrsSz
);
395 debugs(31, 3, "htcpBuildSpecifier: size " << off
);
401 htcpBuildDetail(char *buf
, size_t buflen
, htcpStuff
* stuff
)
405 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.resp_hdrs
, stuff
->D
.respHdrsSz
);
412 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.entity_hdrs
, stuff
->D
.entityHdrsSz
);
419 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.cache_hdrs
, stuff
->D
.cacheHdrsSz
);
430 htcpBuildTstOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
435 debugs(31, 3, "htcpBuildTstOpData: RR_REQUEST");
436 return htcpBuildSpecifier(buf
, buflen
, stuff
);
439 debugs(31, 3, "htcpBuildTstOpData: RR_RESPONSE");
440 debugs(31, 3, "htcpBuildTstOpData: F1 = " << stuff
->f1
);
442 if (stuff
->f1
) /* cache miss */
445 return htcpBuildDetail(buf
, buflen
, stuff
);
448 fatal_dump("htcpBuildTstOpData: bad RR value");
455 htcpBuildClrOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
457 unsigned short reason
;
461 debugs(31, 3, "htcpBuildClrOpData: RR_REQUEST");
462 reason
= htons((unsigned short)stuff
->reason
);
463 memcpy(buf
, &reason
, 2);
464 return htcpBuildSpecifier(buf
+ 2, buflen
- 2, stuff
) + 2;
468 fatal_dump("htcpBuildClrOpData: bad RR value");
475 htcpBuildOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
478 debugs(31, 3, "htcpBuildOpData: opcode " << htcpOpcodeStr
[stuff
->op
]);
483 off
= htcpBuildTstOpData(buf
+ off
, buflen
, stuff
);
487 off
= htcpBuildClrOpData(buf
+ off
, buflen
, stuff
);
499 htcpBuildData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
503 size_t hdr_sz
= sizeof(htcpDataHeader
);
508 off
+= hdr_sz
; /* skip! */
510 op_data_sz
= htcpBuildOpData(buf
+ off
, buflen
- off
, stuff
);
517 debugs(31, 3, "htcpBuildData: hdr.length = " << off
);
519 if (!old_squid_format
) {
521 memset(&hdr
, 0, sizeof(hdr
));
522 /* convert multi-byte fields */
523 hdr
.msg_id
= htonl(stuff
->msg_id
);
524 hdr
.length
= htons(static_cast<uint16_t>(off
));
525 hdr
.opcode
= stuff
->op
;
526 hdr
.response
= stuff
->response
;
529 memcpy(buf
, &hdr
, hdr_sz
);
531 htcpDataHeaderSquid hdrSquid
;
532 memset(&hdrSquid
, 0, sizeof(hdrSquid
));
533 hdrSquid
.length
= htons(static_cast<uint16_t>(off
));
534 hdrSquid
.opcode
= stuff
->op
;
535 hdrSquid
.response
= stuff
->response
;
536 hdrSquid
.F1
= stuff
->f1
;
537 hdrSquid
.RR
= stuff
->rr
;
538 memcpy(buf
, &hdrSquid
, hdr_sz
);
541 debugs(31, 3, "htcpBuildData: size " << off
);
547 * Build an HTCP packet into buf, maximum length buflen.
548 * Returns the packet length, or zero on failure.
551 htcpBuildPacket(char *buf
, size_t buflen
, htcpStuff
* stuff
)
555 size_t hdr_sz
= sizeof(htcpHeader
);
557 /* skip the header -- we don't know the overall length */
559 if (buflen
< hdr_sz
) {
564 s
= htcpBuildData(buf
+ off
, buflen
- off
, stuff
);
571 s
= htcpBuildAuth(buf
+ off
, buflen
- off
);
578 hdr
.length
= htons((uint16_t) off
);
581 if (old_squid_format
)
586 memcpy(buf
, &hdr
, hdr_sz
);
588 debugs(31, 3, "htcpBuildPacket: size " << off
);
594 htcpSend(const char *buf
, int len
, Ip::Address
&to
)
597 htcpHexdump("htcpSend", buf
, len
);
599 if (comm_udp_sendto(htcpOutgoingConn
->fd
, to
, buf
, len
) < 0) {
601 debugs(31, 3, htcpOutgoingConn
<< " sendto: " << xstrerr(xerrno
));
603 ++statCounter
.htcp
.pkts_sent
;
607 * Unpack an HTCP SPECIFIER in place
608 * This will overwrite any following AUTH block
610 // XXX: this needs to be turned into an Htcp1::Parser inheriting from Http1::RequestParser
611 // but with different first-line and block unpacking logic.
612 static htcpSpecifier::Pointer
613 htcpUnpackSpecifier(char *buf
, int sz
)
615 static const htcpSpecifier::Pointer nil
;
616 htcpSpecifier::Pointer
s(new htcpSpecifier
);
617 HttpRequestMethod method
;
619 /* Find length of METHOD */
620 uint16_t l
= ntohs(*(uint16_t *) buf
);
625 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack METHOD");
633 debugs(31, 6, "htcpUnpackSpecifier: METHOD (" << l
<< "/" << sz
<< ") '" << s
->method
<< "'");
635 /* Find length of URI */
636 l
= ntohs(*(uint16_t *) buf
);
640 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack URI");
644 /* Add terminating null to METHOD */
652 debugs(31, 6, "htcpUnpackSpecifier: URI (" << l
<< "/" << sz
<< ") '" << s
->uri
<< "'");
654 /* Find length of VERSION */
655 l
= ntohs(*(uint16_t *) buf
);
659 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack VERSION");
663 /* Add terminating null to URI */
671 debugs(31, 6, "htcpUnpackSpecifier: VERSION (" << l
<< "/" << sz
<< ") '" << s
->version
<< "'");
673 /* Find length of REQ-HDRS */
674 l
= ntohs(*(uint16_t *) buf
);
678 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack REQ-HDRS");
682 /* Add terminating null to URI */
691 debugs(31, 6, "htcpUnpackSpecifier: REQ-HDRS (" << l
<< "/" << sz
<< ") '" << s
->req_hdrs
<< "'");
693 debugs(31, 3, "htcpUnpackSpecifier: " << sz
<< " bytes left");
696 * Add terminating null to REQ-HDRS. This is possible because we allocated
697 * an extra byte when we received the packet. This will overwrite any following
703 method
.HttpRequestMethodXXX(s
->method
);
705 const auto mx
= MasterXaction::MakePortless
<XactionInitiator::initHtcp
>();
706 s
->request
= HttpRequest::FromUrlXXX(s
->uri
, mx
, method
== Http::METHOD_NONE
? HttpRequestMethod(Http::METHOD_GET
) : method
);
708 debugs(31, 3, "failed to create request. Invalid URI?");
716 * Unpack an HTCP DETAIL in place
717 * This will overwrite any following AUTH block
720 htcpUnpackDetail(char *buf
, int sz
)
722 htcpDetail
*d
= new htcpDetail
;
724 /* Find length of RESP-HDRS */
725 uint16_t l
= ntohs(*(uint16_t *) buf
);
730 debugs(31, 3, "htcpUnpackDetail: failed to unpack RESP_HDRS");
741 /* Find length of ENTITY-HDRS */
742 l
= ntohs(*(uint16_t *) buf
);
747 debugs(31, 3, "htcpUnpackDetail: failed to unpack ENTITY_HDRS");
752 /* Add terminating null to RESP-HDRS */
755 /* Set ENTITY-HDRS */
758 d
->entity_hdrs
= buf
;
763 /* Find length of CACHE-HDRS */
764 l
= ntohs(*(uint16_t *) buf
);
769 debugs(31, 3, "htcpUnpackDetail: failed to unpack CACHE_HDRS");
774 /* Add terminating null to ENTITY-HDRS */
785 debugs(31, 3, "htcpUnpackDetail: " << sz
<< " bytes left");
788 * Add terminating null to CACHE-HDRS. This is possible because we allocated
789 * an extra byte when we received the packet. This will overwrite any following
798 htcpAccessAllowed(acl_access
* acl
, const htcpSpecifier::Pointer
&s
, Ip::Address
&from
)
800 /* default deny if no access list present */
804 ACLFilledChecklist
checklist(acl
, s
->request
.getRaw(), nullptr);
805 checklist
.src_addr
= from
;
806 checklist
.my_addr
.setNoAddr();
807 return checklist
.fastCheck().allowed();
811 htcpTstReply(htcpDataHeader
* dhdr
, StoreEntry
* e
, htcpSpecifier
* spec
, Ip::Address
&from
)
813 static char pkt
[8192];
814 HttpHeader
hdr(hoHtcpReply
);
817 htcpStuff
stuff(dhdr
->msg_id
, HTCP_TST
, RR_RESPONSE
, 0);
818 stuff
.response
= e
? 0 : 1;
819 debugs(31, 3, "htcpTstReply: response = " << stuff
.response
);
822 stuff
.S
.method
= spec
->method
;
823 stuff
.S
.request
= spec
->request
;
824 stuff
.S
.uri
= spec
->uri
;
825 stuff
.S
.version
= spec
->version
;
826 stuff
.S
.req_hdrs
= spec
->req_hdrs
;
827 stuff
.S
.reqHdrsSz
= spec
->reqHdrsSz
;
829 hdr
.putInt(Http::HdrType::AGE
, (e
->timestamp
<= squid_curtime
? (squid_curtime
- e
->timestamp
) : 0) );
831 hdr
.putInt(Http::HdrType::AGE
, 0);
835 stuff
.D
.resp_hdrs
= xstrdup(mb
.buf
);
836 stuff
.D
.respHdrsSz
= mb
.contentSize();
837 debugs(31, 3, "htcpTstReply: resp_hdrs = {" << stuff
.D
.resp_hdrs
<< "}");
841 if (e
&& e
->expires
> -1)
842 hdr
.putTime(Http::HdrType::EXPIRES
, e
->expires
);
844 if (e
&& e
->lastModified() > -1)
845 hdr
.putTime(Http::HdrType::LAST_MODIFIED
, e
->lastModified());
849 stuff
.D
.entity_hdrs
= xstrdup(mb
.buf
);
850 stuff
.D
.entityHdrsSz
= mb
.contentSize();
852 debugs(31, 3, "htcpTstReply: entity_hdrs = {" << stuff
.D
.entity_hdrs
<< "}");
858 if (const char *host
= spec
->request
->url
.host()) {
862 netdbHostData(host
, &samp
, &rtt
, &hops
);
865 char cto_buf
[SQUIDHOSTNAMELEN
+128];
866 snprintf(cto_buf
, sizeof(cto_buf
), "%s %d %f %d",
867 host
, samp
, 0.001 * rtt
, hops
);
868 hdr
.putExt("Cache-to-Origin", cto_buf
);
871 #endif /* USE_ICMP */
874 stuff
.D
.cache_hdrs
= xstrdup(mb
.buf
);
875 stuff
.D
.cacheHdrsSz
= mb
.contentSize();
876 debugs(31, 3, "htcpTstReply: cache_hdrs = {" << stuff
.D
.cache_hdrs
<< "}");
881 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
883 safe_free(stuff
.D
.resp_hdrs
);
884 stuff
.D
.respHdrsSz
= 0;
885 safe_free(stuff
.D
.entity_hdrs
);
886 stuff
.D
.entityHdrsSz
= 0;
887 safe_free(stuff
.D
.cache_hdrs
);
888 stuff
.D
.cacheHdrsSz
= 0;
891 debugs(31, 3, "htcpTstReply: htcpBuildPacket() failed");
895 htcpSend(pkt
, (int) pktlen
, from
);
900 htcpClrReply(htcpDataHeader
* dhdr
, int purgeSucceeded
, Ip::Address
&from
)
902 static char pkt
[8192];
905 /* If dhdr->F1 == 0, no response desired */
910 htcpStuff
stuff(dhdr
->msg_id
, HTCP_CLR
, RR_RESPONSE
, 0);
912 stuff
.response
= purgeSucceeded
? 0 : 2;
914 debugs(31, 3, "htcpClrReply: response = " << stuff
.response
);
916 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
919 debugs(31, 3, "htcpClrReply: htcpBuildPacket() failed");
923 htcpSend(pkt
, (int) pktlen
, from
);
927 htcpSpecifier::codeContextGist() const
930 const auto gist
= al
->codeContextGist();
936 if (const auto &mx
= request
->masterXaction
)
937 return mx
->id
.detach();
940 return ScopedId("HTCP w/o master");
944 htcpSpecifier::detailCodeContext(std::ostream
&os
) const
947 return al
->detailCodeContext(os
);
950 if (const auto &mx
= request
->masterXaction
)
951 return os
<< Debug::Extra
<< "current master transaction: " << mx
->id
;
954 // TODO: Report method, uri, and version if they have been set
959 htcpSpecifier::checkHit()
961 checkHitRequest
= request
;
963 if (!checkHitRequest
) {
964 debugs(31, 3, "htcpCheckHit: NO; failed to parse URL");
969 if (!checkHitRequest
->parseHeader(req_hdrs
, reqHdrsSz
)) {
970 debugs(31, 3, "htcpCheckHit: NO; failed to parse request headers");
971 checkHitRequest
= nullptr;
976 const auto e
= storeGetPublicByRequest(checkHitRequest
.getRaw());
977 StoreEntry
*hit
= nullptr;
980 debugs(31, 3, "NO; public object not found");
981 } else if (!e
->validToSend()) {
982 debugs(31, 3, "NO; entry not valid to send" );
983 } else if (refreshCheckHTCP(e
, checkHitRequest
.getRaw())) {
984 debugs(31, 3, "NO; cached response is stale");
985 } else if (e
->hittingRequiresCollapsing() && !startCollapsingOn(*e
, false)) {
986 debugs(31, 3, "NO; prohibited CF hit: " << *e
);
988 debugs(31, 3, "YES!?");
994 // TODO: StoreClients must either store/lock or abandon found entries.
1000 htcpSpecifier::loggingTags() const
1002 // calling htcpSyncAle() here would not change cache.code
1004 al
= new AccessLogEntry();
1005 return &al
->cache
.code
;
1009 htcpSpecifier::fillChecklist(ACLFilledChecklist
&checklist
) const
1011 checklist
.setRequest(request
.getRaw());
1012 htcpSyncAle(al
, from
, dhdr
->opcode
, uri
);
1017 htcpClrStoreEntry(StoreEntry
* e
)
1019 debugs(31, 4, "htcpClrStoreEntry: Clearing store for entry: " << e
->url() );
1020 e
->releaseRequest();
1024 htcpClrStore(const htcpSpecifier::Pointer
&s
)
1026 HttpRequestPointer
request(s
->request
);
1028 debugs(31, 3, "htcpClrStore: failed to parse URL");
1032 /* Parse request headers */
1033 if (!request
->parseHeader(s
->req_hdrs
, s
->reqHdrsSz
)) {
1034 debugs(31, 2, "htcpClrStore: failed to parse request headers");
1038 StoreEntry
*e
= nullptr;
1040 /* Lookup matching entries. This matches both GET and HEAD */
1041 while ((e
= storeGetPublicByRequest(request
.getRaw()))) {
1042 htcpClrStoreEntry(e
);
1047 debugs(31, 4, "htcpClrStore: Cleared " << released
<< " matching entries");
1050 debugs(31, 4, "htcpClrStore: No matching entry found");
1057 htcpHandleTst(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1059 debugs(31, 3, "htcpHandleTst: sz = " << sz
);
1061 if (hdr
->RR
== RR_REQUEST
)
1062 htcpHandleTstRequest(hdr
, buf
, sz
, from
);
1064 htcpHandleTstResponse(hdr
, buf
, sz
, from
);
1067 HtcpReplyData::HtcpReplyData() :
1068 hit(0), hdr(hoHtcpReply
), msg_id(0), version(0.0)
1070 memset(&cto
, 0, sizeof(cto
));
1074 HtcpReplyData::parseHeader(const char *buffer
, const size_t size
)
1076 Http::ContentLengthInterpreter interpreter
;
1077 // no applyStatusCodeRules() -- HTCP replies lack cached HTTP status code
1078 return hdr
.parse(buffer
, size
, interpreter
);
1083 htcpHandleTstResponse(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1085 HtcpReplyData htcpReply
;
1086 cache_key
*key
= nullptr;
1089 htcpDetail
*d
= nullptr;
1092 if (queried_id
[hdr
->msg_id
% N_QUERIED_KEYS
] != hdr
->msg_id
) {
1093 debugs(31, 2, "htcpHandleTstResponse: No matching query id '" <<
1094 hdr
->msg_id
<< "' (expected " <<
1095 queried_id
[hdr
->msg_id
% N_QUERIED_KEYS
] << ") from '" <<
1101 key
= queried_keys
[hdr
->msg_id
% N_QUERIED_KEYS
];
1104 debugs(31, 3, "htcpHandleTstResponse: No query key for response id '" << hdr
->msg_id
<< "' from '" << from
<< "'");
1108 peer
= &queried_addr
[hdr
->msg_id
% N_QUERIED_KEYS
];
1110 if ( *peer
!= from
|| peer
->port() != from
.port() ) {
1111 debugs(31, 3, "htcpHandleTstResponse: Unexpected response source " << from
);
1116 debugs(31, 2, "htcpHandleTstResponse: error condition, F1/MO == 1");
1120 htcpReply
.msg_id
= hdr
->msg_id
;
1121 debugs(31, 3, "htcpHandleTstResponse: msg_id = " << htcpReply
.msg_id
);
1122 htcpReply
.hit
= hdr
->response
? 0 : 1;
1125 debugs(31, 3, "htcpHandleTstResponse: MISS");
1127 debugs(31, 3, "htcpHandleTstResponse: HIT");
1128 d
= htcpUnpackDetail(buf
, sz
);
1131 debugs(31, 3, "htcpHandleTstResponse: bad DETAIL");
1135 if ((t
= d
->resp_hdrs
))
1136 htcpReply
.parseHeader(t
, d
->respHdrsSz
);
1138 if ((t
= d
->entity_hdrs
))
1139 htcpReply
.parseHeader(t
, d
->entityHdrsSz
);
1141 if ((t
= d
->cache_hdrs
))
1142 htcpReply
.parseHeader(t
, d
->cacheHdrsSz
);
1145 debugs(31, 3, "htcpHandleTstResponse: key (" << key
<< ") " << storeKeyText(key
));
1146 neighborsHtcpReply(key
, &htcpReply
, from
);
1147 htcpReply
.hdr
.clean();
1153 htcpHandleTstRequest(htcpDataHeader
* dhdr
, char *buf
, int sz
, Ip::Address
&from
)
1156 debugs(31, 3, "htcpHandleTst: nothing to do");
1163 /* buf should be a SPECIFIER */
1164 htcpSpecifier::Pointer
s(htcpUnpackSpecifier(buf
, sz
));
1167 debugs(31, 3, "htcpHandleTstRequest: htcpUnpackSpecifier failed");
1168 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_INVALID
, dash_str
, nullptr);
1172 s
->setDataHeader(dhdr
);
1176 debugs(31, 3, "htcpHandleTstRequest: failed to parse request");
1177 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_INVALID
, dash_str
, s
->al
);
1181 if (!htcpAccessAllowed(Config
.accessList
.htcp
, s
, from
)) {
1182 debugs(31, 3, "htcpHandleTstRequest: Access denied");
1183 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_DENIED
, s
->uri
, s
->al
);
1187 debugs(31, 2, "HTCP TST request: " << s
->method
<< " " << s
->uri
<< " " << s
->version
);
1188 debugs(31, 2, "HTCP TST headers: " << s
->req_hdrs
);
1193 htcpSpecifier::checkedHit(StoreEntry
*e
)
1196 htcpTstReply(dhdr
, e
, this, from
); /* hit */
1197 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_HIT
, uri
, al
);
1199 htcpTstReply(dhdr
, nullptr, nullptr, from
); /* cache miss */
1200 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_MISS
, uri
, al
);
1205 htcpHandleClr(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1207 /* buf[0/1] is reserved and reason */
1208 int reason
= buf
[1] << 4;
1209 debugs(31, 2, "HTCP CLR reason: " << reason
);
1213 /* buf should be a SPECIFIER */
1216 debugs(31, 4, "htcpHandleClr: nothing to do");
1217 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
, nullptr);
1221 htcpSpecifier::Pointer
s(htcpUnpackSpecifier(buf
, sz
));
1224 debugs(31, 3, "htcpHandleClr: htcpUnpackSpecifier failed");
1225 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
, nullptr);
1229 s
->setDataHeader(hdr
);
1233 debugs(31, 3, "htcpHandleTstRequest: failed to parse request");
1234 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
, s
->al
);
1238 if (!htcpAccessAllowed(Config
.accessList
.htcp_clr
, s
, from
)) {
1239 debugs(31, 3, "htcpHandleClr: Access denied");
1240 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_DENIED
, s
->uri
, s
->al
);
1244 debugs(31, 2, "HTCP CLR request: " << s
->method
<< " " << s
->uri
<< " " << s
->version
);
1245 debugs(31, 2, "HTCP CLR headers: " << s
->req_hdrs
);
1247 /* Release objects from cache
1248 * analog to clientPurgeRequest in client_side.c
1251 switch (htcpClrStore(s
)) {
1254 htcpClrReply(hdr
, 1, from
); /* hit */
1255 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_HIT
, s
->uri
, s
->al
);
1259 htcpClrReply(hdr
, 0, from
); /* miss */
1260 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_MISS
, s
->uri
, s
->al
);
1269 * Forward a CLR request to all peers who have requested that CLRs be
1270 * forwarded to them.
1273 htcpForwardClr(char *buf
, int sz
)
1277 for (p
= Config
.peers
; p
; p
= p
->next
) {
1278 if (!p
->options
.htcp
) {
1281 if (!p
->options
.htcp_forward_clr
) {
1285 htcpSend(buf
, sz
, p
->in_addr
);
1290 * Do the first pass of handling an HTCP message. This used to be two
1291 * separate functions, htcpHandle and htcpHandleData. They were merged to
1292 * allow for forwarding HTCP packets easily to other peers if desired.
1294 * This function now works out what type of message we have received and then
1295 * hands it off to other functions to break apart message-specific data.
1298 htcpHandleMsg(char *buf
, int sz
, Ip::Address
&from
)
1300 // TODO: function-scoped CodeContext::Reset(...("HTCP message from", from))
1307 if (sz
< 0 || (size_t)sz
< sizeof(htcpHeader
)) {
1308 // These are highly likely to be attack packets. Should probably get a bigger warning.
1309 debugs(31, 2, "htcpHandle: msg size less than htcpHeader size from " << from
);
1313 htcpHexdump("htcpHandle", buf
, sz
);
1314 memcpy(&htcpHdr
, buf
, sizeof(htcpHeader
));
1315 htcpHdr
.length
= ntohs(htcpHdr
.length
);
1317 if (htcpHdr
.minor
== 0)
1318 old_squid_format
= 1;
1320 old_squid_format
= 0;
1322 debugs(31, 3, "htcpHandle: htcpHdr.length = " << htcpHdr
.length
);
1323 debugs(31, 3, "htcpHandle: htcpHdr.major = " << htcpHdr
.major
);
1324 debugs(31, 3, "htcpHandle: htcpHdr.minor = " << htcpHdr
.minor
);
1326 if (sz
!= htcpHdr
.length
) {
1327 debugs(31, 3, "htcpHandle: sz/" << sz
<< " != htcpHdr.length/" <<
1328 htcpHdr
.length
<< " from " << from
);
1333 if (htcpHdr
.major
!= 0) {
1334 debugs(31, 3, "htcpHandle: Unknown major version " << htcpHdr
.major
<< " from " << from
);
1339 hbuf
= buf
+ sizeof(htcpHeader
);
1340 hsz
= sz
- sizeof(htcpHeader
);
1342 if ((size_t)hsz
< sizeof(htcpDataHeader
)) {
1343 debugs(31, 3, "htcpHandleData: msg size less than htcpDataHeader size");
1347 if (!old_squid_format
) {
1348 memcpy(&hdr
, hbuf
, sizeof(hdr
));
1350 htcpDataHeaderSquid hdrSquid
;
1351 memcpy(&hdrSquid
, hbuf
, sizeof(hdrSquid
));
1352 hdr
.length
= hdrSquid
.length
;
1353 hdr
.opcode
= hdrSquid
.opcode
;
1354 hdr
.response
= hdrSquid
.response
;
1355 hdr
.F1
= hdrSquid
.F1
;
1356 hdr
.RR
= hdrSquid
.RR
;
1358 hdr
.msg_id
= hdrSquid
.msg_id
;
1361 hdr
.length
= ntohs(hdr
.length
);
1362 hdr
.msg_id
= ntohl(hdr
.msg_id
);
1363 debugs(31, 3, "htcpHandleData: hsz = " << hsz
);
1364 debugs(31, 3, "htcpHandleData: length = " << hdr
.length
);
1366 if (hdr
.opcode
>= HTCP_END
) {
1367 debugs(31, 3, "htcpHandleData: client " << from
<< ", opcode " << hdr
.opcode
<< " out of range");
1371 debugs(31, 3, "htcpHandleData: opcode = " << hdr
.opcode
<< " " << htcpOpcodeStr
[hdr
.opcode
]);
1372 debugs(31, 3, "htcpHandleData: response = " << hdr
.response
);
1373 debugs(31, 3, "htcpHandleData: F1 = " << hdr
.F1
);
1374 debugs(31, 3, "htcpHandleData: RR = " << hdr
.RR
);
1375 debugs(31, 3, "htcpHandleData: msg_id = " << hdr
.msg_id
);
1377 if (hsz
< hdr
.length
) {
1378 debugs(31, 3, "htcpHandleData: sz < hdr.length");
1383 * set sz = hdr.length so we ignore any AUTH fields following
1386 hsz
= (int) hdr
.length
;
1387 hbuf
+= sizeof(htcpDataHeader
);
1388 hsz
-= sizeof(htcpDataHeader
);
1389 debugs(31, 3, "htcpHandleData: hsz = " << hsz
);
1391 htcpHexdump("htcpHandleData", hbuf
, hsz
);
1393 switch (hdr
.opcode
) {
1395 debugs(31, 3, "HTCP NOP not implemented");
1398 htcpHandleTst(&hdr
, hbuf
, hsz
, from
);
1401 debugs(31, 3, "HTCP MON not implemented");
1404 debugs(31, 3, "HTCP SET not implemented");
1407 htcpHandleClr(&hdr
, hbuf
, hsz
, from
);
1408 htcpForwardClr(buf
, sz
);
1416 htcpRecv(int fd
, void *)
1418 static char buf
[8192];
1420 static Ip::Address from
;
1422 /* Receive up to 8191 bytes, leaving room for a null */
1424 len
= comm_udp_recvfrom(fd
, buf
, sizeof(buf
) - 1, 0, from
);
1426 debugs(31, 3, "htcpRecv: FD " << fd
<< ", " << len
<< " bytes from " << from
);
1429 ++statCounter
.htcp
.pkts_recv
;
1431 htcpHandleMsg(buf
, len
, from
);
1433 Comm::SetSelect(fd
, COMM_SELECT_READ
, htcpRecv
, nullptr, 0);
1437 * ======================================================================
1439 * ======================================================================
1445 if (Config
.Port
.htcp
<= 0) {
1446 debugs(31, Important(21), "HTCP Disabled.");
1450 htcpIncomingConn
= new Comm::Connection
;
1451 htcpIncomingConn
->local
= Config
.Addrs
.udp_incoming
;
1452 htcpIncomingConn
->local
.port(Config
.Port
.htcp
);
1454 if (!Ip::EnableIpv6
&& !htcpIncomingConn
->local
.setIPv4()) {
1455 debugs(31, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << htcpIncomingConn
->local
<< " is not an IPv4 address.");
1456 fatal("HTCP port cannot be opened.");
1458 /* split-stack for now requires default IPv4-only HTCP */
1459 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& htcpIncomingConn
->local
.isAnyAddr()) {
1460 htcpIncomingConn
->local
.setIPv4();
1463 auto call
= asyncCallbackFun(31, 2, htcpIncomingConnectionOpened
);
1464 Ipc::StartListening(SOCK_DGRAM
,
1467 Ipc::fdnInHtcpSocket
, call
);
1469 if (!Config
.Addrs
.udp_outgoing
.isNoAddr()) {
1470 htcpOutgoingConn
= new Comm::Connection
;
1471 htcpOutgoingConn
->local
= Config
.Addrs
.udp_outgoing
;
1472 htcpOutgoingConn
->local
.port(Config
.Port
.htcp
);
1474 if (!Ip::EnableIpv6
&& !htcpOutgoingConn
->local
.setIPv4()) {
1475 debugs(31, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << htcpOutgoingConn
->local
<< " is not an IPv4 address.");
1476 fatal("HTCP port cannot be opened.");
1478 /* split-stack for now requires default IPv4-only HTCP */
1479 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& htcpOutgoingConn
->local
.isAnyAddr()) {
1480 htcpOutgoingConn
->local
.setIPv4();
1484 comm_open_listener(SOCK_DGRAM
, IPPROTO_UDP
, htcpOutgoingConn
, "Outgoing HTCP Socket");
1487 if (!Comm::IsConnOpen(htcpOutgoingConn
))
1488 fatal("Cannot open Outgoing HTCP Socket");
1490 Comm::SetSelect(htcpOutgoingConn
->fd
, COMM_SELECT_READ
, htcpRecv
, nullptr, 0);
1492 debugs(31, DBG_IMPORTANT
, "Sending HTCP messages from " << htcpOutgoingConn
->local
);
1498 htcpIncomingConnectionOpened(Ipc::StartListeningAnswer
&answer
)
1500 const auto &conn
= answer
.conn
;
1502 if (!Comm::IsConnOpen(conn
))
1503 fatal("Cannot open HTCP Socket");
1505 Comm::SetSelect(conn
->fd
, COMM_SELECT_READ
, htcpRecv
, nullptr, 0);
1507 debugs(31, DBG_CRITICAL
, "Accepting HTCP messages on " << conn
->local
);
1509 if (Config
.Addrs
.udp_outgoing
.isNoAddr()) {
1510 htcpOutgoingConn
= conn
;
1511 debugs(31, DBG_IMPORTANT
, "Sending HTCP messages from " << htcpOutgoingConn
->local
);
1516 htcpQuery(StoreEntry
* e
, HttpRequest
* req
, CachePeer
* p
)
1518 cache_key
*save_key
;
1519 static char pkt
[8192];
1522 HttpHeader
hdr(hoRequest
);
1523 Http::StateFlags flags
;
1525 if (!Comm::IsConnOpen(htcpIncomingConn
))
1528 old_squid_format
= p
->options
.htcp_oldsquid
;
1529 snprintf(vbuf
, sizeof(vbuf
), "%d/%d",
1530 req
->http_ver
.major
, req
->http_ver
.minor
);
1532 htcpStuff
stuff(++msg_id_counter
, HTCP_TST
, RR_REQUEST
, 1);
1533 SBuf sb
= req
->method
.image();
1534 stuff
.S
.method
= sb
.c_str();
1535 stuff
.S
.uri
= (char *) e
->url();
1536 stuff
.S
.version
= vbuf
;
1537 HttpStateData::httpBuildRequestHeader(req
, e
, nullptr, &hdr
, flags
);
1542 stuff
.S
.req_hdrs
= mb
.buf
;
1543 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
1546 debugs(31, 3, "htcpQuery: htcpBuildPacket() failed");
1550 htcpSend(pkt
, (int) pktlen
, p
->in_addr
);
1552 queried_id
[stuff
.msg_id
% N_QUERIED_KEYS
] = stuff
.msg_id
;
1553 save_key
= queried_keys
[stuff
.msg_id
% N_QUERIED_KEYS
];
1554 storeKeyCopy(save_key
, (const cache_key
*)e
->key
);
1555 queried_addr
[stuff
.msg_id
% N_QUERIED_KEYS
] = p
->in_addr
;
1556 debugs(31, 3, "htcpQuery: key (" << save_key
<< ") " << storeKeyText(save_key
));
1562 * Send an HTCP CLR message for a specified item to a given CachePeer.
1565 htcpClear(StoreEntry
* e
, HttpRequest
* req
, const HttpRequestMethod
&, CachePeer
* p
, htcp_clr_reason reason
)
1567 static char pkt
[8192];
1570 HttpHeader
hdr(hoRequest
);
1572 Http::StateFlags flags
;
1574 if (!Comm::IsConnOpen(htcpIncomingConn
))
1577 old_squid_format
= p
->options
.htcp_oldsquid
;
1578 snprintf(vbuf
, sizeof(vbuf
), "%d/%d",
1579 req
->http_ver
.major
, req
->http_ver
.minor
);
1581 htcpStuff
stuff(++msg_id_counter
, HTCP_CLR
, RR_REQUEST
, 0);
1582 if (reason
== HTCP_CLR_INVALIDATION
)
1585 SBuf sb
= req
->method
.image();
1586 stuff
.S
.method
= sb
.c_str();
1587 stuff
.S
.request
= req
;
1588 SBuf uri
= req
->effectiveRequestUri();
1589 stuff
.S
.uri
= uri
.c_str();
1590 stuff
.S
.version
= vbuf
;
1591 if (reason
!= HTCP_CLR_INVALIDATION
) {
1592 HttpStateData::httpBuildRequestHeader(req
, e
, nullptr, &hdr
, flags
);
1596 stuff
.S
.req_hdrs
= mb
.buf
;
1598 stuff
.S
.req_hdrs
= nullptr;
1600 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
1601 if (reason
!= HTCP_CLR_INVALIDATION
) {
1605 debugs(31, 3, "htcpClear: htcpBuildPacket() failed");
1609 htcpSend(pkt
, (int) pktlen
, p
->in_addr
);
1613 * htcpSocketShutdown only closes the 'in' socket if it is
1614 * different than the 'out' socket.
1617 htcpSocketShutdown(void)
1619 if (!Comm::IsConnOpen(htcpIncomingConn
))
1622 debugs(12, DBG_IMPORTANT
, "Stop accepting HTCP on " << htcpIncomingConn
->local
);
1624 * Here we just unlink htcpIncomingConn because the HTCP 'in'
1625 * and 'out' sockets might be just one FD. This prevents this
1626 * function from executing repeatedly. When we are really ready to
1627 * exit or restart, main will comm_close the 'out' descriptor.
1629 htcpIncomingConn
= nullptr;
1632 * Normally we only write to the outgoing HTCP socket, but
1633 * we also have a read handler there to catch messages sent
1634 * to that specific interface. During shutdown, we must
1635 * disable reading on the outgoing socket.
1637 /* XXX Don't we need this handler to read replies while shutting down?
1638 * I think there should be a separate handler for reading replies..
1640 assert(Comm::IsConnOpen(htcpOutgoingConn
));
1642 Comm::SetSelect(htcpOutgoingConn
->fd
, COMM_SELECT_READ
, nullptr, nullptr, 0);
1646 htcpClosePorts(void)
1648 htcpSocketShutdown();
1650 if (htcpOutgoingConn
!= nullptr) {
1651 debugs(12, DBG_IMPORTANT
, "Stop sending HTCP from " << htcpOutgoingConn
->local
);
1652 htcpOutgoingConn
= nullptr;
1657 htcpLogHtcp(Ip::Address
&caddr
, const int opcode
, const LogTags_ot logcode
, const char *url
, AccessLogEntryPointer al
)
1659 if (!Config
.onoff
.log_udp
)
1662 htcpSyncAle(al
, caddr
, opcode
, url
);
1664 assert(logcode
!= LOG_TAG_NONE
);
1665 al
->cache
.code
.update(logcode
);
1667 accessLogLog(al
, nullptr);