PZ_Rudj (1195118), страница 9
Текст из файла (страница 9)
#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("MainForm\Unit1.cpp", MainForm);
USEFORM("Table\Table.cpp", FrmTable); /* TFrame: File Type */
USEFORM("TCPIPFilter\TCPIPFilterUnit.cpp", TCPIPFilter);
USEFORM("HTTPFilter\HTTPFilter.cpp", FormHTTPFiler);
USEFORM("LogForm\LogFormEx.cpp", LogForm2);
//---------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->CreateForm(__classid(TMainForm), &MainForm);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
catch (...)
{
try
{
throw Exception("");
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
}
return 0;
}
//---------------------------------------------------------------------------
Листинг А.2 Файл «FireWall.cpp»
#pragma hdrstop
#include "FireWall.h"
#include "stdio.h"
char* DirectionalName(int D)
{
switch(D)
{
case FWD_In:return "In";
case FWD_Out:return "Out";
}
}
USHORT PortConv(USHORT Port)
{
return(((Port&0x00FF)<<8)|((Port&0xFF00)>>8));
}
char* ActionName(int Action)
{
switch(Action)
{
case 0:return "Del";
case 1:return "Skip";
default:return "***";
}
}
char* ProtocolName(int Protocol)
{
switch(Protocol)
{
case IPPROTO_IP :return "IP";
case IPPROTO_ICMP :return "ICMP";
case IPPROTO_IGMP :return "IGMP";
case IPPROTO_GGP :return "GGP";
case IPPROTO_TCP :return "TCP";
case IPPROTO_PUP :return "PUP";
case IPPROTO_UDP :return "UDP";
case IPPROTO_IDP :return "IDP";
case IPPROTO_IPV6 :return "IPV6";
case IPPROTO_ND :return "ND";
case IPPROTO_ICLFXBM :return "ICLFXBM";
case IPPROTO_RAW :return "RAW";
}
return NULL;
}
//---------------------------------------------------------------------------
DWORD TFireWall::DeviceIOCTRL(
DWORD Function,
void* Buffer,
unsigned int dInput,
unsigned int dOutput)
{
if (!IsCreate()) return 0;
DWORD Data=0;
DeviceIoControl(hDevice,CTL_CODE(DeviceID,Function|0x800,METHOD_BUFFERED,FILE_ANY_ACCESS),Buffer,dInput,Buffer,dOutput,&Data,NULL);
return Data;
}
void TFireWall::DeviceConnect(char* DeviceName)
{
hDevice = CreateFile(DeviceName,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (IsCreate())
{
Print("Device open");
char Buff[255];
DWORD D = DeviceIOCTRL(FunctionGetVersion,Buff,0,255);
Print("Version:");
Print(Buff);
}
else
Print("Error:device not open");
}
void TFireWall::DeviceDisconnect()
{
CloseHandle(hDevice);
hDevice=NULL;
Print("Device close");
}
TFireWall::~TFireWall()
{
Log=NULL;
DeviceDisconnect();
}
TFireWall::TFireWall(char* DeviceName,TStrings* Log)
{
TFireWall::Log = Log;
DeviceConnect(DeviceName);
if (!IsCreate()) return;
}
#pragma package(smart_init)
void IPToStr(DWORD IP,char *Str)
{
sprintf(Str,"%i.%i.%i.%i",(IP>>0)&0xFF,(IP>>8)&0xFF,(IP>>16)&0xFF,(IP>>24)&0xFF);
}
bool TFireWall::GetTypesSize(LPTypesSize TS)
{
DWORD Data=DeviceIOCTRL(FunctionStructSize,TS,0,sizeof(TTypesSize));
return(Data==sizeof(TTypesSize));
}
bool TFireWall::GetQueryPacket(LPQueryPacket Packet)
{
DWORD Data=DeviceIOCTRL(FunctionGetQueryPacket,Packet,0,sizeof(TQueryPacket));
return(Data==sizeof(TQueryPacket));
}
INT TFireWall::GetQP_PacketDataSize(INT ID)
{
DWORD SizeID = ID;
DWORD Data=DeviceIOCTRL(FunctionGetQP_PacketDataSize,&SizeID,sizeof(INT),sizeof(INT));
if (Data!=sizeof(INT)) SizeID=-1;
return SizeID;
}
void TFireWall::GetQP_PacketData(INT ID,void* Data,int Size)
{
DWORD *SizeID = (DWORD *)Data;
*SizeID = ID;
DWORD Data1=DeviceIOCTRL(FunctionGetQP_PacketData,Data,sizeof(DWORD),Size);
return;
}
void TFireWall::QP_PacketOK(INT ID)
{
DWORD SizeID = ID;
// DWORD Data=DeviceIOCTRL(FunctionQP_PacketOK,&SizeID,sizeof(INT),0);
}
void TFireWall::QP_PacketDel(INT ID)
{
DWORD SizeID = ID;
DWORD Data=DeviceIOCTRL(FunctionQP_PacketDel,&SizeID,sizeof(INT),0);
}
void TFireWall::GetFD(FD* fd)
{
DWORD Data=DeviceIOCTRL(FunctionGetFD,fd,sizeof(FD),sizeof(FD));
}
void TFireWall::SetTableHTTPHost(char* Hosts,int Size)
{
DWORD Data=DeviceIOCTRL(FunctionWriteTableHTTPHost,Hosts,Size,0);
}
void TFireWall::SetTableHTTPURL(char* URLs,int Size)
{
DWORD Data=DeviceIOCTRL(FunctionWriteTableHTTPURL,URLs,Size,Size);
}
void TFireWall::WritePraviloTable(TPraviloIP *TablePravilo,int NumPravilo)
{
DWORD Data=DeviceIOCTRL(FunctionWritePravilo,TablePravilo,sizeof(TPraviloIP)*NumPravilo,0);
}
void TFireWall::ReadPraviloTable(TPraviloIP *TablePravilo,int &NumPravilo,int MaxPravilo)
{
NumPravilo = DeviceIOCTRL(FunctionReadPravilo,TablePravilo,0,sizeof(TPraviloIP)*MaxPravilo)/sizeof(TPraviloIP);
}
Листинг А.3 Файл «HTTPFilter»
//#include <vcl.h>
#pragma hdrstop
#include "HTTPFilter.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormHTTPFiler *FormHTTPFiler;
//---------------------------------------------------------------------------
__fastcall TFormHTTPFiler::TFormHTTPFiler(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormHTTPFiler::HostAddClick(TObject *Sender)
{
HostList->AddItem(HostText->Text,NULL);
}
//---------------------------------------------------------------------------
void __fastcall TFormHTTPFiler::HostDelClick(TObject *Sender)
{
HostList->Items->Delete(HostList->ItemIndex);
}
//---------------------------------------------------------------------------
void __fastcall TFormHTTPFiler::HostSetClick(TObject *Sender)
{
int N = HostList->Count;
int Size=0;
for(int n=0;n<N;n++)
Size+=HostList->Items->Strings[n].Length()+1;
Size+=1;
char *Str=new char[Size];
int S = 0;
for(int n=0;n<N;n++)
{
int Len = HostList->Items->Strings[n].Length();
for(int i=0;i<Len;i++)
Str[S++] = HostList->Items->Strings[n][i+1];
Str[S++] = 0;
}
Str[S++] = 0;
MainForm->FW->SetTableHTTPHost(Str,Size);
}
//---------------------------------------------------------------------------
void ShowHTTPFilter()
{
if (!FormHTTPFiler)FormHTTPFiler = new TFormHTTPFiler(Application);
FormHTTPFiler->Show();
}
void __fastcall TFormHTTPFiler::Button1Click(TObject *Sender)
{
URLList->AddItem(URLText->Text,NULL);
}
//---------------------------------------------------------------------------
void __fastcall TFormHTTPFiler::Button2Click(TObject *Sender)
{
URLList->Items->Delete(URLList->ItemIndex);
}
//---------------------------------------------------------------------------
void __fastcall TFormHTTPFiler::Button3Click(TObject *Sender)
{
int N = URLList->Count;
int Size=0;
for(int n=0;n<N;n++)
Size+=URLList->Items->Strings[n].Length()+1;
Size+=1;
char *Str=new char[Size];
int S = 0;
for(int n=0;n<N;n++)
{
int Len = URLList->Items->Strings[n].Length();
for(int i=0;i<Len;i++)
Str[S++] = URLList->Items->Strings[n][i+1];
Str[S++] = 0;
}
Str[S++] = 0;
MainForm->FW->SetTableHTTPURL(Str,Size);
}
//---------------------------------------------------------------------------
Листинг А.4 Файл «LogFormEx.cpp»
#include <vcl.h>
#pragma hdrstop
#include "stdio.h"
#include "unit1.h"
#include "LogFormEx.h"
#include "STRUCT.h"
#include "MMSystem.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TLogForm2 *LogForm2;
void ShowLogForm()
{
if (!LogForm2) LogForm2 = new TLogForm2(Application);
LogForm2->Show();
}
//---------------------------------------------------------------------------
__fastcall TLogForm2::TLogForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TLogForm2::FormCreate(TObject *Sender)
{
LogTable->ColCount = 11;
NLog=1;
LogTable->RowCount = 2;
LogTable->FixedRows = 1;
LogTable->Cells[0][0] = "IP Отправителя";
LogTable->Cells[1][0] = "IP Получателя";
LogTable->Cells[2][0] = "Направление";
LogTable->Cells[3][0] = "Протокол";
LogTable->Cells[4][0] = "Действие";
LogTable->Cells[5][0] = "Локальный порт";
LogTable->Cells[6][0] = "Удалённый порт";
LogTable->Cells[7][0] = "Размер пакета";
LogTable->Cells[8][0] = "Размер данных";
LogTable->Cells[9][0] = "Правило ID" ;
LogTable->Cells[10][0] = "Время" ;
// LogTable->Canvas->TextWidth()
for(int i=0;i<LogTable->ColCount;i++) LogTable->ColWidths[i]=LogTable->Canvas->TextWidth(LogTable->Cells[i][0])+8;
LogTable->ColWidths[10]=LogTable->Canvas->TextWidth("30.12.2007 24:59:59.999")+8;
// strlen(LogTable->Cells[i][0].c_str())*LogTable->Font->Size;
FormResize(NULL);
}
//---------------------------------------------------------------------------
void WriteLog(FILE*F,
char* IpSrc,char* IpDest,
char* Direct, char* ProtocolName,char* Action,
char* PortDest,char* PortSrc,
char* SizePacket,char* SizeData,
char* PraviloID, char* Time)
{
char Buff[256];
sprintf(Buff,"|%15.15s|%15.15s|%11.11s|%8.8s|%8.8s|%14.14s|%14.14s|%13.13s|%13.13s|%10.10s|%24.24s|\r\n",IpSrc,IpDest,Direct,ProtocolName,Action,PortDest,PortSrc,SizePacket,SizeData,PraviloID,Time);
fwrite(Buff,strlen(Buff),1,F);
}
void __fastcall TLogForm2::Timer1Timer(TObject *Sender)
{
Firewall_Log Log;
bool Redraw = false;
char Str[255];
FILE * F = fopen("MyFireWall.log","rb+");
if (!F)
{
F = fopen("MyFireWall.log","wb+");
char* A="------------------------";
WriteLog(F,A,A,A,A,A,A,A,A,A,A,A);
// WriteLog(F, LogTable->Cells[0][0].c_str(),LogTable->Cells[1][0].c_str(),
// LogTable->Cells[2][0].c_str(),LogTable->Cells[3][0].c_str(),
// LogTable->Cells[4][0].c_str(),LogTable->Cells[5][0].c_str(),
// LogTable->Cells[6][0].c_str(),LogTable->Cells[7][0].c_str(),
// LogTable->Cells[8][0].c_str(),LogTable->Cells[9][0].c_str(),
// LogTable->Cells[10][0].c_str());
WriteLog(F,A,A,A,A,A,A,A,A,A,A,A);
}
fseek(F,0,SEEK_END);
for(;MainForm->FW->ReadLog(&Log);)
{
LogTable->RowCount = NLog+1;
// FrmTable1->Table.SetCountRow(NLog+1);
SYSTEMTIME ST;
FileTimeToSystemTime((FILETIME*)(&Log.Time.QuadPart),&ST);
sprintf(Str,"%i.%i.%i %i:%i:%i.%i",ST.wDay,ST.wMonth,ST.wYear, ST.wHour,ST.wMinute,ST.wSecond,ST.wMilliseconds);
// LogTable->Cells[10][NLog] = IntToHex(Log.Time.QuadPart,16);
LogTable->Cells[10][NLog] = Str;
IPToStr(Log.hIP.iph_src,Str);
LogTable->Cells[0][NLog] = Str;
IPToStr(Log.hIP.iph_dest,Str);
LogTable->Cells[1][NLog] = Str;
LogTable->Cells[2][NLog] = DirectionalName(Log.Direction);















