ef8360f2d25fd97d2364f05a89af35d0f9ac035b
[public/netxms.git] / src / snmp / libnxsnmp / oid.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** SNMP support library
4 ** Copyright (C) 2003, 2004 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 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 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 ** $module: oid.cpp
21 **
22 **/
23
24 #include "libnxsnmp.h"
25
26
27 //
28 // SNMP_ObjectId default constructor
29 //
30
31 SNMP_ObjectId::SNMP_ObjectId()
32 {
33 m_dwLength = 0;
34 m_pdwValue = NULL;
35 m_pszTextValue = NULL;
36 }
37
38
39 //
40 // SNMP_ObjectId constructor from existing gvalue
41 //
42
43 SNMP_ObjectId::SNMP_ObjectId(DWORD dwLength, DWORD *pdwValue)
44 {
45 m_dwLength = dwLength;
46 m_pdwValue = (DWORD *)nx_memdup(pdwValue, sizeof(DWORD) * dwLength);
47 m_pszTextValue = NULL;
48 ConvertToText();
49 }
50
51
52 //
53 // SNMP_ObjectId destructor
54 //
55
56 SNMP_ObjectId::~SNMP_ObjectId()
57 {
58 safe_free(m_pdwValue);
59 safe_free(m_pszTextValue);
60 }
61
62
63 //
64 // Convert binary representation to text
65 //
66
67 void SNMP_ObjectId::ConvertToText(void)
68 {
69 m_pszTextValue = (TCHAR *)realloc(m_pszTextValue, sizeof(TCHAR) * (m_dwLength * 6 + 1));
70 SNMPConvertOIDToText(m_dwLength, m_pdwValue, m_pszTextValue, m_dwLength * 6 + 1);
71 }
72
73
74 //
75 // Compare OID with another
76 //
77
78 int SNMP_ObjectId::Compare(TCHAR *pszOid)
79 {
80 DWORD dwBuffer[MAX_OID_LEN], dwLength;
81
82 dwLength = SNMPParseOID(pszOid, dwBuffer, MAX_OID_LEN);
83 if (dwLength == 0)
84 return OID_ERROR;
85 return Compare(dwBuffer, dwLength);
86 }
87
88
89 //
90 // Compare OID to another
91 //
92
93 int SNMP_ObjectId::Compare(DWORD *pdwOid, DWORD dwLen)
94 {
95 if ((pdwOid == NULL) || (dwLen == 0) || (m_pdwValue == NULL))
96 return OID_ERROR;
97
98 if (memcmp(m_pdwValue, pdwOid, min(dwLen, m_dwLength) * sizeof(DWORD)))
99 return OID_NOT_EQUAL;
100
101 return (dwLen == m_dwLength) ? OID_EQUAL :
102 ((dwLen < m_dwLength) ? OID_SHORTER : OID_LONGER);
103 }
104
105
106 //
107 // Set new value
108 //
109
110 void SNMP_ObjectId::SetValue(DWORD *pdwValue, DWORD dwLength)
111 {
112 safe_free(m_pdwValue);
113 m_dwLength = dwLength;
114 m_pdwValue = (DWORD *)nx_memdup(pdwValue, sizeof(DWORD) * dwLength);
115 ConvertToText();
116 }
117
118
119 //
120 // Extend value by one subid
121 //
122
123 void SNMP_ObjectId::Extend(DWORD dwSubId)
124 {
125 m_pdwValue = (DWORD *)realloc(m_pdwValue, sizeof(DWORD) * (m_dwLength + 1));
126 m_pdwValue[m_dwLength++] = dwSubId;
127 ConvertToText();
128 }