⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 idsnmp.pas

📁 Indy控件的使用源代码
💻 PAS
📖 第 1 页 / 共 2 页
字号:
{ $HDR$}
{**********************************************************************}
{ Unit archived using Team Coherence                                   }
{ Team Coherence is Copyright 2002 by Quality Software Components      }
{                                                                      }
{ For further information / comments, visit our WEB site at            }
{ http://www.TeamCoherence.com                                         }
{**********************************************************************}
{}
{ $Log:  10327: IdSNMP.pas 
{
{   Rev 1.0    2002.11.12 10:51:42 PM  czhower
}
unit IdSNMP;

{
-2001.02.13 - Kudzu - Misc "Indy" Changes.
-Contributions also by: Hernan Sanchez (hernan.sanchez@iname.com)
-Original Author: Lukas Gebauer

The Synapse SNMP component was converted for use in INDY.

| The Original Code is Synapse Delphi Library.                                 |
|==============================================================================|
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
| Portions created by Lukas Gebauer are Copyright (c)2000.                     |
| All Rights Reserved.                                                         |
|==============================================================================|
| Contributor(s):                                                              |
|   Hernan Sanchez (hernan.sanchez@iname.com)  Original author                 |
|   Colin Wilson (colin@wilsonc.demon.co.uk)   Fixed some bugs & added support |
|                                              for Value types                 |
|==============================================================================|
| History: see HISTORY.HTM from distribution package                           |
|          (Found at URL: http://www.ararat.cz/synapse/)                       |
|==============================================================================}

interface

uses
  Classes,
  SysUtils,
  IdUDPBase,
  IdUDPClient,
  IdException,
  IdASN1Util;

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
  TIdSNMP = class;
  TSNMPInfo=class(TObject)
  private
    fOwner : TIdSNMP;
    fCommunity: string;
    function GetValue (idx : Integer) : string;
    function GetValueCount: Integer;
    function GetValueType (idx : Integer) : Integer;
    function GetValueOID(idx: Integer): string;
    procedure SetCommunity(const Value: string);
  protected
    Buffer: string;
    procedure SyncMIB;
  public
    Host : string;
    Port : integer;
    Enterprise: string;
    GenTrap: integer;
    SpecTrap: integer;
    Version : integer;
    PDUType : integer;
    TimeTicks : integer;
    ID : integer;
    ErrorStatus : integer;
    ErrorIndex : integer;
    MIBOID : TStringList;
    MIBValue : TStringList;

    constructor Create (AOwner : TIdSNMP);
    destructor  Destroy; override;
    function    EncodeTrap: integer;
    function    DecodeTrap: integer;
    procedure   DecodeBuf(Buffer:string);
    function    EncodeBuf:string;
    procedure   Clear;
    procedure   MIBAdd(MIB,Value:string; valueType : Integer = ASN1_OCTSTR);
    procedure   MIBDelete(Index:integer);
    function    MIBGet(MIB:string):string;

    property    Owner : TIdSNMP read fOwner;
    property    Community : string read fCommunity write SetCommunity;
    property    ValueCount : Integer read GetValueCount;
    property    Value [idx : Integer] : string read GetValue;
    property    ValueOID [idx : Integer] : string read GetValueOID;
    property    ValueType [idx : Integer] : Integer read GetValueType;
  end;

  TIdSNMP = class(TIdUDPClient)
  private
    fCommunity: string;
    fTrapPort: Integer;
    procedure SetCommunity(const Value: string);
  public
    Query : TSNMPInfo;
    Reply : TSNMPInfo;
    Trap  : TSNMPInfo;

    constructor Create(aOwner : TComponent); override;
    destructor Destroy; override;
    function SendQuery : boolean;
    function QuickSend(const Mib, Community, Host:string; var Value:string):Boolean;
    function QuickSendTrap(const Dest, Enterprise, Community: string;
                      Port, Generic, Specific: integer; MIBName, MIBValue: TStringList): integer;
    function QuickReceiveTrap(var Source, Enterprise, Community: string;
                      var Port, Generic, Specific, Seconds: integer; var MIBName, MIBValue: TStringList): integer;
    function SendTrap: integer;
    function ReceiveTrap: integer;
  published
    property Port default 161;
    property TrapPort : Integer read fTrapPort Write fTrapPort default 162;
    property Community : string read fCommunity write SetCommunity;
  end;


implementation


//Hernan Sanchez
function IPToID(Host: string): string;
var
  s, t: string;
  i, x: integer;
begin
  Result := '';    {Do not Localize}
  for x:= 1 to 3 do
    begin
      t := '';    {Do not Localize}
      s := StrScan(PChar(Host), '.');    {Do not Localize}
      t := Copy(Host, 1, (Length(Host) - Length(s)));
      Delete(Host, 1, (Length(Host) - Length(s) + 1));
      i := StrTointDef(t, 0);
      Result := Result + Chr(i);
    end;
  i := StrTointDef(Host, 0);
  Result := Result + Chr(i);
end;

{========================== SNMP INFO OBJECT ==================================}

{ TIdSNMPInfo }

(*----------------------------------------------------------------------------*
 | constructor TSNMPInfo.Create ()                                            |
 |                                                                            |
 | Constructor for TSNMPInfo                                                  |
 |                                                                            |
 | Parameters:                                                                |
 |   AOwner : TIdSNMP       The owning IdSNMP Component                       |
 |                                                                            |
 *----------------------------------------------------------------------------*)
constructor TSNMPInfo.Create (AOwner : TIdSNMP);
begin
  inherited create;
  fOwner := AOwner;
  MIBOID:=TStringList.create;
  MIBValue:=TStringList.create;
  fCommunity := AOwner.Community;
  Port := AOwner.Port;
end;

(*----------------------------------------------------------------------------*
 | destructor TSNMPInfo.Destroy                                               |
 |                                                                            |
 | Destructor for TSNMPInfo                                                   |
 *----------------------------------------------------------------------------*)
destructor TSNMPInfo.Destroy;
begin
  MIBValue.Free;
  MIBOID.Free;
  inherited destroy;
end;

(*----------------------------------------------------------------------------*
 | procedure TSNMPInfo.SyncMIB                                                |
 |                                                                            |
 | Ensure that there are as many 'values' as 'oids'                           |    {Do not Localize}
 *----------------------------------------------------------------------------*)
procedure TSNMPInfo.SyncMIB;
var
  n,x:integer;
begin
  x:=MIBValue.Count;
  for n:=x to MIBOID.Count-1 do
    MIBValue.Add('');    {Do not Localize}
end;

(*----------------------------------------------------------------------------*
 | procedure TSNMPInfo.DecodeBuf                                              |
 |                                                                            |
 | Decode an ASN buffer into version, community, MIB OID/Value pairs, etc.    |
 |                                                                            |
 | Parameters:                                                                |
 |   Buffer:string             The ASN buffer to decode                       |
 *----------------------------------------------------------------------------*)
procedure TSNMPInfo.DecodeBuf(Buffer:string);
var
  Pos:integer;
  endpos,vt:integer;
  sm,sv:string;
begin
  Pos:=2;
  Endpos:=ASNDecLen(Pos,buffer);
  Self.version:=StrToIntDef(ASNItem(Pos,buffer,vt),0);
  Self.community:=ASNItem(Pos,buffer,vt);
  Self.PDUType:=StrToIntDef(ASNItem(Pos,buffer,vt),0);
  Self.ID:=StrToIntDef(ASNItem(Pos,buffer,vt),0);
  Self.ErrorStatus:=StrToIntDef(ASNItem(Pos,buffer,vt),0);
  Self.ErrorIndex:=StrToIntDef(ASNItem(Pos,buffer,vt),0);
  ASNItem(Pos,buffer,vt);
  while Pos<Endpos do           // Decode MIB/Value pairs
    begin
      ASNItem(Pos,buffer,vt);
      Sm:=ASNItem(Pos,buffer,vt);
      Sv:=ASNItem(Pos,buffer,vt);

      MIBadd(sm,sv, vt);
    end;
end;

(*----------------------------------------------------------------------------*
 | function TSNMPInfo.EncodeBuf                                               |
 |                                                                            |
 | Encode the details into an ASN string                                      |
 |                                                                            |
 | The function returns the encoded ASN string                                |
 *----------------------------------------------------------------------------*)
function TSNMPInfo.EncodeBuf:string;
var
  data,s:string;
  n:integer;
  objType:Integer;
begin
  data:='';    {Do not Localize}
  SyncMIB;
  for n:=0 to Self.MIBOID.Count-1 do
    begin
      objType := Integer (Self.MIBValue.Objects[n]);
      if objType = 0 then
        objType := ASN1_OCTSTR;

      case objType of
        ASN1_INT:
          s := ASNObject(MibToID(Self.MIBOID[n]), ASN1_OBJID) +
            ASNObject(ASNEncInt(StrToIntDef(Self.MIBValue[n], 0)), objType);
        ASN1_COUNTER, ASN1_GAUGE, ASN1_TIMETICKS:
          s := ASNObject(MibToID(Self.MIBOID[n]), ASN1_OBJID) +
            ASNObject(ASNEncUInt(StrToIntDef(Self.MIBValue[n], 0)), objType);
        ASN1_OBJID:
          s := ASNObject(MibToID(Self.MIBOID[n]), ASN1_OBJID) +
            ASNObject(MibToID(Self.MIBValue[n]), objType);
        ASN1_IPADDR:
          s := ASNObject(MibToID(Self.MIBOID[n]), ASN1_OBJID) +
            ASNObject(IPToID(Self.MIBValue[n]), objType);
        ASN1_NULL:
          s := ASNObject(MibToID(Self.MIBOID[n]), ASN1_OBJID) +
            ASNObject('', ASN1_NULL);    {Do not Localize}
        else
          s := ASNObject(MibToID(Self.MIBOID[n]), ASN1_OBJID) +
            ASNObject(Self.MIBValue[n], objType);
      end;
      data:=data+ASNObject(s,$30);
    end;
  data:=ASNObject(data,$30);
  data:=ASNObject(char(Self.ID),2)+ASNObject(char(Self.ErrorStatus),2)
         +ASNObject(char(Self.ErrorIndex),2)+data;
  data:=ASNObject(char(Self.Version),2)+ASNObject(Self.community,4)+ASNObject(data,Self.PDUType);
  data:=ASNObject(data,$30);
  Result:=data;
end;

(*----------------------------------------------------------------------------*
 | procedure TSNMPInfo.Clear                                                  |
 |                                                                            |
 | Clear the header info and  MIBOID/Value lists.                             |
 *----------------------------------------------------------------------------*)
procedure TSNMPInfo.Clear;
begin
  version:=0;
  fCommunity:=Owner.Community;
  if Self = fOwner.Trap then
    Port := Owner.TrapPort
  else
    Port := Owner.Port;
  Host := Owner.Host;
  PDUType:=0;
  ID:=0;
  ErrorStatus:=0;
  ErrorIndex:=0;
  MIBOID.Clear;
  MIBValue.Clear;
end;

(*----------------------------------------------------------------------------*
 | procedure TSNMPInfo.MIBAdd                                                 |
 |                                                                            |
 | Add a MIBOID/Value pair                                                    |
 |                                                                            |
 | Parameters:                                                                |
 |  MIB  : string               The MIBOID to add                             |
 |  Value : string              The Value                                     |
 |  valueType : Integer         The Value's type.  Optional - defaults to     |    {Do not Localize}
 |                              ASN1_OCTSTR                                   |
 *----------------------------------------------------------------------------*)
procedure TSNMPInfo.MIBAdd(MIB,Value:string; valueType : Integer);
var
  x:integer;
begin
  SyncMIB;
  MIBOID.Add (MIB);
  x:=MIBOID.Count;
  if MIBValue.Count>x then
  begin
    MIBvalue[x-1]:=Value;
    MibValue.Objects [x-1] := TObject (valueType)
  end
  else MIBValue.AddObject(Value, TObject (valueType));
end;

(*----------------------------------------------------------------------------*
 | procedure TSNMPInfo.MIBDelete                                              |
 |                                                                            |
 | Delete a MIBOID/Value pair                                                 |
 |                                                                            |
 | Parameters:                                                                |
 |   Index:integer                      The index of the pair to delete       |
 *----------------------------------------------------------------------------*)
procedure TSNMPInfo.MIBDelete(Index:integer);
begin
  SyncMIB;
  MIBOID.Delete(Index);
  if (MIBValue.Count-1)>= Index then MIBValue.Delete(Index);
end;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -