]> git.ipfire.org Git - thirdparty/squid.git/blob - src/icp_v3.cc
Making HttpRequest more class-like. Removed some non-class functions such
[thirdparty/squid.git] / src / icp_v3.cc
1
2 /*
3 * $Id: icp_v3.cc,v 1.40 2006/01/19 18:40:28 wessels 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):ICPState(aHeader){}
46
47 ~ICP3State();
48 void created (StoreEntry *newEntry);
49 };
50
51 static void
52
53 doV3Query(int fd, struct sockaddr_in from, char *buf, icp_common_t header)
54 {
55 /* We have a valid packet */
56 char *url = buf + sizeof(icp_common_t) + sizeof(u_int32_t);
57 HttpRequest *icp_request = icpGetRequest (url, header.reqnum, fd, &from);
58
59 if (!icp_request)
60 return;
61
62 if (!icpAccessAllowed(&from, icp_request))
63 {
64 icpDenyAccess (&from, url, header.reqnum, fd);
65 delete icp_request;
66 return;
67 }
68
69 /* The peer is allowed to use this cache */
70 ICP3State *state = new ICP3State (header);
71
72 state->request = 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 debug(12, 5) ("icpHandleIcpV3: OPCODE %s\n",
91 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 /* Currently Harvest cached-2.x uses ICP_VERSION_3 */
107 void
108
109 icpHandleIcpV3(int fd, struct sockaddr_in from, char *buf, int len)
110 {
111 if (len <= 0)
112 {
113 debug(12, 3) ("icpHandleIcpV3: ICP message is too small\n");
114 return;
115 }
116
117 icp_common_t header (buf, len);
118 /*
119 * Length field should match the number of bytes read
120 */
121
122 if (len != header.length)
123 {
124 debug(12, 3) ("icpHandleIcpV3: ICP message is too small\n");
125 return;
126 }
127
128 switch (header.opcode)
129 {
130
131 case ICP_QUERY:
132 doV3Query(fd, from,buf, header);
133 break;
134
135 case ICP_HIT:
136 #if ALLOW_SOURCE_PING
137
138 case ICP_SECHO:
139 #endif
140
141 case ICP_DECHO:
142
143 case ICP_MISS:
144
145 case ICP_DENIED:
146
147 case ICP_MISS_NOFETCH:
148 header.handleReply(buf, &from);
149 break;
150
151 case ICP_INVALID:
152
153 case ICP_ERR:
154 break;
155
156 default:
157 debug(12, 0) ("icpHandleIcpV3: UNKNOWN OPCODE: %d from %s\n",
158 header.opcode, inet_ntoa(from.sin_addr));
159 break;
160 }
161 }