license changed to LGPL for libnxcl, libnxsnmp, libnxlp, libnxsl, and libnxmap
[public/netxms.git] / src / snmp / libnxsnmp / main.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** SNMP support 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: main.cpp
21 **
22 **/
23
24 #include "libnxsnmp.h"
25
26
27 //
28 // Convert OID to text
29 //
30
31 TCHAR LIBNXSNMP_EXPORTABLE *SNMPConvertOIDToText(DWORD dwLength, DWORD *pdwValue, TCHAR *pszBuffer, DWORD dwBufferSize)
32 {
33 DWORD i, dwBufPos, dwNumChars;
34
35 pszBuffer[0] = 0;
36 for(i = 0, dwBufPos = 0; (i < dwLength) && (dwBufPos < dwBufferSize); i++)
37 {
38 dwNumChars = _sntprintf(&pszBuffer[dwBufPos], dwBufferSize - dwBufPos, _T(".%d"), pdwValue[i]);
39 dwBufPos += dwNumChars;
40 }
41 return pszBuffer;
42 }
43
44
45 //
46 // Parse OID in text into binary format
47 // Will return 0 if OID is invalid or empty, and OID length (in DWORDs) on success
48 // Buffer size should be given in number of DWORDs
49 //
50
51 DWORD LIBNXSNMP_EXPORTABLE SNMPParseOID(const TCHAR *pszText, DWORD *pdwBuffer, DWORD dwBufferSize)
52 {
53 TCHAR *pCurr = (TCHAR *)pszText, *pEnd, szNumber[32];
54 DWORD dwLength = 0;
55 int iNumLen;
56
57 if (*pCurr == 0)
58 return 0;
59
60 // Skip initial dot if present
61 if (*pCurr == _T('.'))
62 pCurr++;
63
64 for(pEnd = pCurr; (*pEnd != 0) && (dwLength < dwBufferSize); pCurr = pEnd + 1)
65 {
66 for(iNumLen = 0, pEnd = pCurr; (*pEnd >= _T('0')) && (*pEnd <= _T('9')); pEnd++, iNumLen++);
67 if ((iNumLen > 15) || ((*pEnd != _T('.')) && (*pEnd != 0)))
68 return 0; // Number is definitely too large or not a number
69 memcpy(szNumber, pCurr, sizeof(TCHAR) * iNumLen);
70 szNumber[iNumLen] = 0;
71 pdwBuffer[dwLength++] = _tcstoul(szNumber, NULL, 10);
72 }
73 return dwLength;
74 }
75
76
77 //
78 // Check if given OID is syntaxically correct
79 //
80
81 BOOL LIBNXSNMP_EXPORTABLE SNMPIsCorrectOID(const TCHAR *pszText)
82 {
83 DWORD dwLength, *pdwBuffer;
84
85 pdwBuffer = (DWORD *)malloc(sizeof(DWORD) * MAX_OID_LEN);
86 dwLength = SNMPParseOID(pszText, pdwBuffer, MAX_OID_LEN);
87 free(pdwBuffer);
88 return (dwLength > 0);
89 }
90
91
92 //
93 // Get text for libnxsnmp error code
94 //
95
96 const TCHAR LIBNXSNMP_EXPORTABLE *SNMPGetErrorText(DWORD dwError)
97 {
98 static const TCHAR *pszErrorText[] =
99 {
100 _T("Operation completed successfully"),
101 _T("Request timed out"),
102 _T("Invalid parameters passed to function"),
103 _T("Unable to create socket"),
104 _T("Communication error"),
105 _T("Error parsing PDU"),
106 _T("No such object"),
107 _T("Invalid hostname or IP address"),
108 _T("OID is incorrect"),
109 _T("Agent error"),
110 _T("Unknown variable data type"),
111 _T("File I/O error"),
112 _T("Invalid file header"),
113 _T("Invalid or corrupted file data"),
114 _T("Unsupported security level"),
115 _T("Not in time window"),
116 _T("Unknown security name"),
117 _T("Unknown engine ID"),
118 _T("Authentication failure"),
119 _T("Decryption error"),
120 _T("Malformed or unexpected response from agent")
121 };
122
123 return ((dwError >= SNMP_ERR_SUCCESS) && (dwError <= SNMP_ERR_BAD_RESPONSE)) ?
124 pszErrorText[dwError] : _T("Unknown error");
125 }
126
127
128 //
129 // Resolve text representation of data type to integer value
130 //
131
132 DWORD LIBNXSNMP_EXPORTABLE SNMPResolveDataType(const TCHAR *pszType)
133 {
134 static struct
135 {
136 const TCHAR *pszName;
137 DWORD dwValue;
138 } typeList[] =
139 {
140 { _T("INT"), ASN_INTEGER },
141 { _T("INTEGER"), ASN_INTEGER },
142 { _T("STRING"), ASN_OCTET_STRING },
143 { _T("OID"), ASN_OBJECT_ID },
144 { _T("IPADDR"), ASN_IP_ADDR },
145 { _T("COUNTER32"), ASN_COUNTER32 },
146 { _T("GAUGE32"), ASN_GAUGE32 },
147 { _T("TIMETICKS"), ASN_TIMETICKS },
148 { _T("COUNTER64"), ASN_COUNTER64 },
149 { _T("UINT32"), ASN_UINTEGER32 },
150 { _T("UINTEGER32"), ASN_UINTEGER32 },
151 { NULL, 0 }
152 };
153 int i;
154
155 for(i = 0; typeList[i].pszName != NULL; i++)
156 if (!_tcsicmp(typeList[i].pszName, pszType))
157 return typeList[i].dwValue;
158 return ASN_NULL;
159 }
160
161
162 //
163 // DLL entry point
164 //
165
166 #ifdef _WIN32
167
168 #ifndef UNDER_CE // FIXME
169 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
170 {
171 if (dwReason == DLL_PROCESS_ATTACH)
172 DisableThreadLibraryCalls(hInstance);
173 return TRUE;
174 }
175 #endif // UNDER_CE
176
177 #endif /* _WIN32 */