diff options
author | Paul Jensen <pauljensen@google.com> | 2014-05-30 01:45:51 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2014-05-30 01:45:52 +0000 |
commit | 034bfbb1130511cd826b36be55f4489f31ff4cbe (patch) | |
tree | ec4a3aae408bf5d225a48fbc63e4020adb86fc62 /core | |
parent | 4f8a92bd20be374811b2cab855f390221893279a (diff) | |
parent | 8ef3401d2ae613ff0f14d75be860f5c833019494 (diff) | |
download | frameworks_base-034bfbb1130511cd826b36be55f4489f31ff4cbe.zip frameworks_base-034bfbb1130511cd826b36be55f4489f31ff4cbe.tar.gz frameworks_base-034bfbb1130511cd826b36be55f4489f31ff4cbe.tar.bz2 |
Merge "Adjust Network's SocketFactory to restrict host name resolution to that Network" into lmp-preview-dev
Diffstat (limited to 'core')
-rw-r--r-- | core/java/android/net/Network.java | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/core/java/android/net/Network.java b/core/java/android/net/Network.java index e489e05..64516e6 100644 --- a/core/java/android/net/Network.java +++ b/core/java/android/net/Network.java @@ -94,11 +94,26 @@ public class Network implements Parcelable { mNetId = netId; } + private void connectToHost(Socket socket, String host, int port) throws IOException { + // Lookup addresses only on this Network. + InetAddress[] hostAddresses = getAllByName(host); + // Try all but last address ignoring exceptions. + for (int i = 0; i < hostAddresses.length - 1; i++) { + try { + socket.connect(new InetSocketAddress(hostAddresses[i], port)); + return; + } catch (IOException e) { + } + } + // Try last address. Do throw exceptions. + socket.connect(new InetSocketAddress(hostAddresses[hostAddresses.length - 1], port)); + } + @Override public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException { Socket socket = createSocket(); socket.bind(new InetSocketAddress(localHost, localPort)); - socket.connect(new InetSocketAddress(host, port)); + connectToHost(socket, host, port); return socket; } @@ -121,7 +136,7 @@ public class Network implements Parcelable { @Override public Socket createSocket(String host, int port) throws IOException { Socket socket = createSocket(); - socket.connect(new InetSocketAddress(host, port)); + connectToHost(socket, host, port); return socket; } |