Skip to content

dev.net

IP addresses, address blocks and subnets, and which service uses which port

IP addresses and blocks of them, as used when setting up networks.

A block of addresses is written in CIDR notation, like 10.0.0.0/22. The number after the slash says how many of the 32 bits at the start are fixed, and the rest can be anything, so a /22 holds 2 to the power of 10, or 1024 addresses, and a /24 holds 256. The first address in a block names the network itself and the last is the broadcast address that reaches every machine on it, so neither can be given to a single machine.

Tags: network

  • ip an IPv4 or IPv6 address as a plain integer, or an integer back as an address
  • cidr facts about an IPv4 block like “10.0.0.0/22”
  • in_cidr whether an IPv4 address is inside a block like “10.0.0.0/22”
  • subnets split an IPv4 block into smaller blocks with the given prefix length, so a /22 splits into four /24s
  • ip_kind what an address is for
  • port the service that normally listens on a port number, like “ssh” for 22, or the port for a service name, or nil if unknown
ip(s: str) -> int / ip(n: int) -> str

an IPv4 or IPv6 address as a plain integer, or an integer back as an address

An IPv4 address like 192.168.1.5 is really a 32-bit number written as four bytes, so this lets you do arithmetic on addresses

1
ip("192.168.1.5")3232235781
2
ip(3232235781)"192.168.1.5"
3
ip("::1")1
Example outputCtrl+Enter to run

See also cidr, ip_kind

cidr(s: str) -> map

facts about an IPv4 block like “10.0.0.0/22”

Its network and broadcast addresses, netmask, the first and last address a machine can use, and how many there are (hosts)

1
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}
Example outputCtrl+Enter to run

See also in_cidr, subnets

in_cidr(ip: str, block: str) -> bool

whether an IPv4 address is inside a block like “10.0.0.0/22”

1
in_cidr("10.0.3.7", "10.0.0.0/22")true
Example outputCtrl+Enter to run

See also cidr

subnets(block: str, prefix: int) -> list

split an IPv4 block into smaller blocks with the given prefix length, so a /22 splits into four /24s

1
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"]
Example outputCtrl+Enter to run

See also cidr

ip_kind(ip: str) -> str

what an address is for

“loopback” (this machine), “private” (a home or office network), “link-local” (self-assigned when there’s no router), “multicast”, “unspecified” (0.0.0.0) or “public”

1
ip_kind("10.1.2.3")"private"
2
ip_kind("8.8.8.8")"public"
Example outputCtrl+Enter to run

See also ip

port(n: int) -> str|nil / port(name: str) -> int|nil

the service that normally listens on a port number, like “ssh” for 22, or the port for a service name, or nil if unknown

1
port(22)"ssh"
2
port("postgresql")5432
Example outputCtrl+Enter to run

See also http_status, ip

1
# how many machines fit in a /22
2
cidr("10.0.0.0/22").hosts1022
3
# is it in the block?
4
in_cidr("10.0.3.7", "10.0.0.0/22")true
5
# an address as bits
6
ip("192.168.1.5") to bin0b11000000101010000000000100000101
7
# the address after 10.0.0.255
8
ip(ip("10.0.0.255") + 1)"10.0.1.0"
9
# split into /24s
10
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"]
11
# what runs on 5432?
12
port(5432)"postgresql"
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.