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

📄 testmib.c

📁 windows的snmp api源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*++ BUILD Version: 0001    // Increment this if a change has global effects

Copyright (c) 1992-1996  Microsoft Corporation

Module Name:

    testmib.c

Abstract:

    Sample SNMP Extension Agent for Windows NT.

    These files (testdll.c, testmib.c, and testmib.h) provide an example of 
    how to structure an Extension Agent DLL which works in conjunction with 
    the SNMP Extendible Agent for Windows NT.

    Extensive comments have been included to describe its structure and
    operation.  See also "Microsoft Windows NT SNMP Programmer's Reference".

--*/


// This Extension Agent implements the Internet toaster MIB.  It's 
// definition follows here:
//
//
//         TOASTER-MIB DEFINITIONS ::= BEGIN
//
//         IMPORTS
//                 enterprises
//                         FROM RFC1155-SMI
//                 OBJECT-TYPE
//                         FROM RFC-1212
//                 DisplayString
//                         FROM RFC-1213;
//
//         epilogue        OBJECT IDENTIFIER ::= { enterprises 12 }
//         toaster         OBJECT IDENTIFIER ::= { epilogue 2 }
//
//         -- toaster MIB
//
//         toasterManufacturer OBJECT-TYPE
//             SYNTAX  DisplayString
//             ACCESS  read-only
//             STATUS  mandatory
//             DESCRIPTION
//                     "The name of the toaster's manufacturer. For instance,
//                      Sunbeam."
//             ::= { toaster 1 }
//
//         toasterModelNumber OBJECT-TYPE
//             SYNTAX  DisplayString
//             ACCESS  read-only
//             STATUS  mandatory
//             DESCRIPTION
//                     "The name of the toaster's model. For instance,
//                      Radiant Automatic."
//             ::= { toaster 2 }
//
//         toasterControl OBJECT-TYPE
//             SYNTAX  INTEGER  {
//                         up(1),
//                         down(2)
//                     }
//             ACCESS  read-write
//             STATUS  mandatory
//             DESCRIPTION
//                     "This variable controls the current state of the 
//                      toaster. To begin toasting, set it to down(2). To 
//                      abort toasting (perhaps in the event of an 
//                      emergency), set it to up(2)."
//             ::= { toaster 3 }
//
//         toasterDoneness OBJECT-TYPE
//             SYNTAX  INTEGER (1..10)
//             ACCESS  read-write
//             STATUS  mandatory
//             DESCRIPTION
//                     "This variable controls how well done ensuing toast 
//                      should be on a scale of 1 to 10. Toast made at 10 
//                      is generally considered unfit for human consumption; 
//                      toast made at 1 is lightly warmed."
//             ::= { toaster 4 }
//
//         toasterToastType OBJECT-TYPE
//             SYNTAX  INTEGER  {
//                         white-bread(1),
//                         wheat-bread(2),
//                         wonder-bread(3),
//                         frozen-waffle(4),
//                         frozen-bagel(5),
//                         hash-brown(6),
//                         other(7)
//                     }
//             ACCESS  read-write
//             STATUS  mandatory
//             DESCRIPTION
//                     "This variable informs the toaster of the type of 
//                      material being toasted. The toaster uses this 
//                      information combined with toasterToastDoneness to 
//                      compute how long the material must be toasted for 
//                      to achieve the desired doneness."
//             ::= { toaster 5 }
//
//         END


// Necessary includes.

#include <windows.h>

#include <snmp.h>


// Contains definitions for the table structure describing the MIB.  This
// is used in conjunction with testmib.c where the MIB requests are resolved.

#include "testmib.h"


// If an addition or deletion to the MIB is necessary, there are several
// places in the code that must be checked and possibly changed.
//
// The last field in each MIB entry is used to point to the NEXT
// leaf variable.  If an addition or deletetion is made, these pointers
// may need to be updated to reflect the modification.


// The prefix to all of these MIB variables is 1.3.6.1.4.1.12

UINT OID_Prefix[] = { 1, 3, 6, 1, 4, 1, 12 };
AsnObjectIdentifier MIB_OidPrefix = { OID_SIZEOF(OID_Prefix), OID_Prefix };



//                         //
// OID definitions for MIB //
//                         //


// Definition of the toaster group

UINT MIB_toaster[]  = { 2 };


// Definition of leaf variables under the toaster group
// All leaf variables have a zero appended to their OID to indicate
// that it is the only instance of this variable and it exists.

UINT MIB_toasterManufacturer[]     = { 2, 1, 0 };
UINT MIB_toasterModelNumber[]      = { 2, 2, 0 };
UINT MIB_toasterControl[]          = { 2, 3, 0 };
UINT MIB_toasterDoneness[]         = { 2, 4, 0 };
UINT MIB_toasterToastType[]        = { 2, 5, 0 };



//                             //
// Storage definitions for MIB //
//                             //

char       MIB_toasterManStor[]     = "Microsoft Corporation";
char       MIB_toasterModelStor[]   = 
               "Example SNMP Extension Agent for Windows/NT (TOASTER-MIB).";
AsnInteger MIB_toasterControlStor   = 1;
AsnInteger MIB_toasterDonenessStor  = 2;
AsnInteger MIB_toasterToastTypeStor = 3;



// MIB definiton

MIB_ENTRY Mib[] = {
      { { OID_SIZEOF(MIB_toasterManufacturer), MIB_toasterManufacturer },
        &MIB_toasterManStor, ASN_RFC1213_DISPSTRING,
        MIB_ACCESS_READ, MIB_leaf_func, &Mib[1] },

      { { OID_SIZEOF(MIB_toasterModelNumber), MIB_toasterModelNumber },
        &MIB_toasterModelStor, ASN_RFC1213_DISPSTRING,
        MIB_ACCESS_READ, MIB_leaf_func, &Mib[2] },

      { { OID_SIZEOF(MIB_toasterControl), MIB_toasterControl },
        &MIB_toasterControlStor, ASN_INTEGER,
        MIB_ACCESS_READWRITE, MIB_control_func, &Mib[3] },

      { { OID_SIZEOF(MIB_toasterDoneness), MIB_toasterDoneness },
        &MIB_toasterDonenessStor, ASN_INTEGER,
        MIB_ACCESS_READWRITE, MIB_doneness_func, &Mib[4] },

      { { OID_SIZEOF(MIB_toasterToastType), MIB_toasterToastType },
        &MIB_toasterToastTypeStor, ASN_INTEGER,
        MIB_ACCESS_READWRITE, MIB_toasttype_func, NULL }
      };

UINT MIB_num_variables = sizeof Mib / sizeof( MIB_ENTRY );



//
// ResolveVarBind
//    Resolves a single variable binding.  Modifies the variable on a GET
//    or a GET-NEXT.
//
// Notes:
//
// Return Codes:
//    Standard PDU error codes.
//
// Error Codes:
//    None.
//
UINT ResolveVarBind(
        IN OUT RFC1157VarBind *VarBind, // Variable Binding to resolve
	IN UINT PduAction               // Action specified in PDU
	)

{
MIB_ENTRY            *MibPtr;
AsnObjectIdentifier  TempOid;
int                  CompResult;
UINT                 I;
UINT                 nResult;


   // Search for var bind name in the MIB
   I      = 0;
   MibPtr = NULL;
   while ( MibPtr == NULL && I < MIB_num_variables )
      {
      // Construct OID with complete prefix for comparison purposes
      SnmpUtilOidCpy( &TempOid, &MIB_OidPrefix );
      SnmpUtilOidAppend( &TempOid, &Mib[I].Oid );

      // Check for OID in MIB - On a GET-NEXT the OID does not have to exactly
      // match a variable in the MIB, it must only fall under the MIB root.
      CompResult = SnmpUtilOidCmp( &VarBind->name, &TempOid );
      if ( 0 > CompResult )
	 {
	 // Since there is not an exact match, the only valid action is GET-NEXT
	 if ( MIB_ACTION_GETNEXT != PduAction )
	    {
	    nResult = SNMP_ERRORSTATUS_NOSUCHNAME;
	    goto Exit;
	    }

	 // Since the match was not exact, but var bind name is within MIB,
	 // we are at the NEXT MIB variable down from the one specified.
	 PduAction = MIB_ACTION_GET;
	 MibPtr = &Mib[I];

         // Replace var bind name with new name
         SnmpUtilOidFree( &VarBind->name );
         SnmpUtilOidCpy( &VarBind->name, &MIB_OidPrefix );
         SnmpUtilOidAppend( &VarBind->name, &MibPtr->Oid );
	 }
      else
         {
	 // An exact match was found.
         if ( 0 == CompResult )
            {
	    MibPtr = &Mib[I];
	    }
	 }

      // Free OID memory before checking another variable
      SnmpUtilOidFree( &TempOid );

      I++;
      } // while

   // If OID not within scope of MIB, then no such name
   if ( MibPtr == NULL )
      {
      nResult = SNMP_ERRORSTATUS_NOSUCHNAME;
      goto Exit;
      }

   // Call function to process request.  Each MIB entry has a function pointer
   // that knows how to process its MIB variable.
   nResult = (*MibPtr->MibFunc)( PduAction, MibPtr, VarBind );

   // Free temp memory
   SnmpUtilOidFree( &TempOid );

Exit:
   return nResult;
} // ResolveVarBind



//
// MIB_leaf_func
//    Performs generic actions on LEAF variables in the MIB.
//
// Notes:
//
// Return Codes:
//    Standard PDU error codes.
//
// Error Codes:
//    None.
//
UINT MIB_leaf_func(
        IN UINT Action,
	IN MIB_ENTRY *MibPtr,
	IN RFC1157VarBind *VarBind
	)

{
UINT   ErrStat;

   switch ( Action )
      {
      case MIB_ACTION_GETNEXT:
	 // If there is no GET-NEXT pointer, this is the end of this MIB
	 if ( MibPtr->MibNext == NULL )
	    {
	    ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;

⌨️ 快捷键说明

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