RejectedSoftware Forums

Sign up

lookup IP ?

I don't get the usage of resolveHost()...

How can I query the IP address of the server my vibe app is running on ?

Re: lookup IP ?

On Mon, 11 Nov 2013 16:08:37 GMT, Stephan Dilly wrote:

I don't get the usage of resolveHost()...

It allows to resolve any host to IP via DNS query (to DNS server configured in the system)

How can I query the IP address of the server my vibe app is running on ?

You may be surprised how terribly imprecise this question is :) What do you exactly mean by this? IP of the interface(s) you listen on? Public IP as seen by external services for every listening interface? Former should be unknown to vibe.d itself if mask was used as listen address (0.0.0.0) but can be queried from the OS (it is OS socket magic). Latter is undefined in general case as it is completely routing-defined.

Re: lookup IP ?

On Mon, 11 Nov 2013 16:36:56 GMT, Dicebot wrote:

On Mon, 11 Nov 2013 16:08:37 GMT, Stephan Dilly wrote:

I don't get the usage of resolveHost()...

It allows to resolve any host to IP via DNS query (to DNS server configured in the system)

How can I query the IP address of the server my vibe app is running on ?

You may be surprised how terribly imprecise this question is :) What do you exactly mean by this? IP of the interface(s) you listen on? Public IP as seen by external services for every listening interface? Former should be unknown to vibe.d itself if mask was used as listen address (0.0.0.0) but can be queried from the OS (it is OS socket magic). Latter is undefined in general case as it is completely routing-defined.

I just know that I was able to query my ip on a nodejs server using dns.resolve(OS.hostname()) and it was the public IP as seen by external services for every listening interface.

Re: lookup IP ?

On Mon, 11 Nov 2013 19:26:00 GMT, Stephan Dilly wrote:

I just know that I was able to query my ip on a nodejs server using dns.resolve(OS.hostname()) and it was the public IP as seen by external services for every listening interface.

This should be equivalent:

import std.socket : Socket;
import vibe.core.net : resolvehost;

auto ip = resolveHost(Socket.hostName);

Re: lookup IP ?

Am 11.11.2013 17:08, schrieb Stephan Dilly:

I don't get the usage of resolveHost()...

How can I query the IP address of the server my vibe app is running on ?

You should be able to get that using core.sys.posix.unistd.gethostname
and by passing the result as a string to resolveHost. If you need the
textual representation of the address, there is
core.sys.posix.arpa.inet_ntoa.

These functions will eventually be available by some vibe.d function,
but aren't yet because of differences between Posix systems and Windows
(and different Windows versions).

Re: lookup IP ?

Am 11.11.2013 21:24, schrieb Dicebot:

On Mon, 11 Nov 2013 19:26:00 GMT, Stephan Dilly wrote:

I just know that I was able to query my ip on a nodejs server using dns.resolve(OS.hostname()) and it was the public IP as seen by external services for every listening interface.

This should be equivalent:

import std.socket : Socket;
import vibe.core.net : resolvehost;

auto ip = resolveHost(Socket.hostName);

Socket.hostName is nicer of course.

Re: lookup IP ?

However, as I have already said, I really don't recommend using such detection in any real project as it does not do what you may think it does. It essentially figures out what IP system hostname resolves to from the server itself. It is extremely reliant on server and network configuration and only happens to work if this hostname is exported to global DNS servers and your HTTP server is bound exactly to it and no local DNS server interferes. And probably lot of other possible issues that did not came to my mind at once.

What to you want to use it for? Maybe I can recommend a more robust alternative?

Re: lookup IP ?

On Mon, 11 Nov 2013 20:29:09 GMT, Dicebot wrote:

However, as I have already said, I really don't recommend using such detection in any real project as it does not do what you may think it does. It essentially figures out what IP system hostname resolves to from the server itself. It is extremely reliant on server and network configuration and only happens to work if this hostname is exported to global DNS servers and your HTTP server is bound exactly to it and no local DNS server interferes. And probably lot of other possible issues that did not came to my mind at once.

What to you want to use it for? Maybe I can recommend a more robust alternative?

Well it is just for logging purpose. My vibe.d server backend reports whenever it starts (it is running as a monitored process) via email. I wanted to add the ip it is running on simply because i was used to do that with the nodejs code that served that same purpose befor. it helped me to quickly see which of those servers it was where the process had ended and got restarted.

Re: lookup IP ?

On Mon, 11 Nov 2013 21:26:24 +0100, Sönke Ludwig wrote:

Am 11.11.2013 21:24, schrieb Dicebot:

On Mon, 11 Nov 2013 19:26:00 GMT, Stephan Dilly wrote:

I just know that I was able to query my ip on a nodejs server using dns.resolve(OS.hostname()) and it was the public IP as seen by external services for every listening interface.

This should be equivalent:

import std.socket : Socket;
import vibe.core.net : resolvehost;

auto ip = resolveHost(Socket.hostName);

Socket.hostName is nicer of course.

This crashes on win32 with this:
object.Exception@C:\Users\Stephan\AppData\Roaming\dub\packages\vibe-d-master\source\vibe\core\drivers\libevent2.d(201): Invalid IP address string: <PC-Name>

Re: lookup IP ?

On Tue, 12 Nov 2013 00:19:39 GMT, Stephan Dilly wrote:

On Mon, 11 Nov 2013 21:26:24 +0100, Sönke Ludwig wrote:

Am 11.11.2013 21:24, schrieb Dicebot:

On Mon, 11 Nov 2013 19:26:00 GMT, Stephan Dilly wrote:

I just know that I was able to query my ip on a nodejs server using dns.resolve(OS.hostname()) and it was the public IP as seen by external services for every listening interface.

This should be equivalent:

import std.socket : Socket;
import vibe.core.net : resolvehost;

auto ip = resolveHost(Socket.hostName);

Socket.hostName is nicer of course.

This crashes on win32 with this:
object.Exception@C:\Users\Stephan\AppData\Roaming\dub\packages\vibe-d-master\source\vibe\core\drivers\libevent2.d(201): Invalid IP address string: <PC-Name>

Did you set the no_dns parameter of resolveHost to true? It needs to be false in order to resolve host names. But there is an additional issue - libevent doesn't parse the local "hosts" file, so if the host name is not registered in DNS, it will still fail.

As an alternative, I'd recommend to use "subConfigurations": {"vibe.d": "win32"} to use the native Win32 driver instead of libevent.