Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Unified Diff: net/android/java/src/org/chromium/net/AndroidNetworkLibrary.java

Issue 10905207: Implement net::GetNetworkList() for Android. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Refine the patch Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « build/android/gtest_filter/net_unittests_disabled ('k') | net/android/network_library.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/android/java/src/org/chromium/net/AndroidNetworkLibrary.java
diff --git a/net/android/java/src/org/chromium/net/AndroidNetworkLibrary.java b/net/android/java/src/org/chromium/net/AndroidNetworkLibrary.java
index ef8fa032e8fbc9884a214f14d3a59b2022d3385c..28208a76bdbdfb58be8baf453894798a362f66ff 100644
--- a/net/android/java/src/org/chromium/net/AndroidNetworkLibrary.java
+++ b/net/android/java/src/org/chromium/net/AndroidNetworkLibrary.java
@@ -12,6 +12,8 @@ import android.util.Log;
import org.chromium.base.CalledByNative;
import org.chromium.base.CalledByNativeUnchecked;
+import java.net.Inet6Address;
+import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URLConnection;
@@ -87,6 +89,59 @@ class AndroidNetworkLibrary {
}
/**
+ * @return the network interfaces list (if any) string. The items in
+ * the list string are delimited by a semicolon ";", each item
+ * is a network interface name and address pair and formatted
+ * as "name,address". e.g.
+ * eth0,10.0.0.2;eth0,fe80::5054:ff:fe12:3456
+ * represents a network list string which containts two items.
+ */
+ @CalledByNative
+ static public String getNetworkList() {
+ Enumeration<NetworkInterface> list = null;
+ try {
+ list = NetworkInterface.getNetworkInterfaces();
+ if (list == null) return "";
+ } catch (SocketException e) {
+ Log.w(TAG, "Unable to get network interfaces: " + e);
+ return "";
+ }
+
+ StringBuilder result = new StringBuilder();
+ while (list.hasMoreElements()) {
+ NetworkInterface netIf = list.nextElement();
+ try {
+ // Skip loopback interfaces, and ones which are down.
+ if (!netIf.isUp() || netIf.isLoopback())
+ continue;
+ Enumeration<InetAddress> addressList = netIf.getInetAddresses();
+ while (addressList.hasMoreElements()) {
+ InetAddress address = addressList.nextElement();
+ // Skip loopback addresses configured on non-loopback interfaces.
+ if (address.isLoopbackAddress())
+ continue;
+ StringBuilder addressString = new StringBuilder();
+ addressString.append(netIf.getName());
+ addressString.append(",");
+
+ String ipAddress = address.getHostAddress();
+ if (address instanceof Inet6Address && ipAddress.contains("%")) {
+ ipAddress = ipAddress.substring(0, ipAddress.lastIndexOf("%"));
+ }
+ addressString.append(ipAddress);
+
+ if (result.length() != 0)
+ result.append(";");
+ result.append(addressString.toString());
+ }
+ } catch (SocketException e) {
+ continue;
+ }
+ }
+ return result.toString();
+ }
+
+ /**
* Validate the server's certificate chain is trusted.
*
* @param certChain The ASN.1 DER encoded bytes for certificates.
« no previous file with comments | « build/android/gtest_filter/net_unittests_disabled ('k') | net/android/network_library.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698