8bcb1130970b7105f9b45e715d8336cb5f757d7e
[public/netxms.git] / src / libnxsnmp / main.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: main.cpp
21 **
22 **/
23
24 #include "libnxsnmp.h"
25
26
27 //
28 // Convert OID to text
29 //
30
31 void 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 }
42
43
44 //
45 // Parse OID in text into binary format
46 // Will return 0 if OID is invalid or empty, and OID length (in DWORDs) on success
47 // Buffer size should be given in number of DWORDs
48 //
49
50 DWORD LIBNXSNMP_EXPORTABLE SNMPParseOID(const TCHAR *pszText, DWORD *pdwBuffer, DWORD dwBufferSize)
51 {
52 TCHAR *pCurr = (TCHAR *)pszText, *pEnd, szNumber[32];
53 DWORD dwLength = 0;
54 int iNumLen;
55
56 if (*pCurr == 0)
57 return 0;
58
59 // Skip initial dot if persent
60 if (*pCurr == _T('.'))
61 pCurr++;
62
63 for(pEnd = pCurr; (*pEnd != 0) && (dwLength < dwBufferSize); pCurr = pEnd + 1)
64 {
65 for(iNumLen = 0, pEnd = pCurr; (*pEnd >= _T('0')) && (*pEnd <= _T('9')); pEnd++, iNumLen++);
66 if ((iNumLen > 15) || ((*pEnd != _T('.')) && (*pEnd != 0)))
67 return 0; // Number is definitely too large or not a number
68 memcpy(szNumber, pCurr, sizeof(TCHAR) * iNumLen);
69 szNumber[iNumLen] = 0;
70 pdwBuffer[dwLength++] = _tcstoul(szNumber, NULL, 10);
71 }
72 return dwLength;
73 }
74
75
76 //
77 // Check if given OID is syntaxically correct
78 //
79
80 BOOL LIBNXSNMP_EXPORTABLE SNMPIsCorrectOID(const TCHAR *pszText)
81 {
82 DWORD dwLength, *pdwBuffer;
83
84 pdwBuffer = (DWORD *)malloc(sizeof(DWORD) * MAX_OID_LEN);
85 dwLength = SNMPParseOID(pszText, pdwBuffer, MAX_OID_LEN);
86 free(pdwBuffer);
87 return (dwLength > 0);
88 }
89
90
91 //
92 // Get text for libnxsnmp error code
93 //
94
95 const TCHAR LIBNXSNMP_EXPORTABLE *SNMPGetErrorText(DWORD dwError)
96 {
97 static TCHAR *pszErrorText[] =
98 {
99 _T("Operation completed successfully"),
100 _T("Request timed out"),
101 _T("Invalid parameters passed to function"),
102 _T("Unable to create socket"),
103 _T("Communication error"),
104 _T("Rrror parsing PDU"),
105 _T("No such object"),
106 _T("Invalid hostname or IP address"),
107 _T("OID is incorrect"),
108 _T("Agent error"),
109 _T("Unknown variable data type"),
110 _T("File I/O error"),
111 _T("Invalid file header"),
112 _T("Invalid or corrupted file data")
113 };
114
115 return ((dwError >= SNMP_ERR_SUCCESS) && (dwError <= SNMP_ERR_BAD_FILE_DATA)) ?
116 pszErrorText[dwError] : _T("Unknown error");
117 }
118
119
120 //
121 // DLL entry point
122 //
123
124 #ifdef _WIN32
125
126 #ifndef UNDER_CE // FIXME
127 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
128 {
129 return TRUE;
130 }
131 #endif // UNDER_CE
132
133 #endif /* _WIN32 */