Release 6

git-svn-id: https://svn.code.sf.net/p/synalist/code/trunk@13 7c85be65-684b-0410-a082-b2ed4fbef004
This commit is contained in:
geby
2008-04-23 20:32:54 +00:00
parent 9ff57d68f5
commit a077e1a0cc
5 changed files with 544 additions and 122 deletions

201
asn1util.pas Normal file
View File

@@ -0,0 +1,201 @@
{==============================================================================|
| Project : Delphree - Synapse | 001.000.000 |
|==============================================================================|
| Content: support for ASN.1 coding and decoding |
|==============================================================================|
| The contents of this file are subject to the Mozilla Public License Ver. 1.0 |
| (the "License"); you may not use this file except in compliance with the |
| License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ |
| |
| Software distributed under the License is distributed on an "AS IS" basis, |
| WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for |
| the specific language governing rights and limitations under the License. |
|==============================================================================|
| The Original Code is Synapse Delphi Library. |
|==============================================================================|
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
| Portions created by Lukas Gebauer are Copyright (c) 1999, 2000. |
| Portions created by Hernan Sanchez are Copyright (c) 2000. |
| All Rights Reserved. |
|==============================================================================|
| Contributor(s): |
| Hernan Sanchez (hernan.sanchez@iname.com) |
|==============================================================================|
| History: see HISTORY.HTM from distribution package |
| (Found at URL: http://www.mlp.cz/space/gebauerl/synapse/) |
|==============================================================================}
unit ASN1Util;
interface
uses
SysUtils, SynaUtil;
const
ASN1_INT = $02;
ASN1_OCTSTR = $04;
ASN1_NULL = $05;
ASN1_OBJID = $06;
ASN1_SEQ = $30;
ASN1_IPADDR = $40;
ASN1_COUNTER = $41;
ASN1_GAUGE = $42;
ASN1_TIMETICKS = $43;
function ASNEncLen(Len: integer): string;
function ASNDecLen(var Start: integer; Buffer: string): integer;
function ASNEncInt(Len: integer): string;
function ASNObject(Data: string; ASNType: integer): string;
function ASNItem(var Start: integer; Buffer: string): string;
implementation
function ASNEncLen(Len: integer): string;
var
x, y: integer;
begin
if (Len < $80) then
Result := Char(Len)
else
if (Len < $FF) then
Result := Char($81) + Char(Len)
else
begin
x := Len div $FF;
y := Len mod $FF;
Result := Char($82) + Char(x) + Char(y);
end;
end;
function ASNDecLen(var Start: integer; Buffer: string): integer;
var
x: integer;
begin
x := Ord(Buffer[Start]);
if (x < $80) then
begin
Inc(Start);
Result := x;
end
else
if (x = $81) then
begin
Inc(Start);
Result := Ord(Buffer[Start]);
Inc(Start);
end
else
begin
Inc(Start);
x := Ord(Buffer[Start]);
Inc(Start);
Result := x * $FF + Ord(Buffer[Start]);
Inc(Start);
end;
end;
function ASNEncInt(Len: integer): string;
var
j, y: integer;
begin
Result := '';
j := 0;
y := Len div $FFFFFF;
Len := Len - (y * $FFFFFF);
if ((y > 0) or (j = 1)) then
begin
j := 1;
Result := Result + Char(y);
end;
y := Len div $FFFF;
Len := Len - (y * $FFFF);
if ((y > 0) or (j = 1)) then
begin
j := 1;
Result := Result + Char(y);
end;
y := Len div $FF;
Len := Len - (y * $FF);
if ((y > 0) or (j = 1)) then
Result := Result + Char(y);
Result := Result + Char(Len);
end;
function ASNObject(Data: string; ASNType: integer): string;
begin
Result := Char(ASNType) + ASNEncLen(Length(Data)) + Data;
end;
function ASNItem(var Start: integer; Buffer: string): string;
var
ASNType: integer;
ASNSize: integer;
y, n: integer;
s: string;
c: char;
begin
ASNType := Ord(Buffer[Start]);
Inc(start);
ASNSize := ASNDecLen(Start, Buffer);
Result := '';
if ((ASNType and $20) > 0) then
begin
Result := '$' + IntToHex(ASNType, 2);
end
else
case ASNType of
ASN1_INT, ASN1_COUNTER, ASN1_GAUGE, ASN1_TIMETICKS:
begin
y := 0;
for n:=1 to ASNSize do
begin
y := y * 256 + Ord(Buffer[Start]);
Inc(Start);
end;
Result := IntToStr(y);
end;
ASN1_OCTSTR, $44:
begin
for n:=1 to ASNSize do
begin
c := Char(Buffer[Start]);
Inc(Start);
s := s + c;
end;
Result := s;
end;
ASN1_OBJID:
begin
for n:=1 to ASNSize do
begin
c := Char(Buffer[Start]);
Inc(Start);
s := s + c;
end;
Result := IdToMib(s);
end;
ASN1_IPADDR:
begin
s:='';
for n:=1 to ASNSize do
begin
if (n<>1) then
s := s + '.';
y := Ord(Buffer[Start]);
Inc(Start);
s := s + IntToStr(y);
end;
Result := s;
end;
else // NULL
begin
Result := '';
Inc(Start);
Start := Start + ASNSize;
end;
end;
end;
end.

View File

@@ -93,7 +93,6 @@ end;
{get}
function get(Host,URI:string;Response:TStrings):Boolean;
var
s:string;
HTTP:THTTPSend;
Query:TStringList;
begin

View File

@@ -1,5 +1,5 @@
{==============================================================================|
| Project : Delphree - Synapse | 001.000.000 |
| Project : Delphree - Synapse | 001.001.000 |
|==============================================================================|
| Content: SNMP client |
|==============================================================================|
@@ -28,7 +28,7 @@ unit SNMPSend;
interface
uses
BlckSock, synautil, classes, sysutils;
BlckSock, synautil, classes, sysutils, ASN1util;
const
@@ -123,104 +123,21 @@ var
Pos:integer;
endpos:integer;
sm,sv:string;
function ASNlen(var start:integer):integer;
var
x:integer;
begin
x:=ord(buffer[start]);
if x>$80 then
begin
inc(start);
x:=x and $7f;
x:=x*$80;
x:=x+ord(buffer[start]);
end;
inc(start);
result:=x;
end;
function ASNitem (var start:integer):string;
var
ASNType:integer;
ASNSize:integer;
y,n:integer;
s:string;
c:char;
begin
ASNType:=Ord(Buffer[start]);
Inc(start);
ASNSize:=ASNLen(start);
Result:='';
if (ASNType and $20)>0 then
begin
Result:='$'+IntToHex(ASNType,2);
end
else
case ASNType of
2, $41, $42, $43: begin //integer
y:=0;
for n:=1 to ASNsize do
begin
y:=y*256+ord(buffer[start]);
inc(start);
end;
result:=inttostr(y);
end;
4, $44: begin //string
for n:=1 to ASNSize do
begin
c:=char(buffer[start]);
inc(start);
s:=s+c;
end;
Result:=s;
end;
6: begin //OID
for n:=1 to ASNsize do
begin
c:=char(buffer[start]);
inc(start);
s:=s+c;
end;
result:=IdToMib(s);
end;
$40: begin //IP address
s:='';
for n:=1 to ASNsize do
begin
if n<>1 then
s:=s+'.';
y:=Ord(buffer[start]);
inc(start);
s:=s+IntToStr(y);
end;
result:=s;
end;
else //NULL
begin
Result:='';
inc(start);
start:=start+ASNSize;
end;
end;
end;
begin
Pos:=2;
Endpos:=ASNLen(Pos);
Self.version:=StrToIntDef(ASNItem(Pos),0);
Self.community:=ASNItem(Pos);
Self.PDUType:=StrToIntDef(ASNItem(Pos),0);
Self.ID:=StrToIntDef(ASNItem(Pos),0);
Self.ErrorStatus:=StrToIntDef(ASNItem(Pos),0);
Self.ErrorIndex:=StrToIntDef(ASNItem(Pos),0);
ASNItem(Pos);
Endpos:=ASNDecLen(Pos,buffer);
Self.version:=StrToIntDef(ASNItem(Pos,buffer),0);
Self.community:=ASNItem(Pos,buffer);
Self.PDUType:=StrToIntDef(ASNItem(Pos,buffer),0);
Self.ID:=StrToIntDef(ASNItem(Pos,buffer),0);
Self.ErrorStatus:=StrToIntDef(ASNItem(Pos,buffer),0);
Self.ErrorIndex:=StrToIntDef(ASNItem(Pos,buffer),0);
ASNItem(Pos,buffer);
while Pos<Endpos do
begin
ASNItem(Pos);
Sm:=ASNItem(Pos);
Sv:=ASNItem(Pos);
ASNItem(Pos,buffer);
Sm:=ASNItem(Pos,buffer);
Sv:=ASNItem(Pos,buffer);
Self.MIBadd(sm,sv);
end;
end;
@@ -230,23 +147,6 @@ function TSNMPRec.EncodeBuf:string;
var
data,s:string;
n:integer;
function ASNEncLen (len:integer):string;
var
x,y:integer;
begin
Result:='';
x:=len div $80;
y:=len mod $80;
if x>0 then Result:=char(x);
Result:=Result+char(y);
end;
function ASNObject (data:string;ASNType:integer):string;
begin
Result:=char(ASNType)+ASNEncLen(Length(data))+data;
end;
begin
data:='';
SyncMIB;

297
snmptrap.pas Normal file
View File

@@ -0,0 +1,297 @@
{==============================================================================|
| Project : Delphree - Synapse | 001.002.000 |
|==============================================================================|
| Content: SNMP traps |
|==============================================================================|
| The contents of this file are subject to the Mozilla Public License Ver. 1.0 |
| (the "License"); you may not use this file except in compliance with the |
| License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ |
| |
| Software distributed under the License is distributed on an "AS IS" basis, |
| WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for |
| the specific language governing rights and limitations under the License. |
|==============================================================================|
| The Original Code is Synapse Delphi Library. |
|==============================================================================|
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
| Portions created by Hernan Sanchez are Copyright (c) 2000. |
| All Rights Reserved. |
|==============================================================================|
| Contributor(s): |
| Hernan Sanchez (hernan.sanchez@iname.com) |
|==============================================================================|
| History: see HISTORY.HTM from distribution package |
| (Found at URL: http://www.mlp.cz/space/gebauerl/synapse/) |
|==============================================================================}
unit SNMPTrap;
interface
uses
Classes, SysUtils, BlckSock, SynaUtil, ASN1Util;
const
TRAP_PORT = 162;
SNMP_VERSION = 0;
PDU_GET = $A0;
PDU_GETN = $A1;
PDU_RESP = $A2;
PDU_SET = $A3;
PDU_TRAP = $A4;
type
TTrapPDU = class(TObject)
private
protected
Buffer: string;
public
TrapPort: integer;
Version: integer;
PDUType: integer;
Community: string;
Enterprise: string;
TrapHost: string;
GenTrap: integer;
SpecTrap: integer;
TimeTicks: integer;
MIBOID: TStringList;
MIBValue: TStringList;
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure MIBAdd(MIB, Value: string);
procedure MIBDelete(Index: integer);
function MIBGet(MIB: string): string;
function EncodeTrap: integer;
function DecodeTrap: integer;
end;
TTrapSNMP = class(TObject)
private
sock: TUDPBlockSocket;
public
Trap: TTrapPDU;
SNMPHost: string;
Timeout: integer;
constructor Create;
destructor Destroy; override;
function Send: integer;
function Recv: integer;
end;
function SendTrap(Dest, Source, Enterprise, Community: string;
Generic, Specific, Seconds: integer; MIBName, MIBValue: TStringList): integer;
function RecvTrap(var Dest, Source, Enterprise, Community: string;
var Generic, Specific, Seconds: integer; var MIBName, MIBValue: TStringList): integer;
implementation
constructor TTrapPDU.Create;
begin
inherited Create;
MIBOID := TStringList.Create;
MIBValue := TStringList.Create;
TrapPort := TRAP_PORT;
Version := SNMP_VERSION;
PDUType := PDU_TRAP;
Community := 'public';
end;
destructor TTrapPDU.Destroy;
begin
MIBValue.Free;
MIBOID.Free;
inherited Destroy;
end;
procedure TTrapPDU.Clear;
begin
MIBOID.Clear;
MIBValue.Clear;
TrapPort := TRAP_PORT;
Version := SNMP_VERSION;
PDUType := PDU_TRAP;
Community := 'public';
end;
procedure TTrapPDU.MIBAdd(MIB, Value: string);
begin
MIBOID.Add(MIB);
MIBValue.Add(Value);
end;
procedure TTrapPDU.MIBDelete(Index: integer);
begin
MIBOID.Delete(Index);
MIBValue.Delete(Index);
end;
function TTrapPDU.MIBGet(MIB: string): string;
var
x: integer;
begin
x := MIBOID.IndexOf(MIB);
if (x < 0) then
Result := ''
else
Result := MIBValue[x];
end;
function TTrapPDU.EncodeTrap: integer;
var
s: string;
n: integer;
begin
Buffer := '';
for n:=0 to MIBOID.Count-1 do
begin
s := ASNObject(MibToID(MIBOID[n]), ASN1_OBJID)
+ ASNObject(MIBValue[n], ASN1_OCTSTR);
Buffer := Buffer + ASNObject(s, ASN1_SEQ);
end;
Buffer := ASNObject(Buffer, ASN1_SEQ);
Buffer := ASNObject(ASNEncInt(GenTrap), ASN1_INT)
+ ASNObject(ASNEncInt(SpecTrap), ASN1_INT)
+ ASNObject(ASNEncInt(TimeTicks), ASN1_TIMETICKS)
+ Buffer;
Buffer := ASNObject(MibToID(Enterprise), ASN1_OBJID)
+ ASNObject(IPToID(TrapHost), ASN1_IPADDR)
+ Buffer;
Buffer := ASNObject(Char(Version), ASN1_INT)
+ ASNObject(Community, ASN1_OCTSTR)
+ ASNObject(Buffer, Self.PDUType);
Buffer := ASNObject(Buffer, ASN1_SEQ);
Result := 1;
end;
function TTrapPDU.DecodeTrap: integer;
var
Pos, EndPos: integer;
Sm, Sv: string;
begin
Pos := 2;
EndPos := ASNDecLen(Pos, Buffer);
Version := StrToIntDef(ASNItem(Pos, Buffer), 0);
Community := ASNItem(Pos, Buffer);
PDUType := StrToIntDef(ASNItem(Pos, Buffer), PDU_TRAP);
Enterprise := IdToMIB(ASNItem(Pos, Buffer));
TrapHost := ASNItem(Pos, Buffer);
GenTrap := StrToIntDef(ASNItem(Pos, Buffer), 0);
Spectrap := StrToIntDef(ASNItem(Pos, Buffer), 0);
TimeTicks := StrToIntDef(ASNItem(Pos, Buffer), 0);
ASNItem(Pos, Buffer);
while (Pos < EndPos) do
begin
ASNItem(Pos, Buffer);
Sm := ASNItem(Pos, Buffer);
Sv := ASNItem(Pos, Buffer);
MIBAdd(Sm, Sv);
end;
Result := 1;
end;
constructor TTrapSNMP.Create;
begin
inherited Create;
Sock := TUDPBlockSocket.Create;
Trap := TTrapPDU.Create;
Timeout := 5;
SNMPHost := '127.0.0.1';
Sock.CreateSocket;
end;
destructor TTrapSNMP.Destroy;
begin
Trap.Free;
Sock.Free;
inherited Destroy;
end;
function TTrapSNMP.Send: integer;
begin
Trap.EncodeTrap;
Sock.Connect(SNMPHost, IntToStr(Trap.TrapPort));
Sock.SendBuffer(PChar(Trap.Buffer), Length(Trap.Buffer));
Result := 1;
end;
function TTrapSNMP.Recv: integer;
var
x: integer;
begin
Result := 0;
Sock.Connect(SNMPHost, IntToStr(Trap.TrapPort));
if Sock.CanRead(Timeout) then
begin
x := Sock.WaitingData;
if (x > 0) then
begin
SetLength(Trap.Buffer, x);
Sock.RecvBuffer(PChar(Trap.Buffer), x);
Trap.DecodeTrap;
Result := 1;
end;
end;
end;
function SendTrap(Dest, Source, Enterprise, Community: string;
Generic, Specific, Seconds: integer; MIBName, MIBValue: TStringList): integer;
var
SNMP: TTrapSNMP;
i: integer;
begin
SNMP := TTrapSNMP.Create;
try
SNMP.SNMPHost := Dest;
SNMP.Trap.TrapHost := Source;
SNMP.Trap.Enterprise := Enterprise;
SNMP.Trap.Community := Community;
SNMP.Trap.GenTrap := Generic;
SNMP.Trap.SpecTrap := Specific;
SNMP.Trap.TimeTicks := Seconds;
for i:=0 to (MIBName.Count - 1) do
SNMP.Trap.MIBAdd(MIBName[i], MIBValue[i]);
Result := SNMP.Send;
finally
SNMP.Free;
end;
end;
function RecvTrap(var Dest, Source, Enterprise, Community: string;
var Generic, Specific, Seconds: integer; var MIBName, MIBValue: TStringList):
integer;
var
SNMP: TTrapSNMP;
i: integer;
begin
SNMP := TTrapSNMP.Create;
try
SNMP.SNMPHost := Dest;
Result := SNMP.Recv;
if (Result <> 0) then
begin
Dest := SNMP.SNMPHost;
Source := SNMP.Trap.TrapHost;
Enterprise := SNMP.Trap.Enterprise;
Community := SNMP.Trap.Community;
Generic := SNMP.Trap.GenTrap;
Specific := SNMP.Trap.SpecTrap;
Seconds := SNMP.Trap.TimeTicks;
MIBName.Clear;
MIBValue.Clear;
for i:=0 to (SNMP.Trap.MIBOID.Count - 1) do
begin
MIBName.Add(SNMP.Trap.MIBOID[i]);
MIBValue.Add(SNMP.Trap.MIBValue[i]);
end;
end;
finally
SNMP.Free;
end;
end;
end.

View File

@@ -1,5 +1,5 @@
{==============================================================================|
| Project : Delphree - Synapse | 001.001.000 |
| Project : Delphree - Synapse | 001.002.000 |
|==============================================================================|
| Content: support procedures and functions |
|==============================================================================|
@@ -14,10 +14,12 @@
| The Original Code is Synapse Delphi Library. |
|==============================================================================|
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
| Portions created by Lukas Gebauer are Copyright (c) 1999. |
| Portions created by Lukas Gebauer are Copyright (c) 1999, 2000. |
| Portions created by Hernan Sanchez are Copyright (c) 2000. |
| All Rights Reserved. |
|==============================================================================|
| Contributor(s): |
| Hernan Sanchez (hernan.sanchez@iname.com) |
|==============================================================================|
| History: see HISTORY.HTM from distribution package |
| (Found at URL: http://www.mlp.cz/space/gebauerl/synapse/) |
@@ -40,6 +42,7 @@ procedure Dump (Buffer:string;DumpFile:string);
Function MibToId(mib:string):string;
Function IdToMib(id:string):string;
Function IntMibToStr(int:string):string;
function IPToID(Host: string): string;
implementation
@@ -115,9 +118,9 @@ function DeCodeInt(Value:string;Index:integer):word;
var
x,y:Byte;
begin
if Length(Value)<index then x:=Ord(Value[index])
if Length(Value)>index then x:=Ord(Value[index])
else x:=0;
if Length(Value)<(Index+1) then y:=Ord(Value[Index+1])
if Length(Value)>(Index+1) then y:=Ord(Value[Index+1])
else y:=0;
Result:=x*256+y;
end;
@@ -132,7 +135,7 @@ begin
Result:=true;
x:=0;
for n:=1 to Length(Value) do
if not (Value[n] in ['1'..'0','.'])
if not (Value[n] in ['0'..'9','.'])
then begin
Result:=False;
break;
@@ -153,11 +156,12 @@ begin
Result:='';
repeat
x:=LastDelimiter('.',Value);
Result:=Result+Copy(Value,x,Length(Value)-x);
Delete(Value,x,Length(Value)-x);
Result:=Result+'.'+Copy(Value,x+1,Length(Value)-x);
Delete(Value,x,Length(Value)-x+1);
until x<1;
if Length(Result)>0 then
if Value[1]='.' then Delete(Result, 1, 1);
if Result[1]='.' then
Delete(Result, 1, 1);
end;
{==============================================================================}
@@ -255,5 +259,26 @@ end;
{==============================================================================}
{IPToID} //Hernan Sanchez
function IPToID(Host: string): string;
var
s, t: string;
i, x: integer;
begin
Result := '';
for x:= 1 to 3 do
begin
t := '';
s := StrScan(PChar(Host), '.');
t := Copy(Host, 1, (Length(Host) - Length(s)));
Delete(Host, 1, (Length(Host) - Length(s) + 1));
i := StrTointDef(t, 0);
Result := Result + Chr(i);
end;
i := StrTointDef(Host, 0);
Result := Result + Chr(i);
end;
{==============================================================================}
end.