]> git.ipfire.org Git - thirdparty/squid.git/blob - src/snmp/Forwarder.cc
merge from trunk
[thirdparty/squid.git] / src / snmp / Forwarder.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 49 SNMP Interface
5 *
6 */
7
8 #include "config.h"
9 #include "base/TextException.h"
10 #include "CommCalls.h"
11 #include "ipc/Port.h"
12 #include "snmp_core.h"
13 #include "snmp/Forwarder.h"
14 #include "snmp/Request.h"
15 #include "snmp/Response.h"
16
17
18 CBDATA_NAMESPACED_CLASS_INIT(Snmp, Forwarder);
19
20
21 Snmp::Forwarder::Forwarder(const Pdu& aPdu, const Session& aSession, int aFd,
22 const Ip::Address& anAddress):
23 Ipc::Forwarder(new Request(KidIdentifier, 0, aPdu, aSession, aFd, anAddress), 2),
24 fd(aFd)
25 {
26 debugs(49, 5, HERE << "FD " << aFd);
27 Must(fd >= 0);
28 closer = asyncCall(49, 5, "Snmp::Forwarder::noteCommClosed",
29 CommCbMemFunT<Forwarder, CommCloseCbParams>(this, &Forwarder::noteCommClosed));
30 comm_add_close_handler(fd, closer);
31 }
32
33 /// removes our cleanup handler of the client connection socket
34 void
35 Snmp::Forwarder::cleanup()
36 {
37 if (fd >= 0) {
38 if (closer != NULL) {
39 comm_remove_close_handler(fd, closer);
40 closer = NULL;
41 }
42 fd = -1;
43 }
44 }
45
46 /// called when the client socket gets closed by some external force
47 void
48 Snmp::Forwarder::noteCommClosed(const CommCloseCbParams& params)
49 {
50 debugs(49, 5, HERE);
51 Must(fd == params.fd);
52 fd = -1;
53 mustStop("commClosed");
54 }
55
56 void
57 Snmp::Forwarder::handleTimeout()
58 {
59 sendError(SNMP_ERR_RESOURCEUNAVAILABLE);
60 Ipc::Forwarder::handleTimeout();
61 }
62
63 void
64 Snmp::Forwarder::handleException(const std::exception& e)
65 {
66 debugs(49, 3, HERE << e.what());
67 if (fd >= 0)
68 sendError(SNMP_ERR_GENERR);
69 Ipc::Forwarder::handleException(e);
70 }
71
72 /// send error SNMP response
73 void
74 Snmp::Forwarder::sendError(int error)
75 {
76 debugs(49, 3, HERE);
77 Snmp::Request& req = static_cast<Snmp::Request&>(*request);
78 req.pdu.command = SNMP_PDU_RESPONSE;
79 req.pdu.errstat = error;
80 u_char buffer[SNMP_REQUEST_SIZE];
81 int len = sizeof(buffer);
82 snmp_build(&req.session, &req.pdu, buffer, &len);
83 comm_udp_sendto(fd, req.address, buffer, len);
84 }
85
86 void
87 Snmp::SendResponse(unsigned int requestId, const Pdu& pdu)
88 {
89 debugs(49, 5, HERE);
90 // snmpAgentResponse() can modify arg
91 Pdu tmp = pdu;
92 Snmp::Response response(requestId);
93 snmp_pdu* response_pdu = NULL;
94 try {
95 response_pdu = snmpAgentResponse(&tmp);
96 Must(response_pdu != NULL);
97 response.pdu = static_cast<Pdu&>(*response_pdu);
98 snmp_free_pdu(response_pdu);
99 } catch (const std::exception& e) {
100 debugs(49, DBG_CRITICAL, HERE << e.what());
101 response.pdu.command = SNMP_PDU_RESPONSE;
102 response.pdu.errstat = SNMP_ERR_GENERR;
103 }
104 Ipc::TypedMsgHdr message;
105 response.pack(message);
106 Ipc::SendMessage(Ipc::coordinatorAddr, message);
107 }