Recent

Author Topic: [SOLVED] gethostbyname(gethostname, host) returns random host.addr on MacOs  (Read 3734 times)

Key-Real

  • Full Member
  • ***
  • Posts: 189
I'm trying to get my local IP Address under MacOS.

Code: Pascal  [Select][+][-]
  1. uses netdb;
  2. var
  3.      host : THostEntry;
  4. begin
  5.      gethostbyname(gethostname, host);
  6.      writeln(HostAddrToStr(host.addr));
  7. end.
  8.  

this writes a random ip address :(

what to do?
« Last Edit: November 22, 2023, 06:20:36 pm by Key-Real »

loaded

  • Hero Member
  • *****
  • Posts: 825
Re: gethostbyname(gethostname, host) returns random host.addr on MacOs
« Reply #1 on: November 10, 2023, 01:11:38 pm »
The reason is written here.
https://wiki.freepascal.org/Netdb
If you want to know the LAN or WAN addresses of your computer. You need to use a different method.
I remember an example I wrote on the subject a long time ago. If I can find it I will send it.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

Key-Real

  • Full Member
  • ***
  • Posts: 189
Re: gethostbyname(gethostname, host) returns random host.addr on MacOs
« Reply #2 on: November 10, 2023, 01:44:43 pm »
yes, cnetdb (I use it under linux) .... but it is not there for Mac

loaded

  • Hero Member
  • *****
  • Posts: 825
Re: gethostbyname(gethostname, host) returns random host.addr on MacOs
« Reply #3 on: November 10, 2023, 02:00:37 pm »
yes, cnetdb (I use it under linux) .... but it is not there for Mac
Yes you are right. I was wrong here.  ;D
In my solution it was valid for windows. In this case, I think the advice of someone experienced with MacOs will be useful.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1313
    • Lebeau Software
Re: gethostbyname(gethostname, host) returns random host.addr on MacOs
« Reply #4 on: November 10, 2023, 07:14:59 pm »
I'm trying to get my local IP Address under MacOS.
...
this writes a random ip address :(

Well, for one thing, you are not checking the return value of gethostbyname() for failure before using the data.  For another thing, this is not the best way to get local IPs anyway (even on Windows).  Use getifaddrs() instead (or GetAdapterAddresses() on Windows).
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Key-Real

  • Full Member
  • ***
  • Posts: 189
Re: gethostbyname(gethostname, host) returns random host.addr on MacOs
« Reply #5 on: November 21, 2023, 11:08:18 am »
In which unit is getifaddrs() ?

Key-Real

  • Full Member
  • ***
  • Posts: 189
Re: gethostbyname(gethostname, host) returns random host.addr on MacOs
« Reply #6 on: November 21, 2023, 05:08:54 pm »
I wrote custom bindings for getifaddrs(ifap).
It works because I get the ifap^.ifa_name right!

how to get the ip address string out of ifap^.ifa_addr^ ???
« Last Edit: November 21, 2023, 05:41:47 pm by Key-Real »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1313
    • Lebeau Software
Re: gethostbyname(gethostname, host) returns random host.addr on MacOs
« Reply #7 on: November 21, 2023, 09:17:57 pm »
how to get the ip address string out of ifap^.ifa_addr^ ???

You can use getnameinfo() or inet_ntop().
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Key-Real

  • Full Member
  • ***
  • Posts: 189
Re: gethostbyname(gethostname, host) returns random host.addr on MacOs
« Reply #8 on: November 21, 2023, 09:30:54 pm »
in which unit is getnameinfo.

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


 

TinyPortal © 2005-2018