| /* Copyright 2015-2023 Rivoreo |
| |
| Permission is hereby granted, free of charge, to any person obtaining |
| a copy of this software and associated documentation files (the |
| "Software"), to deal in the Software without restriction, including |
| without limitation the rights to use, copy, modify, merge, publish, |
| distribute, sublicense, and/or sell copies of the Software, and to |
| permit persons to whom the Software is furnished to do so, subject to |
| the following conditions: |
| |
| The above copyright notice and this permission notice shall be |
| included in all copies or substantial portions of the Software. |
| |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE |
| FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| */ |
| |
| package rivoreo.net; |
| |
| import java.net.InetAddress; |
| import java.util.Map; |
| import java.lang.reflect.Field; |
| |
| public final class InetAddressCacheControl { |
| private static boolean sun_inetaddress_available = true; |
| private static Field sun_inetaddresscachepolicy_cache_policy_field; |
| private static Field sun_inetaddresscachepolicy_negative_cache_policy_field; |
| |
| public static boolean set_sun_inetaddress_cache_policy(boolean is_negative, int value) { |
| if(!sun_inetaddress_available) return false; |
| Field field = is_negative ? sun_inetaddresscachepolicy_negative_cache_policy_field : sun_inetaddresscachepolicy_cache_policy_field; |
| if(field == null) try { |
| Class<?> sun_inetaddresscachepolicy_class = Class.forName("sun.net.InetAddressCachePolicy"); |
| field = sun_inetaddresscachepolicy_class.getDeclaredField( |
| is_negative ? "negativeCachePolicy" : "cachePolicy" |
| ); |
| field.setAccessible(true); |
| if(is_negative) sun_inetaddresscachepolicy_negative_cache_policy_field = field; |
| else sun_inetaddresscachepolicy_cache_policy_field = field; |
| } catch(ClassNotFoundException e) { |
| sun_inetaddress_available = false; |
| return false; |
| } catch(Exception e) { |
| return false; |
| } |
| try { |
| field.setInt(null, value); |
| } catch(IllegalAccessException e) { |
| e.printStackTrace(); |
| return false; |
| } |
| return true; |
| } |
| |
| private static Field inetaddress_cache_cache_field; |
| private static Map<String, ?> inetaddress_address_cache; |
| private static Map<String, ?> inetaddress_negative_cache; |
| |
| private static Map<String, ?> get_cache_map(String field_name) throws NoSuchFieldException, IllegalAccessException { |
| Field f = InetAddress.class.getDeclaredField(field_name); |
| f.setAccessible(true); |
| if(inetaddress_cache_cache_field == null) { |
| inetaddress_cache_cache_field = f.getType().getDeclaredField("cache"); |
| inetaddress_cache_cache_field.setAccessible(true); |
| } |
| return (Map<String, ?>)inetaddress_cache_cache_field.get(f.get(null)); |
| } |
| |
| public static boolean evict_caches() { |
| if(inetaddress_address_cache == null) try { |
| inetaddress_address_cache = get_cache_map("addressCache"); |
| } catch(Exception e) { |
| return false; |
| } |
| inetaddress_address_cache.clear(); |
| if(inetaddress_negative_cache == null) try { |
| inetaddress_negative_cache = get_cache_map("negativeCache"); |
| } catch(Exception e) { |
| return false; |
| } |
| inetaddress_negative_cache.clear(); |
| return true; |
| } |
| } |