@Override public Ip4Prefix read(Kryo kryo, Input input, Class<Ip4Prefix> type) { int octLen = input.readInt(); checkArgument(octLen <= Ip4Address.BYTE_LENGTH); byte[] octs = new byte[octLen]; input.readBytes(octs); int prefLen = input.readInt(); return Ip4Prefix.valueOf(octs, prefLen); } }
@Override public void write(Kryo kryo, Output output, Ip4Prefix object) { byte[] octs = object.address().toOctets(); // It is always Ip4Address.BYTE_LENGTH output.writeInt(octs.length); output.writeBytes(octs); output.writeInt(object.prefixLength()); }
/** * Converts an IPv4 address and a prefix length into an IPv4 prefix. * * @param address the IPv4 address * @param prefixLength the prefix length * @return an IPv4 prefix * @throws IllegalArgumentException if the prefix length value is invalid */ public static Ip4Prefix valueOf(Ip4Address address, int prefixLength) { return new Ip4Prefix(address, prefixLength); }
@Override public int write(ChannelBuffer cb) { int iLenStartIndex = cb.writerIndex(); cb.writeByte(FLOW_SPEC_TYPE); cb.writeByte(length); cb.writeInt(ipPrefix.getIp4Prefix().address().toInt()); return cb.writerIndex() - iLenStartIndex; }
private void buildMatchRemoteIp(TrafficSelector.Builder sBuilder, IpPrefix remoteIpPrefix, OpenstackSecurityGroupRule.Direction direction) { if (remoteIpPrefix != null && !remoteIpPrefix.getIp4Prefix().equals(IP_PREFIX_ANY)) { if (direction.equals(OpenstackSecurityGroupRule.Direction.EGRESS)) { sBuilder.matchIPDst(remoteIpPrefix); } else { sBuilder.matchIPSrc(remoteIpPrefix); } } }
@Override public int write(ChannelBuffer cb) { int iLenStartIndex = cb.writerIndex(); cb.writeByte(FLOW_SPEC_TYPE); cb.writeByte(length); cb.writeInt(ipPrefix.getIp4Prefix().address().toInt()); return cb.writerIndex() - iLenStartIndex; }
/** * Gets the {@link Ip4Prefix} view of the IP prefix. * * @return the {@link Ip4Prefix} view of the IP prefix if it is IPv4, * otherwise null */ public Ip4Prefix getIp4Prefix() { if (!isIp4()) { return null; } // Return this object itself if it is already instance of Ip4Prefix if (this instanceof Ip4Prefix) { return (Ip4Prefix) this; } return Ip4Prefix.valueOf(address.getIp4Address(), prefixLength); }
ipCriterion = (IPCriterion) c; ip4Prefix = ipCriterion.ip().getIp4Prefix(); if (ip4Prefix.prefixLength() != Ip4Prefix.MAX_MASK_LENGTH) { Ip4Address maskAddr = Ip4Address.makeMaskPrefix(ip4Prefix.prefixLength()); Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip4Prefix.address().toInt()), IPv4Address.of(maskAddr.toInt())); mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp); } else { mBuilder.setExact(MatchField.IPV4_SRC, IPv4Address.of(ip4Prefix.address().toInt())); ipCriterion = (IPCriterion) c; ip4Prefix = ipCriterion.ip().getIp4Prefix(); if (ip4Prefix.prefixLength() != Ip4Prefix.MAX_MASK_LENGTH) { Ip4Address maskAddr = Ip4Address.makeMaskPrefix(ip4Prefix.prefixLength()); Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip4Prefix.address().toInt()), IPv4Address.of(maskAddr.toInt())); mBuilder.setMasked(MatchField.IPV4_DST, maskedIp); } else { mBuilder.setExact(MatchField.IPV4_DST, IPv4Address.of(ip4Prefix.address().toInt()));
/** * Converts an integer and a prefix length into an IPv4 prefix. * * @param address an integer representing the IPv4 address * @param prefixLength the prefix length * @return an IPv4 prefix * @throws IllegalArgumentException if the prefix length value is invalid */ public static Ip4Prefix valueOf(int address, int prefixLength) { return new Ip4Prefix(Ip4Address.valueOf(address), prefixLength); }
Ip4Prefix prefix = Ip4Prefix.valueOf(Ip4Address.valueOf(buffer), prefixBitlen); result.add(prefix);
/** * Converts a byte array and a prefix length into an IPv4 prefix. * * @param address the IPv4 address value stored in network byte order * @param prefixLength the prefix length * @return an IPv4 prefix * @throws IllegalArgumentException if the prefix length value is invalid */ public static Ip4Prefix valueOf(byte[] address, int prefixLength) { return new Ip4Prefix(Ip4Address.valueOf(address), prefixLength); }
if (match.isPartiallyMasked(MatchField.IPV4_SRC)) { Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC); ip4Prefix = Ip4Prefix.valueOf( maskedIp.getValue().getInt(), maskedIp.getMask().asCidrMaskLength()); } else { ip4Prefix = Ip4Prefix.valueOf( match.get(MatchField.IPV4_SRC).getInt(), Ip4Prefix.MAX_MASK_LENGTH); if (match.isPartiallyMasked(MatchField.IPV4_DST)) { Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST); ip4Prefix = Ip4Prefix.valueOf( maskedIp.getValue().getInt(), maskedIp.getMask().asCidrMaskLength()); } else { ip4Prefix = Ip4Prefix.valueOf( match.get(MatchField.IPV4_DST).getInt(), Ip4Prefix.MAX_MASK_LENGTH);
/** * Converts a CIDR (slash) notation string (e.g., "10.1.0.0/16") * into an IPv4 prefix. * * @param address an IP prefix in string form (e.g., "10.1.0.0/16") * @return an IPv4 prefix * @throws IllegalArgumentException if the arguments are invalid */ public static Ip4Prefix valueOf(String address) { final String[] parts = address.split("/"); if (parts.length != 2) { String msg = "Malformed IPv4 prefix string: " + address + ". " + "Address must take form \"x.x.x.x/y\""; throw new IllegalArgumentException(msg); } Ip4Address ipAddress = Ip4Address.valueOf(parts[0]); int prefixLength = Integer.parseInt(parts[1]); return new Ip4Prefix(ipAddress, prefixLength); } }