2008-04-23 23:28:55 +03:00
|
|
|
{==============================================================================|
|
2008-04-24 09:44:13 +03:00
|
|
|
| Project : Delphree - Synapse | 002.003.000 |
|
2008-04-23 23:28:55 +03:00
|
|
|
|==============================================================================|
|
|
|
|
| Content: SNMP client |
|
|
|
|
|==============================================================================|
|
|
|
|
| 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).|
|
2008-04-24 09:44:13 +03:00
|
|
|
| Portions created by Lukas Gebauer are Copyright (c)2000,2001. |
|
2008-04-23 23:28:55 +03:00
|
|
|
| All Rights Reserved. |
|
|
|
|
|==============================================================================|
|
|
|
|
| Contributor(s): |
|
2008-04-23 23:39:31 +03:00
|
|
|
| Jean-Fabien Connault (jfconnault@mail.dotcom.fr) |
|
2008-04-23 23:28:55 +03:00
|
|
|
|==============================================================================|
|
|
|
|
| History: see HISTORY.HTM from distribution package |
|
2008-04-23 23:48:39 +03:00
|
|
|
| (Found at URL: http://www.ararat.cz/synapse/) |
|
2008-04-23 23:28:55 +03:00
|
|
|
|==============================================================================}
|
|
|
|
|
|
|
|
unit SNMPSend;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2008-04-23 23:32:54 +03:00
|
|
|
BlckSock, synautil, classes, sysutils, ASN1util;
|
2008-04-23 23:28:55 +03:00
|
|
|
|
|
|
|
const
|
|
|
|
|
|
|
|
//PDU type
|
|
|
|
PDUGetRequest=$a0;
|
|
|
|
PDUGetNextRequest=$a1;
|
|
|
|
PDUGetResponse=$a2;
|
|
|
|
PDUSetRequest=$a3;
|
|
|
|
PDUTrap=$a4;
|
|
|
|
|
|
|
|
//errors
|
|
|
|
ENoError=0;
|
|
|
|
ETooBig=1;
|
|
|
|
ENoSuchName=2;
|
|
|
|
EBadValue=3;
|
|
|
|
EReadOnly=4;
|
|
|
|
EGenErr=5;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
2008-04-23 23:39:31 +03:00
|
|
|
TSNMPMib = class
|
|
|
|
OID: string;
|
|
|
|
Value: string;
|
|
|
|
ValueType: integer;
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:28:55 +03:00
|
|
|
TSNMPRec=class(TObject)
|
|
|
|
public
|
|
|
|
version:integer;
|
|
|
|
community:string;
|
|
|
|
PDUType:integer;
|
|
|
|
ID:integer;
|
|
|
|
ErrorStatus:integer;
|
|
|
|
ErrorIndex:integer;
|
2008-04-23 23:39:31 +03:00
|
|
|
SNMPMibList: TList;
|
2008-04-23 23:28:55 +03:00
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
2008-04-24 09:44:13 +03:00
|
|
|
function DecodeBuf(Buffer:string):boolean;
|
2008-04-23 23:28:55 +03:00
|
|
|
function EncodeBuf:string;
|
|
|
|
procedure Clear;
|
2008-04-23 23:46:58 +03:00
|
|
|
procedure MIBAdd(MIB,Value:string; ValueType:integer);
|
2008-04-23 23:28:55 +03:00
|
|
|
procedure MIBdelete(Index:integer);
|
|
|
|
function MIBGet(MIB:string):string;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TSNMPSend=class(TObject)
|
|
|
|
private
|
|
|
|
Sock:TUDPBlockSocket;
|
|
|
|
Buffer:string;
|
|
|
|
public
|
2008-04-23 23:39:31 +03:00
|
|
|
Timeout:integer;
|
|
|
|
Host:string;
|
2008-04-24 09:44:13 +03:00
|
|
|
HostIP:string;
|
2008-04-23 23:28:55 +03:00
|
|
|
Query:TSNMPrec;
|
|
|
|
Reply:TSNMPrec;
|
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
|
|
|
function DoIt:boolean;
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:39:31 +03:00
|
|
|
function SNMPget (Oid, Community, SNMPHost:string; var Value:string):Boolean;
|
2008-04-23 23:46:58 +03:00
|
|
|
function SNMPSet (Oid, Community, SNMPHost, Value: string; ValueType: integer): boolean;
|
2008-04-23 23:28:55 +03:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{==============================================================================}
|
|
|
|
|
|
|
|
{TSNMPRec.Create}
|
|
|
|
constructor TSNMPRec.Create;
|
|
|
|
begin
|
|
|
|
inherited create;
|
2008-04-23 23:39:31 +03:00
|
|
|
SNMPMibList := TList.create;
|
2008-04-23 23:49:43 +03:00
|
|
|
id:=1;
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
{TSNMPRec.Destroy}
|
|
|
|
destructor TSNMPRec.Destroy;
|
2008-04-24 09:44:13 +03:00
|
|
|
var
|
|
|
|
i:integer;
|
2008-04-23 23:28:55 +03:00
|
|
|
begin
|
2008-04-24 09:44:13 +03:00
|
|
|
for i := 0 to SNMPMibList.count - 1 do
|
|
|
|
TSNMPMib(SNMPMibList[i]).Free;
|
2008-04-23 23:39:31 +03:00
|
|
|
SNMPMibList.free;
|
2008-04-23 23:28:55 +03:00
|
|
|
inherited destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{TSNMPRec.DecodeBuf}
|
2008-04-24 09:44:13 +03:00
|
|
|
function TSNMPRec.DecodeBuf(Buffer:string):boolean;
|
2008-04-23 23:28:55 +03:00
|
|
|
var
|
|
|
|
Pos:integer;
|
|
|
|
endpos:integer;
|
|
|
|
sm,sv:string;
|
2008-04-23 23:46:58 +03:00
|
|
|
svt: integer;
|
2008-04-23 23:28:55 +03:00
|
|
|
begin
|
2008-04-24 09:44:13 +03:00
|
|
|
result:=false;
|
|
|
|
if length(buffer)<2
|
|
|
|
then exit;
|
|
|
|
if (ord(buffer[1]) and $20)=0
|
|
|
|
then exit;
|
2008-04-23 23:28:55 +03:00
|
|
|
Pos:=2;
|
2008-04-23 23:32:54 +03:00
|
|
|
Endpos:=ASNDecLen(Pos,buffer);
|
2008-04-24 09:44:13 +03:00
|
|
|
if length(buffer)<(Endpos+2)
|
|
|
|
then exit;
|
2008-04-23 23:46:58 +03:00
|
|
|
Self.version:=StrToIntDef(ASNItem(Pos,buffer,svt),0);
|
|
|
|
Self.community:=ASNItem(Pos,buffer,svt);
|
|
|
|
Self.PDUType:=StrToIntDef(ASNItem(Pos,buffer,svt),0);
|
|
|
|
Self.ID:=StrToIntDef(ASNItem(Pos,buffer,svt),0);
|
|
|
|
Self.ErrorStatus:=StrToIntDef(ASNItem(Pos,buffer,svt),0);
|
|
|
|
Self.ErrorIndex:=StrToIntDef(ASNItem(Pos,buffer,svt),0);
|
|
|
|
ASNItem(Pos,buffer,svt);
|
2008-04-23 23:28:55 +03:00
|
|
|
while Pos<Endpos do
|
|
|
|
begin
|
2008-04-23 23:46:58 +03:00
|
|
|
ASNItem(Pos,buffer,svt);
|
|
|
|
Sm:=ASNItem(Pos,buffer,svt);
|
|
|
|
Sv:=ASNItem(Pos,buffer,svt);
|
2008-04-23 23:39:31 +03:00
|
|
|
Self.MIBadd(sm,sv, svt);
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
2008-04-24 09:44:13 +03:00
|
|
|
result:=true;
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
{TSNMPRec.EncodeBuf}
|
|
|
|
function TSNMPRec.EncodeBuf:string;
|
|
|
|
var
|
2008-04-23 23:46:58 +03:00
|
|
|
data,s:string;
|
2008-04-23 23:39:31 +03:00
|
|
|
SNMPMib: TSNMPMib;
|
2008-04-23 23:28:55 +03:00
|
|
|
n:integer;
|
|
|
|
begin
|
|
|
|
data:='';
|
2008-04-23 23:39:31 +03:00
|
|
|
for n:=0 to SNMPMibList.Count-1 do
|
2008-04-23 23:28:55 +03:00
|
|
|
begin
|
2008-04-23 23:39:31 +03:00
|
|
|
SNMPMib := SNMPMibList[n];
|
|
|
|
case (SNMPMib.ValueType) of
|
2008-04-23 23:49:43 +03:00
|
|
|
ASN1_INT:
|
2008-04-23 23:39:31 +03:00
|
|
|
begin
|
2008-04-23 23:46:58 +03:00
|
|
|
s := ASNObject(MibToID(SNMPMib.OID),ASN1_OBJID)
|
|
|
|
+ASNObject(ASNEncInt(strToIntDef(SNMPMib.Value,0)),SNMPMib.ValueType);
|
|
|
|
end;
|
2008-04-23 23:49:43 +03:00
|
|
|
ASN1_COUNTER, ASN1_GAUGE, ASN1_TIMETICKS:
|
|
|
|
begin
|
|
|
|
s := ASNObject(MibToID(SNMPMib.OID),ASN1_OBJID)
|
|
|
|
+ASNObject(ASNEncUInt(strToIntDef(SNMPMib.Value,0)),SNMPMib.ValueType);
|
|
|
|
end;
|
2008-04-23 23:46:58 +03:00
|
|
|
ASN1_OBJID:
|
|
|
|
begin
|
|
|
|
s := ASNObject(MibToID(SNMPMib.OID),ASN1_OBJID) + ASNObject(MibToID(SNMPMib.Value),SNMPMib.ValueType);
|
|
|
|
end;
|
|
|
|
ASN1_IPADDR:
|
|
|
|
begin
|
|
|
|
s := ASNObject(MibToID(SNMPMib.OID),ASN1_OBJID) + ASNObject(IPToID(SNMPMib.Value),SNMPMib.ValueType);
|
2008-04-23 23:39:31 +03:00
|
|
|
end;
|
2008-04-23 23:46:58 +03:00
|
|
|
ASN1_NULL:
|
|
|
|
begin
|
|
|
|
s := ASNObject(MibToID(SNMPMib.OID),ASN1_OBJID) + ASNObject('',ASN1_NULL);
|
|
|
|
end;
|
|
|
|
else
|
|
|
|
s := ASNObject(MibToID(SNMPMib.OID),ASN1_OBJID) + ASNObject(SNMPMib.Value,SNMPMib.ValueType);
|
2008-04-23 23:39:31 +03:00
|
|
|
end;
|
2008-04-23 23:46:58 +03:00
|
|
|
data := data + ASNObject(s, ASN1_SEQ);
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
2008-04-23 23:46:58 +03:00
|
|
|
data:=ASNObject(data,ASN1_SEQ);
|
|
|
|
data:=ASNObject(char(Self.ID),ASN1_INT)
|
|
|
|
+ASNObject(char(Self.ErrorStatus),ASN1_INT)
|
|
|
|
+ASNObject(char(Self.ErrorIndex),ASN1_INT)
|
2008-04-23 23:28:55 +03:00
|
|
|
+data;
|
2008-04-23 23:46:58 +03:00
|
|
|
data:=ASNObject(char(Self.Version),ASN1_INT)
|
|
|
|
+ASNObject(Self.community,ASN1_OCTSTR)
|
2008-04-23 23:28:55 +03:00
|
|
|
+ASNObject(data,Self.PDUType);
|
2008-04-23 23:46:58 +03:00
|
|
|
data:=ASNObject(data,ASN1_SEQ);
|
2008-04-23 23:28:55 +03:00
|
|
|
Result:=data;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{TSNMPRec.Clear}
|
|
|
|
procedure TSNMPRec.Clear;
|
2008-04-23 23:39:31 +03:00
|
|
|
var
|
|
|
|
i:integer;
|
2008-04-23 23:28:55 +03:00
|
|
|
begin
|
|
|
|
version:=0;
|
|
|
|
community:='';
|
|
|
|
PDUType:=0;
|
|
|
|
ErrorStatus:=0;
|
|
|
|
ErrorIndex:=0;
|
2008-04-23 23:39:31 +03:00
|
|
|
for i := 0 to SNMPMibList.count - 1 do
|
|
|
|
TSNMPMib(SNMPMibList[i]).Free;
|
|
|
|
SNMPMibList.Clear;
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
{TSNMPRec.MIBAdd}
|
2008-04-23 23:46:58 +03:00
|
|
|
procedure TSNMPRec.MIBAdd(MIB,Value:string; ValueType:integer);
|
2008-04-23 23:28:55 +03:00
|
|
|
var
|
2008-04-23 23:39:31 +03:00
|
|
|
SNMPMib: TSNMPMib;
|
2008-04-23 23:28:55 +03:00
|
|
|
begin
|
2008-04-23 23:39:31 +03:00
|
|
|
SNMPMib := TSNMPMib.Create;
|
|
|
|
SNMPMib.OID := MIB;
|
|
|
|
SNMPMib.Value := Value;
|
2008-04-23 23:46:58 +03:00
|
|
|
SNMPMib.ValueType := ValueType;
|
2008-04-23 23:39:31 +03:00
|
|
|
SNMPMibList.Add(SNMPMib);
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
{TSNMPRec.MIBdelete}
|
|
|
|
procedure TSNMPRec.MIBdelete(Index:integer);
|
|
|
|
begin
|
2008-04-23 23:39:31 +03:00
|
|
|
if (Index >= 0) and (Index < SNMPMibList.count) then
|
|
|
|
begin
|
|
|
|
TSNMPMib(SNMPMibList[Index]).Free;
|
|
|
|
SNMPMibList.Delete(Index);
|
|
|
|
end;
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
{TSNMPRec.MIBGet}
|
|
|
|
function TSNMPRec.MIBGet(MIB:string):string;
|
|
|
|
var
|
2008-04-23 23:39:31 +03:00
|
|
|
i: integer;
|
2008-04-23 23:28:55 +03:00
|
|
|
begin
|
2008-04-23 23:39:31 +03:00
|
|
|
Result := '';
|
|
|
|
for i := 0 to SNMPMibList.count - 1 do
|
|
|
|
begin
|
|
|
|
if ((TSNMPMib(SNMPMibList[i])).OID = MIB) then
|
|
|
|
begin
|
|
|
|
Result := (TSNMPMib(SNMPMibList[i])).Value;
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
end;
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-23 23:39:31 +03:00
|
|
|
|
2008-04-23 23:28:55 +03:00
|
|
|
{==============================================================================}
|
|
|
|
|
|
|
|
{TSNMPSend.Create}
|
|
|
|
constructor TSNMPSend.Create;
|
|
|
|
begin
|
|
|
|
inherited create;
|
|
|
|
Query:=TSNMPRec.Create;
|
|
|
|
Reply:=TSNMPRec.Create;
|
|
|
|
Query.Clear;
|
|
|
|
Reply.Clear;
|
|
|
|
sock:=TUDPBlockSocket.create;
|
|
|
|
sock.createsocket;
|
2008-04-23 23:46:58 +03:00
|
|
|
timeout:=5000;
|
2008-04-23 23:39:31 +03:00
|
|
|
host:='localhost';
|
2008-04-24 09:44:13 +03:00
|
|
|
HostIP:='';
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
{TSNMPSend.Destroy}
|
|
|
|
destructor TSNMPSend.Destroy;
|
|
|
|
begin
|
|
|
|
Sock.Free;
|
|
|
|
Reply.Free;
|
|
|
|
Query.Free;
|
|
|
|
inherited destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{TSNMPSend.DoIt}
|
|
|
|
function TSNMPSend.DoIt:boolean;
|
|
|
|
var
|
|
|
|
x:integer;
|
|
|
|
begin
|
|
|
|
Result:=false;
|
|
|
|
reply.clear;
|
|
|
|
Buffer:=Query.Encodebuf;
|
2008-04-23 23:39:31 +03:00
|
|
|
sock.connect(host,'161');
|
2008-04-24 09:44:13 +03:00
|
|
|
HostIP:=sock.GetRemoteSinIP;
|
2008-04-23 23:28:55 +03:00
|
|
|
sock.SendBuffer(PChar(Buffer),Length(Buffer));
|
|
|
|
if sock.canread(timeout)
|
|
|
|
then begin
|
|
|
|
x:=sock.WaitingData;
|
|
|
|
if x>0 then
|
|
|
|
begin
|
|
|
|
setlength(Buffer,x);
|
|
|
|
sock.RecvBuffer(PChar(Buffer),x);
|
|
|
|
result:=true;
|
|
|
|
end;
|
|
|
|
end;
|
2008-04-23 23:39:31 +03:00
|
|
|
if Result
|
2008-04-24 09:44:13 +03:00
|
|
|
then result:=reply.DecodeBuf(Buffer);
|
2008-04-23 23:28:55 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
{==============================================================================}
|
|
|
|
|
2008-04-23 23:39:31 +03:00
|
|
|
function SNMPget (Oid, Community, SNMPHost:string; var Value:string):Boolean;
|
2008-04-23 23:28:55 +03:00
|
|
|
var
|
|
|
|
SNMP:TSNMPSend;
|
|
|
|
begin
|
|
|
|
SNMP:=TSNMPSend.Create;
|
|
|
|
try
|
|
|
|
Snmp.Query.community:=Community;
|
|
|
|
Snmp.Query.PDUType:=PDUGetRequest;
|
2008-04-23 23:46:58 +03:00
|
|
|
Snmp.Query.MIBAdd(Oid,'',ASN1_NULL);
|
2008-04-23 23:39:31 +03:00
|
|
|
Snmp.host:=SNMPHost;
|
2008-04-23 23:28:55 +03:00
|
|
|
Result:=Snmp.DoIt;
|
|
|
|
if Result then
|
2008-04-23 23:39:31 +03:00
|
|
|
Value:=Snmp.Reply.MIBGet(Oid);
|
2008-04-23 23:28:55 +03:00
|
|
|
finally
|
|
|
|
SNMP.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:46:58 +03:00
|
|
|
function SNMPSet(Oid, Community, SNMPHost, Value: string; ValueType: integer): boolean;
|
2008-04-23 23:39:31 +03:00
|
|
|
var
|
|
|
|
SNMPSend: TSNMPSend;
|
|
|
|
begin
|
|
|
|
SNMPSend := TSNMPSend.Create;
|
|
|
|
try
|
|
|
|
SNMPSend.Query.community := Community;
|
|
|
|
SNMPSend.Query.PDUType := PDUSetRequest;
|
|
|
|
SNMPSend.Query.MIBAdd(Oid, Value, ValueType);
|
|
|
|
SNMPSend.Host := SNMPHost;
|
|
|
|
result:= SNMPSend.DoIt=true;
|
|
|
|
finally
|
|
|
|
SNMPSend.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2008-04-23 23:28:55 +03:00
|
|
|
|
|
|
|
end.
|