Computer Networks: OSI layers, what are those?

Currently we have a huge variations of communication systems; cable internet, telephone networks (cellular or landline), starlink network, interplanetary communication networks and many more. Wide variation of hardware and software protocols are used to build these systems. Also different systems are designed for different use cases. For example, one engineer trying to design a walkie-talkie aims to transfer audio data in short distance with wireless technology while another engineer trying to design a internet network might have to worry about large distance communication and also reliability of data transfer.

Even though each communication system is targeted for different domain use cases, they share lots of common problems like,

  1. How can we send data reliably?
  2. How can we send data to a certain device? or send to all of the devices?
  3. What if we want to transfer sensitive data? how can we protect the data we sent through our communication channel?
  4. How do we make sure the data sent is accurately?

Be it the celluar network or the internet network, these common set of communication problems are applicable to all the networks/system that aims to achieve a data transfer. So, with the set of common problems and a single goal to achieve, OSI model was proposed as a theoritical model to group a set of functionalities required for a network system. OSI has classified the functionalities as 7 different groups/layers; each layers aim to solve certain class of problems. Also OSI is a theoritical model and not every network system fits/should fit in perfectly with OSI model.

So let's quickly revise 7 layers of OSI model and what kind of problems they address.

  1. Layer 7: Application layer

    The protocol in this layer is responsible for gathering data from user and represent the data as per application.

    Example:
    The internet, the web browser is the Layer 7 application. Web browser is responsible for gathering data user inputs and convert it to a http request and it also gets to decide how to display the data/website from the http response. Http protocol works on this layer.
    Radio network(walkie-talkie), the driver software for speaker and microphone can be claimed as the Layer 7 protocols.

  2. Layer 6: Presentation Layer

    The protocol in this layer is responsible for following tasks:

    • how to represent the data we receive from the Layer 7.
    • also encrypt data to secure the data(optional)
    • compress data if desired(optional) 

    Example:

    Cable internet, the data from http are represented either as text(ascii). however the compression and encryption are already handled in application layer by http protocol. so there is no really a different protocol at this layer for cable internet.
    Radio network(for Walkie-talkie), the data representation is a analog signal picked up by the microphone and we may want to further process that data to be noise resilient or maybe compress by downsampling. The protocol used to process the raw analog signal from microphone is the Layer 6 protocol.

  3. Layer 5: Session Layer

    OSI states that, this layer will have the responsibility of maintaining sessions; opening, closing and maintain connection between communicating nodes.

    Example:
    Cable internet doesn't have a specific component that is in session layer. The advantage of having a session layer protocol is not much as compared to having separate communication stream. So this layer is unused in cable internet.
    Radio network (Walkie-talkie): Same for Walkie-talkie network, in my view we won't have any software that works on Layer 5

  4. Layer 4: Transport Layer

    This layer takes the data from Session layer, breaks it down to segments, pass the segments to Network layer(layer 3) to be transmitted. While on receiving end, it receives the segments from Network layer, check if segment is corrupted and optionally re-request if it is corrupted. This layer also includes a flow control mechanism to adjust transmission rate based on bandwidth available. 

     Example:
    For cable internet, TCP or UDP protocols handles the transport layer work. The protocols takes care of packet re-transmission, error check and flow control.
    For radio network
    , it might as well use the TCP or UDP protocol or we can also develop another protocol on that can handle the re-transmission, flow control depending on use case.

  5. Layer 3: Network layer

    Now we have segments of data (and reliability of TCP) from Transport layer, Network layer deals with where to send the data or how to route it to the receiver. Network layer device/protocol is responsible for adding the destination address to each packet, and send it to Layer 2 protocol for transmission.

    Example:

    For cable internet, IP protocol is the Network layer protocol, it adds IP addresses to data and sends it to Data Link layer device for transmission.
    For celluar network
    , every mobile device has a unique identifier. When we communicate from user1 to user2, I assume each packet data might be tagged with user2 as target and sent to layer 2 protocol for transmission on this layer.

  6. Layer 2: Data Link Layer

    Now our data packets have source and destinations attached, we now need to find the nearest node that can transmit the packet in direction of the destination. Data link layer facilitates the transfer of packets between two connected nodes. Devices are identified based on interfaces and physical MAC addresses on this layer. This layer adds physical address of nearest available node on top of layer 3 packet and sends to layer 1 for transmission.

    Example:

    For cable internet, after OS generates the IP packets, it now attaches the MAC address(physical address) of nearest router and sends the appended packet to the NIC card for transmission. Layer 2 protocol on Router then removes the old layer 2 header and looks for another nearest router, attach nearest router's physical address(MAC address) and forwards the packet.
    For radio network, the communication language between two towers operates on this layer.

  7. Layer 1: Physical Layer

    Now we have data to transfer between two connected device on network, now we need a channel for transfer the signals and protocols to encode data to the signal. Physical layer deals with how we transfer the data; maybe using wifi protocol or bluetooth or fiber optics or electric signal via wire or other radio signals or lasers.

    Example:
    For cable internet,
    NIC card generates electric pulses based on data from Data Link layer and sends it through cable.
    For radio network
    , the data gets converted to radio wave based on a Layer 1 protocol and the transmitter transmits the data using radio frequency signal to nearest tower. 

To summarize, we have following

OSI Layer Task Cable Internet Where is the protocol implemented
Layer 7: Application Layer
Format Data with respect to application
HTTP protocol
Application on top of OS
Layer 6: Presentation Layer
Encode, compress and encrypt data
-

Layer 5: Session Layer
Maintain session for a single connection
-

Layer 4: Transport Layer
Ensure data transmission is loss less, uncorrupted, reliable and within the limit of the bandwidth of channel
TCP
OS provides libraries
Layer 3: Network Layer Add source and target destination of final recipent
IP
OS
Layer 2: Data-link Layer
Add the source and target for nearest available network device
MAC
OS / Routers
Layer 1: Physical
Receive data from layer 2 and encode it as per transmission media
NIC/Cable
Routers/ Channel

Examples

So let's walk through a chat application application, what would our network architecture look like if we compare it with the OSI model ?

For an chat application, 

  1. First for the Application layer we can use normal text as a payload layer 7 on application layer, user sends or receives normal text, so text is the payload (payload: "Hello") 
  2. Layer 6 on presentation layer, we can write a program to encrypt and compress the payload from layer 7 (payload: "h930a") 
  3. Layer 5 on session layer, we can skip this layer.(payload: "h930a") 
  4. Layer 4 on Transport layer, now depending on our requirement we can choose to add TCP header or UDP header to the payload. it takes care of reliablility and flow control of the data (payload: (TCP headers + ("h930a"))) 
  5. Layer 3 on Network layer, now the data from layer 4 is broken down into transferable data chunks and ip headers are added to each chunks. IP headers adds the source/destination address to each packet (IP headers + (TCP headers + ("h930a"))) 
  6. Layer 2 on Data link layer, now we look for the nearest physical device and add the transimission data headers to send the data to that device (MAC header + (IP headers + (TCP headers + ("h930a")))) 
  7. Layer 1 Physical Layer: the data from layer 2 is received, converted to signals using layer 1 protocol and transmitted through wire to destination device(identified by MAC) 
  8. After the data reaches layer 1 to transmit the data to the desired client, we will have intermediate routers working on Layer 1 and Layer 2 to navigate the data until we reach the final destination. On reaching the destination the process is reveresed 
  9. Layer 1, Physical Layer, receives data (MAC header + (IP headers + (TCP headers + ("h930a")))) 
  10. Layer 2, DataLink Layer, removes MAC header from the payload (IP headers + (TCP headers + ("h930a"))) 
  11. Layer 3, Network Layer, removes the IP headers and waits for all the data packets to arrive (TCP headers + ("h930a")) 
  12. Layer 4, Transport Layer, removes the TCP headers and verify we receive all the data reliably ("h930a") 
  13. Layer 5, Session Layer, no used ("h930a") 
  14. Layer 6, Presentation Layer, decompress and decrypt data ("hello") 
  15. Layer 7, Application layer, receive the data and show it to user ("hello") 

A Chat application on top of Starlink Network 

If we want to use the Starlink Network to communicate, 

  1. We can still use Layer 3-7 to achieve the routing, flow control, reliability, data encryption and compression. 
  2. However Starlink Network will differ on Layer 1 and 2. In Layer 2, after we receive IP packet from Layer 3, now we have to find nearest satelite to communicate with, and add the physical address of that satelite to the IP packet. 
  3. Layer 1: now we use layer 1 protocol to convert data to signal and can use the narrow beam antennas to transmit the data to the satelite. 
  4. After Satelite receives the data, it can either choose satelite to satelite communication (using lasers) or satelite to base station communication(narrow band). Either way, satelite has to remove the layer2 header on the received data and add new layer2 header with next satelite to transmit it forward. 
  5. But finally one of the Satelite will choose to finally transmit it to ground station then the ground station can use the ground based routers to forward the package into the ground based internet using same protocols as cable internet. Here the satelite-to-satelite and satelite-to-ground communication protocol operates on Layer 1 and 2. ( Starlink network's architecture https://www.youtube.com/watch?v=m05abdGSOxY)

OSI model for Interplanetary Communication (Preserverance mars rover)

The Interplanetary Communication network mainly consist of rovers, orbiters, and ground stations in earth. Rover collects the data sends it to orbiters and orbiter sends it to earth ground stations. Rovers can also establish direct link to earth stations to transfer the data. Different from normal network in earth, delay between the signal is huge in the Interplanetary Network, so we need different protocol that takes the delay in consideration. With the combined effort, CCSDS space link protocols have been used for the space communication. CCSDS includes improvement on the Layer 3, 2 and 1, to better suit the space communication and also have some improvements for the protocols above the Transport layer 4 to support the store-and-forward information network to address frequent disconnects, errors and delays. You can find more about the Interplanetary Communication here

Interesting Stuff: Dynamic Content in Github Readme.md file

Github badges are cool and lots of github repo use shields.io to generate one. With shields.io, we can also create our own custom badges like this one that has been used for a nepali calendar events repo

The github badges are just a static image on web that we can include in our markdown file. Shields.io also provides dynamic badges (dynamic images) generated by some webserver. Getting to know about dynamic github badges, got an idea to have today's date on one of open source BS Date conversion repo (pyBSDate) that I currently maintain. The idea is similar to dynamic github badges, we can generate a dynamic image based on today's BS date and place it in the Readme file for the repo. So every time we go to the pyBSDate repo we will get to see today's BS date. Sounds super cool, right? Let's get it done.

Things we need:

  1. First thing, we need is a tool that can generate an image with a dynamic text(for our case a Nepali BS date text).
  2. After we have that script, we can add that script to a cron job that would run every day to get a new image for that day.
  3. Finally we can serve that image using a webserver and have it placed in a Readme.md file as an image.

Hmmmm. Imagemagick library can do step number 1 and we can run a cronjob and a webserver for step 2 and 3 on our server.

After some hours of hit and trial and lots of duckduckgo, following script perfectly generates a image with dynamic date on a gif file.


convert -size 1000x140 xc:white -font Mukta-Light -pointsize 70 -fill '#bf092d' -gravity East -draw "text 200,0 \"`PYTHONIOENCODING=UTF-8 python -c \"from pyBSDate import bsdate;print(bsdate.today().strftime('%B %d %Y, %A', 'ne'))\"`\" " null: \( nepal_flag.gif -coalesce \) -gravity East   -layers Composite -layers Optimize today.gif

Now we can use a cronjob to automatically run that script once in a day at 00:00 UTC time using following cron script


0 0 */1 * * convert -size 1000x140 xc:white -font Mukta-Light -pointsize 70 -fill '#bf092d' -gravity East -draw "text 200,0 \"`PYTHONIOENCODING=UTF-8 python -c \"from pyBSDate import bsdate;print(bsdate.today().strftime('%B %d %Y, %A', 'ne'))\"`\" " null: \( nepal_flag.gif -coalesce \) -gravity East   -layers Composite -layers Optimize today.gif

And finally serve our dish (today.gif) using a webserver. Following is the final image we'll get and is supposed to have today's BS date.

Final product is also included in Readme.md file of the pyBSDate repository.

Stay hungry, stay foolish fellas.

PS: pyBSDate is an opensource python library to convert between BS date and AD date. The library is compactible with python's native date library. If you are interested in contribution to the project, feel free to send us the pull requests or just drop us an email to know about the development.

My Blog Season 2

A long long gap since my last blog post and don't know if I can be consistent about maintaining an active blog but here it goes. Let's call it "My Blog Season 2". We'll be learning some new things this lockdown season for sure and will be blogging tooo. Stay tuned folks, lots of exciting things on the list !!!

Mathematics : Finding Pi using Spigot algorithm

Irrational numbers are interesting bit of mathematics. Those numbers can't be written in fractions. Their decimal goes on like forever without repeating. Pi, e, Golden Ratio are some of well known irrational numbers. And Pi is the subject of interest here. "Pi is an infinite, non-repeating decimal -- meaning that every possible number combination exists somewhere in pi. Converted into ASCII text, somewhere in that infinite string of digits is the name of every person you will ever love, the date, time, and manner of your death, and the answers to all great questions of the universe." (Copied that one from internet :D )

Intresting Stuff : Turn your local desktop to a webserver

Lets try some cool networking stuff. How about establishing a private hosting server on your local machine. In this post, we will be discussing about how to make your localhost accessible from public network. That is pretty much like having your own web server. Generally the basic requirements for establishing web servers are very high speed computer with very good internet connection (high uplink) and a static IP address. However none of these mentioned requirements can prevent you from establishing a temporary webserver. You can get one right on your desktop.

Code recipe : Download Manager (Python Version)

Recently I had published my blog post Building a Download Manager(in JAVA). And I am presenting the python implementation of download manager. The whole concept is same except the language of implementation. You can check the older JAVA implementation here.
Python has nice libraries urllib and urllib2 we can use for our purpose. urllib2 provides nice feature of request header modification. Here's my recipe python version.

Interesting Stuff : Sleep sort

A new sorting algorithm, don't know who invented it but it works well atleast for positive integers. You can follow the link for original version of the article on web http://dis.4chan.org/read/prog/1295544154 . The algorithm is simple. For sorting a list of positive numbers, simply create a thread for each one of them and then set out the thread for sleep some constant times the number assigned to it. And finally print out the numbers as threads wakes up from the sleep.