Switches and Routers are essential hardware pieces for the foundation of a network. In the beginning stages of the development of the computer, the need arose for computers to be able to talk to each other. Two computers were connected by a wire, and gradually, multiple computers were connected by switches and routers.
In this article, we will talk about Switches and Routers, how they work, and their differences.
First, to understand the development of Switches, we will take a look at hubs.
Hubs, the outdated switches
Hubs are a predecessor of switches that became outdated for good reason. Let’s say we have a network of 4 computers that want to talk to each other. All 4 computers are connected to a hub via ethernet cables, forming a plus sign with the hub in the middle.
When Computer 1 wants to talk to Computer 2, it sends out a message to the hub, which then sends this message out to the other 3 computers on the network. Then when Computer 2 responds, it sends its message to the hub, who broadcasts it to the other 3 computers again.
The obvious problem here is that Computers 3 and 4 get to see the whole conversation that Computers 1 and 2 are having. Not only will it be annoying for people that don’t want to hear conversations not directed towards them, but also it makes it easier for hackers to listen in on any conversation as long as they are on the network.
Switches allow two computers on a network to talk to each other privately. With the same example of four computers connected by a switch in the middle, Computer 1 sends a message through an ethernet cable to the switch, who then sends it only to Computer 2. Computer 2 responds back to the switch, who redirects the message back to Computer 1. This way, no one else on the network will receive the messages.
Switches do this through the use of MAC addresses (Media Access Control). Each computer on the network has a MAC address that might look something like this: 00:1B:54:21:3A:A7. The switch recognizes each computer by their MAC address, and is thus able to send messages to the correct computer.
Like the hub, he switch is a device with a bunch of ethernet ports to which computers connect. The switch creates a CAM table that matches all of the connected devices to their corresponding physical ports. As long as a machine sends a message across the network through the switch, the switch will add its MAC address and physical port to the CAM table.
A computer trying to talk to another computer does so through IP addresses. So how does the switch match the IP address to the MAC address? The answer is through ARP (Address Resolution Protocol). Computer 1 sends out a request to talk to Computer 2 by sending an IP address to the switch. The switch broadcasts the ARP frame to everyone, asking for “Who has this IP?”. Computer 2, who has that IP, will respond “Me, and this is my MAC address.” Now the switch will know Computer 2’s MAC and IP address. This process only needs to happen once.
A router’s job is to connect different networks to each other. Let’s say we have computers 1–4 connected by a switch on network 1, and computers 5–8 connected via a switch in the same way on network 2. The router would be sitting in the middle of these two networks, connecting to both switches via ethernet.
When let’s say computer 1 wants to talk to computer 5, it will send a request to the switch, asking to send a message to the router’s IP address. Computer 1 automatically knows that Computer 5’s IP does not belong on the same network, and sends this request to the gateway, or router.
It is able to do this through the configuration of the IP addresses. Let’s say computers 1–4 have IP’s in the range 10.1.1.0 to 10.1.1.225. This means that every computer on the network will have an IP that starts with 10.1.1. On Network 2, computers 5–8 have IP’s in the range 192.168.1.0–225, meaning that their IP’s all start with 192.168.1. When Computer 1 sends a request to talk to Computer 5, whose IP starts with 192.168.1, it knows that this computer does not belong on the same network, and will send the request directly to the router, who might have an IP of 10.1.1.1.
Example of message sent across a network
Computer 1 wants to talk to Computer 5. Computer 1 first sends an ARP frame to the switch asking for the IP of the router, 10.1.1.1. The switch broadcasts this message to all devices, and the router returns the message with its MAC address. Now computer 1 knows the router’s MAC address.
Computer 1 now sends a packet to the router with the IP address of computer 5. The router sends an ARP frame to the switch on Network 2, who broadcasts it to find out what MAC address belongs to that IP. Computer 5 responds with its MAC address.
Computer 1 now knows Computer 5’s IP and MAC address. It sends a packet to Computer 5 and Computer 5 responds back with a message.
The OSI Model and “Layers”
To end this article, let’s go over some terminology. The various devices and protocols that comprise what we know as the internet are often divided into seven “layers.”
The first layer, known as the “Physical Layer,” comprises of the wires and other physical media through which bits of data are sent across the internet.
The second layer, known as the “Data Link Layer,” handles the transfer of data between two directly connected devices, identified by their MAC addresses. In other words, the switches discussed in this article comprise this layer. Data processed by this layer is divided into frames, which was the word we used to describe ARP requests/responses.
The third layer, known as the “Network Layer,” handles the transfer of data between networks. Routers comprise this layer, and endpoints are identified by IP addresses instead of MAC addresses. Data processed through this layer is divided into packets.
If this seems confusion, don’t worry! We will cover this in our next article.
Bonus: WAP, DNS, Layer 3 switches
Wireless Access Point: You can think of a wireless access point as a wireless hub. Messages sent from the access point is broadcasted to all devices connected to it. This is what Wi-fi is.
DNS: DNS stands for domain name server. This can be a server on a network that contains information to match IP addresses to URLs. Computer 1 figures out who the DNS server is by sending an ARP frame to the switch, which broadcasts it, then the DNS server tells computer 1 the IP address of the URL it’s trying to connect to.
Layer 1: Physical, ethernet cables, wires.
Layer 2: Switches(connect computers), MAC addresses, messages sent are called frames.
Layer 3: Routers(connect networks), IP addresses, messages sent are called packets.
At Safehouse, we are trying to make networks more secure, read more at www.safehouse.dev.