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.

502 lines
14 KiB

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