181 { |
182 { |
182 translateException(x, false); |
183 translateException(x, false); |
183 } |
184 } |
184 |
185 |
185 /** |
186 /** |
|
187 * Returns the local address after performing a SecurityManager#checkConnect. |
|
188 */ |
|
189 static InetSocketAddress getRevealedLocalAddress(InetSocketAddress addr) { |
|
190 SecurityManager sm = System.getSecurityManager(); |
|
191 if (addr == null || sm == null) |
|
192 return addr; |
|
193 |
|
194 try{ |
|
195 sm.checkConnect(addr.getAddress().getHostAddress(), -1); |
|
196 // Security check passed |
|
197 } catch (SecurityException e) { |
|
198 // Return loopback address only if security check fails |
|
199 addr = getLoopbackAddress(addr.getPort()); |
|
200 } |
|
201 return addr; |
|
202 } |
|
203 |
|
204 static String getRevealedLocalAddressAsString(InetSocketAddress addr) { |
|
205 return System.getSecurityManager() == null ? addr.toString() : |
|
206 getLoopbackAddress(addr.getPort()).toString(); |
|
207 } |
|
208 |
|
209 private static InetSocketAddress getLoopbackAddress(int port) { |
|
210 return new InetSocketAddress(InetAddress.getLoopbackAddress(), |
|
211 port); |
|
212 } |
|
213 |
|
214 /** |
186 * Returns any IPv4 address of the given network interface, or |
215 * Returns any IPv4 address of the given network interface, or |
187 * null if the interface does not have any IPv4 addresses. |
216 * null if the interface does not have any IPv4 addresses. |
188 */ |
217 */ |
189 static Inet4Address anyInet4Address(final NetworkInterface interf) { |
218 static Inet4Address anyInet4Address(final NetworkInterface interf) { |
190 return AccessController.doPrivileged(new PrivilegedAction<Inet4Address>() { |
219 return AccessController.doPrivileged(new PrivilegedAction<Inet4Address>() { |