⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 capturedata_u.pas

📁 dekphi下的封包控件
💻 PAS
字号:
unit CaptureData_u;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cap_ip, Grids, ExtCtrls, StdCtrls, DB, DBClient, Buttons;

type
  Tmy_data=record
    buf:array of char;
  end;
  TCaptureData_F = class(TForm)
    Panel1: TPanel;
    grdDataList: TStringGrid;
    Memo1: TMemo;
    Memo2: TMemo;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    edtPort1: TEdit;
    Label4: TLabel;
    edtPort2: TEdit;
    Label5: TLabel;
    cbbProtocol: TComboBox;
    cap_ip1: Tcap_ip;
    SaveDialog1: TSaveDialog;
    edtAddr1: TEdit;
    edtAddr2: TEdit;
    btnStart: TBitBtn;
    btnStop: TBitBtn;
    procedure grdDataListDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure FormCreate(Sender: TObject);
    procedure cap_ip1Cap(ip, proto, sourceIP, destIP, SourcePort,
      DestPort: String; header: PAnsiChar; header_size: Integer;
      data: PAnsiChar; data_size: Integer);
    procedure btnStartClick(Sender: TObject);
    procedure btnStopClick(Sender: TObject);
    procedure grdDataListClick(Sender: TObject);
  private
    { Private declarations }
  public
    buf_list:array of Tmy_data;
    function checkFilter(proto, sourceIP, destIP, SourcePort,DestPort: String):Boolean;
    { Public declarations }
  end;

var
  CaptureData_F: TCaptureData_F;

implementation

{$R *.dfm}

procedure TCaptureData_F.grdDataListDrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  i: integer;
begin
  with grdDataList.Canvas do
  begin
    if ARow = 0 then
    begin
      Brush.Color := rgb(128, 128, 128);
      FillRect(Rect);
      Pen.Color := rgb(0, 0, 0);
      for i := 0 to ((Rect.Bottom - Rect.Top) div 3) do
      begin
        MoveTo(Rect.Left, i * 3 + Rect.Top);
        LineTo(Rect.Right, i * 3 + Rect.Top);
      end;
      Brush.Style := bsclear;
      Font.Color := rgb(0, 0, 0);
      TextOut(Rect.Left + 3, Rect.Top + 3, grdDataList.Cells[ACol, ARow]);
      Font.Color := rgb(255, 255, 255);
      TextOut(Rect.Left + 2, Rect.Top + 2, grdDataList.Cells[ACol, ARow]);
      Brush.Style := bssolid;
    end
    else
    begin
      Brush.Style := bsSolid;
      Brush.Color := RGB(0, 0, 0);
      FillRect(rect);
      if grdDataList.Cells[4, ARow] <> '脱机' then
        Font.Color := clLime
      else
        Font.Color := clred;
      TextOut(Rect.Left + 3, Rect.Top + 3, grdDataList.Cells[ACol, ARow]);
    end;
  end;
end;

procedure TCaptureData_F.FormCreate(Sender: TObject);
begin
  grdDataList.Cells[0,0]:='Protocol';
  grdDataList.Cells[1,0]:='Source Addr';
  grdDataList.Cells[2,0]:='Source Port';
  grdDataList.Cells[3,0]:='Dest Addr';
  grdDataList.Cells[4,0]:='Dest Port';
  grdDataList.Cells[5,0]:='Data Size';
  grdDataList.Cells[6,0]:='Data';
  setlength(buf_list,200);
end;

procedure TCaptureData_F.cap_ip1Cap(ip, proto, sourceIP, destIP,
  SourcePort, DestPort: String; header: PAnsiChar; header_size: Integer;
  data: PAnsiChar; data_size: Integer);
//var
  //lStream:TMemoryStream;
begin
  if checkFilter(proto,sourceIP,destIP,SourcePort,DestPort) then
    exit;
//  cdCapture.Append;
//  cdCapture.Fields.Fields[0].AsString:=Proto;
//  cdCapture.Fields.Fields[1].AsString:=sourceIP;
//  cdCapture.Fields.Fields[2].AsString:=SourcePort;
//  cdCapture.Fields.Fields[3].AsString:=destIP;
//  cdCapture.Fields.Fields[4].AsString:=DestPort;
//  cdCapture.Fields.Fields[5].AsInteger:=data_size;
//  lStream:=TMemoryStream.Create;
//  lStream.Write(Data^,data_size);
//  lStream.Position:=0;
//  cdCapturecfData.LoadFromStream(lStream);
//  cdCapture.Post;
//  lStream.Free;
  with grdDataList do
   begin
    Cells[0,grdDataList.RowCount-1]:=proto;
    Cells[1,grdDataList.RowCount-1]:=sourceIP;
    Cells[2,grdDataList.RowCount-1]:=SourcePort;
    Cells[3,grdDataList.RowCount-1]:=destIP;
    Cells[4,grdDataList.RowCount-1]:=DestPort;
    Cells[5,grdDataList.RowCount-1]:=inttostr(data_size);
    Cells[6,grdDataList.RowCount-1]:=data;
   end;
   setlength(buf_list[grdDataList.RowCount-2].buf,data_size);
   copymemory(buf_list[grdDataList.RowCount-2].buf,data,data_size);
 if grdDataList.RowCount>200 then
    grdDataList.RowCount:=2
   else
   begin
    grdDataList.RowCount:=grdDataList.RowCount+1;
    grdDataList.Rows[grdDataList.RowCount].Clear;
   end;

 grdDataList.toprow:=grdDataList.RowCount- grdDataList.VisibleRowCount;
end;

procedure TCaptureData_F.btnStartClick(Sender: TObject);
begin
//  ShowMessage(edtAddr1.Text);

  grdDataList.RowCount:=2;
  grdDataList.Rows[grdDataList.RowCount].Clear;
  cap_ip1.StartCap;
  btnStart.Enabled:=false;
  btnstop.Enabled:=true;
  label1.Enabled:=false;
  label2.Enabled:=false;
  label3.Enabled:=false;
  label4.Enabled:=false;
  label5.Enabled:=false;
  edtAddr1.Enabled:=false;
  edtAddr2.Enabled:=false;
  edtPort1.Enabled:=false;
  edtport2.Enabled:=false;
  cbbProtocol.Enabled:=false;
end;

procedure TCaptureData_F.btnStopClick(Sender: TObject);
begin
  cap_ip1.StopCap;
  btnstart.Enabled:=true;
  btnstop.Enabled:=false;
  label1.Enabled:=true;
  label2.Enabled:=true;
  label3.Enabled:=true;
  label4.Enabled:=true;
  label5.Enabled:=true;
  edtAddr1.Enabled:=true;
  edtAddr2.Enabled:=true;
  edtPort1.Enabled:=true;
  edtport2.Enabled:=true;
  cbbProtocol.Enabled:=true;
end;

procedure TCaptureData_F.grdDataListClick(Sender: TObject);
var text_str,hex_str,all_str:string;
    i:integer;
    no: Integer;
begin
  memo1.lines.Clear;
  memo2.lines.Clear;
  text_str:='';hex_str:='';all_str:='';
  i:=0;
  while i<= high(buf_list[grdDataList.Selection.Top-1].buf) do
   begin
     no:=ord(buf_list[grdDataList.Selection.Top-1].buf[i]);
     hex_str:=hex_str+format('%0.2x',[no])+' ';
     if no<20 then
      begin
        text_str:=text_str+'.';
        all_str:=all_str+'.';
      end else
      begin
        text_str:=text_str+buf_list[grdDataList.Selection.Top-1].buf[i];
        all_str:=all_str+buf_list[grdDataList.Selection.Top-1].buf[i];
      end;

     if ((i mod 8)=7) then
      begin
       memo1.lines.add(hex_str+'  |  '+text_str);
       text_str:='';hex_str:='';
      end;
     inc(i);
   end;
 if hex_str<>'' then
   memo1.lines.add(hex_str+format('%'+inttostr(24-length(hex_str))+'s',[' '])+'  |  '+text_str);

  memo2.lines.Add(all_str);
end;

function TCaptureData_F.checkFilter(proto, sourceIP, destIP, SourcePort,
  DestPort: String): Boolean;
var
  lUsed:Byte;
begin
  Result:=true;
  if (trim(cbbProtocol.Text)='') and (EdtAddr1.Text='0.0.0.0') and (EdtAddr2.Text='0.0.0.0') and
    (StrToIntDef(edtPort1.Text,0)=0) and (StrToIntDef(EdtPort2.Text,0)=0) then
  begin
    Result:=false;
    exit;
  end;
  if trim(cbbProtocol.Text)<>'' then
    if Proto<>cbbProtocol.Text then
      exit;
  lUsed:=0;
  if edtaddr1.Text<>'0.0.0.0' then
    lUsed:=lUsed or 1;
  if StrToIntDef(EdtPort1.Text,0)<>0 then
    lUsed:=lUsed or 2;
  if edtAddr2.Text<>'0.0.0.0' then
    lUsed:=lused or 4;
  if StrtoIntDef(EdtPort2.Text,0)<>0 then
    lUsed:=lused or 8;
  case lUsed of    //
    1:
      if (sourceIP<>edtAddr1.Text) and (destIP<>edtAddr1.Text) then
        exit;
    2:
      if (SourcePort<>edtPort1.Text) and (DestPort<>edtPort1.Text) then
        exit;
    3:
      if not (((sourceIP=edtAddr1.Text) and (SourcePort=edtPort1.Text)) or ((destIP=edtAddr1.Text)  and (DestPort=edtPort1.Text))) then
        exit;        
    4:
      if (sourceIP<>edtAddr2.Text) and (destIP<>edtAddr2.Text) then
        exit;
    5:
      if not (((sourceIP=edtAddr1.Text) and (destIP=edtAddr2.Text)) or ((sourceIP=edtAddr2.Text) and (destIP=edtAddr1.Text))) then
        exit;        
    6:
      if not (((SourcePort=EdtPort1.Text) and (DestIP=EdtAddr2.Text)) or ((DestPort=EdtPort1.Text) and (SourceIP=edtAddr2.Text))) then
        exit;        
    7:
      if not (((SourceIP=EdtAddr1.Text) and (SourcePort=EdtPort1.Text) and (DestIP=edtAddr2.Text)) or
         ((DestIP=EdtAddr1.Text) and (DestPort=EdtPort1.Text) and (SourceIP=EdtAddr2.Text))) then
        exit;
    8:
      if (SourcePort<>edtPort2.Text) and (DestPort<>edtPort2.Text) then
        exit;
    9:
      if not (((SourcePort=EdtPort2.Text) and (DestIP=EdtAddr1.Text)) or ((DestPort=EdtPort2.Text) and (SourceIP=edtAddr1.Text))) then
        exit;        
    10:
      if not (((SourcePort=EdtPort1.Text) and (DestPort=EdtPort2.Text)) or ((DestPort=EdtPort1.Text) and (SourcePort=EdtPort2.Text))) then
        exit;        
    11:
      if not (((SourceIP=EdtAddr1.Text) and (SourcePort=EdtPort1.Text) and (DestPort=edtPort2.Text)) or
         ((DestIP=EdtAddr1.Text) and (DestPort=EdtPort1.Text) and (SourcePort=EdtPort2.Text))) then
        exit;
    12:
      if not (((sourceIP=edtAddr2.Text) and (SourcePort=edtPort2.Text)) or ((destIP=edtAddr2.Text)  and (DestPort=edtPort2.Text))) then
        exit;        
    13:
      if not (((SourceIP=EdtAddr2.Text) and (SourcePort=EdtPort2.Text) and (DestIP=edtAddr1.Text)) or
         ((DestIP=EdtAddr2.Text) and (DestPort=EdtPort2.Text) and (SourceIP=EdtAddr1.Text))) then
        exit;
    14:
      if not (((SourceIP=EdtAddr2.Text) and (SourcePort=EdtPort2.Text) and (DestPort=edtPort1.Text)) or
         ((DestIP=EdtAddr2.Text) and (DestPort=EdtPort2.Text) and (SourcePort=EdtPort1.Text))) then
        exit;
    15:
      if not (((SourceIP=EdtAddr1.Text) and (SourcePort=EdtPort1.Text) and (DestIP=EdtAddr2.Text) and (DestPort=EdtPort2.Text)) or
        ((SourceIP=EdtAddr2.Text) and (SourcePort=EdtPort2.Text) and (destIP=EdtAddr1.Text) and (DestPort=EdtPort1.Text))) then
        exit;        
  end;    // case
  Result:=false;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -