/** * Returns the NetworkInterface corresponding to the given interface index, or null if no * interface has this index. * * @throws SocketException if an error occurs. * @since 1.7 */ public static NetworkInterface getByIndex(int index) throws SocketException { String name = Libcore.os.if_indextoname(index); if (name == null) { return null; } return NetworkInterface.getByName(name); }
private static List<NetworkInterface> getNetworkInterfacesList() throws SocketException { String[] interfaceNames = getInterfaceNames(); NetworkInterface[] interfaces = new NetworkInterface[interfaceNames.length]; boolean[] done = new boolean[interfaces.length]; for (int i = 0; i < interfaceNames.length; ++i) { interfaces[i] = NetworkInterface.getByName(interfaceNames[i]); // readdir(2) and our stat(2), so mark interfaces that disappeared as 'done'. if (interfaces[i] == null) { done[i] = true; } } List<NetworkInterface> result = new ArrayList<NetworkInterface>(); for (int counter = 0; counter < interfaces.length; counter++) { // If this interface has been dealt with already, continue. if (done[counter]) { continue; } int counter2 = counter; // Checks whether the following interfaces are children. for (; counter2 < interfaces.length; counter2++) { if (done[counter2]) { continue; } if (interfaces[counter2].name.startsWith(interfaces[counter].name + ":")) { interfaces[counter].children.add(interfaces[counter2]); interfaces[counter2].parent = interfaces[counter]; interfaces[counter].addresses.addAll(interfaces[counter2].addresses); done[counter2] = true; } } result.add(interfaces[counter]); done[counter] = true; } return result; }