IPv4 extends BasePacket { public static int toIPv4Address(String ipAddress) { if (ipAddress == null) throw new IllegalArgumentException("Specified IPv4 address must" + "contain 4 sets of numerical digits separated by periods"); String[] octets = ipAddress.split("\\."); if (octets.length != 4) throw new IllegalArgumentException("Specified IPv4 address must" + "contain 4 sets of numerical digits separated by periods"); int result = 0; for (int i = 0; i < 4; ++i) { int oct = Integer.valueOf(octets[i]); if (oct > 255 || oct < 0) throw new IllegalArgumentException("Octet values in specified" + " IPv4 address must be 0 <= value <= 255"); result |= oct << ((3-i)*8); } return result; } IPv4(); byte getVersion(); IPv4 setVersion(byte version); byte getHeaderLength(); byte getDiffServ(); IPv4 setDiffServ(byte diffServ); short getTotalLength(); short getIdentification(); boolean isTruncated(); void setTruncated(boolean isTruncated); boolean isFragment(); void setFragment(boolean isFrag); IPv4 setIdentification(short identification); byte getFlags(); IPv4 setFlags(byte flags); short getFragmentOffset(); IPv4 setFragmentOffset(short fragmentOffset); byte getTtl(); IPv4 setTtl(byte ttl); IpProtocol getProtocol(); IPv4 setProtocol(IpProtocol protocol); short getChecksum(); IPv4 setChecksum(short checksum); @Override void resetChecksum(); IPv4Address getSourceAddress(); IPv4 setSourceAddress(IPv4Address sourceAddress); IPv4 setSourceAddress(int sourceAddress); IPv4 setSourceAddress(String sourceAddress); IPv4Address getDestinationAddress(); IPv4 setDestinationAddress(IPv4Address destinationAddress); IPv4 setDestinationAddress(int destinationAddress); IPv4 setDestinationAddress(String destinationAddress); byte[] getOptions(); IPv4 setOptions(byte[] options); @Override byte[] serialize(); @Override IPacket deserialize(byte[] data, int offset, int length); static int toIPv4Address(String ipAddress); static int toIPv4Address(byte[] ipAddress); static String fromIPv4Address(int ipAddress); static String fromIPv4AddressCollection(Collection<Integer> ipAddresses); static byte[] toIPv4AddressBytes(String ipAddress); static byte[] toIPv4AddressBytes(int ipAddress); @Override int hashCode(); @Override boolean equals(Object obj); static Map<IpProtocol, Class<? extends IPacket>> protocolClassMap; static final byte IPV4_FLAGS_MOREFRAG; static final byte IPV4_FLAGS_DONTFRAG; static final byte IPV4_FLAGS_MASK; static final byte IPV4_FLAGS_SHIFT; static final short IPV4_OFFSET_MASK; }
@Test public void testToIPv4Address() { int intIp = 0xc0a80001; String stringIp = "192.168.0.1"; byte[] byteIp = new byte[] {(byte)192, (byte)168, (byte)0, (byte)1}; assertEquals(intIp, IPv4.toIPv4Address(stringIp)); assertEquals(intIp, IPv4.toIPv4Address(byteIp)); assertTrue(Arrays.equals(byteIp, IPv4.toIPv4AddressBytes(intIp))); assertTrue(Arrays.equals(byteIp, IPv4.toIPv4AddressBytes(stringIp))); }