Uniy-2 CN 1 Notes
Uniy-2 CN 1 Notes
The Application Layer is the topmost layer in the OSI and TCP/IP models, responsible for
providing network services directly to end-users and applications. It enables
communication between software applications running on different devices across a network.
1. Client-Server Architecture:
o A centralized server provides services, while multiple clients request
services.
o The server is always on and has a fixed IP address.
o Clients communicate with the server but not with each other.
o Examples: Web Browsing (HTTP), Email (SMTP, IMAP, POP3), File
Transfer (FTP)
2. Peer-to-Peer (P2P) Architecture:
o There is no central server; instead, each peer (node) acts as both a client and
a server.
o Resources and services are distributed among peers.
o Examples: BitTorrent, Skype (earlier versions), Blockchain Networks
python
CopyEdit
import socket
c) Email Services
4. Congestion Control
6. Flow Control
The transport layer provides a flow control mechanism between the
adjacent layers of the TCP/IP model. TCP also prevents data loss due to
a fast sender and slow receiver by imposing some flow control
techniques. It uses the method of sliding window protocol which is
accomplished by the receiver by sending a window back to the sender
informing the size of data it can receive.
The application layer of the OSI and TCP/IP models is responsible for providing end-user services,
such as email, file transfer, and web browsing. One of the most widely used application layer
protocols is the HyperText Transfer Protocol (HTTP), which forms the foundation of the World Wide
Web (WWW).
Web Browsers (e.g., Chrome, Firefox, Edge): Software that retrieves and renders
web pages.
Web Servers (e.g., Apache, Nginx, IIS): Hosts websites and serves web content to
clients.
Web Pages: Documents written in HTML (HyperText Markup Language), often
styled with CSS and made interactive with JavaScript.
URLs (Uniform Resource Locators): Addresses used to access web resources (e.g.,
https://www.example.com).
HTTP stands for HyperText Transfer Protocol. It is the main way web
browsers and servers communicate to share information on the internet.
Tim Berner invents it. HyperText is the type of text that is specially coded
with the help of some standard coding language called HyperText
Markup Language (HTML). HTTP/2 is the new version of HTTP. HTTP/3
is the latest version of HTTP.
Basic Structure: HTTP forms the foundation of the web, enabling data
communication and file sharing.
Web Browsing: Most websites use HTTP, so when you click on a link
or download a file, HTTP is at work.
Client-Server Model: HTTP works on a request-response system.
Your browser (client) asks for information, and the website’s server
responds with the data.
Application Layer Protocol: HTTP operates within the Internet
Protocol Suite, managing how data is transmitted and received.
WORKING:
whenever we want to open any website we first open a web browser after
that we will type the URL of that website (e.g., www.facebook.com ). This
URL is now sent to the Domain Name Server (DNS). Then DNS first
checks records for this URL in their database, and then DNS will return
the IP address to the web browser corresponding to this URL. Now the
browser is able to send requests to the actual server.
After the server sends data to the client, the connection will be closed. If
we want something else from the server we should have to re-establish
the connection between the client and the server.
HTTP Method
HTTP Methods are simply HTTP Verb. In spite of being present so many
HTTP Methods, the most common HTTP Methods are HTTP GET and
HTTP POST. These two are generally used in HTTP cases. In HTTP
GET, the information is received in the form of a website.
HTTP Response:
HTTP Response is simply the answer to what a Server gets when the
request is raised. There are various things contained in HTTP Response,
some of them are listed below.
HTTP Status Code
HTTP Headers
HTTP Body
HTTP Status Codes are the 3-digit codes that tell the message or simply
tell us about the HTTP Request whether it has been completed or not.
There are simply 5 types of status codes.
Informational
Successful
Re-directional
Client-Error
Server-Error
Characteristics of HTTP
Cookies in HTTP
An HTTP cookie (web cookie, browser cookie) is a little piece of data that
a server transmits to a user’s web browser. When making subsequent
queries, the browser may keep the cookie and transmit it back to the
same server. An HTTP cookie is typically used, for example, to maintain a
user’s login state, and to determine whether two requests originate from
the same browser. For the stateless HTTP protocol, it retains stateful
information.
Advantages of HTTP
Memory usage and CPU usage are low because of fewer simultaneous
connections.
Since there are few TCP connections hence network congestion is
less.
Since handshaking is done at the initial connection stage, then latency
is reduced because there is no further need for handshaking for
subsequent requests.
The error can be reported without closing the connection.
In HTTP communication, connections between a client (browser) and a web server can be persistent
or non-persistent, depending on how they handle multiple requests and responses.
High latency: Opening and closing a connection for every request increases delay.
More overhead: Repeated TCP handshakes consume network and server resources.
Slow page loading: A webpage with multiple elements (images, CSS, JS) requires
multiple connections.
Example:
A webpage contains 1 HTML file, 5 images, and 1 CSS file → Requires 7 separate
connections.
1. Without Pipelining: The client sends a request, waits for the response, then sends the
next request.
2. With Pipelining (HTTP/1.1): The client sends multiple requests without waiting for
responses, improving performance. (However, HTTP/2 multiplexing has replaced
pipelining.)
Example:
A webpage contains 1 HTML file, 5 images, and 1 CSS file → Only 1 connection is
required to fetch all resources.
HTTP messages are the way clients (browsers) and servers communicate over the web. These
messages follow a structured format and are classified into two types:
1. HTTP Request Messages (sent by the client to request data from a server).
2. HTTP Response Messages (sent by the server to respond to a client's request).
Status Line (includes HTTP version, status code, and status message).
Headers (metadata about the response).
Optional Body (contains the requested resource or error message).
Explanation:
Starts
Request Line (GET / HTTP/1.1) Status Line (HTTP/1.1 200 OK)
With
A proxy server is an intermediary between a client (e.g., a web browser) and a destination server.
Instead of connecting directly to a website, a client sends its request to the proxy server, which then
forwards the request to the website on behalf of the client. The website's response is also routed
through the proxy before reaching the client.
1. Forward Proxy:
o Used by clients to access external networks (e.g., the internet).
o Common in businesses and schools to filter, monitor, and control internet
access.
2. Reverse Proxy:
o Used by web servers to handle requests from clients.
o Protects web servers, improves performance, and enables load balancing.
3. Transparent Proxy:
o Does not modify requests and responses.
o Users may not even be aware they are using a proxy.
4. Anonymous Proxy:
o Hides the client’s IP address to enhance privacy.
5. Caching Proxy:
o Stores copies of frequently accessed web pages to reduce load times and
bandwidth usage.
6. VPN Proxy:
o Encrypts internet traffic and masks the user’s real IP address.
Cookies:
Cookies are small text files stored on a user's browser by a website. They contain data about
the user’s activity, preferences, and session information to enhance the browsing experience.
Types of Cookies:
1. Session Cookies:
o Temporary cookies that expire when the browser is closed.
o Used for login sessions and shopping carts.
2. Persistent Cookies:
o Stored on the user's device for a set period.
o Used for remembering preferences, login details, and tracking behavior.
3. First-Party Cookies:
o Set by the website being visited.
o Used for user authentication and site preferences.
4. Third-Party Cookies:
o Set by external websites (e.g., advertisers and analytics services).
o Used for tracking users across multiple sites for targeted ads.
5. Secure Cookies:
o Sent only over HTTPS to prevent data theft.
6. HTTP-Only Cookies:
o Cannot be accessed by JavaScript, reducing the risk of cross-site scripting
(XSS) attacks.
Uses of Cookies:
conditional GET:
A Conditional GET is an HTTP request that asks the server only to send a resource if it has been
modified since the last time the client accessed it. This helps reduce bandwidth usage and improve
performance by avoiding unnecessary data transfers.
Working:
A Conditional GET request includes specific header fields that instruct the server to check whether
the resource has changed before sending a response.
1. If-Modified-Since
o Asks the server to send the resource only if it has been modified since the
given date.
o If unchanged, the server responds with 304 Not Modified instead of sending
the resource again.
Example Request:
yaml
CopyEdit
GET /image.jpg HTTP/1.1
Host: www.example.com
If-Modified-Since: Tue, 12 Mar 2024 10:30:00 GMT
mathematica
CopyEdit
HTTP/1.1 304 Not Modified
arduino
CopyEdit
HTTP/1.1 200 OK
Content-Type: image/jpeg
Content-Length: 45231
(Binary image data...)
2. If-Unmodified-Since
o Opposite of If-Modified-Since.
o The server sends the resource only if it has NOT been modified since the
given date.
o If the resource has been modified, the server responds with 412 Precondition
Failed.
3. If-None-Match (ETag-based Validation)
o Uses an ETag (Entity Tag), a unique identifier for the resource, instead of a
timestamp.
o The server only sends the resource if the ETag has changed.
Example Request:
sql
CopyEdit
GET /style.css HTTP/1.1
Host: www.example.com
If-None-Match: "abc123etag"
mathematica
CopyEdit
HTTP/1.1 304 Not Modified
yaml
CopyEdit
HTTP/1.1 200 OK
ETag: "xyz456etag"
Content-Type: text/css
Content-Length: 1024
(CSS file data...)
Conditional GET and Caching
Web browsers use Conditional GET to check if cached resources are still valid.
If unchanged, the browser reuses the cached version, saving bandwidth and
improving page load speed.
Conditional GET is commonly used for static assets like images, stylesheets, and
JavaScript files.
The File Transfer Protocol (FTP) is a standard network protocol used to transfer files between a
client and a server over a TCP-based network, such as the internet. FTP is one of the oldest
protocols, designed to facilitate the upload, download, and management of files on remote servers.
b) Authenticated FTP
FTP is not encrypted by default, making it vulnerable to attacks. Secure alternatives include:
FTP Commands
FTP uses text-based commands to communicate. Some common commands include:
Command Description
USER username Logs in with a username.
PASS password Logs in with a password.
LIST Lists files in a directory.
RETR filename Downloads a file.
STOR filename Uploads a file.
DELE filename Deletes a file.
CWD directory Changes the working directory.
Command Description
QUIT Closes the connection.
Advantages of FTP
✅Efficient for bulk file transfers
✅Supports resume and scheduling
✅Widely supported across platforms
✅Can be automated using scripts
Disadvantages of FTP
❌Lack of security (unencrypted data in basic FTP)
❌Requires manual configuration for firewall/NAT traversal
❌Not optimized for modern web-based file sharing
SMTP is a protocol used to send emails from a client (e.g., an email app) to a mail server or between
mail servers. It is responsible for email transmission but not for receiving emails.
SMTP WORKING:
SMTP Ports
Port Usage
25 Standard SMTP (mostly for server-to-server communication)
587 SMTP with encryption (STARTTLS) – Recommended for sending emails
465 SMTP with SSL encryption (deprecated, but still used by some services)
SMTP Commands
SMTP uses text-based commands for email transmission. Some common ones include:
Command Description
HELO / EHLO Identifies the client to the server
MAIL FROM: Specifies the sender’s email address
Command Description
RCPT TO: Specifies the recipient’s email address
DATA Starts the message body
QUIT Ends the session
Advantages of SMTP
Disadvantages of SMTP
POP3 is a protocol used by email clients to retrieve emails from a mail server. It downloads emails
to the user’s device and, by default, deletes them from the server.
Working:
POP3 Ports
Port Usage
110 Standard POP3 (unencrypted)
995 POP3 with SSL/TLS encryption (recommended)
POP3 Commands
Command Description
USER Provides the username
PASS Provides the password
LIST Lists all emails on the server
RETR Retrieves an email
DELE Deletes an email
QUIT Closes the session
Advantages of POP3
✅Works offline (emails stored on the device)
✅Faster access once emails are downloaded
✅Reduces server storage use
Disadvantages of POP3
❌Emails are deleted from the server (unless set to keep a copy)
❌Emails can’t be accessed from multiple devices easily
❌If the device crashes, emails may be lost
IMAP, MIME
IMAP is a protocol used for retrieving emails from a mail server. Unlike POP3, IMAP keeps emails
stored on the server, allowing users to access their email from multiple devices (e.g., phone, laptop,
tablet).
WORKING:
IMAP Ports
Port Usage
143 Standard IMAP (unencrypted)
993 IMAP with SSL/TLS encryption (recommended)
IMAP Commands
Command Description
Command Description
LOGIN Authenticates the user
LIST Lists available mailboxes
SELECT Selects a mailbox to access
FETCH Retrieves emails
STORE Updates email flags (e.g., read/unread)
LOGOUT Closes the session
Advantages of IMAP
Disadvantages of IMAP
MIME is an extension of SMTP that allows emails to include multimedia content, such as:
1. When a user sends an email with attachments or formatted text, MIME encodes the
content.
2. The email is transmitted as plain text with special MIME headers describing the
format.
3. The recipient’s email client decodes the MIME message and displays it properly.
MIME Headers
Header Description
Content-Type Defines the type of content (e.g., text/html, image/png)
Content-Transfer- Specifies how the content is encoded (e.g., Base64 for
Encoding attachments)
Content-Disposition Suggests how to display the file (inline or attachment)
MIME-Version Indicates the email supports MIME (e.g., 1.0)
Example MIME Email with an Attachment
bash
CopyEdit
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="boundary123"
--boundary123
Content-Type: text/plain
--boundary123
Content-Type: image/png
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="image.png"
(iVBORw0KGgoAAAANSUhEUgAAA...)
--boundary123--
Here, the text/plain part contains the email body, and the image/png part contains an
attachment encoded in Base64.
DNS (Domain Name System) is a decentralized system that translates human-readable domain
names (e.g., www.example.com) into IP addresses (e.g., 192.168.1.1). Since computers
communicate using IP addresses, DNS allows users to access websites using easy-to-remember
names instead of numerical addresses.
DNS converts domain names into IP addresses so that browsers can load websites.
Example:
b) Load Balancing
DNS servers cache previously resolved queries to reduce response times and improve
efficiency.
Example: If you visit www.amazon.com, your local DNS resolver stores the IP for
faster future access.
The request goes to your Internet Service Provider’s (ISP) DNS resolver.
If the ISP has the record cached, it returns the IP.
If not, the ISP forwards the request to root DNS servers.
Root servers don’t know the exact IP but direct the request to a Top-Level Domain
(TLD) server (e.g., .com server).
The TLD server (e.g., .com server) directs the request to the Authoritative Name
Server for example.com.
Queries other DNS servers on behalf of the client (e.g., ISP's DNS
Recursive Resolver
server).
Root DNS Server Directs queries to TLD servers (e.g., .com, .org).
Authoritative Name Server Provides the final IP address for the domain.
TXT Record Stores text data for domain verification, security (e.g., SPF, DKIM).
Man-in-the-Middle
Attackers modify DNS responses. Use Encrypted DNS (DoH, DoT).
Attacks
DNS MESSAGE
DNS messages are used to resolve domain names into IP addresses. The DNS protocol follows a
client-server model, where a DNS client (resolver) sends a query, and a DNS server responds with a
DNS response.
Section Description
Header Contains general information about the query/response.
Question Specifies the domain name being queried.
Answer Contains the response (IP address or other record).
Authority Specifies authoritative name servers.
Additional Provides extra information (e.g., caching details).
Field Description
QNAME Domain name being queried (e.g., www.google.com).
QTYPE Type of record requested (A, MX, CNAME, etc.).
QCLASS Typically IN for Internet.
Example:
If a client wants the IP of www.example.com, the Question section will contain:
QNAME: www.example.com
QTYPE: A (IPv4 address request)
QCLASS: IN (Internet class)
Field Description
NAME Domain name being answered.
TYPE Record type (A, AAAA, MX, etc.).
CLASS Typically IN for Internet.
TTL Time-to-Live (how long to cache the response).
RDLENGTH Length of response data.
RDATA The actual response (e.g., an IP address).
Example Answer:
NAME: www.example.com
TYPE: A
TTL: 300
RDATA: 192.168.1.100
This section provides the name servers responsible for the domain.
Field Description
NAME: example.com
TYPE: NS
RDATA: ns1.example.com (authoritative server for example.com).
This section contains additional helpful information, like IP addresses of name servers.
Example:
NAME: ns1.example.com
TYPE: A
RDATA: 192.168.2.1 (IP address of the authoritative name server).
Question Section:
QNAME: www.google.com
QTYPE: A
QCLASS: IN
Answer Section:
NAME: www.google.com
TYPE: A
TTL: 300
RDATA: 142.250.190.46
Recursive Query The DNS resolver finds the answer and returns it to the client.
Iterative Query The DNS resolver refers the client to another DNS server.
Peer-to-Peer (P2P) file distribution is a method of sharing files where each participant
(peer) acts as both a client and a server. Unlike traditional client-server models, P2P
networks distribute the workload among multiple peers, making file transfers faster,
scalable, and more efficient.
BitTorrent is one of the most popular P2P file-sharing protocols designed for efficient large-scale file
distribution. Instead of downloading a file from a single server, BitTorrent allows users to download
different parts of a file from multiple peers simultaneously, making it faster and more reliable.
To share a file using BitTorrent, the uploader (seeder) creates a torrent file.
This small file contains metadata about the shared file, including:
o File name and size
o Hash values for integrity verification
o Tracker URL (helps peers find each other)
The torrent file is loaded into a BitTorrent client (e.g., uTorrent, qBittorrent).
The client contacts a tracker (a central server) to find other peers sharing the same
file.
Instead of downloading the file sequentially, BitTorrent splits it into small chunks
(pieces).
The client downloads different chunks from multiple peers simultaneously.
Once a peer downloads a chunk, it uploads (seeds) it to others.
After downloading the entire file, a user can continue sharing (seeding) with others.
The more seeders a file has, the faster the download speeds for new peers.
Components of BitTorrent
Term Description
Seeder A peer that has 100% of the file and shares it with others.
Leecher A peer that is downloading the file but hasn’t finished yet.
Magnet Link A URL-style alternative to torrent files that contains hash info to find peers.
Download Speed Slows down with more users Increases with more users
Fault Tolerance Server failure = no downloads Works even if some peers go offline
Bandwidth Efficiency Consumes high server bandwidth Uses multiple peers’ bandwidth