Source code of Windows XP (NT5)
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.

363 lines
11 KiB

  1. /*
  2. * gendll.c v0.11 March 14, 1996
  3. *
  4. ****************************************************************************
  5. * *
  6. * (C) Copyright 1995, 1996 DIGITAL EQUIPMENT CORPORATION *
  7. * *
  8. * This software is an unpublished work protected under the *
  9. * the copyright laws of the United States of America, all *
  10. * rights reserved. *
  11. * *
  12. * In the event this software is licensed for use by the United *
  13. * States Government, all use, duplication or disclosure by the *
  14. * United States Government is subject to restrictions as set *
  15. * forth in either subparagraph (c)(1)(ii) of the Rights in *
  16. * Technical Data And Computer Software Clause at DFARS *
  17. * 252.227-7013, or the Commercial Computer Software Restricted *
  18. * Rights Clause at FAR 52.221-19, whichever is applicable. *
  19. * *
  20. ****************************************************************************
  21. *
  22. * Facility:
  23. *
  24. * SNMP Extension Agent
  25. *
  26. * Abstract:
  27. *
  28. * This module contains the code for plugging into the Windows NT Extendible
  29. * Agent. It contains the dll's main routine and the three exported SNMP
  30. * routines.
  31. *
  32. * Functions:
  33. *
  34. * DllMain()
  35. * SnmpExtensionInit()
  36. * SnmpExtensionTrap()
  37. * SnmpExtensionQuery()
  38. *
  39. * Author:
  40. * Miriam Amos Nihart, Kathy Faust
  41. *
  42. * Date:
  43. * 2/17/95
  44. *
  45. * Revision History:
  46. * 6/26/95 KKF, register using Subroot_oid
  47. * 7/31/95 ags readded above code + uncommented calls to SNMP_oidfree
  48. * 3/14/96 kkf, modified SNMPExtensionTrap
  49. */
  50. // General notes:
  51. //
  52. // Microsoft's Extendible Agent for Windows NT is implemented by dynamically
  53. // linking to Extension Agent DLLs that implement portions of the MIB. These
  54. // Extension Agents are configured in the Windows NT Registration Database.
  55. // When the Extendible Agent Service is started, it queries the registry to
  56. // determine which Extension Agent DLLs have been installed and need to be
  57. // loaded and initialized. The Extendible Agent invokes various DLL entry
  58. // points (examples follow in this file) to request MIB queries and obtain
  59. // Extension Agent generated traps.
  60. // Necessary includes.
  61. #include <windows.h>
  62. #include <malloc.h>
  63. #include <stdio.h>
  64. #include <snmp.h>
  65. //
  66. // The file mib.h is a user supplied header file (could be from a code
  67. // generator). This file should contain the definition, macros, forward
  68. // declarations, etc. The file mib_xtrn.h contains the external
  69. // declarations for the variable tables and classes composing this MIB.
  70. //
  71. #include "mib.h"
  72. #include "mib_xtrn.h"
  73. // Extension Agent DLLs need access to the elapsed time that the agent has
  74. // been active. This is implemented by initializing the Extension Agent
  75. // with a time zero reference, and allowing the agent to compute elapsed
  76. // time by subtracting the time zero reference from the current system time.
  77. // This example Extension Agent implements this reference with dwTimeZero.
  78. DWORD dwTimeZero = 0 ;
  79. extern DWORD dwTimeZero ;
  80. //
  81. // Trap Queue
  82. //
  83. q_hdr_t trap_q = { NULL, NULL } ;
  84. extern q_hdr_t trap_q ; // make it global
  85. HANDLE hTrapQMutex ;
  86. extern HANDLE hTrapQMutex ;
  87. HANDLE hEnabledTraps ;
  88. extern HANDLE hEnabledTraps ;
  89. /*
  90. * DllMain
  91. *
  92. * This is a standard Win32 DLL entry point. See the Win32 SDK for more
  93. * information on its arguments and their meanings. This example DLL does
  94. * not perform any special actions using this mechanism.
  95. *
  96. * Arguments:
  97. *
  98. * Results:
  99. *
  100. * Side Effects:
  101. *
  102. */
  103. BOOL WINAPI
  104. DllMain( HANDLE hDll ,
  105. DWORD dwReason ,
  106. LPVOID lpReserved )
  107. {
  108. switch( dwReason )
  109. {
  110. case DLL_PROCESS_ATTACH :
  111. case DLL_PROCESS_DETACH :
  112. case DLL_THREAD_ATTACH :
  113. case DLL_THREAD_DETACH :
  114. default :
  115. break ;
  116. }
  117. return TRUE ;
  118. } /* end of DllMain() (the DllEntryPoint) */
  119. /*
  120. * SnmpExtensionInit
  121. *
  122. * Extension Agent DLLs provide the following entry point to coordinate the
  123. * initializations of the Extension Agent and the Extendible Agent. The
  124. * Extendible Agent provides the Extension Agent with a time zero reference;
  125. * and the Extension Agent provides the Extendible Agent with an Event
  126. * handle for communicating occurence of traps, and an object identifier
  127. * representing the root of the MIB subtree that the Extension Agent
  128. * supports.
  129. *
  130. * Traps support is determined by the user in the UserMibInit() routine.
  131. * If a valid handle from CreateEvent() is returned in the argument
  132. * hPollForTrapEvent, then traps have been implemented and the SNMP
  133. * Extendible Agent will poll this Extension Agent to retrieve the traps
  134. * upon notification through the SetEvent() routine. Polling is done
  135. * through the SnmpExtensionTrap() routine. If NULL is returned in the
  136. * argument hPollForTrapEvent, there are no traps.
  137. *
  138. * Arguments:
  139. *
  140. * Results:
  141. *
  142. */
  143. BOOL WINAPI
  144. SnmpExtensionInit( IN DWORD dwTimeZeroReference ,
  145. OUT HANDLE *hPollForTrapEvent ,
  146. OUT AsnObjectIdentifier *supportedView )
  147. {
  148. // Record the time reference provided by the Extendible Agent.
  149. dwTimeZero = dwTimeZeroReference ;
  150. // Indicate the MIB view supported by this Extension Agent, an object
  151. // identifier representing the sub root of the MIB that is supported.
  152. *supportedView = Subroot_oid ; // NOTE! structure copy
  153. // *supportedView = *(class_info[ 0 ].oid) ; // NOTE! structure copy
  154. // Call the User's initialization routine
  155. if ( !UserMibInit( hPollForTrapEvent ) )
  156. return FALSE ;
  157. // Indicate that Extension Agent initialization was successful.
  158. return TRUE ;
  159. } /* end of SnmpExtensionInit() */
  160. /*
  161. * SnmpExtensionTrap
  162. *
  163. * Extension Agent DLLs provide the following entry point to communicate
  164. * traps to the Extendible Agent. The Extendible Agent will query this
  165. * entry point when the trap Event (supplied at initialization time) is
  166. * asserted, which indicates that zero or more traps may have occured.
  167. * The Extendible Agent will repetedly call this entry point until FALSE
  168. * is returned, indicating that all outstanding traps have been processed.
  169. *
  170. * Arguments:
  171. *
  172. * Results:
  173. *
  174. |=========================================================================
  175. | There are no Traps associated with the HostMIB. Consequently this
  176. | routine is taken over and used to refresh the cached information
  177. | associated with SNMP attribute "hrProcessorLoad" (in "HRPROCES.C") thru
  178. | a call to function "hrProcessLoad_Refresh()" (also in "HRPROCES.C").
  179. |
  180. | This function is being entered because a timer has expired that was
  181. | initialized by code in "TrapInit()" (in "GENNT.C"). The timer automatically
  182. | resets itself.
  183. |
  184. | All the standard generated code is subsumed by a simple call to
  185. | "hrProcessLoad_Refresh()".
  186. */
  187. BOOL WINAPI
  188. SnmpExtensionTrap( OUT AsnObjectIdentifier *enterprise ,
  189. OUT AsnInteger *genericTrap ,
  190. OUT AsnInteger *specificTrap ,
  191. OUT AsnTimeticks *timeStamp ,
  192. OUT RFC1157VarBindList *variableBindings )
  193. {
  194. #if 0
  195. tcb_t *entry ;
  196. // Traps are process by processing the traps on the trap queue.
  197. // The Extendible Agent will call this routine upon the receipt of
  198. // the event on the handle passed back in the SnmpExtensionInit routine.
  199. // The Extendible Agent calls this return back as long as this routine
  200. // returns true.
  201. // acquire mutex for trap_q
  202. WaitForSingleObject( hTrapQMutex, INFINITE ) ;
  203. // Dequeue a trap entry
  204. QUEUE_REMOVE( trap_q, entry ) ;
  205. // release the mutex for trap_q
  206. ReleaseMutex( hTrapQMutex ) ;
  207. if (entry == NULL)
  208. return FALSE ;
  209. *enterprise = entry->enterprise ; // note structure copy
  210. *genericTrap = entry->genericTrap ;
  211. *specificTrap = entry->specificTrap ;
  212. *timeStamp = entry->timeStamp ;
  213. *variableBindings = entry->varBindList ; // note structure copy
  214. free(entry) ;
  215. return TRUE ;
  216. #endif
  217. /*
  218. |========================
  219. | Special HostMIB code:
  220. */
  221. /* Re-fetch CPU statistics from kernel */
  222. hrProcessLoad_Refresh();
  223. /* Don't call again until the timer goes off again */
  224. return FALSE;
  225. } /* end of SnmpExtensionTrap() */
  226. /*
  227. * SnmpExtensionQuery
  228. *
  229. * Extension Agent DLLs provide the following entry point to resolve queries
  230. * for MIB variables in their supported MIB view (supplied at initialization
  231. * time). The requestType is Get/GetNext/Set.
  232. *
  233. * Arguments:
  234. *
  235. * Results:
  236. *
  237. */
  238. BOOL WINAPI
  239. SnmpExtensionQuery( IN BYTE requestType ,
  240. IN OUT RFC1157VarBindList *variableBindings ,
  241. OUT AsnInteger *errorStatus ,
  242. OUT AsnInteger *errorIndex )
  243. {
  244. UINT index ;
  245. UINT *tmp ;
  246. UINT status ;
  247. // Iterate through the variable bindings list to resolve individual
  248. // variable bindings.
  249. for ( index = 0 ; index < variableBindings->len ; index++ )
  250. {
  251. *errorStatus = ResolveVarBind( &variableBindings->list[ index ] ,
  252. requestType ) ;
  253. // Test and handle case where Get Next past end of MIB view supported
  254. // by this Extension Agent occurs. Special processing is required to
  255. // communicate this situation to the Extendible Agent so it can take
  256. // appropriate action, possibly querying other Extension Agents.
  257. if ( *errorStatus == SNMP_ERRORSTATUS_NOSUCHNAME &&
  258. requestType == MIB_ACTION_GETNEXT )
  259. {
  260. *errorStatus = SNMP_ERRORSTATUS_NOERROR ;
  261. // Modify variable binding of such variables so the OID points
  262. // just outside the MIB view supported by this Extension Agent.
  263. // The Extendible Agent tests for this, and takes appropriate
  264. // action.
  265. SNMP_oidfree( &variableBindings->list[ index ].name ) ;
  266. status = SNMP_oidcpy( &variableBindings->list[ index ].name, &Subroot_oid ) ;
  267. if ( !status )
  268. {
  269. *errorStatus = SNMP_ERRORSTATUS_GENERR;
  270. }
  271. else
  272. {
  273. tmp = variableBindings->list[ index ].name.ids ;
  274. (tmp[ SUBROOT_LENGTH - 1 ])++ ;
  275. }
  276. }
  277. // If an error was indicated, communicate error status and error
  278. // index to the Extendible Agent. The Extendible Agent will ensure
  279. // that the origional variable bindings are returned in the response
  280. // packet.
  281. if ( *errorStatus != SNMP_ERRORSTATUS_NOERROR )
  282. {
  283. *errorIndex = index + 1 ;
  284. goto Exit ;
  285. }
  286. }
  287. Exit:
  288. // Indicate that Extension Agent processing was sucessfull.
  289. return SNMPAPI_NOERROR ;
  290. } /* end of SnmpExtensionQuery() */
  291. /* end of gendll.c */