license for libnxdb and libnxsrv changed to LGPL
[public/netxms.git] / src / server / libnxsrv / snmpproxy.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Server Library
4 ** Copyright (C) 2003-2010 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
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
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 **
16 ** You should have received a copy of the GNU Lesser General Public License
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
26
27 //
28 // Constructor
29 //
30
31 SNMP_ProxyTransport::SNMP_ProxyTransport(AgentConnection *pConn, DWORD dwIpAddr, WORD wPort)
32 {
33 m_pAgentConnection = pConn;
34 m_dwIpAddr = dwIpAddr;
35 m_wPort = wPort;
36 m_pResponse = NULL;
37 }
38
39
40 //
41 // Destructor
42 //
43
44 SNMP_ProxyTransport::~SNMP_ProxyTransport()
45 {
46 delete m_pAgentConnection;
47 delete m_pResponse;
48 }
49
50
51 //
52 // Send PDU
53 //
54
55 int SNMP_ProxyTransport::sendMessage(SNMP_PDU *pdu)
56 {
57 BYTE *pBuffer;
58 DWORD dwSize;
59 int nRet = -1;
60 CSCPMessage msg(m_pAgentConnection->getProtocolVersion());
61
62 dwSize = pdu->encode(&pBuffer, m_securityContext);
63 if (dwSize != 0)
64 {
65 msg.SetCode(CMD_SNMP_REQUEST);
66 msg.SetVariable(VID_IP_ADDRESS, m_dwIpAddr);
67 msg.SetVariable(VID_PORT, m_wPort);
68 msg.SetVariable(VID_PDU_SIZE, dwSize);
69 msg.SetVariable(VID_PDU, pBuffer, dwSize);
70 free(pBuffer);
71
72 m_pResponse = m_pAgentConnection->customRequest(&msg);
73 if (m_pResponse != NULL)
74 {
75 nRet = 1;
76 }
77 }
78
79 return nRet;
80 }
81
82
83 //
84 // Receive PDU
85 //
86
87 int SNMP_ProxyTransport::readMessage(SNMP_PDU **ppData, DWORD dwTimeout,
88 struct sockaddr *pSender, socklen_t *piAddrSize,
89 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t))
90 {
91 int nRet;
92 BYTE *pBuffer;
93 DWORD dwSize;
94
95 if (m_pResponse == NULL)
96 return -1;
97
98 if (m_pResponse->GetVariableLong(VID_RCC) == ERR_SUCCESS)
99 {
100 dwSize = m_pResponse->GetVariableLong(VID_PDU_SIZE);
101 pBuffer = (BYTE *)malloc(dwSize);
102 m_pResponse->GetVariableBinary(VID_PDU, pBuffer, dwSize);
103
104 if (contextFinder != NULL)
105 setSecurityContext(contextFinder(pSender, *piAddrSize));
106
107 *ppData = new SNMP_PDU;
108 if (!(*ppData)->parse(pBuffer, dwSize, m_securityContext, m_enableEngineIdAutoupdate))
109 {
110 delete *ppData;
111 *ppData = NULL;
112 }
113 nRet = dwSize;
114 }
115 else
116 {
117 nRet = -1;
118 }
119
120 delete_and_null(m_pResponse);
121 return nRet;
122 }