added virtual method SNMP_Transport::isProxyTransport()
[public/netxms.git] / src / server / libnxsrv / snmpproxy.cpp
CommitLineData
489b117b 1/*
5039dede
AK
2** NetXMS - Network Management System
3** Server Library
c75e9ee4 4** Copyright (C) 2003-2015 Victor Kirhenshtein
5039dede
AK
5**
6** This program is free software; you can redistribute it and/or modify
0702ed69
VK
7** it under the terms of the GNU Lesser General Public License as published by
8** the Free Software Foundation; either version 3 of the License, or
5039dede
AK
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
0702ed69 16** You should have received a copy of the GNU Lesser General Public License
5039dede
AK
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19**
20** File: snmpproxy.cpp
21**
22**/
23
24#include "libnxsrv.h"
25
596dc3aa
VK
26/**
27 * Constructor
28 */
a3dda7be 29SNMP_ProxyTransport::SNMP_ProxyTransport(AgentConnection *conn, const InetAddress& ipAddr, UINT16 port)
5039dede 30{
014fbefc 31 m_reliable = true; // no need for retries on server side, agent will do retry if needed
c75e9ee4
VK
32 m_agentConnection = conn;
33 m_ipAddr = ipAddr;
34 m_port = port;
35 m_response = NULL;
489b117b 36 m_waitForResponse = true;
5039dede
AK
37}
38
596dc3aa
VK
39/**
40 * Destructor
41 */
5039dede
AK
42SNMP_ProxyTransport::~SNMP_ProxyTransport()
43{
1693f955 44 m_agentConnection->decRefCount();
c75e9ee4 45 delete m_response;
5039dede
AK
46}
47
596dc3aa
VK
48/**
49 * Send PDU
50 */
c4366266 51int SNMP_ProxyTransport::sendMessage(SNMP_PDU *pdu)
5039dede 52{
5039dede 53 int nRet = -1;
c75e9ee4 54 NXCPMessage msg(m_agentConnection->getProtocolVersion());
5039dede 55
d6fec35c 56 BYTE *pBuffer;
e0471fad
VK
57 size_t size = pdu->encode(&pBuffer, m_securityContext);
58 if (size != 0)
5039dede 59 {
b368969c 60 msg.setCode(CMD_SNMP_REQUEST);
c75e9ee4
VK
61 msg.setField(VID_IP_ADDRESS, m_ipAddr);
62 msg.setField(VID_PORT, m_port);
b368969c 63 msg.setField(VID_PDU_SIZE, (UINT32)size);
d6fec35c 64 msg.setField(VID_PDU, pBuffer, size);
5039dede
AK
65 free(pBuffer);
66
a64c346b 67 if (m_waitForResponse)
489b117b 68 {
c75e9ee4
VK
69 m_response = m_agentConnection->customRequest(&msg);
70 if (m_response != NULL)
489b117b 71 {
72 nRet = 1;
73 }
74 }
83191808
VK
75 else
76 {
77 m_agentConnection->sendMessage(&msg);
78 }
5039dede
AK
79 }
80
81 return nRet;
82}
83
596dc3aa
VK
84/**
85 * Receive PDU
86 */
489b117b 87int SNMP_ProxyTransport::readMessage(SNMP_PDU **ppData, UINT32 dwTimeout,
cd9f247e
VK
88 struct sockaddr *pSender, socklen_t *piAddrSize,
89 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t))
5039dede
AK
90{
91 int nRet;
92 BYTE *pBuffer;
967893bb 93 UINT32 dwSize;
5039dede 94
c75e9ee4 95 if (m_response == NULL)
5039dede
AK
96 return -1;
97
c75e9ee4 98 if (m_response->getFieldAsUInt32(VID_RCC) == ERR_SUCCESS)
5039dede 99 {
c75e9ee4 100 dwSize = m_response->getFieldAsUInt32(VID_PDU_SIZE);
5039dede 101 pBuffer = (BYTE *)malloc(dwSize);
c75e9ee4 102 m_response->getFieldAsBinary(VID_PDU, pBuffer, dwSize);
cd9f247e
VK
103
104 if (contextFinder != NULL)
105 setSecurityContext(contextFinder(pSender, *piAddrSize));
106
5039dede 107 *ppData = new SNMP_PDU;
cd9f247e 108 if (!(*ppData)->parse(pBuffer, dwSize, m_securityContext, m_enableEngineIdAutoupdate))
5039dede
AK
109 {
110 delete *ppData;
111 *ppData = NULL;
112 }
aabe5b72
VK
113 free(pBuffer);
114 nRet = (int)dwSize;
5039dede
AK
115 }
116 else
117 {
118 nRet = -1;
119 }
120
c75e9ee4 121 delete_and_null(m_response);
5039dede
AK
122 return nRet;
123}
596dc3aa
VK
124
125/**
c75e9ee4 126 * Get peer IP address
596dc3aa 127 */
c75e9ee4 128InetAddress SNMP_ProxyTransport::getPeerIpAddress()
596dc3aa 129{
c75e9ee4 130 return m_ipAddr;
596dc3aa 131}
a3dda7be
VK
132
133/**
134 * Get port number
135 */
136UINT16 SNMP_ProxyTransport::getPort()
137{
138 return m_port;
139}
140
141/**
142 * Check if this transport is a proxy transport
143 */
144bool SNMP_ProxyTransport::isProxyTransport()
145{
146 return true;
147}