Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
9.8 KiB

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