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 typedef struct _Countstr Countstr
;
42 typedef struct _htcpHeader htcpHeader
;
44 typedef struct _htcpDataHeader htcpDataHeader
;
46 typedef struct _htcpDataHeaderSquid htcpDataHeaderSquid
;
48 typedef struct _htcpAuthHeader htcpAuthHeader
;
50 typedef struct _htcpStuff htcpStuff
;
52 typedef struct _htcpDetail htcpDetail
;
65 struct _htcpDataHeaderSquid
{
69 unsigned int opcode
:4;
70 unsigned int response
:4;
72 unsigned int response
:4;
73 unsigned int opcode
:4;
77 unsigned int reserved
:6;
83 unsigned int reserved
:6;
89 struct _htcpDataHeader
{
123 /* RR == 0 --> F1 = RESPONSE DESIRED FLAG */
124 /* RR == 1 --> F1 = MESSAGE OVERALL FLAG */
125 /* RR == 0 --> REQUEST */
126 /* RR == 1 --> RESPONSE */
128 struct _htcpAuthHeader
{
136 class htcpSpecifier
: public StoreClient
140 MEMPROXY_CLASS(htcpSpecifier
);
142 void created (StoreEntry
*newEntry
);
144 void checkedHit(StoreEntry
*e
);
146 void setFrom(Ip::Address
&from
);
147 void setDataHeader(htcpDataHeader
*);
152 HttpRequest
*request
;
155 HttpRequest
*checkHitRequest
;
157 Ip::Address from
; // was a ptr. return to such IFF needed. otherwise copy should do.
158 htcpDataHeader
*dhdr
;
161 MEMPROXY_CLASS_INLINE(htcpSpecifier
);
189 static const char *const htcpOpcodeStr
[] = {
199 * values for htcpDataHeader->response
204 OPCODE_UNIMPLEMENTED
,
205 MAJOR_VERSION_UNSUPPORTED
,
206 MINOR_VERSION_UNSUPPORTED
,
211 * values for htcpDataHeader->RR
218 static void htcpIncomingConnectionOpened(const Comm::ConnectionPointer
&conn
, int errNo
);
219 static uint32_t msg_id_counter
= 0;
221 static Comm::ConnectionPointer htcpOutgoingConn
= NULL
;
222 static Comm::ConnectionPointer htcpIncomingConn
= NULL
;
223 #define N_QUERIED_KEYS 8192
224 static uint32_t queried_id
[N_QUERIED_KEYS
];
225 static cache_key queried_keys
[N_QUERIED_KEYS
][SQUID_MD5_DIGEST_LENGTH
];
227 static Ip::Address queried_addr
[N_QUERIED_KEYS
];
228 static MemAllocator
*htcpDetailPool
= NULL
;
230 static int old_squid_format
= 0;
232 static ssize_t
htcpBuildPacket(char *buf
, size_t buflen
, htcpStuff
* stuff
);
233 static htcpSpecifier
*htcpUnpackSpecifier(char *buf
, int sz
);
234 static htcpDetail
*htcpUnpackDetail(char *buf
, int sz
);
235 static ssize_t
htcpBuildAuth(char *buf
, size_t buflen
);
236 static ssize_t
htcpBuildCountstr(char *buf
, size_t buflen
, const char *s
);
237 static ssize_t
htcpBuildData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
238 static ssize_t
htcpBuildDetail(char *buf
, size_t buflen
, htcpStuff
* stuff
);
239 static ssize_t
htcpBuildOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
240 static ssize_t
htcpBuildSpecifier(char *buf
, size_t buflen
, htcpStuff
* stuff
);
241 static ssize_t
htcpBuildTstOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
242 static void htcpFreeSpecifier(htcpSpecifier
* s
);
243 static void htcpFreeDetail(htcpDetail
* s
);
245 static void htcpHandleMsg(char *buf
, int sz
, Ip::Address
&from
);
247 static void htcpLogHtcp(Ip::Address
&, int, LogTags
, const char *);
248 static void htcpHandleMon(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
250 static void htcpHandleNop(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
252 static void htcpHandleSet(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
254 static void htcpHandleTst(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
256 static void htcpRecv(int fd
, void *data
);
258 static void htcpSend(const char *buf
, int len
, Ip::Address
&to
);
260 static void htcpTstReply(htcpDataHeader
*, StoreEntry
*, htcpSpecifier
*, Ip::Address
&);
262 static void htcpHandleTstRequest(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
264 static void htcpHandleTstResponse(htcpDataHeader
*, char *, int, Ip::Address
&);
267 htcpHexdump(const char *tag
, const char *s
, int sz
)
273 debugs(31, 3, "htcpHexdump " << tag
);
274 memset(hex
, '\0', 80);
276 for (i
= 0; i
< sz
; ++i
) {
278 snprintf(&hex
[k
* 3], 4, " %02x", (int) *(s
+ i
));
280 if (k
< 15 && i
< (sz
- 1))
283 debugs(31, 3, "\t" << hex
);
285 memset(hex
, '\0', 80);
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
)
316 if (buflen
- off
< 2)
324 debugs(31, 3, "htcpBuildCountstr: LENGTH = " << len
);
326 debugs(31, 3, "htcpBuildCountstr: TEXT = {" << (s
? s
: "<NULL>") << "}");
328 length
= htons((uint16_t) len
);
330 memcpy(buf
+ off
, &length
, 2);
334 if (buflen
- off
< len
)
338 memcpy(buf
+ off
, s
, len
);
346 htcpBuildSpecifier(char *buf
, size_t buflen
, htcpStuff
* stuff
)
350 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.method
);
357 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.uri
);
364 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.version
);
371 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.req_hdrs
);
378 debugs(31, 3, "htcpBuildSpecifier: size " << off
);
384 htcpBuildDetail(char *buf
, size_t buflen
, htcpStuff
* stuff
)
388 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.resp_hdrs
);
395 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.entity_hdrs
);
402 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.cache_hdrs
);
413 htcpBuildTstOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
418 debugs(31, 3, "htcpBuildTstOpData: RR_REQUEST");
419 return htcpBuildSpecifier(buf
, buflen
, stuff
);
422 debugs(31, 3, "htcpBuildTstOpData: RR_RESPONSE");
423 debugs(31, 3, "htcpBuildTstOpData: F1 = " << stuff
->f1
);
425 if (stuff
->f1
) /* cache miss */
428 return htcpBuildDetail(buf
, buflen
, stuff
);
431 fatal_dump("htcpBuildTstOpData: bad RR value");
438 htcpBuildClrOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
440 unsigned short reason
;
444 debugs(31, 3, "htcpBuildClrOpData: RR_REQUEST");
445 reason
= htons((unsigned short)stuff
->reason
);
446 memcpy(buf
, &reason
, 2);
447 return htcpBuildSpecifier(buf
+ 2, buflen
- 2, stuff
) + 2;
451 fatal_dump("htcpBuildClrOpData: bad RR value");
458 htcpBuildOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
461 debugs(31, 3, "htcpBuildOpData: opcode " << htcpOpcodeStr
[stuff
->op
]);
466 off
= htcpBuildTstOpData(buf
+ off
, buflen
, stuff
);
470 off
= htcpBuildClrOpData(buf
+ off
, buflen
, stuff
);
482 htcpBuildData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
486 size_t hdr_sz
= sizeof(htcpDataHeader
);
492 off
+= hdr_sz
; /* skip! */
494 op_data_sz
= htcpBuildOpData(buf
+ off
, buflen
- off
, stuff
);
501 debugs(31, 3, "htcpBuildData: hdr.length = " << off
);
503 hdr
.length
= (uint16_t) off
;
505 hdr
.opcode
= stuff
->op
;
507 hdr
.response
= stuff
->response
;
513 hdr
.msg_id
= stuff
->msg_id
;
515 /* convert multi-byte fields */
516 hdr
.length
= htons(hdr
.length
);
518 hdr
.msg_id
= htonl(hdr
.msg_id
);
520 if (!old_squid_format
) {
521 memcpy(buf
, &hdr
, hdr_sz
);
523 htcpDataHeaderSquid hdrSquid
;
524 memset(&hdrSquid
, 0, sizeof(hdrSquid
));
525 hdrSquid
.length
= hdr
.length
;
526 hdrSquid
.opcode
= hdr
.opcode
;
527 hdrSquid
.response
= hdr
.response
;
528 hdrSquid
.F1
= hdr
.F1
;
529 hdrSquid
.RR
= hdr
.RR
;
530 memcpy(buf
, &hdrSquid
, hdr_sz
);
533 debugs(31, 3, "htcpBuildData: size " << off
);
539 * Build an HTCP packet into buf, maximum length buflen.
540 * Returns the packet length, or zero on failure.
543 htcpBuildPacket(char *buf
, size_t buflen
, htcpStuff
* stuff
)
547 size_t hdr_sz
= sizeof(htcpHeader
);
549 /* skip the header -- we don't know the overall length */
551 if (buflen
< hdr_sz
) {
556 s
= htcpBuildData(buf
+ off
, buflen
- off
, stuff
);
563 s
= htcpBuildAuth(buf
+ off
, buflen
- off
);
570 hdr
.length
= htons((uint16_t) off
);
573 if (old_squid_format
)
578 memcpy(buf
, &hdr
, hdr_sz
);
580 debugs(31, 3, "htcpBuildPacket: size " << off
);
586 htcpSend(const char *buf
, int len
, Ip::Address
&to
)
588 debugs(31, 3, HERE
<< to
);
589 htcpHexdump("htcpSend", buf
, len
);
591 if (comm_udp_sendto(htcpOutgoingConn
->fd
, to
, buf
, len
) < 0)
592 debugs(31, 3, HERE
<< htcpOutgoingConn
<< " sendto: " << xstrerror());
594 ++statCounter
.htcp
.pkts_sent
;
598 * STUFF FOR RECEIVING HTCP MESSAGES
602 htcpSpecifier::setFrom(Ip::Address
&aSocket
)
608 htcpSpecifier::setDataHeader(htcpDataHeader
*aDataHeader
)
614 htcpFreeSpecifier(htcpSpecifier
* s
)
616 HTTPMSGUNLOCK(s
->request
);
622 htcpFreeDetail(htcpDetail
* d
)
624 htcpDetailPool
->freeOne(d
);
628 * Unpack an HTCP SPECIFIER in place
629 * This will overwrite any following AUTH block
631 static htcpSpecifier
*
632 htcpUnpackSpecifier(char *buf
, int sz
)
634 htcpSpecifier
*s
= new htcpSpecifier
;
635 HttpRequestMethod method
;
637 /* Find length of METHOD */
638 uint16_t l
= ntohs(*(uint16_t *) buf
);
643 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack METHOD");
644 htcpFreeSpecifier(s
);
652 debugs(31, 6, "htcpUnpackSpecifier: METHOD (" << l
<< "/" << sz
<< ") '" << s
->method
<< "'");
654 /* Find length of URI */
655 l
= ntohs(*(uint16_t *) buf
);
659 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack URI");
660 htcpFreeSpecifier(s
);
664 /* Add terminating null to METHOD */
672 debugs(31, 6, "htcpUnpackSpecifier: URI (" << l
<< "/" << sz
<< ") '" << s
->uri
<< "'");
674 /* Find length of VERSION */
675 l
= ntohs(*(uint16_t *) buf
);
679 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack VERSION");
680 htcpFreeSpecifier(s
);
684 /* Add terminating null to URI */
692 debugs(31, 6, "htcpUnpackSpecifier: VERSION (" << l
<< "/" << sz
<< ") '" << s
->version
<< "'");
694 /* Find length of REQ-HDRS */
695 l
= ntohs(*(uint16_t *) buf
);
699 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack REQ-HDRS");
700 htcpFreeSpecifier(s
);
704 /* Add terminating null to URI */
712 debugs(31, 6, "htcpUnpackSpecifier: REQ-HDRS (" << l
<< "/" << sz
<< ") '" << s
->req_hdrs
<< "'");
714 debugs(31, 3, "htcpUnpackSpecifier: " << sz
<< " bytes left");
717 * Add terminating null to REQ-HDRS. This is possible because we allocated
718 * an extra byte when we received the packet. This will overwrite any following
726 method
= HttpRequestMethod(s
->method
, NULL
);
728 s
->request
= HttpRequest::CreateFromUrlAndMethod(s
->uri
, method
== Http::METHOD_NONE
? HttpRequestMethod(Http::METHOD_GET
) : method
);
731 HTTPMSGLOCK(s
->request
);
737 * Unpack an HTCP DETAIL in place
738 * This will overwrite any following AUTH block
741 htcpUnpackDetail(char *buf
, int sz
)
743 htcpDetail
*d
= static_cast<htcpDetail
*>(htcpDetailPool
->alloc());
745 /* Find length of RESP-HDRS */
746 uint16_t l
= ntohs(*(uint16_t *) buf
);
751 debugs(31, 3, "htcpUnpackDetail: failed to unpack RESP_HDRS");
763 /* Find length of ENTITY-HDRS */
764 l
= ntohs(*(uint16_t *) buf
);
769 debugs(31, 3, "htcpUnpackDetail: failed to unpack ENTITY_HDRS");
774 /* Add terminating null to RESP-HDRS */
777 /* Set ENTITY-HDRS */
780 d
->entity_hdrs
= buf
;
786 /* Find length of CACHE-HDRS */
787 l
= ntohs(*(uint16_t *) buf
);
792 debugs(31, 3, "htcpUnpackDetail: failed to unpack CACHE_HDRS");
797 /* Add terminating null to ENTITY-HDRS */
809 debugs(31, 3, "htcpUnpackDetail: " << sz
<< " bytes left");
812 * Add terminating null to CACHE-HDRS. This is possible because we allocated
813 * an extra byte when we received the packet. This will overwrite any following
822 htcpAccessAllowed(acl_access
* acl
, htcpSpecifier
* s
, Ip::Address
&from
)
824 /* default deny if no access list present */
828 ACLFilledChecklist
checklist(acl
, s
->request
, NULL
);
829 checklist
.src_addr
= from
;
830 checklist
.my_addr
.setNoAddr();
831 return (checklist
.fastCheck() == ACCESS_ALLOWED
);
835 htcpTstReply(htcpDataHeader
* dhdr
, StoreEntry
* e
, htcpSpecifier
* spec
, Ip::Address
&from
)
838 static char pkt
[8192];
839 HttpHeader
hdr(hoHtcpReply
);
843 memset(&stuff
, '\0', sizeof(stuff
));
845 stuff
.rr
= RR_RESPONSE
;
847 stuff
.response
= e
? 0 : 1;
848 debugs(31, 3, "htcpTstReply: response = " << stuff
.response
);
849 stuff
.msg_id
= dhdr
->msg_id
;
853 packerToMemInit(&p
, &mb
);
854 stuff
.S
.method
= spec
->method
;
855 stuff
.S
.uri
= spec
->uri
;
856 stuff
.S
.version
= spec
->version
;
857 stuff
.S
.req_hdrs
= spec
->req_hdrs
;
859 hdr
.putInt(HDR_AGE
, (e
->timestamp
<= squid_curtime
? (squid_curtime
- e
->timestamp
) : 0) );
861 hdr
.putInt(HDR_AGE
, 0);
863 stuff
.D
.resp_hdrs
= xstrdup(mb
.buf
);
864 debugs(31, 3, "htcpTstReply: resp_hdrs = {" << stuff
.D
.resp_hdrs
<< "}");
868 if (e
&& e
->expires
> -1)
869 hdr
.putTime(HDR_EXPIRES
, e
->expires
);
871 if (e
&& e
->lastmod
> -1)
872 hdr
.putTime(HDR_LAST_MODIFIED
, e
->lastmod
);
876 stuff
.D
.entity_hdrs
= xstrdup(mb
.buf
);
878 debugs(31, 3, "htcpTstReply: entity_hdrs = {" << stuff
.D
.entity_hdrs
<< "}");
885 if (char *host
= urlHostname(spec
->uri
)) {
889 netdbHostData(host
, &samp
, &rtt
, &hops
);
893 snprintf(cto_buf
, 128, "%s %d %f %d",
894 host
, samp
, 0.001 * rtt
, hops
);
895 hdr
.putExt("Cache-to-Origin", cto_buf
);
898 #endif /* USE_ICMP */
901 stuff
.D
.cache_hdrs
= xstrdup(mb
.buf
);
902 debugs(31, 3, "htcpTstReply: cache_hdrs = {" << stuff
.D
.cache_hdrs
<< "}");
908 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
910 safe_free(stuff
.D
.resp_hdrs
);
911 safe_free(stuff
.D
.entity_hdrs
);
912 safe_free(stuff
.D
.cache_hdrs
);
915 debugs(31, 3, "htcpTstReply: htcpBuildPacket() failed");
919 htcpSend(pkt
, (int) pktlen
, from
);
924 htcpClrReply(htcpDataHeader
* dhdr
, int purgeSucceeded
, Ip::Address
&from
)
927 static char pkt
[8192];
930 /* If dhdr->F1 == 0, no response desired */
935 memset(&stuff
, '\0', sizeof(stuff
));
939 stuff
.rr
= RR_RESPONSE
;
943 stuff
.response
= purgeSucceeded
? 0 : 2;
945 debugs(31, 3, "htcpClrReply: response = " << stuff
.response
);
947 stuff
.msg_id
= dhdr
->msg_id
;
949 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
952 debugs(31, 3, "htcpClrReply: htcpBuildPacket() failed");
956 htcpSend(pkt
, (int) pktlen
, from
);
961 htcpHandleNop(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
963 debugs(31, 3, "htcpHandleNop: Unimplemented");
967 htcpSpecifier::checkHit()
970 checkHitRequest
= request
;
972 if (NULL
== checkHitRequest
) {
973 debugs(31, 3, "htcpCheckHit: NO; failed to parse URL");
974 checkedHit(NullStoreEntry::getInstance());
978 blk_end
= req_hdrs
+ strlen(req_hdrs
);
980 if (!checkHitRequest
->header
.parse(req_hdrs
, blk_end
)) {
981 debugs(31, 3, "htcpCheckHit: NO; failed to parse request headers");
982 delete checkHitRequest
;
983 checkHitRequest
= NULL
;
984 checkedHit(NullStoreEntry::getInstance());
988 StoreEntry::getPublicByRequest(this, checkHitRequest
);
992 htcpSpecifier::created (StoreEntry
*e
)
994 StoreEntry
*hit
=NULL
;
998 debugs(31, 3, "htcpCheckHit: NO; public object not found");
999 } else if (!e
->validToSend()) {
1000 debugs(31, 3, "htcpCheckHit: NO; entry not valid to send" );
1001 } else if (refreshCheckHTCP(e
, checkHitRequest
)) {
1002 debugs(31, 3, "htcpCheckHit: NO; cached response is stale");
1004 debugs(31, 3, "htcpCheckHit: YES!?");
1012 htcpClrStoreEntry(StoreEntry
* e
)
1014 debugs(31, 4, "htcpClrStoreEntry: Clearing store for entry: " << e
->url() );
1015 e
->releaseRequest();
1019 htcpClrStore(const htcpSpecifier
* s
)
1021 HttpRequest
*request
= s
->request
;
1023 StoreEntry
*e
= NULL
;
1026 if (request
== NULL
) {
1027 debugs(31, 3, "htcpClrStore: failed to parse URL");
1031 /* Parse request headers */
1032 blk_end
= s
->req_hdrs
+ strlen(s
->req_hdrs
);
1034 if (!request
->header
.parse(s
->req_hdrs
, blk_end
)) {
1035 debugs(31, 2, "htcpClrStore: failed to parse request headers");
1039 /* Lookup matching entries. This matches both GET and HEAD */
1040 while ((e
= storeGetPublicByRequest(request
)) != NULL
) {
1042 htcpClrStoreEntry(e
);
1048 debugs(31, 4, "htcpClrStore: Cleared " << released
<< " matching entries");
1051 debugs(31, 4, "htcpClrStore: No matching entry found");
1058 htcpHandleTst(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1060 debugs(31, 3, "htcpHandleTst: sz = " << sz
);
1062 if (hdr
->RR
== RR_REQUEST
)
1063 htcpHandleTstRequest(hdr
, buf
, sz
, from
);
1065 htcpHandleTstResponse(hdr
, buf
, sz
, from
);
1068 HtcpReplyData::HtcpReplyData() :
1069 hit(0), hdr(hoHtcpReply
), msg_id(0), version(0.0)
1071 memset(&cto
, 0, sizeof(cto
));
1076 htcpHandleTstResponse(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1078 HtcpReplyData htcpReply
;
1079 cache_key
*key
= NULL
;
1082 htcpDetail
*d
= NULL
;
1085 if (queried_id
[hdr
->msg_id
% N_QUERIED_KEYS
] != hdr
->msg_id
) {
1086 debugs(31, 2, "htcpHandleTstResponse: No matching query id '" <<
1087 hdr
->msg_id
<< "' (expected " <<
1088 queried_id
[hdr
->msg_id
% N_QUERIED_KEYS
] << ") from '" <<
1094 key
= queried_keys
[hdr
->msg_id
% N_QUERIED_KEYS
];
1097 debugs(31, 3, "htcpHandleTstResponse: No query key for response id '" << hdr
->msg_id
<< "' from '" << from
<< "'");
1101 peer
= &queried_addr
[hdr
->msg_id
% N_QUERIED_KEYS
];
1103 if ( *peer
!= from
|| peer
->port() != from
.port() ) {
1104 debugs(31, 3, "htcpHandleTstResponse: Unexpected response source " << from
);
1109 debugs(31, 2, "htcpHandleTstResponse: error condition, F1/MO == 1");
1113 htcpReply
.msg_id
= hdr
->msg_id
;
1114 debugs(31, 3, "htcpHandleTstResponse: msg_id = " << htcpReply
.msg_id
);
1115 htcpReply
.hit
= hdr
->response
? 0 : 1;
1118 debugs(31, 3, "htcpHandleTstResponse: MISS");
1120 debugs(31, 3, "htcpHandleTstResponse: HIT");
1121 d
= htcpUnpackDetail(buf
, sz
);
1124 debugs(31, 3, "htcpHandleTstResponse: bad DETAIL");
1128 if ((t
= d
->resp_hdrs
))
1129 htcpReply
.hdr
.parse(t
, t
+ strlen(t
));
1131 if ((t
= d
->entity_hdrs
))
1132 htcpReply
.hdr
.parse(t
, t
+ strlen(t
));
1134 if ((t
= d
->cache_hdrs
))
1135 htcpReply
.hdr
.parse(t
, t
+ strlen(t
));
1138 debugs(31, 3, "htcpHandleTstResponse: key (" << key
<< ") " << storeKeyText(key
));
1139 neighborsHtcpReply(key
, &htcpReply
, from
);
1140 htcpReply
.hdr
.clean();
1147 htcpHandleTstRequest(htcpDataHeader
* dhdr
, char *buf
, int sz
, Ip::Address
&from
)
1149 /* buf should be a SPECIFIER */
1153 debugs(31, 3, "htcpHandleTst: nothing to do");
1160 /* s is a new object */
1161 s
= htcpUnpackSpecifier(buf
, sz
);
1164 debugs(31, 3, "htcpHandleTstRequest: htcpUnpackSpecifier failed");
1165 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1169 s
->setDataHeader(dhdr
);
1173 debugs(31, 3, "htcpHandleTstRequest: failed to parse request");
1174 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1175 htcpFreeSpecifier(s
);
1179 if (!htcpAccessAllowed(Config
.accessList
.htcp
, s
, from
)) {
1180 debugs(31, 3, "htcpHandleTstRequest: Access denied");
1181 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_DENIED
, s
->uri
);
1182 htcpFreeSpecifier(s
);
1186 debugs(31, 2, "HTCP TST request: " << s
->method
<< " " << s
->uri
<< " " << s
->version
);
1187 debugs(31, 2, "HTCP TST headers: " << s
->req_hdrs
);
1192 htcpSpecifier::checkedHit(StoreEntry
*e
)
1195 htcpTstReply(dhdr
, e
, this, from
); /* hit */
1196 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_HIT
, uri
);
1198 htcpTstReply(dhdr
, NULL
, NULL
, from
); /* cache miss */
1199 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_MISS
, uri
);
1202 htcpFreeSpecifier(this);
1207 htcpHandleMon(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1209 debugs(31, 3, "htcpHandleMon: Unimplemented");
1214 htcpHandleSet(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1216 debugs(31, 3, "htcpHandleSet: Unimplemented");
1220 htcpHandleClr(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1223 /* buf[0/1] is reserved and reason */
1224 int reason
= buf
[1] << 4;
1225 debugs(31, 2, "HTCP CLR reason: " << reason
);
1229 /* buf should be a SPECIFIER */
1232 debugs(31, 4, "htcpHandleClr: nothing to do");
1233 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1237 s
= htcpUnpackSpecifier(buf
, sz
);
1240 debugs(31, 3, "htcpHandleClr: htcpUnpackSpecifier failed");
1241 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1246 debugs(31, 3, "htcpHandleTstRequest: failed to parse request");
1247 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1248 htcpFreeSpecifier(s
);
1252 if (!htcpAccessAllowed(Config
.accessList
.htcp_clr
, s
, from
)) {
1253 debugs(31, 3, "htcpHandleClr: Access denied");
1254 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_DENIED
, s
->uri
);
1255 htcpFreeSpecifier(s
);
1259 debugs(31, 2, "HTCP CLR request: " << s
->method
<< " " << s
->uri
<< " " << s
->version
);
1260 debugs(31, 2, "HTCP CLR headers: " << s
->req_hdrs
);
1262 /* Release objects from cache
1263 * analog to clientPurgeRequest in client_side.c
1266 switch (htcpClrStore(s
)) {
1269 htcpClrReply(hdr
, 1, from
); /* hit */
1270 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_HIT
, s
->uri
);
1274 htcpClrReply(hdr
, 0, from
); /* miss */
1275 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_MISS
, s
->uri
);
1282 htcpFreeSpecifier(s
);
1286 * Forward a CLR request to all peers who have requested that CLRs be
1287 * forwarded to them.
1290 htcpForwardClr(char *buf
, int sz
)
1294 for (p
= Config
.peers
; p
; p
= p
->next
) {
1295 if (!p
->options
.htcp
) {
1298 if (!p
->options
.htcp_forward_clr
) {
1302 htcpSend(buf
, sz
, p
->in_addr
);
1307 * Do the first pass of handling an HTCP message. This used to be two
1308 * separate functions, htcpHandle and htcpHandleData. They were merged to
1309 * allow for forwarding HTCP packets easily to other peers if desired.
1311 * This function now works out what type of message we have received and then
1312 * hands it off to other functions to break apart message-specific data.
1315 htcpHandleMsg(char *buf
, int sz
, Ip::Address
&from
)
1322 if (sz
< 0 || (size_t)sz
< sizeof(htcpHeader
)) {
1323 // These are highly likely to be attack packets. Should probably get a bigger warning.
1324 debugs(31, 2, "htcpHandle: msg size less than htcpHeader size from " << from
);
1328 htcpHexdump("htcpHandle", buf
, sz
);
1329 memcpy(&htcpHdr
, buf
, sizeof(htcpHeader
));
1330 htcpHdr
.length
= ntohs(htcpHdr
.length
);
1332 if (htcpHdr
.minor
== 0)
1333 old_squid_format
= 1;
1335 old_squid_format
= 0;
1337 debugs(31, 3, "htcpHandle: htcpHdr.length = " << htcpHdr
.length
);
1338 debugs(31, 3, "htcpHandle: htcpHdr.major = " << htcpHdr
.major
);
1339 debugs(31, 3, "htcpHandle: htcpHdr.minor = " << htcpHdr
.minor
);
1341 if (sz
!= htcpHdr
.length
) {
1342 debugs(31, 3, "htcpHandle: sz/" << sz
<< " != htcpHdr.length/" <<
1343 htcpHdr
.length
<< " from " << from
);
1348 if (htcpHdr
.major
!= 0) {
1349 debugs(31, 3, "htcpHandle: Unknown major version " << htcpHdr
.major
<< " from " << from
);
1354 hbuf
= buf
+ sizeof(htcpHeader
);
1355 hsz
= sz
- sizeof(htcpHeader
);
1357 if ((size_t)hsz
< sizeof(htcpDataHeader
)) {
1358 debugs(31, 3, "htcpHandleData: msg size less than htcpDataHeader size");
1362 if (!old_squid_format
) {
1363 memcpy(&hdr
, hbuf
, sizeof(hdr
));
1365 htcpDataHeaderSquid hdrSquid
;
1366 memcpy(&hdrSquid
, hbuf
, sizeof(hdrSquid
));
1367 hdr
.length
= hdrSquid
.length
;
1368 hdr
.opcode
= hdrSquid
.opcode
;
1369 hdr
.response
= hdrSquid
.response
;
1370 hdr
.F1
= hdrSquid
.F1
;
1371 hdr
.RR
= hdrSquid
.RR
;
1373 hdr
.msg_id
= hdrSquid
.msg_id
;
1376 hdr
.length
= ntohs(hdr
.length
);
1377 hdr
.msg_id
= ntohl(hdr
.msg_id
);
1378 debugs(31, 3, "htcpHandleData: hsz = " << hsz
);
1379 debugs(31, 3, "htcpHandleData: length = " << hdr
.length
);
1381 if (hdr
.opcode
>= HTCP_END
) {
1382 debugs(31, 3, "htcpHandleData: client " << from
<< ", opcode " << hdr
.opcode
<< " out of range");
1386 debugs(31, 3, "htcpHandleData: opcode = " << hdr
.opcode
<< " " << htcpOpcodeStr
[hdr
.opcode
]);
1387 debugs(31, 3, "htcpHandleData: response = " << hdr
.response
);
1388 debugs(31, 3, "htcpHandleData: F1 = " << hdr
.F1
);
1389 debugs(31, 3, "htcpHandleData: RR = " << hdr
.RR
);
1390 debugs(31, 3, "htcpHandleData: msg_id = " << hdr
.msg_id
);
1392 if (hsz
< hdr
.length
) {
1393 debugs(31, 3, "htcpHandleData: sz < hdr.length");
1398 * set sz = hdr.length so we ignore any AUTH fields following
1401 hsz
= (int) hdr
.length
;
1402 hbuf
+= sizeof(htcpDataHeader
);
1403 hsz
-= sizeof(htcpDataHeader
);
1404 debugs(31, 3, "htcpHandleData: hsz = " << hsz
);
1406 htcpHexdump("htcpHandleData", hbuf
, hsz
);
1408 switch (hdr
.opcode
) {
1410 htcpHandleNop(&hdr
, hbuf
, hsz
, from
);
1413 htcpHandleTst(&hdr
, hbuf
, hsz
, from
);
1416 htcpHandleMon(&hdr
, hbuf
, hsz
, from
);
1419 htcpHandleSet(&hdr
, hbuf
, hsz
, from
);
1422 htcpHandleClr(&hdr
, hbuf
, hsz
, from
);
1423 htcpForwardClr(buf
, sz
);
1431 htcpRecv(int fd
, void *data
)
1433 static char buf
[8192];
1435 static Ip::Address from
;
1437 /* Receive up to 8191 bytes, leaving room for a null */
1439 len
= comm_udp_recvfrom(fd
, buf
, sizeof(buf
) - 1, 0, from
);
1441 debugs(31, 3, "htcpRecv: FD " << fd
<< ", " << len
<< " bytes from " << from
);
1444 ++statCounter
.htcp
.pkts_recv
;
1446 htcpHandleMsg(buf
, len
, from
);
1448 Comm::SetSelect(fd
, COMM_SELECT_READ
, htcpRecv
, NULL
, 0);
1452 * ======================================================================
1454 * ======================================================================
1460 if (Config
.Port
.htcp
<= 0) {
1461 debugs(31, DBG_IMPORTANT
, "HTCP Disabled.");
1465 htcpIncomingConn
= new Comm::Connection
;
1466 htcpIncomingConn
->local
= Config
.Addrs
.udp_incoming
;
1467 htcpIncomingConn
->local
.port(Config
.Port
.htcp
);
1469 if (!Ip::EnableIpv6
&& !htcpIncomingConn
->local
.setIPv4()) {
1470 debugs(31, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << htcpIncomingConn
->local
<< " is not an IPv4 address.");
1471 fatal("HTCP port cannot be opened.");
1473 /* split-stack for now requires default IPv4-only HTCP */
1474 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& htcpIncomingConn
->local
.isAnyAddr()) {
1475 htcpIncomingConn
->local
.setIPv4();
1478 AsyncCall::Pointer call
= asyncCall(31, 2,
1479 "htcpIncomingConnectionOpened",
1480 Comm::UdpOpenDialer(&htcpIncomingConnectionOpened
));
1482 Ipc::StartListening(SOCK_DGRAM
,
1485 Ipc::fdnInHtcpSocket
, call
);
1487 if (!Config
.Addrs
.udp_outgoing
.isNoAddr()) {
1488 htcpOutgoingConn
= new Comm::Connection
;
1489 htcpOutgoingConn
->local
= Config
.Addrs
.udp_outgoing
;
1490 htcpOutgoingConn
->local
.port(Config
.Port
.htcp
);
1492 if (!Ip::EnableIpv6
&& !htcpOutgoingConn
->local
.setIPv4()) {
1493 debugs(31, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << htcpOutgoingConn
->local
<< " is not an IPv4 address.");
1494 fatal("HTCP port cannot be opened.");
1496 /* split-stack for now requires default IPv4-only HTCP */
1497 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& htcpOutgoingConn
->local
.isAnyAddr()) {
1498 htcpOutgoingConn
->local
.setIPv4();
1502 comm_open_listener(SOCK_DGRAM
, IPPROTO_UDP
, htcpOutgoingConn
, "Outgoing HTCP Socket");
1505 if (!Comm::IsConnOpen(htcpOutgoingConn
))
1506 fatal("Cannot open Outgoing HTCP Socket");
1508 Comm::SetSelect(htcpOutgoingConn
->fd
, COMM_SELECT_READ
, htcpRecv
, NULL
, 0);
1510 debugs(31, DBG_IMPORTANT
, "Sending HTCP messages from " << htcpOutgoingConn
->local
);
1513 if (!htcpDetailPool
) {
1514 htcpDetailPool
= memPoolCreate("htcpDetail", sizeof(htcpDetail
));
1519 htcpIncomingConnectionOpened(const Comm::ConnectionPointer
&conn
, int)
1521 if (!Comm::IsConnOpen(conn
))
1522 fatal("Cannot open HTCP Socket");
1524 Comm::SetSelect(conn
->fd
, COMM_SELECT_READ
, htcpRecv
, NULL
, 0);
1526 debugs(31, DBG_CRITICAL
, "Accepting HTCP messages on " << conn
->local
);
1528 if (Config
.Addrs
.udp_outgoing
.isNoAddr()) {
1529 htcpOutgoingConn
= conn
;
1530 debugs(31, DBG_IMPORTANT
, "Sending HTCP messages from " << htcpOutgoingConn
->local
);
1535 htcpQuery(StoreEntry
* e
, HttpRequest
* req
, CachePeer
* p
)
1537 cache_key
*save_key
;
1538 static char pkt
[8192];
1542 HttpHeader
hdr(hoRequest
);
1545 HttpStateFlags flags
;
1547 if (!Comm::IsConnOpen(htcpIncomingConn
))
1550 old_squid_format
= p
->options
.htcp_oldsquid
;
1551 memset(&flags
, '\0', sizeof(flags
));
1552 snprintf(vbuf
, sizeof(vbuf
), "%d/%d",
1553 req
->http_ver
.major
, req
->http_ver
.minor
);
1554 stuff
.op
= HTCP_TST
;
1555 stuff
.rr
= RR_REQUEST
;
1558 stuff
.msg_id
= ++msg_id_counter
;
1559 SBuf sb
= req
->method
.image();
1560 stuff
.S
.method
= sb
.c_str();
1561 stuff
.S
.uri
= (char *) e
->url();
1562 stuff
.S
.version
= vbuf
;
1563 HttpStateData::httpBuildRequestHeader(req
, e
, NULL
, &hdr
, flags
);
1565 packerToMemInit(&pa
, &mb
);
1569 stuff
.S
.req_hdrs
= mb
.buf
;
1570 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
1573 debugs(31, 3, "htcpQuery: htcpBuildPacket() failed");
1577 htcpSend(pkt
, (int) pktlen
, p
->in_addr
);
1579 queried_id
[stuff
.msg_id
% N_QUERIED_KEYS
] = stuff
.msg_id
;
1580 save_key
= queried_keys
[stuff
.msg_id
% N_QUERIED_KEYS
];
1581 storeKeyCopy(save_key
, (const cache_key
*)e
->key
);
1582 queried_addr
[stuff
.msg_id
% N_QUERIED_KEYS
] = p
->in_addr
;
1583 debugs(31, 3, "htcpQuery: key (" << save_key
<< ") " << storeKeyText(save_key
));
1589 * Send an HTCP CLR message for a specified item to a given CachePeer.
1592 htcpClear(StoreEntry
* e
, const char *uri
, HttpRequest
* req
, const HttpRequestMethod
&method
, CachePeer
* p
, htcp_clr_reason reason
)
1594 static char pkt
[8192];
1598 HttpHeader
hdr(hoRequest
);
1601 HttpStateFlags flags
;
1603 if (!Comm::IsConnOpen(htcpIncomingConn
))
1606 old_squid_format
= p
->options
.htcp_oldsquid
;
1607 memset(&flags
, '\0', sizeof(flags
));
1608 snprintf(vbuf
, sizeof(vbuf
), "%d/%d",
1609 req
->http_ver
.major
, req
->http_ver
.minor
);
1610 stuff
.op
= HTCP_CLR
;
1611 stuff
.rr
= RR_REQUEST
;
1614 stuff
.msg_id
= ++msg_id_counter
;
1616 case HTCP_CLR_INVALIDATION
:
1623 SBuf sb
= req
->method
.image();
1624 stuff
.S
.method
= sb
.c_str();
1625 if (e
== NULL
|| e
->mem_obj
== NULL
) {
1629 stuff
.S
.uri
= xstrdup(uri
);
1631 stuff
.S
.uri
= (char *) e
->url();
1633 stuff
.S
.version
= vbuf
;
1634 if (reason
!= HTCP_CLR_INVALIDATION
) {
1635 HttpStateData::httpBuildRequestHeader(req
, e
, NULL
, &hdr
, flags
);
1637 packerToMemInit(&pa
, &mb
);
1641 stuff
.S
.req_hdrs
= mb
.buf
;
1643 stuff
.S
.req_hdrs
= NULL
;
1645 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
1646 if (reason
!= HTCP_CLR_INVALIDATION
) {
1653 debugs(31, 3, "htcpClear: htcpBuildPacket() failed");
1657 htcpSend(pkt
, (int) pktlen
, p
->in_addr
);
1661 * htcpSocketShutdown only closes the 'in' socket if it is
1662 * different than the 'out' socket.
1665 htcpSocketShutdown(void)
1667 if (!Comm::IsConnOpen(htcpIncomingConn
))
1670 debugs(12, DBG_IMPORTANT
, "Stop accepting HTCP on " << htcpIncomingConn
->local
);
1672 * Here we just unlink htcpIncomingConn because the HTCP 'in'
1673 * and 'out' sockets might be just one FD. This prevents this
1674 * function from executing repeatedly. When we are really ready to
1675 * exit or restart, main will comm_close the 'out' descriptor.
1677 htcpIncomingConn
= NULL
;
1680 * Normally we only write to the outgoing HTCP socket, but
1681 * we also have a read handler there to catch messages sent
1682 * to that specific interface. During shutdown, we must
1683 * disable reading on the outgoing socket.
1685 /* XXX Don't we need this handler to read replies while shutting down?
1686 * I think there should be a separate hander for reading replies..
1688 assert(Comm::IsConnOpen(htcpOutgoingConn
));
1690 Comm::SetSelect(htcpOutgoingConn
->fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
1694 htcpClosePorts(void)
1696 htcpSocketShutdown();
1698 if (htcpOutgoingConn
!= NULL
) {
1699 debugs(12, DBG_IMPORTANT
, "Stop sending HTCP from " << htcpOutgoingConn
->local
);
1700 htcpOutgoingConn
= NULL
;
1705 htcpLogHtcp(Ip::Address
&caddr
, int opcode
, LogTags logcode
, const char *url
)
1707 AccessLogEntry::Pointer al
= new AccessLogEntry
;
1708 if (LOG_TAG_NONE
== logcode
)
1710 if (!Config
.onoff
.log_udp
)
1712 al
->htcp
.opcode
= htcpOpcodeStr
[opcode
];
1714 al
->cache
.caddr
= caddr
;
1715 al
->cache
.code
= logcode
;
1717 accessLogLog(al
, NULL
);