icp_v3.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 12 Internet Cache Protocol (ICP) */
10 
16 #include "squid.h"
17 #include "acl/FilledChecklist.h"
18 #include "HttpRequest.h"
19 #include "ICP.h"
20 #include "Store.h"
21 
23 class ICP3State: public ICPState
24 {
25 
26 public:
27  ICP3State(icp_common_t &aHeader, HttpRequest *aRequest) :
28  ICPState(aHeader, aRequest) {}
29 
30  ~ICP3State() = default;
31 };
32 
34 static void
35 doV3Query(int fd, Ip::Address &from, char *buf, icp_common_t header)
36 {
37  /* We have a valid packet */
38  char *url = buf + sizeof(icp_common_t) + sizeof(uint32_t);
39  HttpRequest *icp_request = icpGetRequest(url, header.reqnum, fd, from);
40 
41  if (!icp_request)
42  return;
43 
44  if (!icpAccessAllowed(from, icp_request)) {
45  icpDenyAccess (from, url, header.reqnum, fd);
46  delete icp_request;
47  return;
48  }
49 
50  /* The peer is allowed to use this cache */
51  ICP3State state(header, icp_request);
52  state.fd = fd;
53  state.from = from;
54  state.url = xstrdup(url);
55 
56  icp_opcode codeToSend;
57 
58  if (state.isHit()) {
59  codeToSend = ICP_HIT;
60  } else if (icpGetCommonOpcode() == ICP_ERR)
61  codeToSend = ICP_MISS;
62  else
63  codeToSend = icpGetCommonOpcode();
64 
65  icpCreateAndSend(codeToSend, 0, url, header.reqnum, 0, fd, from, state.al);
66 }
67 
69 /* Currently Harvest cached-2.x uses ICP_VERSION_3 */
70 void
71 icpHandleIcpV3(int fd, Ip::Address &from, char *buf, int len)
72 {
73  if (len <= 0) {
74  debugs(12, 3, "icpHandleIcpV3: ICP message is too small");
75  return;
76  }
77 
78  icp_common_t header (buf, len);
79  /*
80  * Length field should match the number of bytes read
81  */
82 
83  if (len != header.length) {
84  debugs(12, 3, "icpHandleIcpV3: ICP message is too small");
85  return;
86  }
87 
88  debugs(12, 5, "OPCODE " << icp_opcode_str[header.getOpCode()] << '=' << uint8_t(header.opcode));
89 
90  switch (header.opcode) {
91 
92  case ICP_QUERY:
93  doV3Query(fd, from, buf, header);
94  break;
95 
96  case ICP_HIT:
97 
98  case ICP_DECHO:
99 
100  case ICP_MISS:
101 
102  case ICP_DENIED:
103 
104  case ICP_MISS_NOFETCH:
105  header.handleReply(buf, from);
106  break;
107 
108  case ICP_INVALID:
109 
110  case ICP_ERR:
111  break;
112 
113  default:
114  debugs(12, DBG_CRITICAL, "icpHandleIcpV3: UNKNOWN OPCODE: " << header.opcode << " from " << from);
115  break;
116  }
117 }
118 
AccessLogEntryPointer al
Definition: ICP.h:77
bool icpAccessAllowed(Ip::Address &from, HttpRequest *icp_request)
Definition: icp_v2.cc:447
char * url
Definition: ICP.h:76
uint32_t reqnum
Definition: ICP.h:44
@ ICP_ERR
Definition: icp_opcode.h:19
void icpDenyAccess(Ip::Address &from, char *url, int reqnum, int fd)
Definition: icp_v2.cc:431
int fd
Definition: ICP.h:73
ICP3State(icp_common_t &aHeader, HttpRequest *aRequest)
Definition: icp_v3.cc:27
#define xstrdup
@ ICP_DECHO
Definition: icp_opcode.h:26
void icpCreateAndSend(icp_opcode, int flags, char const *url, int reqnum, int pad, int fd, const Ip::Address &from, AccessLogEntryPointer)
Definition: icp_v2.cc:421
#define DBG_CRITICAL
Definition: Debug.h:40
unsigned short length
Definition: ICP.h:42
HttpRequest * icpGetRequest(char *url, int reqnum, int fd, Ip::Address &from)
Definition: icp_v2.cc:460
@ ICP_MISS_NOFETCH
Definition: icp_opcode.h:36
@ ICP_INVALID
Definition: icp_opcode.h:15
unsigned char opcode
Definition: ICP.h:38
Ip::Address from
Definition: ICP.h:75
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
icp_opcode icpGetCommonOpcode()
Definition: icp_v2.cc:379
bool isHit() const
whether the cache contains the requested entry
Definition: icp_v2.cc:158
static void doV3Query(int fd, Ip::Address &from, char *buf, icp_common_t header)
Definition: icp_v3.cc:35
@ ICP_HIT
Definition: icp_opcode.h:17
icp_opcode getOpCode() const
Definition: icp_v2.cc:132
~ICP3State()=default
@ ICP_QUERY
Definition: icp_opcode.h:16
@ ICP_MISS
Definition: icp_opcode.h:18
void handleReply(char *buf, Ip::Address &from)
Definition: icp_v2.cc:543
@ ICP_DENIED
Definition: icp_opcode.h:37
icp_opcode
Definition: icp_opcode.h:13
void icpHandleIcpV3(int fd, Ip::Address &from, char *buf, int len)
Definition: icp_v3.cc:71
Definition: ICP.h:62
const char * icp_opcode_str[]

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors