2 * Copyright (C) 1996-2015 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 "HttpStateFlags.h"
26 #include "icmp/net_db.h"
31 #include "SquidConfig.h"
32 #include "SquidTime.h"
33 #include "StatCounters.h"
35 #include "store_key_md5.h"
36 #include "StoreClient.h"
40 /** htcpDetail uses explicit alloc()/freeOne()
41 * XXX: convert to MEMPROXY_CLASS() API
45 typedef struct _Countstr Countstr
;
47 typedef struct _htcpHeader htcpHeader
;
49 typedef struct _htcpDataHeader htcpDataHeader
;
51 typedef struct _htcpDataHeaderSquid htcpDataHeaderSquid
;
53 typedef struct _htcpAuthHeader htcpAuthHeader
;
55 typedef struct _htcpDetail htcpDetail
;
68 struct _htcpDataHeaderSquid
{
72 unsigned int opcode
:4;
73 unsigned int response
:4;
75 unsigned int response
:4;
76 unsigned int opcode
:4;
80 unsigned int reserved
:6;
86 unsigned int reserved
:6;
92 struct _htcpDataHeader
{
126 /* RR == 0 --> F1 = RESPONSE DESIRED FLAG */
127 /* RR == 1 --> F1 = MESSAGE OVERALL FLAG */
128 /* RR == 0 --> REQUEST */
129 /* RR == 1 --> RESPONSE */
131 struct _htcpAuthHeader
{
139 class htcpSpecifier
: public StoreClient
141 MEMPROXY_CLASS(htcpSpecifier
);
151 checkHitRequest(NULL
),
156 void created (StoreEntry
*newEntry
);
158 void checkedHit(StoreEntry
*e
);
160 void setFrom(Ip::Address
&from
);
161 void setDataHeader(htcpDataHeader
*);
166 size_t reqHdrsSz
; ///< size of the req_hdrs content
167 HttpRequest
*request
;
170 HttpRequest
*checkHitRequest
;
172 Ip::Address from
; // was a ptr. return to such IFF needed. otherwise copy should do.
173 htcpDataHeader
*dhdr
;
190 htcpStuff(uint32_t id
, int o
, int r
, int f
) :
198 memset(&D
, 0, sizeof(D
));
220 static const char *const htcpOpcodeStr
[] = {
230 * values for htcpDataHeader->response
235 OPCODE_UNIMPLEMENTED
,
236 MAJOR_VERSION_UNSUPPORTED
,
237 MINOR_VERSION_UNSUPPORTED
,
242 * values for htcpDataHeader->RR
249 static void htcpIncomingConnectionOpened(const Comm::ConnectionPointer
&conn
, int errNo
);
250 static uint32_t msg_id_counter
= 0;
252 static Comm::ConnectionPointer htcpOutgoingConn
= NULL
;
253 static Comm::ConnectionPointer htcpIncomingConn
= NULL
;
254 #define N_QUERIED_KEYS 8192
255 static uint32_t queried_id
[N_QUERIED_KEYS
];
256 static cache_key queried_keys
[N_QUERIED_KEYS
][SQUID_MD5_DIGEST_LENGTH
];
258 static Ip::Address queried_addr
[N_QUERIED_KEYS
];
259 static MemAllocator
*htcpDetailPool
= NULL
;
261 static int old_squid_format
= 0;
263 static ssize_t
htcpBuildPacket(char *buf
, size_t buflen
, htcpStuff
* stuff
);
264 static htcpSpecifier
*htcpUnpackSpecifier(char *buf
, int sz
);
265 static htcpDetail
*htcpUnpackDetail(char *buf
, int sz
);
266 static ssize_t
htcpBuildAuth(char *buf
, size_t buflen
);
267 static ssize_t
htcpBuildCountstr(char *buf
, size_t buflen
, const char *s
, size_t len
);
268 static ssize_t
htcpBuildData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
269 static ssize_t
htcpBuildDetail(char *buf
, size_t buflen
, htcpStuff
* stuff
);
270 static ssize_t
htcpBuildOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
271 static ssize_t
htcpBuildSpecifier(char *buf
, size_t buflen
, htcpStuff
* stuff
);
272 static ssize_t
htcpBuildTstOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
273 static void htcpFreeSpecifier(htcpSpecifier
* s
);
274 static void htcpFreeDetail(htcpDetail
* s
);
276 static void htcpHandleMsg(char *buf
, int sz
, Ip::Address
&from
);
278 static void htcpLogHtcp(Ip::Address
&, int, LogTags
, const char *);
279 static void htcpHandleTst(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
281 static void htcpRecv(int fd
, void *data
);
283 static void htcpSend(const char *buf
, int len
, Ip::Address
&to
);
285 static void htcpTstReply(htcpDataHeader
*, StoreEntry
*, htcpSpecifier
*, Ip::Address
&);
287 static void htcpHandleTstRequest(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
289 static void htcpHandleTstResponse(htcpDataHeader
*, char *, int, Ip::Address
&);
292 htcpHexdump(const char *tag
, const char *s
, int sz
)
296 debugs(31, 3, "htcpHexdump " << tag
);
297 memset(hex
, '\0', sizeof(hex
));
299 for (int i
= 0; i
< sz
; ++i
) {
301 snprintf(&hex
[k
* 3], 4, " %02x", (int) *(s
+ i
));
303 if (k
< 15 && i
< (sz
- 1))
306 debugs(31, 3, "\t" << hex
);
308 memset(hex
, '\0', sizeof(hex
));
314 * STUFF FOR SENDING HTCP MESSAGES
318 htcpBuildAuth(char *buf
, size_t buflen
)
322 assert(2 == sizeof(uint16_t));
323 auth
.length
= htons(2);
325 if (buflen
< copy_sz
)
327 memcpy(buf
, &auth
, copy_sz
);
332 htcpBuildCountstr(char *buf
, size_t buflen
, const char *s
, size_t len
)
336 if (buflen
- off
< 2)
339 debugs(31, 3, "htcpBuildCountstr: LENGTH = " << len
);
341 debugs(31, 3, "htcpBuildCountstr: TEXT = {" << (s
? s
: "<NULL>") << "}");
343 uint16_t length
= htons((uint16_t) len
);
345 memcpy(buf
+ off
, &length
, 2);
349 if (buflen
- off
< len
)
353 memcpy(buf
+ off
, s
, len
);
361 htcpBuildSpecifier(char *buf
, size_t buflen
, htcpStuff
* stuff
)
365 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.method
, (stuff
->S
.method
?strlen(stuff
->S
.method
):0));
372 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.uri
, (stuff
->S
.uri
?strlen(stuff
->S
.uri
):0));
379 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.version
, (stuff
->S
.version
?strlen(stuff
->S
.version
):0));
386 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.req_hdrs
, stuff
->S
.reqHdrsSz
);
393 debugs(31, 3, "htcpBuildSpecifier: size " << off
);
399 htcpBuildDetail(char *buf
, size_t buflen
, htcpStuff
* stuff
)
403 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.resp_hdrs
, stuff
->D
.respHdrsSz
);
410 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.entity_hdrs
, stuff
->D
.entityHdrsSz
);
417 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.cache_hdrs
, stuff
->D
.cacheHdrsSz
);
428 htcpBuildTstOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
433 debugs(31, 3, "htcpBuildTstOpData: RR_REQUEST");
434 return htcpBuildSpecifier(buf
, buflen
, stuff
);
437 debugs(31, 3, "htcpBuildTstOpData: RR_RESPONSE");
438 debugs(31, 3, "htcpBuildTstOpData: F1 = " << stuff
->f1
);
440 if (stuff
->f1
) /* cache miss */
443 return htcpBuildDetail(buf
, buflen
, stuff
);
446 fatal_dump("htcpBuildTstOpData: bad RR value");
453 htcpBuildClrOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
455 unsigned short reason
;
459 debugs(31, 3, "htcpBuildClrOpData: RR_REQUEST");
460 reason
= htons((unsigned short)stuff
->reason
);
461 memcpy(buf
, &reason
, 2);
462 return htcpBuildSpecifier(buf
+ 2, buflen
- 2, stuff
) + 2;
466 fatal_dump("htcpBuildClrOpData: bad RR value");
473 htcpBuildOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
476 debugs(31, 3, "htcpBuildOpData: opcode " << htcpOpcodeStr
[stuff
->op
]);
481 off
= htcpBuildTstOpData(buf
+ off
, buflen
, stuff
);
485 off
= htcpBuildClrOpData(buf
+ off
, buflen
, stuff
);
497 htcpBuildData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
501 size_t hdr_sz
= sizeof(htcpDataHeader
);
507 off
+= hdr_sz
; /* skip! */
509 op_data_sz
= htcpBuildOpData(buf
+ off
, buflen
- off
, stuff
);
516 debugs(31, 3, "htcpBuildData: hdr.length = " << off
);
518 hdr
.length
= (uint16_t) off
;
520 hdr
.opcode
= stuff
->op
;
522 hdr
.response
= stuff
->response
;
528 hdr
.msg_id
= stuff
->msg_id
;
530 /* convert multi-byte fields */
531 hdr
.length
= htons(hdr
.length
);
533 hdr
.msg_id
= htonl(hdr
.msg_id
);
535 if (!old_squid_format
) {
536 memcpy(buf
, &hdr
, hdr_sz
);
538 htcpDataHeaderSquid hdrSquid
;
539 memset(&hdrSquid
, 0, sizeof(hdrSquid
));
540 hdrSquid
.length
= hdr
.length
;
541 hdrSquid
.opcode
= hdr
.opcode
;
542 hdrSquid
.response
= hdr
.response
;
543 hdrSquid
.F1
= hdr
.F1
;
544 hdrSquid
.RR
= hdr
.RR
;
545 memcpy(buf
, &hdrSquid
, hdr_sz
);
548 debugs(31, 3, "htcpBuildData: size " << off
);
554 * Build an HTCP packet into buf, maximum length buflen.
555 * Returns the packet length, or zero on failure.
558 htcpBuildPacket(char *buf
, size_t buflen
, htcpStuff
* stuff
)
562 size_t hdr_sz
= sizeof(htcpHeader
);
564 /* skip the header -- we don't know the overall length */
566 if (buflen
< hdr_sz
) {
571 s
= htcpBuildData(buf
+ off
, buflen
- off
, stuff
);
578 s
= htcpBuildAuth(buf
+ off
, buflen
- off
);
585 hdr
.length
= htons((uint16_t) off
);
588 if (old_squid_format
)
593 memcpy(buf
, &hdr
, hdr_sz
);
595 debugs(31, 3, "htcpBuildPacket: size " << off
);
601 htcpSend(const char *buf
, int len
, Ip::Address
&to
)
603 debugs(31, 3, HERE
<< to
);
604 htcpHexdump("htcpSend", buf
, len
);
606 if (comm_udp_sendto(htcpOutgoingConn
->fd
, to
, buf
, len
) < 0)
607 debugs(31, 3, HERE
<< htcpOutgoingConn
<< " sendto: " << xstrerror());
609 ++statCounter
.htcp
.pkts_sent
;
613 * STUFF FOR RECEIVING HTCP MESSAGES
617 htcpSpecifier::setFrom(Ip::Address
&aSocket
)
623 htcpSpecifier::setDataHeader(htcpDataHeader
*aDataHeader
)
629 htcpFreeSpecifier(htcpSpecifier
* s
)
631 HTTPMSGUNLOCK(s
->request
);
637 htcpFreeDetail(htcpDetail
* d
)
639 htcpDetailPool
->freeOne(d
);
643 * Unpack an HTCP SPECIFIER in place
644 * This will overwrite any following AUTH block
646 // XXX: this needs to be turned into an Htcp1::Parser inheriting from Http1::RequestParser
647 // but with different first-line and block unpacking logic.
648 static htcpSpecifier
*
649 htcpUnpackSpecifier(char *buf
, int sz
)
651 htcpSpecifier
*s
= new htcpSpecifier
;
652 HttpRequestMethod method
;
654 /* Find length of METHOD */
655 uint16_t l
= ntohs(*(uint16_t *) buf
);
660 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack METHOD");
661 htcpFreeSpecifier(s
);
669 debugs(31, 6, "htcpUnpackSpecifier: METHOD (" << l
<< "/" << sz
<< ") '" << s
->method
<< "'");
671 /* Find length of URI */
672 l
= ntohs(*(uint16_t *) buf
);
676 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack URI");
677 htcpFreeSpecifier(s
);
681 /* Add terminating null to METHOD */
689 debugs(31, 6, "htcpUnpackSpecifier: URI (" << l
<< "/" << sz
<< ") '" << s
->uri
<< "'");
691 /* Find length of VERSION */
692 l
= ntohs(*(uint16_t *) buf
);
696 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack VERSION");
697 htcpFreeSpecifier(s
);
701 /* Add terminating null to URI */
709 debugs(31, 6, "htcpUnpackSpecifier: VERSION (" << l
<< "/" << sz
<< ") '" << s
->version
<< "'");
711 /* Find length of REQ-HDRS */
712 l
= ntohs(*(uint16_t *) buf
);
716 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack REQ-HDRS");
717 htcpFreeSpecifier(s
);
721 /* Add terminating null to URI */
730 debugs(31, 6, "htcpUnpackSpecifier: REQ-HDRS (" << l
<< "/" << sz
<< ") '" << s
->req_hdrs
<< "'");
732 debugs(31, 3, "htcpUnpackSpecifier: " << sz
<< " bytes left");
735 * Add terminating null to REQ-HDRS. This is possible because we allocated
736 * an extra byte when we received the packet. This will overwrite any following
742 method
.HttpRequestMethodXXX(s
->method
);
744 s
->request
= HttpRequest::CreateFromUrlAndMethod(s
->uri
, method
== Http::METHOD_NONE
? HttpRequestMethod(Http::METHOD_GET
) : method
);
747 HTTPMSGLOCK(s
->request
);
753 * Unpack an HTCP DETAIL in place
754 * This will overwrite any following AUTH block
757 htcpUnpackDetail(char *buf
, int sz
)
759 htcpDetail
*d
= static_cast<htcpDetail
*>(htcpDetailPool
->alloc());
761 /* Find length of RESP-HDRS */
762 uint16_t l
= ntohs(*(uint16_t *) buf
);
767 debugs(31, 3, "htcpUnpackDetail: failed to unpack RESP_HDRS");
778 /* Find length of ENTITY-HDRS */
779 l
= ntohs(*(uint16_t *) buf
);
784 debugs(31, 3, "htcpUnpackDetail: failed to unpack ENTITY_HDRS");
789 /* Add terminating null to RESP-HDRS */
792 /* Set ENTITY-HDRS */
795 d
->entity_hdrs
= buf
;
800 /* Find length of CACHE-HDRS */
801 l
= ntohs(*(uint16_t *) buf
);
806 debugs(31, 3, "htcpUnpackDetail: failed to unpack CACHE_HDRS");
811 /* Add terminating null to ENTITY-HDRS */
822 debugs(31, 3, "htcpUnpackDetail: " << sz
<< " bytes left");
825 * Add terminating null to CACHE-HDRS. This is possible because we allocated
826 * an extra byte when we received the packet. This will overwrite any following
835 htcpAccessAllowed(acl_access
* acl
, htcpSpecifier
* s
, Ip::Address
&from
)
837 /* default deny if no access list present */
841 ACLFilledChecklist
checklist(acl
, s
->request
, NULL
);
842 checklist
.src_addr
= from
;
843 checklist
.my_addr
.setNoAddr();
844 return (checklist
.fastCheck() == ACCESS_ALLOWED
);
848 htcpTstReply(htcpDataHeader
* dhdr
, StoreEntry
* e
, htcpSpecifier
* spec
, Ip::Address
&from
)
850 static char pkt
[8192];
851 HttpHeader
hdr(hoHtcpReply
);
856 htcpStuff
stuff(dhdr
->msg_id
, HTCP_TST
, RR_RESPONSE
, 0);
857 stuff
.response
= e
? 0 : 1;
858 debugs(31, 3, "htcpTstReply: response = " << stuff
.response
);
862 packerToMemInit(&p
, &mb
);
863 stuff
.S
.method
= spec
->method
;
864 stuff
.S
.uri
= spec
->uri
;
865 stuff
.S
.version
= spec
->version
;
866 stuff
.S
.req_hdrs
= spec
->req_hdrs
;
867 stuff
.S
.reqHdrsSz
= spec
->reqHdrsSz
;
869 hdr
.putInt(HDR_AGE
, (e
->timestamp
<= squid_curtime
? (squid_curtime
- e
->timestamp
) : 0) );
871 hdr
.putInt(HDR_AGE
, 0);
873 stuff
.D
.resp_hdrs
= xstrdup(mb
.buf
);
874 stuff
.D
.respHdrsSz
= mb
.contentSize();
875 debugs(31, 3, "htcpTstReply: resp_hdrs = {" << stuff
.D
.resp_hdrs
<< "}");
879 if (e
&& e
->expires
> -1)
880 hdr
.putTime(HDR_EXPIRES
, e
->expires
);
882 if (e
&& e
->lastmod
> -1)
883 hdr
.putTime(HDR_LAST_MODIFIED
, e
->lastmod
);
887 stuff
.D
.entity_hdrs
= xstrdup(mb
.buf
);
888 stuff
.D
.entityHdrsSz
= mb
.contentSize();
890 debugs(31, 3, "htcpTstReply: entity_hdrs = {" << stuff
.D
.entity_hdrs
<< "}");
897 if (char *host
= urlHostname(spec
->uri
)) {
901 netdbHostData(host
, &samp
, &rtt
, &hops
);
905 snprintf(cto_buf
, 128, "%s %d %f %d",
906 host
, samp
, 0.001 * rtt
, hops
);
907 hdr
.putExt("Cache-to-Origin", cto_buf
);
910 #endif /* USE_ICMP */
913 stuff
.D
.cache_hdrs
= xstrdup(mb
.buf
);
914 stuff
.D
.cacheHdrsSz
= mb
.contentSize();
915 debugs(31, 3, "htcpTstReply: cache_hdrs = {" << stuff
.D
.cache_hdrs
<< "}");
921 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
923 safe_free(stuff
.D
.resp_hdrs
);
924 stuff
.D
.respHdrsSz
= 0;
925 safe_free(stuff
.D
.entity_hdrs
);
926 stuff
.D
.entityHdrsSz
= 0;
927 safe_free(stuff
.D
.cache_hdrs
);
928 stuff
.D
.cacheHdrsSz
= 0;
931 debugs(31, 3, "htcpTstReply: htcpBuildPacket() failed");
935 htcpSend(pkt
, (int) pktlen
, from
);
940 htcpClrReply(htcpDataHeader
* dhdr
, int purgeSucceeded
, Ip::Address
&from
)
942 static char pkt
[8192];
945 /* If dhdr->F1 == 0, no response desired */
950 htcpStuff
stuff(dhdr
->msg_id
, HTCP_CLR
, RR_RESPONSE
, 0);
952 stuff
.response
= purgeSucceeded
? 0 : 2;
954 debugs(31, 3, "htcpClrReply: response = " << stuff
.response
);
956 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
959 debugs(31, 3, "htcpClrReply: htcpBuildPacket() failed");
963 htcpSend(pkt
, (int) pktlen
, from
);
967 htcpSpecifier::checkHit()
969 checkHitRequest
= request
;
971 if (NULL
== checkHitRequest
) {
972 debugs(31, 3, "htcpCheckHit: NO; failed to parse URL");
973 checkedHit(NullStoreEntry::getInstance());
977 if (!checkHitRequest
->header
.parse(req_hdrs
, reqHdrsSz
)) {
978 debugs(31, 3, "htcpCheckHit: NO; failed to parse request headers");
979 delete checkHitRequest
;
980 checkHitRequest
= NULL
;
981 checkedHit(NullStoreEntry::getInstance());
985 StoreEntry::getPublicByRequest(this, checkHitRequest
);
989 htcpSpecifier::created (StoreEntry
*e
)
991 StoreEntry
*hit
=NULL
;
995 debugs(31, 3, "htcpCheckHit: NO; public object not found");
996 } else if (!e
->validToSend()) {
997 debugs(31, 3, "htcpCheckHit: NO; entry not valid to send" );
998 } else if (refreshCheckHTCP(e
, checkHitRequest
)) {
999 debugs(31, 3, "htcpCheckHit: NO; cached response is stale");
1001 debugs(31, 3, "htcpCheckHit: YES!?");
1009 htcpClrStoreEntry(StoreEntry
* e
)
1011 debugs(31, 4, "htcpClrStoreEntry: Clearing store for entry: " << e
->url() );
1012 e
->releaseRequest();
1016 htcpClrStore(const htcpSpecifier
* s
)
1018 HttpRequest
*request
= s
->request
;
1019 StoreEntry
*e
= NULL
;
1022 if (request
== NULL
) {
1023 debugs(31, 3, "htcpClrStore: failed to parse URL");
1027 /* Parse request headers */
1028 if (!request
->header
.parse(s
->req_hdrs
, s
->reqHdrsSz
)) {
1029 debugs(31, 2, "htcpClrStore: failed to parse request headers");
1033 /* Lookup matching entries. This matches both GET and HEAD */
1034 while ((e
= storeGetPublicByRequest(request
)) != NULL
) {
1036 htcpClrStoreEntry(e
);
1042 debugs(31, 4, "htcpClrStore: Cleared " << released
<< " matching entries");
1045 debugs(31, 4, "htcpClrStore: No matching entry found");
1052 htcpHandleTst(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1054 debugs(31, 3, "htcpHandleTst: sz = " << sz
);
1056 if (hdr
->RR
== RR_REQUEST
)
1057 htcpHandleTstRequest(hdr
, buf
, sz
, from
);
1059 htcpHandleTstResponse(hdr
, buf
, sz
, from
);
1062 HtcpReplyData::HtcpReplyData() :
1063 hit(0), hdr(hoHtcpReply
), msg_id(0), version(0.0)
1065 memset(&cto
, 0, sizeof(cto
));
1070 htcpHandleTstResponse(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1072 HtcpReplyData htcpReply
;
1073 cache_key
*key
= NULL
;
1076 htcpDetail
*d
= NULL
;
1079 if (queried_id
[hdr
->msg_id
% N_QUERIED_KEYS
] != hdr
->msg_id
) {
1080 debugs(31, 2, "htcpHandleTstResponse: No matching query id '" <<
1081 hdr
->msg_id
<< "' (expected " <<
1082 queried_id
[hdr
->msg_id
% N_QUERIED_KEYS
] << ") from '" <<
1088 key
= queried_keys
[hdr
->msg_id
% N_QUERIED_KEYS
];
1091 debugs(31, 3, "htcpHandleTstResponse: No query key for response id '" << hdr
->msg_id
<< "' from '" << from
<< "'");
1095 peer
= &queried_addr
[hdr
->msg_id
% N_QUERIED_KEYS
];
1097 if ( *peer
!= from
|| peer
->port() != from
.port() ) {
1098 debugs(31, 3, "htcpHandleTstResponse: Unexpected response source " << from
);
1103 debugs(31, 2, "htcpHandleTstResponse: error condition, F1/MO == 1");
1107 htcpReply
.msg_id
= hdr
->msg_id
;
1108 debugs(31, 3, "htcpHandleTstResponse: msg_id = " << htcpReply
.msg_id
);
1109 htcpReply
.hit
= hdr
->response
? 0 : 1;
1112 debugs(31, 3, "htcpHandleTstResponse: MISS");
1114 debugs(31, 3, "htcpHandleTstResponse: HIT");
1115 d
= htcpUnpackDetail(buf
, sz
);
1118 debugs(31, 3, "htcpHandleTstResponse: bad DETAIL");
1122 if ((t
= d
->resp_hdrs
))
1123 htcpReply
.hdr
.parse(t
, d
->respHdrsSz
);
1125 if ((t
= d
->entity_hdrs
))
1126 htcpReply
.hdr
.parse(t
, d
->entityHdrsSz
);
1128 if ((t
= d
->cache_hdrs
))
1129 htcpReply
.hdr
.parse(t
, d
->cacheHdrsSz
);
1132 debugs(31, 3, "htcpHandleTstResponse: key (" << key
<< ") " << storeKeyText(key
));
1133 neighborsHtcpReply(key
, &htcpReply
, from
);
1134 htcpReply
.hdr
.clean();
1141 htcpHandleTstRequest(htcpDataHeader
* dhdr
, char *buf
, int sz
, Ip::Address
&from
)
1143 /* buf should be a SPECIFIER */
1147 debugs(31, 3, "htcpHandleTst: nothing to do");
1154 /* s is a new object */
1155 s
= htcpUnpackSpecifier(buf
, sz
);
1158 debugs(31, 3, "htcpHandleTstRequest: htcpUnpackSpecifier failed");
1159 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1163 s
->setDataHeader(dhdr
);
1167 debugs(31, 3, "htcpHandleTstRequest: failed to parse request");
1168 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1169 htcpFreeSpecifier(s
);
1173 if (!htcpAccessAllowed(Config
.accessList
.htcp
, s
, from
)) {
1174 debugs(31, 3, "htcpHandleTstRequest: Access denied");
1175 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_DENIED
, s
->uri
);
1176 htcpFreeSpecifier(s
);
1180 debugs(31, 2, "HTCP TST request: " << s
->method
<< " " << s
->uri
<< " " << s
->version
);
1181 debugs(31, 2, "HTCP TST headers: " << s
->req_hdrs
);
1186 htcpSpecifier::checkedHit(StoreEntry
*e
)
1189 htcpTstReply(dhdr
, e
, this, from
); /* hit */
1190 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_HIT
, uri
);
1192 htcpTstReply(dhdr
, NULL
, NULL
, from
); /* cache miss */
1193 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_MISS
, uri
);
1196 htcpFreeSpecifier(this);
1200 htcpHandleClr(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1203 /* buf[0/1] is reserved and reason */
1204 int reason
= buf
[1] << 4;
1205 debugs(31, 2, "HTCP CLR reason: " << reason
);
1209 /* buf should be a SPECIFIER */
1212 debugs(31, 4, "htcpHandleClr: nothing to do");
1213 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1217 s
= htcpUnpackSpecifier(buf
, sz
);
1220 debugs(31, 3, "htcpHandleClr: htcpUnpackSpecifier failed");
1221 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1226 debugs(31, 3, "htcpHandleTstRequest: failed to parse request");
1227 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1228 htcpFreeSpecifier(s
);
1232 if (!htcpAccessAllowed(Config
.accessList
.htcp_clr
, s
, from
)) {
1233 debugs(31, 3, "htcpHandleClr: Access denied");
1234 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_DENIED
, s
->uri
);
1235 htcpFreeSpecifier(s
);
1239 debugs(31, 2, "HTCP CLR request: " << s
->method
<< " " << s
->uri
<< " " << s
->version
);
1240 debugs(31, 2, "HTCP CLR headers: " << s
->req_hdrs
);
1242 /* Release objects from cache
1243 * analog to clientPurgeRequest in client_side.c
1246 switch (htcpClrStore(s
)) {
1249 htcpClrReply(hdr
, 1, from
); /* hit */
1250 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_HIT
, s
->uri
);
1254 htcpClrReply(hdr
, 0, from
); /* miss */
1255 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_MISS
, s
->uri
);
1262 htcpFreeSpecifier(s
);
1266 * Forward a CLR request to all peers who have requested that CLRs be
1267 * forwarded to them.
1270 htcpForwardClr(char *buf
, int sz
)
1274 for (p
= Config
.peers
; p
; p
= p
->next
) {
1275 if (!p
->options
.htcp
) {
1278 if (!p
->options
.htcp_forward_clr
) {
1282 htcpSend(buf
, sz
, p
->in_addr
);
1287 * Do the first pass of handling an HTCP message. This used to be two
1288 * separate functions, htcpHandle and htcpHandleData. They were merged to
1289 * allow for forwarding HTCP packets easily to other peers if desired.
1291 * This function now works out what type of message we have received and then
1292 * hands it off to other functions to break apart message-specific data.
1295 htcpHandleMsg(char *buf
, int sz
, Ip::Address
&from
)
1302 if (sz
< 0 || (size_t)sz
< sizeof(htcpHeader
)) {
1303 // These are highly likely to be attack packets. Should probably get a bigger warning.
1304 debugs(31, 2, "htcpHandle: msg size less than htcpHeader size from " << from
);
1308 htcpHexdump("htcpHandle", buf
, sz
);
1309 memcpy(&htcpHdr
, buf
, sizeof(htcpHeader
));
1310 htcpHdr
.length
= ntohs(htcpHdr
.length
);
1312 if (htcpHdr
.minor
== 0)
1313 old_squid_format
= 1;
1315 old_squid_format
= 0;
1317 debugs(31, 3, "htcpHandle: htcpHdr.length = " << htcpHdr
.length
);
1318 debugs(31, 3, "htcpHandle: htcpHdr.major = " << htcpHdr
.major
);
1319 debugs(31, 3, "htcpHandle: htcpHdr.minor = " << htcpHdr
.minor
);
1321 if (sz
!= htcpHdr
.length
) {
1322 debugs(31, 3, "htcpHandle: sz/" << sz
<< " != htcpHdr.length/" <<
1323 htcpHdr
.length
<< " from " << from
);
1328 if (htcpHdr
.major
!= 0) {
1329 debugs(31, 3, "htcpHandle: Unknown major version " << htcpHdr
.major
<< " from " << from
);
1334 hbuf
= buf
+ sizeof(htcpHeader
);
1335 hsz
= sz
- sizeof(htcpHeader
);
1337 if ((size_t)hsz
< sizeof(htcpDataHeader
)) {
1338 debugs(31, 3, "htcpHandleData: msg size less than htcpDataHeader size");
1342 if (!old_squid_format
) {
1343 memcpy(&hdr
, hbuf
, sizeof(hdr
));
1345 htcpDataHeaderSquid hdrSquid
;
1346 memcpy(&hdrSquid
, hbuf
, sizeof(hdrSquid
));
1347 hdr
.length
= hdrSquid
.length
;
1348 hdr
.opcode
= hdrSquid
.opcode
;
1349 hdr
.response
= hdrSquid
.response
;
1350 hdr
.F1
= hdrSquid
.F1
;
1351 hdr
.RR
= hdrSquid
.RR
;
1353 hdr
.msg_id
= hdrSquid
.msg_id
;
1356 hdr
.length
= ntohs(hdr
.length
);
1357 hdr
.msg_id
= ntohl(hdr
.msg_id
);
1358 debugs(31, 3, "htcpHandleData: hsz = " << hsz
);
1359 debugs(31, 3, "htcpHandleData: length = " << hdr
.length
);
1361 if (hdr
.opcode
>= HTCP_END
) {
1362 debugs(31, 3, "htcpHandleData: client " << from
<< ", opcode " << hdr
.opcode
<< " out of range");
1366 debugs(31, 3, "htcpHandleData: opcode = " << hdr
.opcode
<< " " << htcpOpcodeStr
[hdr
.opcode
]);
1367 debugs(31, 3, "htcpHandleData: response = " << hdr
.response
);
1368 debugs(31, 3, "htcpHandleData: F1 = " << hdr
.F1
);
1369 debugs(31, 3, "htcpHandleData: RR = " << hdr
.RR
);
1370 debugs(31, 3, "htcpHandleData: msg_id = " << hdr
.msg_id
);
1372 if (hsz
< hdr
.length
) {
1373 debugs(31, 3, "htcpHandleData: sz < hdr.length");
1378 * set sz = hdr.length so we ignore any AUTH fields following
1381 hsz
= (int) hdr
.length
;
1382 hbuf
+= sizeof(htcpDataHeader
);
1383 hsz
-= sizeof(htcpDataHeader
);
1384 debugs(31, 3, "htcpHandleData: hsz = " << hsz
);
1386 htcpHexdump("htcpHandleData", hbuf
, hsz
);
1388 switch (hdr
.opcode
) {
1390 debugs(31, 3, "HTCP NOP not implemented");
1393 htcpHandleTst(&hdr
, hbuf
, hsz
, from
);
1396 debugs(31, 3, "HTCP MON not implemented");
1399 debugs(31, 3, "HTCP SET not implemented");
1402 htcpHandleClr(&hdr
, hbuf
, hsz
, from
);
1403 htcpForwardClr(buf
, sz
);
1411 htcpRecv(int fd
, void *)
1413 static char buf
[8192];
1415 static Ip::Address from
;
1417 /* Receive up to 8191 bytes, leaving room for a null */
1419 len
= comm_udp_recvfrom(fd
, buf
, sizeof(buf
) - 1, 0, from
);
1421 debugs(31, 3, "htcpRecv: FD " << fd
<< ", " << len
<< " bytes from " << from
);
1424 ++statCounter
.htcp
.pkts_recv
;
1426 htcpHandleMsg(buf
, len
, from
);
1428 Comm::SetSelect(fd
, COMM_SELECT_READ
, htcpRecv
, NULL
, 0);
1432 * ======================================================================
1434 * ======================================================================
1440 if (Config
.Port
.htcp
<= 0) {
1441 debugs(31, DBG_IMPORTANT
, "HTCP Disabled.");
1445 htcpIncomingConn
= new Comm::Connection
;
1446 htcpIncomingConn
->local
= Config
.Addrs
.udp_incoming
;
1447 htcpIncomingConn
->local
.port(Config
.Port
.htcp
);
1449 if (!Ip::EnableIpv6
&& !htcpIncomingConn
->local
.setIPv4()) {
1450 debugs(31, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << htcpIncomingConn
->local
<< " is not an IPv4 address.");
1451 fatal("HTCP port cannot be opened.");
1453 /* split-stack for now requires default IPv4-only HTCP */
1454 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& htcpIncomingConn
->local
.isAnyAddr()) {
1455 htcpIncomingConn
->local
.setIPv4();
1458 AsyncCall::Pointer call
= asyncCall(31, 2,
1459 "htcpIncomingConnectionOpened",
1460 Comm::UdpOpenDialer(&htcpIncomingConnectionOpened
));
1462 Ipc::StartListening(SOCK_DGRAM
,
1465 Ipc::fdnInHtcpSocket
, call
);
1467 if (!Config
.Addrs
.udp_outgoing
.isNoAddr()) {
1468 htcpOutgoingConn
= new Comm::Connection
;
1469 htcpOutgoingConn
->local
= Config
.Addrs
.udp_outgoing
;
1470 htcpOutgoingConn
->local
.port(Config
.Port
.htcp
);
1472 if (!Ip::EnableIpv6
&& !htcpOutgoingConn
->local
.setIPv4()) {
1473 debugs(31, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << htcpOutgoingConn
->local
<< " is not an IPv4 address.");
1474 fatal("HTCP port cannot be opened.");
1476 /* split-stack for now requires default IPv4-only HTCP */
1477 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& htcpOutgoingConn
->local
.isAnyAddr()) {
1478 htcpOutgoingConn
->local
.setIPv4();
1482 comm_open_listener(SOCK_DGRAM
, IPPROTO_UDP
, htcpOutgoingConn
, "Outgoing HTCP Socket");
1485 if (!Comm::IsConnOpen(htcpOutgoingConn
))
1486 fatal("Cannot open Outgoing HTCP Socket");
1488 Comm::SetSelect(htcpOutgoingConn
->fd
, COMM_SELECT_READ
, htcpRecv
, NULL
, 0);
1490 debugs(31, DBG_IMPORTANT
, "Sending HTCP messages from " << htcpOutgoingConn
->local
);
1493 if (!htcpDetailPool
) {
1494 htcpDetailPool
= memPoolCreate("htcpDetail", sizeof(htcpDetail
));
1499 htcpIncomingConnectionOpened(const Comm::ConnectionPointer
&conn
, int)
1501 if (!Comm::IsConnOpen(conn
))
1502 fatal("Cannot open HTCP Socket");
1504 Comm::SetSelect(conn
->fd
, COMM_SELECT_READ
, htcpRecv
, NULL
, 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
);
1524 HttpStateFlags flags
;
1526 if (!Comm::IsConnOpen(htcpIncomingConn
))
1529 old_squid_format
= p
->options
.htcp_oldsquid
;
1530 memset(&flags
, '\0', sizeof(flags
));
1531 snprintf(vbuf
, sizeof(vbuf
), "%d/%d",
1532 req
->http_ver
.major
, req
->http_ver
.minor
);
1534 htcpStuff
stuff(++msg_id_counter
, HTCP_TST
, RR_REQUEST
, 1);
1535 SBuf sb
= req
->method
.image();
1536 stuff
.S
.method
= sb
.c_str();
1537 stuff
.S
.uri
= (char *) e
->url();
1538 stuff
.S
.version
= vbuf
;
1539 HttpStateData::httpBuildRequestHeader(req
, e
, NULL
, &hdr
, flags
);
1541 packerToMemInit(&pa
, &mb
);
1545 stuff
.S
.req_hdrs
= mb
.buf
;
1546 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
1549 debugs(31, 3, "htcpQuery: htcpBuildPacket() failed");
1553 htcpSend(pkt
, (int) pktlen
, p
->in_addr
);
1555 queried_id
[stuff
.msg_id
% N_QUERIED_KEYS
] = stuff
.msg_id
;
1556 save_key
= queried_keys
[stuff
.msg_id
% N_QUERIED_KEYS
];
1557 storeKeyCopy(save_key
, (const cache_key
*)e
->key
);
1558 queried_addr
[stuff
.msg_id
% N_QUERIED_KEYS
] = p
->in_addr
;
1559 debugs(31, 3, "htcpQuery: key (" << save_key
<< ") " << storeKeyText(save_key
));
1565 * Send an HTCP CLR message for a specified item to a given CachePeer.
1568 htcpClear(StoreEntry
* e
, const char *uri
, HttpRequest
* req
, const HttpRequestMethod
&, CachePeer
* p
, htcp_clr_reason reason
)
1570 static char pkt
[8192];
1573 HttpHeader
hdr(hoRequest
);
1576 HttpStateFlags flags
;
1578 if (!Comm::IsConnOpen(htcpIncomingConn
))
1581 old_squid_format
= p
->options
.htcp_oldsquid
;
1582 memset(&flags
, '\0', sizeof(flags
));
1583 snprintf(vbuf
, sizeof(vbuf
), "%d/%d",
1584 req
->http_ver
.major
, req
->http_ver
.minor
);
1586 htcpStuff
stuff(++msg_id_counter
, HTCP_CLR
, RR_REQUEST
, 0);
1587 if (reason
== HTCP_CLR_INVALIDATION
)
1590 SBuf sb
= req
->method
.image();
1591 stuff
.S
.method
= sb
.c_str();
1592 if (e
== NULL
|| e
->mem_obj
== NULL
) {
1596 stuff
.S
.uri
= xstrdup(uri
);
1598 stuff
.S
.uri
= (char *) e
->url();
1600 stuff
.S
.version
= vbuf
;
1601 if (reason
!= HTCP_CLR_INVALIDATION
) {
1602 HttpStateData::httpBuildRequestHeader(req
, e
, NULL
, &hdr
, flags
);
1604 packerToMemInit(&pa
, &mb
);
1608 stuff
.S
.req_hdrs
= mb
.buf
;
1610 stuff
.S
.req_hdrs
= NULL
;
1612 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
1613 if (reason
!= HTCP_CLR_INVALIDATION
) {
1620 debugs(31, 3, "htcpClear: htcpBuildPacket() failed");
1624 htcpSend(pkt
, (int) pktlen
, p
->in_addr
);
1628 * htcpSocketShutdown only closes the 'in' socket if it is
1629 * different than the 'out' socket.
1632 htcpSocketShutdown(void)
1634 if (!Comm::IsConnOpen(htcpIncomingConn
))
1637 debugs(12, DBG_IMPORTANT
, "Stop accepting HTCP on " << htcpIncomingConn
->local
);
1639 * Here we just unlink htcpIncomingConn because the HTCP 'in'
1640 * and 'out' sockets might be just one FD. This prevents this
1641 * function from executing repeatedly. When we are really ready to
1642 * exit or restart, main will comm_close the 'out' descriptor.
1644 htcpIncomingConn
= NULL
;
1647 * Normally we only write to the outgoing HTCP socket, but
1648 * we also have a read handler there to catch messages sent
1649 * to that specific interface. During shutdown, we must
1650 * disable reading on the outgoing socket.
1652 /* XXX Don't we need this handler to read replies while shutting down?
1653 * I think there should be a separate hander for reading replies..
1655 assert(Comm::IsConnOpen(htcpOutgoingConn
));
1657 Comm::SetSelect(htcpOutgoingConn
->fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
1661 htcpClosePorts(void)
1663 htcpSocketShutdown();
1665 if (htcpOutgoingConn
!= NULL
) {
1666 debugs(12, DBG_IMPORTANT
, "Stop sending HTCP from " << htcpOutgoingConn
->local
);
1667 htcpOutgoingConn
= NULL
;
1672 htcpLogHtcp(Ip::Address
&caddr
, int opcode
, LogTags logcode
, const char *url
)
1674 AccessLogEntry::Pointer al
= new AccessLogEntry
;
1675 if (LOG_TAG_NONE
== logcode
)
1677 if (!Config
.onoff
.log_udp
)
1679 al
->htcp
.opcode
= htcpOpcodeStr
[opcode
];
1681 al
->cache
.caddr
= caddr
;
1682 al
->cache
.code
= logcode
;
1683 al
->cache
.trTime
.tv_sec
= 0;
1684 al
->cache
.trTime
.tv_usec
= 0;
1685 accessLogLog(al
, NULL
);