Getting server IP by host name or url (All OS)

Share your advanced PureBasic knowledge/code with the community.
rodego
User
User
Posts: 10
Joined: Thu Nov 08, 2007 7:39 pm

Getting server IP by host name or url (All OS)

Post 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)))
Marlin
Enthusiast
Enthusiast
Posts: 406
Joined: Sun Sep 17, 2006 1:24 pm
Location: Germany

Re: Getting server IP by host name or url (All OS)

Post by Marlin »

This is nice. :D

Thanks for sharing this rodego.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: Getting server IP by host name or url (All OS)

Post 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 :D
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 :D
I like logic, hence I dislike humans but love computers.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Getting server IP by host name or url (All OS)

Post 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 8)
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Re: Getting server IP by host name or url (All OS)

Post 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?
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
User_Russian
Addict
Addict
Posts: 1519
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Getting server IP by host name or url (All OS)

Post by User_Russian »

With enabled unicode support, it is not working. Function OpenNetworkConnection returns 0.
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: Getting server IP by host name or url (All OS)

Post 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.
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Getting server IP by host name or url (All OS)

Post 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.
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User_Russian
Addict
Addict
Posts: 1519
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Getting server IP by host name or url (All OS)

Post 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
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Getting server IP by host name or url (All OS)

Post 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.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Post Reply