RejectedSoftware Forums

Sign up

Resolving Host Name to IPv4

Two questions:

  1. How to set the address.family so requestHTTP will use a ipv4 address.
  2. Where do I find the enumeration for the address.family using AFINET results in the following compiler error :source/app.d(8,70): Error: undefined identifier 'AFINET'.
  3. How would I make it work using the web framework and/or the REST Interface.

The following code and execution output demonstrate the problem. In the first line a call to resolveHost with address family and use dns using the default an ipv6 address is returned [2607:f8b0:400d:c02:0:0:0:64]:0

The second line to resolveHost sets the address family set to 2 and use dns set to the default and ipv4 address is returned 98.124.44.234:0.

The call to requestHost results in the execution output detailed below. Essentially it Failed to connect to [2607:f8b0:400b:807:0:0:0:2004]:80 (Network is unreachable)

Any help is sincerely appreciated.

Thanks
John

import vibe.d;
import std.stdio;

import vibe.d;
import std.stdio;

void main()
{

writeln(" with address family and use dns not set ..",resolveHost("google.com"));
writeln(" with address family set to 2 ..",resolveHost("google.com",2));
writeln("");

requestHTTP("http://www.google.com/",
	(scope req) {
		req.method = HTTPMethod.POST;
		//req.writeJsonBody(["name": "My Name"]);
	},
	(scope res) {
		logInfo("Response: %s", res.bodyReader.readAllUTF8());
	}
);

};

Performing "debug" build using dmd for x86_64.
vibe-d:utils 0.7.27: target for configuration "library" is up to date.
vibe-d:data 0.7.27: target for configuration "library" is up to date.
vibe-d:core 0.7.27: target for configuration "libevent" is up to date.
vibe-d:http 0.7.27: target for configuration "library" is up to date.
vibe-d:diet 0.7.27: target for configuration "library" is up to date.
vibe-d:mail 0.7.27: target for configuration "library" is up to date.
vibe-d:mongodb 0.7.27: target for configuration "library" is up to date.
vibe-d:redis 0.7.27: target for configuration "library" is up to date.
vibe-d:web 0.7.27: target for configuration "library" is up to date.
vibe-d 0.7.27: target for configuration "libevent" is up to date.
resolvetest ~master: building configuration "application"...
Linking...
To force a rebuild of up-to-date targets, run again with --force.
Running ./resolvetest
with address family and use dns not set ..[2607:f8b0:400d:c02:0:0:0:64]:0
with address family set to 2 ..98.124.44.234:0

std.exception.ErrnoException@../../../../../../.dub/packages/vibe-d-0.7.27/source/vibe/core/drivers/libevent2.d(320): Failed to connect to [2607:f8b0:400b:807:0:0:0:2004]:80 (Network is unreachable)

/usr/include/dmd/phobos/std/exception.d:615 @safe bool std.exception.enforceEx!(std.exception.ErrnoException).enforceEx!(bool).enforceEx(bool, lazy immutable(char)[], immutable(char)[], ulong) [0x822142]
../../../../../../.dub/packages/vibe-d-0.7.27/source/vibe/core/drivers/utils.d:59 @safe bool vibe.core.drivers.utils.socketEnforce!(bool).socketEnforce(bool, lazy immutable(char)[], immutable(char)[], ulong) [0x7e1146]
../../../../../../.dub/packages/vibe-d-0.7.27/source/vibe/core/drivers/libevent2.d:320 vibe.core.drivers.libevent2tcp.Libevent2TCPConnection vibe.core.drivers.libevent2.Libevent2Driver.connectTCP(vibe.core.net.NetworkAddress) [0x7d2e53]
../../../../../../.dub/packages/vibe-d-0.7.27/source/vibe/core/net.d:99 vibe.core.net.TCPConnection vibe.core.net.connectTCP(vibe.core.net.NetworkAddress) [0x7f04d0]
../../../../../../.dub/packages/vibe-d-0.7.27/source/vibe/core/net.d:95 vibe.core.net.TCPConnection vibe.core.net.connectTCP(immutable(char)[], ushort) [0x7f04a0]
../../../../../../.dub/packages/vibe-d-0.7.27/source/vibe/http/client.d:534 bool vibe.http.client.HTTPClient.doRequest(scope void delegate(vibe.http.client.HTTPClientRequest), bool*, bool, std.datetime.SysTime) [0x779102]
../../../../../../.dub/packages/vibe-d-0.7.27/source/vibe/http/client.d:472 bool vibe.http.client.HTTPClient.doRequestWithRetry(scope void delegate(vibe.http.client.HTTPClientRequest), bool, out bool, out std.datetime.SysTime) [0x778ebc]
../../../../../../.dub/packages/vibe-d-0.7.27/source/vibe/http/client.d:406 void vibe.http.client.HTTPClient.request(scope void delegate(scope vibe.http.client.HTTPClientRequest), scope void delegate(scope vibe.http.client.HTTPClientResponse)) [0x7789c1]
../../../../../../.dub/packages/vibe-d-0.7.27/source/vibe/http/client.d:121 void vibe.http.client.requestHTTP(vibe.inet.url.URL, scope void delegate(scope vibe.http.client.HTTPClientRequest), scope void delegate(scope vibe.http.client.HTTPClientResponse), vibe.http.client.HTTPClientSettings) [0x777c2a]
../../../../../../.dub/packages/vibe-d-0.7.27/source/vibe/http/client.d:106 void vibe.http.client.requestHTTP(immutable(char)[], scope void delegate(scope vibe.http.client.HTTPClientRequest), scope void delegate(scope vibe.http.client.HTTPClientResponse), vibe.http.client.HTTPClientSettings) [0x777993]
source/app.d:16
Dmain [0x6e41df]
??:? D2rt6dmain211drunmainUiPPaPUAAaZiZ6runAllMFZ9lambda1MFZv [0x87481a]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x874770]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll() [0x8747d6]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x874770]
??:? _d_run_main [0x8746cd]
??:? main [0x6f3917]
??:?
libcstartmain [0xc5be7ec4]
Program exited with code 1

Re: Resolving Host Name to IPv4

Am 22.02.2016 um 04:30 schrieb John More:

Two questions:

  1. How to set the address.family so requestHTTP will use a ipv4 address.

Currently this can't be accessed. A way to add this with minimal changes
would be to control the DNS resolver using the address family of the
HTTPClientSettings.networkInterface. By default, this is AF_UNKNOWN,
so the default behavior would stay unaffected.

However, the network interface might be IPv6, while there could still be
an IPv4 gateway in-between, so it probably makes more sense to add a new
settings field for this.

  1. Where do I find the enumeration for the address.family using AFINET results in the following compiler error :source/app.d(8,70): Error: undefined identifier 'AFINET'.

AF_INET is in the system headers somewhere in
core.sys.posix/core.sys.windows. However, you can also use the
AddressFamily enum in std.socket.

  1. How would I make it work using the web framework and/or the REST Interface.

I'll add a RestInterfaceSettings.httpClientSettings to enable
controlling the HTTP requests.

Re: Resolving Host Name to IPv4

On Mon, 22 Feb 2016 13:14:31 +0100, Sönke Ludwig wrote:

Am 22.02.2016 um 04:30 schrieb John More:

Two questions:

  1. How to set the address.family so requestHTTP will use a ipv4 address.

Currently this can't be accessed. A way to add this with minimal changes
would be to control the DNS resolver using the address family of the
HTTPClientSettings.networkInterface. By default, this is AF_UNKNOWN,
so the default behavior would stay unaffected.

However, the network interface might be IPv6, while there could still be
an IPv4 gateway in-between, so it probably makes more sense to add a new
settings field for this.

  1. Where do I find the enumeration for the address.family using AFINET results in the following compiler error :source/app.d(8,70): Error: undefined identifier 'AFINET'.

AF_INET is in the system headers somewhere in
core.sys.posix/core.sys.windows. However, you can also use the
AddressFamily enum in std.socket.

  1. How would I make it work using the web framework and/or the REST Interface.

I'll add a RestInterfaceSettings.httpClientSettings to enable
controlling the HTTP requests.

Thanks for the quick reply. I can make this work for now simply by using the resolveHost and take the address I get the make the request. I did look at the Linux host command which actually requests both the ipv4 and ipv6 addresses. Perhaps both should be returned and then allow the user to set a precedence and if that fails use the other.

Thanks again.