Microsoft KB Archive/192796

{|
 * width="100%"|

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

 * }

Q192796

-

The information in this article applies to:


 * Microsoft Win32 Software Development Kit (SDK), used with:
 * the operating system: Microsoft Windows NT, versions 3.51, 4.0, 5.0
 * the operating system: Microsoft Windows 2000
 * Microsoft Win32 Application Programming Interface (API), included with:
 * the operating system: Microsoft Windows NT, versions 3.51, 4.0, 5.0
 * the operating system: Microsoft Windows 2000

-

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 or Windows 2000 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:

  Include the mgmtapi.h header file to your testdll.c file as follows:      #include       #include    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.  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:

 Configure your SNMP service to send trap to localhost.  Run the snmputil.exe SNMP manager from Platform SDK sample in console A as follows: <pre class="CODESAMP">     C:\>snmputil trap snmputil: listening for traps... </li>  Run another instance of snmputil.exe in another console as follows: <pre class="CODESAMP">     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. </li>  You will see the trap data in console A as follows: <pre class="CODESAMP">     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 </li></ol>

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 kbOSWinNT351 kbOSWinNT400 kbOSWin2000 kbSDKPlatform kbSNMP kbSNMPAgent kbGrpDSNet

Issue type : kbhowto

Technology : kbWin32SDKSearch kbAudDeveloper kbSDKSearch kbWin32sSearch