Cidr class final

A CIDR block: a network address and how many leading bits of it the prefix covers, written 10.0.0.0/8 or 2001:db8::/32. Standards: RFC 4632 for v4, RFC 4291 §2.3 for v6.

network is an IpAddress, not a slice of text, so contains masks bits. Match on the text and 10.0.0.0/8 looks like it covers 100.0.0.1. Why: /APPENDIX.md#compose-from-modelled-parts.

Important

Host bits must be clear. 192.168.1.5/24 is refused rather than masked to 192.168.1.0/24, and the failure carries the block most likely meant. Why: APPENDIX.md#cidr-value-type.

Named values: CidrConstants.

final block = Cidr.tryParse('10.0.0.0/8')!;
print(block.asString); // 10.0.0.0/8
print(block.lastAddress.value); // 10.255.255.255
print(block.contains(IpAddress.tryParse('10.1.2.3')!)); // true
print(block.contains(IpAddress.tryParse('100.0.0.1')!)); // false (a text prefix match says true)
print(
  Cidr.parse('192.168.1.5/24').reasonOrNull?.message,
); // has host bits set below the prefix. The network is "192.168.1.0/24"
Annotations
  • @immutable

Properties

asString → String
The canonical text, 10.0.0.0/8. Round-trips through parse.
no setter
hashCode → int
The hash code for this object.
no setteroverride
lastAddress → IpAddress
The last address the block covers, which for v4 is what other tools call the broadcast address. Named for what it is, since IPv6 has no broadcast.
no setter
network → IpAddress
The network address, every bit below prefixLength clear.
final
prefixLength → int
How many leading bits the prefix covers: 0 to 32 for v4, 0 to 128 for v6.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

contains(IpAddress address) → bool
Whether address falls inside this block. A different family is never inside, so a v6 address is not in 10.0.0.0/8.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
override

Operators

operator ==(Object other) → bool
The equality operator.
override

Static Methods

from({required IpAddress network, required int prefixLength}) → ParseOutcome<CidrFailure, Cidr>
The block at network covering prefixLength bits, reporting the CidrFailure when the prefix does not fit the family or network has bits set below it.
parse(String input) → ParseOutcome<CidrFailure, Cidr>
Parses input, reporting the CidrFailure saying which rule broke.
tryParse(String input) → Cidr?
Parses input, or null if it isn't a CIDR block.