Skip to content

No IP classification functions (is_ip_loopback, is_ip_unspecified, is_public_addr, is_private_addr) #116

Description

@sumanjeet0012

Go's manet package provides comprehensive IP classification functions that operate on Multiaddr objects. Python has is_wildcard() and is_link_local_ip() in utils.py, but they take raw strings instead of Multiaddr objects and miss several important classifications.

Problem

Go provides:

Function Description
IsThinWaist(ma) Is it /{ip4,ip6}[/{tcp,udp}]?
IsIPLoopback(ma) Is it 127.0.0.0/8 or ::1?
IsIPUnspecified(ma) Is it 0.0.0.0 or ::?
IsIP6LinkLocal(ma) Is it fe80::/10?
IsPublicAddr(ma) Is it publicly routable?
IsPrivateAddr(ma) Is it in a private network?
IsNAT64IPv4ConvertedIPv6Addr(ma) Is it NAT64?

Python provides:

Function Takes Description
is_wildcard(ip: str) Raw string Only checks "0.0.0.0" or "::"
is_link_local_ip(ip: str) Raw string Checks fe80: or 169.254
get_multiaddr_options(ma) Multiaddr Implicit thin waist check

Missing: is_ip_loopback, is_ip_unspecified (on Multiaddr), is_public_addr, is_private_addr, is_thin_waist, is_nat64.

Proposed Solution

Add to utils.py:

import ipaddress

# Well-known multiaddr constants
IP4_LOOPBACK = Multiaddr("/ip4/127.0.0.1")
IP6_LOOPBACK = Multiaddr("/ip6/::1")
IP4_UNSPECIFIED = Multiaddr("/ip4/0.0.0.0")
IP6_UNSPECIFIED = Multiaddr("/ip6/::")

# Private CIDR ranges
PRIVATE4 = [ipaddress.ip_network(cidr) for cidr in [
    "127.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10",
    "172.16.0.0/12", "192.168.0.0/16", "169.254.0.0/16",
]]
PRIVATE6 = [ipaddress.ip_network(cidr) for cidr in [
    "::1/128", "fc00::/7", "fe80::/10",
]]

def is_thin_waist(ma: Multiaddr) -> bool: ...
def is_ip_loopback(ma: Multiaddr) -> bool: ...
def is_ip_unspecified(ma: Multiaddr) -> bool: ...
def is_ip6_link_local(ma: Multiaddr) -> bool: ...
def is_public_addr(ma: Multiaddr) -> bool: ...
def is_private_addr(ma: Multiaddr) -> bool: ...

Each function extracts the IP from the multiaddr and checks against the appropriate ranges.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions