Remi NGUYEN VAN | 342dddd | 2021-03-18 23:27:19 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 android.util.Log; |
| 20 | |
| 21 | import java.lang.reflect.InvocationTargetException; |
| 22 | import java.net.InetAddress; |
| 23 | import java.net.UnknownHostException; |
| 24 | |
| 25 | /** |
| 26 | * Compatibility utility for InetAddress core platform APIs. |
| 27 | * |
| 28 | * Connectivity has access to such APIs, but they are not part of the module_current stubs yet |
| 29 | * (only core_current). Most stable core platform APIs are included manually in the connectivity |
| 30 | * build rules, but because InetAddress is also part of the base java SDK that is earlier on the |
| 31 | * classpath, the extra core platform APIs are not seen. |
| 32 | * |
| 33 | * TODO (b/183097033): remove this utility as soon as core_current is part of module_current |
| 34 | * @hide |
| 35 | */ |
| 36 | public class InetAddressCompat { |
| 37 | |
| 38 | /** |
| 39 | * @see InetAddress#clearDnsCache() |
| 40 | */ |
| 41 | public static void clearDnsCache() { |
| 42 | try { |
| 43 | InetAddress.class.getMethod("clearDnsCache").invoke(null); |
Remi NGUYEN VAN | 57707a5 | 2021-03-19 14:42:50 +0000 | [diff] [blame] | 44 | } catch (InvocationTargetException e) { |
| 45 | if (e.getCause() instanceof RuntimeException) { |
| 46 | throw (RuntimeException) e.getCause(); |
| 47 | } |
| 48 | throw new IllegalStateException("Unknown InvocationTargetException", e.getCause()); |
| 49 | } catch (IllegalAccessException | NoSuchMethodException e) { |
Remi NGUYEN VAN | 342dddd | 2021-03-18 23:27:19 +0900 | [diff] [blame] | 50 | Log.wtf(InetAddressCompat.class.getSimpleName(), "Error clearing DNS cache", e); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @see InetAddress#getAllByNameOnNet(String, int) |
| 56 | */ |
| 57 | public static InetAddress[] getAllByNameOnNet(String host, int netId) throws |
| 58 | UnknownHostException { |
Remi NGUYEN VAN | 57707a5 | 2021-03-19 14:42:50 +0000 | [diff] [blame] | 59 | return (InetAddress[]) callGetByNameMethod("getAllByNameOnNet", host, netId); |
Remi NGUYEN VAN | 342dddd | 2021-03-18 23:27:19 +0900 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @see InetAddress#getByNameOnNet(String, int) |
| 64 | */ |
| 65 | public static InetAddress getByNameOnNet(String host, int netId) throws |
| 66 | UnknownHostException { |
Remi NGUYEN VAN | 57707a5 | 2021-03-19 14:42:50 +0000 | [diff] [blame] | 67 | return (InetAddress) callGetByNameMethod("getByNameOnNet", host, netId); |
| 68 | } |
| 69 | |
| 70 | private static Object callGetByNameMethod(String method, String host, int netId) |
| 71 | throws UnknownHostException { |
Remi NGUYEN VAN | 342dddd | 2021-03-18 23:27:19 +0900 | [diff] [blame] | 72 | try { |
Remi NGUYEN VAN | 57707a5 | 2021-03-19 14:42:50 +0000 | [diff] [blame] | 73 | return InetAddress.class.getMethod(method, String.class, int.class) |
| 74 | .invoke(null, host, netId); |
| 75 | } catch (InvocationTargetException e) { |
| 76 | if (e.getCause() instanceof UnknownHostException) { |
| 77 | throw (UnknownHostException) e.getCause(); |
| 78 | } |
| 79 | if (e.getCause() instanceof RuntimeException) { |
| 80 | throw (RuntimeException) e.getCause(); |
| 81 | } |
| 82 | throw new IllegalStateException("Unknown InvocationTargetException", e.getCause()); |
| 83 | } catch (IllegalAccessException | NoSuchMethodException e) { |
| 84 | Log.wtf(InetAddressCompat.class.getSimpleName(), "Error calling " + method, e); |
| 85 | throw new IllegalStateException("Error querying via " + method, e); |
Remi NGUYEN VAN | 342dddd | 2021-03-18 23:27:19 +0900 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | } |