Inquirer.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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 49 SNMP Interface */
10 
11 #include "squid.h"
12 #include "base/TextException.h"
13 #include "comm.h"
14 #include "comm/Connection.h"
15 #include "CommCalls.h"
16 #include "ipc/UdsOp.h"
17 #include "snmp/Inquirer.h"
18 #include "snmp/Request.h"
19 #include "snmp/Response.h"
20 #include "snmp_core.h"
21 
23 
24 Snmp::Inquirer::Inquirer(const Request& aRequest, const Ipc::StrandCoords& coords):
25  Ipc::Inquirer(aRequest.clone(), coords, 2),
26  aggrPdu(aRequest.pdu)
27 {
28  conn = new Comm::Connection;
29  conn->fd = aRequest.fd;
30  ImportFdIntoComm(conn, SOCK_DGRAM, IPPROTO_UDP, Ipc::fdnInSnmpSocket);
31 
32  debugs(49, 5, MYNAME);
33  closer = asyncCall(49, 5, "Snmp::Inquirer::noteCommClosed",
36 
37  // forget client FD to avoid sending it to strands that may forget to close
38  if (Request *snmpRequest = dynamic_cast<Request*>(request.getRaw()))
39  snmpRequest->fd = -1;
40 }
41 
43 void
45 {
46  if (Comm::IsConnOpen(conn)) {
47  if (closer != nullptr) {
48  comm_remove_close_handler(conn->fd, closer);
49  closer = nullptr;
50  }
51  conn->close();
52  }
53  conn = nullptr;
54 }
55 
56 void
58 {
59  debugs(49, 5, MYNAME);
61  Must(Comm::IsConnOpen(conn));
62  inquire();
63 }
64 
65 void
66 Snmp::Inquirer::handleException(const std::exception& e)
67 {
68  aggrPdu.errstat = SNMP_ERR_GENERR;
70 }
71 
72 bool
74 {
75  Snmp::Response& response = static_cast<Snmp::Response&>(*aResponse);
76  bool error = response.pdu.errstat != SNMP_ERR_NOERROR;
77  if (error) {
78  aggrPdu = response.pdu;
79  } else {
80  aggrPdu.aggregate(response.pdu);
81  }
82  return !error;
83 }
84 
86 void
88 {
89  debugs(49, 5, MYNAME);
90  Must(!Comm::IsConnOpen(conn) || conn->fd == params.conn->fd);
91  closer = nullptr;
92  if (conn) {
93  conn->noteClosure();
94  conn = nullptr;
95  }
96  mustStop("commClosed");
97 }
98 
99 bool
101 {
102  return !writer && Ipc::Inquirer::doneAll();
103 }
104 
105 void
107 {
108  debugs(49, 5, MYNAME);
109 
110  if (!Comm::IsConnOpen(conn))
111  return; // client gone
112 
113  aggrPdu.fixAggregate();
114  aggrPdu.command = SNMP_PDU_RESPONSE;
115  u_char buffer[SNMP_REQUEST_SIZE];
116  int len = sizeof(buffer);
117  Snmp::Request& req = static_cast<Snmp::Request&>(*request);
118  snmp_build(&req.session, &aggrPdu, buffer, &len);
119  comm_udp_sendto(conn->fd, req.address, buffer, len);
120 }
121 
AsyncCall::Pointer comm_add_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:952
#define SNMP_PDU_RESPONSE
Definition: snmp_pdu.h:94
int snmp_build(struct snmp_session *, struct snmp_pdu *, u_char *, int *)
Definition: snmp_api.c:106
Request::Pointer request
cache manager request received from client
Definition: Inquirer.h:69
int comm_udp_sendto(int fd, const Ip::Address &to_addr, const void *buf, int len)
Definition: comm.cc:921
void error(char *format,...)
bool doneAll() const override
whether positive goal has been reached
Definition: Inquirer.cc:146
C * getRaw() const
Definition: RefCount.h:89
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:27
Pdu pdu
SNMP protocol data unit.
Definition: Response.h:33
bool doneAll() const override
whether positive goal has been reached
Definition: Inquirer.cc:100
RefCount< AsyncCallT< Dialer > > asyncCall(int aDebugSection, int aDebugLevel, const char *aName, const Dialer &aDialer)
Definition: AsyncCall.h:156
Definition: forward.h:14
CBDATA_NAMESPACED_CLASS_INIT(Mgr, Inquirer)
void start() override
called by AsyncStart; do not call directly
Definition: Inquirer.cc:89
SNMP request.
Definition: Request.h:24
Inquirer(const Request &aRequest, const Ipc::StrandCoords &coords)
Definition: Inquirer.cc:24
void start() override
called by AsyncStart; do not call directly
Definition: Inquirer.cc:57
Comm::ConnectionPointer conn
Definition: CommCalls.h:80
int fd
client connection descriptor
Definition: Request.h:38
SSL Connection
Definition: Session.h:49
void handleException(const std::exception &e) override
do specific exception handling
Definition: Inquirer.cc:66
Ip::Address address
client address
Definition: Request.h:39
void noteCommClosed(const CommCloseCbParams &params)
called when the some external force closed our socket
Definition: Inquirer.cc:87
bool aggregate(Ipc::Response::Pointer aResponse) override
perform aggregating of responses and returns true if need to continue
Definition: Inquirer.cc:73
virtual void handleException(const std::exception &e)
do specific exception handling
Definition: Inquirer.cc:152
void cleanup() override
closes our copy of the client connection socket
Definition: Inquirer.cc:44
void sendResponse() override
send response to client
Definition: Inquirer.cc:106
void aggregate(const Pdu &pdu)
Definition: Pdu.cc:55
@ fdnInSnmpSocket
Definition: FdNotes.h:22
AsyncCall::Pointer closer
comm_close handler
Definition: Inquirer.h:52
Session session
SNMP session.
Definition: Request.h:37
#define SNMP_ERR_GENERR
Definition: snmp_error.h:47
#define Must(condition)
Definition: TextException.h:75
std::vector< StrandCoord > StrandCoords
a collection of strand coordinates; the order, if any, is owner-dependent
Definition: StrandCoords.h:19
#define MYNAME
Definition: Stream.h:219
const Comm::ConnectionPointer & ImportFdIntoComm(const Comm::ConnectionPointer &conn, int socktype, int protocol, FdNoteId noteId)
import socket fd from another strand into our Comm state
Definition: UdsOp.cc:194
#define SNMP_REQUEST_SIZE
Definition: snmp_core.h:23
Comm::ConnectionPointer conn
client connection descriptor
Definition: Inquirer.h:49
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
void comm_remove_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:981
#define SNMP_ERR_NOERROR
Definition: snmp_error.h:42
Definition: IpcIoFile.h:23
int errstat
Definition: snmp_pdu.h:79

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors