1. 程式人生 > >Computer Networks and how to actually understand them

Computer Networks and how to actually understand them

Class A: As shown in the third column of the above image, for Class A IP addresses, the first bit of the first octet of IP address is constant and is “0”.

The Second column indicates the Network bits and the host bits of the corresponding class of IP address. Consider in case of Class A IP address, we have the formula,

Number of networks/subnets = 2^(# of network bits) .

Number of valid hosts in each subnet = 2^(# of host bits) — 2 .

The number of network bits and host bits are decided by the default subnet mask of the class of IP address.

Default subnet mask for class A IP addresses is 255.0.0.0 that is 11111111.00000000.0000000.00000000`. Thus, for class A,

Network bits = 8, and Host bits = 24.

Since, Network bits = 8, Host bits = 24, (obviously their sum has to be 32, since IPv4 addresses are of 32 bits). But, since we are using the one bit (first bit in the first octet) to identify the class,

Number of usable network bits = Number of network bits — Number of constant bits = 8–1 = 7

Thus, Number of possible networks in Class A = 2^7 — 2 = 126 and,

Number of possible hosts(that is devices that can be connected to the network) per network in Class A = 2^24-2 = 16277214 .

Now, here, for class A, you may wonder why I subtracted an extra 2 from the number of possible networks? It is because, for class A, 127.x.y.z was kept reserved. For other classes, usual formula is used.

Thus, IP addresses in class A range from 1.x.x.x to 126.x.x.x.

Class B: Similar is the case with Class B. Only difference is 2 bits of the first octet are constant(10) and they identify the class of IP address that is class B. All other calculations are same and I am not mentioning them here since they are easy to grab from the table above. They range from 128.0.x.x to 191.255.x.x .

Class C: 3 bits of the first octet are constant(110) and they identify the class of IP address that is class C. They range from 192.0.0.x to 223.255.255.x .

Class D and Class E: Class D and Class E are used for experimental purposes.

IPv4 addresses are mainly of two types:

  • Static: These IP addresses are the ones which remain constant for a device over the time. Example of these are the remote servers that we use to host our apps, websites, etc. where we use the ssh client to ssh to our server.
  • Dynamic: Generally, these are the IP addresses that a common computer in an Internet network is assigned. Try switching your router off and you will see a change in the IP address of your computer! (But only after reading this article ?). Now, you may be thinking who allocates these IP addresses, it is the DHCP (Dynamic Host Configuration Protocol) server which is explained briefly further in this article.
Note: A device can have multiple IP addresses at the same time. Consider a device connected to two networks, wifi as well as any LAN network, it will have two IP addresses. This implies that the IP addresses are assigned to the interfaces and not directly to the computer.

Okay, so far so good. Let’s continue.

Routers

As its name suggests, a Router is a hardware component which takes care of routing of packets that is which node did the packet come from and which destination node does the sender node want to send it to. No computer knows where other computers are located, packets are not sent to every computer. A Router identifies the destination node address to which a network packet has to be sent and it forwards it to the desired address.

Routers have a specific “Routing Protocol” which defines the format in which they exchange data with another router or networking nodes that is routing protocol defines how routers communicate with each other.

Routers build up a “Routing Table” which identifies the best-optimized paths to be taken in the network while sending packets.

A Router.

Technically, a routing table is just a table with the list of “routes” from one router to other. Each route consists of the address of the other routers/nodes in the network and how to reach them.

Routing table:
Destination  Gateway     Genmask        Flags Metric Refs Ifacedefault      192.168.0.1 0.0.0.0        UG    1024   233  eth0192.168.0.0  *           255.255.255.0  UC    0      0    wlan0192.168.0.0  *           255.255.255.0  UH    0      2    eth0

Above is an example of a routing table. The key points to take a note of here are:

  • Destination: This is the IP address of the destination node. It indicates where the network data packet should reach at the end.
  • Gateway: Gateway is the component which connects two networks. Consider, you have a router connected to another router. Each of the routers has devices connected to it. So, the address of the last router(say R1 here) after which the network packet enters the other network(say R2’s network) is called the gateway. Usually, the gateways are nothing but the routers. Let me give one more example, consider, your room is one network and your sibling sitting next room to yours is another network, then the “door” between the two rooms can be considered as the gateway. People sometimes refer to the “routers” as the gateway, because, that’s what they are, “a gateway to another network”.
  • Genmask/Subnet mask: It is nothing but the net/subnet mask. “A subnet mask is a number which when combined with an IP address allows you to divide the IP space into smaller and smaller chunks for use in both physical and logical networks.”. The explanation of how subnet mask calculations happen is beyond the scope of this article.
  • Flags: Different flags have a different meaning. For example, in the first route, “U” in “UG” means the route is UP, whereas “G” in “UG” means GATEWAY. Since the route signifies a GATEWAY, it is a door to the other network and whenever we send any data through this route, it gets sent to another network.
  • Iface (Network interface): Network interface refers to the network that the route defined in the routing table is having the destination computer in. That is if you are connected to Wifi, then it would be “wlan” and when you are connected to a LAN, then it would be “eth”.

Thus, this is the way, a router works, with the help of Routing Protocol and Routing Table.

All good till now. But, you must be thinking that :-

“Okay! But hey, we are learning about components here. I need to glue them up and get to know how the internet works.”

Cool! Some more terms and I will glue them up for you and you will have a proper understanding of how everything goes.

Network Address Translation (NAT)

Network address translation is a technique used by routers to provide internet service to more number of devices with less usage of public IPs. Thus, a router is assigned a single IP address by the ISP and it assigns the private IPs to all the devices connected to it. NAT helps the ISPs provide internet access to more consumers.

Thus, if you are connected to the router of your house, your public IP will be visible to the world and not the private one. Whatever network packets will be communicated will be addressed by your public IP (that is the public IP assigned to the router).

Network address translation (NAT)

Consider the above figure. Let’s say that in your home network, you are trying to access medium.com(remote static IP: 72.14.204.147), from your computer(private IP: 192.168.1.100).

So, for your computer, the connection looks like:

192.168.1.100:3764172.14.204.147:80 .

“37641” is the random port number assigned by NAT router to your device/computer. (When there is network communication between daemons running on different ports on a computer, the respective port is used by NAT). Each outbound connection gets an assigned port by the NAT router.

The connection is established in NAT like:

Private IP   |PrivatePort |PublicIP |PublicPort |Remote |RemotePort
------------- ------------ --------- ----------- ------- -----------
192.168.1.100 | 37641 | 104.244.42.129 | 59273 | 72.14.204.147 | 80

But, since the outside world of the network doesn’t know about your private address, the connection looks like the following to medium.com:

104.244.42.129:5927372.14.204.147:80 .

That way, we achieve assigning a higher number of IP addresses without wasting much public IPs.

Now, when medium.com sends the response back to 104.244.42.129:59273 , it travels all the way to your home router which then looks up for the respective private IP and private port and redirects the packet to your device/computer.

Note: NAT is a generalised concept. NAT can be achieved as 1:1, 1:N where 1, N are the number of IP addresses in the network. A technique called as “IP Masquerading” is a 1:N NAT.

Dynamic Host Configuration Protocol (DHCP)

“Dynamic Host Configuration Protocol” or DHCP is responsible for assigning dynamic IP addresses to the hosts. DHCP server is maintained by ISP or previous router if there is a chain of routers to reach the host.

Thus, allocation of IP addresses is carried out by DHCP server. Generally, ISP maintains a DHCP server and the routers in our houses get assigned a public IP from the DHCP server.

Note: Whenever a router or say a DHCP server maintained by ISP or router restarts, the IP address allocation starts again and devices are allocated IPs which are different than the previous one.

Domain Name System/Server

We have already discussed that any machine is identified by the IP address.

Okay, so you are running a web server on your localhost on your machine. If you have dug up in the hosts on any Linux machine, you would have encountered that it would look somewhat like this:

127.0.0.1        localhost255.255.255.255  broadcasthost::1              localhost

which means that even if you type 127.0.0.1 in your browser’s URL bar, it would mean the same as localhost .

Similar to the above, the websites you use daily are web servers running on some remote instance/node having a static IP address. So, typing that IP address in your browser’s URL bar will take you to the website?

Yes, Surely it will. But, are you a superhuman to remember the IP addresses of thousands of sites?

NO.

Thus, there come the domains that we use, say medium.com, twitter.com, behance.net, codementor.io, etc.

“Domain Name Server is a server having huge records of domain name mapping IP addresses which searches for the domain input and returns the respective IP address of the machine on which the website you want to access is hosted.”
Domain Name System (DNS)

How does DNS work actually?

  1. DNS is managed by your ISP that is Internet service provider.
  2. When we type an URL in the address bar, the data packets travel through your router, maybe multiple routers to your ISP where your DNS server is present.
  3. DNS server present at the ISP looks up for the domain in its database. If an entry is found, then it returns it.
  4. If any entry is not found in its primary database that it maintains, the DNS server will travel through the internet to another DNS server maintained by another ISP and check if the entry is available in that another DNS server’s database. Along with returning the IP address taken from another DNS, it will update the primary database with this new entry also.
  5. Thus, sometimes(very rare) it may take for a DNS server to traverse to multiple DNS servers to get a matching entry.
  6. If after traversing a lot of DNS servers across the internet, it doesn’t get matching entry, then the DNS server throws an error indicating that the “domain name is invalid or doesn’t exist”.

Note:

The Internet Corporation for Assigned Names and Numbers (ICANN). It’s a consortium (a non-profit corporation) that manages the assignment of domain names and IP address ranges on behalf of the community.

A domain is divided into three parts as shown in the following figure.

  1. Protocol: The protocol used to access the website, for example, HTTP, HTTPS, etc.
  2. Domain name: The main domain name in our domain. This can be anything that is available as per ICANN registry.
  3. Domain extension: This is one which is considered important while buying a domain. Generally, it is classified into two types:
  • Generic Top-level Domains (gTLDs): This includes most popular domain extensions like .com, .org, .net, .edu, .co, etc.
  • Country Code Top-level Domains(ccTLDs): These indicate that the domain is related to the country code specified in the domain extension. For example, “.in” indicates that the website is originated from India. Also, some of the ccTLDs require that the person purchasing the domain should be from the same country. Most of the small country code extensions are not searchable from outside that country.

Internet Service Providers (ISPs)

Internet Service Providers are the companies that provide everyone Internet. The article you are reading now is because of the internet that your ISP provides you.

ISPs provide internet, handle routing your requests to the correct destination, resolve domain names with the help of DNS cache that they maintain and handle all this network infrastructure which enables us to use the internet.