2246c32d4da775fbd6f6bab24c66683370d5ef36
[public/netxms.git] / src / server / libnxsrv / snmpproxy.cpp
1 /* $Id$ */
2 /*
3 ** NetXMS - Network Management System
4 ** Server Library
5 ** Copyright (C) 2003, 2004, 2005, 2006 Victor Kirhenshtein
6 **
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation; either version 2 of the License, or
10 ** (at your option) any later version.
11 **
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
16 **
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; if not, write to the Free Software
19 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 **
21 ** File: snmpproxy.cpp
22 **
23 **/
24
25 #include "libnxsrv.h"
26
27
28 //
29 // Constructor
30 //
31
32 SNMP_ProxyTransport::SNMP_ProxyTransport(AgentConnection *pConn, DWORD dwIpAddr, WORD wPort)
33 {
34 m_pAgentConnection = pConn;
35 m_dwIpAddr = dwIpAddr;
36 m_wPort = wPort;
37 m_pResponse = NULL;
38 }
39
40
41 //
42 // Destructor
43 //
44
45 SNMP_ProxyTransport::~SNMP_ProxyTransport()
46 {
47 delete m_pAgentConnection;
48 delete m_pResponse;
49 }
50
51
52 //
53 // Send PDU
54 //
55
56 int SNMP_ProxyTransport::sendMessage(SNMP_PDU *pdu)
57 {
58 BYTE *pBuffer;
59 DWORD dwSize;
60 int nRet = -1;
61 CSCPMessage msg(m_pAgentConnection->getProtocolVersion());
62
63 dwSize = pdu->encode(&pBuffer, m_securityContext);
64 if (dwSize != 0)
65 {
66 msg.SetCode(CMD_SNMP_REQUEST);
67 msg.SetVariable(VID_IP_ADDRESS, m_dwIpAddr);
68 msg.SetVariable(VID_PORT, m_wPort);
69 msg.SetVariable(VID_PDU_SIZE, dwSize);
70 msg.SetVariable(VID_PDU, pBuffer, dwSize);
71 free(pBuffer);
72
73 m_pResponse = m_pAgentConnection->customRequest(&msg);
74 if (m_pResponse != NULL)
75 {
76 nRet = 1;
77 }
78 }
79
80 return nRet;
81 }
82
83
84 //
85 // Receive PDU
86 //
87
88 int SNMP_ProxyTransport::readMessage(SNMP_PDU **ppData, DWORD dwTimeout,
89 struct sockaddr *pSender, socklen_t *piAddrSize,
90 SNMP_SecurityContext* (*contextFinder)(struct sockaddr *, socklen_t))
91 {
92 int nRet;
93 BYTE *pBuffer;
94 DWORD dwSize;
95
96 if (m_pResponse == NULL)
97 return -1;
98
99 if (m_pResponse->GetVariableLong(VID_RCC) == ERR_SUCCESS)
100 {
101 dwSize = m_pResponse->GetVariableLong(VID_PDU_SIZE);
102 pBuffer = (BYTE *)malloc(dwSize);
103 m_pResponse->GetVariableBinary(VID_PDU, pBuffer, dwSize);
104
105 if (contextFinder != NULL)
106 setSecurityContext(contextFinder(pSender, *piAddrSize));
107
108 *ppData = new SNMP_PDU;
109 if (!(*ppData)->parse(pBuffer, dwSize, m_securityContext, m_enableEngineIdAutoupdate))
110 {
111 delete *ppData;
112 *ppData = NULL;
113 }
114 nRet = dwSize;
115 }
116 else
117 {
118 nRet = -1;
119 }
120
121 delete_and_null(m_pResponse);
122 return nRet;
123 }