Search in sources :

Example 46 with Inet4Address

use of java.net.Inet4Address in project android_frameworks_base by DirtyUnicorns.

the class WifiDisplayController method getSessionInfo.

private WifiDisplaySessionInfo getSessionInfo(WifiP2pGroup info, int session) {
    if (info == null) {
        return null;
    }
    Inet4Address addr = getInterfaceAddress(info);
    WifiDisplaySessionInfo sessionInfo = new WifiDisplaySessionInfo(!info.getOwner().deviceAddress.equals(mThisDevice.deviceAddress), session, info.getOwner().deviceAddress + " " + info.getNetworkName(), info.getPassphrase(), (addr != null) ? addr.getHostAddress() : "");
    if (DEBUG) {
        Slog.d(TAG, sessionInfo.toString());
    }
    return sessionInfo;
}
Also used : Inet4Address(java.net.Inet4Address) WifiDisplaySessionInfo(android.hardware.display.WifiDisplaySessionInfo)

Example 47 with Inet4Address

use of java.net.Inet4Address in project android_frameworks_base by DirtyUnicorns.

the class WifiDisplayController method getInterfaceAddress.

private static Inet4Address getInterfaceAddress(WifiP2pGroup info) {
    NetworkInterface iface;
    try {
        iface = NetworkInterface.getByName(info.getInterface());
    } catch (SocketException ex) {
        Slog.w(TAG, "Could not obtain address of network interface " + info.getInterface(), ex);
        return null;
    }
    Enumeration<InetAddress> addrs = iface.getInetAddresses();
    while (addrs.hasMoreElements()) {
        InetAddress addr = addrs.nextElement();
        if (addr instanceof Inet4Address) {
            return (Inet4Address) addr;
        }
    }
    Slog.w(TAG, "Could not obtain address of network interface " + info.getInterface() + " because it had no IPv4 addresses.");
    return null;
}
Also used : SocketException(java.net.SocketException) Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 48 with Inet4Address

use of java.net.Inet4Address in project android_frameworks_base by DirtyUnicorns.

the class IpConfigStore method readIpAndProxyConfigurations.

public SparseArray<IpConfiguration> readIpAndProxyConfigurations(String filePath) {
    SparseArray<IpConfiguration> networks = new SparseArray<IpConfiguration>();
    DataInputStream in = null;
    try {
        in = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
        int version = in.readInt();
        if (version != 2 && version != 1) {
            loge("Bad version on IP configuration file, ignore read");
            return null;
        }
        while (true) {
            int id = -1;
            // Default is DHCP with no proxy
            IpAssignment ipAssignment = IpAssignment.DHCP;
            ProxySettings proxySettings = ProxySettings.NONE;
            StaticIpConfiguration staticIpConfiguration = new StaticIpConfiguration();
            String proxyHost = null;
            String pacFileUrl = null;
            int proxyPort = -1;
            String exclusionList = null;
            String key;
            do {
                key = in.readUTF();
                try {
                    if (key.equals(ID_KEY)) {
                        id = in.readInt();
                    } else if (key.equals(IP_ASSIGNMENT_KEY)) {
                        ipAssignment = IpAssignment.valueOf(in.readUTF());
                    } else if (key.equals(LINK_ADDRESS_KEY)) {
                        LinkAddress linkAddr = new LinkAddress(NetworkUtils.numericToInetAddress(in.readUTF()), in.readInt());
                        if (linkAddr.getAddress() instanceof Inet4Address && staticIpConfiguration.ipAddress == null) {
                            staticIpConfiguration.ipAddress = linkAddr;
                        } else {
                            loge("Non-IPv4 or duplicate address: " + linkAddr);
                        }
                    } else if (key.equals(GATEWAY_KEY)) {
                        LinkAddress dest = null;
                        InetAddress gateway = null;
                        if (version == 1) {
                            // only supported default gateways - leave the dest/prefix empty
                            gateway = NetworkUtils.numericToInetAddress(in.readUTF());
                            if (staticIpConfiguration.gateway == null) {
                                staticIpConfiguration.gateway = gateway;
                            } else {
                                loge("Duplicate gateway: " + gateway.getHostAddress());
                            }
                        } else {
                            if (in.readInt() == 1) {
                                dest = new LinkAddress(NetworkUtils.numericToInetAddress(in.readUTF()), in.readInt());
                            }
                            if (in.readInt() == 1) {
                                gateway = NetworkUtils.numericToInetAddress(in.readUTF());
                            }
                            RouteInfo route = new RouteInfo(dest, gateway);
                            if (route.isIPv4Default() && staticIpConfiguration.gateway == null) {
                                staticIpConfiguration.gateway = gateway;
                            } else {
                                loge("Non-IPv4 default or duplicate route: " + route);
                            }
                        }
                    } else if (key.equals(DNS_KEY)) {
                        staticIpConfiguration.dnsServers.add(NetworkUtils.numericToInetAddress(in.readUTF()));
                    } else if (key.equals(PROXY_SETTINGS_KEY)) {
                        proxySettings = ProxySettings.valueOf(in.readUTF());
                    } else if (key.equals(PROXY_HOST_KEY)) {
                        proxyHost = in.readUTF();
                    } else if (key.equals(PROXY_PORT_KEY)) {
                        proxyPort = in.readInt();
                    } else if (key.equals(PROXY_PAC_FILE)) {
                        pacFileUrl = in.readUTF();
                    } else if (key.equals(EXCLUSION_LIST_KEY)) {
                        exclusionList = in.readUTF();
                    } else if (key.equals(EOS)) {
                        break;
                    } else {
                        loge("Ignore unknown key " + key + "while reading");
                    }
                } catch (IllegalArgumentException e) {
                    loge("Ignore invalid address while reading" + e);
                }
            } while (true);
            if (id != -1) {
                IpConfiguration config = new IpConfiguration();
                networks.put(id, config);
                switch(ipAssignment) {
                    case STATIC:
                        config.staticIpConfiguration = staticIpConfiguration;
                        config.ipAssignment = ipAssignment;
                        break;
                    case DHCP:
                        config.ipAssignment = ipAssignment;
                        break;
                    case UNASSIGNED:
                        loge("BUG: Found UNASSIGNED IP on file, use DHCP");
                        config.ipAssignment = IpAssignment.DHCP;
                        break;
                    default:
                        loge("Ignore invalid ip assignment while reading.");
                        config.ipAssignment = IpAssignment.UNASSIGNED;
                        break;
                }
                switch(proxySettings) {
                    case STATIC:
                        ProxyInfo proxyInfo = new ProxyInfo(proxyHost, proxyPort, exclusionList);
                        config.proxySettings = proxySettings;
                        config.httpProxy = proxyInfo;
                        break;
                    case PAC:
                        ProxyInfo proxyPacProperties = new ProxyInfo(pacFileUrl);
                        config.proxySettings = proxySettings;
                        config.httpProxy = proxyPacProperties;
                        break;
                    case NONE:
                        config.proxySettings = proxySettings;
                        break;
                    case UNASSIGNED:
                        loge("BUG: Found UNASSIGNED proxy on file, use NONE");
                        config.proxySettings = ProxySettings.NONE;
                        break;
                    default:
                        loge("Ignore invalid proxy settings while reading");
                        config.proxySettings = ProxySettings.UNASSIGNED;
                        break;
                }
            } else {
                if (DBG)
                    log("Missing id while parsing configuration");
            }
        }
    } catch (EOFException ignore) {
    } catch (IOException e) {
        loge("Error parsing configuration: " + e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) {
            }
        }
    }
    return networks;
}
Also used : LinkAddress(android.net.LinkAddress) Inet4Address(java.net.Inet4Address) IpConfiguration(android.net.IpConfiguration) StaticIpConfiguration(android.net.StaticIpConfiguration) IpAssignment(android.net.IpConfiguration.IpAssignment) ProxySettings(android.net.IpConfiguration.ProxySettings) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) EOFException(java.io.EOFException) ProxyInfo(android.net.ProxyInfo) SparseArray(android.util.SparseArray) BufferedInputStream(java.io.BufferedInputStream) StaticIpConfiguration(android.net.StaticIpConfiguration) EOFException(java.io.EOFException) RouteInfo(android.net.RouteInfo) InetAddress(java.net.InetAddress)

Example 49 with Inet4Address

use of java.net.Inet4Address in project jdk8u_jdk by JetBrains.

the class ListenAddress method main.

public static void main(String[] args) throws Exception {
    ListeningConnector connector = (ListeningConnector) findConnector("com.sun.jdi.SocketListen");
    // check wildcard address
    check(connector, (InetAddress) null);
    // iterate over all IPv4 addresses and check that binding to
    // that address results in the correct result from startListening(Map)
    Enumeration nifs = NetworkInterface.getNetworkInterfaces();
    while (nifs.hasMoreElements()) {
        NetworkInterface ni = (NetworkInterface) nifs.nextElement();
        Enumeration addrs = ni.getInetAddresses();
        while (addrs.hasMoreElements()) {
            InetAddress addr = (InetAddress) addrs.nextElement();
            // JPDA implementation only currently supports IPv4
            if (!(addr instanceof Inet4Address)) {
                continue;
            }
            check(connector, addr);
        }
    }
    if (failures > 0) {
        throw new RuntimeException(failures + " test(s) failed - see output for details.");
    }
}
Also used : Inet4Address(java.net.Inet4Address) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 50 with Inet4Address

use of java.net.Inet4Address in project android_frameworks_base by AOSPA.

the class LinkAddressTest method testConstructors.

public void testConstructors() throws SocketException {
    LinkAddress address;
    // Valid addresses work as expected.
    address = new LinkAddress(V4_ADDRESS, 25);
    assertEquals(V4_ADDRESS, address.getAddress());
    assertEquals(25, address.getPrefixLength());
    assertEquals(0, address.getFlags());
    assertEquals(RT_SCOPE_UNIVERSE, address.getScope());
    address = new LinkAddress(V6_ADDRESS, 127);
    assertEquals(V6_ADDRESS, address.getAddress());
    assertEquals(127, address.getPrefixLength());
    assertEquals(0, address.getFlags());
    assertEquals(RT_SCOPE_UNIVERSE, address.getScope());
    // Nonsensical flags/scopes or combinations thereof are acceptable.
    address = new LinkAddress(V6 + "/64", IFA_F_DEPRECATED | IFA_F_PERMANENT, RT_SCOPE_LINK);
    assertEquals(V6_ADDRESS, address.getAddress());
    assertEquals(64, address.getPrefixLength());
    assertEquals(IFA_F_DEPRECATED | IFA_F_PERMANENT, address.getFlags());
    assertEquals(RT_SCOPE_LINK, address.getScope());
    address = new LinkAddress(V4 + "/23", 123, 456);
    assertEquals(V4_ADDRESS, address.getAddress());
    assertEquals(23, address.getPrefixLength());
    assertEquals(123, address.getFlags());
    assertEquals(456, address.getScope());
    // InterfaceAddress doesn't have a constructor. Fetch some from an interface.
    List<InterfaceAddress> addrs = NetworkInterface.getByName("lo").getInterfaceAddresses();
    // We expect to find 127.0.0.1/8 and ::1/128, in any order.
    LinkAddress ipv4Loopback, ipv6Loopback;
    assertEquals(2, addrs.size());
    if (addrs.get(0).getAddress() instanceof Inet4Address) {
        ipv4Loopback = new LinkAddress(addrs.get(0));
        ipv6Loopback = new LinkAddress(addrs.get(1));
    } else {
        ipv4Loopback = new LinkAddress(addrs.get(1));
        ipv6Loopback = new LinkAddress(addrs.get(0));
    }
    assertEquals(NetworkUtils.numericToInetAddress("127.0.0.1"), ipv4Loopback.getAddress());
    assertEquals(8, ipv4Loopback.getPrefixLength());
    assertEquals(NetworkUtils.numericToInetAddress("::1"), ipv6Loopback.getAddress());
    assertEquals(128, ipv6Loopback.getPrefixLength());
    // Null addresses are rejected.
    try {
        address = new LinkAddress(null, 24);
        fail("Null InetAddress should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress((String) null, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
        fail("Null string should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress((InterfaceAddress) null);
        fail("Null string should cause NullPointerException");
    } catch (NullPointerException expected) {
    }
    // Invalid prefix lengths are rejected.
    try {
        address = new LinkAddress(V4_ADDRESS, -1);
        fail("Negative IPv4 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress(V6_ADDRESS, -1);
        fail("Negative IPv6 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress(V4_ADDRESS, 33);
        fail("/33 IPv4 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress(V4 + "/33", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
        fail("/33 IPv4 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress(V6_ADDRESS, 129, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
        fail("/129 IPv6 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress(V6 + "/129", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
        fail("/129 IPv6 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    // Multicast addresses are rejected.
    try {
        address = new LinkAddress("224.0.0.2/32");
        fail("IPv4 multicast address should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress("ff02::1/128");
        fail("IPv6 multicast address should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
}
Also used : LinkAddress(android.net.LinkAddress) Inet4Address(java.net.Inet4Address) InterfaceAddress(java.net.InterfaceAddress)

Aggregations

Inet4Address (java.net.Inet4Address)184 InetAddress (java.net.InetAddress)85 Inet6Address (java.net.Inet6Address)45 NetworkInterface (java.net.NetworkInterface)39 UnknownHostException (java.net.UnknownHostException)28 LinkAddress (android.net.LinkAddress)24 SocketException (java.net.SocketException)23 IOException (java.io.IOException)22 ArrayList (java.util.ArrayList)19 InterfaceAddress (java.net.InterfaceAddress)17 ByteBuffer (java.nio.ByteBuffer)17 RouteInfo (android.net.RouteInfo)12 LinkProperties (android.net.LinkProperties)7 InetSocketAddress (java.net.InetSocketAddress)6 Test (org.junit.Test)6 WifiDisplay (android.hardware.display.WifiDisplay)5 WifiDisplaySessionInfo (android.hardware.display.WifiDisplaySessionInfo)5 DhcpResults (android.net.DhcpResults)5 IpConfiguration (android.net.IpConfiguration)5 IpAssignment (android.net.IpConfiguration.IpAssignment)5