1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| unit UDPServerMain;
interface
uses
Windows, Classes, Messages, Graphics, Controls, Forms, Dialogs, IdWinsock2,
stdctrls, SysUtils, IdBaseComponent, IdAntiFreezeBase,
IdAntiFreeze, IdComponent, IdUDPBase, IdUDPClient, IdStack,
IdUDPServer, IdSocketHandle,IdGlobal, IdSNMP, ComCtrls, Grids,
StringGridToText, DBGrids;
type
TUDPMainForm = class(TForm)
IdSNMP1: TIdSNMP;
UDPServer1: TIdUDPServer;
IdAntiFreeze1: TIdAntiFreeze;
PageControl1: TPageControl;
Reception: TTabSheet;
DECODAGE: TTabSheet;
SourceGroupBox: TGroupBox;
HostNameLabel: TLabel;
HostAddressLabel: TLabel;
HostName: TLabel;
HostAddress: TLabel;
PortLabel: TLabel;
Port: TLabel;
BufferSizeLabel: TLabel;
BufferSize: TLabel;
UDPMemo: TMemo;
StringGridToText1: TStringGridToText;
Visu: TTabSheet;
DBGrid1: TDBGrid;
StatusBar1: TStatusBar;
procedure UDPServer1UDPRead(Sender: TObject; AData: TBytes;
ABinding: TIdSocketHandle);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
UDPMainForm: TUDPMainForm;
enterprise:String;
origOid: string;
lastpos:integer;
implementation
const
HOSTNAMELENGTH = 80;
{$R *.DFM}
procedure TUDPMainForm.FormCreate(Sender: TObject);
begin
HostAddress.Caption := GStack.LocalAddress;
Port.Caption := IntToStr(UDPServer1.DefaultPort);
BufferSize.Caption := IntToStr(UDPServer1.BufferSize);
UDPServer1.Active := True;
//HostName.Caption := IdSNMP1.Name;
HostName.Caption := GStack.HostName;
Port.Caption := IntToStr(IdSNMP1.TrapPort);
//BufferSize.Caption := IntToStr(IdSNMP1.BufferSize);
IdSNMP1.Community := 'public';
IdSNMP1.Trap.Enterprise := '1.3.6.1.4.1';
IdSNMP1.Trap.GenTrap := 6; //ENTERPRISESPECIFIC
IdSNMP1.Trap.SpecTrap := 1;
IdSNMP1.Trap.TimeTicks := 1000;
IdSNMP1.Trap.port:=162;
IdSNMP1.Trap.PDUType:=PDUTrap;
IdSNMP1.Active := True;
//IdSNMP1.Trap.MIBOID.LoadFromFile('MiBsodielec.txt');
lastpos:=0;
StringGridToText1.Cells[0,0]:='Adr IP'; StringGridToText1.Cells[1,0]:='Type';
StringGridToText1.Cells[2,0]:='Libellé';StringGridToText1.Cells[3,0]:='Date';
StringGridToText1.Cells[4,0]:='Heure';//StringGridToText1.Cells[7,0]:='Heure';
end;
procedure TUDPMainForm.UDPServer1UDPRead(Sender: TObject; AData: TBytes;
ABinding: TIdSocketHandle);
var
list:TStrings;
i,j :integer;
s,addstr,trav: String;
begin
list:=TStringlist.Create;
addstr:=String(Adata);
try
IdSNMP1.Trap.DecodeBuf(Addstr);
s:='';
UDPMemo.Lines.Add('Reçu '+inttostr(length(addstr))+
'car(s) de' + ABinding.PeerIP + ' :' +
IntToStr(ABinding.PeerPort)+' pos:'+intToStr(Lastpos));
list.Add(ABinding.PeerIP);
i:=0;
repeat
j:=i+lastpos;
//ma cuisine pour récupérer ce qui m'interesse
if Not(i in [0,3,4,7,8,9]) then begin
trav:=IdSNMP1.Trap.ValueOID[j];
if i=5 then
trav:=copy(Trav,7,2)+'/'+copy(Trav,5,2)+'/'+copy(Trav,1,4);
if i=6 then
trav:=copy(Trav,1,2)+':'+copy(Trav,3,2)+':'+copy(Trav,5,2);
s:=s+trav+',';list.Add(trav);
end;
inc(i);
Until j=IdSNMP1.trap.ValueCount;
lastpos:= IdSNMP1.trap.ValueCount ;
UDPMemo.Lines.Add(s);
StringGridToText1.Rows[StringGridToText1.RowCount-1]:=list;
StringGridToText1.RowCount:=StringGridToText1.RowCount+1;
finally
s:=''; addstr:='';
end;
end;
end. |
Partager