src/java.base/share/classes/sun/nio/ch/NativeSocketAddress.java
changeset 59329 289000934908
equal deleted inserted replaced
59328:f280911d3427 59329:289000934908
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     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
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     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.nio.ch;
       
    27 
       
    28 import java.net.Inet6Address;
       
    29 import java.net.InetAddress;
       
    30 import java.net.InetSocketAddress;
       
    31 import java.net.SocketException;
       
    32 import java.net.UnknownHostException;
       
    33 
       
    34 import jdk.internal.misc.Unsafe;
       
    35 import jdk.internal.util.ArraysSupport;
       
    36 
       
    37 /**
       
    38  * A native socket address that is the union of struct sockaddr, struct sockaddr_in,
       
    39  * and struct sockaddr_in6.
       
    40  *
       
    41  * This class is not thread safe.
       
    42  */
       
    43 class NativeSocketAddress {
       
    44     private static final Unsafe UNSAFE = Unsafe.getUnsafe();
       
    45     private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(byte[].class);
       
    46 
       
    47     private static final int AF_INET  = AFINET();
       
    48     private static final int AF_INET6 = AFINET6();
       
    49 
       
    50     private static final int SIZEOF_SOCKETADDRESS = sizeofSOCKETADDRESS();
       
    51     private static final int SIZEOF_FAMILY        = sizeofFamily();
       
    52     private static final int OFFSET_FAMILY        = offsetFamily();
       
    53     private static final int OFFSET_SIN4_PORT     = offsetSin4Port();
       
    54     private static final int OFFSET_SIN4_ADDR     = offsetSin4Addr();
       
    55     private static final int OFFSET_SIN6_PORT     = offsetSin6Port();
       
    56     private static final int OFFSET_SIN6_ADDR     = offsetSin6Addr();
       
    57     private static final int OFFSET_SIN6_SCOPE_ID = offsetSin6ScopeId();
       
    58 
       
    59     // SOCKETADDRESS
       
    60     private final long address;
       
    61 
       
    62     // cached copy of SOCKETADDRESS and the corresponding InetSocketAddress
       
    63     private final long cachedSocketAddress;
       
    64     private InetSocketAddress cachedInetSocketAddress;
       
    65 
       
    66     NativeSocketAddress() {
       
    67         // allocate 2 * SOCKETADDRESS
       
    68         int size = SIZEOF_SOCKETADDRESS << 1;
       
    69         long base = UNSAFE.allocateMemory(size);
       
    70         UNSAFE.setMemory(base, size, (byte) 0);
       
    71 
       
    72         this.address = base;
       
    73         this.cachedSocketAddress = base + SIZEOF_SOCKETADDRESS;
       
    74     }
       
    75 
       
    76     long address() {
       
    77         return address;
       
    78     }
       
    79 
       
    80     void free() {
       
    81         UNSAFE.freeMemory(address);
       
    82     }
       
    83 
       
    84     /**
       
    85      * Return an InetSocketAddress to represent the socket address in this buffer.
       
    86      * @throws SocketException if the socket address is not AF_INET or AF_INET6
       
    87      */
       
    88     InetSocketAddress toInetSocketAddress() throws SocketException {
       
    89         // return cached InetSocketAddress if the SOCKETADDRESS bytes match
       
    90         if (cachedInetSocketAddress != null && mismatch() < 0) {
       
    91             return cachedInetSocketAddress;
       
    92         }
       
    93 
       
    94         // decode SOCKETADDRESS to InetSocketAddress
       
    95         int family = family();
       
    96         if (family != AF_INET && family != AF_INET6)
       
    97             throw new SocketException("Socket family not recognized");
       
    98         var isa = new InetSocketAddress(address(family), port(family));
       
    99 
       
   100         // copy SOCKETADDRESS and InetSocketAddress
       
   101         UNSAFE.copyMemory(null, address, null, cachedSocketAddress, SIZEOF_SOCKETADDRESS);
       
   102         this.cachedInetSocketAddress = isa;
       
   103         return isa;
       
   104     }
       
   105 
       
   106     /**
       
   107      * Find a mismatch between the SOCKETADDRESS structures stored at address
       
   108      * and cachedSocketAddress.
       
   109      * @return the byte offset of the first mismatch or -1 if no mismatch
       
   110      */
       
   111     private int mismatch() {
       
   112         int i = ArraysSupport.vectorizedMismatch(null,
       
   113                 address,
       
   114                 null,
       
   115                 cachedSocketAddress,
       
   116                 SIZEOF_SOCKETADDRESS,
       
   117                 ArraysSupport.LOG2_ARRAY_BYTE_INDEX_SCALE);
       
   118         if (i >= 0)
       
   119             return i;
       
   120         i = SIZEOF_SOCKETADDRESS - ~i;
       
   121         for (; i < SIZEOF_SOCKETADDRESS; i++) {
       
   122             if (UNSAFE.getByte(address + i) != UNSAFE.getByte(cachedSocketAddress + i)) {
       
   123                 return i;
       
   124             }
       
   125         }
       
   126         return -1;
       
   127     }
       
   128 
       
   129     @Override
       
   130     public String toString() {
       
   131         int family = family();
       
   132         if (family == AF_INET || family == AF_INET6) {
       
   133             return ((family == AF_INET) ? "AF_INET" : "AF_INET6")
       
   134                     + ", address=" + address(family) + ", port=" + port(family);
       
   135         } else {
       
   136             return "<unknown>";
       
   137         }
       
   138     }
       
   139 
       
   140     /**
       
   141      * Return the value of the sa_family field.
       
   142      */
       
   143     private int family() {
       
   144         if (SIZEOF_FAMILY == 1) {
       
   145             return UNSAFE.getByte(address + OFFSET_FAMILY);
       
   146         } else if (SIZEOF_FAMILY == 2) {
       
   147             return UNSAFE.getShort(address + OFFSET_FAMILY);
       
   148         } else {
       
   149             throw new InternalError();
       
   150         }
       
   151     }
       
   152 
       
   153     /**
       
   154      * Return the value of the sin4_port or sin6_port field. These fields are
       
   155      * stored in network order.
       
   156      */
       
   157     private int port(int family) {
       
   158         byte b1, b2;
       
   159         if (family == AF_INET) {
       
   160             b1 = UNSAFE.getByte(address + OFFSET_SIN4_PORT);
       
   161             b2 = UNSAFE.getByte(address + OFFSET_SIN4_PORT + 1);
       
   162         } else {
       
   163             b1 = UNSAFE.getByte(address + OFFSET_SIN6_PORT);
       
   164             b2 = UNSAFE.getByte(address + OFFSET_SIN6_PORT + 1);
       
   165         }
       
   166         return (Byte.toUnsignedInt(b1) << 8) + Byte.toUnsignedInt(b2);
       
   167     }
       
   168 
       
   169     /**
       
   170      * Return an InetAddress to represent the value of the address in the
       
   171      * sin4_addr or sin6_addr fields.
       
   172      */
       
   173     private InetAddress address(int family) {
       
   174         int len;
       
   175         int offset;
       
   176         int scope_id;
       
   177         if (family == AF_INET) {
       
   178             len = 4;
       
   179             offset = OFFSET_SIN4_ADDR;
       
   180             scope_id = 0;
       
   181         } else {
       
   182             len = 16;
       
   183             offset = OFFSET_SIN6_ADDR;
       
   184             scope_id = UNSAFE.getInt(address + OFFSET_SIN6_SCOPE_ID);
       
   185         }
       
   186         byte[] bytes = new byte[len];
       
   187         UNSAFE.copyMemory(null, address + offset, bytes, ARRAY_BASE_OFFSET, len);
       
   188         try {
       
   189             if (scope_id == 0) {
       
   190                 return InetAddress.getByAddress(bytes);
       
   191             } else {
       
   192                 return Inet6Address.getByAddress(null, bytes, scope_id);
       
   193             }
       
   194         } catch (UnknownHostException e) {
       
   195             throw new InternalError(e);
       
   196         }
       
   197     }
       
   198 
       
   199     private static native int AFINET();
       
   200     private static native int AFINET6();
       
   201     private static native int sizeofSOCKETADDRESS();
       
   202     private static native int sizeofFamily();
       
   203     private static native int offsetFamily();
       
   204     private static native int offsetSin4Port();
       
   205     private static native int offsetSin4Addr();
       
   206     private static native int offsetSin6Port();
       
   207     private static native int offsetSin6Addr();
       
   208     private static native int offsetSin6ScopeId();
       
   209 
       
   210     static {
       
   211         IOUtil.load();
       
   212     }
       
   213 }