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