53 lines
2.9 KiB
Markdown
53 lines
2.9 KiB
Markdown
# IP Addresses, Subnet Masks, and CIDR.
|
|
|
|
**Disclaimer:** Most, if not all, information in this article applies to both
|
|
IPv4 and IPv6. However, an IPv6 address is `4` times the size of an IPv4
|
|
address, which is a lot more math that the author isn't willing to do. Due to
|
|
this, all examples will only be in the context of IPv4.
|
|
|
|
---
|
|
|
|
Cloudflare owns the number `16843009`. At least, as far as networks are
|
|
concerned they do. You'd be more likely to recognize it in the form `1.1.1.1`.
|
|
So how does the number `16843009` relate to the IP address `1.1.1.1`? Let's
|
|
start with what an IP address really is.
|
|
|
|
An IPv4 address is normally expressed as `A.B.C.D`, where each letter represents
|
|
an octet in decimal form. Since there are `4` octets, an IPv4 address is exactly
|
|
`32`-bits long. If you take the IP address `10.42.7.19` and write it in binary
|
|
form, you get `00001010.00101010.00000111.00010011`. This is referred to as a
|
|
bit string, which you'll notice contains exactly `32` bits as expected. Since
|
|
an octet will always be `8` bits, the dot separators aren't needed in a bit
|
|
string. This means that the bit string should really be written as
|
|
`00001010001010100000011100010011`. These bit strings can also be referred to as
|
|
a `32`-bit integer. In other words, they are whole numbers that take up exactly
|
|
`32` bits of memory. The example bit string in this case could be written in
|
|
decimal form as the number `170526483`. We now have 3 ways to describe the same
|
|
IP address:
|
|
|
|
- `10.42.7.19`
|
|
- `00001010001010100000011100010011`
|
|
- `170526483`
|
|
|
|
You'll likely never need to write an IP address in any other form besides the
|
|
first one (commonly referred to as dotted decimal form), so why is this
|
|
important? For computers, it is fast and straight-forward to operate on IP
|
|
addresses when they are bit strings. The most common operations are determining
|
|
if an IP address is within a given range of IP addresses, finding the start of
|
|
the range, and finding the end of the range. To do this, they use a special tool
|
|
known as a subnet mask.
|
|
|
|
A subnet mask is a 32-bit string, similar to an IPv4 address, with the added
|
|
constraint that it must be an initial series of `1` bits immediately followed by
|
|
a series of `0` bits. This means that as soon as the first `0` bit appears, all
|
|
remaining bits must be `0`. The following bit string is a valid subnet mask:
|
|
`11111111111111111111111100000000`. If you split up the 32 bits into 4 groups of
|
|
octets, and convert those groups to decimal form, you get `255`, `255`, `255`,
|
|
and `0`. We would write this as `255.255.255.0`. Another way of writing a subnet
|
|
mask is CIDR notation, which is of the form `/N`, where `N` represents the
|
|
number of `1` bits at the beginning of the mask. This means that writing
|
|
`10.42.7.19/24` indicates you have the IP address `10.42.7.19` with the subnet
|
|
mask of `11111111111111111111111100000000` (or `255.255.255.0`). You'll notice
|
|
the mask has 24 `1` bits, the value of `N`.
|
|
|
|
How does a computer apply a subnet mask to an IP address?
|