unit Unit1;

interface

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

type
  TMainForm = class(TForm)
    Snoop1: TSnoop;
    Panel1: TPanel;
    cbxAdapter: TComboBox;
    btnOpen: TButton;
    btnClose: TButton;
    ListView1: TListView;
    Button1: TButton;
	procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
	procedure cbxAdapterChange(Sender: TObject);
	procedure btnOpenClick(Sender: TObject);
	procedure btnCloseClick(Sender: TObject);
	procedure Button1Click(Sender: TObject);
	procedure Snoop1Capture(Sender: TObject; Header: PETHERNET_HDR;
	  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) + 'SnoopTest.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) + 'SnoopTest.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 := Snoop1.AdapterNames;
	LoadControl;
	Snoop1.AdapterIndex := cbxAdapter.ItemIndex;
end;

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

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

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

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

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

procedure TMainForm.Snoop1Capture(Sender: TObject; Header: PETHERNET_HDR;
  Data: PChar);
var
	ListItem: TListItem;
begin
	ListItem := ListView1.Items.Add;
	ListItem.Caption := Mac2Str(@Header.Source[0]);
	ListItem.SubItems.Add(Mac2Str(@Header.Destination[0]));
	ListItem.SubItems.Add(IntToHex(ntohs(Header.Protocol), 4));
end;




end.
