//Author: ^-^ Veerle ^-^ //Does all the calculations public class subnetCalcEngine { //The addresses are always stored in decimal protected String[] strNetworkAddress = {"0","0","0","0"}; protected String[] strHostAddress = {"0","0","0","0"}; protected String[] strIPAddress = {"0","0","0","0"}; protected String[] strSubAddress = {"0","0","0","0"}; protected boolean usingDec = true; protected int numHostBits = 0; public void subnetCalcEngine () { } protected String[] format (String[] strAddressReference, boolean setAddress) { //Have to make a separate variable to store a copy of the address in, otherwise //it would change the origional address. This is because String is an object, //instead of passing a copy of the value, it passes a reference. String[] strAnswer = new String[4]; for (int section = 0; section < 4; section++) { //Can't directly copy the String array, instead have to do each section strAnswer[section] = strAddressReference[section]; } if (setAddress) //If setting the address to be stored in the engine { if (!usingDec) //If the number is binary, change it to decimal { for (int section = 0; section < 4; section++) { strAnswer[section] = convertBinToDec(strAnswer[section]); } } } else //If getting a copy of the address to output { if (!usingDec) //If you want the number to be binary { for (int section = 0; section < 4; section++) { strAnswer[section] = convertDecToBin(strAnswer[section]); } } } return strAnswer; } private boolean checkSubAddress () //Checks the subnet address is legal { //Once a value other then 255 is used, the remaining sections can only be 0 boolean subnetEndReached = false; int decNum = 0; for (int section = 0; section < 4; section++) { decNum = Integer.parseInt(strSubAddress[section]); if (subnetEndReached && decNum != 0) { return false; } else { //Only these values are legal subnet numbers switch (decNum) { case 0: case 128: case 192: case 224: case 240: case 248: case 252: case 254: subnetEndReached = true; break; case 255: break; default: return false; } } } return true; } protected boolean performCalculation () //Calculates the network and host addresses { if (!checkSubAddress()) { return false; } else { String[] strIP = new String[4]; String[] strSub = new String[4]; for (int section = 0; section < 4; section++) { strIP[section] = convertDecToBin(strIPAddress[section]); strSub[section] = convertDecToBin(strSubAddress[section]); } strIP = addPadding(strIP); strSub = addPadding(strSub); String[] strNetwork = new String[4]; String[] strHost = new String[4]; boolean endOfNetwork = false; numHostBits = 0; for (int section = 0; section < 4; section++) { strNetwork[section] = ""; strHost[section] = ""; for (int bit = 0; bit < 8; bit++) { if (endOfNetwork) { strNetwork[section] += "0"; strHost[section] += strIP[section].charAt(bit); numHostBits++; } else { if (strSub[section].charAt(bit) == '1') { strNetwork[section] += strIP[section].charAt(bit); strHost[section] += "0"; } else { endOfNetwork = true; strNetwork[section] += "0"; strHost[section] += strIP[section].charAt(bit); numHostBits++; } } } } for (int section = 0; section < 4; section++) { strNetworkAddress[section] = convertBinToDec(strNetwork[section]); strHostAddress[section] = convertBinToDec(strHost[section]); } return true; } } private String[] addPadding (String[] strCurrentAddress) { for (int section = 0; section < 4; section++) { strCurrentAddress[section] = addPadding2(strCurrentAddress[section]); } return strCurrentAddress; } private String addPadding2 (String strCurrentAddressSection) { for (int padding = (8 - strCurrentAddressSection.length()); padding > 0; padding--) { strCurrentAddressSection = "0" + strCurrentAddressSection; } return strCurrentAddressSection; } private String convertDecToBin (String strDec) { int decNum = Integer.parseInt(strDec), length = 0; String strBinNum = "", answer = ""; while (decNum != 0) { if (decNum > 1) { strBinNum += Integer.toString(decNum % 2); decNum /= 2; } else { strBinNum += Integer.toString(decNum); decNum = 0; } } length = strBinNum.length(); for (int padding = (8 - length); padding > 0; padding--) { answer += "0"; } for (int i = length; i > 0; i--) { answer += strBinNum.charAt(i - 1); } return answer; } private String convertBinToDec (String strBin) { for (int padding = (8 - strBin.length()); padding > 0; padding--) { strBin = "0" + strBin; } int counter = 1, decNum = 0; for (int bit = 7; bit >= 0; bit--) { if (strBin.charAt(bit) == '1') { decNum += counter; } counter *= 2; } return "" + decNum; } }