Skip to content

Commit

Permalink
Allow to access the SSLEngine for a QuicChannel (java-native-access#277)
Browse files Browse the repository at this point in the history
Motivation:

We should allow to access the SSLEngine for a QuicChannel as it may contain interesting infos for the user like the application protocol / ciphers etc

Modifications:

- Add sslEngine() method to QuicChannel
- Adjust testcase to check if we can access the engine and if it contains the application protocol

Result:

Be able to access the engine
  • Loading branch information
normanmaurer authored May 12, 2021
1 parent 975963b commit 211c8cc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/main/java/io/netty/incubator/codec/quic/QuicChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.Promise;

import javax.net.ssl.SSLEngine;
import java.net.SocketAddress;

/**
Expand Down Expand Up @@ -148,6 +149,13 @@ default ChannelPromise voidPromise() {
@Override
QuicChannelConfig config();

/**
* Returns the used {@link SSLEngine} or {@code null} if none is used (yet).
*
* @return the engine.
*/
SSLEngine sslEngine();

/**
* Returns the number of streams that can be created before stream creation will fail
* with {@link QuicError#STREAM_LIMIT} error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

import javax.net.ssl.SSLEngine;
import java.io.File;
import java.net.ConnectException;
import java.net.InetSocketAddress;
Expand All @@ -50,9 +51,7 @@
import java.nio.channels.AlreadyConnectedException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ConnectionPendingException;
import java.util.ArrayDeque;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
Expand Down Expand Up @@ -111,7 +110,7 @@ public void operationComplete(ChannelFuture future) {
private final TimeoutHandler timeoutHandler = new TimeoutHandler();
private final InetSocketAddress remote;

private QuicheQuicConnection connection;
private volatile QuicheQuicConnection connection;
private boolean inFireChannelReadCompleteQueue;
private boolean fireChannelReadCompletePending;
private ByteBuf finBuffer;
Expand Down Expand Up @@ -179,6 +178,12 @@ static QuicheQuicChannel forServer(Channel parent, ByteBuffer key, InetSocketAdd
streamHandler, streamOptionsArray, streamAttrsArray);
}

@Override
public SSLEngine sslEngine() {
QuicheQuicConnection connection = this.connection;
return connection == null ? null : connection.engine();
}

@Override
public long peerAllowedStreams(QuicStreamType type) {
switch (type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

final class QuicheQuicConnection {
private final ReferenceCounted refCnt;
private final QuicheQuicSslEngine engine;
private long connection;

QuicheQuicConnection(long connection, ReferenceCounted refCnt) {
QuicheQuicConnection(long connection, QuicheQuicSslEngine engine, ReferenceCounted refCnt) {
this.connection = connection;
this.engine = engine;
this.refCnt = refCnt;
}

Expand All @@ -39,6 +41,10 @@ void free() {
}
}

QuicheQuicSslEngine engine() {
return engine;
}

long address() {
assert connection != -1;
return connection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ QuicheQuicConnection createConnection(LongFunction<Long> connectionCreator, Quic
return null;
}
// The connection will call nativeSslContext.release() once it is freed.
return new QuicheQuicConnection(connection, nativeSslContext);
return new QuicheQuicConnection(connection, engine, nativeSslContext);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ public int maxTokenLength() {
stream.writeAndFlush(Unpooled.directBuffer().writeZero(numBytes)).sync();
clientLatch.await();

assertEquals(QuicTestUtils.PROTOS[0], quicChannel.sslEngine().getApplicationProtocol());
stream.close().sync();
quicChannel.close().sync();
ChannelFuture closeFuture = quicChannel.closeFuture().await();
Expand Down

0 comments on commit 211c8cc

Please sign in to comment.