NetworkUtils.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
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.UnknownHostException;
21
22/**
23 * Native methods for managing network interfaces.
24 *
25 * {@hide}
26 */
27public class NetworkUtils {
28    /** Bring the named network interface down. */
29    public native static int disableInterface(String interfaceName);
30
31    /** Add a route to the specified host via the named interface. */
32    public native static int addHostRoute(String interfaceName, int hostaddr);
33
34    /** Add a default route for the named interface. */
35    public native static int setDefaultRoute(String interfaceName, int gwayAddr);
36
37    /** Return the gateway address for the default route for the named interface. */
38    public native static int getDefaultRoute(String interfaceName);
39
40    /** Remove host routes that uses the named interface. */
41    public native static int removeHostRoutes(String interfaceName);
42
43    /** Remove the default route for the named interface. */
44    public native static int removeDefaultRoute(String interfaceName);
45
46    /** Reset any sockets that are connected via the named interface. */
47    public native static int resetConnections(String interfaceName);
48
49    /**
50     * Start the DHCP client daemon, in order to have it request addresses
51     * for the named interface, and then configure the interface with those
52     * addresses. This call blocks until it obtains a result (either success
53     * or failure) from the daemon.
54     * @param interfaceName the name of the interface to configure
55     * @param ipInfo if the request succeeds, this object is filled in with
56     * the IP address information.
57     * @return {@code true} for success, {@code false} for failure
58     */
59    public native static boolean runDhcp(String interfaceName, DhcpInfo ipInfo);
60
61    /**
62     * Shut down the DHCP client daemon.
63     * @param interfaceName the name of the interface for which the daemon
64     * should be stopped
65     * @return {@code true} for success, {@code false} for failure
66     */
67    public native static boolean stopDhcp(String interfaceName);
68
69    /**
70     * Release the current DHCP lease.
71     * @param interfaceName the name of the interface for which the lease should
72     * be released
73     * @return {@code true} for success, {@code false} for failure
74     */
75    public native static boolean releaseDhcpLease(String interfaceName);
76
77    /**
78     * Return the last DHCP-related error message that was recorded.
79     * <p/>NOTE: This string is not localized, but currently it is only
80     * used in logging.
81     * @return the most recent error message, if any
82     */
83    public native static String getDhcpError();
84
85    /**
86     * When static IP configuration has been specified, configure the network
87     * interface according to the values supplied.
88     * @param interfaceName the name of the interface to configure
89     * @param ipInfo the IP address, default gateway, and DNS server addresses
90     * with which to configure the interface.
91     * @return {@code true} for success, {@code false} for failure
92     */
93    public static boolean configureInterface(String interfaceName, DhcpInfo ipInfo) {
94        return configureNative(interfaceName,
95            ipInfo.ipAddress,
96            ipInfo.netmask,
97            ipInfo.gateway,
98            ipInfo.dns1,
99            ipInfo.dns2);
100    }
101
102    private native static boolean configureNative(
103        String interfaceName, int ipAddress, int netmask, int gateway, int dns1, int dns2);
104
105    /**
106     * Look up a host name and return the result as an int. Works if the argument
107     * is an IP address in dot notation. Obviously, this can only be used for IPv4
108     * addresses.
109     * @param hostname the name of the host (or the IP address)
110     * @return the IP address as an {@code int} in network byte order
111     */
112    public static int lookupHost(String hostname) {
113        InetAddress inetAddress;
114        try {
115            inetAddress = InetAddress.getByName(hostname);
116        } catch (UnknownHostException e) {
117            return -1;
118        }
119        byte[] addrBytes;
120        int addr;
121        addrBytes = inetAddress.getAddress();
122        addr = ((addrBytes[3] & 0xff) << 24)
123                | ((addrBytes[2] & 0xff) << 16)
124                | ((addrBytes[1] & 0xff) << 8)
125                |  (addrBytes[0] & 0xff);
126        return addr;
127    }
128}
129