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