Finding IPAddress belongs to same subnet

             We (me, Sree Pratheep, Parthi) along with some of our friends will usually go out for Sunday dinner. Last week Senthil joined us and made the dinner a special occassion:).We went to Tummy Full @ Velachery, Chennai, India. As a regular trend, they too have some puzzles in the table paper to be solved. But these guys went a step ahead and provided us with a Pencil, awesome perfect customer engagement!! This kindled my thought and gave me an impulse to write (to say the truth, scribble) something on the paper (for sure no idea to solve the puzzles given). As I started my scribbling, our genius Sree put an open question, “Given two IP Addresses and a subnet how will you programmatically identify is the two IP Addresses belongs to same subnet”. As usual I gave non-stop answers, but none of them are perfect to the problem. Our junior genius Parthi, gave out the key of the solution, which is “If bitwise AND of IPAddress and Subnet results in same value for any two IPAddresses then it means that those two IPAddresses are in the same subnet”, Awesome, Hats off to Parthi. Since I got the key for the solution, I was able to frame the answers. This time Sree gave one other optimized way for getting the byte array of an IPAddress through, InetAddress.getByName(ipAddress). So now we have all the needed data, the end is to just collate them as a solution, I did it using Java and now it’s for you

import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author veechand
**/
public class IPAddressCheck {
  public static void main(String[] args) {
    try {
       byte[] firstAddressInBytes = InetAddress.getByName(args[0]).getAddress();
       byte[] secondAddressInBytes = InetAddress.getByName(args[1]).getAddress();
       byte[] subnetAddressInBytes = InetAddress.getByName(args[2]).getAddress();
       int position = 0; byte secondByte; byte subnetByte;
       for (byte oneByte : firstAddressInBytes) {
           secondByte = secondAddressInBytes[position];
           subnetByte = subnetAddressInBytes[position++];
          if ((oneByte & subnetByte) != (secondByte & subnetByte)) {
            System.out.println("Both the IP Address (" + args[0] + ","+ args[1] + ") are NOT in the same network ("+ args[2] + ")");
            System.exit(0);
          }
       }
     System.out.println("Both the IP Address (" + args[0] + ","+ args[1] + ") are in the same network ("+ args[2] + ")");
     }catch(UnknownHostException e){
       e.printStackTrace();
     }
 }
}

That looks good, though I haven’t tested much on this. The first optimization that comes to my mind as I see the above piece of code is refactoring the message string shown in output. Only the NOT is diff between the two messages. Though for this kind of sample/test program this doesn’t seem to be a big deal, for a production standard program this refactoring will help a lot in maintenance, Trust me I have felt the pain of not refactoring.

At last, after few minutes of “food for thought” we had just few hours of “food for stomach” :). We came out with the pencil that kindles our thought, shhhhhh don’t let the hotel owner know this 🙂

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Furl

Advertisements

2 Responses

  1. If ip1 = 192.168.9.6/24 , ip2 = 192.168.9.4/16 and given mask is 255.255.0.0 , then the program says both ips are in the same subnetwork, but it is actually not .. they are in 192.168.9.0 and 192.168.0.0 networks. !!!

  2. @anonymous (@parthi) they “>are in 192.168.9.0 and 192.168.0.0” that’s not as per the input given to the program as per the input 192.168.9.6/16 and 192.168.9.4/16 are in the same subnet… Catch is only one subnet you can give as input to the program 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: