home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 November / Chip_2002-11_cd1.bin / zkuste / delphi / kompon / d3456 / SYNAPSE.ZIP / source / lib / SNMPTrap.pas < prev    next >
Pascal/Delphi Source File  |  2002-07-07  |  12KB  |  360 lines

  1. {==============================================================================|
  2. | Project : Delphree - Synapse                                   | 002.003.000 |
  3. |==============================================================================|
  4. | Content: SNMP traps                                                          |
  5. |==============================================================================|
  6. | Copyright (c)1999-2002, Lukas Gebauer                                        |
  7. | All rights reserved.                                                         |
  8. |                                                                              |
  9. | Redistribution and use in source and binary forms, with or without           |
  10. | modification, are permitted provided that the following conditions are met:  |
  11. |                                                                              |
  12. | Redistributions of source code must retain the above copyright notice, this  |
  13. | list of conditions and the following disclaimer.                             |
  14. |                                                                              |
  15. | Redistributions in binary form must reproduce the above copyright notice,    |
  16. | this list of conditions and the following disclaimer in the documentation    |
  17. | and/or other materials provided with the distribution.                       |
  18. |                                                                              |
  19. | Neither the name of Lukas Gebauer nor the names of its contributors may      |
  20. | be used to endorse or promote products derived from this software without    |
  21. | specific prior written permission.                                           |
  22. |                                                                              |
  23. | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  |
  24. | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE    |
  25. | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE   |
  26. | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR  |
  27. | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL       |
  28. | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR   |
  29. | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER   |
  30. | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT           |
  31. | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY    |
  32. | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH  |
  33. | DAMAGE.                                                                      |
  34. |==============================================================================|
  35. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  36. | Portions created by Hernan Sanchez are Copyright (c)2000,2001.               |
  37. | All Rights Reserved.                                                         |
  38. |==============================================================================|
  39. | Contributor(s):                                                              |
  40. |   Hernan Sanchez (hernan.sanchez@iname.com)                                  |
  41. |==============================================================================|
  42. | History: see HISTORY.HTM from distribution package                           |
  43. |          (Found at URL: http://www.ararat.cz/synapse/)                       |
  44. |==============================================================================}
  45.  
  46. {$Q-}
  47. {$WEAKPACKAGEUNIT ON}
  48.  
  49. unit SNMPTrap;
  50.  
  51. interface
  52.  
  53. uses
  54.   Classes, SysUtils,
  55.   blckSock, SynaUtil, ASN1Util, SNMPSend;
  56.  
  57. const
  58.   cSnmpTrapProtocol = '162';
  59.  
  60.   SNMP_VERSION = 0;
  61.  
  62.   PDU_GET = $A0;
  63.   PDU_GETN = $A1;
  64.   PDU_RESP = $A2;
  65.   PDU_SET = $A3;
  66.   PDU_TRAP = $A4;
  67.  
  68. type
  69.   TTrapPDU = class(TObject)
  70.   private
  71.     FBuffer: string;
  72.     FVersion: Integer;
  73.     FPDUType: Integer;
  74.     FCommunity: string;
  75.     FEnterprise: string;
  76.     FTrapHost: string;
  77.     FGenTrap: Integer;
  78.     FSpecTrap: Integer;
  79.     FTimeTicks: Integer;
  80.     FSNMPMibList: TList;
  81.   public
  82.     constructor Create;
  83.     destructor Destroy; override;
  84.     procedure Clear;
  85.     procedure MIBAdd(const MIB, Value: string; ValueType: Integer);
  86.     procedure MIBDelete(Index: Integer);
  87.     function MIBGet(const MIB: string): string;
  88.     function EncodeTrap: Integer;
  89.     function DecodeTrap: Boolean;
  90.   published
  91.     property Version: Integer read FVersion Write FVersion;
  92.     property Community: string read FCommunity Write FCommunity;
  93.     property PDUType: Integer read FPDUType Write FPDUType;
  94.     property Enterprise: string read FEnterprise Write FEnterprise;
  95.     property TrapHost: string read FTrapHost Write FTrapHost;
  96.     property GenTrap: Integer read FGenTrap Write FGenTrap;
  97.     property SpecTrap: Integer read FSpecTrap Write FSpecTrap;
  98.     property TimeTicks: Integer read FTimeTicks Write FTimeTicks;
  99.     property SNMPMibList: TList read FSNMPMibList;
  100.   end;
  101.  
  102.   TTrapSNMP = class(TSynaClient)
  103.   private
  104.     FSock: TUDPBlockSocket;
  105.     FTrap: TTrapPDU;
  106.   public
  107.     constructor Create;
  108.     destructor Destroy; override;
  109.     function Send: Integer;
  110.     function Recv: Integer;
  111.   published
  112.     property Trap: TTrapPDU read FTrap;
  113.     property Sock: TUDPBlockSocket read FSock;
  114.   end;
  115.  
  116. function SendTrap(const Dest, Source, Enterprise, Community: string;
  117.   Generic, Specific, Seconds: Integer; const MIBName, MIBValue: string;
  118.   MIBtype: Integer): Integer;
  119. function RecvTrap(var Dest, Source, Enterprise, Community: string;
  120.   var Generic, Specific, Seconds: Integer; const MIBName,
  121.   MIBValue: TStringList): Integer;
  122.  
  123. implementation
  124.  
  125. constructor TTrapPDU.Create;
  126. begin
  127.   inherited Create;
  128.   FSNMPMibList := TList.Create;
  129.   FVersion := SNMP_VERSION;
  130.   FPDUType := PDU_TRAP;
  131.   FCommunity := 'public';
  132. end;
  133.  
  134. destructor TTrapPDU.Destroy;
  135. var
  136.   i: Integer;
  137. begin
  138.   for i := 0 to FSNMPMibList.Count - 1 do
  139.     TSNMPMib(FSNMPMibList[i]).Free;
  140.   FSNMPMibList.Free;
  141.   inherited Destroy;
  142. end;
  143.  
  144. procedure TTrapPDU.Clear;
  145. var
  146.   i: Integer;
  147. begin
  148.   for i := 0 to FSNMPMibList.Count - 1 do
  149.     TSNMPMib(FSNMPMibList[i]).Free;
  150.   FSNMPMibList.Clear;
  151.   FVersion := SNMP_VERSION;
  152.   FPDUType := PDU_TRAP;
  153.   FCommunity := 'public';
  154. end;
  155.  
  156. procedure TTrapPDU.MIBAdd(const MIB, Value: string; ValueType: Integer);
  157. var
  158.   SNMPMib: TSNMPMib;
  159. begin
  160.   SNMPMib := TSNMPMib.Create;
  161.   SNMPMib.OID := MIB;
  162.   SNMPMib.Value := Value;
  163.   SNMPMib.ValueType := ValueType;
  164.   FSNMPMibList.Add(SNMPMib);
  165. end;
  166.  
  167. procedure TTrapPDU.MIBDelete(Index: Integer);
  168. begin
  169.   if (Index >= 0) and (Index < FSNMPMibList.Count) then
  170.   begin
  171.     TSNMPMib(FSNMPMibList[Index]).Free;
  172.     FSNMPMibList.Delete(Index);
  173.   end;
  174. end;
  175.  
  176. function TTrapPDU.MIBGet(const MIB: string): string;
  177. var
  178.   i: Integer;
  179. begin
  180.   Result := '';
  181.   for i := 0 to FSNMPMibList.Count - 1 do
  182.   begin
  183.     if TSNMPMib(FSNMPMibList[i]).OID = MIB then
  184.     begin
  185.       Result := TSNMPMib(FSNMPMibList[i]).Value;
  186.       Break;
  187.     end;
  188.   end;
  189. end;
  190.  
  191. function TTrapPDU.EncodeTrap: Integer;
  192. var
  193.   s: string;
  194.   n: Integer;
  195.   SNMPMib: TSNMPMib;
  196. begin
  197.   FBuffer := '';
  198.   for n := 0 to FSNMPMibList.Count - 1 do
  199.   begin
  200.     SNMPMib := FSNMPMibList[n];
  201.     case SNMPMib.ValueType of
  202.       ASN1_INT:
  203.         s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  204.           ASNObject(ASNEncInt(StrToIntDef(SNMPMib.Value, 0)), SNMPMib.ValueType);
  205.       ASN1_COUNTER, ASN1_GAUGE, ASN1_TIMETICKS:
  206.         s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  207.           ASNObject(ASNEncUInt(StrToIntDef(SNMPMib.Value, 0)), SNMPMib.ValueType);
  208.       ASN1_OBJID:
  209.         s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  210.           ASNObject(MibToID(SNMPMib.Value), SNMPMib.ValueType);
  211.       ASN1_IPADDR:
  212.         s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  213.           ASNObject(IPToID(SNMPMib.Value), SNMPMib.ValueType);
  214.       ASN1_NULL:
  215.         s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  216.           ASNObject('', ASN1_NULL);
  217.     else
  218.       s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  219.         ASNObject(SNMPMib.Value, SNMPMib.ValueType);
  220.     end;
  221.     FBuffer := FBuffer + ASNObject(s, ASN1_SEQ);
  222.   end;
  223.   FBuffer := ASNObject(FBuffer, ASN1_SEQ);
  224.   FBuffer := ASNObject(ASNEncInt(FGenTrap), ASN1_INT) +
  225.     ASNObject(ASNEncInt(FSpecTrap), ASN1_INT) +
  226.     ASNObject(ASNEncUInt(FTimeTicks), ASN1_TIMETICKS) +
  227.     FBuffer;
  228.   FBuffer := ASNObject(MibToID(FEnterprise), ASN1_OBJID) +
  229.     ASNObject(IPToID(FTrapHost), ASN1_IPADDR) +
  230.     FBuffer;
  231.   FBuffer := ASNObject(ASNEncInt(FVersion), ASN1_INT) +
  232.     ASNObject(FCommunity, ASN1_OCTSTR) +
  233.     ASNObject(FBuffer, Self.FPDUType);
  234.   FBuffer := ASNObject(FBuffer, ASN1_SEQ);
  235.   Result := 1;
  236. end;
  237.  
  238. function TTrapPDU.DecodeTrap: Boolean;
  239. var
  240.   Pos, EndPos: Integer;
  241.   Sm, Sv: string;
  242.   Svt: Integer;
  243. begin
  244.   Clear;
  245.   Result := False;
  246.   if Length(FBuffer) < 2 then
  247.     Exit;
  248.   if (Ord(FBuffer[1]) and $20) = 0 then
  249.     Exit;
  250.   Pos := 2;
  251.   EndPos := ASNDecLen(Pos, FBuffer);
  252.   FVersion := StrToIntDef(ASNItem(Pos, FBuffer, Svt), 0);
  253.   FCommunity := ASNItem(Pos, FBuffer, Svt);
  254.   FPDUType := StrToIntDef(ASNItem(Pos, FBuffer, Svt), PDU_TRAP);
  255.   FEnterprise := ASNItem(Pos, FBuffer, Svt);
  256.   FTrapHost := ASNItem(Pos, FBuffer, Svt);
  257.   FGenTrap := StrToIntDef(ASNItem(Pos, FBuffer, Svt), 0);
  258.   FSpecTrap := StrToIntDef(ASNItem(Pos, FBuffer, Svt), 0);
  259.   FTimeTicks := StrToIntDef(ASNItem(Pos, FBuffer, Svt), 0);
  260.   ASNItem(Pos, FBuffer, Svt);
  261.   while Pos < EndPos do
  262.   begin
  263.     ASNItem(Pos, FBuffer, Svt);
  264.     Sm := ASNItem(Pos, FBuffer, Svt);
  265.     Sv := ASNItem(Pos, FBuffer, Svt);
  266.     MIBAdd(Sm, Sv, Svt);
  267.   end;
  268.   Result := True;
  269. end;
  270.  
  271. constructor TTrapSNMP.Create;
  272. begin
  273.   inherited Create;
  274.   FSock := TUDPBlockSocket.Create;
  275.   FSock.CreateSocket;
  276.   FTrap := TTrapPDU.Create;
  277.   FTimeout := 5000;
  278.   FTargetPort := cSnmpTrapProtocol;
  279. end;
  280.  
  281. destructor TTrapSNMP.Destroy;
  282. begin
  283.   FTrap.Free;
  284.   FSock.Free;
  285.   inherited Destroy;
  286. end;
  287.  
  288. function TTrapSNMP.Send: Integer;
  289. begin
  290.   FTrap.EncodeTrap;
  291.   FSock.Bind(FIPInterface, cAnyPort);
  292.   FSock.Connect(FTargetHost, FTargetPort);
  293.   FSock.SendString(FTrap.FBuffer);
  294.   Result := 1;
  295. end;
  296.  
  297. function TTrapSNMP.Recv: Integer;
  298. begin
  299.   Result := 0;
  300.   FSock.Bind(FIPInterface, FTargetPort);
  301.   FTrap.FBuffer := FSock.RecvPacket(FTimeout);
  302.   if Fsock.Lasterror = 0 then
  303.     if FTrap.DecodeTrap then
  304.       Result := 1;
  305. end;
  306.  
  307. function SendTrap(const Dest, Source, Enterprise, Community: string;
  308.   Generic, Specific, Seconds: Integer; const MIBName, MIBValue: string;
  309.   MIBtype: Integer): Integer;
  310. begin
  311.   with TTrapSNMP.Create do
  312.   try
  313.     TargetHost := Dest;
  314.     Trap.TrapHost := Source;
  315.     Trap.Enterprise := Enterprise;
  316.     Trap.Community := Community;
  317.     Trap.GenTrap := Generic;
  318.     Trap.SpecTrap := Specific;
  319.     Trap.TimeTicks := Seconds;
  320.     Trap.MIBAdd(MIBName, MIBValue, MIBType);
  321.     Result := Send;
  322.   finally
  323.     Free;
  324.   end;
  325. end;
  326.  
  327. function RecvTrap(var Dest, Source, Enterprise, Community: string;
  328.   var Generic, Specific, Seconds: Integer;
  329.   const MIBName, MIBValue: TStringList): Integer;
  330. var
  331.   i: Integer;
  332. begin
  333.   with TTrapSNMP.Create do
  334.   try
  335.     TargetHost := Dest;
  336.     Result := Recv;
  337.     if Result <> 0 then
  338.     begin
  339.       Dest := TargetHost;
  340.       Source := Trap.TrapHost;
  341.       Enterprise := Trap.Enterprise;
  342.       Community := Trap.Community;
  343.       Generic := Trap.GenTrap;
  344.       Specific := Trap.SpecTrap;
  345.       Seconds := Trap.TimeTicks;
  346.       MIBName.Clear;
  347.       MIBValue.Clear;
  348.       for i := 0 to Trap.SNMPMibList.Count - 1 do
  349.       begin
  350.         MIBName.Add(TSNMPMib(Trap.SNMPMibList[i]).OID);
  351.         MIBValue.Add(TSNMPMib(Trap.SNMPMibList[i]).Value);
  352.       end;
  353.     end;
  354.   finally
  355.     Free;
  356.   end;
  357. end;
  358.  
  359. end.
  360.