4 * DEBUG: section 49 SNMP Interface
9 #include "base/TextException.h"
10 #include "CommCalls.h"
12 #include "snmp_core.h"
13 #include "snmp/Forwarder.h"
14 #include "snmp/Request.h"
15 #include "snmp/Response.h"
18 CBDATA_NAMESPACED_CLASS_INIT(Snmp
, Forwarder
);
21 Snmp::Forwarder::Forwarder(const Pdu
& aPdu
, const Session
& aSession
, int aFd
,
22 const Ip::Address
& anAddress
):
23 Ipc::Forwarder(new Request(KidIdentifier
, 0, aPdu
, aSession
, aFd
, anAddress
), 2),
26 debugs(49, 5, HERE
<< "FD " << aFd
);
28 closer
= asyncCall(49, 5, "Snmp::Forwarder::noteCommClosed",
29 CommCbMemFunT
<Forwarder
, CommCloseCbParams
>(this, &Forwarder::noteCommClosed
));
30 comm_add_close_handler(fd
, closer
);
33 /// removes our cleanup handler of the client connection socket
35 Snmp::Forwarder::cleanup()
39 comm_remove_close_handler(fd
, closer
);
46 /// called when the client socket gets closed by some external force
48 Snmp::Forwarder::noteCommClosed(const CommCloseCbParams
& params
)
51 Must(fd
== params
.fd
);
53 mustStop("commClosed");
57 Snmp::Forwarder::handleTimeout()
59 sendError(SNMP_ERR_RESOURCEUNAVAILABLE
);
60 Ipc::Forwarder::handleTimeout();
64 Snmp::Forwarder::handleException(const std::exception
& e
)
66 debugs(49, 3, HERE
<< e
.what());
68 sendError(SNMP_ERR_GENERR
);
69 Ipc::Forwarder::handleException(e
);
72 /// send error SNMP response
74 Snmp::Forwarder::sendError(int error
)
77 Snmp::Request
& req
= static_cast<Snmp::Request
&>(*request
);
78 req
.pdu
.command
= SNMP_PDU_RESPONSE
;
79 req
.pdu
.errstat
= error
;
80 u_char buffer
[SNMP_REQUEST_SIZE
];
81 int len
= sizeof(buffer
);
82 snmp_build(&req
.session
, &req
.pdu
, buffer
, &len
);
83 comm_udp_sendto(fd
, req
.address
, buffer
, len
);
87 Snmp::SendResponse(unsigned int requestId
, const Pdu
& pdu
)
90 // snmpAgentResponse() can modify arg
92 Snmp::Response
response(requestId
);
93 snmp_pdu
* response_pdu
= NULL
;
95 response_pdu
= snmpAgentResponse(&tmp
);
96 Must(response_pdu
!= NULL
);
97 response
.pdu
= static_cast<Pdu
&>(*response_pdu
);
98 snmp_free_pdu(response_pdu
);
99 } catch (const std::exception
& e
) {
100 debugs(49, DBG_CRITICAL
, HERE
<< e
.what());
101 response
.pdu
.command
= SNMP_PDU_RESPONSE
;
102 response
.pdu
.errstat
= SNMP_ERR_GENERR
;
104 Ipc::TypedMsgHdr message
;
105 response
.pack(message
);
106 Ipc::SendMessage(Ipc::coordinatorAddr
, message
);