2 * Copyright (C) 1996-2017 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 "CachePeer.h"
17 #include "comm/Connection.h"
18 #include "comm/Loops.h"
19 #include "comm/UdpOpenDialer.h"
20 #include "compat/xalloc.h"
24 #include "HttpRequest.h"
25 #include "icmp/net_db.h"
28 #include "mem/forward.h"
31 #include "SquidConfig.h"
32 #include "SquidTime.h"
33 #include "StatCounters.h"
35 #include "store_key_md5.h"
36 #include "StoreClient.h"
40 typedef struct _Countstr Countstr
;
42 typedef struct _htcpHeader htcpHeader
;
44 typedef struct _htcpDataHeader htcpDataHeader
;
46 typedef struct _htcpDataHeaderSquid htcpDataHeaderSquid
;
48 typedef struct _htcpAuthHeader htcpAuthHeader
;
61 struct _htcpDataHeaderSquid
{
65 unsigned int opcode
:4;
66 unsigned int response
:4;
68 unsigned int response
:4;
69 unsigned int opcode
:4;
73 unsigned int reserved
:6;
79 unsigned int reserved
:6;
85 struct _htcpDataHeader
{
109 /* RR == 0 --> F1 = RESPONSE DESIRED FLAG */
110 /* RR == 1 --> F1 = MESSAGE OVERALL FLAG */
111 /* RR == 0 --> REQUEST */
112 /* RR == 1 --> RESPONSE */
114 struct _htcpAuthHeader
{
122 class htcpSpecifier
: public RefCountable
, public StoreClient
124 MEMPROXY_CLASS(htcpSpecifier
);
127 typedef RefCount
<htcpSpecifier
> Pointer
;
130 void checkedHit(StoreEntry
*);
132 void setFrom(Ip::Address
&anIp
) { from
= anIp
; }
133 void setDataHeader(htcpDataHeader
*aDataHeader
) {
137 /* StoreClient API */
138 void created(StoreEntry
*);
141 const char *method
= nullptr;
143 char *version
= nullptr;
144 char *req_hdrs
= nullptr;
145 size_t reqHdrsSz
= 0; ///< size of the req_hdrs content
146 HttpRequest::Pointer request
;
149 HttpRequest::Pointer checkHitRequest
;
152 htcpDataHeader
*dhdr
= nullptr;
156 MEMPROXY_CLASS(htcpDetail
);
158 htcpDetail() : resp_hdrs(nullptr), respHdrsSz(0), entity_hdrs(nullptr), entityHdrsSz(0), cache_hdrs(nullptr), cacheHdrsSz(0) {}
172 htcpStuff(uint32_t id
, int o
, int r
, int f
) :
180 memset(&D
, 0, sizeof(D
));
202 static const char *const htcpOpcodeStr
[] = {
212 * values for htcpDataHeader->response
217 OPCODE_UNIMPLEMENTED
,
218 MAJOR_VERSION_UNSUPPORTED
,
219 MINOR_VERSION_UNSUPPORTED
,
224 * values for htcpDataHeader->RR
231 static void htcpIncomingConnectionOpened(const Comm::ConnectionPointer
&conn
, int errNo
);
232 static uint32_t msg_id_counter
= 0;
234 static Comm::ConnectionPointer htcpOutgoingConn
= NULL
;
235 static Comm::ConnectionPointer htcpIncomingConn
= NULL
;
236 #define N_QUERIED_KEYS 8192
237 static uint32_t queried_id
[N_QUERIED_KEYS
];
238 static cache_key queried_keys
[N_QUERIED_KEYS
][SQUID_MD5_DIGEST_LENGTH
];
240 static Ip::Address queried_addr
[N_QUERIED_KEYS
];
242 static int old_squid_format
= 0;
244 static ssize_t
htcpBuildPacket(char *buf
, size_t buflen
, htcpStuff
* stuff
);
245 static htcpDetail
*htcpUnpackDetail(char *buf
, int sz
);
246 static ssize_t
htcpBuildAuth(char *buf
, size_t buflen
);
247 static ssize_t
htcpBuildCountstr(char *buf
, size_t buflen
, const char *s
, size_t len
);
248 static ssize_t
htcpBuildData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
249 static ssize_t
htcpBuildDetail(char *buf
, size_t buflen
, htcpStuff
* stuff
);
250 static ssize_t
htcpBuildOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
251 static ssize_t
htcpBuildSpecifier(char *buf
, size_t buflen
, htcpStuff
* stuff
);
252 static ssize_t
htcpBuildTstOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
254 static void htcpHandleMsg(char *buf
, int sz
, Ip::Address
&from
);
256 static void htcpLogHtcp(Ip::Address
&, int, LogTags
, const char *);
257 static void htcpHandleTst(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
259 static void htcpRecv(int fd
, void *data
);
261 static void htcpSend(const char *buf
, int len
, Ip::Address
&to
);
263 static void htcpTstReply(htcpDataHeader
*, StoreEntry
*, htcpSpecifier
*, Ip::Address
&);
265 static void htcpHandleTstRequest(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
267 static void htcpHandleTstResponse(htcpDataHeader
*, char *, int, Ip::Address
&);
270 htcpHexdump(const char *tag
, const char *s
, int sz
)
274 debugs(31, 3, "htcpHexdump " << tag
);
275 memset(hex
, '\0', sizeof(hex
));
277 for (int i
= 0; i
< sz
; ++i
) {
279 snprintf(&hex
[k
* 3], 4, " %02x", (int) *(s
+ i
));
281 if (k
< 15 && i
< (sz
- 1))
284 debugs(31, 3, "\t" << hex
);
286 memset(hex
, '\0', sizeof(hex
));
292 * STUFF FOR SENDING HTCP MESSAGES
296 htcpBuildAuth(char *buf
, size_t buflen
)
300 assert(2 == sizeof(uint16_t));
301 auth
.length
= htons(2);
303 if (buflen
< copy_sz
)
305 memcpy(buf
, &auth
, copy_sz
);
310 htcpBuildCountstr(char *buf
, size_t buflen
, const char *s
, size_t len
)
314 if (buflen
- off
< 2)
317 debugs(31, 3, "htcpBuildCountstr: LENGTH = " << len
);
319 debugs(31, 3, "htcpBuildCountstr: TEXT = {" << (s
? s
: "<NULL>") << "}");
321 uint16_t length
= htons((uint16_t) len
);
323 memcpy(buf
+ off
, &length
, 2);
327 if (buflen
- off
< len
)
331 memcpy(buf
+ off
, s
, len
);
339 htcpBuildSpecifier(char *buf
, size_t buflen
, htcpStuff
* stuff
)
343 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.method
, (stuff
->S
.method
?strlen(stuff
->S
.method
):0));
350 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.uri
, (stuff
->S
.uri
?strlen(stuff
->S
.uri
):0));
357 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.version
, (stuff
->S
.version
?strlen(stuff
->S
.version
):0));
364 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.req_hdrs
, stuff
->S
.reqHdrsSz
);
371 debugs(31, 3, "htcpBuildSpecifier: size " << off
);
377 htcpBuildDetail(char *buf
, size_t buflen
, htcpStuff
* stuff
)
381 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.resp_hdrs
, stuff
->D
.respHdrsSz
);
388 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.entity_hdrs
, stuff
->D
.entityHdrsSz
);
395 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.cache_hdrs
, stuff
->D
.cacheHdrsSz
);
406 htcpBuildTstOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
411 debugs(31, 3, "htcpBuildTstOpData: RR_REQUEST");
412 return htcpBuildSpecifier(buf
, buflen
, stuff
);
415 debugs(31, 3, "htcpBuildTstOpData: RR_RESPONSE");
416 debugs(31, 3, "htcpBuildTstOpData: F1 = " << stuff
->f1
);
418 if (stuff
->f1
) /* cache miss */
421 return htcpBuildDetail(buf
, buflen
, stuff
);
424 fatal_dump("htcpBuildTstOpData: bad RR value");
431 htcpBuildClrOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
433 unsigned short reason
;
437 debugs(31, 3, "htcpBuildClrOpData: RR_REQUEST");
438 reason
= htons((unsigned short)stuff
->reason
);
439 memcpy(buf
, &reason
, 2);
440 return htcpBuildSpecifier(buf
+ 2, buflen
- 2, stuff
) + 2;
444 fatal_dump("htcpBuildClrOpData: bad RR value");
451 htcpBuildOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
454 debugs(31, 3, "htcpBuildOpData: opcode " << htcpOpcodeStr
[stuff
->op
]);
459 off
= htcpBuildTstOpData(buf
+ off
, buflen
, stuff
);
463 off
= htcpBuildClrOpData(buf
+ off
, buflen
, stuff
);
475 htcpBuildData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
479 size_t hdr_sz
= sizeof(htcpDataHeader
);
484 off
+= hdr_sz
; /* skip! */
486 op_data_sz
= htcpBuildOpData(buf
+ off
, buflen
- off
, stuff
);
493 debugs(31, 3, "htcpBuildData: hdr.length = " << off
);
495 if (!old_squid_format
) {
497 memset(&hdr
, 0, sizeof(hdr
));
498 /* convert multi-byte fields */
499 hdr
.msg_id
= htonl(stuff
->msg_id
);
500 hdr
.length
= htons(static_cast<uint16_t>(off
));
501 hdr
.opcode
= stuff
->op
;
502 hdr
.response
= stuff
->response
;
505 memcpy(buf
, &hdr
, hdr_sz
);
507 htcpDataHeaderSquid hdrSquid
;
508 memset(&hdrSquid
, 0, sizeof(hdrSquid
));
509 hdrSquid
.length
= htons(static_cast<uint16_t>(off
));
510 hdrSquid
.opcode
= stuff
->op
;
511 hdrSquid
.response
= stuff
->response
;
512 hdrSquid
.F1
= stuff
->f1
;
513 hdrSquid
.RR
= stuff
->rr
;
514 memcpy(buf
, &hdrSquid
, hdr_sz
);
517 debugs(31, 3, "htcpBuildData: size " << off
);
523 * Build an HTCP packet into buf, maximum length buflen.
524 * Returns the packet length, or zero on failure.
527 htcpBuildPacket(char *buf
, size_t buflen
, htcpStuff
* stuff
)
531 size_t hdr_sz
= sizeof(htcpHeader
);
533 /* skip the header -- we don't know the overall length */
535 if (buflen
< hdr_sz
) {
540 s
= htcpBuildData(buf
+ off
, buflen
- off
, stuff
);
547 s
= htcpBuildAuth(buf
+ off
, buflen
- off
);
554 hdr
.length
= htons((uint16_t) off
);
557 if (old_squid_format
)
562 memcpy(buf
, &hdr
, hdr_sz
);
564 debugs(31, 3, "htcpBuildPacket: size " << off
);
570 htcpSend(const char *buf
, int len
, Ip::Address
&to
)
573 htcpHexdump("htcpSend", buf
, len
);
575 if (comm_udp_sendto(htcpOutgoingConn
->fd
, to
, buf
, len
) < 0) {
577 debugs(31, 3, htcpOutgoingConn
<< " sendto: " << xstrerr(xerrno
));
579 ++statCounter
.htcp
.pkts_sent
;
583 * Unpack an HTCP SPECIFIER in place
584 * This will overwrite any following AUTH block
586 // XXX: this needs to be turned into an Htcp1::Parser inheriting from Http1::RequestParser
587 // but with different first-line and block unpacking logic.
588 static htcpSpecifier::Pointer
589 htcpUnpackSpecifier(char *buf
, int sz
)
591 static const htcpSpecifier::Pointer nil
;
592 htcpSpecifier::Pointer
s(new htcpSpecifier
);
593 HttpRequestMethod method
;
595 /* Find length of METHOD */
596 uint16_t l
= ntohs(*(uint16_t *) buf
);
601 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack METHOD");
609 debugs(31, 6, "htcpUnpackSpecifier: METHOD (" << l
<< "/" << sz
<< ") '" << s
->method
<< "'");
611 /* Find length of URI */
612 l
= ntohs(*(uint16_t *) buf
);
616 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack URI");
620 /* Add terminating null to METHOD */
628 debugs(31, 6, "htcpUnpackSpecifier: URI (" << l
<< "/" << sz
<< ") '" << s
->uri
<< "'");
630 /* Find length of VERSION */
631 l
= ntohs(*(uint16_t *) buf
);
635 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack VERSION");
639 /* Add terminating null to URI */
647 debugs(31, 6, "htcpUnpackSpecifier: VERSION (" << l
<< "/" << sz
<< ") '" << s
->version
<< "'");
649 /* Find length of REQ-HDRS */
650 l
= ntohs(*(uint16_t *) buf
);
654 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack REQ-HDRS");
658 /* Add terminating null to URI */
667 debugs(31, 6, "htcpUnpackSpecifier: REQ-HDRS (" << l
<< "/" << sz
<< ") '" << s
->req_hdrs
<< "'");
669 debugs(31, 3, "htcpUnpackSpecifier: " << sz
<< " bytes left");
672 * Add terminating null to REQ-HDRS. This is possible because we allocated
673 * an extra byte when we received the packet. This will overwrite any following
679 method
.HttpRequestMethodXXX(s
->method
);
681 const MasterXaction::Pointer mx
= new MasterXaction(XactionInitiator::initHtcp
);
682 s
->request
= HttpRequest::FromUrl(s
->uri
, mx
, method
== Http::METHOD_NONE
? HttpRequestMethod(Http::METHOD_GET
) : method
);
684 debugs(31, 3, "failed to create request. Invalid URI?");
692 * Unpack an HTCP DETAIL in place
693 * This will overwrite any following AUTH block
696 htcpUnpackDetail(char *buf
, int sz
)
698 htcpDetail
*d
= new htcpDetail
;
700 /* Find length of RESP-HDRS */
701 uint16_t l
= ntohs(*(uint16_t *) buf
);
706 debugs(31, 3, "htcpUnpackDetail: failed to unpack RESP_HDRS");
717 /* Find length of ENTITY-HDRS */
718 l
= ntohs(*(uint16_t *) buf
);
723 debugs(31, 3, "htcpUnpackDetail: failed to unpack ENTITY_HDRS");
728 /* Add terminating null to RESP-HDRS */
731 /* Set ENTITY-HDRS */
734 d
->entity_hdrs
= buf
;
739 /* Find length of CACHE-HDRS */
740 l
= ntohs(*(uint16_t *) buf
);
745 debugs(31, 3, "htcpUnpackDetail: failed to unpack CACHE_HDRS");
750 /* Add terminating null to ENTITY-HDRS */
761 debugs(31, 3, "htcpUnpackDetail: " << sz
<< " bytes left");
764 * Add terminating null to CACHE-HDRS. This is possible because we allocated
765 * an extra byte when we received the packet. This will overwrite any following
774 htcpAccessAllowed(acl_access
* acl
, const htcpSpecifier::Pointer
&s
, Ip::Address
&from
)
776 /* default deny if no access list present */
780 ACLFilledChecklist
checklist(acl
, s
->request
.getRaw(), nullptr);
781 checklist
.src_addr
= from
;
782 checklist
.my_addr
.setNoAddr();
783 return checklist
.fastCheck().allowed();
787 htcpTstReply(htcpDataHeader
* dhdr
, StoreEntry
* e
, htcpSpecifier
* spec
, Ip::Address
&from
)
789 static char pkt
[8192];
790 HttpHeader
hdr(hoHtcpReply
);
793 htcpStuff
stuff(dhdr
->msg_id
, HTCP_TST
, RR_RESPONSE
, 0);
794 stuff
.response
= e
? 0 : 1;
795 debugs(31, 3, "htcpTstReply: response = " << stuff
.response
);
798 stuff
.S
.method
= spec
->method
;
799 stuff
.S
.uri
= spec
->uri
;
800 stuff
.S
.version
= spec
->version
;
801 stuff
.S
.req_hdrs
= spec
->req_hdrs
;
802 stuff
.S
.reqHdrsSz
= spec
->reqHdrsSz
;
804 hdr
.putInt(Http::HdrType::AGE
, (e
->timestamp
<= squid_curtime
? (squid_curtime
- e
->timestamp
) : 0) );
806 hdr
.putInt(Http::HdrType::AGE
, 0);
810 stuff
.D
.resp_hdrs
= xstrdup(mb
.buf
);
811 stuff
.D
.respHdrsSz
= mb
.contentSize();
812 debugs(31, 3, "htcpTstReply: resp_hdrs = {" << stuff
.D
.resp_hdrs
<< "}");
816 if (e
&& e
->expires
> -1)
817 hdr
.putTime(Http::HdrType::EXPIRES
, e
->expires
);
819 if (e
&& e
->lastModified() > -1)
820 hdr
.putTime(Http::HdrType::LAST_MODIFIED
, e
->lastModified());
824 stuff
.D
.entity_hdrs
= xstrdup(mb
.buf
);
825 stuff
.D
.entityHdrsSz
= mb
.contentSize();
827 debugs(31, 3, "htcpTstReply: entity_hdrs = {" << stuff
.D
.entity_hdrs
<< "}");
833 if (char *host
= urlHostname(spec
->uri
)) {
837 netdbHostData(host
, &samp
, &rtt
, &hops
);
841 snprintf(cto_buf
, 128, "%s %d %f %d",
842 host
, samp
, 0.001 * rtt
, hops
);
843 hdr
.putExt("Cache-to-Origin", cto_buf
);
846 #endif /* USE_ICMP */
849 stuff
.D
.cache_hdrs
= xstrdup(mb
.buf
);
850 stuff
.D
.cacheHdrsSz
= mb
.contentSize();
851 debugs(31, 3, "htcpTstReply: cache_hdrs = {" << stuff
.D
.cache_hdrs
<< "}");
856 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
858 safe_free(stuff
.D
.resp_hdrs
);
859 stuff
.D
.respHdrsSz
= 0;
860 safe_free(stuff
.D
.entity_hdrs
);
861 stuff
.D
.entityHdrsSz
= 0;
862 safe_free(stuff
.D
.cache_hdrs
);
863 stuff
.D
.cacheHdrsSz
= 0;
866 debugs(31, 3, "htcpTstReply: htcpBuildPacket() failed");
870 htcpSend(pkt
, (int) pktlen
, from
);
875 htcpClrReply(htcpDataHeader
* dhdr
, int purgeSucceeded
, Ip::Address
&from
)
877 static char pkt
[8192];
880 /* If dhdr->F1 == 0, no response desired */
885 htcpStuff
stuff(dhdr
->msg_id
, HTCP_CLR
, RR_RESPONSE
, 0);
887 stuff
.response
= purgeSucceeded
? 0 : 2;
889 debugs(31, 3, "htcpClrReply: response = " << stuff
.response
);
891 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
894 debugs(31, 3, "htcpClrReply: htcpBuildPacket() failed");
898 htcpSend(pkt
, (int) pktlen
, from
);
902 htcpSpecifier::checkHit()
904 checkHitRequest
= request
;
906 if (!checkHitRequest
) {
907 debugs(31, 3, "htcpCheckHit: NO; failed to parse URL");
908 checkedHit(NullStoreEntry::getInstance());
912 if (!checkHitRequest
->header
.parse(req_hdrs
, reqHdrsSz
)) {
913 debugs(31, 3, "htcpCheckHit: NO; failed to parse request headers");
914 checkHitRequest
= nullptr;
915 checkedHit(NullStoreEntry::getInstance());
919 StoreEntry::getPublicByRequest(this, checkHitRequest
.getRaw());
923 htcpSpecifier::created(StoreEntry
*e
)
925 StoreEntry
*hit
= nullptr;
927 if (!e
|| e
->isNull()) {
928 debugs(31, 3, "htcpCheckHit: NO; public object not found");
929 } else if (!e
->validToSend()) {
930 debugs(31, 3, "htcpCheckHit: NO; entry not valid to send" );
931 } else if (refreshCheckHTCP(e
, checkHitRequest
.getRaw())) {
932 debugs(31, 3, "htcpCheckHit: NO; cached response is stale");
934 debugs(31, 3, "htcpCheckHit: YES!?");
942 htcpClrStoreEntry(StoreEntry
* e
)
944 debugs(31, 4, "htcpClrStoreEntry: Clearing store for entry: " << e
->url() );
949 htcpClrStore(const htcpSpecifier::Pointer
&s
)
951 HttpRequestPointer
request(s
->request
);
953 debugs(31, 3, "htcpClrStore: failed to parse URL");
957 /* Parse request headers */
958 if (!request
->header
.parse(s
->req_hdrs
, s
->reqHdrsSz
)) {
959 debugs(31, 2, "htcpClrStore: failed to parse request headers");
963 StoreEntry
*e
= nullptr;
965 /* Lookup matching entries. This matches both GET and HEAD */
966 while ((e
= storeGetPublicByRequest(request
.getRaw()))) {
967 htcpClrStoreEntry(e
);
972 debugs(31, 4, "htcpClrStore: Cleared " << released
<< " matching entries");
975 debugs(31, 4, "htcpClrStore: No matching entry found");
982 htcpHandleTst(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
984 debugs(31, 3, "htcpHandleTst: sz = " << sz
);
986 if (hdr
->RR
== RR_REQUEST
)
987 htcpHandleTstRequest(hdr
, buf
, sz
, from
);
989 htcpHandleTstResponse(hdr
, buf
, sz
, from
);
992 HtcpReplyData::HtcpReplyData() :
993 hit(0), hdr(hoHtcpReply
), msg_id(0), version(0.0)
995 memset(&cto
, 0, sizeof(cto
));
1000 htcpHandleTstResponse(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1002 HtcpReplyData htcpReply
;
1003 cache_key
*key
= NULL
;
1006 htcpDetail
*d
= NULL
;
1009 if (queried_id
[hdr
->msg_id
% N_QUERIED_KEYS
] != hdr
->msg_id
) {
1010 debugs(31, 2, "htcpHandleTstResponse: No matching query id '" <<
1011 hdr
->msg_id
<< "' (expected " <<
1012 queried_id
[hdr
->msg_id
% N_QUERIED_KEYS
] << ") from '" <<
1018 key
= queried_keys
[hdr
->msg_id
% N_QUERIED_KEYS
];
1021 debugs(31, 3, "htcpHandleTstResponse: No query key for response id '" << hdr
->msg_id
<< "' from '" << from
<< "'");
1025 peer
= &queried_addr
[hdr
->msg_id
% N_QUERIED_KEYS
];
1027 if ( *peer
!= from
|| peer
->port() != from
.port() ) {
1028 debugs(31, 3, "htcpHandleTstResponse: Unexpected response source " << from
);
1033 debugs(31, 2, "htcpHandleTstResponse: error condition, F1/MO == 1");
1037 htcpReply
.msg_id
= hdr
->msg_id
;
1038 debugs(31, 3, "htcpHandleTstResponse: msg_id = " << htcpReply
.msg_id
);
1039 htcpReply
.hit
= hdr
->response
? 0 : 1;
1042 debugs(31, 3, "htcpHandleTstResponse: MISS");
1044 debugs(31, 3, "htcpHandleTstResponse: HIT");
1045 d
= htcpUnpackDetail(buf
, sz
);
1048 debugs(31, 3, "htcpHandleTstResponse: bad DETAIL");
1052 if ((t
= d
->resp_hdrs
))
1053 htcpReply
.hdr
.parse(t
, d
->respHdrsSz
);
1055 if ((t
= d
->entity_hdrs
))
1056 htcpReply
.hdr
.parse(t
, d
->entityHdrsSz
);
1058 if ((t
= d
->cache_hdrs
))
1059 htcpReply
.hdr
.parse(t
, d
->cacheHdrsSz
);
1062 debugs(31, 3, "htcpHandleTstResponse: key (" << key
<< ") " << storeKeyText(key
));
1063 neighborsHtcpReply(key
, &htcpReply
, from
);
1064 htcpReply
.hdr
.clean();
1070 htcpHandleTstRequest(htcpDataHeader
* dhdr
, char *buf
, int sz
, Ip::Address
&from
)
1073 debugs(31, 3, "htcpHandleTst: nothing to do");
1080 /* buf should be a SPECIFIER */
1081 htcpSpecifier::Pointer
s(htcpUnpackSpecifier(buf
, sz
));
1084 debugs(31, 3, "htcpHandleTstRequest: htcpUnpackSpecifier failed");
1085 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1089 s
->setDataHeader(dhdr
);
1093 debugs(31, 3, "htcpHandleTstRequest: failed to parse request");
1094 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1098 if (!htcpAccessAllowed(Config
.accessList
.htcp
, s
, from
)) {
1099 debugs(31, 3, "htcpHandleTstRequest: Access denied");
1100 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_DENIED
, s
->uri
);
1104 debugs(31, 2, "HTCP TST request: " << s
->method
<< " " << s
->uri
<< " " << s
->version
);
1105 debugs(31, 2, "HTCP TST headers: " << s
->req_hdrs
);
1110 htcpSpecifier::checkedHit(StoreEntry
*e
)
1113 htcpTstReply(dhdr
, e
, this, from
); /* hit */
1114 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_HIT
, uri
);
1116 htcpTstReply(dhdr
, NULL
, NULL
, from
); /* cache miss */
1117 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_MISS
, uri
);
1122 htcpHandleClr(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1124 /* buf[0/1] is reserved and reason */
1125 int reason
= buf
[1] << 4;
1126 debugs(31, 2, "HTCP CLR reason: " << reason
);
1130 /* buf should be a SPECIFIER */
1133 debugs(31, 4, "htcpHandleClr: nothing to do");
1134 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1138 htcpSpecifier::Pointer
s(htcpUnpackSpecifier(buf
, sz
));
1141 debugs(31, 3, "htcpHandleClr: htcpUnpackSpecifier failed");
1142 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1147 debugs(31, 3, "htcpHandleTstRequest: failed to parse request");
1148 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1152 if (!htcpAccessAllowed(Config
.accessList
.htcp_clr
, s
, from
)) {
1153 debugs(31, 3, "htcpHandleClr: Access denied");
1154 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_DENIED
, s
->uri
);
1158 debugs(31, 2, "HTCP CLR request: " << s
->method
<< " " << s
->uri
<< " " << s
->version
);
1159 debugs(31, 2, "HTCP CLR headers: " << s
->req_hdrs
);
1161 /* Release objects from cache
1162 * analog to clientPurgeRequest in client_side.c
1165 switch (htcpClrStore(s
)) {
1168 htcpClrReply(hdr
, 1, from
); /* hit */
1169 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_HIT
, s
->uri
);
1173 htcpClrReply(hdr
, 0, from
); /* miss */
1174 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_MISS
, s
->uri
);
1183 * Forward a CLR request to all peers who have requested that CLRs be
1184 * forwarded to them.
1187 htcpForwardClr(char *buf
, int sz
)
1191 for (p
= Config
.peers
; p
; p
= p
->next
) {
1192 if (!p
->options
.htcp
) {
1195 if (!p
->options
.htcp_forward_clr
) {
1199 htcpSend(buf
, sz
, p
->in_addr
);
1204 * Do the first pass of handling an HTCP message. This used to be two
1205 * separate functions, htcpHandle and htcpHandleData. They were merged to
1206 * allow for forwarding HTCP packets easily to other peers if desired.
1208 * This function now works out what type of message we have received and then
1209 * hands it off to other functions to break apart message-specific data.
1212 htcpHandleMsg(char *buf
, int sz
, Ip::Address
&from
)
1219 if (sz
< 0 || (size_t)sz
< sizeof(htcpHeader
)) {
1220 // These are highly likely to be attack packets. Should probably get a bigger warning.
1221 debugs(31, 2, "htcpHandle: msg size less than htcpHeader size from " << from
);
1225 htcpHexdump("htcpHandle", buf
, sz
);
1226 memcpy(&htcpHdr
, buf
, sizeof(htcpHeader
));
1227 htcpHdr
.length
= ntohs(htcpHdr
.length
);
1229 if (htcpHdr
.minor
== 0)
1230 old_squid_format
= 1;
1232 old_squid_format
= 0;
1234 debugs(31, 3, "htcpHandle: htcpHdr.length = " << htcpHdr
.length
);
1235 debugs(31, 3, "htcpHandle: htcpHdr.major = " << htcpHdr
.major
);
1236 debugs(31, 3, "htcpHandle: htcpHdr.minor = " << htcpHdr
.minor
);
1238 if (sz
!= htcpHdr
.length
) {
1239 debugs(31, 3, "htcpHandle: sz/" << sz
<< " != htcpHdr.length/" <<
1240 htcpHdr
.length
<< " from " << from
);
1245 if (htcpHdr
.major
!= 0) {
1246 debugs(31, 3, "htcpHandle: Unknown major version " << htcpHdr
.major
<< " from " << from
);
1251 hbuf
= buf
+ sizeof(htcpHeader
);
1252 hsz
= sz
- sizeof(htcpHeader
);
1254 if ((size_t)hsz
< sizeof(htcpDataHeader
)) {
1255 debugs(31, 3, "htcpHandleData: msg size less than htcpDataHeader size");
1259 if (!old_squid_format
) {
1260 memcpy(&hdr
, hbuf
, sizeof(hdr
));
1262 htcpDataHeaderSquid hdrSquid
;
1263 memcpy(&hdrSquid
, hbuf
, sizeof(hdrSquid
));
1264 hdr
.length
= hdrSquid
.length
;
1265 hdr
.opcode
= hdrSquid
.opcode
;
1266 hdr
.response
= hdrSquid
.response
;
1267 hdr
.F1
= hdrSquid
.F1
;
1268 hdr
.RR
= hdrSquid
.RR
;
1270 hdr
.msg_id
= hdrSquid
.msg_id
;
1273 hdr
.length
= ntohs(hdr
.length
);
1274 hdr
.msg_id
= ntohl(hdr
.msg_id
);
1275 debugs(31, 3, "htcpHandleData: hsz = " << hsz
);
1276 debugs(31, 3, "htcpHandleData: length = " << hdr
.length
);
1278 if (hdr
.opcode
>= HTCP_END
) {
1279 debugs(31, 3, "htcpHandleData: client " << from
<< ", opcode " << hdr
.opcode
<< " out of range");
1283 debugs(31, 3, "htcpHandleData: opcode = " << hdr
.opcode
<< " " << htcpOpcodeStr
[hdr
.opcode
]);
1284 debugs(31, 3, "htcpHandleData: response = " << hdr
.response
);
1285 debugs(31, 3, "htcpHandleData: F1 = " << hdr
.F1
);
1286 debugs(31, 3, "htcpHandleData: RR = " << hdr
.RR
);
1287 debugs(31, 3, "htcpHandleData: msg_id = " << hdr
.msg_id
);
1289 if (hsz
< hdr
.length
) {
1290 debugs(31, 3, "htcpHandleData: sz < hdr.length");
1295 * set sz = hdr.length so we ignore any AUTH fields following
1298 hsz
= (int) hdr
.length
;
1299 hbuf
+= sizeof(htcpDataHeader
);
1300 hsz
-= sizeof(htcpDataHeader
);
1301 debugs(31, 3, "htcpHandleData: hsz = " << hsz
);
1303 htcpHexdump("htcpHandleData", hbuf
, hsz
);
1305 switch (hdr
.opcode
) {
1307 debugs(31, 3, "HTCP NOP not implemented");
1310 htcpHandleTst(&hdr
, hbuf
, hsz
, from
);
1313 debugs(31, 3, "HTCP MON not implemented");
1316 debugs(31, 3, "HTCP SET not implemented");
1319 htcpHandleClr(&hdr
, hbuf
, hsz
, from
);
1320 htcpForwardClr(buf
, sz
);
1328 htcpRecv(int fd
, void *)
1330 static char buf
[8192];
1332 static Ip::Address from
;
1334 /* Receive up to 8191 bytes, leaving room for a null */
1336 len
= comm_udp_recvfrom(fd
, buf
, sizeof(buf
) - 1, 0, from
);
1338 debugs(31, 3, "htcpRecv: FD " << fd
<< ", " << len
<< " bytes from " << from
);
1341 ++statCounter
.htcp
.pkts_recv
;
1343 htcpHandleMsg(buf
, len
, from
);
1345 Comm::SetSelect(fd
, COMM_SELECT_READ
, htcpRecv
, NULL
, 0);
1349 * ======================================================================
1351 * ======================================================================
1357 if (Config
.Port
.htcp
<= 0) {
1358 debugs(31, DBG_IMPORTANT
, "HTCP Disabled.");
1362 htcpIncomingConn
= new Comm::Connection
;
1363 htcpIncomingConn
->local
= Config
.Addrs
.udp_incoming
;
1364 htcpIncomingConn
->local
.port(Config
.Port
.htcp
);
1366 if (!Ip::EnableIpv6
&& !htcpIncomingConn
->local
.setIPv4()) {
1367 debugs(31, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << htcpIncomingConn
->local
<< " is not an IPv4 address.");
1368 fatal("HTCP port cannot be opened.");
1370 /* split-stack for now requires default IPv4-only HTCP */
1371 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& htcpIncomingConn
->local
.isAnyAddr()) {
1372 htcpIncomingConn
->local
.setIPv4();
1375 AsyncCall::Pointer call
= asyncCall(31, 2,
1376 "htcpIncomingConnectionOpened",
1377 Comm::UdpOpenDialer(&htcpIncomingConnectionOpened
));
1379 Ipc::StartListening(SOCK_DGRAM
,
1382 Ipc::fdnInHtcpSocket
, call
);
1384 if (!Config
.Addrs
.udp_outgoing
.isNoAddr()) {
1385 htcpOutgoingConn
= new Comm::Connection
;
1386 htcpOutgoingConn
->local
= Config
.Addrs
.udp_outgoing
;
1387 htcpOutgoingConn
->local
.port(Config
.Port
.htcp
);
1389 if (!Ip::EnableIpv6
&& !htcpOutgoingConn
->local
.setIPv4()) {
1390 debugs(31, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << htcpOutgoingConn
->local
<< " is not an IPv4 address.");
1391 fatal("HTCP port cannot be opened.");
1393 /* split-stack for now requires default IPv4-only HTCP */
1394 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& htcpOutgoingConn
->local
.isAnyAddr()) {
1395 htcpOutgoingConn
->local
.setIPv4();
1399 comm_open_listener(SOCK_DGRAM
, IPPROTO_UDP
, htcpOutgoingConn
, "Outgoing HTCP Socket");
1402 if (!Comm::IsConnOpen(htcpOutgoingConn
))
1403 fatal("Cannot open Outgoing HTCP Socket");
1405 Comm::SetSelect(htcpOutgoingConn
->fd
, COMM_SELECT_READ
, htcpRecv
, NULL
, 0);
1407 debugs(31, DBG_IMPORTANT
, "Sending HTCP messages from " << htcpOutgoingConn
->local
);
1413 htcpIncomingConnectionOpened(const Comm::ConnectionPointer
&conn
, int)
1415 if (!Comm::IsConnOpen(conn
))
1416 fatal("Cannot open HTCP Socket");
1418 Comm::SetSelect(conn
->fd
, COMM_SELECT_READ
, htcpRecv
, NULL
, 0);
1420 debugs(31, DBG_CRITICAL
, "Accepting HTCP messages on " << conn
->local
);
1422 if (Config
.Addrs
.udp_outgoing
.isNoAddr()) {
1423 htcpOutgoingConn
= conn
;
1424 debugs(31, DBG_IMPORTANT
, "Sending HTCP messages from " << htcpOutgoingConn
->local
);
1429 htcpQuery(StoreEntry
* e
, HttpRequest
* req
, CachePeer
* p
)
1431 cache_key
*save_key
;
1432 static char pkt
[8192];
1435 HttpHeader
hdr(hoRequest
);
1436 Http::StateFlags flags
;
1438 if (!Comm::IsConnOpen(htcpIncomingConn
))
1441 old_squid_format
= p
->options
.htcp_oldsquid
;
1442 memset(&flags
, '\0', sizeof(flags
));
1443 snprintf(vbuf
, sizeof(vbuf
), "%d/%d",
1444 req
->http_ver
.major
, req
->http_ver
.minor
);
1446 htcpStuff
stuff(++msg_id_counter
, HTCP_TST
, RR_REQUEST
, 1);
1447 SBuf sb
= req
->method
.image();
1448 stuff
.S
.method
= sb
.c_str();
1449 stuff
.S
.uri
= (char *) e
->url();
1450 stuff
.S
.version
= vbuf
;
1451 HttpStateData::httpBuildRequestHeader(req
, e
, NULL
, &hdr
, flags
);
1456 stuff
.S
.req_hdrs
= mb
.buf
;
1457 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
1460 debugs(31, 3, "htcpQuery: htcpBuildPacket() failed");
1464 htcpSend(pkt
, (int) pktlen
, p
->in_addr
);
1466 queried_id
[stuff
.msg_id
% N_QUERIED_KEYS
] = stuff
.msg_id
;
1467 save_key
= queried_keys
[stuff
.msg_id
% N_QUERIED_KEYS
];
1468 storeKeyCopy(save_key
, (const cache_key
*)e
->key
);
1469 queried_addr
[stuff
.msg_id
% N_QUERIED_KEYS
] = p
->in_addr
;
1470 debugs(31, 3, "htcpQuery: key (" << save_key
<< ") " << storeKeyText(save_key
));
1476 * Send an HTCP CLR message for a specified item to a given CachePeer.
1479 htcpClear(StoreEntry
* e
, const char *uri
, HttpRequest
* req
, const HttpRequestMethod
&, CachePeer
* p
, htcp_clr_reason reason
)
1481 static char pkt
[8192];
1484 HttpHeader
hdr(hoRequest
);
1486 Http::StateFlags flags
;
1488 if (!Comm::IsConnOpen(htcpIncomingConn
))
1491 old_squid_format
= p
->options
.htcp_oldsquid
;
1492 memset(&flags
, '\0', sizeof(flags
));
1493 snprintf(vbuf
, sizeof(vbuf
), "%d/%d",
1494 req
->http_ver
.major
, req
->http_ver
.minor
);
1496 htcpStuff
stuff(++msg_id_counter
, HTCP_CLR
, RR_REQUEST
, 0);
1497 if (reason
== HTCP_CLR_INVALIDATION
)
1500 SBuf sb
= req
->method
.image();
1501 stuff
.S
.method
= sb
.c_str();
1502 if (e
== NULL
|| e
->mem_obj
== NULL
) {
1506 stuff
.S
.uri
= xstrdup(uri
);
1508 stuff
.S
.uri
= (char *) e
->url();
1510 stuff
.S
.version
= vbuf
;
1511 if (reason
!= HTCP_CLR_INVALIDATION
) {
1512 HttpStateData::httpBuildRequestHeader(req
, e
, NULL
, &hdr
, flags
);
1516 stuff
.S
.req_hdrs
= mb
.buf
;
1518 stuff
.S
.req_hdrs
= NULL
;
1520 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
1521 if (reason
!= HTCP_CLR_INVALIDATION
) {
1528 debugs(31, 3, "htcpClear: htcpBuildPacket() failed");
1532 htcpSend(pkt
, (int) pktlen
, p
->in_addr
);
1536 * htcpSocketShutdown only closes the 'in' socket if it is
1537 * different than the 'out' socket.
1540 htcpSocketShutdown(void)
1542 if (!Comm::IsConnOpen(htcpIncomingConn
))
1545 debugs(12, DBG_IMPORTANT
, "Stop accepting HTCP on " << htcpIncomingConn
->local
);
1547 * Here we just unlink htcpIncomingConn because the HTCP 'in'
1548 * and 'out' sockets might be just one FD. This prevents this
1549 * function from executing repeatedly. When we are really ready to
1550 * exit or restart, main will comm_close the 'out' descriptor.
1552 htcpIncomingConn
= NULL
;
1555 * Normally we only write to the outgoing HTCP socket, but
1556 * we also have a read handler there to catch messages sent
1557 * to that specific interface. During shutdown, we must
1558 * disable reading on the outgoing socket.
1560 /* XXX Don't we need this handler to read replies while shutting down?
1561 * I think there should be a separate hander for reading replies..
1563 assert(Comm::IsConnOpen(htcpOutgoingConn
));
1565 Comm::SetSelect(htcpOutgoingConn
->fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
1569 htcpClosePorts(void)
1571 htcpSocketShutdown();
1573 if (htcpOutgoingConn
!= NULL
) {
1574 debugs(12, DBG_IMPORTANT
, "Stop sending HTCP from " << htcpOutgoingConn
->local
);
1575 htcpOutgoingConn
= NULL
;
1580 htcpLogHtcp(Ip::Address
&caddr
, int opcode
, LogTags logcode
, const char *url
)
1582 AccessLogEntry::Pointer al
= new AccessLogEntry
;
1583 if (LOG_TAG_NONE
== logcode
.oldType
)
1585 if (!Config
.onoff
.log_udp
)
1587 al
->htcp
.opcode
= htcpOpcodeStr
[opcode
];
1589 al
->cache
.caddr
= caddr
;
1590 al
->cache
.code
= logcode
;
1591 al
->cache
.trTime
.tv_sec
= 0;
1592 al
->cache
.trTime
.tv_usec
= 0;
1593 accessLogLog(al
, NULL
);