Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5ca706f9 authored by Natasha Lee's avatar Natasha Lee Committed by Gerrit Code Review
Browse files

Merge "Remove the concept of byte order from StructNlAttr."

parents 3c141ccc 277d9486
Loading
Loading
Loading
Loading
+36 −17
Original line number Original line Diff line number Diff line
@@ -18,8 +18,8 @@ package android.net.netlink;


import java.net.InetAddress;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.UnknownHostException;
import java.nio.ByteOrder;
import java.nio.ByteBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;




/**
/**
@@ -48,9 +48,7 @@ public class StructNlAttr {
        }
        }
        final int baseOffset = byteBuffer.position();
        final int baseOffset = byteBuffer.position();


        // Assume the byte order of the buffer is the expected byte order of the value.
        final StructNlAttr struct = new StructNlAttr();
        final StructNlAttr struct = new StructNlAttr(byteBuffer.order());
        // The byte order of nla_len and nla_type is always native.
        final ByteOrder originalOrder = byteBuffer.order();
        final ByteOrder originalOrder = byteBuffer.order();
        byteBuffer.order(ByteOrder.nativeOrder());
        byteBuffer.order(ByteOrder.nativeOrder());
        try {
        try {
@@ -91,16 +89,8 @@ public class StructNlAttr {
    public short nla_type;
    public short nla_type;
    public byte[] nla_value;
    public byte[] nla_value;


    // The byte order used to read/write the value member. Netlink length and
    // type members are always read/written in native order.
    private ByteOrder mByteOrder = ByteOrder.nativeOrder();

    public StructNlAttr() {}
    public StructNlAttr() {}


    public StructNlAttr(ByteOrder byteOrder) {
        mByteOrder = byteOrder;
    }

    public StructNlAttr(short type, byte value) {
    public StructNlAttr(short type, byte value) {
        nla_type = type;
        nla_type = type;
        setValue(new byte[1]);
        setValue(new byte[1]);
@@ -112,10 +102,16 @@ public class StructNlAttr {
    }
    }


    public StructNlAttr(short type, short value, ByteOrder order) {
    public StructNlAttr(short type, short value, ByteOrder order) {
        this(order);
        nla_type = type;
        nla_type = type;
        setValue(new byte[Short.BYTES]);
        setValue(new byte[Short.BYTES]);
        getValueAsByteBuffer().putShort(value);
        final ByteBuffer buf = getValueAsByteBuffer();
        final ByteOrder originalOrder = buf.order();
        try {
            buf.order(order);
            buf.putShort(value);
        } finally {
            buf.order(originalOrder);
        }
    }
    }


    public StructNlAttr(short type, int value) {
    public StructNlAttr(short type, int value) {
@@ -123,10 +119,16 @@ public class StructNlAttr {
    }
    }


    public StructNlAttr(short type, int value, ByteOrder order) {
    public StructNlAttr(short type, int value, ByteOrder order) {
        this(order);
        nla_type = type;
        nla_type = type;
        setValue(new byte[Integer.BYTES]);
        setValue(new byte[Integer.BYTES]);
        getValueAsByteBuffer().putInt(value);
        final ByteBuffer buf = getValueAsByteBuffer();
        final ByteOrder originalOrder = buf.order();
        try {
            buf.order(order);
            buf.putInt(value);
        } finally {
            buf.order(originalOrder);
        }
    }
    }


    public StructNlAttr(short type, InetAddress ip) {
    public StructNlAttr(short type, InetAddress ip) {
@@ -152,10 +154,27 @@ public class StructNlAttr {
        return NetlinkConstants.alignedLengthOf(nla_len);
        return NetlinkConstants.alignedLengthOf(nla_len);
    }
    }


    public int getValueAsBe32(int defaultValue) {
        final ByteBuffer byteBuffer = getValueAsByteBuffer();
        if (byteBuffer == null || byteBuffer.remaining() != Integer.BYTES) {
            return defaultValue;
        }
        final ByteOrder originalOrder = byteBuffer.order();
        try {
            byteBuffer.order(ByteOrder.BIG_ENDIAN);
            return byteBuffer.getInt();
        } finally {
            byteBuffer.order(originalOrder);
        }
    }

    public ByteBuffer getValueAsByteBuffer() {
    public ByteBuffer getValueAsByteBuffer() {
        if (nla_value == null) { return null; }
        if (nla_value == null) { return null; }
        final ByteBuffer byteBuffer = ByteBuffer.wrap(nla_value);
        final ByteBuffer byteBuffer = ByteBuffer.wrap(nla_value);
        byteBuffer.order(mByteOrder);
        // By convention, all buffers in this library are in native byte order because netlink is in
        // native byte order. It's the order that is used by NetlinkSocket.recvMessage and the only
        // order accepted by NetlinkMessage.parse.
        byteBuffer.order(ByteOrder.nativeOrder());
        return byteBuffer;
        return byteBuffer;
    }
    }


+7 −0
Original line number Original line Diff line number Diff line
@@ -151,6 +151,13 @@ public class RtNetlinkNeighborMessageTest {
            assertEquals(0, hdr.nlmsg_seq);
            assertEquals(0, hdr.nlmsg_seq);
            assertEquals(11070, hdr.nlmsg_pid);
            assertEquals(11070, hdr.nlmsg_pid);


            final int probes = neighMsg.getProbes();
            assertTrue("Unexpected number of probes. Got " +  probes + ", max=5",
                    probes < 5);
            final int ndm_refcnt = neighMsg.getCacheInfo().ndm_refcnt;
            assertTrue("nda_cacheinfo has unexpectedly high ndm_refcnt: " + ndm_refcnt,
                    ndm_refcnt < 0x100);

            messageCount++;
            messageCount++;
        }
        }
        // TODO: add more detailed spot checks.
        // TODO: add more detailed spot checks.