The below is a basic short example that resolves an IPv6 or IPv4 URL address to an IP address and also provides the host for that resolved IP address. If URL entered is an IPv4 address the familiar dotted-decimal IP address is given, for a IPv6 URL the IPv6 hexidecimal Ip address is given, along with the hosts (reverse DNS) for the resolved IP address. For Windows Vista and up (winsock version 2.2), tested mostly with and originally intended for Windows 7 and Windows 8.
Code: Select all
Prototype PGetAddrInfoEx(pName, pServiceName, dwNameSpace, lpNspId, pHints, ppResult, timeout, lpOverlapped, lpCompletionRoutine, lpNameHandle)
Prototype PWSAStartup(wVersionRequested, lpWSAData)
Prototype PWSACleanup()
Prototype PFreeAddrInfoEx(ai)
Prototype PRtlZeroMemory(mem, sze)
Prototype Pgetnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
Prototype PInetNtop(Family, pAddr, pStringBuf, StringBufSize)
Global GetAddrInfoEx.PGetAddrInfoEx
Global WSAStartup.PWSAStartup
Global WSACleanup.PWSACleanup
Global FreeAddrInfoEx.PFreeAddrInfoEx
Global ZeroMemory.PRtlZeroMemory
Global getnameinfo.Pgetnameinfo
Global InetNtop.PInetNtop
Structure IN6_ADDR ;->Structure in6_addr
StructureUnion
Byte.b[16]
Word.w[8]
EndStructureUnion
EndStructure
Structure sockaddr_in6 ;->Structure sockaddr_in6
sin6_family.w
sin6_port.w
sin6_flowinfo.l
sin6_addr.IN6_ADDR
sin6_scope_id.l
EndStructure
Structure ADDRINFOEX ;->Structure ADDRINFOEX
ai_flags.i
ai_family.i
ai_socktype.i
ai_protocol.i
ai_addrlen.i
*ai_canonname
*ai_addr.SOCKET_ADDRESS
*ai_blob
ai_bloblen.i
ai_provider.l
*ai_next.ADDRINFOEX
EndStructure
#AF_INET6 = 23
#INET6_ADDRSTRLEN = 65
#INET_ADDRSTRLEN = 22
#NS_DNS = 12
#NI_MAXHOST = 1025
Procedure LoadLibraries()
Global Lib_Winsock.l, Lib_Kernel32.l
Lib_Winsock = OpenLibrary(#PB_Any,"ws2_32.dll")
If IsLibrary(Lib_Winsock)<> 0
WSAStartup.PWSAStartup=GetFunction(Lib_Winsock,"WSAStartup")
WSACleanup.PWSACleanup=GetFunction(Lib_Winsock,"WSACleanup")
CompilerIf #PB_Compiler_Unicode ; if compiled in unicode use this
FreeAddrInfoEx.PFreeAddrInfoEx=GetFunction(Lib_Winsock,"FreeAddrInfoExW")
GetAddrInfoEx.PGetAddrInfoEx=GetFunction(Lib_Winsock,"GetAddrInfoExW")
getnameinfo.Pgetnameinfo=GetFunction(Lib_Winsock,"GetNameInfoW")
InetNtop.PInetNtop=GetFunction(Lib_Winsock,"InetNtopW")
CompilerElse ; if not compiled in unicode use this
FreeAddrInfoEx.PFreeAddrInfoEx=GetFunction(Lib_Winsock,"FreeAddrInfoEx")
GetAddrInfoEx.PGetAddrInfoEx=GetFunction(Lib_Winsock,"GetAddrInfoExA")
getnameinfo.Pgetnameinfo=GetFunction(Lib_Winsock,"getnameinfo")
InetNtop.PInetNtop=GetFunction(Lib_Winsock,"inet_ntop")
CompilerEndIf
EndIf
Lib_Kernel32 = OpenLibrary(#PB_Any,"Kernel32.dll")
If IsLibrary(Lib_Kernel32)<> 0
ZeroMemory.PRtlZeroMemory=GetFunction(Lib_Kernel32,"RtlZeroMemory")
EndIf
EndProcedure
Macro MAKEWORD(a, b)
(a & $FF)|((b & $FF)<<8)
EndMacro
Procedure.s GetIPFromURL(ip$)
Protected hints.ADDRINFOEX, *res.ADDRINFOEX, *p.ADDRINFOEX
Protected ipstr.s, inurl$ = ip$
ZeroMemory(@hints, SizeOf(hints))
hints\ai_family = #AF_UNSPEC
hints\ai_socktype = #SOCK_STREAM
status.i = GetAddrInfoEx(@inurl$, #Null, #NS_DNS, #Null, @hints, @*res, #Null, #Null, #Null, #Null)
If status > #NO_ERROR
FreeAddrInfoEx(*res)
ProcedureReturn Str(0)
EndIf
*p=*res
If *p\ai_family = #AF_INET
*ipv4.sockaddr_in = *p\ai_addr
*addr = @*ipv4\sin_addr
ipstr_spc.i = #INET_ADDRSTRLEN
host_name.s = Space(#NI_MAXHOST)
getnameinfo(*p\ai_addr, SizeOf(sockaddr_in), @host_name, #NI_MAXHOST, #Null, #Null, 0)
ipver$ = "IPv4"
ElseIf *p\ai_family = #AF_INET6
*ipv6.sockaddr_in6 = *p\ai_addr
*addr = *ipv6\sin6_addr
ipstr_spc.i = #INET6_ADDRSTRLEN
host_name.s = Space(#NI_MAXHOST)
getnameinfo(*p\ai_addr, SizeOf(sockaddr_in6), @host_name, #NI_MAXHOST, #Null, #Null, 0)
ipver$ = "IPv6"
EndIf
ipstr.s = Space(ipstr_spc)
InetNtop(*p\ai_family, *addr, @ipstr, Len(ipstr))
FreeAddrInfoEx(*res)
ProcedureReturn "Address entered was : " + inurl$ + " - The " + ipver$ + " address is : " + ipstr + " - which resolves to host name : " + host_name
EndProcedure
LoadLibraries()
wVersionRequested.w = MAKEWORD(2,2)
WSAStart.i = WSAStartup(wVersionRequested,@wsaData.WSADATA)
Debug GetIPFromURL("www.v6.facebook.com")
Debug GetIPFromURL("ipv6.google.com")
Debug GetIPFromURL("www.google.com")
Debug GetIPFromURL("www.purebasic.com")
Debug GetIPFromURL("2620:0:1cfe:face:b00c::3")
Debug GetIPFromURL("74.125.130.106")
WSACleanup()
If Lib_Winsock : CloseLibrary(Lib_Winsock) : EndIf
If Lib_Kernel32 : CloseLibrary(Lib_Kernel32) : EndIf