/** * Set the source port number. * * @param port A short integer value which indicates the source port. * @return An integer value which indicates the source port. */ private int setSourcePort(short port) { sourcePort = NumberUtils.getUnsigned(port); return sourcePort; }
/** * Return the hash code of this object. * * @return The hash code. */ @Override public int hashCode() { return NumberUtils.hashCode(address); }
/** * Convert an integer value into an byte array. * * @param v An integer value. * @return A converted byte array. */ public static byte[] toBytes(int v) { byte[] b = new byte[NUM_OCTETS_INTEGER]; setInt(b, 0, v); return b; }
/** * Sets the ICMP identifier for the current ICMP object instance. * @param id The ICMP identifier to be set. * @return This instance. */ public ICMP setIdentifier(short id) { getHeaderFieldMap().put(IDENTIFIER, toBytes(id)); return this; }
/** * Return the value of the specified field as an integer. * * @param name The name of the field. * @return An integer value. */ protected final int getInt(String name) { byte[] value = hdrFieldsMap.get(name); return (value == null) ? 0 : toInteger(value); }
/** * Create a pseudo IPv4 header used for computing TCP/UDP checksum. * * @param proto An IP protocol number. * @param len The number of octets in a payload. * @return A byte array which represents the pseudo IPv4 header. */ public byte[] getHeaderForChecksum(byte proto, short len) { byte[] header = new byte[CKSUM_HEADER_SIZE]; int src = getSourceAddress().getAddress(); int dst = getDestinationAddress().getAddress(); NumberUtils.setInt(header, CKSUM_OFF_SRC, src); NumberUtils.setInt(header, CKSUM_OFF_DST, dst); header[CKSUM_OFF_PROTO] = proto; NumberUtils.setShort(header, CKSUM_OFF_LEN, len); return header; }
/** * Determine whether the given value represents a zero short value or not. * * @param value A byte array that contains a short integer. * @return {@code true} only if the given value represents a zero. */ protected final boolean isZeroShort(byte[] value) { return (value == null || toShort(value) == 0); }
/** * Convert a short integer value into an byte array. * * @param v A short integer value. * @return A converted byte array. * @since Beryllium */ public static byte[] toBytes(short v) { byte[] b = new byte[NUM_OCTETS_SHORT]; setShort(b, 0, v); return b; }
/** * Convert a 2 bytes array into a short integer number. * * @param b A 2 bytes array. * @return A short integer number. * @throws NullPointerException * {@code b} is {@code null}. * @throws IllegalArgumentException * The length of {@code b} is not 2. * @since Beryllium */ public static short toShort(byte[] b) { checkLength(b, NUM_OCTETS_SHORT); int value = (b[0] & MASK_BYTE) << Byte.SIZE; return (short)(value | (b[1] & MASK_BYTE)); }
/** * Return a {@link VtnEtherMatchBuilder} instance which contains the flow * conditions configured in this instance. * * @return A {@link VtnEtherMatchBuilder} instance. */ public VtnEtherMatchBuilder toVtnEtherMatchBuilder() { VtnEtherMatchBuilder builder = new VtnEtherMatchBuilder(); if (sourceAddress != null) { builder.setSourceAddress(sourceAddress.getMacAddress()); } if (destinationAddress != null) { builder.setDestinationAddress(destinationAddress.getMacAddress()); } if (etherType != null) { Long type = NumberUtils.toLong(etherType); builder.setEtherType(new EtherType(type)); } if (vlanId != null) { builder.setVlanId(new VlanId(vlanId)); } if (vlanPriority != null) { builder.setVlanPcp(new VlanPcp(vlanPriority)); } return builder; }
/** * Stores the total length of IPv4 packet in bytes. * * @param len The total length of the IPv4 packet. * @return This instance. */ public IPv4 setTotalLength(short len) { getHeaderFieldMap().put(TOTLENGTH, toBytes(len)); return this; }
/** * Return an IPv4 prefix length represented by the given byte array. * * <p> * Note that this method returns 32 if all the bits in the given array * are not set. * </p> * * @param bytes A byte array which represents IPv4 network mask. * @return The IPv4 prefix length represented by the given byte array. * @throws NullPointerException * {@code bytes} is {@code null}. * @throws IllegalArgumentException * The given byte array does not represent an IPv4 network mask. */ public static int getPrefixLength(byte[] bytes) { // Verify the given network mask. int mask = NumberUtils.toInteger(bytes); if (mask == 0) { return Integer.SIZE; } int inv = ~mask; int p2 = inv + 1; if ((p2 & inv) != 0) { throw new IllegalArgumentException( "Invalid IPv4 netmask: " + Integer.toHexString(mask)); } return Integer.numberOfLeadingZeros(inv); }
/** * Return the value of the specified field as a short integer. * * @param name The name of the field. * @return A short integer value. */ protected final short getShort(String name) { byte[] value = hdrFieldsMap.get(name); return (value == null) ? 0 : toShort(value); }
/** * Compute the checksum of the given packet. * * @param ipv4 An {@link Inet4Packet} instance that contains the given * packet. * @param packet A {@link PortPacket} instance. * @param sumOff Offset in bytes to the checksum field. * @return A computed checksum. * @throws VTNException * An error occurred. */ protected static final short computeChecksum( Inet4Packet ipv4, PortPacket<?> packet, int sumOff) throws VTNException { // Serialize the given packet. byte[] data; try { data = packet.serialize(); } catch (Exception e) { // This should never happen. throw new VTNException("Failed to serialize the packet.", e); } // Clear checksum field. NumberUtils.setShort(data, sumOff, (short)0); // Compute checksum. return (short)~computeChecksum(ipv4, data); }
/** * Convert a 4 bytes array into an integer number. * * @param b A 4 bytes array. * @return An integer number. * @throws NullPointerException * {@code b} is {@code null}. * @throws IllegalArgumentException * The length of {@code b} is not 4. */ public static int toInteger(byte[] b) { checkLength(b, NUM_OCTETS_INTEGER); int index = 0; int value = (b[index++] & MASK_BYTE) << INT_SHIFT_OCTET1; value |= (b[index++] & MASK_BYTE) << INT_SHIFT_OCTET2; value |= (b[index++] & MASK_BYTE) << INT_SHIFT_OCTET3; return value | (b[index] & MASK_BYTE); }
setType(new EtherType(NumberUtils.toLong(etherType))).build(); ematch = create(ematch).setEthernetType(etype);
/** * Set the ICMP type. * * @param value A byte integer value which indicates the ICMP type. * @return A short value which represents the given ICMP type. */ private short setType(byte value) { type = (short)NumberUtils.getUnsigned(value); return type; }
/** * Stores the IP fragmentation offset value. * * @param off The fragmentation offset. * @return This instance. */ public IPv4 setFragmentOffset(short off) { getHeaderFieldMap().put(FRAGOFFSET, toBytes(off)); return this; }
/** * {@inheritDoc} */ @Override InetAddress init(InetAddress iaddr, int prefix) { int addr = NumberUtils.toInteger(iaddr.getAddress()); int mask = getNetMask(prefix); int maskedAddr = addr & mask; address = maskedAddr; netMask = mask; return getInetAddress(maskedAddr); }
/** * Return a hash code of the given double value. * * @param value A double value. * @return A hash code of the given value. */ public static int hashCode(double value) { return hashCode(Double.doubleToLongBits(value)); }