3 * DEBUG: section 31 Hypertext Caching Protocol
4 * AUTHOR: Duane Wesssels
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 #include "AccessLogEntry.h"
37 #include "acl/FilledChecklist.h"
38 #include "CachePeer.h"
40 #include "comm/Connection.h"
41 #include "comm/Loops.h"
42 #include "comm/UdpOpenDialer.h"
43 #include "compat/xalloc.h"
47 #include "HttpRequest.h"
48 #include "HttpStateFlags.h"
49 #include "icmp/net_db.h"
54 #include "SquidConfig.h"
55 #include "SquidTime.h"
56 #include "StatCounters.h"
57 #include "store_key_md5.h"
59 #include "StoreClient.h"
63 typedef struct _Countstr Countstr
;
65 typedef struct _htcpHeader htcpHeader
;
67 typedef struct _htcpDataHeader htcpDataHeader
;
69 typedef struct _htcpDataHeaderSquid htcpDataHeaderSquid
;
71 typedef struct _htcpAuthHeader htcpAuthHeader
;
73 typedef struct _htcpStuff htcpStuff
;
75 typedef struct _htcpDetail htcpDetail
;
88 struct _htcpDataHeaderSquid
{
92 unsigned int opcode
:4;
93 unsigned int response
:4;
95 unsigned int response
:4;
96 unsigned int opcode
:4;
100 unsigned int reserved
:6;
106 unsigned int reserved
:6;
112 struct _htcpDataHeader
{
146 /* RR == 0 --> F1 = RESPONSE DESIRED FLAG */
147 /* RR == 1 --> F1 = MESSAGE OVERALL FLAG */
148 /* RR == 0 --> REQUEST */
149 /* RR == 1 --> RESPONSE */
151 struct _htcpAuthHeader
{
159 class htcpSpecifier
: public StoreClient
163 MEMPROXY_CLASS(htcpSpecifier
);
165 void created (StoreEntry
*newEntry
);
167 void checkedHit(StoreEntry
*e
);
169 void setFrom(Ip::Address
&from
);
170 void setDataHeader(htcpDataHeader
*);
175 HttpRequest
*request
;
178 HttpRequest
*checkHitRequest
;
180 Ip::Address from
; // was a ptr. return to such IFF needed. otherwise copy should do.
181 htcpDataHeader
*dhdr
;
184 MEMPROXY_CLASS_INLINE(htcpSpecifier
);
212 static const char *const htcpOpcodeStr
[] = {
222 * values for htcpDataHeader->response
227 OPCODE_UNIMPLEMENTED
,
228 MAJOR_VERSION_UNSUPPORTED
,
229 MINOR_VERSION_UNSUPPORTED
,
234 * values for htcpDataHeader->RR
241 static void htcpIncomingConnectionOpened(const Comm::ConnectionPointer
&conn
, int errNo
);
242 static uint32_t msg_id_counter
= 0;
244 static Comm::ConnectionPointer htcpOutgoingConn
= NULL
;
245 static Comm::ConnectionPointer htcpIncomingConn
= NULL
;
246 #define N_QUERIED_KEYS 8192
247 static uint32_t queried_id
[N_QUERIED_KEYS
];
248 static cache_key queried_keys
[N_QUERIED_KEYS
][SQUID_MD5_DIGEST_LENGTH
];
250 static Ip::Address queried_addr
[N_QUERIED_KEYS
];
251 static MemAllocator
*htcpDetailPool
= NULL
;
253 static int old_squid_format
= 0;
255 static ssize_t
htcpBuildPacket(char *buf
, size_t buflen
, htcpStuff
* stuff
);
256 static htcpSpecifier
*htcpUnpackSpecifier(char *buf
, int sz
);
257 static htcpDetail
*htcpUnpackDetail(char *buf
, int sz
);
258 static ssize_t
htcpBuildAuth(char *buf
, size_t buflen
);
259 static ssize_t
htcpBuildCountstr(char *buf
, size_t buflen
, const char *s
);
260 static ssize_t
htcpBuildData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
261 static ssize_t
htcpBuildDetail(char *buf
, size_t buflen
, htcpStuff
* stuff
);
262 static ssize_t
htcpBuildOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
263 static ssize_t
htcpBuildSpecifier(char *buf
, size_t buflen
, htcpStuff
* stuff
);
264 static ssize_t
htcpBuildTstOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
265 static void htcpFreeSpecifier(htcpSpecifier
* s
);
266 static void htcpFreeDetail(htcpDetail
* s
);
268 static void htcpHandleMsg(char *buf
, int sz
, Ip::Address
&from
);
270 static void htcpLogHtcp(Ip::Address
&, int, LogTags
, const char *);
271 static void htcpHandleMon(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
273 static void htcpHandleNop(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
275 static void htcpHandleSet(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
277 static void htcpHandleTst(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
279 static void htcpRecv(int fd
, void *data
);
281 static void htcpSend(const char *buf
, int len
, Ip::Address
&to
);
283 static void htcpTstReply(htcpDataHeader
*, StoreEntry
*, htcpSpecifier
*, Ip::Address
&);
285 static void htcpHandleTstRequest(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
287 static void htcpHandleTstResponse(htcpDataHeader
*, char *, int, Ip::Address
&);
290 htcpHexdump(const char *tag
, const char *s
, int sz
)
296 debugs(31, 3, "htcpHexdump " << tag
);
297 memset(hex
, '\0', 80);
299 for (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', 80);
315 * STUFF FOR SENDING HTCP MESSAGES
319 htcpBuildAuth(char *buf
, size_t buflen
)
323 assert(2 == sizeof(uint16_t));
324 auth
.length
= htons(2);
326 if (buflen
< copy_sz
)
328 memcpy(buf
, &auth
, copy_sz
);
333 htcpBuildCountstr(char *buf
, size_t buflen
, const char *s
)
339 if (buflen
- off
< 2)
347 debugs(31, 3, "htcpBuildCountstr: LENGTH = " << len
);
349 debugs(31, 3, "htcpBuildCountstr: TEXT = {" << (s
? s
: "<NULL>") << "}");
351 length
= htons((uint16_t) len
);
353 memcpy(buf
+ off
, &length
, 2);
357 if (buflen
- off
< len
)
361 memcpy(buf
+ off
, s
, len
);
369 htcpBuildSpecifier(char *buf
, size_t buflen
, htcpStuff
* stuff
)
373 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.method
);
380 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.uri
);
387 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.version
);
394 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.req_hdrs
);
401 debugs(31, 3, "htcpBuildSpecifier: size " << off
);
407 htcpBuildDetail(char *buf
, size_t buflen
, htcpStuff
* stuff
)
411 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.resp_hdrs
);
418 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.entity_hdrs
);
425 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.cache_hdrs
);
436 htcpBuildTstOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
441 debugs(31, 3, "htcpBuildTstOpData: RR_REQUEST");
442 return htcpBuildSpecifier(buf
, buflen
, stuff
);
445 debugs(31, 3, "htcpBuildTstOpData: RR_RESPONSE");
446 debugs(31, 3, "htcpBuildTstOpData: F1 = " << stuff
->f1
);
448 if (stuff
->f1
) /* cache miss */
451 return htcpBuildDetail(buf
, buflen
, stuff
);
454 fatal_dump("htcpBuildTstOpData: bad RR value");
461 htcpBuildClrOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
463 unsigned short reason
;
467 debugs(31, 3, "htcpBuildClrOpData: RR_REQUEST");
468 reason
= htons((unsigned short)stuff
->reason
);
469 memcpy(buf
, &reason
, 2);
470 return htcpBuildSpecifier(buf
+ 2, buflen
- 2, stuff
) + 2;
474 fatal_dump("htcpBuildClrOpData: bad RR value");
481 htcpBuildOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
484 debugs(31, 3, "htcpBuildOpData: opcode " << htcpOpcodeStr
[stuff
->op
]);
489 off
= htcpBuildTstOpData(buf
+ off
, buflen
, stuff
);
493 off
= htcpBuildClrOpData(buf
+ off
, buflen
, stuff
);
505 htcpBuildData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
509 size_t hdr_sz
= sizeof(htcpDataHeader
);
515 off
+= hdr_sz
; /* skip! */
517 op_data_sz
= htcpBuildOpData(buf
+ off
, buflen
- off
, stuff
);
524 debugs(31, 3, "htcpBuildData: hdr.length = " << off
);
526 hdr
.length
= (uint16_t) off
;
528 hdr
.opcode
= stuff
->op
;
530 hdr
.response
= stuff
->response
;
536 hdr
.msg_id
= stuff
->msg_id
;
538 /* convert multi-byte fields */
539 hdr
.length
= htons(hdr
.length
);
541 hdr
.msg_id
= htonl(hdr
.msg_id
);
543 if (!old_squid_format
) {
544 memcpy(buf
, &hdr
, hdr_sz
);
546 htcpDataHeaderSquid hdrSquid
;
547 memset(&hdrSquid
, 0, sizeof(hdrSquid
));
548 hdrSquid
.length
= hdr
.length
;
549 hdrSquid
.opcode
= hdr
.opcode
;
550 hdrSquid
.response
= hdr
.response
;
551 hdrSquid
.F1
= hdr
.F1
;
552 hdrSquid
.RR
= hdr
.RR
;
553 memcpy(buf
, &hdrSquid
, hdr_sz
);
556 debugs(31, 3, "htcpBuildData: size " << off
);
562 * Build an HTCP packet into buf, maximum length buflen.
563 * Returns the packet length, or zero on failure.
566 htcpBuildPacket(char *buf
, size_t buflen
, htcpStuff
* stuff
)
570 size_t hdr_sz
= sizeof(htcpHeader
);
572 /* skip the header -- we don't know the overall length */
574 if (buflen
< hdr_sz
) {
579 s
= htcpBuildData(buf
+ off
, buflen
- off
, stuff
);
586 s
= htcpBuildAuth(buf
+ off
, buflen
- off
);
593 hdr
.length
= htons((uint16_t) off
);
596 if (old_squid_format
)
601 memcpy(buf
, &hdr
, hdr_sz
);
603 debugs(31, 3, "htcpBuildPacket: size " << off
);
609 htcpSend(const char *buf
, int len
, Ip::Address
&to
)
611 debugs(31, 3, HERE
<< to
);
612 htcpHexdump("htcpSend", buf
, len
);
614 if (comm_udp_sendto(htcpOutgoingConn
->fd
, to
, buf
, len
) < 0)
615 debugs(31, 3, HERE
<< htcpOutgoingConn
<< " sendto: " << xstrerror());
617 ++statCounter
.htcp
.pkts_sent
;
621 * STUFF FOR RECEIVING HTCP MESSAGES
625 htcpSpecifier::setFrom(Ip::Address
&aSocket
)
631 htcpSpecifier::setDataHeader(htcpDataHeader
*aDataHeader
)
637 htcpFreeSpecifier(htcpSpecifier
* s
)
639 HTTPMSGUNLOCK(s
->request
);
645 htcpFreeDetail(htcpDetail
* d
)
647 htcpDetailPool
->freeOne(d
);
651 * Unpack an HTCP SPECIFIER in place
652 * This will overwrite any following AUTH block
654 static htcpSpecifier
*
655 htcpUnpackSpecifier(char *buf
, int sz
)
657 htcpSpecifier
*s
= new htcpSpecifier
;
658 HttpRequestMethod method
;
660 /* Find length of METHOD */
661 uint16_t l
= ntohs(*(uint16_t *) buf
);
666 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack METHOD");
667 htcpFreeSpecifier(s
);
675 debugs(31, 6, "htcpUnpackSpecifier: METHOD (" << l
<< "/" << sz
<< ") '" << s
->method
<< "'");
677 /* Find length of URI */
678 l
= ntohs(*(uint16_t *) buf
);
682 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack URI");
683 htcpFreeSpecifier(s
);
687 /* Add terminating null to METHOD */
695 debugs(31, 6, "htcpUnpackSpecifier: URI (" << l
<< "/" << sz
<< ") '" << s
->uri
<< "'");
697 /* Find length of VERSION */
698 l
= ntohs(*(uint16_t *) buf
);
702 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack VERSION");
703 htcpFreeSpecifier(s
);
707 /* Add terminating null to URI */
715 debugs(31, 6, "htcpUnpackSpecifier: VERSION (" << l
<< "/" << sz
<< ") '" << s
->version
<< "'");
717 /* Find length of REQ-HDRS */
718 l
= ntohs(*(uint16_t *) buf
);
722 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack REQ-HDRS");
723 htcpFreeSpecifier(s
);
727 /* Add terminating null to URI */
735 debugs(31, 6, "htcpUnpackSpecifier: REQ-HDRS (" << l
<< "/" << sz
<< ") '" << s
->req_hdrs
<< "'");
737 debugs(31, 3, "htcpUnpackSpecifier: " << sz
<< " bytes left");
740 * Add terminating null to REQ-HDRS. This is possible because we allocated
741 * an extra byte when we received the packet. This will overwrite any following
749 method
= HttpRequestMethod(s
->method
, NULL
);
751 s
->request
= HttpRequest::CreateFromUrlAndMethod(s
->uri
, method
== Http::METHOD_NONE
? HttpRequestMethod(Http::METHOD_GET
) : method
);
754 HTTPMSGLOCK(s
->request
);
760 * Unpack an HTCP DETAIL in place
761 * This will overwrite any following AUTH block
764 htcpUnpackDetail(char *buf
, int sz
)
766 htcpDetail
*d
= static_cast<htcpDetail
*>(htcpDetailPool
->alloc());
768 /* Find length of RESP-HDRS */
769 uint16_t l
= ntohs(*(uint16_t *) buf
);
774 debugs(31, 3, "htcpUnpackDetail: failed to unpack RESP_HDRS");
786 /* Find length of ENTITY-HDRS */
787 l
= ntohs(*(uint16_t *) buf
);
792 debugs(31, 3, "htcpUnpackDetail: failed to unpack ENTITY_HDRS");
797 /* Add terminating null to RESP-HDRS */
800 /* Set ENTITY-HDRS */
803 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 */
832 debugs(31, 3, "htcpUnpackDetail: " << sz
<< " bytes left");
835 * Add terminating null to CACHE-HDRS. This is possible because we allocated
836 * an extra byte when we received the packet. This will overwrite any following
845 htcpAccessAllowed(acl_access
* acl
, htcpSpecifier
* s
, Ip::Address
&from
)
847 /* default deny if no access list present */
851 ACLFilledChecklist
checklist(acl
, s
->request
, NULL
);
852 checklist
.src_addr
= from
;
853 checklist
.my_addr
.setNoAddr();
854 return (checklist
.fastCheck() == ACCESS_ALLOWED
);
858 htcpTstReply(htcpDataHeader
* dhdr
, StoreEntry
* e
, htcpSpecifier
* spec
, Ip::Address
&from
)
861 static char pkt
[8192];
862 HttpHeader
hdr(hoHtcpReply
);
866 memset(&stuff
, '\0', sizeof(stuff
));
868 stuff
.rr
= RR_RESPONSE
;
870 stuff
.response
= e
? 0 : 1;
871 debugs(31, 3, "htcpTstReply: response = " << stuff
.response
);
872 stuff
.msg_id
= dhdr
->msg_id
;
876 packerToMemInit(&p
, &mb
);
877 stuff
.S
.method
= spec
->method
;
878 stuff
.S
.uri
= spec
->uri
;
879 stuff
.S
.version
= spec
->version
;
880 stuff
.S
.req_hdrs
= spec
->req_hdrs
;
882 hdr
.putInt(HDR_AGE
, (e
->timestamp
<= squid_curtime
? (squid_curtime
- e
->timestamp
) : 0) );
884 hdr
.putInt(HDR_AGE
, 0);
886 stuff
.D
.resp_hdrs
= xstrdup(mb
.buf
);
887 debugs(31, 3, "htcpTstReply: resp_hdrs = {" << stuff
.D
.resp_hdrs
<< "}");
891 if (e
&& e
->expires
> -1)
892 hdr
.putTime(HDR_EXPIRES
, e
->expires
);
894 if (e
&& e
->lastmod
> -1)
895 hdr
.putTime(HDR_LAST_MODIFIED
, e
->lastmod
);
899 stuff
.D
.entity_hdrs
= xstrdup(mb
.buf
);
901 debugs(31, 3, "htcpTstReply: entity_hdrs = {" << stuff
.D
.entity_hdrs
<< "}");
908 if (char *host
= urlHostname(spec
->uri
)) {
912 netdbHostData(host
, &samp
, &rtt
, &hops
);
916 snprintf(cto_buf
, 128, "%s %d %f %d",
917 host
, samp
, 0.001 * rtt
, hops
);
918 hdr
.putExt("Cache-to-Origin", cto_buf
);
921 #endif /* USE_ICMP */
924 stuff
.D
.cache_hdrs
= xstrdup(mb
.buf
);
925 debugs(31, 3, "htcpTstReply: cache_hdrs = {" << stuff
.D
.cache_hdrs
<< "}");
931 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
933 safe_free(stuff
.D
.resp_hdrs
);
934 safe_free(stuff
.D
.entity_hdrs
);
935 safe_free(stuff
.D
.cache_hdrs
);
938 debugs(31, 3, "htcpTstReply: htcpBuildPacket() failed");
942 htcpSend(pkt
, (int) pktlen
, from
);
947 htcpClrReply(htcpDataHeader
* dhdr
, int purgeSucceeded
, Ip::Address
&from
)
950 static char pkt
[8192];
953 /* If dhdr->F1 == 0, no response desired */
958 memset(&stuff
, '\0', sizeof(stuff
));
962 stuff
.rr
= RR_RESPONSE
;
966 stuff
.response
= purgeSucceeded
? 0 : 2;
968 debugs(31, 3, "htcpClrReply: response = " << stuff
.response
);
970 stuff
.msg_id
= dhdr
->msg_id
;
972 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
975 debugs(31, 3, "htcpClrReply: htcpBuildPacket() failed");
979 htcpSend(pkt
, (int) pktlen
, from
);
984 htcpHandleNop(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
986 debugs(31, 3, "htcpHandleNop: Unimplemented");
990 htcpSpecifier::checkHit()
993 checkHitRequest
= request
;
995 if (NULL
== checkHitRequest
) {
996 debugs(31, 3, "htcpCheckHit: NO; failed to parse URL");
997 checkedHit(NullStoreEntry::getInstance());
1001 blk_end
= req_hdrs
+ strlen(req_hdrs
);
1003 if (!checkHitRequest
->header
.parse(req_hdrs
, blk_end
)) {
1004 debugs(31, 3, "htcpCheckHit: NO; failed to parse request headers");
1005 delete checkHitRequest
;
1006 checkHitRequest
= NULL
;
1007 checkedHit(NullStoreEntry::getInstance());
1011 StoreEntry::getPublicByRequest(this, checkHitRequest
);
1015 htcpSpecifier::created (StoreEntry
*e
)
1017 StoreEntry
*hit
=NULL
;
1021 debugs(31, 3, "htcpCheckHit: NO; public object not found");
1022 } else if (!e
->validToSend()) {
1023 debugs(31, 3, "htcpCheckHit: NO; entry not valid to send" );
1024 } else if (refreshCheckHTCP(e
, checkHitRequest
)) {
1025 debugs(31, 3, "htcpCheckHit: NO; cached response is stale");
1027 debugs(31, 3, "htcpCheckHit: YES!?");
1035 htcpClrStoreEntry(StoreEntry
* e
)
1037 debugs(31, 4, "htcpClrStoreEntry: Clearing store for entry: " << e
->url() );
1038 e
->releaseRequest();
1042 htcpClrStore(const htcpSpecifier
* s
)
1044 HttpRequest
*request
= s
->request
;
1046 StoreEntry
*e
= NULL
;
1049 if (request
== NULL
) {
1050 debugs(31, 3, "htcpClrStore: failed to parse URL");
1054 /* Parse request headers */
1055 blk_end
= s
->req_hdrs
+ strlen(s
->req_hdrs
);
1057 if (!request
->header
.parse(s
->req_hdrs
, blk_end
)) {
1058 debugs(31, 2, "htcpClrStore: failed to parse request headers");
1062 /* Lookup matching entries. This matches both GET and HEAD */
1063 while ((e
= storeGetPublicByRequest(request
)) != NULL
) {
1065 htcpClrStoreEntry(e
);
1071 debugs(31, 4, "htcpClrStore: Cleared " << released
<< " matching entries");
1074 debugs(31, 4, "htcpClrStore: No matching entry found");
1081 htcpHandleTst(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1083 debugs(31, 3, "htcpHandleTst: sz = " << sz
);
1085 if (hdr
->RR
== RR_REQUEST
)
1086 htcpHandleTstRequest(hdr
, buf
, sz
, from
);
1088 htcpHandleTstResponse(hdr
, buf
, sz
, from
);
1091 HtcpReplyData::HtcpReplyData() :
1092 hit(0), hdr(hoHtcpReply
), msg_id(0), version(0.0)
1097 htcpHandleTstResponse(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1099 HtcpReplyData htcpReply
;
1100 cache_key
*key
= NULL
;
1103 htcpDetail
*d
= NULL
;
1106 if (queried_id
[hdr
->msg_id
% N_QUERIED_KEYS
] != hdr
->msg_id
) {
1107 debugs(31, 2, "htcpHandleTstResponse: No matching query id '" <<
1108 hdr
->msg_id
<< "' (expected " <<
1109 queried_id
[hdr
->msg_id
% N_QUERIED_KEYS
] << ") from '" <<
1115 key
= queried_keys
[hdr
->msg_id
% N_QUERIED_KEYS
];
1118 debugs(31, 3, "htcpHandleTstResponse: No query key for response id '" << hdr
->msg_id
<< "' from '" << from
<< "'");
1122 peer
= &queried_addr
[hdr
->msg_id
% N_QUERIED_KEYS
];
1124 if ( *peer
!= from
|| peer
->port() != from
.port() ) {
1125 debugs(31, 3, "htcpHandleTstResponse: Unexpected response source " << from
);
1130 debugs(31, 2, "htcpHandleTstResponse: error condition, F1/MO == 1");
1134 htcpReply
.msg_id
= hdr
->msg_id
;
1135 debugs(31, 3, "htcpHandleTstResponse: msg_id = " << htcpReply
.msg_id
);
1136 htcpReply
.hit
= hdr
->response
? 0 : 1;
1139 debugs(31, 3, "htcpHandleTstResponse: MISS");
1141 debugs(31, 3, "htcpHandleTstResponse: HIT");
1142 d
= htcpUnpackDetail(buf
, sz
);
1145 debugs(31, 3, "htcpHandleTstResponse: bad DETAIL");
1149 if ((t
= d
->resp_hdrs
))
1150 htcpReply
.hdr
.parse(t
, t
+ strlen(t
));
1152 if ((t
= d
->entity_hdrs
))
1153 htcpReply
.hdr
.parse(t
, t
+ strlen(t
));
1155 if ((t
= d
->cache_hdrs
))
1156 htcpReply
.hdr
.parse(t
, t
+ strlen(t
));
1159 debugs(31, 3, "htcpHandleTstResponse: key (" << key
<< ") " << storeKeyText(key
));
1160 neighborsHtcpReply(key
, &htcpReply
, from
);
1161 htcpReply
.hdr
.clean();
1168 htcpHandleTstRequest(htcpDataHeader
* dhdr
, char *buf
, int sz
, Ip::Address
&from
)
1170 /* buf should be a SPECIFIER */
1174 debugs(31, 3, "htcpHandleTst: nothing to do");
1181 /* s is a new object */
1182 s
= htcpUnpackSpecifier(buf
, sz
);
1185 debugs(31, 3, "htcpHandleTstRequest: htcpUnpackSpecifier failed");
1186 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1190 s
->setDataHeader(dhdr
);
1194 debugs(31, 3, "htcpHandleTstRequest: failed to parse request");
1195 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1196 htcpFreeSpecifier(s
);
1200 if (!htcpAccessAllowed(Config
.accessList
.htcp
, s
, from
)) {
1201 debugs(31, 3, "htcpHandleTstRequest: Access denied");
1202 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_DENIED
, s
->uri
);
1203 htcpFreeSpecifier(s
);
1207 debugs(31, 2, "HTCP TST request: " << s
->method
<< " " << s
->uri
<< " " << s
->version
);
1208 debugs(31, 2, "HTCP TST headers: " << s
->req_hdrs
);
1213 htcpSpecifier::checkedHit(StoreEntry
*e
)
1216 htcpTstReply(dhdr
, e
, this, from
); /* hit */
1217 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_HIT
, uri
);
1219 htcpTstReply(dhdr
, NULL
, NULL
, from
); /* cache miss */
1220 htcpLogHtcp(from
, dhdr
->opcode
, LOG_UDP_MISS
, uri
);
1223 htcpFreeSpecifier(this);
1228 htcpHandleMon(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1230 debugs(31, 3, "htcpHandleMon: Unimplemented");
1235 htcpHandleSet(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1237 debugs(31, 3, "htcpHandleSet: Unimplemented");
1241 htcpHandleClr(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
1244 /* buf[0/1] is reserved and reason */
1245 int reason
= buf
[1] << 4;
1246 debugs(31, 2, "HTCP CLR reason: " << reason
);
1250 /* buf should be a SPECIFIER */
1253 debugs(31, 4, "htcpHandleClr: nothing to do");
1254 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1258 s
= htcpUnpackSpecifier(buf
, sz
);
1261 debugs(31, 3, "htcpHandleClr: htcpUnpackSpecifier failed");
1262 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1267 debugs(31, 3, "htcpHandleTstRequest: failed to parse request");
1268 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_INVALID
, dash_str
);
1269 htcpFreeSpecifier(s
);
1273 if (!htcpAccessAllowed(Config
.accessList
.htcp_clr
, s
, from
)) {
1274 debugs(31, 3, "htcpHandleClr: Access denied");
1275 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_DENIED
, s
->uri
);
1276 htcpFreeSpecifier(s
);
1280 debugs(31, 2, "HTCP CLR request: " << s
->method
<< " " << s
->uri
<< " " << s
->version
);
1281 debugs(31, 2, "HTCP CLR headers: " << s
->req_hdrs
);
1283 /* Release objects from cache
1284 * analog to clientPurgeRequest in client_side.c
1287 switch (htcpClrStore(s
)) {
1290 htcpClrReply(hdr
, 1, from
); /* hit */
1291 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_HIT
, s
->uri
);
1295 htcpClrReply(hdr
, 0, from
); /* miss */
1296 htcpLogHtcp(from
, hdr
->opcode
, LOG_UDP_MISS
, s
->uri
);
1303 htcpFreeSpecifier(s
);
1307 * Forward a CLR request to all peers who have requested that CLRs be
1308 * forwarded to them.
1311 htcpForwardClr(char *buf
, int sz
)
1315 for (p
= Config
.peers
; p
; p
= p
->next
) {
1316 if (!p
->options
.htcp
) {
1319 if (!p
->options
.htcp_forward_clr
) {
1323 htcpSend(buf
, sz
, p
->in_addr
);
1328 * Do the first pass of handling an HTCP message. This used to be two
1329 * separate functions, htcpHandle and htcpHandleData. They were merged to
1330 * allow for forwarding HTCP packets easily to other peers if desired.
1332 * This function now works out what type of message we have received and then
1333 * hands it off to other functions to break apart message-specific data.
1336 htcpHandleMsg(char *buf
, int sz
, Ip::Address
&from
)
1343 if (sz
< 0 || (size_t)sz
< sizeof(htcpHeader
)) {
1344 // These are highly likely to be attack packets. Should probably get a bigger warning.
1345 debugs(31, 2, "htcpHandle: msg size less than htcpHeader size from " << from
);
1349 htcpHexdump("htcpHandle", buf
, sz
);
1350 memcpy(&htcpHdr
, buf
, sizeof(htcpHeader
));
1351 htcpHdr
.length
= ntohs(htcpHdr
.length
);
1353 if (htcpHdr
.minor
== 0)
1354 old_squid_format
= 1;
1356 old_squid_format
= 0;
1358 debugs(31, 3, "htcpHandle: htcpHdr.length = " << htcpHdr
.length
);
1359 debugs(31, 3, "htcpHandle: htcpHdr.major = " << htcpHdr
.major
);
1360 debugs(31, 3, "htcpHandle: htcpHdr.minor = " << htcpHdr
.minor
);
1362 if (sz
!= htcpHdr
.length
) {
1363 debugs(31, 3, "htcpHandle: sz/" << sz
<< " != htcpHdr.length/" <<
1364 htcpHdr
.length
<< " from " << from
);
1369 if (htcpHdr
.major
!= 0) {
1370 debugs(31, 3, "htcpHandle: Unknown major version " << htcpHdr
.major
<< " from " << from
);
1375 hbuf
= buf
+ sizeof(htcpHeader
);
1376 hsz
= sz
- sizeof(htcpHeader
);
1378 if ((size_t)hsz
< sizeof(htcpDataHeader
)) {
1379 debugs(31, 3, "htcpHandleData: msg size less than htcpDataHeader size");
1383 if (!old_squid_format
) {
1384 memcpy(&hdr
, hbuf
, sizeof(hdr
));
1386 htcpDataHeaderSquid hdrSquid
;
1387 memcpy(&hdrSquid
, hbuf
, sizeof(hdrSquid
));
1388 hdr
.length
= hdrSquid
.length
;
1389 hdr
.opcode
= hdrSquid
.opcode
;
1390 hdr
.response
= hdrSquid
.response
;
1391 hdr
.F1
= hdrSquid
.F1
;
1392 hdr
.RR
= hdrSquid
.RR
;
1394 hdr
.msg_id
= hdrSquid
.msg_id
;
1397 hdr
.length
= ntohs(hdr
.length
);
1398 hdr
.msg_id
= ntohl(hdr
.msg_id
);
1399 debugs(31, 3, "htcpHandleData: hsz = " << hsz
);
1400 debugs(31, 3, "htcpHandleData: length = " << hdr
.length
);
1402 if (hdr
.opcode
>= HTCP_END
) {
1403 debugs(31, 3, "htcpHandleData: client " << from
<< ", opcode " << hdr
.opcode
<< " out of range");
1407 debugs(31, 3, "htcpHandleData: opcode = " << hdr
.opcode
<< " " << htcpOpcodeStr
[hdr
.opcode
]);
1408 debugs(31, 3, "htcpHandleData: response = " << hdr
.response
);
1409 debugs(31, 3, "htcpHandleData: F1 = " << hdr
.F1
);
1410 debugs(31, 3, "htcpHandleData: RR = " << hdr
.RR
);
1411 debugs(31, 3, "htcpHandleData: msg_id = " << hdr
.msg_id
);
1413 if (hsz
< hdr
.length
) {
1414 debugs(31, 3, "htcpHandleData: sz < hdr.length");
1419 * set sz = hdr.length so we ignore any AUTH fields following
1422 hsz
= (int) hdr
.length
;
1423 hbuf
+= sizeof(htcpDataHeader
);
1424 hsz
-= sizeof(htcpDataHeader
);
1425 debugs(31, 3, "htcpHandleData: hsz = " << hsz
);
1427 htcpHexdump("htcpHandleData", hbuf
, hsz
);
1429 switch (hdr
.opcode
) {
1431 htcpHandleNop(&hdr
, hbuf
, hsz
, from
);
1434 htcpHandleTst(&hdr
, hbuf
, hsz
, from
);
1437 htcpHandleMon(&hdr
, hbuf
, hsz
, from
);
1440 htcpHandleSet(&hdr
, hbuf
, hsz
, from
);
1443 htcpHandleClr(&hdr
, hbuf
, hsz
, from
);
1444 htcpForwardClr(buf
, sz
);
1452 htcpRecv(int fd
, void *data
)
1454 static char buf
[8192];
1456 static Ip::Address from
;
1458 /* Receive up to 8191 bytes, leaving room for a null */
1460 len
= comm_udp_recvfrom(fd
, buf
, sizeof(buf
) - 1, 0, from
);
1462 debugs(31, 3, "htcpRecv: FD " << fd
<< ", " << len
<< " bytes from " << from
);
1465 ++statCounter
.htcp
.pkts_recv
;
1467 htcpHandleMsg(buf
, len
, from
);
1469 Comm::SetSelect(fd
, COMM_SELECT_READ
, htcpRecv
, NULL
, 0);
1473 * ======================================================================
1475 * ======================================================================
1481 if (Config
.Port
.htcp
<= 0) {
1482 debugs(31, DBG_IMPORTANT
, "HTCP Disabled.");
1486 htcpIncomingConn
= new Comm::Connection
;
1487 htcpIncomingConn
->local
= Config
.Addrs
.udp_incoming
;
1488 htcpIncomingConn
->local
.port(Config
.Port
.htcp
);
1490 if (!Ip::EnableIpv6
&& !htcpIncomingConn
->local
.setIPv4()) {
1491 debugs(31, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << htcpIncomingConn
->local
<< " is not an IPv4 address.");
1492 fatal("HTCP port cannot be opened.");
1494 /* split-stack for now requires default IPv4-only HTCP */
1495 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& htcpIncomingConn
->local
.isAnyAddr()) {
1496 htcpIncomingConn
->local
.setIPv4();
1499 AsyncCall::Pointer call
= asyncCall(31, 2,
1500 "htcpIncomingConnectionOpened",
1501 Comm::UdpOpenDialer(&htcpIncomingConnectionOpened
));
1503 Ipc::StartListening(SOCK_DGRAM
,
1506 Ipc::fdnInHtcpSocket
, call
);
1508 if (!Config
.Addrs
.udp_outgoing
.isNoAddr()) {
1509 htcpOutgoingConn
= new Comm::Connection
;
1510 htcpOutgoingConn
->local
= Config
.Addrs
.udp_outgoing
;
1511 htcpOutgoingConn
->local
.port(Config
.Port
.htcp
);
1513 if (!Ip::EnableIpv6
&& !htcpOutgoingConn
->local
.setIPv4()) {
1514 debugs(31, DBG_CRITICAL
, "ERROR: IPv6 is disabled. " << htcpOutgoingConn
->local
<< " is not an IPv4 address.");
1515 fatal("HTCP port cannot be opened.");
1517 /* split-stack for now requires default IPv4-only HTCP */
1518 if (Ip::EnableIpv6
&IPV6_SPECIAL_SPLITSTACK
&& htcpOutgoingConn
->local
.isAnyAddr()) {
1519 htcpOutgoingConn
->local
.setIPv4();
1523 comm_open_listener(SOCK_DGRAM
, IPPROTO_UDP
, htcpOutgoingConn
, "Outgoing HTCP Socket");
1526 if (!Comm::IsConnOpen(htcpOutgoingConn
))
1527 fatal("Cannot open Outgoing HTCP Socket");
1529 Comm::SetSelect(htcpOutgoingConn
->fd
, COMM_SELECT_READ
, htcpRecv
, NULL
, 0);
1531 debugs(31, DBG_IMPORTANT
, "Sending HTCP messages from " << htcpOutgoingConn
->local
);
1534 if (!htcpDetailPool
) {
1535 htcpDetailPool
= memPoolCreate("htcpDetail", sizeof(htcpDetail
));
1540 htcpIncomingConnectionOpened(const Comm::ConnectionPointer
&conn
, int)
1542 if (!Comm::IsConnOpen(conn
))
1543 fatal("Cannot open HTCP Socket");
1545 Comm::SetSelect(conn
->fd
, COMM_SELECT_READ
, htcpRecv
, NULL
, 0);
1547 debugs(31, DBG_CRITICAL
, "Accepting HTCP messages on " << conn
->local
);
1549 if (Config
.Addrs
.udp_outgoing
.isNoAddr()) {
1550 htcpOutgoingConn
= conn
;
1551 debugs(31, DBG_IMPORTANT
, "Sending HTCP messages from " << htcpOutgoingConn
->local
);
1556 htcpQuery(StoreEntry
* e
, HttpRequest
* req
, CachePeer
* p
)
1558 cache_key
*save_key
;
1559 static char pkt
[8192];
1563 HttpHeader
hdr(hoRequest
);
1566 HttpStateFlags flags
;
1568 if (!Comm::IsConnOpen(htcpIncomingConn
))
1571 old_squid_format
= p
->options
.htcp_oldsquid
;
1572 memset(&flags
, '\0', sizeof(flags
));
1573 snprintf(vbuf
, sizeof(vbuf
), "%d/%d",
1574 req
->http_ver
.major
, req
->http_ver
.minor
);
1575 stuff
.op
= HTCP_TST
;
1576 stuff
.rr
= RR_REQUEST
;
1579 stuff
.msg_id
= ++msg_id_counter
;
1580 stuff
.S
.method
= (char *) RequestMethodStr(req
->method
);
1581 stuff
.S
.uri
= (char *) e
->url();
1582 stuff
.S
.version
= vbuf
;
1583 HttpStateData::httpBuildRequestHeader(req
, e
, NULL
, &hdr
, flags
);
1585 packerToMemInit(&pa
, &mb
);
1589 stuff
.S
.req_hdrs
= mb
.buf
;
1590 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
1593 debugs(31, 3, "htcpQuery: htcpBuildPacket() failed");
1597 htcpSend(pkt
, (int) pktlen
, p
->in_addr
);
1599 queried_id
[stuff
.msg_id
% N_QUERIED_KEYS
] = stuff
.msg_id
;
1600 save_key
= queried_keys
[stuff
.msg_id
% N_QUERIED_KEYS
];
1601 storeKeyCopy(save_key
, (const cache_key
*)e
->key
);
1602 queried_addr
[stuff
.msg_id
% N_QUERIED_KEYS
] = p
->in_addr
;
1603 debugs(31, 3, "htcpQuery: key (" << save_key
<< ") " << storeKeyText(save_key
));
1609 * Send an HTCP CLR message for a specified item to a given CachePeer.
1612 htcpClear(StoreEntry
* e
, const char *uri
, HttpRequest
* req
, const HttpRequestMethod
&method
, CachePeer
* p
, htcp_clr_reason reason
)
1614 static char pkt
[8192];
1618 HttpHeader
hdr(hoRequest
);
1621 HttpStateFlags flags
;
1623 if (!Comm::IsConnOpen(htcpIncomingConn
))
1626 old_squid_format
= p
->options
.htcp_oldsquid
;
1627 memset(&flags
, '\0', sizeof(flags
));
1628 snprintf(vbuf
, sizeof(vbuf
), "%d/%d",
1629 req
->http_ver
.major
, req
->http_ver
.minor
);
1630 stuff
.op
= HTCP_CLR
;
1631 stuff
.rr
= RR_REQUEST
;
1634 stuff
.msg_id
= ++msg_id_counter
;
1636 case HTCP_CLR_INVALIDATION
:
1643 stuff
.S
.method
= (char *) RequestMethodStr(req
->method
);
1644 if (e
== NULL
|| e
->mem_obj
== NULL
) {
1648 stuff
.S
.uri
= xstrdup(uri
);
1650 stuff
.S
.uri
= (char *) e
->url();
1652 stuff
.S
.version
= vbuf
;
1653 if (reason
!= HTCP_CLR_INVALIDATION
) {
1654 HttpStateData::httpBuildRequestHeader(req
, e
, NULL
, &hdr
, flags
);
1656 packerToMemInit(&pa
, &mb
);
1660 stuff
.S
.req_hdrs
= mb
.buf
;
1662 stuff
.S
.req_hdrs
= NULL
;
1664 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
1665 if (reason
!= HTCP_CLR_INVALIDATION
) {
1672 debugs(31, 3, "htcpClear: htcpBuildPacket() failed");
1676 htcpSend(pkt
, (int) pktlen
, p
->in_addr
);
1680 * htcpSocketShutdown only closes the 'in' socket if it is
1681 * different than the 'out' socket.
1684 htcpSocketShutdown(void)
1686 if (!Comm::IsConnOpen(htcpIncomingConn
))
1689 debugs(12, DBG_IMPORTANT
, "Stop accepting HTCP on " << htcpIncomingConn
->local
);
1691 * Here we just unlink htcpIncomingConn because the HTCP 'in'
1692 * and 'out' sockets might be just one FD. This prevents this
1693 * function from executing repeatedly. When we are really ready to
1694 * exit or restart, main will comm_close the 'out' descriptor.
1696 htcpIncomingConn
= NULL
;
1699 * Normally we only write to the outgoing HTCP socket, but
1700 * we also have a read handler there to catch messages sent
1701 * to that specific interface. During shutdown, we must
1702 * disable reading on the outgoing socket.
1704 /* XXX Don't we need this handler to read replies while shutting down?
1705 * I think there should be a separate hander for reading replies..
1707 assert(Comm::IsConnOpen(htcpOutgoingConn
));
1709 Comm::SetSelect(htcpOutgoingConn
->fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
1713 htcpClosePorts(void)
1715 htcpSocketShutdown();
1717 if (htcpOutgoingConn
!= NULL
) {
1718 debugs(12, DBG_IMPORTANT
, "Stop sending HTCP from " << htcpOutgoingConn
->local
);
1719 htcpOutgoingConn
= NULL
;
1724 htcpLogHtcp(Ip::Address
&caddr
, int opcode
, LogTags logcode
, const char *url
)
1726 AccessLogEntry::Pointer al
= new AccessLogEntry
;
1727 if (LOG_TAG_NONE
== logcode
)
1729 if (!Config
.onoff
.log_udp
)
1731 al
->htcp
.opcode
= htcpOpcodeStr
[opcode
];
1733 al
->cache
.caddr
= caddr
;
1734 al
->cache
.code
= logcode
;
1736 accessLogLog(al
, NULL
);