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.

386 lines
11 KiB

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