]>
Commit | Line | Data |
---|---|---|
9cef6668 | 1 | /* |
cc192b50 | 2 | * DEBUG: section 12 Internet Cache Protocol (ICP) |
9cef6668 | 3 | * AUTHOR: Duane Wessels |
4 | * | |
2b6662ba | 5 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
9cef6668 | 6 | * ---------------------------------------------------------- |
7 | * | |
2b6662ba | 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. | |
9cef6668 | 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. | |
26ac0430 | 21 | * |
9cef6668 | 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. | |
26ac0430 | 26 | * |
9cef6668 | 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 | ||
63be0a78 | 33 | /** |
34 | \defgroup ServerProtocolICPInternal2 ICPv2 Internals | |
35 | \ingroup ServerProtocolICPAPI | |
36 | */ | |
37 | ||
582c2af2 FC |
38 | #include "squid.h" |
39 | #include "AccessLogEntry.h" | |
40 | #include "acl/Acl.h" | |
41 | #include "acl/FilledChecklist.h" | |
95e6d864 | 42 | #include "client_db.h" |
063dc1eb | 43 | #include "comm.h" |
09cee743 | 44 | #include "comm/Connection.h" |
d841c88d | 45 | #include "comm/Loops.h" |
09cee743 | 46 | #include "comm/UdpOpenDialer.h" |
c4ad1349 | 47 | #include "fd.h" |
f9b72e0c | 48 | #include "HttpRequest.h" |
9b5c4a9a | 49 | #include "icmp/net_db.h" |
582c2af2 | 50 | #include "ICP.h" |
96d89ea0 | 51 | #include "ip/Address.h" |
e0f8b709 | 52 | #include "ip/tools.h" |
714e68b7 | 53 | #include "ipcache.h" |
582c2af2 | 54 | #include "md5.h" |
afabcc13 | 55 | #include "multicast.h" |
f0ba2534 | 56 | #include "neighbors.h" |
c6f15d40 | 57 | #include "refresh.h" |
1fa9b1a7 | 58 | #include "rfc1738.h" |
4d5904f7 | 59 | #include "SquidConfig.h" |
582c2af2 FC |
60 | #include "SquidTime.h" |
61 | #include "StatCounters.h" | |
62 | #include "Store.h" | |
fb548aaf | 63 | #include "store_key_md5.h" |
582c2af2 | 64 | #include "SwapDir.h" |
4e540555 | 65 | #include "tools.h" |
582c2af2 | 66 | #include "wordlist.h" |
7a2f978b | 67 | |
21d845b1 FC |
68 | #if HAVE_ERRNO_H |
69 | #include <errno.h> | |
70 | #endif | |
71 | ||
09cee743 | 72 | static void icpIncomingConnectionOpened(const Comm::ConnectionPointer &conn, int errNo); |
013e320c | 73 | |
63be0a78 | 74 | /// \ingroup ServerProtocolICPInternal2 |
02c8dde5 | 75 | static void icpLogIcp(const Ip::Address &, LogTags, int, const char *, int); |
62e76326 | 76 | |
63be0a78 | 77 | /// \ingroup ServerProtocolICPInternal2 |
b7ac5457 | 78 | static void icpHandleIcpV2(int, Ip::Address &, char *, int); |
63be0a78 | 79 | |
80 | /// \ingroup ServerProtocolICPInternal2 | |
071a3ae7 | 81 | static void icpCount(void *, int, size_t, int); |
17b6e784 | 82 | |
63be0a78 | 83 | /** |
84 | \ingroup ServerProtocolICPInternal2 | |
48382032 | 85 | * IcpQueueHead is global so comm_incoming() knows whether or not |
86 | * to call icpUdpSendQueue. | |
87 | */ | |
d4cb310b | 88 | static icpUdpData *IcpQueueHead = NULL; |
63be0a78 | 89 | /// \ingroup ServerProtocolICPInternal2 |
90 | static icpUdpData *IcpQueueTail = NULL; | |
7a2f978b | 91 | |
e1f7507e | 92 | /// \ingroup ServerProtocolICPInternal2 |
e0d28505 AJ |
93 | Comm::ConnectionPointer icpIncomingConn = NULL; |
94 | /// \ingroup ServerProtocolICPInternal2 | |
95 | Comm::ConnectionPointer icpOutgoingConn = NULL; | |
96 | ||
e6ccf245 | 97 | /* icp_common_t */ |
fad2588a | 98 | _icp_common_t::_icp_common_t() : |
f53156c2 A |
99 | opcode(ICP_INVALID), version(0), length(0), reqnum(0), |
100 | flags(0), pad(0), shostid(0) | |
62e76326 | 101 | {} |
e6ccf245 | 102 | |
fad2588a | 103 | _icp_common_t::_icp_common_t(char *buf, unsigned int len) : |
f53156c2 | 104 | opcode(ICP_INVALID), version(0), reqnum(0), flags(0), pad(0), shostid(0) |
e6ccf245 | 105 | { |
106 | if (len < sizeof(_icp_common_t)) { | |
62e76326 | 107 | /* mark as invalid */ |
108 | length = len + 1; | |
109 | return; | |
e6ccf245 | 110 | } |
62e76326 | 111 | |
41d00cd3 | 112 | memcpy(this, buf, sizeof(icp_common_t)); |
e6ccf245 | 113 | /* |
114 | * Convert network order sensitive fields | |
115 | */ | |
116 | length = ntohs(length); | |
117 | reqnum = ntohl(reqnum); | |
118 | flags = ntohl(flags); | |
119 | pad = ntohl(pad); | |
120 | } | |
121 | ||
122 | icp_opcode | |
123 | _icp_common_t::getOpCode() const | |
124 | { | |
125 | if (opcode > (char)ICP_END) | |
62e76326 | 126 | return ICP_INVALID; |
127 | ||
e6ccf245 | 128 | return (icp_opcode)opcode; |
129 | } | |
130 | ||
131 | /* ICPState */ | |
132 | ||
63be0a78 | 133 | ICPState::ICPState(icp_common_t &aHeader, HttpRequest *aRequest): |
26ac0430 | 134 | header(aHeader), |
b248c2a3 | 135 | request(aRequest), |
62e76326 | 136 | fd(-1), |
137 | url(NULL) | |
b248c2a3 AJ |
138 | { |
139 | HTTPMSGLOCK(request); | |
140 | } | |
e6ccf245 | 141 | |
142 | ICPState::~ICPState() | |
143 | { | |
144 | safe_free(url); | |
f72fb56b | 145 | HTTPMSGUNLOCK(request); |
e6ccf245 | 146 | } |
147 | ||
e6ccf245 | 148 | /* End ICPState */ |
149 | ||
150 | /* ICP2State */ | |
62e76326 | 151 | |
63be0a78 | 152 | /// \ingroup ServerProtocolICPInternal2 |
153 | class ICP2State : public ICPState, public StoreClient | |
62e76326 | 154 | { |
155 | ||
156 | public: | |
f72fb56b | 157 | ICP2State(icp_common_t & aHeader, HttpRequest *aRequest): |
26ac0430 | 158 | ICPState(aHeader, aRequest),rtt(0),src_rtt(0),flags(0) {} |
62e76326 | 159 | |
160 | ~ICP2State(); | |
e6ccf245 | 161 | void created(StoreEntry * newEntry); |
162 | ||
163 | int rtt; | |
164 | int src_rtt; | |
09aabd84 | 165 | uint32_t flags; |
e6ccf245 | 166 | }; |
167 | ||
63be0a78 | 168 | ICP2State::~ICP2State() |
62e76326 | 169 | {} |
e6ccf245 | 170 | |
171 | void | |
63be0a78 | 172 | ICP2State::created(StoreEntry *newEntry) |
e6ccf245 | 173 | { |
174 | StoreEntry *entry = newEntry->isNull () ? NULL : newEntry; | |
bf8fe701 | 175 | debugs(12, 5, "icpHandleIcpV2: OPCODE " << icp_opcode_str[header.opcode]); |
e6ccf245 | 176 | icp_opcode codeToSend; |
62e76326 | 177 | |
e6ccf245 | 178 | if (icpCheckUdpHit(entry, request)) { |
62e76326 | 179 | codeToSend = ICP_HIT; |
e6ccf245 | 180 | } else { |
9b5c4a9a | 181 | #if USE_ICMP |
62e76326 | 182 | if (Config.onoff.test_reachability && rtt == 0) { |
cc192b50 | 183 | if ((rtt = netdbHostRtt(request->GetHost())) == 0) |
184 | netdbPingSite(request->GetHost()); | |
62e76326 | 185 | } |
9b5c4a9a | 186 | #endif /* USE_ICMP */ |
62e76326 | 187 | |
188 | if (icpGetCommonOpcode() != ICP_ERR) | |
189 | codeToSend = icpGetCommonOpcode(); | |
190 | else if (Config.onoff.test_reachability && rtt == 0) | |
191 | codeToSend = ICP_MISS_NOFETCH; | |
192 | else | |
193 | codeToSend = ICP_MISS; | |
e6ccf245 | 194 | } |
62e76326 | 195 | |
cc192b50 | 196 | icpCreateAndSend(codeToSend, flags, url, header.reqnum, src_rtt, fd, from); |
e6ccf245 | 197 | delete this; |
198 | } | |
199 | ||
200 | /* End ICP2State */ | |
201 | ||
63be0a78 | 202 | /// \ingroup ServerProtocolICPInternal2 |
7a2f978b | 203 | static void |
02c8dde5 | 204 | icpLogIcp(const Ip::Address &caddr, LogTags logcode, int len, const char *url, int delay) |
7a2f978b | 205 | { |
41ebd397 | 206 | AccessLogEntry::Pointer al = new AccessLogEntry(); |
62e76326 | 207 | |
96c617da | 208 | if (LOG_TAG_NONE == logcode) |
62e76326 | 209 | return; |
210 | ||
071a3ae7 | 211 | if (LOG_ICP_QUERY == logcode) |
62e76326 | 212 | return; |
213 | ||
0c3d3f65 | 214 | clientdbUpdate(caddr, logcode, AnyP::PROTO_ICP, len); |
62e76326 | 215 | |
7a2f978b | 216 | if (!Config.onoff.log_udp) |
62e76326 | 217 | return; |
218 | ||
41ebd397 | 219 | al->icp.opcode = ICP_QUERY; |
62e76326 | 220 | |
41ebd397 | 221 | al->url = url; |
62e76326 | 222 | |
41ebd397 | 223 | al->cache.caddr = caddr; |
62e76326 | 224 | |
d6df21d2 | 225 | // XXX: move to use icp.clientReply instead |
cc0ca3b9 | 226 | al->http.clientReplySz.payloadData = len; |
62e76326 | 227 | |
41ebd397 | 228 | al->cache.code = logcode; |
62e76326 | 229 | |
41ebd397 | 230 | al->cache.msec = delay; |
62e76326 | 231 | |
41ebd397 | 232 | accessLogLog(al, NULL); |
7a2f978b | 233 | } |
234 | ||
63be0a78 | 235 | /// \ingroup ServerProtocolICPInternal2 |
48382032 | 236 | void |
17b6e784 | 237 | icpUdpSendQueue(int fd, void *unused) |
7a2f978b | 238 | { |
17b6e784 | 239 | icpUdpData *q; |
62e76326 | 240 | |
48382032 | 241 | while ((q = IcpQueueHead) != NULL) { |
b115733c | 242 | int delay = tvSubUsec(q->queue_time, current_time); |
62e76326 | 243 | /* increment delay to prevent looping */ |
b115733c | 244 | const int x = icpUdpSend(fd, q->address, (icp_common_t *) q->msg, q->logcode, ++delay); |
62e76326 | 245 | IcpQueueHead = q->next; |
b115733c | 246 | xfree(q); |
62e76326 | 247 | |
248 | if (x < 0) | |
249 | break; | |
7a2f978b | 250 | } |
251 | } | |
252 | ||
e6ccf245 | 253 | _icp_common_t * |
254 | _icp_common_t::createMessage( | |
7a2f978b | 255 | icp_opcode opcode, |
256 | int flags, | |
257 | const char *url, | |
258 | int reqnum, | |
259 | int pad) | |
260 | { | |
261 | char *buf = NULL; | |
262 | icp_common_t *headerp = NULL; | |
263 | char *urloffset = NULL; | |
264 | int buf_len; | |
265 | buf_len = sizeof(icp_common_t) + strlen(url) + 1; | |
62e76326 | 266 | |
27cd7235 | 267 | if (opcode == ICP_QUERY) |
09aabd84 | 268 | buf_len += sizeof(uint32_t); |
62e76326 | 269 | |
e6ccf245 | 270 | buf = (char *) xcalloc(buf_len, 1); |
62e76326 | 271 | |
7a2f978b | 272 | headerp = (icp_common_t *) (void *) buf; |
62e76326 | 273 | |
79d39a72 | 274 | headerp->opcode = (char) opcode; |
62e76326 | 275 | |
7a2f978b | 276 | headerp->version = ICP_VERSION_CURRENT; |
62e76326 | 277 | |
09aabd84 | 278 | headerp->length = (uint16_t) htons(buf_len); |
62e76326 | 279 | |
7a2f978b | 280 | headerp->reqnum = htonl(reqnum); |
62e76326 | 281 | |
7a2f978b | 282 | headerp->flags = htonl(flags); |
62e76326 | 283 | |
7a2f978b | 284 | headerp->pad = htonl(pad); |
62e76326 | 285 | |
9ca56f05 | 286 | headerp->shostid = 0; |
62e76326 | 287 | |
7a2f978b | 288 | urloffset = buf + sizeof(icp_common_t); |
62e76326 | 289 | |
27cd7235 | 290 | if (opcode == ICP_QUERY) |
09aabd84 | 291 | urloffset += sizeof(uint32_t); |
62e76326 | 292 | |
41d00cd3 | 293 | memcpy(urloffset, url, strlen(url)); |
62e76326 | 294 | |
e6ccf245 | 295 | return (icp_common_t *)buf; |
7a2f978b | 296 | } |
297 | ||
17b6e784 | 298 | int |
7a2f978b | 299 | icpUdpSend(int fd, |
b7ac5457 | 300 | const Ip::Address &to, |
62e76326 | 301 | icp_common_t * msg, |
02c8dde5 | 302 | LogTags logcode, |
62e76326 | 303 | int delay) |
7a2f978b | 304 | { |
17b6e784 | 305 | icpUdpData *queue; |
8e68922c | 306 | int x; |
17b6e784 | 307 | int len; |
308 | len = (int) ntohs(msg->length); | |
bf8fe701 | 309 | debugs(12, 5, "icpUdpSend: FD " << fd << " sending " << |
cc192b50 | 310 | icp_opcode_str[msg->opcode] << ", " << len << " bytes to " << to); |
bf8fe701 | 311 | |
cc192b50 | 312 | x = comm_udp_sendto(fd, to, msg, len); |
62e76326 | 313 | |
26ac0430 | 314 | if (x >= 0) { |
62e76326 | 315 | /* successfully written */ |
cc192b50 | 316 | icpLogIcp(to, logcode, len, (char *) (msg + 1), delay); |
62e76326 | 317 | icpCount(msg, SENT, (size_t) len, delay); |
318 | safe_free(msg); | |
26ac0430 | 319 | } else if (0 == delay) { |
62e76326 | 320 | /* send failed, but queue it */ |
321 | queue = (icpUdpData *) xcalloc(1, sizeof(icpUdpData)); | |
cc192b50 | 322 | queue->address = to; |
62e76326 | 323 | queue->msg = msg; |
324 | queue->len = (int) ntohs(msg->length); | |
325 | queue->queue_time = current_time; | |
326 | queue->logcode = logcode; | |
327 | ||
328 | if (IcpQueueHead == NULL) { | |
329 | IcpQueueHead = queue; | |
330 | IcpQueueTail = queue; | |
331 | } else if (IcpQueueTail == IcpQueueHead) { | |
332 | IcpQueueTail = queue; | |
333 | IcpQueueHead->next = queue; | |
334 | } else { | |
335 | IcpQueueTail->next = queue; | |
336 | IcpQueueTail = queue; | |
337 | } | |
338 | ||
d841c88d | 339 | Comm::SetSelect(fd, COMM_SELECT_WRITE, icpUdpSendQueue, NULL, 0); |
95dc7ff4 | 340 | ++statCounter.icp.replies_queued; |
26ac0430 | 341 | } else { |
62e76326 | 342 | /* don't queue it */ |
95dc7ff4 | 343 | ++statCounter.icp.replies_dropped; |
8e68922c | 344 | } |
62e76326 | 345 | |
17b6e784 | 346 | return x; |
7a2f978b | 347 | } |
348 | ||
349 | int | |
190154cf | 350 | icpCheckUdpHit(StoreEntry * e, HttpRequest * request) |
7a2f978b | 351 | { |
352 | if (e == NULL) | |
62e76326 | 353 | return 0; |
354 | ||
3900307b | 355 | if (!e->validToSend()) |
62e76326 | 356 | return 0; |
357 | ||
7a2f978b | 358 | if (Config.onoff.icp_hit_stale) |
62e76326 | 359 | return 1; |
360 | ||
829a9357 | 361 | if (refreshCheckICP(e, request)) |
62e76326 | 362 | return 0; |
363 | ||
7a2f978b | 364 | return 1; |
365 | } | |
7a2f978b | 366 | |
63be0a78 | 367 | /** |
bef81ea5 | 368 | * This routine selects an ICP opcode for ICP misses. |
63be0a78 | 369 | * |
370 | \retval ICP_ERR no opcode selected here | |
371 | \retval ICP_MISS_NOFETCH store is rebuilding, no fetch is possible yet | |
bef81ea5 | 372 | */ |
e6ccf245 | 373 | icp_opcode |
374 | icpGetCommonOpcode() | |
375 | { | |
bef81ea5 | 376 | /* if store is rebuilding, return a UDP_MISS_NOFETCH */ |
62e76326 | 377 | |
871899ca | 378 | if ((StoreController::store_dirs_rebuilding && opt_reload_hit_only) || |
62e76326 | 379 | hit_only_mode_until > squid_curtime) { |
380 | return ICP_MISS_NOFETCH; | |
e6ccf245 | 381 | } |
62e76326 | 382 | |
e6ccf245 | 383 | return ICP_ERR; |
384 | } | |
385 | ||
02c8dde5 | 386 | LogTags |
e6ccf245 | 387 | icpLogFromICPCode(icp_opcode opcode) |
388 | { | |
389 | if (opcode == ICP_ERR) | |
62e76326 | 390 | return LOG_UDP_INVALID; |
391 | ||
e6ccf245 | 392 | if (opcode == ICP_DENIED) |
62e76326 | 393 | return LOG_UDP_DENIED; |
394 | ||
e6ccf245 | 395 | if (opcode == ICP_HIT) |
62e76326 | 396 | return LOG_UDP_HIT; |
397 | ||
e6ccf245 | 398 | if (opcode == ICP_MISS) |
62e76326 | 399 | return LOG_UDP_MISS; |
400 | ||
e6ccf245 | 401 | if (opcode == ICP_MISS_NOFETCH) |
62e76326 | 402 | return LOG_UDP_MISS_NOFETCH; |
403 | ||
e6ccf245 | 404 | fatal("expected ICP opcode\n"); |
62e76326 | 405 | |
e6ccf245 | 406 | return LOG_UDP_INVALID; |
407 | } | |
408 | ||
409 | void | |
b7ac5457 | 410 | icpCreateAndSend(icp_opcode opcode, int flags, char const *url, int reqnum, int pad, int fd, const Ip::Address &from) |
e6ccf245 | 411 | { |
412 | icp_common_t *reply = _icp_common_t::createMessage(opcode, flags, url, reqnum, pad); | |
413 | icpUdpSend(fd, from, reply, icpLogFromICPCode(opcode), 0); | |
414 | } | |
415 | ||
416 | void | |
b7ac5457 | 417 | icpDenyAccess(Ip::Address &from, char *url, int reqnum, int fd) |
e6ccf245 | 418 | { |
cc192b50 | 419 | debugs(12, 2, "icpDenyAccess: Access Denied for " << from << " by " << AclMatchedName << "."); |
62e76326 | 420 | |
26ac0430 | 421 | if (clientdbCutoffDenied(from)) { |
62e76326 | 422 | /* |
423 | * count this DENIED query in the clientdb, even though | |
424 | * we're not sending an ICP reply... | |
425 | */ | |
0c3d3f65 | 426 | clientdbUpdate(from, LOG_UDP_DENIED, AnyP::PROTO_ICP, 0); |
26ac0430 | 427 | } else { |
62e76326 | 428 | icpCreateAndSend(ICP_DENIED, 0, url, reqnum, 0, fd, from); |
e6ccf245 | 429 | } |
430 | } | |
431 | ||
2efeb0b7 | 432 | bool |
b7ac5457 | 433 | icpAccessAllowed(Ip::Address &from, HttpRequest * icp_request) |
7a2f978b | 434 | { |
638402dd | 435 | /* absent any explicit rules, we deny all */ |
b50e327b | 436 | if (!Config.accessList.icp) |
638402dd | 437 | return false; |
b50e327b | 438 | |
c0941a6a | 439 | ACLFilledChecklist checklist(Config.accessList.icp, icp_request, NULL); |
cc192b50 | 440 | checklist.src_addr = from; |
4dd643d5 | 441 | checklist.my_addr.setNoAddr(); |
2efeb0b7 | 442 | return (checklist.fastCheck() == ACCESS_ALLOWED); |
e6ccf245 | 443 | } |
444 | ||
445 | char const * | |
446 | icpGetUrlToSend(char *url) | |
447 | { | |
448 | if (strpbrk(url, w_space)) | |
62e76326 | 449 | return rfc1738_escape(url); |
e6ccf245 | 450 | else |
62e76326 | 451 | return url; |
e6ccf245 | 452 | } |
453 | ||
190154cf | 454 | HttpRequest * |
b7ac5457 | 455 | icpGetRequest(char *url, int reqnum, int fd, Ip::Address &from) |
e6ccf245 | 456 | { |
26ac0430 | 457 | if (strpbrk(url, w_space)) { |
62e76326 | 458 | url = rfc1738_escape(url); |
459 | icpCreateAndSend(ICP_ERR, 0, rfc1738_escape(url), reqnum, 0, fd, from); | |
460 | return NULL; | |
e6ccf245 | 461 | } |
62e76326 | 462 | |
190154cf | 463 | HttpRequest *result; |
62e76326 | 464 | |
c21ad0f5 | 465 | if ((result = HttpRequest::CreateFromUrl(url)) == NULL) |
62e76326 | 466 | icpCreateAndSend(ICP_ERR, 0, url, reqnum, 0, fd, from); |
467 | ||
e6ccf245 | 468 | return result; |
469 | ||
470 | } | |
471 | ||
472 | static void | |
b7ac5457 | 473 | doV2Query(int fd, Ip::Address &from, char *buf, icp_common_t header) |
e6ccf245 | 474 | { |
475 | int rtt = 0; | |
7a2f978b | 476 | int src_rtt = 0; |
09aabd84 | 477 | uint32_t flags = 0; |
e6ccf245 | 478 | /* We have a valid packet */ |
09aabd84 | 479 | char *url = buf + sizeof(icp_common_t) + sizeof(uint32_t); |
cc192b50 | 480 | HttpRequest *icp_request = icpGetRequest(url, header.reqnum, fd, from); |
62e76326 | 481 | |
e6ccf245 | 482 | if (!icp_request) |
62e76326 | 483 | return; |
484 | ||
6dd9f4bd | 485 | HTTPMSGLOCK(icp_request); |
319bf5a7 | 486 | |
26ac0430 | 487 | if (!icpAccessAllowed(from, icp_request)) { |
cc192b50 | 488 | icpDenyAccess(from, url, header.reqnum, fd); |
6dd9f4bd | 489 | HTTPMSGUNLOCK(icp_request); |
62e76326 | 490 | return; |
e6ccf245 | 491 | } |
9b5c4a9a | 492 | #if USE_ICMP |
26ac0430 | 493 | if (header.flags & ICP_FLAG_SRC_RTT) { |
cc192b50 | 494 | rtt = netdbHostRtt(icp_request->GetHost()); |
495 | int hops = netdbHostHops(icp_request->GetHost()); | |
62e76326 | 496 | src_rtt = ((hops & 0xFFFF) << 16) | (rtt & 0xFFFF); |
497 | ||
498 | if (rtt) | |
499 | flags |= ICP_FLAG_SRC_RTT; | |
e6ccf245 | 500 | } |
9b5c4a9a | 501 | #endif /* USE_ICMP */ |
62e76326 | 502 | |
e6ccf245 | 503 | /* The peer is allowed to use this cache */ |
f72fb56b | 504 | ICP2State *state = new ICP2State (header, icp_request); |
62e76326 | 505 | |
e6ccf245 | 506 | state->fd = fd; |
62e76326 | 507 | |
e6ccf245 | 508 | state->from = from; |
62e76326 | 509 | |
e6ccf245 | 510 | state->url = xstrdup (url); |
62e76326 | 511 | |
e6ccf245 | 512 | state->flags = flags; |
62e76326 | 513 | |
e6ccf245 | 514 | state->rtt = rtt; |
62e76326 | 515 | |
e6ccf245 | 516 | state->src_rtt = src_rtt; |
62e76326 | 517 | |
c2a7cefd | 518 | StoreEntry::getPublic (state, url, Http::METHOD_GET); |
319bf5a7 | 519 | |
6dd9f4bd | 520 | HTTPMSGUNLOCK(icp_request); |
e6ccf245 | 521 | } |
522 | ||
523 | void | |
b7ac5457 | 524 | _icp_common_t::handleReply(char *buf, Ip::Address &from) |
e6ccf245 | 525 | { |
26ac0430 | 526 | if (neighbors_do_private_keys && reqnum == 0) { |
fa84c01d FC |
527 | debugs(12, DBG_CRITICAL, "icpHandleIcpV2: Neighbor " << from << " returned reqnum = 0"); |
528 | debugs(12, DBG_CRITICAL, "icpHandleIcpV2: Disabling use of private keys"); | |
62e76326 | 529 | neighbors_do_private_keys = 0; |
e6ccf245 | 530 | } |
62e76326 | 531 | |
e6ccf245 | 532 | char *url = buf + sizeof(icp_common_t); |
cc192b50 | 533 | debugs(12, 3, "icpHandleIcpV2: " << icp_opcode_str[opcode] << " from " << from << " for '" << url << "'"); |
bf8fe701 | 534 | |
e6ccf245 | 535 | const cache_key *key = icpGetCacheKey(url, (int) reqnum); |
536 | /* call neighborsUdpAck even if ping_status != PING_WAITING */ | |
537 | neighborsUdpAck(key, this, from); | |
538 | } | |
539 | ||
540 | static void | |
b7ac5457 | 541 | icpHandleIcpV2(int fd, Ip::Address &from, char *buf, int len) |
e6ccf245 | 542 | { |
26ac0430 | 543 | if (len <= 0) { |
bf8fe701 | 544 | debugs(12, 3, "icpHandleIcpV2: ICP message is too small"); |
62e76326 | 545 | return; |
e6ccf245 | 546 | } |
62e76326 | 547 | |
e6ccf245 | 548 | icp_common_t header(buf, len); |
7b83b3d9 | 549 | /* |
550 | * Length field should match the number of bytes read | |
551 | */ | |
62e76326 | 552 | |
26ac0430 | 553 | if (len != header.length) { |
bf8fe701 | 554 | debugs(12, 3, "icpHandleIcpV2: ICP message is too small"); |
62e76326 | 555 | return; |
7b83b3d9 | 556 | } |
62e76326 | 557 | |
26ac0430 | 558 | switch (header.opcode) { |
62e76326 | 559 | |
27cd7235 | 560 | case ICP_QUERY: |
62e76326 | 561 | /* We have a valid packet */ |
562 | doV2Query(fd, from, buf, header); | |
563 | break; | |
7a2f978b | 564 | |
27cd7235 | 565 | case ICP_HIT: |
62e76326 | 566 | |
27cd7235 | 567 | case ICP_DECHO: |
62e76326 | 568 | |
27cd7235 | 569 | case ICP_MISS: |
62e76326 | 570 | |
27cd7235 | 571 | case ICP_DENIED: |
62e76326 | 572 | |
27cd7235 | 573 | case ICP_MISS_NOFETCH: |
cc192b50 | 574 | header.handleReply(buf, from); |
62e76326 | 575 | break; |
7a2f978b | 576 | |
27cd7235 | 577 | case ICP_INVALID: |
62e76326 | 578 | |
27cd7235 | 579 | case ICP_ERR: |
62e76326 | 580 | break; |
7a2f978b | 581 | |
582 | default: | |
fa84c01d | 583 | debugs(12, DBG_CRITICAL, "icpHandleIcpV2: UNKNOWN OPCODE: " << header.opcode << " from " << from); |
bf8fe701 | 584 | |
62e76326 | 585 | break; |
7a2f978b | 586 | } |
7a2f978b | 587 | } |
588 | ||
589 | #ifdef ICP_PKT_DUMP | |
590 | static void | |
591 | icpPktDump(icp_common_t * pkt) | |
592 | { | |
b7ac5457 | 593 | Ip::Address a; |
7a2f978b | 594 | |
4a7a3d56 | 595 | debugs(12, 9, "opcode: " << std::setw(3) << pkt->opcode << " " << icp_opcode_str[pkt->opcode]); |
596 | debugs(12, 9, "version: "<< std::left << std::setw(8) << pkt->version); | |
597 | debugs(12, 9, "length: "<< std::left << std::setw(8) << ntohs(pkt->length)); | |
bf8fe701 | 598 | debugs(12, 9, "reqnum: "<< std::left << std::setw(8) << ntohl(pkt->reqnum)); |
599 | debugs(12, 9, "flags: "<< std::left << std::hex << std::setw(8) << ntohl(pkt->flags)); | |
cc192b50 | 600 | a = (struct in_addr)pkt->shostid; |
601 | debugs(12, 9, "shostid: " << a ); | |
bf8fe701 | 602 | debugs(12, 9, "payload: " << (char *) pkt + sizeof(icp_common_t)); |
7a2f978b | 603 | } |
62e76326 | 604 | |
7a2f978b | 605 | #endif |
606 | ||
607 | void | |
ba4f8e5a | 608 | icpHandleUdp(int sock, void *data) |
7a2f978b | 609 | { |
d193a436 | 610 | int *N = &incoming_sockets_accepted; |
62e76326 | 611 | |
b7ac5457 | 612 | Ip::Address from; |
7a2f978b | 613 | LOCAL_ARRAY(char, buf, SQUID_UDP_SO_RCVBUF); |
0b6d1955 | 614 | int len; |
7a2f978b | 615 | int icp_version; |
65d448bc | 616 | int max = INCOMING_UDP_MAX; |
d841c88d | 617 | Comm::SetSelect(sock, COMM_SELECT_READ, icpHandleUdp, NULL, 0); |
62e76326 | 618 | |
5e263176 FC |
619 | while (max) { |
620 | --max; | |
62e76326 | 621 | len = comm_udp_recvfrom(sock, |
622 | buf, | |
623 | SQUID_UDP_SO_RCVBUF - 1, | |
624 | 0, | |
cc192b50 | 625 | from); |
62e76326 | 626 | |
627 | if (len == 0) | |
628 | break; | |
629 | ||
630 | if (len < 0) { | |
631 | if (ignoreErrno(errno)) | |
632 | break; | |
633 | ||
1191b93b | 634 | #if _SQUID_LINUX_ |
62e76326 | 635 | /* Some Linux systems seem to set the FD for reading and then |
636 | * return ECONNREFUSED when sendto() fails and generates an ICMP | |
637 | * port unreachable message. */ | |
638 | /* or maybe an EHOSTUNREACH "No route to host" message */ | |
639 | if (errno != ECONNREFUSED && errno != EHOSTUNREACH) | |
7a2f978b | 640 | #endif |
62e76326 | 641 | |
e0236918 | 642 | debugs(50, DBG_IMPORTANT, "icpHandleUdp: FD " << sock << " recvfrom: " << xstrerror()); |
62e76326 | 643 | |
644 | break; | |
645 | } | |
646 | ||
95dc7ff4 | 647 | ++(*N); |
62e76326 | 648 | icpCount(buf, RECV, (size_t) len, 0); |
649 | buf[len] = '\0'; | |
bf8fe701 | 650 | debugs(12, 4, "icpHandleUdp: FD " << sock << ": received " << |
cc192b50 | 651 | (unsigned long int)len << " bytes from " << from); |
bf8fe701 | 652 | |
7a2f978b | 653 | #ifdef ICP_PACKET_DUMP |
62e76326 | 654 | |
655 | icpPktDump(buf); | |
7a2f978b | 656 | #endif |
62e76326 | 657 | |
0b6d1955 | 658 | if ((size_t) len < sizeof(icp_common_t)) { |
bf8fe701 | 659 | debugs(12, 4, "icpHandleUdp: Ignoring too-small UDP packet"); |
62e76326 | 660 | break; |
661 | } | |
662 | ||
663 | icp_version = (int) buf[1]; /* cheat! */ | |
664 | ||
1b76e6c1 | 665 | if (icpOutgoingConn->local == from) |
7c8931a1 AJ |
666 | // ignore ICP packets which loop back (multicast usually) |
667 | debugs(12, 4, "icpHandleUdp: Ignoring UDP packet sent by myself"); | |
668 | else if (icp_version == ICP_VERSION_2) | |
62e76326 | 669 | icpHandleIcpV2(sock, from, buf, len); |
670 | else if (icp_version == ICP_VERSION_3) | |
671 | icpHandleIcpV3(sock, from, buf, len); | |
672 | else | |
e0236918 | 673 | debugs(12, DBG_IMPORTANT, "WARNING: Unused ICP version " << icp_version << |
26ac0430 | 674 | " received from " << from); |
7a2f978b | 675 | } |
7a2f978b | 676 | } |
15df8349 | 677 | |
678 | void | |
65d448bc | 679 | icpOpenPorts(void) |
15df8349 | 680 | { |
09aabd84 | 681 | uint16_t port; |
62e76326 | 682 | |
15df8349 | 683 | if ((port = Config.Port.icp) <= 0) |
62e76326 | 684 | return; |
685 | ||
e0d28505 AJ |
686 | icpIncomingConn = new Comm::Connection; |
687 | icpIncomingConn->local = Config.Addrs.udp_incoming; | |
4dd643d5 | 688 | icpIncomingConn->local.port(port); |
62e76326 | 689 | |
4dd643d5 | 690 | if (!Ip::EnableIpv6 && !icpIncomingConn->local.setIPv4()) { |
e0d28505 | 691 | debugs(12, DBG_CRITICAL, "ERROR: IPv6 is disabled. " << icpIncomingConn->local << " is not an IPv4 address."); |
e0f8b709 AJ |
692 | fatal("ICP port cannot be opened."); |
693 | } | |
e0209dae | 694 | /* split-stack for now requires default IPv4-only ICP */ |
4dd643d5 AJ |
695 | if (Ip::EnableIpv6&IPV6_SPECIAL_SPLITSTACK && icpIncomingConn->local.isAnyAddr()) { |
696 | icpIncomingConn->local.setIPv4(); | |
e0209dae | 697 | } |
e0f8b709 | 698 | |
013e320c | 699 | AsyncCall::Pointer call = asyncCall(12, 2, |
5667a628 | 700 | "icpIncomingConnectionOpened", |
09cee743 | 701 | Comm::UdpOpenDialer(&icpIncomingConnectionOpened)); |
62e76326 | 702 | |
013e320c | 703 | Ipc::StartListening(SOCK_DGRAM, |
5667a628 | 704 | IPPROTO_UDP, |
e0d28505 | 705 | icpIncomingConn, |
5667a628 | 706 | Ipc::fdnInIcpSocket, call); |
62e76326 | 707 | |
4dd643d5 | 708 | if ( !Config.Addrs.udp_outgoing.isNoAddr() ) { |
e0d28505 AJ |
709 | icpOutgoingConn = new Comm::Connection; |
710 | icpOutgoingConn->local = Config.Addrs.udp_outgoing; | |
4dd643d5 | 711 | icpOutgoingConn->local.port(port); |
e0f8b709 | 712 | |
4dd643d5 | 713 | if (!Ip::EnableIpv6 && !icpOutgoingConn->local.setIPv4()) { |
e0d28505 | 714 | debugs(49, DBG_CRITICAL, "ERROR: IPv6 is disabled. " << icpOutgoingConn->local << " is not an IPv4 address."); |
e0f8b709 AJ |
715 | fatal("ICP port cannot be opened."); |
716 | } | |
e0209dae | 717 | /* split-stack for now requires default IPv4-only ICP */ |
4dd643d5 AJ |
718 | if (Ip::EnableIpv6&IPV6_SPECIAL_SPLITSTACK && icpOutgoingConn->local.isAnyAddr()) { |
719 | icpOutgoingConn->local.setIPv4(); | |
e0209dae | 720 | } |
e0f8b709 | 721 | |
e0d28505 AJ |
722 | enter_suid(); |
723 | comm_open_listener(SOCK_DGRAM, IPPROTO_UDP, icpOutgoingConn, "Outgoing ICP Port"); | |
62e76326 | 724 | leave_suid(); |
725 | ||
e0d28505 | 726 | if (!Comm::IsConnOpen(icpOutgoingConn)) |
62e76326 | 727 | fatal("Cannot open Outgoing ICP Port"); |
728 | ||
e0d28505 | 729 | debugs(12, DBG_CRITICAL, "Sending ICP messages from " << icpOutgoingConn->local); |
62e76326 | 730 | |
8bbb16e3 | 731 | Comm::SetSelect(icpOutgoingConn->fd, COMM_SELECT_READ, icpHandleUdp, NULL, 0); |
25b481e6 | 732 | fd_note(icpOutgoingConn->fd, "Outgoing ICP socket"); |
15df8349 | 733 | } |
236dedc0 | 734 | } |
62e76326 | 735 | |
013e320c | 736 | static void |
09cee743 | 737 | icpIncomingConnectionOpened(const Comm::ConnectionPointer &conn, int errNo) |
013e320c | 738 | { |
09cee743 | 739 | if (!Comm::IsConnOpen(conn)) |
013e320c AR |
740 | fatal("Cannot open ICP Port"); |
741 | ||
09cee743 | 742 | Comm::SetSelect(conn->fd, COMM_SELECT_READ, icpHandleUdp, NULL, 0); |
013e320c AR |
743 | |
744 | for (const wordlist *s = Config.mcast_group_list; s; s = s->next) | |
09cee743 | 745 | ipcache_nbgethostbyname(s->key, mcastJoinGroups, NULL); // XXX: pass the conn for mcastJoinGroups usage. |
013e320c | 746 | |
09cee743 | 747 | debugs(12, DBG_IMPORTANT, "Accepting ICP messages on " << conn->local); |
013e320c | 748 | |
09cee743 | 749 | fd_note(conn->fd, "Incoming ICP port"); |
013e320c | 750 | |
4dd643d5 | 751 | if (Config.Addrs.udp_outgoing.isNoAddr()) { |
09cee743 | 752 | icpOutgoingConn = conn; |
e0d28505 AJ |
753 | debugs(12, DBG_IMPORTANT, "Sending ICP messages from " << icpOutgoingConn->local); |
754 | } | |
013e320c AR |
755 | } |
756 | ||
63be0a78 | 757 | /** |
26ac0430 | 758 | * icpConnectionShutdown only closes the 'in' socket if it is |
17e6c0a1 | 759 | * different than the 'out' socket. |
760 | */ | |
c0fbae16 | 761 | void |
17e6c0a1 | 762 | icpConnectionShutdown(void) |
c0fbae16 | 763 | { |
e0d28505 | 764 | if (!Comm::IsConnOpen(icpIncomingConn)) |
62e76326 | 765 | return; |
766 | ||
e0d28505 | 767 | debugs(12, DBG_IMPORTANT, "Stop receiving ICP on " << icpIncomingConn->local); |
62e76326 | 768 | |
e0d28505 AJ |
769 | /** Release the 'in' socket for lazy closure. |
770 | * in and out sockets may be sharing one same FD. | |
771 | * This prevents this function from executing repeatedly. | |
c0fbae16 | 772 | */ |
e0d28505 | 773 | icpIncomingConn = NULL; |
62e76326 | 774 | |
63be0a78 | 775 | /** |
c0fbae16 | 776 | * Normally we only write to the outgoing ICP socket, but |
777 | * we also have a read handler there to catch messages sent | |
778 | * to that specific interface. During shutdown, we must | |
779 | * disable reading on the outgoing socket. | |
780 | */ | |
e0d28505 | 781 | assert(Comm::IsConnOpen(icpOutgoingConn)); |
62e76326 | 782 | |
8bbb16e3 | 783 | Comm::SetSelect(icpOutgoingConn->fd, COMM_SELECT_READ, NULL, NULL, 0); |
c0fbae16 | 784 | } |
17e6c0a1 | 785 | |
786 | void | |
65d448bc | 787 | icpClosePorts(void) |
17e6c0a1 | 788 | { |
789 | icpConnectionShutdown(); | |
62e76326 | 790 | |
e0d28505 AJ |
791 | if (icpOutgoingConn != NULL) { |
792 | debugs(12, DBG_IMPORTANT, "Stop sending ICP from " << icpOutgoingConn->local); | |
793 | icpOutgoingConn = NULL; | |
17e6c0a1 | 794 | } |
795 | } | |
071a3ae7 | 796 | |
797 | static void | |
798 | icpCount(void *buf, int which, size_t len, int delay) | |
799 | { | |
e6ccf245 | 800 | icp_common_t *icp = (icp_common_t *) buf; |
62e76326 | 801 | |
071a3ae7 | 802 | if (len < sizeof(*icp)) |
62e76326 | 803 | return; |
804 | ||
071a3ae7 | 805 | if (SENT == which) { |
95dc7ff4 | 806 | ++statCounter.icp.pkts_sent; |
62e76326 | 807 | kb_incr(&statCounter.icp.kbytes_sent, len); |
808 | ||
809 | if (ICP_QUERY == icp->opcode) { | |
95dc7ff4 | 810 | ++statCounter.icp.queries_sent; |
62e76326 | 811 | kb_incr(&statCounter.icp.q_kbytes_sent, len); |
812 | } else { | |
95dc7ff4 | 813 | ++statCounter.icp.replies_sent; |
62e76326 | 814 | kb_incr(&statCounter.icp.r_kbytes_sent, len); |
815 | /* this is the sent-reply service time */ | |
e8baef82 | 816 | statCounter.icp.replySvcTime.count(delay); |
62e76326 | 817 | } |
818 | ||
819 | if (ICP_HIT == icp->opcode) | |
95dc7ff4 | 820 | ++statCounter.icp.hits_sent; |
071a3ae7 | 821 | } else if (RECV == which) { |
95dc7ff4 | 822 | ++statCounter.icp.pkts_recv; |
62e76326 | 823 | kb_incr(&statCounter.icp.kbytes_recv, len); |
824 | ||
825 | if (ICP_QUERY == icp->opcode) { | |
95dc7ff4 | 826 | ++statCounter.icp.queries_recv; |
62e76326 | 827 | kb_incr(&statCounter.icp.q_kbytes_recv, len); |
828 | } else { | |
95dc7ff4 | 829 | ++statCounter.icp.replies_recv; |
62e76326 | 830 | kb_incr(&statCounter.icp.r_kbytes_recv, len); |
e8baef82 | 831 | /* statCounter.icp.querySvcTime set in clientUpdateCounters */ |
62e76326 | 832 | } |
833 | ||
834 | if (ICP_HIT == icp->opcode) | |
95dc7ff4 | 835 | ++statCounter.icp.hits_recv; |
071a3ae7 | 836 | } |
837 | } | |
007b8be4 | 838 | |
839 | #define N_QUERIED_KEYS 8192 | |
840 | #define N_QUERIED_KEYS_MASK 8191 | |
c3031d67 | 841 | static cache_key queried_keys[N_QUERIED_KEYS][SQUID_MD5_DIGEST_LENGTH]; |
007b8be4 | 842 | |
843 | int | |
5942e8d4 | 844 | icpSetCacheKey(const cache_key * key) |
007b8be4 | 845 | { |
846 | static int reqnum = 0; | |
62e76326 | 847 | |
007b8be4 | 848 | if (++reqnum < 0) |
62e76326 | 849 | reqnum = 1; |
850 | ||
007b8be4 | 851 | storeKeyCopy(queried_keys[reqnum & N_QUERIED_KEYS_MASK], key); |
62e76326 | 852 | |
007b8be4 | 853 | return reqnum; |
854 | } | |
855 | ||
856 | const cache_key * | |
857 | icpGetCacheKey(const char *url, int reqnum) | |
858 | { | |
859 | if (neighbors_do_private_keys && reqnum) | |
62e76326 | 860 | return queried_keys[reqnum & N_QUERIED_KEYS_MASK]; |
861 | ||
c2a7cefd | 862 | return storeKeyPublic(url, Http::METHOD_GET); |
007b8be4 | 863 | } |