/** * 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); }
/** * 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); }
/** * {@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); }
/** * Create an IPv4 network address from the given IPv4 address and * prefix length. * * @param iaddr An {@link InetAddress} instance. * @param length The IPv4 prefix length. * Note that zero means "no mask". So zero is treated as if * the maximum prefix length is specified. * @return An {@link InetAddress} instance which represents the IPv4 * network address specified by the given pair of IPv4 address and * prefix length. * @throws NullPointerException * {@code iaddr} is {@code null}. * @throws IllegalArgumentException * The given IPv4 address or prefix length is invalid. */ public static InetAddress getNetworkAddress(InetAddress iaddr, int length) { int addr = NumberUtils.toInteger(iaddr.getAddress()); int mask = getNetMask(length); return getInetAddress(addr & mask); }
/** * Construct a new instance. * * @param bytes A byte array which represents an IPv4 address. * @param prefix Prefix length that specifies network range. * Note that zero means "no mask". So zero is treated as if * the maximum prefix length is specified. * @throws NullPointerException * {@code bytes} is {@code null}. * @throws IllegalArgumentException * The given prefix length is invalid. * @throws IllegalArgumentException * The given byte address does not represent an IPv4 address. */ public Ip4Network(byte[] bytes, int prefix) { super(prefix); int addr = NumberUtils.toInteger(bytes); int plen = getPrefixLength(); int mask = getNetMask(plen); netMask = mask; if (plen == Integer.SIZE) { address = addr; byteAddress = bytes.clone(); } else { address = addr & mask; } }
byte[] sender = arp.getSenderProtocolAddress(); int addr = NumberUtils.toInteger(sender); if (addr != 0) { src = new Ip4Network(addr);