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"
58 #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 size_t reqHdrsSz
; ///< size of the req_hdrs content
176 HttpRequest
*request
;
179 HttpRequest
*checkHitRequest
;
181 Ip::Address from
; // was a ptr. return to such IFF needed. otherwise copy should do.
182 htcpDataHeader
*dhdr
;
185 MEMPROXY_CLASS_INLINE(htcpSpecifier
);
218 static const char *const htcpOpcodeStr
[] = {
228 * values for htcpDataHeader->response
233 OPCODE_UNIMPLEMENTED
,
234 MAJOR_VERSION_UNSUPPORTED
,
235 MINOR_VERSION_UNSUPPORTED
,
240 * values for htcpDataHeader->RR
247 static void htcpIncomingConnectionOpened(const Comm::ConnectionPointer
&conn
, int errNo
);
248 static uint32_t msg_id_counter
= 0;
250 static Comm::ConnectionPointer htcpOutgoingConn
= NULL
;
251 static Comm::ConnectionPointer htcpIncomingConn
= NULL
;
252 #define N_QUERIED_KEYS 8192
253 static uint32_t queried_id
[N_QUERIED_KEYS
];
254 static cache_key queried_keys
[N_QUERIED_KEYS
][SQUID_MD5_DIGEST_LENGTH
];
256 static Ip::Address queried_addr
[N_QUERIED_KEYS
];
257 static MemAllocator
*htcpDetailPool
= NULL
;
259 static int old_squid_format
= 0;
261 static ssize_t
htcpBuildPacket(char *buf
, size_t buflen
, htcpStuff
* stuff
);
262 static htcpSpecifier
*htcpUnpackSpecifier(char *buf
, int sz
);
263 static htcpDetail
*htcpUnpackDetail(char *buf
, int sz
);
264 static ssize_t
htcpBuildAuth(char *buf
, size_t buflen
);
265 static ssize_t
htcpBuildCountstr(char *buf
, size_t buflen
, const char *s
, size_t len
);
266 static ssize_t
htcpBuildData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
267 static ssize_t
htcpBuildDetail(char *buf
, size_t buflen
, htcpStuff
* stuff
);
268 static ssize_t
htcpBuildOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
269 static ssize_t
htcpBuildSpecifier(char *buf
, size_t buflen
, htcpStuff
* stuff
);
270 static ssize_t
htcpBuildTstOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
);
271 static void htcpFreeSpecifier(htcpSpecifier
* s
);
272 static void htcpFreeDetail(htcpDetail
* s
);
274 static void htcpHandleMsg(char *buf
, int sz
, Ip::Address
&from
);
276 static void htcpLogHtcp(Ip::Address
&, int, LogTags
, const char *);
277 static void htcpHandleMon(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
279 static void htcpHandleNop(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
281 static void htcpHandleSet(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
283 static void htcpHandleTst(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
285 static void htcpRecv(int fd
, void *data
);
287 static void htcpSend(const char *buf
, int len
, Ip::Address
&to
);
289 static void htcpTstReply(htcpDataHeader
*, StoreEntry
*, htcpSpecifier
*, Ip::Address
&);
291 static void htcpHandleTstRequest(htcpDataHeader
*, char *buf
, int sz
, Ip::Address
&from
);
293 static void htcpHandleTstResponse(htcpDataHeader
*, char *, int, Ip::Address
&);
296 htcpHexdump(const char *tag
, const char *s
, int sz
)
302 debugs(31, 3, "htcpHexdump " << tag
);
303 memset(hex
, '\0', 80);
305 for (i
= 0; i
< sz
; ++i
) {
307 snprintf(&hex
[k
* 3], 4, " %02x", (int) *(s
+ i
));
309 if (k
< 15 && i
< (sz
- 1))
312 debugs(31, 3, "\t" << hex
);
314 memset(hex
, '\0', 80);
321 * STUFF FOR SENDING HTCP MESSAGES
325 htcpBuildAuth(char *buf
, size_t buflen
)
329 assert(2 == sizeof(uint16_t));
330 auth
.length
= htons(2);
332 if (buflen
< copy_sz
)
334 memcpy(buf
, &auth
, copy_sz
);
339 htcpBuildCountstr(char *buf
, size_t buflen
, const char *s
, size_t len
)
343 if (buflen
- off
< 2)
346 debugs(31, 3, "htcpBuildCountstr: LENGTH = " << len
);
348 debugs(31, 3, "htcpBuildCountstr: TEXT = {" << (s
? s
: "<NULL>") << "}");
350 uint16_t length
= htons((uint16_t) len
);
352 memcpy(buf
+ off
, &length
, 2);
356 if (buflen
- off
< len
)
360 memcpy(buf
+ off
, s
, len
);
368 htcpBuildSpecifier(char *buf
, size_t buflen
, htcpStuff
* stuff
)
372 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.method
, (stuff
->S
.method
?strlen(stuff
->S
.method
):0));
379 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.uri
, (stuff
->S
.uri
?strlen(stuff
->S
.uri
):0));
386 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.version
, (stuff
->S
.version
?strlen(stuff
->S
.version
):0));
393 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->S
.req_hdrs
, stuff
->S
.reqHdrsSz
);
400 debugs(31, 3, "htcpBuildSpecifier: size " << off
);
406 htcpBuildDetail(char *buf
, size_t buflen
, htcpStuff
* stuff
)
410 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.resp_hdrs
, stuff
->D
.respHdrsSz
);
417 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.entity_hdrs
, stuff
->D
.entityHdrsSz
);
424 s
= htcpBuildCountstr(buf
+ off
, buflen
- off
, stuff
->D
.cache_hdrs
, stuff
->D
.cacheHdrsSz
);
435 htcpBuildTstOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
440 debugs(31, 3, "htcpBuildTstOpData: RR_REQUEST");
441 return htcpBuildSpecifier(buf
, buflen
, stuff
);
444 debugs(31, 3, "htcpBuildTstOpData: RR_RESPONSE");
445 debugs(31, 3, "htcpBuildTstOpData: F1 = " << stuff
->f1
);
447 if (stuff
->f1
) /* cache miss */
450 return htcpBuildDetail(buf
, buflen
, stuff
);
453 fatal_dump("htcpBuildTstOpData: bad RR value");
460 htcpBuildClrOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
462 unsigned short reason
;
466 debugs(31, 3, "htcpBuildClrOpData: RR_REQUEST");
467 reason
= htons((unsigned short)stuff
->reason
);
468 memcpy(buf
, &reason
, 2);
469 return htcpBuildSpecifier(buf
+ 2, buflen
- 2, stuff
) + 2;
473 fatal_dump("htcpBuildClrOpData: bad RR value");
480 htcpBuildOpData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
483 debugs(31, 3, "htcpBuildOpData: opcode " << htcpOpcodeStr
[stuff
->op
]);
488 off
= htcpBuildTstOpData(buf
+ off
, buflen
, stuff
);
492 off
= htcpBuildClrOpData(buf
+ off
, buflen
, stuff
);
504 htcpBuildData(char *buf
, size_t buflen
, htcpStuff
* stuff
)
508 size_t hdr_sz
= sizeof(htcpDataHeader
);
514 off
+= hdr_sz
; /* skip! */
516 op_data_sz
= htcpBuildOpData(buf
+ off
, buflen
- off
, stuff
);
523 debugs(31, 3, "htcpBuildData: hdr.length = " << off
);
525 hdr
.length
= (uint16_t) off
;
527 hdr
.opcode
= stuff
->op
;
529 hdr
.response
= stuff
->response
;
535 hdr
.msg_id
= stuff
->msg_id
;
537 /* convert multi-byte fields */
538 hdr
.length
= htons(hdr
.length
);
540 hdr
.msg_id
= htonl(hdr
.msg_id
);
542 if (!old_squid_format
) {
543 memcpy(buf
, &hdr
, hdr_sz
);
545 htcpDataHeaderSquid hdrSquid
;
546 memset(&hdrSquid
, 0, sizeof(hdrSquid
));
547 hdrSquid
.length
= hdr
.length
;
548 hdrSquid
.opcode
= hdr
.opcode
;
549 hdrSquid
.response
= hdr
.response
;
550 hdrSquid
.F1
= hdr
.F1
;
551 hdrSquid
.RR
= hdr
.RR
;
552 memcpy(buf
, &hdrSquid
, hdr_sz
);
555 debugs(31, 3, "htcpBuildData: size " << off
);
561 * Build an HTCP packet into buf, maximum length buflen.
562 * Returns the packet length, or zero on failure.
565 htcpBuildPacket(char *buf
, size_t buflen
, htcpStuff
* stuff
)
569 size_t hdr_sz
= sizeof(htcpHeader
);
571 /* skip the header -- we don't know the overall length */
573 if (buflen
< hdr_sz
) {
578 s
= htcpBuildData(buf
+ off
, buflen
- off
, stuff
);
585 s
= htcpBuildAuth(buf
+ off
, buflen
- off
);
592 hdr
.length
= htons((uint16_t) off
);
595 if (old_squid_format
)
600 memcpy(buf
, &hdr
, hdr_sz
);
602 debugs(31, 3, "htcpBuildPacket: size " << off
);
608 htcpSend(const char *buf
, int len
, Ip::Address
&to
)
610 debugs(31, 3, HERE
<< to
);
611 htcpHexdump("htcpSend", buf
, len
);
613 if (comm_udp_sendto(htcpOutgoingConn
->fd
, to
, buf
, len
) < 0)
614 debugs(31, 3, HERE
<< htcpOutgoingConn
<< " sendto: " << xstrerror());
616 ++statCounter
.htcp
.pkts_sent
;
620 * STUFF FOR RECEIVING HTCP MESSAGES
624 htcpSpecifier::setFrom(Ip::Address
&aSocket
)
630 htcpSpecifier::setDataHeader(htcpDataHeader
*aDataHeader
)
636 htcpFreeSpecifier(htcpSpecifier
* s
)
638 HTTPMSGUNLOCK(s
->request
);
644 htcpFreeDetail(htcpDetail
* d
)
646 htcpDetailPool
->freeOne(d
);
650 * Unpack an HTCP SPECIFIER in place
651 * This will overwrite any following AUTH block
653 // XXX: this needs to be turned into an Htcp1::Parser inheriting from Http1::RequestParser
654 // but with different first-line and block unpacking logic.
655 static htcpSpecifier
*
656 htcpUnpackSpecifier(char *buf
, int sz
)
658 htcpSpecifier
*s
= new htcpSpecifier
;
659 HttpRequestMethod method
;
661 /* Find length of METHOD */
662 uint16_t l
= ntohs(*(uint16_t *) buf
);
667 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack METHOD");
668 htcpFreeSpecifier(s
);
676 debugs(31, 6, "htcpUnpackSpecifier: METHOD (" << l
<< "/" << sz
<< ") '" << s
->method
<< "'");
678 /* Find length of URI */
679 l
= ntohs(*(uint16_t *) buf
);
683 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack URI");
684 htcpFreeSpecifier(s
);
688 /* Add terminating null to METHOD */
696 debugs(31, 6, "htcpUnpackSpecifier: URI (" << l
<< "/" << sz
<< ") '" << s
->uri
<< "'");
698 /* Find length of VERSION */
699 l
= ntohs(*(uint16_t *) buf
);
703 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack VERSION");
704 htcpFreeSpecifier(s
);
708 /* Add terminating null to URI */
716 debugs(31, 6, "htcpUnpackSpecifier: VERSION (" << l
<< "/" << sz
<< ") '" << s
->version
<< "'");
718 /* Find length of REQ-HDRS */
719 l
= ntohs(*(uint16_t *) buf
);
723 debugs(31, 3, "htcpUnpackSpecifier: failed to unpack REQ-HDRS");
724 htcpFreeSpecifier(s
);
728 /* Add terminating null to URI */
737 debugs(31, 6, "htcpUnpackSpecifier: REQ-HDRS (" << l
<< "/" << sz
<< ") '" << s
->req_hdrs
<< "'");
739 debugs(31, 3, "htcpUnpackSpecifier: " << sz
<< " bytes left");
742 * Add terminating null to REQ-HDRS. This is possible because we allocated
743 * an extra byte when we received the packet. This will overwrite any following
751 method
= HttpRequestMethod(s
->method
, NULL
);
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
)
860 static char pkt
[8192];
861 HttpHeader
hdr(hoHtcpReply
);
865 memset(&stuff
, '\0', sizeof(stuff
));
867 stuff
.rr
= RR_RESPONSE
;
869 stuff
.response
= e
? 0 : 1;
870 debugs(31, 3, "htcpTstReply: response = " << stuff
.response
);
871 stuff
.msg_id
= dhdr
->msg_id
;
875 packerToMemInit(&p
, &mb
);
876 stuff
.S
.method
= spec
->method
;
877 stuff
.S
.uri
= spec
->uri
;
878 stuff
.S
.version
= spec
->version
;
879 stuff
.S
.req_hdrs
= spec
->req_hdrs
;
880 stuff
.S
.reqHdrsSz
= spec
->reqHdrsSz
;
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 stuff
.D
.respHdrsSz
= mb
.contentSize();
888 debugs(31, 3, "htcpTstReply: resp_hdrs = {" << stuff
.D
.resp_hdrs
<< "}");
892 if (e
&& e
->expires
> -1)
893 hdr
.putTime(HDR_EXPIRES
, e
->expires
);
895 if (e
&& e
->lastmod
> -1)
896 hdr
.putTime(HDR_LAST_MODIFIED
, e
->lastmod
);
900 stuff
.D
.entity_hdrs
= xstrdup(mb
.buf
);
901 stuff
.D
.entityHdrsSz
= mb
.contentSize();
903 debugs(31, 3, "htcpTstReply: entity_hdrs = {" << stuff
.D
.entity_hdrs
<< "}");
910 if (char *host
= urlHostname(spec
->uri
)) {
914 netdbHostData(host
, &samp
, &rtt
, &hops
);
918 snprintf(cto_buf
, 128, "%s %d %f %d",
919 host
, samp
, 0.001 * rtt
, hops
);
920 hdr
.putExt("Cache-to-Origin", cto_buf
);
923 #endif /* USE_ICMP */
926 stuff
.D
.cache_hdrs
= xstrdup(mb
.buf
);
927 stuff
.D
.cacheHdrsSz
= mb
.contentSize();
928 debugs(31, 3, "htcpTstReply: cache_hdrs = {" << stuff
.D
.cache_hdrs
<< "}");
934 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
936 safe_free(stuff
.D
.resp_hdrs
);
937 stuff
.D
.respHdrsSz
= 0;
938 safe_free(stuff
.D
.entity_hdrs
);
939 stuff
.D
.entityHdrsSz
= 0;
940 safe_free(stuff
.D
.cache_hdrs
);
941 stuff
.D
.cacheHdrsSz
= 0;
944 debugs(31, 3, "htcpTstReply: htcpBuildPacket() failed");
948 htcpSend(pkt
, (int) pktlen
, from
);
953 htcpClrReply(htcpDataHeader
* dhdr
, int purgeSucceeded
, Ip::Address
&from
)
956 static char pkt
[8192];
959 /* If dhdr->F1 == 0, no response desired */
964 memset(&stuff
, '\0', sizeof(stuff
));
968 stuff
.rr
= RR_RESPONSE
;
972 stuff
.response
= purgeSucceeded
? 0 : 2;
974 debugs(31, 3, "htcpClrReply: response = " << stuff
.response
);
976 stuff
.msg_id
= dhdr
->msg_id
;
978 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
981 debugs(31, 3, "htcpClrReply: htcpBuildPacket() failed");
985 htcpSend(pkt
, (int) pktlen
, from
);
990 htcpHandleNop(htcpDataHeader
* hdr
, char *buf
, int sz
, Ip::Address
&from
)
992 debugs(31, 3, "htcpHandleNop: Unimplemented");
996 htcpSpecifier::checkHit()
998 checkHitRequest
= request
;
1000 if (NULL
== checkHitRequest
) {
1001 debugs(31, 3, "htcpCheckHit: NO; failed to parse URL");
1002 checkedHit(NullStoreEntry::getInstance());
1006 if (!checkHitRequest
->header
.parse(req_hdrs
, reqHdrsSz
)) {
1007 debugs(31, 3, "htcpCheckHit: NO; failed to parse request headers");
1008 delete checkHitRequest
;
1009 checkHitRequest
= NULL
;
1010 checkedHit(NullStoreEntry::getInstance());
1014 StoreEntry::getPublicByRequest(this, checkHitRequest
);
1018 htcpSpecifier::created (StoreEntry
*e
)
1020 StoreEntry
*hit
=NULL
;
1024 debugs(31, 3, "htcpCheckHit: NO; public object not found");
1025 } else if (!e
->validToSend()) {
1026 debugs(31, 3, "htcpCheckHit: NO; entry not valid to send" );
1027 } else if (refreshCheckHTCP(e
, checkHitRequest
)) {
1028 debugs(31, 3, "htcpCheckHit: NO; cached response is stale");
1030 debugs(31, 3, "htcpCheckHit: YES!?");
1038 htcpClrStoreEntry(StoreEntry
* e
)
1040 debugs(31, 4, "htcpClrStoreEntry: Clearing store for entry: " << e
->url() );
1041 e
->releaseRequest();
1045 htcpClrStore(const htcpSpecifier
* s
)
1047 HttpRequest
*request
= s
->request
;
1048 StoreEntry
*e
= NULL
;
1051 if (request
== NULL
) {
1052 debugs(31, 3, "htcpClrStore: failed to parse URL");
1056 /* Parse request headers */
1057 if (!request
->header
.parse(s
->req_hdrs
, s
->reqHdrsSz
)) {
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
, d
->respHdrsSz
);
1152 if ((t
= d
->entity_hdrs
))
1153 htcpReply
.hdr
.parse(t
, d
->entityHdrsSz
);
1155 if ((t
= d
->cache_hdrs
))
1156 htcpReply
.hdr
.parse(t
, d
->cacheHdrsSz
);
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 SBuf sb
= req
->method
.image();
1581 stuff
.S
.method
= sb
.c_str();
1582 stuff
.S
.uri
= (char *) e
->url();
1583 stuff
.S
.version
= vbuf
;
1584 HttpStateData::httpBuildRequestHeader(req
, e
, NULL
, &hdr
, flags
);
1586 packerToMemInit(&pa
, &mb
);
1590 stuff
.S
.req_hdrs
= mb
.buf
;
1591 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
1594 debugs(31, 3, "htcpQuery: htcpBuildPacket() failed");
1598 htcpSend(pkt
, (int) pktlen
, p
->in_addr
);
1600 queried_id
[stuff
.msg_id
% N_QUERIED_KEYS
] = stuff
.msg_id
;
1601 save_key
= queried_keys
[stuff
.msg_id
% N_QUERIED_KEYS
];
1602 storeKeyCopy(save_key
, (const cache_key
*)e
->key
);
1603 queried_addr
[stuff
.msg_id
% N_QUERIED_KEYS
] = p
->in_addr
;
1604 debugs(31, 3, "htcpQuery: key (" << save_key
<< ") " << storeKeyText(save_key
));
1610 * Send an HTCP CLR message for a specified item to a given CachePeer.
1613 htcpClear(StoreEntry
* e
, const char *uri
, HttpRequest
* req
, const HttpRequestMethod
&method
, CachePeer
* p
, htcp_clr_reason reason
)
1615 static char pkt
[8192];
1619 HttpHeader
hdr(hoRequest
);
1622 HttpStateFlags flags
;
1624 if (!Comm::IsConnOpen(htcpIncomingConn
))
1627 old_squid_format
= p
->options
.htcp_oldsquid
;
1628 memset(&flags
, '\0', sizeof(flags
));
1629 snprintf(vbuf
, sizeof(vbuf
), "%d/%d",
1630 req
->http_ver
.major
, req
->http_ver
.minor
);
1631 stuff
.op
= HTCP_CLR
;
1632 stuff
.rr
= RR_REQUEST
;
1635 stuff
.msg_id
= ++msg_id_counter
;
1637 case HTCP_CLR_INVALIDATION
:
1644 SBuf sb
= req
->method
.image();
1645 stuff
.S
.method
= sb
.c_str();
1646 if (e
== NULL
|| e
->mem_obj
== NULL
) {
1650 stuff
.S
.uri
= xstrdup(uri
);
1652 stuff
.S
.uri
= (char *) e
->url();
1654 stuff
.S
.version
= vbuf
;
1655 if (reason
!= HTCP_CLR_INVALIDATION
) {
1656 HttpStateData::httpBuildRequestHeader(req
, e
, NULL
, &hdr
, flags
);
1658 packerToMemInit(&pa
, &mb
);
1662 stuff
.S
.req_hdrs
= mb
.buf
;
1664 stuff
.S
.req_hdrs
= NULL
;
1666 pktlen
= htcpBuildPacket(pkt
, sizeof(pkt
), &stuff
);
1667 if (reason
!= HTCP_CLR_INVALIDATION
) {
1674 debugs(31, 3, "htcpClear: htcpBuildPacket() failed");
1678 htcpSend(pkt
, (int) pktlen
, p
->in_addr
);
1682 * htcpSocketShutdown only closes the 'in' socket if it is
1683 * different than the 'out' socket.
1686 htcpSocketShutdown(void)
1688 if (!Comm::IsConnOpen(htcpIncomingConn
))
1691 debugs(12, DBG_IMPORTANT
, "Stop accepting HTCP on " << htcpIncomingConn
->local
);
1693 * Here we just unlink htcpIncomingConn because the HTCP 'in'
1694 * and 'out' sockets might be just one FD. This prevents this
1695 * function from executing repeatedly. When we are really ready to
1696 * exit or restart, main will comm_close the 'out' descriptor.
1698 htcpIncomingConn
= NULL
;
1701 * Normally we only write to the outgoing HTCP socket, but
1702 * we also have a read handler there to catch messages sent
1703 * to that specific interface. During shutdown, we must
1704 * disable reading on the outgoing socket.
1706 /* XXX Don't we need this handler to read replies while shutting down?
1707 * I think there should be a separate hander for reading replies..
1709 assert(Comm::IsConnOpen(htcpOutgoingConn
));
1711 Comm::SetSelect(htcpOutgoingConn
->fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
1715 htcpClosePorts(void)
1717 htcpSocketShutdown();
1719 if (htcpOutgoingConn
!= NULL
) {
1720 debugs(12, DBG_IMPORTANT
, "Stop sending HTCP from " << htcpOutgoingConn
->local
);
1721 htcpOutgoingConn
= NULL
;
1726 htcpLogHtcp(Ip::Address
&caddr
, int opcode
, LogTags logcode
, const char *url
)
1728 AccessLogEntry::Pointer al
= new AccessLogEntry
;
1729 if (LOG_TAG_NONE
== logcode
)
1731 if (!Config
.onoff
.log_udp
)
1733 al
->htcp
.opcode
= htcpOpcodeStr
[opcode
];
1735 al
->cache
.caddr
= caddr
;
1736 al
->cache
.code
= logcode
;
1738 accessLogLog(al
, NULL
);