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