Understanding IP Addresses and Subnet Masks
Tags: webdev, http, ipaddresses
Published: 2020-10-20 22:23:11

{% youtube fXJqetmLPo0 %}

For most home users, trying to understand a home network is not of interest due to the perceived complexity of the issue. In this post, whether you are a seasoned engineer or simply a Dad trying to figure out why the router isn't working again, I will explain the basics of how your home network works.

The first thing we need to understand is what an internet service provider actually does. ISPs are confusing in many cases because they offer more than one service, but in the scope of this post, let's consider an ISP that offers only internet as a service. An ISP is simply a company that owns hardware (wires) and makes that hardware available to customers in the form of internet service. What are these wires we speak of? Many ISPs own a variety of hardware that all are capable of carrying the analog signals we know as the "internet".

- Coaxial cables (what you have in your house)
- Unshielded twisted pair (UTP) cables (100 Mhz)
- Shielded twisted pair (STP) cables
- Fiber optic cables (fastest type - many are in the ocean)

There are multiple ways that internet can reach your home, but the most common is through telephone wires (UTP cables). Other methods include underground cables and dish satellites. The method of telephone wires often confuses people because given the name, you would assume that only telephone signals can be sent through telephone wires. This is not the case. The internet bootstrapped off of the phone system, and nowadays, there are multiple channels of communication running through each telephone wire. We call this "broadband" and it is the reason we can talk on the phone, watch Netflix, and search the web all at the same time. Previously, you had to use "dial up" where you would literally "call in" through the phone line to access the internet.

Regardless the method your ISP uses to get internet to your home, you will always be faced with the same problem. The signals traveling through the telephone wires your ISP owns are _analog_ signals (wave frequencies), while your computer runs on _digital_ signals (1s and 0s). This is where the modem comes in.

The modem takes that analog signal, converts it to a digital signal, and sends it over to the router. And this is where the fun begins, because the router has a lot of responsibility for making your home network functional. Before reading one more word, you need to remember one important fact.

**Your ISP assigns you ONE IP address, and ONE Subnet mask**.

You will soon understand what this means in full, but for now, just know that your entire home network is represented as a single IP address.

I am assuming that most people reading have an idea what an IP address is, but as a refresher, an IP address is a 32 bit number that identifies a specific network or computer. Today, most IP addresses are IPV4 addresses, which means that they are 32 bit rather than 64 bit IPV6 addresses. The reason we need IPV6 addresses is because we are running out of the former. If you calculate how many combinations you can have with 32 bits, it is 4.2 billion (2^32). This seems like a large number, but with a world population between 7 and 8 billion, you can see how this could be constraining. With IPV6, we have 1.8x10^19 addresses (2^64), which will never pose a capacity issue.

To find your computer's IP address, you can type `ipconfig` into a Windows terminal or `ifconfig` into a Mac/Linux terminal. You will want to look for the entry that shows a "inet" address. Our goal in this post is to understand how each device on your network receives a unique address despite the fact that your ISP only provided you 1 IP address to start with.

## Subnetting

When IPV4 was invented, it was clear that the IP address space needed to be conserved. I'm sure the engineers working on it didn't think we would ever reach 4.2 billion addresses, but they had a suspicion that if they weren't conservative with the assignments of new addresses, it would not only become a scarce resource, but also a pain in the butt to manage.

To solve this problem, the concept of subnetting came into existence. Rather than your ISP assigning a new IP address for each device that is added to your network, they just give you an "address space". In other words, your ISP assigns a single IP address for your network which has X number of IP addresses available for new devices. With just two numbers (network IP and subnet mask), you can calculate the number of available IP addresses for new devices on your network. This is easiest to explain through an example. Consider a device with an IP address of `192.168.22.187` and a subnet mask of `255.255.255.0`. Let's convert these to binary notation (use [this calculator](https://www.browserling.com/tools/ip-to-bin) or teach yourself how to convert from dot notation to binary if you're that curious).

```
# 192.168.22.187
11000000 10101000 00010110 10111011

# 255.255.255.0
11111111.11111111.11111111.00000000
```

At first glance, these two numbers are boring and meaningless, but if we do a little transformation on them, you will see the ingenuity of the subnetting system. To see what I mean, every time the two addresses both have a 1 in the same position, write a 1. Otherwise, write a 0. It's kind of like a math problem.

```
11000000 10101000 00010110 10111011
11111111.11111111.11111111.00000000
-----------------------------------
11000000.10101000.00010110.00000000
```

Now convert that result into decimal notation.

```
11000000.10101000.00010110.00000000
192.168.22.0
```

At this point, we have two IP addresses. The first is `192.168.22.187`, and the second is `192.168.22.0`. But what are these two addresses?

To figure that out, let's return to that subnet mask that our ISP provided to us.

```
# 255.255.255.0
11111111.11111111.11111111.00000000
```

Notice how the last 8 bits are zeros. This means that our ISP has given us all 8 of those bits for new devices on our network. In other words, we have 256 possible IP addresses to use within our network (2^8). This also means that the first 24 bits of the IP address that our ISP assigned is the IP address of our _network_. In other words, what we calculated when we combined the assigned IP and the subnet mask was the actual IP address of our _network_ (192.168.22.0). If we plugged in a single device to our modem and skipped the router altogether, this would be the IP address of that device. The other address `192.168.22.187` is nothing special. It is simply one of the 256 possible IP addresses within the address space of 0-255. If this was a home network, you might see devices lined up one after the other like so:

Ipad: `192.168.22.187`
Iphone: `192.168.22.188`
Mac: `192.168.22.189`
Printer: `192.168.22.190`

We could do this all the way to `192.168.22.254` or as low as `192.168.22.1`. Notice that there are two addresses that we cannot assign to devices

1. `192.168.22.255`
2. `192.168.22.0`

The first is the "broadcast" address, which is a special IP that is used for various purposes such as address resolution (ARP) and dynamic host configuration (DHCP). The second is of course our network address. Below is a recap visual to illustrate how we come to our IP addresses for a given network. Remember, the `192.168.22.187` address is a random address that I created for the purpose of the visual and carries no significance while `192.168.22.255` and `192.168.22.0` are significant.

![IP addresses](https://dev-to-uploads.s3.amazonaws.com/i/g0tkqfz9j8titzbpr11d.PNG)

### Broadcast Address vs. Default Gateway vs. Network Address

In the above example, we talked about a "broadcast" and "network" IP address; both of which are reserved addresses within every network address space. I used the word "network" address, but this could also be considered the "default gateway" address, and is where all traffic leaves the network.

### Alternative Notation

IP addresses can be written in a slightly different notation called "CIDR Notation". This is where the number of bits in the subnet mask is added as a postfix to the IP address. For example, the subnet mask `255.255.255.0` (`11111111.11111111.11111111.00000000` in binary) has 24 leading bits, and therefore to combine this with our IP address above, we would write `192.168.22.187/24`. This indicates that we have an IP address of `192.168.22.187` with a subnet mask of `255.255.255.0`.

### Dynamic Host Configuration Protocol

Many routers nowadays act as a DHCP server to dynamically assign new IP addresses to devices on the network. While a website server needs to have a static IP address so that people can find the website, your home computing devices don't need a static IP.

We could get into a full discussion of DHCP, but to do so, we would need to discuss communication protocols like UDP and the different layers of the internet via the OSI model. Those topics are out of scope of this post, but it is still important to understand what DHCP is on a high level.

As stated, the function of a DHCP server (a router in our case) is to dynamically assign an IP address to a new device entering a network (LAN in our case). The DHCP server has a range of addresses it can assign (as described with subnetting earlier), and sets a specific amount of time that an IP address is valid for a device ("lease time"). This is the main function of such a server, but the reason it was invented in the first place is to eliminate the need for an administrator to assign a new IP address to each new device manually. With a DHCP server (remember, this is just a router in our case), the physical device can send out a request to join the network and the DHCP server can validate it and assign it an IP address that will allow it to communicate with other devices on the network.

Let's consider a classic example of DHCP in action. Say you are staying at a friends house for the weekend, but need to get on the internet while there. You will of course beg your friend for their wifi password and your friend will scramble around the house trying to remember where he wrote it down. When he finally finds the password, you select the network and type in the password. When you press enter, a bunch of things happen that you are unaware of.

First, you must understand that every _physical_ computing device whether it be an iPhone, printer, or PC will have a unique Media Access Control (MAC) address. This identifier never changes because it was assigned to the device by the manufacturer. Although it is similar to an IP address, a MAC address identifies each device in what we call the "Data Layer" of the [OSI model](https://en.wikipedia.org/wiki/OSI_model) while an IP address identifies each device in the "Network Layer".

When you type in the password to your friend's wifi, the first thing that happens is your device broadcasts a signal into that network with its MAC address attached to the request. Within that network, the router (running a DHCP server) will pick up this signal, recognize that it is a request for an IP address, and assuming the password is correct, it will assign that device a unique IP address on the network.

During this assignment, the router will only be using IP addresses within its range (as defined by the IP address assigned by the ISP and the subnet mask), and will assign it on "leased" terms. Usually the lease is 120 minutes long, and the reason for leasing IP addresses is so the router's address space does not become full. You probably will not be back to your friend's house for a while, so it wouldn't make sense for the router to permanently lock up an IP address solely for your device. By leasing the IP address, when you leave his house, the DHCP server will regain that IP address and will be able to assign it to other devices in the future.

But if the lease is only 120 minutes, how does your computer stay connected to wifi for hours on end? By default, most devices will auto-renew their leases and keep the same IP address so long as they continue to auto-renew.

So next time you visit a friend and use their wifi network, you can be relieved of all guilt knowing that you will be returning the IP address you borrowed upon departure!

## Your Router's IP Address is not static after all

To this point, I have explained that your ISP assigns your home network a single IP address which we call the "default gateway" or "network IP address". This is entirely true, but now that we understand DHCP, I can reveal the fact that your router's IP address is dynamic too. Most ISPs run DHCP servers and dynamically assign their customers IP addresses just like your router does on the home network. This makes sense because as customers unsubscribe from internet service, the ISP wants to get that IP address back to assign to a new customer!

The topic of networking and the internet has many layers (literally... Check out the OSI Model), but most people will never need to think about or troubleshoot outside of their local area network (LAN), hence the brevity of the post.

If you liked this post, you might also like [my post on Bash networking commands](https://dev.to/zachgoll/basic-bash-networking-commands-kb).