Thread: [Javanetsim-cvs] javaNetSim/core/protocolsuite/tcp_ip DHCPD.java, 1.12, 1.13 IPV4Address.java, 1.9,
Status: Beta
Brought to you by:
darkkey
From: QweR <qw...@us...> - 2008-10-29 19:36:40
|
Update of /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip In directory fdv4jf1.ch3.sourceforge.com:/tmp/cvs-serv12987/core/protocolsuite/tcp_ip Modified Files: DHCPD.java IPV4Address.java Log Message: dhcp exclude was added some commands was fixed Index: DHCPD.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/DHCPD.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** DHCPD.java 24 Oct 2008 17:46:42 -0000 1.12 --- DHCPD.java 29 Oct 2008 19:36:21 -0000 1.13 *************** *** 231,235 **** if(Network.equals(PoolNetwork)){ ! if(!leases.containsKey(IP)){ l.IP = IP; l.Genmask = p.Genmask; --- 231,235 ---- if(Network.equals(PoolNetwork)){ ! if(!leases.containsKey(IP) && !isExcludes(IP)){ l.IP = IP; l.Genmask = p.Genmask; *************** *** 419,421 **** --- 419,430 ---- } + private boolean isExcludes(String ip){ + boolean result = false; + for(int i=0; i<exclude.size() && !result; i++){ + Pair ips = exclude.get(i); + result = IPV4Address.isBetween(ip, (String)ips.getFirst(), (String)ips.getSecond()); + } + return result; + } + } Index: IPV4Address.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/IPV4Address.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** IPV4Address.java 26 Oct 2008 15:37:40 -0000 1.9 --- IPV4Address.java 29 Oct 2008 19:36:21 -0000 1.10 *************** *** 25,29 **** TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ! */ package core.protocolsuite.tcp_ip; --- 25,29 ---- TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ! */ package core.protocolsuite.tcp_ip; *************** *** 47,57 **** private static final String CLASS_B_BINARY ="11111111111111110000000000000000"; private static final String CLASS_C_BINARY ="11111111111111111111111100000000"; ! //These are the class types public static final int CLASS_A_TYPE = 1; public static final int CLASS_B_TYPE = 2; public static final int CLASS_C_TYPE = 3; ! public static final int NO_CLASS = 4; ! /** * Creates a new IPV4Address object containing the IP address saved in binary --- 47,57 ---- private static final String CLASS_B_BINARY ="11111111111111110000000000000000"; private static final String CLASS_C_BINARY ="11111111111111111111111100000000"; ! //These are the class types public static final int CLASS_A_TYPE = 1; public static final int CLASS_B_TYPE = 2; public static final int CLASS_C_TYPE = 3; ! public static final int NO_CLASS = 4; ! /** * Creates a new IPV4Address object containing the IP address saved in binary *************** *** 64,86 **** setIp(inDecimal); } ! ! public boolean isBroadcast(){ ! //binaryIpAddress ! String[] ip = getDecimalIp().split("\\."); ! Integer firstoctet = new Integer(ip[0].trim()); ! ! if(firstoctet.intValue() == 255){ ! return true; ! }else{ ! return false; ! } ! } ! ! ! public static boolean isValidIp(final String ip) ! { ! return ip.matches("^[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}$"); ! } ! /** * This method returns the binary subnetmask in decimal --- 64,86 ---- setIp(inDecimal); } ! ! public boolean isBroadcast(){ ! //binaryIpAddress ! String[] ip = getDecimalIp().split("\\."); ! Integer firstoctet = new Integer(ip[0].trim()); ! ! if(firstoctet.intValue() == 255){ ! return true; ! }else{ ! return false; ! } ! } ! ! ! public static boolean isValidIp(final String ip) ! { ! return ip.matches("^[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}$"); ! } ! /** * This method returns the binary subnetmask in decimal *************** *** 105,109 **** String decimalip = new String(); String[] binary = new String[4]; ! for (int i = 0, c = 0; i < 32; i=i+8, c++) { binary[c] = inBinaryIpAddress.substring(i,i+8); --- 105,109 ---- String decimalip = new String(); String[] binary = new String[4]; ! for (int i = 0, c = 0; i < 32; i=i+8, c++) { binary[c] = inBinaryIpAddress.substring(i,i+8); *************** *** 114,132 **** return decimalip; } ! ! public static boolean IPEqLower(String IP1, String IP2){ ! String IPOct1[] = IP1.split("\\."); String IPOct2[] = IP2.split("\\."); ! for(int i = 0; i < 4; i++){ if(Integer.valueOf(IPOct1[i]) - Integer.valueOf(IPOct2[i]) < 0) return false; } ! return true; } ! /** * This method will return the binary version of an IPV4 address or subnet mask --- 114,132 ---- return decimalip; } ! ! public static boolean IPEqLower(String IP1, String IP2){ ! String IPOct1[] = IP1.split("\\."); String IPOct2[] = IP2.split("\\."); ! for(int i = 0; i < 4; i++){ if(Integer.valueOf(IPOct1[i]) - Integer.valueOf(IPOct2[i]) < 0) return false; } ! return true; } ! /** * This method will return the binary version of an IPV4 address or subnet mask *************** *** 142,173 **** String[] ip = inDecimalIp.split("\\."); // Split string into array String binaryAddress = new String(); ! for (int i = 0; i < ip.length; i++) { ! Integer octet = new Integer(ip[i].trim()); ! ! //This tests every octet within the inDecimalIp ! //to make sure the user hasnt entered a Negative value. ! //This code fixs bug #1056000 ! if(octet.intValue() < 0) { ! throw new InvalidIPAddressException("Invalid IP Address, cant use Negative IP address"); ! } ! ! if (octet.intValue() <= 255){ ! ! StringBuffer temp = new StringBuffer(Integer.toBinaryString(octet.intValue())); ! if(temp.length() !=8){ ! ! while(temp.length() != 8){ ! temp.insert(0,0); ! } ! } ! binaryAddress = binaryAddress + temp; ! }else ! throw new InvalidIPAddressException("Invalid IP Address"); } ! return binaryAddress; } ! /** * This class is called when an ip address is set. It then set the default --- 142,173 ---- String[] ip = inDecimalIp.split("\\."); // Split string into array String binaryAddress = new String(); ! for (int i = 0; i < ip.length; i++) { ! Integer octet = new Integer(ip[i].trim()); ! ! //This tests every octet within the inDecimalIp ! //to make sure the user hasnt entered a Negative value. ! //This code fixs bug #1056000 ! if(octet.intValue() < 0) { ! throw new InvalidIPAddressException("Invalid IP Address, cant use Negative IP address"); } ! ! if (octet.intValue() <= 255){ ! ! StringBuffer temp = new StringBuffer(Integer.toBinaryString(octet.intValue())); ! if(temp.length() !=8){ ! ! while(temp.length() != 8){ ! temp.insert(0,0); ! } ! } ! binaryAddress = binaryAddress + temp; ! }else ! throw new InvalidIPAddressException("Invalid IP Address"); ! } ! return binaryAddress; } ! /** * This class is called when an ip address is set. It then set the default *************** *** 193,201 **** return CLASS_C_BINARY; } ! **/ return "00000000000000000000000000000000"; ! } ! /** --- 193,201 ---- return CLASS_C_BINARY; } ! **/ return "00000000000000000000000000000000"; ! } ! /** *************** *** 209,213 **** String[] ip = inDecimalIP.split("\\."); Integer firstoctet = new Integer(ip[0].trim()); ! if (firstoctet.intValue() >=1 && firstoctet.intValue() <=127){ //test for class A return IPV4Address.CLASS_A_TYPE; --- 209,213 ---- String[] ip = inDecimalIP.split("\\."); Integer firstoctet = new Integer(ip[0].trim()); ! if (firstoctet.intValue() >=1 && firstoctet.intValue() <=127){ //test for class A return IPV4Address.CLASS_A_TYPE; *************** *** 219,227 **** return IPV4Address.CLASS_C_TYPE; }else { ! return IPV4Address.NO_CLASS; ! } //return 0; UGLY FIX } ! /** * This method will return a Decimal Subnet Mask. --- 219,227 ---- return IPV4Address.CLASS_C_TYPE; }else { ! return IPV4Address.NO_CLASS; ! } //return 0; UGLY FIX } ! /** * This method will return a Decimal Subnet Mask. *************** *** 233,237 **** */ public static String getDefaultSubnetMask(String inDecimalIp){ ! if(IPV4Address.getDefaultSubnetMaskClassType(inDecimalIp) == IPV4Address.CLASS_A_TYPE) return IPV4Address.toDecimalString(IPV4Address.CLASS_A_BINARY); --- 233,237 ---- */ public static String getDefaultSubnetMask(String inDecimalIp){ ! if(IPV4Address.getDefaultSubnetMaskClassType(inDecimalIp) == IPV4Address.CLASS_A_TYPE) return IPV4Address.toDecimalString(IPV4Address.CLASS_A_BINARY); *************** *** 242,247 **** return null; //This should never happen } ! ! /** * This method set the ip address in binary form. It then sets --- 242,247 ---- return null; //This should never happen } ! ! /** * This method set the ip address in binary form. It then sets *************** *** 261,267 **** throw new InvalidIPAddressException("Invalid IP Address, this address is reserved for the loopback device"); } ! String[] ip = inDecimalIp.split("\\."); //split string into an array ! for (int i = 0; i < ip.length; i++) { try { --- 261,267 ---- throw new InvalidIPAddressException("Invalid IP Address, this address is reserved for the loopback device"); } ! String[] ip = inDecimalIp.split("\\."); //split string into an array ! for (int i = 0; i < ip.length; i++) { try { *************** *** 270,275 **** // throw new InvalidIPAddressException("Invalid IP Address, cant use Subnet Mask as IP address"); //} ! // FIXME ! if (i == 0){ //test first octect for 0 eg 0.1.2.3 is an invalid address if (octet.intValue() == 0){ --- 270,275 ---- // throw new InvalidIPAddressException("Invalid IP Address, cant use Subnet Mask as IP address"); //} ! // FIXME ! if (i == 0){ //test first octect for 0 eg 0.1.2.3 is an invalid address if (octet.intValue() == 0){ *************** *** 288,292 **** throw new InvalidIPAddressException("Invalid IP Address, invalid hostID"); } ! /** * This method returns the number of bits used for the network ID of an IP Address --- 288,292 ---- throw new InvalidIPAddressException("Invalid IP Address, invalid hostID"); } ! /** * This method returns the number of bits used for the network ID of an IP Address *************** *** 299,303 **** return inBinarySubnetMask.lastIndexOf("1")+1; } ! /** * This method check to see if the host ID of an IP address is valid --- 299,303 ---- return inBinarySubnetMask.lastIndexOf("1")+1; } ! /** * This method check to see if the host ID of an IP address is valid *************** *** 317,328 **** if(inBinaryIPAddress.length() == 32){ String temp = inBinaryIPAddress.substring(SubnetBits,32); ! if(temp.indexOf("1") != -1){ ! return true; //the address is valid ! } ! return false; } ! throw new InvalidIPAddressException("Invalid ip address"); } ! /** * This method returns the number of bits used in the network ID of an IP Address --- 317,328 ---- if(inBinaryIPAddress.length() == 32){ String temp = inBinaryIPAddress.substring(SubnetBits,32); ! if(temp.indexOf("1") != -1){ ! return true; //the address is valid ! } ! return false; } ! throw new InvalidIPAddressException("Invalid ip address"); } ! /** * This method returns the number of bits used in the network ID of an IP Address *************** *** 334,338 **** return binarySubnetMask.indexOf("0"); //-1 } ! /** * This method returns a decimal string containing the ip address --- 334,338 ---- return binarySubnetMask.indexOf("0"); //-1 } ! /** * This method returns a decimal string containing the ip address *************** *** 344,348 **** return IPV4Address.toDecimalString(binaryIpAddress); } ! /** * This method overrides the toString method. Please note that --- 344,348 ---- return IPV4Address.toDecimalString(binaryIpAddress); } ! /** * This method overrides the toString method. Please note that *************** *** 352,361 **** * @version v0.20 */ ! //TODO: Finish overriding to string method with more details about the object. @Override public String toString(){ return IPV4Address.toDecimalString(binaryIpAddress); } ! /** * This method returns the binary string of the IP address --- 352,361 ---- * @version v0.20 */ ! //TODO: Finish overriding to string method with more details about the object. @Override public String toString(){ return IPV4Address.toDecimalString(binaryIpAddress); } ! /** * This method returns the binary string of the IP address *************** *** 367,371 **** return binaryIpAddress; } ! /** * This method sets a custom subnetMask it will check if the current address --- 367,371 ---- return binaryIpAddress; } ! /** * This method sets a custom subnetMask it will check if the current address *************** *** 381,392 **** */ public void setCustomSubnetMask(String inDecimalSubnetIp) throws InvalidSubnetMaskException{ ! try{ ! String binSubnetMask = IPV4Address.toBinaryString(inDecimalSubnetIp); ! binarySubnetMask = binSubnetMask; ! }catch(Exception e){} /* try{ String binSubnetMask = toBinaryString(inDecimalSubnetIp); ! if(ClassType == CLASS_A_TYPE){ if(binSubnetMask.substring(0,8).equals("11111111")){ --- 381,392 ---- */ public void setCustomSubnetMask(String inDecimalSubnetIp) throws InvalidSubnetMaskException{ ! try{ ! String binSubnetMask = IPV4Address.toBinaryString(inDecimalSubnetIp); ! binarySubnetMask = binSubnetMask; ! }catch(Exception e){} /* try{ String binSubnetMask = toBinaryString(inDecimalSubnetIp); ! if(ClassType == CLASS_A_TYPE){ if(binSubnetMask.substring(0,8).equals("11111111")){ *************** *** 423,429 **** throw new InvalidSubnetMaskException("Invalid Subnet Mask"); } ! */ } ! /** * This method takes a substring of the Subnet mask and validates --- 423,429 ---- throw new InvalidSubnetMaskException("Invalid Subnet Mask"); } ! */ } ! /** * This method takes a substring of the Subnet mask and validates *************** *** 446,452 **** return true; } ! return false; } ! /** * This method returns a binary string of the subnet mask --- 446,452 ---- return true; } ! return false; } ! /** * This method returns a binary string of the subnet mask *************** *** 458,462 **** return binarySubnetMask; } ! /** * This method returns the subnet mask in a decmial string --- 458,462 ---- return binarySubnetMask; } ! /** * This method returns the subnet mask in a decmial string *************** *** 468,472 **** return IPV4Address.toDecimalString(binarySubnetMask); } ! /** * This is a static method that will validate the IP Address --- 468,472 ---- return IPV4Address.toDecimalString(binarySubnetMask); } ! /** * This is a static method that will validate the IP Address *************** *** 482,490 **** return false; } ! ! if(inDecIPAddress.contains("255.255.255.255")){ ! return true; ! } ! for (int i = 0; i < ip.length; i++){ try { --- 482,490 ---- return false; } ! ! if(inDecIPAddress.contains("255.255.255.255")){ ! return true; ! } ! for (int i = 0; i < ip.length; i++){ try { *************** *** 503,523 **** }catch(NumberFormatException e){ return false; ! } ! try { ! //This code check to make sure the host id of an ipaddress is also valid. boolean x = IPV4Address.checkHostId(IPV4Address.toBinaryString(IPV4Address.getDefaultSubnetMask(inDecIPAddress)), IPV4Address.toBinaryString(inDecIPAddress)); ! if(x){ return true; } return false; ! } catch (Exception e) {} //This should never happen! (fingers crossed) } return true; // if it has passed all of these tests return true } ! /** --- 503,523 ---- }catch(NumberFormatException e){ return false; ! } ! try { ! //This code check to make sure the host id of an ipaddress is also valid. boolean x = IPV4Address.checkHostId(IPV4Address.toBinaryString(IPV4Address.getDefaultSubnetMask(inDecIPAddress)), IPV4Address.toBinaryString(inDecIPAddress)); ! if(x){ return true; } return false; ! } catch (Exception e) {} //This should never happen! (fingers crossed) } return true; // if it has passed all of these tests return true } ! /** *************** *** 546,550 **** try { String binSubnetMask = toBinaryString(inDecimalSubnetIp); ! if(getDefaultSubnetMaskClassType(inDecIPAddress) == CLASS_A_TYPE){ if(binSubnetMask.substring(0,8).equals("11111111")){ --- 546,550 ---- try { String binSubnetMask = toBinaryString(inDecimalSubnetIp); ! if(getDefaultSubnetMaskClassType(inDecIPAddress) == CLASS_A_TYPE){ if(binSubnetMask.substring(0,8).equals("11111111")){ *************** *** 564,568 **** } return false; ! }else{ if(binSubnetMask.substring(0,24).equals("111111111111111111111111")){ --- 564,568 ---- } return false; ! }else{ if(binSubnetMask.substring(0,24).equals("111111111111111111111111")){ *************** *** 577,596 **** return false; } ! */ } ! ! public static String IPandMask(String binIP, String binMask){ ! String b1, b2, b3; ! String result = ""; ! for (int i = 0; i < 32; i++) { b1 = binIP.substring(i,i+1); ! b2 = binMask.substring(i,i+1); if(b1.contains("1") && b2.contains("1")) b3="1"; ! else b3="0"; ! result = result + b3; } ! return result; ! } ! /** * This method will test the passed in ipaddress to see --- 577,596 ---- return false; } ! */ } ! ! public static String IPandMask(String binIP, String binMask){ ! String b1, b2, b3; ! String result = ""; ! for (int i = 0; i < 32; i++) { b1 = binIP.substring(i,i+1); ! b2 = binMask.substring(i,i+1); if(b1.contains("1") && b2.contains("1")) b3="1"; ! else b3="0"; ! result = result + b3; } ! return result; ! } ! /** * This method will test the passed in ipaddress to see *************** *** 605,635 **** String subIP; //Substring of the current ip address String subInIP; //substring of the passed in ip address ! ! try { ! int i = getNetworkIDBits(); ! ! subIP = IPV4Address.IPandMask(binaryIpAddress, binarySubnetMask); //Create substring containing only the network bits of the binary address ! subInIP = IPV4Address.IPandMask(IPV4Address.toBinaryString(inIPAddress), binarySubnetMask); ! if(subIP.equals(subInIP)){ //Compare the two substrings return true; } ! return false; } catch (Exception e) { } return false; } ! ! /* ! * int i = getNetworkIDBits(); ! subIP = binaryIpAddress.substring(0,i); //Create substring containing only the network bits of the binary address subInIP = IPV4Address.toBinaryString(inIPAddress).substring(0,getNetworkIDBits()); ! if(subIP.equals(subInIP)){ //Compare the two substrings return true; } ! */ ! }//EOF --- 605,667 ---- String subIP; //Substring of the current ip address String subInIP; //substring of the passed in ip address ! ! try { ! int i = getNetworkIDBits(); ! ! subIP = IPV4Address.IPandMask(binaryIpAddress, binarySubnetMask); //Create substring containing only the network bits of the binary address ! subInIP = IPV4Address.IPandMask(IPV4Address.toBinaryString(inIPAddress), binarySubnetMask); ! if(subIP.equals(subInIP)){ //Compare the two substrings return true; } ! return false; } catch (Exception e) { } return false; } ! ! /* ! * int i = getNetworkIDBits(); ! subIP = binaryIpAddress.substring(0,i); //Create substring containing only the network bits of the binary address subInIP = IPV4Address.toBinaryString(inIPAddress).substring(0,getNetworkIDBits()); ! if(subIP.equals(subInIP)){ //Compare the two substrings return true; } ! */ ! ! public static long IPString2Number(String sip){ ! long nip = 0; ! String[] asip = sip.split("\\."); ! if(asip.length==4){ ! for(int i=0; i<4; i++){ ! try{ ! int val = Integer.parseInt(asip[i]); ! if(val>=0 && val<=255){ ! nip = (nip<<8) + val; ! } ! else return -1; ! } ! catch(NumberFormatException e){ ! return -1; ! } ! } ! } ! else return -1; ! return nip; ! } ! ! public static boolean isBetween(String ip, String low, String high){ ! boolean result = false; ! long nip = IPString2Number(ip); ! long nlow = IPString2Number(low); ! long nhigh = IPString2Number(high); ! if(nip>-1 && nlow>-1 && nhigh>-1){ ! result = (nip>=nlow && nip<=nhigh); ! } ! return result; ! } ! }//EOF |