From 007b8be4cfc066113c492b50203aefd1cad457ff Mon Sep 17 00:00:00 2001 From: wessels <> Date: Fri, 11 Sep 1998 23:07:41 +0000 Subject: [PATCH] Removed 'method bits' hack from ICP query/reply. This will break interoperability. Instead the ICP server treats all queries as METHOD_GET. The ICP client code, however, uses an array of the most recent 8192 keys sent in ICP queries. The reqnum is an index to this array, so we can look up the key for an ICP reply easily. Also changed storeKeyPublic and storeKeyPrivate to NOT use snprintf, but just pass integers 'id' and 'method' directly to md5update. --- src/client_side.cc | 4 ++-- src/icmp.cc | 8 ++------ src/icp_v2.cc | 38 ++++++++++++++++++++++++++------------ src/icp_v3.cc | 16 ++++------------ src/neighbors.cc | 10 ++++++---- src/protos.h | 4 +++- src/store.cc | 20 ++++++++++---------- src/store_key_md5.cc | 24 ++++++++---------------- src/structs.h | 6 +++--- 9 files changed, 64 insertions(+), 66 deletions(-) diff --git a/src/client_side.cc b/src/client_side.cc index 26f870e6c7..f321d7ff36 100644 --- a/src/client_side.cc +++ b/src/client_side.cc @@ -1,6 +1,6 @@ /* - * $Id: client_side.cc,v 1.391 1998/09/04 23:04:39 wessels Exp $ + * $Id: client_side.cc,v 1.392 1998/09/11 17:07:41 wessels Exp $ * * DEBUG: section 33 Client-side Routines * AUTHOR: Duane Wessels @@ -1503,7 +1503,7 @@ clientWriteComplete(int fd, char *bufnotused, size_t size, int errflag, void *da int done; http->out.size += size; debug(33, 5) ("clientWriteComplete: FD %d, sz %d, err %d, off %d, len %d\n", - fd, size, errflag, (int) http->out.offset, objectLen(entry)); + fd, size, errflag, (int) http->out.offset, entry ? objectLen(entry) : 0); if (size > 0) { kb_incr(&Counter.client_http.kbytes_out, size); if (isTcpHit(http->log_type)) diff --git a/src/icmp.cc b/src/icmp.cc index d6d50a391e..749b965f2d 100644 --- a/src/icmp.cc +++ b/src/icmp.cc @@ -1,6 +1,6 @@ /* - * $Id: icmp.cc,v 1.63 1998/09/04 23:04:51 wessels Exp $ + * $Id: icmp.cc,v 1.64 1998/09/11 17:07:43 wessels Exp $ * * DEBUG: section 37 ICMP Routines * AUTHOR: Duane Wessels @@ -126,11 +126,7 @@ icmpHandleSourcePing(const struct sockaddr_in *from, const char *buf) const char *url; xmemcpy(&header, buf, sizeof(icp_common_t)); url = buf + sizeof(icp_common_t); - if (neighbors_do_private_keys && header.reqnum) { - key = storeKeyPrivate(url, METHOD_GET, header.reqnum); - } else { - key = storeKeyPublic(url, METHOD_GET); - } + key = icpGetCacheKey(url, (int) header.reqnum); debug(37, 3) ("icmpHandleSourcePing: from %s, key '%s'\n", inet_ntoa(from->sin_addr), storeKeyText(key)); /* call neighborsUdpAck even if ping_status != PING_WAITING */ diff --git a/src/icp_v2.cc b/src/icp_v2.cc index 1a1ca93d07..9b8c34c43c 100644 --- a/src/icp_v2.cc +++ b/src/icp_v2.cc @@ -1,6 +1,6 @@ /* - * $Id: icp_v2.cc,v 1.50 1998/09/04 23:04:52 wessels Exp $ + * $Id: icp_v2.cc,v 1.51 1998/09/11 17:07:43 wessels Exp $ * * DEBUG: section 12 Internet Cache Protocol * AUTHOR: Duane Wessels @@ -191,7 +191,6 @@ icpHandleIcpV2(int fd, struct sockaddr_in from, char *buf, int len) icp_common_t *reply; int src_rtt = 0; u_num32 flags = 0; - method_t method; int rtt = 0; int hops = 0; xmemcpy(&header, buf, sizeof(icp_common_t)); @@ -203,10 +202,6 @@ icpHandleIcpV2(int fd, struct sockaddr_in from, char *buf, int len) header.flags = ntohl(header.flags); header.pad = ntohl(header.pad); - method = header.reqnum >> 24; - /* Squid-1.1 doesn't use the "method bits" for METHOD_GET */ - if (METHOD_NONE == method || METHOD_ENUM_END <= method) - method = METHOD_GET; switch (header.opcode) { case ICP_QUERY: /* We have a valid packet */ @@ -217,7 +212,7 @@ icpHandleIcpV2(int fd, struct sockaddr_in from, char *buf, int len) icpUdpSend(fd, &from, reply, LOG_UDP_INVALID, 0); break; } - if ((icp_request = urlParse(method, url)) == NULL) { + if ((icp_request = urlParse(METHOD_GET, url)) == NULL) { reply = icpCreateMessage(ICP_ERR, 0, url, header.reqnum, 0); icpUdpSend(fd, &from, reply, LOG_UDP_INVALID, 0); break; @@ -248,7 +243,7 @@ icpHandleIcpV2(int fd, struct sockaddr_in from, char *buf, int len) flags |= ICP_FLAG_SRC_RTT; } /* The peer is allowed to use this cache */ - key = storeKeyPublic(url, method); + key = storeKeyPublic(url, METHOD_GET); entry = storeGet(key); debug(12, 5) ("icpHandleIcpV2: OPCODE %s\n", icp_opcode_str[header.opcode]); if (icpCheckUdpHit(entry, icp_request)) { @@ -293,10 +288,7 @@ icpHandleIcpV2(int fd, struct sockaddr_in from, char *buf, int len) icp_opcode_str[header.opcode], inet_ntoa(from.sin_addr), url); - if (neighbors_do_private_keys && header.reqnum) - key = storeKeyPrivate(url, method, header.reqnum); - else - key = storeKeyPublic(url, method); + key = icpGetCacheKey(url, (int) header.reqnum); /* call neighborsUdpAck even if ping_status != PING_WAITING */ neighborsUdpAck(key, &header, &from); break; @@ -539,3 +531,25 @@ icpCount(void *buf, int which, size_t len, int delay) Counter.icp.hits_recv++; } } + +#define N_QUERIED_KEYS 8192 +#define N_QUERIED_KEYS_MASK 8191 +static cache_key queried_keys[N_QUERIED_KEYS][MD5_DIGEST_CHARS]; + +int +icpSetCacheKey(const cache_key *key) +{ + static int reqnum = 0; + if (++reqnum < 0) + reqnum = 1; + storeKeyCopy(queried_keys[reqnum & N_QUERIED_KEYS_MASK], key); + return reqnum; +} + +const cache_key * +icpGetCacheKey(const char *url, int reqnum) +{ + if (neighbors_do_private_keys && reqnum) + return queried_keys[reqnum & N_QUERIED_KEYS_MASK]; + return storeKeyPublic(url, METHOD_GET); +} diff --git a/src/icp_v3.cc b/src/icp_v3.cc index cf662b71c0..a0de45cb83 100644 --- a/src/icp_v3.cc +++ b/src/icp_v3.cc @@ -1,6 +1,6 @@ /* - * $Id: icp_v3.cc,v 1.24 1998/08/18 16:00:24 wessels Exp $ + * $Id: icp_v3.cc,v 1.25 1998/09/11 17:07:44 wessels Exp $ * * DEBUG: section 12 Internet Cache Protocol * AUTHOR: Duane Wessels @@ -47,7 +47,6 @@ icpHandleIcpV3(int fd, struct sockaddr_in from, char *buf, int len) request_t *icp_request = NULL; int allow = 0; aclCheck_t checklist; - method_t method; xmemcpy(&header, buf, sizeof(icp_common_t)); /* * Only these fields need to be converted @@ -57,10 +56,6 @@ icpHandleIcpV3(int fd, struct sockaddr_in from, char *buf, int len) header.flags = ntohl(header.flags); header.pad = ntohl(header.pad); - method = header.reqnum >> 24; - /* Squid-1.1 doesn't use the "method bits" for METHOD_GET */ - if (METHOD_NONE == method || METHOD_ENUM_END <= method) - method = METHOD_GET; switch (header.opcode) { case ICP_QUERY: /* We have a valid packet */ @@ -71,7 +66,7 @@ icpHandleIcpV3(int fd, struct sockaddr_in from, char *buf, int len) icpUdpSend(fd, &from, reply, LOG_UDP_INVALID, 0); break; } - if ((icp_request = urlParse(method, url)) == NULL) { + if ((icp_request = urlParse(METHOD_GET, url)) == NULL) { reply = icpCreateMessage(ICP_ERR, 0, url, header.reqnum, 0); icpUdpSend(fd, &from, reply, LOG_UDP_INVALID, 0); break; @@ -95,7 +90,7 @@ icpHandleIcpV3(int fd, struct sockaddr_in from, char *buf, int len) break; } /* The peer is allowed to use this cache */ - key = storeKeyPublic(url, method); + key = storeKeyPublic(url, METHOD_GET); entry = storeGet(key); debug(12, 5) ("icpHandleIcpV3: OPCODE %s\n", icp_opcode_str[header.opcode]); @@ -134,10 +129,7 @@ icpHandleIcpV3(int fd, struct sockaddr_in from, char *buf, int len) icp_opcode_str[header.opcode], inet_ntoa(from.sin_addr), url); - if (neighbors_do_private_keys && header.reqnum) - key = storeKeyPrivate(url, method, header.reqnum); - else - key = storeKeyPublic(url, method); + key = icpGetCacheKey(url, (int) header.reqnum); /* call neighborsUdpAck even if ping_status != PING_WAITING */ neighborsUdpAck(key, &header, &from); break; diff --git a/src/neighbors.cc b/src/neighbors.cc index 7476255c59..8a6c94bd22 100644 --- a/src/neighbors.cc +++ b/src/neighbors.cc @@ -1,6 +1,6 @@ /* - * $Id: neighbors.cc,v 1.243 1998/09/11 15:56:43 wessels Exp $ + * $Id: neighbors.cc,v 1.244 1998/09/11 17:07:45 wessels Exp $ * * DEBUG: section 15 Neighbor Routines * AUTHOR: Harvest Derived @@ -360,6 +360,7 @@ neighborsUdpPing(request_t * request, mem->ping_reply_callback = callback; mem->ircb_data = callback_data; *timeout = 0.0; + reqnum = icpSetCacheKey(entry->key); for (i = 0, p = first_ping; i++ < Config.npeers; p = p->next) { if (p == NULL) p = Config.peers; @@ -371,7 +372,6 @@ neighborsUdpPing(request_t * request, p->host, url); if (p->type == PEER_MULTICAST) mcastSetTtl(theOutIcpConnection, p->mcast.ttl); - reqnum = mem->reqnum; debug(15, 3) ("neighborsUdpPing: key = '%s'\n", storeKeyText(entry->key)); debug(15, 3) ("neighborsUdpPing: reqnum = %d\n", reqnum); @@ -977,6 +977,7 @@ peerCountMcastPeersStart(void *data) StoreEntry *fake; MemObject *mem; icp_common_t *query; + int reqnum; LOCAL_ARRAY(char, url, MAX_URL); assert(p->type == PEER_MULTICAST); p->mcast.flags &= ~PEER_COUNT_EVENT_PENDING; @@ -995,8 +996,9 @@ peerCountMcastPeersStart(void *data) mem->ping_reply_callback = peerCountHandleIcpReply; mem->ircb_data = psstate; mcastSetTtl(theOutIcpConnection, p->mcast.ttl); - p->mcast.reqnum = mem->reqnum; - query = icpCreateMessage(ICP_QUERY, 0, url, p->mcast.reqnum, 0); + p->mcast.id = mem->id; + reqnum = icpSetCacheKey(fake->key); + query = icpCreateMessage(ICP_QUERY, 0, url, reqnum, 0); icpUdpSend(theOutIcpConnection, &p->in_addr, query, diff --git a/src/protos.h b/src/protos.h index 5c27cb335a..0b8647f5a0 100644 --- a/src/protos.h +++ b/src/protos.h @@ -1,6 +1,6 @@ /* - * $Id: protos.h,v 1.261 1998/09/10 19:50:55 wessels Exp $ + * $Id: protos.h,v 1.262 1998/09/11 17:07:46 wessels Exp $ * * * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ @@ -491,6 +491,8 @@ extern int icpCheckUdpHit(StoreEntry *, request_t * request); extern void icpConnectionsOpen(void); extern void icpConnectionShutdown(void); extern void icpConnectionClose(void); +extern int icpSetCacheKey(const cache_key *key); +extern const cache_key * icpGetCacheKey(const char *url, int reqnum); extern void ipcache_nbgethostbyname(const char *name, IPH * handler, diff --git a/src/store.cc b/src/store.cc index 848486b8be..26a010587b 100644 --- a/src/store.cc +++ b/src/store.cc @@ -1,6 +1,6 @@ /* - * $Id: store.cc,v 1.456 1998/09/10 19:50:56 wessels Exp $ + * $Id: store.cc,v 1.457 1998/09/11 17:07:48 wessels Exp $ * * DEBUG: section 20 Storage Manager * AUTHOR: Harvest Derived @@ -86,7 +86,7 @@ static MemObject *new_MemObject(const char *, const char *); static void destroy_MemObject(StoreEntry *); static FREE destroy_StoreEntry; static void storePurgeMem(StoreEntry *); -static unsigned int getKeyCounter(method_t); +static int getKeyCounter(void); static int storeKeepInMemory(const StoreEntry *); static OBJH storeCheckCachableStats; @@ -279,13 +279,13 @@ storeGet(const cache_key * key) return (StoreEntry *) hash_lookup(store_table, key); } -static unsigned int -getKeyCounter(method_t method) +static int +getKeyCounter(void) { - static unsigned int key_counter = 0; - if (++key_counter == (1 << 24)) + static int key_counter = 0; + if (++key_counter < 0) key_counter = 1; - return (method << 24) | key_counter; + return key_counter; } void @@ -301,10 +301,10 @@ storeSetPrivateKey(StoreEntry * e) storeHashDelete(e); } if (mem != NULL) { - mem->reqnum = getKeyCounter(mem->method); - newkey = storeKeyPrivate(mem->url, mem->method, mem->reqnum); + mem->id = getKeyCounter(); + newkey = storeKeyPrivate(mem->url, mem->method, mem->id); } else { - newkey = storeKeyPrivate("JUNK", METHOD_NONE, getKeyCounter(METHOD_NONE)); + newkey = storeKeyPrivate("JUNK", METHOD_NONE, getKeyCounter()); } assert(hash_lookup(store_table, newkey) == NULL); EBIT_SET(e->flag, KEY_PRIVATE); diff --git a/src/store_key_md5.cc b/src/store_key_md5.cc index 1a57f7147a..d2e11e59e1 100644 --- a/src/store_key_md5.cc +++ b/src/store_key_md5.cc @@ -1,6 +1,6 @@ /* - * $Id: store_key_md5.cc,v 1.14 1998/09/03 18:39:06 wessels Exp $ + * $Id: store_key_md5.cc,v 1.15 1998/09/11 17:07:50 wessels Exp $ * * DEBUG: section 20 Storage Manager MD5 Cache Keys * AUTHOR: Duane Wessels @@ -92,21 +92,17 @@ storeKeyHashHash(const void *key, unsigned int n) } const cache_key * -storeKeyPrivate(const char *url, method_t method, int num) +storeKeyPrivate(const char *url, method_t method, int id) { static cache_key digest[MD5_DIGEST_CHARS]; MD5_CTX M; - int n; - char key_buf[MAX_URL + 100]; - assert(num > 0); + assert(id > 0); debug(20, 3) ("storeKeyPrivate: %s %s\n", RequestMethodStr[method], url); - n = snprintf(key_buf, sizeof(key_buf), "%d %s %s", - num, - RequestMethodStr[method], - url); MD5Init(&M); - MD5Update(&M, (unsigned char *) key_buf, n); + MD5Update(&M, (unsigned char *) &id, sizeof(id)); + MD5Update(&M, (unsigned char *) &method, sizeof(method)); + MD5Update(&M, (unsigned char *) url, strlen(url)); MD5Final(digest, &M); return digest; } @@ -116,13 +112,9 @@ storeKeyPublic(const char *url, method_t method) { static cache_key digest[MD5_DIGEST_CHARS]; MD5_CTX M; - int n; - char key_buf[MAX_URL + 100]; - n = snprintf(key_buf, sizeof(key_buf), "%s %s", - RequestMethodStr[method], - url); MD5Init(&M); - MD5Update(&M, (unsigned char *) key_buf, n); + MD5Update(&M, (unsigned char *) &method, sizeof(method)); + MD5Update(&M, (unsigned char *) url, strlen(url)); MD5Final(digest, &M); return digest; } diff --git a/src/structs.h b/src/structs.h index 6f6f5ac689..95db79c995 100644 --- a/src/structs.h +++ b/src/structs.h @@ -1,6 +1,6 @@ /* - * $Id: structs.h,v 1.219 1998/09/09 20:05:54 wessels Exp $ + * $Id: structs.h,v 1.220 1998/09/11 17:07:50 wessels Exp $ * * * SQUID Internet Object Cache http://squid.nlanr.net/Squid/ @@ -995,7 +995,7 @@ struct _peer { int n_times_counted; int n_replies_expected; int ttl; - u_num32 reqnum; + int id; int flags; } mcast; PeerDigest digest; @@ -1167,7 +1167,7 @@ struct _MemObject { } abort; char *log_url; dlink_node lru; - u_num32 reqnum; + int id; ssize_t object_sz; size_t swap_hdr_sz; }; -- 2.47.3