address6 - multiple declarations

Function address6

Parses a string containing an IPv6 address.

Option!Address6 address6(R) (
  R range
)
if (isForwardRange!R && is(Unqual!(ElementType!R) == char) && hasLength!R);

This function isn't pure since an IPv6 address can contain interface name or interface ID (separated from the address by %). If an interface name is specified (i.e. first character after % is not a digit), the parser tries to convert it to the ID of that interface. If the interface with the given name can't be found, the parser doesn't fail, but just ignores the invalid interface name, scope ID is 0 then.

If an ID is given (i.e. first character after % is a digit), address6 just stores it in Address6.scopeID without checking whether an interface with this ID really exists. If the ID is invalid (if it is too long or contains non decimal characters), parsing fails and nothing is returned.

If neither an ID nor a name is given, Address6.scopeID is set to 0.

Parameters

NameDescription
R Input range type.
range Stringish range containing the address.

Returns

Option containing the address if the parsing was successful, or nothing otherwise.

Function address6

Constructs an Address6 from raw bytes in network byte order and the scope ID.

Option!Address6 address6(R) (
  R range,
  uint scopeID = 0
)
if (isInputRange!R && is(Unqual!(ElementType!R) == ubyte));

Parameters

NameDescription
R Input range type.
range ubyte range containing the address.
scopeID Scope ID.

Returns

Option containing the address if the range contains exactly 16 bytes, or nothing otherwise.

Example

{
    ubyte[16] actual = [ 1, 2, 3, 4, 5, 6, 7, 8,
                         9, 10, 11, 12, 13, 14, 15, 16 ];
    assert(!address6(actual[]).isNothing);
}
{
    ubyte[15] actual = [ 1, 2, 3, 4, 5, 6, 7, 8,
                         9, 10, 11, 12, 13, 14, 15 ];
    assert(address6(actual[]).isNothing);
}
{
    ubyte[17] actual = [ 1, 2, 3, 4, 5, 6, 7, 8, 9,
                         10, 11, 12, 13, 14, 15, 16, 17 ];
    assert(address6(actual[]).isNothing);
}
{
    assert(address6(cast(ubyte[]) []).isNothing);
}