Advertisement
savsanta

ipcalc-java

Aug 19th, 2022
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.57 KB | None | 0 0
  1. # BSD
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class IPv4 {
  7.     int baseIPnumeric;
  8.     int netmaskNumeric;
  9.  
  10.     /**
  11. * Specify IP address and netmask like: new
  12. * IPv4("10.1.0.25","255.255.255.16")
  13. *
  14. *@param symbolicIP
  15. *@param netmask
  16. */
  17.     public IPv4(String symbolicIP, String netmask) throws NumberFormatException {
  18.  
  19.         /* IP */
  20.         String[] st = symbolicIP.split("\\.");
  21.  
  22.         if (st.length != 4)
  23.             throw new NumberFormatException("Invalid IP address: " + symbolicIP);
  24.  
  25.         int i = 24;
  26.         baseIPnumeric = 0;
  27.  
  28.         for (int n = 0; n < st.length; n++) {
  29.  
  30.             int value = Integer.parseInt(st[n]);
  31.  
  32.             if (value != (value & 0xff)) {
  33.  
  34.                 throw new NumberFormatException("Invalid IP address: "+ symbolicIP);
  35.             }
  36.  
  37.             baseIPnumeric += value << i;
  38.             i -= 8;
  39.         }
  40.  
  41.         /* Netmask */
  42.         st = netmask.split("\\.");
  43.         //System.out.println("The Netmask After being Split is: " + st);
  44.  
  45.         if (st.length != 4)
  46.             throw new NumberFormatException("Invalid netmask address: "
  47.  
  48.                     + netmask);
  49.  
  50.         i = 24;
  51.         netmaskNumeric = 0;
  52.  
  53.         if (Integer.parseInt(st[0]) < 255) {
  54.  
  55.             throw new NumberFormatException(
  56.                     "The first byte of netmask can not be less than 255");
  57.         }
  58.         for (int n = 0; n < st.length; n++) {
  59.            
  60.             //System.out.println("NetmaskNumeric at iteration " + n + " is value of " + st[n] + "\n\n");
  61.            
  62.             int value = Integer.parseInt(st[n]);
  63.  
  64.             if (value != (value & 0xff)) {
  65.  
  66.                 throw new NumberFormatException("Invalid netmask address: "  + netmask);
  67.             }
  68.            
  69.             System.out.println("BEFORE --> Netmask-Numeric value is " + netmaskNumeric + " whilst var i is " + i);
  70.             System.out.println("Commencing " + value + " << " + i );
  71.             netmaskNumeric += value << i;
  72.             i -= 8;
  73.            
  74.             System.out.println("AFTER --> Netmask-Numeric value is " + netmaskNumeric + " whilst var i is " + i);
  75.  
  76.         }
  77. /*
  78. * see if there are zeroes inside netmask, like: 1111111101111 This is
  79. * illegal, throw exception if encountered. Netmask should always have
  80. * only ones, then only zeroes, like: 11111111110000
  81. */
  82.         boolean encounteredOne = false;
  83.         int ourMaskBitPattern = 1;
  84.  
  85.         for (i = 0; i < 32; i++) {
  86.  
  87.             if ((netmaskNumeric & ourMaskBitPattern) != 0) {
  88.  
  89.                 encounteredOne = true; // the bit is 1
  90.             } else { // the bit is 0
  91.                 if (encounteredOne == true)
  92.  
  93.                     throw new NumberFormatException("Invalid netmask: " +
  94.                     netmask + " (bit " + (i + 1) + ")");
  95.             }
  96.  
  97.             ourMaskBitPattern = ourMaskBitPattern << 1;
  98.         }
  99.     }
  100.  
  101. /**
  102. * Specify IP in CIDR format like: new IPv4("10.1.0.25/16");
  103. *
  104. *@param IPinCIDRFormat
  105. */
  106.     public IPv4(String IPinCIDRFormat) throws NumberFormatException {
  107.  
  108.         String[] st = IPinCIDRFormat.split("/");
  109.         if (st.length != 2)
  110.  
  111.             throw new NumberFormatException("Invalid CIDR format '"
  112.                     + IPinCIDRFormat + "', should be: xx.xx.xx.xx/xx");
  113.  
  114.         String symbolicIP = st[0];
  115.         String symbolicCIDR = st[1];
  116.  
  117.         Integer numericCIDR = new Integer(symbolicCIDR);
  118.         if (numericCIDR > 32)
  119.  
  120.             throw new NumberFormatException("CIDR can not be greater than 32");
  121.  
  122.         /* IP */
  123.         st = symbolicIP.split("\\.");
  124.  
  125.         if (st.length != 4)
  126.             throw new NumberFormatException("Invalid IP address: " + symbolicIP);
  127.  
  128.         int i = 24;
  129.         baseIPnumeric = 0;
  130.  
  131.         for (int n = 0; n < st.length; n++) {
  132.  
  133.             int value = Integer.parseInt(st[n]);
  134.  
  135.             if (value != (value & 0xff)) {
  136.  
  137.                 throw new NumberFormatException("Invalid IP address: " + symbolicIP);
  138.             }
  139.  
  140.             baseIPnumeric += value << i;
  141.             i -= 8;
  142.         }
  143.  
  144.         /* netmask from CIDR */
  145.         if (numericCIDR < 8)
  146.             throw new NumberFormatException("Netmask CIDR can not be less than 8");
  147.         netmaskNumeric = 0xffffffff;
  148.         netmaskNumeric = netmaskNumeric << (32 - numericCIDR);
  149.  
  150.     }
  151.  
  152.     /**
  153. * Get the IP in symbolic form, i.e. xxx.xxx.xxx.xxx
  154. *
  155. *@return
  156. */
  157.     public String getIP() {
  158.         return convertNumericIpToSymbolic(baseIPnumeric);
  159.     }
  160.  
  161.     private String convertNumericIpToSymbolic(Integer ip) {
  162.         StringBuffer sb = new StringBuffer(15);
  163.  
  164.         for (int shift = 24; shift > 0; shift -= 8) {
  165.  
  166.             // process 3 bytes, from high order byte down.
  167.             sb.append(Integer.toString((ip >>> shift) & 0xff));
  168.  
  169.             sb.append('.');
  170.         }
  171.         sb.append(Integer.toString(ip & 0xff));
  172.  
  173.         return sb.toString();
  174.     }
  175.  
  176. /**
  177. * Get the net mask in symbolic form, i.e. xxx.xxx.xxx.xxx
  178. *
  179. *@return
  180. */
  181.  
  182.     public String getNetmask() {
  183.         StringBuffer sb = new StringBuffer(15);
  184.  
  185.         for (int shift = 24; shift > 0; shift -= 8) {
  186.  
  187.             // process 3 bytes, from high order byte down.
  188.             sb.append(Integer.toString((netmaskNumeric >>> shift) & 0xff));
  189.  
  190.             sb.append('.');
  191.         }
  192.         sb.append(Integer.toString(netmaskNumeric & 0xff));
  193.  
  194.         return sb.toString();
  195.     }
  196.  
  197. /**
  198. * Get the IP and netmask in CIDR form, i.e. xxx.xxx.xxx.xxx/xx
  199. *
  200. *@return
  201. */
  202.  
  203.     public String getCIDR() {
  204.         int i;
  205.         for (i = 0; i < 32; i++) {
  206.  
  207.             if ((netmaskNumeric << i) == 0)
  208.                 break;
  209.  
  210.         }
  211.         return convertNumericIpToSymbolic(baseIPnumeric & netmaskNumeric) + "/" + i;
  212.     }
  213.  
  214. /**
  215. * Get an arry of all the IP addresses available for the IP and netmask/CIDR
  216. * given at initialization
  217. *
  218. *@return
  219. */
  220.     public List<String> getAvailableIPs(Integer numberofIPs) {
  221.  
  222.         ArrayList<String> result = new ArrayList<String>();
  223.         int numberOfBits;
  224.  
  225.         for (numberOfBits = 0; numberOfBits < 32; numberOfBits++) {
  226.  
  227.             if ((netmaskNumeric << numberOfBits) == 0)
  228.                 break;
  229.         }
  230.         Integer numberOfIPs = 0;
  231.         for (int n = 0; n < (32 - numberOfBits); n++) {
  232.  
  233.             numberOfIPs = numberOfIPs << 1;
  234.             numberOfIPs = numberOfIPs | 0x01;
  235.         }
  236.  
  237.         Integer baseIP = baseIPnumeric & netmaskNumeric;
  238.  
  239.         for (int i = 1; i < (numberOfIPs) && i < numberofIPs; i++) {
  240.  
  241.             Integer ourIP = baseIP + i;
  242.  
  243.             String ip = convertNumericIpToSymbolic(ourIP);
  244.  
  245.             result.add(ip);
  246.         }
  247.         return result;
  248.     }
  249.  
  250. /**
  251. * Range of hosts
  252. *
  253. *@return
  254. */
  255.     public String getHostAddressRange() {
  256.  
  257.         int numberOfBits;
  258.         for (numberOfBits = 0; numberOfBits < 32; numberOfBits++) {
  259.  
  260.             if ((netmaskNumeric << numberOfBits) == 0)
  261.                 break;
  262.         }
  263.         Integer numberOfIPs = 0;
  264.         for (int n = 0; n < (32 - numberOfBits); n++) {
  265.  
  266.             numberOfIPs = numberOfIPs << 1;
  267.             numberOfIPs = numberOfIPs | 0x01;
  268.         }
  269.  
  270.         Integer baseIP = baseIPnumeric & netmaskNumeric;
  271.         String firstIP = convertNumericIpToSymbolic(baseIP + 1);
  272.         String lastIP = convertNumericIpToSymbolic(baseIP + numberOfIPs - 1);
  273.         return firstIP + " - " + lastIP;
  274.     }
  275.  
  276. /**
  277. * Returns number of hosts available in given range
  278. *
  279. *@return number of hosts
  280. */
  281.     public Long getNumberOfHosts() {
  282.         int numberOfBits;
  283.  
  284.         for (numberOfBits = 0; numberOfBits < 32; numberOfBits++) {
  285.  
  286.             if ((netmaskNumeric << numberOfBits) == 0)
  287.                 break;
  288.         }
  289.  
  290.         Double x = Math.pow(2, (32 - numberOfBits));
  291.  
  292.         if (x == -1)
  293.             x = 1D;
  294.  
  295.         return x.longValue();
  296.     }
  297.  
  298. /**
  299. * The XOR of the netmask
  300. *
  301. *@return wildcard mask in text form, i.e. 0.0.15.255
  302. */
  303.  
  304.     public String getWildcardMask() {
  305.         Integer wildcardMask = netmaskNumeric ^ 0xffffffff;
  306.  
  307.         StringBuffer sb = new StringBuffer(15);
  308.         for (int shift = 24; shift > 0; shift -= 8) {
  309.  
  310.             // process 3 bytes, from high order byte down.
  311.             sb.append(Integer.toString((wildcardMask >>> shift) & 0xff));
  312.  
  313.             sb.append('.');
  314.         }
  315.         sb.append(Integer.toString(wildcardMask & 0xff));
  316.  
  317.         return sb.toString();
  318.     }
  319.  
  320.     public String getBroadcastAddress() {
  321.  
  322.         if (netmaskNumeric == 0xffffffff)
  323.             return "0.0.0.0";
  324.  
  325.         int numberOfBits;
  326.         for (numberOfBits = 0; numberOfBits < 32; numberOfBits++) {
  327.  
  328.             if ((netmaskNumeric << numberOfBits) == 0)
  329.                 break;
  330.         }
  331.         Integer numberOfIPs = 0;
  332.         for (int n = 0; n < (32 - numberOfBits); n++) {
  333.  
  334.             numberOfIPs = numberOfIPs << 1;
  335.             numberOfIPs = numberOfIPs | 0x01;
  336.         }
  337.  
  338.         Integer baseIP = baseIPnumeric & netmaskNumeric;
  339.         Integer ourIP = baseIP + numberOfIPs;
  340.  
  341.         String ip = convertNumericIpToSymbolic(ourIP);
  342.  
  343.         return ip;
  344.     }
  345.  
  346.     private String getBinary(Integer number) {
  347.         String result = "";
  348.  
  349.         Integer ourMaskBitPattern = 1;
  350.         for (int i = 1; i <= 32; i++) {
  351.  
  352.             if ((number & ourMaskBitPattern) != 0) {
  353.  
  354.                 result = "1" + result; // the bit is 1
  355.             } else { // the bit is 0
  356.  
  357.                 result = "0" + result;
  358.             }
  359.             if ((i % 8) == 0 && i != 0 && i != 32)
  360.  
  361.                 result = "." + result;
  362.             ourMaskBitPattern = ourMaskBitPattern << 1;
  363.  
  364.         }
  365.         return result;
  366.     }
  367.  
  368.     public String getNetmaskInBinary() {
  369.  
  370.         return getBinary(netmaskNumeric);
  371.     }
  372.  
  373. /**
  374. * Checks if the given IP address contains in subnet
  375. *
  376. *@param IPaddress
  377. *@return
  378. */
  379.     public boolean contains(String IPaddress) {
  380.  
  381.         Integer checkingIP = 0;
  382.         String[] st = IPaddress.split("\\.");
  383.  
  384.         if (st.length != 4)
  385.             throw new NumberFormatException("Invalid IP address: " + IPaddress);
  386.  
  387.         int i = 24;
  388.         for (int n = 0; n < st.length; n++) {
  389.  
  390.             int value = Integer.parseInt(st[n]);
  391.  
  392.             if (value != (value & 0xff)) {
  393.  
  394.                 throw new NumberFormatException("Invalid IP address: "
  395.                         + IPaddress);
  396.             }
  397.  
  398.             checkingIP += value << i;
  399.             i -= 8;
  400.         }
  401.  
  402.         if ((baseIPnumeric & netmaskNumeric) == (checkingIP & netmaskNumeric))
  403.  
  404.             return true;
  405.         else
  406.             return false;
  407.     }
  408.  
  409.     public boolean contains(IPv4 child) {
  410.  
  411.         Integer subnetID = child.baseIPnumeric;
  412.  
  413.         Integer subnetMask = child.netmaskNumeric;
  414.  
  415.         if ((subnetID & this.netmaskNumeric) == (this.baseIPnumeric & this.netmaskNumeric)) {
  416.  
  417.             if ((this.netmaskNumeric < subnetMask) == true
  418.                     && this.baseIPnumeric <= subnetID) {
  419.  
  420.                 return true;
  421.             }
  422.  
  423.         }
  424.         return false;
  425.     }
  426.  
  427.     public boolean validateIPAddress() {
  428.         String IPAddress = getIP();
  429.  
  430.         if (IPAddress.startsWith("0")) {
  431.             return false;
  432.  
  433.         }
  434.  
  435.         if (IPAddress.isEmpty()) {
  436.  
  437.             return false;
  438.         }
  439.  
  440.         if (IPAddress
  441.                 .matches("\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z")) {
  442.  
  443.             return true;
  444.         }
  445.         return false;
  446.     }
  447.    
  448.     public static void main(String[] args) {
  449.    
  450.  
  451.    
  452.    
  453.    
  454.    
  455.     }
  456.    
  457.    
  458. }
  459.  
  460.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement