Merge "Add an API to select a network for a DatagramSocket." into lmp-mr1-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
b1df48e469
@@ -17034,6 +17034,7 @@ package android.net {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public class Network implements android.os.Parcelable {
|
public class Network implements android.os.Parcelable {
|
||||||
|
method public void bindSocket(java.net.DatagramSocket) throws java.io.IOException;
|
||||||
method public void bindSocket(java.net.Socket) throws java.io.IOException;
|
method public void bindSocket(java.net.Socket) throws java.io.IOException;
|
||||||
method public int describeContents();
|
method public int describeContents();
|
||||||
method public java.net.InetAddress[] getAllByName(java.lang.String) throws java.net.UnknownHostException;
|
method public java.net.InetAddress[] getAllByName(java.lang.String) throws java.net.UnknownHostException;
|
||||||
|
|||||||
@@ -21,7 +21,9 @@ import android.os.Parcelable;
|
|||||||
import android.os.Parcel;
|
import android.os.Parcel;
|
||||||
import android.system.ErrnoException;
|
import android.system.ErrnoException;
|
||||||
|
|
||||||
|
import java.io.FileDescriptor;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
@@ -263,19 +265,41 @@ public class Network implements Parcelable {
|
|||||||
.open(url);
|
.open(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Binds the specified {@link DatagramSocket} to this {@code Network}. All data traffic on the
|
||||||
|
* socket will be sent on this {@code Network}, irrespective of any process-wide network binding
|
||||||
|
* set by {@link ConnectivityManager#setProcessDefaultNetwork}. The socket must not be
|
||||||
|
* connected.
|
||||||
|
*/
|
||||||
|
public void bindSocket(DatagramSocket socket) throws IOException {
|
||||||
|
// Apparently, the kernel doesn't update a connected UDP socket's routing upon mark changes.
|
||||||
|
if (socket.isConnected()) {
|
||||||
|
throw new SocketException("Socket is connected");
|
||||||
|
}
|
||||||
|
// Query a property of the underlying socket to ensure that the socket's file descriptor
|
||||||
|
// exists, is available to bind to a network and is not closed.
|
||||||
|
socket.getReuseAddress();
|
||||||
|
bindSocketFd(socket.getFileDescriptor$());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Binds the specified {@link Socket} to this {@code Network}. All data traffic on the socket
|
* Binds the specified {@link Socket} to this {@code Network}. All data traffic on the socket
|
||||||
* will be sent on this {@code Network}, irrespective of any process-wide network binding set by
|
* will be sent on this {@code Network}, irrespective of any process-wide network binding set by
|
||||||
* {@link ConnectivityManager#setProcessDefaultNetwork}. The socket must not be connected.
|
* {@link ConnectivityManager#setProcessDefaultNetwork}. The socket must not be connected.
|
||||||
*/
|
*/
|
||||||
public void bindSocket(Socket socket) throws IOException {
|
public void bindSocket(Socket socket) throws IOException {
|
||||||
|
// Apparently, the kernel doesn't update a connected TCP socket's routing upon mark changes.
|
||||||
if (socket.isConnected()) {
|
if (socket.isConnected()) {
|
||||||
throw new SocketException("Socket is connected");
|
throw new SocketException("Socket is connected");
|
||||||
}
|
}
|
||||||
// Query a property of the underlying socket to ensure the underlying
|
// Query a property of the underlying socket to ensure that the socket's file descriptor
|
||||||
// socket exists so a file descriptor is available to bind to a network.
|
// exists, is available to bind to a network and is not closed.
|
||||||
socket.getReuseAddress();
|
socket.getReuseAddress();
|
||||||
int err = NetworkUtils.bindSocketToNetwork(socket.getFileDescriptor$().getInt$(), netId);
|
bindSocketFd(socket.getFileDescriptor$());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void bindSocketFd(FileDescriptor fd) throws IOException {
|
||||||
|
int err = NetworkUtils.bindSocketToNetwork(fd.getInt$(), netId);
|
||||||
if (err != 0) {
|
if (err != 0) {
|
||||||
// bindSocketToNetwork returns negative errno.
|
// bindSocketToNetwork returns negative errno.
|
||||||
throw new ErrnoException("Binding socket to network " + netId, -err)
|
throw new ErrnoException("Binding socket to network " + netId, -err)
|
||||||
|
|||||||
Reference in New Issue
Block a user