public NioWritableChannel(SocketChannel channel, NioEventLoop nioEventLoop) throws IOException { this.channel = channel; this.nioEventLoop = nioEventLoop; SocketAddress sa = channel.getRemoteAddress(); if (sa instanceof InetSocketAddress) { InetSocketAddress address = (InetSocketAddress) sa; host = address.getHostString(); port = address.getPort(); } else { host = ""; port = -1; } }
@Override public HandlerState call() throws Exception { SocketChannel client = this.server.accept(); LOG.info("Accepted connection from {}", client.getRemoteAddress()); try { new ProxySetupHandler(client, selector, config); } catch (IOException ioe) { client.close(); } return HandlerState.ACCEPTING; } }
@Override public String getSocketChannelRemoteAddress() { try { if(getChannel() != null) { return getChannel().getRemoteAddress().toString(); } } catch (IOException e) { log.error("Exception", e); } return "NULL"; }
public SSLSocketChannel(final SSLEngine sslEngine, final SocketChannel socketChannel) throws IOException { if (!socketChannel.isConnected()) { throw new IllegalArgumentException("Cannot pass an un-connected SocketChannel"); } this.channel = socketChannel; this.socketAddress = socketChannel.getRemoteAddress(); final Socket socket = socketChannel.socket(); this.hostname = socket.getInetAddress().getHostName(); this.port = socket.getPort(); // don't set useClientMode or needClientAuth, use the engine as is and let the caller configure it this.engine = sslEngine; streamInManager = new BufferStateManager(ByteBuffer.allocate(engine.getSession().getPacketBufferSize())); streamOutManager = new BufferStateManager(ByteBuffer.allocate(engine.getSession().getPacketBufferSize())); appDataManager = new BufferStateManager(ByteBuffer.allocate(engine.getSession().getApplicationBufferSize())); }
public SSLSocketChannel(final SSLContext sslContext, final SocketChannel socketChannel, final boolean client) throws IOException { if (!socketChannel.isConnected()) { throw new IllegalArgumentException("Cannot pass an un-connected SocketChannel"); } this.channel = socketChannel; this.socketAddress = socketChannel.getRemoteAddress(); final Socket socket = socketChannel.socket(); this.hostname = socket.getInetAddress().getHostName(); this.port = socket.getPort(); this.engine = sslContext.createSSLEngine(); this.engine.setUseClientMode(client); this.engine.setNeedClientAuth(true); streamInManager = new BufferStateManager(ByteBuffer.allocate(engine.getSession().getPacketBufferSize())); streamOutManager = new BufferStateManager(ByteBuffer.allocate(engine.getSession().getPacketBufferSize())); appDataManager = new BufferStateManager(ByteBuffer.allocate(engine.getSession().getApplicationBufferSize())); }
public String toString() { InetSocketAddress local=null, remote=null; try {local=channel != null? (InetSocketAddress)channel.getLocalAddress() : null;} catch(Throwable t) {} try {remote=channel != null? (InetSocketAddress)channel.getRemoteAddress() : null;} catch(Throwable t) {} String loc=local == null ? "n/a" : local.getHostString() + ":" + local.getPort(), rem=remote == null? "n/a" : remote.getHostString() + ":" + remote.getPort(); return String.format("<%s --> %s> (%d secs old) [%s] [recv_buf: %d, reader=%b]", loc, rem, TimeUnit.SECONDS.convert(getTimestamp() - last_access, TimeUnit.NANOSECONDS), status(), recv_buf.get(1) != null? recv_buf.get(1).capacity() : 0, readerRunning()); }
SocketChannel client_ch=ch.accept(); if(client_ch != null) { // accept() may return null... System.out.printf("accepted connection from %s\n", client_ch.getRemoteAddress()); client_ch.configureBlocking(false); client_ch.register(selector, SelectionKey.OP_READ, create(SIZE, direct));
@Override public void closeChannel(SocketChannel channel) { if (channel == null) { return; } try { InetSocketAddress inetSocketAddress = (InetSocketAddress) channel.getRemoteAddress(); removeConnection(inetSocketAddress); } catch (Exception e1) { } nioEventLoopAdapter.closeChannel(channel); } }
public boolean finishConnect() throws IOException { //we need to grab remoteAddr before finishConnect() is called otherwise //it becomes inaccessible if the connection was refused. SocketChannel socketChannel = transportLayer.socketChannel(); if (socketChannel != null) { remoteAddress = socketChannel.getRemoteAddress(); } boolean connected = transportLayer.finishConnect(); if (connected) { if (ready()) { state = ChannelState.READY; } else if (remoteAddress != null) { state = new ChannelState(ChannelState.State.AUTHENTICATE, remoteAddress.toString()); } else { state = ChannelState.AUTHENTICATE; } } return connected; }
@Override public HandlerState call() throws Exception { try { switch (this.state) { case CONNECTING: connect(); break; case WRITING: write(); break; case READING: read(); break; default: throw new IllegalStateException("ProxySetupHandler should not be in state " + this.state); } } catch (IOException ioe) { LOG.warn("Failed to establish a proxy connection for {}", this.client.getRemoteAddress(), ioe); closeChannels(); } return this.state; }
currentConnections.decrementAndGet(); logger.warn("Rejecting connection from {} because max connections has been met", new Object[]{ socketChannel.getRemoteAddress().toString() }); IOUtils.closeQuietly(socketChannel); continue; new Object[]{socketChannel.getRemoteAddress().toString()});
public NioConnection(SocketChannel channel, NioBaseServer server) throws Exception { this.channel=channel; this.server=server; setSocketParameters(this.channel.socket()); channel.configureBlocking(false); this.connected=channel.isConnected(); send_buf=new Buffers(server.maxSendBuffers() *2); // space for actual bufs and length bufs! this.peer_addr=server.usePeerConnections()? null /* read by first receive() */ : new IpAddress((InetSocketAddress)channel.getRemoteAddress()); last_access=getTimestamp(); // last time a message was sent or received (ns) }
final SocketAddress remoteSocketAddress = socketChannel.getRemoteAddress(); socketChannel.socket().setSoTimeout(socketReadTimeout); socketChannel.socket().setReceiveBufferSize(receiveBufferSize);
hash = localAddress.hashCode(); final SocketAddress remoteAddress = accepted.getRemoteAddress(); if (remoteAddress instanceof InetSocketAddress) { final InetSocketAddress address = (InetSocketAddress) remoteAddress;
hash = localAddress.hashCode(); final SocketAddress remoteAddress = accepted.getRemoteAddress(); if (remoteAddress instanceof InetSocketAddress) { final InetSocketAddress address = (InetSocketAddress) remoteAddress;
private void createIoFilter(SocketChannel channel, boolean clientSocket) throws IOException { if (getConduit().useSSL() && channel != null) { InetSocketAddress address = (InetSocketAddress) channel.getRemoteAddress(); SSLEngine engine = getConduit().getSocketCreator().createSSLEngine(address.getHostName(), address.getPort()); if (!clientSocket) { engine.setWantClientAuth(true); engine.setNeedClientAuth(true); } int packetBufferSize = engine.getSession().getPacketBufferSize(); if (inputBuffer == null || (inputBuffer.capacity() < packetBufferSize)) { // TLS has a minimum input buffer size constraint if (inputBuffer != null) { Buffers.releaseReceiveBuffer(inputBuffer, getConduit().getStats()); } inputBuffer = Buffers.acquireReceiveBuffer(packetBufferSize, getConduit().getStats()); } if (channel.socket().getReceiveBufferSize() < packetBufferSize) { channel.socket().setReceiveBufferSize(packetBufferSize); } if (channel.socket().getSendBufferSize() < packetBufferSize) { channel.socket().setSendBufferSize(packetBufferSize); } ioFilter = getConduit().getSocketCreator().handshakeSSLSocketChannel(channel, engine, getConduit().idleConnectionTimeout, clientSocket, inputBuffer, getConduit().getStats()); } else { ioFilter = new NioPlainEngine(); } }
short version=buf.getShort(); if(!Version.isBinaryCompatible(version)) throw new IOException("packet from " + channel.getRemoteAddress() + " has different version (" + Version.print(version) + ") from ours (" + Version.printVersion() + "); discarding it"); recv_buf.add(ByteBuffer.allocate(Global.SHORT_SIZE));
final long timeTaken = System.currentTimeMillis() - startTimeMills; if (timeTaken > 15000L) { s_logger.warn("SSL Handshake has taken more than 15s to connect to: " + socketChannel.getRemoteAddress() + ". Please investigate this connection."); return false;
} catch (final SSLException sslException) { s_logger.error(String.format("SSL error caught during wrap data: %s, for local address=%s, remote address=%s.", sslException.getMessage(), socketChannel.getLocalAddress(), socketChannel.getRemoteAddress())); sslEngine.closeOutbound(); return new HandshakeHolder(myAppData, myNetData, true);
@Override public void run() { _selector.wakeup(); try { sslEngine.beginHandshake(); if (!Link.doHandshake(socketChannel, sslEngine)) { throw new IOException("SSL handshake timed out with " + socketChannel.getRemoteAddress()); } if (s_logger.isTraceEnabled()) { s_logger.trace("SSL: Handshake done"); } final InetSocketAddress saddr = (InetSocketAddress)socket.getRemoteSocketAddress(); final Link link = new Link(saddr, nioConnection); link.setSSLEngine(sslEngine); link.setKey(socketChannel.register(key.selector(), SelectionKey.OP_READ, link)); final Task task = _factory.create(Task.Type.CONNECT, link, null); registerLink(saddr, link); _executor.submit(task); } catch (IOException e) { if (s_logger.isTraceEnabled()) { s_logger.trace("Connection closed due to failure: " + e.getMessage()); } closeAutoCloseable(socket, "accepting socket"); closeAutoCloseable(socketChannel, "accepting socketChannel"); } finally { _selector.wakeup(); } } });