< prev index next >

src/java.base/share/classes/sun/net/util/IPAddressUtil.java

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.net.util;
  27 
  28 import java.io.IOException;
  29 import java.io.UncheckedIOException;
  30 import java.net.Inet6Address;
  31 import java.net.InetAddress;
  32 import java.net.InetSocketAddress;
  33 import java.net.NetworkInterface;
  34 import java.net.SocketException;
  35 import java.security.AccessController;
  36 import java.security.PrivilegedExceptionAction;
  37 import java.security.PrivilegedActionException;
  38 import java.util.List;
  39 import java.util.concurrent.ConcurrentHashMap;
  40 import java.util.stream.Collectors;
  41 
  42 public class IPAddressUtil {
  43     private static final int INADDR4SZ = 4;
  44     private static final int INADDR16SZ = 16;
  45     private static final int INT16SZ = 2;
  46 
  47     /*
  48      * Converts IPv4 address in its textual presentation form
  49      * into its numeric binary form.
  50      *
  51      * @param src a String representing an IPv4 address in standard format
  52      * @return a byte array representing the IPv4 numeric address
  53      */
  54     @SuppressWarnings("fallthrough")
  55     public static byte[] textToNumericFormatV4(String src)
  56     {
  57         byte[] res = new byte[INADDR4SZ];
  58 
  59         long tmpValue = 0;
  60         int currByte = 0;
  61         boolean newOctet = true;


 283      * Utility routine to check if the InetAddress is an
 284      * IPv4 mapped IPv6 address.
 285      *
 286      * @return a <code>boolean</code> indicating if the InetAddress is
 287      * an IPv4 mapped IPv6 address; or false if address is IPv4 address.
 288      */
 289     private static boolean isIPv4MappedAddress(byte[] addr) {
 290         if (addr.length < INADDR16SZ) {
 291             return false;
 292         }
 293         if ((addr[0] == 0x00) && (addr[1] == 0x00) &&
 294             (addr[2] == 0x00) && (addr[3] == 0x00) &&
 295             (addr[4] == 0x00) && (addr[5] == 0x00) &&
 296             (addr[6] == 0x00) && (addr[7] == 0x00) &&
 297             (addr[8] == 0x00) && (addr[9] == 0x00) &&
 298             (addr[10] == (byte)0xff) &&
 299             (addr[11] == (byte)0xff))  {
 300             return true;
 301         }
 302         return false;
 303     }
 304     /**
 305      * Mapping from unscoped local Inet(6)Address to the same address
 306      * including the correct scope-id, determined from NetworkInterface.
 307      */
 308     private final static ConcurrentHashMap<InetAddress,InetAddress>
 309         cache = new ConcurrentHashMap<>();
 310 
 311     /**
 312      * Returns a scoped version of the supplied local, link-local ipv6 address
 313      * if that scope-id can be determined from local NetworkInterfaces.
 314      * If the address already has a scope-id or if the address is not local, ipv6
 315      * or link local, then the original address is returned.
 316      *
 317      * @param addr
 318      * @exception SocketException if the given ipv6 link local address is found
 319      *            on more than one local interface
 320      * @return
 321      */
 322     public static InetAddress toScopedAddress(InetAddress address)
 323         throws SocketException {
 324 
 325         if (address instanceof Inet6Address && address.isLinkLocalAddress()
 326             && ((Inet6Address) address).getScopeId() == 0) {
 327 
 328             InetAddress cached = null;
 329             try {
 330                 cached = cache.computeIfAbsent(address, k -> findScopedAddress(k));
 331             } catch (UncheckedIOException e) {
 332                 throw (SocketException)e.getCause();
 333             }
 334             return cached != null ? cached : address;
 335         } else {
 336             return address;
 337         }
 338     }
 339 
 340     /**
 341      * Same as above for InetSocketAddress
 342      */
 343     public static InetSocketAddress toScopedAddress(InetSocketAddress address)
 344         throws SocketException {
 345         InetAddress addr;
 346         InetAddress orig = address.getAddress();
 347         if ((addr = toScopedAddress(orig)) == orig) {
 348             return address;
 349         } else {
 350             return new InetSocketAddress(addr, address.getPort());
 351         }
 352     }
 353 
 354     private static InetAddress findScopedAddress(InetAddress address) {
 355         PrivilegedExceptionAction<List<InetAddress>> pa = () -> NetworkInterface.networkInterfaces()
 356                 .flatMap(NetworkInterface::inetAddresses)
 357                 .filter(a -> (a instanceof Inet6Address)
 358                         && address.equals(a)
 359                         && ((Inet6Address) a).getScopeId() != 0)
 360                 .collect(Collectors.toList());
 361         List<InetAddress> result;
 362         try {
 363             result = AccessController.doPrivileged(pa);
 364             var sz = result.size();
 365             if (sz == 0)
 366                 return null;
 367             if (sz > 1)
 368                 throw new UncheckedIOException(new SocketException(
 369                     "Duplicate link local addresses: must specify scope-id"));
 370             return result.get(0);
 371         } catch (PrivilegedActionException pae) {
 372             return null;
 373         }
 374     }
 375 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.net.util;
  27 














  28 public class IPAddressUtil {
  29     private static final int INADDR4SZ = 4;
  30     private static final int INADDR16SZ = 16;
  31     private static final int INT16SZ = 2;
  32 
  33     /*
  34      * Converts IPv4 address in its textual presentation form
  35      * into its numeric binary form.
  36      *
  37      * @param src a String representing an IPv4 address in standard format
  38      * @return a byte array representing the IPv4 numeric address
  39      */
  40     @SuppressWarnings("fallthrough")
  41     public static byte[] textToNumericFormatV4(String src)
  42     {
  43         byte[] res = new byte[INADDR4SZ];
  44 
  45         long tmpValue = 0;
  46         int currByte = 0;
  47         boolean newOctet = true;


 269      * Utility routine to check if the InetAddress is an
 270      * IPv4 mapped IPv6 address.
 271      *
 272      * @return a <code>boolean</code> indicating if the InetAddress is
 273      * an IPv4 mapped IPv6 address; or false if address is IPv4 address.
 274      */
 275     private static boolean isIPv4MappedAddress(byte[] addr) {
 276         if (addr.length < INADDR16SZ) {
 277             return false;
 278         }
 279         if ((addr[0] == 0x00) && (addr[1] == 0x00) &&
 280             (addr[2] == 0x00) && (addr[3] == 0x00) &&
 281             (addr[4] == 0x00) && (addr[5] == 0x00) &&
 282             (addr[6] == 0x00) && (addr[7] == 0x00) &&
 283             (addr[8] == 0x00) && (addr[9] == 0x00) &&
 284             (addr[10] == (byte)0xff) &&
 285             (addr[11] == (byte)0xff))  {
 286             return true;
 287         }
 288         return false;







































































 289     }
 290 }
< prev index next >