Path: blob/master/src/java.base/share/classes/sun/nio/ch/UnixDomainSockets.java
41159 views
/*1* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.ch;2627import java.io.FileDescriptor;28import java.io.IOException;29import java.net.BindException;30import java.net.NetPermission;31import java.net.SocketAddress;32import java.net.UnixDomainSocketAddress;33import java.nio.channels.UnsupportedAddressTypeException;34import java.nio.file.FileSystems;35import java.nio.file.InvalidPathException;36import java.nio.file.Path;37import java.nio.file.spi.FileSystemProvider;38import java.security.NoSuchAlgorithmException;39import java.security.SecureRandom;40import java.util.Random;41import sun.nio.fs.AbstractFileSystemProvider;4243class UnixDomainSockets {44private UnixDomainSockets() { }4546static final UnixDomainSocketAddress UNNAMED = UnixDomainSocketAddress.of("");4748private static final boolean supported;4950private static final String tempDir = UnixDomainSocketsUtil.getTempDir();5152private static final NetPermission accessUnixDomainSocket =53new NetPermission("accessUnixDomainSocket");5455static boolean isSupported() {56return supported;57}5859static void checkPermission() {60@SuppressWarnings("removal")61SecurityManager sm = System.getSecurityManager();62if (sm != null)63sm.checkPermission(accessUnixDomainSocket);64}6566static UnixDomainSocketAddress getRevealedLocalAddress(SocketAddress sa) {67UnixDomainSocketAddress addr = (UnixDomainSocketAddress) sa;68try {69checkPermission();70// Security check passed71} catch (SecurityException e) {72// Return unnamed address only if security check fails73addr = UNNAMED;74}75return addr;76}7778static UnixDomainSocketAddress localAddress(FileDescriptor fd) throws IOException {79String path = new String(localAddress0(fd), UnixDomainSocketsUtil.getCharset());80return UnixDomainSocketAddress.of(path);81}8283private static native byte[] localAddress0(FileDescriptor fd) throws IOException;8485@SuppressWarnings("removal")86static String getRevealedLocalAddressAsString(SocketAddress sa) {87return (System.getSecurityManager() != null) ? sa.toString() : "";88}8990static UnixDomainSocketAddress checkAddress(SocketAddress sa) {91if (sa == null)92throw new NullPointerException();93if (!(sa instanceof UnixDomainSocketAddress))94throw new UnsupportedAddressTypeException();95return (UnixDomainSocketAddress) sa;96}9798static byte[] getPathBytes(Path path) {99FileSystemProvider provider = FileSystems.getDefault().provider();100return ((AbstractFileSystemProvider) provider).getSunPathForSocketFile(path);101}102103static FileDescriptor socket() throws IOException {104return IOUtil.newFD(socket0());105}106107static void bind(FileDescriptor fd, Path addr) throws IOException {108byte[] path = getPathBytes(addr);109if (path.length == 0) {110throw new BindException("Server socket cannot bind to unnamed address");111}112bind0(fd, path);113}114115private static Random getRandom() {116try {117return SecureRandom.getInstance("NativePRNGNonBlocking");118} catch (NoSuchAlgorithmException e) {119return new SecureRandom(); // This should not fail120}121}122123private static final Random random = getRandom();124125/**126* Return a possible temporary name to bind to, which is different for each call127* Name is of the form <temp dir>/socket_<random>128*/129static UnixDomainSocketAddress generateTempName() throws IOException {130String dir = UnixDomainSockets.tempDir;131if (dir == null)132throw new BindException("Could not locate temporary directory for sockets");133int rnd = random.nextInt(Integer.MAX_VALUE);134try {135Path path = Path.of(dir, "socket_" + rnd);136return UnixDomainSocketAddress.of(path);137} catch (InvalidPathException e) {138throw new BindException("Invalid temporary directory");139}140}141142static int connect(FileDescriptor fd, SocketAddress sa) throws IOException {143return UnixDomainSockets.connect(fd, ((UnixDomainSocketAddress) sa).getPath());144}145146static int connect(FileDescriptor fd, Path path) throws IOException {147return connect0(fd, getPathBytes(path));148}149150static int accept(FileDescriptor fd, FileDescriptor newfd, String[] paths)151throws IOException152{153Object[] array = new Object[1];154int n = accept0(fd, newfd, array);155if (n > 0) {156byte[] bytes = (byte[]) array[0];157paths[0] = new String(bytes, UnixDomainSocketsUtil.getCharset());158}159return n;160}161162private static native boolean socketSupported();163164private static native int socket0() throws IOException;165166private static native void bind0(FileDescriptor fd, byte[] path)167throws IOException;168169private static native int connect0(FileDescriptor fd, byte[] path)170throws IOException;171172private static native int accept0(FileDescriptor fd, FileDescriptor newfd, Object[] array)173throws IOException;174175static {176// Load all required native libs177IOUtil.load();178supported = socketSupported();179}180}181182183