]> git.ipfire.org Git - thirdparty/squid.git/blob - src/icp_v3.cc
Merged from trunk
[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 state->fd = fd;
75 state->from = from;
76 state->url = xstrdup(url);
77
78 StoreEntry::getPublic (state, url, Http::METHOD_GET);
79 }
80
81 ICP3State::~ICP3State()
82 {}
83
84 void
85 ICP3State::created(StoreEntry *newEntry)
86 {
87 StoreEntry *entry = newEntry->isNull () ? NULL : newEntry;
88 debugs(12, 5, "icpHandleIcpV3: OPCODE " << icp_opcode_str[header.opcode]);
89 icp_opcode codeToSend;
90
91 if (icpCheckUdpHit(entry, request)) {
92 codeToSend = ICP_HIT;
93 } else if (icpGetCommonOpcode() == ICP_ERR)
94 codeToSend = ICP_MISS;
95 else
96 codeToSend = icpGetCommonOpcode();
97
98 icpCreateAndSend (codeToSend, 0, url, header.reqnum, 0, fd, from);
99
100 delete this;
101 }
102
103 /// \ingroup ServerProtocolICPInternal3
104 /* Currently Harvest cached-2.x uses ICP_VERSION_3 */
105 void
106 icpHandleIcpV3(int fd, Ip::Address &from, char *buf, int len)
107 {
108 if (len <= 0) {
109 debugs(12, 3, "icpHandleIcpV3: ICP message is too small");
110 return;
111 }
112
113 icp_common_t header (buf, len);
114 /*
115 * Length field should match the number of bytes read
116 */
117
118 if (len != header.length) {
119 debugs(12, 3, "icpHandleIcpV3: ICP message is too small");
120 return;
121 }
122
123 switch (header.opcode) {
124
125 case ICP_QUERY:
126 doV3Query(fd, from, buf, header);
127 break;
128
129 case ICP_HIT:
130
131 case ICP_DECHO:
132
133 case ICP_MISS:
134
135 case ICP_DENIED:
136
137 case ICP_MISS_NOFETCH:
138 header.handleReply(buf, from);
139 break;
140
141 case ICP_INVALID:
142
143 case ICP_ERR:
144 break;
145
146 default:
147 debugs(12, DBG_CRITICAL, "icpHandleIcpV3: UNKNOWN OPCODE: " << header.opcode << " from " << from);
148 break;
149 }
150 }