OSI Model & TCP IP Model
What a networking Model is
Networking model is a just blueprint of the how data flow in network and one system to another system, this model is very important bcz in the world we have different type of devices but we need to connect each other so the OSI creates a blueprint of how data travel over the network.
The 7 OSI Layers
Physical:- This is the first layer from receiver side, this is the physical mediums like cables, WiFi signals, fiber optics.
Data Link:- This is the second layer, and it's main job is send data one to device to another with reliable and also detect any transmission errors.
Network:- Third layer, it's main job is find the actual device via the IP/Mac and send data.
Transport:- Fourth layer, This is a very important layer and it actually use two protocols TCP(Transmission control Protocol) and UDP(User Datagram Protocol) and this is layer that divide data into the packets and that is called segments.
Session:- Fifth layer, and it's main job to maintain the session between two devices. for example when i send a message to my friend on whatsapp that app is create a session between me and my friend.
Presentation:- Sixth layer, this layer main job is to translate data into user understandable bcz when we receive data over the network that data is encrypted.
Application:- Seventh layer, this is last layer of OSI and this is the layer we can see, every software is a application layer.
Real World Mapping (Whatsapp message journey through the layers)
When i send a message to my friend on the whatsapp that use the whole 7 layers. the first layer is my application layer when i type something in my friend's chat and click on enter button the message first go through the application to presentation layer and this layer is convert that message to system understandable and then the message go to session layer that layer ensure is the session manages in both devices and then the message go to transport layer and this layer add the protocol of TCP or UDP based on the packet and divide that packet into the segments and it also have ability to add encryption, decryption after this that packet go to network layer and this layer check the actual address of the receiver and then send that message to the data link layer and that data link layer send to the actual device via the physical layer. and the same thing reverse in the receiver side like physical ---> data link ---> network so on..
Mac Address
It's made globally unique by manufacturers at production time, so that no two devices ever clash, even though it's only actually used within a local network.
IPv4 Address
IPv4 have the 4 octet and every octet contain the 8 bits for example this 192.168.1.10 is the ipv4 and it have octet the 192 is 1 octet the 168 is 1 octet and so on.
Binary to decimal conversion
The first thing you need to know the ipv4 is maximum octet value is 255 bcz of the bits as you know the each octet contain the 8 bits for example:-
192.168.1.10 <------- This is ipv4
11000000.10101000.00000001.00001010 <------- This is bits
2^8 = 256
and each octet start from 0 to 255 and that's why the maximum size is 255
Subnetting
What is subnetting
Subnetting is a technique to divide one big network into small, small networks. When we have one big network then all devices are share the same broadcast domain and this creates unnecessary noise and security risk also. So subnetting solve this problem by creating a small "rooms" inside one big "building" and each room can only talk to inside devices, if two subnet want to talk to each other than we need extra configuration on router/gateway.
Subnet Mask
Subnet mask is a 32 bit number and this number decide which part of the IP is Network part and which part is Host part. For example the subnet mask 255.255.255.0 means the first 3 octet is Network and last octet is Host, and this also decide how many devices can connect on that network.
CIDR Notation
CIDR stand for Classless Inter-Domain Routing and this is short way to write the subnet mask. Instead of writing 255.255.255.0 we can just write /24, this number tell us how many bits are use for Network part. For example:
/24 = 24 bits Network, 8 bits Host = 256 total address
/26 = 26 bits Network, 6 bits Host = 64 total address
Network Address, Broadcast Address and Usable Host Range
In every subnet, 2 address are reserve and can't assign to any device:
Network Address (first address of subnet) — this is like official name of that subnet, example 192.168.1.0
Broadcast Address (last address of subnet) — this address use to send message to all device in that subnet at once, example 192.168.1.255
So the usable host range is always total address minus 2, for example in /24 we have 256 total address but only 254 is usable for real devices.
Hands-on with ipcalc
ipcalc is a tool that calculate all this information automatically, we just give the IP and CIDR and it show us Network address, Broadcast address, usable range and total host, this make subnetting calculation very easy and fast instead of manual binary math.
VLSM (Variable Length Subnet Masking)
Problem with equal size subnet
If we divide one network into equal size subnet then some department waste lot of address. For example if Finance team only have 5 employee but we give them 64 address subnet then 59 address is waste and unused.
Allocating custom subnet size
VLSM solve this problem, instead of giving equal size to every department we give exact size base on how many host they actually need. Like:
Big team (100 host) → get bigger subnet
Small team (5 host) → get small subnet
This way we save lot of IP address and reduce wastage.
Real subnet design example
For example if we have network 10.0.0.0/24 and we need to design for Web, Application, Database and Monitoring server, then we allocate subnet base on the requirement of each one, biggest requirement get subnet first and then move to next available address for next subnet, this way whole /24 network is efficiently divide between all requirement.
TCP vs UDP
Transport layer's role
Transport layer is responsible to take data from upper layer and prepare it for delivery, it break data into small piece call segments, and also make sure data reach to correct application using port number.
TCP — reliable, ordered, connection based
TCP means Transmission Control Protocol, this protocol is reliable bcz before sending real data it first do handshake to confirm both side is ready, then it send data in numbered segments and wait for acknowledgment, if any segment lost then TCP resend that segment again, this is why TCP is little slow but very reliable.
UDP — fast, connectionless
UDP means User Datagram Protocol, this protocol don't do any handshake before sending data and don't wait for any acknowledgment, if data lost in between then UDP don't resend it, this make UDP very fast but unreliable.
Real world use case
TCP is use in the place where reliability is more important than speed, example: loading website, sending email, chat message.
UDP is use in the place where speed is more important than perfect reliability, example: video call, live streaming, online gaming, DNS lookup.
TCP 3-Way Handshake
SYN, SYN-ACK, ACK
Before any real data transfer TCP first do a 3 step process call handshake:
SYN — client send request to server saying "I want to connect, here is my starting sequence number"
SYN-ACK — server reply back "I got your request, here is my starting sequence number too"
ACK — client confirm back "Got it, let's start real communication"
After this 3 step, both side is ready and real data start flowing.
Sequence numbers and acknowledgment logic
Every byte send in TCP have a sequence number, and acknowledgment number always mean "I receive up to this point, please send me next byte after this," this way both side always know what data is already receive and what is still pending.
TCP connection states
During the life of TCP connection it pass through different states:
LISTEN — server waiting for new connection
ESTABLISHED — connection is live and data is flowing
TIME-WAIT — connection just closed, system wait little bit before fully remove it
CLOSE-WAIT — other side already close connection but our side not yet close it
Hands-on with tcpdump and ss -tan
tcpdump tool let us capture real packet traveling on network, using this we can literally see SYN, SYN-ACK, ACK packet happening in real time. And ss -tan command show us all current TCP connection on our system along with their state, this help us understand and debug network connection in real environment.
Top comments (0)