|
1 /* |
|
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. |
|
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.io.FileDescriptor; |
|
29 import java.io.IOException; |
|
30 import java.net.ServerSocket; |
|
31 import java.net.SocketAddress; |
|
32 import java.net.SocketOption; |
|
33 import java.net.SocketTimeoutException; |
|
34 import java.net.StandardProtocolFamily; |
|
35 import java.net.StandardSocketOptions; |
|
36 import java.nio.channels.AlreadyBoundException; |
|
37 import java.nio.channels.AsynchronousCloseException; |
|
38 import java.nio.channels.ClosedChannelException; |
|
39 import java.nio.channels.IllegalBlockingModeException; |
|
40 import java.nio.channels.NotYetBoundException; |
|
41 import java.nio.channels.SelectionKey; |
|
42 import java.nio.channels.ServerSocketChannel; |
|
43 import java.nio.channels.SocketChannel; |
|
44 import java.nio.channels.UnixDomainSocketAddress; |
|
45 import java.nio.channels.spi.SelectorProvider; |
|
46 import java.util.Collections; |
|
47 import java.util.HashSet; |
|
48 import java.util.Objects; |
|
49 import java.util.Set; |
|
50 import java.util.concurrent.locks.ReentrantLock; |
|
51 |
|
52 import sun.net.NetHooks; |
|
53 import sun.net.ext.ExtendedSocketOptions; |
|
54 |
|
55 /** |
|
56 * An implementation of ServerSocketChannels |
|
57 */ |
|
58 |
|
59 public class UnixDomainServerSocketChannelImpl |
|
60 extends ServerSocketChannelImpl |
|
61 { |
|
62 static { |
|
63 // register with InheritedChannel mechanism so it can create instances |
|
64 // not yet sun.nio.ch.InheritedChannel.register(UnixDomainServerSocketChannelImpl::create); |
|
65 } |
|
66 |
|
67 public UnixDomainServerSocketChannelImpl(SelectorProvider sp) throws IOException { |
|
68 super(sp, Net.unixDomainSocket()); |
|
69 } |
|
70 |
|
71 public UnixDomainServerSocketChannelImpl(SelectorProvider sp, FileDescriptor fd, boolean bound) |
|
72 throws IOException |
|
73 { |
|
74 super(sp, fd); |
|
75 if (bound) { |
|
76 synchronized (stateLock) { |
|
77 localAddress = Net.localUnixAddress(fd); |
|
78 } |
|
79 } |
|
80 } |
|
81 |
|
82 @Override |
|
83 public ServerSocket socket() { |
|
84 throw new UnsupportedOperationException("socket not supported"); |
|
85 } |
|
86 |
|
87 @Override |
|
88 public <T> ServerSocketChannel setOption(SocketOption<T> name, T value) |
|
89 throws IOException |
|
90 { |
|
91 Objects.requireNonNull(name); |
|
92 if (!supportedOptions().contains(name)) |
|
93 throw new UnsupportedOperationException("'" + name + "' not supported"); |
|
94 if (!name.type().isInstance(value)) |
|
95 throw new IllegalArgumentException("Invalid value '" + value + "'"); |
|
96 |
|
97 synchronized (stateLock) { |
|
98 ensureOpen(); |
|
99 // no options that require special handling |
|
100 Net.setSocketOption(fd, Net.UNSPEC, name, value); |
|
101 return this; |
|
102 } |
|
103 } |
|
104 |
|
105 @Override |
|
106 @SuppressWarnings("unchecked") |
|
107 public <T> T getOption(SocketOption<T> name) |
|
108 throws IOException |
|
109 { |
|
110 Objects.requireNonNull(name); |
|
111 if (!supportedOptions().contains(name)) |
|
112 throw new UnsupportedOperationException("'" + name + "' not supported"); |
|
113 |
|
114 synchronized (stateLock) { |
|
115 ensureOpen(); |
|
116 // no options that require special handling |
|
117 return (T) Net.getSocketOption(fd, Net.UNSPEC, name); |
|
118 } |
|
119 } |
|
120 |
|
121 private static class DefaultOptionsHolder { |
|
122 static final Set<SocketOption<?>> defaultOptions = defaultOptions(); |
|
123 |
|
124 private static Set<SocketOption<?>> defaultOptions() { |
|
125 HashSet<SocketOption<?>> set = new HashSet<>(); |
|
126 set.add(StandardSocketOptions.SO_RCVBUF); |
|
127 set.addAll(ExtendedSocketOptions.serverSocketOptions()); |
|
128 return Collections.unmodifiableSet(set); |
|
129 } |
|
130 } |
|
131 |
|
132 @Override |
|
133 public final Set<SocketOption<?>> supportedOptions() { |
|
134 return DefaultOptionsHolder.defaultOptions; |
|
135 } |
|
136 |
|
137 @Override |
|
138 public ServerSocketChannel bind(SocketAddress local, int backlog) throws IOException { |
|
139 synchronized (stateLock) { |
|
140 ensureOpen(); |
|
141 if (localAddress != null) |
|
142 throw new AlreadyBoundException(); |
|
143 UnixDomainSocketAddress usa = Net.checkUnixAddress(local); |
|
144 Net.unixDomainBind(fd, usa); |
|
145 Net.listen(fd, backlog < 1 ? 50 : backlog); |
|
146 localAddress = Net.localUnixAddress(fd); |
|
147 } |
|
148 return this; |
|
149 } |
|
150 |
|
151 |
|
152 @Override |
|
153 int implAccept(FileDescriptor fd, FileDescriptor newfd, SocketAddress[] addrs) |
|
154 throws IOException |
|
155 { |
|
156 |
|
157 return Net.unixDomainAccept(this.fd, newfd, addrs); |
|
158 } |
|
159 |
|
160 @Override |
|
161 String getRevealedLocalAddressAsString(SocketAddress addr) { |
|
162 // TODO |
|
163 return addr.toString(); |
|
164 } |
|
165 |
|
166 @Override |
|
167 SocketAddress getRevealedLocalAddress(SocketAddress addr) { |
|
168 return addr; // TODO |
|
169 } |
|
170 |
|
171 SocketChannel finishAccept(FileDescriptor newfd, SocketAddress isa) |
|
172 throws IOException |
|
173 { |
|
174 try { |
|
175 // newly accepted socket is initially in blocking mode |
|
176 IOUtil.configureBlocking(newfd, true); |
|
177 return new UnixDomainSocketChannelImpl(provider(), newfd, isa); |
|
178 } catch (Exception e) { |
|
179 nd.close(newfd); |
|
180 throw e; |
|
181 } |
|
182 } |
|
183 |
|
184 /** |
|
185 * Returns the local address, or null if not bound |
|
186 */ |
|
187 SocketAddress localAddress() { |
|
188 synchronized (stateLock) { |
|
189 return localAddress; |
|
190 } |
|
191 } |
|
192 |
|
193 public String toString() { |
|
194 StringBuilder sb = new StringBuilder(); |
|
195 sb.append(this.getClass().getName()); |
|
196 sb.append('['); |
|
197 if (!isOpen()) { |
|
198 sb.append("closed"); |
|
199 } else { |
|
200 synchronized (stateLock) { |
|
201 UnixDomainSocketAddress addr = (UnixDomainSocketAddress)localAddress; |
|
202 if (addr == null) { |
|
203 sb.append("unbound"); |
|
204 } else { |
|
205 sb.append(getRevealedLocalAddressAsString(addr)); |
|
206 } |
|
207 } |
|
208 } |
|
209 sb.append(']'); |
|
210 return sb.toString(); |
|
211 } |
|
212 } |