]> git.ipfire.org Git - thirdparty/squid.git/blob - src/icp_v3.cc
Maintenance: automate header guards 2/3 (#1655)
[thirdparty/squid.git] / src / icp_v3.cc
1 /*
2 * Copyright (C) 1996-2023 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
11 /**
12 \defgroup ServerProtocolICPInternal3 ICPv3 Internals
13 \ingroup ServerProtocolICPAPI
14 */
15
16 #include "squid.h"
17 #include "acl/FilledChecklist.h"
18 #include "HttpRequest.h"
19 #include "ICP.h"
20 #include "Store.h"
21
22 /// \ingroup ServerProtocolICPInternal3
23 class ICP3State: public ICPState
24 {
25
26 public:
27 ICP3State(icp_common_t &aHeader, HttpRequest *aRequest) :
28 ICPState(aHeader, aRequest) {}
29
30 ~ICP3State() override = default;
31 };
32
33 /// \ingroup ServerProtocolICPInternal3
34 static void
35 doV3Query(int fd, Ip::Address &from, char *buf, icp_common_t header)
36 {
37 /* We have a valid packet */
38 char *url = buf + sizeof(icp_common_t) + sizeof(uint32_t);
39 HttpRequest *icp_request = icpGetRequest(url, header.reqnum, fd, from);
40
41 if (!icp_request)
42 return;
43
44 if (!icpAccessAllowed(from, icp_request)) {
45 icpDenyAccess (from, url, header.reqnum, fd);
46 delete icp_request;
47 return;
48 }
49
50 /* The peer is allowed to use this cache */
51 ICP3State state(header, icp_request);
52 state.fd = fd;
53 state.from = from;
54 state.url = xstrdup(url);
55
56 icp_opcode codeToSend;
57
58 if (state.isHit()) {
59 codeToSend = ICP_HIT;
60 } else if (icpGetCommonOpcode() == ICP_ERR)
61 codeToSend = ICP_MISS;
62 else
63 codeToSend = icpGetCommonOpcode();
64
65 icpCreateAndSend(codeToSend, 0, url, header.reqnum, 0, fd, from, state.al);
66 }
67
68 /// \ingroup ServerProtocolICPInternal3
69 /* Currently Harvest cached-2.x uses ICP_VERSION_3 */
70 void
71 icpHandleIcpV3(int fd, Ip::Address &from, char *buf, int len)
72 {
73 if (len <= 0) {
74 debugs(12, 3, "icpHandleIcpV3: ICP message is too small");
75 return;
76 }
77
78 icp_common_t header (buf, len);
79 /*
80 * Length field should match the number of bytes read
81 */
82
83 if (len != header.length) {
84 debugs(12, 3, "icpHandleIcpV3: ICP message is too small");
85 return;
86 }
87
88 debugs(12, 5, "OPCODE " << icp_opcode_str[header.getOpCode()] << '=' << uint8_t(header.opcode));
89
90 switch (header.opcode) {
91
92 case ICP_QUERY:
93 doV3Query(fd, from, buf, header);
94 break;
95
96 case ICP_HIT:
97
98 case ICP_DECHO:
99
100 case ICP_MISS:
101
102 case ICP_DENIED:
103
104 case ICP_MISS_NOFETCH:
105 header.handleReply(buf, from);
106 break;
107
108 case ICP_INVALID:
109
110 case ICP_ERR:
111 break;
112
113 default:
114 debugs(12, DBG_CRITICAL, "ERROR: icpHandleIcpV3: Unknown opcode: " << header.opcode << " from " << from);
115 break;
116 }
117 }
118