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