Skip to content

Commit 6bb7e45

Browse files
8245194: Unix domain socket channel implementation
Reviewed-by: erikj, dfuchs, alanb, chegar
1 parent 8bde2f4 commit 6bb7e45

File tree

73 files changed

+5431
-1113
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+5431
-1113
lines changed

make/modules/java.base/Copy.gmk

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,16 @@ endif
182182

183183
################################################################################
184184

185-
$(eval $(call SetupCopyFiles, COPY_NET_PROPERTIES, \
186-
FILES := $(TOPDIR)/src/java.base/share/conf/net.properties, \
187-
DEST := $(CONF_DST_DIR), \
188-
))
185+
NET_PROPERTIES_SRCS := $(TOPDIR)/src/java.base/share/conf/net.properties \
186+
$(TOPDIR)/src/java.base/$(OPENJDK_TARGET_OS_TYPE)/conf/net.properties
187+
188+
NET_PROPERTIES_DST := $(CONF_DST_DIR)/net.properties
189+
190+
$(NET_PROPERTIES_DST): $(NET_PROPERTIES_SRCS)
191+
$(call MakeTargetDir)
192+
$(CAT) $(NET_PROPERTIES_SRCS) > $@
189193

190-
TARGETS += $(COPY_NET_PROPERTIES)
194+
TARGETS += $(NET_PROPERTIES_DST)
191195

192196
ifeq ($(call isTargetOs, linux), true)
193197
$(eval $(call SetupCopyFiles, COPY_SDP_CONF, \

src/java.base/share/classes/java/net/NetPermission.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@
6666
* </tr>
6767
*
6868
* <tr>
69+
* <th scope="row">accessUnixDomainSocket</th>
70+
* <td>The ability to accept, bind, connect or get the local address
71+
* of a <i>Unix Domain</i> socket.
72+
* </td>
73+
* <td>Malicious code could connect to local processes using Unix domain sockets
74+
* or impersonate local processes, by binding to the same pathnames (assuming they
75+
* have the required Operating System permissions.</td>
76+
* </tr>
77+
*
78+
* <tr>
6979
* <th scope="row">getCookieHandler</th>
7080
* <td>The ability to get the cookie handler that processes highly
7181
* security sensitive cookie information for an Http session.</td>

src/java.base/share/classes/java/net/StandardProtocolFamily.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2009, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -41,5 +41,11 @@ public enum StandardProtocolFamily implements ProtocolFamily {
4141
/**
4242
* Internet Protocol Version 6 (IPv6)
4343
*/
44-
INET6
44+
INET6,
45+
46+
/**
47+
* Unix domain (Local) interprocess communication.
48+
* @since 16
49+
*/
50+
UNIX
4551
}
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
/*
2+
* Copyright (c) 2020, 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 java.net;
27+
28+
import java.io.ObjectStreamException;
29+
import java.io.Serializable;
30+
import java.net.SocketAddress;
31+
import java.nio.channels.SocketChannel;
32+
import java.nio.file.FileSystem;
33+
import java.nio.file.FileSystems;
34+
import java.nio.file.InvalidPathException;
35+
import java.nio.file.Path;
36+
37+
/**
38+
* A <a href="package-summary.html#unixdomain">Unix domain</a> socket address.
39+
* A Unix domain socket address encapsulates a file-system path that Unix domain sockets
40+
* bind or connect to.
41+
*
42+
* <p> An <a id="unnamed"></a><i>unnamed</i> {@code UnixDomainSocketAddress} has
43+
* an empty path. The local address of a {@link SocketChannel} to a Unix domain socket
44+
* that is <i>automatically</i> or <i>implicitly</i> bound will be unnamed.
45+
*
46+
* <p> {@link Path} objects used to create instances of this class must be obtained
47+
* from the {@linkplain FileSystems#getDefault system-default} file system.
48+
*
49+
* @see java.nio.channels.SocketChannel
50+
* @see java.nio.channels.ServerSocketChannel
51+
* @since 16
52+
*/
53+
public final class UnixDomainSocketAddress extends SocketAddress {
54+
@java.io.Serial
55+
static final long serialVersionUID = 92902496589351288L;
56+
57+
private final transient Path path;
58+
59+
/**
60+
* A serial proxy for all {@link UnixDomainSocketAddress} instances.
61+
* It captures the file path name and reconstructs using the public static
62+
* {@link #of(String) factory}.
63+
*
64+
* @serial include
65+
*/
66+
private static final class Ser implements Serializable {
67+
@java.io.Serial
68+
static final long serialVersionUID = -7955684448513979814L;
69+
70+
/**
71+
* The path name.
72+
* @serial
73+
*/
74+
private final String pathname;
75+
76+
Ser(String pathname) {
77+
this.pathname = pathname;
78+
}
79+
80+
/**
81+
* Creates a {@link UnixDomainSocketAddress} instance, by an invocation
82+
* of the {@link #of(String) factory} method passing the path name.
83+
* @return a UnixDomainSocketAddress
84+
*/
85+
@java.io.Serial
86+
private Object readResolve() {
87+
return UnixDomainSocketAddress.of(pathname);
88+
}
89+
}
90+
91+
/**
92+
* Returns a
93+
* <a href="{@docRoot}/serialized-form.html#java.net.UnixDomainSocketAddress.Ser">
94+
* Ser</a> containing the path name of this instance.
95+
*
96+
* @return a {@link Ser}
97+
* representing the path name of this instance
98+
*/
99+
@java.io.Serial
100+
private Object writeReplace() throws ObjectStreamException {
101+
return new Ser(path.toString());
102+
}
103+
104+
/**
105+
* Throws InvalidObjectException, always.
106+
* @param s the stream
107+
* @throws java.io.InvalidObjectException always
108+
*/
109+
@java.io.Serial
110+
private void readObject(java.io.ObjectInputStream s)
111+
throws java.io.InvalidObjectException
112+
{
113+
throw new java.io.InvalidObjectException("Proxy required");
114+
}
115+
116+
/**
117+
* Throws InvalidObjectException, always.
118+
* @throws java.io.InvalidObjectException always
119+
*/
120+
@java.io.Serial
121+
private void readObjectNoData()
122+
throws java.io.InvalidObjectException
123+
{
124+
throw new java.io.InvalidObjectException("Proxy required");
125+
}
126+
127+
private UnixDomainSocketAddress(Path path) {
128+
this.path = path;
129+
}
130+
131+
/**
132+
* Creates a UnixDomainSocketAddress from the given path string.
133+
*
134+
* @param pathname
135+
* The path string, which can be empty
136+
*
137+
* @return A UnixDomainSocketAddress
138+
*
139+
* @throws InvalidPathException
140+
* If the path cannot be converted to a Path
141+
*
142+
* @throws NullPointerException if pathname is {@code null}
143+
*/
144+
public static UnixDomainSocketAddress of(String pathname) {
145+
return of(Path.of(pathname));
146+
}
147+
148+
/**
149+
* Creates a UnixDomainSocketAddress for the given path.
150+
*
151+
* @param path
152+
* The path to the socket, which can be empty
153+
*
154+
* @return A UnixDomainSocketAddress
155+
*
156+
* @throws IllegalArgumentException
157+
* If the path is not associated with the default file system
158+
*
159+
* @throws NullPointerException if path is {@code null}
160+
*/
161+
public static UnixDomainSocketAddress of(Path path) {
162+
FileSystem fs = path.getFileSystem();
163+
if (fs != FileSystems.getDefault()) {
164+
throw new IllegalArgumentException();
165+
}
166+
if (fs.getClass().getModule() != Object.class.getModule()) {
167+
throw new IllegalArgumentException();
168+
}
169+
return new UnixDomainSocketAddress(path);
170+
}
171+
172+
/**
173+
* Returns this address's path.
174+
*
175+
* @return this address's path
176+
*/
177+
public Path getPath() {
178+
return path;
179+
}
180+
181+
/**
182+
* Returns the hash code of this {@code UnixDomainSocketAddress}
183+
*/
184+
@Override
185+
public int hashCode() {
186+
return path.hashCode();
187+
}
188+
189+
/**
190+
* Compares this address with another object.
191+
*
192+
* @return true if the path fields are equal
193+
*/
194+
@Override
195+
public boolean equals(Object o) {
196+
if (!(o instanceof UnixDomainSocketAddress))
197+
return false;
198+
UnixDomainSocketAddress that = (UnixDomainSocketAddress)o;
199+
return this.path.equals(that.path);
200+
}
201+
202+
/**
203+
* Returns a string representation of this {@code UnixDomainSocketAddress}.
204+
*
205+
* @return this address's path which may be empty for an unnamed address
206+
*/
207+
@Override
208+
public String toString() {
209+
return path.toString();
210+
}
211+
}

src/java.base/share/classes/java/nio/channels/DatagramChannel.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ protected DatagramChannel(SelectorProvider provider) {
150150
*
151151
* @throws IOException
152152
* If an I/O error occurs
153+
*
154+
* @see <a href="../../net/doc-files/net-properties.html#Ipv4IPv6">
155+
* java.net.preferIPv4Stack</a> system property
153156
*/
154157
public static DatagramChannel open() throws IOException {
155158
return SelectorProvider.provider().openDatagramChannel();
@@ -169,6 +172,9 @@ public static DatagramChannel open() throws IOException {
169172
* java.nio.channels.spi.SelectorProvider} object. The channel will not be
170173
* connected.
171174
*
175+
* @apiNote <a href="package-summary.html#unixdomain">Unix domain</a> sockets
176+
* are not supported by DatagramChannel.
177+
*
172178
* @param family
173179
* The protocol family
174180
*
@@ -182,6 +188,9 @@ public static DatagramChannel open() throws IOException {
182188
* @throws IOException
183189
* If an I/O error occurs
184190
*
191+
* @see <a href="../../net/doc-files/net-properties.html#Ipv4IPv6">
192+
* java.net.preferIPv4Stack</a> system property
193+
*
185194
* @since 1.7
186195
*/
187196
public static DatagramChannel open(ProtocolFamily family) throws IOException {
@@ -629,5 +638,4 @@ public final long write(ByteBuffer[] srcs) throws IOException {
629638
*/
630639
@Override
631640
public abstract SocketAddress getLocalAddress() throws IOException;
632-
633641
}

0 commit comments

Comments
 (0)