public ISocketChannelFactory getSocketChannelFactory() { if (config.isSslEnabled()) { return sslSocketFactory; } return PlainSocketChannelFactory.INSTANCE; }
@Override default boolean isOpen() { return getSocketChannel().isOpen(); } }
public static void transferBufferToChannel(ISocketChannel socketChannel, ByteBuffer requestBuffer) throws IOException { while (requestBuffer.hasRemaining()) { socketChannel.write(requestBuffer); } }
public static ISocketChannel establishReplicaConnection(INcApplicationContext appCtx, InetSocketAddress location) throws IOException { final SocketChannel socketChannel = SocketChannel.open(); NetworkUtil.configure(socketChannel); socketChannel.connect(location); // perform handshake in a non-blocking mode socketChannel.configureBlocking(false); final ISocketChannelFactory socketChannelFactory = appCtx.getServiceContext().getControllerService().getNetworkSecurityManager().getSocketChannelFactory(); final ISocketChannel clientChannel = socketChannelFactory.createClientChannel(socketChannel); if (clientChannel.requiresHandshake() && !clientChannel.handshake()) { throw new IllegalStateException("handshake failure"); } // switch to blocking mode after handshake success socketChannel.configureBlocking(true); return clientChannel; }
private void connectionReceived(SocketChannel channel) { final ISocketChannel socketChannel = socketChannelFactory.createServerChannel(channel); final PendingHandshakeConnection conn = new PendingHandshakeConnection(socketChannel, null, INCOMING); if (socketChannel.requiresHandshake()) { asyncHandshake(conn); } else { conn.handshakeSuccess = true; handshakeCompletedConnections.add(conn); } }
@Override public ServerSocket createServerSocket(int port) throws IOException { if (securityManager.getConfiguration().isSslEnabled()) { return securityManager.newSSLContext().getServerSocketFactory().createServerSocket(port); } return ServerSocketFactory.getDefault().createServerSocket(port); } }
private void socketConnected(InetSocketAddress remoteAddress, SocketChannel channel) { final ISocketChannel socketChannel = socketChannelFactory.createClientChannel(channel); final PendingHandshakeConnection conn = new PendingHandshakeConnection(socketChannel, remoteAddress, OUTGOING); if (socketChannel.requiresHandshake()) { asyncHandshake(conn); } else { conn.handshakeSuccess = true; handshakeCompletedConnections.add(conn); } }
private synchronized void closeLogReplicationChannel() { try { if (logRepChannel != null && logRepChannel.getSocketChannel().isOpen()) { ReplicationProtocol.sendGoodbye(logRepChannel); logRepChannel.close(); logRepChannel = null; } } catch (IOException e) { LOGGER.warn("Exception while closing socket", e); } }
public static IAsterixStateProxy registerRemoteObject(INetworkSecurityManager networkSecurityManager, int metadataCallbackPort) throws RemoteException { final RMIServerFactory serverSocketFactory = new RMIServerFactory(networkSecurityManager); final RMIClientFactory clientSocketFactory = new RMIClientFactory(networkSecurityManager.getConfiguration().isSslEnabled()); final IAsterixStateProxy stub = (IAsterixStateProxy) UnicastRemoteObject.exportObject(cc, metadataCallbackPort, clientSocketFactory, serverSocketFactory); LOGGER.info("Asterix Distributed State Proxy Bound"); return stub; }
public static void readBytes(ISocketChannel socketChannel, ByteBuffer byteBuffer, int length) throws IOException { byteBuffer.clear(); byteBuffer.limit(length); while (byteBuffer.remaining() > 0 && socketChannel.read(byteBuffer) > 0); if (byteBuffer.remaining() > 0) { throw new EOFException(); } byteBuffer.flip(); }
public NetworkSecurityManager(INetworkSecurityConfig config) { this.config = config; if (config.isSslEnabled()) { System.setProperty("javax.net.ssl.trustStore", config.getTrustStoreFile().getAbsolutePath()); } sslSocketFactory = new SslSocketChannelFactory(this); }
public synchronized void close() { key.cancel(); try { channel.close(); } catch (IOException e) { LOGGER.error(() -> "Error closing channel at: " + remoteAddress, e); } }
@Override public ISocketChannel createServerChannel(SocketChannel socketChannel) { final SSLEngine sslEngine = networkSecurityManager.newSSLEngine(); sslEngine.setUseClientMode(false); return new SslSocketChannel(socketChannel, sslEngine); }
private char[] getKeyStorePassword() { final String pass = config.getKeyStorePassword(); return pass == null || pass.isEmpty() ? null : pass.toCharArray(); } }
public static boolean isHealthy(ISocketChannel sc) { return sc != null && sc.getSocketChannel().isOpen() && sc.getSocketChannel().isConnected(); } }
private void accept() { SocketChannel channel = null; SelectionKey channelKey = null; try { channel = serverSocketChannel.accept(); register(channel); final ISocketChannel serverChannel = socketChannelFactory.createServerChannel(channel); channelKey = channel.register(selector, SelectionKey.OP_READ); if (serverChannel.requiresHandshake()) { asyncHandshake(serverChannel, null, channelKey); } else { connectionReceived(serverChannel, channelKey); } } catch (IOException e) { LOGGER.error("Failed to accept channel ", e); close(channelKey, channel); } }
@Override public ISocketChannel createClientChannel(SocketChannel socketChannel) { final SSLEngine sslEngine = networkSecurityManager.newSSLEngine(); sslEngine.setUseClientMode(true); return new SslSocketChannel(socketChannel, sslEngine); } }
public TCPConnection(TCPEndpoint endpoint, ISocketChannel channel, SelectionKey key, Selector selector, ConnectionType type) { this.endpoint = endpoint; this.channel = channel; this.key = key; this.selector = selector; this.type = type; remoteAddress = (InetSocketAddress) channel.getSocketChannel().socket().getRemoteSocketAddress(); }
public InetSocketAddress getLocalAddress() { return (InetSocketAddress) channel.getSocketChannel().socket().getLocalSocketAddress(); }
private void handleHandshakeCompletion(Boolean handshakeSuccess, ISocketChannel socketChannel, IPCHandle handle, SelectionKey channelKey) { if (handshakeSuccess) { if (handle == null) { connectionReceived(socketChannel, channelKey); } else { connectionEstablished(handle, channelKey, socketChannel); } } else { close(channelKey, socketChannel.getSocketChannel()); } }