4 * DEBUG: section 49 SNMP Interface
9 #include "base/TextException.h"
10 #include "CommCalls.h"
13 #include "snmp_core.h"
14 #include "snmp/Forwarder.h"
15 #include "snmp/Request.h"
16 #include "snmp/Response.h"
19 CBDATA_NAMESPACED_CLASS_INIT(Snmp
, Forwarder
);
22 Snmp::Forwarder::Forwarder(const Pdu
& aPdu
, const Session
& aSession
, int aFd
,
23 const Ip::Address
& anAddress
):
24 Ipc::Forwarder(new Request(KidIdentifier
, 0, aPdu
, aSession
, aFd
, anAddress
), 2),
27 debugs(49, 5, HERE
<< "FD " << aFd
);
29 closer
= asyncCall(49, 5, "Snmp::Forwarder::noteCommClosed",
30 CommCbMemFunT
<Forwarder
, CommCloseCbParams
>(this, &Forwarder::noteCommClosed
));
31 comm_add_close_handler(fd
, closer
);
34 /// removes our cleanup handler of the client connection socket
36 Snmp::Forwarder::cleanup()
40 comm_remove_close_handler(fd
, closer
);
47 /// called when the client socket gets closed by some external force
49 Snmp::Forwarder::noteCommClosed(const CommCloseCbParams
& params
)
52 Must(fd
== params
.fd
);
54 mustStop("commClosed");
58 Snmp::Forwarder::handleTimeout()
60 sendError(SNMP_ERR_RESOURCEUNAVAILABLE
);
61 Ipc::Forwarder::handleTimeout();
65 Snmp::Forwarder::handleException(const std::exception
& e
)
67 debugs(49, 3, HERE
<< e
.what());
69 sendError(SNMP_ERR_GENERR
);
70 Ipc::Forwarder::handleException(e
);
73 /// send error SNMP response
75 Snmp::Forwarder::sendError(int error
)
78 Snmp::Request
& req
= static_cast<Snmp::Request
&>(*request
);
79 req
.pdu
.command
= SNMP_PDU_RESPONSE
;
80 req
.pdu
.errstat
= error
;
81 u_char buffer
[SNMP_REQUEST_SIZE
];
82 int len
= sizeof(buffer
);
83 snmp_build(&req
.session
, &req
.pdu
, buffer
, &len
);
84 comm_udp_sendto(fd
, req
.address
, buffer
, len
);
88 Snmp::SendResponse(unsigned int requestId
, const Pdu
& pdu
)
91 // snmpAgentResponse() can modify arg
93 Snmp::Response
response(requestId
);
94 snmp_pdu
* response_pdu
= NULL
;
96 response_pdu
= snmpAgentResponse(&tmp
);
97 Must(response_pdu
!= NULL
);
98 response
.pdu
= static_cast<Pdu
&>(*response_pdu
);
99 snmp_free_pdu(response_pdu
);
100 } catch (const std::exception
& e
) {
101 debugs(49, DBG_CRITICAL
, HERE
<< e
.what());
102 response
.pdu
.command
= SNMP_PDU_RESPONSE
;
103 response
.pdu
.errstat
= SNMP_ERR_GENERR
;
105 Ipc::TypedMsgHdr message
;
106 response
.pack(message
);
107 Ipc::SendMessage(Ipc::coordinatorAddr
, message
);