Skip to content

Commit

Permalink
Fire SniCompletionEvent as soon as the SNI hostname was selected (jav…
Browse files Browse the repository at this point in the history
…a-native-access#644)

Motivation:

We did fire the SniCompletionEvent only once we were done with the
handshake which is not really the correct time to do this. We should
fire the event as soon as we were able to process the client hello and
selected the hostname.

Modifications:

- Fire the event as soon as we select the hostname

Result:

Fire the event at the correct time
  • Loading branch information
normanmaurer authored Jan 24, 2024
1 parent a9ce5e6 commit c8e1f81
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,6 @@ private void notifyAboutHandshakeCompletionIfNeeded(SSLHandshakeException cause)
case NOT_HANDSHAKING:
case FINISHED:
handshakeCompletionNotified = true;
String sniHostname = connection.engine().sniHostname;
if (sniHostname != null) {
connection.engine().sniHostname = null;
pipeline().fireUserEventTriggered(new SniCompletionEvent(sniHostname));
}
pipeline().fireUserEventTriggered(SslHandshakeCompletionEvent.SUCCESS);
break;
default:
Expand Down Expand Up @@ -279,7 +274,8 @@ void attachQuicheConnection(QuicheQuicConnection connection) {
this.traceId = new String(traceId);
}

connection.initInfo(local, remote);
connection.init(local, remote,
sniHostname -> pipeline().fireUserEventTriggered(new SniCompletionEvent(sniHostname)));

// Setup QLOG if needed.
QLogConfiguration configuration = config.getQLogConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.function.Consumer;
import java.util.function.Supplier;

final class QuicheQuicConnection {
Expand Down Expand Up @@ -178,7 +179,7 @@ long address() {
return connection;
}

void initInfo(InetSocketAddress local, InetSocketAddress remote) {
void init(InetSocketAddress local, InetSocketAddress remote, Consumer<String> sniSelectedCallback) {
assert connection != -1;
assert recvInfoBuffer.refCnt() != 0;
assert sendInfoBuffer.refCnt() != 0;
Expand All @@ -190,6 +191,7 @@ void initInfo(InetSocketAddress local, InetSocketAddress remote) {
// Fill both quiche_send_info structs with the same address.
QuicheSendInfo.setSendInfo(sendInfoBuffer1, local, remote);
QuicheSendInfo.setSendInfo(sendInfoBuffer2, local, remote);
engine.sniSelectedCallback = sniSelectedCallback;
}

ByteBuffer nextRecvInfo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.LongFunction;

final class QuicheQuicSslEngine extends QuicSslEngine {
Expand All @@ -54,7 +55,7 @@ final class QuicheQuicSslEngine extends QuicSslEngine {
final String tlsHostName;
volatile QuicheQuicConnection connection;

String sniHostname;
volatile Consumer<String> sniSelectedCallback;

QuicheQuicSslEngine(QuicheQuicSslContext ctx, String peerHost, int peerPort) {
this.ctx = ctx;
Expand All @@ -75,7 +76,10 @@ long moveTo(String hostname, QuicheQuicSslContext ctx) {
this.ctx.remove(this);
this.ctx = ctx;
long added = ctx.add(this);
sniHostname = hostname;
Consumer<String> sniSelectedCallback = this.sniSelectedCallback;
if (sniSelectedCallback != null) {
sniSelectedCallback.accept(hostname);
}
return added;
}

Expand Down

0 comments on commit c8e1f81

Please sign in to comment.