4 * DEBUG: section 49 SNMP Interface
9 #include "base/TextException.h"
10 #include "CommCalls.h"
14 #include "snmp_core.h"
15 #include "snmp/Forwarder.h"
16 #include "snmp/Request.h"
17 #include "snmp/Response.h"
20 CBDATA_NAMESPACED_CLASS_INIT(Snmp
, Forwarder
);
23 Snmp::Forwarder::Forwarder(const Pdu
& aPdu
, const Session
& aSession
, int aFd
,
24 const Ip::Address
& anAddress
):
25 Ipc::Forwarder(new Request(KidIdentifier
, 0, aPdu
, aSession
, aFd
, anAddress
), 2),
28 debugs(49, 5, HERE
<< "FD " << aFd
);
30 closer
= asyncCall(49, 5, "Snmp::Forwarder::noteCommClosed",
31 CommCbMemFunT
<Forwarder
, CommCloseCbParams
>(this, &Forwarder::noteCommClosed
));
32 comm_add_close_handler(fd
, closer
);
35 /// removes our cleanup handler of the client connection socket
37 Snmp::Forwarder::cleanup()
41 comm_remove_close_handler(fd
, closer
);
48 /// called when the client socket gets closed by some external force
50 Snmp::Forwarder::noteCommClosed(const CommCloseCbParams
& params
)
53 Must(fd
== params
.fd
);
55 mustStop("commClosed");
59 Snmp::Forwarder::handleTimeout()
61 sendError(SNMP_ERR_RESOURCEUNAVAILABLE
);
62 Ipc::Forwarder::handleTimeout();
66 Snmp::Forwarder::handleException(const std::exception
& e
)
68 debugs(49, 3, HERE
<< e
.what());
70 sendError(SNMP_ERR_GENERR
);
71 Ipc::Forwarder::handleException(e
);
74 /// send error SNMP response
76 Snmp::Forwarder::sendError(int error
)
79 Snmp::Request
& req
= static_cast<Snmp::Request
&>(*request
);
80 req
.pdu
.command
= SNMP_PDU_RESPONSE
;
81 req
.pdu
.errstat
= error
;
82 u_char buffer
[SNMP_REQUEST_SIZE
];
83 int len
= sizeof(buffer
);
84 snmp_build(&req
.session
, &req
.pdu
, buffer
, &len
);
85 comm_udp_sendto(fd
, req
.address
, buffer
, len
);
89 Snmp::SendResponse(unsigned int requestId
, const Pdu
& pdu
)
92 // snmpAgentResponse() can modify arg
94 Snmp::Response
response(requestId
);
95 snmp_pdu
* response_pdu
= NULL
;
97 response_pdu
= snmpAgentResponse(&tmp
);
98 Must(response_pdu
!= NULL
);
99 response
.pdu
= static_cast<Pdu
&>(*response_pdu
);
100 snmp_free_pdu(response_pdu
);
101 } catch (const std::exception
& e
) {
102 debugs(49, DBG_CRITICAL
, HERE
<< e
.what());
103 response
.pdu
.command
= SNMP_PDU_RESPONSE
;
104 response
.pdu
.errstat
= SNMP_ERR_GENERR
;
106 Ipc::TypedMsgHdr message
;
107 response
.pack(message
);
108 Ipc::SendMessage(Ipc::coordinatorAddr
, message
);