ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

DonationCoder.com Software > Post New Requests Here

Show IP Address from domain name

<< < (3/4) > >>

KodeZwerg:
i do not have delphi at hand, so here is code that does what you want, convert domain-name (example: google.com) into ip-adress.
unicode, ip4 & ip6 supported

--- ---uses
  Winapi.Winsock2;

type
  PAddrInfo = ^TAddrInfo;
  TAddrInfo = packed record
    ai_flags: integer;
    ai_family: integer;
    ai_socktype: integer;
    ai_protocol: integer;
    ai_addrlen: NativeInt;
    ai_canonname: PCHAR;
    ai_addr: PSOCKADDR;
    ai_next: PAddrInfo;
  end;

function GetAddrInfo(const nodeName: PCHAR; const serviceName : PChar; const hints: PAddrInfo; var result: PAddrInfo): integer; stdcall; external 'ws2_32.dll' name 'GetAddrInfoW';
procedure FreeAddrInfo(const addrInfo: PAddrInfo); stdcall; external 'ws2_32.dll' name 'FreeAddrInfoW';

function ResolveIpAddress(const hostName: string; const ipv6: boolean): string;
const
  BUFFER_SIZE = 32768;
var
  data: TWSAData;
  error: integer;
  requestError: integer;
  r: PAddrInfo;
  s: string;
  hints: TAddrInfo;
  buffer: TArray<byte>;
  length: DWORD;
begin
  error:= WSAStartup(MAKEWORD(2, 2), data);
  try
    if (error = 0)  then
    begin
      r:= nil;
      try
        ZeroMemory(@hints, sizeof(TAddrInfo));
        if (ipv6) then
          hints.ai_family:= AF_INET6
        else
          hints.ai_family:= AF_INET;

        requestError:= GetAddrInfo(PCHAR(hostName), nil, @hints, r);
        if (requestError = 0) then
        begin
          length:= BUFFER_SIZE;
          SetLength(buffer, BUFFER_SIZE);
          if (WSAAddressToString(r.ai_addr^, r.ai_addrlen, nil, @buffer[0], length) = 0) then
          begin
            setLength(buffer, length * 2);
            s:= TUnicodeEncoding.Unicode.GetString(@buffer[0]);
            exit(s);
          end
          else
            exit('0.0.0.0');
        end
        else
          exit('0.0.0.0');

      finally
        FreeAddrInfo(r);
      end;
    end
  finally
    if (error = 0) then
      WSACleanup();
  end;
end;
if 0.0.0.0 is returned, there was an error

publicdomain:
Vic, that is awesome ! Thank you.-davcom (October 18, 2020, 02:38 PM)
--- End quote ---
My pleasure! :) Currently adding more features toward first public release later today/tomorrow [code finished @ GitHub] :Thmbsup:



Downloading from Github now.-davcom (October 18, 2020, 02:38 PM)
--- End quote ---
Just pushed new code @ GitHub repo. Stay tuned!

davcom:
Definitely very tuned :)

KodeZwerg:
Cool @vic, i just saw first preview image where ip was missing (reason of this topic) thats why i posted code.
Since it changed now I do not interfere with my own resolver-project anymore (works for IPv4/IPv6/DNStoIP/IPtoDNS)  :Thmbsup:

publicdomain:
Definitely very tuned :)
-davcom (October 19, 2020, 05:33 PM)
--- End quote ---

Thank you! Just released @ DC members space :Thmbsup:

Release: Dynamic IP Monitor v0.1.0

Cool @vic, i just saw first preview image where ip was missing (reason of this topic) thats why i posted code.-KodeZwerg (October 20, 2020, 03:25 AM)
--- End quote ---

Appreciated :)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version