summaryrefslogtreecommitdiff
path: root/java/ssl/jdk1.5/IceSSL/AcceptorI.java
blob: ce4a1c804b75ddd504d8c73ec6e5980f94592246 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// **********************************************************************
//
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

package IceSSL;

final class AcceptorI implements IceInternal.Acceptor
{
    public java.nio.channels.ServerSocketChannel
    fd()
    {
	return _fd;
    }

    public void
    close()
    {
	if(_instance.networkTraceLevel() >= 1)
	{
	    String s = "stopping to accept ssl connections at " + toString();
	    _logger.trace(_instance.networkTraceCategory(), s);
	}

	java.nio.channels.ServerSocketChannel fd;
	java.nio.channels.Selector selector;
	synchronized(this)
	{
	    fd = _fd;
	    selector = _selector;
	    _fd = null;
	    _selector = null;
	}
	if(fd != null)
	{
	    try
	    {
		fd.close();
	    }
	    catch(java.io.IOException ex)
	    {
		// Ignore.
	    }
	}
	if(selector != null)
	{
	    try
	    {
		selector.close();
	    }
	    catch(java.io.IOException ex)
	    {
		// Ignore.
	    }
	}
    }

    public void
    listen()
    {
	// Nothing to do.

	if(_instance.networkTraceLevel() >= 1)
	{
	    String s = "accepting ssl connections at " + toString();
	    _logger.trace(_instance.networkTraceCategory(), s);
	}
    }

    public IceInternal.Transceiver
    accept(int timeout)
    {
	//
	// The plugin may not be fully initialized.
	//
	if(!_instance.initialized())
	{
	    Ice.PluginInitializationException ex = new Ice.PluginInitializationException();
	    ex.reason = "IceSSL: plugin is not initialized";
	    throw ex;
	}

	java.nio.channels.SocketChannel fd = null;
	while(fd == null)
	{
	    try
	    {
		fd = _fd.accept();
		if(fd == null)
		{
		    if(_selector == null)
		    {
			_selector = java.nio.channels.Selector.open();
		    }

		    while(true)
		    {
			try
			{
			    java.nio.channels.SelectionKey key =
				_fd.register(_selector, java.nio.channels.SelectionKey.OP_ACCEPT);
			    if(timeout > 0)
			    {
				if(_selector.select(timeout) == 0)
				{
				    throw new Ice.TimeoutException();
				}
			    }
			    else if(timeout == 0)
			    {
				if(_selector.selectNow() == 0)
				{
				    throw new Ice.TimeoutException();
				}
			    }
			    else
			    {
				_selector.select();
			    }

			    break;
			}
			catch(java.io.IOException ex)
			{
			    if(IceInternal.Network.interrupted(ex))
			    {
				continue;
			    }
			    Ice.SocketException se = new Ice.SocketException();
			    se.initCause(ex);
			    throw se;
			}
		    }
		}
	    }
	    catch(java.io.IOException ex)
	    {
		if(IceInternal.Network.interrupted(ex))
		{
		    continue;
		}
		Ice.SocketException se = new Ice.SocketException();
		se.initCause(ex);
		throw se;
	    }
	}

	//
	// Check whether this socket is the result of a call to connectToSelf.
	// Despite the fact that connectToSelf immediately closes the socket,
	// the server-side handshake process does not raise an exception.
	// Furthermore, we can't simply proceed with the regular handshake
	// process because we don't want to pass such a socket to the
	// certificate verifier (if any).
	//
	// In order to detect a call to connectToSelf, we compare the remote
	// address of the newly-accepted socket to that in _connectToSelfAddr.
	//
	java.net.SocketAddress remoteAddr = fd.socket().getRemoteSocketAddress();
	synchronized(this)
	{
	    if(remoteAddr.equals(_connectToSelfAddr))
	    {
		try
		{
		    fd.close();
		}
		catch(java.io.IOException e)
		{
		}
		return null;
	    }
	}

	javax.net.ssl.SSLEngine engine = null;
	try
	{
	    try
	    {
		java.net.Socket socket = fd.socket();
		socket.setTcpNoDelay(true);
		socket.setKeepAlive(true);
	    }
	    catch(java.io.IOException ex)
	    {
		Ice.SocketException se = new Ice.SocketException();
		se.initCause(ex);
		throw se;
	    }

	    IceInternal.Network.setBlock(fd, false);

	    engine = _instance.createSSLEngine(true);
	}
	catch(RuntimeException ex)
	{
	    try
	    {
		fd.close();
	    }
	    catch(java.io.IOException e)
	    {
		// Ignore.
	    }
	    throw ex;
	}

        if(_instance.networkTraceLevel() >= 1)
        {
            _logger.trace(_instance.networkTraceCategory(), "attempting to accept ssl connection\n" +
                          IceInternal.Network.fdToString(fd));
        }

	return new TransceiverI(_instance, engine, fd, "", true, _adapterName);
    }

    public void
    connectToSelf()
    {
	java.nio.channels.SocketChannel fd = IceInternal.Network.createTcpSocket();
	IceInternal.Network.setBlock(fd, false);
	synchronized(this)
	{
	    //
	    // connectToSelf is called to wake up the thread blocked in
	    // accept. We remember the originating address for use in
	    // accept. See accept for details.
	    //
	    IceInternal.Network.doConnect(fd, _addr, -1);
	    _connectToSelfAddr = (java.net.InetSocketAddress)fd.socket().getLocalSocketAddress();
	}
	IceInternal.Network.closeSocket(fd);
    }

    public String
    toString()
    {
	return IceInternal.Network.addrToString(_addr);
    }

    final boolean
    equivalent(String host, int port)
    {
	java.net.InetSocketAddress addr = IceInternal.Network.getAddress(host, port);
	return addr.equals(_addr);
    }

    int
    effectivePort()
    {
	return _addr.getPort();
    }

    AcceptorI(Instance instance, String adapterName, String host, int port)
    {
	_instance = instance;
	_adapterName = adapterName;
	_logger = instance.communicator().getLogger();
	_backlog = 0;

	if(_backlog <= 0)
	{
	    _backlog = 5;
	}

	try
	{
	    _fd = IceInternal.Network.createTcpServerSocket();
	    IceInternal.Network.setBlock(_fd, false);
	    _addr = new java.net.InetSocketAddress(host, port);
	    if(_instance.networkTraceLevel() >= 2)
	    {
		String s = "attempting to bind to ssl socket " + toString();
		_logger.trace(_instance.networkTraceCategory(), s);
	    }
	    _addr = IceInternal.Network.doBind(_fd, _addr);
	}
	catch(RuntimeException ex)
	{
	    _fd = null;
	    throw ex;
	}
    }

    protected synchronized void
    finalize()
	throws Throwable
    {
	IceUtil.Assert.FinalizerAssert(_fd == null);

	super.finalize();
    }

    private Instance _instance;
    private String _adapterName;
    private Ice.Logger _logger;
    private java.nio.channels.ServerSocketChannel _fd;
    private int _backlog;
    private java.net.InetSocketAddress _addr;
    private java.nio.channels.Selector _selector;
    private java.net.InetSocketAddress _connectToSelfAddr;
}