--------------------- PatchSet 5278 Date: 2002/10/07 15:09:28 Author: rbcollins Branch: rbcollins_cxxtest Tag: (none) Log: extract method on icp v2 Members: src/icp_v2.cc:1.1.2.2->1.1.2.3 src/icp_v3.cc:1.1.2.4->1.1.2.5 src/protos.h:1.63.2.9->1.63.2.10 Index: squid/src/icp_v2.cc =================================================================== RCS file: /cvsroot/squid-sf//squid/src/Attic/icp_v2.cc,v retrieving revision 1.1.2.2 retrieving revision 1.1.2.3 diff -u -r1.1.2.2 -r1.1.2.3 --- squid/src/icp_v2.cc 7 Oct 2002 14:35:34 -0000 1.1.2.2 +++ squid/src/icp_v2.cc 7 Oct 2002 15:09:28 -0000 1.1.2.3 @@ -1,6 +1,6 @@ /* - * $Id: icp_v2.cc,v 1.1.2.2 2002/10/07 14:35:34 rbcollins Exp $ + * $Id: icp_v2.cc,v 1.1.2.3 2002/10/07 15:09:28 rbcollins Exp $ * * DEBUG: section 12 Internet Cache Protocol * AUTHOR: Duane Wessels @@ -35,6 +35,7 @@ #include "squid.h" #include "Store.h" +#include "StoreClient.h" static void icpLogIcp(struct in_addr, log_type, int, const char *, int); static void icpHandleIcpV2(int, struct sockaddr_in, char *, int); @@ -46,6 +47,18 @@ */ static icpUdpData *IcpQueueTail = NULL; +/* todo: mempool this */ +class ICP2State:public StoreClient { + public: + ~ICP2State(); + void created(StoreEntry * newEntry); + icp_common_t header; + request_t *request; + int fd; + struct sockaddr_in from; + char *url; +}; + static void icpLogIcp(struct in_addr caddr, log_type logcode, int len, const char *url, int delay) { @@ -76,7 +89,7 @@ while ((q = IcpQueueHead) != NULL) { delay = tvSubUsec(q->queue_time, current_time); /* increment delay to prevent looping */ - x = icpUdpSend(fd, &q->address, (icp_common_t * )q->msg, q->logcode, ++delay); + x = icpUdpSend(fd, &q->address, (icp_common_t *) q->msg, q->logcode, ++delay); IcpQueueHead = q->next; safe_free(q); if (x < 0) @@ -99,7 +112,7 @@ buf_len = sizeof(icp_common_t) + strlen(url) + 1; if (opcode == ICP_QUERY) buf_len += sizeof(u_int32_t); - buf = (char *)xcalloc(buf_len, 1); + buf = (char *) xcalloc(buf_len, 1); headerp = (icp_common_t *) (void *) buf; headerp->opcode = (char) opcode; headerp->version = ICP_VERSION_CURRENT; @@ -140,7 +153,7 @@ safe_free(msg); } else if (0 == delay) { /* send failed, but queue it */ - queue = (icpUdpData *)xcalloc(1, sizeof(icpUdpData)); + queue = (icpUdpData *) xcalloc(1, sizeof(icpUdpData)); queue->address = *to; queue->msg = msg; queue->len = (int) ntohs(msg->length); @@ -181,40 +194,107 @@ /* if true, reply sent */ int -icpCreateAndSend (int fd, const struct sockaddr_in *from, int flags, - const char *url, - int reqnum, - int pad) +icpCreateAndSend(int fd, const struct sockaddr_in *from, int flags, + const char *url, + int reqnum, + int pad) { /* if store is rebuilding, return a UDP_HIT, but not a MISS */ if (store_dirs_rebuilding && opt_reload_hit_only) { - icp_common_t * reply = (icp_common_t *)icpCreateMessage(ICP_MISS_NOFETCH, flags, url, reqnum, pad); + icp_common_t *reply = (icp_common_t *) icpCreateMessage(ICP_MISS_NOFETCH, flags, url, reqnum, pad); icpUdpSend(fd, from, reply, LOG_UDP_MISS_NOFETCH, 0); return 1; } else if (hit_only_mode_until > squid_curtime) { - icp_common_t * reply = (icp_common_t *)icpCreateMessage(ICP_MISS_NOFETCH, flags, url, reqnum, pad); + icp_common_t *reply = (icp_common_t *) icpCreateMessage(ICP_MISS_NOFETCH, flags, url, reqnum, pad); icpUdpSend(fd, from, reply, LOG_UDP_MISS_NOFETCH, 0); return 1; } - return 0; } static void -icpHandleIcpV2(int fd, struct sockaddr_in from, char *buf, int len) +doV2Query(int fd, struct sockaddr_in from, char *buf, icp_common_t header) { - icp_common_t header; - StoreEntry *entry = NULL; - char *url = NULL; - const cache_key *key; + icp_common_t *reply; request_t *icp_request = NULL; - int allow = 0; aclCheck_t checklist; - icp_common_t *reply; - int src_rtt = 0; - u_int32_t flags = 0; + int allow = 0; int rtt = 0; int hops = 0; + int src_rtt = 0; + u_int32_t flags = 0; + /* We have a valid packet */ + char *url = buf + sizeof(icp_common_t) + sizeof(u_int32_t); + if (strpbrk(url, w_space)) { + url = rfc1738_escape(url); + reply = (icp_common_t *) icpCreateMessage(ICP_ERR, 0, url, header.reqnum, 0); + icpUdpSend(fd, &from, reply, LOG_UDP_INVALID, 0); + return; + } + if ((icp_request = urlParse(METHOD_GET, url)) == NULL) { + reply = (icp_common_t *) icpCreateMessage(ICP_ERR, 0, url, header.reqnum, 0); + icpUdpSend(fd, &from, reply, LOG_UDP_INVALID, 0); + return; + } + memset(&checklist, '\0', sizeof(checklist)); + checklist.src_addr = from.sin_addr; + checklist.my_addr = no_addr; + checklist.request = icp_request; + allow = aclCheckFast(Config.accessList.icp, &checklist); + if (!allow) { + debug(12, 2) ("icpHandleIcpV2: Access Denied for %s by %s.\n", + inet_ntoa(from.sin_addr), AclMatchedName); + if (clientdbCutoffDenied(from.sin_addr)) { + /* + * count this DENIED query in the clientdb, even though + * we're not sending an ICP reply... + */ + clientdbUpdate(from.sin_addr, LOG_UDP_DENIED, PROTO_ICP, 0); + } else { + reply = (icp_common_t *) icpCreateMessage(ICP_DENIED, 0, url, header.reqnum, 0); + icpUdpSend(fd, &from, reply, LOG_UDP_DENIED, 0); + } + requestDestroy(icp_request); + return; + } + if (header.flags & ICP_FLAG_SRC_RTT) { + rtt = netdbHostRtt(icp_request->host); + hops = netdbHostHops(icp_request->host); + src_rtt = ((hops & 0xFFFF) << 16) | (rtt & 0xFFFF); + if (rtt) + flags |= ICP_FLAG_SRC_RTT; + } + /* The peer is allowed to use this cache */ + StoreEntry *entry = storeGetPublic(url, METHOD_GET); + debug(12, 5) ("icpHandleIcpV2: OPCODE %s\n", icp_opcode_str[header.opcode]); + if (icpCheckUdpHit(entry, icp_request)) { + reply = (icp_common_t *) icpCreateMessage(ICP_HIT, flags, url, header.reqnum, src_rtt); + icpUdpSend(fd, &from, reply, LOG_UDP_HIT, 0); + requestDestroy(icp_request); + return; + } + if (Config.onoff.test_reachability && rtt == 0) { + if ((rtt = netdbHostRtt(icp_request->host)) == 0) + netdbPingSite(icp_request->host); + } + if (!icpCreateAndSend(fd, &from, flags, url, header.reqnum, src_rtt)) { + if (Config.onoff.test_reachability && rtt == 0) { + reply = (icp_common_t *) icpCreateMessage(ICP_MISS_NOFETCH, flags, url, header.reqnum, src_rtt); + icpUdpSend(fd, &from, reply, LOG_UDP_MISS_NOFETCH, 0); + } else { + reply = (icp_common_t *) icpCreateMessage(ICP_MISS, flags, url, header.reqnum, src_rtt); + icpUdpSend(fd, &from, reply, LOG_UDP_MISS, 0); + } + } + requestDestroy(icp_request); +} + +static void +icpHandleIcpV2(int fd, struct sockaddr_in from, char *buf, int len) +{ + icp_common_t header; + char *url = NULL; + const cache_key *key; xmemcpy(&header, buf, sizeof(icp_common_t)); /* * Only these fields need to be converted @@ -233,66 +313,7 @@ switch (header.opcode) { case ICP_QUERY: /* We have a valid packet */ - url = buf + sizeof(icp_common_t) + sizeof(u_int32_t); - if (strpbrk(url, w_space)) { - url = rfc1738_escape(url); - reply = (icp_common_t *)icpCreateMessage(ICP_ERR, 0, url, header.reqnum, 0); - icpUdpSend(fd, &from, reply, LOG_UDP_INVALID, 0); - break; - } - if ((icp_request = urlParse(METHOD_GET, url)) == NULL) { - reply = (icp_common_t *)icpCreateMessage(ICP_ERR, 0, url, header.reqnum, 0); - icpUdpSend(fd, &from, reply, LOG_UDP_INVALID, 0); - break; - } - memset(&checklist, '\0', sizeof(checklist)); - checklist.src_addr = from.sin_addr; - checklist.my_addr = no_addr; - checklist.request = icp_request; - allow = aclCheckFast(Config.accessList.icp, &checklist); - if (!allow) { - debug(12, 2) ("icpHandleIcpV2: Access Denied for %s by %s.\n", - inet_ntoa(from.sin_addr), AclMatchedName); - if (clientdbCutoffDenied(from.sin_addr)) { - /* - * count this DENIED query in the clientdb, even though - * we're not sending an ICP reply... - */ - clientdbUpdate(from.sin_addr, LOG_UDP_DENIED, PROTO_ICP, 0); - } else { - reply = (icp_common_t *)icpCreateMessage(ICP_DENIED, 0, url, header.reqnum, 0); - icpUdpSend(fd, &from, reply, LOG_UDP_DENIED, 0); - } - break; - } - if (header.flags & ICP_FLAG_SRC_RTT) { - rtt = netdbHostRtt(icp_request->host); - hops = netdbHostHops(icp_request->host); - src_rtt = ((hops & 0xFFFF) << 16) | (rtt & 0xFFFF); - if (rtt) - flags |= ICP_FLAG_SRC_RTT; - } - /* The peer is allowed to use this cache */ - entry = storeGetPublic(url, METHOD_GET); - debug(12, 5) ("icpHandleIcpV2: OPCODE %s\n", icp_opcode_str[header.opcode]); - if (icpCheckUdpHit(entry, icp_request)) { - reply = (icp_common_t *)icpCreateMessage(ICP_HIT, flags, url, header.reqnum, src_rtt); - icpUdpSend(fd, &from, reply, LOG_UDP_HIT, 0); - break; - } - if (Config.onoff.test_reachability && rtt == 0) { - if ((rtt = netdbHostRtt(icp_request->host)) == 0) - netdbPingSite(icp_request->host); - } - if (!icpCreateAndSend(fd, &from, flags, url, header.reqnum, src_rtt)) { - if (Config.onoff.test_reachability && rtt == 0) { - reply = (icp_common_t *)icpCreateMessage(ICP_MISS_NOFETCH, flags, url, header.reqnum, src_rtt); - icpUdpSend(fd, &from, reply, LOG_UDP_MISS_NOFETCH, 0); - } else { - reply = (icp_common_t *)icpCreateMessage(ICP_MISS, flags, url, header.reqnum, src_rtt); - icpUdpSend(fd, &from, reply, LOG_UDP_MISS, 0); - } - } + doV2Query (fd, from, buf, header); break; case ICP_HIT: @@ -328,8 +349,6 @@ header.opcode, inet_ntoa(from.sin_addr)); break; } - if (icp_request) - requestDestroy(icp_request); } #ifdef ICP_PKT_DUMP @@ -527,7 +546,7 @@ static void icpCount(void *buf, int which, size_t len, int delay) { - icp_common_t *icp = (icp_common_t *)buf; + icp_common_t *icp = (icp_common_t *) buf; if (len < sizeof(*icp)) return; if (SENT == which) { Index: squid/src/icp_v3.cc =================================================================== RCS file: /cvsroot/squid-sf//squid/src/Attic/icp_v3.cc,v retrieving revision 1.1.2.4 retrieving revision 1.1.2.5 diff -u -r1.1.2.4 -r1.1.2.5 --- squid/src/icp_v3.cc 7 Oct 2002 14:35:36 -0000 1.1.2.4 +++ squid/src/icp_v3.cc 7 Oct 2002 15:09:28 -0000 1.1.2.5 @@ -1,6 +1,6 @@ /* - * $Id: icp_v3.cc,v 1.1.2.4 2002/10/07 14:35:36 rbcollins Exp $ + * $Id: icp_v3.cc,v 1.1.2.5 2002/10/07 15:09:28 rbcollins Exp $ * * DEBUG: section 12 Internet Cache Protocol * AUTHOR: Duane Wessels @@ -50,9 +50,8 @@ }; static void -doV3Query(int fd, struct sockaddr_in from, char *buf) +doV3Query(int fd, struct sockaddr_in from, char *buf, icp_common_t header) { - icp_common_t header; icp_common_t *reply; request_t *icp_request = NULL; aclCheck_t checklist; @@ -148,7 +147,7 @@ } switch (header.opcode) { case ICP_QUERY: - doV3Query(fd, from,buf); + doV3Query(fd, from,buf, header); break; case ICP_HIT: Index: squid/src/protos.h =================================================================== RCS file: /cvsroot/squid-sf//squid/src/protos.h,v retrieving revision 1.63.2.9 retrieving revision 1.63.2.10 diff -u -r1.63.2.9 -r1.63.2.10 --- squid/src/protos.h 7 Oct 2002 14:35:36 -0000 1.63.2.9 +++ squid/src/protos.h 7 Oct 2002 15:09:28 -0000 1.63.2.10 @@ -1,6 +1,6 @@ /* - * $Id: protos.h,v 1.63.2.9 2002/10/07 14:35:36 rbcollins Exp $ + * $Id: protos.h,v 1.63.2.10 2002/10/07 15:09:28 rbcollins Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -539,6 +539,7 @@ SQUIDCEXTERN int icpCreateAndSend(int fd, const struct sockaddr_in *from, int flags, const char *url, int reqnum, int pad); SQUIDCEXTERN int icpUdpSend(int, const struct sockaddr_in *, icp_common_t *, log_type, int); +SQUIDCEXTERN log_type icpLogFromICPCode(icp_opcode opcode); SQUIDCEXTERN PF icpHandleUdp; SQUIDCEXTERN PF icpUdpSendQueue; SQUIDCEXTERN PF httpAccept;