home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / snmp / testdll / testdll.c < prev    next >
C/C++ Source or Header  |  1996-08-09  |  9KB  |  285 lines

  1. /*++ BUILD Version: 0001    // Increment this if a change has global effects
  2.  
  3. Copyright (c) 1992-1996  Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     testdll.c
  8.  
  9. Abstract:
  10.  
  11.     Sample SNMP Extension Agent for Windows NT.
  12.  
  13.     These files (testdll.c, testmib.c, and testmib.h) provide an example of 
  14.     how to structure an Extension Agent DLL which works in conjunction with 
  15.     the SNMP Extendible Agent for Windows NT.
  16.  
  17.     Extensive comments have been included to describe its structure and
  18.     operation.  See also "Microsoft Windows NT SNMP Programmer's Reference".
  19.  
  20. --*/
  21.  
  22.  
  23. // General notes:
  24. //
  25. //   Microsoft's Extendible Agent for Windows NT is implemented by dynamically
  26. // linking to Extension Agent DLLs that implement portions of the MIB.  These
  27. // Extension Agents are configured in the Windows NT Registration Database.
  28. // When the Extendible Agent Service is started, it queries the registry to
  29. // determine which Extension Agent DLLs have been installed and need to be
  30. // loaded and initialized.  The Extendible Agent invokes various DLL entry
  31. // points (examples follow in this file) to request MIB queries and obtain
  32. // Extension Agent generated traps.
  33.  
  34.  
  35. // Necessary includes.
  36.  
  37. #include <windows.h>
  38.  
  39. #include <snmp.h>
  40.  
  41.  
  42. // Contains definitions for the table structure describing the MIB.  This
  43. // is used in conjunction with testmib.c where the MIB requests are resolved.
  44.  
  45. #include "testmib.h"
  46.  
  47.  
  48. // Extension Agent DLLs need access to elapsed time agent has been active.
  49. // This is implemented by initializing the Extension Agent with a time zero
  50. // reference, and allowing the agent to compute elapsed time by subtracting
  51. // the time zero reference from the current system time.  This example
  52. // Extension Agent implements this reference with dwTimeZero.
  53.  
  54. DWORD dwTimeZero = 0;
  55.  
  56.  
  57. // Extension Agent DLLs that generate traps must create a Win32 Event object
  58. // to communicate occurence of traps to the Extendible Agent.  The event
  59. // handle is given to the Extendible Agent when the Extension Agent is 
  60. // initialized, it should be NULL if traps will not be generated.  This
  61. // example Extension Agent simulates the occurance of traps with hSimulateTrap.
  62.  
  63. HANDLE hSimulateTrap = NULL;
  64.  
  65.  
  66. // This is a standard Win32 DLL entry point.  See the Win32 SDK for more
  67. // information on its arguments and their meanings.  This example DLL does 
  68. // not perform any special actions using this mechanism.
  69.  
  70. BOOL WINAPI DLLEntry(
  71.     HANDLE hDll,
  72.     DWORD  dwReason,
  73.     LPVOID lpReserved)
  74.     {
  75.     switch(dwReason)
  76.         {
  77.         case DLL_PROCESS_ATTACH:
  78.         case DLL_PROCESS_DETACH:
  79.         case DLL_THREAD_ATTACH:
  80.         case DLL_THREAD_DETACH:
  81.         default:
  82.             break;
  83.  
  84.         } // end switch()
  85.  
  86.     return TRUE;
  87.  
  88.     } // end DllEntryPoint()
  89.  
  90.  
  91. // Extension Agent DLLs provide the following entry point to coordinate the
  92. // initializations of the Extension Agent and the Extendible Agent.  The
  93. // Extendible Agent provides the Extension Agent with a time zero reference;
  94. // and the Extension Agent provides the Extendible Agent with an Event handle 
  95. // for communicating occurence of traps, and an object identifier representing
  96. // the root of the MIB subtree that the Extension Agent supports.
  97.  
  98. BOOL WINAPI SnmpExtensionInit(
  99.     IN  DWORD               dwTimeZeroReference,
  100.     OUT HANDLE              *hPollForTrapEvent,
  101.     OUT AsnObjectIdentifier *supportedView)
  102.     {
  103.  
  104.     // Record the time reference provided by the Extendible Agent.
  105.  
  106.     dwTimeZero = dwTimeZeroReference;
  107.  
  108.  
  109.     // Create an Event that will be used to communicate the occurence of traps
  110.     // to the Extendible Agent.  The Extension Agent will assert this Event
  111.     // when a trap has occured.  This is explained further later in this file.
  112.  
  113.     if ((*hPollForTrapEvent = CreateEvent(NULL, FALSE, FALSE, NULL)) == NULL)
  114.         {
  115.         // Indicate error?, be sure that NULL is returned to Extendible Agent.
  116.         }
  117.  
  118.  
  119.     // Indicate the MIB view supported by this Extension Agent, an object
  120.     // identifier representing the sub root of the MIB that is supported.
  121.  
  122.     *supportedView = MIB_OidPrefix; // NOTE!  structure copy
  123.  
  124.  
  125.     // Record the trap Event.  This example Extension Agent simulates traps by 
  126.     // generating a trap after every given number of processed requests.
  127.  
  128.     hSimulateTrap = *hPollForTrapEvent;
  129.  
  130.  
  131.     // Indicate that Extension Agent initialization was sucessfull.
  132.  
  133.     return TRUE;
  134.  
  135.     } // end SnmpExtensionInit()
  136.  
  137.  
  138. // Extension Agent DLLs provide the following entry point to communcate traps
  139. // to the Extendible Agent.  The Extendible Agent will query this entry point
  140. // when the trap Event (supplied at initialization time) is asserted, which
  141. // indicates that zero or more traps may have occured.  The Extendible Agent 
  142. // will repetedly call this entry point until FALSE is returned, indicating 
  143. // that all outstanding traps have been processed.
  144.  
  145. BOOL WINAPI SnmpExtensionTrap(
  146.     OUT AsnObjectIdentifier *enterprise,
  147.     OUT AsnInteger          *genericTrap,
  148.     OUT AsnInteger          *specificTrap,
  149.     OUT AsnTimeticks        *timeStamp,
  150.     OUT RFC1157VarBindList  *variableBindings)
  151.     {
  152.     // The body of this routine is an extremely simple example/simulation of
  153.     // the trap functionality.  A real implementation will be more complex.
  154.  
  155.  
  156.     // The following define data inserted into the trap below.  The Lan Manager
  157.     // bytesAvailAlert from the Lan Manager Alerts-2 MIB is generated with an
  158.     // empty variable bindings list.
  159.  
  160.     static UINT OidList[]  = { 1, 3, 6, 1, 4, 1, 77, 2 };
  161.     static UINT OidListLen = 8;
  162.  
  163.  
  164.     // The following variable is used for the simulation, it allows a single
  165.     // trap to be generated and then causes FALSE to be returned indicating
  166.     // no more traps.
  167.  
  168.     static whichTime = 0;
  169.  
  170.  
  171.     // The following if/else support the simulation.
  172.  
  173.     if (whichTime == 0)
  174.         {
  175.         whichTime = 1;    // Supports the simulation.
  176.  
  177.  
  178.         // Communicate the trap data to the Extendible Agent.
  179.  
  180.         enterprise->idLength = OidListLen;
  181.         enterprise->ids = OidList;
  182.  
  183.         *genericTrap      = SNMP_GENERICTRAP_ENTERSPECIFIC;
  184.  
  185.         *specificTrap     = 1;                    // the bytesAvailAlert trap
  186.  
  187.         *timeStamp        = GetCurrentTime() - dwTimeZero;
  188.  
  189.         variableBindings->list = NULL;
  190.         variableBindings->len  = 0;
  191.  
  192.  
  193.         // Indicate that valid trap data exists in the parameters.
  194.  
  195.         return TRUE;
  196.         }
  197.     else
  198.         {
  199.         whichTime = 0;    // Supports the simulation.
  200.  
  201.  
  202.         // Indicate that no more traps are available and parameters do not
  203.         // refer to any valid data.
  204.  
  205.         return FALSE;
  206.         }
  207.  
  208.     } // end SnmpExtensionTrap()
  209.  
  210.  
  211. // Extension Agent DLLs provide the following entry point to resolve queries
  212. // for MIB variables in their supported MIB view (supplied at initialization
  213. // time).  The requestType is Get/GetNext/Set.
  214.  
  215. BOOL WINAPI SnmpExtensionQuery(
  216.     IN BYTE                   requestType,
  217.     IN OUT RFC1157VarBindList *variableBindings,
  218.     OUT AsnInteger            *errorStatus,
  219.     OUT AsnInteger            *errorIndex)
  220.     {
  221.     static unsigned long requestCount = 0;  // Supports the trap simulation.
  222.     UINT    I;
  223.  
  224.  
  225.     // Iterate through the variable bindings list to resolve individual
  226.     // variable bindings.
  227.  
  228.     for ( I=0;I < variableBindings->len;I++ )
  229.         {
  230.         *errorStatus = ResolveVarBind( &variableBindings->list[I],
  231.                                        requestType );
  232.  
  233.  
  234.         // Test and handle case where Get Next past end of MIB view supported
  235.         // by this Extension Agent occurs.  Special processing is required to 
  236.         // communicate this situation to the Extendible Agent so it can take 
  237.         // appropriate action, possibly querying other Extension Agents.
  238.  
  239.         if ( *errorStatus == SNMP_ERRORSTATUS_NOSUCHNAME &&
  240.              requestType == MIB_ACTION_GETNEXT )
  241.            {
  242.            *errorStatus = SNMP_ERRORSTATUS_NOERROR;
  243.  
  244.  
  245.            // Modify variable binding of such variables so the OID points
  246.            // just outside the MIB view supported by this Extension Agent.
  247.            // The Extendible Agent tests for this, and takes appropriate
  248.            // action.
  249.  
  250.            SnmpUtilOidFree( &variableBindings->list[I].name );
  251.            SnmpUtilOidCpy( &variableBindings->list[I].name, &MIB_OidPrefix );
  252.            variableBindings->list[I].name.ids[MIB_PREFIX_LEN-1] ++;
  253.            }
  254.  
  255.  
  256.         // If an error was indicated, communicate error status and error
  257.         // index to the Extendible Agent.  The Extendible Agent will ensure
  258.         // that the origional variable bindings are returned in the response
  259.         // packet.
  260.  
  261.         if ( *errorStatus != SNMP_ERRORSTATUS_NOERROR )
  262.            {
  263.        *errorIndex = I+1;
  264.        goto Exit;
  265.        }
  266.         }
  267.  
  268. Exit:
  269.  
  270.  
  271.     // Supports the trap simulation.
  272.  
  273.     if (++requestCount % 3 == 0 && hSimulateTrap != NULL)
  274.         SetEvent(hSimulateTrap);
  275.  
  276.  
  277.     // Indicate that Extension Agent processing was sucessfull.
  278.  
  279.     return SNMPAPI_NOERROR;
  280.  
  281.     } // end SnmpExtensionQuery()
  282.  
  283.  
  284. //-------------------------------- END --------------------------------------
  285.