"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.8.166/src/snmp/libnxsnmp/main.cpp" (23 Feb 2021, 6462 Bytes) of package /linux/misc/netxms-3.8.166.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "main.cpp" see the Fossies "Dox" file reference documentation.

    1 /* 
    2 ** NetXMS - Network Management System
    3 ** SNMP support library
    4 ** Copyright (C) 2003-2020 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  * Convert OID to text
   28  */
   29 TCHAR LIBNXSNMP_EXPORTABLE *SNMPConvertOIDToText(size_t length, const uint32_t *value, TCHAR *buffer, size_t bufferSize)
   30 {
   31    buffer[0] = 0;
   32    for(size_t i = 0, bufPos = 0; (i < length) && (bufPos < bufferSize); i++)
   33    {
   34       size_t numChars = _sntprintf(&buffer[bufPos], bufferSize - bufPos, _T(".%u"), value[i]);
   35       bufPos += numChars;
   36    }
   37     return buffer;
   38 }
   39 
   40 /**
   41  * Parse OID in text into binary format
   42  * Will return 0 if OID is invalid or empty, and OID length (in UINT32s) on success
   43  * Buffer size should be given in number of UINT32s
   44  */
   45 size_t LIBNXSNMP_EXPORTABLE SNMPParseOID(const TCHAR *text, uint32_t *buffer, size_t bufferSize)
   46 {
   47    TCHAR *pCurr = (TCHAR *)text, *pEnd, szNumber[32];
   48    size_t length = 0;
   49    int iNumLen;
   50 
   51    if (*pCurr == 0)
   52       return 0;
   53 
   54    // Skip initial dot if present
   55    if (*pCurr == _T('.'))
   56       pCurr++;
   57 
   58    for(pEnd = pCurr; (*pEnd != 0) && (length < bufferSize); pCurr = pEnd + 1)
   59    {
   60       for(iNumLen = 0, pEnd = pCurr; (*pEnd >= _T('0')) && (*pEnd <= _T('9')); pEnd++, iNumLen++);
   61       if ((iNumLen > 15) || ((*pEnd != _T('.')) && (*pEnd != 0)))
   62          return 0;   // Number is definitely too large or not a number
   63       memcpy(szNumber, pCurr, sizeof(TCHAR) * iNumLen);
   64       szNumber[iNumLen] = 0;
   65       buffer[length++] = _tcstoul(szNumber, NULL, 10);
   66    }
   67    return length;
   68 }
   69 
   70 /**
   71  * Check if given OID is syntaxically correct
   72  */
   73 bool LIBNXSNMP_EXPORTABLE SNMPIsCorrectOID(const TCHAR *oid)
   74 {
   75    uint32_t buffer[MAX_OID_LEN];
   76    size_t len = SNMPParseOID(oid, buffer, MAX_OID_LEN);
   77    return (len > 0);
   78 }
   79 
   80 /**
   81  * Check if given OID is syntaxically correct
   82  */
   83 size_t LIBNXSNMP_EXPORTABLE SNMPGetOIDLength(const TCHAR *oid)
   84 {
   85    uint32_t buffer[MAX_OID_LEN];
   86    return SNMPParseOID(oid, buffer, MAX_OID_LEN);
   87 }
   88 
   89 /**
   90  * Get text for libnxsnmp error code
   91  */
   92 const TCHAR LIBNXSNMP_EXPORTABLE *SNMPGetErrorText(uint32_t errorCode)
   93 {
   94    static const TCHAR *errorText[] =
   95    {
   96       _T("Operation completed successfully"),
   97       _T("Request timed out"),
   98       _T("Invalid parameters passed to function"),
   99       _T("Unable to create socket"),
  100       _T("Communication error"),
  101       _T("Error parsing PDU"),
  102       _T("No such object"),
  103       _T("Invalid hostname or IP address"),
  104       _T("OID is incorrect"),
  105       _T("Agent error"),
  106       _T("Unknown variable data type"),
  107       _T("File I/O error"),
  108       _T("Invalid file header"),
  109       _T("Invalid or corrupted file data"),
  110       _T("Unsupported security level"),
  111       _T("Not in time window"),
  112       _T("Unknown security name"),
  113       _T("Unknown engine ID"),
  114       _T("Authentication failure"),
  115       _T("Decryption error"),
  116       _T("Malformed or unexpected response from agent"),
  117       _T("Operation aborted")
  118    };
  119 
  120    return (errorCode <= SNMP_ERR_ABORTED) ? errorText[errorCode] : _T("Unknown error");
  121 }
  122 
  123 /**
  124  * Get text for protocol error code
  125  */
  126 const TCHAR LIBNXSNMP_EXPORTABLE *SNMPGetProtocolErrorText(SNMP_ErrorCode errorCode)
  127 {
  128    static const TCHAR *errorText[] =
  129    {
  130       _T("Success"),
  131       _T("Response is too big"),
  132       _T("No such name"),
  133       _T("Bad value"),
  134       _T("Read only variable"),
  135       _T("Generic error"),
  136       _T("No access"),
  137       _T("Wrong type"),
  138       _T("Wrong length"),
  139       _T("Wrong encoding"),
  140       _T("Wrong value"),
  141       _T("Creation not allowed"),
  142       _T("Inconsistent value"),
  143       _T("Resource unavailable"),
  144       _T("Commit failed"),
  145       _T("Undo failed"),
  146       _T("Authorization error"),
  147       _T("Not writable"),
  148       _T("Inconsistent name")
  149    };
  150 
  151    return ((static_cast<int>(errorCode) >= 0) && (static_cast<int>(errorCode) <= SNMP_PDU_ERR_INCONSISTENT_NAME)) ? errorText[errorCode] : _T("Unknown error");
  152 }
  153 
  154 /**
  155  * SNMP type names
  156  */
  157 static CodeLookupElement s_typeList[] =
  158 {
  159    { ASN_BIT_STRING, _T("BIT STRING") },
  160    { ASN_COUNTER32, _T("COUNTER32") },
  161    { ASN_COUNTER64, _T("COUNTER64") },
  162    { ASN_DOUBLE, _T("DOUBLE") },
  163    { ASN_FLOAT, _T("FLOAT") },
  164    { ASN_GAUGE32, _T("GAUGE32") },
  165    { ASN_INTEGER, _T("INTEGER") },
  166    { ASN_INTEGER, _T("INT") },
  167    { ASN_INTEGER64, _T("INTEGER64") },
  168    { ASN_IP_ADDR, _T("IP ADDRESS") },
  169    { ASN_IP_ADDR, _T("IPADDR") },
  170    { ASN_NSAP_ADDR, _T("NSAP ADDRESS") },
  171    { ASN_NULL, _T("NULL") },
  172    { ASN_OBJECT_ID, _T("OBJECT IDENTIFIER") },
  173    { ASN_OBJECT_ID, _T("OID") },
  174    { ASN_OCTET_STRING, _T("STRING") },
  175    { ASN_OCTET_STRING, _T("OCTET STRING") },
  176    { ASN_OPAQUE, _T("OPAQUE") },
  177    { ASN_SEQUENCE, _T("SEQUENCE") },
  178    { ASN_TIMETICKS, _T("TIMETICKS") },
  179    { ASN_UINTEGER32, _T("UINTEGER32") },
  180    { ASN_UINTEGER32, _T("UINT32") },
  181    { ASN_UINTEGER64, _T("UINTEGER64") },
  182    { 0, nullptr }
  183 };
  184 
  185 /**
  186  * Resolve text representation of data type to integer value
  187  */
  188 uint32_t LIBNXSNMP_EXPORTABLE SNMPResolveDataType(const TCHAR *type)
  189 {
  190    for(int i = 0; s_typeList[i].text != nullptr; i++)
  191       if (!_tcsicmp(s_typeList[i].text, type))
  192          return s_typeList[i].code;
  193    return ASN_NULL;
  194 }
  195 
  196 /**
  197  * Get type name
  198  */
  199 TCHAR LIBNXSNMP_EXPORTABLE *SNMPDataTypeName(uint32_t type, TCHAR *buffer, size_t bufferSize)
  200 {
  201     for(int i = 0; s_typeList[i].text != nullptr; i++)
  202         if (s_typeList[i].code == type)
  203         {
  204             _tcslcpy(buffer, s_typeList[i].text, bufferSize);
  205             return buffer;
  206         }
  207 
  208     _sntprintf(buffer, bufferSize, _T("0x%02x"), type);
  209     return buffer;
  210 }
  211 
  212 /**
  213  * DLL entry point
  214  */
  215 #ifdef _WIN32
  216 
  217 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  218 {
  219    if (dwReason == DLL_PROCESS_ATTACH)
  220       DisableThreadLibraryCalls(hInstance);
  221    return TRUE;
  222 }
  223 
  224 #endif   /* _WIN32 */