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

Commit cf1cf81c authored by Etan Cohen's avatar Etan Cohen Committed by Android (Google) Code Review
Browse files

Merge "[AWARE] Add Wi-Fi Aware-specific Network info: port, transport protocol"

parents b2d70dd5 7faa4fac
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -29941,12 +29941,16 @@ package android.net.wifi.aware {
    method public android.net.NetworkSpecifier build();
    method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setDiscoverySession(android.net.wifi.aware.DiscoverySession);
    method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setPeerHandle(android.net.wifi.aware.PeerHandle);
    method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setPort(int);
    method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setPskPassphrase(java.lang.String);
    method public android.net.wifi.aware.WifiAwareManager.NetworkSpecifierBuilder setTransportProtocol(int);
  }
  public final class WifiAwareNetworkInfo implements android.os.Parcelable android.net.TransportInfo {
    method public int describeContents();
    method public java.net.Inet6Address getPeerIpv6Addr();
    method public int getPort();
    method public int getTransportProtocol();
    method public void writeToParcel(android.os.Parcel, int);
    field public static final android.os.Parcelable.Creator<android.net.wifi.aware.WifiAwareNetworkInfo> CREATOR;
  }
+69 −1
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import libcore.util.HexEncoding;
@@ -434,6 +435,8 @@ public class WifiAwareManager {
                null, // peerMac (not used in this method)
                pmk,
                passphrase,
                0, // no port info for deprecated IB APIs
                -1, // no transport info for deprecated IB APIs
                Process.myUid());
    }

@@ -473,6 +476,8 @@ public class WifiAwareManager {
                peer,
                pmk,
                passphrase,
                0, // no port info for OOB APIs
                -1, // no transport protocol info for OOB APIs
                Process.myUid());
    }

@@ -824,6 +829,8 @@ public class WifiAwareManager {
        private PeerHandle mPeerHandle;
        private String mPskPassphrase;
        private byte[] mPmk;
        private int mPort = 0; // invalid value
        private int mTransportProtocol = -1; // invalid value

        /**
         * Configure the {@link PublishDiscoverySession} or {@link SubscribeDiscoverySession}
@@ -901,6 +908,55 @@ public class WifiAwareManager {
            return this;
        }

        /**
         * Configure the port number which will be used to create a connection over this link. This
         * configuration should only be done on the server device, e.g. the device creating the
         * {@link java.net.ServerSocket}.
         * <p>Notes:
         * <ul>
         *     <li>The server device must be the Publisher device!
         *     <li>The port information can only be specified on secure links, specified using
         *     {@link #setPskPassphrase(String)}.
         * </ul>
         *
         * @param port A positive integer indicating the port to be used for communication.
         * @return the current {@link NetworkSpecifierBuilder} builder, enabling chaining of builder
         *         methods.
         */
        public @NonNull NetworkSpecifierBuilder setPort(int port) {
            if (port <= 0 || port > 65535) {
                throw new IllegalArgumentException("The port must be a positive value (0, 65535]");
            }
            mPort = port;
            return this;
        }

        /**
         * Configure the transport protocol which will be used to create a connection over this
         * link. This configuration should only be done on the server device, e.g. the device
         * creating the {@link java.net.ServerSocket} for TCP.
         * <p>Notes:
         * <ul>
         *     <li>The server device must be the Publisher device!
         *     <li>The transport protocol information can only be specified on secure links,
         *     specified using {@link #setPskPassphrase(String)}.
         * </ul>
         * The transport protocol number is assigned by the Internet Assigned Numbers Authority
         * (IANA) https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml.
         *
         * @param transportProtocol The transport protocol to be used for communication.
         * @return the current {@link NetworkSpecifierBuilder} builder, enabling chaining of builder
         *         methods.
         */
        public @NonNull NetworkSpecifierBuilder setTransportProtocol(int transportProtocol) {
            if (transportProtocol < 0 || transportProtocol > 255) {
                throw new IllegalArgumentException(
                        "The transport protocol must be in range [0, 255]");
            }
            mTransportProtocol = transportProtocol;
            return this;
        }

        /**
         * Create a {@link android.net.NetworkRequest.Builder#setNetworkSpecifier(NetworkSpecifier)}
         * for a WiFi Aware connection (link) to the specified peer. The
@@ -929,6 +985,18 @@ public class WifiAwareManager {
                    ? WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_INITIATOR
                    : WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_RESPONDER;

            if (mPort != 0 || mTransportProtocol != -1) {
                if (role != WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_RESPONDER) {
                    throw new IllegalStateException(
                            "Port and transport protocol information can only "
                                    + "be specified on the Publisher device (which is the server");
                }
                if (TextUtils.isEmpty(mPskPassphrase) && mPmk == null) {
                    throw new IllegalStateException("Port and transport protocol information can "
                            + "only be specified on a secure link");
                }
            }

            if (role == WIFI_AWARE_DATA_PATH_ROLE_INITIATOR && mPeerHandle == null) {
                throw new IllegalStateException("Null peerHandle!?");
            }
@@ -936,7 +1004,7 @@ public class WifiAwareManager {
            return new WifiAwareNetworkSpecifier(
                    WifiAwareNetworkSpecifier.NETWORK_SPECIFIER_TYPE_IB, role,
                    mDiscoverySession.mClientId, mDiscoverySession.mSessionId, mPeerHandle.peerId,
                    null, mPmk, mPskPassphrase, Process.myUid());
                    null, mPmk, mPskPassphrase, mPort, mTransportProtocol, Process.myUid());
        }
    }
}
+53 −5
Original line number Diff line number Diff line
@@ -38,17 +38,30 @@ import java.util.Objects;
 * android.net.NetworkCapabilities)} callback.
 * <p>
 * The Wi-Fi Aware-specific network information include the peer's scoped link-local IPv6 address
 * for the Wi-Fi Aware link. The scoped link-local IPv6 can then be used to create a
 * for the Wi-Fi Aware link, as well as (optionally) the port and transport protocol specified by
 * the peer.
 * The scoped link-local IPv6, port, and transport protocol can then be used to create a
 * {@link java.net.Socket} connection to the peer.
 * <p>
 * Note: these are the peer's IPv6 and port information - not the local device's!
 */
public final class WifiAwareNetworkInfo implements TransportInfo, Parcelable {
    private Inet6Address mIpv6Addr;
    private int mPort = 0; // a value of 0 is considered invalid
    private int mTransportProtocol = -1; // a value of -1 is considered invalid

    /** @hide */
    public WifiAwareNetworkInfo(Inet6Address ipv6Addr) {
        mIpv6Addr = ipv6Addr;
    }

    /** @hide */
    public WifiAwareNetworkInfo(Inet6Address ipv6Addr, int port, int transportProtocol) {
        mIpv6Addr = ipv6Addr;
        mPort = port;
        mTransportProtocol = transportProtocol;
    }

    /**
     * Get the scoped link-local IPv6 address of the Wi-Fi Aware peer (not of the local device!).
     *
@@ -59,6 +72,34 @@ public final class WifiAwareNetworkInfo implements TransportInfo, Parcelable {
        return mIpv6Addr;
    }

    /**
     * Get the port number to be used to create a network connection to the Wi-Fi Aware peer.
     * The port information is provided by the app running on the peer which requested the
     * connection, using the {@link WifiAwareManager.NetworkSpecifierBuilder#setPort(int)}.
     *
     * @return A port number on the peer. A value of 0 indicates that no port was specified by the
     *         peer.
     */
    public int getPort() {
        return mPort;
    }

    /**
     * Get the transport protocol to be used to communicate over a network connection to the Wi-Fi
     * Aware peer. The transport protocol is provided by the app running on the peer which requested
     * the connection, using the
     * {@link WifiAwareManager.NetworkSpecifierBuilder#setTransportProtocol(int)}.
     * <p>
     * The transport protocol number is assigned by the Internet Assigned Numbers Authority
     * (IANA) https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml.
     *
     * @return A transport protocol id. A value of -1 indicates that no transport protocol was
     *         specified by the peer.
     */
    public int getTransportProtocol() {
        return mTransportProtocol;
    }

    // parcelable methods

    @Override
@@ -71,6 +112,8 @@ public final class WifiAwareNetworkInfo implements TransportInfo, Parcelable {
        dest.writeByteArray(mIpv6Addr.getAddress());
        NetworkInterface ni = mIpv6Addr.getScopedInterface();
        dest.writeString(ni == null ? null : ni.getName());
        dest.writeInt(mPort);
        dest.writeInt(mTransportProtocol);
    }

    public static final Creator<WifiAwareNetworkInfo> CREATOR =
@@ -94,8 +137,10 @@ public final class WifiAwareNetworkInfo implements TransportInfo, Parcelable {
                        e.printStackTrace();
                        return null;
                    }
                    int port = in.readInt();
                    int transportProtocol = in.readInt();

                    return new WifiAwareNetworkInfo(ipv6Addr);
                    return new WifiAwareNetworkInfo(ipv6Addr, port, transportProtocol);
                }

                @Override
@@ -109,7 +154,9 @@ public final class WifiAwareNetworkInfo implements TransportInfo, Parcelable {

    @Override
    public String toString() {
        return new StringBuilder("AwareNetworkInfo: IPv6=").append(mIpv6Addr).toString();
        return new StringBuilder("AwareNetworkInfo: IPv6=").append(mIpv6Addr).append(
                ", port=").append(mPort).append(", transportProtocol=").append(
                mTransportProtocol).toString();
    }

    /** @hide */
@@ -124,12 +171,13 @@ public final class WifiAwareNetworkInfo implements TransportInfo, Parcelable {
        }

        WifiAwareNetworkInfo lhs = (WifiAwareNetworkInfo) obj;
        return Objects.equals(mIpv6Addr, lhs.mIpv6Addr);
        return Objects.equals(mIpv6Addr, lhs.mIpv6Addr) && mPort == lhs.mPort
                && mTransportProtocol == lhs.mTransportProtocol;
    }

    /** @hide */
    @Override
    public int hashCode() {
        return Objects.hash(mIpv6Addr);
        return Objects.hash(mIpv6Addr, mPort, mTransportProtocol);
    }
}
+40 −16
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package android.net.wifi.aware;
import android.net.NetworkSpecifier;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import java.util.Arrays;
import java.util.Objects;
@@ -116,6 +115,32 @@ public final class WifiAwareNetworkSpecifier extends NetworkSpecifier implements
     */
    public final String passphrase;

    /**
     * The port information to be used for this link. This information will be communicated to the
     * peer as part of the layer 2 link setup.
     *
     * Information only allowed on secure links since a single layer-2 link is set up for all
     * requestors. Therefore if multiple apps on a single device request links to the same peer
     * device they all get the same link. However, the link is only set up on the first request -
     * hence only the first can transmit the port information. But we don't want to expose that
     * information to other apps. Limiting to secure links would (usually) imply single app usage.
     *
     * @hide
     */
    public final int port;

    /**
     * The transport protocol information to be used for this link. This information will be
     * communicated to the peer as part of the layer 2 link setup.
     *
     * Information only allowed on secure links since a single layer-2 link is set up for all
     * requestors. Therefore if multiple apps on a single device request links to the same peer
     * device they all get the same link. However, the link is only set up on the first request -
     * hence only the first can transmit the port information. But we don't want to expose that
     * information to other apps. Limiting to secure links would (usually) imply single app usage.
     */
    public final int transportProtocol;

    /**
     * The UID of the process initializing this network specifier. Validated by receiver using
     * checkUidIfNecessary() and is used by satisfiedBy() to determine whether matches the
@@ -127,7 +152,8 @@ public final class WifiAwareNetworkSpecifier extends NetworkSpecifier implements

    /** @hide */
    public WifiAwareNetworkSpecifier(int type, int role, int clientId, int sessionId, int peerId,
            byte[] peerMac, byte[] pmk, String passphrase, int requestorUid) {
            byte[] peerMac, byte[] pmk, String passphrase, int port, int transportProtocol,
            int requestorUid) {
        this.type = type;
        this.role = role;
        this.clientId = clientId;
@@ -136,6 +162,8 @@ public final class WifiAwareNetworkSpecifier extends NetworkSpecifier implements
        this.peerMac = peerMac;
        this.pmk = pmk;
        this.passphrase = passphrase;
        this.port = port;
        this.transportProtocol = transportProtocol;
        this.requestorUid = requestorUid;
    }

@@ -152,6 +180,8 @@ public final class WifiAwareNetworkSpecifier extends NetworkSpecifier implements
                        in.createByteArray(), // peerMac
                        in.createByteArray(), // pmk
                        in.readString(), // passphrase
                        in.readInt(), // port
                        in.readInt(), // transportProtocol
                        in.readInt()); // requestorUid
                }

@@ -186,6 +216,8 @@ public final class WifiAwareNetworkSpecifier extends NetworkSpecifier implements
        dest.writeByteArray(peerMac);
        dest.writeByteArray(pmk);
        dest.writeString(passphrase);
        dest.writeInt(port);
        dest.writeInt(transportProtocol);
        dest.writeInt(requestorUid);
    }

@@ -202,19 +234,8 @@ public final class WifiAwareNetworkSpecifier extends NetworkSpecifier implements
    /** @hide */
    @Override
    public int hashCode() {
        int result = 17;

        result = 31 * result + type;
        result = 31 * result + role;
        result = 31 * result + clientId;
        result = 31 * result + sessionId;
        result = 31 * result + peerId;
        result = 31 * result + Arrays.hashCode(peerMac);
        result = 31 * result + Arrays.hashCode(pmk);
        result = 31 * result + Objects.hashCode(passphrase);
        result = 31 * result + requestorUid;

        return result;
        return Objects.hash(type, role, clientId, sessionId, peerId, Arrays.hashCode(peerMac),
                Arrays.hashCode(pmk), passphrase, port, transportProtocol, requestorUid);
    }

    /** @hide */
@@ -238,6 +259,8 @@ public final class WifiAwareNetworkSpecifier extends NetworkSpecifier implements
                && Arrays.equals(peerMac, lhs.peerMac)
                && Arrays.equals(pmk, lhs.pmk)
                && Objects.equals(passphrase, lhs.passphrase)
                && port == lhs.port
                && transportProtocol == lhs.transportProtocol
                && requestorUid == lhs.requestorUid;
    }

@@ -256,7 +279,8 @@ public final class WifiAwareNetworkSpecifier extends NetworkSpecifier implements
                .append(", pmk=").append((pmk == null) ? "<null>" : "<non-null>")
                // masking PII
                .append(", passphrase=").append((passphrase == null) ? "<null>" : "<non-null>")
                .append(", requestorUid=").append(requestorUid)
                .append(", port=").append(port).append(", transportProtocol=")
                .append(transportProtocol).append(", requestorUid=").append(requestorUid)
                .append("]");
        return sb.toString();
    }
+2 −1
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ public class WifiAwareAgentNetworkSpecifierTest {
                WifiAwareAgentNetworkSpecifier.CREATOR.createFromParcel(parcelR);

        assertEquals(dut, rereadDut);
        assertEquals(dut.hashCode(), rereadDut.hashCode());

        // Ensure that individual network specifiers are satisfied by both the original & marshaled
        // |WifiAwareNetworkAgentSpecifier instances.
@@ -181,6 +182,6 @@ public class WifiAwareAgentNetworkSpecifierTest {
    WifiAwareNetworkSpecifier getDummyNetworkSpecifier(int clientId) {
        return new WifiAwareNetworkSpecifier(WifiAwareNetworkSpecifier.NETWORK_SPECIFIER_TYPE_OOB,
                WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_INITIATOR, clientId, 0, 0, new byte[6],
                null, null, 0);
                null, null, 10, 5, 0);
    }
}
Loading