/frameworks/base/services/java/com/android/server/wm/remotecontrol/HostInterface.java

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk · Java · 294 lines · 230 code · 32 blank · 32 comment · 53 complexity · 9d0f7a03331cf68c8aebf6947fea7f8d MD5 · raw file

  1. package com.android.server.wm.remotecontrol;
  2. import java.net.Inet4Address;
  3. import java.net.Inet6Address;
  4. import java.net.InetAddress;
  5. import java.net.InterfaceAddress;
  6. import java.net.NetworkInterface;
  7. import java.net.SocketException;
  8. import java.util.ArrayList;
  9. import java.util.Enumeration;
  10. import java.util.Iterator;
  11. import java.util.List;
  12. import java.util.Vector;
  13. @SuppressWarnings("rawtypes")
  14. public class HostInterface
  15. {
  16. ////////////////////////////////////////////////
  17. // Constants
  18. ////////////////////////////////////////////////
  19. public static boolean USE_LOOPBACK_ADDR = false;
  20. public static boolean USE_ONLY_IPV4_ADDR = true;
  21. public static boolean USE_ONLY_IPV6_ADDR = false;
  22. ////////////////////////////////////////////////
  23. // Network Interfaces
  24. ////////////////////////////////////////////////
  25. private static String ifAddress = "";
  26. public final static int IPV4_BITMASK = 0x0001;
  27. public final static int IPV6_BITMASK = 0x0010;
  28. public final static int LOCAL_BITMASK = 0x0100;
  29. public final static void setInterface(String ifaddr)
  30. {
  31. ifAddress = ifaddr;
  32. }
  33. public final static String getInterface()
  34. {
  35. return ifAddress;
  36. }
  37. private final static boolean hasAssignedInterface()
  38. {
  39. return (0 < ifAddress.length()) ? true : false;
  40. }
  41. ////////////////////////////////////////////////
  42. // Network Interfaces
  43. ////////////////////////////////////////////////
  44. // Thanks for Theo Beisch (10/27/04)
  45. private final static boolean isUsableAddress(InetAddress addr)
  46. {
  47. if (USE_LOOPBACK_ADDR == false) {
  48. if (addr.isLoopbackAddress() == true)
  49. return false;
  50. }
  51. if (USE_ONLY_IPV4_ADDR == true) {
  52. if (addr instanceof Inet6Address)
  53. return false;
  54. }
  55. if (USE_ONLY_IPV6_ADDR == true) {
  56. if (addr instanceof Inet4Address)
  57. return false;
  58. }
  59. return true;
  60. }
  61. public final static int getNHostAddresses()
  62. {
  63. if (hasAssignedInterface() == true)
  64. return 1;
  65. int nHostAddrs = 0;
  66. try {
  67. Enumeration nis = NetworkInterface.getNetworkInterfaces();
  68. while (nis.hasMoreElements()){
  69. NetworkInterface ni = (NetworkInterface)nis.nextElement();
  70. Enumeration addrs = ni.getInetAddresses();
  71. while (addrs.hasMoreElements()) {
  72. InetAddress addr = (InetAddress)addrs.nextElement();
  73. if (isUsableAddress(addr) == false)
  74. continue;
  75. nHostAddrs++;
  76. }
  77. }
  78. }
  79. catch(Exception e){
  80. }
  81. return nHostAddrs;
  82. }
  83. /**
  84. *
  85. * @param ipfilter
  86. * @param interfaces
  87. * @return
  88. * @since 1.8.0
  89. * @author Stefano "Kismet" Lenzi &lt;kismet.sl@gmail.com&gt;
  90. */
  91. @SuppressWarnings("unchecked")
  92. public final static InetAddress[] getInetAddress(int ipfilter,String[] interfaces){
  93. Enumeration nis;
  94. if(interfaces!=null){
  95. Vector iflist = new Vector();
  96. for (int i = 0; i < interfaces.length; i++) {
  97. NetworkInterface ni;
  98. try {
  99. ni = NetworkInterface.getByName(interfaces[i]);
  100. } catch (SocketException e) {
  101. continue;
  102. }
  103. if(ni != null) iflist.add(ni);
  104. }
  105. nis = iflist.elements();
  106. }else{
  107. try {
  108. nis = NetworkInterface.getNetworkInterfaces();
  109. } catch (SocketException e) {
  110. return null;
  111. }
  112. }
  113. ArrayList addresses = new ArrayList();
  114. while (nis.hasMoreElements()){
  115. NetworkInterface ni = (NetworkInterface)nis.nextElement();
  116. Enumeration addrs = ni.getInetAddresses();
  117. while (addrs.hasMoreElements()) {
  118. InetAddress addr = (InetAddress)addrs.nextElement();
  119. if(((ipfilter & LOCAL_BITMASK)==0) && addr.isLoopbackAddress())
  120. continue;
  121. if (((ipfilter & IPV4_BITMASK)!=0) && addr instanceof Inet4Address ) {
  122. addresses.add(addr);
  123. }else if (((ipfilter & IPV6_BITMASK)!=0)&& addr instanceof InetAddress) {
  124. addresses.add(addr);
  125. }
  126. }
  127. }
  128. return (InetAddress[]) addresses.toArray(new InetAddress[]{});
  129. }
  130. public final static String getHostAddress(int n)
  131. {
  132. if (hasAssignedInterface() == true)
  133. return getInterface();
  134. int hostAddrCnt = 0;
  135. try {
  136. Enumeration nis = NetworkInterface.getNetworkInterfaces();
  137. while (nis.hasMoreElements()){
  138. NetworkInterface ni = (NetworkInterface)nis.nextElement();
  139. Enumeration addrs = ni.getInetAddresses();
  140. while (addrs.hasMoreElements()) {
  141. InetAddress addr = (InetAddress)addrs.nextElement();
  142. if (isUsableAddress(addr) == false)
  143. continue;
  144. if (hostAddrCnt < n) {
  145. hostAddrCnt++;
  146. continue;
  147. }
  148. String host = addr.getHostAddress();
  149. //if (addr instanceof Inet6Address)
  150. // host = "[" + host + "]";
  151. return host;
  152. }
  153. }
  154. }
  155. catch(Exception e){};
  156. return "";
  157. }
  158. public final static InterfaceAddress getInterfaceAddressByAddress(String address)
  159. {
  160. try {
  161. Enumeration nis = NetworkInterface.getNetworkInterfaces();
  162. while (nis.hasMoreElements()){
  163. NetworkInterface ni = (NetworkInterface)nis.nextElement();
  164. List<InterfaceAddress> interfaceAddrs = ni.getInterfaceAddresses();
  165. Iterator<InterfaceAddress> it = interfaceAddrs.iterator();
  166. while (it.hasNext()) {
  167. InterfaceAddress addr = it.next();
  168. if(addr.getAddress().getHostAddress().equals(address)){
  169. return addr;
  170. }
  171. }
  172. }
  173. }
  174. catch(Exception e){};
  175. return null;
  176. }
  177. ////////////////////////////////////////////////
  178. // isIPv?Address
  179. ////////////////////////////////////////////////
  180. public final static boolean isIPv6Address(String host)
  181. {
  182. try {
  183. InetAddress addr = InetAddress.getByName(host);
  184. if (addr instanceof Inet6Address)
  185. return true;
  186. return false;
  187. }
  188. catch (Exception e) {}
  189. return false;
  190. }
  191. public final static boolean isIPv4Address(String host)
  192. {
  193. try {
  194. InetAddress addr = InetAddress.getByName(host);
  195. if (addr instanceof Inet4Address)
  196. return true;
  197. return false;
  198. }
  199. catch (Exception e) {}
  200. return false;
  201. }
  202. ////////////////////////////////////////////////
  203. // hasIPv?Interfaces
  204. ////////////////////////////////////////////////
  205. public final static boolean hasIPv4Addresses()
  206. {
  207. int addrCnt = getNHostAddresses();
  208. for (int n=0; n<addrCnt; n++) {
  209. String addr = getHostAddress(n);
  210. if (isIPv4Address(addr) == true)
  211. return true;
  212. }
  213. return false;
  214. }
  215. public final static boolean hasIPv6Addresses()
  216. {
  217. int addrCnt = getNHostAddresses();
  218. for (int n=0; n<addrCnt; n++) {
  219. String addr = getHostAddress(n);
  220. if (isIPv6Address(addr) == true)
  221. return true;
  222. }
  223. return false;
  224. }
  225. ////////////////////////////////////////////////
  226. // hasIPv?Interfaces
  227. ////////////////////////////////////////////////
  228. public final static String getIPv4Address()
  229. {
  230. int addrCnt = getNHostAddresses();
  231. for (int n=0; n<addrCnt; n++) {
  232. String addr = getHostAddress(n);
  233. if (isIPv4Address(addr) == true)
  234. return addr;
  235. }
  236. return "";
  237. }
  238. public final static String getIPv6Address()
  239. {
  240. int addrCnt = getNHostAddresses();
  241. for (int n=0; n<addrCnt; n++) {
  242. String addr = getHostAddress(n);
  243. if (isIPv6Address(addr) == true)
  244. return addr;
  245. }
  246. return "";
  247. }
  248. ////////////////////////////////////////////////
  249. // getHostURL
  250. ////////////////////////////////////////////////
  251. public final static String getHostURL(String host, int port, String uri)
  252. {
  253. String hostAddr = host;
  254. if (isIPv6Address(host) == true)
  255. hostAddr = "[" + host + "]";
  256. return
  257. "http://" +
  258. hostAddr +
  259. ":" + Integer.toString(port) +
  260. uri;
  261. }
  262. }