Forwarder.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 49 SNMP Interface */
10 
11 #include "squid.h"
12 #include "base/TextException.h"
13 #include "comm.h"
14 #include "CommCalls.h"
15 #include "globals.h"
16 #include "ipc/Port.h"
17 #include "snmp/Forwarder.h"
18 #include "snmp/Request.h"
19 #include "snmp/Response.h"
20 #include "snmp_core.h"
21 
23 
24 Snmp::Forwarder::Forwarder(const Pdu& aPdu, const Session& aSession, int aFd,
25  const Ip::Address& anAddress):
26  Ipc::Forwarder(new Request(KidIdentifier, Ipc::RequestId(), aPdu, aSession, aFd, anAddress), 2),
27  fd(aFd)
28 {
29  debugs(49, 5, "FD " << aFd);
30  Must(fd >= 0);
31  closer = asyncCall(49, 5, "Snmp::Forwarder::noteCommClosed",
34 }
35 
37 void
39 {
40  if (fd >= 0) {
41  if (closer != nullptr) {
42  comm_remove_close_handler(fd, closer);
43  closer = nullptr;
44  }
45  fd = -1;
46  }
48 }
49 
51 void
53 {
54  debugs(49, 5, MYNAME);
55  Must(fd == params.fd);
56  closer = nullptr;
57  fd = -1;
58  mustStop("commClosed");
59 }
60 
61 void
63 {
66 }
67 
68 void
69 Snmp::Forwarder::handleException(const std::exception& e)
70 {
71  debugs(49, 3, e.what());
72  sendError(SNMP_ERR_GENERR);
74 }
75 
77 void
79 {
80  debugs(49, 3, MYNAME);
81 
82  if (fd < 0)
83  return; // client gone
84 
85  Snmp::Request& req = static_cast<Snmp::Request&>(*request);
87  req.pdu.errstat = error;
88  u_char buffer[SNMP_REQUEST_SIZE];
89  int len = sizeof(buffer);
90  snmp_build(&req.session, &req.pdu, buffer, &len);
91  comm_udp_sendto(fd, req.address, buffer, len);
92 }
93 
94 void
95 Snmp::SendResponse(const Ipc::RequestId requestId, const Pdu &pdu)
96 {
97  debugs(49, 5, MYNAME);
98  // snmpAgentResponse() can modify arg
99  Pdu tmp = pdu;
100  Snmp::Response response(requestId);
101  snmp_pdu* response_pdu = nullptr;
102  try {
103  response_pdu = snmpAgentResponse(&tmp);
104  Must(response_pdu != nullptr);
105  response.pdu = static_cast<Pdu&>(*response_pdu);
106  snmp_free_pdu(response_pdu);
107  } catch (const std::exception& e) {
108  debugs(49, DBG_CRITICAL, e.what());
109  response.pdu.command = SNMP_PDU_RESPONSE;
110  response.pdu.errstat = SNMP_ERR_GENERR;
111  }
112  Ipc::TypedMsgHdr message;
113  response.pack(message);
115 }
116 
void handleTimeout() override
Definition: Forwarder.cc:62
AsyncCall::Pointer comm_add_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:952
#define SNMP_PDU_RESPONSE
Definition: snmp_pdu.h:94
int snmp_build(struct snmp_session *, struct snmp_pdu *, u_char *, int *)
Definition: snmp_api.c:106
#define DBG_CRITICAL
Definition: Stream.h:37
snmp_session wrapper add pack/unpack feature
Definition: Session.h:22
void snmp_free_pdu(struct snmp_pdu *)
Definition: snmp_pdu.c:284
int KidIdentifier
virtual void handleException(const std::exception &e)
terminate with an error
Definition: Forwarder.cc:132
int comm_udp_sendto(int fd, const Ip::Address &to_addr, const void *buf, int len)
Definition: comm.cc:921
void error(char *format,...)
#define SNMP_ERR_RESOURCEUNAVAILABLE
Definition: snmp_error.h:56
int fd
FD which the call was about. Set by the async call creator.
Definition: CommCalls.h:85
int fd
client connection descriptor
Definition: Forwarder.h:47
void swanSong() override
Definition: Forwarder.cc:67
Pdu pdu
SNMP protocol data unit.
Definition: Response.h:33
RefCount< AsyncCallT< Dialer > > asyncCall(int aDebugSection, int aDebugLevel, const char *aName, const Dialer &aDialer)
Definition: AsyncCall.h:156
Definition: forward.h:14
void handleException(const std::exception &e) override
terminate with an error
Definition: Forwarder.cc:69
static String CoordinatorAddr()
get the IPC message address for coordinator process
Definition: Port.cc:65
void pack(Ipc::TypedMsgHdr &msg) const override
prepare for sendmsg()
Definition: Response.cc:31
virtual void handleTimeout()
Definition: Forwarder.cc:125
void swanSong() override
removes our cleanup handler of the client connection socket
Definition: Forwarder.cc:38
SNMP request.
Definition: Request.h:24
struct snmp_pdu * snmpAgentResponse(struct snmp_pdu *PDU)
Definition: snmp_core.cc:460
AsyncCall::Pointer closer
comm_close handler for the connection
Definition: Forwarder.h:48
int command
Definition: snmp_pdu.h:75
void SendMessage(const String &toAddress, const TypedMsgHdr &message)
Definition: UdsOp.cc:188
Ip::Address address
client address
Definition: Request.h:39
Definition: Pdu.h:23
void sendError(int error)
send error SNMP response
Definition: Forwarder.cc:78
Forwarder(const Pdu &aPdu, const Session &aSession, int aFd, const Ip::Address &anAddress)
Definition: Forwarder.cc:24
Session session
SNMP session.
Definition: Request.h:37
void SendResponse(Ipc::RequestId, const Pdu &)
Definition: Forwarder.cc:95
#define SNMP_ERR_GENERR
Definition: snmp_error.h:47
#define Must(condition)
Definition: TextException.h:75
Pdu pdu
SNMP protocol data unit.
Definition: Request.h:36
struct msghdr with a known type, fixed-size I/O and control buffers
Definition: TypedMsgHdr.h:34
#define MYNAME
Definition: Stream.h:219
CBDATA_NAMESPACED_CLASS_INIT(Mgr, Forwarder)
#define SNMP_REQUEST_SIZE
Definition: snmp_core.h:23
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
void noteCommClosed(const CommCloseCbParams &params)
called when the client socket gets closed by some external force
Definition: Forwarder.cc:52
void comm_remove_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:981
Definition: IpcIoFile.h:23
int errstat
Definition: snmp_pdu.h:79

 

Introduction

Documentation

Support

Miscellaneous