1

I was looking at the std.socket and I was able to do the following to check the IP type but I can't find a way to get the byte array for the IP address.

auto results = getAddressInfo(ipaddress, AddressInfoFlags.NUMERICHOST);

if (results.length && results[0].family == AddressFamily.INET) {
    writeln("IPv4 : " ~ results[0].address.toAddrString());
    return 4;
}
else if (results.length && results[0].family == AddressFamily.INET6) {
    writeln("IPv6 : " ~ results[0].address.toAddrString());
    return 6;
}

I saw a couple of classes like InternetAddress and Internet6Address which have the addr() property that returns the byte array but I am not sure how to go from my code above to these 2 classes.

1 Answer 1

1

One way to do this is to get a pointer to the core.sys.posix.sys.socket.sockaddr structure using the Address.name. Something like auto saddr_ptr = results[0].address.name; would do. Then you can extract the data you need by casting this pointer to a pointer of appropriate type (depending on the address family).

Here is a modified example from getAddress() documentation which uses sockaddr* to give you addresses as uint32_t values and conver them to ubyte[4] arrays:

module main;

import std.stdio;
import std.socket;
import std.c.linux.socket: sockaddr_in, in_addr;

void main(string[] args) {
    // auto results = getAddressInfo("www.digitalmars.com");

    try {
        Address[] addresses = getAddress("dlang.org", 80);
        writefln("  %d addresses found.", addresses.length);
        foreach (int i, Address a; addresses) {

            writefln("  Address %d:", i+1);
            writefln("    IP address: %s", a.toAddrString());
            writefln("    Hostname: %s", a.toHostNameString());
            writefln("    Port: %s", a.toPortString());
            writefln("    Service name: %s", a.toServiceNameString());
            auto saddr_ptr = a.name;
            auto in_ptr = *(cast(sockaddr_in*) saddr_ptr);
            // let's convert it to ubyte[4] so we can access individual
            // parts  of the IPv4 address.
            writeln(cast(ubyte[4])(in_ptr.sin_addr));
        }
    } catch (SocketException e) {
        writefln("  Lookup error: %s", e.msg);
    }

    // Lets the user press <Return> before program returns
    stdin.readln();
}

Output:

  3 addresses found.
  Address 1:
    IP address: 162.217.114.56
    Hostname: digitalmars.com
    Port: 80
    Service name: http
[162, 217, 114, 56]
  Address 2:
    IP address: 162.217.114.56
    Hostname: digitalmars.com
    Port: 80
    Service name: http
[162, 217, 114, 56]
  Address 3:
    IP address: 162.217.114.56
    Hostname: digitalmars.com
    Port: 80
    Service name: http
[162, 217, 114, 56]
Sign up to request clarification or add additional context in comments.

1 Comment

Encountered error when trying to import std.c.linux.socket as it is deprecated. So just went ahead with uint ipno = new InternetAddress(ip, 80).addr();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.