FirewallAppView.cpp
上传用户:wlquartz
上传日期:2022-03-04
资源大小:89k
文件大小:6k
开发平台:

Visual C++

  1. // FirewallAppView.cpp : implementation of the CFirewallAppView class
  2. //
  3. #include "stdafx.h"
  4. #include "FirewallApp.h"
  5. #include "FirewallAppDoc.h"
  6. #include "FirewallAppView.h"
  7. #include "SockUtil.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CFirewallAppView
  15. IMPLEMENT_DYNCREATE(CFirewallAppView, CFormView)
  16. BEGIN_MESSAGE_MAP(CFirewallAppView, CFormView)
  17. //{{AFX_MSG_MAP(CFirewallAppView)
  18. // NOTE - the ClassWizard will add and remove mapping macros here.
  19. //    DO NOT EDIT what you see in these blocks of generated code!
  20. //}}AFX_MSG_MAP
  21. // Standard printing commands
  22. ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
  23. ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
  24. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CFirewallAppView construction/destruction
  28. CFirewallAppView::CFirewallAppView()
  29. : CFormView(CFirewallAppView::IDD)
  30. {
  31. //{{AFX_DATA_INIT(CFirewallAppView)
  32. // NOTE: the ClassWizard will add member initialization here
  33. //}}AFX_DATA_INIT
  34. // TODO: add construction code here
  35. }
  36. CFirewallAppView::~CFirewallAppView()
  37. {
  38. }
  39. void CFirewallAppView::DoDataExchange(CDataExchange* pDX)
  40. {
  41. CFormView::DoDataExchange(pDX);
  42. //{{AFX_DATA_MAP(CFirewallAppView)
  43. DDX_Control(pDX, IDC_LIST1, m_rules);
  44. //}}AFX_DATA_MAP
  45. }
  46. BOOL CFirewallAppView::PreCreateWindow(CREATESTRUCT& cs)
  47. {
  48. // TODO: Modify the Window class or styles here by modifying
  49. //  the CREATESTRUCT cs
  50. return CFormView::PreCreateWindow(cs);
  51. }
  52. void CFirewallAppView::OnInitialUpdate()
  53. {
  54. CFormView::OnInitialUpdate();
  55. GetParentFrame()->RecalcLayout();
  56. ResizeParentToFit();
  57. RECT rc;
  58. m_rules.GetClientRect(&rc);
  59. int width=rc.right-rc.left-110;
  60. m_rules.InsertColumn(0, "Source Ip",LVCFMT_LEFT , width/6, 0);
  61. m_rules.InsertColumn(1, "Source Mask",LVCFMT_LEFT , width/6, 1);
  62. m_rules.InsertColumn(2, "Source Port",LVCFMT_LEFT ,width/6, 2);
  63. m_rules.InsertColumn(3, "Dest. Ip",LVCFMT_LEFT , width/6, 3);
  64. m_rules.InsertColumn(4, "Dest. Mask",LVCFMT_LEFT , width/6, 4);
  65. m_rules.InsertColumn(5, "Dest. Port",LVCFMT_LEFT , width/6, 5);
  66. m_rules.InsertColumn(6, "Protocol",LVCFMT_LEFT ,60, 6);
  67. m_rules.InsertColumn(7, "Action",LVCFMT_LEFT , 50, 7);
  68. m_rules.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
  69. }
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CFirewallAppView printing
  72. BOOL CFirewallAppView::OnPreparePrinting(CPrintInfo* pInfo)
  73. {
  74. // default preparation
  75. return DoPreparePrinting(pInfo);
  76. }
  77. void CFirewallAppView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  78. {
  79. // TODO: add extra initialization before printing
  80. }
  81. void CFirewallAppView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  82. {
  83. // TODO: add cleanup after printing
  84. }
  85. void CFirewallAppView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
  86. {
  87. // TODO: add customized printing code here
  88. }
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CFirewallAppView diagnostics
  91. #ifdef _DEBUG
  92. void CFirewallAppView::AssertValid() const
  93. {
  94. CFormView::AssertValid();
  95. }
  96. void CFirewallAppView::Dump(CDumpContext& dc) const
  97. {
  98. CFormView::Dump(dc);
  99. }
  100. CFirewallAppDoc* CFirewallAppView::GetDocument() // non-debug version is inline
  101. {
  102. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFirewallAppDoc)));
  103. return (CFirewallAppDoc*)m_pDocument;
  104. }
  105. #endif //_DEBUG
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CFirewallAppView message handlers
  108. void CFirewallAppView::UpdateList()
  109. {
  110. CFirewallAppDoc *doc = GetDocument();
  111. // Actualizo la tabla de reglas
  112. m_rules.DeleteAllItems();
  113. unsigned int i;
  114. for(i=0;i<doc->nRules;i++)
  115. {
  116. AddRuleToList(doc->rules[i].sourceIp,
  117.   doc->rules[i].sourceMask,
  118.   doc->rules[i].sourcePort,
  119.   doc->rules[i].destinationIp,
  120.   doc->rules[i].destinationMask,
  121.   doc->rules[i].destinationPort,
  122.   doc->rules[i].protocol,
  123.   doc->rules[i].action);
  124. }
  125. }
  126. void CFirewallAppView::AddRuleToList(unsigned long srcIp, 
  127.  unsigned long srcMask,
  128.  unsigned short srcPort, 
  129.  unsigned long dstIp, 
  130.  unsigned long dstMask,
  131.  unsigned short dstPort, 
  132.  unsigned int protocol, 
  133.  int action)
  134. {
  135. char ip[16];
  136. char port[6];
  137. LVITEM it;
  138. int pos;
  139. it.mask = LVIF_TEXT;
  140. it.iItem = m_rules.GetItemCount();
  141. it.iSubItem = 0;
  142. it.pszText = (srcIp == 0) ? "All" : IpToString(ip, srcIp);
  143. pos = m_rules.InsertItem(&it);
  144. it.iItem = pos;
  145. it.iSubItem = 1;
  146. it.pszText = IpToString(ip, srcMask);
  147. m_rules.SetItem(&it);
  148. it.iItem = pos;
  149. it.iSubItem = 2;
  150. it.pszText = (srcPort == 0) ? "All" : itoa(srcPort, port, 10);
  151. m_rules.SetItem(&it);
  152. it.iItem = pos;
  153. it.iSubItem = 3;
  154. it.pszText = (dstIp == 0) ? "All" : IpToString(ip, dstIp);
  155. m_rules.SetItem(&it);
  156. it.iItem = pos;
  157. it.iSubItem = 4;
  158. it.pszText = IpToString(ip, dstMask);
  159. m_rules.SetItem(&it);
  160. it.iItem = pos;
  161. it.iSubItem = 5;
  162. it.pszText = (dstPort == 0) ? "All" : itoa(dstPort, port, 10);
  163. m_rules.SetItem(&it);
  164. it.iItem = pos;
  165. it.iSubItem = 6;
  166. if(protocol == 1)
  167. it.pszText = "ICMP";
  168. else if(protocol == 6)
  169. it.pszText = "TCP";
  170. else if(protocol == 17)
  171. it.pszText = "UDP";
  172. else
  173. it.pszText = "All";
  174. m_rules.SetItem(&it);
  175. it.iItem = pos;
  176. it.iSubItem = 7;
  177. it.pszText = action ? "Drop" : "Forward";
  178. m_rules.SetItem(&it);
  179. }