Skip to content

Instantly share code, notes, and snippets.

@alexhsamuel
Last active July 29, 2016 21:55
Show Gist options
  • Save alexhsamuel/0d79960c1a644f83db0215ec3cfae028 to your computer and use it in GitHub Desktop.
Save alexhsamuel/0d79960c1a644f83db0215ec3cfae028 to your computer and use it in GitHub Desktop.
IP block calculator

Overview

In this assignment, you will write a program that performs computations on IP blocks. As you may already know, Internet Protocol version 4 (IPv4, or often just IP) is the main network protocol used on the Internet.

An IP address, which identifies a computer on the Internet, is an unsigned 32-bit integer. It is traditionally written as four 8-bit numbers, with the highest (most significant) number first, separated by dots. For example, the IP number 0xACD9002E is written "172.217.0.46".

An IP block, often called a subnet, is a group of IP addresses that share the same bit prefix: all addresses in the block share the same leading (most significant) bits, in their binary representation. The block is traditionally written as an IP address followed by a slash and the number of bits in the prefix. The prefix size is between 0 and 32.

For example, "172.217.0.46/24" is the block consisting of all addresses that share the same first 24 bits with 172.217.0.46. This block contains all addresses of the form 172.217.0.x, since the first 24 bits are specified but the last 8 may take any value.

The number of addresses in the block is determined by the size of the prefix. For example, "172.217.0.46/24" contains 256 addresses.

Assignment

Please complete as much as you can in 3 hours. Don't worry if you don't complete the entire assignment.

You may use any programming language you wish. You may use general-purpose libraries, but do not use any libraries or APIs for networking or network address calculation.

Your submission will be evaluated on correctness, code quality, style, and neatness.

Part 1 - block membership

Start by writing a console (shell or terminal) program that takes two arguments on its command line: an IP block and and IP address. The program should parse the block and address in the formats described above. It should then print a message indicating whether or not the address is in the block.

For example, for this invocation, ($ is the shell prompt; ipblock is the program you will write),

$ ipblock 12.23.34.45/15 12.22.35.44

the program responds,

in block

while for this invocation,

$ ipblock 12.23.34.45/16 12.22.35.44

the program responds,

not in block

Part 2 - cleanup

Create a new GitHub repository and commit your program to it. You will submit this repo at the end of the assignment.

Once you are satisfied that your code works for basic cases, add some tests to confirm this. Which test cases are you most concerned about?

Tidy up your code, add comments for clarity, and add validation if you didn't previously. Your code should be as if you are submitting it, for production use, in a normal engineering job.

Part 3 - enumerating a block

If you still have time left, add one more feature. If your program is invoked with only a single argument, an IP block, print out all the addresses in the block.

For example, for this invocation,

$ ipblock 12.23.34.45/29

the program responds,

12.23.34.40
12.23.34.41
12.23.34.42
12.23.34.43
12.23.34.44
12.23.34.45
12.23.34.46
12.23.34.47

Note that the list will be quite large if the prefix is small; the number of addresses in the block is 2(32-prefix).

Conclusion

If—after you submit your solution!—you are curious about what IP blocks are used for, see the Wikipedia page on CIDR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment