Network Educative
Network Educative
The Components #
An Introduction to the Basics #
The course starts off by introducing some key concepts and networks lingo
that will be used throughout the rest of the course.
You will learn about each layer in a separate chapter. Each chapter will start
off by laying down the theoretical foundations and then will give some hands-
on experience with bash shell or python playgrounds that you can run
commands in.
Here's a short summary of some tools used throughout this course to maximize your learning.
• Coding Widgets
• Fun Facts
• Notes
• Security Warnings
• Quotes
• Graphics
• Quizzes
• Links
Coding Widgets #
The course demonstrates the usage of some key command-line network
tools. These will help you immensely in your career regardless of what area
of computer science you work in. Additionally, you’ll learn the essentials of
socket programming in Python.
Fun Facts #
Notes #
📝 Note This course is scattered with memos and ‘FYIs’ as well. These
notes are important to read and generally cannot be skipped.
Security Warnings #
Quotes #
Graphics #
We have several slides and drawings throughout this course that make
learning easier.
Quizzes #
Last but not least, you’ll be taking a fair amount of quizzes to help solidify
your knowledge!
Links #
We’ve given links to several external resources throughout this course if you
are interested in further reading. Most of them can be skipped.
Let’s begin!
What is the Internet?
The Internet permeates our very existence. Most of us cannot imagine life
without it. We often depend on it for livelihoods, for routine commutes, and
for entertainment. It has become almost like a utility. You’re accessing this
course through the Internet.
But how does it actually work? What goes on behind the scenes? Well, you’ve
come to the right place to learn that! The Internet is a global network of
computer networks.
Why Computer
Networks?
There are two main purposes of
computer networks:
Communication using computers
and sharing of resources. An
“internet” allows doing these two
things across different computer
networks.
The Internet
The Internet is essentially a network of
computer networks.
In the next lesson, we’ll take a look at a short history of the Internet and how
it all began.
A Quick History of The Internet
Understand why some things are designed and work the way they do
today.
– George Santayana
ARPA sent out a request for the design of a network that would allow
computers across the entire country to talk to each other.
A network called the ARPANET was developed over the course of a year.
In September 1969, the ARPANET was turned on. The first four nodes
were at UCLA, Stanford, MIT, and the University of Utah. Over the '70s,
other computer networks just like ARPANET sprang up.
While the computers on one network could communicate via a default
way to communicate set by the network administrator of each network,
It was also in RFC 675 that the term “Internet” was first used. Later RFCs
continued the use.
The World Wide Web got even more popular with the advent of browsers
such as Mosaic and Netscape which allowed combining graphics with
web navigation!
… Creating the web was really an act of desperation because the situation
without it was very difficult when I was working at CERN later. Most of the
technology involved in the web, like the hypertext, like the Internet, multi
font text objects, had all been designed already. I just had to put them
together. It was a step of generalizing, going to a higher level of
abstraction, thinking about all the documentation systems out there as
being possibly part of a larger imaginary documentation system."
– Tim Berners Lee
Search Engines #
Another fun fact that initially, there were no search engines.
So how would you find a website? Well, you couldn’t. Either you knew it or
you didn’t. Of course, you could land on a website by accident or by following
a link from another website that you knew.
Then, people started creating static indices of the web - a categorized listing of
websites. People would sit and randomly click on links to find web pages and
add links to their index. But this couldn t scale. So, eventually, search engines
were “invented.”
Altavista and Yahoo! were among the earlier search engines. According to this
Wikipedia article, the first automated web index was World Wide Web
Wanderer. Soon afterward, the first web search engine, the W3Catalog was
invented.
And now here we are. In an age in which all our devices run on the Internet,
even toasters for some – the era of the Internet of Things is here! Life without
it seems almost inconceivable.
A Video #
Here’s the history of the Internet in a short video.
ARPANET - The First Internet
ﻣﺷﺎرﻛﺔ اﻟﻣﺷﺎھدة ﻻﺣﻘًﺎ
Quick Quiz! #
COMPLETED 0%
1 of 7
Now that we have an overview of where the Internet comes from, let’s discuss
what it’s actually made of in the next lesson!
Internet Standards Documents
Let's look at what can be called the "of cial documentation of the Internet" now! We'll refer to these standards
documents throughout this course.
Also, it’s important to know what’s out there, who designed what and why.
Also, you’d know where to submit your ideas in case you come up with a
better design for any of the protocols we’re going to study.
What Is an RFC? #
at s a C
Today, RFCs are submitted to and handled by the Internet Society which
has a sub-body called the Internet Engineering Task Force (IETF). This
sub-body works on the standardization of Internet protocols and systems.
All Internet protocols, like the world wide web, are described by one or
more RFCs.
History #
RFCs were started by Steve Crocker to document details of ARPANET
while it was being created. These documents were called Requests For
Comments to encourage discussion and not seem too assertive. They
used to be written on a typewriter and distributed around ARPA’s office
as physical copies with requests for comments.
Contents of an RFC #
RFC’s generally start off with a header that contains the category of the
document, its identification number, the name(s) of the author(s), and the
date.
Then the document contains its title, a status, and an abstract.
Then a table of contents after which the document starts.
The document usually starts with an introduction.
Here is an example of the first page RFC standards document, RFC 2046.
An example of the first page of an RFC
Types of RFCs #
Standards Track #
There are two kinds of standards documents: Proposed Standard, and
Internet Standard.
Proposed Standard documents are well reviewed and stable but not as mature
as an Internet Standard document. Internet Standard documents are
technically competent, practically applicable, and publicly recognized.
Perhaps one of the most important standard documents that we know about
from the Internet history lesson is the one on the Internet protocol, RFC 791.
IETF has documented its internet standards process in RFC 2026. Have a look
if you want to learn more about it.
Historic #
These RFCs are usually obsolete and contain details about technologies that
are not in use anymore.
Unknown #
Some RFCs cannot be categorized or often do not specify any new standards
or findings. These are categorized as unknown.
You can browse all of these categories of RFCs on the RFC retrieve page.
Quick Quiz! #
1
Standardization enables ______
COMPLETED 0%
1 of 2
Now that we have a solid foundation to start learning all about computer
networks, let’s get right into it from the next chapter!
Communication Over the Internet
Before we dive deeper into the course, let's study some key concepts to understand how communication over the
Internet works
• What Is a Protocol?
• An Analogy
• TCP
• UDP
• HTTP
• Packets
• Addressing
• IP Addresses
• Ports
• Quick Quiz!
What Is a Protocol? #
An Analogy #
Let’s start with an analogy. Think of your routine conversations. They usually
follow a general pattern dictated by predefined rules. For example, most
conversations start with greetings and end with goodbyes. They probably go
something like this:
You: Hello
Friend: Hey!
...conversation ensues...
You: Bye!
Friend: Goodbye :)
Turns out that end systems also follow such protocols to communicate with
each other effectively on the network.
TCP #
The Transmission Control Protocol (TCP) is one such protocol. It was
created to allow end systems to communicate effectively. The distinguishing
feature of TCP is that it ensures that data reaches the intended destination and
is not corrupted along the way.
UDP #
The User Datagram Protocol (UDP) is also one such key protocol. However, it
does not ensure that data reaches the destination and that it remains
incorrupt.
HTTP #
HyperText Transfer Protocol (HTTP) is a web protocol that defines the
format of messages to be exchanged between web clients, e.g., web browsers
and web servers and what action is to be taken in response to the message.
The World Wide Web uses this as its underlying protocol.
Packets #
Now that we’ve established that end systems communicate with each other
based on set protocols, let’s discuss how they actually communicate.
Computers send messages to each other that are made up of ones and zeros
(bits).
However, instead of sending messages of possibly trillions of bits all in one go,
they’re broken down into smaller units called packets to make transmission
more manageable. These smaller sizes make transmission more manageable
because most links are shared by a few end-systems. Sending smaller units in
succession instead of one big file all in one go makes usage of the network
fairer amongst end-systems.
We’ll talk about the exact technical definition of a packet in a future chapter.
Addressing #
So, applications communicate with each other by sending messages based on
protocols. However, packets have to be addressed to a certain application on a
certain end system. How do you do that out of potentially millions of end
systems and hundreds of applications on each of them? The answer lies in
addressing.
IP Addresses #
Every device that is connected to the Internet has an address called an ‘IP
Address’ which is much like a mailing address.
The human readable way for looking at these numbers is the dotted
decimal notation, whereby the number is considered one octet of bits (8
bits) at a time. Those octets are read out in decimals, then separated by
dots.
curl ifconfig.me -s
All of the code on our platform is run on one of our servers, and the
output is returned and printed on your screen. Hence, the IP address
here belongs to an Educative server!
Ports #
Any host connected to the Internet could be running many network
applications. In order to distinguish these applications, all bound to the same
IP address, from one another, another form of addressing, known as port
numbers, is used. Each endpoint in a communication session is identified
with a unique IP address and port combination. This combination is also
known as a socket. So in essence, ports help to address the packet to specific
applications on hosts.
It is generally considered best practice not to use these ports for any
user defined applications although there is no technical restriction
on using them.
The ports 49152–65535 can be used by user applications or for other
purposes (dynamic port allocation for instance, but more on that later).
Quick Quiz! #
1
Which of the following is a valid IP version 4 address?
COMPLETED 0%
1 of 4
In the next lesson, we’ll study some physical and hardware aspects of
computer networks.
The Edge of The Internet: End Systems
Here's an introduction to the important physical components of the Internet that make it tick!
• End Systems
• The Network Edge
• Quick Quiz!
End Systems #
End systems are devices that are connected to the Internet. They include:
Desktop computers
Servers
Mobile devices
IoT devices.
Router
Laptop
Router
Server
Desktop
Desktop
The end systems out of them -- they reside on the "edge of the network"
Note that the two networks shown could be connected through any number
of intermediate networks such as those for their Internet Service Providers.
Since the actual path doesn’t matter, we obfuscate the interconnectivity by
using the cloud symbol.
Quick Quiz! #
1
Which of the following is NOT an end system?
COMPLETED 0%
1 of 2
Now that we understand the basic structure of the Internet, let’s look into how
end systems access the Internet in the next lesson!
What Are Access Networks?
• Access Networks
• Transmission Rates
• Quick Quiz!
Access Networks #
Access networks, also known as the last mile, consist of the media through
which end systems connect to the Internet. In other words, access networks
are networks that physically connect end systems to the first router on a
path which connects them to some other end systems.
= Edge Device
Router
Laptop
Router
Server
Desktop
Desktop
Access networks
Network Interface
Adapter
The network interface adapter enables a
computer to attach to a network. Since
there are so many different types of
networks, network adapters are used so
that the user can install one to suit the
network to which they want to attach.
A network card. Taken from:
https://commons.wikimedia.org/wiki/File:Network_card.jpg#filelinks
Network interfaces also usually have an Notice the port that the cable would go into.
address associated with them. One
machine may have multiple such
interfaces.
Most machines then have external ports which network cables can be plugged
into. The type of access network depends on the physical media involved.
Here are some common access networks:
We’ll go through the what and how of most of these access network in the
next few lessons.
But first, how is the speed of a network measured? What exactly is the unit of
speed of a network? Let’s have a look!
Transmission Rates #
The rate at which data is transmitted from one point to another in a network
is called the transmission rate. In other words, the speed of the network is its
transmission rate.
The smallest unit that digital data can be divided into is a bit: a 1 or a 0.
Transmission rates are measured in units of bits/sec. However, since bits/sec
is a really small unit, multiples/prefixes are commonly used. Common
prefixes and their interpretation is given below:
For example, a speed of 240 Mbit/second means that 240, 000, 000 or 240
million bits get transmitted every second!
There are essentially two ways that data flows in a network: from an end
system or to an end system. The outgoing transmission rate is called the
upload rate, and the incoming transmission rate is called the download rate.
Some networks have varying upload and download transmission rates, called
asymmetric transmission rates. This is useful because traffic going out from
end hosts generally consists of small requests which solicit comparatively
much larger responses.
📝 Note You can check the upload and download transmission rate of
your Internet connection from https://www.speedtest.net!
Quick Quiz! #
1
What does ‘asymmetric transmission rate’ mean?
COMPLETED 0%
1 of 2
Now that we’ve covered the basics, let’s move on to some detail about the
physical media that actually make up these networks.
Types of Access Networks: DSL
There are a number of ways that your end system can access the Internet, let's look at each in detail!
Now that we know what access networks are, let’s look at some common
types.
For reference, the human hearing range goes from 20 Hz to 20 kHz and the
average human voice range goes from 85 Hz to 255 Hz.
0 Hz 1 MHz
0 Hz 1 MHz
0 Hz 1 MHz
The upstream channel takes up the 4-50KHz band
3 of 4
0 Hz 1 MHz
These signals are then carried by telephone wires over to the ISP
Then, these high-frequency analog signals are converted back to digital
signals using a device at the ISP’s end called a Digital Subscriber Line
Access Multiplexer (DSLAM).
The signals are then forwarded to the end system that it was meant to
reach
The end system outputs data to a 'DSL router' (a DSL modem and router combined into one)
2 of 5
Telephone wires
The DSL router encodes the digital data into audio frequencies and sends it over telephone wires
towards the ISP
3 of 5
Telephone wires
ISP
Telephone wires
ISP
The DSLAM decodes the audio frequencies into digital signals and sends them over to the
intended end system
5 of 5
Due to the asymmetry between the width of the three channels, this type of
DSL is termed as Asymmetric DSL (ADSL). Symmetric DSL, on the other
hand, offers equal upstream and downstream bandwidth.
Quick Quiz! #
Q What’s a DSLAM?
COMPLETED 0%
1 of 1
Now that we have an overview of DSL, let’s look at a few other common access
networks in the next lesson!
Types of Access Networks: Cable, FTTH, Dial-Up, and
Satellite
• Cable Internet
• How It Works
• Slower During Peak Hours
• Hybrid Fiber Coax
• Transmission Rate
• Fiber To The Home: FTTH
• Transmission Rate
• Dial-Up
• Transmission Rate
• Satellite
• Transmission Rate
• Quick Quiz!
Cable Internet #
In the case of cable Internet, the TV cable company is the ISP and it relies
on the preexisting infrastructure of cable TV to grant Internet access.
It runs on coaxial cable. Coaxial cable has enough of a frequency range to
carry TV channels and a stream of upstream and downstream Internet.
How It Works #
In essence, cable Internet works very similarly to DSL.
A device on both the user’s end and the ISP’s end modulates the analog
signals to digital and vice versa.
Slower During Peak Hours #
However, cable Internet can be slower during peak hours, when a majority of
users are online at once. This is because cable Internet is a shared broadcast
medium: every signal that comes from the ISP is sent to every single home
regardless of which one it was meant for.
Fiber
Coaxial Cable
... an entire
ISP Optical Node
neighborhood
Transmission Rate #
According to DOCSIS 4.0, cable Internet can now operate in symmetric speeds
(where both upstream and downstream channels have the same speed) of up
to 10 gbps.
Transmission Rate #
FTTH can be very fast up to 2.5 gbps.
Dial-Up #
Dial-Up uses a modem over the telephone line, but does not fully utilize the
spectrum of the transmission medium. It only uses the traditional voice
channel frequencies. Hence, it is slower than DSL.
Transmission Rate #
Dial-up is non-broadband and very uncommon now. The speed is at most 56
kbps.
Satellite #
The Internet can also be accessed via satellites. This can be beneficial in
remote areas where other physical access networks are not available.
Transmission Rate #
This would depend on a number of factors including the kind of satellite.
Some setups can provide incredibly fast downlink and uplink connections,
however, on average, the download rate is at around 1 mbps and the average
upload rate 256 kbps.
Quick Quiz! #
1
Which access network utilizes telephony infrastructure on the last mile?
COMPLETED 0%
1 of 4
Now that we've discussed the infrastructure of the edge of the Internet let's discuss some actual hardware
components.
• Communication Media
• The Internet Is Under the Sea, Not in the Clouds!
• Quick Quiz!
Communication Media #
Data needs to be transmitted from one end system to another over a medium.
There are two kinds of media: guided and unguided. Each has its own
advantages and disadvantages. Let’s discuss the common guided ones in more
detail now.
Guided Media
A medium in which the signal is transported on a
confined pathway is called guided. Some commonly
used examples are given below.
Twisted Pair Copper Wire Twisted pair copper wire taken from
https://commons.wikimedia.org/wiki/File:TwistedPair_
FTP.jpg under GNU free documentation license
This kind of wire is used in DSL and Dial-Up. It
consists of pairs of copper wires that are twisted
together to reduce electrical interference. Each wire
is about a milimeter thick and is one
communication link. Generally, several pairs are
bundled together in a protective plastic or rubber
wrapping.
Coaxial Cable
Cable Internet runs on coaxial cables. A coaxial
cable consists of one copper wire surrounded by an
insulating material, followed by a mesh-like
cylindrical metallic shield, followed by another
Coaxial Cable taken from
insulating cover. https://en.wikipedia.org/wiki/File:Coaxial_cable_cutaway
under CC-BY-3.0
Fiber Optics
Fiber optic cables carry light instead of electrical
signals.
Multimode
Multimode uses LED send signals.
Therefore it’s, significantly cheaper than
counterpart.
It can work over several tens of kilometers.
However, fiber optic has not dominated over the rest, because of the high cost
of optical devices. However, fiber to the home is becoming increasingly
common.
Also, check out the Optical Carrier transmission rates. They specify the
transmission rates of fiber optic cable. At the time of writing, they range from
to 51.84 Mbit/sec to 200 Gbit/sec!
Quick Quiz! #
1
Why is fiber optic cable not as popular for commercial use as one would
expect considering the advantages of high speed minimal loss
transmission?
COMPLETED 0%
1 of 2
• Unguided Media
• Terrestrial Radio Channels
• Long-Term Evolution (LTE)
• Free-space optical communication
• Quick Quiz!
Unguided Media #
Means of transmission that are not bound by a confined pathway are called
unguided media, such as radio waves. Let’s discuss them in more detail.
In free space optics, lasers can be used to achieve high data rates. However,
free space optics suffers from interference by factors like fog, dust particles
and smog. Recently, researchers have demonstrated the utility of free space
optics for high speed communication in data centers.
Quick Quiz! #
COMPLETED 0%
1 of 2
In this lesson, we'll discuss the types of networks based on geographical distance.
• Introduction
• Local Area Networks (LANs)
• Ethernet
• WiFi
• Metropolitan Area Network (MAN)
• Wide Area Networks (WAN)
• SONET/SDH
• Frame Relay
• Quick Quiz!
Introduction #
Computers or end systems are generally connected together to share
resources and information such as an Internet connection and devices such as
printers. These networks can be classified by the geographical distance that
they span. Have a look.
📝 Note ‘small area’ does not imply anything about the number of end
systems connected together – just the geographical area. A LAN can
consist of hundreds or even thousands of systems.
Let’s discuss some examples of LANs which also are access networks that we
skipped previously.
Ethernet #
Most LANs consist of end hosts connected using Ethernet network adapters to
Ethernet switches. Every Ethernet switch has a limited number of ports, and
therefore can interconnect a limited number of end hosts. Larger networks
within a building are built using multiple Ethernet switches interconnecting
different sets of end hosts. These switches may then be connected to each
other and the Internet
WiFi #
Increasingly, however, wireless Internet access has become very common. In
Wireless LANs or WLANs, a wireless router interconnects different “subnets”
and/or may have connectivity to the Internet, which it can extend to the hosts
connected to it.
WANs are typically optical fiber-based. Frame relay, ATM, and SONET/SDH
are examples of technologies that may be in use.
Frame Relay #
A frame relay was a popular way to connect your LAN to the Internet or to
provide an interconnection between LANs at two or more company sites.
Quick Quiz! #
1
A computer network that spans a large geographical area connecting
several sites of an organization, such as a university or company,
possibly across many countries is called a ______
COMPLETED 0%
1 of 2
We'll study the types of computer networks based on topologies in this lesson.
• Bus
• Limitations
• Ring
• Limitations
• Star
• Limitations
• Tree
• Mesh
• Limitations
• Quick Quiz!
Bus #
Every end system will receive any signal that is sent on the main or backbone
medium. The medium can be guided or unguided.
Limitations #
A break in the cable will disrupt the entire network.
Ring #
In this topology, end systems communicate with each other through each
other. So the message travels along the ring passing each system until the
target system itself is reached. Theoretically, n/2 systems can be transmitting
to their adjacent neighbor at the same time.
Limitations #
The basic ring topology is unidirectional so n − 1 end systems would
have to transfer messages for end system #1 to talk to end system #n
Ring Topology
Star #
All end systems talk to each other through one central device such as a router
or switch. Routers and switches are discussed in-depth in the data link layer
chapter!
Limitations #
Hosts can all be transmitting at the same time. However, if the central
device fails, the network is completely down.
Star topology
Tree #
This topology is also known as the star-bus topology. It essentially consists of
a bunch of star networks connected together with a large bus.
tree topology
Mesh #
In this topology, every end system is directly connected to every other end
system.
Limitations #
The mesh topology (if physically realized as a mesh):
Is expensive
Hard to scale
Used in specialized applications only
mesh topology
Quick Quiz! #
COMPLETED 0%
1 of 2
Starting in the next lesson, we’ll learn about how the working of the Internet
is organized into conceptual layers.
Layered Architectures & Protocol Stacks
Layered architectures are a way to organize computer networks. Let's dive right in!
Why Layers? #
Layered architectures give us modularity by allowing us to discuss specific,
well-defined parts of larger systems. This makes changing
implementation-level details and identifying bugs easier.
An Analogy: Post #
Before we dive deep into different models of the network layer stack, let’s look
at an interesting analogy.
Think about posting a letter or a package. The general steps to doing so are as
follows,
The local post office sorts 6 The letter is received at the local
3 Post Office Post Office post office on the receiver's end
the letter and sends it
Transportation such as a plane, ship, or van takes 4 5 Transportation such as a plane, ship, or van takes
the letter to the receiver's local post office. This Transportation Transportation the letter to the receiver's local post office. This
actually may be a chain of transportation mediums. actually may be a chain of transportation mediums.
Posting a letter
Notice that a few things are in parallel with computer networking here.
Here are some examples of how that is the case:
Furthermore, all a sender knows and cares about is that once they write a
letter, put it in an envelope, stick a stamp on it and drop it in a letterbox, it
will eventually be delivered at the destination. Whether it’s transported on
pickup trucks, on railway trains or by air is irrelevant and immaterial to
senders. So, how layer 4 does its job is irrelevant to the layers above, and
that’s called abstraction.
For example, the top layer in most layered models is called the
application layer. End-user applications live in the application layer,
which includes the web and email and are almost always implemented in
software. The application layer is also where an outgoing message starts
its journey.
Since the underlying layer collects messages from the upper layer for delivery
to the destination and hands over messages destined for the upper layer, it
serves the application layer. Furthermore, the application layer abstracts,
and hence is not concerned with any implementation details of the layers
below.
2 .. .. 7
3 .. .. 6
4 .. .. 5
The applications in the application layer can send and receive almost any
form of data, be it an mp3 file or a word document.
Take a look at the following drawing. We have not given names to these layers
because we have not introduced them yet, but the general idea is depicted.
Layer 3 Layer 3
Layer 2 Layer 2
Layer 1 Layer 1
The OSI layer model will help us to understand the overall picture of how computer networks work without getting
into too many low-level details.
• Common Models
• The OSI Model
• The Layers of the OSI Model
• Mnemonic
• Application Layer
• Presentation Layer
• Session Layer
• Transport Layer
• Network Layer
• Data Link Layer
• Physical Layer
• Quick Quiz!
Common Models #
There are several models along which computer networks are organized. The
two most common ones are the Open Systems Interconnection (OSI) model
and the Transmission Control Protocol/Internet Protocol (TCP/IP) model.
We will discuss each model and the differences between the two in detail
starting with the OSI model.
protocols had not fully matured. The OSI model provides a standard for
different computer systems to be able to communicate with each other.
Application
Presentation
Session
Transport
Network
Data Link
Physical
Mnemonic #
A good mnemonic device to help remember these layers is:
Application Away
Presentation Pizza
Session Sausage
Transport Throw
Network Not
Data Link Do
Physical Please
Here are some key responsibilities of each layer. Note that we are listing only
some of the responsibilities of each layer. The exhaustive discussion is
deferred to later chapters.
Application Layer #
These applications or protocols are almost always implemented in
software.
End-users interact with the application layer.
The application layer is where most end-user applications such as web
browsing and email live.
The application layer is where an outgoing message starts its journey so it
provides data for the layer below.
Presentation Layer #
Presents data in a way that can be easily understood and displayed by
the application layer.
Session Layer #
The session layer’s responsibility is to take the services of the transport
layer and build a service on top of it that manages user sessions.
Transport Layer #
The transport layer also has protocols implemented largely in software.
Since the application, presentation and session layers may be handing off
large chunks of data, the transport layer segments it into smaller chunks.
These chunks are called datagrams or segments depending on the
protocol used.
Furthermore, sometimes some additional information is required to
transmit the segment/datagram reliably. The transport layer adds this
information to the segment/datagram.
Network Layer #
Network layer messages are termed as packets.
They facilitate the transportation of packets from one end system to
another and help to determine the best routes that messages should
take from one end system to another.
Routing protocols are applications that run on the network layer and
exchange messages with each other to develop information that helps
them route transport layer messages.
Load Balancing There are many links (copper wire, optical fiber,
wireless) in a given network and one objective of the network layer is to
keep them all roughly equally utilized. Otherwise, if some links are
under-utilized, there will be concerns about the economic sense of
deploying and managing them.
Physical Layer #
Consists largely of hardware.
Provides a solid electrical and mechanical medium to transmit the data.
Transmits bits. Not logical packets, datagrams, or segments.
Also has to deal with mechanical specifications about the makeup of the
cables and the design of the connectors.
We’ve mostly already studied what constitutes the physical layer. We don’t
need to know more than what we’ve looked at in the Phyical Communication
Media chapter.
Presentation Presentation
Session Session
Transport Transport
Network Network
Physical Physical
Presentation Presentation
Session Session
Transport Transport
Network Network
Physical Physical
Presentation Presentation
Session Session
Transport Transport
Network Network
Physical Physical
Presentation Presentation
Session Session
Transport Transport
Network Network
Physical Physical
Presentation Presentation
Session Session
Transport Transport
Network Network
Physical Physical
Presentation Presentation
Session Session
Transport Transport
Network Network
Physical Physical
Presentation Presentation
Session Session
Transport Transport
Network Network
Presentation Presentation
Session Session
Transport Transport
Network Network
Presentation Presentation
Session Session
Transport Transport
Network Network
Presentation Presentation
Session Session
Transport Transport
Network Network
Presentation Presentation
Session Session
Transport Transport
Network Network
Presentation Presentation
Session Session
Transport Transport
Network Network
Presentation Presentation
Session Session
Transport Transport
Each layer 'thinks' that it
is talking to the peer
entity on the other side!
Network Network
Quick Quiz! #
• Introduction
• The Layers of The TCP/IP Stack
• TCP/IP vs OSI
• Key Differences
• Differences in Layer Functionality
• There is No Unanimous Stack
• The End-To-End Argument in System Design
• Packet Switched Core
• Quick Quiz!
Introduction #
The TCP/IP Model, also known as the Internet protocol suite, was
developed in 1989.
This model is primarily based upon the most protocols of the Internet,
namely the Internet Protocol (IP) and the Transmission Control
Protocol (TCP).
The protocols in each layer are clearly defined, unlike in the OSI model.
In this course, we’ll largely adhere to the TCP/IP model and take a
protocol-oriented approach.
Application
Transport
Network
Data Link
Physical
TCP/IP vs OSI #
Key Differences #
Here are some main differences between TCP/IP and OSI.
TCP/IP OSI
Presentation Application
Session
Transport Transport
Network Network
Physical Physical
The TCP/IP protocol suite is heavily influenced by the following design choice,
also known as the end-to-end argument.
Quick Quiz! #
1
The responsibilities of the presentation layer from the OSI model are
handled by the ______ layer in the TCP/IP model.
COMPLETED 0%
1 of 2
Let’s start on the application layer from the next chapter!
What Is the Application Layer?
Application you
are
Transport
here
Network
Data Link
Physical
Writing data off to the network in a format that is compliant with the
protocol in use.
Reading data from the end-user.
Providing useful applications to end users.
Some applications also ensure that the data from the end-user is in the
correct format.
Error handling and recovery is also done by some applications.
Where It Exists #
The application layer resides entirely on end-systems. These end-systems can
be any Internet-enabled device, be it a refrigerator or a tower PC.
The development of the Internet in the last century started with text-
based network apps such as e-mail.
Then came the app: the World Wide Web which revolutionized
everything.
Since then, we have come up with voice over IP, (WhatsApp calls), video
chat (Skype), and video streaming (YouTube).
Social media has also taken the world by storm resulting in complex
human social networks and businesses building on top of these websites.
Let’s uncover some of the underlying application layer protocols, in the next
few lessons.
Network Application Architectures
• Client-Server Architecture
• Servers
• Clients
• An Example
• Data Centers
• Peer-to-Peer Architecture (P2P)
• An Example
• Hybrid
• Quick Quiz!
Client-Server Architecture #
In this architecture, a network application consists of two parts: client-side
software and server-side software. These pieces of software are generally
called processes, and they communicate with each other through messages.
Servers #
The server process controls access to a centralized resource or service such as
a website.
A good analogy is a 24/7 pizza delivery place. They are always open and have
a phone number with which they can always be reached.
Clients #
Client processes use the Internet to consume content and use the services.
Client processes almost always initiate connections to servers, while server
processes wait for requests from clients.
An Example #
A good example of the client-server architecture is the web.
Take Google for instance. Google has several servers that control access to
videos. So when a google.com is accessed, a client process (a browser)
requests Google’s homepage from one of Google’s servers. That server was
presumably online, got the request, and granted access to the page by sending
it.
Data Centers #
Now, you might have noticed that we mentioned that Google has servers and
not one server. That’s because, as mentioned previously, when client-server
applications scale, one or even two servers can’t handle the requests from a
large number of clients. Additionally, servers may crash due to any reason
and might stop working. Most applications have several servers in case one
fails. Therefore, several machines host server processes (these machines are
called servers too), and they reside in data centers.
Data centers are buildings that house servers. Facebook, for example, has
“nearly 15 million square feet of data center space completed or under
construction, with several million more feet in the planning stages” as of 2018.
Client Server
Clients obtain content (such as videos) and/or services (such as an online calculator) from servers.
The key advantage of the P2P architecture is that it can scale rapidly – without
the need of spending large amounts of money, time or effort.
server. Strictly speaking, the peer that initiates a connection is the client, and
the other one is called the server.
An Example #
A lot of popular applications today, like BitTorrent, are based on P2P
architectures.
When a file is downloaded via BitTorrent, the downloading party accesses bits
of the file on several other users’ computers and puts them together on its
end. No traditional ‘server’ is involved in this scenario.
📝 Note: P2P Is Not the Same as File Sharing! Some early P2P
applications were used for file sharing. For example, Napster and
Gnutella. Because of the massive impact of these P2P applications, a lot
of people associate file sharing exclusively with P2P.
Also, file sharing is not the only application of P2P. Other examples
include: streaming media, telephony, content distribution, routing, and
volunteer computing.
Hybrid #
The hybrid architecture involves server involvement to some degree. It’s
essentially a combination of the P2P and client-server architectures.
Quick Quiz! #
1
What architecture is the web based on?
COMPLETED 0%
1 of 5
Let’s look at how processes communicate across machines in the next lesson!
P2P vs. Client-Server
Before we move on with the details of BitTorrent, it's useful to do a quantitative comparison of the hybrid
architecture with the client-server architecture.
A server that can upload at a rate of ups where ups is the upload speed
in bits/second.
There are N clients all wanting to download the same file from the
server. Client i can upload at a rate of upi bits/second and download at a
rate of dwni bits/second.
The size of the file that all the peers want is S .
Client-Server #
Let’s start with the client-server architecture. The following can be observed.
Since N clients each want a file of size S , the server will have to upload
N S bits. The upload rate of the server is ups so the server will take at
least N
S
ups
time to transmit the file to all N clients.
The client with the lowest download rate (dwnmin = min(dwni )) will
take at least dwnS time to download the full file.
min
So, in total the time taken to transmit the file will be the maximum of both of
the times above, i.e.:
NS S
max { , }
ups dwnmin
P2P #
We can make the following observations:
Initially, only the server has the file. It has to throw the file out into the
S
network and to do that, it will take at least up time. While the file is
s
being sent out into the network of peers, they start to distribute it
amongst themselves.
The peer with the lowest download rate (dwnmin ) will take at least dwnS
min
time to download the full file.
The file cannot be transmitted faster than the total upload speed of the
entire network: (upsum = {up1 + up2 + up3 + ... + upN }). Since the
file has to be distributed to all N peers, N S bits have to be transmitted,
NS
that will take up time.
sum
Therefore, the time taken in total to distribute a file of size S to N peers is:
S S NS
max { , , }
ups dwnmin upsum
Note that as the number of clients/peers, N , grows, the time taken by the
client-server architecture also grows. Here is a graph of how the distribution
time grows for each architecture as the number of clients/peers grow:
Graph of How p2p Scales vs Client-Server attributed to:
https://pdfs.semanticscholar.org/3de3/1a9b45a3d071c638574117af8e046b578004.pdf
Quick Quiz! #
1
In a client-server model, the rate at which a client can download a file is
limited by the ______.
COMPLETED 0%
1 of 2
Let’s now get into how processes communicate!
How Processes Communicate
Let's have a quick look at the technical aspect of how applications communicate
Thread 2:
Thread 2:
Loads default
Handles fonts
fonts
Processes that exist on the same machine can and do regularly communicate
with each other following the rules of the machine’s OS. However, we are
more interested in how processes that run on different machines
communicate.
Sockets #
Processes on different machines send messages to each other through the
computer network. The interface between a process and the computer
network is called a socket. Note that sockets do not have anything to do with
hardware – they are software interfaces.
Processes simply direct their messages to sockets and don’t worry about it
after that.
Addressing #
Messages have to be addressed to a certain application on a certain end
system. How is it done with potentially millions of end systems and hundreds
of applications on each of them?
Well, it’s done via addressing constructs like IP addresses and ports. While
both were touched upon previously, we would like to reintroduce ports a bit
more in-depth.
Ports
Since every end-system may have a number of
applications running, ports are used to address the
packet to specific applications. As stated previously,
some ports are reserved such as port 80 for HTTP
and port 443 for HTTPS.
An Analogy: Post
Ephemeral Ports
Well, the answer lies in Ephemeral Ports. Different port numbers are
dynamically generated for each instance of an application. The port is freed
once the application is done using it.
Furthermore, server processes need to have well defined and fixed port
numbers so that clients can connect to them in a systematic and predictable
way. However, clients don’t need to have reserved ports. They can use
ephemeral ports. Servers can also use ephemeral ports in addition to the
reserved ones. For instance, a client makes the initial connection to the server
on a well-known port and the rest of the communication is carried out by
connecting to an ephemeral port on the server.
Quick Quiz! #
1
A process is a running instance of a program
COMPLETED 0%
1 of 3
Now that we are familiar with some basic application layer terms, in the next
lesson, let’s get into the finer details of some key application layer protocols!
HTTP: The Basics
Welcome to the core of this course! We are nally getting started with protocols, the rst of which is HTTP.
• Introduction
• Objects
• The Anatomy of a URL
• HTTP
• HTTP Requires Lower Layer Reliability
• Types of HTTP Connections
• Non-persistent HTTP
• Persistent HTTP
• Quick Quiz!
Introduction #
The Internet was an obscure set of methods for file transfer and email used by
academics and researchers. The World Wide Web was invented to allow the
European research organization CERN to present documents linked by
hypertexts. All of that changed though when it caught the public’s eye and
popularized the Internet. The web was different from other services such as
cable television, because it served content based on demand. People could
watch what they wanted. HTTP or HyperText Transfer Protocol is the
protocol at the core of the web.
Objects #
Web pages are objects that consist of other objects.
An object is simply a file like an HTML file, PNG file, MP3 file, etc.
Each object has a URL
The base object of a web page is often an HTML file that has references
to other objects by making requests for them via their URL.
Protocol in use
The hostname of the server
The location of the file
Arguments to the file
http://www.educative.io/allourses/course.php?auth=44&user=5
1 of 5
http://www.educative.io/allourses/course.php?auth=44&user=5
Protocol
2 of 5
Hostname of server
http://www.educative.io/allourses/course.php?auth=44&user=5
Protocol
3 of 5
Hostname of server
http://www.educative.io/allourses/course.php?auth=44&user=5
Protocol path to resource
4 of 5
Hostname of server Arguments to course.php
http://www.educative.io/allourses/course.php?auth=44&user=5
Protocol path to resource
5 of 5
HTTP #
Let’s get back into HTTP. It’s a client-server protocol that specifies how Web
clients request Web pages from Web servers and how Web servers send them.
Client Server
Clients obtain content (such as videos) and/or services (such as an online calculator) from servers.
The first message is called an HTTP request and the second one an HTTP
response. There’s a whole class of protocols that are considered request-
response protocols. HTTP is one of them. We will look in more detail at the
HTTP request message in the next lesson and response after that!
Note that HTTP is a stateless protocol: servers do not store any information
about clients by default. So if a client requests the same object multiple times
in a row, the server would send it and would not know that the same client is
requesting the same object repeatedly.
For now, all you need to know is that TCP ensures that messages are
always delivered. Messages get delivered in the order that they are sent.
UDP does not ensure that messages get delivered. This means that
some messages may get dropped and so never be received.
HTTP uses TCP as its underlying transport protocol so that messages are
guaranteed to get delivered in order. This allows the application to
function without having to build any extra reliability as it would’ve had
to with UDP.
This sort of reliance on other layers for certain jobs is one of the key
advantages of a layered architecture!
These two kinds of HTTP connections use TCP differently. Let’s discuss the key
advantages and disadvantages of each.
Non-persistent HTTP #
Non-persistent HTTP connections use one TCP connection per request.
Assume a client requests the base HTML file of a web page. Here is what
happens:
3. The server retrieves the requested object from its storage and sends it
4. The client receives the object which in this case is an HTML file. If that
file has references to more objects, steps 1-4 are repeated for each of
those
5. The server closes the TCP connection
For each HTTP request, more requests tend to follow, as well to fetch images,
javascript files, CSS files, and other objects.
The underlying TCP connection requires three TCP messages are sent between
the client and server. Similarly, when the connection is closed, three TCP
messages are sent back and forth between the client and server.
Persistent HTTP #
An HTTP session typically involves multiple HTTP request-response pairs, for
which separate TCP connections are established and then torn down between
the same client and server. This is inefficient. Later on, Persistent HTTP was
developed, which used a single client-server TCP connection for all the HTTP
request-responses for a session.
Typically, if there have been no requests for a while, the server closes the
connection. The duration of time before the server closes the connection is
configurable.
Quick Quiz! #
1
What does HTTP stand for?
COMPLETED 0%
1 of 8
In the next lesson, we’ll discuss HTTP request messages in more detail.
HTTP: Request Messages
HTTP request messages are a pivotal part of the protocol. Let's have a close look at them!
• Introduction
• HTTP Request Messages
• The Anatomy of an HTTP Request Line
• HTTP Methods
• URL
• Version
• The Anatomy of HTTP Header Lines
Introduction #
There are two types of HTTP messages as discussed previously:
Method
URL
Version
Request Method
URL to resource
3 of 4
HTTP Version
4 of 4
HTTP Methods #
HTTP methods tell the server what to do. There are a lot of HTTP methods but
we’ll study the most common ones: GET , POST , HEAD , PUT , or DELETE .
This method is generally used when the client is not sure where the
new data would reside. The server responds with the location of the
object.
The data posted can be a message for a bulletin board, newsgroup,
mailing list, a command, a web form, or an item to add to a database.
The POST method technically requests a page but that depends on
what was entered.
HEAD is similar to the GET method except that the resource requested
does not get sent in response. Only the HTTP headers are sent instead.
object still exists. This includes its meta-data, like the last modified
date. The latter can be useful for caching.
This is also useful for testing and debugging.
Note that while most forms are sent from the POST method, the GET method is
also used sometimes with the entries of the form appended to the URL, as in
arguments like this:
http://www.website.com/form.php/?Name=PostMan?Age=45?Interest=Post
1. The amount of data that can be sent via a post request is unlimited.
2. The form’s fields are not shown in the URL.
Version #
The HTTP version is also specified in the request line. The latest version of
HTTP is HTTP/2.
The first header line specifies the Host that the request is for.
The second one defines the type of HTTP Connection . It’s Non-persistent
in the case of the following drawing as the connection is specified to be
closed.
The user-agent line specifies the client. This is useful when the server
has different web pages that exist for different devices and browsers.
GET/path/to/file/index.html HTTP/1.1
Host: www.educative.io
Connection: close
User-agent:Mozilla/5.0
Accept-language: fr
Accept: text/html
1 of 7
Request
line
GET/path/to/file/index.html HTTP/1.1
Host: www.educative.io
Connection: close
User-agent:Mozilla/5.0
Accept-language: fr
Accept: text/html
2 of 7
3 of 7
GET/path/to/file/index.html HTTP/1.1
Host: www.educative.io Connection indicates
Connection: close if the connection is to
User-agent:Mozilla/5.0 be persistent or not.
Accept-language: fr In this case, it is not
persistent and will be
Accept: text/html closed after every
message.
4 of 7
GET/path/to/file/index.html HTTP/1.1
Host: www.educative.io
Connection: close
The user-agent line User-agent:Mozilla/5.0
specifies the client. In Accept-language: fr
this case, it specifies Accept: text/html
the user's browser
5 of 7
GET/path/to/file/index.html HTTP/1.1
Host: www.educative.io
Connection: close
User-agent:Mozilla/5.0
Accept-language: fr
Accept: text/html The Accept-language
header specifies the
language that is preferred.
In this case, French.
6 of 7
GET/path/to/file/index.html HTTP/1.1
Host: www.educative.io
Connection: close
User-agent:Mozilla/5.0
Accept-language: fr
Accept: text/html
The Accept header
defines the sort of
response to accept. In
this case, it will
accept text or HTML
files
7 of 7
In the next lesson, we’ll conduct an exercise to look at real HTTP request
messages!
Exercise: Looking at a Real HTTP Request
In this lesson, you will be looking at real HTTP messages right from your browser!
NID=somehiddenstring
Now that we have a clear idea of what HTTP request messages look like, let’s
study the response messages in the next lesson.
HTTP: Response Messages
• Introduction
• Status Line
• Status Code
• Header Lines
• How HTTP Headers Are Chosen
• Quick Quiz on HTTP!
Introduction #
Let’s start with a typical example of an HTTP response message:
HTTP/1.1 200 OK
Connection: close
Date: Tue, 18 Aug 2015 15: 44 : 04 GMT
Server: Apache/2.2.3 (CentOS)
Last-Modified: Tue, 18 Aug 2015 15:11:03 GMT
Content-Length: 6821
Content-Type: text/html
It has 3 parts: an initial status line, some header lines and an entity body.
Status
HTTP/1.1 200 OK
Connection: close
Date: Tue, 18 Aug 2015 15: 44 : 04 GMT
Server: Apache/2.4.39 (CentOS)
Header Lines
Last-Modified: Th, 08 Aug 2013 23:54:35 GMT
Content-Length: 6821
Content-Type: text/html
Status Line #
HTTP response status lines start with the HTTP version.
Status Code #
The status code comes next which tells the client if the request
succeeded or failed.
There are a lot of status codes:
1xx codes fall in the informational category
2xx codes fall in the success category
3xx codes are for redirection
4xx is client error
5xx is server error
200 OK : the request was successful, and the result is appended with the
response message.
404 File Not Found : the requested object doesn’t exist on the server.
400 Bad Request : generic error code that indicates that the request was in
a format that the server could not comprehend.
500 HTTP Internal Server Error : the request could not be completed
because the server encountered some unexpected error.
505 HTTP Version Not Supported : the requested HTTP version is not
supported by the server.
Header Lines #
Let’s study the header lines.
Connection type. In this case, indicates that the server will close the
TCP connection after it sends the response.
Last-Modified. The date on which the object being sent was last
modified.
Content-Type. The type of content. The type of the file is not determined
by the file extension of the object, but by this header.
COMPLETED 0%
1 of 5
In the next lesson, we’ll look at real HTTP responses via a simple command-
line tool!
Exercise: Looking at a Real HTTP Response
In this lesson, we'll look at and study real HTTP responses via cURL.
• cURL
• Explanation
• Sample Output
• Quick Quiz!
cURL #
Run the following command to look at a real HTTP response.
Explanation #
Let’s learn about all of its components.
curl is the name of the command that tells the terminal that this is a curl
command.
The --head flag or -I in short, tells cURL to send an HTTP request with
the head method. In other words, the entity body of the HTTP message is
not fetched.
The -silent flag tells cURL to not display the progress meter. The
progress meter is interpreted as an error on our platform, which is why
we decided to remove it. The command is perfectly fine without this flag
otherwise.
We encourage you to explore the cURL command. You can find a list of all the
flags under the ‘options’ heading on cURL’s manpage. Try different websites
and different flags and see what you get!
Sample Output #
The output of this command is an HTTP response such as the following. Notice
the HTTP response code and the headers.
HTTP/1.1 200 OK
Content-Encoding: gzip
Accept-Ranges: bytes
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Mon, 23 Sep 2019 06:48:39 GMT
Etag: "1541025663"
Expires: Mon, 30 Sep 2019 06:48:39 GMT
Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
Server: ECS (ord/5726)
X-Cache: HIT
Content-Length: 606
Quick Quiz! #
1
What is cURL?
COMPLETED 0%
1 of 2
In the next lesson, let’s have a look at one of the key concepts of computer
networks – cookies!
Cookies
• Set-cookie Header
• Example
• Blocking Third-Party Cookies Is Not Enough!
• Quick Quiz!
Introduction
You might have heard of the term ‘cookie’ used a lot
in the context of computer networks and privacy.
Let’s have a closer look at what they are.
These identifiers are set by the server through HTTP headers when the
client first navigates to the website.
After the cookie is set, it’s sent along with subsequent HTTP requests to
the same server. This allows the server to know who is contacting it
and hence serve content accordingly.
So the HTTP request, the HTTP response, the cookie file on the client’s
browser, and a database of cookie-user values on the server’s end are all
involved in the process of setting and using cookies.
Set-cookie Header #
Let’s look at how cookies work in a bit more detail. When a server wants to set
a cookie on the client-side, it includes the header Set-cookie: value in the
HTTP response. This value is then appended to a special cookie file stored
on your browser. The cookie file contains:
Have a look at the following slides to see how cookies work in practice.
HTTP Request
First ever request from a client to
educative.io does not contain any cookies.
1 of 3
HTTP Response
can include a session identifier, i.e., a cookie
via the set-cookie header that tracks a
user once they have authenticated
HTTP/1.1 200 OK
Date: Sat, 19 Feb 2011 02:32:58 GMT
Server: Apache/2.2.3 (CentOS)
Connection: Keep-alive
Last-Modified: Tue, 18 Aug 2015 15:11:03 GMT
Set-cookie: session=44ecb091; path=/servlets
Content-Length: 6821
2 of 3
3 of 3
Third-party Cookies
Also, websites may not necessarily know personally
identifiable information about you such as your
name (by the way, websites that require you to sign-
up do know your name), and they may only know
the value of your cookie. But what if websites can
track what you do on other websites? Well, they
can. Welcome to the concept of third-party cookies.
While we can’t go into too much detail, it suffices to know that third-party
cookies are cookies set for domains that are not being visited.
Example #
3. Suppose, the user visits ebay.com, and eBay also has placed an
advertisement for free-stats.com.
4. The same cookie set on the Amazon site will be reused and sent to
free-stats along in an HTTP request with the name of the host that the
user is on.
5. Free-stats can in this way track every website the user visits that they
are advertising on and create more targeted ads in order to generate
greater revenue.
Respawning cookies
Flash cookies
Entity tags
Canvas fingerprinting
Quick Quiz! #
1
What is a cookie?
COMPLETED 0%
1 of 2
Now that we know the basics of cookies, let’s look at them in practice with a
quick exercise!
Exercise: View and Manage Your Cookies
Managing Cookies #
Most modern browsers allow some degree of cookie management (viewing,
editing and deleting), which can be integral to protecting your privacy on the
Internet. Here are some links to instructions on how to manage cookies for
popular browsers. Unfortunately, for most browsers, all cookies cannot be
viewed at once, but they can be managed.
Chrome #
Google has provided some instructions to view cookies on Chrome. Have a
look!
Safari #
This Apple Support page has some instructions on managing cookies. While
you can’t view the content of the cookie files, you can view which websites
have stored cookies on your browser.
Firefox #
Here’s an official page to manage cookies on Firefox.
Let's now discuss some important protocols that make email what it is.
• History of SMTP
• How SMTP Works
• Error Handling
• Quick Quiz!
Introduction
Email has been a key application of the Internet since
its early days.
SMTP
There are many protocols associated with email. One popular choice is a
combination of POP3 and SMTP . One is used to send emails that are stored in a
user’s inbox and the other is used to retrieve emails sent to you. However, the
very core of electronic mail is the Simple Mail Transfer Protocol (SMTP).
SMTP uses TCP, which means that transfers are reliable. The connection is
established at port 25 .
📝 Note A good mnemonic to remember what SMTP does is Sending
Mail To People.
Also, for ease and consistency, we are defining User Agents as agents that
allow users to compose, view, delete, reply to, and forward emails.
Applications such as Apple Mail, Microsoft Outlook, and Gmail’s webmail are
examples of user agents.
History of SMTP #
Let’s delve a bit into the history of SMTP which is incredibly important to
understand why it’s designed the way it is.
SMTP predates HTTP by quite a margin and therefore has some antiquated
design properties.
For example, all SMTP messages have to be encoded into 7-bit ASCII. This
made sense in the early days when computer networks did not have the
capacity to email large images, audio, or videos, and when email was
primarily text that could fit in ASCII characters. We needed 7-bit encoding and
decoding because mostly US-ASCII characters were being used in which the
MSB (most significant bit) of the byte was 0, so there was no point in sending 8
bits per character. Instead 7 bits per character were transmitted.
Sending text with characters that require a greater number of bits per
character, or binary data became challenging. Therefore, all email
attachments are encoded into 7-bit ASCII even today when sending, and then
decoded upon receiving. This requires additional computational work.
1. When an email is sent, its sent to the sender’s SMTP server using the
SMTP protocol.
The SMTP server is configured in your email client. The general
format of the domain of the SMTP server is smtp.example.com where
the main email address of the sender is [email protected] . But it’s not
mandatory to adhere to this format. We could set up, say,
zeus.example.com to serve as our SMTP server, if we wanted. From a
security point of view, it is probably a good idea, since people are
unlikely to guess it as easily.
3. Then, the SMTP server initiates a connection with the recipient server
and will conduct an initial SMTP handshake.
4. The SMTP server will finally send the message to the recipient’s email
server.
5. The email is then downloaded from the recipient’s SMTP server using
other protocols when the recipient logs in to their email account or
'user agent.’ In other words, the recipient’s SMTP server copies the email
to the recipient’s mail-box.
📝 Note SMTP is a push protocol because the email client sends the
email out to the server when it needs to. Which means it only sends data
to servers. Other protocols called Mail Access Protocols such as POP
and IMAP are used for getting email from a server and are called pull
protocols because the client asks their POP/IMAP server if they have any
new messages whenever they feel like.
Sender Recipient
Sender Recipient
Sender Recipient
Sender Recipient
After opening an initial connection and conducting an SMTP handshake with the receipient's
server, the sender's server sends the email.
4 of 7
Sender's SMTP Server Recipient's SMTP Server
Sender Recipient
The email resides on the recipient's server in the recipient’s inbox. The email will be downloaded to
the recipient's user agent once they log in.
5 of 7
Sender Recipient
Sender Recipient
Error Handling #
There are many scenarios where sending an email may fail. Here are a few.
If the email is not sent for any reason such as a misspelled recipient
address, the email is returned to the sender with a “not delivered”
message.
If the recipient SMTP server is offline, the sending SMTP server keeps
trying to send the email, say, every 30 minutes or so. It stops trying
after a few days and alerts the sender about the mail not delivered
error.
Also, SMTP does not use any intermediate servers. So, even if an attempt
to send an email fails because of any reason such as the receiving server
being down, the email won’t be stored on an intermediary server. It will
be stored on the sending SMTP server.
Quick Quiz! #
1 SMTP is a pull protocol
COMPLETED 0%
1 of 2
• nslookup
• Outlook
• Gmail
• Yahoo!
nslookup #
nslookup , or name server lookup, is a command-line tool that can be used to
find the name and IP address of the SMTP server for a domain like live.com or
gmail.com. Have a look at the following command.
Outlook #
The mx in the -type=mx flag stands for Mail Exchanger record, which
essentially means the SMTP server.
There is a lot more that nslookup can be used for. Here’s the manpage for
nslookup if you want to learn more.
Gmail #
Yahoo! #
Let’s study pull protocols like POP and IMAP in some detail in the next lesson.
Email: POP & IMAP
Let's now discuss the other side of the coin for how email works.
• POP
• POP Phases
• POP Modes
• IMAP
• Quick Quiz!
POP and IMAP are used to retrieve email from an email server. Either one can
be used. Let’s discuss both.
POP #
The most commonly used version of the Post Office Protocol (POP) is version
3, or POP3. This is how it works:
POP Phases #
Emails are simply downloaded from the server in 4 phases: connect,
authorize, transaction, update.
1. Connect: The user agent first connects to the POP3 server on TCP using
port 110 .
2. Authorize: The user agent authenticates the user with a username and a
password.
3. Transaction: The user can now retrieve emails and mark emails for
deletion.
4. Update: After the user agent quits and closes the POP3 session, the server
makes updates based on the user’s commands. So if the user marked an
email for deletion, it will delete it. No copy of a deleted email is kept on
the server.
POP Modes #
POP works in two modes.
Download and delete: Once emails are downloaded from the server to
the user agent, they are all deleted from there.
Download and keep: Emails are not deleted from the server once they
are downloaded onto the user agent.
However, with the download and delete model, you can only use one client to
check your emails. If you use multiple devices to check your email, this
method is not appropriate because emails will not look the same across
devices at different times. Also, users won’t be able to reread emails from
different devices.
Have a look at the following slides for an example of how emails might not be
in sync on multiple devices with POP.
Sender: work
Subject: Meeting
Sender: Brand
Subject:
Advertisement
Sender: Friend
Subject: Meme
Sender: work
Subject: Meeting
Sender: Brand
Subject:
Advertisement
Sender: Friend
Subject: Meme
Authentication
Sender: work
Subject: Meeting
Sender: Brand
Subject:
Advertisement
Sender: Friend
Subject: Meme
Sender: work
Subject: Meeting
Sender: Brand
Subject:
Advertisement
Sender: Friend
Subject: Meme
Transaction
Quit
Sender: work
Subject: Meeting
Sender: Brand
Subject:
Advertisement
Sender: Friend
Subject: Meme
During the update phase, the server updates its records. In this case, all emails are wiped clean
because no new ones are received
8 of 10
Sender: Brand
Subject:
Advertisement
Sender: Friend
Subject: Meme
Sender: Brand
Sender: Friend2
Subject:
Advertisement Subject: Dinner
Sender: Friend
Subject: Meme
Suppose that the user checks their email via another device, say their cell phone. All 4 steps would
occur again but they won't be able to view their previous emails.
10 of 10
IMAP #
The Internet Message Access Protocol (IMAP), like POP, is also a mail access
protocol used for retrieving email. It is a bit more complex than POP and
hence allows you to view your email from multiple devices. With IMAP,
though:
If an email is deleted from one user agent, it will be deleted off the
server.
Deleted emails won’t be visible from other devices either.
Quick Quiz! #
Q
Which of the following are valid differences between POP3 and IMAP?
COMPLETED 0%
1 of 1
Email: Message Format
• Introduction
• Header Lines
• Message Body
• Exercise: View Raw Emails
Introduction #
Email messages have a format the same way that HTTP request and response
messages do. Let’s dive right into it.
Header Lines #
Email messages start with header lines, much akin to HTTP. The header lines
contain important metadata about the email.
Message Body #
The message body of the email follows the header lines after a blank line.
1. Gmail
2. Outlook
3. Yahoo!
Once you have the instructructions, study the headers. Can you figure out
what each does? For your reference, here is a sample of email headers. Note
that they are a bit simplified for your ease.
Delivered-To: [email protected]
Date: Thu, 16 May 2019 03:36:28 +0000 (UTC)
From: Fahim from Educative <[email protected]>
Mime-Version: 1.0
Reply-to: [email protected]
Subject: Data analysis with R
To: [email protected]
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=UTF-8
Mime-Version: 1.0
Hey User,
With the way technology is evolving, more and more data is being produce
d a=
nd tracked every day. And because of that, the skills to work with that da
t=
a, to make sense of it and turn into useful insight, are more in-demand th
a=
n ever before.
If recent trends are anything to go by, in the future the ability to wor
k w=
ith large quantities of data won=E2=80=99t be the field of just data scien
t=
ists - it=E2=80=99s going to become a necessary skill across industries, k
i=
nd of like using a word processor.
For years, R has been at the forefront of the data science revolution. It=
=E2=80=99s beloved by data scientists and statisticians for its robust sta
t=
istical functionality, outstanding graphing ability, and extensibility thr
o=
ugh packages. The recent data science craze has just breathed new life int
o=
it.
Get started with R, stay on top of the data science craze, and solve real-
w=
orld problems with data.
Happy learning!
-- Fahim
📝 Note the headers are from a received email and not the headers
when it was sent, which is what we discussed initially. So, the Delivered-
To header is derived from the To: header in the originally sent email.
The SMTP or the POP server probably make this transformation. More
likely the SMTP server.
If you wish to study each of these headers and the format in detail, have a
look at RFC5322.
1
Why is SMTP not used for transferring emails from the recipient’s mail
server to the recipient’s user agent?
COMPLETED 0%
1 of 7
Now that we have a good idea of email, let’s move on to the directory of the
web: DNS.
DNS: Introduction
In this lesson, our focus will be on DNS, the client-server application layer
protocol that translates hostnames on the Internet to IP addresses.
At the core, the Internet operates on IP addresses, but these are difficult to
remember for humans. So, DNS names are preferably used at the application
layer for which the DNS provides a mapping to IP addresses. For example,
HTTP first translates the DNS hostname provided by the user in the URL to its
IP address and then attempts to connect to the server. Furthermore, DNS is
not just a protocol. It also consists of a distributed database of names that map
to IP addresses. So essentially it’s a directory service.
Single point of failure. If the server that has the database crashes, DNS
would stop working entirely, which is too risky.
Massive amounts of traffic. Everyone would be querying that one server.
It will not be able to handle that amount of load.
Maintenance. Maintaining the server would become critical to the
operation of DNS.
Location. Where would the server be located?
This is why DNS employs several servers, each with part of the database. Also,
the servers exist in a hierarchy. To understand this hierarchy better, you need
to understand how URLs are broken down into their hierarchies. Have a look
at the following diagram.
second-level domain
discuss.educative.io
Root Servers
Top-level
io Servers edu Servers com Servers
... domains
Second-level
educative.io portfol.io ... dartmouth.edu harvard.edu ... facebook.com amazon.com ... domains
As of the writing of this course, there are 1017 instances of root servers
operated by 12 different organizations. To get a full list and an interactive
map, have a look at root-servers.org.
Top-level Servers #
Servers in the top-level domain hold mappings to DNS servers for certain
domains. Each domain is meant to be used by specific organizations only.
Here are some common domains:
com: This TLD was initially meant for commercial organizations only -
but it has now been opened for general use.
Example: codinginterview.com
edu: Used by educational institutions.
Example: stanford.edu
gov: Only used by the U.S. government.
Example: nasa.gov
mil: Used by U.S. military organizations.
Example: army.mil
net: It was initially intended for use by organizations working in network
technology such as ISPs, but it is now a general purpose domain like com.
Example: doubleclick.net
org: This domain was intended for non-profit organizations but has been
opened for general use now.
mozilla.org
pk, uk, us,…: Country suffixes. 244 two-letter ones exist.
Some new and uncommon suffixes include: name, mobi, biz, pro.
International domains: 中國
Authoritative Servers #
Every organization with a public website or email server provides DNS
records. These records have hostname to IP address mappings stored for that
organization. These records can either be stored on a dedicated DNS server
for that organization or they can pay for a service provider to store the
records on their server.
This is the next link in the chain. If this server has the answer that we are
looking for, the IP address that it has is finally returned to the client. However,
this server may not have the sought after answer if the domain has a sub-
domain. In that case, this server may point to a server that has records of the
subdomain.
For instance, if the DNS record for cs.stanford.edu is being looked for, a DNS
server separate from ‘stanford.edu’ may hold records for the sub-domain ‘cs.’
A user opens up a browser to start working on the networks course that they purchased
1 of 12
Root Server
Root Server
DNS starts. The first place to look for is the local cache.
3 of 12
Root Server
Do you have
educative.io?
Assuming the IP address was not found in the local cache, the client contacts the local DNS server.
4 of 12
?
io
Root Server
If the local DNS server does not have a record for the website requested, it asks a root server
5 of 12
r
fo
?
io
r
'" ve
Root Server
'io ser
LD
"T
k
Do you know where
As
educative.io is?
A user The user's local DNS server TLD Server for 'io'
The root server points to the top-level domain server for 'io'
6 of 12
r
fo
?
io
r
'" ve
Root Server
'io ser
LD
"T
k
Do you know where
As
educative.io is?
educative.io?
A user The user's local DNS server TLD Server for 'io'
The local DNS server asks the TLD server for the authoritative name server that knows about
educative.io
7 of 12
r
fo
?
io
r
'" ve
Root Server
'io ser
LD
"T
k
educative.io is?
educative.io?
Ask NS for
Educative
A user The user's local TLD Server for 'io'
DNS server
Server for educative
The TLD server gives the address of the name server for educative
8 of 12
'
io
?
r'
io
fo
Root Server
r
ve
r
se
D
TL
Do you know where
k
As
educative.io is?
educative.io?
Ask NS for
Educative
A user The user's local TLD Server for 'io'
DNS server
ww
w.e
ucd
ati
ve
.io
?
r'
io
fo
Root Server
er
rv
se
D
TL
Do you know where
k
As
educative.io is?
educative.io?
Ask NS for
Educative
A user The user's local TLD Server for 'io'
DNS server
ww
w.e
d
Its
uc
at
ati
a.b
ve
.i
.c.
o?
d
The authoritative name server gives Educative's location to the local DNS server
10 of 12
'
io
?
r'
io
fo
Root Server
er
rv
se
D
TL
educative.io is?
educative.io?
ca
at
tiv
a.b
e.i
.c.
o?
d
Server for educative
The local DNS server returns the IP address of Educative it to the client
11 of 12
'
io
?
r'
io
fo
Root Server
r
ve
r
se
D
TL
Do you know where
k
As
educative.io is?
educative.io?
ww
w.e
d
Its
uc
at
ati
a.b
ve
.io
.c.
?
d
Educative (a.b.c.d)
Server for educative
The client can now connect to the server using other protocols like HTTP
12 of 12
Quick Quiz! #
Now that we have an idea of how DNS works, let’s look at an exercise in the
next lesson!
Exercise: Finding Name Servers
In this lesson, we will look at a few command-line tools to lookup domain name servers.
host -t ns google.com
host -t ns hostname.com
-t is the type flag. It is used to specify the type of the command. Check
out host’s manpage for a list of all the types available.
ns specifies the type. It stands for the name server in this case.
We encourage you to experiment, explore, and get creative with the tool!
Checking What Your Local DNS Server Is #
To check the IP address of your local DNS server, run the following command
on UNIX based machines. If you’re on a mobile machine, try
www.whatsmydnsserver.com. There are a lot of instructions available for
Windows machines online.
cat /etc/resolv.conf
Output #
Here is what the output may look like
You can safely ignore the first two lines since they are comments. On the third
line, nameserver shows the IP address of the local DNS server. On the last line,
search represents the default search domain that is used to resolve a query
for a domain with no suffix (for example, www.facebook).
Now that we have a good idea of how DNS works, let’s study DNS records and
messages in the next lesson.
DNS: Records and Messages
Let's now get into what DNS records and messages look like.
• Resource Records
• Format
• Types of RRs
• DNS Messages
Resource Records #
The DNS distributed database consists of entities called RRs, or Resource
Records.
Format #
RRs are 4-tuples with the following entries:
Every resource record has a type and a TTL along with a name-value pair.
The TTL specifies how long an RR entry can be cached by the client. The
remaining fields are described for each RR type below.
Types of RRs #
Address
Type A addresses are used to map IPv4 addresses to hostnames.
name is the hostname in question.
These resource records are stored in text form in special files called zone
files.
DNS Messages #
There are a few kinds of DNS messages, out of which the most common are
query and reply, and both have the same format. Study the following slides
for a detailed overview of a DNS message.
Identification Flags
Questions
(variable number of questions)
Answers
(variable number of resource records)
Authority
(variable number of resource records)
Additional Information
(variable number of resource records)
Identification Flags
12-byte
Number of questions Number of RRs Header
Questions
(variable number of questions)
Answers
(variable number of resource records)
Authority
(variable number of resource records)
Additional Information
(variable number of resource records)
Let's discuss the header first. It is 12-bytes long and contains a number of fields.
2 of 10
Questions
(variable number of questions)
Answers
(variable number of resource records)
Authority
(variable number of resource records)
Additional Information
(variable number of resource records)
Questions
(variable number of questions)
Answers
(variable number of resource records)
Authority
(variable number of resource records)
Additional Information
(variable number of resource records)
Questions
(variable number of questions)
Answers
(variable number of resource records)
Authority
(variable number of resource records)
Additional Information
(variable number of resource records)
Number-of fields. These indicate the number of instances of the 4 data sections that follow.
5 of 10
Answers
(variable number of resource records)
Authority
(variable number of resource records)
Additional Information
(variable number of resource records)
Authority
(variable number of resource records)
Additional Information
(variable number of resource records)
Additional Information
(variable number of resource records)
The Answers field.
8 of 10
Additional Information
(variable number of resource records)
There are also zone transfer request and response. But, those are not used
by common clients. Backup or secondary DNS servers use them for zone
transfers, which are when zone files are copied from one server to another.
This takes place over TCP.
1
Which of the following are valid DNS record entry types?
COMPLETED 0%
1 of 3
In the next lesson, we’ll use command-line tools to look at DNS response
messages and resource records!
Exercise: Looking At DNS Response Messages and
Resource Records
In this lesson, we'll use command-line tools to look at DNS response messages and resource records!
• Revisiting Nslookup
• Output
• Looking At Real DNS Response Messages With dig
Revisiting Nslookup #
nslookup -type=A educative.io
nslookup is a versatile tool for DNS lookups. The type flag determines the
type of RR that you want to look into!
Output #
nslookup can be used to look at DNS records. In this example, we looked up
educative.io .
Server: 169.254.169.254
Address: 169.254.169.254#53
Non-authoritative answer:
Name: educative.io
Address: 104.20.7.183
Name: educative.io
Address: 104.20.6.183
The first two lines are the IP address of the local DNS server which is
169.254.169.254 in our case.
The last few lines return the type A RR that maps educative.io to the IP
address 104.20.6.183 . It says ‘non-authoritative’ because the answer is
coming from a local DNS server’s cache, and not from Educative’s
authoritative DNS server.
If you’re wondering what TTL values look like, run the following command.
The value in the TTL field is in seconds, so a value of 279 is 4 minutes and 39
seconds.
dig educative.io
dig is a command-line tool used to query DNS servers. dig stands for domain
information groper, and it displays the actual messages that were received
from DNS servers. You can decipher the output for yourself now that you
know what a DNS message looks like.
As always, we encourage you to read the dig manpage and explore the
command for yourself!
BitTorrent is a key protocol and has millions of users simultaneously and actively sharing and downloading
hundreds of thousands of les of all types: music, movies, books, and so on.
• Overview of BitTorrent
• How It Works
• Trackers and torrent les
• A Simpli ed BitTorrent Session
• Swarming
• Tit-for-tat
The file is first supplied to a peer in pieces called chunks, and then they
also distribute the file to other peers.
How It Works #
Trackers and torrent les #
How do clients find peers to connect to? Well, clients connect to a special
tracker node first. The tracker responds with the IP and the port of a few other
peers who are downloading the same file.
So clients can find peers through trackers. But how do clients find the tracker
in the first place? Clients begin by downloading a ‘torrent file’ from a web
server which has the URL of the tracker. The torrent file also contains a SHA1
hash of each file chunk. Can you guess why?
Swarming #
Study the following animation to get an idea of how a file is distributed in
chunks. Note that it starts with one peer that has the file in its entirety and
then the peers start distributing the file to each other.
The distinct chunks of the file are represented by different colors in the
diagram above, as are the bits being transferred. The initial blank gray boxes
represent that none of the machines has any bit of the file. Eventually, all the
machines have the entire file.
So what this protocol needs really is to give peers the incentive to upload. That
is where the tit-for-tat scheme comes in.
Tit-for-tat #
Every ten seconds, a peer in the network will calculate which four peers
are supplying data at the highest rate to it. It will then supply data to
them in return. These 4 peers are said to be unchoked in the sense that
they are now receiving data in return.
📝 Note: This list of top four peers may change every 10 seconds.
A peer in the network will randomly pick another peer every thirty
seconds and supply data to them. The best-case scenario would be that
the peer becomes one of the randomly picked peer’s top 4 suppliers.
Naturally, that random peer would start supplying data in return. Then if
the randomly picked peer is sending data at a fast enough rate, it may
also become part of the peer’s top four suppliers. In other words, two
peers partnered randomly will continue working with each other if they
are satisfied with the trading. This randomly picked partner is said to be
optimistically unchoked.
The result of this scheme is that everyone has an incentive to upload. The
scheme is an instance of an old successful idea that stems from Axelrod’s
tournament.
Quiz on BitTorrent
1
What category does BitTorrent fall in?
COMPLETED 0%
1 of 2
That concludes our study of application layer protocols! Let’s move on to the
next layer at last.
What Is the Transport Layer?
We nished the application layer, and now we'll study the transport layer.
Application
Transport you
are
Network
here
Data Link
Physical
Host 1 Host 2
suppose two end-systems are communicating with each other on Skype via the internet
1 of 4
Host 1 Host 2
Host 1 Host 2
the network layer will handle delivering the message to the host
3 of 4
Host 2
Host 1
however, delivering the message to the relevant application on the other end system is handled by
the transport layer
4 of 4
Segments data. The transport layer also divides the data into
manageable pieces called ‘segments’ or ‘datagrams.’
Multiplexes & demultiplexes data. It ensures that the data reaches the
relevant application within an end-system. So if multiple packets get sent
to one host, each will end up at the correct application.
Where It Exists #
The transport layer does not have anything to do with the core of the
network. Its only responsibility is to take messages from an application
on a machine and hand them off to the network layer. The network layer
transfers messages from one host to another.
The transport layer also receives messages from the network layer and
transports them to the correct application.
TCP UDP
Delivers messages that we call Does not ensure in-order
‘segments’ reliably and in order. delivery of messages that we call
‘datagrams.’
Detects any modifications that
may have been introduced in the Detects any modifications that
packets during delivery and may have been introduced in the
corrects them. packets during delivery but does
not correct them by default.
Handles the volumes of traffic at
one time within the network Does not ensure reliable
core by sending only an delivery.
appropriate amount of data at
Generally faster than TCP
one time.
because of the reduced overhead
Examples of of ensuring uncorrupted
applications/application delivery of packets in order.
protocols that use TCP are: HTTP,
Applications that use UDP
E-mail, File Transfers.
include: Domain Name System
(DNS), live video streaming, and
Voice over IP (VoIP).
Quick Quiz! #
1
The transport layer in the OSI reference model uses the services of ______
layer.
COMPLETED 0%
1 of 2
In the next lesson, we’ll have a more in-depth look at multiplexing and
demultiplexing!
Multiplexing and Demultiplexing
Let's discuss how the transport layer handles so many simultaneous connections over one network!
So how does the end-system know which process to deliver packets to? Well,
that’s where the transport layer’s demultiplexing comes in.
What is Demultiplexing? #
Demultiplexing is the process of delivering the correct packets to the correct
applications from one stream.
Transport
Network
Link
Physical
Data
as bits
End systems run many programs at once which leaves us with the question: what process to
deliver which packet to?
What is Multiplexing? #
Also, multiplexing allows messages to be sent to more than one destination
host via a single medium.
Transport
Network
Link
Physical
Data as
bits
End systems can be talking to many other applications at once which leaves us with the question:
how to send messages such that they arrive to the correct process?
Application 1
Application 2
Application 3
Transport Layer
. Multiplexing
.
.
Application n
Multiplexing
1 of 5
Application 1
Application 2
Application 3
Transport Layer
. Multiplexing
.
.
Application n
Multiplexing
2 of 5
Application 1
Application 2
Application 3
Transport Layer Transport Layer
. Multiplexing Demultiplexing
.
.
Application n
Demultiplexing
3 of 5
Application 1
Application 2
Application 3
Transport Layer Transport Layer
. Multiplexing Demultiplexing
.
.
Application n
Demultiplexing
4 of 5
Application 1
Application 1
Application 2
Application 2
Application 3
Application 3
Transport Layer Transport Layer
. Multiplexing Demultiplexing
.
.
.
.
.
Application n
Application n
Demultiplexing
5 of 5
Port numbers are 16-bit long and range from 0 and 65,535.
The first 1023 ports are reserved for certain applications and are called
well-known ports. For example, port 80 is reserved for HTTP.
The transport layer labels packets with the port number of the application a
message is from and the one it is addressed to. This is what allows the layer to
multiplex and demultiplex data.
Quick Quiz! #
Connectionless refers to multiplexing and demultiplexing with UDP. Let's dive right in.
• Ports
• Multiplexing & Demultiplexing in UDP
• On Port Assignment in UDP
• Quick Quiz!
Ports #
Here’s a quick refresher on what ports are because that needs to be crystal
clear in order for you to understand multiplexing and demultiplexing.
Socket
Out of these, the port numbers 0 − 1023 are well-known and are
reserved for certain standard protocols. Port 80, for instance, is reserved
for HTTP whereas port 22 is reserved for SSH.
Refer to page 16 of RFC 1700 for more details regarding what port
number is assigned to what protocol.
Multiplexing & Demultiplexing in UDP #
When a datagram is sent out from an application, the port number of the
associated source and destination application is appended to it in the
UDP protocol header.
If the source port and source IP address of two datagrams are different
but the destination port and IP address are the same, the datagrams will
still get sent to the same application.
Here are some slides to give you a quick overview of what happens.
Src Application
1 of 6
An application produces data
2 of 6
It writes the data out to its socket and it turns into a datagram
src port
Src App Data
socket
3 of 6
It writes data out to its socket and turns it into a datagram
src port
Src App Data src
socket
Datagram
4 of 6
src port
Src App Data src
socket
5 of 6
The data is then received by the application
src port
Src App Data src
socket
6 of 6
Quick Quiz! #
1
Ports 0 - 1023 are termed as ephemeral ports.
COMPLETED 0%
1 of 3
Let’s look at the principles of reliable data transfer in the next lesson. This is
key to building a good foundation for later lesson on TCP!
Introduction to Congestion Control
What Is Congestion?
When more packets than the network has bandwidth
for are sent through, some of them start getting
dropped and others get delayed. This phenomenon
leads to an overall drop in performance and is called
Traffic congestion at
congestion.
Times Square, NYC.
Congestion control algorithms are based on these general ideas and are built
into transport layer protocols like TCP. Let’s also look at a few principles of
bandwidth allocation before moving on.
Not all hosts are created equal; Some can send and receive at a higher data
rate than others. Furthermore, if the bottleneck bandwidth was allocated
equally to all hosts, some of them wouldn’t be able to use the bandwidth to its
full capacity and some wouldn’t have enough. For example, if an Internet-
enabled doorbell and a busy server had the same bandwidth, the doorbell
would have too much and the server would likely not have enough.
Suppose 4 end-systems are to use a link with a bandwidth of 200 Mbps. It may
seem that in order to make the most efficient use of this link, the bandwidth
should be divided equally i.e., 200
4
= 50 Mbps should be allocated to each
host. However, in a real setting, each end-system would be able to use less
than the anticipated 50 while avoiding congestion. Why? Because real traffic
is transmitted in bursts and not in one continuous stream. Have a look at the
following plot of a Wireshark traffic capture for a clearer picture.
Simultaneous bursts of traffic from all end-systems can cause more than the
allocated bandwidth to be used which results in congestion and a consequent
drop in performance.
Transmission Threshold #
The following graph shows that the end-to-end delay in the delivery of the
packets increases exponentially when the packet transmission rate increases
beyond a certain threshold. Furthermore, the delay can never be infinite, so
the packets are simply dropped instead after a certain point.
Onset of
congestion
Capacity
Delay (seconds)
have not yet reached the destination. So, much of the network’s
bandwidth ends up being consumed by a small number of packets.
To sum up, congestion occurs before the maximum capacity of the network is
reached and congestion collapse occurs as it’s approached.
Note that after a certain threshold, increase in transmission rate will cause a
very high increase in delay decreasing the overall power.
Quick Quiz! #
1
If the applications increase sending rate, the throughput ______ at first,
then ______.
COMPLETED 0% 1 of 3
We’ve now learned the first principle of congestion control: the entire
bandwidth of a network should not be allocated. Let’s continue with the next
two in the next lesson!
More on Principles of Congestion Control
Let's look at a couple of more key principles that congestion control algorithms adhere to!
• Max-min Fairness
• Convergence
• Quick Quiz!
Max-min Fairness #
Furthermore, the congestion control scheme should be fair. Most congestion
schemes aim at achieving max-min fairness. An allocation of transmission
rates to sources is said to be max-min fair if:
To visualize the different rate allocations, it’s useful to consider the graph
shown below. Consider hosts A and B that share a bottleneck link.
Host A rate
Region
Fairness Line
Host B rate
2 Mbps
Efficiency Line
In this graph, we plot the rate allocated to host B on the x-axis and we plot
the rate allocated to host A on the y-axis . A point in the graph (rB , rA )
corresponds to a possible allocation of the transmission rates. Since there is a
2 Mbps bottleneck link in this network, the graph can be divided into
regions:
1. The white-colored part of the graph contains all allocations (rB , rA ) such
that the bottleneck link is not congested (rA + rB < 2).
2. The right border of this region is the efficiency line or the set of
allocations that completely utilize the bottleneck link (rA + rB = 2).
3. Finally, the fairness line is the set of fair allocations.
Depending on the network, a max-min fair allocation may not always exist. In
practice, max-min fairness is an ideal objective that cannot necessarily be
achieved. When there is a single bottleneck link as in the example above,
max-min fairness implies that each source should be allocated the same
transmission rate.
Convergence #
Additionally, bandwidth should be allocated such that it converges to a fair (a
host does not hog all of it), and efficient value. This means it should not
oscillate and most of it will be used. Furthermore, it should also change in
response to changes in the demands of the network over time. Here are some
slides that demonstrate this convergence.
100 Flow #1
Bandwidth Allocated (percentage)
Flow #2
Flow #3
50
0
0 2 4 6 8 10 12
Time (sec)
Flow #1 starts and continues for 2 seconds. Flow #1 is using all of the bandwidth.
1 of 7
100 Flow #1
Bandwidth Allocated (percentage)
Flow #2
Flow #3
50
0
0 2 4 6 8 10 12
Time (sec)
100 Flow #1
Bandwidth Allocated (percentage)
Flow #2
Flow #3
50
0
0 2 4 6 8 10 12
Time (sec)
Flow #1 rapidly converges and drops its bandwidth usage to half of the available bandwidth. Both
flows use 50% of the available bandwidth and continue for another 4 seconds or so.
3 of 7
100 Flow #1
Bandwidth Allocated (percentage)
Flow #2
Flow #3
50
0
0 2 4 6 8 10 12
Time (sec)
100 Flow #1
Bandwidth Allocated (percentage)
Flow #2
Flow #3
50
0
0 2 4 6 8 10 12
Time (sec)
Flow #2
Flow #3
50
0
0 2 4 6 8 10 12
Time (sec)
100 Flow #1
Flow #2
Bandwidth Allocated (percentage)
Flow #3
50
0
0 2 4 6 8 10 12
Time (sec)
At the 10-second mark, all flow #1 drops out, so flows #2 and #3 converge back to half the
available bandwidth
7 of 7
📝 Note These values (for example, ‘half’ the bandwidth) are just for
demonstration purposes. In real life, as discussed previously, all of the
available bandwidth can never be used. Furthermore, bandwidth
Quick Quiz! #
1
In the context of congestion control, an efficient rate allocation is
desirable because ______.
COMPLETED 0%
1 of 3
Let’s look at some workarounds for these problems that the transport layer
employs.
Checksums #
The first imperfection of the network layer is that segments may be
corrupted by transmission errors. The simplest error detection scheme is
the checksum.
and can choose what to do in case it is not valid. Quite often, the segments
received with an invalid checksum are discarded.
Data
checksum
Retransmission Timers #
The second imperfection of the network layer is that segments may be lost.
Since the receiver sends an acknowledgment segment after having received
each data segment, the simplest solution to deal with losses is to use a
retransmission timer.
A retransmission timer starts when the sender sends a segment. The value of
this retransmission timer should be greater than the round-trip-time, for
example, the delay between the transmission of a data segment and the
reception of the corresponding acknowledgment. Note that TCP sends an
acknowledgment for almost every segment! We’ll look at this in more detail in
later lessons. When the retransmission timer expires, the sender assumes that
the data segment has been lost and retransmits it.
send segment 1
send segment 1
Start Timer Segment 1
send segment 1
Start Timer Segment 1
send ACK of
segment 1
ACK of Segment 1
send segment 1
Start Timer Segment 1
send ACK of
segment 1
ACK of Segment 1
Cancel Timer
send segment 2
Start Timer
x
segment 2
lost
send segment 1
Start Timer Segment 1
send ACK of
segment 1
ACK of Segment 1
Cancel Timer
send segment 2
Start Timer
x
segment 2
lost
Timer expires
send segment 1
Start Timer Segment 1
send ACK of
segment 1
ACK of Segment 1
Cancel Timer
send segment 2
Start Timer
x
segment 2
lost
Timer expires
retransmit segment 2
Segment 2
send segment 1
Start Timer Segment 1
send ACK of
segment 1
ACK of Segment 1
Cancel Timer
send segment 2
Start Timer
x
segment 2
lost
Timer expires
retransmit segment 2
Segment 2
send ACK of
segment 2
ACK of Segment 2
send segment 1
send segment 1
Start Timer Segment 1
send segment 1
Start Timer Segment 1
send ACK of
segment 1
ACK of Segment 1
send segment 1
Start Timer Segment 1
send ACK of
segment 1
ACK of Segment 1
Cancel Timer
send segment 1
Start Timer Segment 1
send ACK of
segment 1
ACK of Segment 1
Cancel Timer
send segment 2
Start Timer
Segment 2
send segment 1
Start Timer Segment 1
send ACK of
segment 1
ACK of Segment 1
Cancel Timer
send segment 2
Start Timer
Segment 2
send ACK of
segment 2
ACK of Segment 2
x
ACK of
segment 2 lost
send segment 1
Start Timer Segment 1
send ACK of
segment 1
ACK of Segment 1
Cancel Timer
send segment 2
Start Timer
Segment 2
send ACK of
segment 2
ACK of Segment 2
x
ACK of
segment 2 lost
Timer Expires
Segment 2 Duplicate
segment 2
received!!
Sequence Numbers #
To identify duplicates, transport protocols associate an identification number
with each segment called the sequence number. This sequence number is
prepended to the segments and sent. This way, the end entity can identify
duplicates.
Quick Quiz! #
1
Checksums address which imperfection of the network layer?
COMPLETED 0%
1 of 5
Let’s continue our discussion of reliable transfer data in the next lesson!
Reliable Data Transfer: Sliding Window
• Pipelining
• Sliding Window
• Quick Quiz!
Pipelining #
Applications may generate data at a rate much higher than the network can
transport it. Processor speed is generally much higher than the speed of
writing out and reading data to/from the network (I/O).
1. The processor emits the message to be sent, the network carries the
message to the destination.
2. The receiver processor receives and emits an acknowledgment message.
3. The network carries the acknowledgment to the sender. So, instead of
waiting for an acknowledgment of every packet before transmitting the
next one, it’s more efficient to pipeline the multi-step process. In other
words, instead of waiting for the acknowledgment of a message before
transmitting the next one, the sender keeps transmitting messages
without waiting for an acknowledgment. This makes more efficient use of
the processor’s time.
Sliding Window #
The sliding window is the set of consecutive sequence numbers that the
sender can use when transmitting segments without being forced to wait for
an acknowledgment. At the beginning of a session, the sender and receiver
agree on a sliding window size.
The figure below illustrates the operation of the sliding window. The sliding
window shown contains three segments. The sender can thus transmit three
segments before being forced to wait for an acknowledgment. The sliding
window moves to the higher sequence numbers upon reception of
acknowledgments. When the first acknowledgment (of segment 0) is received,
it allows the sender to move its sliding window to the right, and sequence
number 3 becomes available.
0 1 2 3
A B
Unsent
Unacknowledged
Acknowledged
0 1 2 3
A B
0 Unsent
Unacknowledged
Acknowledged
0 1 2 3
A B
0 Unsent
1 Unacknowledged
Acknowledged
0 1 2 3
A B
0 Unsent
1 Unacknowledged
2 Acknowledged
0 1 2 3
A B
0 Unsent
1 Unacknowledged
2 Acknowledged
0
0 1 2 3
A B
0 Unsent
1 Unacknowledged
2 Acknowledged
0
0 1 2 3
A B
0 Unsent
1 Unacknowledged
2 Acknowledged
0
0 1 2 3
A B
0 Unsent
1 Unacknowledged
2 Acknowledged
0
0 1 2 3
A B
0 Unsent
1 Unacknowledged
2 Acknowledged
0
0 1 2 3
A B
0 Unsent
1 Unacknowledged
2 Acknowledged
0
0 1 2 3
A B
0 Unsent
1 Unacknowledged
2 Acknowledged
0
COMPLETED 0%
1 of 2
In this lesson, we'll study go-back-n: a simple protocol to ensure detection and retransmission of lost packets.
• Go-back-n
• Go-back-n Receiver
• Cumulative Acknowledgements
• Go-back-n Sender
• Retransmission Timer
• Advantages of Go-back-n
• Selective Repeat
• Comparing to go-back-n
• Quick Quiz!
In the last lesson, we discovered that a sending sliding window alone is not
enough to ensure detection and retransmission of lost packets. In order to
do that, we will look at two protocols:
1. Go-back-n
2. Selective Repeat
Go-back-n #
The simplest sliding window protocol uses go-back-n recovery.
Go-back-n Receiver #
Intuitively, go-back-n receiver operates as follows:
has received.
Cumulative Acknowledgements #
1
2
1
2
3
1
2
3
1
2
3
1
2
3
1
ACKs of 1 2
and 2 lost x
3
1
2
3
1
ACKs of 1 2
and 2 lost x
3
1
2
3
1
ACKs of 1 2
and 2 lost x
3
The segments are sent with a sending sliding window that we looked at in
the last lesson.
The sender must wait for an acknowledgment once its sending buffer is
full.
When a go-back-n sender receives an acknowledgment, it removes all the
acknowledged segments from the sending buffer.
Retransmission Timer #
0 1 2 3
A B
Unsent
Unacknowledged
Acknowledged
Go-back-n: example
1 of 7
Sending Window
0 1 2 3
A B
Retransmission
Timer starts
0
1
Unsent
2
Unacknowledged
Acknowledged
Go-back-n: example
2 of 7
Sending Window
0 1 2 3
A B
Retransmission
Timer starts
0
1 Segment 1
x is lost Unsent
2 Unacknowledged
Acknowledged
Go-back-n: example
3 of 7
Sending Window
0 1 2 3
A B
Retransmission
Timer starts
0
1 Segment 1
x is lost Unsent
2 Unacknowledged
x Acknowledged
Go-back-n: example
4 of 7
Sending Window
0 1 2 3
A B
Retransmission
Timer starts
0
1 Segment 1
x is lost Unsent
2 Unacknowledged
0
x Acknowledged
Go-back-n: example
5 of 7
Sending Window
0 1 2 3
A B
Retransmission
Timer starts
1 Unsent
2
x Unacknowledged
Segment with
sequence #2 is lost
Acknowledged
0
Timer expires
Go-back-n: example
6 of 7
Sending Window
0 1 2 3
A B
Retransmission
Timer starts
1 Unsent
2
x Unacknowledged
Segment with
sequence #2 is lost
Acknowledged
0
Timer expires
1
2
Go-back-n: example
7 of 7
Advantages of Go-back-n #
The main advantage of go-back-n is that it can be easily implemented, and it
can also provide good performance when only a few segments are lost. But
when there are many losses, the performance of go-back-n quickly drops for
two reasons:
The go-back-n receiver does not accept out-of-sequence segments.
Since the go-back-n protocol does not accept out of order segments, it can
waste a lot of bandwidth if segments are frequently lost.
Selective Repeat #
Uses a sliding window protocol just like go-back-n.
The window size should be less than or equal to half the sequence
numbers available. This avoids packets being identified incorrectly.
Here’s an example: suppose the window size is greater than half the
buffer size.
The receiver stores correct packets until they can be delivered in order to
the upper application layer.
Quick Quiz! #
1
A cumulative acknowledgement for sequence number n acknowledges
the receipt of all sequences numbers upto and including ______.
COMPLETED 0%
1 of 5
In the next lesson, we’ll start with UDP, a transport layer protocol.
The User Datagram Protocol
This lesson gives an introduction to one of the protocols at the heart of the transport layer: UDP!
• What is UDP?
• How It Works
• Structure of A UDP Datagram
• Header
• Data
• Quick Quiz!
What is UDP? #
UDP, or User Datagram Protocol, is a transport layer protocol that works
over the network layer’s famous Internet protocol (which we’ll look at in-
depth in the next chapter). RFC 768 is the official RFC for UDP.
How It Works #
UDP does not involve any initial handshaking like TCP does, and is hence
called a connectionless protocol. This means that there are no established
‘connections’ between hosts.
UDP prepends the source and destination ports to messages from the
application layer and hands them off to the network layer. The Internet
Protocol of the network layer is a best-effort attempt to deliver the message.
This means that the message-
Data #
Other than the headers, a UDP datagram contains a body of data which can be
up to 65,527 bytes long. Since the maximum possible length of a UDP
datagram is 65,535 bytes which includes the 8-byte header, we are left with
65,527 bytes available. The nature of the data depends on the overlying
application. So if the application is querying a DNS server, it would contain
bytes of a zone file.
Destination Port
Source Port Number
Number
(2 bytes)
(2 bytes)
Length Checksum
(2 bytes) (2 bytes)
Data
A UDP Datagram. The header is a total of 8 bytes or 64 bits long.
Quick Quiz! #
1
An application layer hands down 10 bytes to be sent in a UDP datagram.
The value in the length field in the datagram will be ______.
COMPLETED 0%
1 of 4
Let’s go on to look at how UDP does error detection, why UDP is used, and
what well-known protocols are built on top of it in the next lesson!
UDP Checksum Calculation & Why UDP?
Let's look at how the UDP checksum is calculated in-depth, why UDP would ever get used, and applications that
use UDP.
• Checksum Calculation
• What if the checksum eld gets corrupted?
• Why UDP?
• Well-Known Applications That Use UDP
• Xbox Live
• Name Translation
• Network Management
• Quick Quiz!
UDP detects if any changes were introduced into a message while it traveled
over the network. To do so, it appends a ‘checksum’ to the packet as a field
that can be checked against the message itself to see if it was corrupted. It’s
calculated the same way as in TCP. Here’s a refresher with some extra
information:
Checksum Calculation #
1. The payload and some of the headers (including some IP headers) are all
divided into 16-bit words.
2. These words are then added together, wrapping any overflow around.
3. Lastly, the one’s complement of the resultant sum is taken and appended
to the message as the checksum.
📝 Note Also, note that if a message cannot be perfectly divided into 16-
bit chunks, then the last word is padded to the right with zeros. This is
only for checksum calculation though! The actual message does not have
these zeros.
1110011001100110
0101010101010101
1100010000100010
1110011001100110
+0101010101010101
We will start off by adding the first 2
2 of 24
1110011001100110
+0101010101010101
1
0+1=1
3 of 24
1110011001100110
+0101010101010101
11
1+0=1
4 of 24
1
1110011001100110
+0101010101010101
011
1
1110011001100110
+0101010101010101
1011
0+1=1
7 of 24
1
1110011001100110
+0101010101010101
111011
1+0=1
8 of 24
1 1
1110011001100110
+0101010101010101
0111011
1 + 1 = 10 where 1 is carried
9 of 24
1 1
1110011001100110
+0101010101010101
110111011
0+1=1
10 of 24
1 1
1110011001100110
+0101010101010101
1110111011
1+0=1
11 of 24
1 1 1
1110011001100110
+0101010101010101
01110111011
1+0=1
12 of 24
1 1 1
1110011001100110
+0101010101010101
101110111011
1+0+0=1
13 of 24
1 1 1
1110011001100110
+0101010101010101
1101110111011
0+1=1
14 of 24
1 1 1
1110011001100110
+0101010101010101
11101110111011
1+0=1
15 of 24
1 1 1 1
1110011001100110
+0101010101010101
10011101110111011
We bring the '1' to the right and add it to the rest of the word!
17 of 24
1 1 1 1
1110011001100110
+0101010101010101
0011101110111011
1
We bring the '1' to the right and add it to the rest of the word!
18 of 24
1 1 1 1
1110011001100110
+0101010101010101
0011101110111011
+ 1
We bring the '1' to the right and add it to the rest of the word!
19 of 24
1 1 1 1
1110011001100110
+0101010101010101
0011101110111011
+ 1
0011101110111100
0011101110111100
+1100010000100010
01111111111011110 Final sum
10000000000100001
Why UDP? #
You might be wondering why would anyone use UDP when it has so many
apparent drawbacks and doesn’t really do anything? Well, there are actually a
number of reasons why UDP would be a good choice for certain applications.
1. UDP can be faster. Some applications cannot tolerate the load of the
retransmission mechanism of TCP, the other transport layer protocol.
2. Reliability can be built on top of UDP. TCP ensures that every message is
sent by resending it if necessary. However, this reliability can be built in
the application itself.
3. UDP gives finer control over what message is sent and when it is sent.
This can allow the application developer to decide what messages are
important and which do not need concrete reliability.
5. With the significantly smaller header gives UDP an edge over TCP in
terms of reduced transmission overhead and quicker transmission times.
Well-Known Applications That Use UDP #
Xbox Live #
Xbox live is built on UDP.
Name Translation #
Yes, DNS uses UDP! In the case of failed message delivery, DNS either:
Using UDP instead of TCP makes DNS and consequently, web browsing
significantly faster.
Network Management #
Network management and network monitoring is done using a protocol called
Simple Network Management Protocol and it runs on UDP as well.
Quick Quiz! #
Let’s look at some actual live UDP packets in the next lesson with TCPDUMP!
Exercise: Capturing UDP Packets
We'll now look at a command-line tool that allows us to capture UDP packets.
• What is tcpdump ?
• Sample Output
• Counting Packets with -c
• Printing PCAP Files With -r
• Looking at Real UDP Packet Headers
• Try it Yourself!
What is tcpdump ? #
tcpdump is a command-line tool that can be used to view packets being sent
and received on a computer. The simplest way to run it is to simply type the
following command into a terminal and hit enter. You can try this on the
terminal provided at the end of this lesson!
tcpdump
Packets will start getting printed rapidly to give a comprehensive view of the
traffic.
Sample Output #
However, some might not find it to be very helpful because it does not allow
for a more zoomed-in and fine-grained dissection of the packets, which is
the main purpose of tcpdump (it’s technically a packet analyzer). So you might
want to consider using some flags to filter relevant packets out.
... what??
tcpdump -w filename.ext
The file output.pcap will have all the packets saved to it. Try running this
command in the terminal below. Note that the process does not exit without a
keyboard interrupt. The next flag will help us stop packet capture in a
predetermined fashion.
📝 Note .pcap files are used to store the packet data of a network.
Packet analysis programs such as Wireshark (think of it like tcpdump
with a GUI) export and import packet captures in pcap files.
We’ve gotten pretty far with this. There are plenty of other flags and
arguments you could give to tcpdump to make it capture packets precisely as
per your requirements.
Note that the code may time out before it actually captures a packet. We
would suggest running this one on the terminal.
tcpdump udp -X -c 1 # Capturing 1 UDP packet
The -X flag just prints the payload of the packet (the data) in both hex and
ASCII.
The format of the next line is like so 'IP address of sender > IP address of receiver'. Notice that the
IP addresses have been resolved into hostnames. tcpdump does this by default. If you wish to see
the actual IP address, pass in the '-n' flag. Also notice the time stamp.
3 of 11
Time stamp
} Some tcpdump output hostname
of the packet was resolve
address. tc
IP address this by
of receiver
Time stamp
} Some tcpdump output
hostname o
of the packet It was reso
an IP ad
IP address tcpdump d
of receiver by def
Time stamp
} Some tcpdump output
hostname o
of the packet
was resolv
IP address
IP address
does this
of receiver
10 word IP
Header
The first 160 bits are the IP header. Note that a single hex digit is exactly 4 bits so that means the
header is of 160/4 = 40 hex digits or 40/4 = 10 blocks. We can safely ignore it for now!
6 of 11
Time stamp
} Some tcpdump output
hostname o
of the packet was resolv
IP address
IP address
does this
of receiver
4 block
UDP
The message in Hex
Header
The UDP header is of 64 bits 4 blocks. Each block represents one UDP field.
7 of 11
Time stamp
} Some tcpdump output
hostname
of the packet It was reso
an IP ad
IP address tcpdump do
of receiver defa
4 block
The source and destination ports in UDP
The message in Hex Header
hex. These ports are '123' in decimal.
This is an example of the source and
destination both using well known
port numbers.
The UDP header is of 64 bits i.e., 4 blocks. Each block represents one UDP field. The first two fields
are the source and destination ports which are both 007b or port numbers 123 in decimal.
8 of 11
Time stamp
} Some tcpdump output hostname
of the packet It was reso
an IP ad
IP address tcpdump do
of receiver defa
4 block
These first two hex blocks UDP
The message in Hex
represent source and Header
destination ports. These
The message is part of ports are '123' in decimal.
the NTP protocol as
can be inferred from
the UDP header as well
Note that port 123 is reserved for the NTP protocol (which runs on UDP) as shown by the output
here.
9 of 11
Time stamp
} Some tcpdump output
of the packet hostname
resolved fro
IP address tcpdump do
of receiver
4 block
UDP The length of the
The message in Hex These first two hex blocks
Header message is '0038' or
represent source and
56 in decimal
destination ports. These
The message is part of whereas the
ports are '123' in decimal.
the NTP protocol as checksum is 5ef7
can be inferred from
the UDP header as well
The next two fields are the length and the checksum!
10 of 11
Time stamp
} Some tcpdump output
hostname o
of the packet
was resolv
IP address
IP address
does this
of receiver
4 block
UDP The length of the
The message in Hex These first two hex blocks
Header message is '0038' or
represent source and
56 in decimal
destination ports. These
The message is part of whereas the
ports are '123' in decimal.
the NTP protocol as checksum is 5ef7
can be inferred from
the UDP header as well
That concludes our inspection of a UDP packet. Explore this more! Try capturing a packet on the
command line below and try dissecting it!
11 of 11
Try it Yourself! #
You can try all the commands in this terminal. Click here to go back
Terminal
In the next lesson, we’ll learn about the transmission control protocol!
The Transmission Control Protocol
In this lesson, we'll look at a quick overview of TCP and some famous applications that use it.
• Introduction to TCP
• An Analogy: Talking on a Cell Phone
• What TCP Does
• Well-Known Applications That Use TCP
• File Transfer
• Secure Shell SSH
• Email
• Web Browsing
• Quick Quiz!
Introduction to TCP #
TCP, or the transmission control protocol, is one of the two key protocols of
the transport layer. TCP is what makes most modern applications as enjoyable
and reliable as they are. HTTP’s implementation, for example, would be very
complex, if it weren’t for TCP.
TCP, being THE connection-oriented transport layer protocol for the Internet,
has mechanisms to solves these problems.
3. End to end flow control. Flow control means not overwhelming the
receiver. It’s not the same as congestion control. Congestion control tries
not to choke the network. However, if the receiving machine is slow, it
might drown in data even if the network is not choked. Avoiding
drowning the receiver in data is end to end flow control. There is also
hop by hop flow control, which is done at the data link layer.
Application Layer
Application Layer
Data
Application Layer
Data
Socket + Port
Application Layer
Data
Socket + Port
Transport Layer
Application Layer
Data
Socket + Port
Transport Layer
Email #
All email protocols, SMTP, IMAP, and POP use TCP to ensure complete and
reliable message delivery similar to the reasons that FTP uses TCP.
Web Browsing #
Web browsing on both HTTP and HTTPS is done on TCP as well for the same
reasons as FTP.
Quick Quiz! #
COMPLETED 0%
1 of 4
Now that we have an overview of what TCP is and what it does, let’s look at
some of its key properties in the next lesson!
Key Features of the Transmission Control Protocol
Here are some key properties of TCP. These are important to know to understand the design of the protocol.
• Connection Oriented
• Full Duplex
• Point-to-point Transmission
Connection Oriented #
TCP itself is connection-oriented and creates a long term connection between
hosts. The connection remains until a certain termination procedure is
followed.
Full Duplex #
Furthermore, TCP is full-duplex, which means that both hosts on a TCP
connection can send messages to each other simultaneously.
A B
A B
A B
A B
A B
A B
Also, both hosts can send each other messages simultaneuously because TCP is full duplex!
8 of 12
A B
Also, both hosts can send each other messages simultaneuously because TCP is full duplex!
9 of 12
A B
Also, both hosts can send each other messages simultaneuously because TCP is full duplex!
10 of 12
A B
Also, both hosts can send each other messages simultaneuously because TCP is full duplex!
11 of 12
A B
Also, both hosts can send each other messages simultaneuously because TCP is full duplex!
12 of 12
Point-to-point Transmission #
TCP connections have exactly two endpoints! This means that broadcasting
or multicasting is not possible with TCP.
Error Control
TCP can detect errors in segments and make
corrections to them.
Flow Control
TCP on the sending side controls the amount of data
being sent at once based on the receiver’s specified
capacity to accept and process it. The sender adjusts
the sending rate accordingly.
Now we understand what TCP is and its general design principles. Let’s study
TCP headers in the next lesson!
TCP Segment Header
We'll now study TCP headers. They're are far more complex than UDP headers and really are what allow for TCP
to work properly!
• Introduction
• Source and Destination Ports
• Sequence Number
• Acknowledgement Number
• Example
• Header Length
• Reserved Field
• Quick Quiz!
Introduction #
TCP headers play a crucial role in the implementation of the protocol. In fact,
TCP segments without actual data and with headers are completely valid.
They’re actually used quite often!
The size of the headers range from 20 - 60 bytes. Let’s discuss the header field
by field.
Header
Reserved 8 flags (8 Window Size
Length
(4 bits) bits) (2 bytes)
(4 bits)
The source and destination ports are the first fields of the TCP header.
The source and destination port numbers are self-explanatory. They are
exactly like the source and destination ports in UDP. Just for a refresher
though, the source port is the port of the socket of the application that is
sending the segment and the destination port is the port of the socket of the
receiving application. The size of each field is two bytes.
Sequence Number #
Header
Reserved 8 flags (8 Window Size
Length
(4 bits) bits) (2 bytes)
(4 bits)
The sequence number is the second field of the TCP header. It represents the first byte of data in
the TCP segment.
Every byte of the TCP segment’s data is labeled with a number called a
sequence number. The sequence number field in the header has the
sequence number of the first byte of data in the segment.
Header
The sequence
number of the first
42894
byte of data is sent
42895 in the header
42896
42897
42898
42899
42900
42901
42902
42903
42904
42905
42906
42907
42908
Acknowledgement Number #
Header
Reserved 8 flags (8 Window Size
Length
(4 bits) bits) (2 bytes)
(4 bits)
Header Length #
Header
Reserved 8 flags (8 Window Size
Length
(4 bits) bits) (2 bytes)
(4 bits)
The length of the TCP header is specified here. This helps the receiving end to
identify where the header ends and the data starts from.
Reserved Field #
Source Port Number (2 bytes) Destination Port Number (2 bytes)
Header
Reserved 8 flags (8 Window Size
Length
(4 bits) bits) (2 bytes)
(4 bits)
The reserved bits serve as an offset and are left for potential future use
The header has a 4-bit field that is reserved and is always set to 0. This field
aligns the total header size to be in multiples of 4 (as we saw was necessary
for the header length to be processed).
Quick Quiz! #
1
Given an initial sequence number of 255 and 50 bytes sent in a TCP
segment, what will be the value of the sequence number field in the next
TCP packet header?
COMPLETED 0%
1 of 4
In the last lesson, we discussed eight elds of the TCP header. Let's now discuss the last few!
• Flags
• ACK
• RST
• SYN
• FIN
• TCP Connection Establishment & Termination
• CWR & ECN
• PSH
• URG
• Quick Quiz!
TCP headers have eight 1-bit flags that are imperative to signaling in the
protocol.
Header
Reserved 8 flags (8 Window Size
Length
(4 bits) bits) (2 bytes)
(4 bits)
C E U A P R S F
W C R C S S Y I
R N G K H T N N
The first four discussed below, namely ACK, RST, SYN, and FIN are used in
the establishment, maintenance, and tear-down of a TCP connection.
ACK #
This flag is set to 1 in a segment to acknowledge a segment that was received
previously. This is an important part of the protocol. In other words, when a
receiver wants to acknowledge some received data, it sends a TCP segment
with the ACK flag and the acknowledgment number field appropriately set.
This flag is also used in connection establishment and termination as we will
see in more detail later.
RST #
The reset flag immediately terminates a connection. This is sent due to the
result of some confusion, such as if the host doesn’t recognize the connection,
if the host has crashed, or if the host refuses an attempt to open a connection.
SYN #
The synchronization flag initiates a connection establishment with a new
host. The details will be covered later in the lesson on connection
establishment.
FIN #
The slides below give a very high level overview of how these flags are used to
establish and terminate a TCP connection.
A B
SYN
Host A wants to establish a connection with host B and so sends a segment with the SYN flag set
1 of 8
A B
SYN
SYN-ACK
Host B acknowledges the SYN with a segment with an ACK flag along with a SYN flag of its own
set in the same message
2 of 8
A B
SYN
SYN-ACK
ACK
Host A acknowledges the SYN with a segment with an ACK flag
3 of 8
A B
FIN
Host A wants to TERMINATE a connection with host B and so sends a segment with the FIN flag
set to 1
4 of 8
A B
FIN
ACK
Host B acknowledges the FIN with a segment with an ACK flag
5 of 8
A B
FIN
ACK
FIN
A B
FIN
ACK
FIN
ACK
A B
FIN
ACK
FIN
ACK
The rest of the flags, given below, are not very well-known. However, it
doesn’t hurt to know about them.
PSH #
The default behavior of TCP is in the interest of efficiency; if multiple small
TCP segments were received, the receiving TCP will combine them before
handing them over to the application layer. However, when the Push (PSH)
flag is set, the receiving end immediately flushes the data from its buffer to
the application instead of waiting for the rest of it to arrive.
This is usually used for applications like Telnet, where every keystroke is a
command. It would not make sense to say, buffer 50 keystrokes and send them
to the application layer at once, so, every keystroke is pushed.
A B
A B
Host A will 'flush' that data out to the application when it is full
2 of 9
A B
A B
A B
Host A receives a TCP segment from host B
5 of 9
PSH
A B
A B
PSH
and flushes the buffer out to the application even though the buffer is not full yet
8 of 9
Data gets
flushed to
the A B
application
The buffer gets flushed, is empty and is ready to receive data again
9 of 9
URG #
The Urgent flag marks some data within a message as urgent. Upon receipt of
an urgent segment, the receiving host forwards the urgent data to the
application with an indication that the data is marked as urgent by the sender.
The rest of the data in the segment is processed normally.
This would be used when suppose a large file is being transferred but the
sender realizes that it’s the wrong file and sends a command to stop transfer.
It wouldn’t make sense to have the file finish transferring first, hence the
command to stop transfer is marked as urgent and is executed before the file
is done transferring.
Quick Quiz! #
1 What functionality does the urgent flag allow that the push flag does not?
COMPLETED 0%
1 of 3
Let’s finish off looking at the rest of the headers in the next lesson!
TCP Headers: Window Size, Checksum & More
• Window Size
• Checksum
• Urgent Pointer
• Options & Padding
• Common Options
• Quick Quiz!
Window Size #
Remember the ‘buffer’ we discussed in the last lesson? Well, the window size
is essentially the amount of available space in that buffer. TCP at the receiving
end buffers incoming data that has not been processed yet by the overlaying
application. The amount of available space in this buffer is specified by the
window size.
Header
Reserved 6 flags (6 Window Size
Length
(6 bits) bits) (2 bytes)
(2 bytes)
The window size is communicated to the sender by the receiver in every TCP
message and gets updated as the buffer fills and empties. If the window size
reduces after a bit, the sender will know that it needs to reduce the amount of
data being sent, or give the receiver time to clear the buffer.
To put it another way, the window size is at first equal to as much data as the
receiving entity is willing and able to receive. As it receives some more data,
the window size will decrease and as it hands over some of the received data
to the application layer, the window size will increase. This is useful to
implement flow control.
Header
Reserved 6 flags (6 Window Size
Length
(6 bits) bits) (2 bytes)
(2 bytes)
The checksum allows TCP to be reliable, and the urgent pointer identifies the range of bytes in the
packet that are urgent.
Checksum #
The checksum is calculated exactly like in UDP except that the checksum
calculation is mandatory in TCP!
Urgent Pointer #
The urgent pointer defines the byte to the point of which the urgent data
exists. This is because a single segment can contain both parts of urgent and
regular data. This field is only used in conjunction with the urgent flag.
Source Port Number (2 bytes) Destination Port Number (2 bytes)
Header
Reserved 6 flags (6 Window Size
Length
(6 bits) bits) (2 bytes)
(2 bytes)
The options field allows to build in extra functionality that the regular header does not cover
Common Options #
Some options are commonly used and are well-defined. Here’s a table that
discusses each.
Option Explanation
Quick Quiz! #
1
The window size represents the amount of buffer available at the
receiver. This should be constant and only communicated in the first TCP
segment sent from the receiver to the sender. What do you think?
COMPLETED 0%
1 of 2
We’re finally done with the TCP header! Let’s learn more about the protocol
itself starting from the next lesson.
TCP Connection Establishment: Three-way Handshake
• Initiating a Connection
• Responding to an Initial Connection Message
• Acknowledging The Response
• Quick Quiz!
Initiating a Connection #
When a client host wants to open a TCP connection with a server host, it
creates and sends a TCP segment with:
Sequence Number
Connection Request x
Acknowledgment Number
SYN
ACK
Initiating a Connection
1 of 4
Client Server
Sequence Number
Connection Request x
Acknowledgment Number
SYN
ACK
Connection Response
Sequence Number
y
Acknowledgment Number
x+1
SYN
ACK
Sequence Number
Connection Request x
Acknowledgment Number
SYN
ACK
Connection Response
Sequence Number
y
Acknowledgment Number
x+1
SYN
ACK
Sequence Number
Connection Request x
Acknowledgment Number
SYN
ACK
Connection Response
Sequence Number
y
Acknowledgment Number
x+1
SYN
ACK
Connection established!
Quick Quiz! #
In this lesson, we'll look at some unconventional ways that connection establishment may occur.
In the last lesson, we looked at the most common way that TCP connection
establishment could occur. Now, let’s look at some other ways it can
successfully occur.
Init
SYN+ACK/SYN SYN
ACK ACK/SYN+ACK
Established
TCP FSM for connection establishment
Client-Side #
Let’s carve out the paths in this FSM. Here’s the three-way handshake path.
Server-Side #
1. On the other hand, a server host starts in the Init state.
Connection Established
Connection Established
Both sides must know the port number for each other in this case. It doesn’t
have to be a well-known port number or the same on both sides.
Quick Quiz! #
1
Simultaneous connection may result in more segments being exchanged
than a regular three-way handshake.
COMPLETED 0%
1 of 2
In this lesson, we'll look at a couple of loopholes in TCP's implementation and how modern xes took care of them.
There are other scenarios in which a connection may be refused but these are
the common ones. If a process is listening on a port, but the connection is to
be refused, the server sends a SYN segment with the following properties:
Client Server
Sequence Number
Connection Request x
Acknowledgment Number
SYN
ACK
Initiating a Connection
1 of 3
Client Server
Sequence Number
Connection Request x
Acknowledgment Number
SYN
ACK
Connection Response
Sequence Number
y
Acknowledgment Number
x+1
SYN
ACK
RST
Sequence Number
Connection Request x
Acknowledgment Number
SYN
ACK
Connection Response
Sequence Number
y
Acknowledgment Number
Connection Refused x+1
SYN
ACK
RST
Client Server
Normal behavior with TCBs
1 of 5
Client Server
Client Server
Client Server
Client Server
Sequence Number
y
SYN
ACK
Sequence Number
y+1
ACK
SYN
Syn Cookies #
However, this allowed attackers to carry out an attack where they could
render a resource unavailable in the network by sending it valid messages.
Such attacks are called Denial of Service (DoS) attacks because they deny the
user(s) a service. Here’s how this one was carried out:
1. The attacker would send a few 100 SYN segments every second to a
server
2. The attacker would not reply to any received SYN+ACK segments
3. To avoid being caught, the attacker would send these SYN segments with
a different IP address from their own IP address.
4. Once a server entered the SYN RCVD state, it would remain in that state
for several seconds, waiting for an ACK and not accepting any new,
possibly genuine connections, thus being rendered unavailable.
Client Server
Client Server
Client Server
Client Server
Sequence Number
y
ACK
SYN
Client Server
Sequence Number
y
ACK
SYN
Client Server
Sequence Number
y
ACK
SYN
Client Server
Sequence Number
y
ACK
SYN
To avoid these SYN flood attacks, newer TCP implementations reply directly
with SYN+ACK segments and wait until the reception of a valid ACK to create a
TCB.
The goal is to not store connection state on the server immediately upon
reception of a SYN packet. But, without this information, the server cannot tell
if a subsequent ACK it receives is from a legitimate client that had sent a
benign SYN packet. One way to do it is to verify that if the acknowledgement
number contained in the ACK packet is y , then the server had sent a sequence
number y − 1 in the SYN+ACK packet. But, again, if we are remembering the
initial sequence number for each SYN packet, we are back to square one -
remembering connection state. The way SYN Cookie solves this problem is to
use a function that uses some information from the client’s SYN packet and
some information from the server side to calculate a random initial sequence
number. This number, say, y − 1 is sent to the client in a SYN + ACK message.
If an ACK packet is later received with a sequence number y , using some
packet header fields and some server side information, a reverse function can
verify that the acknowledgement number is valid. If not, the connection is
refused, otherwise a TCB is created and a connection is established.
The advantage of SYN cookies is that the server would not need to create and
store a TCB upon reception of the SYN segment.
Quick Quiz! #
That is it for TCP connection establishment. What we’ll cover next is how TCP
releases a connection.
TCP Connection Release
2. Abrupt connection release, where either one user closes both directions
of data transfer or one TCP entity is forced to close the connection.
An abrupt release is executed when a RST segment is sent. A RST can be sent
for the following reasons:
When a RST segment is sent by a TCP entity, it should contain the current
value of the sequence number for the connection (or 0 if it does not belong to
any existing connection), and the acknowledgment number should be set to
the next expected in-sequence sequence number on this connection.
FSM #
The following figure shows an FSM that depicts the various ‘graceful’ ways
that a TCP connection can be released.
Don’t feel overwhelmed if you don’t understand it yet, we’ll study each
possible path individually.
Connection Release FSM
Starting from the Established state, there are two main paths through this
FSM.
Receiving a FIN #
Throughout the rest of this lesson we’ll refer to the two hosts as client and
server. In the case of this path, the client receives a FIN segment. Let’s trace it.
1. The client receives a segment with sequence number x and the FIN flag
set. The utilization of the FIN flag indicates that the byte before sequence
number x was the last byte of the byte stream sent by the server. The FIN
segment is subject to the same retransmission mechanisms as a normal
TCP segment. In particular, its transmission is protected by the
retransmission timer that we’ll look at in the next few lessons.
2. Once all of the data has been delivered to the application layer entity, the
TCP entity sends an ACK segment to acknowledge the FIN segment it
received in step (1), whose acknowledgment number field is set to
(x + 1) mod 232 .
3. At this point, the TCP connection enters the CLOSE_WAIT state. In this
state, the client can still send data to the server.
4. Once the client has sent all the data that it was supposed to, it sends a FIN
segment and enters the LAST_ACK state. In this state, the client waits for
the acknowledgment of its FIN segment. It may still retransmit
unacknowledged data segments, e.g. if the retransmission timer expires.
5. Upon reception of the acknowledgment for the FIN segment, the TCP
connection is completely closed and its TCB can be discarded.
Tracing through the FSM #
Here are some slides tracing this path through the FSM.
FIN FIN/ACK
FIN
FIN FIN/ACK
FIN
FIN FIN/ACK
FIN
FIN FIN/ACK
FIN
Sending a FIN #
The second path is when the client decides first to send a FIN segment.
1. Upon sending the FIN, the connection enters the FIN_WAIT1 state. In this
state, the client can retransmit unacknowledged segments, but cannot
send new data segments. There are two paths that this one can split into
after this:
A. The client receives an ACK segment in response to its FIN. The TCP
connection enters the FIN_WAIT2 state in which new data segments
from the server are still accepted until the reception of a FIN
segment. The acknowledgment for this segment is sent once all the
data before the FIN segment has been delivered to the client. After
this, the connection enters the TIME_WAIT state.
B. In the second case, a FIN segment is received from the server. The
connection enters the Closing state once all the data from the server
has been delivered to the client. In this state, no new data segments
can be sent and the client waits for an acknowledgment of its FIN
segment before entering the TIME_WAIT state.
2. A TCP connection enters the TIME_WAIT state after the client sends the
last ACK segment to a server. This segment indicates to the server that all
the data that it’s sent has been correctly received and that it can safely
release the TCP connection and discard the corresponding TCB.
FIN FIN/ACK
FIN
FIN FIN/ACK
FIN
FIN FIN/ACK
FIN
FIN FIN/ACK
FIN
FIN FIN/ACK
FIN
FIN FIN/ACK
FIN
FIN FIN/ACK
FIN
FIN FIN/ACK
FIN
Quick Quiz! #
1
What are some reasons why a connection may get abruptly terminated?
COMPLETED 0%
1 of 3
Now that we’re done with what connection release looks like in TCP, let’s move
on to efficient data transmission with TCP using Nagle’s algorithm in the next
lesson!
Efficient data transmission with TCP
In this lesson, we'll study the main data transfer mechanisms used by TCP.
Algorithm #
Nagle's Algorithm
Limitations #
Nagle’s has a few limitations:
2. TCP applications that require low latency and fast response times such
as internet phone calls or real-time online video games, do not work well
when Nagle’s is enabled. The delay caused by the algorithm triggers a
noticeable lag. These applications usually disable Nagle’s with an
interface called the TCP_NODELAY option.
3. The algorithm was originally developed at a time when computer
networks supported much less bandwidth than they do today. It saved
bandwidth and made a lot of sense at the time, however, the algorithm is
much less frequently used today.
4. The algorithm also works poorly with delayed ACKS, a TCP feature that
is used now. With both algorithms enabled, applications experience a
consistent delay because Nagle’s algorithm doesn’t send data until an ACK
is received and delayed ACKs feature doesn’t send an ACK until after a
certain delay.
Quick Quiz! #
1
Consider the following scenario:
The last segment received by a TCP entity had an ACK value of 10.
The MSS value is 536.
There are 50 bytes in the buffer waiting for transmission.
The window size is 500.
The application sends a 500 byte message.
All the data sent so far has been acknowledged.
COMPLETED 0%
1 of 3
For now, that’s all on Nagle’s. Let’s move on to TCP window-scaling
mechanisms.
TCP Window Scaling
So what if the round trip time is short enough to accommodate sending more
data without having to wait for acknowledgments? The table below shows the
rough maximum throughput that can be achieved by a TCP connection with a
64 KBytes window as a function of the connection’s round-trip-time:
This limitation was not a severe problem when TCP was designed, because at
the time the available bandwidth was 56 kbps at best. However, in today’s
networks where the bandwidth can be in order gigabytes, this limitation is not
acceptable.
It takes 2×72000×1000
3×108
= 480 ms to get to the end host and then for the
acknowledgement to return (ignoring the transmission time of the ack).
This is the total distance to be covered by two segments divided by the
speed of light.
Now, even if the sliding window is at its maximum of 64 kB, the sender
will only transmit 64 kB and then wait, sitting idle until an
acknowledgement is received from the other side.
Note that we don’t use satellite links much these days, but effectively,
whenever the product of bandwidth and delay is high, which is common with
today’s high speed networks, we face the same problem. So, if the delay is
small, but the bandwidth is high, the sender can still put out a lot of bytes
really quickly on the wire and still have to wait for an ACK sitting idle.
Basic idea: instead of storing the size of the sending window and receiving
window as 16-bit integers in the TCB, we keep the 16-bit window size, but
introduce a multiplicative scaling factor.
Scaling Factor #
As the TCP segment header only contains 16 bits to place the window field, it
is impossible to copy the size of the sending window in each sent TCP
segment. Instead, the header contains:
Note that the protection mechanism of not maintaining state from the SYN
packet via SYN cookies has the disadvantage that the server wouldn’t
remember the proposed scaling factor.
Improvement #
By using the window scaling extensions defined in RFC 1323, TCP
implementations can use a receive buffer of up to 1 GByte. With such a
receive buffer, the maximum throughput that can be achieved by a single TCP
entity is delineated in the following table:
Quick Quiz! #
1
Given the window scaling factor of 6 and window size of 3125, how
many bytes can the sender send without waiting for an
acknowledgement?
COMPLETED 0%
1 of 2
Did you know that you can measure the round trip time on your network?
Yes, it’s true. Let’s see how in the next lesson.
Exercise: Measuring RTT with Ping
• Round-trip-time
• Pinging Google
• Dissecting The Output
Round-trip-time #
Just to recap the last lesson, the Round Trip Time of a connection is the
amount of time it takes to send a packet and receive its acknowledgment.
The ping command can be used to measure the round-trip-time to send and
receive packets from a remote host. We’re just pinging google from here
because the location of the server that actually runs these commands from
our website may change over time. However, if you try this locally, chose a
remote destination which is far from your current location, e.g., a small web
server in a distant country.
Pinging Google #
Terminal
Size of the
packet
Sequence
number and
TTL of the
packet
Note that the Time To Live (TTL) is the number of routers a packet can hop.
So a ttl of 53 means it can jump 53 more ‘hops’ before being discarded. Each
router or intermediary forwarding device decreases the TTL by one. This was
a rather simplified definition of the term. We will study it in detail in the next
chapter.
Now that we know some quirks and alterations added to optimize TCP over
time, let’s get into TCP congestion control algorithms!
TCP Congestion Control: AIMD
The last part of TCP that we're going to study is congestion control.
But first:
sending window. Recall that after transmitting data equal to the window size,
the sender must pause and wait at least one RTT for the ACK before it can
window
transmit more data. Thus the maximum data rate is: rtt
where window is
the maximum between the host’s sending window and the window advertised
by the receiver.
Hence, TCP’s congestion control scheme can restrict sending windows based
on a congestion window. The current value of the congestion window is
stored in the TCB of each TCP connection. The value of the window that can be
used by the sender is
min(congestion window, receiving window, sending window) .
The figure below illustrates the evolution of the transmission rates allocated
to a host in a network.
Congestion
Rate
Time
How the transmission rate of a host changes with AIMD. Notice the saw tooth pattern.
The Additive Increase part of the TCP congestion control increments the
congestion window by MSS bytes every round-trip time. In the TCP literature,
this phase is often called the congestion avoidance phase. Once congestion is
detected, the Multiplicative Decrease part of the TCP congestion control
reacts by multiplying the current value of the congestion window with a
number greater than 0 and less than 1.
However, since the increase in the window is so slow, the TCP connection may
have to wait for many round-trip times before being able to efficiently use the
available bandwidth. To avoid this, the TCP congestion control scheme
includes the slow-start algorithm.
Quick Quiz! #
1
Which of the following is not a requirement of a congestion control
algorithm?
COMPLETED 0%
1 of 2
Let’s have a look at the slow start algorithm in the next lesson.
TCP Congestion Control: Slow Start
During slow-start:
Threshold
Time
Slow Start
exponential increase of
congestion window
1. Severe Congestion
2. Mild Congestion
Severe Congestion #
TCP considers that the network is severely congested when its retransmission
timer expires. The following process is followed accordingly:
1. The sender performs slow-start until the first segments are lost and the
retransmission timer expires.
2. At this time, TCP retransmits the first segment and the slow start
threshold is set to half of the current congestion window. Then the
congestion window is reset at one segment.
3. The lost segments are retransmitted as the sender again performs slow-
start until the congestion window reaches the slow start threshold.
Figure #
The figure below illustrates the evolution of the congestion window when
there is severe congestion:
Congestion
Window
Threshold
Threshold
Time
Slow Start
Congestion Avoidance
exponential increase of
linear increase of congestion window
congestion window
Mild Congestion #
TCP considers that the network is lightly congested if it receives three
duplicate acknowledgments.
Expects
segment with
ACK sequence
2 number 2 next
SEQ
2
x
Dropped
SEQ
3
x
Dropped
SEQ
4
SEQ
5
1 of 2
SEQ
1
Expects
segment with
ACK sequence
2 number 2 next
SEQ
2
x
Dropped
SEQ
3
x
Dropped
SEQ
4
SEQ
5
SEQ
2
Fast retransmit! Segment
#2 retransmitted without
waiting for timer expiration!
2 of 2
3. If the fast retransmit is successful, this implies that only one segment has
been lost.
Figure #
The figure below illustrates the evolution of the congestion window when the
network is lightly congested and all lost segments can be retransmitted using
fast retransmit.
Congestion
Window
Threshold Threshold
(half of congestion window) (half of congestion window)
Time
Slow Start
Congestion Avoidance
exponential increase of
linear increase of congestion window
congestion window
Evolution of the TCP congestion window when the network is lightly congested
Quick Quiz! #
1
Slow start increases the congestion window size exponentially, whereas
congestion avoidance increases the congestion window size linearly.
COMPLETED 0%
1 of 3
That’s it for the transport layer! Let’s look at socket programming in Python
next!
The Basics
• Socket Programming
• Types of Network Applications
• Socket Programming in Python
Socket Programming #
Recall that network applications usually consist of two programs: the
server program and the client program. These programs reside on two
separate end systems.
Pros: any other developer can write applications that are compatible
with standard ones.
Cons: some customizability will be compromised.
loopholes.
1. We’ll start with the very basics. You’ll learn how to set up a UDP socket
using the socket library.
2. We’ll then write a complete server and a client program that runs on
UDP. We’ll also see them in action.
4. You’ll get to do a project next! You’ll be writing your very own chat app.
5. Lastly, we’ll see how a few tweaks can change the program into one that
runs on TCP.
We're now going to write some basic server code in TCP. Let's get right into it.
Remember that sockets are just software endpoints that processes write and
read data from. They are bound to an IP address and a port. As we will see,
the sending process attaches the IP address and port number of the receiving
application. The IP address and port number of the sending process are also
attached to the packets as headers, but that’s not done manually in the code of
the application itself. Networking libraries are provided with nearly all
programming languages and they take responsibility for lots of plumbing.
Importing socket #
The first step when writing a network application in Python is to import the
socket library. It’s generally already part of the Python bundle, so no extra
library will have to be manually installed.
import socket
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print(s)
The syntax is shown here for completion, however, our main focus will be on
explaining the family and type properties.
If you want to study the rest of the fields, have a look at the documentation.
Default arguments are being used for the remaining arguments, which is fine
in our example.
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
port = 3000
hostname = 127.0.0.1
s.bind((hostname, port))
Now, we bind the socket to an IP address and a port using the bind()
function. It’s given a certain n-tuple as an argument, where n depends on the
family of the socket. In the case of SOCK_DGRAM , it’s a tuple of the IP address
and the port like the following.
Port Number #
We define the hostname and the port as variables on lines 4 and 5. The port
is 3000 in this case, and as mentioned previously, the ports 0 − 1024 should
be avoided as they’re reserved for other system-defined processes. Binding to
them may generate an error as it may already be in use by another
application.
Hostname #
The hostname is the IP address that your server will listen on. You can set it to
one of three options:
2. You could also set it to the empty string '' which represents the
INADDR_ANY . This specifies that the program intends to receive packets
sent to the specified port destined for any of the IP addresses configured
on that machine.
Now that we know how to set up a basic socket, which is necessary for any
kind of network application using sockets, let’s write some code that is specific
to servers.
Writing a UDP Server
In the last lesson, we'd written code to setup an IPV4 socket on TCP. Let's now get into writing a program for a
basic server.
Let’s code!
# Setting up a socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
port = 3000
hostname = '127.0.0.1'
s.bind((hostname, port)) # Binding the socket to a port and IP address
print('Listening at {}'.format(s.getsockname())) # Printing the IP address and port of socket
You can use the getsockname() method on an object of the socket class to find
the current IP address and port that a socket is bound to.
Listening In nitely #
Next, we set up a while loop (lines 10 and 11) so that the server listens
infinitely. If the rest of this code weren’t in this infinite while loop, the server
would exit after dealing with one client.
import socket
# Setting up a socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
port = 3000
hostname = '127.0.0.1'
s.bind((hostname, port)) # Binding the socket to a port and IP address
print('Listening at {}'.format(s.getsockname())) # Printing the IP address and port of socket
while True:
# The code to handle clients will go here
Note that the code stops and waits at recvfrom() until some data is received.
import socket
MAX_SIZE_BYTES = 65535 # Mazimum size of a UDP datagram
# Setting up a socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
port = 3000
hostname = '127.0.0.1'
s.bind((hostname, port)) # Binding the socket to a port and IP address
print('Listening at {}'.format(s.getsockname())) # Printing the IP address and port of socket
while True:
data, clientAddress = s.recvfrom(MAX_SIZE_BYTES) # Receive at most 65535 bytes at once
import socket
# Setting up a socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
port = 3000
hostname = '127.0.0.1'
s.bind((hostname, port)) # Binding the socket to a port and IP address
print('Listening at {}'.format(s.getsockname())) # Printing the IP address and port of socket
while True:
data, clientAddress = s.recvfrom(MAX_SIZE_BYTES)
message = data.decode('ascii')
upperCaseMessage = message.upper()
import socket
# Setting up a socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
port = 3000
hostname = '127.0.0.1'
s.bind((hostname, port))
print('Listening at {}'.format(s.getsockname()))
while True:
data, clientAddress = s.recvfrom(MAX_SIZE_BYTES)
message = data.decode( ascii )
upperCaseMessage = message.upper()
print('The client at {} says {!r}'.format(clientAddress, message))
data = upperCaseMessage.encode('ascii')
import socket
# Setting up a socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
port = 3000
hostname = '127.0.0.1'
s.bind((hostname, port))
print('Listening at {}'.format(s.getsockname()))
while True:
data, clientAddress = s.recvfrom(MAX_SIZE_BYTES)
message = data.decode('ascii')
upperCaseMessage = message.upper()
print('The client at {} says {!r}'.format(clientAddress, message))
data = upperCaseMessage.encode('ascii')
s.sendto(data, clientAddress)
Now we have a basic server that accepts messages from clients, has a defined
purpose (capitalization), and responds to the client’s messages. Let’s write
code for a client to go with this in the next lesson.
Writing a UDP Client Program
• The Server
• Creating a Client Socket
• Reading Data
• Sending It to the Server
• Receiving the Server’s Response
• Decoding & Printing the Capitalized Message
The Server #
Here’s the server code that we have so far for reference.
import socket
# Setting up a socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
port = 3000
hostname = '127.0.0.1'
s.bind((hostname, port))
print('Listening at {}'.format(s.getsockname()))
while True:
data, address = s.recvfrom(MAX_SIZE_BYTES)
message = data.decode('ascii')
upperCaseMessage = message.upper()
print('The client at {} says {!r}'.format(clientAddress, message))
data = upperCaseMessage.encode('ascii')
s.sendto(data, clientAddress)
the OS will bind the socket to a port dynamically. So all we really need is to
create a UDP socket (line 3).
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
In fact, we can check what address and port the OS assigned to the socket
using the following line of code on line 4:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print('The OS assigned the address {} to me'.format(s.getsockname()))
You’ll always get (0.0.0.0,0) for now because we haven’t actually used the
socket. You’ll get the correct answer when we use the socket to send data in
the next lesson.
Reading Data #
Remember that the goal of this client and server was for the client to send a
string to the server that it would capitalize and send back? Well, we’ll get that
string from the user’s keyboard using the Python3 function, input() . The
function displays whatever prompt we specify, in this case the message ‘Input
lowercase sentence:’ and waits for the user to provide an input from the
keyboard (line 4). The user can then type a string of their choice and hit enter.
The string will be stored in the variable message .
Next, we encode the ASCII encoded data to bytes using the encode() function
(line 5).
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
message = input('Input lowercase sentence:' )
data = message.encode('ascii')
Also, notice that the getsockname() function will give us a useful answer at
this point (line 7).
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
message = input('Input lowercase sentence:' )
data = message.encode('ascii')
s.sendto(data, ('127.0.0.1', 3000))
print('The OS assigned the address {} to me'.format(s.getsockname()))
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
message = input('Input lowercase sentence:' )
data = message.encode('ascii')
s.sendto(data, ('127.0.0.1', 3000))
print('The OS assigned the address {} to me'.format(s.getsockname()))
data, address = s.recvfrom(MAX_SIZE_BYTES)
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
message = input('Input lowercase sentence:' )
data = message.encode( ascii )
s.sendto(data, ('127.0.0.1', 3000))
print('The OS assigned the address {} to me'.format(s.getsockname()))
data, address = s.recvfrom(MAX_SIZE_BYTES)
text = data.decode('ascii')
print('The server {} replied with {!r}'.format(address, text))
Now we have both our server and client programs. Let’s see them live in
action next!
Running The UDP Server & Client Together
We've spent the last few lessons writing code for a very basic client and a server. Let's see these in action in this
lesson!
1. Type your code and when you are ready to run the program, click on
Run. The server code should start up automatically.
2. Open another terminal by clicking on +
3. Type the command python3 /usercode/udp.py client Note that it can be
server in place of client .
4. Enter the text in the client window and see the effect.
5. If the program is not running to your satisfaction:
1. Kill the running server program by typing the break sequence
ctrl+c or command+c in both of the terminal windows.
def server(port):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
hostname = '127.0.0.1'
s.bind((hostname, port))
print('Listening at {}'.format(s.getsockname()))
while True:
data, clientAddress = s.recvfrom(MAX_SIZE_BYTES)
message = data.decode('ascii')
upperCaseMessage = message.upper()
print('The client at {} says {!r}'.format(clientAddress, message))
data = upperCaseMessage.encode('ascii')
s.sendto(data, clientAddress)
def client(port):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
message = input('Input lowercase sentence:' )
data = message.encode('ascii')
s.sendto(data, ('127.0.0.1', port))
print('The OS assigned the address {} to me'.format(s.getsockname()))
data, address = s.recvfrom(MAX_SIZE_BYTES)
text = data.decode('ascii')
print('The server {} replied with {!r}'.format(address, text))
if __name__ == '__main__':
funcs = {'client': client, 'server': server}
parser = argparse.ArgumentParser(description='UDP client and server')
parser.add_argument('functions', choices=funcs, help='client or server')
parser.add_argument('-p', metavar='PORT', type=int, default=3000,
help='UDP port (default 3000)')
args = parser.parse_args()
function = funcs[args.functions]
function(args.p)
In the next lesson, we’re going to look at some possible improvements to our
current UDP program.
Improvements to UDP Programs: Avoiding Arbitrary
Servers
There are a few improvements that can easily be made to our UDP program. Let's have a look.
Note how the client does not check who it is receiving the message from. It
just receives a message.
import socket
def client(port):
de c e t(po t):
host = '127.0.0.1'
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((host, port))
message = input('Input lowercase sentence:' )
data = message.encode('ascii')
s.send(data)
print('The OS assigned the address {} to me'.format(s.getsockname()))
data = s.recv(MAX_SIZE_BYTES)
text = data.decode('ascii')
print('The server replied with {!r}'.format(text))
With the sendto() method, we had to specify the IP address and port of the
server every time the client wanted to send a message. However, with the
connect() method we used, we just use send() and recv() without passing
any arguments about which address to send to because the program knows
that.
This also means that no server other than the one the client connected to can
send it messages. The operating system discards any of those messages by
default.
Disadvantages #
The main disadvantage of this method is that the client can only be
connected to one server at a time. In most real life scenarios, singular
applications connect to multiple servers!
import socket
def client(port):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
hosts = []
while True:
host = input('Input host address:' )
hosts.append((host,port))
message = input('Input message to send to server:' )
data = message.encode('ascii')
s.sendto(data, (host, port))
print('The OS assigned the address {} to me'.format(s.getsockname()))
data, address = s.recvfrom(MAX_SIZE_BYTES)
text = data.decode('ascii')
if(address in hosts):
print('The server {} replied with {!r}'.format(address, text))
hosts.remove(address)
else:
print('message {!r} from unexpected host {}!'.format(text, address))
As you can see, we created a list called hosts which contains tuples like
(IP addresses, port numbers) of any host that the client connects to. Upon
receiving every message, it checks whether the message is from a host it
expects to receive a reply from. As soon as a reply is received, it removes the
host from the list.
Quick Quiz! #
1
Why do we remove the host’s address from hosts once a reply is
received?
COMPLETED 0%
1 of 2
In the next lesson, you’re going to try out an exercise for yourself: write a chat
app in UDP!
Project: Write a UDP Chat App!
• Instructions
Instructions #
Writing a chat app is not so different from the capitalization code we saw in
the last lesson. The idea is very simple. The client sends a message to the
server and the server should respond with one. Both messages should be
taken as input from the user. We’ve given you some basic starter code for it.
Here are some other factors you would want to consider to write your app:
1. Your client and server both need to stay alive and not exit after each
message sent.
2. Both the client and the server need to print every message received from
the other party.
3. The server should not be chatting with more than one client.
def server(port):
pass
# Your code goes here
def client(port):
pass
# Your code goes here
if __name__ == '__main__':
funcs = {'client': client, 'server': server}
parser = argparse.ArgumentParser(description='UDP client and server')
parser.add_argument('functions', choices=funcs, help='client or server')
parser.add argument('-p', metavar='PORT', type=int, default=3000,
pa se .add_a gu e t( p , eta a O , type t, de au t 3000,
help='UDP port (default 3000)')
args = parser.parse_args()
function = funcs[args.functions]
function(args.p)
Note that to run the code, you would need to follow these steps:
1. Type your code and when you are ready to run the program, click on
Run. The server code should start up automatically.
2. Open another terminal by clicking on +
3. Type the command python3 /usercode/udp.py client Note that it can be
server in place of client .
4. Enter the text in the client window and see the effect.
5. If the program is not running to your satisfaction:
1. Kill the running server program by typing the break sequence
ctrl+c or command+c in both of the terminal windows.
Every time you make a change to the code you must click run for the
changes to take effect
• Client
• Server
Client #
The client program uses a while loop to keep the conversation with the server
alive. Furthermore, it uses connect() to ensure that only one server is
connected to, and only replies from that server are received.
def client(port):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = '127.0.0.1'
while True:
s.connect((host, port))
message = input('Input message to send to server:' )
data = message.encode('ascii')
s.send(data)
data = s.recv(MAX_SIZE_BYTES)
text = data.decode('ascii')
print('The server replied with {!r}'.format(text))
Server #
import argparse, socket
def server(port):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
hostname = '127.0.0.1'
s.bind((hostname, port))
print('Listening at {}'.format(s.getsockname()))
while True:
data, clientAddress = s.recvfrom(MAX_SIZE_BYTES)
message = data.decode('ascii')
Great! Let’s look at how server and client programs can be written to run on
TCP in Python3 in the next lesson!
A TCP Client-Server Program
In the last chapter, we studied TCP theory in detail. Now, we'll look at how we can code up TCP programs in
Python.
• Introduction
• A TCP Server & Client Program
• Handling Fragmentation
• sendall()
• recvall()
Introduction #
There are a few key points to be noted about TCP programs:
Unlike UDP, TCP segments will be delivered as long as the sender and
receiver are connected by a path and they are both live.
A sending TCP entity might split TCP segments into packets and so,
receiving TCP entities would have to reassemble them. This is unlikely in
our small program but happens all the time in the real world. So we need
to take care of when there is data leftover in the buffer to send or to
receive after one call.
def server(port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('127.0.0.1', port))
sock.listen(1)
print('Listening at', sock.getsockname())
while True:
print('Waiting for a new connection')
sc, sockname = sock.accept()
print('Connection from', sockname)
print(' Socket name:', sc.getsockname())
print(' Socket peer:', sc.getpeername())
message = recvall(sc, 16)
print(' message from client:', repr(message))
sc.sendall(b'Goodbye, client!')
sc.close()
print(' Closing socket')
def client(port):
host = '127.0.0.1'
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
print('Client has been assigned the socket: ', sock.getsockname())
sock.sendall(b'Greetings, server')
reply = recvall(sock, 16)
print('Server: ', repr(reply))
sock.close()
if __name__ == '__main__':
choices = {'client': client, 'server': server}
parser = argparse.ArgumentParser(description='Send and receive over TCP')
parser.add_argument('role', choices=choices, help='which role to play')
parser.add_argument('-p', metavar='PORT', type=int, default=3000, help='TCP port (default
args = parser.parse_args()
function = choices[args.role]
function(args.p)
As you can see, the client program is pretty much the same as a UDP client
program. There are a few key differences which we will explore now:
Handling Fragmentation #
sendall() #
The send() function returns the length of the number of bytes it successfully
transmitted, which can be used to check if the entire segment was sent.
recvall() #
Application
Transport
Network you
are
Data Link here
Physical
1. Every data link layer technology has a limit on maximum frame size.
2. Most of them use a different maximum frame size.
3. Furthermore, each interface on an end system in the data link layer has a
link layer address. This means the link layer has to have an addressing
system of its own.
The network layer must cope with this heterogeneity of the data link layer.
2. The service provided by the network layer does not depend on the
service or the internal organization of the underlying data link layers.
This independence ensures:
1. The data plane. The data plane contains the protocols and
mechanisms that allow hosts and routers to exchange packets
carrying user data.
2. The control plane. The control plane contains the protocols and
mechanisms that enable routers to efficiently learn how to forward
packets towards their final destination.
Network Layer Services #
There are two types of services that can be provided by the network layer:
Quick Quiz! #
1
What is the unit of information in the network layer called?
COMPLETED 0%
1 of 3
In the next lesson, we’ll look at the two most common ways that the network
layer is organized.
Organization of the Network layer
We'll study the two internal organizations of the network layer in this lesson!
• Datagram Organization
• Forwarding Tables
• Virtual Circuit Organization
• Virtual Circuit Organization vs. Datagram Organization
• Advantages of Datagram Organization
• Advantages of The Virtual Circuit Organization
• Quick Quiz!
There are two possible internal organizations of the network layer: datagram
and virtual circuits.
Datagram Organization #
The datagram organization has been very popular in computer networks.
Datagram-based network layers include IPv4 and IPv6 in the global Internet,
CLNP defined by the ISO, IPX defined by Novell or XNS defined by Xerox.
A R1 R5
J
R2
1 of 7
R2
2 of 7
R1 receives packet to
send to J. It checks
its forwarding table to
R1's routing table see where to send
A via West the packet so that it R5's routing table
... gets to J. A via West
I via East ...
J via East R2's routing table I via East
A via West J via East
...
I via East
J via East
A R1 R5
J
R2
3 of 7
A R1 R5
J
R2
4 of 7
R1's routing table
A via West R5's routing table
... A via West
I via East ...
J via East R2's routing table I via East
A via West J via East
...
I via East
J via East
R1 The same R5
A process repeats
J
for every router
on the way
R2
5 of 7
A R1 R5
J
R2
6 of 7
R1's routing table
A via West R5's routing table
... A via West
I via East ... The packet
J via East R2's routing table I via East finally
A via West J via East reaches J!
...
I via East
J via East
A R1 R5
J
R2
7 of 7
Forwarding Tables #
Routers use hop-by-hop forwarding in the datagram organization. This means
that when a router receives a packet that is not destined to itself, it looks up
the destination address of the packet in its forwarding table.
Allow any host in the network to reach any other host. This implies that
each router must know a route towards each destination.
The data plane contains all the protocols and algorithms that are used by
hosts and routers to create and process the packets that contain user data.
The control plane contains all the protocols and mechanisms that are used to
compute, install, and maintain forwarding tables on the routers.
Telephone networks have been designed to carry phone calls that usually
last a few minutes.
The switch cooperates with the other switches in the network to create a
bi-directional channel between the two telephones through the network.
This channel will be used by the two telephones during the lifetime of the
call and will be released at the end of the call.
In a network using virtual circuits, all hosts are identified with a network
layer address. However, a host must explicitly request the establishment of a
virtual circuit before being able to send packets to a destination host. The
request to establish a virtual circuit is processed by the control plane, which
installs state to create the virtual circuit between the source and the
destination through intermediate routers.
The virtual circuit organization has been mainly used in public networks,
starting from X.25, and then Frame Relay and Asynchronous Transfer Mode
(ATM) network.
Also, since the packets follow a particular dedicated path, they reach the
destination in the order they were sent. Virtual circuits can be configured
to provide a variety of services including best effort, in which case some
packets may be dropped. However, in case of bursty traffic, there is a
possibility of packet drops.
Quick Quiz! #
1
Which of the following is not a network layer principle?
COMPLETED 0%
1 of 3
In the next lesson, we’ll look at how the control plane’s routing algorithms
work!
The Control Plane: Static & Dynamic Routing
In this section, we discuss the three main techniques that can be used to maintain the routing tables in a network.
The main purpose of the control plane is to maintain and build routing tables.
This is done via a number of algorithms and protocols which we will discuss
here.
Unit weight. If all links have a unit weight, shortest path routing prefers
the paths with the least number of intermediate routers.
Usually, the same weight is assigned to the two edges that correspond to a
physical link (i.e. R1→R2 and R2→R1). However, it’s not necessary and some
asymmetric links may have different bandwidths upstream and downstream.
Static Routing #
Manually computed routes are manually added to the routing table. This is
useful if there are a few outgoing links from your network. It gets difficult
when you have rich connectivity (in terms of the number of links to other
networks). It also does not automatically adapt to changes – addition or
removal of links or routers.
Link-State Routing #
Another way to create a routing table with the most efficient path between
two routers or ‘nodes’ is by using link-state routing.
Then, based on this learned topology, each router is able to compute its
routing table by using the shortest path computation such as Dijkstra’s
Algorithm. This is phase II.
Routing
Routing Algorithms
Quick Quiz! #
1
Given a network with 4 links with the following available bandwidth on
each:
1. 100
2. 300
3. 40
4. 10
C
weight = bandwidth
COMPLETED 0%
1 of 3
In this lesson, we'll discuss the Routing Information Protocol, a popular distance-vector algorithm, based on the
famous Bellman-Ford algorithm.
• Introduction
• Initial State
• Initial Routing Table
• The Algorithm
• Example
• Count to In nity Problem
• Fix #1: Split Horizon
• Fix #2: Split Horizon with Poison Reverse
• Quick Quiz!
Introduction #
The Routing Information Protocol (RIP) based on the famous Belmman-
Ford algorithm belongs to the distance vector class of routing algorithms and
was used in ARPANET. While it used to be incredibly popular, it’s not used
very much now. There are other distance vector routing algorithms too such
as Ford-Fulkerson.
Initial State #
Each router or ‘node,’ maintains a routing table that initially contains the
estimated cost to each of its neighbors.
Consider the following example of a small network where the yellow circles
represent nodes, the black lines represent links, and the purple numbers
represent the cost of each link.
1
A B 9
5 3 E
2
C D
4
Sample Network
What would the initial routing table look like at node C for example?
A 5 A
B 3 B
D 4 D
1. The names of the destination nodes which are the neighbors in this case.
2. The initial cost of the link to each of C’s neighbors,
3. The ‘next hop’ node, i.e. the node that C would have to send a packet to
in order for it to reach its destination. In this case, the next hop and the
destination are the same since the destinations are all C’s neighbors.
Every node receives all of its neighbors’ routing tables in two cases:
The Algorithm #
Let’s look at how the distance vector routing algorithm would arrive at the
table above.
The estimated cost will finally converge to the optimal cost after a series of
these message exchanges. Have a look at the following slides for an example:
Example #
1 Destination Cost Next Hop Destination Cost Next Hop
A B 9
A 5 A B 1 B
B 3 B C 5 C
5 3 E
D 4 D
2
C D
4
Routing table of C Routing table of A
1
Destination Cost Next Hop Destination Cost Next Hop
A B 9
A 5 A B 1 B
B 3 B C 5 C
5 3 E
D 4 D
2
C D
4
Routing table of C Routing table of A
B 3 B C 5 C
5 3 E
D 4 D
2
C D
4
Routing table of C Routing table of A
1
Destination Cost Next Hop Destination Cost Next Hop
A B 9
A 5 A B 1 B
5 3 B 3 B C 5 C
E
D 4 D
2
C D
4
Routing table of C Routing table of A
B 3 B C 5 C
5 3 E
D 4 D
2
C D
4
Routing table of C Routing table of A
1
Destination Cost Next Hop Destination Cost Next Hop
A B 9
A 5 A B 1 B
5 3 B 3 B C 5 C
E
D 4 D
2
C D
4
Routing table of C Routing table of A
C->A + A-
Node C->node isGreater
>node
B 3 5+1=6 No
C 0 5+5=10 No
B 3 B C 3 C
5 3 E
D 4 D E 9 E
2
C D
4
Routing table of C Routing table of B
C->B + B-
Node C->node isGreater
>node
A 5 3+1=4 Yes
C has received reachability information from B
C 0 3+3=6 No
E ∞ 3+9=12 Yes
1
Destination Cost Next Hop Destination Cost Next Hop
A B 9
A 4 B A 1 A
5 3 B 3 B C 3 C
E
D 4 D E 9 E
2 C 12 B
C D
4
Routing table of C Routing table of B
C->B + B-
Node C->node isGreater
>node
A 5 3+1=4 Yes
C 0 3+3=6 No
E ∞ 3+9=12 Yes
B 3 B E 2 E
5 3 E
D 4 D
2 E 6 D
C D
4
Routing table of C Routing table of D
C->D + D-
Node C->node isGreater
>node
1
Destination Cost Next Hop Destination Cost Next Hop
A B 9
A 4 B C 4 C
5 3 B 3 B E 2 E
E
D 4 D
2 E 6 D
C D
4
Routing table of C Routing table of D
C->D + D-
Node C->node isGreater
>node
B 3 B
5 3 E
D 4 D
2 E 6 D
C D
4
Final routing table of C
2. D corrects its routing table such that the cost to reach E is infinity ∞.
4. Suppose C gets a chance to advertise its routing table first and sends it
over to D.
5. Since C does not yet know about the link failure between D and E, its
routing table has an entry for a route to E with the cost of 6 via D.
6. D will notice that C has a route to E and will update its routing table with
a route to E via C. The cost of the route will be the sum of the cost of D to
C, and the cost C has to reach E: 4 + 6 = 9.
7. When C receives D’s routing table, it will notice that D has changed its
cost to reach E from 2 to 9 and will update its table accordingly to
4 + 9 = 13. Then it advertises it to other neighbors.
9. So, the ‘infinity’, in this case is not very high. The problem is that this
convergence to takes quite a while, and until the routers converge, there
is a forwarding loop. Packets at C destined to E would go around in circles
until their TTL expires.
Quick Quiz! #
Q What would the initial routing table look like at B for the following
example with distance vector routing?
COMPLETED 0%
1 of 1
The most popular routing algorithms today use link state routing, which
we’ll look at in the next lesson.
Programming Challenge: Routing Information Protocol
In this lesson, you'll be writing code for the routing information protocol that we looked at previously.
• Problem Statement
• Starter Code
• topology_reader.py
• Sample Input
• router.py
• What Our Test Does?
• port.py
• rip_packet.py
• Coding Exercise
Problem Statement #
In this challenge, you will implement the routing information protocol that we
just studied in the last lesson! You’re given some starter code files.
Starter Code #
For this coding challenge, we are providing you with a network simulator
written in python3 . The implementation of our simplified version of RIP is
also required in Python. Let’s look at the starter code module by module.
topology_reader.py #
This is the entry point to our code. It takes a network topology in the form of a
Python list as input and returns a list of router objects that reflect that
topology. Here’s what the topology looks like:
Sample Input #
Sa p e put
topology = [
[1, [11, 2, 21, 1], [12, 4, 41, 1]], # Routers and ports
[2,[21, 1, 11, 1],[22, 5, 53, 1], [23,3,31,1]], #[IP of router, [port of router, IP of dest
[3,[31,2,23,1],[32,5,52,1]],
[4,[41,1,12, 1],[42,5,51,1]],
[5,[51,4,42,1],[52,3,32,1],[53,2,22,1]]
]
The list consists of sublists. Each sublist represents one router. So [1, [11, 2,
21, 1], [12, 4, 41, 1]] , for instance, represents a router.
Note that a link between two routers has to be present in both. So a link
to a port on a router with IP 2 from a router with IP 1 [1, [11, 2, 21,
1] , [12, 4, 41, 1]] is reflected in the sublist of router with IP 2 , as
follows: [2, [21, 1, 11, 1] ,[22, 5, 53, 1], [23,3,31,1]]
router.py #
The router_base class contains the IP address, a list of RIP entries and a
list of ports for each router, along with some functions that will help you
implement the protocol. The IP address is self-explanatory but we’ll get to
the other two in a minute.
The router class inherits the router_base class and is the class you’ll be
working in. In particular, you’ll be writing the functions
port.py #
The port_link class defines the links on each port. This class consists of
the destination router’s IP address ( dest_IP_address ), the destination
router’s port’s IP addresses ( dest_port_IP ) and the cost of the link ( cost ).
The port class has two attributes: the IP address of the port ( port_IP ),
and the link on the port ( link ) which is an object of the class port_link .
rip_packet.py #
Coding Exercise #
Great! Now you have some background on the code. Note that we haven’t
discussed the skeleton code in its entirety so you should read it to understand
the methods provided. Try the challenge yourself in the widget below!
Note that main.py is empty. That’s okay, don’t worry about it.
main.py
port.py
rip_packet.py
router.py
topology_reader.py
class router_base:
def __init__(self, IP_address, rip_entries, ports):
self.IP_address = IP_address
self.rip_entries = rip_entries
self.ports = ports
def print_router(self):
print("~~~~ Router IP address = " + str(self.IP_address) + "~~~~")
print("---Ports---")
print("Port IP | Destination Router IP | Destination Port IP | Cost")
for p in self.ports:
p.print_port()
print("---RIP entries---")
print("port IP | destination IP address | next hop | cost")
for re in self.rip_entries:
re.print_rip_entry()
def return_router(self):
r =[]
r.append("~~~~ Router IP address = " + str(self.IP_address) + "~~~~")
r.append("---Ports---")
r.append("Port IP | Destination Router IP | Destination Port IP | Cost")
for p in self.ports:
r.append(p.return_port())
r.append("---RIP entries---")
r.append("port IP | destination IP address | next hop | cost")
for re in self.rip_entries:
r.append(re.return_rip_entry())
return r
class router(router_base):
def send_RIP_packets(self, routers):
# Write your code here
return routers
• Solution
• Explanation
• send_RIP_packets()
• receive_RIP_packets()
Solution #
main.py
port.py
rip_packet.py
router.py
topology_reader.py
class router_base:
def __init__(self, IP_address, rip_entries, ports):
self.IP_address = IP_address
self.rip_entries = rip_entries
self.ports = ports
def print_router(self):
print("~~~~ Router IP address = " + str(self.IP_address) + "~~~~")
print("---Ports---")
print("Port IP | Destination Router IP | Destination Port IP | Cost")
for p in self.ports:
p.print_port()
print("---RIP entries---")
print("port IP | destination IP address | next hop | cost")
for re in self.rip_entries:
re.print_rip_entry()
def return_router(self):
r =[]
r.append("~~~~ Router IP address = " + str(self.IP_address) + "~~~~")
r.append("---Ports---")
r.append("Port IP | Destination Router IP | Destination Port IP | Cost")
for p in self.ports:
r.append(p.return_port())
r.append("---RIP entries---")
r.append("port IP | destination IP address | next hop | cost")
for re in self.rip_entries:
r.append(re.return_rip_entry())
return r
class router(router_base):
def send_RIP_packets(self, routers):
self.rip_packet = RIP_packet(self.rip_entries)
for i in range(len(routers)): # Find all neighbors and send then RIP packet
for j in range(len(self.ports)):
if routers[i].IP_address == self.ports[j].link.dest_IP_address:
routers = routers[i].receive_RIP_packets(self.rip_packet, self.ports[j].link, rout
return routers
Explanation #
send_RIP_packets() #
line 62: We create the RIP packet that we’ll send to all of our neighbors on
this line. We pass the rip_entries list and the length of that list on this
line.
lines 63-66: we now find the neighbors of this router. We do this by
iterating over the given list of routers in the network and checking to see
if any of our ports have a link to them. We do this by iterating over our
ports and checking each port’s link’s destination IP address against the
router’s IP address. If they match, the router is a neighbor and we send it
our RIP packet by calling receive_RIP_packets() on it.
receive_RIP_packets() #
This function consists of the core of the Routing Information Protocol. It works
as follows:
1. The receiving router checks for all RIP entries in the received RIP packet
if they exist in its own RIP entries list.
2. If an entry is for a destination that’s not found in the receiving router’s
RIP entries list, it adds it as done on lines 85-88.
3. If an entry is for a destination that is found in the receiving router’s RIP
entries list, it does one of two things:
1. If the entry is from a router whose IP address is equal to the next
hop IP in the router’s current RIP entry and the cost has changed, it
simply sets the cost as the minimum of 16 and the new cost. Note
that the new cost can be greater or lesser than the current cost.
2. Otherwise, if the cost advertised in this RIP entry is lesser than the
one the router currently has, it sets its RIP entry to the one through
the router that sent the RIP entry.
-Another way to create a routing table with the most efficient path between
two routers or ‘nodes’ is by using link-state routing.
Link state routing works in two phases: reliable flooding and route
calculation. Let’s look at phase I now.
HELLO messages are also used to detect link and router failures. A link is
considered to have failed if no HELLO message has been received from
the neighboring router for a period of k × N seconds.
1
A B 9
5 3 E
2
C D
4
1 of 6
B and C discover A
1
A HELLO
B 9
5 3 E
HELLO
2
C D
4
2 of 6
A, C and E discover B
1
A HELLO
B 9
HELLO
5 3 E
HELLO
2
C D
4
3 of 6
A, B and D discover C
1
A B 9
5 3 E
HELLO HELLO
2
HELLO
C D
4
4 of 6
E and C discover D
1
A B 9
5 3 E
HELLO
2
HELLO
C D
4
5 of 6
5o 6
B and E discover E
1
A B 9
HELLO
5 3 E
HELLO
2
C D
4
6 of 6
LSPs #
Once a router has discovered its neighbours, it must reliably distribute its
local links to all routers in the network to allow them to compute their local
view of the network topology. For this, each router builds a link-state packet
(LSP) that contains the following information:
Flooding Algorithm #
The routers will construct their routing tables based on the LSPs.
Verifies whether this LSP is already stored inside its LSDB. If so, the
router has already distributed the LSP earlier and it does not need to
forward it.
Otherwise, the router forwards the LSP on all links except the link over
which the LSP was received.
To ensure that all routers receive all LSPs, even when there are transmission
errors, link state routing protocols use reliable flooding, which involves
acknowledgments, and if necessary, retransmissions to ensure that all link
state packets are successfully transferred to all neighbouring routers.
What We Have so Far: Routers combine the received LSPs with their
own LSP to compute the entire network topology.
Then in phase II, the routers apply shortest path algorithms to compute the
most efficient path.
Once a router has detected a local link failure, it generates and floods a
new LSP that no longer contains the failed link, and the new LSP
replaces the previous LSP in the network.
As the two routers attached to a link do not detect this failure exactly at
the same time, the status of the link may be advertised differently by one
of the routers.
When a link is reported in the LSP of only one of the attached routers, the
rest consider the link to have failed in both directions and remove it
from the directed graph that they compute from their LSDB! This check
allows link failures to be flooded quickly, as a single LSP is sufficient to
announce such bad news.
Furthermore, a link can only be used once the two attached routers have
sent their LSPs.
The two-way connectivity check also allows for dealing with router
failures. When a router fails, all its links fail by definition. Unfortunately,
it doesn’t send a new LSP to announce its failure. The two-way
connectivity check ensures that the failed router is removed from the
graph.
When a router fails, its LSP must be removed from the LSDB of all
routers. This can be done by using the age field that is included in each
LSP. When a router generates an LSP, it sets the LSP.age to a value called
the LSP’s lifetime (usually measured in seconds). All routers regularly
decrement the age of the LSPs in their LSDBs and an LSP is discarded
once its age reaches 0.
Quick Quiz! #
1 Why are routing loops uncommon in networks that use link state
routing?
COMPLETED 0%
1 of 2
In the next lesson, we’ll study Dijkstra’s algorithm!
The Control Plane: Route Calculation - Dijkstra's
Dijkstra’s Algorithm #
The goal is to find the shortest path from an initial node to all other nodes in
the graph.
We first need to set up some data structures for us to use throughout the
algorithm.
1. Create a set called the unvisited set. All the nodes are initially unvisited.
2. Create a set called the visited set. It’s initially empty.
3. Create a list called the parent list. It will contain mappings of nodes to
their parents.
4. Lastly, every node has a distance of it from the initial node. Initially, all
the nodes besides the initial node itself have a starting distance of
infinity. We call this d_node_n ,
5. Every link between two nodes in the graph has a certain weight. We call
this w_node_n_node_m .
Algorithm #
1. Start with the initial node in the graph. Mark it as the current node.
2. Consider each of its neighbor’s that are NOT in the visited set.
3. If the sum of the distance of the current node and the distance to the
neighbor from the current node is lower than the current distance of the
neighbor, replace it with the new distance.
In other words, if w_node_curr_node_n + d_node_curr < d_node_n , set
d_node_n to w_node_curr_node_n + d_node_curr .
1. Find the parent of the current node. Initially the current node is n.
2. Set the current node to the new parent node.
3. Store each ‘current node’ in a stack.
4. Repeat steps 1-3 until the initial node is reached.
5. Pop and print the contents of the stack until it is empty.
Visual Example #
Have a look at the following example to see how Dijkstra’s would apply to a
graph.
0 ∞
Parent
1
A B
9
E ∞
5 3
C 4
D
∞ ∞ Visited Unvisited
A A
B
Visit all of A's neighbors
C
D
E
1 of 16
0 ∞
Parent
1
A B 9
E ∞
5 3
C 4
D
∞ ∞ Visited Unvisited
A
B
Visit all of A's neighbors
C
D
E
2 of 16
0 ∞
Parent
1
A B
9
E ∞
5 3
C 4
D
∞ ∞ Visited Unvisited
A
Path through A has a
lower cost than infinity B
So replace with new cost
C
D
E
3 of 16
0 1
1
Parent
A B 9
B: A
E ∞
5 3
C: A
2
C 4
D
5 ∞ Visited Unvisited
A
Path through A has a
lower cost than infinity B
So replace with new cost
C
D
E
4 of 16
0 1
1
Parent
A B
9
B: A
E ∞
5 3
C: A
2
C 4
D
5 ∞ Visited Unvisited
A
Check paths to B's unvisited
neighbors next B
C
D
E
5 of 16
0 1
1
Parent
A B 9
B: A
E ∞
5 3
C: A
2
C 4
D
5 ∞ Visited Unvisited
A
Check paths to B's unvisited
neighbors next B
C
D
E
6 of 16
0 1
1
Parent
A B
9
B: A
E 10
5 3
C: B
2 E: B
C 4
D
4 ∞ Visited Unvisited
A
Update costs according
to the algorithm B
C
D
E
7 of 16
0 1
1
Parent
A B 9
B: A
E 10
5 3
C: B
2 E: B
C 4
D
4 ∞ Visited Unvisited
A
Repeat the process for C
B
C
D
E
8 of 16
0 1
1
Parent
A B
9
B: A
E 10
5 3
C: B
2 E: B
C 4
D D: C
4 8
Visited Unvisited
A
Repeat the process
B
C
D
E
9 of 16
0 1
1
Parent
A B 9
B: A
E 10
5 3
C: B
2 E: B
C 4
D D: C
4 8
Visited Unvisited
A
Repeat the process
B
C
D
E
10 of 16
0 1
1
Parent
A B
9
B: A
E 10
5 3
C: B
2 E: B
C 4
D D: C
4 8
Visited Unvisited
A
Repeat the process.
E has no neighbors.
B
C
D
E
11 of 16
0 1
1
Parent
A B 9
B: A
E 10
5 3
C: B
2 E: B
C 4
D D: C
4 8
Visited Unvisited
12 of 16
0 1
1
Parent
A B
9
B: A
E 10
5 3
C: B
2 E: B
C 4
D D: C
4 8
Visited Unvisited
13 of 16
0 1
1
Parent
A B 9
B: A
E 10
5 3
C: B
2 E: B
C 4
D D: C
4 8
Visited Unvisited
E->B A
B
C
D
E
14 of 16
0 1
1
Parent
A B
9
B: A
E 10
5 3
C: B
2 E: B
C 4
D D: C
4 8
Visited Unvisited
E->B->A A
B
C
D
E
15 of 16
0 1
1
Parent
A B 9
B: A
E 10
5 3
C: B
2 E: B
C 4
D D: C
4 8
Visited Unvisited
16 of 16
Quick Quiz! #
1
What is the aim of Dijkstra’s Algorithm?
COMPLETED 0%
1 of 3
• Problem Statement
• Input
• Output
• Sample Input
• Sample Output
• Coding Exercise
Problem Statement #
Given an adjacency matrix in a 2D array, solve the Single Source Shortest
Path algorithm, essentially by implementing the Dijkstra’s algorithm
discussed in the previous lesson. We’ve written some skeleton code for the
function.
Input #
1. An adjacency matrix, i.e., a 2D array, a source node, and a destination
node.
Output #
The shortest path between the source and destination in the form of an array
of integers where each integer represents a node and the total weight of the
path.
Sample Input #
1. graph = [
[0,1,5,-1,-1],
[1,0,3,-1,9],
[5,3,0,4,-1],
[-1,-1,4,0,2],
[-1,9,-1,2,0]
]
1 9
0 1 4
5 3 2
2 3
4
src = 0
dst = 3
Sample Output #
shortest_path = [0,1,2,3]
cost = 8
Coding Exercise #
Try it yourself below!
while(len(unvisited)>0):
# Visit all neighbors of current and update distance
for i in range(number_of_nodes):
if(graph[current][i]>=0 and distance[i] > graph[current][i]+distance[current]):
distance[i] = graph[current][i]+distance[current] # Update distance
parent[i] = current # Set new parent
curr = dst # Some code to get the shortest path from 'parent'
shortest_path.append(curr)
cost = 0
while curr is not src:
if parent[curr] == -1: # If there is no path to the source node
return([[],-1])
cost = cost + graph[curr][parent[curr]] # The cost is the sum of the links in a path
curr = parent[curr]
shortest_path.append(curr)
shortest_path.reverse()
return([shortest_path, cost])
def main():
graph = [
[0,1,5,-1,-1],
[1,0,3,-1,9],
[5,3,0,4,-1],
[-1,-1,4,0,2],
[-1,9,-1,2,0]
]
src = 0
dst = 3
print(Dijkstra(graph,src,dst))
if __name__ == "__main__":
main()
Explanation #
Let’s go through this code line by line.
Lines 1-9: we set up a few variables that are important for the
implementation.
1. The number_of_nodes is the number of nodes in the graph. It’s
equivalent to the number of rows/columns of the given graph . This
variable is not necessary for the algorithm itself, but makes
calculating other variables clear and easy.
2. The parent list will map each node to its ‘parent’ or the previous
node in the shortest path to the source node. Initialized to -1 .
3. The visited list is initially empty.
4. The unvisited list contains all the nodes in the graph. Since the
nodes in our graph are labeled by numbers, this list is simple to
generate.
5. The distance list has all the current distances of all the nodes from
the src node. Note that all the distances besides the distance of the
src node from itself are set to infinity, i.e., 16 .
4. Steps 1-3 are repeated until the while loop exits when no more nodes
are left to visit.
Lines 26-34: Calculating the shortest path and the cost. We traverse the
parent list link by link until a path is generated. Since we start from the
destination, the path has to be reversed. While we calculate the path, we
also sum up the cost of each link that is traversed.
Example: Assume the source node is 0 . IF parent[2] has the value
1 , the previous node from 2 is 1 as part of the shortest path to a
source. Then, suppose parent[1] is 0 . So the shortest path will turn
out to be [2, 1, 0] .
while(len(unvisited)>0):
# Visit all neighbors of current and update distance
for i in range(number_of_nodes):
if(graph[current][i]>=0 and distance[i] > graph[current][i]+distance[current]):
distance[i] = graph[current][i]+distance[current] # Update distance
parent[i] = current # Set new parent
if(current == dst):
break
curr = dst # Some code to get the shortest path from 'parent'
shortest_path.append(curr)
cost = 0
while curr is not src:
if parent[curr] == -1: # If there is no path to the source node
return([[],-1])
cost = cost + graph[curr][parent[curr]] # The cost is the sum of the links in a path
curr = parent[curr]
shortest_path.append(curr)
shortest_path.reverse()
return([shortest_path, cost])
def main():
graph = [
[0,1,5,-1,-1],
[1,0,3,-1,9],
[5,3,0,4,-1],
[-1,-1,4,0,2],
[-1,9,-1,2,0]
]
src = 0
dst = 3
print(Dijkstra(graph,src,dst))
if __name__ == "__main__":
main()
Explanation #
This solution differs from the previous one because it exits the while loop as
soon as the destination node is visited via the if condition on lines 18 and 19.
Since subsequent calculations for the rest of the graph do not change the
shortest path to the destination node, there is no need to visit all of them.
We're nally at the very core of the Internet. This lesson contains an introduction to the Internet protocol!
• IP Version 4
• IP Addresses
• Multihoming
• Address Assignment
• Subnetting
• Address Classes
• Subnet Masks
• Network Address
• Broadcast Address
• Default Subnet Masks
• Variable-Length Subnets
• Quick Quiz!
The Internet Protocol (IP) is the network layer protocol of the TCP/IP
protocol suite. The flexibility of IP and its ability to use various types of
underlying data link layer technologies is one of its key advantages. The
current version of IP is version 4 and is specified in RFC 791. We first describe
this version and later touch upon IP version 6, which is expected to replace IP
version 4 in the not so distant future.
IP Version 4 #
The design of IP version 4 was based on the following assumptions:
IP Addresses #
The addresses are an important part of any network layer protocol. IPv4
addresses are written as 32 bit numbers in dotted-decimal format, such as a
sequence of four integers separated by dots. Dotted decimal is a format
imposed upon the 32-bit numbers for relatively easier human readability. For
example:
1.2.3.4
00000001000000100000001100000100
Multihoming #
An IPv4 address is used to identify an interface on a router or an interface
on a host. Recall network interfaces from the physical communication media
chapter.
A router has thus as many IPv4 addresses as the number of interfaces that it
has in the data link layer. Most hosts have a single data link layer interface
and thus have a single IPv4 address. However, with the growth of wireless
more and more hosts have several data link layer interfaces (for example, an
Ethernet interface and a WiFi interface). These hosts are said to be
multihomed. A multihomed host with two interfaces has thus two IPv4
addresses.
Ethernet
Interface
Wireless
Interface
Router
Multihoming
1 of 2
}
Ethernet
Interface Two interfaces
means two IP
Wireless addresses
Interface
Router
Multihoming
2 of 2
Address Assignment #
Appropriate network layer address allocation is key to the efficiency and
scalability of the Internet.
Subnetting #
One solution is that routers should only maintain routes towards blocks of
addresses and not towards individual hosts. For this, blocks of IP addresses
are assigned to ISPs. The ISPs assign sub blocks of the assigned address space
in a hierarchical manner. These sub blocks of IP addresses are called
subnets.
A typical subnet groups all the hosts that are part of the same enterprise. An
enterprise network is usually composed of several LANs interconnected by
routers. A small block of addresses from the Enterprise’s block is usually
assigned to each LAN. An IPv4 address is composed of two parts:
10001010001100000001101000000001
Subnetwork ID Host ID
Address Classes #
When a router needs to forward a packet, it must know the subnet of the
destination address to be able to consult its routing table to forward the
packet. RFC 791 proposed to use the high-order bits of the address to encode
the length of the subnet identifier. This led to the definition of three classes of
addresses.
Addresses
High-order Length of Number of
Class per
bits subnet id networks
network
16,777,216 (
Class A 0 8 bits 128 (27 )
224 )
2,097,152 (
Class C 110 24 bits 21 256 (28 )
2 )
In this classful address scheme, the range range of the IP addresses in each
class are as follows:
Class D IP addresses are used for multicast, whereas class E IP addresses are
reserved and can’t be used on the Internet. So classes A, B, and C are the ones
used for regular purposes.
Subnet Masks #
Every network that falls into one of these classes has a fixed number of bits in
the network part to identify the network itself. The subnet mask ‘masks’ the
network part of the IP address and leaves the host part open. So a subnet
mask of a class C address could be 203.128.22.0, where the first 3 octets
represent the subnet mask and the last octet can be used to identify hosts
within this network. For instance, 203.128.22.10 can be one machine on this
network.
Network Address #
The network address is just the address with all the host bits set to 0. So
203.128.22.0 is actually a network address. It is technically not a ‘functional’
address, it’s just used for forwarding table entries.
Broadcast Address #
The broadcast address of any network is the one where the host bits are all set
to 1. So the broadcast address in our example subnet mask is 203.128.22.255.
It can be used to broadcast a packet to all devices on a network.
Each class has a default mask as follows where the network ID portion has all
1s and the host ID portion has all 0s.
Class Default Subnet Mask
Class A 255.0.0.0
Class B 255.255.0.0
Class C 255.255.255.0
However, these three classes of addresses were not flexible enough. A class A
subnet was too large for most organizations and a class C subnet was too
small.
Variable-Length Subnets #
Quick Quiz! #
1
Suppose a new address class has a 4-bit subnet ID with one higher order
bit out of the 4. How many addresses per network would that entail?
COMPLETED 0%
1 of 2
That’s it for this lesson! We’ll continue with our discussion of IP address
allocation.
The Internet Protocol: IPV4 Address Allocation
We ended the last lesson with a discussion on variable-length subnets. Let's discuss how blocks of addresses are
allocated to organizations, in this lesson.
The first allocation scheme was to allocate class address blocks on a first
come, first served basis.
Evolution of the size of the routing tables on the Internet (Jul 1988-Dec 1992)
1. IP address classes are deprecated. All IP equipment must use and support
variable-length subnets.
190.10.1.0/26
190.10.1.64/26
190.10.1.128/26
Allocates large Allocates smaller
blocks to ISPs blocks to end users
190.10.1.192/26
Why CIDR? #
The main advantage of this hierarchical address block allocation scheme is
that it allows the routers to maintain fewer routes. For example, consider the
address blocks that were allocated to some Belgian universities as shown in
the table below:
Suppose that these universities are all connected to the Internet exclusively
via ISP A. As each university has been allocated a different address block, the
routers of ISP A must announce one route for each university, and all
routers on the Internet must maintain a route towards each university.
In contrast, suppose all the high schools and the government institutions that
are connected to the Internet via ISP A are assigned one block: 193.190.0.0/15
after the introduction of CIDR. Thanks to this, ISP A has one route for all high
schools and government institutions.
The Internet
R2 R1
ISP A ISP B
Organization X
Both routes match IPv4 address 193.192.145.88. Since RFC 1519 when a
router knows several routes towards the same destination address, it
must forward packets along the route with the longest prefix length. In
our example:
This forwarding rule is called the longest prefix match or the more specific
match. All IPv4 routers implement this forwarding rule.
Quick Quiz! #
1 What would the next hop for the IP address 205.135.3.2 be based on the
following routing table?
Destination Next-hop
205.135.0.0/16 R3
205.135.3.0/24 R1
205.0.0.0/8 R2
Any other R4
COMPLETED 0%
1 of 2
Now that we have clari ed the allocation of IPv4 addresses and the utilization of the longest pre x match to
forward IPv4 packets, we can have a more detailed look at IPv4 by starting with the format of the IPv4 packets.
The IPv4 packet format was defined in RFC 791. Apart from a few
clarifications and some backward compatibility changes, the IPv4 packet
format did not change significantly since the publication of RFC 791. All IPv4
packets use a 20-byte header as shown below. Some IPv4 packets contain an
optional header extension that is described later.
Version IHL (4
DS FIeld (8 bits) Total Length (16 bits)
(4 bits) bits)
Flags
Identification (16 bits) Fragment Offset (13 bits)
(3 bits)
A 4 bit version that indicates the version of IP used to build the header.
Using a version field in the header allows the network layer protocol to
evolve.
A 4 bit IP Header Length (IHL) that indicates the length of the IP header
in 32-bit words. This field allows IPv4 to use options if required, but as it
is encoded as a 4 bits field, the IPv4 header cannot be longer than 64
bytes.
An 8 bit DS field that is used for Quality of Service.
A 16 bit length field that indicates the total length of the entire IPv4
packet (header and payload) in bytes. This implies that an IPv4 packet
cannot be longer than 65535 bytes.
Identification every packet has an identification number which is useful
when reassembling and fragmenting a packet.
Flags. There are three flags in IP headers. We’ll discuss their usage in the
next lesson:
Don’t Fragment
More Fragments
Reserved (must be zero)
Fragment Offset: This is useful when reassembling a packet from its
fragments. More details can be found in the next lesson.
Time To Live: This number is decremented at each hop. When it
becomes 0, the packet is considered to have been in the network for too
long and is dropped.
An 8 bits Protocol field that indicates the transport layer protocol that
must process the packet’s payload at the destination. Common values for
this field are 6 for TCP and 17 for UDP.
A 16 bit checksum that protects only the IPv4 header against
transmission errors.
A 32 bit source address field that contains the IPv4 address of the source
host.
A 32 bit destination address field that contains the IPv4 address of the
destination host.
Options this field is not used very often. It’s often used to test out
experimental features.
IP Data: They payload. This payload is not part of the checksum.
The other fields of the IPv4 header are used for very specific purposes. We’ll
look at a few in this lesson.
Consider, for example, the forwarding loop depicted in the figure below.
Destination D uses address 11.0.0.56. If S sends a packet towards this
destination, the packet is forwarded to router B which forwards it to router C
that forwards it back to router A, and so on.
S A B
D
The TTL field of the IPv4 header ensures that even if there are forwarding
loops in the network, packets will not loop forever.
Hosts send their IPv4 packets with a positive TTL (usually 64 or more). When
a router receives an IPv4 packet, it first decrements the TTL by one. If the
TTL becomes 0, the packet is discarded and a message is sent back to the
packet’s source.
Quick Quiz! #
Q
An Internet Protocol with the version number 15 is possible in theory.
COMPLETED 0% 1 of 1
• Why Fragmentation?
• How Fragmentation Works
• How Reassembly Works
• Handling Loss & Duplicates
• Quick Quiz!
Why Fragmentation? #
Although IPv4 packets can be as big as 64kB, few data link layer technologies
can send a 64 KB IPv4 packet inside a frame.
Furthermore, as in the figure below, if the host on the FDDI network abides by
its own data link layer’s maximum packet size of 4478 bytes, the resulting
data link layer frame would violate the maximum frame size of the Ethernet
between routers R1 and R2. Hence, a host may end up sending a packet that is
too large for a data link layer technology used by (an) intermediate router(s).
Ethernet
11.0.0.0/24
Max: 1500 bytes
FDDI
Token Ring
12.0.0.0/24
10.0.0.0/24
Max: 4478
Max: 102
bytes
R1 R2
The need for fragmentation and reassembly
Length
Identification
The flags
More fragments
Don’t Fragment (DF). When this flag is set, it indicates that the
packet cannot be fragmented
Fragment Offset.
A large packet is fragmented into two or more fragments where the size
of all fragments, except the last one, is equal to the Maximum
Transmission Unit of the link used to forward the packet.
The Length field in each fragment indicates the length of the payload and
the header of the fragment.
When the Don’t Fragment (DF) flag is set, it indicates that the packet
cannot be fragmented.
Finally, the More fragments flag is set only in the last fragment of a large
packet.
4. Finally, the packet with the More fragments flag reset allows the
destination to determine the total length of the original unfragmented
packet.
To deal with fragment losses, the reassembly algorithm must bind the
time during which the fragments of a packet are stored in its buffer while
the packet is being reassembled. This can be implemented by starting a timer
when the first fragment of a packet is received. If the packet has not been
reassembled upon expiration of the timer, all fragments are discarded and the
packet is considered to be lost.
Quick Quiz! #
1
Given the sample MTU size of 200 and an IP datagram of size 1999, how
many fragments will be created?
COMPLETED 0%
1 of 9
In this lesson, we'll study ICMP, the network layer's error reporting protocol.
• What Is ICMP?
• ICMP Headers
• ICMP Messages
• Quick Quiz!
What Is ICMP? #
It’s sometimes necessary for intermediate routers or destination hosts to
inform the sender of a packet about any problems that occur while
processing it. In the TCP/IP protocol suite, this reporting is done by the
Internet Control Message Protocol (ICMP). ICMP is defined in RFC 792.
ICMP Headers #
ICMP messages are carried as the payload of IP packets (the protocol value
reserved for ICMP is 1). An ICMP message is composed of an 8-byte header
and a variable-length payload that usually contains the first bytes of the
packet that triggered the transmission of the ICMP message.
Version IHL (4
DS FIeld (8 bits) Total Length (16 bits)
(4 bits) bits)
Flags
Identification (16 bits) Fragment Offset (13 bits)
(3 bits)
IP
Time To Live (8 bits) Protocol (8 bits) Header Checksum (16 bits)
Header
ICMP
Header
Data
The Type and Code fields indicate the type of problem that was detected
by the sender of the ICMP message.
The Data field contains additional information for some ICMP messages.
ICMP Messages #
The main types of ICMP messages are:
3. Redirect: This ICMP message can be sent when there are two routers on
the same LAN.
Example: Consider a LAN with one host and two routers: R1 and R2.
Assume that R1 is also connected to subnet 130.104.0.0/16 while R2 is
connected to subnet 138.48.0.0/16. If a host on the LAN sends a
packet towards 130.104.1.1 to R2, R2 needs to forward the packet
again on the LAN to reach R1. This is not optimal, since the packet is
sent twice on the same LAN. In this case, R2 could send an ICMP
Redirect message to the host to inform it that it should have sent the
packet directly to R1. This allows the host to send the other packets
to 130.104.1.1 directly via R1.
LAN
R1 R2
130.104.0.0/16 138.48.0.0/16
1 of 3
Message to
LAN 130.104.1.1
R1 R2
130.104.0.0/16 138.48.0.0/16
2 of 3
ICMP redirect:
send your msg
LAN to R1 instead!
R1 R2
130.104.0.0/16 138.48.0.0/16
3 of 3
Source quench: It was envisioned that a router would send this ICMP
message when it had to discard packets due to congestion. However,
sending ICMP messages in case of congestion was not the best way to
5. Time Exceeded: There are two types of Time Exceeded ICMP messages.
6. Echo request & Echo reply: These ICMP messages are used by the
ping(8) network debugging software. Let’s have a look at ping next.
Quick Quiz! #
1
An ICMP destination unreachable message is returned when ______.
COMPLETED 0%
1 of 3
In the next lesson, we’ll send real ICMP messages with command-line tools
like ping and traceroute !
Exercise: Sending ICMP Messages With Ping &
Traceroute
In this lesson, we'll look at real live ICMP packets with ping and traceroute!
• Ping
• Traceroute
• How It Works
• Usage
• Sample Output #1
• Sample Output #2
Ping #
When a client sends ICMP echo messages ( ping ), it sets a certain value in the
TTL field and starts a timer. An echo server software running on the
destination returns an ICMP echo reply message. Since the TTL value is
decremented at each hop, the ping client can know the number of hops
traversed by the packets. Also, when it receives the echo reply, it stops the
timer and calculates the round trip time. There is a maximum value for the
round trip time and when it’s exceeded, the echo message is declared lost.
ping is also often used by network operators to verify that a given IP address
is reachable.
Terminal
Traceroute #
Another very useful debugging tool is traceroute . The traceroute man page
describes this tool as “print the route packets take to network host."
How It Works #
Traceroute uses the TTL exceeded ICMP messages to discover the intermediate
routers on the path towards a destination. The principle behind traceroute is
very simple.
Usage #
Terminal
Sample Output #1 #
Here’s what some simple traceroute output may look like. Notice that the
output is organized in rows and columns where each hop is represented by
one row. Here’s what each column means:
Hop
IP Address RTT 1 RTT 2 RTT 3
Number
108.170.244
2 157.181 157.195 157.714
.16
141.101.73.
3 11.648 11.650 11.721
2
The traceroute output above shows a 3-hop path (in the instance of writing
this course - the number of hops and their IP addresses may be different now)
between a host at Educative and one IETF’s servers. For each hop, traceroute
provides the IPv4 address of the router that sent the ICMP message and
exactly three measured round-trip-times between the source and this router.
Sample Output #2 #
You may also get something slightly more complicated like the above. Here,
there is more than one next-hop each packet can take. For example, the first
hop shows 2 different IP addresses:
So there are multiple routes towards the destination and probes are sent to
each possible next hop.
In the next lesson, we’ll study IPv4 Data Link Layer Address Resolution
Address Resolution Protocol (ARP)
In this lesson, we'll discuss how data link layer addresses are resolved in an IPv4 network
• Introduction
• How It Works
• Quick Quiz!
Introduction #
While end hosts may use IP addresses to communicate with each other, the
underlying data link layer uses its own naming schemes. So, end host
interfaces have unique data link layer addresses. In order to get data to a host,
a mechanism for converting IP addresses to the underlying data link layer
address is needed. This entails that all sending hosts must know the data
link-layer address of their destination hosts in order to send them a packet.
For example, the figure below shows four hosts attached to the same LAN
configured with IPv4 addresses in the 10.0.1.0/24 subnet and data link layer
addresses represented as a single character. In this network, if host
10.0.1.22/24 wants to send an IPv4 packet to the host with address 10.0.1.8, it
must know that the data link layer address of this host is C.
A simple LAN
While manual configuration of the data link address of each host is possible in
small networks such as the one above, it does not scale. Hence, IPv4 hosts and
routers must be able to automatically obtain the data link layer address
corresponding to any IPv4 address on the same LAN. This is the objective of
the Address Resolution Protocol (ARP) defined in RFC 826. ARP is a data link
layer protocol and relies on the ability of the data link layer service to
broadcast a frame to all devices attached to the same LAN.
How It Works #
The easiest way to understand the operation of ARP is to consider the simple
network shown above and:
Each IPv4 host maintains an ARP cache that contains all mappings
between IPv4 addresses and data link layer addresses that it knows.
The sender, 10.0.1.22, first consults its ARP cache. As the cache does not
contain the requested mapping, the sender sends a broadcast ARP
query frame on the LAN.
This broadcast frame is received by all devices on the LAN. Every host
upon receiving the ARP query inserts an entry for the sender’s IP address
and data link layer address into their ARP cache.
Every host on the LAN segment receives the ARP query however, only
the host that owns the requested IPv4 address replies by returning a
unicast ARP reply frame with the requested mapping.
Upon reception of this reply, the sender updates its ARP cache and
sends the IPv4 packet by using the data link layer service.
Note that to deal with devices that move or whose addresses are reconfigured,
most ARP implementations remove the cache entries that have not been used
for a few minutes. Some implementations also revalidate ARP cache entries
from time to time by sending ARP queries.
Quick Quiz! #
1
Suppose a host, A, sends an ARP request to a host C. Immediately after
that, another host, D, wants to send a packet to host A. Will it send an
ARP request assuming that there has not been any traffic on the LAN yet?
COMPLETED 0%
1 of 2
In the next lesson, we’ll look at the dynamic host configuration protocol.
Dynamic Host Configuration Protocol (DHCP)
In this lesson, we'll discuss how IP addresses are assigned to devices on the network.
• Introduction
• How It Works
• Quick Quiz!
Introduction #
In the early days of the Internet, IP addresses were manually configured on
both hosts and routers and almost never changed. However, this manual
configuration can be complex and often causes errors that can be difficult to
debug.
To ease the attachment of hosts to subnets, most networks now support the
Dynamic Host Configuration Protocol (DHCP) RFC 2131. DHCP allows a host
to automatically retrieve its assigned IPv4 address. A DHCP client actually can
retrieve other network parameters too, including subnet mask, default
gateway and DNS server addresses from the DHCP server.
How It Works #
A DHCP server is associated with the subnet to which it is connected.
Routers do not forward DHCP traffic from one subnet to another.
The DHCP request may contain options such as the data link layer
address of the host.
The server captures the DHCP request and selects an unassigned address
in its address pool.
It then sends the assigned IPv4 address in a DHCP reply message which
contains:
The data link layer address of the host and additional information
such as
The subnet mask of the IPv4 address
The address of the default router or the address of the DNS
resolver.
The DHCP reply also specifies the lifetime of the address allocation.
This forces the host to renew its address allocation once it expires.
This DHCP reply message is sent in an IPv4 packet whose source and
destination addresses are respectively the IPv4 address of the DHCP
server and the 255.255.255.255 broadcast address.
Have a look at the following slides to see how an IP address is retrieved from a
DHCP server. Note however, that the DHCP request response is an abstraction
that we have created here. The DHCP protocol specifies several messages and
their formats to do its job. For example, a DHCP server makes an offer for
address assignment to a client, which the client may or may not accept. So,
there are “Offers," “Acks” and “Nacks” etc.
New joiner
DHCP Server
DHCP In Action
1 of 4
New joiner
DHCP Server
S
Destina ource: 0.0.0.0
tion: 25
5.255.2
55.255
New joiner
DHCP Server
S
Destina ource: 0.0.0.0
tion: 25
5.255.2
55.255
DHCP In Action
3 of 4
New joiner
DHCP Server
S
Destina ource: 0.0.0.0
tion: 25
5.255.2
55.255
r
erve
CP s
of DH .255
addr
ess .255
ce: IP .255
Sour t io n: 255 : a.b.c.e
ina ss
Dest ew addre e: x
n
Lifetim
DHCP In Action
4 of 4
Quick Quiz! #
1
What are the responsibilities of the DHCP server?
COMPLETED 0%
1 of 3
In the next lesson, we’ll put everything we’ve learned together to see how an
IPv4 packet travels over the Internet.
IPv4 in Practice: The Life of a Packet
In this lesson, we'll consolidate everything we have learned about the network layer so far by tracing the journey
of a packet.
• Sending a Packet
• Receiving A Packet
• If ICMP is Received
• How Routers Handle Packets
• Quick Quiz!
The simplest case is when a host needs to send a transport layer segment in an
IPv4 packet. In order to do so, it performs two operations.
An IPv4 host with n data link layer interfaces manage n + 1 IPv4 addresses:
One A.B.C.D/p IPv4 address assigned to each of its n data link layer
interfaces.
The host maintains a forwarding table that contains one entry for its
loopback address and one entry for each subnet identifier assigned to its
interfaces.
Furthermore, the host usually uses one of its interfaces as the default
interface when sending packets that are not addressed to a directly
connected destination. This is represented by the default route: 0.0.0.0/0
that is associated with one interface.
Sending a Packet #
When a transport protocol running on the host requests the transmission
of a segment, it usually provides the IPv4 destination address to the IPv4
layer in addition to the segment.
The IPv4 implementation first performs a longest prefix match with the
destination address in its forwarding table. The lookup returns the
identification of the interface that must be used to send the packet.
The host can then create the IPv4 packet that contains the segment! The
source IPv4 address of the packet is the IPv4 address of the host on the
interface returned by the longest prefix match.
The Protocol field of the packet is set to the identification of the local
transport protocol which created the segment.
The TTL field of the packet is set to the default TTL used by the host.
The host must now choose the packet’s Identification. This Identification
is important if the packet becomes fragmented in the network, as it
ensures that the destination is able to reassemble the received fragments.
Ideally, a sending host should never send a packet twice with the
same identification to the same destination host, in order to ensure
that all fragments are correctly reassembled by the destination.
Unfortunately, a 16-bit Identification field and an expected MSL of 2
minutes, and maximum packet size of 65535 implies that the
maximum bandwidth to a given destination is limited to roughly 286
Mbps. Here’s the derivation:
However, with a more realistic 1500 bytes MTU, that bandwidth drops to 6.4
Mbps to make fragmentation possible. This is very low and is another reason
why hosts are highly encouraged to avoid fragmentation. If despite all of this,
the MTU of the outgoing interface is smaller than the packet’s length, the
packet is fragmented.
Receiving A Packet #
When a host receives an IPv4 packet destined to itself, there are several
operations that it must perform.
Then, it must check whether the packet has been fragmented. If yes, the
packet is passed to the reassembly algorithm described earlier.
Otherwise, the packet must be passed to the upper layer. This is done by
looking at the Protocol field (6 for TCP and 17 for UDP).
Pass to upper
layer
Correct
Protocol exists
Receiving a packet
If ICMP is Received #
If the received packet contains an ICMP message (with the protocol field set to
1), the processing is more complex.
The other types of ICMP messages, except for ICMP Echo Response,
indicate an error that was caused by a previously transmitted packet.
These ICMP messages are usually forwarded to the transport protocol
that sent the erroneous packet. This can be done by inspecting the
contents of the ICMP message that includes the header and the first 64
bits of the erroneous packet.
If the IP packet did not contain options, which is the case for most IPv4
packets, the transport protocol can find in the first 32 bits of the transport
header the source and destination ports to determine the affected
transport flow. This is important for Path MTU discovery for example.
Then, the router performs a longest prefix match for the packet’s
destination address in its forwarding table.
Before forwarding the packet over this interface, the router must first
compare the length of the packet with the MTU of the outgoing interface.
Quick Quiz! #
• Why IPV6?
• IPv6 Features
• Pros
• Cons
• Textual representation of IPv6 addresses
• Quick Quiz!
Why IPV6? #
IPv4 was initially designed for a research network that would
interconnect some research labs and universities. For this purpose, 32 bit
addresses, i.e., 232 = 4, 294, 967, 296 ≈ 4.3 billion addresses seemed
sufficient. Also, 32 bits was an incredibly convenient address size for
software-based routers.
However, the popularity of the Internet, i.e., the number of smartphones and
Internet of Things devices, was not anticipated. We’ve made do with 4.3
billion addresses so far by reusing them and with NAT boxes. Nonetheless, we
are running out of addresses. Hence, IPv6 was designed to tackle these
limitations of IPv4.
IPv6 Features #
IPv6 has some distinguishing pros and cons.
Pros #
Simplified Header: All IPv4 options are moved to the end of the IPv6
header. IPv6 header is twice as large as IPv4 headers but only because
IPv6 addresses are four times longer.
Cons #
IPv6 is a complete redesign over IPv4 and hence is not backward
compatible. This means that devices configured over IPv4 can NOT
access websites on servers configured with IPv6!
The preferred format for writing IPv6 addresses is x:x:x:x:x:x:x:x, where the
x‘s are hexadecimal digits representing the eight 16-bit parts of the address.
Here are a few examples of IPv6 addresses:
ABCD:EF01:2345:6789:ABCD:EF01:2345:6789
2001:DB8:0:0:8:800:200C:417A
FE80:0:0:0:219:E3FF:FED7:1204
IPv6 addresses often contain a long sequence of bits set to 0. In this case, a
compact notation has been defined. With this notation, :: is used to indicate
one or more groups of 16 bit blocks containing only bits set to 0. For example:
2001:DB8:0:0:8:800:200C:417A is represented as
2001:DB8::8:800:200C:417A
FF01:0:0:0:0:0:0:101 is represented as FF01::101
2001:0DB8:0000:CD30:0000:0000:0000:0000/60
2001:0DB8::CD30:0:0:0:0/60
2001:0DB8:0:CD30::/60
Here’s a drawing that represents how an IPv6 address is written in text form.
FE80:CD00:0000:0CDE:1257:0000:211E:729C
Quick Quiz! #
1
What’s a compact way of representing the IPv6 address:
FE00:0:0:0:219:A34F:F3D7:1204?
COMPLETED 0%
1 of 3
In the next lesson, we’ll study IPv6 address types!
IPv6 Features
Unicast #
As with IPv4, an IPv6 unicast address is used to identify one data link layer
interface on a host. If a host has several data link layer interfaces (such as an
Ethernet interface and a WiFi interface), then it needs several IPv6 addresses.
An IPv6 unicast address is composed of three parts:
As described in the previous lesson, IPv6 addresses are divided into eight 16-
bit groups separated by colons. Each group is represented by four
hexadecimal digits.
128-bits
N bits M bits 128-N-M bits
These ULA addresses play a similar role as the private IPv4 addresses defined
in RFC 1918. However, the size of the FC00::/7 address block allows ULA to be
much more flexible than private IPv4 addresses. Furthermore, the IETF has
reserved some IPv6 addresses for a special usage. The two most important
ones are:
Each host can compute its own link-local address by concatenating the
FE80::/64 prefix with the 64 bits identifier of its interface.
Link-local addresses can be used when hosts that are attached to the
same link (or local area network) need to exchange packets.
They are used notably for address discovery and auto-configuration
purposes.
Their usage is restricted to each link, and a router cannot forward a
packet whose source or destination address is a link-local address.
Link-local addresses have also been defined for IPv4 RFC 3927. However,
the IPv4 link-local addresses are only used when a host cannot obtain a
regular IPv4 address, e.g., on an isolated LAN.
128 bits
Anycast Addresses #
RFC 4291 defines a special type of IPv6 anycast address. On a subnetwork
having prefix p/n, the IPv6 address whose 128-n low-order bits are set to 0 is
the anycast address that corresponds to all routers inside this subnet-work.
This anycast address can be used by hosts to quickly send a packet to any of
the routers inside their own subnetwork.
Multicast Addresses #
Finally, RFC 4291 defines the structure of the IPv6 multicast addresses. This
structure is depicted in the figure below.
The lower order 112 bits of an IPv6 multicast address are the group’s
identifier. The higher-order bits are used as a marker to distinguish multicast
addresses from unicast addresses.
Notably, the 4 bits flag field indicates whether the address is temporary
or permanent.
128 bits
8 bits 4 bits 4 bits 112 bits
Among these addresses, some are well known. For example, all end-system
automatically belong to the FF02::1 multicast group while all routers
automatically belong to the FF02::2 multicast group.
Quick Quiz! #
1 In an IPv6 unicast address, if 12 bits are used for the global routing
prefix and 64 bits are used for the interface ID, how many are used for
the subnet ID?
COMPLETED 0%
1 of 3
• Introduction
• Firewall Interfaces
• Firewall Filters
• Stateless Vs. Stateful Firewalls
• Host-based Vs. Network-based Firewalls
• Quick Quiz!
Introduction #
When the TCP/IP architecture and the IP protocol were defined, two types of
devices were considered in the network layer:
However, in today’s Internet, there exist devices called middleboxes that are
not strictly routers but which process, sometimes modify, and forward IP
packets (RFC 3234). Some middleboxes only operate in the network layer, but
most middleboxes are able to analyze the payload of the received packets and
extract the transport header, and in some cases the application layer headers.
Over the next couple of lessons, we’ll briefly describe two types of
middleboxes: firewalls and network address translation (NAT) devices.
Firewalls
Why Firewalls?
When the Internet was only a research
network interconnecting research labs,
security was not a concern. However, as the
Internet grew in popularity, security
concerns grew.
🔎 Did You Know? The term firewall originates from a special wall
used to confine the spread of fire in a building. It was also used to refer
to a metallic wall between the engine compartment and the passenger
area in a car. The purpose of this metallic wall is to prevent the spread of
a fire in the engine compartment into the passenger area.
Firewall Interfaces #
These security problems convinced the industry that their networks should be
protected by special devices the way security guards and fences are used to
protect buildings. These special devices came to be called firewalls. A typical
firewall has two interfaces:
Firewall Filters #
The first firewalls included configurable packet filters. A packet filter is a set
of rules defining the security policy of a network. In practice, these rules are
based on the values of fields in the IP or transport layer headers. Any field of
the IP or transport header can be used in a firewall rule, but the most
common ones are:
Filter on the source address. For example, a company may decide to
discard all packets received from one of its competitors in certain
portions of the network while maintaining access to public resources.
Another example of source based filtering is black lists. Any packets
from an IP on the black list will be discarded. IPs known for their use by
spammers, for instance, are blacklisted by many networks.
Filter on the destination address. For example, the hosts of the research
lab of a company may receive packets from the global Internet, but not
the hosts of the financial department.
Filter on the TCP or UDP port numbers. For example, only the DNS
server of a company should receive UDP segments whose destination
port is set to 53, or only the official SMTP servers of the company can
send TCP segments whose source ports are set to 25.
However, a stateful firewall, on the other hand, sees the first packet in a flow
that is allowed by the configured security rules it creates a session state for it.
All subsequent packets belonging to that flow are allowed to go through. This
filtering is more efficient compared to stateless firewalls that have to apply
their rules to each and every packet. The flip side is the maintenance of state,
which needs to be controlled.
Host-based Vs. Network-based Firewalls #
A host based firewalls, however, are software based and are deployed on end-
systems. They are generally not easy to scale and require maintenance.
Quick Quiz! #
COMPLETED 0%
1 of 2
• Introduction
• Broadband Access Routers
• Enterprise Networks
• Sending a Message over a NAT
• Sending a Message
• Receiving a Message
• Disadvantages of NATs
• Quick Quiz!
Introduction #
Network Address Translation (NAT) was proposed as a short term solution to
deal with the expected shortage of IPv4 addresses in the late 1980s to early
1990s. Combined with CIDR, NAT helped to significantly slow down the
consumption of IPv4 addresses. A NAT is a middlebox that interconnects two
networks that are using IPv4 addresses from different addressing spaces.
Usually, one of these addressing spaces is the public Internet while the other is
using a private IPv4 address. Unlike a router, when a NAT box forwards
traffic, it modifies the IP addresses in the IP header, as will be described
shortly.
192.168.10.11
192.168.10.10
Enterprise Networks #
The second type of deployment is in enterprise networks. In this case, the NAT
functionality is installed on a border router of the enterprise. A private IPv4
address is assigned to each enterprise host while the border router manages a
pool containing several public IPv4 addresses.
Sending a Message #
When the NAT receives the first packet from source S in the internal
network which is destined to the public Internet, it creates a mapping
between internal address S and the first address of its pool of public
addresses (P1).
Then, it translates the received packet so that it can be sent to the public
Internet. This translation is performed as followed:
Receiving a Message #
This works as long as the pool of public IP addresses of the NAT does not
become empty. In this case, a mapping must be removed from the mapping
table to allow a packet from a new host to be translated. This garbage
collection can be implemented by adding to each entry in the mapping table
a timestamp that contains the last utilization time of a mapping entry. This
timestamp is updated each time the corresponding entry is used. Then, the
garbage collection algorithm can remove the oldest mapping entry in the
table.
Disadvantages of NATs #
NAT allows many hosts to share one or a few public IPv4 addresses. However,
using NAT has two important drawbacks.
1. First, it’s not easily possible for external hosts to open TCP connections
with hosts that are behind a NAT. Some consider this to be a benefit from
a security perspective. However, a NAT should not be confused with a
firewall, as there are some techniques to traverse NATs.
1
Suppose several end systems including a web server is behind a NAT.
Will external clients be able to initiate connections with it?
COMPLETED 0%
1 of 3
In this lesson, we'll look at an introduction to intradomain and interdomain routing algorithms.
• Introduction
• Intradomain Vs. Interdomain Routing
• Quick Quiz!
Introduction #
If every router on the Internet had to manage routing entries for the entire
Internet, then we would need very high-end and high performing routers.
Also, the scale of exchanging routing information would be humongous.
Instead, the Internet consists of separate administrative domains. Each
domain is run and managed by an independent authority.
As of this writing, the Internet is composed of more than 30, 000 such
different domains and this number is still growing.
Quick Quiz! #
1
Why are domains needed?
COMPLETED 0%
1 of 3
In the next lesson, we’ll look at an intradomain routing algorithm called OSFP.
Intradomain Routing: OSFP
• Intradomain Routing
• OSPF
• Hierarchical Routing
• Areas
• The Backbone Area
Intradomain Routing #
Intradomain routing protocols have two objectives:
OSPF #
Open Shortest Path First (OSPF), defined in RFC 2328, is one of the link-state
routing protocols that has been standardized by the IETF.
Hierarchical Routing #
aggregated information about the topology of the other areas and their
interconnections.
Areas #
For example, the network shown in the figure below has been divided into
three areas:
Some routers in the backbone area are connected to area border routers
from a different area. On the other hand, some routers in the backbone
area may not be connected to routers in any other area. In the following
figure, RA, RB and RC fall in the former category while RD falls in the
latter.
All area border routers not belonging to the backbone area must be
physically connected to an area border router in the backbone area!
📝 Note: It’s possible to establish adjacency between area border
routers that are not physically connected, but this is not preferred.
Sometimes it might not be possible to connect two routers physically. A
certain area border router could be miles and miles away from anything
in the backbone area and not connected physically. In that case, its
adjacency with the backbone area router can be established by
configuration.
R1 R5
Area 1
R3 R4
RA
RD Area 0
RB RC
R7 R8
Area 2
R9 R10
OSPF areas
10.0.1.0/24
10.0.0.0/24 R5
R4
AREA 1
RA
R7 R8
192.168.1.0/24 192.168.10.0/24
R9 R10
AREA 2
1 of 8
10.0.1.0/24
10.0.0.0/24 R5
R4
AREA 1
RA
R7 R8
192.168.1.0/24 192.168.10.0/24
R9 R10
AREA 2
2 of 8
10.0.1.0/24
10.0.0.0/24 R5
R4
AREA 1
R7 R8
192.168.1.0/24 192.168.10.0/24
R9 R10
AREA 2
3 of 8
10.0.1.0/24
10.0.0.0/24 R5
R4
AREA 1
R7 R8
192.168.1.0/24 192.168.10.0/24
R9 R10
AREA 2
4 of 8
10.0.1.0/24
10.0.0.0/24 R5
R4
AREA 1
RA
R7 R8
192.168.1.0/24 192.168.10.0/24
R9 R10
AREA 2
5 of 8
10.0.1.0/24
10.0.0.0/24 R5
R4
AREA 1
On the other hand, consider the prefixes
10.0.0.0/24 and RA10.0.1.0/24 that are inside
area 1. Router RA is the only area border
router that is attached to this area. This
router can create two different network
RA summary advertisements:
- 10.0.0.0/24 at a distance of 1 and
RB RC 10.0.1.0/24 at a distance of 2 from RA
AREA 0 OR
- 10.0.0.0/23 at a distance of 1 from RA
R7 R8
192.168.1.0/24 192.168.10.0/24
R9 R10
AREA 2
6 of 8
10.0.1.0/24
10.0.0.0/24 R5
R4
AREA 1
RA
The first summary advertisement provides
precise information about the distance
used to reach each prefix. However, all
RA
routers in the network have to maintain a
route towards 10.0.0.0/24 and a route
towards 10.0.1.0/24 that are both via router
RB RC
RA.
AREA 0
R7 R8
192.168.1.0/24 192.168.10.0/24
R9 R10
AREA 2
7 of 8
10.0.1.0/24
10.0.0.0/24 R5
R4
AREA 1
RA
The second advertisement would improve
the scalability of OSPF by reducing the
number of routes that are advertised
RA across area boundaries. However, in
practice, this requires manual configuration
RB RC on the border routers.
AREA 0
R7 R8
192.168.1.0/24 192.168.10.0/24
R9 R10
AREA 2
8 of 8
Coming up next, we’ll get an introduction to interdomain routing and the only
interdomain routing protocol in use today: BGP
Interdomain Routing: Border Gateway Protocol
• Introduction
• The Role of BGP
• Advertising BGP Route Information
• Internal Routers & Gateway Routers
• Propagating Information
• Quick Quiz!
Introduction #
Just the way packets need to move around within a domain or autonomous
system, packets need to move across them too. To enable all domains to
communicate with each other, they need to be talking in the same language,
in other words, they need to be using the same protocol. Hence, there exists
just one interdomain routing protocol and it’s called the Border Gateway
Protocol (BGP). We’ll spend a bit of time on this one as it is essentially the
glue that holds the Internet together!
R6
R5 R7
R2
R10
R8
R1 R3
AS2 R9 R11
R4
R12
AS1
AS3
R6
R5 R7
R2
R10
R8
R1 R3
AS2 R9 R11
Gateway
Routers
R4
R12
AS1
AS3
The BGP routers exchange routing information with each other over this
TCP connection. The connection and the information exchange is known
as a BGP session.
R6
R5 R7
R2
R10
R8
R1 R3
AS2 R9 R11
R4
R12
eBGP
AS1 iBGP AS3
Propagating Information #
In order to propagate BGP information, both iBGP and eBGP links are used. In
the example above, consider router R12 advertising a prefix P 12 to AS1 and
AS2.
The gateway router R9 will first send an eBGP message like "AS3 P 12"
to the gateway router R7. This message is in the form “via AS.” So it is
essentially saying "P 12 is reachable via AS3."
The gateway router R7 then will send the iBGP message "AS3 P 12" to all
of the other routers in AS2. This includes the gateway router R5.
The gateway router R5 then sends the eBGP message “AS2 AS3 P 12" to
the gateway router R3. This message says "P 12 is reachable via AS2 then
through AS3”.
Finally, the gateway router R3 uses an iBGP session to send the message
"AS2 AS3 P 12" to all the routers in AS1.
Each router in the network now knows about the presence of P 12 and
how to reach it.
Lastly, only a few routers in a network might speak BGP. Others can be
dedicated to intra-domain routing. Routes learned through the intradomain
routing protocol such as OSPF are injected into the BGP process on a BGP
speaking router, which passes it on. Also unlike OSPF border routers, BGP
neighbors don’t need to be physically connected directly.
Quick Quiz! #
1
What would be the AS path to the prefix P10 in AS1?
COMPLETED 0%
1 of 2
In real life, however, many different paths exist from each router to all other
routers. How do we pick the best one? We’ll look at how BGP does that in the
next lesson!
Border Gateway Protocol: Determining the Best Routes
We left the last lesson off at the question: how do routers pick a path to reach a speci c destination out of a given
number of paths?
• Terminology
• Hot Potato Routing
• Route Selection Algorithm
• Quick Quiz!
Terminology #
Before addressing this question, we need to familiarize ourselves with some
more BGP jargon.
BGP routes are written as a combination of many things out of which we will
consider three:
1. NEXT-HOP
2. AS-PATH
3. Destination Prefix
1. The router will calculate the cost to reach the NEXT-HOP router of each
given path. The cost can be calculated using intradomain routing
protocols like OSPF.
2. The path with the NEXT-HOP router that is least costly to send a packet to
is chosen.
Essentially, a router has learned routes to a prefix from multiple BGP border
routers. It consults its intra domain routing information to determine the
border router that is reachable with the least cost. This is akin to getting rid of
the packet as quickly as possible, without paying any attention to the fact that
some other border router might offer a lower overall distance to the
destination.
Let’s take a modification of our previous example to illustrate how this works
in practice.
R6
R5 R7
R2
R10
R8
R1 R3
AS2 R9 R11
R4 y
R12
AS1
AS3
R5 R7
R2
R10
R8
R1 R3
AS2 R9 R11
R4 y
R12
AS1
AS3
Suppose we are trying to reach prefix y
from router R1 in the given topology
R6
R5 R7
R2
R10
R8
R1 R3
AS2 R9 R11
R4 y
R12
If cost were the number of links
AS1
NEXT-HOP traversed, the least cost to reach NEXT- AS3
HOP router R4 from R1 would be 1
NEXT-HOP
R5 R7
R2
R10
R8
R1 R3
AS2 R9 R11
R4 y
R12
R6
R5 R7
R2
R10
R8
R1 R3
AS2 R9 R11
R4 y
R12
Hence this path is
AS1 chosen
AS3
Note that this is a selfish algorithm, as it only cares about reducing the
immediate cost and does not consider the cost of the path outside of its
domain.
Furthermore, two routers within the same domain may select two different
paths to reach the same destination. An example would be R2 that would
select the path via AS2 to the prefix y , and R1 that would undeniably bypass
AS2 and reach y via AS3.
BGP does use hot potato routing in some cases, but that is based on a more
sophisticated algorithm described as follows.
To pick the best path out of two or more paths to the same prefix, BGP uses the
following rules to eliminate each path until the best one is left:
R6
R5 R7
R2
R10
R8
R1 R3
AS2 R9 R11
R4
R12
AS1
AS3
R5 R7
R2
R10
R8
R1 R3
AS2 R9 R11
y
R4
R12
AS1
Router R1 has two possible paths through which it can AS3
reach prefix y. One through AS2 and the other through AS3.
R6
R5 R7
R2
R10
200
R8
R1 R3
AS2 R9 R11
100
R4 y
R12
To choose the best one, it'll compare their local preference values and
AS1
pick the one with the greater value. Let's assume the blue path has a AS3
local preference value of 100 while the black one has a value of 200.
R5 R7
R2
R10
200
R8
R1 R3
AS2 R9 R11
100
R4 y
R12
AS1
AS3
Then naturally, the path through AS2 would be
preferred.
2. If several routes have the same highest value, then the one with the
shortest AS-PATH is selected.
Example:
R6
R5 R7
R2
R10
R8
R1 R3
AS2 R9 R11
R4
R12
AS1
AS3
R5 R7
R2
R10
R8
R1 R3
AS2 R9 R11
y
R4
R12
AS1
Router R1 has two possible paths through which it can AS3
reach prefix y. One through AS2 and the other through AS3.
R6
R5 R7
R2
R10
100
R8
R1 R3
AS2 R9 R11
100
R4 y
R12
To choose the best one, it'll compare their local preference values and
AS1
pick the one with the greater value. Let's assume that both paths have a AS3
local preference value of 100.
R5 R7
R2
R10
100
R8
R1 R3
AS2 R9 R11
100
y
R4
R12
AS1 Hence, the path with the shortest AS-PATH, i.e., the one through AS3 is
chosen AS3
R6
R5 R7
R2
R10
R8
R1 R3
AS2 R9 R11
R4
R12
AS1
AS3
BGP route selection
1 of 3
R6
R5 R7
R2
R10
100
R8
R1 R3
AS2 R9 R11
100
y
R4
R12
R6
R5 R7
R2
R10
100
R8
R1 R3
AS2 R9 R11
100
R4 y
R12
6. If more than one route still remains, routes learned from the router with
the lowest router ID is preferred.
Example
R6
R5 R7
R2
R10
R8
R1 R3
AS2 R9 R11
R4
R12
AS1
AS3
R6
R5 R7
R2
R10
100
R8
R1 R3
AS2 R9 R11
100
R4
R12
AS1 R1 has two paths to get to R12. One via R2 and the
AS3
other via R4. Both have the same local preference
number, the same number of AS-HOPS, and the
same cost to reach the border router.
R5 R7
R2
R10
100
R8
R1 R3
AS2 R9 R11
100
R4
R12
AS1
AS3
Since R2 has a lower ID, the path through it is chosen.
Quick Quiz! #
1 Hot potato routing always results in the overall least cost path being
chosen.
COMPLETED 0%
1 of 2
This concludes our discussion of the network layer! We’ll get into the link
layer next.
What is The Data Link Layer?
This lesson will give us a quick introduction to the data link layer!
Application
Transport
Network
1. The simplest data link layer type is one that has only two
communicating systems connected directly through the physical
layer also known as the point-to-point data link layer. This type of layer
can either provide an unreliable service or a reliable service. The
unreliable service is frequently used above physical layers (e.g., optical
fiber, twisted pairs) that have a low bit error ratio, while reliability
mechanisms are often used in wireless networks to recover locally from
transmission errors.
2. The second type of data link layer is the one used in Local Area Networks
(LAN) called Broadcast multi-access. Both end-systems and routers can
be connected to a LAN.
Most LANs also support special broadcast and multicast data link
layer addresses. A frame sent to the broadcast address of the LAN is
delivered to all communicating devices that are attached to the LAN.
The multicast addresses are used to send a frame to one specific
group.
3. The third type of data link layer is used in Non-Broadcast Multi-Access
(NBMA) networks. These networks are used to interconnect devices like
a LAN. All devices attached to an NBMA network are identified by a
unique data link layer address.
What To Expect #
This chapter is organized as follows.
1. We will first discuss the principles of the data link layer as well as the
services that it uses from the physical layer.
3. Finally, we’ll discuss Ethernet, the ubiquitous data link layer type.
Quick Quiz! #
1
What is the name of a data link layer message?
COMPLETED 0%
1 of 2
We'll discuss some key principles of the data link layer in this lesson.
Most data link layer technologies impose limitations on the size of the
frames:
The physical layer may change the value of a bit being transmitted due
to any reason, e.g., electromagnetic interferences.
The Physical layer may deliver more bits to the receiver than the bits
sent by the sender.
The Physical layer may deliver fewer bits to the receiver than the bits
sent by the sender.
1. First, some physical layer implementations can’t remain idle and always
need to transmit bits.
2. Second, inserting an idle period between frames decreases the
maximum bandwidth that can be achieved by the data link layer.
1. Bit stuffing
2. Character stuffing.
Bit Stuf ng #
Bit stuffing reserves a special bit pattern, for example, the 01111110 bit
string as the frame boundary marker. However, if the same bit pattern occurs
in the data link layer payload, it must be modified before being sent,
otherwise, the receiving data link layer entity will detect it as a start or end of
frame.
Assuming that the 01111110 pattern is used as the frame delimiter, a frame is
sent as follows:
2. Then, it sends all the bits of the frame and inserts an additional bit set to
0 after each sequence of five consecutive 1 bits. This ensures that the sent
frame never contains a sequence of six consecutive bits set to 1. As a
consequence, the marker pattern cannot appear inside the frame sent.
The table below illustrates the application of bit stuffing to some frames.
011111100001001001001001001000
0001001001001001001000011
01101111110
01111110 0111111001111101001111110
Example #
1. The sender will first send the 01111110 marker followed by 011011111.
2. After these five consecutive bits set to 1, it inserts a bit set to 0 followed
by 11111.
4. A new 0 is inserted followed by the end of the frame 110010 and the
01111110 marker.
0110111111111111111110010
01111110+0110111111111111111110010
Starting segment added
01111110011011111+0+1111111111110010
0 inserted because 5 1s
are encountered
01111110011011111011111+0+11111110010
0 inserted because 5 1s
are encountered
011111100110111110111110111110110010+01111110
Ending sequence
appended
01111110011011111011111011111011001001111110
Final sequence
0111 11 00
0111 11 01
0111 11 10
0111 11 11
0111 11 0 00
0111 11 0 01
0111 11 0 10
0111 11 0 11 So, in any case, the receiver can only expect 01111110 at
the beginning and end of frame. If it receives five consecutive 1s,
followed by a 0, it removes the 0 as redundancy. If it receives six
consecutive 1s, there must’ve been an error.
Character Stuf ng #
If the character DLE appears in the payload, the data link layer entity
prepends DLE as an escape character before the transmitted DLE character
from the payload. This ensures that none of the markers can appear inside the
transmitted frame. The receiver detects the frame boundaries and removes
the second DLE when it receives two consecutive DLE characters.
Examples #
2. Followed by 1 2 3 DLE
5. The final string is: DLE STX 1 2 3 DLE DLE STX 4 DLE ETX
Have a look at the following table for more details:
🔎 Did You Know? DLE is the bit pattern 00010000, STX is 00000010
and ETX is 00000011.
1 2 3 DLE STX 4
Disadvantages of Stuf ng #
1. In character stuffing and in bit stuffing, the length of the transmitted
frames is increased. The worst case redundant frame in case of bit
stuffing is one that has a long sequence of all 1s, whereas in the case of
character stuffing, it’s a frame consisting entirely of DLE characters.
2. When transmission errors occur, the receiver may incorrectly decode one
or two frames (e.g., if the errors occur in the markers). However, it’ll be
able to resynchronize itself with the next correctly received markers.
Quick Quiz! #
1110011111100100010101000111111010
COMPLETED 0%
1 of 4
In the next lesson, we’ll study error detection in the data link layer.
Principles of The Data Link Layer: Error Detection
This lesson will go into depth with how the data link layer does error detection
Many error detection schemes exist and entire books have been written on
the subject. A detailed discussion of these techniques is outside the scope of
this course, and we will only discuss some examples to illustrate the key
principles.
Parity Bit #
The simplest error detection code is the parity bit. In this case, the number of
redundant bits is 1. There are two types of parity schemes:
1. Even parity: With the even parity scheme, the redundant bit is chosen so
that an even number of bits are set to 1 in the transmitted bit string of
N + 1 bits.
2. Odd parity: With the odd parity scheme, the redundant bit is chosen so
that an odd number of bits are set to 1 in the transmitted bit string of
N + 1 bits.
The receiver can easily recompute the parity of each received bit string and
discard the strings with an invalid parity. The parity scheme is often used
when 7-bit characters are exchanged. In this case, the eighth bit is often a
parity bit.
Example #
The table below shows the parity bits that are computed for bit strings
containing three bits.
000 1 0
001 0 1
010 0 1
100 0 1
111 0 1
110 1 0
101 1 0
011 1 0
The parity bit allows a receiver to detect transmission errors that have
affected a single bit among the transmitted N + 1 bits. If there are an even
number of bits in error, the errors wouldn’t be detected. An odd number of
errors will still be detected.
To transmit a bit set to 1, the sender transmits 111 and to transmit a bit
set to 0, the sender transmits 000.
When there are no transmission errors, the receiver can decode 111 as 1.
If transmission errors have affected a single bit, the receiver performs
majority voting as shown in the table below. This scheme allows the
receiver to correct all transmission errors that affect a single bit.
001 0
010 0
100 0
111 1
110 1
101 1
011 1
Other Techniques #
Other more powerful error correction codes have been proposed and are used
in some applications. The Hamming Code is a clever combination of parity
bits that provides error detection and correction capabilities.
In practice, data link layer protocols combine bit stuffing or character stuffing
with a length indication in the frame header and a checksum. The checksum
is computed by the sender and placed in the frame before applying
bit/character stuffing.
Quick Quiz! #
1
What would be the parity bit for the following string in an odd parity
scheme?
100101001
COMPLETED 0%
1 of 3
In the next lesson, we’ll start with data link layer medium access control!
Medium Access Control: Static Allocation
• Introduction
• LAN Organizations
• Collisions
• Medium Access Control Algorithms
• Static allocation Algorithms
• Frequency Division Multiplexing
• TDM
• Dynamic TDM
• Disadvantages
• Quick Quiz!
Introduction #
Point-to-point data link layer types need to select one of the framing
techniques described previously and optionally add retransmission
algorithms, such as those explained for the transport layer to provide a
reliable service.
LAN Organizations #
A LAN is composed of several hosts that are attached to the same shared
physical medium. And LAN can be organized in a few different ways. We’ll
focus on four main ones:
1. A bus-shaped network where all hosts are attached to the same physical
cable.
2. A ring-shaped network where all hosts are attached to an upstream and
a downstream node so that the entire network forms a ring.
3. A star-shaped network where all hosts are attached to the same device.
4. A wireless network where all hosts can send and receive frames using
radio signals.
Collisions #
The common problem among all of these network organizations is how to
efficiently share access to the local area network. If two devices send a frame
at the same time, the two electrical, wireless, or optical signals that
correspond to these frames will appear at the same time on the transmission
medium, and a receiver will not be able to decode either frame. Such
simultaneous transmissions are called collisions. A collision may involve
frames transmitted by two or more devices attached to the Local Area
Network. Collisions are the main cause of errors in wired Local Area
Networks. They also reduce the throughput of the network, which is
problematic.
TDM #
Time Division Multiplexing (TDM) is a static bandwidth allocation method
that was initially defined for the telephone network. In the fixed telephone
network, a voice conversation is usually transmitted as a 64 kbps signal. Thus,
a telephone conservation generates 8 kbps, or 1 byte every 125 microseconds.
Telephone conversations often need to be multiplexed together on a single
line. For example, in Europe, thirty 64 Kbps voice signals are multiplexed over
a single 2 Mbps line. This is done by using Time Division Multiplexing (TDM).
Time-division multiplexing
Dynamic TDM #
TDM, as shown above, can be completely static, i.e., the same conversations
always share the link. However, if the time slot allocation is static and some
users are not currently transmitting, the resources are being wasted.
Dynamic TDM can be used in that scenario, which simply allows for the time
slot allocations to be dynamically adjusted to make more efficient use of the
transmission resources. In order to do so, the two parties part of a session
must exchange messages specifying which conversation uses which byte
inside each slot. Thanks to these signaling messages, it is possible to
dynamically add and remove voice conversations from a given link.
Disadvantages #
TDM and FDM are widely used in telephone networks to support fixed
bandwidth conversations. Using them in Local Area Networks that support
computers would probably be inefficient though, because computers
usually don’t send information at a fixed rate. Instead, they often have an on-
off behavior. During the on-period, the computer tries to send at the highest
possible rate, e.g., to transfer a file. During the off-period, which is often much
longer than the on-period, the computer does not transmit any packet. Using a
static allocation scheme for computers attached to a LAN would lead to huge
1
inefficiencies, as they would only be able to transmit at N of the total
bandwidth during their on-period. This is despite the fact that the other
computers are in their off-period and therefore don’t need to transmit any
information.
Quick Quiz! #
1 There is a fiber optic channel with a bandwidth of 30 MHz that needs to
be equally shared amongst 30 users based on a static allocation scheme.
The bandwidth allocated to each user will be ______.
COMPLETED 0%
1 of 2
The dynamic MAC algorithms discussed in the remainder of this chapter aim
to solve this problem. In the next lesson, we’ll look at some optimistic
allocation protocols!
Medium Access Control: Stochastic Methods - ALOHA
ALOHANet #
ALOHANet showed that it was possible to use radio signals to interconnect
computers. The first version of ALOHANet, operated as follows:
1. The first channel was used by the mainframe to send frames to all
terminals.
2. The second channel was shared among all terminals to send frames
to the mainframe.
As all terminals shared the same transmission channel, there was a risk
of collision. To deal with this problem as well as transmission errors, the
mainframe verified the parity bits of the received frame and sent an
acknowledgment on its channel for each correctly received frame. The
terminals, on the other hand, had to retransmit the unacknowledged
frames.
Pseudocode #
The pseudo-code below shows the operation of an ALOHANet terminal. We
use this python syntax for all Medium Access Control algorithms described in
this chapter. The algorithm is applied to each new frame that needs to be
transmitted. It attempts to transmit a frame at most max times (while loop).
Each transmission attempt is performed as follows:
# ALOHA
N=1
while N <= max:
send(frame)
wait(ack_on_return_channel or timeout)
if (ack_on_return_channel):
break # transmission was successful
else if(timeout):
# timeout
wait(random_time)
N=N+1
else:
# Too many transmission attempts
Slotted ALOHA #
Many improvements to ALOHANet have been proposed, and this technique,
or some of its variants, are still found in wireless networks today. The slotted
technique proposed in Roberts’ 1975 paper titled “ALOHA packet system with
and without slots and capture” is important because it shows that a simple
modification can significantly improve channel utilization.
Instead of allowing all terminals to transmit at any time, divide the time
into slots and allow terminals to transmit only at the beginning of each
slot.
Each slot corresponds to the time required to transmit one fixed size
frame.
In practice, these slots can be imposed by a single clock that is received by
all terminals. In ALOHANet, it could have been located on the central
mainframe.
Quick Quiz! #
1
Slotted ALOHA improves channel utilization
COMPLETED 0%
1 of 2
In the next lesson, we’ll look at another stochastic MAC protocol called carrier
sense multiple access.
Medium Access Control: Stochastic Methods - CSMA
In this lesson, we'll study the carrier sense multiple access protocol.
How It Works #
CSMA requires all nodes to listen to the transmission channel to verify that it’s
free before transmitting a frame. When a node senses the channel to be busy,
it defers its transmission until the channel becomes free again.
Pseudocode #
# persistent CSMA
N=1
while N <= max:
wait(channel_becomes_free)
send(frame)
wait(ack or timeout)
if ack:
break # transmission was successful
else :
# timeout
N=N+1
# end of while loop
# Too many transmission attempts
The above pseudocode is often called persistent CSMA as the terminal will
continuously listen to the channel and transmit its frame as soon as the
channel becomes free.
Non-persistent CSMA #
Another important variant of CSMA is the non-persistent CSMA. The main
difference between persistent and non-persistent CSMA described in the
pseudocode below is that a non-persistent CSMA node does not continuously
listen to the channel to determine when it becomes free. When a non-
persistent CSMA terminal senses the transmission channel to be busy, it
waits for a random time before sensing the channel again. This improves
channel utilization compared to persistent CSMA. With persistent CSMA,
when two terminals sense the channel to be busy, they will both transmit (and
thus cause a collision) as soon as the channel becomes free.
if received(ack):
break # transmission was successful
else:
# timeout
N=N+1
else:
wait(random_time)
# end of while loop
# Too many transmission attempts
ALOHA 18.4%
Quick Quiz! #
1
What’s the difference between persistent and non-persistent CSMA?
COMPLETED 0%
1 of 2
In the next lesson, we’ll look at an incredibly popular variant of the carrier
sense multiple access protocol.
Medium Access Control: Stochastic Methods -
CSMA/CD
In this lesson, we'll look at the variant of CSMA that also detects collisions.
Consider the situation of two terminals that are connected to the same cable.
This cable could, for example, be a coaxial cable, or it could also be built with
twisted pairs. Before extending CSMA, it’s useful to understand more
intuitively, how frames are transmitted in such a network and how
collisions can occur.
1. The first step is thus to begin the transmission of the electrical signal.
This is depicted in the first slide below.
2. This electrical signal will travel along the cable. Although electrical
signals travel fast, we know that information cannot travel faster than
the speed of light (i.e. 300, 000 kilometers/second). On a coaxial cable, an
electrical signal is slightly slower than the speed of light in a vacuum
which is at about 200, 000 kilometers/second.
This implies that if the cable has a length of one kilometer, the
electrical signal will need 5 microseconds to travel from one end of
the cable to the other.
3. The ends of coaxial cables are equipped with termination points that
ensure that the electrical signal is not reflected back to its source. This is
illustrated in the third slide below, where the electrical signal has
reached the left endpoint and host B.
5. In slide 4, the electrical signal has reached both ends of the cable and
occupies it completely. Host A continues to transmit the electrical signal
until the end of the frame.
6. As shown in slide 5, when the sending host stops its transmission, the
electrical signal corresponding to the end of the frame leaves the coaxial
cable.
7. The channel becomes empty again once the entire electrical signal has
been removed from the cable.
A B
Start of frame
A B
A B
A B
Hosts A and B start their transmission at the same time (first slide).
This will not last a long time, and in the second slide the electrical signals
from both host A and host B reach host C.
Since host C cannot decode the frames, it cannot determine which hosts
are sending the colliding frames. Note that host A (and host B) will detect
the collision after host C (third slide).
In a wired network, a host is able to detect such a collision both while it’s
listening (e.g., like host C in the figure above) and also while it is sending
its own frame. When a host transmits a frame, it can compare the
electrical signal that it transmits with the electrical signal that it senses
on the wire.
In the first and second slides in the figure above, host A senses only its
own signal.
In the third slide, it senses an electrical signal that differs from its own
signal and can thus detect the collision.
At this point, its frame is corrupted and it can stop its transmission.
The ability to detect collisions while transmitting is the starting point for the
Carrier Sense Multiple Access with Collision Detection (CSMA/CD) MAC
algorithm which is used in Ethernet networks. When an Ethernet host detects
a collision while it’s transmitting, it immediately stops its transmission.
Compared with pure CSMA, CSMA/CD is an important improvement since
when collisions occur, they only last until colliding hosts have detected it and
stopped their transmission instead of continuing to transport the rest of the
frame unnecessarily. In practice, when a host detects a collision, it sends a
special jamming signal on the cable to ensure that all hosts have detected the
collision.
A C B
A C B
A C B
Worst Case #
To better understand these collisions, it’s useful to analyse what would be the
worst collision on a shared bus network. Let’s consider a wire with two hosts
attached at both ends, as shown in the slides below.
Host A starts to transmit its frame and its electrical signal is propagated
on the cable. Its propagation time depends on the physical length of the
cable and the speed of the electrical signal. Let us use τ to represent this
propagation delay in seconds.
Slightly less than τ seconds after the beginning of the transmission of A’s
frame, B decides to start transmitting its own frame.
After ϵ seconds, B senses A’s frame, detects the collision and stops
transmitting.
The beginning of B’s frame travels on the cable until it reaches host A.
An important point to note is that a collision can only occur during the
first 2 × τ seconds of its transmission. If a collision did not occur during
this period, it cannot occur afterward since the transmission channel is
busy after τ seconds and CSMA/CD hosts sense the transmission channel
before transmitting their frame.
A B
A B
After τ seconds, A's frame reaches B. At time τ-ε, B starts to transmit its
own frame. B notices the collision immediately and stops transmitting
A B
Quick Quiz! #
1
What would happen if a collision occurs and regular CSMA was
deployed?
COMPLETED 0%
1 of 3
Now that we have a basic idea of how CSMA/CD works, we’ll look at a few
ways that it’s optimized in the next lesson.
Medium Access Control: Stochastic Methods -
Optimizing CSMA/CD
• Removing Acknowledgements
• Edge Case: Short Frames
• Retransmission Timeout
• Performance
• Pseudocode
• Quick Quiz!
Removing Acknowledgements #
On the wired networks where CSMA/CD is used, collisions are almost the only
cause of transmission errors that affect frames. Transmission errors that only
affect a few bits inside a frame seldom occur in these wired networks. For this
reason, the designers of CSMA/CD chose to completely remove the
acknowledgment frames in the data link layer.
If not, given the negligible Bit Error Ratio of the underlying network, it
assumes that the frame was received correctly by its destination. So, the
bit errors might be detected or corrected using a checksum field. If not,
we can rely on the layers above to implement retransmission.
Let us consider two hosts, A and B, that are sending a small frame to host C as
illustrated in the slides below. If the frames sent by A and B are very short, the
situation illustrated below may occur.
1. Hosts A and B send their frame and stop transmitting (first slide). Since
until the end of the transmission, no collision was detected, hosts A and B
are content that they were successfully able to use the channel to
transmit the frame.
2. When the two short frames arrive at the location of host C, they collide
and host C cannot decode them (second slide).
3. The two frames are absorbed by the ends of the wire. Neither host A nor
host B has detected the collision. They both consider their frame to
have been received correctly by its destination.
A C B
A C B
A C B
To solve this problem, networks using CSMA/CD require hosts to transmit for
at least 2 × τ seconds. Since the network transmission speed is fixed for a
given network technology, this implies that a technology that uses CSMA/CD
enforces a minimum frame size. In the most popular CSMA/CD technology,
Ethernet, 2 × τ is called the slot time.
Retransmission Timeout #
The last innovation introduced by CSMA/CD is the computation of the
retransmission timeout.
A short timeout would lead to a low network access delay but with a
higher risk of collisions.
On the other hand, a long timeout would cause a long network access
delay but a lower risk of collisions.
Once it has detected the collision, a host can either retransmit its frame
immediately or defer its transmission for some time.
2. The first host retransmits immediately and the second defers its
retransmission.
3. The second host retransmits immediately and the first defers its
retransmission.
In the second and third cases, both hosts have flipped different coins. The
delay chosen by the host that defers its retransmission should be long
enough to ensure that its retransmission will not collide with the
immediate retransmission of the other host.
However, the delay should not be longer than the time necessary to avoid
the collision, because if both hosts decide to defer their transmission, the
network will be idle during this delay.
The slot time is the optimal delay since it is the shortest delay that
ensures that the first host will be able to retransmit its frame completely
without any collision.
Performance #
If two hosts are competing, the algorithm above will avoid a second
collision 50% of the time.
Pseudocode #
The complete pseudocode for the CSMA/CD algorithm is shown in the figure
below.
N=1
while N <= max:
wait(channel_becomes_free)
send(frame)
wait_until (end_of_frame) or (collision)
if collision detected:
stop transmitting
end(jamming)
k = min (10, N)
r = random(0, 2k - 1)*slotTime
wait(r*slotTime)
N=N+1
else:
wait(inter-frame_delay)
break
# end of while loop
# Too many transmission attempts
Pseudocode: CSMA/MD
Despite this delay, there are still conditions where CSMA/CD is not completely
fair. Consider for example a network with two hosts: a server sending long
frames and a client sending acknowledgments. Measurements reported have
shown that there are situations where the client could suffer from repeated
collisions that lead it to wait for long periods of time due to the exponential
back-off algorithm.
Quick Quiz! #
1 Why is a minimum frame size necessary?
COMPLETED 0%
1 of 2
Now that we’re done with stochastic algorithms, we’ll study some key data
link layer technologies.
Introduction to Ethernet
• Introduction
• First Of cial Ethernet Speci cation
• Important Parameters
• Changes Recommended by First Of cial Speci cation
• MAC Addresses
• Checking Your MAC Address
• Quick Quiz!
Introduction #
Ethernet was designed in the 1970s at the Palo Alto Research Center. The first
prototype used a coaxial cable as the shared medium and 3 Mbps of
bandwidth.
Important Parameters #
This specification defines several important parameters for Ethernet
networks.
2. The second decision was the duration of the slot time. In Ethernet, a
long slot time enables networks to span a long distance but forces the
host to use a larger minimum frame size. The compromise was a slot
3. The third decision was the frame format. The experimental 3 Mbps
Ethernet network built at Xerox used short frames containing 8 bit
source and destination address fields. Up to 554 bytes of payload using 8
bit addresses was suitable for an experimental network, but it was
clearly too small for commercial deployments. Hence, they came up with
48 bit source and destination address fields and up to 1500 bytes of
payload.
The first bit of the address indicates whether the address identifies a
network adapter or a multicast group.
The upper 24 bits are used to encode an Organization Unique Identifier
(OUI). This OUI identifies a block of addresses that has been allocated by
the secretariat who is responsible for the uniqueness of Ethernet
addresses to a manufacturer. For instance, 00000C belongs to Cisco
Systems Inc.. Once a manufacturer has received an OUI, it can build and
sell products with any of the ~16 million addresses in this block. A
manufacturer may obtain more than one OUIs.
OUI
48-bits
ifconfig
Pick the ethernet interface. Our’s is called ens4 . You can run the following
command to get only your ethernet interface’s details.
ifconfig ens4
The MAC Address is printed after HWaddr . So the output may be as follows:
Next, we can use a tool called macchanger to change our MAC address.
ifconfig ens4
ifconfig ens4 down # Turn the interface off
macchanger -r ens4 # Change Mac Address
ifconfig eth0 up # Turn it back on
ifconfig ens4 # Check new MAC address
Quick Quiz! #
1
Which of the following is NOT a valid unicast MAC address?
COMPLETED 0%
1 of 3
• Ethernet Frames
• Problem: Sending Short Frames
• Solution: Add Length Field
• Quick Quiz!
Ethernet Frames #
The original 10 Mbps Ethernet specification defined a simple frame format
where each frame is composed of five fields.
0. The Ethernet frame starts with a preamble (not shown in the figure
below) that’s used by the physical layer of the receiver to synchronise its
clock with the sender’s clock.
1. The first field of the frame is the destination address. As this address is
placed at the beginning of the frame, an Ethernet interface can quickly
verify whether it’s the frame recipient and if not, cancel the processing of
the arriving frame.
2. The second field is the source address. While the destination address
can be either a unicast or a multicast/broadcast address, the source
address must always be a unicast address.
3. The third field is a 16 bit integer that indicates which type of network
layer packet is carried inside the frame. This field is often called the
Ether Type. Frequently used EtherType values include: 0x0800 for IPv4,
0x86DD for IPv6, and 0x806 for the Address Resolution Protocol (ARP).
4. The fourth part of the Ethernet frame is the payload. The minimum
length of the payload is 46 bytes to ensure a minimum frame size,
The Ethernet frame format shown above is highlighted in DIX, i.e., the first
ethernet specification. This is the format used to send both IPv4 and IPv6
packets.
While developing its Ethernet standard, the IEEE 802.3 working group
was confronted with a problem: Ethernet mandated a minimum payload
size of 46 bytes, while some companies were looking for a LAN
technology that could transparently transport short frames containing
only a few bytes of payload.
header does not contain a length field, it’s impossible for the receiver to
determine how many useful bytes were placed inside the payload field.
Without the type field, however, it’s impossible for a receiving host to identify
the type of network layer packet inside a received frame. To solve this new
problem, IEEE developed a completely new sublayer called the Logical Link
Control. Several protocols were defined in this sublayer. One of them
provided a slightly different version of the Type field of the original Ethernet
frame format. Another contained acknowledgments and retransmissions to
provide a reliable service.
Quick Quiz! #
1
What would be the total size of a minimal sized TCP packet encapsulated
inside an IP packet, encapsulated inside an Ethernet frame?
COMPLETED 0%
1 of 3
In the next lesson, we’ll look at physical layers that have been defined for
Ethernet networks.
Physical Layers for Ethernet
In this lesson, we'll look at various types of physical layers and their limitations and bene ts to Ethernet.
• 10Base5
• 10Base2
• 10BaseF
• 10BaseT
• Twisted Pairs
• Changes to Ethernet
• Change in Topology
• Introduction of Ethernet Hubs
• Fast Ethernet
• Quick Quiz!
10Base5 #
The first type of physical layer, usually called 10Base5, provided 10 Mbps over
a thick coaxial cable. The characteristics of the cable and transceivers that
were used then enabled the utilization of 500 meter long segments. A 10Base5
network can also include repeaters between segments.
10Base2 #
The second type of physical layer was 10Base2. 10Base2 used a thin coaxial
cable that was easier to install than the 10Base5 cable but could not be longer
than 185 meters.
10BaseF #
A 10BaseF type of physical layer was also defined to transport Ethernet over
point-to-point optical links.
10BaseT #
Twisted Pairs #
The major change to the physical layer was the support of twisted pairs in the
10BaseT specification. Twisted pair cables are traditionally used to support
the telephone service in office buildings. Most office buildings today are
equipped with structured cabling. Several twisted pair cables are installed
between any room and a central telecom closet per building or per floor in
large buildings. These telecom closets act not only as concentration points for
the telephone service but also for LANs.
Changes to Ethernet #
The introduction of the twisted pairs led to two major changes to Ethernet.
Let’s discuss each.
Change in Topology #
The first change concerns the physical topology of the network. 10Base2 and
10Base5 networks are shared buses, the coaxial cable typically passes through
each room that contains a connected computer. A 10BaseT network, however,
is a star-shaped network. All the devices connected to the network are
attached to a twisted pair cable that ends in the telecom closet. From a
maintenance perspective, this is a major improvement. The cable is a weak
point in 10Base2 and 10Base5 networks. Any physical damage on the cable
broke the entire network and when such a failure occurred, the network
administrator had to manually check the entire cable to detect where it was
damaged. With 10BaseT, when one twisted pair is damaged, only the device
connected to this twisted pair is affected and this does not affect the other
devices.
The second major change introduced by 10BaseT was that is was impossible to
build a 10BaseT network by simply connecting all the twisted pairs together.
All the twisted pairs must be connected to a relay that operates in the physical
layer. This relay is called an Ethernet hub. A hub is thus a physical layer relay
that receives an electrical signal on one of its interfaces, regenerates the signal
and transmits it over all its other interfaces. Some hubs are also able to
convert the electrical signal from one physical layer to another, such as a
10BaseT to 10Base2 conversion.
Host A Host B
1. First, the network topology must be a tree. As hubs are relays in the
physical layer, adding a link between Hub 2 and Hub 3 in the network
below would create an electrical shortcut that would completely disrupt
the network. This implies that there cannot be any redundancy in a hub-
based network. A failure of a hub or a link between two hubs would
partition the network into two isolated networks.
2. Second, as hubs are relays in the physical layer, collisions can happen
and must be handled by CSMA/CD as in a 10Base5 network. This implies
that the maximum delay between any pair of devices in the network can’t
be longer than the 51.2 microseconds, slot time. If the delay is longer,
collisions between short frames may not be correctly detected. This
constraint limits the geographical spread of 10BaseT networks
containing hubs.
Hub 1
Hub 3
Hub 2
Hub 4
Fast Ethernet #
In the late 1980s, 10 Mbps became too slow for some applications and network
manufacturers developed several LAN technologies that offered higher
bandwidth, such as the 100 Mbps FDDI LAN that used optical fibers.
1. First, Fast Ethernet had to support twisted pairs. Although it was easier
from a physical layer perspective to support higher bandwidth on coaxial
cables than on twisted pairs, coaxial cables were a nightmare from
deployment and maintenance perspectives.
2. Second, Fast Ethernet had to be perfectly compatible with the existing 10
Mbps Ethernets to allow Fast Ethernet technology to be used initially as a
backbone technology to interconnect 10 Mbps Ethernet networks. This
forced fast Ethernet to use exactly the same frame format as 10 Mbps
Ethernet. This implied that the minimum Fast Ethernet frame size
remained at 512 bits. To preserve CSMA/CD with this minimum frame
size and 100 Mbps instead of 10 Mbps, the duration of the slot time was
The evolution of Ethernet did not stop. In 1998, the IEEE published the first
standard to provide Gigabit Ethernet over optical fibers. Several other types of
physical layers were added afterward. The 10 Gigabit Ethernet standard
appeared in 2002.
Work is ongoing to create faster ethernet standards. The table below lists the
main Ethernet standards. A more detailed list may be found here.
Standard Comments
COMPLETED 0%
1 of 3
• Introduction
• MAC Address Tables
• Retaining Plug & Play with Switches
• MAC address learning algorithm
• Pseudocode
• Timestamp
• Unicast, Broadcast, & Multicast Frames
• Handling Failures
• Spanning Tree Protocol
• Quick Quiz!
Introduction #
Increasing the physical layer bandwidth as in Fast Ethernet was only one of
the solutions to improve the performance of Ethernet LANs.
A second solution was to replace the hubs with more intelligent devices. As
Ethernet hubs operate in the physical layer, they can only regenerate the
electrical signal to extend the geographical reach of the network. From a
performance perspective, it would be more interesting to have devices that
operate in the data link layer and can analyze the destination address of
each frame and forward the frames selectively on the link that leads to the
destination. This would allow two hosts to communicate on one pair of
interfaces while other pairs of interfaces can be simultaneously used for other
communication, thereby improving communication efficiency. Such devices
are usually called Ethernet switches. An Ethernet switch is a relay that
operates in the data link layer.
Switch 1 Eth: D
Eth: E
Address Port
A West
B South
Switch 2 C South
Eth: A
Src: A
D North
Dst: B
E North
Eth: B Eth: C
It is important to retain this plug and play capability for Ethernet switches as
well. This implies that Ethernet switches must be able to build their MAC
address table automatically without requiring any manual configuration.
This automatic configuration is performed by the MAC address learning
algorithm that runs on each Ethernet switch.
1. This algorithm extracts the source address of the received frames and
remembers the port over which a frame from each source Ethernet
address has been received.
2. This information is inserted into the MAC address table that the switch
uses to forward frames.
3. This allows the switch to automatically learn the ports that it can use to
reach each destination address, provided that this host has previously
sent at least one frame. This is not a problem since most upper-layer
protocols use acknowledgments at some layer and thus even an Ethernet
printer sends Ethernet frames as well.
Pseudocode #
The pseudocode below details how an Ethernet switch forwards Ethernet
frames. It first updates its MAC address table with the source address of the
frame.
Timestamp #
The MAC address table used by some switches also contains a timestamp that
is updated each time a frame is received from each known source address.
This timestamp is used to remove from the MAC address table entries that
have not been active during the last n minutes. This limits the growth of the
MAC address table, but also allows hosts to move from one port to another.
Handling Failures #
The MAC address learning algorithm combined with the forwarding algorithm
work well in a tree-shaped network such as the one shown above. However,
to deal with link and switch failures, network administrators often add
redundant links to ensure that their network remains connected even after a
failure. Let us consider what happens in the Ethernet network shown in the
figure below.
Eth: A Sw 1 Eth: C
Sw 2
Sw 3
Eth: B
When all switches boot, their MAC address table is empty. Assume that host A
sends a frame towards host C.
Upon reception of this frame, switch 1 updates its MAC address table to
remember that address A is reachable via its West port.
When switch 2 receives the frame, it updates its MAC address table for
address A and forwards the frame to host C as well as to switch 3. switch
3 has thus received two copies of the same frame.
Quickly, all the available link bandwidth will be exhausted to forward all
the copies of this frame.
As Ethernet does not contain any TTL or Hop Limit, this loop will never
stop.
Quick Quiz! #
1 Consider the first network given in this lesson. Suppose host D joins it
later than the rest and immediately sends a frame to host C. Will host A
then be able to send it a frame right after?
COMPLETED 0%
1 of 2
• Problem Statement
• Spanning Tree Protocol
• Con guration Bridge Protocol Data Units
• Comparing Bridge Protocol Data Units
• How It Works
• Coding Challenge
Problem Statement #
In this challenge, you will implement a simplified version of the spanning tree
protocol. For this challenge, you should assume that a switch and a bridge are
the same things. The only practical difference is that bridges have few ports,
whereas switches have many many ports.
A BPDU with a lower root bridge ID is better than a BPDU with a higher
root bridge ID.
If the root bridge ID of both BPDUs is the same, then a BPDU with a lower
cost is better than a BPDU with a higher cost.
If the cost and bridge ID of both BPDUs is the same, then a BPDU with a
lower transmitting bridge ID is better than a BPDU with a higher
transmitting bridge ID.
If the cost, the bridge ID, transmitting bridge ID of both BPDUs is the
same, then a BPDU with a lower transmitting port ID is better than a
BPDU with a higher transmitting port ID.
How It Works #
As described above, the protocol aims to build a tree in which one bridge is
determined to be the root. Bridges can only forward data frames towards the
root bridge or away from the root bridge. In this way, cycles are avoided.
Each bridge is assigned a unique bridge ID and the root bridge is the one with
the smallest bridge ID.
1. When a bridge first boots up, it believes itself to be the root, and hence its
BPDU looks like [its bridge ID, 0, its bridge ID, Port ID] .
2. It multicasts this BPDU to all of its neighbors. The neighbors of this bridge
are all the bridges on the LAN segment to which it is connected via any of
its ports.
3. It also receives BPDUs from all of its neighbors.
4. It then processes these BPDUS to determine a couple of things:
1. The root bridge. It declares a port to be root if the root bridge is
accessible from this port.
2. Which ports it should declare blocking . It declares a port blocking if
it receives a better BPDU than the one it would have sent on that
port. The ports are forwarding by default.
5. Finally, the bridge sends all of this newly learned information to all
bridges accessible via its forwarding or root ports.
Coding Challenge #
We’ve given you some starter code. Some helper methods have been created
that you might find useful and others are declared but left empty. The main
task is to fill in the functions send_BPDUs() and receive_BPDUs() . Good luck!
main.py
topology_reader.py
ports.py
simulator.py
bridge.py
class bridge:
def __init__(self, bridge_ID, port_list):
self.bridge_ID = bridge_ID
self.port_list = port_list # port_list[0] is the port with port number 0
self.config_BPDU = [bridge_ID, 0, bridge_ID, None] # Root ID, Cost, Transmitting Bridge
self.receive_queue = {}
def get_root_port_id(self):
for p in range(len(self.port list)):
if self.port_list[p].port_type == 2:
return p
return None
def print_bridge(self):
print("~~~~~~Bridge ID: " + str(self.bridge_ID) + " Root ID: " + str(self.config_BPDU[0]
print("BPDU:")
print(self.config_BPDU)
Coming up next, we’ll look at the solution to the spanning tree protocol
programming challenge!
Solution Review: Spanning Tree Protocol
In this lesson, we'll look at a solution to the spanning tree protocol programming assignment.
• Solution
• Explanation
• send_BPDUs()
• receive_BPDUs()
Solution #
main.py
topology_reader.py
ports.py
simulator.py
bridge.py
class bridge:
def __init__(self, bridge_ID, port_list):
self.bridge_ID = bridge_ID
self.port_list port_list # port_list[0] is the port with port number 0
self.config_BPDU = [bridge_ID, 0, bridge_ID, None] # Root ID, Cost, Transmitting Bridge
self.receive_queue = {}
def get_root_port_id(self):
for p in range(len(self.port_list)):
if self.port_list[p].port_type == 2:
return p
return None
def print_bridge(self):
print("~~~~~~Bridge ID: " + str(self.bridge_ID) + " Root ID: " + str(self.config_BPDU[0]
print("BPDU:")
print(self.config_BPDU)
Explanation #
send_BPDUs() #
It first iterates over all of non-blocking ports to find its neighbors. The
neighbors are found by calling the function get_reachable_bridge_ID() on
each port. This function returns a list of bridge IDs that are reachable from
that port. Each of those bridges is sent the bridge’s current BPDU by
appending them to that bridge’s receive_queue .
receive_BPDUs() #
2. update_ports() : this function iterates over the best BPDUs and updates
the ports to be blocking if the one received is better than the one it would
have sent and to forwarding otherwise. It uses the helper function
is_better() to do this. is_better() is fairly straightforward: it takes two
BPDUs as input and returns 1 if the first one is better and 0 if it’s not.
3. elect_root : lastly, the root is elected. The best BPDUs are iterated over
and if any one of them’s root ID is smaller than the one that the bridge
currently believes to be the root, it’s updated to the new values and the
port from which this BPDU was received is set to be the new root.
Furthermore, if an old root port existed, it is changed to forwarding .
In this lesson, we'll look at some possible career paths that you can pursue after you nish this course!
Thank you for taking this course! We hope that you have gained an in-depth
and working knowledge of all things networks.
— Team Educative