NetworkUtils.java revision 9bc709d46e1165ca0c9a02bd970767c401b990e5
1/*
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.Inet4Address;
21import java.net.Inet6Address;
22import java.net.UnknownHostException;
23
24import android.util.Log;
25
26/**
27 * Native methods for managing network interfaces.
28 *
29 * {@hide}
30 */
31public class NetworkUtils {
32
33    private static final String TAG = "NetworkUtils";
34
35    /** Bring the named network interface up. */
36    public native static int enableInterface(String interfaceName);
37
38    /** Bring the named network interface down. */
39    public native static int disableInterface(String interfaceName);
40
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);
53
54    /** Return the gateway address for the default route for the named interface. */
55    public native static int getDefaultRoute(String interfaceName);
56
57    /** Remove host routes that uses the named interface. */
58    public native static int removeHostRoutes(String interfaceName);
59
60    /** Remove the default route for the named interface. */
61    public native static int removeDefaultRoute(String interfaceName);
62
63    /** Reset any sockets that are connected via the named interface. */
64    public native static int resetConnections(String interfaceName);
65
66    /**
67     * Start the DHCP client daemon, in order to have it request addresses
68     * for the named interface, and then configure the interface with those
69     * addresses. This call blocks until it obtains a result (either success
70     * or failure) from the daemon.
71     * @param interfaceName the name of the interface to configure
72     * @param ipInfo if the request succeeds, this object is filled in with
73     * the IP address information.
74     * @return {@code true} for success, {@code false} for failure
75     */
76    public native static boolean runDhcp(String interfaceName, DhcpInfo ipInfo);
77
78    /**
79     * Shut down the DHCP client daemon.
80     * @param interfaceName the name of the interface for which the daemon
81     * should be stopped
82     * @return {@code true} for success, {@code false} for failure
83     */
84    public native static boolean stopDhcp(String interfaceName);
85
86    /**
87     * Release the current DHCP lease.
88     * @param interfaceName the name of the interface for which the lease should
89     * be released
90     * @return {@code true} for success, {@code false} for failure
91     */
92    public native static boolean releaseDhcpLease(String interfaceName);
93
94    /**
95     * Return the last DHCP-related error message that was recorded.
96     * <p/>NOTE: This string is not localized, but currently it is only
97     * used in logging.
98     * @return the most recent error message, if any
99     */
100    public native static String getDhcpError();
101
102    /**
103     * When static IP configuration has been specified, configure the network
104     * interface according to the values supplied.
105     * @param interfaceName the name of the interface to configure
106     * @param ipInfo the IP address, default gateway, and DNS server addresses
107     * with which to configure the interface.
108     * @return {@code true} for success, {@code false} for failure
109     */
110    public static boolean configureInterface(String interfaceName, DhcpInfo ipInfo) {
111        return configureNative(interfaceName,
112            ipInfo.ipAddress,
113            ipInfo.netmask,
114            ipInfo.gateway,
115            ipInfo.dns1,
116            ipInfo.dns2);
117    }
118
119    private native static boolean configureNative(
120        String interfaceName, int ipAddress, int netmask, int gateway, int dns1, int dns2);
121
122    /**
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.
127     */
128    public static InetAddress intToInetAddress(int hostAddress) {
129        InetAddress inetAddress;
130        byte[] addressBytes = { (byte)(0xff & hostAddress),
131                                (byte)(0xff & (hostAddress >> 8)),
132                                (byte)(0xff & (hostAddress >> 16)),
133                                (byte)(0xff & (hostAddress >> 24)) };
134
135        try {
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;
194        }
195        return addRoute(interfaceName, dstStr, prefixLength, gwStr) == 0;
196    }
197}
198