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

  1. {==============================================================================|
  2. | Project : Delphree - Synapse                                   | 002.003.005 |
  3. |==============================================================================|
  4. | Content: SNMP client                                                         |
  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 Lukas Gebauer are Copyright (c)2000,2001.                |
  18. | All Rights Reserved.                                                         |
  19. |==============================================================================|
  20. | Contributor(s):                                                              |
  21. |   Jean-Fabien Connault (jfconnault@mail.dotcom.fr)                           |
  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 SNMPSend;
  31.  
  32. interface
  33.  
  34. uses
  35.   Classes, SysUtils,
  36.   blckSock, SynaUtil, ASN1Util;
  37.  
  38. const
  39.   cSnmpProtocol = '161';
  40.  
  41. //PDU type
  42.   PDUGetRequest = $A0;
  43.   PDUGetNextRequest = $A1;
  44.   PDUGetResponse = $A2;
  45.   PDUSetRequest = $A3;
  46.   PDUTrap = $A4;
  47.  
  48. //errors
  49.   ENoError = 0;
  50.   ETooBig = 1;
  51.   ENoSuchName = 2;
  52.   EBadValue = 3;
  53.   EReadOnly = 4;
  54.   EGenErr = 5;
  55.  
  56. type
  57.   TSNMPMib = class(TObject)
  58.   private
  59.     FOID: string;
  60.     FValue: string;
  61.     FValueType: Integer;
  62.   published
  63.     property OID: string read FOID Write FOID;
  64.     property Value: string read FValue Write FValue;
  65.     property ValueType: Integer read FValueType Write FValueType;
  66.   end;
  67.  
  68.   TSNMPRec = class(TObject)
  69.   private
  70.     FVersion: Integer;
  71.     FCommunity: string;
  72.     FPDUType: Integer;
  73.     FID: Integer;
  74.     FErrorStatus: Integer;
  75.     FErrorIndex: Integer;
  76.     FSNMPMibList: TList;
  77.   public
  78.     constructor Create;
  79.     destructor Destroy; override;
  80.     function DecodeBuf(const Buffer: string): Boolean;
  81.     function EncodeBuf: string;
  82.     procedure Clear;
  83.     procedure MIBAdd(const MIB, Value: string; ValueType: Integer);
  84.     procedure MIBDelete(Index: Integer);
  85.     function MIBGet(const MIB: string): string;
  86.   published
  87.     property Version: Integer read FVersion Write FVersion;
  88.     property Community: string read FCommunity Write FCommunity;
  89.     property PDUType: Integer read FPDUType Write FPDUType;
  90.     property ID: Integer read FID Write FID;
  91.     property ErrorStatus: Integer read FErrorStatus Write FErrorStatus;
  92.     property ErrorIndex: Integer read FErrorIndex Write FErrorIndex;
  93.     property SNMPMibList: TList read FSNMPMibList;
  94.   end;
  95.  
  96.   TSNMPSend = class(TObject)
  97.   private
  98.     FSock: TUDPBlockSocket;
  99.     FBuffer: string;
  100.     FTimeout: Integer;
  101.     FHost: string;
  102.     FHostIP: string;
  103.     FQuery: TSNMPRec;
  104.     FReply: TSNMPRec;
  105.   public
  106.     constructor Create;
  107.     destructor Destroy; override;
  108.     function DoIt: Boolean;
  109.   published
  110.     property Timeout: Integer read FTimeout Write FTimeout;
  111.     property Host: string read FHost Write FHost;
  112.     property HostIP: string read FHostIP;
  113.     property Query: TSNMPRec read FQuery;
  114.     property Reply: TSNMPRec read FReply;
  115.     property Sock: TUDPBlockSocket read FSock;
  116.   end;
  117.  
  118. function SNMPGet(const Oid, Community, SNMPHost: string;
  119.   var Value: string): Boolean;
  120. function SNMPSet(const Oid, Community, SNMPHost, Value: string;
  121.   ValueType: Integer): Boolean;
  122.  
  123. implementation
  124.  
  125. {==============================================================================}
  126.  
  127. constructor TSNMPRec.Create;
  128. begin
  129.   inherited Create;
  130.   FSNMPMibList := TList.Create;
  131.   id := 1;
  132. end;
  133.  
  134. destructor TSNMPRec.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. function TSNMPRec.DecodeBuf(const Buffer: string): Boolean;
  145. var
  146.   Pos: Integer;
  147.   EndPos: Integer;
  148.   sm, sv: string;
  149.   Svt: Integer;
  150. begin
  151.   Result := False;
  152.   if Length(Buffer) < 2 then
  153.     Exit;
  154.   if (Ord(Buffer[1]) and $20) = 0 then
  155.     Exit;
  156.   Pos := 2;
  157.   EndPos := ASNDecLen(Pos, Buffer);
  158.   if Length(Buffer) < (EndPos + 2) then
  159.     Exit;
  160.   Self.FVersion := StrToIntDef(ASNItem(Pos, Buffer, Svt), 0);
  161.   Self.FCommunity := ASNItem(Pos, Buffer, Svt);
  162.   Self.FPDUType := StrToIntDef(ASNItem(Pos, Buffer, Svt), 0);
  163.   Self.FID := StrToIntDef(ASNItem(Pos, Buffer, Svt), 0);
  164.   Self.FErrorStatus := StrToIntDef(ASNItem(Pos, Buffer, Svt), 0);
  165.   Self.FErrorIndex := StrToIntDef(ASNItem(Pos, Buffer, Svt), 0);
  166.   ASNItem(Pos, Buffer, Svt);
  167.   while Pos < EndPos do
  168.   begin
  169.     ASNItem(Pos, Buffer, Svt);
  170.     Sm := ASNItem(Pos, Buffer, Svt);
  171.     Sv := ASNItem(Pos, Buffer, Svt);
  172.     Self.MIBAdd(sm, sv, Svt);
  173.   end;
  174.   Result := True;
  175. end;
  176.  
  177. function TSNMPRec.EncodeBuf: string;
  178. var
  179.   data, s: string;
  180.   SNMPMib: TSNMPMib;
  181.   n: Integer;
  182. begin
  183.   data := '';
  184.   for n := 0 to FSNMPMibList.Count - 1 do
  185.   begin
  186.     SNMPMib := FSNMPMibList[n];
  187.     case SNMPMib.ValueType of
  188.       ASN1_INT:
  189.         s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  190.           ASNObject(ASNEncInt(StrToIntDef(SNMPMib.Value, 0)), SNMPMib.ValueType);
  191.       ASN1_COUNTER, ASN1_GAUGE, ASN1_TIMETICKS:
  192.         s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  193.           ASNObject(ASNEncUInt(StrToIntDef(SNMPMib.Value, 0)), SNMPMib.ValueType);
  194.       ASN1_OBJID:
  195.         s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  196.           ASNObject(MibToID(SNMPMib.Value), SNMPMib.ValueType);
  197.       ASN1_IPADDR:
  198.         s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  199.           ASNObject(IPToID(SNMPMib.Value), SNMPMib.ValueType);
  200.       ASN1_NULL:
  201.         s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  202.           ASNObject('', ASN1_NULL);
  203.     else
  204.       s := ASNObject(MibToID(SNMPMib.OID), ASN1_OBJID) +
  205.         ASNObject(SNMPMib.Value, SNMPMib.ValueType);
  206.     end;
  207.     data := data + ASNObject(s, ASN1_SEQ);
  208.   end;
  209.   data := ASNObject(data, ASN1_SEQ);
  210.   data := ASNObject(ASNEncInt(Self.FID), ASN1_INT) +
  211.     ASNObject(ASNEncInt(Self.FErrorStatus), ASN1_INT) +
  212.     ASNObject(ASNEncInt(Self.FErrorIndex), ASN1_INT) +
  213.     data;
  214.   data := ASNObject(ASNEncInt(Self.FVersion), ASN1_INT) +
  215.     ASNObject(Self.FCommunity, ASN1_OCTSTR) +
  216.     ASNObject(data, Self.FPDUType);
  217.   data := ASNObject(data, ASN1_SEQ);
  218.   Result := data;
  219. end;
  220.  
  221. procedure TSNMPRec.Clear;
  222. var
  223.   i: Integer;
  224. begin
  225.   FVersion := 0;
  226.   FCommunity := '';
  227.   FPDUType := 0;
  228.   FErrorStatus := 0;
  229.   FErrorIndex := 0;
  230.   for i := 0 to FSNMPMibList.Count - 1 do
  231.     TSNMPMib(FSNMPMibList[i]).Free;
  232.   FSNMPMibList.Clear;
  233. end;
  234.  
  235. procedure TSNMPRec.MIBAdd(const MIB, Value: string; ValueType: Integer);
  236. var
  237.   SNMPMib: TSNMPMib;
  238. begin
  239.   SNMPMib := TSNMPMib.Create;
  240.   SNMPMib.OID := MIB;
  241.   SNMPMib.Value := Value;
  242.   SNMPMib.ValueType := ValueType;
  243.   FSNMPMibList.Add(SNMPMib);
  244. end;
  245.  
  246. procedure TSNMPRec.MIBDelete(Index: Integer);
  247. begin
  248.   if (Index >= 0) and (Index < FSNMPMibList.Count) then
  249.   begin
  250.     TSNMPMib(FSNMPMibList[Index]).Free;
  251.     FSNMPMibList.Delete(Index);
  252.   end;
  253. end;
  254.  
  255. function TSNMPRec.MIBGet(const MIB: string): string;
  256. var
  257.   i: Integer;
  258. begin
  259.   Result := '';
  260.   for i := 0 to FSNMPMibList.Count - 1 do
  261.   begin
  262.     if ((TSNMPMib(FSNMPMibList[i])).OID = MIB) then
  263.     begin
  264.       Result := (TSNMPMib(FSNMPMibList[i])).Value;
  265.       Break;
  266.     end;
  267.   end;
  268. end;
  269.  
  270. {==============================================================================}
  271.  
  272. constructor TSNMPSend.Create;
  273. begin
  274.   inherited Create;
  275.   FQuery := TSNMPRec.Create;
  276.   FReply := TSNMPRec.Create;
  277.   FQuery.Clear;
  278.   FReply.Clear;
  279.   FSock := TUDPBlockSocket.Create;
  280.   FSock.CreateSocket;
  281.   FTimeout := 5000;
  282.   FHost := cLocalhost;
  283.   FHostIP := '';
  284. end;
  285.  
  286. destructor TSNMPSend.Destroy;
  287. begin
  288.   FSock.Free;
  289.   FReply.Free;
  290.   FQuery.Free;
  291.   inherited Destroy;
  292. end;
  293.  
  294. function TSNMPSend.DoIt: Boolean;
  295. var
  296.   x: Integer;
  297. begin
  298.   Result := False;
  299.   FReply.Clear;
  300.   FBuffer := Query.EncodeBuf;
  301.   FSock.Connect(FHost, cSnmpProtocol);
  302.   FHostIP := '0.0.0.0';
  303.   FSock.SendString(FBuffer);
  304.   FBuffer := FSock.RecvPacket(FTimeout);
  305.   if FSock.LastError = 0 then
  306.   begin
  307.     FHostIP := FSock.GetRemoteSinIP;
  308.     Result := True;
  309.   end;
  310.   if Result then
  311.     Result := FReply.DecodeBuf(FBuffer);
  312. end;
  313.  
  314. {==============================================================================}
  315.  
  316. function SNMPGet(const Oid, Community, SNMPHost: string;
  317.   var Value: string): Boolean;
  318. var
  319.   SNMP: TSNMPSend;
  320. begin
  321.   SNMP := TSNMPSend.Create;
  322.   try
  323.     SNMP.Query.Community := Community;
  324.     SNMP.Query.PDUType := PDUGetRequest;
  325.     SNMP.Query.MIBAdd(Oid, '', ASN1_NULL);
  326.     SNMP.Host := SNMPHost;
  327.     Result := SNMP.DoIt;
  328.     if Result then
  329.       Value := SNMP.Reply.MIBGet(Oid);
  330.   finally
  331.     SNMP.Free;
  332.   end;
  333. end;
  334.  
  335. function SNMPSet(const Oid, Community, SNMPHost, Value: string;
  336.   ValueType: Integer): Boolean;
  337. var
  338.   SNMPSend: TSNMPSend;
  339. begin
  340.   SNMPSend := TSNMPSend.Create;
  341.   try
  342.     SNMPSend.Query.Community := Community;
  343.     SNMPSend.Query.PDUType := PDUSetRequest;
  344.     SNMPSend.Query.MIBAdd(Oid, Value, ValueType);
  345.     SNMPSend.Host := SNMPHost;
  346.     Result := SNMPSend.DoIt = True;
  347.   finally
  348.     SNMPSend.Free;
  349.   end;
  350. end;
  351.  
  352. end.
  353.