Forwarder.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 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, 0, aPdu, aSession, aFd, anAddress), 2),
27  fd(aFd)
28 {
29  debugs(49, 5, HERE << "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 != NULL) {
43  closer = NULL;
44  }
45  fd = -1;
46  }
48 }
49 
51 void
53 {
54  debugs(49, 5, HERE);
55  Must(fd == params.fd);
56  fd = -1;
57  mustStop("commClosed");
58 }
59 
60 void
62 {
65 }
66 
67 void
68 Snmp::Forwarder::handleException(const std::exception& e)
69 {
70  debugs(49, 3, HERE << e.what());
71  if (fd >= 0)
74 }
75 
77 void
79 {
80  debugs(49, 3, HERE);
81  Snmp::Request& req = static_cast<Snmp::Request&>(*request);
83  req.pdu.errstat = error;
84  u_char buffer[SNMP_REQUEST_SIZE];
85  int len = sizeof(buffer);
86  snmp_build(&req.session, &req.pdu, buffer, &len);
87  comm_udp_sendto(fd, req.address, buffer, len);
88 }
89 
90 void
91 Snmp::SendResponse(unsigned int requestId, const Pdu& pdu)
92 {
93  debugs(49, 5, HERE);
94  // snmpAgentResponse() can modify arg
95  Pdu tmp = pdu;
96  Snmp::Response response(requestId);
97  snmp_pdu* response_pdu = NULL;
98  try {
99  response_pdu = snmpAgentResponse(&tmp);
100  Must(response_pdu != NULL);
101  response.pdu = static_cast<Pdu&>(*response_pdu);
102  snmp_free_pdu(response_pdu);
103  } catch (const std::exception& e) {
104  debugs(49, DBG_CRITICAL, HERE << e.what());
105  response.pdu.command = SNMP_PDU_RESPONSE;
106  response.pdu.errstat = SNMP_ERR_GENERR;
107  }
108  Ipc::TypedMsgHdr message;
109  response.pack(message);
111 }
112 
virtual void handleException(const std::exception &e)
terminate with an error
Definition: Forwarder.cc:129
snmp_session wrapper add pack/unpack feature
Definition: Session.h:22
int fd
client connection descriptor
Definition: Forwarder.h:46
virtual void handleException(const std::exception &e)
terminate with an error
Definition: Forwarder.cc:68
void mustStop(const char *aReason)
Definition: AsyncJob.cc:69
void error(char *format,...)
Definition: IpcIoFile.h:23
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:69
virtual void handleTimeout()
Definition: Forwarder.cc:61
#define DBG_CRITICAL
Definition: Debug.h:45
#define SNMP_ERR_GENERR
Definition: snmp_error.h:47
int comm_udp_sendto(int fd, const Ip::Address &to_addr, const void *buf, int len)
Definition: comm.cc:927
virtual void swanSong()
removes our cleanup handler of the client connection socket
Definition: Forwarder.cc:38
#define SNMP_PDU_RESPONSE
Definition: snmp_pdu.h:94
Pdu pdu
SNMP protocol data unit.
Definition: Request.h:39
AsyncCall * asyncCall(int aDebugSection, int aDebugLevel, const char *aName, const Dialer &aDialer)
Definition: AsyncCall.h:156
virtual void handleTimeout()
Definition: Forwarder.cc:122
void sendError(int error)
send error SNMP response
Definition: Forwarder.cc:78
int fd
FD which the call was about. Set by the async call creator.
Definition: CommCalls.h:90
void SendResponse(unsigned int requestId, const Pdu &pdu)
Definition: Forwarder.cc:91
Definition: Pdu.h:23
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:124
CBDATA_NAMESPACED_CLASS_INIT(Ipc, Forwarder)
virtual void swanSong()
Definition: Forwarder.cc:67
Ip::Address address
client address
Definition: Request.h:42
SNMP request.
Definition: Request.h:24
virtual void pack(Ipc::TypedMsgHdr &msg) const
prepare for sendmsg()
Definition: Response.cc:42
void SendMessage(const String &toAddress, const TypedMsgHdr &message)
Definition: UdsOp.cc:188
int snmp_build(struct snmp_session *, struct snmp_pdu *, u_char *, int *)
Definition: snmp_api.c:106
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:153
AsyncCall::Pointer closer
comm_close handler for the connection
Definition: Forwarder.h:47
void noteCommClosed(const CommCloseCbParams &params)
called when the client socket gets closed by some external force
Definition: Forwarder.cc:52
int KidIdentifier
Definition: forward.h:14
int command
Definition: snmp_pdu.h:51
#define SNMP_ERR_RESOURCEUNAVAILABLE
Definition: snmp_error.h:56
struct msghdr with a known type, fixed-size I/O and control buffers
Definition: TypedMsgHdr.h:31
Pdu pdu
SNMP protocol data unit.
Definition: Response.h:36
void snmp_free_pdu(struct snmp_pdu *)
Definition: snmp_pdu.c:284
AsyncCall::Pointer comm_add_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:961
void comm_remove_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:988
#define SNMP_REQUEST_SIZE
Definition: snmp_core.h:21
int errstat
Definition: snmp_pdu.h:55
static String CoordinatorAddr()
get the IPC message address for coordinator process
Definition: Port.cc:64
#define NULL
Definition: types.h:166
Session session
SNMP session.
Definition: Request.h:40
Forwarder(const Pdu &aPdu, const Session &aSession, int aFd, const Ip::Address &anAddress)
Definition: Forwarder.cc:24
struct snmp_pdu * snmpAgentResponse(struct snmp_pdu *PDU)
Definition: snmp_core.cc:456

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors