1 /* |
1 /* |
2 * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
68 * Which means same family as addr because at this point it could |
70 * Which means same family as addr because at this point it could |
69 * be either an IPv6 address or an IPv4 address (case of a dual |
71 * be either an IPv6 address or an IPv4 address (case of a dual |
70 * stack system). |
72 * stack system). |
71 */ |
73 */ |
72 java.util.Enumeration<InetAddress> it = netif.getInetAddresses(); |
74 java.util.Enumeration<InetAddress> it = netif.getInetAddresses(); |
73 InetAddress inetaddr = null; |
75 InetAddress inetaddr; |
74 while (it.hasMoreElements()) { |
76 while (it.hasMoreElements()) { |
75 inetaddr = it.nextElement(); |
77 inetaddr = it.nextElement(); |
76 if (inetaddr.getClass().isInstance(addr)) { |
78 if (inetaddr.getClass().isInstance(addr)) { |
77 ifaddr = inetaddr.getAddress(); |
79 ifaddr = inetaddr.getAddress(); |
78 if (inetaddr instanceof Inet6Address) { |
80 if (inetaddr instanceof Inet6Address) { |
108 public synchronized InetAddress loopbackAddress() { |
110 public synchronized InetAddress loopbackAddress() { |
109 if (loopbackAddress == null) { |
111 if (loopbackAddress == null) { |
110 boolean preferIPv6Address = |
112 boolean preferIPv6Address = |
111 InetAddress.preferIPv6Address == PREFER_IPV6_VALUE || |
113 InetAddress.preferIPv6Address == PREFER_IPV6_VALUE || |
112 InetAddress.preferIPv6Address == PREFER_SYSTEM_VALUE; |
114 InetAddress.preferIPv6Address == PREFER_SYSTEM_VALUE; |
113 InetAddress loopback4 = (new Inet4AddressImpl()).loopbackAddress(); |
115 |
114 InetAddress loopback6 = new Inet6Address("localhost", |
116 for (int i = 0; i < 2; i++) { |
115 new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
117 InetAddress address; |
116 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}); |
118 // Order the candidate addresses by preference. |
117 // Order the candidate addresses by preference. |
119 if (i == (preferIPv6Address ? 0 : 1)) { |
118 InetAddress[] addresses = preferIPv6Address |
120 address = new Inet6Address("localhost", |
119 ? new InetAddress[] {loopback6, loopback4} |
121 new byte[]{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
120 : new InetAddress[] {loopback4, loopback6}; |
122 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}); |
121 // In case of failure, default to the preferred address. |
123 } else { |
122 loopbackAddress = addresses[0]; |
124 address = new Inet4Address("localhost", new byte[]{ 0x7f,0x00,0x00,0x01 }); |
123 // Pick the first candidate address that actually exists. |
125 } |
124 for (InetAddress address : addresses) { |
126 if (i == 0) { |
|
127 // In case of failure, default to the preferred address. |
|
128 loopbackAddress = address; |
|
129 } |
125 try { |
130 try { |
126 if (NetworkInterface.getByInetAddress(address) == null) { |
131 if (!NetworkInterface.isBoundInetAddress(address)) { |
127 continue; |
132 continue; |
128 } |
133 } |
129 } catch (SocketException e) { |
134 } catch (SocketException e) { |
130 continue; |
135 continue; |
131 } |
136 } |