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