package globalfunctions;
import java.net.*;

import org.apache.commons.net.util.*; // Depends on apache commons-net-3.3 library
public class IpAddress 
{
	public static long ipToLong(InetAddress ip) //Converts IP address to a long integer in order to make comparisons
	{
        byte[] octets = ip.getAddress();
        long result = 0;
        for (byte octet : octets) // for a variable octet defined by each element in the array octets
        {
            result <<= 8;//left shift by 8 (due to 8 bytes)
            result |= octet & 0xff;// Or the byte with the octet anded with the number 1 (0xff is hexadecimal for 1)
        }
        return result;
	}
	
	
 //***checks if the specified ip address falls in the specified network address
	
	public static boolean iprangeverify(String ipadd, String netadd) throws IllegalArgumentException // Format of netadd : xxx.xxx.xxx.xxx/xx where /xx is network mask
	{
		SubnetUtils utils = new SubnetUtils(netadd);
		return utils.getInfo().isInRange(ipadd);	
	}
	
	public static String findmac(NetworkInterface n) throws SocketException // Gives mac address of specific network interface
	{
		byte [] mac = n.getHardwareAddress();
    	StringBuilder sb = new StringBuilder();
		for (int i = 0; i < mac.length; i++) 
		{
			sb.append(String.format("%02X", mac[i]));		
		}
		return sb.toString();
	}
	
}

@@ -36,16 +36,46 @@
 a.net.*;
+%0Aimport java.util.Enumeration;
 %0A%0Aimport
@@ -1371,10 +1371,1328 @@
 );%0A%09%7D%0A%09%0A
+%09public static String current_Mac_and_IP()// returns ipadd:mac address ###Split returned string using### String %5B%5D netinfo = IpAddress.current_Mac_and_IP().split(%22:%22); netinfo%5B0%5D = mac and netinfo%5B1%5D = ip %0A%09%7B%0A%09%09boolean flag_foundinterface = false;%0A%09%09String mac = null;%0A%09%09String ipadd=null;%0A%09%09try %0A    %09%7B%0A%09%09%09Enumeration%3CNetworkInterface%3E e = NetworkInterface.getNetworkInterfaces();%0A%09%09%09while(e.hasMoreElements() && flag_foundinterface==false)%0A%09%09    %7B%0A%09            NetworkInterface n=(NetworkInterface) e.nextElement();%0A%09            if (!n.isLoopback() && n.isUp())%0A%09            %7B%0A%09            %09mac = new String(IpAddress.findmac(n));%0A%09            %09Enumeration%3CInetAddress%3E ee = n.getInetAddresses();%0A%09                while(ee.hasMoreElements())%0A%09                %7B%0A%09                %09InetAddress i= (InetAddress) ee.nextElement();%0A%09                %09boolean isIPv6 = i instanceof Inet6Address;%0A%09                %09if (!isIPv6)%0A%09                %09%7B%0A%09                %09%09flag_foundinterface=true;%0A%09                %09%09ipadd= i.getHostAddress();%0A%09            %09%09%09return mac+%22:%22+ipadd;%0A%09                %09%7D%0A%09                %09%09%0A%09                %7D%0A%09            %7D%0A%09%09    %7D%0A%09%09%09return %22You don't seem to be connected to any network or you have a non-private ip address!%22;%0A%09%09%09%7D%0A%09%09catch (SocketException e) %0A%09%09%7B%0A%09%09%09return %22Error%22;%0A%09%09%7D%0A%09%09%0A%09%7D%0A%09%0A
 %7D%0A

Return Mac address and IP address function added