/// <summary> /// Consumes the test server's connection backlog by spamming non-blocking /// SocketChannel client connections. /// </summary> /// <remarks> /// Consumes the test server's connection backlog by spamming non-blocking /// SocketChannel client connections. We never do anything with these sockets /// beyond just initiaing the connections. The method saves a reference to each /// new SocketChannel so that it can be closed during tearDown. We define a /// very small connection backlog, but the OS may silently enforce a larger /// minimum backlog than requested. To work around this, we create far more /// client connections than our defined backlog. /// </remarks> /// <exception cref="System.IO.IOException">thrown for any I/O error</exception> private void ConsumeConnectionBacklog() { for (int i = 0; i < ClientsToConsumeBacklog; ++i) { SocketChannel client = SocketChannel.Open(); client.ConfigureBlocking(false); client.Connect(nnHttpAddress); clients.AddItem(client); } }
public TcpTunnel(InetSocketAddress serverAddress, Selector selector, short portKey) { SocketChannel innerChannel = SocketChannel.Open(); innerChannel.ConfigureBlocking(false); mInnerChannel = innerChannel; mSelector = selector; mServerEP = serverAddress; this.portKey = portKey; sessionCount++; }
private void InitializeConnection(Java.Lang.String ipAndPort, InetAddress destinationAddress, int destinationPort, Packet currentPacket, TCPHeader tcpHeader, ByteBuffer responseBuffer) { currentPacket.SwapSourceAndDestination(); if (tcpHeader.isSYN()) { SocketChannel outputChannel = SocketChannel.Open(); outputChannel.ConfigureBlocking(false); vpnService.Protect(outputChannel.Socket()); TCB tcb = new TCB(ipAndPort, random.NextInt(Short.MaxValue + 1), tcpHeader.sequenceNumber, tcpHeader.sequenceNumber + 1, tcpHeader.acknowledgementNumber, outputChannel, currentPacket); TCB.PutTCB(ipAndPort, tcb); try { outputChannel.Connect(new InetSocketAddress(destinationAddress, destinationPort)); if (outputChannel.FinishConnect()) { tcb.status = TCB.TCBStatus.SYN_RECEIVED; // TODO: Set MSS for receiving larger packets from the device currentPacket.updateTCPBuffer(responseBuffer, (byte)(TCPHeader.SYN | TCPHeader.ACK), tcb.mySequenceNum, tcb.myAcknowledgementNum, 0); tcb.mySequenceNum++; // SYN counts as a byte } else { tcb.status = TCB.TCBStatus.SYN_SENT; selector.Wakeup(); tcb.selectionKey = outputChannel.Register(selector, SelectionKey.OpConnect, tcb); return; } } catch (IOException e) { Log.Error(TAG, "Connection error: " + ipAndPort, e); currentPacket.updateTCPBuffer(responseBuffer, (byte)TCPHeader.RST, 0, tcb.myAcknowledgementNum, 0); TCB.CloseTCB(tcb); } } else { currentPacket.updateTCPBuffer(responseBuffer, (byte)TCPHeader.RST, 0, tcpHeader.sequenceNumber + 1, 0); } outputQueue.Offer(responseBuffer); }
/// <exception cref="System.IO.IOException"/> public override Socket CreateSocket() { /* * NOTE: This returns an NIO socket so that it has an associated * SocketChannel. As of now, this unfortunately makes streams returned * by Socket.getInputStream() and Socket.getOutputStream() unusable * (because a blocking read on input stream blocks write on output stream * and vice versa). * * So users of these socket factories should use * NetUtils.getInputStream(socket) and * NetUtils.getOutputStream(socket) instead. * * A solution for hiding from this from user is to write a * 'FilterSocket' on the lines of FilterInputStream and extend it by * overriding getInputStream() and getOutputStream(). */ return(SocketChannel.Open().Socket()); }