Use this function and add the following units: wsports and snmp

function IsPortFree(Prt: Integer): Boolean;
var p: TWSPorts;
begin
 p := TWSPorts.Create;
 p.Refresh;
 if p.PortInUse(Prt) then Result:= False else Result:= True;
 p.Free;
end;


//

// © 2000 Epsylon Software Development

//

// WSPorts.pas is based on NetView Version 1.0 (Main.pas)

//

// written by Crits Vadim

//

unit WSPorts;

interface

uses

Windows, SysUtils, Classes, WinSock, Snmp;

type

TWSPorts = class(TObject)

private

{ Private declarations }

FPorts: TList;

procedure EnumPorts;

function GetPortCount: Integer;

function GetPort(index: Integer): Integer;

public

{ Public declarations }

constructor Create;

destructor Destroy; override;

procedure GetUsedPorts(sl: TStrings);

function PortInUse(port: Integer): Boolean;

procedure Refresh;

property PortCount: Integer read GetPortCount;

property Ports[index: Integer]: Integer read GetPort;

end;

implementation

function SnmpExtensionInit(dwTimeZeroReference: DWORD; hPollForTrapEvent: 
PHandle;

supportedView: PAsnObjectIdentifier): BOOL; stdcall;

external 'inetmib1.dll' name 'SnmpExtensionInit';

function SnmpExtensionQuery(requestType: BYTE; variableBindings: 
PRFC1157VarBindList;

errorStatus, errorIndex: PAsnInteger): BOOL; stdcall;

external 'inetmib1.dll' name 'SnmpExtensionQuery';

const

tcpidentifiers: array[0..9] of UINT = (1, 3, 6, 1, 2, 1, 6, 13, 1, 1);

udpidentifiers: array[0..9] of UINT = (1, 3, 6, 1, 2, 1, 7, 5, 1, 1);

function SortPorts(Item1, Item2: Pointer): Integer;

begin

result := Integer(Item1) - Integer(Item2);

end;

{ TWSPort }

constructor TWSPorts.Create;

var WSAData: TWSAData;

begin

inherited;

if WSAStartup($0101, WSAData) <> 0 then

begin

raise EExternal.Create('Could not initialize Winsock');

Exit;

end;

FPorts := TList.Create;

EnumPorts;

end;

destructor TWSPorts.Destroy;

begin

FPorts.Free;

WSACleanup;

inherited;

end;

procedure TWSPorts.EnumPorts;

var hTrapEvent: THandle;

hIdentifier: TAsnObjectIdentifier;

bindList: TRFC1157VarBindList;

bindEntry: TRFC1157VarBind;

errorStatus, errorIndex: TAsnInteger;

error: Boolean;

begin

FPorts.Clear;

if not SnmpExtensionInit(GetCurrentTime, @hTrapEvent, @hIdentifier) then 
Exit;

// TCP connections

bindList.list := @bindEntry;

bindList.len := 1;

bindEntry.name.idLength := 10;

bindEntry.name.ids := @tcpidentifiers;

error := False;

while not error do

begin

if not SnmpExtensionQuery(ASN_RFC1157_GETNEXTREQUEST,

@bindList, @errorStatus, @errorIndex) then

begin

error := True;

Continue;

end;

if bindEntry.name.idLength < 10 then Break;

if bindEntry.name.ids[9] = 3 then

if not PortInUse(bindEntry.value.number) then

FPorts.Add(Pointer(bindEntry.value.number));

end;

// UDP connections

bindList.list := @bindEntry;

bindList.len := 1;

bindEntry.name.idLength := 10;

bindEntry.name.ids := @udpidentifiers;

error := False;

while not error do

begin

if not SnmpExtensionQuery(ASN_RFC1157_GETNEXTREQUEST,

@bindList, @errorStatus, @errorIndex) then

begin

error := True;

Continue;

end;

if bindEntry.name.idLength < 10 then Break;

if bindEntry.name.ids[9] = 2 then

if not PortInUse(bindEntry.value.number) then

FPorts.Add(Pointer(bindEntry.value.number));

end;

FPorts.Sort(SortPorts);

end;

function TWSPorts.GetPort(index: Integer): Integer;

begin

Result := Integer(FPorts[index]);

end;

function TWSPorts.GetPortCount: Integer;

begin

Result := FPorts.Count;

end;

procedure TWSPorts.GetUsedPorts(sl: TStrings);

var i: Integer;

begin

for i := 0 to FPorts.Count - 1 do

sl.Add(IntToStr(Integer(FPorts[i])));

end;

function TWSPorts.PortInUse(port: Integer): Boolean;

begin

Result := FPorts.IndexOf(Pointer(port)) <> -1;

end;

procedure TWSPorts.Refresh;

begin

EnumPorts;

end;

end.





{*******************************************************}
{                                                       }
{       Delphi Runtime Library                          }
{       SNMP Interface Unit                             }
{                                                       }
{       Copyright (c) 1999 Crits Vadim                  }
{                                                       }
{*******************************************************}

unit Snmp;

interface

uses Windows;

///////////////////////////////////////////////////////////////////////////////
// 
//
// SNMP API return code definitions 
//
// 
//
///////////////////////////////////////////////////////////////////////////////
const

  SNMPAPI_NOERROR = INTEGER(TRUE);
  SNMPAPI_ERROR   = INTEGER(FALSE);

///////////////////////////////////////////////////////////////////////////////
// 
//
// SNMP API error code definitions 
//
// 
//
///////////////////////////////////////////////////////////////////////////////

  SNMP_MEM_ALLOC_ERROR = 1;

///////////////////////////////////////////////////////////////////////////////
// 
//
// BER API error code definitions 
//
// 
//
///////////////////////////////////////////////////////////////////////////////

  SNMP_BERAPI_INVALID_LENGTH  = 10;
  SNMP_BERAPI_INVALID_TAG     = 11;
  SNMP_BERAPI_OVERFLOW        = 12;
  SNMP_BERAPI_SHORT_BUFFER    = 13;
  SNMP_BERAPI_INVALID_OBJELEM = 14;

///////////////////////////////////////////////////////////////////////////////
// 
//
// PDU API error code definitions 
//
// 
//
///////////////////////////////////////////////////////////////////////////////

  SNMP_PDUAPI_UNRECOGNIZED_PDU = 20;
  SNMP_PDUAPI_INVALID_ES       = 21;
  SNMP_PDUAPI_INVALID_GT       = 22;

///////////////////////////////////////////////////////////////////////////////
// 
//
// AUTH API error code definitions 
//
// 
//
///////////////////////////////////////////////////////////////////////////////

  SNMP_AUTHAPI_INVALID_VERSION  = 30;
  SNMP_AUTHAPI_INVALID_MSG_TYPE = 31;
  SNMP_AUTHAPI_TRIV_AUTH_FAILED = 32;

///////////////////////////////////////////////////////////////////////////////
// 
//
// SNMP PDU error status definitions 
//
// 
//
///////////////////////////////////////////////////////////////////////////////

  SNMP_ERRORSTATUS_NOERROR    = 0;
  SNMP_ERRORSTATUS_TOOBIG     = 1;
  SNMP_ERRORSTATUS_NOSUCHNAME = 2;
  SNMP_ERRORSTATUS_BADVALUE   = 3;
  SNMP_ERRORSTATUS_READONLY   = 4;
  SNMP_ERRORSTATUS_GENERR     = 5;

///////////////////////////////////////////////////////////////////////////////
// 
//
// SNMP PDU generic trap definitions 
//
// 
//
///////////////////////////////////////////////////////////////////////////////

 SNMP_GENERICTRAP_COLDSTART     = 0;
 SNMP_GENERICTRAP_WARMSTART     = 1;
 SNMP_GENERICTRAP_LINKDOWN      = 2;
 SNMP_GENERICTRAP_LINKUP        = 3;
 SNMP_GENERICTRAP_AUTHFAILURE   = 4;
 SNMP_GENERICTRAP_EGPNEIGHLOSS  = 5;
 SNMP_GENERICTRAP_ENTERSPECIFIC = 6;

///////////////////////////////////////////////////////////////////////////////
// 
//
// BER encoding definitions 
//
// 
//
///////////////////////////////////////////////////////////////////////////////

  ASN_UNIVERSAL       = $00;
  ASN_APPLICATION     = $40;
  ASN_CONTEXTSPECIFIC = $80;
  ASN_PRIVATE         = $C0;
  ASN_PRIMATIVE       = $00;
  ASN_CONSTRUCTOR     = $20;

///////////////////////////////////////////////////////////////////////////////
// 
//
// ASN.1 simple types 
//
// 
//
///////////////////////////////////////////////////////////////////////////////

  ASN_INTEGER          = (ASN_UNIVERSAL or ASN_PRIMATIVE or $02);
  ASN_OCTETSTRING      = (ASN_UNIVERSAL or ASN_PRIMATIVE or $04);
  ASN_NULL             = (ASN_UNIVERSAL or ASN_PRIMATIVE or $05);
  ASN_OBJECTIDENTIFIER = (ASN_UNIVERSAL or ASN_PRIMATIVE or $06);

///////////////////////////////////////////////////////////////////////////////
// 
//
// ASN.1 constructor types 
//
// 
//
///////////////////////////////////////////////////////////////////////////////

  ASN_SEQUENCE   = (ASN_UNIVERSAL or ASN_CONSTRUCTOR or $10);
  ASN_SEQUENCEOF = ASN_SEQUENCE;

///////////////////////////////////////////////////////////////////////////////
// 
//
// ASN.1 application specific primatives 
//
// 
//
///////////////////////////////////////////////////////////////////////////////

  ASN_RFC1155_IPADDRESS  = (ASN_APPLICATION or ASN_PRIMATIVE or $00);
  ASN_RFC1155_COUNTER    = (ASN_APPLICATION or ASN_PRIMATIVE or $01);
  ASN_RFC1155_GAUGE      = (ASN_APPLICATION or ASN_PRIMATIVE or $02);
  ASN_RFC1155_TIMETICKS  = (ASN_APPLICATION or ASN_PRIMATIVE or $03);
  ASN_RFC1155_OPAQUE     = (ASN_APPLICATION or ASN_PRIMATIVE or $04);
  ASN_RFC1213_DISPSTRING = ASN_OCTETSTRING;

///////////////////////////////////////////////////////////////////////////////
// 
//
// ASN.1 application specific constructors 
//
// 
//
///////////////////////////////////////////////////////////////////////////////

  ASN_RFC1157_GETREQUEST     = ASN_CONTEXTSPECIFIC or ASN_CONSTRUCTOR or 
$00;
  ASN_RFC1157_GETNEXTREQUEST = ASN_CONTEXTSPECIFIC or ASN_CONSTRUCTOR or 
$01;
  ASN_RFC1157_GETRESPONSE    = ASN_CONTEXTSPECIFIC or ASN_CONSTRUCTOR or 
$02;
  ASN_RFC1157_SETREQUEST     = ASN_CONTEXTSPECIFIC or ASN_CONSTRUCTOR or 
$03;
  ASN_RFC1157_TRAP           = ASN_CONTEXTSPECIFIC or ASN_CONSTRUCTOR or 
$04;

///////////////////////////////////////////////////////////////////////////////
// 
//
// SNMP ASN type definitions 
//
// 
//
///////////////////////////////////////////////////////////////////////////////

type

  PAsnOctetString = ^TAsnOctetString;
  TAsnOctetString = packed record
    stream: ^BYTE;
    length: UINT;
    dynam: BOOL;
  end;

  PIdentifiers = ^TIdentifiers;
  TIdentifiers = array[0..9] of UINT;

  PAsnObjectIdentifier = ^TAsnObjectIdentifier;
  TAsnObjectIdentifier = packed record
    idLength: UINT;
    ids: PIdentifiers;
  end;

  PAsnInteger = ^TAsnInteger;
  TAsnInteger = Longint;
  TAsnCounter = DWORD;
  TAsnGauge = DWORD;
  TAsnTimeticks = DWORD;

  TAsnSequence = TAsnOctetString;
  TAsnImplicitSequence = TAsnOctetString;
  TAsnIPAddress = TAsnOctetString;
  TAsnDisplayString = TAsnOctetString;
  TAsnOpaque = TAsnOctetString;

  TAsnObjectName = TAsnObjectIdentifier;
  TAsnNetworkAddress = TAsnIPAddress;


  PAsnAny = ^TAsnAny;
  TAsnAny = packed record
    asnType: BYTE;
    align : array[0..2] of BYTE;//size 13 byte necessary 16 byte
    case Integer of
      0: (number: TAsnInteger);
      1: (str: TAsnOctetString);
      2: (obj: TAsnObjectIdentifier);
      3: (sequence: TAsnSequence);
      4: (address: TAsnIPAddress);
      5: (counter: TAsnCounter);
      6: (gauge: TAsnGauge);
      7: (ticks: TAsnTimeticks);
      8: (arbitrary: TAsnOpaque);
  end;

  TAsnObjectSyntax = TAsnAny;

///////////////////////////////////////////////////////////////////////////////
// 
//
// SNMP API type definitions 
//
// 
//
///////////////////////////////////////////////////////////////////////////////

  PRFC1157VarBind = ^TRFC1157VarBind;
  TRFC1157VarBind = packed record
    name: TAsnObjectName;
    value: TAsnObjectSyntax;
  end;

  PRFC1157VarBindList = ^TRFC1157VarBindList;
  TRFC1157VarBindList = packed record
    list: PRFC1157VarBind;
    len: UINT;
  end;

///////////////////////////////////////////////////////////////////////////////
// 
//
// SNMP API prototypes 
//
// 
//
///////////////////////////////////////////////////////////////////////////////

function  SnmpUtilOidCpy(DstObjId: PAsnObjectIdentifier; SrcObjId: 
PAsnObjectIdentifier): Integer; stdcall;
function  SnmpUtilOidAppend(DstObjId: PAsnObjectIdentifier; SrcObjId: 
PAsnObjectIdentifier): Integer; stdcall;
function  SnmpUtilOidNCmp(ObjIdA, ObjIdB: PAsnObjectIdentifier; Len: UINT): 
Integer; stdcall;
function  SnmpUtilOidCmp(ObjIdA, ObjIdB: PAsnObjectIdentifier): Integer; 
stdcall;
procedure SnmpUtilOidFree(ObjId: PAsnObjectIdentifier); stdcall;
function  SnmpUtilVarBindListCpy(DstVarBindList, SrcVarBindList: 
PRFC1157VarBindList): Integer; stdcall;
function  SnmpUtilVarBindCpy(DstVarBind, SrcVarBind: PRFC1157VarBind): 
Integer; stdcall;
procedure SnmpUtilVarBindListFree(VarBindList: PRFC1157VarBindList); 
stdcall;
procedure SnmpUtilVarBindFree(VarBind: PRFC1157VarBind); stdcall;
procedure SnmpUtilPrintAsnAny(Any: PAsnAny); stdcall;
procedure SnmpUtilMemFree(Addr: Pointer); stdcall;
function  SnmpUtilMemAlloc(Size: UINT): Pointer; stdcall;
function  SnmpUtilMemReAlloc(Addr: Pointer; NewSize: UINT): Pointer; 
stdcall;

///////////////////////////////////////////////////////////////////////////////
// 
//
// SNMP debugging definitions 
//
// 
//
///////////////////////////////////////////////////////////////////////////////

const
  SNMP_LOG_SILENT  = $0;
  SNMP_LOG_FATAL   = $1;
  SNMP_LOG_ERROR   = $2;
  SNMP_LOG_WARNING = $3;
  SNMP_LOG_TRACE   = $4;
  SNMP_LOG_VERBOSE = $5;

///////////////////////////////////////////////////////////////////////////////
// 
//
// Miscellaneous definitions 
//
// 
//
///////////////////////////////////////////////////////////////////////////////

const
  SNMP_MAX_OID_LEN = $7F00;

implementation

const
  snmplib = 'Snmpapi.dll';

function  SnmpUtilOidCpy;          external snmplib name 'SnmpUtilOidCpy';
function  SnmpUtilOidAppend;       external snmplib name 
'SnmpUtilOidAppend';
function  SnmpUtilOidNCmp;         external snmplib name 'SnmpUtilOidNCmp';
function  SnmpUtilOidCmp;          external snmplib name 'SnmpUtilOidCmp';
procedure SnmpUtilOidFree;         external snmplib name 'SnmpUtilOidFree';
function  SnmpUtilVarBindListCpy;  external snmplib name 
'SnmpUtilVarBindListCpy';
function  SnmpUtilVarBindCpy;      external snmplib name 
'SnmpUtilVarBindCpy';
procedure SnmpUtilVarBindListFree; external snmplib name 
'SnmpUtilVarBindListFree';
procedure SnmpUtilVarBindFree;     external snmplib name 
'SnmpUtilVarBindFree';
procedure SnmpUtilPrintAsnAny;     external snmplib name 
'SnmpUtilPrintAsnAny';
procedure SnmpUtilMemFree;         external snmplib name 'SnmpUtilMemFree';
function  SnmpUtilMemAlloc;        external snmplib name 'SnmpUtilMemAlloc';
function  SnmpUtilMemReAlloc;      external snmplib name 
'SnmpUtilMemReAlloc';

end.






-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://www.elists.org/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to