blob: 6b1fe99715f0c8b4be9da8680eab1d4d15dc7714 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
19import java.net.InetAddress;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070020import java.net.Inet4Address;
21import java.net.Inet6Address;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import java.net.UnknownHostException;
23
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070024import android.util.Log;
25
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026/**
27 * Native methods for managing network interfaces.
28 *
29 * {@hide}
30 */
31public class NetworkUtils {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070032
33 private static final String TAG = "NetworkUtils";
34
Mike Lockwood0900f362009-07-10 17:24:07 -040035 /** Bring the named network interface up. */
36 public native static int enableInterface(String interfaceName);
37
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 /** Bring the named network interface down. */
39 public native static int disableInterface(String interfaceName);
40
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070041 /**
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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54 /** Return the gateway address for the default route for the named interface. */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070055 public static InetAddress getDefaultRoute(String interfaceName) {
56 int addr = getDefaultRouteNative(interfaceName);
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070057 return intToInetAddress(addr);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070058 }
59 private native static int getDefaultRouteNative(String interfaceName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
61 /** Remove host routes that uses the named interface. */
62 public native static int removeHostRoutes(String interfaceName);
63
64 /** Remove the default route for the named interface. */
65 public native static int removeDefaultRoute(String interfaceName);
66
67 /** Reset any sockets that are connected via the named interface. */
68 public native static int resetConnections(String interfaceName);
69
70 /**
71 * Start the DHCP client daemon, in order to have it request addresses
72 * for the named interface, and then configure the interface with those
73 * addresses. This call blocks until it obtains a result (either success
74 * or failure) from the daemon.
75 * @param interfaceName the name of the interface to configure
76 * @param ipInfo if the request succeeds, this object is filled in with
77 * the IP address information.
78 * @return {@code true} for success, {@code false} for failure
79 */
80 public native static boolean runDhcp(String interfaceName, DhcpInfo ipInfo);
81
82 /**
83 * Shut down the DHCP client daemon.
84 * @param interfaceName the name of the interface for which the daemon
85 * should be stopped
86 * @return {@code true} for success, {@code false} for failure
87 */
88 public native static boolean stopDhcp(String interfaceName);
89
90 /**
91 * Release the current DHCP lease.
92 * @param interfaceName the name of the interface for which the lease should
93 * be released
94 * @return {@code true} for success, {@code false} for failure
95 */
96 public native static boolean releaseDhcpLease(String interfaceName);
97
98 /**
99 * Return the last DHCP-related error message that was recorded.
100 * <p/>NOTE: This string is not localized, but currently it is only
101 * used in logging.
102 * @return the most recent error message, if any
103 */
104 public native static String getDhcpError();
105
106 /**
107 * When static IP configuration has been specified, configure the network
108 * interface according to the values supplied.
109 * @param interfaceName the name of the interface to configure
110 * @param ipInfo the IP address, default gateway, and DNS server addresses
111 * with which to configure the interface.
112 * @return {@code true} for success, {@code false} for failure
113 */
114 public static boolean configureInterface(String interfaceName, DhcpInfo ipInfo) {
115 return configureNative(interfaceName,
116 ipInfo.ipAddress,
117 ipInfo.netmask,
118 ipInfo.gateway,
119 ipInfo.dns1,
120 ipInfo.dns2);
121 }
122
123 private native static boolean configureNative(
124 String interfaceName, int ipAddress, int netmask, int gateway, int dns1, int dns2);
125
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700126 /**
127 * Convert a IPv4 address from an integer to an InetAddress.
128 * @param hostAddr is an Int corresponding to the IPv4 address in network byte order
129 * @return the IP address as an {@code InetAddress}, returns null if
130 * unable to convert or if the int is an invalid address.
131 */
132 public static InetAddress intToInetAddress(int hostAddress) {
133 InetAddress inetAddress;
134 byte[] addressBytes = { (byte)(0xff & hostAddress),
135 (byte)(0xff & (hostAddress >> 8)),
136 (byte)(0xff & (hostAddress >> 16)),
137 (byte)(0xff & (hostAddress >> 24)) };
138
139 try {
140 inetAddress = InetAddress.getByAddress(addressBytes);
141 } catch(UnknownHostException e) {
142 return null;
143 }
144
145 return inetAddress;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700146 }
147
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700148 /**
Irfan Sheriff96ca9172010-10-05 16:12:25 -0700149 * Convert a IPv4 address from an InetAddress to an integer
150 * @param inetAddr is an InetAddress corresponding to the IPv4 address
151 * @return the IP address as an integer in network byte order
152 */
153 public static int inetAddressToInt(InetAddress inetAddr)
154 throws IllegalArgumentException {
155 byte [] addr = inetAddr.getAddress();
156 if (addr.length != 4) {
157 throw new IllegalArgumentException("Not an IPv4 address");
158 }
159 return ((addr[3] & 0xff) << 24) | ((addr[2] & 0xff) << 16) |
160 ((addr[1] & 0xff) << 8) | (addr[0] & 0xff);
161 }
162
163 /**
164 * Convert a network prefix length to an IPv4 netmask integer
165 * @param prefixLength
166 * @return the IPv4 netmask as an integer in network byte order
167 */
168 public static int prefixLengthToNetmaskInt(int prefixLength)
169 throws IllegalArgumentException {
170 if (prefixLength < 0 || prefixLength > 32) {
171 throw new IllegalArgumentException("Invalid prefix length (0 <= prefix <= 32)");
172 }
173 int value = 0xffffffff << (32 - prefixLength);
174 return Integer.reverseBytes(value);
175 }
176
177 public static boolean isIpAddress(String address) {
178 //TODO: Add NetworkUtils support for IPv6 configuration and
179 //remove IPv4 validation and use a generic InetAddress validation
180 try {
181 String[] parts = address.split("\\.");
182 if (parts.length != 4) {
183 return false;
184 }
185 int a = Integer.parseInt(parts[0]);
186 if (a < 0 || a > 255) return false;
187 a = Integer.parseInt(parts[1]);
188 if (a < 0 || a > 255) return false;
189 a = Integer.parseInt(parts[2]);
190 if (a < 0 || a > 255) return false;
191 a = Integer.parseInt(parts[3]);
192 if (a < 0 || a > 255) return false;
193 } catch (NumberFormatException ex) {
194 return false;
195 }
196 return true;
197 }
198
199 /**
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700200 * Add a default route through the specified gateway.
201 * @param interfaceName interface on which the route should be added
202 * @param gw the IP address of the gateway to which the route is desired,
203 * @return {@code true} on success, {@code false} on failure
204 */
205 public static boolean addDefaultRoute(String interfaceName, InetAddress gw) {
206 String dstStr;
207 String gwStr = gw.getHostAddress();
208
209 if (gw instanceof Inet4Address) {
210 dstStr = "0.0.0.0";
211 } else if (gw instanceof Inet6Address) {
212 dstStr = "::";
213 } else {
214 Log.w(TAG, "addDefaultRoute failure: address is neither IPv4 nor IPv6" +
215 "(" + gwStr + ")");
216 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 }
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700218 return addRoute(interfaceName, dstStr, 0, gwStr) == 0;
219 }
220
221 /**
222 * Add a host route.
223 * @param interfaceName interface on which the route should be added
224 * @param dst the IP address of the host to which the route is desired,
225 * this should not be null.
226 * @param gw the IP address of the gateway to which the route is desired,
227 * if null, indicates a directly-connected route.
228 * @return {@code true} on success, {@code false} on failure
229 */
230 public static boolean addHostRoute(String interfaceName, InetAddress dst,
231 InetAddress gw) {
232 if (dst == null) {
233 Log.w(TAG, "addHostRoute: dst should not be null");
234 return false;
235 }
236
237 int prefixLength;
238 String dstStr = dst.getHostAddress();
239 String gwStr = (gw != null) ? gw.getHostAddress() : null;
240
241 if (dst instanceof Inet4Address) {
242 prefixLength = 32;
243 } else if (dst instanceof Inet6Address) {
244 prefixLength = 128;
245 } else {
246 Log.w(TAG, "addHostRoute failure: address is neither IPv4 nor IPv6" +
247 "(" + dst + ")");
248 return false;
249 }
250 return addRoute(interfaceName, dstStr, prefixLength, gwStr) == 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 }
252}