]> git.ipfire.org Git - thirdparty/squid.git/blob - src/icp_v3.cc
C++ conversion
[thirdparty/squid.git] / src / icp_v3.cc
1
2 /*
3 * $Id: icp_v3.cc,v 1.35 2002/10/13 20:35:01 robertc 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
40 class ICP3State : public ICPState, public StoreClient {
41 public:
42 ICP3State(icp_common_t &aHeader):ICPState(aHeader){}
43 ~ICP3State();
44 void created (StoreEntry *newEntry);
45 };
46
47 static void
48 doV3Query(int fd, struct sockaddr_in from, char *buf, icp_common_t header)
49 {
50 /* We have a valid packet */
51 char *url = buf + sizeof(icp_common_t) + sizeof(u_int32_t);
52 request_t *icp_request = icpGetRequest (url, header.reqnum, fd, &from);
53 if (!icp_request)
54 return;
55 if (!icpAccessAllowed(&from, icp_request)) {
56 icpDenyAccess (&from, url, header.reqnum, fd);
57 requestDestroy(icp_request);
58 return;
59 }
60 /* The peer is allowed to use this cache */
61 ICP3State *state = new ICP3State (header);
62 state->request = icp_request;
63 state->fd = fd;
64 state->from = from;
65 state->url = xstrdup (url);
66 _StoreEntry::getPublic (state, url, METHOD_GET);
67 }
68
69 ICP3State::~ICP3State ()
70 {
71 }
72
73 void
74 ICP3State::created (StoreEntry *newEntry)
75 {
76 StoreEntry *entry = newEntry->isNull () ? NULL : newEntry;
77 debug(12, 5) ("icpHandleIcpV3: OPCODE %s\n",
78 icp_opcode_str[header.opcode]);
79 icp_opcode codeToSend;
80 if (icpCheckUdpHit(entry, request)) {
81 codeToSend = ICP_HIT;
82 } else if (icpGetCommonOpcode() == ICP_ERR)
83 codeToSend = ICP_MISS;
84 else
85 codeToSend = icpGetCommonOpcode();
86 icpCreateAndSend (codeToSend, 0, url, header.reqnum, 0, fd, &from);
87 delete this;
88 }
89
90 /* Currently Harvest cached-2.x uses ICP_VERSION_3 */
91 void
92 icpHandleIcpV3(int fd, struct sockaddr_in from, char *buf, int len)
93 {
94 if (len <= 0) {
95 debug(12, 3) ("icpHandleIcpV3: ICP message is too small\n");
96 return;
97 }
98 icp_common_t header (buf, len);
99 /*
100 * Length field should match the number of bytes read
101 */
102 if (len != header.length) {
103 debug(12, 3) ("icpHandleIcpV3: ICP message is too small\n");
104 return;
105 }
106 switch (header.opcode) {
107 case ICP_QUERY:
108 doV3Query(fd, from,buf, header);
109 break;
110
111 case ICP_HIT:
112 #if ALLOW_SOURCE_PING
113 case ICP_SECHO:
114 #endif
115 case ICP_DECHO:
116 case ICP_MISS:
117 case ICP_DENIED:
118 case ICP_MISS_NOFETCH:
119 header.handleReply(buf, &from);
120 break;
121
122 case ICP_INVALID:
123 case ICP_ERR:
124 break;
125
126 default:
127 debug(12, 0) ("icpHandleIcpV3: UNKNOWN OPCODE: %d from %s\n",
128 header.opcode, inet_ntoa(from.sin_addr));
129 break;
130 }
131 }