2 * Copyright (C) 1996-2014 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 htcpHandleMon(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
281 static void htcpHandleNop(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
283 static void htcpHandleSet(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
285 static void htcpHandleTst(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
287 static void htcpRecv(int fd
, void *data
);
289 static void htcpSend(const char *buf
, int len
, Ip::Address
&to
);
291 static void htcpTstReply(htcpDataHeader
*, StoreEntry
*, htcpSpecifier
*, Ip::Address
&);
293 static void htcpHandleTstRequest(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
295 static void htcpHandleTstResponse(htcpDataHeader
*, char *, int, Ip::Address
&);
298 htcpHexdump(const char *tag
, const char *s
, int sz
)
304 debugs(31, 3, "htcpHexdump " << tag
);
305 memset(hex
, '\0', 80);
307 for (i
= 0; i
< sz
; ++i
) {
309 snprintf(&hex
[k
* 3], 4, " %02x", (int) *(s
+ i
));
311 if (k
< 15 && i
< (sz
- 1))
314 debugs(31, 3, "\t" << hex
);
316 memset(hex
, '\0', 80);
323 * STUFF FOR SENDING HTCP MESSAGES
327 htcpBuildAuth(char *buf
, size_t buflen
)
331 assert(2 == sizeof(uint16_t));
332 auth
.length
= htons(2);
334 if (buflen
< copy_sz
)
336 memcpy(buf
, &auth
, copy_sz
);
341 htcpBuildCountstr(char *buf
, size_t buflen
, const char *s
, size_t len
)
345 if (buflen
- off
< 2)
348 debugs(31, 3, "htcpBuildCountstr: LENGTH = " << len
);
350 debugs(31, 3, "htcpBuildCountstr: TEXT = {" << (s
? s
: "<NULL>") << "}");
352 uint16_t length
= htons((uint16_t) len
);
354 memcpy(buf
+ off
, &length
, 2);
358 if (buflen
- off
< len
)
362 memcpy(buf
+ off
, s
, len
);
370 htcpBuildSpecifier(char *buf
, size_t buflen
, htcpStuff
* stuff
)
374 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.method
, (stuff
->S
.method
?strlen(stuff
->S
.method
):0));
381 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.uri
, (stuff
->S
.uri
?strlen(stuff
->S
.uri
):0));
388 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.version
, (stuff
->S
.version
?strlen(stuff
->S
.version
):0));
395 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.req_hdrs
, stuff
->S
.reqHdrsSz
);
402 debugs(31, 3, "htcpBuildSpecifier: size " << off
);
408 htcpBuildDetail(char *buf
, size_t buflen
, htcpStuff
* stuff
)
412 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.resp_hdrs
, stuff
->D
.respHdrsSz
);
419 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.entity_hdrs
, stuff
->D
.entityHdrsSz
);
426 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.cache_hdrs
, stuff
->D
.cacheHdrsSz
);
437 htcpBuildTstOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
442 debugs(31, 3, "htcpBuildTstOpData: RR_REQUEST");
443 return htcpBuildSpecifier(buf
, buflen
, stuff
);
446 debugs(31, 3, "htcpBuildTstOpData: RR_RESPONSE");
447 debugs(31, 3, "htcpBuildTstOpData: F1 = " << stuff
->f1
);
449 if (stuff
->f1
) /* cache miss */
452 return htcpBuildDetail(buf
, buflen
, stuff
);
455 fatal_dump("htcpBuildTstOpData: bad RR value");
462 htcpBuildClrOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
464 unsigned short reason
;
468 debugs(31, 3, "htcpBuildClrOpData: RR_REQUEST");
469 reason
= htons((unsigned short)stuff
->reason
);
470 memcpy(buf
, &reason
, 2);
471 return htcpBuildSpecifier(buf
+ 2, buflen
- 2, stuff
) + 2;
475 fatal_dump("htcpBuildClrOpData: bad RR value");
482 htcpBuildOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
485 debugs(31, 3, "htcpBuildOpData: opcode " << htcpOpcodeStr
[stuff
->op
]);
490 off
= htcpBuildTstOpData(buf
+ off
, buflen
, stuff
);
494 off
= htcpBuildClrOpData(buf
+ off
, buflen
, stuff
);
506 htcpBuildData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
510 size_t hdr_sz
= sizeof(htcpDataHeader
);
516 off
+= hdr_sz
; /* skip! */
518 op_data_sz
= htcpBuildOpData(buf
+ off
, buflen
- off
, stuff
);
525 debugs(31, 3, "htcpBuildData: hdr.length = " << off
);
527 hdr
.length
= (uint16_t) off
;
529 hdr
.opcode
= stuff
->op
;
531 hdr
.response
= stuff
->response
;
537 hdr
.msg_id
= stuff
->msg_id
;
539 /* convert multi-byte fields */
540 hdr
.length
= htons(hdr
.length
);
542 hdr
.msg_id
= htonl(hdr
.msg_id
);
544 if (!old_squid_format
) {
545 memcpy(buf
, &hdr
, hdr_sz
);
547 htcpDataHeaderSquid hdrSquid
;
548 memset(&hdrSquid
, 0, sizeof(hdrSquid
));
549 hdrSquid
.length
= hdr
.length
;
550 hdrSquid
.opcode
= hdr
.opcode
;
551 hdrSquid
.response
= hdr
.response
;
552 hdrSquid
.F1
= hdr
.F1
;
553 hdrSquid
.RR
= hdr
.RR
;
554 memcpy(buf
, &hdrSquid
, hdr_sz
);
557 debugs(31, 3, "htcpBuildData: size " << off
);
563 * Build an HTCP packet into buf, maximum length buflen.
564 * Returns the packet length, or zero on failure.
567 htcpBuildPacket(char *buf
, size_t buflen
, htcpStuff
* stuff
)
571 size_t hdr_sz
= sizeof(htcpHeader
);
573 /* skip the header -- we don't know the overall length */
575 if (buflen
< hdr_sz
) {
580 s
= htcpBuildData(buf
+ off
, buflen
- off
, stuff
);
587 s
= htcpBuildAuth(buf
+ off
, buflen
- off
);
594 hdr
.length
= htons((uint16_t) off
);
597 if (old_squid_format
)
602 memcpy(buf
, &hdr
, hdr_sz
);
604 debugs(31, 3, "htcpBuildPacket: size " << off
);
610 htcpSend(const char *buf
, int len
, Ip::Address
&to
)
612 debugs(31, 3, HERE
<< to
);
613 htcpHexdump("htcpSend", buf
, len
);
615 if (comm_udp_sendto(htcpOutgoingConn
->fd
, to
, buf
, len
) < 0)
616 debugs(31, 3, HERE
<< htcpOutgoingConn
<< " sendto: " << xstrerror());
618 ++statCounter
.htcp
.pkts_sent
;
622 * STUFF FOR RECEIVING HTCP MESSAGES
626 htcpSpecifier::setFrom(Ip::Address
&aSocket
)
632 htcpSpecifier::setDataHeader(htcpDataHeader
*aDataHeader
)
638 htcpFreeSpecifier(htcpSpecifier
* s
)
640 HTTPMSGUNLOCK(s
->request
);
646 htcpFreeDetail(htcpDetail
* d
)
648 htcpDetailPool
->freeOne(d
);
652 * Unpack an HTCP SPECIFIER in place
653 * This will overwrite any following AUTH block
655 // XXX: this needs to be turned into an Htcp1::Parser inheriting from Http1::RequestParser
656 // but with different first-line and block unpacking logic.
657 static htcpSpecifier
*
658 htcpUnpackSpecifier(char *buf
, int sz
)
660 htcpSpecifier
*s
= new htcpSpecifier
;
661 HttpRequestMethod method
;
663 /* Find length of METHOD */
664 uint16_t l
= ntohs(*(uint16_t *) buf
);
669 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack METHOD");
670 htcpFreeSpecifier(s
);
678 debugs(31, 6, "htcpUnpackSpecifier: METHOD (" << l
<< "/" << sz
<< ") '" << s
->method
<< "'");
680 /* Find length of URI */
681 l
= ntohs(*(uint16_t *) buf
);
685 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack URI");
686 htcpFreeSpecifier(s
);
690 /* Add terminating null to METHOD */
698 debugs(31, 6, "htcpUnpackSpecifier: URI (" << l
<< "/" << sz
<< ") '" << s
->uri
<< "'");
700 /* Find length of VERSION */
701 l
= ntohs(*(uint16_t *) buf
);
705 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack VERSION");
706 htcpFreeSpecifier(s
);
710 /* Add terminating null to URI */
718 debugs(31, 6, "htcpUnpackSpecifier: VERSION (" << l
<< "/" << sz
<< ") '" << s
->version
<< "'");
720 /* Find length of REQ-HDRS */
721 l
= ntohs(*(uint16_t *) buf
);
725 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack REQ-HDRS");
726 htcpFreeSpecifier(s
);
730 /* Add terminating null to URI */
739 debugs(31, 6, "htcpUnpackSpecifier: REQ-HDRS (" << l
<< "/" << sz
<< ") '" << s
->req_hdrs
<< "'");
741 debugs(31, 3, "htcpUnpackSpecifier: " << sz
<< " bytes left");
744 * Add terminating null to REQ-HDRS. This is possible because we allocated
745 * an extra byte when we received the packet. This will overwrite any following
751 method
.HttpRequestMethodXXX(s
->method
);
753 s
->request
= HttpRequest::CreateFromUrlAndMethod(s
->uri
, method
== Http::METHOD_NONE
? HttpRequestMethod(Http::METHOD_GET
) : method
);
756 HTTPMSGLOCK(s
->request
);
762 * Unpack an HTCP DETAIL in place
763 * This will overwrite any following AUTH block
766 htcpUnpackDetail(char *buf
, int sz
)
768 htcpDetail
*d
= static_cast<htcpDetail
*>(htcpDetailPool
->alloc());
770 /* Find length of RESP-HDRS */
771 uint16_t l
= ntohs(*(uint16_t *) buf
);
776 debugs(31, 3, "htcpUnpackDetail: failed to unpack RESP_HDRS");
787 /* Find length of ENTITY-HDRS */
788 l
= ntohs(*(uint16_t *) buf
);
793 debugs(31, 3, "htcpUnpackDetail: failed to unpack ENTITY_HDRS");
798 /* Add terminating null to RESP-HDRS */
801 /* Set ENTITY-HDRS */
804 d
->entity_hdrs
= buf
;
809 /* Find length of CACHE-HDRS */
810 l
= ntohs(*(uint16_t *) buf
);
815 debugs(31, 3, "htcpUnpackDetail: failed to unpack CACHE_HDRS");
820 /* Add terminating null to ENTITY-HDRS */
831 debugs(31, 3, "htcpUnpackDetail: " << sz
<< " bytes left");
834 * Add terminating null to CACHE-HDRS. This is possible because we allocated
835 * an extra byte when we received the packet. This will overwrite any following
844 htcpAccessAllowed(acl_access
* acl
, htcpSpecifier
* s
, Ip::Address
&from
)
846 /* default deny if no access list present */
850 ACLFilledChecklist
checklist(acl
, s
->request
, NULL
);
851 checklist
.src_addr
= from
;
852 checklist
.my_addr
.setNoAddr();
853 return (checklist
.fastCheck() == ACCESS_ALLOWED
);
857 htcpTstReply(htcpDataHeader
* dhdr
, StoreEntry
* e
, htcpSpecifier
* spec
, Ip::Address
&from
)
859 static char pkt
[8192];
860 HttpHeader
hdr(hoHtcpReply
);
865 htcpStuff
stuff(dhdr
->msg_id
, HTCP_TST
, RR_RESPONSE
, 0);
866 stuff
.response
= e
? 0 : 1;
867 debugs(31, 3, "htcpTstReply: response = " << stuff
.response
);
871 packerToMemInit(&p
, &mb
);
872 stuff
.S
.method
= spec
->method
;
873 stuff
.S
.uri
= spec
->uri
;
874 stuff
.S
.version
= spec
->version
;
875 stuff
.S
.req_hdrs
= spec
->req_hdrs
;
876 stuff
.S
.reqHdrsSz
= spec
->reqHdrsSz
;
878 hdr
.putInt(HDR_AGE
, (e
->timestamp
<= squid_curtime
? (squid_curtime
- e
->timestamp
) : 0) );
880 hdr
.putInt(HDR_AGE
, 0);
882 stuff
.D
.resp_hdrs
= xstrdup(mb
.buf
);
883 stuff
.D
.respHdrsSz
= mb
.contentSize();
884 debugs(31, 3, "htcpTstReply: resp_hdrs = {" << stuff
.D
.resp_hdrs
<< "}");
888 if (e
&& e
->expires
> -1)
889 hdr
.putTime(HDR_EXPIRES
, e
->expires
);
891 if (e
&& e
->lastmod
> -1)
892 hdr
.putTime(HDR_LAST_MODIFIED
, e
->lastmod
);
896 stuff
.D
.entity_hdrs
= xstrdup(mb
.buf
);
897 stuff
.D
.entityHdrsSz
= mb
.contentSize();
899 debugs(31, 3, "htcpTstReply: entity_hdrs = {" << stuff
.D
.entity_hdrs
<< "}");
906 if (char *host
= urlHostname(spec
->uri
)) {
910 netdbHostData(host
, &samp
, &rtt
, &hops
);
914 snprintf(cto_buf
, 128, "%s %d %f %d",
915 host
, samp
, 0.001 * rtt
, hops
);
916 hdr
.putExt("Cache-to-Origin", cto_buf
);
919 #endif /* USE_ICMP */
922 stuff
.D
.cache_hdrs
= xstrdup(mb
.buf
);
923 stuff
.D
.cacheHdrsSz
= mb
.contentSize();
924 debugs(31, 3, "htcpTstReply: cache_hdrs = {" << stuff
.D
.cache_hdrs
<< "}");
930 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
932 safe_free(stuff
.D
.resp_hdrs
);
933 stuff
.D
.respHdrsSz
= 0;
934 safe_free(stuff
.D
.entity_hdrs
);
935 stuff
.D
.entityHdrsSz
= 0;
936 safe_free(stuff
.D
.cache_hdrs
);
937 stuff
.D
.cacheHdrsSz
= 0;
940 debugs(31, 3, "htcpTstReply: htcpBuildPacket() failed");
944 htcpSend(pkt
, (int) pktlen
, from
);
949 htcpClrReply(htcpDataHeader
* dhdr
, int purgeSucceeded
, Ip::Address
&from
)
951 static char pkt
[8192];
954 /* If dhdr->F1 == 0, no response desired */
959 htcpStuff
stuff(dhdr
->msg_id
, HTCP_CLR
, RR_RESPONSE
, 0);
961 stuff
.response
= purgeSucceeded
? 0 : 2;
963 debugs(31, 3, "htcpClrReply: response = " << stuff
.response
);
965 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
968 debugs(31, 3, "htcpClrReply: htcpBuildPacket() failed");
972 htcpSend(pkt
, (int) pktlen
, from
);
977 htcpHandleNop(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
979 debugs(31, 3, "htcpHandleNop: Unimplemented");
983 htcpSpecifier::checkHit()
985 checkHitRequest
= request
;
987 if (NULL
== checkHitRequest
) {
988 debugs(31, 3, "htcpCheckHit: NO; failed to parse URL");
989 checkedHit(NullStoreEntry::getInstance());
993 if (!checkHitRequest
->header
.parse(req_hdrs
, reqHdrsSz
)) {
994 debugs(31, 3, "htcpCheckHit: NO; failed to parse request headers");
995 delete checkHitRequest
;
996 checkHitRequest
= NULL
;
997 checkedHit(NullStoreEntry::getInstance());
1001 StoreEntry::getPublicByRequest(this, checkHitRequest
);
1005 htcpSpecifier::created (StoreEntry
*e
)
1007 StoreEntry
*hit
=NULL
;
1011 debugs(31, 3, "htcpCheckHit: NO; public object not found");
1012 } else if (!e
->validToSend()) {
1013 debugs(31, 3, "htcpCheckHit: NO; entry not valid to send" );
1014 } else if (refreshCheckHTCP(e
, checkHitRequest
)) {
1015 debugs(31, 3, "htcpCheckHit: NO; cached response is stale");
1017 debugs(31, 3, "htcpCheckHit: YES!?");
1025 htcpClrStoreEntry(StoreEntry
* e
)
1027 debugs(31, 4, "htcpClrStoreEntry: Clearing store for entry: " << e
->url() );
1028 e
->releaseRequest();
1032 htcpClrStore(const htcpSpecifier
* s
)
1034 HttpRequest
*request
= s
->request
;
1035 StoreEntry
*e
= NULL
;
1038 if (request
== NULL
) {
1039 debugs(31, 3, "htcpClrStore: failed to parse URL");
1043 /* Parse request headers */
1044 if (!request
->header
.parse(s
->req_hdrs
, s
->reqHdrsSz
)) {
1045 debugs(31, 2, "htcpClrStore: failed to parse request headers");
1049 /* Lookup matching entries. This matches both GET and HEAD */
1050 while ((e
= storeGetPublicByRequest(request
)) != NULL
) {
1052 htcpClrStoreEntry(e
);
1058 debugs(31, 4, "htcpClrStore: Cleared " << released
<< " matching entries");
1061 debugs(31, 4, "htcpClrStore: No matching entry found");
1068 htcpHandleTst(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1070 debugs(31, 3, "htcpHandleTst: sz = " << sz
);
1072 if (hdr
->RR
== RR_REQUEST
)
1073 htcpHandleTstRequest(hdr
, buf
, sz
, from
);
1075 htcpHandleTstResponse(hdr
, buf
, sz
, from
);
1078 HtcpReplyData::HtcpReplyData() :
1079 hit(0), hdr(hoHtcpReply
), msg_id(0), version(0.0)
1081 memset(&cto
, 0, sizeof(cto
));
1086 htcpHandleTstResponse(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1088 HtcpReplyData htcpReply
;
1089 cache_key
*key
= NULL
;
1092 htcpDetail
*d
= NULL
;
1095 if (queried_id
[hdr
->msg_id
% N_QUERIED_KEYS
] != hdr
->msg_id
) {
1096 debugs(31, 2, "htcpHandleTstResponse: No matching query id '" <<
1097 hdr
->msg_id
<< "' (expected " <<
1098 queried_id
[hdr
->msg_id
% N_QUERIED_KEYS
] << ") from '" <<
1104 key
= queried_keys
[hdr
->msg_id
% N_QUERIED_KEYS
];
1107 debugs(31, 3, "htcpHandleTstResponse: No query key for response id '" << hdr
->msg_id
<< "' from '" << from
<< "'");
1111 peer
= &queried_addr
[hdr
->msg_id
% N_QUERIED_KEYS
];
1113 if ( *peer
!= from
|| peer
->port() != from
.port() ) {
1114 debugs(31, 3, "htcpHandleTstResponse: Unexpected response source " << from
);
1119 debugs(31, 2, "htcpHandleTstResponse: error condition, F1/MO == 1");
1123 htcpReply
.msg_id
= hdr
->msg_id
;
1124 debugs(31, 3, "htcpHandleTstResponse: msg_id = " << htcpReply
.msg_id
);
1125 htcpReply
.hit
= hdr
->response
? 0 : 1;
1128 debugs(31, 3, "htcpHandleTstResponse: MISS");
1130 debugs(31, 3, "htcpHandleTstResponse: HIT");
1131 d
= htcpUnpackDetail(buf
, sz
);
1134 debugs(31, 3, "htcpHandleTstResponse: bad DETAIL");
1138 if ((t
= d
->resp_hdrs
))
1139 htcpReply
.hdr
.parse(t
, d
->respHdrsSz
);
1141 if ((t
= d
->entity_hdrs
))
1142 htcpReply
.hdr
.parse(t
, d
->entityHdrsSz
);
1144 if ((t
= d
->cache_hdrs
))
1145 htcpReply
.hdr
.parse(t
, d
->cacheHdrsSz
);
1148 debugs(31, 3, "htcpHandleTstResponse: key (" << key
<< ") " << storeKeyText(key
));
1149 neighborsHtcpReply(key
, &htcpReply
, from
);
1150 htcpReply
.hdr
.clean();
1157 htcpHandleTstRequest(htcpDataHeader
* dhdr
, char *buf
, int sz
, Ip::Address
&from
)
1159 /* buf should be a SPECIFIER */
1163 debugs(31, 3, "htcpHandleTst: nothing to do");
1170 /* s is a new object */
1171 s
= htcpUnpackSpecifier(buf
, sz
);
1174 debugs(31, 3, "htcpHandleTstRequest: htcpUnpackSpecifier failed");
1175 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1179 s
->setDataHeader(dhdr
);
1183 debugs(31, 3, "htcpHandleTstRequest: failed to parse request");
1184 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1185 htcpFreeSpecifier(s
);
1189 if (!htcpAccessAllowed(Config
.accessList
.htcp
, s
, from
)) {
1190 debugs(31, 3, "htcpHandleTstRequest: Access denied");
1191 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_DENIED
, s
->uri
);
1192 htcpFreeSpecifier(s
);
1196 debugs(31, 2, "HTCP TST request: " << s
->method
<< " " << s
->uri
<< " " << s
->version
);
1197 debugs(31, 2, "HTCP TST headers: " << s
->req_hdrs
);
1202 htcpSpecifier::checkedHit(StoreEntry
*e
)
1205 htcpTstReply(dhdr
, e
, this, from
); /* hit */
1206 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_HIT
, uri
);
1208 htcpTstReply(dhdr
, NULL
, NULL
, from
); /* cache miss */
1209 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_MISS
, uri
);
1212 htcpFreeSpecifier(this);
1217 htcpHandleMon(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1219 debugs(31, 3, "htcpHandleMon: Unimplemented");
1224 htcpHandleSet(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1226 debugs(31, 3, "htcpHandleSet: Unimplemented");
1230 htcpHandleClr(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1233 /* buf[0/1] is reserved and reason */
1234 int reason
= buf
[1] << 4;
1235 debugs(31, 2, "HTCP CLR reason: " << reason
);
1239 /* buf should be a SPECIFIER */
1242 debugs(31, 4, "htcpHandleClr: nothing to do");
1243 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1247 s
= htcpUnpackSpecifier(buf
, sz
);
1250 debugs(31, 3, "htcpHandleClr: htcpUnpackSpecifier failed");
1251 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1256 debugs(31, 3, "htcpHandleTstRequest: failed to parse request");
1257 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1258 htcpFreeSpecifier(s
);
1262 if (!htcpAccessAllowed(Config
.accessList
.htcp_clr
, s
, from
)) {
1263 debugs(31, 3, "htcpHandleClr: Access denied");
1264 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_DENIED
, s
->uri
);
1265 htcpFreeSpecifier(s
);
1269 debugs(31, 2, "HTCP CLR request: " << s
->method
<< " " << s
->uri
<< " " << s
->version
);
1270 debugs(31, 2, "HTCP CLR headers: " << s
->req_hdrs
);
1272 /* Release objects from cache
1273 * analog to clientPurgeRequest in client_side.c
1276 switch (htcpClrStore(s
)) {
1279 htcpClrReply(hdr
, 1, from
); /* hit */
1280 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_HIT
, s
->uri
);
1284 htcpClrReply(hdr
, 0, from
); /* miss */
1285 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_MISS
, s
->uri
);
1292 htcpFreeSpecifier(s
);
1296 * Forward a CLR request to all peers who have requested that CLRs be
1297 * forwarded to them.
1300 htcpForwardClr(char *buf
, int sz
)
1304 for (p
= Config
.peers
; p
; p
= p
->next
) {
1305 if (!p
->options
.htcp
) {
1308 if (!p
->options
.htcp_forward_clr
) {
1312 htcpSend(buf
, sz
, p
->in_addr
);
1317 * Do the first pass of handling an HTCP message. This used to be two
1318 * separate functions, htcpHandle and htcpHandleData. They were merged to
1319 * allow for forwarding HTCP packets easily to other peers if desired.
1321 * This function now works out what type of message we have received and then
1322 * hands it off to other functions to break apart message-specific data.
1325 htcpHandleMsg(char *buf
, int sz
, Ip::Address
&from
)
1332 if (sz
< 0 || (size_t)sz
< sizeof(htcpHeader
)) {
1333 // These are highly likely to be attack packets. Should probably get a bigger warning.
1334 debugs(31, 2, "htcpHandle: msg size less than htcpHeader size from " << from
);
1338 htcpHexdump("htcpHandle", buf
, sz
);
1339 memcpy(&htcpHdr
, buf
, sizeof(htcpHeader
));
1340 htcpHdr
.length
= ntohs(htcpHdr
.length
);
1342 if (htcpHdr
.minor
== 0)
1343 old_squid_format
= 1;
1345 old_squid_format
= 0;
1347 debugs(31, 3, "htcpHandle: htcpHdr.length = " << htcpHdr
.length
);
1348 debugs(31, 3, "htcpHandle: htcpHdr.major = " << htcpHdr
.major
);
1349 debugs(31, 3, "htcpHandle: htcpHdr.minor = " << htcpHdr
.minor
);
1351 if (sz
!= htcpHdr
.length
) {
1352 debugs(31, 3, "htcpHandle: sz/" << sz
<< " != htcpHdr.length/" <<
1353 htcpHdr
.length
<< " from " << from
);
1358 if (htcpHdr
.major
!= 0) {
1359 debugs(31, 3, "htcpHandle: Unknown major version " << htcpHdr
.major
<< " from " << from
);
1364 hbuf
= buf
+ sizeof(htcpHeader
);
1365 hsz
= sz
- sizeof(htcpHeader
);
1367 if ((size_t)hsz
< sizeof(htcpDataHeader
)) {
1368 debugs(31, 3, "htcpHandleData: msg size less than htcpDataHeader size");
1372 if (!old_squid_format
) {
1373 memcpy(&hdr
, hbuf
, sizeof(hdr
));
1375 htcpDataHeaderSquid hdrSquid
;
1376 memcpy(&hdrSquid
, hbuf
, sizeof(hdrSquid
));
1377 hdr
.length
= hdrSquid
.length
;
1378 hdr
.opcode
= hdrSquid
.opcode
;
1379 hdr
.response
= hdrSquid
.response
;
1380 hdr
.F1
= hdrSquid
.F1
;
1381 hdr
.RR
= hdrSquid
.RR
;
1383 hdr
.msg_id
= hdrSquid
.msg_id
;
1386 hdr
.length
= ntohs(hdr
.length
);
1387 hdr
.msg_id
= ntohl(hdr
.msg_id
);
1388 debugs(31, 3, "htcpHandleData: hsz = " << hsz
);
1389 debugs(31, 3, "htcpHandleData: length = " << hdr
.length
);
1391 if (hdr
.opcode
>= HTCP_END
) {
1392 debugs(31, 3, "htcpHandleData: client " << from
<< ", opcode " << hdr
.opcode
<< " out of range");
1396 debugs(31, 3, "htcpHandleData: opcode = " << hdr
.opcode
<< " " << htcpOpcodeStr
[hdr
.opcode
]);
1397 debugs(31, 3, "htcpHandleData: response = " << hdr
.response
);
1398 debugs(31, 3, "htcpHandleData: F1 = " << hdr
.F1
);
1399 debugs(31, 3, "htcpHandleData: RR = " << hdr
.RR
);
1400 debugs(31, 3, "htcpHandleData: msg_id = " << hdr
.msg_id
);
1402 if (hsz
< hdr
.length
) {
1403 debugs(31, 3, "htcpHandleData: sz < hdr.length");
1408 * set sz = hdr.length so we ignore any AUTH fields following
1411 hsz
= (int) hdr
.length
;
1412 hbuf
+= sizeof(htcpDataHeader
);
1413 hsz
-= sizeof(htcpDataHeader
);
1414 debugs(31, 3, "htcpHandleData: hsz = " << hsz
);
1416 htcpHexdump("htcpHandleData", hbuf
, hsz
);
1418 switch (hdr
.opcode
) {
1420 htcpHandleNop(&hdr
, hbuf
, hsz
, from
);
1423 htcpHandleTst(&hdr
, hbuf
, hsz
, from
);
1426 htcpHandleMon(&hdr
, hbuf
, hsz
, from
);
1429 htcpHandleSet(&hdr
, hbuf
, hsz
, from
);
1432 htcpHandleClr(&hdr
, hbuf
, hsz
, from
);
1433 htcpForwardClr(buf
, sz
);
1441 htcpRecv(int fd
, void *data
)
1443 static char buf
[8192];
1445 static Ip::Address from
;
1447 /* Receive up to 8191 bytes, leaving room for a null */
1449 len
= comm_udp_recvfrom(fd
, buf
, sizeof(buf
) - 1, 0, from
);
1451 debugs(31, 3, "htcpRecv: FD " << fd
<< ", " << len
<< " bytes from " << from
);
1454 ++statCounter
.htcp
.pkts_recv
;
1456 htcpHandleMsg(buf
, len
, from
);
1458 Comm::SetSelect(fd
, COMM_SELECT_READ
, htcpRecv
, NULL
, 0);
1462 * ======================================================================
1464 * ======================================================================
1470 if (Config
.Port
.htcp
<= 0) {
1471 debugs(31, DBG_IMPORTANT
, "HTCP Disabled.");
1475 htcpIncomingConn
= new Comm::Connection
;
1476 htcpIncomingConn
->local
= Config
.Addrs
.udp_incoming
;
1477 htcpIncomingConn
->local
.port(Config
.Port
.htcp
);
1479 if (!Ip::EnableIpv6
&& !htcpIncomingConn
->local
.setIPv4()) {
1480 debugs(31, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << htcpIncomingConn
->local
<< " is not an IPv4 address.");
1481 fatal("HTCP port cannot be opened.");
1483 /* split-stack for now requires default IPv4-only HTCP */
1484 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& htcpIncomingConn
->local
.isAnyAddr()) {
1485 htcpIncomingConn
->local
.setIPv4();
1488 AsyncCall::Pointer call
= asyncCall(31, 2,
1489 "htcpIncomingConnectionOpened",
1490 Comm::UdpOpenDialer(&htcpIncomingConnectionOpened
));
1492 Ipc::StartListening(SOCK_DGRAM
,
1495 Ipc::fdnInHtcpSocket
, call
);
1497 if (!Config
.Addrs
.udp_outgoing
.isNoAddr()) {
1498 htcpOutgoingConn
= new Comm::Connection
;
1499 htcpOutgoingConn
->local
= Config
.Addrs
.udp_outgoing
;
1500 htcpOutgoingConn
->local
.port(Config
.Port
.htcp
);
1502 if (!Ip::EnableIpv6
&& !htcpOutgoingConn
->local
.setIPv4()) {
1503 debugs(31, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << htcpOutgoingConn
->local
<< " is not an IPv4 address.");
1504 fatal("HTCP port cannot be opened.");
1506 /* split-stack for now requires default IPv4-only HTCP */
1507 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& htcpOutgoingConn
->local
.isAnyAddr()) {
1508 htcpOutgoingConn
->local
.setIPv4();
1512 comm_open_listener(SOCK_DGRAM
, IPPROTO_UDP
, htcpOutgoingConn
, "Outgoing HTCP Socket");
1515 if (!Comm::IsConnOpen(htcpOutgoingConn
))
1516 fatal("Cannot open Outgoing HTCP Socket");
1518 Comm::SetSelect(htcpOutgoingConn
->fd
, COMM_SELECT_READ
, htcpRecv
, NULL
, 0);
1520 debugs(31, DBG_IMPORTANT
, "Sending HTCP messages from " << htcpOutgoingConn
->local
);
1523 if (!htcpDetailPool
) {
1524 htcpDetailPool
= memPoolCreate("htcpDetail", sizeof(htcpDetail
));
1529 htcpIncomingConnectionOpened(const Comm::ConnectionPointer
&conn
, int)
1531 if (!Comm::IsConnOpen(conn
))
1532 fatal("Cannot open HTCP Socket");
1534 Comm::SetSelect(conn
->fd
, COMM_SELECT_READ
, htcpRecv
, NULL
, 0);
1536 debugs(31, DBG_CRITICAL
, "Accepting HTCP messages on " << conn
->local
);
1538 if (Config
.Addrs
.udp_outgoing
.isNoAddr()) {
1539 htcpOutgoingConn
= conn
;
1540 debugs(31, DBG_IMPORTANT
, "Sending HTCP messages from " << htcpOutgoingConn
->local
);
1545 htcpQuery(StoreEntry
* e
, HttpRequest
* req
, CachePeer
* p
)
1547 cache_key
*save_key
;
1548 static char pkt
[8192];
1551 HttpHeader
hdr(hoRequest
);
1554 HttpStateFlags flags
;
1556 if (!Comm::IsConnOpen(htcpIncomingConn
))
1559 old_squid_format
= p
->options
.htcp_oldsquid
;
1560 memset(&flags
, '\0', sizeof(flags
));
1561 snprintf(vbuf
, sizeof(vbuf
), "%d/%d",
1562 req
->http_ver
.major
, req
->http_ver
.minor
);
1564 htcpStuff
stuff(++msg_id_counter
, HTCP_TST
, RR_REQUEST
, 1);
1565 SBuf sb
= req
->method
.image();
1566 stuff
.S
.method
= sb
.c_str();
1567 stuff
.S
.uri
= (char *) e
->url();
1568 stuff
.S
.version
= vbuf
;
1569 HttpStateData::httpBuildRequestHeader(req
, e
, NULL
, &hdr
, flags
);
1571 packerToMemInit(&pa
, &mb
);
1575 stuff
.S
.req_hdrs
= mb
.buf
;
1576 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
1579 debugs(31, 3, "htcpQuery: htcpBuildPacket() failed");
1583 htcpSend(pkt
, (int) pktlen
, p
->in_addr
);
1585 queried_id
[stuff
.msg_id
% N_QUERIED_KEYS
] = stuff
.msg_id
;
1586 save_key
= queried_keys
[stuff
.msg_id
% N_QUERIED_KEYS
];
1587 storeKeyCopy(save_key
, (const cache_key
*)e
->key
);
1588 queried_addr
[stuff
.msg_id
% N_QUERIED_KEYS
] = p
->in_addr
;
1589 debugs(31, 3, "htcpQuery: key (" << save_key
<< ") " << storeKeyText(save_key
));
1595 * Send an HTCP CLR message for a specified item to a given CachePeer.
1598 htcpClear(StoreEntry
* e
, const char *uri
, HttpRequest
* req
, const HttpRequestMethod
&method
, CachePeer
* p
, htcp_clr_reason reason
)
1600 static char pkt
[8192];
1603 HttpHeader
hdr(hoRequest
);
1606 HttpStateFlags flags
;
1608 if (!Comm::IsConnOpen(htcpIncomingConn
))
1611 old_squid_format
= p
->options
.htcp_oldsquid
;
1612 memset(&flags
, '\0', sizeof(flags
));
1613 snprintf(vbuf
, sizeof(vbuf
), "%d/%d",
1614 req
->http_ver
.major
, req
->http_ver
.minor
);
1616 htcpStuff
stuff(++msg_id_counter
, HTCP_CLR
, RR_REQUEST
, 0);
1617 if (reason
== HTCP_CLR_INVALIDATION
)
1620 SBuf sb
= req
->method
.image();
1621 stuff
.S
.method
= sb
.c_str();
1622 if (e
== NULL
|| e
->mem_obj
== NULL
) {
1626 stuff
.S
.uri
= xstrdup(uri
);
1628 stuff
.S
.uri
= (char *) e
->url();
1630 stuff
.S
.version
= vbuf
;
1631 if (reason
!= HTCP_CLR_INVALIDATION
) {
1632 HttpStateData::httpBuildRequestHeader(req
, e
, NULL
, &hdr
, flags
);
1634 packerToMemInit(&pa
, &mb
);
1638 stuff
.S
.req_hdrs
= mb
.buf
;
1640 stuff
.S
.req_hdrs
= NULL
;
1642 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
1643 if (reason
!= HTCP_CLR_INVALIDATION
) {
1650 debugs(31, 3, "htcpClear: htcpBuildPacket() failed");
1654 htcpSend(pkt
, (int) pktlen
, p
->in_addr
);
1658 * htcpSocketShutdown only closes the 'in' socket if it is
1659 * different than the 'out' socket.
1662 htcpSocketShutdown(void)
1664 if (!Comm::IsConnOpen(htcpIncomingConn
))
1667 debugs(12, DBG_IMPORTANT
, "Stop accepting HTCP on " << htcpIncomingConn
->local
);
1669 * Here we just unlink htcpIncomingConn because the HTCP 'in'
1670 * and 'out' sockets might be just one FD. This prevents this
1671 * function from executing repeatedly. When we are really ready to
1672 * exit or restart, main will comm_close the 'out' descriptor.
1674 htcpIncomingConn
= NULL
;
1677 * Normally we only write to the outgoing HTCP socket, but
1678 * we also have a read handler there to catch messages sent
1679 * to that specific interface. During shutdown, we must
1680 * disable reading on the outgoing socket.
1682 /* XXX Don't we need this handler to read replies while shutting down?
1683 * I think there should be a separate hander for reading replies..
1685 assert(Comm::IsConnOpen(htcpOutgoingConn
));
1687 Comm::SetSelect(htcpOutgoingConn
->fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
1691 htcpClosePorts(void)
1693 htcpSocketShutdown();
1695 if (htcpOutgoingConn
!= NULL
) {
1696 debugs(12, DBG_IMPORTANT
, "Stop sending HTCP from " << htcpOutgoingConn
->local
);
1697 htcpOutgoingConn
= NULL
;
1702 htcpLogHtcp(Ip::Address
&caddr
, int opcode
, LogTags logcode
, const char *url
)
1704 AccessLogEntry::Pointer al
= new AccessLogEntry
;
1705 if (LOG_TAG_NONE
== logcode
)
1707 if (!Config
.onoff
.log_udp
)
1709 al
->htcp
.opcode
= htcpOpcodeStr
[opcode
];
1711 al
->cache
.caddr
= caddr
;
1712 al
->cache
.code
= logcode
;
1713 al
->cache
.trTime
.tv_sec
= 0;
1714 al
->cache
.trTime
.tv_usec
= 0;
1715 accessLogLog(al
, NULL
);