Search in sources :

Example 61 with InterfaceAddress

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();
    }
}
Also used : Command(com.android.server.NativeDaemonConnector.Command) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 62 with InterfaceAddress

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();
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager) InterfaceAddress(java.net.InterfaceAddress) Network(android.net.Network) ArrayList(java.util.ArrayList) NetworkInterface(java.net.NetworkInterface) List(java.util.List) ArrayList(java.util.ArrayList)

Example 63 with InterfaceAddress

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;
}
Also used : Field(java.lang.reflect.Field) InterfaceAddress(java.net.InterfaceAddress) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 64 with InterfaceAddress

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");
}
Also used : Inet4Address(java.net.Inet4Address) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 65 with InterfaceAddress

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();
    }
}
Also used : Command(com.android.server.NativeDaemonConnector.Command) InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Aggregations

InterfaceAddress (java.net.InterfaceAddress)104 NetworkInterface (java.net.NetworkInterface)69 InetAddress (java.net.InetAddress)60 SocketException (java.net.SocketException)27 Inet4Address (java.net.Inet4Address)26 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)18 IOException (java.io.IOException)9 Inet6Address (java.net.Inet6Address)8 UnknownHostException (java.net.UnknownHostException)7 NotifyListener (net.mm2d.upnp.SsdpNotifyReceiver.NotifyListener)7 Command (com.android.server.NativeDaemonConnector.Command)6 LinkAddress (android.net.LinkAddress)5 DatagramPacket (java.net.DatagramPacket)5 List (java.util.List)5 InetSocketAddress (java.net.InetSocketAddress)4 SharedPreferences (android.content.SharedPreferences)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Enumeration (java.util.Enumeration)3 LinkedList (java.util.LinkedList)3