Recent

Author Topic: [SOLVED] How to get IP Local Address?  (Read 1719 times)

Key-Real

  • Sr. Member
  • ****
  • Posts: 372
[SOLVED] How to get IP Local Address?
« on: November 22, 2023, 09:00:14 am »
How to get IP Local Address?
« Last Edit: November 22, 2023, 06:19:17 pm by Key-Real »

balazsszekely

  • Guest
Re: How to get IP Local Address?
« Reply #1 on: November 22, 2023, 09:19:18 am »
How to get IP Local Address?
Which OS?

bytebites

  • Hero Member
  • *****
  • Posts: 694
Re: How to get IP Local Address?
« Reply #2 on: November 22, 2023, 09:29:26 am »
This is MacOS subforum.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to get IP Local Address?
« Reply #3 on: November 22, 2023, 09:36:23 am »
I have attached a version that uses the Indy package from OPM.
I am unaware if it is compatible to MacOS, tested under Windows.

This is what I do:
Code: Pascal  [Select][+][-]
  1. function getLocalIP: string;
  2. var
  3.  IPList: TIdStackLocalAddressList;
  4.  IPStrings: TStringList;
  5.  i: integer;
  6. begin
  7.  Result := '';
  8.  try
  9.    IPList := TIdStackLocalAddressList.Create;
  10.    try
  11.      TIdStack.IncUsage;
  12.      try
  13.        GStack.GetLocalAddressList(IPList);
  14.      finally
  15.        TIdStack.DecUsage;
  16.      end;
  17.  
  18.      if IPList.Count > 0 then
  19.      begin
  20.        IPStrings := TStringList.Create;
  21.        try
  22.          for i := 0 to IPList.Count - 1 do
  23.          begin
  24.            if IPList[i].IPVersion = Id_IPv4 then
  25.            begin
  26.              IPStrings.Add(IPList[i].IPAddress + ':' + TIdStackLocalAddressIPv4(IPList[i]).SubNetMask);
  27.            end;
  28.          end;
  29.  
  30.          if IPStrings.Count > 0 then
  31.          begin
  32.            Result := IPStrings[0];
  33.          end;
  34.        finally
  35.          IPStrings.Free;
  36.        end;
  37.      end;
  38.    finally
  39.      IPList.Free;
  40.    end;
  41.  except
  42.    On E: Exception do
  43.    begin
  44.      Result := '';
  45.    end;
  46.  end;
  47. end;
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

balazsszekely

  • Guest
Re: How to get IP Local Address?
« Reply #4 on: November 22, 2023, 09:41:32 am »
@bytebites
Quote
This is MacOS subforum.
Ok fair enough. I missed that. :)

Since Indy is cross platform @KodeZwerg's example should work on MacOS too. Another solution is to run command ifconfig with TProcess. Please search the net for the appropriate parameters.
« Last Edit: November 22, 2023, 09:43:56 am by GetMem »

Key-Real

  • Sr. Member
  • ****
  • Posts: 372
Re: How to get IP Local Address?
« Reply #5 on: November 22, 2023, 01:47:05 pm »
This is a Mac Subforum :)

my question is mac only.

is there a solution without using such big packages as Indy?

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to get IP Local Address?
« Reply #6 on: November 22, 2023, 02:13:32 pm »
is there a solution without using such big packages as Indy?
That question GetMen answered already, also Remy answered in your other Thread with same topic.
Another solution is to run command ifconfig with TProcess. Please search the net for the appropriate parameters.
So I question, what has package size to do with your question?
Just install and use it, done.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Key-Real

  • Sr. Member
  • ****
  • Posts: 372
Re: How to get IP Local Address?
« Reply #7 on: November 22, 2023, 05:46:37 pm »
OK, don't wanna use extra packages!

I have getifaddrs(ifap).
It works because I get a valid ifap^.ifa_name.

how to get the ip address string out of ifap^.ifa_addr^ ???

Remy told me to use getnameinfo().
in which unit is getnameinfo()?

for inet_ntop the unit network.pp is not availible on mac

rvk

  • Hero Member
  • *****
  • Posts: 6641
Re: How to get IP Local Address?
« Reply #8 on: November 22, 2023, 05:57:59 pm »
OK, don't wanna use extra packages!
In my upnplib.pas I had a GetIPAddr() for Linux available.

Doesn't that work on Mac OS?
(uses unixtype and netdb)

I think it tries to connect to 127.0.0.1 port 53 and then sees with IP it uses.
I didn't write that and I'm not sure it would work on Mac but you can try.


Key-Real

  • Sr. Member
  • ****
  • Posts: 372
Re: How to get IP Local Address?
« Reply #9 on: November 22, 2023, 06:18:56 pm »
Yes, it works perfect!


Code: Pascal  [Select][+][-]
  1. procedure GetIPAddr(var buf: array of char; const len: longint);
  2. const
  3.   CN_GDNS_ADDR = '127.0.0.1';
  4.   CN_GDNS_PORT = 53;
  5. var
  6.   s: string;
  7.   sock: longint;
  8.   HostAddr: TSockAddr;
  9.   l: integer;
  10.   IPAddr: TInetSockAddr;
  11. begin
  12.   Assert(len >= 16);
  13.   sock := fpsocket(AF_INET, SOCK_DGRAM, 0);
  14.   assert(sock <> -1);
  15.   IPAddr.sin_family := AF_INET;
  16.   IPAddr.sin_port := htons(CN_GDNS_PORT);
  17.   IPAddr.sin_addr.s_addr := StrToHostAddr(CN_GDNS_ADDR).s_addr;
  18.   if (fpConnect(sock, @IPAddr, SizeOf(IPAddr)) <> 0) then exit;
  19.   try
  20.     l := SizeOf(HostAddr);
  21.     if (fpgetsockname(sock, @HostAddr, @l) = 0) then
  22.     begin
  23.       s := NetAddrToStr(HostAddr.sin_addr);
  24.       StrPCopy(PChar(Buf), s);
  25.     end;
  26.   finally
  27.     if (CloseSocket(sock) <> 0) then;
  28.   end;
  29. end;
  30.  
  31. function GetInternalIP: string;
  32. var
  33.   IPStr: array[0..30] of char;
  34. begin
  35.   GetIPAddr(IPStr, SizeOf(IPStr));
  36.   Result := IPStr;
  37. end;
  38.  

 

TinyPortal © 2005-2018