[SOLVED] How to get IP Local Address? lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- Key-Real · 2023-11-22 08:00 How to get IP Local Address? ---------------------------------------------------------------------- balazsszekely · 2023-11-22 08:19 Quote from: Key-Real on November 22, 2023, 09:00:14 amHow to get IP Local Address? Which OS? ---------------------------------------------------------------------- bytebites · 2023-11-22 08:29 This is MacOS subforum. ---------------------------------------------------------------------- KodeZwerg · 2023-11-22 08:36 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function getLocalIP: string;var IPList: TIdStackLocalAddressList; IPStrings: TStringList; i: integer;begin Result := ''; try   IPList := TIdStackLocalAddressList.Create;   try     TIdStack.IncUsage;     try       GStack.GetLocalAddressList(IPList);     finally       TIdStack.DecUsage;     end;      if IPList.Count > 0 then     begin     IPStrings := TStringList.Create;       try         for i := 0 to IPList.Count - 1 do         begin           if IPList[i].IPVersion = Id_IPv4 then           begin             IPStrings.Add(IPList[i].IPAddress + ':' + TIdStackLocalAddressIPv4(IPList[i]).SubNetMask);           end;       end;          if IPStrings.Count > 0 then         begin           Result := IPStrings[0];         end;       finally         IPStrings.Free;       end;     end;   finally     IPList.Free;   end; except   On E: Exception do   begin     Result := '';   end; end;end; ---------------------------------------------------------------------- balazsszekely · 2023-11-22 08:41 @bytebites QuoteThis 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. ---------------------------------------------------------------------- Key-Real · 2023-11-22 12:47 This is a Mac Subforum my question is mac only. is there a solution without using such big packages as Indy? ---------------------------------------------------------------------- KodeZwerg · 2023-11-22 13:13 Quote from: Key-Real on November 22, 2023, 01:47:05 pmis 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. Quote from: GetMem on November 22, 2023, 09:41:32 amAnother 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. ---------------------------------------------------------------------- Key-Real · 2023-11-22 16:46 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 · 2023-11-22 16:57 Quote from: Key-Real on November 22, 2023, 05:46:37 pmOK, 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 · 2023-11-22 17:18 Yes, it works perfect! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure GetIPAddr(var buf: array of char; const len: longint);const  CN_GDNS_ADDR = '127.0.0.1';  CN_GDNS_PORT = 53;var  s: string;  sock: longint;  HostAddr: TSockAddr;  l: integer;  IPAddr: TInetSockAddr;begin  Assert(len >= 16);  sock := fpsocket(AF_INET, SOCK_DGRAM, 0);  assert(sock <> -1);  IPAddr.sin_family := AF_INET;  IPAddr.sin_port := htons(CN_GDNS_PORT);  IPAddr.sin_addr.s_addr := StrToHostAddr(CN_GDNS_ADDR).s_addr;  if (fpConnect(sock, @IPAddr, SizeOf(IPAddr)) <> 0) then exit;  try    l := SizeOf(HostAddr);    if (fpgetsockname(sock, @HostAddr, @l) = 0) then    begin      s := NetAddrToStr(HostAddr.sin_addr);      StrPCopy(PChar(Buf), s);    end;  finally    if (CloseSocket(sock) <> 0) then;  end;end; function GetInternalIP: string;var  IPStr: array[0..30] of char;begin  GetIPAddr(IPStr, SizeOf(IPStr));  Result := IPStr;end;