Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

dev.net

IP addresses, CIDR blocks, subnets, well-known ports

Example results generated on 2026-10-08. Ones using now, today or randomness will differ when you run them: press Run on any example to run it in your browser, after editing it if you like.

Functions

functiondescription
ip(v: str|int)an IPv4 or IPv6 address as an integer, or an integer as an address
cidr(s: str)an IPv4 block’s network, broadcast, netmask, host range and count
in_cidr(ip: str, block: str)whether an IPv4 address is inside a CIDR block
subnets(block: str, prefix: int)split an IPv4 block into smaller blocks
ip_kind(ip: str)loopback, private, link-local, multicast, unspecified or public
port(n: int) / port(name: str)the service on a well-known port, or the port for a service; nil if unknown

ip

ip(v: str|int): an IPv4 or IPv6 address as an integer, or an integer as an address

ip("192.168.1.5")
# → 3232235781
ip(3232235781)
# → "192.168.1.5"
ip("::1")
# → 1

See also: cidr, ip_kind

cidr

cidr(s: str): an IPv4 block’s network, broadcast, netmask, host range and count

cidr("192.168.1.77/26")
# → {network: "192.168.1.64", broadcast: "192.168.1.127", netmask: "255.255.255.192", prefix: 26, first: "192.168.1.65", last: "192.168.1.126", hosts: 62}

See also: in_cidr, subnets

in_cidr

in_cidr(ip: str, block: str): whether an IPv4 address is inside a CIDR block

in_cidr("10.0.3.7", "10.0.0.0/22")
# → true

See also: cidr

subnets

subnets(block: str, prefix: int): split an IPv4 block into smaller blocks

subnets("10.0.0.0/24", 26)
# → ["10.0.0.0/26", "10.0.0.64/26", "10.0.0.128/26", "10.0.0.192/26"]

See also: cidr

ip_kind

ip_kind(ip: str): loopback, private, link-local, multicast, unspecified or public

ip_kind("10.1.2.3")
# → "private"
ip_kind("8.8.8.8")
# → "public"

See also: ip

port

port(n: int) / port(name: str): the service on a well-known port, or the port for a service; nil if unknown

port(22)
# → "ssh"
port("postgresql")
# → 5432

See also: http_status, ip

More examples

net

# hosts in a /22
cidr("10.0.0.0/22").hosts
# → 1022
# is it in the block?
in_cidr("10.0.3.7", "10.0.0.0/22")
# → true
# address as bits
ip("192.168.1.5") to bin
# → 0b11000000101010000000000100000101
# next address
ip(ip("10.0.0.255") + 1)
# → "10.0.1.0"
# split into /24s
subnets("10.0.0.0/22", 24)
# → ["10.0.0.0/24", "10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
# what runs on 5432?
port(5432)
# → "postgresql"