Dev Learning Lab

The labs on this site need JavaScript to run. The same content is written out below.

Learn › Networking › Addresses and names

IP addresses and subnets

Are two IP addresses with the same first three numbers on the same network?

Why it exists — If IP addresses are handled one by one, computers and routers must know where to send for each destination separately, and if the size of a network is chosen only from a few fixed classes, many addresses are left over or too few are available. The subnet mask and CIDR set the first few bits of an address as the network number so that addresses with the same front part are handled as one block, and they let you choose the size of that block as needed with /number.

Where it's used today — A common use is to set a range such as 10.0.0.0/16 when you create a VPC in the cloud and divide it into several /24 subnets to place web servers and database servers separately. CIDR is also used to write a range when you specify "allow connections only from addresses in this range" in a firewall or security group.

An IP address is divided into a front part that shows "which network" and a back part that shows "which device within it". A number after the address, such as /24, decides where the front part ends. A computer uses this number to judge whether the destination is on the same network, and so decides whether to send the data directly or to hand it to the default gateway (a home router or another router).

Partly checked against real execution · The address calculation and the same-network judgment were checked for all 72 combinations against Node.js 24 net.BlockList and two other calculations. The NAT explanation follows RFC 3022

Concepts in this lab

IP address (IPv4)
Why it exists — On the internet, where many networks are joined, each device needs a common address from which it is possible to know which way to send to. An IP address is divided into a network part and a device part, so that a router can decide where to send next by looking only at the front part of the destination.
Where it's used today — You use it when you connect to a server or set a DB address, and when you check in a log who sent a request. 32 bits are not enough addresses, so addresses are shared out using private addresses and NAT, and IPv6, which uses 128-bit addresses, is also used alongside.
This is a 32-bit numeric address used to find a device on the internet. To make it easy for people to read, it is cut into 8-bit groups and written as four decimal numbers from 0 to 255, such as 192.168.0.10. The front part of the address shows which network the device is on, and the back part shows which device it is within that network. Where the front part ends cannot be known from the address alone; it is decided only together with a subnet mask.
Subnet mask and CIDR
Where it's used today — In the network settings of a PC or a router, you write a subnet mask such as 255.255.255.0 together with the IP address. Tools such as Docker also express the range of the internal network they create in CIDR, as in 172.17.0.0/16.
A subnet mask is a 32-bit number that shows where the network part of an IP address ends; the network part is 1s and the host part is 0s. CIDR notation gives the same information by writing the number of bits in the network part after the address, as in 192.168.0.10/24, and /24 means the same as the subnet mask 255.255.255.0. A computer applies the same mask to its own address and to the destination address. If the results are the same, it treats the destination as being on the same network and sends directly; if not, it sends to the default gateway. If the mask is entered wrongly, communication with even a nearby device may fail.
Network address and broadcast address
Where it's used today — In routing tables and in the subnet settings of the cloud, a network is written as a network address, such as 10.0.1.0/24. The cloud may also set aside addresses besides these two, so AWS does not give the first four and the last one address of every subnet to servers.
The network address is the address whose host-part bits are all 0, and the broadcast address is the address whose host-part bits are all 1. The network address is used to refer to the network itself, and the broadcast address is used to send to all devices on that network at once. Neither can be given to a device, so the number of host addresses usable on a network is the total number of addresses minus 2. For example, a /24 network has 256 addresses, but only 254 of them can be given to devices.
Private IP address
Why it exists — IPv4 addresses are 32 bits, so their number is fixed, and it is not enough to give every device in homes and companies an address that does not overlap across the whole internet. Private IP addresses are set aside so that internal networks may reuse the same numbers, letting public addresses be used only where communication with the outside happens.
Where it's used today — The 192.168.0.x that a home router gives devices, company internal networks, and the server addresses in a cloud VPC are usually private addresses. A server that must not be reached directly from outside, such as a DB server, is given only a private address and not a public one.
This is an address range set aside so that anyone can use it inside a home or company network without asking permission. There are three ranges, 10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16, and they are defined by RFC 1918. Because the same addresses are used in many places at once, routers on the internet do not find a route to them. So a device with a private address must borrow a public address through NAT to communicate with the internet.
NAT
Why it exists — Routers on the internet do not find routes for private addresses, so a device that has only such an address cannot communicate with servers outside. If the NAT function of a router changes the address, several internal devices can use the internet with one public address.
Where it's used today — It is something a home router does by default, and in the cloud you put a NAT gateway so that servers in a private subnet can download updates. To let traffic come in from outside to a server inside, you write the pairs in advance, as with port forwarding on a router.
This is a function that replaces the IP address written in a packet with another address as the packet passes through a router. A home router replaces the private source address and port of a packet sent out by an internal device with its own public address and a new port, and records the pair in a translation table. When the response comes back, it looks at the table, changes the destination back to the original private address and port, and delivers the packet to that device. Devices are told apart by port number, so several devices can share one public address, but a host on the outside cannot start a connection to an inside device, because the translation table has no pair for it.

Try these

Running with the default settings

  1. The address of my PC is 192.168.0.10/25. An IP address (IPv4) is a 32-bit number, and to make it easy for people to read, it is cut into 8-bit groups and written as four decimal numbers. Written in binary, it is 11000000.10101000.00000000.00001010.
  2. /25: This means that the first 25 bits are the network part, also called the network prefix (CIDR notation). The remaining 7 bits are the host part. The number made by filling the network part with 1s and the host part with 0s is the subnet mask, and written in decimal it is 255.255.255.128.
  3. Network address: The address made by changing all 7 host-part bits of my address to 0, which is 192.168.0.0. It points to the network itself, so it is not given to a device.
  4. Broadcast address: The address made by changing the host part to all 1s, which is 192.168.0.127. It is used to send to all devices on this network at once, so it too is not given to a device.
  5. Usable hosts: The host part of 7 bits can make 2 to the power of 7 addresses, which is 128. Subtracting the network address and the broadcast address leaves 126. The range is 192.168.0.1 to 192.168.0.126.
  6. Is the destination (192.168.0.200) on the same network? Apply my subnet mask to the destination address too and keep only the first 25 bits, and the result is 192.168.0.128. Compared with my network address (192.168.0.0), it is different. It is a different network. In decimal, the first three numbers are the same, but what is compared is the first 25 bits, and there is a difference within them.
  7. Because it is a different network, the data cannot be sent directly. It is handed to the default gateway, the exit of my network (in this example 192.168.0.1, usually a home router or another router). The destination IP address stays as the final destination in the packet, and the gateway is only asked to deliver it.
  8. The destination is also a private address. In a place where several private networks are connected by routers, such as the internal network of a company, the gateway passes the packet on without changing the address. A private address does not work on the internet, so this destination can be reached only if it is inside the internal network of the same organization.

Result of this run

Code

// JavaScript. Paste it into the console of the browser developer tools to run it.
const toNumber = ip => ip.split('.').reduce((n, part) => n * 256 + Number(part), 0);   // the address as a 32-bit number
const toText = n => [n >>> 24, (n >>> 16) & 255, (n >>> 8) & 255, n & 255].join('.');

const prefix = 25;
const mask = (0xFFFFFFFF << (32 - prefix)) >>> 0;                 // subnet mask in which only the first 25 bits are 1
const network = (toNumber('192.168.0.10') & mask) >>> 0;          // host part all 0
const broadcast = (network | ~mask) >>> 0;                        // host part all 1

console.log(toText(mask), toText(network), toText(broadcast));
console.log(2 ** (32 - prefix) - 2);                              // number of usable hosts

// apply the same mask to the destination and see whether the network addresses are the same
console.log(((toNumber('192.168.0.200') & mask) >>> 0) === network);

Assumptions of this model

The network address, the broadcast address, the number of usable hosts, and the judgment of whether two addresses are on the same network were recalculated and checked for all 72 combinations. Three methods different from the model were used for the check: the values from running the bit-operation code shown on the screen on Node.js 24, the values obtained by cutting and joining a 32-character binary string, and the two ends and the count of the range found by putting my address and the length into net.BlockList in Node.js and asking about the addresses one by one. The rule "if the results of applying the same mask are the same, send directly; if not, send to the gateway" follows RFC 1122 section 3.3.1.1, and the private address ranges follow RFC 1918 section 3. NAT was not verified by execution in our test run; it was written following the address and port translation (NAPT) described in RFC 3022 (sections 2.2 and 4.1). The public address 198.51.100.7, the ports 40001 and 40002, and placing the gateway at the first address of the network are examples for explanation. A destination that is a private address is assumed to be in the internal network of the same organization. 203.0.113.7 is an address reserved for documentation, and it is used here as an example of a public address. Prefix lengths such as /31 and /32, for which the rule "subtract 2 addresses" does not hold (RFC 3021), and IPv6 are not covered.

Previous lab: DNS · Next lab: Switches and routers

Concepts in this lab

What this lab shows

Settings

Code


      
    

Diagram

Run comparison

Try these