After it ran immediately on Windows and macOS, it did not run on Linux at first.
When I noticed the next day that the structures were minimally different, it also worked.

update v1.01.2
- Fixed structures
update v1.01.4
- Fixed memory-leak
Update v1.02.0
- Some fixed and optimization
Code: Select all
;-TOP
; *************************************************
; Comment : Network Get Address From Host (All OS)
; Authors : mk-soft, infratec
; Version : v1.02.0
; Create : 31.12.2022
; Update : 12.08.2023
; Link : https://www.purebasic.fr/english/viewtopic.php?t=80387
; Description
; Family : PF_UNSPEC IPv4 and IPv6
; Family : PF_INET IPv4
; Family : PF_INET6 IPv6
EnableExplicit
;-- Imports
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
Import ""
CompilerElse
ImportC ""
CompilerEndIf
getaddrinfo(NodeName.p-Ascii, ServiceName.p-Ascii, *pHints, *ppResult)
freeaddrinfo(*ppResult)
inet_ntop(Family, *pAddr, pStringBuf, StringBufSize)
EndImport
;-- Contants
#AF_UNSPEC = 0
#AF_INET = 2
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
#AF_INET6 = 23
CompilerCase #PB_OS_Linux
#AF_INET6 = 10
CompilerCase #PB_OS_MacOS
#AF_INET6 = 30
CompilerEndSelect
#PF_UNSPEC = #AF_UNSPEC
#PF_INET = #AF_INET
#PF_INET6 = #AF_INET6
#AI_CANONNAME = 2
#SOCK_STREAM = 1
;-- Structures
CompilerIf Not Defined(in_addr, #PB_Structure)
Structure in_addr Align #PB_Structure_AlignC
StructureUnion
s_b.a[4]
s_w.u[2]
s_addr.l
EndStructureUnion
EndStructure
CompilerEndIf
CompilerIf Not Defined(in6_addr, #PB_Structure)
Structure in6_addr Align #PB_Structure_AlignC
s6_addr.a[16]
EndStructure
CompilerEndIf
CompilerIf Not Defined(SOCKADDR, #PB_Structure)
Structure SOCKADDR Align #PB_Structure_AlignC
sa_family.u
sa_data.a[14]
EndStructure
CompilerEndIf
CompilerIf Not Defined(SOCKADDR_IN4, #PB_Structure)
Structure SOCKADDR_IN4 Align #PB_Structure_AlignC
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
sin_len.a
sin_family.a
CompilerElse
sin_family.u
CompilerEndIf
sin_port.u
sin_addr.in_addr
sin_zero.a[8]
EndStructure
CompilerEndIf
CompilerIf Not Defined(SOCKADDR_IN6, #PB_Structure)
Structure SOCKADDR_IN6 Align #PB_Structure_AlignC
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
sin6_len.a
sin6_family.a
CompilerElse
sin6_family.u
CompilerEndIf
sin6_port.u
sin6_flowinfo.l
sin6_addr.in6_addr
sin6_scope_id.l
EndStructure
CompilerEndIf
CompilerIf #PB_Compiler_OS = #PB_OS_Linux
Structure addrinfo Align #PB_Structure_AlignC
ai_flags.l
ai_family.l
ai_socktype.l
ai_protocol.l
ai_addrlen.l
StructureUnion
*ai_addr.SOCKADDR
*ai_addr_in.SOCKADDR_IN4
*ai_addr_in6.SOCKADDR_IN6
EndStructureUnion
*ai_canonname
*ai_next.addrinfo
EndStructure
CompilerElse
Structure addrinfo Align #PB_Structure_AlignC
ai_flags.l
ai_family.l
ai_socktype.l
ai_protocol.l
ai_addrlen.l
*ai_canonname
StructureUnion
*ai_addr.SOCKADDR
*ai_addr_in.SOCKADDR_IN4
*ai_addr_in6.SOCKADDR_IN6
EndStructureUnion
*ai_next.addrinfo
EndStructure
CompilerEndIf
;-- Functions
Procedure.s GetAddrString(HostName.s, Family = #AF_UNSPEC, CanonName = #False)
Protected r1.s
Protected hints.addrinfo, *res.addrinfo, *result.addrinfo
Protected errcode.l
Protected *addrstr
Protected *ptr4.in_addr, *ptr6.in6_addr
Protected name.s
hints\ai_family = Family
hints\ai_socktype = #SOCK_STREAM
If CanonName
hints\ai_flags = #AI_CANONNAME
Else
hints\ai_flags = 0
EndIf
errcode = getaddrinfo(Hostname, #Empty$, hints, @*result)
If errcode = 0
*addrstr = AllocateMemory(258)
*res = *result
While *res
Select (*res\ai_family)
Case #AF_INET
If *res\ai_addr
*ptr4 = @*res\ai_addr_in\sin_addr
If inet_ntop(*res\ai_family, *ptr4, *addrstr, 256)
r1 + PeekS(*addrstr, -1, #PB_Ascii)
EndIf
EndIf
Case #AF_INET6
If *res\ai_addr
*ptr6 = @*res\ai_addr_in6\sin6_addr
If inet_ntop(*res\ai_family, *ptr6, *addrstr, 256)
r1 + PeekS(*addrstr, -1, #PB_Ascii)
EndIf
EndIf
EndSelect
If CanonName And *res\ai_canonname
name = PeekS(*res\ai_canonname, -1, #PB_Ascii)
EndIf
*res = *res\ai_next
If *res
r1 + ";"
EndIf
Wend
If CanonName
r1 + ";" + name
EndIf
FreeMemory(*addrstr)
freeaddrinfo(*result)
Else
; Error
EndIf
ProcedureReturn r1
EndProcedure
; ****
CompilerIf #PB_Compiler_IsMainFile
;- Example
Define r1.s, host.s
host = "localhost" ; local network
r1.s = GetAddrString(host, #PF_INET6, #True)
Debug host + "/ " + r1
host = "raspberrypi" ; local network
r1.s = GetAddrString(host, #PF_UNSPEC, #True)
Debug host + "/ " + r1
host = "michaels-mini-2" ; local network
r1.s = GetAddrString(host, #PF_INET, #True)
Debug host + "/ " + r1
host = "www.purebasic.fr"
r1.s = GetAddrString(host, #PF_UNSPEC, #True)
Debug host + "/ " + r1
host = "raspberry.com" ; internet
r1.s = GetAddrString(host, #PF_UNSPEC, #True)
Debug host + "/ " + r1
CompilerEndIf