Skip to content

Commit b998f31

Browse files
Robert GreenwaltAndroid Code Review
authored andcommitted
Merge "Add an API to request route to an IPv6 host."
2 parents 1ffd753 + 9bc709d commit b998f31

File tree

7 files changed

+191
-54
lines changed

7 files changed

+191
-54
lines changed

core/java/android/net/ConnectivityManager.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import android.os.Binder;
2222
import android.os.RemoteException;
2323

24+
import java.net.InetAddress;
25+
import java.net.UnknownHostException;
26+
2427
/**
2528
* Class that answers queries about the state of network connectivity. It also
2629
* notifies applications when network connectivity changes. Get an instance
@@ -309,8 +312,29 @@ public int stopUsingNetworkFeature(int networkType, String feature) {
309312
* @return {@code true} on success, {@code false} on failure
310313
*/
311314
public boolean requestRouteToHost(int networkType, int hostAddress) {
315+
InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
316+
317+
if (inetAddress == null) {
318+
return false;
319+
}
320+
321+
return requestRouteToHostAddress(networkType, inetAddress);
322+
}
323+
324+
/**
325+
* Ensure that a network route exists to deliver traffic to the specified
326+
* host via the specified network interface. An attempt to add a route that
327+
* already exists is ignored, but treated as successful.
328+
* @param networkType the type of the network over which traffic to the specified
329+
* host is to be routed
330+
* @param hostAddress the IP address of the host to which the route is desired
331+
* @return {@code true} on success, {@code false} on failure
332+
* @hide
333+
*/
334+
public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
335+
byte[] address = hostAddress.getAddress();
312336
try {
313-
return mService.requestRouteToHost(networkType, hostAddress);
337+
return mService.requestRouteToHostAddress(networkType, address);
314338
} catch (RemoteException e) {
315339
return false;
316340
}

core/java/android/net/IConnectivityManager.aidl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ interface IConnectivityManager
4747

4848
boolean requestRouteToHost(int networkType, int hostAddress);
4949

50+
boolean requestRouteToHostAddress(int networkType, in byte[] hostAddress);
51+
5052
boolean getBackgroundDataSetting();
5153

5254
void setBackgroundDataSetting(boolean allowBackgroundData);

core/java/android/net/MobileDataStateTracker.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616

1717
package android.net;
1818

19+
import java.net.InetAddress;
20+
1921
import android.content.BroadcastReceiver;
2022
import android.content.Context;
2123
import android.content.Intent;
2224
import android.content.IntentFilter;
2325
import android.os.RemoteException;
2426
import android.os.Handler;
2527
import android.os.ServiceManager;
26-
import android.os.SystemProperties;
2728
import com.android.internal.telephony.ITelephony;
2829
import com.android.internal.telephony.Phone;
2930
import com.android.internal.telephony.TelephonyIntents;
@@ -460,17 +461,16 @@ public int stopUsingNetworkFeature(String feature, int callingPid, int callingUi
460461
* Ensure that a network route exists to deliver traffic to the specified
461462
* host via the mobile data network.
462463
* @param hostAddress the IP address of the host to which the route is desired,
463-
* in network byte order.
464464
* @return {@code true} on success, {@code false} on failure
465465
*/
466466
@Override
467-
public boolean requestRouteToHost(int hostAddress) {
467+
public boolean requestRouteToHost(InetAddress hostAddress) {
468468
if (DBG) {
469-
Log.d(TAG, "Requested host route to " + Integer.toHexString(hostAddress) +
469+
Log.d(TAG, "Requested host route to " + hostAddress.getHostAddress() +
470470
" for " + mApnType + "(" + mInterfaceName + ")");
471471
}
472-
if (mInterfaceName != null && hostAddress != -1) {
473-
return NetworkUtils.addHostRoute(mInterfaceName, hostAddress) == 0;
472+
if (mInterfaceName != null) {
473+
return NetworkUtils.addHostRoute(mInterfaceName, hostAddress, null);
474474
} else {
475475
return false;
476476
}

core/java/android/net/NetworkStateTracker.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818

1919
import java.io.FileWriter;
2020
import java.io.IOException;
21+
import java.net.InetAddress;
22+
import java.net.UnknownHostException;
2123

2224
import android.os.Handler;
2325
import android.os.Message;
2426
import android.os.SystemProperties;
2527
import android.content.Context;
2628
import android.text.TextUtils;
27-
import android.util.Config;
2829
import android.util.Log;
2930

3031

@@ -129,13 +130,18 @@ public void addPrivateDnsRoutes() {
129130
}
130131
if (mInterfaceName != null && !mPrivateDnsRouteSet) {
131132
for (String addrString : getNameServers()) {
132-
int addr = NetworkUtils.lookupHost(addrString);
133-
if (addr != -1 && addr != 0) {
134-
if (DBG) Log.d(TAG, " adding "+addrString+" ("+addr+")");
135-
NetworkUtils.addHostRoute(mInterfaceName, addr);
133+
if (addrString != null) {
134+
try {
135+
InetAddress inetAddress = InetAddress.getByName(addrString);
136+
if (DBG) Log.d(TAG, " adding " + addrString);
137+
if (NetworkUtils.addHostRoute(mInterfaceName, inetAddress, null)) {
138+
mPrivateDnsRouteSet = true;
139+
}
140+
} catch (UnknownHostException e) {
141+
if (DBG) Log.d(TAG, " DNS address " + addrString + " : Exception " + e);
142+
}
136143
}
137144
}
138-
mPrivateDnsRouteSet = true;
139145
}
140146
}
141147

@@ -159,8 +165,16 @@ public void addDefaultRoute() {
159165
Log.d(TAG, "addDefaultRoute for " + mNetworkInfo.getTypeName() +
160166
" (" + mInterfaceName + "), GatewayAddr=" + mDefaultGatewayAddr);
161167
}
162-
NetworkUtils.setDefaultRoute(mInterfaceName, mDefaultGatewayAddr);
163-
mDefaultRouteSet = true;
168+
InetAddress inetAddress = NetworkUtils.intToInetAddress(mDefaultGatewayAddr);
169+
if (inetAddress == null) {
170+
if (DBG) Log.d(TAG, " Unable to add default route. mDefaultGatewayAddr Error");
171+
} else {
172+
if (NetworkUtils.addDefaultRoute(mInterfaceName, inetAddress)) {
173+
mDefaultRouteSet = true;
174+
} else {
175+
if (DBG) Log.d(TAG, " Unable to add default route.");
176+
}
177+
}
164178
}
165179
}
166180

@@ -398,7 +412,7 @@ protected void setSubtype(int subtype, String subtypeName) {
398412
* @param hostAddress the IP address of the host to which the route is desired
399413
* @return {@code true} on success, {@code false} on failure
400414
*/
401-
public boolean requestRouteToHost(int hostAddress) {
415+
public boolean requestRouteToHost(InetAddress hostAddress) {
402416
return false;
403417
}
404418

core/java/android/net/NetworkUtils.java

Lines changed: 88 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,39 @@
1717
package android.net;
1818

1919
import java.net.InetAddress;
20+
import java.net.Inet4Address;
21+
import java.net.Inet6Address;
2022
import java.net.UnknownHostException;
2123

24+
import android.util.Log;
25+
2226
/**
2327
* Native methods for managing network interfaces.
2428
*
2529
* {@hide}
2630
*/
2731
public class NetworkUtils {
32+
33+
private static final String TAG = "NetworkUtils";
34+
2835
/** Bring the named network interface up. */
2936
public native static int enableInterface(String interfaceName);
3037

3138
/** Bring the named network interface down. */
3239
public native static int disableInterface(String interfaceName);
3340

34-
/** Add a route to the specified host via the named interface. */
35-
public native static int addHostRoute(String interfaceName, int hostaddr);
36-
37-
/** Add a default route for the named interface. */
38-
public native static int setDefaultRoute(String interfaceName, int gwayAddr);
41+
/**
42+
* Add a route to the routing table.
43+
*
44+
* @param interfaceName the interface to route through.
45+
* @param dst the network or host to route to. May be IPv4 or IPv6, e.g.
46+
* "0.0.0.0" or "2001:4860::".
47+
* @param prefixLength the prefix length of the route.
48+
* @param gw the gateway to use, e.g., "192.168.251.1". If null,
49+
* indicates a directly-connected route.
50+
*/
51+
public native static int addRoute(String interfaceName, String dst,
52+
int prefixLength, String gw);
3953

4054
/** Return the gateway address for the default route for the named interface. */
4155
public native static int getDefaultRoute(String interfaceName);
@@ -106,26 +120,78 @@ private native static boolean configureNative(
106120
String interfaceName, int ipAddress, int netmask, int gateway, int dns1, int dns2);
107121

108122
/**
109-
* Look up a host name and return the result as an int. Works if the argument
110-
* is an IP address in dot notation. Obviously, this can only be used for IPv4
111-
* addresses.
112-
* @param hostname the name of the host (or the IP address)
113-
* @return the IP address as an {@code int} in network byte order
123+
* Convert a IPv4 address from an integer to an InetAddress.
124+
* @param hostAddr is an Int corresponding to the IPv4 address in network byte order
125+
* @return the IP address as an {@code InetAddress}, returns null if
126+
* unable to convert or if the int is an invalid address.
114127
*/
115-
public static int lookupHost(String hostname) {
128+
public static InetAddress intToInetAddress(int hostAddress) {
116129
InetAddress inetAddress;
130+
byte[] addressBytes = { (byte)(0xff & hostAddress),
131+
(byte)(0xff & (hostAddress >> 8)),
132+
(byte)(0xff & (hostAddress >> 16)),
133+
(byte)(0xff & (hostAddress >> 24)) };
134+
117135
try {
118-
inetAddress = InetAddress.getByName(hostname);
119-
} catch (UnknownHostException e) {
120-
return -1;
136+
inetAddress = InetAddress.getByAddress(addressBytes);
137+
} catch(UnknownHostException e) {
138+
return null;
139+
}
140+
141+
return inetAddress;
142+
}
143+
144+
/**
145+
* Add a default route through the specified gateway.
146+
* @param interfaceName interface on which the route should be added
147+
* @param gw the IP address of the gateway to which the route is desired,
148+
* @return {@code true} on success, {@code false} on failure
149+
*/
150+
public static boolean addDefaultRoute(String interfaceName, InetAddress gw) {
151+
String dstStr;
152+
String gwStr = gw.getHostAddress();
153+
154+
if (gw instanceof Inet4Address) {
155+
dstStr = "0.0.0.0";
156+
} else if (gw instanceof Inet6Address) {
157+
dstStr = "::";
158+
} else {
159+
Log.w(TAG, "addDefaultRoute failure: address is neither IPv4 nor IPv6" +
160+
"(" + gwStr + ")");
161+
return false;
162+
}
163+
return addRoute(interfaceName, dstStr, 0, gwStr) == 0;
164+
}
165+
166+
/**
167+
* Add a host route.
168+
* @param interfaceName interface on which the route should be added
169+
* @param dst the IP address of the host to which the route is desired,
170+
* this should not be null.
171+
* @param gw the IP address of the gateway to which the route is desired,
172+
* if null, indicates a directly-connected route.
173+
* @return {@code true} on success, {@code false} on failure
174+
*/
175+
public static boolean addHostRoute(String interfaceName, InetAddress dst,
176+
InetAddress gw) {
177+
if (dst == null) {
178+
Log.w(TAG, "addHostRoute: dst should not be null");
179+
return false;
180+
}
181+
182+
int prefixLength;
183+
String dstStr = dst.getHostAddress();
184+
String gwStr = (gw != null) ? gw.getHostAddress() : null;
185+
186+
if (dst instanceof Inet4Address) {
187+
prefixLength = 32;
188+
} else if (dst instanceof Inet6Address) {
189+
prefixLength = 128;
190+
} else {
191+
Log.w(TAG, "addHostRoute failure: address is neither IPv4 nor IPv6" +
192+
"(" + dst + ")");
193+
return false;
121194
}
122-
byte[] addrBytes;
123-
int addr;
124-
addrBytes = inetAddress.getAddress();
125-
addr = ((addrBytes[3] & 0xff) << 24)
126-
| ((addrBytes[2] & 0xff) << 16)
127-
| ((addrBytes[1] & 0xff) << 8)
128-
| (addrBytes[0] & 0xff);
129-
return addr;
195+
return addRoute(interfaceName, dstStr, prefixLength, gwStr) == 0;
130196
}
131197
}

core/jni/android_net_NetUtils.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,23 @@ static jint android_net_utils_disableInterface(JNIEnv* env, jobject clazz, jstri
6666
return (jint)result;
6767
}
6868

69-
static jint android_net_utils_addHostRoute(JNIEnv* env, jobject clazz, jstring ifname, jint addr)
69+
static jint android_net_utils_addRoute(JNIEnv* env, jobject clazz, jstring ifname,
70+
jstring dst, jint prefixLength, jstring gw)
7071
{
7172
int result;
7273

7374
const char *nameStr = env->GetStringUTFChars(ifname, NULL);
74-
result = ::ifc_add_host_route(nameStr, addr);
75+
const char *dstStr = env->GetStringUTFChars(dst, NULL);
76+
const char *gwStr = NULL;
77+
if (gw != NULL) {
78+
gwStr = env->GetStringUTFChars(gw, NULL);
79+
}
80+
result = ::ifc_add_route(nameStr, dstStr, prefixLength, gwStr);
7581
env->ReleaseStringUTFChars(ifname, nameStr);
82+
env->ReleaseStringUTFChars(dst, dstStr);
83+
if (gw != NULL) {
84+
env->ReleaseStringUTFChars(gw, gwStr);
85+
}
7686
return (jint)result;
7787
}
7888

@@ -86,16 +96,6 @@ static jint android_net_utils_removeHostRoutes(JNIEnv* env, jobject clazz, jstri
8696
return (jint)result;
8797
}
8898

89-
static jint android_net_utils_setDefaultRoute(JNIEnv* env, jobject clazz, jstring ifname, jint gateway)
90-
{
91-
int result;
92-
93-
const char *nameStr = env->GetStringUTFChars(ifname, NULL);
94-
result = ::ifc_set_default_route(nameStr, gateway);
95-
env->ReleaseStringUTFChars(ifname, nameStr);
96-
return (jint)result;
97-
}
98-
9999
static jint android_net_utils_getDefaultRoute(JNIEnv* env, jobject clazz, jstring ifname)
100100
{
101101
int result;
@@ -201,9 +201,9 @@ static JNINativeMethod gNetworkUtilMethods[] = {
201201

202202
{ "enableInterface", "(Ljava/lang/String;)I", (void *)android_net_utils_enableInterface },
203203
{ "disableInterface", "(Ljava/lang/String;)I", (void *)android_net_utils_disableInterface },
204-
{ "addHostRoute", "(Ljava/lang/String;I)I", (void *)android_net_utils_addHostRoute },
204+
{ "addRoute", "(Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)I",
205+
(void *)android_net_utils_addRoute },
205206
{ "removeHostRoutes", "(Ljava/lang/String;)I", (void *)android_net_utils_removeHostRoutes },
206-
{ "setDefaultRoute", "(Ljava/lang/String;I)I", (void *)android_net_utils_setDefaultRoute },
207207
{ "getDefaultRoute", "(Ljava/lang/String;)I", (void *)android_net_utils_getDefaultRoute },
208208
{ "removeDefaultRoute", "(Ljava/lang/String;)I", (void *)android_net_utils_removeDefaultRoute },
209209
{ "resetConnections", "(Ljava/lang/String;)I", (void *)android_net_utils_resetConnections },

0 commit comments

Comments
 (0)