[Javanetsim-cvs] javaNetSim/core/protocolsuite/tcp_ip IPV4Address.java, 1.12, 1.13 IP_packet.java,
Status: Beta
Brought to you by:
darkkey
From: QweR <qw...@us...> - 2009-10-23 20:06:30
|
Update of /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip In directory fdv4jf1.ch3.sourceforge.com:/tmp/cvs-serv31290 Modified Files: IPV4Address.java IP_packet.java ProtocolStack.java Log Message: fixed problems with spaces in IP addresses Index: ProtocolStack.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/ProtocolStack.java,v retrieving revision 1.78 retrieving revision 1.79 diff -C2 -d -r1.78 -r1.79 *** ProtocolStack.java 4 Jan 2009 15:43:16 -0000 1.78 --- ProtocolStack.java 23 Oct 2009 20:06:21 -0000 1.79 *************** *** 637,641 **** } ! if (dest != "") { pingPacket = mICMPprotocol.sendPing(dest); sendPacket(pingPacket); --- 637,641 ---- } ! if (dest!=null && dest != "") { pingPacket = mICMPprotocol.sendPing(dest); sendPacket(pingPacket); Index: IP_packet.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/IP_packet.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** IP_packet.java 24 Oct 2008 16:18:48 -0000 1.8 --- IP_packet.java 23 Oct 2009 20:06:21 -0000 1.9 *************** *** 109,119 **** public IP_packet(String inDestIPAddress) - { ! ! mDestIPAddress = inDestIPAddress; NatInsideMark = false; NatOutsideMark = false; - } --- 109,116 ---- public IP_packet(String inDestIPAddress) { ! setDestIPAddress(inDestIPAddress); NatInsideMark = false; NatOutsideMark = false; } *************** *** 133,141 **** public void setDestIPAddress(String inDestIPAddress) - { ! ! mDestIPAddress = inDestIPAddress; ! } --- 130,135 ---- public void setDestIPAddress(String inDestIPAddress) { ! mDestIPAddress = IPV4Address.sanitizeDecIP(inDestIPAddress); } *************** *** 157,163 **** { ! ! mSourceIPAddress = inSourceIPAddress; ! } --- 151,155 ---- { ! mSourceIPAddress = IPV4Address.sanitizeDecIP(inSourceIPAddress); } Index: IPV4Address.java =================================================================== RCS file: /cvsroot/javanetsim/javaNetSim/core/protocolsuite/tcp_ip/IPV4Address.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** IPV4Address.java 4 Jan 2009 15:43:16 -0000 1.12 --- IPV4Address.java 23 Oct 2009 20:06:20 -0000 1.13 *************** *** 82,86 **** public static boolean isValidIp(final String ip) { ! return ip.matches("^[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}$"); } --- 82,86 ---- public static boolean isValidIp(final String ip) { ! return ip.matches("^[\\s]*[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}[\\s]*$"); } *************** *** 520,523 **** --- 520,524 ---- */ public static boolean validateDecIP(String inDecIPAddress) { + inDecIPAddress = inDecIPAddress.trim(); String[] ip = inDecIPAddress.split("\\."); // split string into an array if (ip.length != 4) { // ip address must have 4 octets *************** *** 568,571 **** --- 569,643 ---- /** + * This is a static method that will sanitize the IP Address valid IPV4 address or null + * + * @author QweR + * @param inIPAddress + * - The IP address to be tested eg: 192.168.0.2 + * @return String + * @version v0.20 + */ + public static String sanitizeDecIP(String inDecIPAddress) { + if(inDecIPAddress==null) return null; + inDecIPAddress = inDecIPAddress.trim(); + String ipaddr = ""; + String[] ip = inDecIPAddress.split("\\."); // split string into an array + if (ip.length != 4) { // ip address must have 4 octets + return null; + } + + if (inDecIPAddress.contains("255.255.255.255")) { + return inDecIPAddress; + } + + for (int i = 0; i < ip.length; i++) { + try { + Integer octet = new Integer(ip[i].trim()); + // if this is the first octect test to see if it is a valid + // a,b or address in the range of 1 to 223 + if (i == 0) { + if (octet.intValue() < 1 || octet.intValue() > 223) { + return null; + } + ipaddr = octet.toString(); + } else { + if (octet.intValue() < 0 || octet.intValue() > 255) { + return null; + } + ipaddr += "."+octet.toString(); + } + } catch (NumberFormatException e) { + return null; + } + 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 null; + } + } catch (Exception e) { + return null; + } // This should never happen! (fingers crossed) + } + return ipaddr; // if it has passed all of these tests return true + } + + /** + * This is a static method that will sanitize the IP Address valid IPV4 address or null + * + * @author QweR + * @param inIPAddress + * - The IP address to be tested eg: 192.168.0.2 + * @return String + * @version v0.20 + */ + public static String sanitizeDecIPorName(String inDecIPAddress) { + String ip = sanitizeDecIP(inDecIPAddress); + if(ip==null) return inDecIPAddress; + else return ip; + } + + /** * This static method will validate a subnet mask against the passed in IP * Addres. If found as a Class A, B, C and passes substring test return *************** *** 583,587 **** if (ipn.length == 4) { for (int i = 0; i < 4; i++) { ! int num = Integer.parseInt(ipn[i]); if (num < 0 || num > 255) throw new NumberFormatException(); --- 655,659 ---- if (ipn.length == 4) { for (int i = 0; i < 4; i++) { ! int num = Integer.parseInt(ipn[i].trim()); if (num < 0 || num > 255) throw new NumberFormatException(); *************** *** 685,689 **** for (int i = 0; i < 4; i++) { try { ! int val = Integer.parseInt(asip[i]); if (val >= 0 && val <= 255) { nip = (nip << 8) + val; --- 757,761 ---- for (int i = 0; i < 4; i++) { try { ! int val = Integer.parseInt(asip[i].trim()); if (val >= 0 && val <= 255) { nip = (nip << 8) + val; |