CIDR

License: MIT Coverage

A small Dart library that parses CIDR notation and checks whether an IP address is inside a CIDR range. Supports IPv4 and IPv6.

Features

  • Parse CIDR strings like 10.0.0.0/8 and 2001:db8::/32
  • Check membership with contains()
  • Works with both IPv4 and IPv6 addresses
  • Lightweight, no dependencies beyond Dart core

Installation

Add the package to your project:

dart pub add cidr

Usage

Create a CIDR range and test an address:

import 'package:cidr/cidr.dart';

void main() {
	final range = CIDR('10.0.0.0/8');
	print(range.contains('10.42.1.99')); // true
	print(range.contains('192.168.0.1')); // false
}

IPv6 works the same way:

import 'package:cidr/cidr.dart';

void main() {
	final range = CIDR('2001:db8::/32');
	print(range.contains('2001:db8::1')); // true
	print(range.contains('2001:dead::1')); // false
}

API

  • CIDR(String cidr) parses a CIDR string such as 192.168.0.0/16.
  • bool contains(String address) returns whether an address is inside the range.
  • toString() returns the original CIDR string.

Errors

Invalid input throws FormatException:

  • CIDR is not in address/prefix form
  • Address is not valid IPv4 or IPv6
  • Mask length is out of range for the address family

AI Usage Disclosure

AI assistance was used to generate parts of the test cases and documentation. No production source code in this package was generated by AI.

Libraries

cidr
CIDR parsing and range checks for IPv4 and IPv6.