⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oidconv.c

📁 windows的snmp api源码
💻 C
📖 第 1 页 / 共 2 页
字号:
//    Converts an OID to its textual description.
//
// Notes:
//
// Return Codes:
//    SNMPAPI_NOERROR
//    SNMPAPI_ERROR
//
// Error Codes:
//    None.
//
SNMPAPI SnmpMgrOid2Text(
           IN AsnObjectIdentifier *Oid, // Pointer to OID to convert
	   OUT LPSTR *lpszTextOid       // Resulting text OID
	   )

{
T_FILE_NODE  Node;
OFSTRUCT     of;
HFILE        fh;
UINT         Siblings;
UINT         OidSubId;
UINT         uTxtOidLen;
BOOL         bFound;
BOOL         bPartial;
BOOL         bDot;
SNMPAPI      nResult;

   // OPENISSUE - this code does not generate errors if subid 0 is embeded
   // OPENISSUE - opening file every time could be a performance issue
   // OPENISSUE - optimization of file access could improve performance

   *lpszTextOid = NULL;

   // Open file and check for errors
   if ( -1 == (fh = OpenFile(lpInputFileName, &of, OF_READ|OF_SHARE_DENY_WRITE)) )
      {
      nResult = SNMPAPI_ERROR;
      goto Exit;
      }

   // Test for MIB prefix
   bDot = !( bPartial = OID_PREFIX_LEN < Oid->idLength &&
                        !SnmpUtilOidNCmp(Oid, &MIB_OidPrefix, OID_PREFIX_LEN) );

   // Loop until conversion is finished
   OidSubId          = 0;
   uTxtOidLen        = 0;
   Node.uNumChildren = 1;
   while ( OidSubId < Oid->idLength )
      {
      // Init to not found on this level
      bFound   = FALSE;
      Siblings = Node.uNumChildren;

      // While their are siblings and the sub id is not found keep looking
      while ( Siblings && !bFound )
         {
	 Node.lpszTextSubID = NULL;

	 // Get next sibling
         if ( SNMPAPI_ERROR == GetNextNode(fh, &Node) )
            {
            SnmpUtilMemFree( Node.lpszTextSubID );

            nResult = SNMPAPI_ERROR;
            goto Exit;
            }

         Siblings --;

	 // Compare the numeric subid's
         if ( Oid->ids[OidSubId] == Node.uNumSubID )
	    {
	    bFound = TRUE;

	    // If OID is a partial, then skip prefix subid's
	    if ( OidSubId >= OID_PREFIX_LEN || !bPartial )
	       {
	       // Realloc space for text id - add 2 for '.' and NULL terminator
	       if ( NULL == (*lpszTextOid =
	                     SnmpUtilMemReAlloc(*lpszTextOid,
		                  (uTxtOidLen+Node.uStrLen+2) * sizeof(char))) )
	          {
                  SnmpUtilMemFree( Node.lpszTextSubID );

                  nResult = SNMPAPI_ERROR;
	          goto Exit;
	          }

	       // Add DOT separator
	       if ( bDot )
	          {
	          (*lpszTextOid)[uTxtOidLen] = '.';

	          // Save text subid
	          memcpy( &(*lpszTextOid)[uTxtOidLen+1],
	                  Node.lpszTextSubID, Node.uStrLen+1 );

	          // Update length of text oid - add one for separator
	          uTxtOidLen += Node.uStrLen + 1;
	          }
	       else
	          {
	          bDot = TRUE;

	          // Save text subid
	          memcpy( &(*lpszTextOid)[uTxtOidLen],
	                  Node.lpszTextSubID, Node.uStrLen );

	          // Update length of text oid
	          uTxtOidLen += Node.uStrLen;
	          }
	       }

	    // Skip to next OID subid
	    OidSubId ++;
	    }
	 else
	    {
            // Skip over subtree since not a match
            if ( SNMPAPI_ERROR == SkipSubTree(fh, &Node) )
               {
	       SnmpUtilMemFree( Node.lpszTextSubID );

	       nResult = SNMPAPI_ERROR;
               goto Exit;
               }
	    }

	 // Free the text sub id read
	 SnmpUtilMemFree( Node.lpszTextSubID );
         } // while

      // If no sub id matches
      if ( !bFound )
         {
	 break;
	 }
      } // while

   // Make sure that the entire OID was converted
   while ( OidSubId < Oid->idLength )
      {
      char NumChar[100];


      _itoa( Oid->ids[OidSubId], NumChar, 10 );
      // Realloc space for text id - add 2 for '.' and NULL terminator
      if ( NULL ==
           (*lpszTextOid = SnmpUtilMemReAlloc(*lpszTextOid,
                          (uTxtOidLen+strlen(NumChar)+4) * sizeof(char))) )
         {
	 nResult = SNMPAPI_ERROR;
	 goto Exit;
	 }

      // Add DOT separator
      (*lpszTextOid)[uTxtOidLen] = '.';

      // Save text subid
      memcpy( &(*lpszTextOid)[uTxtOidLen+1], NumChar, strlen(NumChar)+1 );

      // Skip to next OID subid
      OidSubId ++;

      // Update length of text oid - add one for separator
      uTxtOidLen += strlen(NumChar) + 1;
      } // while

   nResult = SNMPAPI_NOERROR;

Exit:
   if ( -1 != fh )
      {
      _lclose( fh );
      }

   if ( SNMPAPI_ERROR == nResult )
      {
      SnmpUtilMemFree( *lpszTextOid );
      *lpszTextOid = NULL;
      }

   return nResult;
} // SnmpMgrOid2Text



//
// SnmpMgrText2Oid
//    Converts an OID text description to its numerical equivalent.
//
// Notes:
//
// Return Codes:
//    SNMPAPI_NOERROR
//    SNMPAPI_ERROR
//
// Error Codes:
//    None.
//
SNMPAPI SnmpMgrText2Oid(
         IN LPSTR lpszTextOid,           // Pointer to text OID to convert
	 IN OUT AsnObjectIdentifier *Oid // Resulting numeric OID
	 )

{
#define DELIMETERS         ".\0"


T_FILE_NODE  Node;
OFSTRUCT     of;
HFILE        fh;
UINT         Siblings;
LPSTR        lpszSubId;
LPSTR        lpszWrkOid = NULL;
BOOL         bFound;
UINT         uSubId;
SNMPAPI      nResult;

   // OPENISSUE - this code does not generate errors if subid 0 is embeded
   // OPENISSUE - opening file every time could be a performance issue
   // OPENISSUE - optimization of file access could improve performance

   // Init. OID structure
   Oid->idLength = 0;
   Oid->ids      = NULL;

   // check for null string and empty string
   if ( NULL == lpszTextOid || '\0' == lpszTextOid[0] )
      {
      fh = -1;

      nResult = SNMPAPI_NOERROR;
      goto Exit;
      }

   // Open file and check for errors
   if ( -1 == (fh = OpenFile(lpInputFileName, &of, OF_READ|OF_SHARE_DENY_WRITE)) )
      {
      nResult = SNMPAPI_ERROR;
      goto Exit;
      }

   // Make working copy of string
   if ( ('.' == lpszTextOid[0]) )
      {
      if ( NULL == (lpszWrkOid = SnmpUtilMemAlloc((strlen(lpszTextOid)+1) * sizeof(char))) )
         {
         nResult = SNMPAPI_ERROR;
         goto Exit;
         }

      strcpy( lpszWrkOid, lpszTextOid+1 );
      }
   else
      {
      if ( NULL ==
           (lpszWrkOid =
            SnmpUtilMemAlloc((strlen(lpszTextOid)+STR_PREFIX_LEN+1+1) * sizeof(char))) )
         {
         nResult = SNMPAPI_ERROR;
         goto Exit;
         }

      strcpy( lpszWrkOid, MIB_StrPrefix );
      lpszWrkOid[STR_PREFIX_LEN] = '.';
      strcpy( &lpszWrkOid[STR_PREFIX_LEN+1], lpszTextOid );
      }

   Node.uNumChildren = 1;
   lpszSubId = strtok( lpszWrkOid, DELIMETERS );

   // Loop until conversion is finished
   while ( NULL != lpszSubId )
      {

      // Init to not found on this level
      bFound   = FALSE;
      Siblings = Node.uNumChildren;

      // Check for imbedded numbers
      if ( isdigit(*lpszSubId) )
         {
         UINT I;


         // Make sure this is a NUMBER without alpha's
         for ( I=0;I < strlen(lpszSubId);I++ )
            {
            if ( !isdigit(lpszSubId[I]) )
               {
	       nResult = SNMPAPI_ERROR;
	       goto Exit;
	       }
	    }

         uSubId = atoi( lpszSubId );
         }
      else
         {
         uSubId = 0;
         }

      // While their are siblings and the sub id is not found keep looking
      while ( Siblings && !bFound )
         {
	 Node.lpszTextSubID = NULL;

	 // Get next sibling
         if ( SNMPAPI_ERROR == GetNextNode(fh, &Node) )
            {

            SnmpUtilMemFree( Node.lpszTextSubID );

            nResult = SNMPAPI_ERROR;
            goto Exit;
            }

         Siblings --;

	 if ( uSubId )
	    {
	    // Compare the numeric subid's
	    if ( Node.uNumSubID == uSubId )
	       {
	       bFound = TRUE;

	       // Add space for new sub id
	       if ( NULL ==
                    (Oid->ids =
                     SnmpUtilMemReAlloc(Oid->ids, (Oid->idLength+1) * sizeof(UINT))) )
	          {
                  SnmpUtilMemFree( Node.lpszTextSubID );

	          nResult = SNMPAPI_ERROR;
	          goto Exit;
	          }

	       // Append this sub id to end of numeric OID
               Oid->ids[Oid->idLength++] = Node.uNumSubID;
	       }
	    }
         else
            {
	    // Compare the text subid's
	    if ( !strcmp(lpszSubId, Node.lpszTextSubID) )
	       {
	       bFound = TRUE;

	       // Add space for new sub id
	       if ( NULL ==
                    (Oid->ids =
                     SnmpUtilMemReAlloc(Oid->ids, (Oid->idLength+1) * sizeof(UINT))) )
	          {
                  SnmpUtilMemFree( Node.lpszTextSubID );

	          nResult = SNMPAPI_ERROR;
	          goto Exit;
	          }

	       // Append this sub id to end of numeric OID
               Oid->ids[Oid->idLength++] = Node.uNumSubID;
	       }
	    }

         // Skip over subtree since not a match
         if ( !bFound && SNMPAPI_ERROR == SkipSubTree(fh, &Node) )
            {
	    SnmpUtilMemFree( Node.lpszTextSubID );

            nResult = SNMPAPI_ERROR;
            goto Exit;
            }

	 // Free the text sub id read
	 SnmpUtilMemFree( Node.lpszTextSubID );
         } // while

      // If no sub id matches
      if ( !bFound )
         {
	 break;
	 }

      // Advance to next sub id
      lpszSubId = strtok( NULL, DELIMETERS );
      } // while

   // Make sure that the entire OID was converted
   while ( NULL != lpszSubId )
      {
      UINT I;


      // Make sure this is a NUMBER without alpha's
      for ( I=0;I < strlen(lpszSubId);I++ )
         {
         if ( !isdigit(lpszSubId[I]) )
            {
	    nResult = SNMPAPI_ERROR;
	    goto Exit;
	    }
	 }

      // Add space for new sub id
      if ( NULL ==
           (Oid->ids = SnmpUtilMemReAlloc(Oid->ids, (Oid->idLength+1) * sizeof(UINT))) )
         {
         SnmpUtilMemFree( Node.lpszTextSubID );

         nResult = SNMPAPI_ERROR;
	 goto Exit;
	 }

      // Append this sub id to end of numeric OID
      Oid->ids[Oid->idLength++] = atoi( lpszSubId );

      // Advance to next sub id
      lpszSubId = strtok( NULL, DELIMETERS );
      } // while


   // it is illegal for an oid to be less than two subidentifiers
   if (Oid->idLength < 2)
       {
       nResult = SNMPAPI_ERROR;
       goto Exit;
       }


   nResult = SNMPAPI_NOERROR;

Exit:
   if ( -1 != fh )
      {
      _lclose( fh );
      }

   if ( SNMPAPI_ERROR == nResult )
      {
      SnmpUtilOidFree( Oid );
      }

   if ( NULL != lpszWrkOid ) {
        SnmpUtilMemFree ( lpszWrkOid );
   }

   return nResult;
} // SnmpMgrText2Oid

//------------------------------- END ---------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -