use of java.net.InterfaceAddress in project android_frameworks_base by DirtyUnicorns.
the class NetworkManagementService method modifyNat.
private void modifyNat(String action, String internalInterface, String externalInterface) throws SocketException {
final Command cmd = new Command("nat", action, internalInterface, externalInterface);
final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(internalInterface);
if (internalNetworkInterface == null) {
cmd.appendArg("0");
} else {
// Don't touch link-local routes, as link-local addresses aren't routable,
// kernel creates link-local routes on all interfaces automatically
List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(internalNetworkInterface.getInterfaceAddresses());
cmd.appendArg(interfaceAddresses.size());
for (InterfaceAddress ia : interfaceAddresses) {
InetAddress addr = NetworkUtils.getNetworkPart(ia.getAddress(), ia.getNetworkPrefixLength());
cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
}
}
try {
mConnector.execute(cmd);
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
}
}
use of java.net.InterfaceAddress in project NetGuard by M66B.
the class Util method getNetworkInfo.
public static String getNetworkInfo(Context context) {
StringBuilder sb = new StringBuilder();
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ani = cm.getActiveNetworkInfo();
List<NetworkInfo> listNI = new ArrayList<>();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
listNI.addAll(Arrays.asList(cm.getAllNetworkInfo()));
else
for (Network network : cm.getAllNetworks()) {
NetworkInfo ni = cm.getNetworkInfo(network);
if (ni != null)
listNI.add(ni);
}
for (NetworkInfo ni : listNI) {
sb.append(ni.getTypeName()).append('/').append(ni.getSubtypeName()).append(' ').append(ni.getDetailedState()).append(TextUtils.isEmpty(ni.getExtraInfo()) ? "" : " " + ni.getExtraInfo()).append(ni.getType() == ConnectivityManager.TYPE_MOBILE ? " " + Util.getNetworkGeneration(ni.getSubtype()) : "").append(ni.isRoaming() ? " R" : "").append(ani != null && ni.getType() == ani.getType() && ni.getSubtype() == ani.getSubtype() ? " *" : "").append("\r\n");
}
try {
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
if (nis != null)
while (nis.hasMoreElements()) {
NetworkInterface ni = nis.nextElement();
if (ni != null && !ni.isLoopback()) {
List<InterfaceAddress> ias = ni.getInterfaceAddresses();
if (ias != null)
for (InterfaceAddress ia : ias) sb.append(ni.getName()).append(' ').append(ia.getAddress().getHostAddress()).append('/').append(ia.getNetworkPrefixLength()).append(' ').append(ni.getMTU()).append(' ').append(ni.isUp() ? '^' : 'v').append("\r\n");
}
}
} catch (Throwable ex) {
sb.append(ex.toString()).append("\r\n");
}
if (sb.length() > 2)
sb.setLength(sb.length() - 2);
return sb.toString();
}
use of java.net.InterfaceAddress in project jdk8u_jdk by JetBrains.
the class Equals method createInterfaceAddress.
/**
* Returns an InterfaceAddress instance with its fields set the the values
* specificed.
*/
static InterfaceAddress createInterfaceAddress(InetAddress address, InetAddress broadcast, short prefixlength) {
try {
Class<InterfaceAddress> IAClass = InterfaceAddress.class;
InterfaceAddress ia;
Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor();
ctr.setAccessible(true);
Field addressField = IAClass.getDeclaredField("address");
addressField.setAccessible(true);
Field broadcastField = IAClass.getDeclaredField("broadcast");
broadcastField.setAccessible(true);
Field maskLengthField = IAClass.getDeclaredField("maskLength");
maskLengthField.setAccessible(true);
ia = ctr.newInstance();
addressField.set(ia, address);
broadcastField.set(ia, broadcast);
maskLengthField.setShort(ia, prefixlength);
return ia;
} catch (NoSuchFieldException nsfe) {
nsfe.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException ie) {
ie.printStackTrace();
} catch (IllegalAccessException iae) {
iae.printStackTrace();
} catch (InvocationTargetException ite) {
ite.printStackTrace();
}
return null;
}
use of java.net.InterfaceAddress in project jdk8u_jdk by JetBrains.
the class NetworkPrefixLength method main.
public static void main(String[] args) throws Exception {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
while (nics.hasMoreElements()) {
NetworkInterface nic = nics.nextElement();
for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
boolean valid = checkPrefix(iaddr);
if (!valid) {
passed = false;
debug(nic.getName(), iaddr);
}
InetAddress ia = iaddr.getAddress();
if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
// assumption: prefix length will always be 8
if (iaddr.getNetworkPrefixLength() != 8) {
out.println("Expected prefix of 8, got " + iaddr);
passed = false;
}
}
}
}
if (!passed)
throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
use of java.net.InterfaceAddress in project android_frameworks_base by crdroidandroid.
the class NetworkManagementService method modifyNat.
private void modifyNat(String action, String internalInterface, String externalInterface) throws SocketException {
final Command cmd = new Command("nat", action, internalInterface, externalInterface);
final NetworkInterface internalNetworkInterface = NetworkInterface.getByName(internalInterface);
if (internalNetworkInterface == null) {
cmd.appendArg("0");
} else {
// Don't touch link-local routes, as link-local addresses aren't routable,
// kernel creates link-local routes on all interfaces automatically
List<InterfaceAddress> interfaceAddresses = excludeLinkLocal(internalNetworkInterface.getInterfaceAddresses());
cmd.appendArg(interfaceAddresses.size());
for (InterfaceAddress ia : interfaceAddresses) {
InetAddress addr = NetworkUtils.getNetworkPart(ia.getAddress(), ia.getNetworkPrefixLength());
cmd.appendArg(addr.getHostAddress() + "/" + ia.getNetworkPrefixLength());
}
}
try {
mConnector.execute(cmd);
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
}
}
Aggregations