]> git.ipfire.org Git - thirdparty/squid.git/blob - src/icp_v3.cc
Merged from trunk 13172.
[thirdparty/squid.git] / src / icp_v3.cc
1 /*
2 * DEBUG: section 12 Internet Cache Protocol (ICP)
3 * AUTHOR: Duane Wessels
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32
33 /**
34 \defgroup ServerProtocolICPInternal3 ICPv3 Internals
35 \ingroup ServerProtocolICPAPI
36 */
37
38 #include "squid.h"
39 #include "HttpRequest.h"
40 #include "ICP.h"
41 #include "Store.h"
42
43 /// \ingroup ServerProtocolICPInternal3
44 class ICP3State : public ICPState, public StoreClient
45 {
46
47 public:
48 ICP3State(icp_common_t &aHeader, HttpRequest *aRequest) :
49 ICPState(aHeader, aRequest) {}
50
51 ~ICP3State();
52 void created (StoreEntry *newEntry);
53 };
54
55 /// \ingroup ServerProtocolICPInternal3
56 static void
57 doV3Query(int fd, Ip::Address &from, char *buf, icp_common_t header)
58 {
59 /* We have a valid packet */
60 char *url = buf + sizeof(icp_common_t) + sizeof(uint32_t);
61 HttpRequest *icp_request = icpGetRequest (url, header.reqnum, fd, from);
62
63 if (!icp_request)
64 return;
65
66 if (!icpAccessAllowed(from, icp_request)) {
67 icpDenyAccess (from, url, header.reqnum, fd);
68 delete icp_request;
69 return;
70 }
71
72 /* The peer is allowed to use this cache */
73 ICP3State *state = new ICP3State (header, icp_request);
74
75 state->fd = fd;
76
77 state->from = from;
78
79 state->url = xstrdup (url);
80
81 StoreEntry::getPublic (state, url, Http::METHOD_GET);
82 }
83
84 ICP3State::~ICP3State()
85 {}
86
87 void
88 ICP3State::created(StoreEntry *newEntry)
89 {
90 StoreEntry *entry = newEntry->isNull () ? NULL : newEntry;
91 debugs(12, 5, "icpHandleIcpV3: OPCODE " << icp_opcode_str[header.opcode]);
92 icp_opcode codeToSend;
93
94 if (icpCheckUdpHit(entry, request)) {
95 codeToSend = ICP_HIT;
96 } else if (icpGetCommonOpcode() == ICP_ERR)
97 codeToSend = ICP_MISS;
98 else
99 codeToSend = icpGetCommonOpcode();
100
101 icpCreateAndSend (codeToSend, 0, url, header.reqnum, 0, fd, from);
102
103 delete this;
104 }
105
106 /// \ingroup ServerProtocolICPInternal3
107 /* Currently Harvest cached-2.x uses ICP_VERSION_3 */
108 void
109 icpHandleIcpV3(int fd, Ip::Address &from, char *buf, int len)
110 {
111 if (len <= 0) {
112 debugs(12, 3, "icpHandleIcpV3: ICP message is too small");
113 return;
114 }
115
116 icp_common_t header (buf, len);
117 /*
118 * Length field should match the number of bytes read
119 */
120
121 if (len != header.length) {
122 debugs(12, 3, "icpHandleIcpV3: ICP message is too small");
123 return;
124 }
125
126 switch (header.opcode) {
127
128 case ICP_QUERY:
129 doV3Query(fd, from, buf, header);
130 break;
131
132 case ICP_HIT:
133
134 case ICP_DECHO:
135
136 case ICP_MISS:
137
138 case ICP_DENIED:
139
140 case ICP_MISS_NOFETCH:
141 header.handleReply(buf, from);
142 break;
143
144 case ICP_INVALID:
145
146 case ICP_ERR:
147 break;
148
149 default:
150 debugs(12, DBG_CRITICAL, "icpHandleIcpV3: UNKNOWN OPCODE: " << header.opcode << " from " << from);
151 break;
152 }
153 }