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.

159 lines
4.9 KiB

  1. /*++
  2. Copyright (c) 2000, Microsoft Corporation
  3. Module Name:
  4. ipfwc.c
  5. Abstract:
  6. This module contains the control-program for the IP firewall hook
  7. test driver.
  8. Author:
  9. Abolade Gbadegesin (aboladeg) 7-March-2000
  10. Revision History:
  11. --*/
  12. #include <nt.h>
  13. #include <ntrtl.h>
  14. #include <nturtl.h>
  15. #include <windows.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include "ipfw.h"
  19. typedef struct _IPFWC_ENTRY {
  20. HANDLE FileHandle;
  21. ULONG Priority;
  22. ULONG Flags;
  23. } IPFWC_ENTRY, *PIPFWC_ENTRY;
  24. #define IPFWC_ENTRY_FLAG_REGISTERED 0x00000001
  25. IPFWC_ENTRY EntryTable[IPFW_ROUTINE_COUNT];
  26. int __cdecl
  27. main(
  28. int argc,
  29. char* argv[]
  30. )
  31. {
  32. ULONG i;
  33. ULONG Selection;
  34. //
  35. // Enter a loop where we prompt the user to either register or deregister
  36. // a firewall hook with a specified priority.
  37. //
  38. ZeroMemory(EntryTable, sizeof(EntryTable));
  39. for (;;) {
  40. for (i = 0; i < IPFW_ROUTINE_COUNT; i++) {
  41. if (EntryTable[i].Flags & IPFWC_ENTRY_FLAG_REGISTERED) {
  42. printf("\tEntry %d Priority %d\n", i, EntryTable[i].Priority);
  43. }
  44. }
  45. printf("1. Register new entry.\n");
  46. printf("2. Deregister existing entry.\n");
  47. printf("3. Quit.\n");
  48. printf("Enter selection:");
  49. if (!scanf("%d", &Selection)) { break; }
  50. switch(Selection) {
  51. case 1: {
  52. UCHAR Buffer[512];
  53. PIPFW_CREATE_PACKET CreatePacket;
  54. PFILE_FULL_EA_INFORMATION EaBuffer;
  55. ULONG EaBufferLength;
  56. IO_STATUS_BLOCK IoStatus;
  57. OBJECT_ATTRIBUTES ObjectAttributes;
  58. NTSTATUS status;
  59. UNICODE_STRING UnicodeString;
  60. for (i = 0; i < IPFW_ROUTINE_COUNT; i++) {
  61. if (!(EntryTable[i].Flags & IPFWC_ENTRY_FLAG_REGISTERED)) {
  62. break;
  63. }
  64. }
  65. if (i >= IPFW_ROUTINE_COUNT) {
  66. printf("No entries available.\n");
  67. break;
  68. }
  69. printf("Enter priority: ");
  70. if (!scanf("%d", &EntryTable[i].Priority)) { break; }
  71. if (!EntryTable[i].Priority) {
  72. printf("Invalid priority.\n");
  73. break;
  74. }
  75. EaBuffer = (PFILE_FULL_EA_INFORMATION)Buffer;
  76. EaBufferLength =
  77. sizeof(FILE_FULL_EA_INFORMATION) +
  78. IPFW_CREATE_NAME_LENGTH + sizeof(IPFW_CREATE_PACKET);
  79. EaBuffer->NextEntryOffset = 0;
  80. EaBuffer->Flags = 0;
  81. EaBuffer->EaNameLength = IPFW_CREATE_NAME_LENGTH;
  82. CopyMemory(
  83. EaBuffer->EaName, IPFW_CREATE_NAME,
  84. IPFW_CREATE_NAME_LENGTH + 1
  85. );
  86. EaBuffer->EaValueLength = sizeof(IPFW_CREATE_PACKET);
  87. CreatePacket =
  88. (PIPFW_CREATE_PACKET)
  89. (EaBuffer->EaName + EaBuffer->EaNameLength + 1);
  90. CreatePacket->Priority = EntryTable[i].Priority;
  91. RtlInitUnicodeString(&UnicodeString, DD_IPFW_DEVICE_NAME);
  92. InitializeObjectAttributes(
  93. &ObjectAttributes, &UnicodeString, OBJ_CASE_INSENSITIVE,
  94. NULL, NULL
  95. );
  96. status =
  97. NtCreateFile(
  98. &EntryTable[i].FileHandle,
  99. GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE,
  100. &ObjectAttributes,
  101. &IoStatus,
  102. NULL,
  103. 0,
  104. FILE_SHARE_READ|FILE_SHARE_WRITE,
  105. FILE_OPEN_IF,
  106. 0,
  107. EaBuffer,
  108. EaBufferLength
  109. );
  110. if (!NT_SUCCESS(status)) {
  111. printf("Registration failed (status=%x).\n", status);
  112. } else {
  113. EntryTable[i].Flags |= IPFWC_ENTRY_FLAG_REGISTERED;
  114. }
  115. break;
  116. }
  117. case 2: {
  118. printf("Enter entry index: ");
  119. if (!scanf("%d", &i)) { break; }
  120. if (i >= IPFW_ROUTINE_COUNT) {
  121. printf("Invalid entry index.\n");
  122. break;
  123. }
  124. EntryTable[i].Flags &= ~IPFWC_ENTRY_FLAG_REGISTERED;
  125. NtClose(EntryTable[i].FileHandle);
  126. break;
  127. }
  128. case 3: {
  129. return 0;
  130. }
  131. default:
  132. printf("Invalid selection.\n");
  133. break;
  134. }
  135. }
  136. return 0;
  137. }