Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:ClickHouse ClickHouse Poco IPAddress

From Leeroopedia


Knowledge Sources
Domains Networking, IP
Last Updated 2026-02-08 00:00 GMT

Overview

Representation of IPv4 and IPv6 addresses with parsing, validation, and classification capabilities.

Description

The Poco `IPAddress` class provides a unified interface for working with both IPv4 and IPv6 addresses. It supports parsing addresses from text (dotted decimal for IPv4, hex for IPv6), creating addresses from binary representations, and querying address properties like wildcard, broadcast, loopback, multicast, and various address scopes.

The class includes bitwise operators for subnet masking and address manipulation, methods to check address categories (unicast, multicast, link-local, site-local), and support for IPv6 scope identifiers. It handles IPv4-mapped and IPv4-compatible IPv6 addresses transparently.

Usage

ClickHouse uses this vendored Poco component throughout its networking layer for representing and manipulating IP addresses in socket operations, access control, network interface queries, and DNS resolution.

Code Reference

Source Location

Signature

class IPAddress {
public:
    typedef AddressFamily::Family Family;
    static const Family IPv4;
    static const Family IPv6;

    IPAddress();
    IPAddress(const IPAddress& addr);
    explicit IPAddress(Family family);
    explicit IPAddress(const std::string& addr);
    IPAddress(const std::string& addr, Family family);
    IPAddress(const void* addr, poco_socklen_t length);
    IPAddress(const void* addr, poco_socklen_t length, Poco::UInt32 scope);
    IPAddress(unsigned prefix, Family family);
    IPAddress(const struct sockaddr& sockaddr);

    IPAddress& operator=(const IPAddress& addr);

    Family family() const;
    Poco::UInt32 scope() const;
    std::string toString() const;

    bool isWildcard() const;
    bool isBroadcast() const;
    bool isLoopback() const;
    bool isMulticast() const;
    bool isUnicast() const;
    bool isLinkLocal() const;
    bool isSiteLocal() const;
    bool isIPv4Compatible() const;
    bool isIPv4Mapped() const;
    bool isWellKnownMC() const;
    bool isNodeLocalMC() const;
    bool isLinkLocalMC() const;
    bool isSiteLocalMC() const;
    bool isOrgLocalMC() const;
    bool isGlobalMC() const;

    bool operator==(const IPAddress& addr) const;
    bool operator!=(const IPAddress& addr) const;
    bool operator<(const IPAddress& addr) const;
    IPAddress operator&(const IPAddress& addr) const;
    IPAddress operator|(const IPAddress& addr) const;
    IPAddress operator^(const IPAddress& addr) const;
    IPAddress operator~() const;

    poco_socklen_t length() const;
    const void* addr() const;
    int af() const;
    unsigned prefixLength() const;

    void mask(const IPAddress& mask);
    void mask(const IPAddress& mask, const IPAddress& set);

    static IPAddress parse(const std::string& addr);
    static bool tryParse(const std::string& addr, IPAddress& result);
    static IPAddress wildcard(Family family = IPv4);
    static IPAddress broadcast();
};

Import

#include <Poco/Net/IPAddress.h>

I/O Contract

Input Output
Text string ("192.168.1.1" or "2001:db8::1") Parsed `IPAddress` object
Binary address data (`in_addr` or `in6_addr`) `IPAddress` object
Prefix length and address family Subnet mask `IPAddress`
Address query methods Boolean indicating address properties

Address Classification Methods

Method Description
`isWildcard` All zeros (0.0.0.0 or ::)
`isBroadcast` All ones (255.255.255.255, IPv4 only)
`isLoopback` Loopback address (127.0.0.1 or ::1)
`isMulticast` Multicast address (224.0.0.0/4 or ff00::/8)
`isLinkLocal` Link-local address (169.254.0.0/16 or fe80::/10)
`isSiteLocal` Site-local/private (10.0.0.0/8, 192.168.0.0/16, etc.)
`isIPv4Compatible` IPv6 address with IPv4 compatibility
`isIPv4Mapped` IPv4-mapped IPv6 (::ffff:x.x.x.x)

Usage Examples

// Parse IP addresses
IPAddress ipv4("192.168.1.100");
IPAddress ipv6("2001:db8::1");

// Check address family
if (ipv4.family() == IPAddress::IPv4) {
    std::cout << "IPv4 address" << std::endl;
}

// Query address properties
if (ipv4.isPrivate() || ipv4.isSiteLocal()) {
    std::cout << "Private/site-local address" << std::endl;
}

if (ipv4.isLoopback()) {
    std::cout << "Loopback address" << std::endl;
}

// Create special addresses
IPAddress wildcard = IPAddress::wildcard(IPAddress::IPv4);  // 0.0.0.0
IPAddress broadcast = IPAddress::broadcast();  // 255.255.255.255

// Subnet operations
IPAddress addr("192.168.1.100");
IPAddress mask(24, IPAddress::IPv4);  // 255.255.255.0
addr.mask(mask);
std::cout << "Network address: " << addr.toString() << std::endl;
// Output: 192.168.1.0

// Bitwise operations
IPAddress net1("192.168.1.0");
IPAddress net2("192.168.2.0");
IPAddress combined = net1 | net2;

// Safe parsing with error handling
IPAddress result;
if (IPAddress::tryParse("invalid.address", result)) {
    // Parsed successfully
} else {
    // Parse failed
}

// Compare addresses
std::vector<IPAddress> addrs = {
    IPAddress("192.168.1.1"),
    IPAddress("10.0.0.1"),
    IPAddress("172.16.0.1")
};
std::sort(addrs.begin(), addrs.end());

// Access raw address data
const void* raw = ipv4.addr();
int addressFamily = ipv4.af();  // AF_INET or AF_INET6

// IPv6 with scope
IPAddress linkLocal("fe80::1", IPAddress::IPv6);
std::cout << "Scope: " << linkLocal.scope() << std::endl;

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment