]> git.ipfire.org Git - thirdparty/squid.git/blame - src/snmp/Forwarder.cc
Detail certificate validation errors during TLS handshake (#770)
[thirdparty/squid.git] / src / snmp / Forwarder.cc
CommitLineData
51ea0904 1/*
77b1029d 2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
51ea0904 3 *
bbc27441
AJ
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.
51ea0904
CT
7 */
8
bbc27441
AJ
9/* DEBUG: section 49 SNMP Interface */
10
f7f3304a 11#include "squid.h"
51ea0904 12#include "base/TextException.h"
1b76e6c1 13#include "comm.h"
602d9612 14#include "CommCalls.h"
582c2af2 15#include "globals.h"
51ea0904 16#include "ipc/Port.h"
d6e3ad20
CT
17#include "snmp/Forwarder.h"
18#include "snmp/Request.h"
19#include "snmp/Response.h"
602d9612 20#include "snmp_core.h"
51ea0904 21
51ea0904
CT
22CBDATA_NAMESPACED_CLASS_INIT(Snmp, Forwarder);
23
51ea0904
CT
24Snmp::Forwarder::Forwarder(const Pdu& aPdu, const Session& aSession, int aFd,
25 const Ip::Address& anAddress):
f53969cc
SM
26 Ipc::Forwarder(new Request(KidIdentifier, 0, aPdu, aSession, aFd, anAddress), 2),
27 fd(aFd)
51ea0904
CT
28{
29 debugs(49, 5, HERE << "FD " << aFd);
30 Must(fd >= 0);
933a6aed 31 closer = asyncCall(49, 5, "Snmp::Forwarder::noteCommClosed",
51ea0904
CT
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
37void
ebaabe74 38Snmp::Forwarder::swanSong()
51ea0904
CT
39{
40 if (fd >= 0) {
41 if (closer != NULL) {
42 comm_remove_close_handler(fd, closer);
43 closer = NULL;
44 }
45 fd = -1;
46 }
ebaabe74 47 Ipc::Forwarder::swanSong();
51ea0904
CT
48}
49
50/// called when the client socket gets closed by some external force
51void
52Snmp::Forwarder::noteCommClosed(const CommCloseCbParams& params)
53{
54 debugs(49, 5, HERE);
55 Must(fd == params.fd);
56 fd = -1;
57 mustStop("commClosed");
58}
59
60void
61Snmp::Forwarder::handleTimeout()
62{
63 sendError(SNMP_ERR_RESOURCEUNAVAILABLE);
64 Ipc::Forwarder::handleTimeout();
65}
66
67void
68Snmp::Forwarder::handleException(const std::exception& e)
69{
70 debugs(49, 3, HERE << e.what());
71 if (fd >= 0)
72 sendError(SNMP_ERR_GENERR);
73 Ipc::Forwarder::handleException(e);
74}
75
76/// send error SNMP response
77void
78Snmp::Forwarder::sendError(int error)
79{
80 debugs(49, 3, HERE);
81 Snmp::Request& req = static_cast<Snmp::Request&>(*request);
82 req.pdu.command = SNMP_PDU_RESPONSE;
83 req.pdu.errstat = error;
84 u_char buffer[SNMP_REQUEST_SIZE];
85 int len = sizeof(buffer);
86 snmp_build(&req.session, &req.pdu, buffer, &len);
87 comm_udp_sendto(fd, req.address, buffer, len);
88}
89
90void
91Snmp::SendResponse(unsigned int requestId, const Pdu& pdu)
92{
93 debugs(49, 5, HERE);
94 // snmpAgentResponse() can modify arg
95 Pdu tmp = pdu;
96 Snmp::Response response(requestId);
97 snmp_pdu* response_pdu = NULL;
98 try {
99 response_pdu = snmpAgentResponse(&tmp);
100 Must(response_pdu != NULL);
101 response.pdu = static_cast<Pdu&>(*response_pdu);
102 snmp_free_pdu(response_pdu);
103 } catch (const std::exception& e) {
104 debugs(49, DBG_CRITICAL, HERE << e.what());
105 response.pdu.command = SNMP_PDU_RESPONSE;
106 response.pdu.errstat = SNMP_ERR_GENERR;
107 }
108 Ipc::TypedMsgHdr message;
109 response.pack(message);
1ee292b7 110 Ipc::SendMessage(Ipc::Port::CoordinatorAddr(), message);
51ea0904 111}
f53969cc 112