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.

597 lines
17 KiB

  1. /*++ BUILD Version: 0001 // Increment this if a change has global effects
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. main.c
  5. Abstract:
  6. SNMP Extension Agent for Gopher Service on Windows NT.
  7. Created:
  8. MuraliK 22-Feb-1995
  9. Revision History:
  10. --*/
  11. /************************************************************
  12. * Include Headers
  13. ************************************************************/
  14. # include "mib.h"
  15. # include "dbgutil.h"
  16. /************************************************************
  17. * Variable Definitions
  18. ************************************************************/
  19. //
  20. // Definition of the MIB objects
  21. //
  22. //
  23. // The InternetServer section of the OID tree is organized as follows:
  24. //
  25. // iso(1)
  26. // org(3)
  27. // dod(6)
  28. // internet(1)
  29. // private(4)
  30. // enterprises(1)
  31. // microsoft(311)
  32. // software(1)
  33. // InternetServer(7)
  34. // InetSrvCommon(1)
  35. // InetSrvStatistics(1)
  36. // FtpServer(2)
  37. // FtpStatistics(1)
  38. // W3Server(3)
  39. // W3Statistics(1)
  40. // GopherServer(4)
  41. // GopherStatistics(1)
  42. //
  43. static UINT sg_rguiPrefix[] = { 1, 3, 6, 1, 4, 1, 311, 1, 7, 4};
  44. static AsnObjectIdentifier sg_MibOidPrefix =
  45. OID_FROM_UINT_ARRAY( sg_rguiPrefix);
  46. # define GOPHER_PREFIX_OID_LENGTH ( GET_OID_LENGTH( sg_MibOidPrefix))
  47. # define GOPHER_STATISTICS_OID_SUFFIX ( 1)
  48. //
  49. // Following is the global description of all MIB Entries ( Mibe s) for
  50. // Gopher Service.
  51. // Definition appears as:
  52. // Mibe( MIB Entry Name, Index in MIB Block, GopherStatisticsField)
  53. //
  54. // Incidentally, MIB Entry suffix coincides with the entry name in OID Tree
  55. //
  56. // Any New MIB should be added here. and dont change use of after this defn.
  57. //
  58. # define ALL_MIB_ENTRIES() \
  59. Mibe( TotalBytesSent_HighWord, 1, TotalBytesSent.HighPart) \
  60. Mibe( TotalBytesSent_LowWord, 2, TotalBytesSent.LowPart) \
  61. Mibe( TotalBytesReceived_HighWord, 3, TotalBytesRecvd.HighPart)\
  62. Mibe( TotalBytesReceived_LowWord, 4, TotalBytesRecvd.LowPart) \
  63. Mibe( TotalFilesSent, 5, TotalFilesSent) \
  64. Mibe( TotalDirectorySent, 6, TotalDirectoryListings) \
  65. Mibe( TotalSearchesSent, 7, TotalSearches) \
  66. Mibe( CurrentAnonymousUsers, 8, CurrentAnonymousUsers) \
  67. Mibe( CurrentNonAnonymousUsers, 9, CurrentNonAnonymousUsers)\
  68. Mibe( TotalAnonymousUsers, 10, TotalAnonymousUsers) \
  69. Mibe( TotalNonAnonymousUsers, 11, TotalNonAnonymousUsers) \
  70. Mibe( MaxAnonymousUsers, 12, MaxAnonymousUsers) \
  71. Mibe( MaxNonAnonymousUsers, 13, MaxNonAnonymousUsers) \
  72. Mibe( CurrentConnections, 14, CurrentConnections) \
  73. Mibe( MaxConnections, 15, MaxConnections) \
  74. Mibe( ConnectionAttempts, 16, ConnectionAttempts) \
  75. Mibe( LogonAttempts, 17, LogonAttempts) \
  76. Mibe( AbortedAttempts, 18, AbortedAttempts) \
  77. //
  78. // Individual OID Definitions.
  79. // All Leaf variables should have a zero appended to their OID to indicate
  80. // that it is the only instance of this variable and that it exists.
  81. // Declare just the id's starting from next to the prefix given above.
  82. //
  83. //
  84. // Few Convenience Macros for MIB entries addition.
  85. //
  86. # define MIB_VAR_NAME( NameSuffix) MIB_ ## NameSuffix
  87. # define DEFINE_MIBOID( NameSuffix, uiArray) \
  88. UINT MIB_VAR_NAME( NameSuffix)[] = uiArray
  89. # define DEFINE_MIBOID_LEAF( NameSuffix, NodeNumber) \
  90. UINT MIB_VAR_NAME( NameSuffix)[] = \
  91. { GOPHER_STATISTICS_OID_SUFFIX, ( NodeNumber), 0 }
  92. //
  93. // Define all the OIDs. First define the higher level node and then leaves.
  94. //
  95. DEFINE_MIBOID( Statistics, { GOPHER_STATISTICS_OID_SUFFIX} );
  96. //
  97. // Define the Leaf OIDs.
  98. //
  99. # define Mibe( NameSuffix, Index, FieldName) \
  100. DEFINE_MIBOID_LEAF( NameSuffix, Index);
  101. //
  102. // Expand the macro ALL_MIB_ENTRIES to obtain definitions of MIB Leafs.
  103. //
  104. ALL_MIB_ENTRIES()
  105. # undef Mibe
  106. //
  107. // MIB Variable definition
  108. //
  109. //
  110. // Define Mibe() to be for variable definitions of counters.
  111. // Note that the comma is appearing before a new counter name. It is used
  112. // for structure initialization.
  113. //
  114. # define OFFSET_IN_GOPHER_STATISTICS( Field) \
  115. FIELD_OFFSET( GOPHERD_STATISTICS_INFO, Field)
  116. # define Mibe( NameSuffix, Index, Field) \
  117. , MIB_COUNTER( OID_FROM_UINT_ARRAY( MIB_VAR_NAME( NameSuffix)), \
  118. OFFSET_IN_GOPHER_STATISTICS(Field), \
  119. MibStatisticsWorker)
  120. static MIB_ENTRY sg_rgGopherMib[] = {
  121. //
  122. // Statistics
  123. //
  124. MIB_ENTRY_HEADER( OID_FROM_UINT_ARRAY( MIB_VAR_NAME( Statistics)))
  125. ALL_MIB_ENTRIES()
  126. };
  127. # undef Mibe
  128. static MIB_ENTRIES sg_GopherMibs =
  129. {
  130. &sg_MibOidPrefix,
  131. ( sizeof( sg_rgGopherMib) / sizeof( MIB_ENTRY)),
  132. sg_rgGopherMib
  133. };
  134. DECLARE_DEBUG_PRINTS_OBJECT();
  135. DECLARE_DEBUG_VARIABLE();
  136. /************************************************************
  137. * Functions
  138. ************************************************************/
  139. BOOL WINAPI
  140. DllLibMain(
  141. IN HINSTANCE hinstDll,
  142. IN DWORD fdwReason,
  143. IN LPVOID lpvContext OPTIONAL)
  144. /*++
  145. Routine Description:
  146. This function DllLibMain() is the main initialization function for
  147. Gopher MIB DLL. It initialises local variables and prepares the
  148. interface for the process to use SNMP Extension Agents for Gopher service.
  149. Messages Actions
  150. ProcessAttach Initializes winsock and data structures.
  151. It fails if winsock has not already been started.
  152. ProcessDetach Cleans up local data structures and disconnects from
  153. winsock.
  154. Arguments:
  155. hinstDll Instance Handle of the DLL
  156. fdwReason Reason why NT called this DLL
  157. lpvReserved Reserved parameter for future use.
  158. Return Value:
  159. Returns TRUE is successful; otherwise FALSE is returned.
  160. --*/
  161. {
  162. BOOL fReturn = TRUE;
  163. switch (fdwReason ) {
  164. case DLL_PROCESS_ATTACH: {
  165. //
  166. // Initialize various modules
  167. //
  168. OutputDebugString( "Initialized Gopher MIB Module\n");
  169. break;
  170. } /* case DLL_PROCESS_ATTACH */
  171. case DLL_PROCESS_DETACH: {
  172. //
  173. // Only cleanup when we are called because of a FreeLibrary().
  174. // i.e., when lpvContext == NULL
  175. // If we are called because of a process termination, dont free anything
  176. // the system will free resources and memory for us.
  177. //
  178. OutputDebugString( "Quitting the Gopher Mib DLL\n");
  179. if ( lpvContext == NULL) {
  180. //
  181. // Code to be executed on successful termination
  182. //
  183. DELETE_DEBUG_PRINT_OBJECT();
  184. }
  185. break;
  186. } /* case DLL_PROCESS_DETACH */
  187. default:
  188. break;
  189. } /* switch */
  190. return ( fReturn);
  191. } /* DllLibMain() */
  192. /************************************************************
  193. * Entry Points of SNMP Extension DLL For Gopher Service
  194. ************************************************************/
  195. //
  196. // Extension Agent DLLs need access to elapsed time agent has been active.
  197. // This is implemented by initializing the Extension Agent with a time zero
  198. // reference, and allowing the agent to compute elapsed time by subtracting
  199. // the time zero reference from the current system time. This example
  200. // Extension Agent implements this reference with dwTimeZero.
  201. //
  202. DWORD dwTimeZero = 0;
  203. BOOL
  204. SnmpExtensionInit(
  205. IN DWORD dwTimeZeroReference,
  206. OUT HANDLE * phPollForTrapEvent,
  207. OUT AsnObjectIdentifier * pAsnOidSupportedView
  208. )
  209. /*++
  210. Description:
  211. The Extension Agent DLLs provide this entry point SnmpExtensionInit()
  212. to co-ordinate the initializations of the extension agent and the
  213. extendible agent.
  214. The Extendible agent provides extension agent with a time zero reference.
  215. The Extension Agent provides Extendible agent with an Event Handle
  216. for communicating occurences of traps.
  217. The Extension Agent also provides Extendible agent with an ObjectId
  218. representing the root of the MIB structure
  219. that it (extension) supports.
  220. Arguments:
  221. dwTimeZeroReference DWORD containing the Time Zero Reference for sync.
  222. phPollForTrapEvent pointer to handle which on successful return
  223. may contain an event handle to be polled for
  224. traps.
  225. pAsnOidSupportedView pointer to ASN ( Abstract Syntax Notation OID)
  226. that contains the oid representing root of the
  227. MIB structure.
  228. Returns:
  229. TRUE on success and FALSE if there is any failure.
  230. --*/
  231. {
  232. CREATE_DEBUG_PRINT_OBJECT( "gdmib");
  233. SET_DEBUG_FLAGS( DEBUG_SNMP_RESOLVE); // enable flags if needed.
  234. IF_DEBUG( SNMP_INIT) {
  235. DBGPRINTF( ( DBG_CONTEXT,
  236. "Entering SnmpExtensionInit( %u, %08x, %08x)\n",
  237. dwTimeZeroReference,
  238. phPollForTrapEvent,
  239. pAsnOidSupportedView));
  240. }
  241. //
  242. // Record the time reference provided by the Extendible Agent.
  243. //
  244. dwTimeZero = dwTimeZeroReference;
  245. //
  246. // Indicate the MIB view supported by this Extension Agent, an object
  247. // identifier representing the sub root of the MIB that is supported.
  248. //
  249. *pAsnOidSupportedView = sg_MibOidPrefix; // NOTE! structure copy
  250. //
  251. // Though the following is a handle, dont use INVALID_HANDLE_VALUE ( -1)
  252. // because that constant is only for few people ( Win32). But all through
  253. // NT invalid handle value is NULL ( 0).
  254. //
  255. *phPollForTrapEvent = NULL;
  256. //
  257. // Indicate that Extension Agent initialization was sucessfull.
  258. //
  259. return ( TRUE);
  260. } // SnmpExtensionInit()
  261. BOOL
  262. SnmpExtensionTrap(
  263. OUT AsnObjectIdentifier * pAsnOidEnterprise,
  264. OUT AsnInteger * pAsniGenericTrap,
  265. OUT AsnInteger * pAsniSpecificTrap,
  266. OUT AsnTimeticks * pAsnTimeStamp,
  267. OUT RFC1157VarBindList * pRfcVariableBindings
  268. )
  269. /*++
  270. Description:
  271. This function is used to communicate traps to the Extendible Agent.
  272. The Extendible Agent will invoke this entry point when the trap event
  273. ( supplied at the initialization time) is asserted, which indicates
  274. that zero or more traps had occured.
  275. The Extendible agent will repeatedly query this function till this
  276. function returns FALSE.
  277. Arguments:
  278. pAsnOidEnterprise pointer to ASN OID for Enterprise, indicating
  279. original enterprise generating trap.
  280. pAsniGenericTrap pointer to ASN Integer which on return will
  281. contain the indication of the generic trap.
  282. pAsniSpecificTrap pointer to ASN Integer which on return will
  283. contain the specific trap generated.
  284. pAsnTimeStamp pointer to ASN containing the received Time-Stamp.
  285. pRfcVariableBindings pointer to RFC 1157 compliant variable bindings.
  286. Returns:
  287. TRUE if success and there are more traps to be queried.
  288. FALSE if all traps are answered and work done.
  289. --*/
  290. {
  291. IF_DEBUG( SNMP_TRAP) {
  292. DBGPRINTF( ( DBG_CONTEXT,
  293. " Entering SnmpExtensionTrap( pOidE=%08x, pGenTrap=%08x,"
  294. " pSpecificTrap = %08x, pTimeStamp=%08x, pVars=%08x)\n",
  295. pAsnOidEnterprise,
  296. pAsniGenericTrap,
  297. pAsniSpecificTrap,
  298. pAsnTimeStamp,
  299. pRfcVariableBindings));
  300. }
  301. //
  302. // We don't support traps (yet).
  303. //
  304. return ( FALSE);
  305. } // SnmpExtensionTrap()
  306. BOOL
  307. SnmpExtensionQuery(
  308. IN BYTE bRequestType,
  309. IN OUT RFC1157VarBindList * pRfcVariableBindings,
  310. OUT AsnInteger * pAsniErrorStatus,
  311. OUT AsnInteger * pAsniErrorIndex
  312. )
  313. /*++
  314. Description:
  315. This function is called by Extendible Agent to resolve the SNMP requests
  316. for queries on MIB Variables in the Extension Agent's supported MIB view.
  317. ( which was supplied at initialization time).
  318. The Request Type is GET, GETNEXT, and SET.
  319. Arguments:
  320. bRequestType byte containing the type of request.
  321. It can be one of
  322. ASN_RFC1157_GETREQUEST
  323. ASN_RFC1157_GETNEXTREQUEST
  324. ASN_RFC1157_SETREQUEST
  325. pRfcVariableBindings
  326. pointer to RFC 1157 compliant variable bindings.
  327. pAsniErrorStatus
  328. pointer to ASN Integer for Error Status
  329. pAsniErrorIndex
  330. pointer to ASN INteger giving the index for error.
  331. Returns:
  332. TRUE on success and FALSE on failure.
  333. --*/
  334. {
  335. LPGOPHERD_STATISTICS_INFO pStatistics;
  336. NET_API_STATUS Status;
  337. IF_DEBUG( SNMP_QUERY) {
  338. DBGPRINTF( ( DBG_CONTEXT,
  339. " Entering SnmpExtensionQuery( Req=0x%x, pVars=%08x,"
  340. " pAsniError=%08x, pAsniIndex=%08x).\n",
  341. bRequestType, pRfcVariableBindings,
  342. pAsniErrorStatus, pAsniErrorIndex));
  343. }
  344. //
  345. // Try to query the statistics now so we'll have a consitent
  346. // view across all variable bindings.
  347. //
  348. Status = GdQueryStatistics2(
  349. NULL, // pszServer
  350. 0,
  351. INET_INSTANCE_GLOBAL,
  352. 0,
  353. (LPBYTE* ) &pStatistics );
  354. if ( Status != NO_ERROR) {
  355. return ( SNMP_ERRORSTATUS_GENERR);
  356. }
  357. //
  358. // Status Errors not checked for here!
  359. // Reason:
  360. // If the verb is GET_NEXT beyond the block we support,
  361. // then there is no need to worry about the error at all.
  362. // If the verb is GET within the block, it will get NULL value
  363. // ( due the memset() done above).
  364. //
  365. try
  366. {
  367. //
  368. // Iterate through the variable bindings list to resolve individual
  369. // variable bindings.
  370. //
  371. RFC1157VarBind * pVarBinding;
  372. for( pVarBinding = pRfcVariableBindings->list;
  373. pVarBinding < ( pRfcVariableBindings->list +
  374. pRfcVariableBindings->len);
  375. pVarBinding++ ) {
  376. *pAsniErrorStatus = ResolveVarBinding( pVarBinding,
  377. bRequestType,
  378. pStatistics,
  379. &sg_GopherMibs);
  380. //
  381. // Test and handle case where Get Next past end of MIB view
  382. // supported by this Extension Agent occurs. Special
  383. // processing is required to communicate this situation to
  384. // the Extendible Agent so it can take appropriate action,
  385. // possibly querying other Extension Agents.
  386. //
  387. if(( *pAsniErrorStatus == SNMP_ERRORSTATUS_NOSUCHNAME ) &&
  388. ( bRequestType == MIB_GETNEXT ) ) {
  389. IF_DEBUG( SNMP_QUERY) {
  390. DBGPRINTF( ( DBG_CONTEXT,
  391. "At the End of Mib View for this Agent."
  392. "Incrementing the variable to next block.\n"));
  393. }
  394. *pAsniErrorStatus = SNMP_ERRORSTATUS_NOERROR;
  395. //
  396. // Modify variable binding of such variables so the OID
  397. // points just outside the MIB view supported by this
  398. // Extension Agent. The Extendible Agent tests for this,
  399. // and takes appropriate action.
  400. //
  401. SNMP_oidfree( &pVarBinding->name );
  402. SNMP_oidcpy( &pVarBinding->name, &sg_MibOidPrefix);
  403. pVarBinding->name.ids[ GOPHER_PREFIX_OID_LENGTH - 1]++;
  404. }
  405. //
  406. // If an error was indicated, communicate error status and error
  407. // index to the Extendible Agent. The Extendible Agent will
  408. // ensure that the origional variable bindings are returned in
  409. // the response packet.
  410. *pAsniErrorIndex =
  411. (( *pAsniErrorStatus != SNMP_ERRORSTATUS_NOERROR ) ?
  412. (( pVarBinding - pRfcVariableBindings->list) + 1) : 0);
  413. } // for
  414. } // try
  415. except( EXCEPTION_EXECUTE_HANDLER ) {
  416. //
  417. // For now do nothing.
  418. //
  419. IF_DEBUG( SNMP_QUERY) {
  420. DBGPRINTF( ( DBG_CONTEXT,
  421. "Exception occured while resolving "
  422. " SnmpExtensionQuery()"));
  423. }
  424. }
  425. IF_DEBUG( SNMP_QUERY) {
  426. DBGPRINTF( ( DBG_CONTEXT,
  427. "Returning from GophMib::Query() with "
  428. " Error Status = %u. Error Index = %u.\n",
  429. *pAsniErrorStatus,
  430. *pAsniErrorIndex));
  431. }
  432. return ( SNMPAPI_NOERROR);
  433. } // SnmpExtensionQuery()