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