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?
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)
- 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
- 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
- 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
- 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
- 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
- Default settings — From 192.168.0.10/25 to 192.168.0.200. The first three numbers are the same, but they are on different networks.
- Changing to /24 — The same two addresses are now on the same network, so the data is sent directly.
- Different third number, same network — With /23, 192.168.0.x and 192.168.1.x are one network.
- To a server on the internet — The gateway replaces the source address with a public address (NAT).
- A network of only two devices — With /30, the host part is 2 bits, so there are 2 usable addresses.
- A big network, /16 — There are 65534 usable addresses.
Running with the default settings
- 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.
- /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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
- Whether two addresses are on the same network is decided not by the leading decimal numbers but by the first 25 bits that the subnet mask picks out. With a prefix longer than /24 (/25, /26, /30), the networks split even within the last number. If you enter the wrong subnet mask, data that should go to the device right next to you goes to the gateway, or the other way around.
- NAT is needed when data goes from a private address out to a public address. In an internal network where private addresses talk to each other, a router only forwards the data and does not change the addresses.
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 on the PC used to write this lab; 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