C++ Advance Address Library (libaddr)


libaddr is an easy to use C++ library that parses IP addresses to objects. This includes parsing one or more IPv4 and IPv6 addresses, with a port or a CIDR mask.

Address Support

The parser accepts IPv4 and IPv6 addresses. Because we support addresses followed by a port number, we force you to put IPv6 addresses between square brackets (For example: "[::]").

The parser accepts a default address, if the input string does not include an address, then the parser makes use of the default one instead.

Addresses can be marked as required. In that case a missing address is an error.

The parser supports lists of addresses separated by commas, spaces, or both. The result of the parser is always a list of address ranges. When multiple addresses are defined, expect the result to include multiple addresses.

Two addresses can be separated by a dash to define a range of addresses. This is similar to using a CIDR, only you may start and end your range on any address instead of a specific number of bits. (Not yet implemented)

IPv4 support

The IPv4 support is limited to the 4 number dot notation (A.B.C.D).

IPv6 support

IPv6 requires square brackets as we otherwise view the colon (:) as the port separator. So something like [::1]:123 will work. Just ::1 is an error.

Port support

We support basic port notation by separating the address and port with a colon.

You may define a port range by separating two decimal numbers by a dash (-). For example 1-1023 would define all reserved ports. (Not yet implemented)

Multiports is also available. You can write multiple ports separated by commas. This is useful if you want to handle a certain number of specific ports that are not clearly defined in a range. For example,443 references IPv4 address and ports 80 and 443. (Not yet implemented)

Mask support (CIDR)

Masks are written after a slash (/).

A mask can be one decimal number, in which case it represents the number of bits from left to right that are set to 1. The special case 0 is supported (i.e. match all.)

The mask can also be an address. In case of an address, it has to use the same format as the first part (IPv4/IPv4 or IPv6/IPv6). Such a mask can be absolutely anything. (i.e. would clear all even bits of an IPv4 address.)


The source code is part of the snapcpp environment. You can find libaddr under the contrib directory.

There are two dependencies found in the snapcpp environment: the libexcept library and the top cmake directory to run cmake. The easiest is still to get the entire environment and run snap-ubuntu-packages and build-snap scripts (under snapcpp/bin/). This compiles everything, including the libaddr library.


The technical documentation is available on the References page.

Coverage Test Results

Access full page here.

The library has a test suite that covers 100% of the code, making it a little more certain that it does not include too many bugs.


Syndicate content

Snap! Websites
An Open Source CMS System in C++

Contact Us Directly