Lets start from beginning -
When you install a router, it receives a public IP from the ISP on its WAN interface.
Usually, when u setup a router in your home or anywhere, basic default setting will already be set like
- DHCP enabled
- NAT enabled
- Default routing connecting LAN to WAN and back
DHCP
When a host connects via Ethernet or Wi-fi,
- The Host sends broadcast
DHCPDISCOVER
to the Network - This asks the router to assign a IP Address to itself(host)
- now when the router(DHCP server) receives the request
- the DHCP server send back a
DHCPOFFER
this contains
IP Address - host expected IP address
Subnet Mask - the local networks range
Default Gateway - if its outside the local to external IPs
DNS server - usually the router itself
Lease Time - duration of the IP address
some other parameters
- now after the host receives host responds with
DHCPREQUEST
confirming the IP Address - DHCP server confirms the assignment by sending
DHCPACK
IP Address
- Internet Protocol is a unique address that identifies a device on the internet.
- DHCP assigns a IP address selecting in a address pool
- it also checks the lease table for assigned and unassigned address
-basically two types
- IPv4(32 bit addressing)
- IPv6(128 bits addressing)
Subnet Mask
- a subnet is a portion of a larger network, basically it tells the network boundary or range of the IP address in the local subnet
- subnet mask - is 32 bit number that is used to differentiate between network and host part in the IP address
DNS
- Domain Name System is the internet phonebook mapping/translating the domain names(people can remember) to the IP addresses(what system/routers understand) via hierarchical name servers.
- server for translating the names to IP address
Lease Time
duration of the IP is valid before renewal sent when
DHCPOFFER
to host.after assigning, after sometime the host sends a renewal (
DHCPREQUEST
) request to the DHCP server to maintain the IP address
When you enter google.com in the browser, the OS checks for local cache in your system. If it is not found, the host sends a DNS query to the router. then the router forwards to the actual DNS server i.e. ISP. after that the ISP sends back a IP address back to the host.
lets say the host doesn't know the mac address of the router it only knows the IP address of the router, so it can't send a DNS lookup, so the ARP request/reply exchange is done
ARP - request/reply Process
basic analogy
- host - ARP Table (mapping of MAC address to IP Addresses)
- switch - MAC table (mapping of MAC address to switch ports)
- router - Arp table (mapping of MAC address to IP Addresses)
lets say all tables are empty
- the host sends broadcast ARP requesting the mac address of the default gateway something like this -
source IP address : 192.xx.xx.xx (host's IP)
source MAC address : 00:1A:2B:3C:4D:5E (host's mac)
destination IP address : 192.1xx.x.1 (this will be the router's IP)
destination : FF:FF:FF:FF:FF:FF(this says unknown)
the switch doesn't know, but it is a broadcasts, so it sends the message to the whole LAN and updates the mac table about the host and maps the port number.
other hosts check and discard the IP packet, because in the ARP request the destination IP address is not their owns so.
the router also gets the ARP broadcast, the router gets to know that the host wants the MAC address of the router(the default gateway). The router also updates the ARP table in router and maps mac to IP address of the host.
the router sends the unicast message back to host, as it knows the mac address of the host, by checking the ARP request. here the switch also updates the mac table by adding, the router's mac address and maps the port.
6.the host updates the ARP table and adds the MAC address
now the host has a mac address and can communicate to the internet through router.
the browser sends a an HTTP or HTTPS(secure) request to the server.
the router performs a NAT.
NAT
- it is a process of allowing the multiple private IPs to share a single public IP address to interact with the WAN or to access the internet. its in the router
- NAT contains a translation table, which contains the mapping of internal host address and port that is connected to the internet.
let me explain when a host A sends a request to a external server via router, the NAT here maps it to a port number before sending it the actual server and send it with a port number.
- Host A (192.168.1.2:5000) sends a request to a web server.
- Router replaces sourceIP:port with publicIP:10000 in NAT table. for the same server the host B also sends a request
- Host B (192.168.1.3:5000) sends a request to the same server
- Router replaces source IP:port with publicIP:10001 in NAT table.
the server replies to publicIP:10000, so router checks and forwards it to the host A.
This process is called Port Address Translation (PAT).
so, like this the NAT replaces the private IP address with the Public IP address using NAT table. when the router receives the reply from the server it check the table and forwards back to host.
Finally, the host’s transport layer checks the destination port to deliver the payload to the proper application/session(here the browser), with TCP/UDP connections potentially reused for multiple requests.
Top comments (0)