Inquirer.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 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, HERE);
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 != NULL) {
49  closer = NULL;
50  }
51  conn->close();
52  }
53  conn = NULL;
54 }
55 
56 void
58 {
59  debugs(49, 5, HERE);
62  inquire();
63 }
64 
65 void
66 Snmp::Inquirer::handleException(const std::exception& e)
67 {
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, HERE);
90  Must(!Comm::IsConnOpen(conn) || conn->fd == params.conn->fd);
91  conn = NULL;
92  mustStop("commClosed");
93 }
94 
95 bool
97 {
98  return !writer && Ipc::Inquirer::doneAll();
99 }
100 
101 void
103 {
104  debugs(49, 5, HERE);
107  u_char buffer[SNMP_REQUEST_SIZE];
108  int len = sizeof(buffer);
109  Snmp::Request& req = static_cast<Snmp::Request&>(*request);
110  snmp_build(&req.session, &aggrPdu, buffer, &len);
111  comm_udp_sendto(conn->fd, req.address, buffer, len);
112 }
113 
Inquirer(const Request &aRequest, const Ipc::StrandCoords &coords)
Definition: Inquirer.cc:24
CBDATA_NAMESPACED_CLASS_INIT(Ipc, Inquirer)
void mustStop(const char *aReason)
Definition: AsyncJob.cc:69
void error(char *format,...)
Definition: IpcIoFile.h:23
Comm::ConnectionPointer conn
client connection descriptor
Definition: Inquirer.h:49
virtual bool doneAll() const
whether positive goal has been reached
Definition: Inquirer.cc:115
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:69
#define SNMP_ERR_GENERR
Definition: snmp_error.h:47
int comm_udp_sendto(int fd, const Ip::Address &to_addr, const void *buf, int len)
Definition: comm.cc:927
#define SNMP_PDU_RESPONSE
Definition: snmp_pdu.h:94
AsyncCall * asyncCall(int aDebugSection, int aDebugLevel, const char *aName, const Dialer &aDialer)
Definition: AsyncCall.h:156
void fixAggregate()
Definition: Pdu.cc:229
int fd
client connection descriptor
Definition: Request.h:41
Comm::ConnectionPointer conn
Definition: CommCalls.h:85
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:24
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
virtual void handleException(const std::exception &e)
do specific exception handling
Definition: Inquirer.cc:121
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:124
void aggregate(const Pdu &pdu)
Definition: Pdu.cc:55
virtual void start()
called by AsyncStart; do not call directly
Definition: Inquirer.cc:57
Pdu aggrPdu
aggregated pdu
Definition: Inquirer.h:48
#define SNMP_ERR_NOERROR
Definition: snmp_error.h:42
Ip::Address address
client address
Definition: Request.h:42
std::vector< StrandCoord > StrandCoords
a collection of strand coordinates; the order, if any, is owner-dependent
Definition: StrandCoords.h:19
SNMP request.
Definition: Request.h:24
virtual void inquire()
inquire the next strand
Definition: Inquirer.cc:62
int snmp_build(struct snmp_session *, struct snmp_pdu *, u_char *, int *)
Definition: snmp_api.c:106
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
virtual bool aggregate(Ipc::Response::Pointer aResponse)
perform aggregating of responses and returns true if need to continue
Definition: Inquirer.cc:73
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:153
void noteCommClosed(const CommCloseCbParams &params)
called when the some external force closed our socket
Definition: Inquirer.cc:87
Request::Pointer request
cache manager request received from client
Definition: Inquirer.h:71
Definition: forward.h:14
int command
Definition: snmp_pdu.h:51
AsyncCall::Pointer closer
comm_close handler
Definition: Inquirer.h:52
virtual bool doneAll() const
whether positive goal has been reached
Definition: Inquirer.cc:96
Pdu pdu
SNMP protocol data unit.
Definition: Response.h:36
C * getRaw() const
Definition: RefCount.h:74
AsyncCall::Pointer writer
comm_write callback
Definition: Inquirer.h:51
virtual void cleanup()
closes our copy of the client connection socket
Definition: Inquirer.cc:44
virtual void handleException(const std::exception &e)
do specific exception handling
Definition: Inquirer.cc:66
AsyncCall::Pointer comm_add_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:961
virtual void start()
called by AsyncStart; do not call directly
Definition: Inquirer.cc:56
void comm_remove_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:988
#define SNMP_REQUEST_SIZE
Definition: snmp_core.h:21
int errstat
Definition: snmp_pdu.h:55
#define NULL
Definition: types.h:166
Session session
SNMP session.
Definition: Request.h:40
virtual void sendResponse()
send response to client
Definition: Inquirer.cc:102

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors