src/java.base/share/classes/java/net/NetworkInterface.java
changeset 55596 d01b345865d7
parent 52499 768b1c612100
child 55693 9a97b1393e72
equal deleted inserted replaced
55595:cf5a438b3c41 55596:d01b345865d7
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 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
   319      */
   319      */
   320     public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketException {
   320     public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketException {
   321         if (addr == null) {
   321         if (addr == null) {
   322             throw new NullPointerException();
   322             throw new NullPointerException();
   323         }
   323         }
   324         if (addr instanceof Inet4Address) {
   324 
   325             Inet4Address inet4Address = (Inet4Address) addr;
   325         if (addr.holder.family == InetAddress.IPv4) {
   326             if (inet4Address.holder.family != InetAddress.IPv4) {
   326             if (!(addr instanceof Inet4Address)) {
   327                 throw new IllegalArgumentException("invalid family type: "
   327                 throw new IllegalArgumentException("invalid family type: "
   328                         + inet4Address.holder.family);
   328                         + addr.holder.family);
   329             }
   329             }
   330         } else if (addr instanceof Inet6Address) {
   330         } else if (addr.holder.family == InetAddress.IPv6) {
   331             Inet6Address inet6Address = (Inet6Address) addr;
   331             if (!(addr instanceof Inet6Address)) {
   332             if (inet6Address.holder.family != InetAddress.IPv6) {
       
   333                 throw new IllegalArgumentException("invalid family type: "
   332                 throw new IllegalArgumentException("invalid family type: "
   334                         + inet6Address.holder.family);
   333                         + addr.holder.family);
   335             }
   334             }
   336         } else {
   335         } else {
   337             throw new IllegalArgumentException("invalid address type: " + addr);
   336             throw new IllegalArgumentException("invalid address type: " + addr);
   338         }
   337         }
   339         return getByInetAddress0(addr);
   338         return getByInetAddress0(addr);
   392         }  else {
   391         }  else {
   393             throw new SocketException("No network interfaces configured");
   392             throw new SocketException("No network interfaces configured");
   394         }
   393         }
   395     }
   394     }
   396 
   395 
       
   396     /**
       
   397      * Checks if the given address is bound to any of the interfaces on this
       
   398      * machine.
       
   399      *
       
   400      * @param   addr
       
   401      *          The {@code InetAddress} to search with.
       
   402      * @return  true iff the addr parameter is currently bound to one of
       
   403      *          the interfaces on this machine.
       
   404      *
       
   405      * @throws  SocketException
       
   406      *          If an I/O error occurs.
       
   407      */
       
   408     /* package-private */ static boolean isBoundInetAddress(InetAddress addr)
       
   409         throws SocketException {
       
   410         return boundInetAddress0(addr);
       
   411     }
       
   412 
   397     private static <T> Enumeration<T> enumerationFromArray(T[] a) {
   413     private static <T> Enumeration<T> enumerationFromArray(T[] a) {
   398         return new Enumeration<>() {
   414         return new Enumeration<>() {
   399             int i = 0;
   415             int i = 0;
   400 
   416 
   401             @Override
   417             @Override
   428     private static native NetworkInterface getByName0(String name)
   444     private static native NetworkInterface getByName0(String name)
   429         throws SocketException;
   445         throws SocketException;
   430 
   446 
   431     private static native NetworkInterface getByIndex0(int index)
   447     private static native NetworkInterface getByIndex0(int index)
   432         throws SocketException;
   448         throws SocketException;
       
   449 
       
   450     private static native boolean boundInetAddress0(InetAddress addr)
       
   451             throws SocketException;
   433 
   452 
   434     private static native NetworkInterface getByInetAddress0(InetAddress addr)
   453     private static native NetworkInterface getByInetAddress0(InetAddress addr)
   435         throws SocketException;
   454         throws SocketException;
   436 
   455 
   437     /**
   456     /**