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.

365 lines
12 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( HINSTANCE hInstDLL,
  105. DWORD dwReason ,
  106. LPVOID lpReserved )
  107. {
  108. switch( dwReason )
  109. {
  110. case DLL_PROCESS_ATTACH :
  111. DisableThreadLibraryCalls( hInstDLL );
  112. break;
  113. case DLL_PROCESS_DETACH :
  114. case DLL_THREAD_ATTACH :
  115. case DLL_THREAD_DETACH :
  116. default :
  117. break ;
  118. }
  119. return TRUE ;
  120. } /* end of DllMain() (the DllEntryPoint) */
  121. /*
  122. * SnmpExtensionInit
  123. *
  124. * Extension Agent DLLs provide the following entry point to coordinate the
  125. * initializations of the Extension Agent and the Extendible Agent. The
  126. * Extendible Agent provides the Extension Agent with a time zero reference;
  127. * and the Extension Agent provides the Extendible Agent with an Event
  128. * handle for communicating occurence of traps, and an object identifier
  129. * representing the root of the MIB subtree that the Extension Agent
  130. * supports.
  131. *
  132. * Traps support is determined by the user in the UserMibInit() routine.
  133. * If a valid handle from CreateEvent() is returned in the argument
  134. * hPollForTrapEvent, then traps have been implemented and the SNMP
  135. * Extendible Agent will poll this Extension Agent to retrieve the traps
  136. * upon notification through the SetEvent() routine. Polling is done
  137. * through the SnmpExtensionTrap() routine. If NULL is returned in the
  138. * argument hPollForTrapEvent, there are no traps.
  139. *
  140. * Arguments:
  141. *
  142. * Results:
  143. *
  144. */
  145. BOOL WINAPI
  146. SnmpExtensionInit( IN DWORD dwTimeZeroReference ,
  147. OUT HANDLE *hPollForTrapEvent ,
  148. OUT AsnObjectIdentifier *supportedView )
  149. {
  150. // Record the time reference provided by the Extendible Agent.
  151. dwTimeZero = dwTimeZeroReference ;
  152. // Indicate the MIB view supported by this Extension Agent, an object
  153. // identifier representing the sub root of the MIB that is supported.
  154. *supportedView = Subroot_oid ; // NOTE! structure copy
  155. // *supportedView = *(class_info[ 0 ].oid) ; // NOTE! structure copy
  156. // Call the User's initialization routine
  157. if ( !UserMibInit( hPollForTrapEvent ) )
  158. return FALSE ;
  159. // Indicate that Extension Agent initialization was successful.
  160. return TRUE ;
  161. } /* end of SnmpExtensionInit() */
  162. /*
  163. * SnmpExtensionTrap
  164. *
  165. * Extension Agent DLLs provide the following entry point to communicate
  166. * traps to the Extendible Agent. The Extendible Agent will query this
  167. * entry point when the trap Event (supplied at initialization time) is
  168. * asserted, which indicates that zero or more traps may have occured.
  169. * The Extendible Agent will repetedly call this entry point until FALSE
  170. * is returned, indicating that all outstanding traps have been processed.
  171. *
  172. * Arguments:
  173. *
  174. * Results:
  175. *
  176. |=========================================================================
  177. | There are no Traps associated with the HostMIB. Consequently this
  178. | routine is taken over and used to refresh the cached information
  179. | associated with SNMP attribute "hrProcessorLoad" (in "HRPROCES.C") thru
  180. | a call to function "hrProcessLoad_Refresh()" (also in "HRPROCES.C").
  181. |
  182. | This function is being entered because a timer has expired that was
  183. | initialized by code in "TrapInit()" (in "GENNT.C"). The timer automatically
  184. | resets itself.
  185. |
  186. | All the standard generated code is subsumed by a simple call to
  187. | "hrProcessLoad_Refresh()".
  188. */
  189. BOOL WINAPI
  190. SnmpExtensionTrap( OUT AsnObjectIdentifier *enterprise ,
  191. OUT AsnInteger *genericTrap ,
  192. OUT AsnInteger *specificTrap ,
  193. OUT AsnTimeticks *timeStamp ,
  194. OUT RFC1157VarBindList *variableBindings )
  195. {
  196. #if 0
  197. tcb_t *entry ;
  198. // Traps are process by processing the traps on the trap queue.
  199. // The Extendible Agent will call this routine upon the receipt of
  200. // the event on the handle passed back in the SnmpExtensionInit routine.
  201. // The Extendible Agent calls this return back as long as this routine
  202. // returns true.
  203. // acquire mutex for trap_q
  204. WaitForSingleObject( hTrapQMutex, INFINITE ) ;
  205. // Dequeue a trap entry
  206. QUEUE_REMOVE( trap_q, entry ) ;
  207. // release the mutex for trap_q
  208. ReleaseMutex( hTrapQMutex ) ;
  209. if (entry == NULL)
  210. return FALSE ;
  211. *enterprise = entry->enterprise ; // note structure copy
  212. *genericTrap = entry->genericTrap ;
  213. *specificTrap = entry->specificTrap ;
  214. *timeStamp = entry->timeStamp ;
  215. *variableBindings = entry->varBindList ; // note structure copy
  216. free(entry) ;
  217. return TRUE ;
  218. #endif
  219. /*
  220. |========================
  221. | Special HostMIB code:
  222. */
  223. /* Re-fetch CPU statistics from kernel */
  224. hrProcessLoad_Refresh();
  225. /* Don't call again until the timer goes off again */
  226. return FALSE;
  227. } /* end of SnmpExtensionTrap() */
  228. /*
  229. * SnmpExtensionQuery
  230. *
  231. * Extension Agent DLLs provide the following entry point to resolve queries
  232. * for MIB variables in their supported MIB view (supplied at initialization
  233. * time). The requestType is Get/GetNext/Set.
  234. *
  235. * Arguments:
  236. *
  237. * Results:
  238. *
  239. */
  240. BOOL WINAPI
  241. SnmpExtensionQuery( IN BYTE requestType ,
  242. IN OUT RFC1157VarBindList *variableBindings ,
  243. OUT AsnInteger *errorStatus ,
  244. OUT AsnInteger *errorIndex )
  245. {
  246. UINT index ;
  247. UINT *tmp ;
  248. UINT status ;
  249. // Iterate through the variable bindings list to resolve individual
  250. // variable bindings.
  251. for ( index = 0 ; index < variableBindings->len ; index++ )
  252. {
  253. *errorStatus = ResolveVarBind( &variableBindings->list[ index ] ,
  254. requestType ) ;
  255. // Test and handle case where Get Next past end of MIB view supported
  256. // by this Extension Agent occurs. Special processing is required to
  257. // communicate this situation to the Extendible Agent so it can take
  258. // appropriate action, possibly querying other Extension Agents.
  259. if ( *errorStatus == SNMP_ERRORSTATUS_NOSUCHNAME &&
  260. requestType == MIB_ACTION_GETNEXT )
  261. {
  262. *errorStatus = SNMP_ERRORSTATUS_NOERROR ;
  263. // Modify variable binding of such variables so the OID points
  264. // just outside the MIB view supported by this Extension Agent.
  265. // The Extendible Agent tests for this, and takes appropriate
  266. // action.
  267. SNMP_oidfree( &variableBindings->list[ index ].name ) ;
  268. status = SNMP_oidcpy( &variableBindings->list[ index ].name, &Subroot_oid ) ;
  269. if ( !status )
  270. {
  271. *errorStatus = SNMP_ERRORSTATUS_GENERR;
  272. }
  273. else
  274. {
  275. tmp = variableBindings->list[ index ].name.ids ;
  276. (tmp[ SUBROOT_LENGTH - 1 ])++ ;
  277. }
  278. }
  279. // If an error was indicated, communicate error status and error
  280. // index to the Extendible Agent. The Extendible Agent will ensure
  281. // that the origional variable bindings are returned in the response
  282. // packet.
  283. if ( *errorStatus != SNMP_ERRORSTATUS_NOERROR )
  284. {
  285. *errorIndex = index + 1 ;
  286. goto Exit ;
  287. }
  288. }
  289. Exit:
  290. // Indicate that Extension Agent processing was sucessfull.
  291. return SNMPAPI_NOERROR ;
  292. } /* end of SnmpExtensionQuery() */
  293. /* end of gendll.c */