下面是我看到别人定义的一个UNIT,用这个定义中的SendTrap能够发送TRAP,别人也能收到;我想问的是
RecvTrap(Dest,Source,Enterprise,Community:string;Generic, Specific, Seconds: Integer;MIBName, MIBValue:TStringList)
的参数到底是什么意思.怎么设定使用.
unit SNMPTrap;interfaceuses
Classes, SysUtils,
blckSock, SynaUtil, ASN1Util, SNMPSend,Dialogs;const
cSnmpTrapProtocol = '162'; SNMP_VERSION = 0; PDU_GET = $A0;
PDU_GETN = $A1;
PDU_RESP = $A2;
PDU_SET = $A3;
PDU_TRAP = $A4;type
TTrapPDU = class(TObject)
private
FBuffer: string;
FTrapPort: string;
FVersion: Integer;
FPDUType: Integer;
FCommunity: string;
FEnterprise: string;
FTrapHost: string;
FGenTrap: Integer;
FSpecTrap: Integer;
FTimeTicks: Integer;
FSNMPMibList: TList;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure MIBAdd(const MIB, Value: string; ValueType: Integer);
procedure MIBDelete(Index: Integer);
function MIBGet(const MIB: string): string;
function EncodeTrap: Integer;
function DecodeTrap: Boolean;
published
property Version: Integer read FVersion Write FVersion;
property Community: string read FCommunity Write FCommunity;
property PDUType: Integer read FPDUType Write FPDUType;
property TrapPort: string read FTrapPort Write FTrapPort;
property Enterprise: string read FEnterprise Write FEnterprise;
property TrapHost: string read FTrapHost Write FTrapHost;
property GenTrap: Integer read FGenTrap Write FGenTrap;
property SpecTrap: Integer read FSpecTrap Write FSpecTrap;
property TimeTicks: Integer read FTimeTicks Write FTimeTicks;
property SNMPMibList: TList read FSNMPMibList;
end; TTrapSNMP = class(TObject)
private
FSock: TUDPBlockSocket;
FTrap: TTrapPDU;
FSNMPHost: string;
FTimeout: Integer;
public
constructor Create;
destructor Destroy; override;
function Send: Integer;
function Recv: Integer;
published
property Trap: TTrapPDU read FTrap;
property SNMPHost: string read FSNMPHost Write FSNMPHost;
property Timeout: Integer read FTimeout Write FTimeout;
property Sock: TUDPBlockSocket read FSock;
end;function SendTrap(const Dest, Source, Enterprise, Community: string;
Generic, Specific, Seconds: Integer; const MIBName, MIBValue: string;
MIBtype: Integer): Integer;
function RecvTrap( Dest, Source, Enterprise, Community: string;
Generic, Specific, Seconds: Integer; MIBName,
MIBValue: TStringList): Integer;implementationconstructor TTrapPDU.Create;
begin
inherited Create;
FSNMPMibList := TList.Create;
FTrapPort := cSnmpTrapProtocol;
FVersion := SNMP_VERSION;
FPDUType := PDU_TRAP;
FCommunity := 'public';
end;destructor TTrapPDU.Destroy;
var
i: Integer;
begin
for i := 0 to FSNMPMibList.Count - 1 do
TSNMPMib(FSNMPMibList[i]).Free;
FSNMPMibList.Free;
inherited Destroy;
end;procedure TTrapPDU.Clear;
var
i: Integer;
begin
for i := 0 to FSNMPMibList.Count - 1 do
TSNMPMib(FSNMPMibList[i]).Free;
FSNMPMibList.Clear;
FTrapPort := cSnmpTrapProtocol;
FVersion := SNMP_VERSION;
FPDUType := PDU_TRAP;
FCommunity := 'public';
end;procedure TTrapPDU.MIBAdd(const MIB, Value: string; ValueType: Integer);
var
SNMPMib: TSNMPMib;
begin
SNMPMib := TSNMPMib.Create;
SNMPMib.OID := MIB;
SNMPMib.Value := Value;
SNMPMib.ValueType := ValueType;
FSNMPMibList.Add(SNMPMib);
end;procedure TTrapPDU.MIBDelete(Index: Integer);
begin
if (Index >= 0) and (Index < FSNMPMibList.Count) then
begin
TSNMPMib(FSNMPMibList[Index]).Free;
FSNMPMibList.Delete(Index);
end;
end;function TTrapPDU.MIBGet(const MIB: string): string;
var
i: Integer;
begin
Result := '';
for i := 0 to FSNMPMibList.Count - 1 do
begin
if TSNMPMib(FSNMPMibList[i]).OID = MIB then
begin
Result := TSNMPMib(FSNMPMibList[i]).Value;
Break;
end;
end;
end;function TTrapPDU.EncodeTrap: Integer;
begin
end;function TTrapPDU.DecodeTrap: Boolean;
begin
end;constructor TTrapSNMP.Create;
begin
inherited Create;
FSock := TUDPBlockSocket.Create;
FTrap := TTrapPDU.Create;
FTimeout := 500;
FSNMPHost := cLocalhost;
FSock.CreateSocket;
end;destructor TTrapSNMP.Destroy;
begin
FTrap.Free;
FSock.Free;
inherited Destroy;
end;function TTrapSNMP.Send: Integer;
begin
FTrap.EncodeTrap;
FSock.Connect(SNMPHost, FTrap.TrapPort);
FSock.SendString(FTrap.FBuffer);
Result := 1;
end;function TTrapSNMP.Recv: Integer;
begin
Result := 0;
FSock.Bind('0.0.0.0', FTrap.TrapPort);
FTrap.FBuffer := FSock.RecvPacket(FTimeout);
if Fsock.Lasterror = 0 then
if FTrap.DecodeTrap then
Result := 1;
end;function SendTrap(const Dest, Source, Enterprise, Community: string;
Generic, Specific, Seconds: Integer; const MIBName, MIBValue: string;
MIBtype: Integer): Integer;
begin
with TTrapSNMP.Create do
try
SNMPHost := Dest;
Trap.TrapHost := Source;
Trap.Enterprise := Enterprise;
Trap.Community := Community;
Trap.GenTrap := Generic;
Trap.SpecTrap := Specific;
Trap.TimeTicks := Seconds;
Trap.MIBAdd(MIBName, MIBValue, MIBType);
Result := Send;
finally
Free;
end;
end;function RecvTrap( Dest, Source, Enterprise, Community: string;
Generic, Specific, Seconds: Integer;
MIBName, MIBValue: TStringList): Integer;
var
i: Integer;
begin
with TTrapSNMP.Create do
try
SNMPHost := Dest;
Result := Recv;
if Result <> 0 then
begin
Dest := SNMPHost;
Source := Trap.TrapHost;
Enterprise := Trap.Enterprise;
Community := Trap.Community;
Generic := Trap.GenTrap;
Specific := Trap.SpecTrap;
Seconds := Trap.TimeTicks;
MIBName.Clear;
MIBValue.Clear;
for i := 0 to Trap.SNMPMibList.Count - 1 do
begin
MIBName.Add(TSNMPMib(Trap.SNMPMibList[i]).OID);
MIBValue.Add(TSNMPMib(Trap.SNMPMibList[i]).Value);
end;
end;
finally
Free;
end;
end;end.
RecvTrap(Dest,Source,Enterprise,Community:string;Generic, Specific, Seconds: Integer;MIBName, MIBValue:TStringList)
的参数到底是什么意思.怎么设定使用.
unit SNMPTrap;interfaceuses
Classes, SysUtils,
blckSock, SynaUtil, ASN1Util, SNMPSend,Dialogs;const
cSnmpTrapProtocol = '162'; SNMP_VERSION = 0; PDU_GET = $A0;
PDU_GETN = $A1;
PDU_RESP = $A2;
PDU_SET = $A3;
PDU_TRAP = $A4;type
TTrapPDU = class(TObject)
private
FBuffer: string;
FTrapPort: string;
FVersion: Integer;
FPDUType: Integer;
FCommunity: string;
FEnterprise: string;
FTrapHost: string;
FGenTrap: Integer;
FSpecTrap: Integer;
FTimeTicks: Integer;
FSNMPMibList: TList;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure MIBAdd(const MIB, Value: string; ValueType: Integer);
procedure MIBDelete(Index: Integer);
function MIBGet(const MIB: string): string;
function EncodeTrap: Integer;
function DecodeTrap: Boolean;
published
property Version: Integer read FVersion Write FVersion;
property Community: string read FCommunity Write FCommunity;
property PDUType: Integer read FPDUType Write FPDUType;
property TrapPort: string read FTrapPort Write FTrapPort;
property Enterprise: string read FEnterprise Write FEnterprise;
property TrapHost: string read FTrapHost Write FTrapHost;
property GenTrap: Integer read FGenTrap Write FGenTrap;
property SpecTrap: Integer read FSpecTrap Write FSpecTrap;
property TimeTicks: Integer read FTimeTicks Write FTimeTicks;
property SNMPMibList: TList read FSNMPMibList;
end; TTrapSNMP = class(TObject)
private
FSock: TUDPBlockSocket;
FTrap: TTrapPDU;
FSNMPHost: string;
FTimeout: Integer;
public
constructor Create;
destructor Destroy; override;
function Send: Integer;
function Recv: Integer;
published
property Trap: TTrapPDU read FTrap;
property SNMPHost: string read FSNMPHost Write FSNMPHost;
property Timeout: Integer read FTimeout Write FTimeout;
property Sock: TUDPBlockSocket read FSock;
end;function SendTrap(const Dest, Source, Enterprise, Community: string;
Generic, Specific, Seconds: Integer; const MIBName, MIBValue: string;
MIBtype: Integer): Integer;
function RecvTrap( Dest, Source, Enterprise, Community: string;
Generic, Specific, Seconds: Integer; MIBName,
MIBValue: TStringList): Integer;implementationconstructor TTrapPDU.Create;
begin
inherited Create;
FSNMPMibList := TList.Create;
FTrapPort := cSnmpTrapProtocol;
FVersion := SNMP_VERSION;
FPDUType := PDU_TRAP;
FCommunity := 'public';
end;destructor TTrapPDU.Destroy;
var
i: Integer;
begin
for i := 0 to FSNMPMibList.Count - 1 do
TSNMPMib(FSNMPMibList[i]).Free;
FSNMPMibList.Free;
inherited Destroy;
end;procedure TTrapPDU.Clear;
var
i: Integer;
begin
for i := 0 to FSNMPMibList.Count - 1 do
TSNMPMib(FSNMPMibList[i]).Free;
FSNMPMibList.Clear;
FTrapPort := cSnmpTrapProtocol;
FVersion := SNMP_VERSION;
FPDUType := PDU_TRAP;
FCommunity := 'public';
end;procedure TTrapPDU.MIBAdd(const MIB, Value: string; ValueType: Integer);
var
SNMPMib: TSNMPMib;
begin
SNMPMib := TSNMPMib.Create;
SNMPMib.OID := MIB;
SNMPMib.Value := Value;
SNMPMib.ValueType := ValueType;
FSNMPMibList.Add(SNMPMib);
end;procedure TTrapPDU.MIBDelete(Index: Integer);
begin
if (Index >= 0) and (Index < FSNMPMibList.Count) then
begin
TSNMPMib(FSNMPMibList[Index]).Free;
FSNMPMibList.Delete(Index);
end;
end;function TTrapPDU.MIBGet(const MIB: string): string;
var
i: Integer;
begin
Result := '';
for i := 0 to FSNMPMibList.Count - 1 do
begin
if TSNMPMib(FSNMPMibList[i]).OID = MIB then
begin
Result := TSNMPMib(FSNMPMibList[i]).Value;
Break;
end;
end;
end;function TTrapPDU.EncodeTrap: Integer;
begin
end;function TTrapPDU.DecodeTrap: Boolean;
begin
end;constructor TTrapSNMP.Create;
begin
inherited Create;
FSock := TUDPBlockSocket.Create;
FTrap := TTrapPDU.Create;
FTimeout := 500;
FSNMPHost := cLocalhost;
FSock.CreateSocket;
end;destructor TTrapSNMP.Destroy;
begin
FTrap.Free;
FSock.Free;
inherited Destroy;
end;function TTrapSNMP.Send: Integer;
begin
FTrap.EncodeTrap;
FSock.Connect(SNMPHost, FTrap.TrapPort);
FSock.SendString(FTrap.FBuffer);
Result := 1;
end;function TTrapSNMP.Recv: Integer;
begin
Result := 0;
FSock.Bind('0.0.0.0', FTrap.TrapPort);
FTrap.FBuffer := FSock.RecvPacket(FTimeout);
if Fsock.Lasterror = 0 then
if FTrap.DecodeTrap then
Result := 1;
end;function SendTrap(const Dest, Source, Enterprise, Community: string;
Generic, Specific, Seconds: Integer; const MIBName, MIBValue: string;
MIBtype: Integer): Integer;
begin
with TTrapSNMP.Create do
try
SNMPHost := Dest;
Trap.TrapHost := Source;
Trap.Enterprise := Enterprise;
Trap.Community := Community;
Trap.GenTrap := Generic;
Trap.SpecTrap := Specific;
Trap.TimeTicks := Seconds;
Trap.MIBAdd(MIBName, MIBValue, MIBType);
Result := Send;
finally
Free;
end;
end;function RecvTrap( Dest, Source, Enterprise, Community: string;
Generic, Specific, Seconds: Integer;
MIBName, MIBValue: TStringList): Integer;
var
i: Integer;
begin
with TTrapSNMP.Create do
try
SNMPHost := Dest;
Result := Recv;
if Result <> 0 then
begin
Dest := SNMPHost;
Source := Trap.TrapHost;
Enterprise := Trap.Enterprise;
Community := Trap.Community;
Generic := Trap.GenTrap;
Specific := Trap.SpecTrap;
Seconds := Trap.TimeTicks;
MIBName.Clear;
MIBValue.Clear;
for i := 0 to Trap.SNMPMibList.Count - 1 do
begin
MIBName.Add(TSNMPMib(Trap.SNMPMibList[i]).OID);
MIBValue.Add(TSNMPMib(Trap.SNMPMibList[i]).Value);
end;
end;
finally
Free;
end;
end;end.
http://borland.mblogger.cn/jinjazz/posts/18391.aspx