/** * Tests whether the IP version of this address is IPv6. * * @return true if the IP version of this address is IPv6, otherwise false. */ public boolean isIp6() { return (version() == Ip6Address.VERSION); }
/** * Tests whether the IP version of this address is IPv4. * * @return true if the IP version of this address is IPv4, otherwise false. */ public boolean isIp4() { return (version() == Ip4Address.VERSION); }
/** * Returns the IP version of the prefix. * * @return the IP version of the prefix */ public IpAddress.Version version() { return address.version(); }
/** * Creates a byte array that represents an IP address masked with * a network mask of given mask length. * * @param addr the address to mask * @param prefixLength the length of the mask prefix. Must be in the * interval [0, 32] for IPv4, or [0, 128] for IPv6 * @return a byte array that represents the IP address masked with * a mask prefix of the specified length * @throws IllegalArgumentException if the prefix length is invalid */ static byte[] makeMaskedAddressArray(final IpAddress addr, int prefixLength) { byte[] mask = IpAddress.makeMaskPrefixArray(addr.version(), prefixLength); byte[] net = new byte[mask.length]; // Mask each byte for (int i = 0; i < net.length; i++) { net[i] = (byte) (addr.octets[i] & mask[i]); } return net; }
/** * Constructor for given IP address, and a prefix length. * * @param address the IP address * @param prefixLength the prefix length * @throws IllegalArgumentException if the prefix length value is invalid */ protected IpPrefix(IpAddress address, int prefixLength) { checkPrefixLength(address.version(), prefixLength); this.address = IpAddress.makeMaskedAddress(address, prefixLength); this.prefixLength = (short) prefixLength; }
@Override protected void write(DefaultMessage message, ByteBuffer buffer) { Endpoint sender = message.sender(); byte[] messageTypeBytes = message.type().getBytes(Charsets.UTF_8); IpAddress senderIp = sender.host(); byte[] ipOctets = senderIp.toOctets(); byte[] payload = message.payload(); int messageLength = 21 + ipOctets.length + messageTypeBytes.length + payload.length; buffer.putInt(messageLength); buffer.putLong(message.id()); if (senderIp.version() == Version.INET) { buffer.put((byte) 0x0); } else { buffer.put((byte) 0x1); } buffer.put(ipOctets); // write sender port buffer.putInt(sender.port()); // write length of message type buffer.putInt(messageTypeBytes.length); // write message type bytes buffer.put(messageTypeBytes); // write payload length buffer.putInt(payload.length); // write payload. buffer.put(payload); }
@Override protected void encode( ChannelHandlerContext context, InternalMessage message, ByteBuf out) throws Exception { out.writeInt(this.preamble); // write message id out.writeLong(message.id()); Endpoint sender = message.sender(); IpAddress senderIp = sender.host(); if (senderIp.version() == Version.INET) { out.writeByte(0); } else { out.writeByte(1); } out.writeBytes(senderIp.toOctets()); // write sender port out.writeInt(sender.port()); byte[] messageTypeBytes = message.type().getBytes(Charsets.UTF_8); // write length of message type out.writeInt(messageTypeBytes.length); // write message type bytes out.writeBytes(messageTypeBytes); byte[] payload = message.payload(); // write payload length out.writeInt(payload.length); // write payload. out.writeBytes(payload); }
/** * Determines whether a given IP address is contained within this prefix. * * @param other the IP address to test * @return true if the IP address is contained in this prefix, otherwise * false */ public boolean contains(IpAddress other) { if (version() != other.version()) { return false; } // // Mask the other address with my prefix length. // If the other prefix is within this prefix, the masked address must // be same as the address of this prefix. // IpAddress maskedAddr = IpAddress.makeMaskedAddress(other, this.prefixLength); return this.address.equals(maskedAddr); }
/** * Creates an IP address by masking it with a network mask of given * mask length. * * @param address the address to mask * @param prefixLength the length of the mask prefix. Must be in the * interval [0, 32] for IPv4, or [0, 128] for IPv6 * @return a new IP address that is masked with a mask prefix of the * specified length * @throws IllegalArgumentException if the prefix length is invalid */ public static IpAddress makeMaskedAddress(final IpAddress address, int prefixLength) { if (address instanceof Ip4Address) { Ip4Address ip4a = (Ip4Address) address; return Ip4Address.makeMaskedAddress(ip4a, prefixLength); } else if (address instanceof Ip6Address) { Ip6Address ip6a = (Ip6Address) address; return Ip6Address.makeMaskedAddress(ip6a, prefixLength); } else { byte[] net = makeMaskedAddressArray(address, prefixLength); return IpAddress.valueOf(address.version(), net); } }