A small C library for parsing, formatting, and comparing IP addresses, with no dynamic allocation. Supports IPv6 and IPv4, CIDR masks, ports, zone IDs, and IPv4 embedded in IPv6. JavaScript bindings are available through WebAssembly.
Try it in your browser · IPv6 introduction · Releases
The parser reads an address into a structure. The formatter converts it back to text, including any IPv6 mask, zone ID, or port.
flowchart TD
input["Address string"] -->|ipv6_from_str| address["ipv6_address_full_t"]
address -->|ipv6_to_str| output["Formatted string"]
#include "ipv6.h"
#include <stdio.h>
#include <string.h>
int main(void) {
const char *input = "[2001:db8::1/64]:8080";
ipv6_address_full_t addr = {0};
char output[IPV6_STRING_SIZE];
if (!ipv6_from_str(input, strlen(input), &addr)) {
return 1;
}
ipv6_to_str(&addr, output, sizeof(output));
puts(output); // [2001:db8::1/64]:8080
return 0;
}Zone IDs reference the original input string; keep it alive while using the parsed address. See the C API reference for diagnostics, field flags, and comparison options.
Requires CMake and a C/C++ toolchain. From the repository root:
cmake -S . -B build
cmake --build build --config Release
cmake --install build --config ReleaseThis builds a static library by default. Use -DBUILD_SHARED_LIBS=ON for a shared
library. The installation guide
covers CMake integration, pkg-config, and package managers.
npm install ipv6-parseconst ipv6 = require('ipv6-parse');
async function main() {
const addr = await ipv6.parse('2001:db8::1');
console.log(addr.formatted); // 2001:db8::1
}
main().catch(console.error);See the JavaScript guide for synchronous parsing and browser usage, or the TypeScript guide for typed examples.
To build and run the native tests:
cmake -S . -B build -DIPV6_PARSE_LIBRARY_ONLY=OFF
cmake --build build --config Release
ctest --test-dir build --build-config Release --output-on-failure