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.

435 lines
12 KiB

  1. //
  2. //
  3. // Copyright (c) 1991 Microsoft Corporation
  4. //
  5. //-------------------------- MODULE DESCRIPTION ----------------------------
  6. //
  7. // winsmain.c
  8. //
  9. //---------------------------------------------------------------------------
  10. //--------------------------- WINDOWS DEPENDENCIES --------------------------
  11. #include <windows.h>
  12. //--------------------------- STANDARD DEPENDENCIES -- #include<xxxxx.h> ----
  13. #include <stdio.h>
  14. #include <malloc.h>
  15. //--------------------------- MODULE DEPENDENCIES -- #include"xxxxx.h" ------
  16. #include <snmp.h>
  17. #include <util.h>
  18. #include "winsmib.h"
  19. //--------------------------- SELF-DEPENDENCY -- ONE #include"module.h" -----
  20. //--------------------------- PUBLIC VARIABLES --(same as in module.h file)--
  21. //--------------------------- PRIVATE CONSTANTS -----------------------------
  22. //--------------------------- PRIVATE STRUCTS -------------------------------
  23. //--------------------------- PRIVATE VARIABLES -----------------------------
  24. //--------------------------- PRIVATE PROTOTYPES ----------------------------
  25. //--------------------------- PRIVATE PROCEDURES ----------------------------
  26. //--------------------------- PUBLIC PROCEDURES -----------------------------
  27. typedef AsnObjectIdentifier View; // temp until view is defined...
  28. _cdecl main(
  29. IN int argumentCount,
  30. IN char *argumentVector[])
  31. {
  32. HANDLE hExtension;
  33. FARPROC initAddr;
  34. FARPROC queryAddr;
  35. FARPROC trapAddr;
  36. DWORD timeZeroReference;
  37. HANDLE hPollForTrapEvent;
  38. View supportedView;
  39. UINT Val;
  40. DWORD Choice;
  41. DWORD Oper;
  42. BYTE Name[255];
  43. INT numQueries = 10;
  44. extern INT nLogLevel;
  45. extern INT nLogType;
  46. nLogLevel = 15;
  47. nLogType = 1;
  48. // avoid compiler warning...
  49. UNREFERENCED_PARAMETER(argumentCount);
  50. UNREFERENCED_PARAMETER(argumentVector);
  51. timeZeroReference = GetCurrentTime();
  52. // load the extension agent dll and resolve the entry points...
  53. if (GetModuleHandle("winsmib.dll") == NULL)
  54. {
  55. if ((hExtension = LoadLibrary("winsmib.dll")) == NULL)
  56. {
  57. dbgprintf(1, "error on LoadLibrary %d\n", GetLastError());
  58. }
  59. else
  60. {
  61. if ((initAddr = GetProcAddress(hExtension,
  62. "SnmpExtensionInit")) == NULL)
  63. {
  64. dbgprintf(1, "error on GetProcAddress %d\n", GetLastError());
  65. }
  66. else
  67. {
  68. if ((queryAddr = GetProcAddress(hExtension,
  69. "SnmpExtensionQuery")) == NULL)
  70. {
  71. dbgprintf(1, "error on GetProcAddress %d\n",
  72. GetLastError());
  73. }
  74. else
  75. {
  76. if ((trapAddr = GetProcAddress(hExtension,
  77. "SnmpExtensionTrap")) == NULL)
  78. {
  79. dbgprintf(1, "error on GetProcAddress %d\n",
  80. GetLastError());
  81. }
  82. else
  83. {
  84. // initialize the extension agent via its init entry point...
  85. (*initAddr)(
  86. timeZeroReference,
  87. &hPollForTrapEvent,
  88. &supportedView);
  89. }
  90. }
  91. }
  92. }
  93. } // end if (Already loaded)
  94. {
  95. RFC1157VarBindList varBinds;
  96. AsnInteger errorStatus;
  97. AsnInteger errorIndex;
  98. UINT OID_Prefix[] = { 1, 3, 6, 1, 4, 1, 311, 1 };
  99. UINT OID_Suffix1[] = { 1, 2, 0};
  100. UINT OID_Suffix2[] = { 1, 3, 0};
  101. UINT OID_Suffix3[] = { 1, 4, 0};
  102. AsnObjectIdentifier MIB_OidPrefix = { OID_SIZEOF(OID_Prefix), OID_Prefix };
  103. AsnObjectIdentifier Sf1Oid = { OID_SIZEOF(OID_Suffix1), OID_Suffix1};
  104. AsnObjectIdentifier Sf2Oid = { OID_SIZEOF(OID_Suffix2), OID_Suffix2};
  105. AsnObjectIdentifier Sf3Oid = { OID_SIZEOF(OID_Suffix3), OID_Suffix3};
  106. errorStatus = 0;
  107. errorIndex = 0;
  108. varBinds.list = (RFC1157VarBind *)SNMP_malloc( sizeof(RFC1157VarBind));
  109. // varBinds.list = (RFC1157VarBind *)SNMP_malloc( sizeof(RFC1157VarBind) * 4);
  110. // varBinds.len = 4;
  111. varBinds.len = 1;
  112. SNMP_oidcpy( &varBinds.list[0].name, &MIB_OidPrefix );
  113. varBinds.list[0].value.asnType = ASN_NULL;
  114. #if 0
  115. SNMP_oidcpy( &varBinds.list[1].name, &MIB_OidPrefix );
  116. SNMP_oidappend( &varBinds.list[1].name, &Sf1Oid );
  117. varBinds.list[1].value.asnType = ASN_NULL;
  118. SNMP_oidcpy( &varBinds.list[2].name, &MIB_OidPrefix );
  119. SNMP_oidappend( &varBinds.list[2].name, &Sf2Oid );
  120. varBinds.list[2].value.asnType = ASN_NULL;
  121. SNMP_oidcpy( &varBinds.list[3].name, &MIB_OidPrefix );
  122. SNMP_oidappend( &varBinds.list[3].name, &Sf3Oid );
  123. varBinds.list[3].value.asnType = ASN_NULL;
  124. #endif
  125. printf("Walk ? (1 for yes) -- ");
  126. scanf("%d", &Choice);
  127. if (Choice == 1)
  128. {
  129. do
  130. {
  131. printf( "\nGET-NEXT of: " ); SNMP_oiddisp( &varBinds.list[0].name );
  132. printf( " " );
  133. (*queryAddr)( (AsnInteger)ASN_RFC1157_GETNEXTREQUEST,
  134. &varBinds,
  135. &errorStatus,
  136. &errorIndex
  137. );
  138. printf( "\n is " ); SNMP_oiddisp( &varBinds.list[0].name );
  139. if ( errorStatus )
  140. {
  141. printf( "\nErrorstatus: %lu\n\n", errorStatus );
  142. }
  143. else
  144. {
  145. printf( "\n = " ); SNMP_printany( &varBinds.list[0].value );
  146. }
  147. // putchar( '\n' );
  148. } while ( varBinds.list[0].name.ids[MIB_PREFIX_LEN-1] == 1 );
  149. // Free the memory
  150. SNMP_FreeVarBindList( &varBinds );
  151. }
  152. } // block
  153. {
  154. char String[80];
  155. DWORD i;
  156. RFC1157VarBindList varBinds;
  157. UINT OID_Prefix[] = { 1, 3, 6, 1, 4, 1, 311, 1 };
  158. UINT TempOid[255];
  159. AsnObjectIdentifier MIB_OidPrefix = { OID_SIZEOF(OID_Prefix), OID_Prefix };
  160. AsnObjectIdentifier MIB_Suffix = { OID_SIZEOF(TempOid), TempOid};
  161. AsnInteger errorStatus;
  162. AsnInteger errorIndex;
  163. UINT Code;
  164. varBinds.list = (RFC1157VarBind *)SNMP_malloc( sizeof(RFC1157VarBind) );
  165. varBinds.len = 1;
  166. Loop:
  167. printf("Enter Code for Group \nPar\t1\nPull\t2\nPush\t3\nDF\t4\nCmd\t5\nCode is --");
  168. scanf("%d", &TempOid[0]);
  169. if (TempOid[0] < 1 || TempOid[0] > 5)
  170. {
  171. goto Loop;
  172. }
  173. LoopLT:
  174. printf("Leaf or Table access (0/1) -- ");
  175. scanf("%d", &Code);
  176. if (Code != 0 && Code != 1)
  177. {
  178. goto LoopLT;
  179. }
  180. printf("Enter Code for var. --");
  181. scanf("%d", &TempOid[1]);
  182. if (Code == 0)
  183. {
  184. TempOid[2] = 0;
  185. MIB_Suffix.idLength = 3;
  186. }
  187. else
  188. {
  189. TempOid[2] = 1;
  190. printf("Enter Code for field --");
  191. scanf("%d", &TempOid[3]);
  192. switch(TempOid[0])
  193. {
  194. case(2):
  195. case(3):
  196. printf("Input IP address in host order\n");
  197. for (i=0; i< 4; i++)
  198. {
  199. printf("\nByte (%d) -- ", i);
  200. scanf("%d", &TempOid[4 + i]);
  201. }
  202. MIB_Suffix.idLength = 8;
  203. break;
  204. case(4):
  205. printf("Index of file ? -- ");
  206. scanf("%d", &TempOid[4]);
  207. MIB_Suffix.idLength = 5;
  208. case(5):
  209. printf("name -- ");
  210. scanf("%s", Name);
  211. for (i=0; i<strlen(Name); i++)
  212. {
  213. TempOid[4+i] = (UINT)Name[i];
  214. }
  215. TempOid[4+i] = 0;
  216. MIB_Suffix.idLength = 4 + i + 1;
  217. break;
  218. }
  219. }
  220. //
  221. // Construct OID with complete prefix for comparison purposes
  222. //
  223. SNMP_oidcpy( &varBinds.list[0].name, &MIB_OidPrefix );
  224. SNMP_oidappend( &varBinds.list[0].name, &MIB_Suffix );
  225. //printf( "SET: " );
  226. SNMP_oiddisp( &varBinds.list[0].name );
  227. Loop1:
  228. printf("\nSET/GET/GET_NEXT - 0/1/? -- ");
  229. scanf("%d", &Oper);
  230. printf("\nEnter Type (1 - Integer, 2-Octet String, 3 -IP address, 4 -Counter -- ");
  231. scanf("%d",&Choice);
  232. if (Choice < 1 || Choice > 4)
  233. {
  234. goto Loop1;
  235. }
  236. switch(Choice)
  237. {
  238. case(1):
  239. varBinds.list[0].value.asnType = ASN_INTEGER;
  240. if (Oper == 0)
  241. {
  242. printf("\nInteger Value -- ");
  243. scanf("%d", &Val);
  244. varBinds.list[0].value.asnValue.number = Val;
  245. }
  246. break;
  247. case(2):
  248. varBinds.list[0].value.asnType = ASN_OCTETSTRING;
  249. if (Oper == 0)
  250. {
  251. printf("\nCharacter array -- ");
  252. scanf("%s", String);
  253. varBinds.list[0].value.asnValue.string.length =
  254. strlen( (LPSTR)String );
  255. varBinds.list[0].value.asnValue.string.stream = String;
  256. varBinds.list[0].value.asnValue.string.dynamic = FALSE;
  257. }
  258. break;
  259. case(3):
  260. varBinds.list[0].value.asnType = ASN_RFC1155_IPADDRESS;
  261. if (Oper == 0)
  262. {
  263. printf("\nInput ip address bytes in network byte order\n");
  264. for (i=0; i< 4; i++)
  265. {
  266. printf("\nByte (%d) -- ", i);
  267. scanf("%d", &String[i]);
  268. }
  269. String[4] = 0;
  270. varBinds.list[0].value.asnValue.string.length =
  271. strlen( (LPSTR)String );
  272. varBinds.list[0].value.asnValue.string.stream = String;
  273. varBinds.list[0].value.asnValue.string.dynamic = TRUE;
  274. }
  275. break;
  276. case(4):
  277. varBinds.list[0].value.asnType = ASN_RFC1155_COUNTER;
  278. if (Oper == 0)
  279. {
  280. printf("\nInteger Value -- ");
  281. scanf("%d", &Val);
  282. varBinds.list[0].value.asnValue.number = Val;
  283. }
  284. break;
  285. default:
  286. printf("wrong type\n");
  287. break;
  288. }
  289. errorStatus = 0;
  290. errorIndex = 0;
  291. switch(Oper)
  292. {
  293. case(0):
  294. Code = ASN_RFC1157_SETREQUEST;
  295. printf( "SET: " ); SNMP_oiddisp( &varBinds.list[0].name );
  296. printf( " to " ); SNMP_printany( &varBinds.list[0].value );
  297. break;
  298. case(1):
  299. Code = ASN_RFC1157_GETREQUEST;
  300. printf( "GET: " ); SNMP_oiddisp( &varBinds.list[0].name );
  301. break;
  302. default:
  303. printf( "GETNEXT: " ); SNMP_oiddisp( &varBinds.list[0].name );
  304. Code = ASN_RFC1157_GETNEXTREQUEST;
  305. break;
  306. }
  307. (*queryAddr)( (BYTE)Code,
  308. &varBinds,
  309. &errorStatus,
  310. &errorIndex
  311. );
  312. printf( "\n Errorstatus: %lu\n\n", errorStatus );
  313. if (Code != ASN_RFC1157_SETREQUEST)
  314. {
  315. if ( errorStatus == SNMP_ERRORSTATUS_NOERROR )
  316. {
  317. printf( "Value: " );
  318. SNMP_printany( &varBinds.list[0].value ); putchar( '\n' );
  319. SNMP_oidfree(&varBinds.list[0].name);
  320. }
  321. }
  322. #if 0
  323. varBinds.list = (RFC1157VarBind *)SNMP_malloc( sizeof(RFC1157VarBind) );
  324. varBinds.len = 1;
  325. varBinds.list[0].name.idLength = sizeof itemn / sizeof(UINT);
  326. varBinds.list[0].name.ids = (UINT *)SNMP_malloc( sizeof(UINT)*
  327. varBinds.list[0].name.idLength );
  328. memcpy( varBinds.list[0].name.ids, &itemn,
  329. sizeof(UINT)*varBinds.list[0].name.idLength );
  330. varBinds.list[0].value.asnType = ASN_INTEGER;
  331. printf("Value ? -- ");
  332. scanf("%d", &Val);
  333. varBinds.list[0].value.asnValue.number = Val;
  334. printf( "SET: " ); SNMP_oiddisp( &varBinds.list[0].name );
  335. printf( " to " ); SNMP_printany( &varBinds.list[0].value );
  336. (*queryAddr)( ASN_RFC1157_SETREQUEST,
  337. &varBinds,
  338. &errorStatus,
  339. &errorIndex
  340. );
  341. printf( "\nSET Errorstatus: %lu\n\n", errorStatus );
  342. (*queryAddr)( ASN_RFC1157_GETREQUEST,
  343. &varBinds,
  344. &errorStatus,
  345. &errorIndex
  346. );
  347. if ( errorStatus == SNMP_ERRORSTATUS_NOERROR )
  348. {
  349. printf( "Value: " );
  350. SNMP_printany( &varBinds.list[0].value ); putchar( '\n' );
  351. SNMP_oidfree(&varBinds.list[0].name);
  352. }
  353. printf( "\nGET Errorstatus: %lu\n\n", errorStatus );
  354. #endif
  355. #if 0
  356. // Free the memory
  357. SNMP_FreeVarBindList( &varBinds );
  358. #endif
  359. printf( "\n\n" );
  360. printf("Enter 1 to exit -- ");
  361. scanf("%d", &Choice);
  362. if (Choice != 1)
  363. {
  364. goto Loop;
  365. }
  366. }
  367. return 0;
  368. } // end main()
  369. //-------------------------------- END --------------------------------------