Networking Basics

Its been a while that docker or container technologies are threatening the way we are delivering software. Cloud platforms are offering the capabilities which were only possible for the powerful and rich (ok, may be I could have written “were hard to get”). Networks, their administration and management are being abstracted away and the software developers who were not very much concerned with infrastructure in first place couldn’t be more happier and why not? They can focus more on developing what matters most and that is, a software to handle business problems.

Before cloud offerings and container technologies, everything was close to metal and you had no other choice but to learn. As time has progressed, this stuff has been abstracted more and more. so thats easier and more productive to work with. Thats the paradox of technological progress. Things become easier to work with but harder to understand (e.g. a car which is easier to drive but hard to understand its mechanics for a general driver).

However, I still believe having a general understanding of computer networking is still beneficial even if you are fully invested in software development and in this post I will try to explain some of the general but powerful concepts related to networking and in later posts we will build upon these concepts and show its practical usage.

Previously I have written a post about TCP/IP, Ports and network communication and you can read that one on this link. However today’s post is more high level and I think more simple to understand. So let’s start.

What is Protocol

A protocol defines the format and the order of the messages exchanged between two or more communicating entities, as well as the actions taken on the transmission and/or receipt of a message or other events.

The Transmission Control Protocol(TCP) and the Internet Protocol(IP) are the two of the most important protocols that are sent and received among the routers and end systems. The internet’s principle protocols are collectively known as TCP/IP.

What is an IP address?

For one device to communicate with another, it needs an IP address, and it must be unique. If there is another device on the same network with the same IP there will be an IP address conflict and both devices will lose network capability until this is resolved.

The IP address consists of 4 numbers separated by decimals. The IP address itself is separated into a network address and a host address. This means that one part of the IP address identifies the computer network ID and the other part identifies the host ID.

As an example, an IP address of 192.168.0.11 with subnet mask(discussed later) 255.255.255.0 uses the first 3 numbers to identify the network and the last number to identify the host. So, the network id would be 192.168.0 and the host id would be 11.

Devices can only communicate with other devices on the same network id. In other words, communication will work between 2 devices with IPs 192.168.0.221 and 192.168.0.11 respectively but neither can communicate with 192.168.1.31 because it is part of the 192.168.1 network.

What is Subnet Mask

As a general rule wherever there is a 255 in the subnet mask then the corresponding number of the IP address is part of the network id; where there is 0 in the subnet mask the corresponding number in the IP address is part of the host id.

For an IP address of 192.168.0.1 with a subnet mask of 255.0.0.0. This tells the device that the first number of the IP address is to be used as the network address and the last 3 are to be used as the host id.

In above example, the computer network would be 192.x.x.x. As long as another computer has the same subnet mask and an IP address starting with 192 they can communicate with each other.

If the subnet mask was 255.255.0.0 then this means that the first 2 numbers identify the network instead (192.168.x.x). Therefore to be on the same network both devices must have IP addresses starting with 192.168.

How do devices on different networks communicate?

Communication across different network IDs take place with the help of a router.

A router is a network device with 2 network interfaces (NICs), each being on separate network ids.

So, we may have 2 networks; 192.168.1.x and 192.168.2.x. On one NIC the router would have the IP address 192.168.1.1 and on the other, it would have an IP address of 192.168.2.1. Devices on the 192.168.1.x network can now communicate with devices on the 192.168.2.x network via the router.

How does the data travel from origin end system to destination end system?

The default gateway is where a network device sends traffic to if it doesn’t know where the destination IP address. The default gateway is always a router.

When a network device tries to communicate with another on the same network it sends the data directly to it. If it is on a separate network it forwards the data to whatever IP address is specified in the default gateway.

This is because it doesn’t know of this other network and it needs to send the data to a gateway out of its own network. This is why we always put the IP address of the router in the default gateway field.

Because a router will be attached to multiple networks, so it knows where these other networks are and it can route traffic to them. Routers also have default gateways so that if they don’t know where the destination is then they can also send the data to its own default gateway.

This continues up the IP network hierarchy until it eventually finds a router that is part of the destination network. This last router knows where the destination is and sends it on its way.

What is DHCP?

Network devices need to be configured with an IP address, subnet mask and default gateway that will be unique to that network. Generally, we don’t manually configure them but are configured automatically using DHCP servers. DHCP stands for Dynamic Host Configuration Protocol.

Servers and some routers can be configured to act as a DHCP server. It allots the IP addresses to the connecting devices so to prevent IP address conflicts.

How does a router function?

A router should have at least two network cards (NICs), one physically connected to one network and the other physically connected to another network. A router can connect any number of networks together providing it has a dedicated NIC for each network.

Routers also learn which are the fastest routes and use them first. Each route the router knows of has a metric value assigned to it. A metric value is basically a preference number. If there are two routes to the same destination then the one with the lowest metric is assumed to be the most efficient. Routers will always use this route first until it fails, in which case it will then try the route with the next lowest metric and so on.

All network devices that use the TCP/IP protocol have a routing table. On Linux based system run netstat -rn command to view this table (If on Windows then I will say please its time to switch to linux). All devices use their routing table to determine where to send packets.

When a device sends packets to another device, it looks at its routing table to determine the best route possible. If it finds the destination address is “on-link” it knows that it is a part of the same subnet as the destination and sends the packets directly to the device. If not it forwards the packet onto whatever is in the gateway field of the matching route entry. This same process is repeated at every router/hop along the way until it eventually arrives at a router that is part of the destination network.

How can we configure a router?

To configure a router we have to access the software installed on it. We can do this by using a web browser. The IP address of the router is the default gateway of the computer. It generally starts with host id 1, example: 192.168.1.1.

When we enter the router IP address on the browser, it asks for the username and password. Normally they are mentioned on the bottom of the router and are generally username: admin and password: admin.

What is a port?

A port that we generally heard of are used to connect monitor, webcam, speakers etc. On the physical layer, a computer port is a specialized outlet on a piece of equipment to which a plug or cable connects.

But the port that we are referring to from the internet or software perspective has a different meaning. In the internet protocol suite, a port is an endpoint of communication in an operating system, in software, it is a logical construct that identifies a specific process or a type of network service.

A port is always associated with an IP address of a host and the protocol type of the communication, and thus completes the destination or origination network address of a communication session.

A port is identified for each address and protocol by a 16-bit number, commonly known as the port number. For example, an address may be “protocol: TCP, IP address: 1.2.3.4, port number: 80”, which may be written 1.2.3.4:80 when the protocol is known from context.

What is port forwarding?

Port forwarding or port mapping is an application of network address translation (NAT) that redirects a communication request from one address and port number combination to another while the packets are traversing a network gateway, such as a router or firewall.

This technique is most commonly used to make services on a host residing on a protected or masqueraded (internal) network available to hosts on the opposite side of the gateway (external network), by remapping the destination IP address and port number of the communication to an internal host.

What this mean is that when a request arrives at a router at a specific port then it reroutes this request to a server in the local network of the router so to be able to process it (if configured on NAT settings). So, it in a way works as a gatekeeper.

IP Address and Domain Name

The IP address on the web server in a number like 123.123.123.123 but how does http://www.example.com be able to communicate with that server?

example.com is called as a Domain name. Domain names are the human-friendly forms of Internet addresses and are commonly used to find websites.

The domain name system(DNS) is essentially a global addressing system. It is the way that domain names are located and translated into Internet Protocol (IP) addresses, and vice versa.

A domain name such as example.com is a unique alias for an IP address http://123.123.123.123:80, which is an actual physical point on the Internet.

We can buy a domain name like I did with hexquote.com from a vendor for a period and then tell that domain to point to an actual server address using the websites of those vendors.

What is public IP address?

A public IP address is an IP address that our home or business router receives from your ISP.

Public IP addresses are required for any publicly accessible network devices, like for our home router as well as for the servers that host websites.

Public IP addresses are what differentiate all devices that are plugged into the public internet. Each and every device that’s accessing the internet is using a unique IP address. It’s this address that each Internet Service Provider uses to forward internet requests to a specific home or business.

Summary

As you noticed that networking itself is not that complicated and most of the time it seems hard due to some very technical noise in its mentions. That’s true that it is a technical topic but its understandable. Having a basic understanding of these concepts will help you very often in your career. I will write another post where we will apply some of these concepts in a practical fashion. Till next time, Happy coding.

References

  • https://hexquote.com/pretty-little-things-tcp-ip-ports-and-sockets/
  • https://afteracademy.com/blog/how-to-convert-your-laptop-desktop-into-a-server-and-host-internet-accessible-website-on-it-part-1-545940164ab9