Squid Web Cache master
Loading...
Searching...
No Matches
icp_v3.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2026 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9/* DEBUG: section 12 Internet Cache Protocol (ICP) */
10
16#include "squid.h"
17#include "acl/FilledChecklist.h"
18#include "HttpRequest.h"
19#include "ICP.h"
20#include "Store.h"
21
23class ICP3State: public ICPState
24{
25
26public:
27 ICP3State(icp_common_t &aHeader, HttpRequest *aRequest) :
28 ICPState(aHeader, aRequest) {}
29
30 ~ICP3State() override = default;
31};
32
34static void
35doV3Query(int fd, Ip::Address &from, const char * const buf, icp_common_t header)
36{
37 const auto url = icpGetUrl(from, buf, header);
38 if (!url) {
39 icpCreateAndSend(ICP_ERR, 0, "", header.reqnum, 0, fd, from, nullptr);
40 return;
41 }
42
43 const auto icp_request = icpGetRequest(url, header.reqnum, fd, from);
44
45 if (!icp_request)
46 return;
47
48 /* The peer is allowed to use this cache */
49 ICP3State state(header, icp_request.getRaw());
50 state.fd = fd;
51 state.from = from;
52 state.url = xstrdup(url);
53
54 icp_opcode codeToSend;
55
56 if (state.isHit()) {
57 codeToSend = ICP_HIT;
58 } else if (icpGetCommonOpcode() == ICP_ERR)
59 codeToSend = ICP_MISS;
60 else
61 codeToSend = icpGetCommonOpcode();
62
63 icpCreateAndSend(codeToSend, 0, url, header.reqnum, 0, fd, from, state.al);
64}
65
67/* Currently Harvest cached-2.x uses ICP_VERSION_3 */
68void
69icpHandleIcpV3(int fd, Ip::Address &from, char *buf, int len)
70{
71 if (len <= 0) {
72 debugs(12, 3, "icpHandleIcpV3: ICP message is too small");
73 return;
74 }
75
76 icp_common_t header (buf, len);
77 /*
78 * Length field should match the number of bytes read
79 */
80
81 if (len != header.length) {
82 debugs(12, 3, "icpHandleIcpV3: ICP message is too small");
83 return;
84 }
85
86 debugs(12, 5, "OPCODE " << icp_opcode_str[header.getOpCode()] << '=' << uint8_t(header.opcode));
87
88 switch (header.opcode) {
89
90 case ICP_QUERY:
91 doV3Query(fd, from, buf, header);
92 break;
93
94 case ICP_HIT:
95
96 case ICP_DECHO:
97
98 case ICP_MISS:
99
100 case ICP_DENIED:
101
102 case ICP_MISS_NOFETCH:
103 header.handleReply(buf, from);
104 break;
105
106 case ICP_INVALID:
107
108 case ICP_ERR:
109 break;
110
111 default:
112 debugs(12, DBG_CRITICAL, "ERROR: icpHandleIcpV3: Unknown opcode: " << header.opcode << " from " << from);
113 break;
114 }
115}
116
const char * icpGetUrl(const Ip::Address &from, const char *, const icp_common_t &)
Definition icp_v2.cc:462
~ICP3State() override=default
ICP3State(icp_common_t &aHeader, HttpRequest *aRequest)
Definition icp_v3.cc:27
Definition ICP.h:62
bool isHit() const
whether the cache contains the requested entry
Definition icp_v2.cc:155
AccessLogEntryPointer al
Definition ICP.h:77
Ip::Address from
Definition ICP.h:75
char * url
Definition ICP.h:76
int fd
Definition ICP.h:73
void handleReply(char *buf, Ip::Address &from)
Definition icp_v2.cc:578
icp_opcode getOpCode() const
Definition icp_v2.cc:129
uint32_t reqnum
Definition ICP.h:44
unsigned char opcode
Definition ICP.h:38
unsigned short length
Definition ICP.h:42
#define debugs(SECTION, LEVEL, CONTENT)
Definition Stream.h:192
#define DBG_CRITICAL
Definition Stream.h:37
icp_opcode icpGetCommonOpcode()
Definition icp_v2.cc:376
void icpCreateAndSend(icp_opcode, int flags, char const *url, int reqnum, int pad, int fd, const Ip::Address &from, AccessLogEntryPointer)
Definition icp_v2.cc:418
HttpRequestPointer icpGetRequest(const char *url, int reqnum, int fd, const Ip::Address &from)
Definition icp_v2.cc:495
icp_opcode
Definition icp_opcode.h:13
@ ICP_DECHO
Definition icp_opcode.h:26
@ ICP_INVALID
Definition icp_opcode.h:15
@ ICP_QUERY
Definition icp_opcode.h:16
@ ICP_MISS_NOFETCH
Definition icp_opcode.h:36
@ ICP_DENIED
Definition icp_opcode.h:37
@ ICP_ERR
Definition icp_opcode.h:19
@ ICP_MISS
Definition icp_opcode.h:18
@ ICP_HIT
Definition icp_opcode.h:17
static void doV3Query(int fd, Ip::Address &from, const char *const buf, icp_common_t header)
Definition icp_v3.cc:35
void icpHandleIcpV3(int fd, Ip::Address &from, char *buf, int len)
Definition icp_v3.cc:69
const char * icp_opcode_str[]
#define xstrdup