ribs_ip 1.0.0-dev.6
ribs_ip: ^1.0.0-dev.6 copied to clipboard
Typesafe representations for hosts, ports and addresses.
ribs_ip #
ribs_ip is a type-safe, functional library for working with network addresses in Dart. It provides robust representations for IP addresses (IPv4 and IPv6), Hostnames, Ports, and CIDR network ranges.
Designed for the ribs ecosystem, it leverages Option for safer parsing and IO for side-effecting operations like DNS resolution.
Features #
- Type-Safe IP Addresses: Distinguished types for
Ipv4Address,Ipv6Address, and their common baseIpAddress. - Hostname Support: Validation and normalization of hostnames (including IDN/Punycode support).
- Network Ranges: CIDR notation support (
192.168.1.0/24) with methods for contains checks, masking, and finding the first/last address in a range. - DNS Operations: Resolve hostnames to IP addresses and perform reverse lookups.
- Port Management: Validated
Porttype (0-65535). - Socket Addresses: Combine a
HostandPortinto aSocketAddress. - Multicast & MAC: Support for multicast addresses and MAC addresses.
Why ribs_ip? #
- Avoid String-ly Typed Code: Stop passing IP addresses and ports as strings. Let the compiler help you.
- Unified API: Work with IPv4 and IPv6 using a consistent interface.
- Safety: Failures during parsing are represented in the type system (e.g.
None). - Functional Integration: Built to work seamlessly within the larger
ribsecosystem.
Quick Examples #
Parsing IP Addresses #
final addressOpt = IpAddress.fromString('192.168.1.1');
// Returns Option<IpAddress> (specifically Some<Ipv4Address>)
addressOpt.map(
(addr) => addr.fold(
(v4) => print('V4: ${v4.toBytes()}'),
(v6) => print('V6: ${v6.toBytes()}'),
),
);
CIDR Network Checks #
(
Cidr.fromString('192.168.1.0/24'),
Ipv4Address.fromString('192.168.1.50')
).mapN((cidr, ip) {
if (cidr.contains(ip)) {
print('$ip is in $cidr');
}
});
DNS Resolution #
final program = IO.fromOption(Hostname.fromString('google.com'), () => 'Invalid hostname')
.flatMap(Dns.resolve)
.flatMap((ips) => IO.exec(() => ips.foreach(print)));
program.unsafeRunFuture();