Thema: Delphi Salat im UDPTable

Einzelnen Beitrag anzeigen

Benutzerbild von Harry M.
Harry M.

Registriert seit: 29. Okt 2004
Ort: Halle
462 Beiträge
 
#1

Salat im UDPTable

  Alt 4. Jan 2007, 09:36
Morgen's

Ich versuche den UDP Table zu holen. Die Anzahl der Verbindungen ist richtig (stimmt mit TCPView von Sysinternals überein) Aber der Rest ist Salat

Delphi-Quellcode:
program UDP;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Windows,
  WinSock;

type
  TTableOwnerPID = record
    PID: Cardinal;
    Protocol: string[3];
    localIP: DWORD;
    localPort: DWORD;
    remoteIP: DWORD;
    remotePort: DWORD;
    Status: DWORD;
    end;

type
  ATableOwnerPID = Array of TTableOwnerPID;

type
  UDP_TABLE_CLASS = Integer;
  ULONG = Integer;

const
  ANY_SIZE = 1;
  IpHlpApiLib = 'iphlpapi.dll';
  UDP_TABLE_OWNER_PID_ALL = 2;

type
  MIB_UDPROW_OWNER_PID = packed record
    dwState: DWORD;
    dwLocalAddr: DWORD;
    dwLocalPort: DWORD;
    dwRemoteAddr: DWORD;
    dwRemotePort: DWORD;
    dwOwningPid: DWORD;
    end;
  PMIB_UDPROW_OWNER_PID = ^MIB_UDPROW_OWNER_PID;

type
  MIB_UDPTABLE_OWNER_PID = packed record
    dwNumEntries: DWord;
    table: array [0..ANY_SIZE - 1] of MIB_UDPROW_OWNER_PID ;
    end;
  PMIB_UDPTABLE_OWNER_PID = ^MIB_UDPTABLE_OWNER_PID;

type
  TGetExtendedUdpTable = function (pUpdTable: Pointer; dwSize: PDWORD; bOrder: BOOL;
    lAf: ULONG; TableClass: UDP_TABLE_CLASS; Reserved: ULONG): DWord; stdcall;

var
  pGetExtendedUdpTable: TGetExtendedUdpTable;
  hLib: Cardinal;

function LoadAPI: Boolean;
begin
  Result := False;
  hLib := LoadLibrary(IpHlpApiLib);

  if hLib <> 0 then begin
    @pGetExtendedUdpTable := GetProcAddress(hLib, 'GetExtendedUdpTable');
    end;

  Result := (Assigned(pGetExtendedUdpTable));
end;

procedure UnLoadAPI;
begin
  if Assigned(pGetExtendedUdpTable) then
    pGetExtendedUdpTable := nil;

  if hLib <> 0 then
    FreeLibrary(hLib);
end;

function GetUPDTable: ATableOwnerPID;
var
  pUdpTable: PMIB_UDPTABLE_OWNER_PID;
  dwSize: DWord;
  Res: Dword;
  I: Integer;
begin
  SetLength(Result, 0);
  pUdpTable:=Nil;
  Try
    dwSize:=0;
    Res := pGetExtendedUdpTable(pUdpTable, @dwSize, True, AF_INET, UDP_TABLE_OWNER_PID_ALL, 0);
    If (Res = ERROR_INSUFFICIENT_BUFFER) Then Begin
      GetMem(pUdpTable,dwSize);
      Res := pGetExtendedUdpTable(pUdpTable, @dwSize, True, AF_INET, UDP_TABLE_OWNER_PID_ALL, 0);
    end;

    if Res = NO_ERROR then begin
      SetLength(Result, pUdpTable.dwNumEntries); // die anzahl der ermittelten einträge stimmt
      for I := 0 to pUdpTable.dwNumEntries-1 do begin
        Result[I].Protocol := 'UDP';
        Result[I].PID := pUdpTable.table[I].dwOwningPid; // aber ab hier steht salat
        Result[I].localIP := pUdpTable.table[I].dwLocalAddr;
        Result[I].localPort := pUdpTable.table[I].dwLocalPort;
        Result[I].remoteIP := 0; // pUdpTable.table[I].dwRemoteAddr;
        Result[I].remotePort := 0; // pUdpTable.table[I].dwRemotePort;
        Result[I].Status := 99; // pUdpTable.table[I].dwState;
        end; // end of for
      end; // end of Res = NO_ERROR
  finally
    If (pUdpTable <> Nil) Then FreeMem(pUdpTable);
  end;
end;

function GetAddress(AIpAdress, APort: DWord): String;
var
  IP: in_addr;
begin
  IP.S_addr := AIpAdress;
  Result := inet_ntoa(IP) + ':' + IntToStr(htons(APort));
end;

var
  UDPArray: ATableOwnerPID;
  I: Integer;
  ResLn: String;
begin
  if LoadApi then begin

    UDPArray := GetUPDTable;
    for I := Low(UDPArray) to High(UDPArray) do begin
      ResLn := IntToStr(UDPArray[I].PID);
      ResLn := ResLn + StringOfchar(' ',5) + UDPArray[I].Protocol;
      ResLn := ResLn + StringOfchar(' ',5) + GetAddress(UDPArray[I].localIP, UDPArray[I].localPort);
      ResLn := ResLn + StringOfchar(' ',5) + GetAddress(UDPArray[I].remoteIP, UDPArray[I].remotePort);

      writeln( ResLn );
      end;

    UnloadApi;
    end else begin
      writeln('Kein API Support.');
      end;
end.
Harry
Gruß Harry
www.H-Soft.info
  Mit Zitat antworten Zitat