use of android.net.RouteInfo in project android_frameworks_base by ParanoidAndroid.
the class WifiService method getDhcpInfo.
/**
* Return the DHCP-assigned addresses from the last successful DHCP request,
* if any.
* @return the DHCP information
* @deprecated
*/
public DhcpInfo getDhcpInfo() {
enforceAccessPermission();
DhcpResults dhcpResults = mWifiStateMachine.syncGetDhcpResults();
if (dhcpResults.linkProperties == null)
return null;
DhcpInfo info = new DhcpInfo();
for (LinkAddress la : dhcpResults.linkProperties.getLinkAddresses()) {
InetAddress addr = la.getAddress();
if (addr instanceof Inet4Address) {
info.ipAddress = NetworkUtils.inetAddressToInt((Inet4Address) addr);
break;
}
}
for (RouteInfo r : dhcpResults.linkProperties.getRoutes()) {
if (r.isDefaultRoute()) {
InetAddress gateway = r.getGateway();
if (gateway instanceof Inet4Address) {
info.gateway = NetworkUtils.inetAddressToInt((Inet4Address) gateway);
}
} else if (r.hasGateway() == false) {
LinkAddress dest = r.getDestination();
if (dest.getAddress() instanceof Inet4Address) {
info.netmask = NetworkUtils.prefixLengthToNetmaskInt(dest.getNetworkPrefixLength());
}
}
}
int dnsFound = 0;
for (InetAddress dns : dhcpResults.linkProperties.getDnses()) {
if (dns instanceof Inet4Address) {
if (dnsFound == 0) {
info.dns1 = NetworkUtils.inetAddressToInt((Inet4Address) dns);
} else {
info.dns2 = NetworkUtils.inetAddressToInt((Inet4Address) dns);
}
if (++dnsFound > 1)
break;
}
}
InetAddress serverAddress = dhcpResults.serverAddress;
if (serverAddress instanceof Inet4Address) {
info.serverAddress = NetworkUtils.inetAddressToInt((Inet4Address) serverAddress);
}
info.leaseDuration = dhcpResults.leaseDuration;
return info;
}
use of android.net.RouteInfo in project android_frameworks_base by ParanoidAndroid.
the class WifiConfigStore method readIpAndProxyConfigurations.
private void readIpAndProxyConfigurations() {
DataInputStream in = null;
try {
in = new DataInputStream(new BufferedInputStream(new FileInputStream(ipConfigFile)));
int version = in.readInt();
if (version != 2 && version != 1) {
loge("Bad version on IP configuration file, ignore read");
return;
}
while (true) {
int id = -1;
// Default is DHCP with no proxy
IpAssignment ipAssignment = IpAssignment.DHCP;
ProxySettings proxySettings = ProxySettings.NONE;
LinkProperties linkProperties = new LinkProperties();
String proxyHost = 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());
linkProperties.addLinkAddress(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());
} else {
if (in.readInt() == 1) {
dest = new LinkAddress(NetworkUtils.numericToInetAddress(in.readUTF()), in.readInt());
}
if (in.readInt() == 1) {
gateway = NetworkUtils.numericToInetAddress(in.readUTF());
}
}
linkProperties.addRoute(new RouteInfo(dest, gateway));
} else if (key.equals(DNS_KEY)) {
linkProperties.addDns(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(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) {
WifiConfiguration config = mConfiguredNetworks.get(mNetworkIds.get(id));
if (config == null) {
loge("configuration found for missing network, ignored");
} else {
config.linkProperties = linkProperties;
switch(ipAssignment) {
case STATIC:
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");
break;
}
switch(proxySettings) {
case STATIC:
config.proxySettings = proxySettings;
ProxyProperties proxyProperties = new ProxyProperties(proxyHost, proxyPort, exclusionList);
linkProperties.setHttpProxy(proxyProperties);
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");
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) {
}
}
}
}
use of android.net.RouteInfo in project platform_frameworks_base by android.
the class LinkPropertiesTest method testRouteInterfaces.
@SmallTest
public void testRouteInterfaces() {
LinkAddress prefix = new LinkAddress(NetworkUtils.numericToInetAddress("2001:db8::"), 32);
InetAddress address = ADDRV6;
// Add a route with no interface to a LinkProperties with no interface. No errors.
LinkProperties lp = new LinkProperties();
RouteInfo r = new RouteInfo(prefix, address, null);
assertTrue(lp.addRoute(r));
assertEquals(1, lp.getRoutes().size());
assertAllRoutesHaveInterface(null, lp);
// Adding the same route twice has no effect.
assertFalse(lp.addRoute(r));
assertEquals(1, lp.getRoutes().size());
// Add a route with an interface. Expect an exception.
r = new RouteInfo(prefix, address, "wlan0");
try {
lp.addRoute(r);
fail("Adding wlan0 route to LP with no interface, expect exception");
} catch (IllegalArgumentException expected) {
}
// Change the interface name. All the routes should change their interface name too.
lp.setInterfaceName("rmnet0");
assertAllRoutesHaveInterface("rmnet0", lp);
// Now add a route with the wrong interface. This causes an exception too.
try {
lp.addRoute(r);
fail("Adding wlan0 route to rmnet0 LP, expect exception");
} catch (IllegalArgumentException expected) {
}
// If the interface name matches, the route is added.
r = new RouteInfo(prefix, null, "wlan0");
lp.setInterfaceName("wlan0");
lp.addRoute(r);
assertEquals(2, lp.getRoutes().size());
assertAllRoutesHaveInterface("wlan0", lp);
// Routes with null interfaces are converted to wlan0.
r = RouteInfo.makeHostRoute(ADDRV6, null);
lp.addRoute(r);
assertEquals(3, lp.getRoutes().size());
assertAllRoutesHaveInterface("wlan0", lp);
// Check comparisons work.
LinkProperties lp2 = new LinkProperties(lp);
assertAllRoutesHaveInterface("wlan0", lp);
assertEquals(0, lp.compareAllRoutes(lp2).added.size());
assertEquals(0, lp.compareAllRoutes(lp2).removed.size());
lp2.setInterfaceName("p2p0");
assertAllRoutesHaveInterface("p2p0", lp2);
assertEquals(3, lp.compareAllRoutes(lp2).added.size());
assertEquals(3, lp.compareAllRoutes(lp2).removed.size());
}
use of android.net.RouteInfo in project platform_frameworks_base by android.
the class LinkPropertiesTest method testIsReachable.
@SmallTest
// Failing.
@Suppress
public void testIsReachable() {
final LinkProperties v4lp = new LinkProperties();
assertFalse(v4lp.isReachable(DNS1));
assertFalse(v4lp.isReachable(DNS2));
// Add an on-link route, making the on-link DNS server reachable,
// but there is still no IPv4 address.
assertTrue(v4lp.addRoute(new RouteInfo(new IpPrefix(NetworkUtils.numericToInetAddress("75.208.0.0"), 16))));
assertFalse(v4lp.isReachable(DNS1));
assertFalse(v4lp.isReachable(DNS2));
// Adding an IPv4 address (right now, any IPv4 address) means we use
// the routes to compute likely reachability.
assertTrue(v4lp.addLinkAddress(new LinkAddress(ADDRV4, 16)));
assertTrue(v4lp.isReachable(DNS1));
assertFalse(v4lp.isReachable(DNS2));
// Adding a default route makes the off-link DNS server reachable.
assertTrue(v4lp.addRoute(new RouteInfo(GATEWAY1)));
assertTrue(v4lp.isReachable(DNS1));
assertTrue(v4lp.isReachable(DNS2));
final LinkProperties v6lp = new LinkProperties();
final InetAddress kLinkLocalDns = NetworkUtils.numericToInetAddress("fe80::6:1");
final InetAddress kLinkLocalDnsWithScope = NetworkUtils.numericToInetAddress("fe80::6:2%43");
final InetAddress kOnLinkDns = NetworkUtils.numericToInetAddress("2001:db8:85a3::53");
assertFalse(v6lp.isReachable(kLinkLocalDns));
assertFalse(v6lp.isReachable(kLinkLocalDnsWithScope));
assertFalse(v6lp.isReachable(kOnLinkDns));
assertFalse(v6lp.isReachable(DNS6));
// Add a link-local route, making the link-local DNS servers reachable. Because
// we assume the presence of an IPv6 link-local address, link-local DNS servers
// are considered reachable, but only those with a non-zero scope identifier.
assertTrue(v6lp.addRoute(new RouteInfo(new IpPrefix(NetworkUtils.numericToInetAddress("fe80::"), 64))));
assertFalse(v6lp.isReachable(kLinkLocalDns));
assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
assertFalse(v6lp.isReachable(kOnLinkDns));
assertFalse(v6lp.isReachable(DNS6));
// Add a link-local address--nothing changes.
assertTrue(v6lp.addLinkAddress(LINKADDRV6LINKLOCAL));
assertFalse(v6lp.isReachable(kLinkLocalDns));
assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
assertFalse(v6lp.isReachable(kOnLinkDns));
assertFalse(v6lp.isReachable(DNS6));
// Add a global route on link, but no global address yet. DNS servers reachable
// via a route that doesn't require a gateway: give them the benefit of the
// doubt and hope the link-local source address suffices for communication.
assertTrue(v6lp.addRoute(new RouteInfo(new IpPrefix(NetworkUtils.numericToInetAddress("2001:db8:85a3::"), 64))));
assertFalse(v6lp.isReachable(kLinkLocalDns));
assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
assertTrue(v6lp.isReachable(kOnLinkDns));
assertFalse(v6lp.isReachable(DNS6));
// Add a global address; the on-link global address DNS server is (still)
// presumed reachable.
assertTrue(v6lp.addLinkAddress(new LinkAddress(ADDRV6, 64)));
assertFalse(v6lp.isReachable(kLinkLocalDns));
assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
assertTrue(v6lp.isReachable(kOnLinkDns));
assertFalse(v6lp.isReachable(DNS6));
// Adding a default route makes the off-link DNS server reachable.
assertTrue(v6lp.addRoute(new RouteInfo(GATEWAY62)));
assertFalse(v6lp.isReachable(kLinkLocalDns));
assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
assertTrue(v6lp.isReachable(kOnLinkDns));
assertTrue(v6lp.isReachable(DNS6));
// Check isReachable on stacked links. This requires that the source IP address be assigned
// on the interface returned by the route lookup.
LinkProperties stacked = new LinkProperties();
// Can't add a stacked link without an interface name.
stacked.setInterfaceName("v4-test0");
v6lp.addStackedLink(stacked);
InetAddress stackedAddress = Address("192.0.0.4");
LinkAddress stackedLinkAddress = new LinkAddress(stackedAddress, 32);
assertFalse(v6lp.isReachable(stackedAddress));
stacked.addLinkAddress(stackedLinkAddress);
assertFalse(v6lp.isReachable(stackedAddress));
stacked.addRoute(new RouteInfo(stackedLinkAddress));
assertTrue(stacked.isReachable(stackedAddress));
assertTrue(v6lp.isReachable(stackedAddress));
assertFalse(v6lp.isReachable(DNS1));
stacked.addRoute(new RouteInfo((IpPrefix) null, stackedAddress));
assertTrue(v6lp.isReachable(DNS1));
}
use of android.net.RouteInfo in project platform_frameworks_base by android.
the class LinkPropertiesTest method testCompareProvisioning.
@SmallTest
public void testCompareProvisioning() {
LinkProperties v4lp = new LinkProperties();
v4lp.addLinkAddress(LINKADDRV4);
v4lp.addRoute(new RouteInfo(GATEWAY1));
v4lp.addDnsServer(DNS1);
assertTrue(v4lp.isProvisioned());
LinkProperties v4r = new LinkProperties(v4lp);
v4r.removeDnsServer(DNS1);
assertFalse(v4r.isProvisioned());
assertEquals(ProvisioningChange.STILL_NOT_PROVISIONED, LinkProperties.compareProvisioning(v4r, v4r));
assertEquals(ProvisioningChange.LOST_PROVISIONING, LinkProperties.compareProvisioning(v4lp, v4r));
assertEquals(ProvisioningChange.GAINED_PROVISIONING, LinkProperties.compareProvisioning(v4r, v4lp));
assertEquals(ProvisioningChange.STILL_PROVISIONED, LinkProperties.compareProvisioning(v4lp, v4lp));
// Check that losing IPv4 provisioning on a dualstack network is
// seen as a total loss of provisioning.
LinkProperties v6lp = new LinkProperties();
v6lp.addLinkAddress(LINKADDRV6);
v6lp.addRoute(new RouteInfo(GATEWAY61));
v6lp.addDnsServer(DNS6);
assertFalse(v6lp.isIPv4Provisioned());
assertTrue(v6lp.isIPv6Provisioned());
assertTrue(v6lp.isProvisioned());
LinkProperties v46lp = new LinkProperties(v6lp);
v46lp.addLinkAddress(LINKADDRV4);
v46lp.addRoute(new RouteInfo(GATEWAY1));
v46lp.addDnsServer(DNS1);
assertTrue(v46lp.isIPv4Provisioned());
assertTrue(v46lp.isIPv6Provisioned());
assertTrue(v46lp.isProvisioned());
assertEquals(ProvisioningChange.STILL_PROVISIONED, LinkProperties.compareProvisioning(v4lp, v46lp));
assertEquals(ProvisioningChange.STILL_PROVISIONED, LinkProperties.compareProvisioning(v6lp, v46lp));
assertEquals(ProvisioningChange.LOST_PROVISIONING, LinkProperties.compareProvisioning(v46lp, v6lp));
assertEquals(ProvisioningChange.LOST_PROVISIONING, LinkProperties.compareProvisioning(v46lp, v4lp));
// Check that losing and gaining a secondary router does not change
// the provisioning status.
LinkProperties v6lp2 = new LinkProperties(v6lp);
v6lp2.addRoute(new RouteInfo(GATEWAY62));
assertTrue(v6lp2.isProvisioned());
assertEquals(ProvisioningChange.STILL_PROVISIONED, LinkProperties.compareProvisioning(v6lp2, v6lp));
assertEquals(ProvisioningChange.STILL_PROVISIONED, LinkProperties.compareProvisioning(v6lp, v6lp2));
}
Aggregations