PageRenderTime 587ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/core/java/android/net/NetworkUtils.java

https://gitlab.com/amardeep434/nitro_base
Java | 376 lines | 180 code | 42 blank | 154 comment | 31 complexity | 29fbcd8a13f91da6fc8fdd8c2ea02e64 MD5 | raw file
  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. package android.net;
  17. import java.io.FileDescriptor;
  18. import java.net.InetAddress;
  19. import java.net.Inet4Address;
  20. import java.net.Inet6Address;
  21. import java.net.SocketException;
  22. import java.net.UnknownHostException;
  23. import java.util.Collection;
  24. import java.util.Locale;
  25. import android.os.Parcel;
  26. import android.util.Log;
  27. import android.util.Pair;
  28. /**
  29. * Native methods for managing network interfaces.
  30. *
  31. * {@hide}
  32. */
  33. public class NetworkUtils {
  34. private static final String TAG = "NetworkUtils";
  35. /**
  36. * Attaches a socket filter that accepts DHCP packets to the given socket.
  37. */
  38. public native static void attachDhcpFilter(FileDescriptor fd) throws SocketException;
  39. /**
  40. * Attaches a socket filter that accepts ICMPv6 router advertisements to the given socket.
  41. * @param fd the socket's {@link FileDescriptor}.
  42. * @param packetType the hardware address type, one of ARPHRD_*.
  43. */
  44. public native static void attachRaFilter(FileDescriptor fd, int packetType) throws SocketException;
  45. /**
  46. * Configures a socket for receiving ICMPv6 router solicitations and sending advertisements.
  47. * @param fd the socket's {@link FileDescriptor}.
  48. * @param ifIndex the interface index.
  49. */
  50. public native static void setupRaSocket(FileDescriptor fd, int ifIndex) throws SocketException;
  51. /**
  52. * Binds the current process to the network designated by {@code netId}. All sockets created
  53. * in the future (and not explicitly bound via a bound {@link SocketFactory} (see
  54. * {@link Network#getSocketFactory}) will be bound to this network. Note that if this
  55. * {@code Network} ever disconnects all sockets created in this way will cease to work. This
  56. * is by design so an application doesn't accidentally use sockets it thinks are still bound to
  57. * a particular {@code Network}. Passing NETID_UNSET clears the binding.
  58. */
  59. public native static boolean bindProcessToNetwork(int netId);
  60. /**
  61. * Return the netId last passed to {@link #bindProcessToNetwork}, or NETID_UNSET if
  62. * {@link #unbindProcessToNetwork} has been called since {@link #bindProcessToNetwork}.
  63. */
  64. public native static int getBoundNetworkForProcess();
  65. /**
  66. * Binds host resolutions performed by this process to the network designated by {@code netId}.
  67. * {@link #bindProcessToNetwork} takes precedence over this setting. Passing NETID_UNSET clears
  68. * the binding.
  69. *
  70. * @deprecated This is strictly for legacy usage to support startUsingNetworkFeature().
  71. */
  72. public native static boolean bindProcessToNetworkForHostResolution(int netId);
  73. /**
  74. * Explicitly binds {@code socketfd} to the network designated by {@code netId}. This
  75. * overrides any binding via {@link #bindProcessToNetwork}.
  76. * @return 0 on success or negative errno on failure.
  77. */
  78. public native static int bindSocketToNetwork(int socketfd, int netId);
  79. /**
  80. * Protect {@code fd} from VPN connections. After protecting, data sent through
  81. * this socket will go directly to the underlying network, so its traffic will not be
  82. * forwarded through the VPN.
  83. */
  84. public static boolean protectFromVpn(FileDescriptor fd) {
  85. return protectFromVpn(fd.getInt$());
  86. }
  87. /**
  88. * Protect {@code socketfd} from VPN connections. After protecting, data sent through
  89. * this socket will go directly to the underlying network, so its traffic will not be
  90. * forwarded through the VPN.
  91. */
  92. public native static boolean protectFromVpn(int socketfd);
  93. /**
  94. * Determine if {@code uid} can access network designated by {@code netId}.
  95. * @return {@code true} if {@code uid} can access network, {@code false} otherwise.
  96. */
  97. public native static boolean queryUserAccess(int uid, int netId);
  98. /**
  99. * Convert a IPv4 address from an integer to an InetAddress.
  100. * @param hostAddress an int corresponding to the IPv4 address in network byte order
  101. */
  102. public static InetAddress intToInetAddress(int hostAddress) {
  103. byte[] addressBytes = { (byte)(0xff & hostAddress),
  104. (byte)(0xff & (hostAddress >> 8)),
  105. (byte)(0xff & (hostAddress >> 16)),
  106. (byte)(0xff & (hostAddress >> 24)) };
  107. try {
  108. return InetAddress.getByAddress(addressBytes);
  109. } catch (UnknownHostException e) {
  110. throw new AssertionError();
  111. }
  112. }
  113. /**
  114. * Convert a IPv4 address from an InetAddress to an integer
  115. * @param inetAddr is an InetAddress corresponding to the IPv4 address
  116. * @return the IP address as an integer in network byte order
  117. */
  118. public static int inetAddressToInt(Inet4Address inetAddr)
  119. throws IllegalArgumentException {
  120. byte [] addr = inetAddr.getAddress();
  121. return ((addr[3] & 0xff) << 24) | ((addr[2] & 0xff) << 16) |
  122. ((addr[1] & 0xff) << 8) | (addr[0] & 0xff);
  123. }
  124. /**
  125. * Convert a network prefix length to an IPv4 netmask integer
  126. * @param prefixLength
  127. * @return the IPv4 netmask as an integer in network byte order
  128. */
  129. public static int prefixLengthToNetmaskInt(int prefixLength)
  130. throws IllegalArgumentException {
  131. if (prefixLength < 0 || prefixLength > 32) {
  132. throw new IllegalArgumentException("Invalid prefix length (0 <= prefix <= 32)");
  133. }
  134. int value = 0xffffffff << (32 - prefixLength);
  135. return Integer.reverseBytes(value);
  136. }
  137. /**
  138. * Convert a IPv4 netmask integer to a prefix length
  139. * @param netmask as an integer in network byte order
  140. * @return the network prefix length
  141. */
  142. public static int netmaskIntToPrefixLength(int netmask) {
  143. return Integer.bitCount(netmask);
  144. }
  145. /**
  146. * Convert an IPv4 netmask to a prefix length, checking that the netmask is contiguous.
  147. * @param netmask as a {@code Inet4Address}.
  148. * @return the network prefix length
  149. * @throws IllegalArgumentException the specified netmask was not contiguous.
  150. * @hide
  151. */
  152. public static int netmaskToPrefixLength(Inet4Address netmask) {
  153. // inetAddressToInt returns an int in *network* byte order.
  154. int i = Integer.reverseBytes(inetAddressToInt(netmask));
  155. int prefixLength = Integer.bitCount(i);
  156. int trailingZeros = Integer.numberOfTrailingZeros(i);
  157. if (trailingZeros != 32 - prefixLength) {
  158. throw new IllegalArgumentException("Non-contiguous netmask: " + Integer.toHexString(i));
  159. }
  160. return prefixLength;
  161. }
  162. /**
  163. * Create an InetAddress from a string where the string must be a standard
  164. * representation of a V4 or V6 address. Avoids doing a DNS lookup on failure
  165. * but it will throw an IllegalArgumentException in that case.
  166. * @param addrString
  167. * @return the InetAddress
  168. * @hide
  169. */
  170. public static InetAddress numericToInetAddress(String addrString)
  171. throws IllegalArgumentException {
  172. return InetAddress.parseNumericAddress(addrString);
  173. }
  174. /**
  175. * Writes an InetAddress to a parcel. The address may be null. This is likely faster than
  176. * calling writeSerializable.
  177. */
  178. protected static void parcelInetAddress(Parcel parcel, InetAddress address, int flags) {
  179. byte[] addressArray = (address != null) ? address.getAddress() : null;
  180. parcel.writeByteArray(addressArray);
  181. }
  182. /**
  183. * Reads an InetAddress from a parcel. Returns null if the address that was written was null
  184. * or if the data is invalid.
  185. */
  186. protected static InetAddress unparcelInetAddress(Parcel in) {
  187. byte[] addressArray = in.createByteArray();
  188. if (addressArray == null) {
  189. return null;
  190. }
  191. try {
  192. return InetAddress.getByAddress(addressArray);
  193. } catch (UnknownHostException e) {
  194. return null;
  195. }
  196. }
  197. /**
  198. * Masks a raw IP address byte array with the specified prefix length.
  199. */
  200. public static void maskRawAddress(byte[] array, int prefixLength) {
  201. if (prefixLength < 0 || prefixLength > array.length * 8) {
  202. throw new RuntimeException("IP address with " + array.length +
  203. " bytes has invalid prefix length " + prefixLength);
  204. }
  205. int offset = prefixLength / 8;
  206. int remainder = prefixLength % 8;
  207. byte mask = (byte)(0xFF << (8 - remainder));
  208. if (offset < array.length) array[offset] = (byte)(array[offset] & mask);
  209. offset++;
  210. for (; offset < array.length; offset++) {
  211. array[offset] = 0;
  212. }
  213. }
  214. /**
  215. * Get InetAddress masked with prefixLength. Will never return null.
  216. * @param address the IP address to mask with
  217. * @param prefixLength the prefixLength used to mask the IP
  218. */
  219. public static InetAddress getNetworkPart(InetAddress address, int prefixLength) {
  220. byte[] array = address.getAddress();
  221. maskRawAddress(array, prefixLength);
  222. InetAddress netPart = null;
  223. try {
  224. netPart = InetAddress.getByAddress(array);
  225. } catch (UnknownHostException e) {
  226. throw new RuntimeException("getNetworkPart error - " + e.toString());
  227. }
  228. return netPart;
  229. }
  230. /**
  231. * Returns the implicit netmask of an IPv4 address, as was the custom before 1993.
  232. */
  233. public static int getImplicitNetmask(Inet4Address address) {
  234. int firstByte = address.getAddress()[0] & 0xff; // Convert to an unsigned value.
  235. if (firstByte < 128) {
  236. return 8;
  237. } else if (firstByte < 192) {
  238. return 16;
  239. } else if (firstByte < 224) {
  240. return 24;
  241. } else {
  242. return 32; // Will likely not end well for other reasons.
  243. }
  244. }
  245. /**
  246. * Utility method to parse strings such as "192.0.2.5/24" or "2001:db8::cafe:d00d/64".
  247. * @hide
  248. */
  249. public static Pair<InetAddress, Integer> parseIpAndMask(String ipAndMaskString) {
  250. InetAddress address = null;
  251. int prefixLength = -1;
  252. try {
  253. String[] pieces = ipAndMaskString.split("/", 2);
  254. prefixLength = Integer.parseInt(pieces[1]);
  255. address = InetAddress.parseNumericAddress(pieces[0]);
  256. } catch (NullPointerException e) { // Null string.
  257. } catch (ArrayIndexOutOfBoundsException e) { // No prefix length.
  258. } catch (NumberFormatException e) { // Non-numeric prefix.
  259. } catch (IllegalArgumentException e) { // Invalid IP address.
  260. }
  261. if (address == null || prefixLength == -1) {
  262. throw new IllegalArgumentException("Invalid IP address and mask " + ipAndMaskString);
  263. }
  264. return new Pair<InetAddress, Integer>(address, prefixLength);
  265. }
  266. /**
  267. * Check if IP address type is consistent between two InetAddress.
  268. * @return true if both are the same type. False otherwise.
  269. */
  270. public static boolean addressTypeMatches(InetAddress left, InetAddress right) {
  271. return (((left instanceof Inet4Address) && (right instanceof Inet4Address)) ||
  272. ((left instanceof Inet6Address) && (right instanceof Inet6Address)));
  273. }
  274. /**
  275. * Convert a 32 char hex string into a Inet6Address.
  276. * throws a runtime exception if the string isn't 32 chars, isn't hex or can't be
  277. * made into an Inet6Address
  278. * @param addrHexString a 32 character hex string representing an IPv6 addr
  279. * @return addr an InetAddress representation for the string
  280. */
  281. public static InetAddress hexToInet6Address(String addrHexString)
  282. throws IllegalArgumentException {
  283. try {
  284. return numericToInetAddress(String.format(Locale.US, "%s:%s:%s:%s:%s:%s:%s:%s",
  285. addrHexString.substring(0,4), addrHexString.substring(4,8),
  286. addrHexString.substring(8,12), addrHexString.substring(12,16),
  287. addrHexString.substring(16,20), addrHexString.substring(20,24),
  288. addrHexString.substring(24,28), addrHexString.substring(28,32)));
  289. } catch (Exception e) {
  290. Log.e("NetworkUtils", "error in hexToInet6Address(" + addrHexString + "): " + e);
  291. throw new IllegalArgumentException(e);
  292. }
  293. }
  294. /**
  295. * Create a string array of host addresses from a collection of InetAddresses
  296. * @param addrs a Collection of InetAddresses
  297. * @return an array of Strings containing their host addresses
  298. */
  299. public static String[] makeStrings(Collection<InetAddress> addrs) {
  300. String[] result = new String[addrs.size()];
  301. int i = 0;
  302. for (InetAddress addr : addrs) {
  303. result[i++] = addr.getHostAddress();
  304. }
  305. return result;
  306. }
  307. /**
  308. * Trim leading zeros from IPv4 address strings
  309. * Our base libraries will interpret that as octel..
  310. * Must leave non v4 addresses and host names alone.
  311. * For example, 192.168.000.010 -> 192.168.0.10
  312. * TODO - fix base libraries and remove this function
  313. * @param addr a string representing an ip addr
  314. * @return a string propertly trimmed
  315. */
  316. public static String trimV4AddrZeros(String addr) {
  317. if (addr == null) return null;
  318. String[] octets = addr.split("\\.");
  319. if (octets.length != 4) return addr;
  320. StringBuilder builder = new StringBuilder(16);
  321. String result = null;
  322. for (int i = 0; i < 4; i++) {
  323. try {
  324. if (octets[i].length() > 3) return addr;
  325. builder.append(Integer.parseInt(octets[i]));
  326. } catch (NumberFormatException e) {
  327. return addr;
  328. }
  329. if (i < 3) builder.append('.');
  330. }
  331. result = builder.toString();
  332. return result;
  333. }
  334. }