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.

546 lines
17 KiB

  1. /*
  2. * HrNetworkEntry.c v0.10
  3. * Generated in conjunction with Management Factory scripts:
  4. * script version: SNMPv1, 0.16, Apr 25, 1996
  5. * project: D:\TEMP\EXAMPLE\HOSTMIB
  6. ****************************************************************************
  7. * *
  8. * (C) Copyright 1995 DIGITAL EQUIPMENT CORPORATION *
  9. * *
  10. * This software is an unpublished work protected under the *
  11. * the copyright laws of the United States of America, all *
  12. * rights reserved. *
  13. * *
  14. * In the event this software is licensed for use by the United *
  15. * States Government, all use, duplication or disclosure by the *
  16. * United States Government is subject to restrictions as set *
  17. * forth in either subparagraph (c)(1)(ii) of the Rights in *
  18. * Technical Data And Computer Software Clause at DFARS *
  19. * 252.227-7013, or the Commercial Computer Software Restricted *
  20. * Rights Clause at FAR 52.221-19, whichever is applicable. *
  21. * *
  22. ****************************************************************************
  23. *
  24. * Facility:
  25. *
  26. * Windows NT SNMP Extension Agent
  27. *
  28. * Abstract:
  29. *
  30. * This module contains the code for dealing with the get, set, and
  31. * instance name routines for the HrNetworkEntry. Actual instrumentation code is
  32. * supplied by the developer.
  33. *
  34. * Functions:
  35. *
  36. * A get and set routine for each attribute in the class.
  37. *
  38. * The routines for instances within the class.
  39. *
  40. * Author:
  41. *
  42. * D. D. Burns @ Webenable Inc
  43. *
  44. * Revision History:
  45. *
  46. * V1.00 - 04/28/97 D. D. Burns Genned: Thu Nov 07 16:42:33 1996
  47. *
  48. */
  49. #include <nt.h>
  50. #include <windef.h>
  51. #include <ntrtl.h>
  52. #include <nturtl.h>
  53. #include <windows.h>
  54. #include <malloc.h>
  55. #include <snmp.h>
  56. #include "mib.h"
  57. #include "smint.h"
  58. #include "hostmsmi.h"
  59. #include "user.h" /* Developer supplied include file */
  60. #include "HMCACHE.H" /* Cache-related definitions */
  61. #include "HRDEVENT.H" /* HrDevice Table-related definitions */
  62. #include "iphlpapi.h" /* Access to MIB2 Utility function */
  63. /*
  64. * GetHrNetworkIfIndex
  65. * The value of the ifIndex which corresponds to this network device.
  66. *
  67. * Gets the value for HrNetworkIfIndex.
  68. *
  69. * Arguments:
  70. *
  71. * outvalue address to return variable value
  72. * accesss Reserved for future security use
  73. * instance address of instance name as ordered native
  74. * data type(s)
  75. *
  76. * Return Codes:
  77. *
  78. * Standard PDU error codes.
  79. *
  80. * SNMP_ERRORSTATUS_NOERROR Successful get
  81. * SNMP_ERRORSTATUS_GENERR Catch-all failure code
  82. * mibtget.c v0.10
  83. *
  84. | =============== From WebEnable Design Spec Rev 3 04/11/97==================
  85. | hrNetworkIfIndex
  86. |
  87. | ACCESS SYNTAX
  88. | read-only INTEGER
  89. |
  90. | "The value of ifIndex which corresponds to this network device."
  91. |
  92. | DISCUSSION:
  93. |
  94. | <POA-13> A mechanism by which I can map a network interface device (as found
  95. | in the course of populating the hrDeviceTable) to the "ifIndex" value in
  96. | MIB-II needs to be described to me.
  97. |
  98. | RESOLVED >>>>>>>>
  99. | <POA-13> We expose this info via MIB2UTIL.DLL.
  100. | RESOLVED >>>>>>>>
  101. |
  102. |============================================================================
  103. | 1.3.6.1.2.1.25.3.4.1.1.<instance>
  104. | | | | |
  105. | | | | *-HrNetworkIfIndex
  106. | | | *-HrNetworkEntry
  107. | | *-HrNetworkTable
  108. | *-hrDevice
  109. */
  110. UINT
  111. GetHrNetworkIfIndex(
  112. OUT Integer *outvalue ,
  113. IN Access_Credential *access ,
  114. IN InstanceName *instance )
  115. {
  116. ULONG index; /* As fetched from instance structure */
  117. CACHEROW *row; /* Row entry fetched from cache */
  118. /*
  119. | Grab the instance information
  120. */
  121. index = GET_INSTANCE(0);
  122. /*
  123. | Use it to find the right entry in the cache
  124. */
  125. if ((row = FindTableRow(index, &hrDevice_cache)) == NULL) {
  126. return SNMP_ERRORSTATUS_GENERR;
  127. }
  128. /*
  129. | By convention with "Gen_HrNetwork_Cache()", the "Hidden Context" attribute
  130. | for the selected row is the value to be returned by hrNetworkIfIndex.
  131. */
  132. *outvalue = row->attrib_list[HIDDEN_CTX].u.unumber_value;
  133. return SNMP_ERRORSTATUS_NOERROR ;
  134. } /* end of GetHrNetworkIfIndex() */
  135. /*
  136. * HrNetworkEntryFindInstance
  137. *
  138. * This routine is used to verify that the specified instance is
  139. * valid.
  140. *
  141. * Arguments:
  142. *
  143. * FullOid Address for the full oid - group, variable,
  144. * and instance information
  145. * instance Address for instance specification as an oid
  146. *
  147. * Return Codes:
  148. *
  149. * SNMP_ERRORSTATUS_NOERROR Instance found and valid
  150. * SNMP_ERRORSTATUS_NOSUCHNAME Invalid instance
  151. *
  152. */
  153. UINT
  154. HrNetworkEntryFindInstance( IN ObjectIdentifier *FullOid ,
  155. IN OUT ObjectIdentifier *instance )
  156. {
  157. UINT tmp_instance; /* Instance arc value */
  158. CACHEROW *row; /* Row entry fetched from cache */
  159. //
  160. // Developer instrumentation code to find appropriate instance goes here.
  161. // For non-tables, it is not necessary to modify this routine. However, if
  162. // there is any context that needs to be set, it can be done here.
  163. //
  164. if ( FullOid->idLength <= HRNETWORKENTRY_VAR_INDEX )
  165. // No instance was specified
  166. return SNMP_ERRORSTATUS_NOSUCHNAME ;
  167. else if ( FullOid->idLength != HRNETWORKENTRY_VAR_INDEX + 1 )
  168. // Instance length is more than 1
  169. return SNMP_ERRORSTATUS_NOSUCHNAME ;
  170. else
  171. // The only valid instance for a non-table are instance 0. If this
  172. // is a non-table, the following code validates the instances. If this
  173. // is a table, developer modification is necessary below.
  174. tmp_instance = FullOid->ids[ HRNETWORKENTRY_VAR_INDEX ] ;
  175. /*
  176. | For hrNetworkTable, the instance arc(s) is a single arc, and it must
  177. | correctly select an entry in the hrDeviceTable cache.
  178. |
  179. | Check that here.
  180. */
  181. if ( (row = FindTableRow(tmp_instance, &hrDevice_cache)) == NULL ) {
  182. return SNMP_ERRORSTATUS_NOSUCHNAME ;
  183. }
  184. else
  185. {
  186. /*
  187. | The instance arc selects an hrDeviceTable row entry, but is that
  188. | entry actually for a device of type "Network"?
  189. |
  190. | (We examine the last arc of the OID that specifies the device
  191. | type in the row entry selected by the instance arc).
  192. */
  193. if (row->attrib_list[HRDV_TYPE].u.unumber_value !=
  194. HRDV_TYPE_LASTARC_NETWORK) {
  195. return SNMP_ERRORSTATUS_NOSUCHNAME;
  196. }
  197. // the instance is valid. Create the instance portion of the OID
  198. // to be returned from this call.
  199. instance->ids[ 0 ] = tmp_instance ;
  200. instance->idLength = 1 ;
  201. }
  202. return SNMP_ERRORSTATUS_NOERROR ;
  203. } /* end of HrNetworkEntryFindInstance() */
  204. /*
  205. * HrNetworkEntryFindNextInstance
  206. *
  207. * This routine is called to get the next instance. If no instance
  208. * was passed than return the first instance (1).
  209. *
  210. * Arguments:
  211. *
  212. * FullOid Address for the full oid - group, variable,
  213. * and instance information
  214. * instance Address for instance specification as an oid
  215. *
  216. * Return Codes:
  217. *
  218. * SNMP_ERRORSTATUS_NOERROR Instance found and valid
  219. * SNMP_ERRORSTATUS_NOSUCHNAME Invalid instance
  220. *
  221. */
  222. UINT
  223. HrNetworkEntryFindNextInstance( IN ObjectIdentifier *FullOid ,
  224. IN OUT ObjectIdentifier *instance )
  225. {
  226. //
  227. // Developer supplied code to find the next instance of class goes here.
  228. // If this is a class with cardinality 1, no modification of this routine
  229. // is necessary unless additional context needs to be set.
  230. // If the FullOid does not specify an instance, then the only instance
  231. // of the class is returned. If this is a table, the first row of the
  232. // table is returned.
  233. //
  234. // If an instance is specified and this is a non-table class, then NOSUCHNAME
  235. // is returned so that correct MIB rollover processing occurs. If this is
  236. // a table, then the next instance is the one following the current instance.
  237. // If there are no more instances in the table, return NOSUCHNAME.
  238. //
  239. CACHEROW *row;
  240. ULONG tmp_instance;
  241. if ( FullOid->idLength <= HRNETWORKENTRY_VAR_INDEX )
  242. {
  243. /*
  244. | Too short: must return the instance arc that selects the first
  245. | entry in the table if there is one.
  246. */
  247. tmp_instance = 0;
  248. }
  249. else {
  250. /*
  251. | There is at least one instance arc. Even if it is the only arc
  252. | we use it as the "index" in a request for the "NEXT" one.
  253. */
  254. tmp_instance = FullOid->ids[ HRNETWORKENTRY_VAR_INDEX ] ;
  255. }
  256. /* Now go off and try to find the next instance in the table */
  257. if ((row = FindNextTableRow(tmp_instance, &hrDevice_cache)) == NULL) {
  258. return SNMP_ERRORSTATUS_NOSUCHNAME ;
  259. }
  260. /*
  261. | The instance arc selects an hrDeviceTable row entry, but is that
  262. | entry actually for a device of type "Network"?
  263. |
  264. | (We examine the last arc of the OID that specifies the device
  265. | type in the row entry selected by the instance arc).
  266. */
  267. do {
  268. if (row->attrib_list[HRDV_TYPE].u.unumber_value ==
  269. HRDV_TYPE_LASTARC_NETWORK) {
  270. /* Found an hrDeviceTable entry for the right device type */
  271. break;
  272. }
  273. /* Step to the next row in the table */
  274. row = GetNextTableRow( row );
  275. }
  276. while ( row != NULL );
  277. /* If we found a proper device-type row . . . */
  278. if ( row != NULL) {
  279. instance->ids[ 0 ] = row->index ;
  280. instance->idLength = 1 ;
  281. }
  282. else {
  283. /*
  284. | Fell off the end of the hrDeviceTable without finding a row
  285. | entry that had the right device type.
  286. */
  287. return SNMP_ERRORSTATUS_NOSUCHNAME ;
  288. }
  289. return SNMP_ERRORSTATUS_NOERROR ;
  290. } /* end of HrNetworkEntryFindNextInstance() */
  291. /*
  292. * HrNetworkEntryConvertInstance
  293. *
  294. * This routine is used to convert the object id specification of an
  295. * instance into an ordered native representation. The object id format
  296. * is that object identifier that is returned from the Find Instance
  297. * or Find Next Instance routines. It is NOT the full object identifier
  298. * that contains the group and variable object ids as well. The native
  299. * representation is an argc/argv-like structure that contains the
  300. * ordered variables that define the instance. This is specified by
  301. * the MIB's INDEX clause. See RFC 1212 for information about the INDEX
  302. * clause.
  303. *
  304. *
  305. * Arguments:
  306. *
  307. * oid_spec Address of the object id instance specification
  308. * native_spec Address to return the ordered native instance
  309. * specification
  310. *
  311. * Return Codes:
  312. *
  313. * SUCCESS Conversion complete successfully
  314. * FAILURE Unable to convert object id into native format
  315. *
  316. */
  317. UINT
  318. HrNetworkEntryConvertInstance( IN ObjectIdentifier *oid_spec ,
  319. IN OUT InstanceName *native_spec )
  320. {
  321. static char *array; /* The address of this (char *) is passed back */
  322. /* as though it were an array of length 1 of these */
  323. /* types. */
  324. static ULONG inst; /* The address of this ULONG is passed back */
  325. /* (Obviously, no "free()" action is needed) */
  326. /* We only expect the one arc in "oid_spec" */
  327. inst = oid_spec->ids[0];
  328. array = (char *) &inst;
  329. native_spec->count = 1;
  330. native_spec->array = &array;
  331. return SUCCESS ;
  332. } /* end of HrNetworkEntryConvertInstance() */
  333. /*
  334. * HrNetworkEntryFreeInstance
  335. *
  336. * This routine is used to free an ordered native representation of an
  337. * instance name.
  338. *
  339. * Arguments:
  340. *
  341. * instance Address to return the ordered native instance
  342. * specification
  343. *
  344. * Return Codes:
  345. *
  346. *
  347. */
  348. void
  349. HrNetworkEntryFreeInstance( IN OUT InstanceName *instance )
  350. {
  351. /* No action needed for hrNetwork Table */
  352. } /* end of HrNetworkEntryFreeInstance() */
  353. /*
  354. | End of Generated Code
  355. */
  356. /* Gen_HrNetwork_Cache - Gen. a initial cache for HrDevice PROCESSOR Table */
  357. /* Gen_HrNetwork_Cache - Gen. a initial cache for HrDevice PROCESSOR Table */
  358. /* Gen_HrNetwork_Cache - Gen. a initial cache for HrDevice PROCESSOR Table */
  359. BOOL
  360. Gen_HrNetwork_Cache(
  361. ULONG type_arc
  362. )
  363. /*
  364. | EXPLICIT INPUTS:
  365. |
  366. | "type_arc" is the number "n" to be used as the last arc in the
  367. | device-type OID:
  368. |
  369. | 1.3.6.1.2.1.25.3.1.n
  370. | | | |
  371. | | | * Identifying arc for type
  372. | | *-hrDeviceTypes (OIDs specifying device types)
  373. | *-hrDevice
  374. |
  375. | for devices created by this cache-population routine.
  376. |
  377. | IMPLICIT INPUTS:
  378. |
  379. | None.
  380. |
  381. | OUTPUTS:
  382. |
  383. | On Success:
  384. | Function returns TRUE indicating that the HrDevice cache has been fully
  385. | populated with all rows required for Network devices.
  386. |
  387. | On any Failure:
  388. | Function returns FALSE (indicating "not enough storage" or other
  389. | internal logic error).
  390. |
  391. | THE BIG PICTURE:
  392. |
  393. | At subagent startup time, the cache for each table in the MIB is
  394. | populated with rows for each row in the table. This function is
  395. | invoked by the start-up code in "Gen_HrDevice_Cache()" to
  396. | populate the cache for the HrDevice table with Network-device
  397. | specific entries.
  398. |
  399. | OTHER THINGS TO KNOW:
  400. |
  401. | Since all the attributes in the HrNetwork "sub" table are computed
  402. | upon request (based on cached information in a selected row in the
  403. | HrDevice table) there is no need to build a cache specifically for
  404. | this sub-table. (This routine is only loading the HrDevice cache.
  405. | --------
  406. |
  407. | This function holds a convention with the GET routines earlier in
  408. | this module that the "HIDDEN_CTX" attribute for Network-devices
  409. | contains a string that is the value of "hrNetworkIfIndex".
  410. |============================================================================
  411. | 1.3.6.1.2.1.25.3.4.1...
  412. | | | |
  413. | | | *-HrNetworkEntry
  414. | | *-HrNetworkTable
  415. | *-hrDevice
  416. */
  417. {
  418. DWORD dwBytesRequired;
  419. MIB_IFTABLE *iftable; /* --> Heap storage containing table */
  420. UINT i; /* iftable index */
  421. /*
  422. | We fetch the IfTable from the Mib2 subsection of the agent and for each
  423. | network interface (ie for every entry in the table) we create a row in
  424. | the hrDeviceTable.
  425. |
  426. | The HIDDEN_CTX attribute value in the row will simply be the "dwIndex"
  427. | entry in the from the iftable entry from which the row was generated.
  428. | This becomes the value of "hrNetworkIfIndex".
  429. */
  430. /* Initialize */
  431. dwBytesRequired = 0;
  432. iftable = NULL;
  433. /* Ask for the size of the table from "iphlpapi" */
  434. if (GetIfTable(iftable, &dwBytesRequired, TRUE) != ERROR_INSUFFICIENT_BUFFER) {
  435. return ( FALSE );
  436. }
  437. /* Allocate necessary memory */
  438. if ((iftable = (MIB_IFTABLE *)malloc(dwBytesRequired)) == NULL) {
  439. return ( FALSE );
  440. }
  441. /* Ask for the table information from "iphlpapi" */
  442. if (GetIfTable(iftable, &dwBytesRequired, TRUE) != NO_ERROR ) {
  443. /* Release */
  444. free(iftable);
  445. /* Something blew */
  446. return ( FALSE );
  447. }
  448. /* Sweep thru any table creating hrDevice rows */
  449. for (i = 0; i < iftable->dwNumEntries; i += 1) {
  450. /*
  451. | "Hidden Context" is the ifTable index passed in from the GetIfTable()
  452. |
  453. | It will be returned as the value of "hrNetworkIfIndex" by the Get
  454. | function.
  455. */
  456. if (AddHrDeviceRow(type_arc, // DeviceType OID Last-Arc
  457. (unsigned char *) &iftable->table[i].bDescr, // Device Description
  458. &iftable->table[i].dwIndex, // Hidden Ctx "index"
  459. CA_NUMBER // Hidden Ctx "type"
  460. ) == NULL ) {
  461. /* Release */
  462. free(iftable);
  463. /* Something blew */
  464. return ( FALSE );
  465. }
  466. }
  467. /* Release */
  468. free(iftable);
  469. return ( TRUE );
  470. }