HOWTO: Send SNMP Trap with Variable Bindings from Testdll.dll

ID: Q192796

The information in this article applies to:

SUMMARY

This article demonstrates how to send an SNMP trap protocol data unit (PDU) consisting of variable bindings from an SNMP extension agent running on a Windows NT computer. This technique is illustrated by modifying the Testdll.dll SNMP extension agent sample located in \mssdk\samples\netds\snmp\testdll of the Platform SDK.

MORE INFORMATION

The following sample code composes the toasterControl MIB (.1.3.6.1.4.1.12.2.3.0), toasterDoness MIB (.1.3.6.1.4.1.12.2.4.0) and toastType MIB(.1.3.6.1.4.1.12.2.5.0) variables into a variable binding list and passes the list back to the SNMP service.

You need to modify the following three steps in the Testdll.dll SNMP extension agent sample:

1. Include the mgmtapi.h header file to your testdll.c file as follows:

      #include <snmp.h>
      #include <mgmtapi.h>

2. Replace the SnmpExtensionTrap entry point in the file testdll.c with
   the following code:

      BOOL WINAPI SnmpExtensionTrap(
         OUT AsnObjectIdentifier *enterprise,
         OUT AsnInteger          *genericTrap,
         OUT AsnInteger          *specificTrap,
         OUT AsnTimeticks        *timeStamp,
         OUT RFC1157VarBindList  *variableBindings)
      {
         // The following define data inserted into the trap below.

         static UINT OidList[]  = { 1, 3, 6, 1, 4, 1, 12, 2 };
         static UINT OidListLen = 8;

         // The following variable is used for the simulation. It allows a
         // single trap to be generated and causes FALSE to be returned,
         // indicating no more traps.

         static whichTime = 0;
         RFC1157VarBind* pVB; // Variable binding.
         AsnObjectIdentifier reqObject;
         int MIB_num_variables = 3; // Hard coded for testing.

         // The following if/else support the simulation.

         if (whichTime == 0)
         {
            whichTime = 1;    // Supports the simulation.

         // Communicate the trap data to the Extendible Agent.

            enterprise->idLength = OidListLen;
            enterprise->ids = OidList;

            *genericTrap      = SNMP_GENERICTRAP_ENTERSPECIFIC;

            *specificTrap     = 1;   // ToasterControl Up trap.

            *timeStamp        = GetCurrentTime() - dwTimeZero;

            // Allocate space for the Variable Bindings.
            pVB = SnmpUtilMemAlloc(MIB_num_variables *
                                   sizeof(RFC1157VarBind));
            memset(pVB, 0, MIB_num_variables * sizeof(RFC1157VarBind));
            if (pVB == NULL)
            {
               whichTime = 0;
               return FALSE;
            }
            // Copy the VarBindList.
            variableBindings->list = pVB;
            variableBindings->len  = MIB_num_variables;

            // ToasterControl.
            if (SnmpMgrStrToOid(".1.3.6.1.4.1.12.2.3.0", &reqObject) == 0)
            {
               whichTime = 0;
               SnmpUtilMemFree(variableBindings->list);
               variableBindings->list = NULL;
               variableBindings->len  = 0;
               return FALSE;
            }

            pVB->name = reqObject; // NOTE: Structure copy.
            pVB->value.asnType = ASN_INTEGER;
            // Toaster control in down position.
            pVB->value.asnValue.number = 2;
            pVB++;

            // ToasterDoness.
            if (SnmpMgrStrToOid(".1.3.6.1.4.1.12.2.4.0", &reqObject) == 0)
            {
               whichTime = 0;

               pVB--;
               // Free previous allocated oid.
               SnmpUtilOidFree(&(pVB->name));
               SnmpUtilMemFree(variableBindings->list);
               variableBindings->list = NULL;
               variableBindings->len  = 0;
               return FALSE;
            }

            pVB->name = reqObject; // NOTE!  structure copy
            pVB->value.asnType = ASN_INTEGER;
            pVB->value.asnValue.number = 1; // Toaster Doness is lightest.
            pVB++;

            // ToastType.
            if (SnmpMgrStrToOid(".1.3.6.1.4.1.12.2.5.0", &reqObject) == 0)
            {
               whichTime = 0;

               pVB--;
               // Free previous allocated oid.
               SnmpUtilOidFree(&(pVB->name));
               pVB--;
               // Free previous allocated oid.
               SnmpUtilOidFree(&(pVB->name));

               SnmpUtilMemFree(variableBindings->list);
               variableBindings->list = NULL;
               variableBindings->len  = 0;
               return FALSE;
            }

            pVB->name = reqObject; // NOTE: Structure copy.
            pVB->value.asnType = ASN_INTEGER;
            pVB->value.asnValue.number = 3; // Wonder-bread(3).

            // Indicate that valid trap data exists in the parameters.
            return TRUE;
         }
         else
         {
            // The previous allocated variable binding has been freed
            // by SNMP service.
            whichTime = 0;    // Supports the simulation.

            // Indicate that no more traps are available, and parameters do
            // not refer to any valid data.

            return FALSE;
         }

      } // End SnmpExtensionTrap().

3. Modify the Makefile to add SNMP Management library mgmtapi.lib to the
   link command.

Do the following to see the trap data sent by the code sample:

1. Configure your SNMP service to send trap to localhost.

2. Run the snmputil.exe SNMP manager from Platform SDK sample in

   console A as follows:

      C:\>snmputil trap
      snmputil: listening for traps...

3. Run another instance of snmputil.exe in another console as follows:

      C:\>snmputil walk localhost public .1.3.6.1.4.1.12.2
      Variable = .iso.org.dod.internet.private.enterprises.12.2.1.0
      Value    = OCTET STRING - Microsoft Corporation

      Variable = .iso.org.dod.internet.private.enterprises.12.2.2.0
      Value    = OCTET STRING - Example SNMP Extension Agent for
                 Windows/NT  (TOASTER-MIB).

      Variable = .iso.org.dod.internet.private.enterprises.12.2.3.0
      Value    = INTEGER - 1

      Variable = .iso.org.dod.internet.private.enterprises.12.2.4.0
      Value    = INTEGER - 2

      Variable = .iso.org.dod.internet.private.enterprises.12.2.5.0
      Value    = INTEGER - 3

      End of MIB subtree.

4. You will see the trap data in console A as follows:

      snmputil: trap generic=6 specific=1
        from -> xxx.xxx.xxx.xxx
      Variable = .iso.org.dod.internet.private.enterprises.12.2.3.0
      Value    = INTEGER - 2
      Variable = .iso.org.dod.internet.private.enterprises.12.2.4.0
      Value    = INTEGER - 1
      Variable = .iso.org.dod.internet.private.enterprises.12.2.5.0
      Value    = INTEGER - 3

NOTE: This code sample will not work under Windows 9x platforms because SnmpMgrStrToOid is a function exported from mgmtapi.lib, which is not available in the Windows 9x platforms. To work around this, you have to provide a similar utility on your own.

Additional query words: variable binding SNMP trap

Keywords          : kbnetwork kbAPI kbSDKPlatform kbSNMP kbSNMPAgent kbGrpNet 
Issue type        : kbhowto

Last Reviewed: September 18, 1998