]>
Commit | Line | Data |
---|---|---|
51ea0904 CT |
1 | /* |
2 | * $Id$ | |
3 | * | |
4 | * DEBUG: section 49 SNMP Interface | |
5 | * | |
6 | */ | |
7 | ||
f7f3304a | 8 | #include "squid.h" |
51ea0904 CT |
9 | #include "base/TextException.h" |
10 | #include "CommCalls.h" | |
1b76e6c1 AJ |
11 | #include "comm.h" |
12 | #include "comm/Connection.h" | |
51ea0904 CT |
13 | #include "ipc/UdsOp.h" |
14 | #include "snmp_core.h" | |
d6e3ad20 CT |
15 | #include "snmp/Inquirer.h" |
16 | #include "snmp/Response.h" | |
17 | #include "snmp/Request.h" | |
51ea0904 | 18 | |
51ea0904 CT |
19 | CBDATA_NAMESPACED_CLASS_INIT(Snmp, Inquirer); |
20 | ||
51ea0904 CT |
21 | Snmp::Inquirer::Inquirer(const Request& aRequest, const Ipc::StrandCoords& coords): |
22 | Ipc::Inquirer(aRequest.clone(), coords, 2), | |
1b76e6c1 | 23 | aggrPdu(aRequest.pdu) |
51ea0904 | 24 | { |
1b76e6c1 AJ |
25 | conn = new Comm::Connection; |
26 | conn->fd = aRequest.fd; | |
27 | ImportFdIntoComm(conn, SOCK_DGRAM, IPPROTO_UDP, Ipc::fdnInSnmpSocket); | |
28 | ||
51ea0904 | 29 | debugs(49, 5, HERE); |
933a6aed | 30 | closer = asyncCall(49, 5, "Snmp::Inquirer::noteCommClosed", |
51ea0904 | 31 | CommCbMemFunT<Inquirer, CommCloseCbParams>(this, &Inquirer::noteCommClosed)); |
1b76e6c1 | 32 | comm_add_close_handler(conn->fd, closer); |
51ea0904 CT |
33 | } |
34 | ||
35 | /// closes our copy of the client connection socket | |
36 | void | |
37 | Snmp::Inquirer::cleanup() | |
38 | { | |
1b76e6c1 | 39 | if (Comm::IsConnOpen(conn)) { |
51ea0904 | 40 | if (closer != NULL) { |
1b76e6c1 | 41 | comm_remove_close_handler(conn->fd, closer); |
51ea0904 CT |
42 | closer = NULL; |
43 | } | |
1b76e6c1 | 44 | conn->close(); |
51ea0904 | 45 | } |
1b76e6c1 | 46 | conn = NULL; |
51ea0904 CT |
47 | } |
48 | ||
49 | void | |
50 | Snmp::Inquirer::start() | |
51 | { | |
52 | debugs(49, 5, HERE); | |
53 | Ipc::Inquirer::start(); | |
1b76e6c1 | 54 | Must(Comm::IsConnOpen(conn)); |
51ea0904 CT |
55 | inquire(); |
56 | } | |
57 | ||
58 | void | |
59 | Snmp::Inquirer::handleException(const std::exception& e) | |
60 | { | |
61 | aggrPdu.errstat = SNMP_ERR_GENERR; | |
62 | Ipc::Inquirer::handleException(e); | |
63 | } | |
64 | ||
65 | bool | |
66 | Snmp::Inquirer::aggregate(Response::Pointer aResponse) | |
67 | { | |
68 | Snmp::Response& response = static_cast<Snmp::Response&>(*aResponse); | |
69 | bool error = response.pdu.errstat != SNMP_ERR_NOERROR; | |
70 | if (error) { | |
71 | aggrPdu = response.pdu; | |
72 | } else { | |
73 | aggrPdu.aggregate(response.pdu); | |
74 | } | |
5654a599 | 75 | return !error; |
51ea0904 CT |
76 | } |
77 | ||
78 | /// called when the some external force closed our socket | |
79 | void | |
80 | Snmp::Inquirer::noteCommClosed(const CommCloseCbParams& params) | |
81 | { | |
82 | debugs(49, 5, HERE); | |
1b76e6c1 AJ |
83 | Must(!Comm::IsConnOpen(conn) || conn->fd == params.conn->fd); |
84 | conn = NULL; | |
51ea0904 CT |
85 | mustStop("commClosed"); |
86 | } | |
87 | ||
88 | bool | |
89 | Snmp::Inquirer::doneAll() const | |
90 | { | |
91 | return !writer && Ipc::Inquirer::doneAll(); | |
92 | } | |
93 | ||
94 | void | |
95 | Snmp::Inquirer::sendResponse() | |
96 | { | |
97 | debugs(49, 5, HERE); | |
e26257b2 | 98 | aggrPdu.fixAggregate(); |
51ea0904 CT |
99 | aggrPdu.command = SNMP_PDU_RESPONSE; |
100 | u_char buffer[SNMP_REQUEST_SIZE]; | |
101 | int len = sizeof(buffer); | |
102 | Snmp::Request& req = static_cast<Snmp::Request&>(*request); | |
103 | snmp_build(&req.session, &aggrPdu, buffer, &len); | |
1b76e6c1 | 104 | comm_udp_sendto(conn->fd, req.address, buffer, len); |
51ea0904 | 105 | } |