Page 1 of 1
Getting server IP by host name or url (All OS)
Posted: Thu Jan 13, 2011 12:59 pm
by rodego
Code: Select all
Procedure.l GetIPByHost(Host$="")
Result.l=0
If Host$=""
Host$=Hostname()
EndIf
SelfLoop=OpenNetworkConnection(Host$,0,#PB_Network_UDP)
If SelfLoop
Result=GetClientIP(SelfLoop)
CloseNetworkConnection(SelfLoop)
EndIf
ProcedureReturn Result
EndProcedure
No real network connection (becouse it`s UDP;)). Fast and simple)))
Re: Getting server IP by host name or url (All OS)
Posted: Thu Jan 13, 2011 1:13 pm
by Marlin
This is nice.
Thanks for sharing this rodego.
Re: Getting server IP by host name or url (All OS)
Posted: Fri Jan 21, 2011 11:23 pm
by Joakim Christiansen
rodego wrote:Code: Select all
Procedure.l GetIPByHost(Host$="")
Result.l=0
If Host$=""
Host$=Hostname()
EndIf
SelfLoop=OpenNetworkConnection(Host$,0,#PB_Network_UDP)
If SelfLoop
Result=GetClientIP(SelfLoop)
CloseNetworkConnection(SelfLoop)
EndIf
ProcedureReturn Result
EndProcedure
No real network connection (becouse it`s UDP;)). Fast and simple)))
What is it actually doing? (port 0)
EDIT:
Okay, I get it. The system has to resolve the IP (using whatever DNS server assigned to it) so your program is able to send UDP traffic too it

Sorry for being slow in my head this evening... I like your solution!
I once researched how to do the same without using any DNS server, but I never automated that procedure, hmm, maybe I should

Re: Getting server IP by host name or url (All OS)
Posted: Tue Jan 12, 2016 9:51 pm
by Lunasole
Very cool hack, just tested and it works fine on WinXP/Win7/Linux, looks like a perfect way to resolve DNS, without messing with APIs, etc.
It is working with any port, not only 0.
And it also is strange that firewalls (Outpost Firewall Pro, windows default and maybe some others) have no any reaction to this. Well, that can be explained this way that DNS queries are sent from the name of svchost.exe on Windows, your program does nothing but calling OS api. So the only way to block it is to use proactive stuff to detect API calls, or to block svchost outcoming DNS queries, but then you will be unable to resolve any hostname from any app

Re: Getting server IP by host name or url (All OS)
Posted: Thu Jan 14, 2016 10:30 pm
by dell_jockey
Lunasole wrote:Very cool hack, just tested and it works fine on WinXP/Win7/Linux, looks like a perfect way to resolve DNS, without messing with APIs, etc
Perhaps this assertion is only correct if the remote site's firewall administrator indeed allows UDP connects?
Re: Getting server IP by host name or url (All OS)
Posted: Fri Jan 15, 2016 10:46 am
by User_Russian
With enabled unicode support, it is not working. Function OpenNetworkConnection returns 0.
Re: Getting server IP by host name or url (All OS)
Posted: Sat Jan 16, 2016 8:52 am
by Bisonte
User_Russian wrote:With enabled unicode support, it is not working. Function OpenNetworkConnection returns 0.
NOT confirmed. I use only the unicode compiler option, and the ip's are all correct !
I'm using Win 10 x64 here.
Re: Getting server IP by host name or url (All OS)
Posted: Sat Jan 16, 2016 11:39 am
by bbanelli
Bisonte wrote:User_Russian wrote:With enabled unicode support, it is not working. Function OpenNetworkConnection returns 0.
NOT confirmed. I use only the unicode compiler option, and the ip's are all correct !
I'm using Win 10 x64 here.
Also
NOT verified, Windows 7 x64 and Windows XP x86.
Re: Getting server IP by host name or url (All OS)
Posted: Sat Jan 16, 2016 1:48 pm
by User_Russian
Maybe it's because of the Cyrillic alphabet. Function Hostname() returns "компьютер".
With disabled of support for Unicode, normal working. But if enable support for Unicode, the function OpenNetworkConnection returns 0.
Because of these bugs, I am opposed to exclusion support ascii.
http://www.purebasic.fr/english/viewtop ... 14&t=60214
Re: Getting server IP by host name or url (All OS)
Posted: Sat Jan 16, 2016 7:49 pm
by Lunasole
dell_jockey wrote:
Perhaps this assertion is only correct if the remote site's firewall administrator indeed allows UDP connects?
No, nothing is sent to remote server, actually nothing is sent at all by application using this code, the only thing it does with network is regular DNS-query sent by OS [if needed and if possible]
User_Russian wrote:With enabled unicode support, it is not working. Function OpenNetworkConnection returns 0.
I'm always compiling with unicode, just finished my client-server based TCP tunneling app (using own simple protocol fully encrypted, to pack original stream), tested on 3 PCs over Internet [2x Win7 x64 and 1 XP x32, all using 32bit exe] and everything worked fine resolving DDNS-host with this stuff.
But of course need more detailed results to be sure, all that Win10/8/Vista, different linux builds, both for 32/64 etc.