private IcmpV4CommonHeader(byte[] rawData, int offset, int length) throws IllegalRawDataException { if (length < ICMPV4_COMMON_HEADER_SIZE) { StringBuilder sb = new StringBuilder(80); sb.append("The data is too short to build an ICMPv4 common header(") .append(ICMPV4_COMMON_HEADER_SIZE) .append(" bytes). data: ") .append(ByteArrays.toHexString(rawData, " ")) .append(", offset: ") .append(offset) .append(", length: ") .append(length); throw new IllegalRawDataException(sb.toString()); } this.type = IcmpV4Type.getInstance(ByteArrays.getByte(rawData, TYPE_OFFSET + offset)); this.code = IcmpV4Code.getInstance(type.value(), ByteArrays.getByte(rawData, CODE_OFFSET + offset)); this.checksum = ByteArrays.getShort(rawData, CHECKSUM_OFFSET + offset); }
/** * @param type ICMPv4 type * @param value value * @return a IcmpV4Code object. */ public static IcmpV4Code getInstance(Byte type, Byte value) { if (registry.containsKey(type) && registry.get(type).containsKey(value)) { return registry.get(type).get(value); } else { return new IcmpV4Code(value, "unknown"); } }
@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!this.getClass().isInstance(obj)) { return false; } IcmpV4CommonHeader other = (IcmpV4CommonHeader) obj; return checksum == other.checksum && type.equals(other.type) && code.equals(other.code); }
@Override public String valueAsString() { return String.valueOf(value() & 0xFF); }
@Override protected int calcHashCode() { int result = 17; result = 31 * result + type.hashCode(); result = 31 * result + code.hashCode(); result = 31 * result + checksum; return result; } }
@Override protected int calcHashCode() { int result = 17; result = 31 * result + type.hashCode(); result = 31 * result + code.hashCode(); result = 31 * result + checksum; return result; } }
code = IcmpV4Code.getInstance(type.value(), Byte.parseByte(strCode)); } catch (NumberFormatException e) { throw new IllegalArgumentException("args[1]: " + strType, e);
/** * @param type type * @param code code * @return a IcmpV4Code object. */ public static IcmpV4Code register(IcmpV4Type type, IcmpV4Code code) { if (registry.containsKey(type.value())) { return registry.get(type.value()).put(code.value(), code); } else { Map<Byte, IcmpV4Code> map = new HashMap<Byte, IcmpV4Code>(); map.put(code.value(), code); registry.put(type.value(), map); return null; } }
/** * @param type ICMPv4 type * @param value value * @return a IcmpV4Code object. */ public static IcmpV4Code getInstance(Byte type, Byte value) { if (registry.containsKey(type) && registry.get(type).containsKey(value)) { return registry.get(type).get(value); } else { return new IcmpV4Code(value, "unknown"); } }
@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!this.getClass().isInstance(obj)) { return false; } IcmpV4CommonHeader other = (IcmpV4CommonHeader) obj; return checksum == other.checksum && type.equals(other.type) && code.equals(other.code); }
private IcmpV4CommonHeader(byte[] rawData, int offset, int length) throws IllegalRawDataException { if (length < ICMPV4_COMMON_HEADER_SIZE) { StringBuilder sb = new StringBuilder(80); sb.append("The data is too short to build an ICMPv4 common header(") .append(ICMPV4_COMMON_HEADER_SIZE) .append(" bytes). data: ") .append(ByteArrays.toHexString(rawData, " ")) .append(", offset: ") .append(offset) .append(", length: ") .append(length); throw new IllegalRawDataException(sb.toString()); } this.type = IcmpV4Type.getInstance(ByteArrays.getByte(rawData, TYPE_OFFSET + offset)); this.code = IcmpV4Code.getInstance(type.value(), ByteArrays.getByte(rawData, CODE_OFFSET + offset)); this.checksum = ByteArrays.getShort(rawData, CHECKSUM_OFFSET + offset); }
private List<byte[]> getRawFields(boolean zeroInsteadOfChecksum) { List<byte[]> rawFields = new ArrayList<byte[]>(); rawFields.add(ByteArrays.toByteArray(type.value())); rawFields.add(ByteArrays.toByteArray(code.value())); rawFields.add(ByteArrays.toByteArray(zeroInsteadOfChecksum ? (short) 0 : checksum)); return rawFields; }
code = IcmpV4Code.getInstance(type.value(), Byte.parseByte(strCode)); } catch (NumberFormatException e) { throw new IllegalArgumentException("args[1]: " + strType, e);
@Override public String valueAsString() { return String.valueOf(value() & 0xFF); }
/** * @param type type * @param code code * @return a IcmpV4Code object. */ public static IcmpV4Code register(IcmpV4Type type, IcmpV4Code code) { if (registry.containsKey(type.value())) { return registry.get(type.value()).put(code.value(), code); } else { Map<Byte, IcmpV4Code> map = new HashMap<Byte, IcmpV4Code>(); map.put(code.value(), code); registry.put(type.value(), map); return null; } }
private List<byte[]> getRawFields(boolean zeroInsteadOfChecksum) { List<byte[]> rawFields = new ArrayList<byte[]>(); rawFields.add(ByteArrays.toByteArray(type.value())); rawFields.add(ByteArrays.toByteArray(code.value())); rawFields.add(ByteArrays.toByteArray(zeroInsteadOfChecksum ? (short) 0 : checksum)); return rawFields; }