Java Code Examples for org.onlab.packet.Ip6Address#BYTE_LENGTH

The following examples show how to use org.onlab.packet.Ip6Address#BYTE_LENGTH . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RouteAttributeGateway.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a decoder for a gateway route attribute.
 *
 * @return gateway route attribute decoder
 */
public static RouteAttributeDecoder<RouteAttributeGateway> decoder() {
    return (int length, int type, byte[] value) -> {

        IpAddress gateway;
        if (value.length == Ip4Address.BYTE_LENGTH) {
            gateway = IpAddress.valueOf(IpAddress.Version.INET, value);
        } else if (value.length == Ip6Address.BYTE_LENGTH) {
            gateway = IpAddress.valueOf(IpAddress.Version.INET6, value);
        } else {
            throw new DeserializationException("Invalid address length");
        }

        return new RouteAttributeGateway(length, type, gateway);
    };
}
 
Example 2
Source File: RouteAttributeGateway.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Encode the RouteAttributeGateway contents into the ChannelBuffer.
 *
 * @param cb channelbuffer to be filled in
 */
@Override
public void encode(ChannelBuffer cb) {

    super.encode(cb);

    ChannelBuffer buffer =  ChannelBuffers.copiedBuffer(gateway.toOctets());
    if (length() == Ip6Address.BYTE_LENGTH +
            RouteAttribute.ROUTE_ATTRIBUTE_HEADER_LENGTH) {
        cb.writeBytes(buffer, Ip6Address.BYTE_LENGTH);
    } else if (length() == Ip4Address.BYTE_LENGTH +
            RouteAttribute.ROUTE_ATTRIBUTE_HEADER_LENGTH) {
        cb.writeBytes(buffer, Ip4Address.BYTE_LENGTH);
    } else {
        throw new IllegalArgumentException("Gateway address length incorrect!");
    }
}
 
Example 3
Source File: RouteAttributeDst.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a decoder for a destination address route attribute.
 *
 * @return destination address route attribute decoder
 */
public static RouteAttributeDecoder<RouteAttributeDst> decoder() {
    return (int length, int type, byte[] value) -> {

        IpAddress dstAddress;
        if (value.length == Ip4Address.BYTE_LENGTH) {
            dstAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
        } else if (value.length == Ip6Address.BYTE_LENGTH) {
            dstAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
        } else {
            throw new DeserializationException("Invalid address length");
        }

        return new RouteAttributeDst(length, type, dstAddress);
    };
}
 
Example 4
Source File: RouteAttributeDst.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Encode the RouteAttributeDst contents into the ChannelBuffer.
 *
 * @param cb channelbuffer to be filled in
 */
@Override
public void encode(ChannelBuffer cb) {

    super.encode(cb);

    ChannelBuffer buffer =  ChannelBuffers.copiedBuffer(dstAddress.toOctets());
    if (length() == Ip6Address.BYTE_LENGTH +
            RouteAttribute.ROUTE_ATTRIBUTE_HEADER_LENGTH) {
        cb.writeBytes(buffer, Ip6Address.BYTE_LENGTH);
    } else if (length() == Ip4Address.BYTE_LENGTH +
            RouteAttribute.ROUTE_ATTRIBUTE_HEADER_LENGTH) {
        cb.writeBytes(buffer, Ip4Address.BYTE_LENGTH);
    } else {
        throw new IllegalArgumentException("Dst address length incorrect!");
    }
}
 
Example 5
Source File: IcmpHandler.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a NDP request for the target IP address to all ports except in-port.
 *
 * @param deviceId Switch device ID
 * @param targetAddress target IP address for ARP
 * @param inPort in-port
 */
public void sendNdpRequest(DeviceId deviceId, IpAddress targetAddress, ConnectPoint inPort) {
    byte[] senderMacAddress = new byte[MacAddress.MAC_ADDRESS_LENGTH];
    byte[] senderIpAddress = new byte[Ip6Address.BYTE_LENGTH];
    // Retrieves device info.
    if (!getSenderInfo(senderMacAddress, senderIpAddress, deviceId, targetAddress)) {
        log.warn("Aborting sendNdpRequest, we cannot get all the information needed");
        return;
    }
    // We have to compute the dst mac address and dst ip address.
    byte[] dstIp = IPv6.getSolicitNodeAddress(targetAddress.toOctets());
    byte[] dstMac = IPv6.getMCastMacAddress(dstIp);
    // Creates the request.
    Ethernet ndpRequest = NeighborSolicitation.buildNdpSolicit(
            targetAddress.getIp6Address(),
            Ip6Address.valueOf(senderIpAddress),
            Ip6Address.valueOf(dstIp),
            MacAddress.valueOf(senderMacAddress),
            MacAddress.valueOf(dstMac),
            VlanId.NONE
    );
    flood(ndpRequest, inPort, targetAddress);
}
 
Example 6
Source File: Dhcp6IaPrefixOption.java    From onos with Apache License 2.0 5 votes vote down vote up
public static Deserializer<Dhcp6Option> deserializer() {
    return (data, offset, length) -> {
        Dhcp6IaPrefixOption iaPrefixOption = new Dhcp6IaPrefixOption();
        Dhcp6Option dhcp6Option =
                Dhcp6Option.deserializer().deserialize(data, offset, length);
        iaPrefixOption.setPayload(dhcp6Option.getPayload());
        if (dhcp6Option.getLength() < DEFAULT_LEN) {
            throw new DeserializationException("Invalid length of IA prefix option");
        }
        ByteBuffer bb = ByteBuffer.wrap(dhcp6Option.getData());
        iaPrefixOption.preferredLifetime = bb.getInt();
        iaPrefixOption.validLifetime = bb.getInt();
        iaPrefixOption.prefixLength = bb.get();
        byte[] ipv6Pref = new byte[Ip6Address.BYTE_LENGTH];
        bb.get(ipv6Pref);
        iaPrefixOption.ip6Prefix = Ip6Address.valueOf(ipv6Pref);

        // options length of IA Address option
        int optionsLen = dhcp6Option.getLength() - DEFAULT_LEN;
        if (optionsLen > 0) {
            byte[] optionsData = new byte[optionsLen];
            bb.get(optionsData);
            iaPrefixOption.options =
                    Data.deserializer().deserialize(optionsData, 0, optionsLen);
        }
        return iaPrefixOption;
    };
}
 
Example 7
Source File: BgpUpdate.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a message that contains encoded IPv6 network prefixes.
 * <p>
 * The IPv6 prefixes are encoded in the form:
 * <Length, Prefix> where Length is the length in bits of the IPv6 prefix,
 * and Prefix is the IPv6 prefix (padded with trailing bits to the end
 * of an octet).
 *
 * @param totalLength the total length of the data to parse
 * @param message the message with data to parse
 * @return a collection of parsed IPv6 network prefixes
 * @throws BgpMessage.BgpParseException
 */
private static Collection<Ip6Prefix> parsePackedIp6Prefixes(
                                            int totalLength,
                                            ChannelBuffer message)
    throws BgpMessage.BgpParseException {
    Collection<Ip6Prefix> result = new ArrayList<>();

    if (totalLength == 0) {
        return result;
    }

    // Parse the data
    byte[] buffer = new byte[Ip6Address.BYTE_LENGTH];
    int dataEnd = message.readerIndex() + totalLength;
    while (message.readerIndex() < dataEnd) {
        int prefixBitlen = message.readUnsignedByte();
        int prefixBytelen = (prefixBitlen + 7) / 8;     // Round-up
        if (message.readerIndex() + prefixBytelen > dataEnd) {
            String errorMsg = "Malformed Network Prefixes";
            throw new BgpMessage.BgpParseException(errorMsg);
        }

        message.readBytes(buffer, 0, prefixBytelen);
        Ip6Prefix prefix = Ip6Prefix.valueOf(Ip6Address.valueOf(buffer),
                                             prefixBitlen);
        result.add(prefix);
    }

    return result;
}