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