2 * DEBUG: section 49 SNMP Interface
7 #include "base/TextException.h"
12 #include "snmp_core.h"
13 #include "snmp/Forwarder.h"
14 #include "snmp/Request.h"
15 #include "snmp/Response.h"
17 CBDATA_NAMESPACED_CLASS_INIT(Snmp
, Forwarder
);
19 Snmp::Forwarder::Forwarder(const Pdu
& aPdu
, const Session
& aSession
, int aFd
,
20 const Ip::Address
& anAddress
):
21 Ipc::Forwarder(new Request(KidIdentifier
, 0, aPdu
, aSession
, aFd
, anAddress
), 2),
24 debugs(49, 5, HERE
<< "FD " << aFd
);
26 closer
= asyncCall(49, 5, "Snmp::Forwarder::noteCommClosed",
27 CommCbMemFunT
<Forwarder
, CommCloseCbParams
>(this, &Forwarder::noteCommClosed
));
28 comm_add_close_handler(fd
, closer
);
31 /// removes our cleanup handler of the client connection socket
33 Snmp::Forwarder::cleanup()
37 comm_remove_close_handler(fd
, closer
);
44 /// called when the client socket gets closed by some external force
46 Snmp::Forwarder::noteCommClosed(const CommCloseCbParams
& params
)
49 Must(fd
== params
.fd
);
51 mustStop("commClosed");
55 Snmp::Forwarder::handleTimeout()
57 sendError(SNMP_ERR_RESOURCEUNAVAILABLE
);
58 Ipc::Forwarder::handleTimeout();
62 Snmp::Forwarder::handleException(const std::exception
& e
)
64 debugs(49, 3, HERE
<< e
.what());
66 sendError(SNMP_ERR_GENERR
);
67 Ipc::Forwarder::handleException(e
);
70 /// send error SNMP response
72 Snmp::Forwarder::sendError(int error
)
75 Snmp::Request
& req
= static_cast<Snmp::Request
&>(*request
);
76 req
.pdu
.command
= SNMP_PDU_RESPONSE
;
77 req
.pdu
.errstat
= error
;
78 u_char buffer
[SNMP_REQUEST_SIZE
];
79 int len
= sizeof(buffer
);
80 snmp_build(&req
.session
, &req
.pdu
, buffer
, &len
);
81 comm_udp_sendto(fd
, req
.address
, buffer
, len
);
85 Snmp::SendResponse(unsigned int requestId
, const Pdu
& pdu
)
88 // snmpAgentResponse() can modify arg
90 Snmp::Response
response(requestId
);
91 snmp_pdu
* response_pdu
= NULL
;
93 response_pdu
= snmpAgentResponse(&tmp
);
94 Must(response_pdu
!= NULL
);
95 response
.pdu
= static_cast<Pdu
&>(*response_pdu
);
96 snmp_free_pdu(response_pdu
);
97 } catch (const std::exception
& e
) {
98 debugs(49, DBG_CRITICAL
, HERE
<< e
.what());
99 response
.pdu
.command
= SNMP_PDU_RESPONSE
;
100 response
.pdu
.errstat
= SNMP_ERR_GENERR
;
102 Ipc::TypedMsgHdr message
;
103 response
.pack(message
);
104 Ipc::SendMessage(Ipc::coordinatorAddr
, message
);