home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / zkuste / delphi / unity / d23456 / SYNAPSE.ZIP / source / lib / SNMPTrap.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-09-23  |  10.5 KB  |  348 lines

  1. {==============================================================================|
  2. | Project : Delphree - Synapse                                   | 002.002.004 |
  3. |==============================================================================|
  4. | Content: SNMP traps                                                          |
  5. |==============================================================================|
  6. | The contents of this file are subject to the Mozilla Public License Ver. 1.1 |
  7. | (the "License"); you may not use this file except in compliance with the     |
  8. | License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ |
  9. |                                                                              |
  10. | Software distributed under the License is distributed on an "AS IS" basis,   |
  11. | WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for |
  12. | the specific language governing rights and limitations under the License.    |
  13. |==============================================================================|
  14. | The Original Code is Synapse Delphi Library.                                 |
  15. |==============================================================================|
  16. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  17. | Portions created by Hernan Sanchez are Copyright (c)2000,2001.               |
  18. | All Rights Reserved.                                                         |
  19. |==============================================================================|
  20. | Contributor(s):                                                              |
  21. |   Hernan Sanchez (hernan.sanchez@iname.com)                                  |
  22. |==============================================================================|
  23. | History: see HISTORY.HTM from distribution package                           |
  24. |          (Found at URL: http://www.ararat.cz/synapse/)                       |
  25. |==============================================================================}
  26.  
  27. {$Q-}
  28. {$WEAKPACKAGEUNIT ON}
  29.  
  30. unit SNMPTrap;
  31.  
  32. interface
  33.  
  34. uses
  35.   Classes, SysUtils,
  36.   blckSock, SynaUtil, ASN1Util, SNMPSend;
  37.  
  38. const
  39.   cSnmpTrapProtocol = '162';
  40.  
  41.   SNMP_VERSION = 0;
  42.  
  43.   PDU_GET = $A0;
  44.   PDU_GETN = $A1;
  45.   PDU_RESP = $A2;
  46.   PDU_SET = $A3;
  47.   PDU_TRAP = $A4;
  48.  
  49. type
  50.   TTrapPDU = class(TObject)
  51.   private
  52.     FBuffer: string;
  53.     FTrapPort: string;
  54.     FVersion: Integer;
  55.     FPDUType: Integer;
  56.     FCommunity: string;
  57.     FEnterprise: string;
  58.     FTrapHost: string;
  59.     FGenTrap: Integer;
  60.     FSpecTrap: Integer;
  61.     FTimeTicks: Integer;
  62.     FSNMPMibList: TList;
  63.   public
  64.     constructor Create;
  65.     destructor Destroy; override;
  66.     procedure Clear;
  67.     procedure MIBAdd(const MIB, Value: string; ValueType: Integer);
  68.     procedure MIBDelete(Index: Integer);
  69.     function MIBGet(const MIB: string): string;
  70.     function EncodeTrap: Integer;
  71.     function DecodeTrap: Boolean;
  72.   published
  73.     property Version: Integer read FVersion Write FVersion;
  74.     property Community: string read FCommunity Write FCommunity;
  75.     property PDUType: Integer read FPDUType Write FPDUType;
  76.     property TrapPort: string read FTrapPort Write FTrapPort;
  77.     property Enterprise: string read FEnterprise Write FEnterprise;
  78.     property TrapHost: string read FTrapHost Write FTrapHost;
  79.     property GenTrap: Integer read FGenTrap Write FGenTrap;
  80.     property SpecTrap: Integer read FSpecTrap Write FSpecTrap;
  81.     property TimeTicks: Integer read FTimeTicks Write FTimeTicks;
  82.     property SNMPMibList: TList read FSNMPMibList;
  83.   end;
  84.  
  85.   TTrapSNMP = class(TObject)
  86.   private
  87.     FSock: TUDPBlockSocket;
  88.     FTrap: TTrapPDU;
  89.     FSNMPHost: string;
  90.     FTimeout: Integer;
  91.   public
  92.     constructor Create;
  93.     destructor Destroy; override;
  94.     function Send: Integer;
  95.     function Recv: Integer;
  96.   published
  97.     property Trap: TTrapPDU read FTrap;
  98.     property SNMPHost: string read FSNMPHost Write FSNMPHost;
  99.     property Timeout: Integer read FTimeout Write FTimeout;
  100.     property Sock: TUDPBlockSocket read FSock;
  101.   end;
  102.  
  103. function SendTrap(const Dest, Source, Enterprise, Community: string;
  104.   Generic, Specific, Seconds: Integer; const MIBName, MIBValue: string;
  105.   MIBtype: Integer): Integer;
  106. function RecvTrap(var Dest, Source, Enterprise, Community: string;
  107.   var Generic, Specific, Seconds: Integer; const MIBName,
  108.   MIBValue: TStringList): Integer;
  109.  
  110. implementation
  111.  
  112. constructor TTrapPDU.Create;
  113. begin
  114.   inherited Create;
  115.   FSNMPMibList := TList.Create;
  116.   FTrapPort := cSnmpTrapProtocol;
  117.   FVersion := SNMP_VERSION;
  118.   FPDUType := PDU_TRAP;
  119.   FCommunity := 'public';
  120. end;
  121.  
  122. destructor TTrapPDU.Destroy;
  123. var
  124.   i: Integer;
  125. begin
  126.   for i := 0 to FSNMPMibList.Count - 1 do
  127.     TSNMPMib(FSNMPMibList[i]).Free;
  128.   FSNMPMibList.Free;
  129.   inherited Destroy;
  130. end;
  131.  
  132. procedure TTrapPDU.Clear;
  133. var
  134.   i: Integer;
  135. begin
  136.   for i := 0 to FSNMPMibList.Count - 1 do
  137.     TSNMPMib(FSNMPMibList[i]).Free;
  138.   FSNMPMibList.Clear;
  139.   FTrapPort := cSnmpTrapProtocol;
  140.   FVersion := SNMP_VERSION;
  141.   FPDUType := PDU_TRAP;
  142.   FCommunity := 'public';
  143. end;
  144.  
  145. procedure TTrapPDU.MIBAdd(const MIB, Value: string; ValueType: Integer);
  146. var
  147.   SNMPMib: TSNMPMib;
  148. begin
  149.   SNMPMib := TSNMPMib.Create;
  150.   SNMPMib.OID := MIB;
  151.   SNMPMib.Value := Value;
  152.   SNMPMib.ValueType := ValueType;
  153.   FSNMPMibList.Add(SNMPMib);
  154. end;
  155.  
  156. procedure TTrapPDU.MIBDelete(Index: Integer);
  157. begin
  158.   if (Index >= 0) and (Index < FSNMPMibList.Count) then
  159.   begin
  160.     TSNMPMib(FSNMPMibList[Index]).Free;
  161.     FSNMPMibList.Delete(Index);
  162.   end;
  163. end;
  164.  
  165. function TTrapPDU.MIBGet(const MIB: string): string;
  166. var
  167.   i: Integer;
  168. begin
  169.   Result := '';
  170.   for i := 0 to FSNMPMibList.Count - 1 do
  171.   begin
  172.     if TSNMPMib(FSNMPMibList[i]).OID = MIB then
  173.     begin
  174.       Result := TSNMPMib(FSNMPMibList[i]).Value;
  175.       Break;
  176.     end;
  177.   end;
  178. end;
  179.  
  180. function TTrapPDU.EncodeTrap: Integer;
  181. var
  182.   s: string;
  183.   n: Integer;
  184.   SNMPMib: TSNMPMib;
  185. begin
  186.   FBuffer := '';
  187.   for n := 0 to FSNMPMibList.Count - 1 do
  188.   begin
  189.     SNMPMib := FSNMPMibList[n];
  190.     case SNMPMib.ValueType of
  191.       ASN1_INT:
  192.         s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  193.           ASNObject(ASNEncInt(StrToIntDef(SNMPMib.Value, 0)), SNMPMib.ValueType);
  194.       ASN1_COUNTER, ASN1_GAUGE, ASN1_TIMETICKS:
  195.         s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  196.           ASNObject(ASNEncUInt(StrToIntDef(SNMPMib.Value, 0)), SNMPMib.ValueType);
  197.       ASN1_OBJID:
  198.         s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  199.           ASNObject(MibToID(SNMPMib.Value), SNMPMib.ValueType);
  200.       ASN1_IPADDR:
  201.         s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  202.           ASNObject(IPToID(SNMPMib.Value), SNMPMib.ValueType);
  203.       ASN1_NULL:
  204.         s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  205.           ASNObject('', ASN1_NULL);
  206.     else
  207.       s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  208.         ASNObject(SNMPMib.Value, SNMPMib.ValueType);
  209.     end;
  210.     FBuffer := FBuffer + ASNObject(s, ASN1_SEQ);
  211.   end;
  212.   FBuffer := ASNObject(FBuffer, ASN1_SEQ);
  213.   FBuffer := ASNObject(ASNEncInt(FGenTrap), ASN1_INT) +
  214.     ASNObject(ASNEncInt(FSpecTrap), ASN1_INT) +
  215.     ASNObject(ASNEncUInt(FTimeTicks), ASN1_TIMETICKS) +
  216.     FBuffer;
  217.   FBuffer := ASNObject(MibToID(FEnterprise), ASN1_OBJID) +
  218.     ASNObject(IPToID(FTrapHost), ASN1_IPADDR) +
  219.     FBuffer;
  220.   FBuffer := ASNObject(ASNEncInt(FVersion), ASN1_INT) +
  221.     ASNObject(FCommunity, ASN1_OCTSTR) +
  222.     ASNObject(FBuffer, Self.FPDUType);
  223.   FBuffer := ASNObject(FBuffer, ASN1_SEQ);
  224.   Result := 1;
  225. end;
  226.  
  227. function TTrapPDU.DecodeTrap: Boolean;
  228. var
  229.   Pos, EndPos: Integer;
  230.   Sm, Sv: string;
  231.   Svt: Integer;
  232. begin
  233.   Clear;
  234.   Result := False;
  235.   if Length(FBuffer) < 2 then
  236.     Exit;
  237.   if (Ord(FBuffer[1]) and $20) = 0 then
  238.     Exit;
  239.   Pos := 2;
  240.   EndPos := ASNDecLen(Pos, FBuffer);
  241.   FVersion := StrToIntDef(ASNItem(Pos, FBuffer, Svt), 0);
  242.   FCommunity := ASNItem(Pos, FBuffer, Svt);
  243.   FPDUType := StrToIntDef(ASNItem(Pos, FBuffer, Svt), PDU_TRAP);
  244.   FEnterprise := ASNItem(Pos, FBuffer, Svt);
  245.   FTrapHost := ASNItem(Pos, FBuffer, Svt);
  246.   FGenTrap := StrToIntDef(ASNItem(Pos, FBuffer, Svt), 0);
  247.   FSpecTrap := StrToIntDef(ASNItem(Pos, FBuffer, Svt), 0);
  248.   FTimeTicks := StrToIntDef(ASNItem(Pos, FBuffer, Svt), 0);
  249.   ASNItem(Pos, FBuffer, Svt);
  250.   while Pos < EndPos do
  251.   begin
  252.     ASNItem(Pos, FBuffer, Svt);
  253.     Sm := ASNItem(Pos, FBuffer, Svt);
  254.     Sv := ASNItem(Pos, FBuffer, Svt);
  255.     MIBAdd(Sm, Sv, Svt);
  256.   end;
  257.   Result := True;
  258. end;
  259.  
  260. constructor TTrapSNMP.Create;
  261. begin
  262.   inherited Create;
  263.   FSock := TUDPBlockSocket.Create;
  264.   FTrap := TTrapPDU.Create;
  265.   FTimeout := 5000;
  266.   FSNMPHost := cLocalhost;
  267.   FSock.CreateSocket;
  268. end;
  269.  
  270. destructor TTrapSNMP.Destroy;
  271. begin
  272.   FTrap.Free;
  273.   FSock.Free;
  274.   inherited Destroy;
  275. end;
  276.  
  277. function TTrapSNMP.Send: Integer;
  278. begin
  279.   FTrap.EncodeTrap;
  280.   FSock.Connect(SNMPHost, FTrap.TrapPort);
  281.   FSock.SendString(FTrap.FBuffer);
  282.   Result := 1;
  283. end;
  284.  
  285. function TTrapSNMP.Recv: Integer;
  286. begin
  287.   Result := 0;
  288.   FSock.Bind('0.0.0.0', FTrap.TrapPort);
  289.   FTrap.FBuffer := FSock.RecvPacket(FTimeout);
  290.   if Fsock.Lasterror = 0 then
  291.     if FTrap.DecodeTrap then
  292.       Result := 1;
  293. end;
  294.  
  295. function SendTrap(const Dest, Source, Enterprise, Community: string;
  296.   Generic, Specific, Seconds: Integer; const MIBName, MIBValue: string;
  297.   MIBtype: Integer): Integer;
  298. begin
  299.   with TTrapSNMP.Create do
  300.   try
  301.     SNMPHost := Dest;
  302.     Trap.TrapHost := Source;
  303.     Trap.Enterprise := Enterprise;
  304.     Trap.Community := Community;
  305.     Trap.GenTrap := Generic;
  306.     Trap.SpecTrap := Specific;
  307.     Trap.TimeTicks := Seconds;
  308.     Trap.MIBAdd(MIBName, MIBValue, MIBType);
  309.     Result := Send;
  310.   finally
  311.     Free;
  312.   end;
  313. end;
  314.  
  315. function RecvTrap(var Dest, Source, Enterprise, Community: string;
  316.   var Generic, Specific, Seconds: Integer;
  317.   const MIBName, MIBValue: TStringList): Integer;
  318. var
  319.   i: Integer;
  320. begin
  321.   with TTrapSNMP.Create do
  322.   try
  323.     SNMPHost := Dest;
  324.     Result := Recv;
  325.     if Result <> 0 then
  326.     begin
  327.       Dest := SNMPHost;
  328.       Source := Trap.TrapHost;
  329.       Enterprise := Trap.Enterprise;
  330.       Community := Trap.Community;
  331.       Generic := Trap.GenTrap;
  332.       Specific := Trap.SpecTrap;
  333.       Seconds := Trap.TimeTicks;
  334.       MIBName.Clear;
  335.       MIBValue.Clear;
  336.       for i := 0 to Trap.SNMPMibList.Count - 1 do
  337.       begin
  338.         MIBName.Add(TSNMPMib(Trap.SNMPMibList[i]).OID);
  339.         MIBValue.Add(TSNMPMib(Trap.SNMPMibList[i]).Value);
  340.       end;
  341.     end;
  342.   finally
  343.     Free;
  344.   end;
  345. end;
  346.  
  347. end.
  348.