Everything you should know about IP addresses as a Developer

Fakhreddine Messaoudi
Nerd For Tech
Published in
6 min readJul 9, 2024

Ipv4 and Ivp6

I have no idea how to start this article, I only want to share my experience with this issue I have encountered while playing with toy problems on Code Wars as usual.

Today I will try to explain things I have learned while trying to solve this coding game which is a bit hard I think, for someone who does not have enough knowledge about IP addresses.

Let’s get started

I do have a reminder on my phone that tells me to solve a toy problem at least once a day. So I landed on This Kata that calls Count IP Addresses

What a beautiful KATA. I have absolutely 0 idea besides that I know that IP addresses are used to address the device that is connected to the network and assign the location of that device to it.
Well, my knowledge is really bad and I thought for a while what if I had an interview asking me about IPs what would I do? What would you do? I would show that I am a noobie developer for sure. Some of the common questions for an engineer would be this.

Perhaps The common issue for developers is that they focus on learning technologies and new stacks and forget about the basics such as protocols and so on which I think is not a best practice.

Count IP Addresses

Here is the KATA:

Implement a function that receives two IPv4 addresses,
and returns the number of addresses between them
(including the first one, excluding the last one).
All inputs will be valid IPv4 addresses in the form of strings. 
The last address will always be greater than the first one.
Examples* With input "10.0.0.0", "10.0.0.50" => return 50
* With input "10.0.0.0", "10.0.1.0" => return 256
* With input "20.0.0.10", "20.0.1.0" => return 246

I had to do some research to understand it better and I am going to share what I have learned so far about IP’s.

IP Address:

An IP (Internet Protocol) address is a unique identifier assigned to each device connected to a network that uses the Internet Protocol for communication. It serves two main purposes: identifying the host or network interface and providing the location of the host in the network.

Types of IP Addresses

IPv4 (Internet Protocol version 4):

  • Format: Consists of four decimal numbers (each ranging from 0 to 255) separated by dots (e.g., 192.168.1.1).
  • Address Space: 32-bit, providing around 4.3 billion unique addresses.
  • Example: 172.16.254.1

IPv6 (Internet Protocol version 6):

  • Format: Consists of eight groups of four hexadecimal digits separated by colons (e.g., 2001:0db8:85a3:0000:0000:8a2e:0370:7334).
  • Address Space: 128-bit, providing a vast number of unique addresses (approximately 3.4 x 1⁰³⁸).
  • Example: 2001:0db8:85a3:0000:0000:8a2e:0370:7334

How IP Addresses are Generated

Static IP Addresses:

  • Assignment: Manually assigned by network administrators.
  • Usage: Typically used for servers, printers, and other critical devices that need a fixed address.

Dynamic IP Addresses:

  • Assignment: Automatically assigned by a DHCP (Dynamic Host Configuration Protocol) server.
  • Usage: Commonly used for end-user devices like smartphones, laptops, and desktops.
  • Lease Time: The DHCP server assigns an IP address for a specific lease time, after which it can be renewed or reassigned.

Public IP Addresses:

  • Assignment: Assigned by Internet Service Providers (ISPs) and are unique across the entire internet.
  • Usage: Used for devices directly connected to the internet, such as routers or servers.

Private IP Addresses:

  • Assignment: Assigned within a private network and are not routable on the internet.
  • Usage: Used within local networks, such as home, office, or enterprise environments.
  • Private Ranges:
  • IPv4: 10.0.0.0 to 10.255.255.255, 172.16.0.0 to 172.31.255.255, 192.168.0.0 to 192.168.255.255
  • IPv6: fc00::/7 (Unique Local Address)

How IP Addresses Work in a Network

Subnetting:

  • Concept: Divides a large network into smaller, manageable subnetworks.
  • Usage: Helps in efficient IP address allocation and improves network performance and security.

NAT (Network Address Translation):

  • Concept: Allows multiple devices on a local network to share a single public IP address.
  • Usage: Commonly used in routers to manage private IP addresses within a local network and translate them to a public IP address for internet access.

Example Scenarios

  • Home Network: Your router is assigned a public IP address by your ISP. Within your home network, the router uses DHCP to assign private IP addresses (e.g., 192.168.1.2) to your devices.
  • Corporate Network: A company’s network might be divided into subnets (e.g., 192.168.1.0/24 for one department and 192.168.2.0/24 for another) for better management and security.

Now Let’s see how to solve this KATA after we learned all of this information together.

To implement a function that calculates the number of IPv4 addresses between two given addresses (including the first address and excluding the last one), we can break the problem down into the following steps:

  • Convert IPv4 addresses to their numerical equivalents: An IPv4 address is composed of four octets separated by dots (e.g., 192.168.1.1). Each octet represents an 8-bit number. By converting each octet to its numerical value and then combining these values, we can represent the IPv4 address as a single 32-bit integer.
  • Calculate the difference between the two numerical addresses: Once we have both addresses as integers, the difference between the two will give us the number of addresses between them.

Step-by-Step Implementation

Convert an IPv4 address to a 32-bit integer:

  • Split the address into its four octets.
  • Convert each octet to its integer representation.
  • Combine the octets into a single 32-bit integer using bitwise operations.

Calculate the number of addresses between the two integer representations:

  • Subtract the integer value of the first address from the integer value of the second address.

We need to create two separate functions for this:

function ipToInt(ip) {
const octets = ip.split('.').map(Number);
return ((octets[0] << 24) >>> 0) + ((octets[1] << 16) >>> 0) + ((octets[2] << 8) >>> 0) + (octets[3] >>> 0);
}
function ipsBetween(start, end) {
const startInt = ipToInt(start);
const endInt = ipToInt(end);
return endInt - startInt;
}
// Example usage
const firstIp = "192.168.1.1";
const lastIp = "192.168.1.10";
console.log(ipsBetween(firstIp, lastIp)); // Output: 9

Problem Understanding:

The task is to implement a function that calculates the number of IPv4 addresses between two given IPv4 addresses. The first address is inclusive, and the last address is exclusive.

Steps to Solve the Problem

Convert IP Address to Integer Representation (ipToInt Function):

  • IPv4 addresses are typically represented in a dotted-decimal format (e.g., “192.168.0.1”).
  • Each octet (segment of the address separated by dots) can range from 0 to 255.
  • To perform arithmetic operations easily, convert each octet into a 32-bit integer.
  • The function ipToInt takes an IPv4 address string and converts it into a single 32-bit unsigned integer.

Calculate Number of Addresses Between Two IPs (ipsBetween Function):

  • This function uses the ipToInt function to convert both the start and end IPv4 addresses into their integer representations.
  • It then calculates the difference between the end integer and the start integer.
  • Since the end IP address is exclusive, subtracting the start IP integer from the end IP integer gives the number of addresses between them.

Handling Edge Cases:

  • The solution handles edge cases where the addresses are at the boundaries of IPv4 space (e.g., “0.0.0.0” to “255.255.255.255”).
  • JavaScript’s bitwise operators (<<, >>>) are used to ensure correct handling of unsigned 32-bit integers, avoiding negative results and maintaining accuracy.

Explanation of Key Functions:

ipToInt(ip) Function:

  • Splits the IP address into octets using .split('.').
  • Maps each octet to a 32-bit integer and combines them using bitwise left-shift (<<) and unsigned right-shift (>>>) operations.
  • Ensures the result is a non-negative 32-bit integer by using >>> 0.

ipsBetween(start, end) Function:

  • Converts both start and end IP addresses to integers using ipToInt.
  • Calculate the difference between endInt and startInt to determine the number of addresses between them.

Example and Test Cases

The provided test cases ensure that the function handles various scenarios correctly, including:

  • Small ranges ("150.0.0.0" to "150.0.0.1"),
  • Larger ranges crossing over octet boundaries ("10.0.0.0" to "10.0.1.0"),
  • Edge cases ("0.0.0.0" to "255.255.255.255").

Thanks, you reached in here today. I haven’t shared anything for a while, I hope you enjoyed it and share it with your friends for more knowledge.

--

--

Fakhreddine Messaoudi
Nerd For Tech

Was man shreibt, bleibt. Support my writing with a simple follow and clapp!