unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, ComCtrls, IniFiles, Snoop, Winsock;

type
  TMainForm = class(TForm)
    Panel1: TPanel;
    cbxAdapter: TComboBox;
    btnOpen: TButton;
    btnClose: TButton;
    btnClear: TButton;
    ListView1: TListView;
    SnoopIP1: TSnoopIP;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure cbxAdapterChange(Sender: TObject);
    procedure btnOpenClick(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure btnClearClick(Sender: TObject);
    procedure SnoopIP1CaptureIP(Sender: TObject; IPHeader: PIP_RHDR;
	  Data: PChar);
  private
    { Private declarations }
    procedure LoadControl;
    procedure SaveControl;
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TMainForm.LoadControl;
var
	IniFile: TIniFile;
begin
	IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'SnoopIPTest.ini');
	Left := IniFile.ReadInteger('Coord', 'Left', 0);
	Top := IniFile.ReadInteger('Coord', 'Top', 0);
	Width := IniFile.ReadInteger('Coord', 'Width', 640);
	Height := IniFile.ReadInteger('Coord', 'Height', 480);
	ListView1.Height := IniFile.ReadInteger('Coord', 'ListViewHeight', 220);
	cbxAdapter.ItemIndex := IniFile.ReadInteger('Option', 'AdapterIndex', 0);
	IniFile.Free;
end;

procedure TMainForm.SaveControl;
var
	IniFile: TIniFile;
begin
	IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'SnoopIPTest.ini');
	try
		IniFile.WriteInteger('Coord', 'Left', Left);
		IniFile.WriteInteger('Coord', 'Top', Top);
		IniFile.WriteInteger('Coord', 'Width', Width);
		IniFile.WriteInteger('Coord', 'Height', Height);
		IniFile.WriteInteger('Coord', 'ListViewHeight', ListView1.Height);
		IniFile.WriteInteger('Option', 'AdapterIndex', cbxAdapter.ItemIndex);
	except
		IniFile.Free;
	end;
end;



procedure TMainForm.FormCreate(Sender: TObject);
begin
	cbxAdapter.Items := SnoopIP1.AdapterNames;
	LoadControl;
	SnoopIP1.AdapterIndex := cbxAdapter.ItemIndex;
end;

procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
	SaveControl;
end;

procedure TMainForm.cbxAdapterChange(Sender: TObject);
begin
	SnoopIP1.AdapterIndex := cbxAdapter.ItemIndex;
end;

procedure TMainForm.btnOpenClick(Sender: TObject);
begin
	SnoopIP1.Open;
	btnOpen.Enabled := false;
	btnClose.Enabled := true;
	cbxAdapter.Enabled := false;
end;

procedure TMainForm.btnCloseClick(Sender: TObject);
begin
	SnoopIP1.Close;
	btnOpen.Enabled := true;
	btnClose.Enabled := false;
	cbxAdapter.Enabled := true;
end;

procedure TMainForm.btnClearClick(Sender: TObject);
begin
	ListView1.Items.Clear;
end;

procedure TMainForm.SnoopIP1CaptureIP(Sender: TObject; IPHeader: PIP_RHDR;
  Data: PChar);
var
	ListItem: TListItem;
begin
	ListItem := ListView1.Items.Add;
	// IP Header Information
	ListItem.Caption := IntToStr(IPHeader.VerLen);
	ListItem.SubItems.Add(IntToStr(IPHeader.Service));
	ListItem.SubItems.Add(IntToStr(htons(IPHeader.Length)));
	ListItem.SubItems.Add(IntToStr(ntohs(IPHeader.Ident)));
	ListItem.SubItems.Add(IntToHex(ntohs(IPHeader.FlagOff), 4));
	ListItem.SubItems.Add(IntToStr(ntohs(IPHeader.TimeLive)));
	ListItem.SubItems.Add(IntToHex(ntohs(IPHeader.Protocol), 4));
	ListItem.SubItems.Add(IntToStr(ntohs(IPHeader.Checksum)));
	ListItem.SubItems.Add(IP2Str(ntohl(IPHeader.Source)));
	ListItem.SubItems.Add(IP2Str(ntohl(IPHeader.Destination)));
end;



end.
