Network Programming: The OSI Model
Tags: networking, programming, computerscience, osi
Published: 2023-10-25 03:31:33

## Table of Contents
&nbsp;1. [INTRODUCTION](#introduction)
&nbsp;2. [Networking Model](#networking-model)
&nbsp;3. [Layers of the OSI model](#layers-of-the-osi-model)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.1. [Layer 7: Application Layer](#layer-7-application-layer)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.2. [Layer 6: Presentation Layer](#layer-6-presentation-layer)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.3. [Layer 5: Session Layer](#layer-5-session-layer)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.4. [Layer 4: Transport Layer](#layer-4-transport-layer)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.5. [Layer 3: Network Layer](#layer-3-network-layer)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.6. [Layer 2: Data Link Layer](#layer-2-data-link-layer)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.7. [Layer 1: Physical Layer](#layer-1-physical-layer)

&nbsp;4. [Conclusion](#conclusion)

&nbsp;5. [Reference](#reference)

## INTRODUCTION

In my [last](https://dev.to/jothamntekim1/network-programming-the-computer-network-312m) article, I talked about how different hosts can communicate with the help of a computer network. I gave background information on computer networks, their types, and their importance in distributed systems.

In this article, I will walk you through the 7 Layers of the **OSI model**, and in subsequent articles, we will explore the different network protocols.

Before we get right into it, here are a few points to keep in mind as you follow along:

- **Hosts**: any device that sends or receives traffic(data) - desktop, laptop, phones, printers, servers, Smart TV, Smart Watches, Speakers etc. They are often referred to as **nodes**. So when I mention hosts or computers later in this article, I am referring to no specific device(s). 

- **Network:** consists of two or more computers that are linked to share resources (such as printers and CDs), exchange files, or allow electronic communications. The computers on a network may be linked through cables, telephone lines, radio waves, satellites, or infrared light beams.


- **Protocol** is a set of rules defining how software and devices in a network should work including how they should work together. These are *logical rules** though, not physical ones. 



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xenjv5z6acrd8avdpja5.png)



The devices on the left may be able to communicate with each other and those on the right too, but those on the left won't be able to communicate with those on the right due to differences in standards. However various devices are expected to communicate with each other. To enable such communication, hence **Networking Models**.

## Networking Model

**Networking model** is a set of guidelines and standards that defines how data is transmitted and received **over** a network. It is also known as a **networking architecture** or a **networking blueprint**. It provides a common framework for network devices and software to communicate with each other.

Networking models categorise and provide a structure for various networking standards and protocols.

The **OSI model** is one attempt at **standardization**, created by **International Organisation of Standardization**(ISO).

## Layers of the OSI model
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ebm12lojqwudvhzozvrx.png)

Network Functions are divided into 7 layers.

### Layer 7: Application Layer
 
- It is the layer closest to the end users. Software applications - web browsers, for example, interact with this layer.
 
- HTTP and HTTPS protocols fall under this layer. 

It does not include the application itself(internet browser in this situation), but rather just the protocols(HTTP and HTTPS).

Functions:
  - Identifying communication partners - figuring destination host(s) IP address(es) in the network.
  - Synchronizing communications

Let's say an internet browser wants to send data into the system on the right(image below). The data is processed through the OSI stack, each layer adding something to the original data(Encapsulation - the original data encapsulated inside the additional information added on). By the time it reaches the physical layer, it is electrical signals to be sent to the destination host(s). The destination system then reverses the process by de-encapsulating the data, stripping off all the additional information added at each layer until it reaches the final layer(application layer). 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3cosa5p1rlyccpzoz76.jpeg)


Interaction between layer 7 in the two systems(host) is called same-layer interaction. It is what enables the application layer to identify communication partners and synchronize communication.

### Layer 6: Presentation Layer

- This layer is responsible for data formatting, such as character encoding and conversions, and data encryption.

Data in the application layer is in application format, one that only the application understands. It thus has to be translated to a format before being sent to the network layer. The presentation layer's job is to translate between application and network format. For example, encryption of data before being sent to the network layer for processing and decryption before being received by the application layer in the destination host. 

### Layer 5: Session Layer

This layer establishes, maintains, and terminates **sessions**.

A **session** is a mutually agreed upon connection that is established between two network applications.

So a **session** is a connection that is established between two **specific** end-user applications. This connection can be in two forms:

- **Client and server model**: the application requesting the information is called the client, and the application that has the requested information is called the server.

- **Request and response model**: while a session is being established and during a session, there is a constant back-and-forth of requests for information and responses containing that information or a response like, “Hey, I don’t have what you’re requesting.”

Sessions may be open for a very short amount of time or a long amount of time.

So this layer...

-  Controls dialogue(session) between communicating hosts.

- Establishes, manages, and terminates the connection between local application(e.g. Your internet browser) and remote application(e.g. YouTube).

For each established connection, a session is created. Session Layer helps to manage all sessions.

**NB:** Network engineers don't usually work with the top three layers, rather application developers do - to connect their applications over the network.

### Layer 4: Transport Layer

- This layer segments and reassembles data for communication between end hosts.

Breaks large sets of data into smaller segments which can be more easily sent over the network with a low possibility of causing transmission problems even if an error occurs. It is why videos skip for a second and continue just fine if a particular segment is not sent, unlike when not segmented, the whole video cannot be watched.

- It provides host-to-host(end-to-end/process-to-process) connections.

- It supports connection-oriented(TCP) and connectionless(UDP) communication.

In the layer, the packaged data is called **segment** - **Data** + **layer 4 header**(L4 Header).

### Layer 3: Network Layer

- It provides connectivity between end hosts on different networks(i.e. hosts outside the LAN). 

- Provides path selection - routing with the aid of routers. It provides Logical addressing(IP addressing).

Routers are the workhorse of Layer 3 - we couldn’t have Layer 3 without them. They move data packets across multiple networks. 

Routers do not only connect to Internet Service Providers (ISPs) to provide access to the Internet, they also keep track of what’s on its network same way switches keep track of all MAC addresses on a network (Switches exist in Layer 2), what other networks it’s connected to, and the different paths for routing data packets across these networks.

 The packaged data at this stage is called **packet** - Data + layer 4 header + layer 3 header. 
The IP address is included in this layer.

### Layer 2: Data Link Layer

- This layer defines how data is formatted for transmission, how much data can flow between nodes, for how long, and what to do when errors are detected in this flow.

It provides node-node connectivity and data transfer(for example, PC to Switch, Switch to Router or Router to Router). It defines how data is formatted for transmission over a physical medium(copper UTP cables for example). It detects and possibly corrects physical layer errors. It uses layer 2 addressing, which is separate from layer 3 addressing. **Switches** operate at this layer. 

The packaged data is called **Frame** - Layer 2 trailer + Data + L4 header + layer 3 header + layer 2 header.

### Layer 1: Physical Layer

- Layer 1 contains the infrastructure that makes communication on networks possible.

It defines the physical characteristics of the medium used to transfer data between devices - for example, voltage level, maximum transmission distances, physical connectors, cable specification etc. 

There’s a lot of technology in Layer 1 - everything from physical network devices, and cabling, to how the cables hook up to the devices. Plus if we don’t need cables, what the signal type and transmission methods are (for example, wireless broadband)?

Layer 1 defines the electrical, mechanical, procedural, and functional specifications for activating, maintaining, and deactivating physical links between network devices.

The data unit on Layer 1 is the **bit**.

A bit is the smallest unit of transmittable digital information. Bits are binary, so either a 0 or a 1. Bytes, consisting of 8 bits, are used to represent single characters, like a letter, numeral, or symbol.

- At this level, digital bits are converted into electrical(for wired connections) or radio(for wireless connections) signals.
 

The complete frame is then sent from one host to the other through ethernet cable for example or wireless broadband. The Data Link Layer translates the raw physical layer into a frame  ... The layer 2 header and trailer are removed at layer 2, leaving a packet. Layer 3 removes the layer 3 header, leaving the layer 4 segment, and layer 4 removes the layer 4 header leaving the original data prepared by the upper layers(top 3 layers) of the original device(De-encapsulation).

**NB:** Data + Segment + Packet + Frame are all called **Protocol Data Units(PDUs)**.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrx2skh5qw4zubnbd2eh.jpeg)

## Conclusion

We've discussed the need for Networking models, and the part each layer of the OSI model plays in ensuring communication within and/or over a network. Recall, that we agreed in the last article that the hardware part of a computer network is beyond the scope of this article series so our focus would be on the first 3 Layers(Application, Presentation and Session layers) of the OSI model since they are more concerned with the Software part of a computer network. But since I've always been intrigued to know how IP addressing works - subnetting and masking, I'll be making an exception for the transport and network layer.

In the next article, we will talk about an interesting topic - **IP addresses**, a Layer 3 Protocol. It is one of the reasons behind my focus in **Network programming** and the need to write these articles.

## Reference

https://www.freecodecamp.org/news/osi-model-networking-layers-explained-in-plain-english/#:~:text=There%20are%20two%20distinct%20sublayers,have%20the%20same%20MAC%20address.

https://www.youtube.com/watch?v=t-ai8JzhHuY

https://www.computernetworkingnotes.com/ccna-study-guide/what-is-a-networking-model-explained.html