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.

451 lines
11 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. SnmpUtilOidFree( &VarBind->name );
  100. SnmpUtilOidCpy( &VarBind->name, &MIB_OidPrefix );
  101. SnmpUtilOidAppend( &VarBind->name, &MIB_SrvcPrefix );
  102. SnmpUtilOidAppend( &VarBind->name, &FieldOid );
  103. SnmpUtilOidAppend( &VarBind->name, &MIB_SrvcTable.Table[0].Oid );
  104. }
  105. //
  106. // Let fall through on purpose
  107. //
  108. case MIB_ACTION_GET:
  109. ErrStat = MIB_srvcs_get( VarBind );
  110. break;
  111. case MIB_ACTION_GETNEXT:
  112. // Fill the Service Table with the info from server
  113. if ( SNMPAPI_ERROR == MIB_srvcs_lmget() )
  114. {
  115. ErrStat = SNMP_ERRORSTATUS_GENERR;
  116. goto Exit;
  117. }
  118. // Determine which field
  119. Field = VarBind->name.ids[SRVC_FIELD_SUBID];
  120. // Lookup OID in table
  121. if (Field < SRVC_FIRST_FIELD)
  122. {
  123. Entry = 0; // will take the first entry into the table
  124. Field = SRVC_FIRST_FIELD; // and the first column of the table
  125. Found = MIB_TBL_POS_BEFORE;
  126. }
  127. else if (Field > SRVC_LAST_FIELD)
  128. Found = MIB_TBL_POS_END;
  129. else
  130. Found = MIB_srvcs_match( &VarBind->name, &Entry );
  131. // Index not found, but could be more fields to base GET on
  132. if ((Found == MIB_TBL_POS_BEFORE && MIB_SrvcTable.Len == 0) ||
  133. Found == MIB_TBL_POS_END )
  134. {
  135. // Index not found in table, get next from field
  136. // Field ++;
  137. // Make sure not past last field
  138. // if ( Field > SRVC_LAST_FIELD )
  139. // {
  140. // Get next VAR in MIB
  141. ErrStat = (*MibPtr->MibNext->MibFunc)( MIB_ACTION_GETFIRST,
  142. MibPtr->MibNext,
  143. VarBind );
  144. break;
  145. // }
  146. }
  147. // Get next TABLE entry
  148. if ( Found == MIB_TBL_POS_FOUND )
  149. {
  150. Entry ++;
  151. if ( Entry > MIB_SrvcTable.Len-1 )
  152. {
  153. Entry = 0;
  154. Field ++;
  155. if ( Field > SRVC_LAST_FIELD )
  156. {
  157. // Get next VAR in MIB
  158. ErrStat = (*MibPtr->MibNext->MibFunc)( MIB_ACTION_GETFIRST,
  159. MibPtr->MibNext,
  160. VarBind );
  161. break;
  162. }
  163. }
  164. }
  165. //
  166. // Place correct OID in VarBind
  167. // Assuming the first field in the first record is the "start"
  168. {
  169. UINT temp_subs[1];
  170. AsnObjectIdentifier FieldOid;
  171. temp_subs[0] = Field;
  172. FieldOid.idLength = 1;
  173. FieldOid.ids = temp_subs;
  174. SnmpUtilOidFree( &VarBind->name );
  175. SnmpUtilOidCpy( &VarBind->name, &MIB_OidPrefix );
  176. SnmpUtilOidAppend( &VarBind->name, &MIB_SrvcPrefix );
  177. SnmpUtilOidAppend( &VarBind->name, &FieldOid );
  178. SnmpUtilOidAppend( &VarBind->name, &MIB_SrvcTable.Table[Entry].Oid );
  179. }
  180. ErrStat = MIB_srvcs_copyfromtable( Entry, Field, VarBind );
  181. break;
  182. case MIB_ACTION_SET:
  183. ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  184. break;
  185. default:
  186. ErrStat = SNMP_ERRORSTATUS_GENERR;
  187. }
  188. Exit:
  189. return ErrStat;
  190. } // MIB_srvcs_func
  191. //
  192. // MIB_srvcs_get
  193. // Retrieve Service Table information.
  194. //
  195. // Notes:
  196. //
  197. // Return Codes:
  198. // None.
  199. //
  200. // Error Codes:
  201. // None.
  202. //
  203. UINT MIB_srvcs_get(
  204. IN OUT RFC1157VarBind *VarBind
  205. )
  206. {
  207. UINT Entry;
  208. int Found;
  209. UINT ErrStat;
  210. if (VarBind->name.ids[SRVC_FIELD_SUBID] < SRVC_FIRST_FIELD ||
  211. VarBind->name.ids[SRVC_FIELD_SUBID] > SRVC_LAST_FIELD)
  212. {
  213. ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  214. goto Exit;
  215. }
  216. // Fill the Service Table with the info from server
  217. if ( SNMPAPI_ERROR == MIB_srvcs_lmget() )
  218. {
  219. ErrStat = SNMP_ERRORSTATUS_GENERR;
  220. goto Exit;
  221. }
  222. Found = MIB_srvcs_match( &VarBind->name, &Entry );
  223. // Look for a complete OID match
  224. if ( Found != MIB_TBL_POS_FOUND )
  225. {
  226. ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  227. goto Exit;
  228. }
  229. // Copy data from table
  230. ErrStat = MIB_srvcs_copyfromtable( Entry, VarBind->name.ids[SRVC_FIELD_SUBID],
  231. VarBind );
  232. Exit:
  233. return ErrStat;
  234. } // MIB_srvcs_get
  235. //
  236. // MIB_srvcs_match
  237. // Match the target OID with a location in the Service Table
  238. //
  239. // Notes:
  240. //
  241. // Return Codes:
  242. // None.
  243. //
  244. // Error Codes:
  245. // None
  246. //
  247. int MIB_srvcs_match(
  248. IN AsnObjectIdentifier *Oid,
  249. OUT UINT *Pos
  250. )
  251. {
  252. AsnObjectIdentifier TempOid;
  253. int nResult;
  254. // Remove prefix including field reference
  255. TempOid.idLength = Oid->idLength - MIB_OidPrefix.idLength -
  256. MIB_SrvcPrefix.idLength - 1;
  257. TempOid.ids = &Oid->ids[MIB_OidPrefix.idLength+MIB_SrvcPrefix.idLength+1];
  258. *Pos = 0;
  259. while ( *Pos < MIB_SrvcTable.Len )
  260. {
  261. nResult = SnmpUtilOidCmp( &TempOid, &MIB_SrvcTable.Table[*Pos].Oid );
  262. if ( !nResult )
  263. {
  264. nResult = MIB_TBL_POS_FOUND;
  265. goto Exit;
  266. }
  267. if ( nResult < 0 )
  268. {
  269. nResult = MIB_TBL_POS_BEFORE;
  270. goto Exit;
  271. }
  272. (*Pos)++;
  273. }
  274. nResult = MIB_TBL_POS_END;
  275. Exit:
  276. return nResult;
  277. }
  278. //
  279. // MIB_srvcs_copyfromtable
  280. // Copy requested data from table structure into Var Bind.
  281. //
  282. // Notes:
  283. //
  284. // Return Codes:
  285. // None.
  286. //
  287. // Error Codes:
  288. // None.
  289. //
  290. UINT MIB_srvcs_copyfromtable(
  291. IN UINT Entry,
  292. IN UINT Field,
  293. OUT RFC1157VarBind *VarBind
  294. )
  295. {
  296. UINT ErrStat;
  297. // Get the requested field and save in var bind
  298. switch( Field )
  299. {
  300. case SRVC_NAME_FIELD:
  301. // Alloc space for string
  302. VarBind->value.asnValue.string.stream = SnmpUtilMemAlloc( sizeof(char)
  303. * MIB_SrvcTable.Table[Entry].svSvcName.length );
  304. if ( VarBind->value.asnValue.string.stream == NULL )
  305. {
  306. ErrStat = SNMP_ERRORSTATUS_GENERR;
  307. goto Exit;
  308. }
  309. // Copy string into return position
  310. memcpy( VarBind->value.asnValue.string.stream,
  311. MIB_SrvcTable.Table[Entry].svSvcName.stream,
  312. MIB_SrvcTable.Table[Entry].svSvcName.length );
  313. // Set string length
  314. VarBind->value.asnValue.string.length =
  315. MIB_SrvcTable.Table[Entry].svSvcName.length;
  316. VarBind->value.asnValue.string.dynamic = TRUE;
  317. // Set type of var bind
  318. VarBind->value.asnType = ASN_RFC1213_DISPSTRING;
  319. break;
  320. case SRVC_INSTALLED_FIELD:
  321. // Set value of var bind
  322. VarBind->value.asnValue.number =
  323. MIB_SrvcTable.Table[Entry].svSvcInstalledState;
  324. VarBind->value.asnType = ASN_INTEGER;
  325. break;
  326. case SRVC_OPERATING_FIELD:
  327. // Set value of var bind
  328. VarBind->value.asnValue.number =
  329. MIB_SrvcTable.Table[Entry].svSvcOperatingState;
  330. VarBind->value.asnType = ASN_INTEGER;
  331. break;
  332. case SRVC_UNINSTALLED_FIELD:
  333. // Set value of var bind
  334. VarBind->value.asnValue.number =
  335. MIB_SrvcTable.Table[Entry].svSvcCanBeUninstalled;
  336. VarBind->value.asnType = ASN_INTEGER;
  337. break;
  338. case SRVC_PAUSED_FIELD:
  339. // Set value of var bind
  340. VarBind->value.asnValue.number =
  341. MIB_SrvcTable.Table[Entry].svSvcCanBePaused;
  342. VarBind->value.asnType = ASN_INTEGER;
  343. break;
  344. default:
  345. SNMPDBG(( SNMP_LOG_TRACE, "LMMIB2: Internal Error Services Table\n" ));
  346. ErrStat = SNMP_ERRORSTATUS_GENERR;
  347. goto Exit;
  348. }
  349. ErrStat = SNMP_ERRORSTATUS_NOERROR;
  350. Exit:
  351. return ErrStat;
  352. } // MIB_srvcs_copyfromtable
  353. //-------------------------------- END --------------------------------------