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.

512 lines
14 KiB

  1. /*++
  2. Copyright (c) 1992-1996 Microsoft Corporation
  3. Module Name:
  4. uses_tbl.c
  5. Abstract:
  6. Routines to perform operations on the Workstation Uses table.
  7. Environment:
  8. User Mode - Win32
  9. Revision History:
  10. 10-May-1996 DonRyan
  11. Removed banner from Technology Dynamics, Inc.
  12. --*/
  13. //--------------------------- WINDOWS DEPENDENCIES --------------------------
  14. //--------------------------- STANDARD DEPENDENCIES -- #include<xxxxx.h> ----
  15. #include <stdio.h>
  16. #include <memory.h>
  17. //--------------------------- MODULE DEPENDENCIES -- #include"xxxxx.h" ------
  18. #include <snmp.h>
  19. #include <snmputil.h>
  20. #include "mibfuncs.h"
  21. //--------------------------- SELF-DEPENDENCY -- ONE #include"module.h" -----
  22. #include "uses_tbl.h"
  23. //--------------------------- PUBLIC VARIABLES --(same as in module.h file)--
  24. // Prefix to the Uses table
  25. static UINT usesSubids[] = { 3, 8, 1 };
  26. static AsnObjectIdentifier MIB_UsesPrefix = { 3, usesSubids };
  27. WKSTA_USES_TABLE MIB_WkstaUsesTable = { 0, NULL };
  28. //--------------------------- PRIVATE CONSTANTS -----------------------------
  29. #define USES_FIELD_SUBID (MIB_UsesPrefix.idLength+MIB_OidPrefix.idLength)
  30. #define USES_FIRST_FIELD USES_LOCAL_FIELD
  31. #define USES_LAST_FIELD USES_STATUS_FIELD
  32. //--------------------------- PRIVATE STRUCTS -------------------------------
  33. //--------------------------- PRIVATE VARIABLES -----------------------------
  34. //--------------------------- PRIVATE PROTOTYPES ----------------------------
  35. UINT MIB_wsuses_get(
  36. IN OUT RFC1157VarBind *VarBind
  37. );
  38. int MIB_wsuses_match(
  39. IN AsnObjectIdentifier *Oid,
  40. OUT UINT *Pos,
  41. IN BOOL Next
  42. );
  43. UINT MIB_wsuses_copyfromtable(
  44. IN UINT Entry,
  45. IN UINT Field,
  46. OUT RFC1157VarBind *VarBind
  47. );
  48. //--------------------------- PRIVATE PROCEDURES ----------------------------
  49. //--------------------------- PUBLIC PROCEDURES -----------------------------
  50. //
  51. // MIB_wsuses_func
  52. // High level routine for handling operations on the uses table
  53. //
  54. // Notes:
  55. //
  56. // Return Codes:
  57. // None.
  58. //
  59. // Error Codes:
  60. // None.
  61. //
  62. UINT MIB_wsuses_func(
  63. IN UINT Action,
  64. IN MIB_ENTRY *MibPtr,
  65. IN OUT RFC1157VarBind *VarBind
  66. )
  67. {
  68. int Found;
  69. UINT Entry;
  70. UINT Field;
  71. UINT ErrStat;
  72. switch ( Action )
  73. {
  74. case MIB_ACTION_GETFIRST:
  75. // Fill the Uses table with the info from server
  76. if ( SNMPAPI_ERROR == MIB_wsuses_lmget() )
  77. {
  78. ErrStat = SNMP_ERRORSTATUS_GENERR;
  79. goto Exit;
  80. }
  81. // If no elements in table, then return next MIB var, if one
  82. if ( MIB_WkstaUsesTable.Len == 0 )
  83. {
  84. if ( MibPtr->MibNext == NULL )
  85. {
  86. ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  87. goto Exit;
  88. }
  89. // Do get first on the next MIB var
  90. ErrStat = (*MibPtr->MibNext->MibFunc)( Action, MibPtr->MibNext,
  91. VarBind );
  92. break;
  93. }
  94. //
  95. // Place correct OID in VarBind
  96. // Assuming the first field in the first record is the "start"
  97. {
  98. UINT temp_subs[] = { USES_FIRST_FIELD };
  99. AsnObjectIdentifier FieldOid = { 1, temp_subs };
  100. AsnObjectIdentifier tmpOid;
  101. tmpOid = VarBind->name; // keep a copy (structure copy)
  102. if (! SnmpUtilOidCpy( &VarBind->name, &MIB_OidPrefix ))
  103. {
  104. VarBind->name = tmpOid; // restore
  105. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  106. goto Exit;
  107. }
  108. if (! SnmpUtilOidAppend( &VarBind->name, &MIB_UsesPrefix ))
  109. {
  110. SnmpUtilOidFree(&VarBind->name);
  111. VarBind->name = tmpOid; // restore
  112. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  113. goto Exit;
  114. }
  115. if (! SnmpUtilOidAppend( &VarBind->name, &FieldOid ))
  116. {
  117. SnmpUtilOidFree(&VarBind->name);
  118. VarBind->name = tmpOid; // restore
  119. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  120. goto Exit;
  121. }
  122. if (! SnmpUtilOidAppend( &VarBind->name, &MIB_WkstaUsesTable.Table[0].Oid ))
  123. {
  124. SnmpUtilOidFree(&VarBind->name);
  125. VarBind->name = tmpOid; // restore
  126. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  127. goto Exit;
  128. }
  129. // free the original VarBind->name
  130. SnmpUtilOidFree(&tmpOid);
  131. }
  132. //
  133. // Let fall through on purpose
  134. //
  135. case MIB_ACTION_GET:
  136. ErrStat = MIB_wsuses_get( VarBind );
  137. break;
  138. case MIB_ACTION_GETNEXT:
  139. // Fill the Uses table with the info from server
  140. if ( SNMPAPI_ERROR == MIB_wsuses_lmget() )
  141. {
  142. ErrStat = SNMP_ERRORSTATUS_GENERR;
  143. goto Exit;
  144. }
  145. // Determine which field
  146. Field = VarBind->name.ids[USES_FIELD_SUBID];
  147. // Lookup OID in table
  148. if (Field < USES_FIRST_FIELD)
  149. {
  150. Entry = 0; // will take the first entry into the table
  151. Field = USES_FIRST_FIELD; // and the first column of the table
  152. Found = MIB_TBL_POS_BEFORE;
  153. }
  154. else if (Field > USES_LAST_FIELD)
  155. Found = MIB_TBL_POS_END;
  156. else
  157. Found = MIB_wsuses_match( &VarBind->name, &Entry, TRUE );
  158. // Index not found, but could be more fields to base GET on
  159. if ((Found == MIB_TBL_POS_BEFORE && MIB_WkstaUsesTable.Len == 0) ||
  160. Found == MIB_TBL_POS_END )
  161. {
  162. // Index not found in table, get next from field
  163. // Field ++;
  164. // Make sure not past last field
  165. // if ( Field > USES_LAST_FIELD )
  166. // {
  167. // Get next VAR in MIB
  168. ErrStat = (*MibPtr->MibNext->MibFunc)( MIB_ACTION_GETFIRST,
  169. MibPtr->MibNext,
  170. VarBind );
  171. break;
  172. // }
  173. }
  174. // Get next TABLE entry
  175. if ( Found == MIB_TBL_POS_FOUND )
  176. {
  177. Entry ++;
  178. if ( Entry > MIB_WkstaUsesTable.Len-1 )
  179. {
  180. Entry = 0;
  181. Field ++;
  182. if ( Field > USES_LAST_FIELD )
  183. {
  184. // Get next VAR in MIB
  185. ErrStat = (*MibPtr->MibNext->MibFunc)( MIB_ACTION_GETFIRST,
  186. MibPtr->MibNext,
  187. VarBind );
  188. break;
  189. }
  190. }
  191. }
  192. //
  193. // Place correct OID in VarBind
  194. // Assuming the first field in the first record is the "start"
  195. {
  196. UINT temp_subs[1];
  197. AsnObjectIdentifier FieldOid;
  198. AsnObjectIdentifier tmpOid;
  199. temp_subs[0] = Field;
  200. FieldOid.idLength = 1;
  201. FieldOid.ids = temp_subs;
  202. tmpOid = VarBind->name; // keep a copy (structure copy)
  203. if (! SnmpUtilOidCpy( &VarBind->name, &MIB_OidPrefix ))
  204. {
  205. VarBind->name = tmpOid; // restore
  206. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  207. goto Exit;
  208. }
  209. if (! SnmpUtilOidAppend( &VarBind->name, &MIB_UsesPrefix ))
  210. {
  211. SnmpUtilOidFree(&VarBind->name);
  212. VarBind->name = tmpOid; // restore
  213. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  214. goto Exit;
  215. }
  216. if (! SnmpUtilOidAppend( &VarBind->name, &FieldOid ))
  217. {
  218. SnmpUtilOidFree(&VarBind->name);
  219. VarBind->name = tmpOid; // restore
  220. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  221. goto Exit;
  222. }
  223. if (! SnmpUtilOidAppend( &VarBind->name, &MIB_WkstaUsesTable.Table[Entry].Oid ))
  224. {
  225. SnmpUtilOidFree(&VarBind->name);
  226. VarBind->name = tmpOid; // restore
  227. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  228. goto Exit;
  229. }
  230. // free the original VarBind->name
  231. SnmpUtilOidFree(&tmpOid);
  232. }
  233. ErrStat = MIB_wsuses_copyfromtable( Entry, Field, VarBind );
  234. break;
  235. case MIB_ACTION_SET:
  236. ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  237. break;
  238. default:
  239. ErrStat = SNMP_ERRORSTATUS_GENERR;
  240. }
  241. Exit:
  242. return ErrStat;
  243. } // MIB_wsuses_func
  244. //
  245. // MIB_wsuses_get
  246. // Retrieve Uses table information.
  247. //
  248. // Notes:
  249. //
  250. // Return Codes:
  251. // None.
  252. //
  253. // Error Codes:
  254. // None.
  255. //
  256. UINT MIB_wsuses_get(
  257. IN OUT RFC1157VarBind *VarBind
  258. )
  259. {
  260. UINT Entry;
  261. int Found;
  262. UINT ErrStat;
  263. if (VarBind->name.ids[USES_FIELD_SUBID] < USES_FIRST_FIELD ||
  264. VarBind->name.ids[USES_FIELD_SUBID] > USES_LAST_FIELD)
  265. {
  266. ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  267. goto Exit;
  268. }
  269. // Fill the Uses table with the info from server
  270. if ( SNMPAPI_ERROR == MIB_wsuses_lmget() )
  271. {
  272. ErrStat = SNMP_ERRORSTATUS_GENERR;
  273. goto Exit;
  274. }
  275. Found = MIB_wsuses_match( &VarBind->name, &Entry, FALSE );
  276. // Look for a complete OID match
  277. if ( Found != MIB_TBL_POS_FOUND )
  278. {
  279. ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  280. goto Exit;
  281. }
  282. // Copy data from table
  283. ErrStat = MIB_wsuses_copyfromtable( Entry, VarBind->name.ids[USES_FIELD_SUBID],
  284. VarBind );
  285. Exit:
  286. return ErrStat;
  287. } // MIB_wsuses_get
  288. //
  289. // MIB_wsuses_match
  290. // Match the target OID with a location in the Uses table
  291. //
  292. // Notes:
  293. //
  294. // Return Codes:
  295. // None.
  296. //
  297. // Error Codes:
  298. // None
  299. //
  300. int MIB_wsuses_match(
  301. IN AsnObjectIdentifier *Oid,
  302. OUT UINT *Pos,
  303. IN BOOL Next
  304. )
  305. {
  306. AsnObjectIdentifier TempOid;
  307. int nResult;
  308. // Remove prefix including field reference
  309. TempOid.idLength = Oid->idLength - MIB_OidPrefix.idLength -
  310. MIB_UsesPrefix.idLength - 1;
  311. TempOid.ids = &Oid->ids[MIB_OidPrefix.idLength+MIB_UsesPrefix.idLength+1];
  312. *Pos = 0;
  313. while ( *Pos < MIB_WkstaUsesTable.Len )
  314. {
  315. nResult = SnmpUtilOidCmp( &TempOid, &MIB_WkstaUsesTable.Table[*Pos].Oid );
  316. if ( !nResult )
  317. {
  318. nResult = MIB_TBL_POS_FOUND;
  319. if (Next) {
  320. while ( ( (*Pos) + 1 < MIB_WkstaUsesTable.Len ) &&
  321. !SnmpUtilOidCmp( &TempOid, &MIB_WkstaUsesTable.Table[(*Pos)+1].Oid)) {
  322. (*Pos)++;
  323. }
  324. }
  325. goto Exit;
  326. }
  327. if ( nResult < 0 )
  328. {
  329. nResult = MIB_TBL_POS_BEFORE;
  330. goto Exit;
  331. }
  332. (*Pos)++;
  333. }
  334. nResult = MIB_TBL_POS_END;
  335. Exit:
  336. return nResult;
  337. } // MIB_wsuses_match
  338. //
  339. // MIB_wsuses_copyfromtable
  340. // Copy requested data from table structure into Var Bind.
  341. //
  342. // Notes:
  343. //
  344. // Return Codes:
  345. // None.
  346. //
  347. // Error Codes:
  348. // None.
  349. //
  350. UINT MIB_wsuses_copyfromtable(
  351. IN UINT Entry,
  352. IN UINT Field,
  353. OUT RFC1157VarBind *VarBind
  354. )
  355. {
  356. UINT ErrStat;
  357. // Get the requested field and save in var bind
  358. switch( Field )
  359. {
  360. case USES_LOCAL_FIELD:
  361. // Alloc space for string
  362. VarBind->value.asnValue.string.stream = SnmpUtilMemAlloc( sizeof(char)
  363. * MIB_WkstaUsesTable.Table[Entry].useLocalName.length );
  364. if ( VarBind->value.asnValue.string.stream == NULL )
  365. {
  366. ErrStat = SNMP_ERRORSTATUS_GENERR;
  367. goto Exit;
  368. }
  369. // Copy string into return position
  370. memcpy( VarBind->value.asnValue.string.stream,
  371. MIB_WkstaUsesTable.Table[Entry].useLocalName.stream,
  372. MIB_WkstaUsesTable.Table[Entry].useLocalName.length );
  373. // Set string length
  374. VarBind->value.asnValue.string.length =
  375. MIB_WkstaUsesTable.Table[Entry].useLocalName.length;
  376. VarBind->value.asnValue.string.dynamic = TRUE;
  377. // Set type of var bind
  378. VarBind->value.asnType = ASN_RFC1213_DISPSTRING;
  379. break;
  380. case USES_REMOTE_FIELD:
  381. // Alloc space for string
  382. VarBind->value.asnValue.string.stream = SnmpUtilMemAlloc( sizeof(char)
  383. * MIB_WkstaUsesTable.Table[Entry].useRemote.length );
  384. if ( VarBind->value.asnValue.string.stream == NULL )
  385. {
  386. ErrStat = SNMP_ERRORSTATUS_GENERR;
  387. goto Exit;
  388. }
  389. // Copy string into return position
  390. memcpy( VarBind->value.asnValue.string.stream,
  391. MIB_WkstaUsesTable.Table[Entry].useRemote.stream,
  392. MIB_WkstaUsesTable.Table[Entry].useRemote.length );
  393. // Set string length
  394. VarBind->value.asnValue.string.length =
  395. MIB_WkstaUsesTable.Table[Entry].useRemote.length;
  396. VarBind->value.asnValue.string.dynamic = TRUE;
  397. // Set type of var bind
  398. VarBind->value.asnType = ASN_RFC1213_DISPSTRING;
  399. break;
  400. case USES_STATUS_FIELD:
  401. VarBind->value.asnValue.number =
  402. MIB_WkstaUsesTable.Table[Entry].useStatus;
  403. VarBind->value.asnType = ASN_INTEGER;
  404. break;
  405. default:
  406. SNMPDBG(( SNMP_LOG_TRACE, "LMMIB2: Internal Error WorkstationUses Table\n" ));
  407. ErrStat = SNMP_ERRORSTATUS_GENERR;
  408. goto Exit;
  409. }
  410. ErrStat = SNMP_ERRORSTATUS_NOERROR;
  411. Exit:
  412. return ErrStat;
  413. } // MIB_wsuses_copyfromtable
  414. //-------------------------------- END --------------------------------------