All Things Techie With Huge, Unstructured, Intuitive Leaps

Java Networking

I have this issue where I need to know the IP address or some identifying part of a computer.  Using Java, I came across the class of java.net.NetworkInterface.  Since there may be several LAN adapters on a computer, I wanted to find a single identifying feature that could be used to positively identify who sent something.

I implemented the following code to see what comes out of it:

String ip;
   try {
       Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
       while (interfaces.hasMoreElements()) {
           NetworkInterface iface = interfaces.nextElement();
           // filters out 127.0.0.1 and inactive interfaces
           if (iface.isLoopback() || !iface.isUp())
               continue;
           System.out.println("HW" + iface.getHardwareAddress());

           Enumeration<InetAddress> addresses = iface.getInetAddresses();
           while(addresses.hasMoreElements()) {
               InetAddress addr = addresses.nextElement();
               
               ip = addr.getHostAddress();
               System.out.println(iface.getDisplayName() + " " + ip);
           }
       }
   } catch (SocketException e) {
       throw new RuntimeException(e);
   }

The very first thing that comes back out of this piece of code is very, very interesting.  It returns the name of the LAN network adapter that was actively used.  Concatenated to that was the byte fe80: which identifies the IPV6 and the unique MAC Address of that computer.  Here's the interesting bit -- immediately preceding the fe80 is the name of the SSID that is being used.  This not only reveals the computer MAC address but also the network (and the place of the network) that is connected to.

Hope this helps someone.

No comments:

Post a Comment