Saturday, October 02, 2010

How the Internet works: IP Packets, Routers and LANs

In a previous post I described TCP/IP. In this post I'll focus more on the lower layers of the protocol stack: the IP layer and below. One key thing to understand is that the Internet is built on a single simple idea: the IP Packet.

So what is it?

An IP Packet is lot like a letter you send via the post office. In the case of IP Packets of course, we're really talking about an array of bytes in your computer memory, not a physical piece of mail. But there really are many similarities. In both case you have a From Address, a To Address, and inside it carries some contents that you want delivered at the other end.

With both IP Packets and Letters, once you send it you no longer have any control over it - it's out of your hands. It goes into the delivery system and you depend on that delivery system to get it where it's supposed to go. There is a small chance it could get delivered to the wrong place, or lost completely. The contents could be examined by some third party, damaged so that they are unreadable, or even intentionally altered by some unscrupulous party before being delivered. There are protections against each of these bad outcomes, which I'll talk about in a later post.

In the case of a letter, you write the from and to addresses on the outside of the envelope and place the contents inside the envelope. IP Packets have a similar idea. An IP Packet starts life as just a sequence of bytes in your computer memory. Typically the first 20 bytes or so are used like the outside of an envelope. This part of the packet is called the "IP Header". This is where the from and to IP Addresses are placed. IP Addresses are 4 bytes each(*), so those take up 8 of the 20 bytes. Some of the other bytes in the header are used by the delivery system for housekeeping. As an analogy, recall that the outside of an envelope has a stamp, and the postal service adds a post mark. Similar idea.

After the IP header, we place the bytes of the payload - the thing we want to get delivered to the recipient. Once we've got the payload, and the header constructed, we need to send it. With letters we need to get them to the post office. The equivalent in the IP World is your local Router. The Router is responsible for looking at the destination IP address and figuring out where to send the packet next. That next hop is likely another Router that is one step closer to the recipient (just as your local post office might send the letter to another postal facility to be sorted and forwarded on towards the destination address).

The way we get the packet from your computer to your local Router is over your local area network (WiFi or Ethernet). If you are hooked up directly to a DSL/Cable Modem, then your "local" Router is really a router located at your Internet Sevice Provider. The main idea here is that you can send the IP Packet to the Router directly over the underlying networking media, whether that is Ethernet, WiFi, DSL, or Dial-up. There is a whole other story on how the packet gets delivered over this media to the router, but for now just think of this media as your local mailman who picks up the letter and carries it to the post office (i.e. to the local Router). I can go into more details on Ethernet, WiFi, DSL etc. in a future post.

Now that the packet has been constructed in your computer memory, and carried to your local Router via your local networking media, the Router gets to work. It looks at the destination IP address, and matches this against a set of configured rules in a table. The rules are called "IP Routes", and the table is called the "IP Routing Table". By matching the destination IP address against the IP Routes, the Router figures out the next place to send the packet. We call this the "next hop". The next hop is going to be another Router that will repeat the same process. Your IP packet might hop through a dozen Routers. Finally it reaches a Router that can talk directly to the recipient, and that becomes the final "next hop". The packet is sent from that final Router to the destination over the local networking media, just like the mailman delivering mail.

The recipient (destination computer) opens up the packet. It can see the senders IP address, and it can read the contents. It's at this point that IP will hand the contents off to some higher layer protocol like TCP or UDP. IP has done it's job which is to get the contents of the packet from the sender to receiver. Any replies that go back to the sender will be determined by a higher layer protocol. For example, TCP may want to send an acknowledgment of the received packet. It will do this by forming yet another IP packet, with the original sender's IP address now placed in the new packet's IP header as the destination IP address (just like you would reply to someone who sent you a letter by sending them back a letter with the from/to addresses reversed).

In the world of TCP/IP networking there are a lot more details, more protocols, more ideas, as well as info on how you can write programs to directly use the TCP/IP protocol stack. There are entire books, and series of books on these subjects, but I hope in this post I've at least given you a flavor of how the Internet really works under the hood.

(*)Note: What I'm describing in this post is IP Version 4 (what the Internet is built on today). There is a new version of IP called IP Version 6 that will work in a very similar way, but has a very different IP header. For instance, in IPv6, IP addresses are 16 bytes each, instead of 4 bytes each. I'll talk more about IPv6 in a future post.

Post a Comment