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.

487 lines
13 KiB

  1. /*++
  2. Copyright (c) 1992-1996 Microsoft Corporation
  3. Module Name:
  4. prnt_tbl.c
  5. Abstract:
  6. Routines supporting operations on the Print Queue 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 "prnt_tbl.h"
  23. //--------------------------- PUBLIC VARIABLES --(same as in module.h file)--
  24. // Prefix to the Print Queue table
  25. static UINT printQSubids[] = { 2, 29, 1 };
  26. static AsnObjectIdentifier MIB_PrintQPrefix = { 3, printQSubids };
  27. PRINTQ_TABLE MIB_PrintQTable = { 0, NULL };
  28. //--------------------------- PRIVATE CONSTANTS -----------------------------
  29. #define PRNTQ_FIELD_SUBID (MIB_PrintQPrefix.idLength+MIB_OidPrefix.idLength)
  30. #define PRNTQ_FIRST_FIELD PRNTQ_NAME_FIELD
  31. #define PRNTQ_LAST_FIELD PRNTQ_JOBS_FIELD
  32. //--------------------------- PRIVATE STRUCTS -------------------------------
  33. //--------------------------- PRIVATE VARIABLES -----------------------------
  34. //--------------------------- PRIVATE PROTOTYPES ----------------------------
  35. UINT MIB_prntq_get(
  36. IN OUT RFC1157VarBind *VarBind
  37. );
  38. int MIB_prntq_match(
  39. IN AsnObjectIdentifier *Oid,
  40. OUT UINT *Pos
  41. );
  42. UINT MIB_prntq_copyfromtable(
  43. IN UINT Entry,
  44. IN UINT Field,
  45. OUT RFC1157VarBind *VarBind
  46. );
  47. //--------------------------- PRIVATE PROCEDURES ----------------------------
  48. //--------------------------- PUBLIC PROCEDURES -----------------------------
  49. //
  50. // MIB_prntq_func
  51. // High level routine for handling operations on the print queue table
  52. //
  53. // Notes:
  54. //
  55. // Return Codes:
  56. // None.
  57. //
  58. // Error Codes:
  59. // None.
  60. //
  61. UINT MIB_prntq_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 Print Queue table with the info from server
  75. if ( SNMPAPI_ERROR == MIB_prntq_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_PrintQTable.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[] = { PRNTQ_FIRST_FIELD };
  98. AsnObjectIdentifier FieldOid = { 1, temp_subs };
  99. // prefix bug 445188
  100. AsnObjectIdentifier tmpOid;
  101. tmpOid = VarBind->name; // keep a copy (structure copy)
  102. if (SnmpUtilOidCpy( &VarBind->name, &MIB_OidPrefix ) == SNMPAPI_ERROR)
  103. {
  104. VarBind->name = tmpOid; // restore
  105. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  106. goto Exit;
  107. }
  108. if (SnmpUtilOidAppend( &VarBind->name, &MIB_PrintQPrefix ) == SNMPAPI_ERROR)
  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 ) == SNMPAPI_ERROR)
  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_PrintQTable.Table[0].Oid ) == SNMPAPI_ERROR)
  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_prntq_get( VarBind );
  137. break;
  138. case MIB_ACTION_GETNEXT:
  139. // Fill the Print Queue table with the info from server
  140. if ( SNMPAPI_ERROR == MIB_prntq_lmget() )
  141. {
  142. ErrStat = SNMP_ERRORSTATUS_GENERR;
  143. goto Exit;
  144. }
  145. // Determine which field
  146. Field = VarBind->name.ids[PRNTQ_FIELD_SUBID];
  147. // Lookup OID in table
  148. if (Field < PRNTQ_FIRST_FIELD)
  149. {
  150. Entry = 0; // will take the first entry into the table
  151. Field = PRNTQ_FIRST_FIELD; // and the first column of the table
  152. Found = MIB_TBL_POS_BEFORE;
  153. }
  154. else if (Field > PRNTQ_LAST_FIELD)
  155. Found = MIB_TBL_POS_END;
  156. else
  157. Found = MIB_prntq_match( &VarBind->name, &Entry );
  158. // Index not found, but could be more fields to base GET on
  159. if ((Found == MIB_TBL_POS_BEFORE && MIB_PrintQTable.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 > PRNTQ_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_PrintQTable.Len-1 )
  179. {
  180. Entry = 0;
  181. Field ++;
  182. if ( Field > PRNTQ_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; // prefix bug 445188
  199. temp_subs[0] = Field;
  200. FieldOid.idLength = 1;
  201. FieldOid.ids = temp_subs;
  202. // prefix bug 445188
  203. tmpOid = VarBind->name; // keep a copy (structure copy)
  204. if (SnmpUtilOidCpy( &VarBind->name, &MIB_OidPrefix ) == SNMPAPI_ERROR)
  205. {
  206. VarBind->name = tmpOid; // restore
  207. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  208. goto Exit;
  209. }
  210. if (SnmpUtilOidAppend( &VarBind->name, &MIB_PrintQPrefix ) == SNMPAPI_ERROR)
  211. {
  212. SnmpUtilOidFree(&VarBind->name);
  213. VarBind->name = tmpOid; // restore
  214. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  215. goto Exit;
  216. }
  217. if (SnmpUtilOidAppend( &VarBind->name, &FieldOid ) == SNMPAPI_ERROR)
  218. {
  219. SnmpUtilOidFree(&VarBind->name);
  220. VarBind->name = tmpOid; // restore
  221. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  222. goto Exit;
  223. }
  224. if (SnmpUtilOidAppend( &VarBind->name, &MIB_PrintQTable.Table[Entry].Oid ) == SNMPAPI_ERROR)
  225. {
  226. SnmpUtilOidFree(&VarBind->name);
  227. VarBind->name = tmpOid; // restore
  228. ErrStat = SNMP_ERRORSTATUS_RESOURCEUNAVAILABLE;
  229. goto Exit;
  230. }
  231. // free the original VarBind->name
  232. SnmpUtilOidFree(&tmpOid);
  233. }
  234. ErrStat = MIB_prntq_copyfromtable( Entry, Field, VarBind );
  235. break;
  236. case MIB_ACTION_SET:
  237. ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  238. break;
  239. default:
  240. ErrStat = SNMP_ERRORSTATUS_GENERR;
  241. }
  242. Exit:
  243. return ErrStat;
  244. } // MIB_prntq_func
  245. //
  246. // MIB_prntq_get
  247. // Retrieve print queue table information.
  248. //
  249. // Notes:
  250. //
  251. // Return Codes:
  252. // None.
  253. //
  254. // Error Codes:
  255. // None.
  256. //
  257. UINT MIB_prntq_get(
  258. IN OUT RFC1157VarBind *VarBind
  259. )
  260. {
  261. UINT Entry;
  262. int Found = MIB_TBL_POS_END;
  263. UINT ErrStat;
  264. if (VarBind->name.ids[PRNTQ_FIELD_SUBID] < PRNTQ_FIRST_FIELD ||
  265. VarBind->name.ids[PRNTQ_FIELD_SUBID] > PRNTQ_LAST_FIELD)
  266. {
  267. ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  268. goto Exit;
  269. }
  270. // Fill the Print Queue table with the info from server
  271. if ( SNMPAPI_ERROR == MIB_prntq_lmget() )
  272. {
  273. ErrStat = SNMP_ERRORSTATUS_GENERR;
  274. goto Exit;
  275. }
  276. // Prefix # 118016
  277. // make sure MIB_prntq_lmget doesn't invalid the global MIB_PrintQTable.Table
  278. if (MIB_PrintQTable.Table)
  279. Found = MIB_prntq_match( &VarBind->name, &Entry );
  280. // Look for a complete OID match
  281. if ( Found != MIB_TBL_POS_FOUND )
  282. {
  283. ErrStat = SNMP_ERRORSTATUS_NOSUCHNAME;
  284. goto Exit;
  285. }
  286. // Copy data from table
  287. ErrStat = MIB_prntq_copyfromtable( Entry,
  288. VarBind->name.ids[PRNTQ_FIELD_SUBID],
  289. VarBind );
  290. Exit:
  291. return ErrStat;
  292. } // MIB_prntq_get
  293. //
  294. // MIB_prntq_match
  295. // Match the target OID with a location in the Print Queue table
  296. //
  297. // Notes:
  298. //
  299. // Return Codes:
  300. // None.
  301. //
  302. // Error Codes:
  303. // None
  304. //
  305. int MIB_prntq_match(
  306. IN AsnObjectIdentifier *Oid,
  307. OUT UINT *Pos
  308. )
  309. {
  310. AsnObjectIdentifier TempOid;
  311. int nResult;
  312. // Remove prefix including field reference
  313. TempOid.idLength = Oid->idLength - MIB_OidPrefix.idLength -
  314. MIB_PrintQPrefix.idLength - 1;
  315. TempOid.ids = &Oid->ids[MIB_OidPrefix.idLength+MIB_PrintQPrefix.idLength+1];
  316. *Pos = 0;
  317. while ( *Pos < MIB_PrintQTable.Len )
  318. {
  319. nResult = SnmpUtilOidCmp( &TempOid, &MIB_PrintQTable.Table[*Pos].Oid );
  320. if ( !nResult )
  321. {
  322. nResult = MIB_TBL_POS_FOUND;
  323. goto Exit;
  324. }
  325. if ( nResult < 0 )
  326. {
  327. nResult = MIB_TBL_POS_BEFORE;
  328. goto Exit;
  329. }
  330. (*Pos)++;
  331. }
  332. nResult = MIB_TBL_POS_END;
  333. Exit:
  334. return nResult;
  335. }
  336. //
  337. // MIB_prntq_copyfromtable
  338. // Copy requested data from table structure into Var Bind.
  339. //
  340. // Notes:
  341. //
  342. // Return Codes:
  343. // None.
  344. //
  345. // Error Codes:
  346. // None.
  347. //
  348. UINT MIB_prntq_copyfromtable(
  349. IN UINT Entry,
  350. IN UINT Field,
  351. OUT RFC1157VarBind *VarBind
  352. )
  353. {
  354. UINT ErrStat;
  355. // Get the requested field and save in var bind
  356. switch( Field )
  357. {
  358. case PRNTQ_NAME_FIELD:
  359. // Alloc space for string
  360. VarBind->value.asnValue.string.stream = SnmpUtilMemAlloc( sizeof(char)
  361. * MIB_PrintQTable.Table[Entry].svPrintQName.length );
  362. if ( VarBind->value.asnValue.string.stream == NULL )
  363. {
  364. ErrStat = SNMP_ERRORSTATUS_GENERR;
  365. goto Exit;
  366. }
  367. // Copy string into return position
  368. memcpy( VarBind->value.asnValue.string.stream,
  369. MIB_PrintQTable.Table[Entry].svPrintQName.stream,
  370. MIB_PrintQTable.Table[Entry].svPrintQName.length );
  371. // Set string length
  372. VarBind->value.asnValue.string.length =
  373. MIB_PrintQTable.Table[Entry].svPrintQName.length;
  374. VarBind->value.asnValue.string.dynamic = TRUE;
  375. // Set type of var bind
  376. VarBind->value.asnType = ASN_RFC1213_DISPSTRING;
  377. break;
  378. case PRNTQ_JOBS_FIELD:
  379. VarBind->value.asnValue.number =
  380. MIB_PrintQTable.Table[Entry].svPrintQNumJobs;
  381. VarBind->value.asnType = ASN_INTEGER;
  382. break;
  383. default:
  384. SNMPDBG(( SNMP_LOG_TRACE, "LMMIB2: Internal Error Print Queue Table\n" ));
  385. ErrStat = SNMP_ERRORSTATUS_GENERR;
  386. goto Exit;
  387. }
  388. ErrStat = SNMP_ERRORSTATUS_NOERROR;
  389. Exit:
  390. return ErrStat;
  391. } // MIB_prntq_copyfromtable
  392. //-------------------------------- END --------------------------------------