/** * Return the value of the specified field as an IPv4 address. * * @param name The name of the field. * @return An {@link Ip4Network} instance. */ protected final Ip4Network getIp4Network(String name) { byte[] value = hdrFieldsMap.get(name); return (value == null) ? new Ip4Network(0) : new Ip4Network(value); }
/** * Stores the IP source address from the header. * * <p> * The network prefix in the given paramter is always ignored. * </p> * * @param addr The source IP address. * @return This instance. */ public IPv4 setSourceAddress(Ip4Network addr) { getHeaderFieldMap().put(SIP, addr.getBytes()); return this; }
/** * Determine whether the source or destination address is modified or not. * * @return {@code true} only if the source or destination address is * modified. */ public boolean isAddressModified() { if (modifiedValues == null) { return false; } Ip4Network oldIp = values.getSourceAddress(); Ip4Network newIp = modifiedValues.getSourceAddress(); if (oldIp.getAddress() != newIp.getAddress()) { return true; } oldIp = values.getDestinationAddress(); newIp = modifiedValues.getDestinationAddress(); return (oldIp.getAddress() != newIp.getAddress()); }
/** * Construct a new instance. * * @param addr An integer value 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 IllegalArgumentException * The given prefix length is invalid. */ public Ip4Network(int addr, int prefix) { super(prefix); int mask = getNetMask(getPrefixLength()); address = addr & mask; netMask = mask; }
/** * Return an {@link InetAddress} instance which represents the IPv4 netmask * specified by the given prefix length. * * @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. * @throws IllegalArgumentException * The given prefix length is invalid. */ public static InetAddress getInetMask(int length) { return getInetAddress(getNetMask(length)); }
src = new Ip4Network(addr); senderIp4Address = src; senderIp4Address = new Ip4Network(0); } else if (src.getAddress() == 0) {
/** * Send a packet for probing the specified host. * * @param ident The identifier for the target vBridge. * @param outq An {@link PacketOutQueue} instance associated with the * current MD-SAL datastore transaction. * @param egress A {@link SalPort} instance that specifies the egress * switch port. * @param vid A VLAN ID to be set in an ARP request. * @param tha The target hardware address. * @param tpa The target protocol address. */ private void probe(BridgeIdentifier<Vbridge> ident, PacketOutQueue outq, SalPort egress, int vid, EtherAddress tha, IpAddress tpa) { if (tpa != null) { Ip4Network ip4 = Ip4Network.create(tpa.getIpv4Address()); if (ip4 != null) { // Send an ARP request. if (LOG.isTraceEnabled()) { LOG.trace("{}: Sending an ARP request: mac={}, ip={}, " + "vid={}, port={}", ident, tha.getText(), ip4.getText(), vid, egress); } Ethernet ether = new ArpPacketBuilder(vid). build(senderAddress, tha, ip4); outq.enqueue(egress, ether); } } }
/** * {@inheritDoc} */ @Override public IpPrefix getIpPrefix() { IpPrefix ip = ipPrefix; if (ip == null) { StringBuilder builder = new StringBuilder(); builder.append(getHostAddress()).append(CIDR_SEPARATOR). append(getPrefixLength()); ip = new IpPrefix(new Ipv4Prefix(builder.toString())); ipPrefix = ip; } return ip; }
/** * Create an {@link IpNetwork} instance which represents the IP address * specified by the given {@link IpAddress} instance. * * @param ip An {@link IpAddress} instance which represents the IP * address. * @return An {@link IpNetwork} instance which represents the given * IP address. Note that {@code null} is returned if {@code ip} * is {@code null} or it does not contain valid value. * @throws IllegalArgumentException * The given {@link IpAddress} instance is invalid. * @since Beryllium */ public static final IpNetwork create(IpAddress ip) { if (ip != null) { Ip4Network ip4 = Ip4Network.create(ip.getIpv4Address()); if (ip4 != null) { return ip4; } if (ip.getIpv6Address() != null) { throw getUnsupportedAddressException(ip); } } return null; }
/** * {@inheritDoc} */ @Override public String getHostAddress() { String addr = networkAddress; if (addr == null) { addr = getInetAddress().getHostAddress(); networkAddress = addr; } return addr; }
/** * {@inheritDoc} */ @Override public String getCidrText() { return getIpPrefix().getIpv4Prefix().getValue(); }
/** * {@inheritDoc} */ @Override public void setDescription(StringBuilder builder) { Ip4Network src = getSourceAddress(); Ip4Network dst = getDestinationAddress(); int proto = (int)getProtocol(); int dscp = (int)getDscp(); builder.append("Inet4[src=").append(src.getText()). append(",dst=").append(dst.getText()). append(",proto=").append(proto). append(",dscp=").append(dscp).append(']'); } }
/** * Determine whether the given object is identical to this object. * * @param o An object to be compared. * @return {@code true} if identical. Otherwise {@code false}. */ @Override public boolean equals(Object o) { if (o == this) { return true; } if (o == null || !Ip4Network.class.equals(o.getClass())) { return false; } Ip4Network ip4 = (Ip4Network)o; return (address == ip4.address && getPrefixLength() == ip4.getPrefixLength()); }
/** * {@inheritDoc} */ @Override public IpAddress getIpAddress() { return new IpAddress(new Ipv4Address(getHostAddress())); }
/** * {@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); }
/** * 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; } }
/** * Create an {@link InetAddress} instance which represents the IPv4 address * specified by the given integer value. * * @param addr An integer value which represents the IPv4 address. * @return An {@link InetAddress} instance. */ public static InetAddress getInetAddress(int addr) { return getInetAddress(NumberUtils.toBytes(addr)); }
/** * Return a MD-SAL IP address which represents this network address. * * @return An {@link Ipv4} instance. */ @Override public Ipv4 getMdAddress() { IpPrefix ipp = getIpPrefix(); return new Ipv4Builder().setIpv4Address(ipp.getIpv4Prefix()).build(); }
/** * Try to probe IP address of the source address of this packet. */ public void probeInetAddress() { Inet4Packet ipv4 = getInet4Packet(); if (ipv4 != null) { // Send an ARP request to the source address of this packet. EtherAddress src = getControllerAddress(); Ip4Network tpa = ipv4.getSourceAddress(); EtherAddress dst = etherFrame.getSourceAddress(); int vlan = etherFrame.getOriginalVlan(); Ethernet ether = new ArpPacketBuilder(vlan).build(src, dst, tpa); SalPort sport = ingressPort; if (LOG.isTraceEnabled()) { String dstmac = dst.getText(); String target = tpa.getText(); LOG.trace("Sending an ARP request to detect IP address: " + "dst={}, tpa={}, vlan={}, port={}", dstmac, target, vlan, sport); } transmit(sport, ether); } }
/** * Create an Ethernet frame which contains an ARP request message to * probe the given IP address. * * @param src An {@link EtherAddress} instance to be used as the source * MAC address. * @param dst The destination MAC address. * @param addr The target IP address. * @return An Ethernet frame. {@code null} is returned if {@code addr} * is invalid. */ public Ethernet build(EtherAddress src, EtherAddress dst, InetAddress addr) { // IP address must be an IPv4 address. return (addr instanceof Inet4Address) ? build(src, dst, new Ip4Network(addr)) : null; }