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.

417 lines
13 KiB

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