blob: 564bc1f357df4c8a40687ba09a42a001e7953baa [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;
20import java.net.UnknownHostException;
21
22/**
23 * Native methods for managing network interfaces.
24 *
25 * {@hide}
26 */
27public class NetworkUtils {
Mike Lockwood0900f362009-07-10 17:24:07 -040028 /** Bring the named network interface up. */
29 public native static int enableInterface(String interfaceName);
30
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031 /** Bring the named network interface down. */
32 public native static int disableInterface(String interfaceName);
33
34 /** Add a route to the specified host via the named interface. */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070035 public static int addHostRoute(String interfaceName, InetAddress hostaddr) {
36 int v4Int = v4StringToInt(hostaddr.getHostAddress());
37 if (v4Int != 0) {
38 return addHostRouteNative(interfaceName, v4Int);
39 } else {
40 return -1;
41 }
42 }
43 private native static int addHostRouteNative(String interfaceName, int hostaddr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
45 /** Add a default route for the named interface. */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070046 public static int setDefaultRoute(String interfaceName, InetAddress gwayAddr) {
47 int v4Int = v4StringToInt(gwayAddr.getHostAddress());
48 if (v4Int != 0) {
49 return setDefaultRouteNative(interfaceName, v4Int);
50 } else {
51 return -1;
52 }
53 }
54 private native static int setDefaultRouteNative(String interfaceName, int hostaddr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
56 /** Return the gateway address for the default route for the named interface. */
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070057 public static InetAddress getDefaultRoute(String interfaceName) {
58 int addr = getDefaultRouteNative(interfaceName);
59 try {
60 return InetAddress.getByAddress(v4IntToArray(addr));
61 } catch (UnknownHostException e) {
62 return null;
63 }
64 }
65 private native static int getDefaultRouteNative(String interfaceName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
67 /** Remove host routes that uses the named interface. */
68 public native static int removeHostRoutes(String interfaceName);
69
70 /** Remove the default route for the named interface. */
71 public native static int removeDefaultRoute(String interfaceName);
72
73 /** Reset any sockets that are connected via the named interface. */
74 public native static int resetConnections(String interfaceName);
75
76 /**
77 * Start the DHCP client daemon, in order to have it request addresses
78 * for the named interface, and then configure the interface with those
79 * addresses. This call blocks until it obtains a result (either success
80 * or failure) from the daemon.
81 * @param interfaceName the name of the interface to configure
82 * @param ipInfo if the request succeeds, this object is filled in with
83 * the IP address information.
84 * @return {@code true} for success, {@code false} for failure
85 */
86 public native static boolean runDhcp(String interfaceName, DhcpInfo ipInfo);
87
88 /**
89 * Shut down the DHCP client daemon.
90 * @param interfaceName the name of the interface for which the daemon
91 * should be stopped
92 * @return {@code true} for success, {@code false} for failure
93 */
94 public native static boolean stopDhcp(String interfaceName);
95
96 /**
97 * Release the current DHCP lease.
98 * @param interfaceName the name of the interface for which the lease should
99 * be released
100 * @return {@code true} for success, {@code false} for failure
101 */
102 public native static boolean releaseDhcpLease(String interfaceName);
103
104 /**
105 * Return the last DHCP-related error message that was recorded.
106 * <p/>NOTE: This string is not localized, but currently it is only
107 * used in logging.
108 * @return the most recent error message, if any
109 */
110 public native static String getDhcpError();
111
112 /**
113 * When static IP configuration has been specified, configure the network
114 * interface according to the values supplied.
115 * @param interfaceName the name of the interface to configure
116 * @param ipInfo the IP address, default gateway, and DNS server addresses
117 * with which to configure the interface.
118 * @return {@code true} for success, {@code false} for failure
119 */
120 public static boolean configureInterface(String interfaceName, DhcpInfo ipInfo) {
121 return configureNative(interfaceName,
122 ipInfo.ipAddress,
123 ipInfo.netmask,
124 ipInfo.gateway,
125 ipInfo.dns1,
126 ipInfo.dns2);
127 }
128
129 private native static boolean configureNative(
130 String interfaceName, int ipAddress, int netmask, int gateway, int dns1, int dns2);
131
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700132 // The following two functions are glue to tie the old int-based address scheme
133 // to the new InetAddress scheme. They should go away when we go fully to InetAddress
134 // TODO - remove when we switch fully to InetAddress
135 public static byte[] v4IntToArray(int addr) {
136 byte[] addrBytes = new byte[4];
137 addrBytes[0] = (byte)(addr & 0xff);
138 addrBytes[1] = (byte)((addr >> 8) & 0xff);
139 addrBytes[2] = (byte)((addr >> 16) & 0xff);
140 addrBytes[3] = (byte)((addr >> 24) & 0xff);
141 return addrBytes;
142 }
143
144 public static int v4StringToInt(String str) {
145 int result = 0;
146 String[] array = str.split("\\.");
147 if (array.length != 4) return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 try {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700149 result = Integer.parseInt(array[3]);
150 result = (result << 8) + Integer.parseInt(array[2]);
151 result = (result << 8) + Integer.parseInt(array[1]);
152 result = (result << 8) + Integer.parseInt(array[0]);
153 } catch (NumberFormatException e) {
154 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700156 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 }
158}