Skip to content

Commit

Permalink
API: add lsquic_conn_get_sni(), fixes issue #203
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitri Tikhonov committed Jan 11, 2021
1 parent e2c4907 commit 38e8359
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bin/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ static lsquic_conn_ctx_t *
http_server_on_new_conn (void *stream_if_ctx, lsquic_conn_t *conn)
{
struct server_ctx *server_ctx = stream_if_ctx;
const char *sni;

sni = lsquic_conn_get_sni(conn);
LSQ_DEBUG("new connection, SNI: %s", sni ? sni : "<not set>");

lsquic_conn_ctx_t *conn_h = malloc(sizeof(*conn_h));
conn_h->conn = conn;
conn_h->server_ctx = server_ctx;
Expand Down
4 changes: 4 additions & 0 deletions docs/apiref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1964,6 +1964,10 @@ Miscellaneous Connection Functions

Get peer context associated with the connection and local address.

.. function:: const char * lsquic_conn_get_sni (lsquic_conn_t *conn)

Get SNI sent by the client.

.. function:: enum LSQUIC_CONN_STATUS lsquic_conn_status (lsquic_conn_t *conn, char *errbuf, size_t bufsz)

Get connection status.
Expand Down
4 changes: 4 additions & 0 deletions include/lsquic.h
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,10 @@ lsquic_conn_set_ctx (lsquic_conn_t *, lsquic_conn_ctx_t *);
void *
lsquic_conn_get_peer_ctx (lsquic_conn_t *, const struct sockaddr *local_sa);

/** Get SNI sent by the client */
const char *
lsquic_conn_get_sni (lsquic_conn_t *);

/**
* Abort connection.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/liblsquic/lsquic_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,13 @@ lsquic_conn_stats_diff (const struct conn_stats *cumulative_stats,


#endif


const char *
lsquic_conn_get_sni (struct lsquic_conn *lconn)
{
if (lconn->cn_esf_c && lconn->cn_esf_c->esf_get_sni)
return lconn->cn_esf_c->esf_get_sni(lconn->cn_enc_session);
else
return NULL;
}
3 changes: 3 additions & 0 deletions src/liblsquic/lsquic_enc_sess.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ struct enc_session_funcs_common
int
(*esf_alg_keysize) (enc_session_t *);

const char *
(*esf_get_sni) (enc_session_t *);

/* Need to pass lconn in encrypt and decrypt methods because enc_session
* is allowed to be NULL for gQUIC.
*/
Expand Down
17 changes: 17 additions & 0 deletions src/liblsquic/lsquic_enc_sess_ietf.c
Original file line number Diff line number Diff line change
Expand Up @@ -2410,6 +2410,21 @@ iquic_esf_decrypt_packet (enc_session_t *enc_session_p,
}


static const char *
iquic_esf_get_sni (enc_session_t *enc_session_p)
{
struct enc_sess_iquic *const enc_sess = enc_session_p;
const char *server_name;

server_name = SSL_get_servername(enc_sess->esi_ssl, TLSEXT_NAMETYPE_host_name);
#ifndef NDEBUG
if (!server_name)
server_name = enc_sess->esi_sni_bypass;
#endif
return server_name;
}


static int
iquic_esf_global_init (int flags)
{
Expand Down Expand Up @@ -2669,6 +2684,7 @@ const struct enc_session_funcs_common lsquic_enc_session_common_ietf_v1 =
.esf_tag_len = IQUIC_TAG_LEN,
.esf_get_server_cert_chain
= iquic_esf_get_server_cert_chain,
.esf_get_sni = iquic_esf_get_sni,
.esf_cipher = iquic_esf_cipher,
.esf_keysize = iquic_esf_keysize,
.esf_alg_keysize = iquic_esf_alg_keysize,
Expand All @@ -2687,6 +2703,7 @@ const struct enc_session_funcs_common lsquic_enc_session_common_ietf_v1_no_flush
.esf_tag_len = IQUIC_TAG_LEN,
.esf_get_server_cert_chain
= iquic_esf_get_server_cert_chain,
.esf_get_sni = iquic_esf_get_sni,
.esf_cipher = iquic_esf_cipher,
.esf_keysize = iquic_esf_keysize,
.esf_alg_keysize = iquic_esf_alg_keysize,
Expand Down
10 changes: 10 additions & 0 deletions src/liblsquic/lsquic_handshake.c
Original file line number Diff line number Diff line change
Expand Up @@ -3502,6 +3502,14 @@ lsquic_enc_session_get_ua (enc_session_t *enc_session_p)
}


static const char *
lsquic_enc_session_get_sni (enc_session_t *enc_session_p)
{
struct lsquic_enc_session *const enc_session = enc_session_p;
return lsquic_str_cstr(&enc_session->hs_ctx.sni);
}


#ifndef NDEBUG
static uint8_t
lsquic_enc_session_have_key (enc_session_t *enc_session_p)
Expand Down Expand Up @@ -3819,6 +3827,7 @@ struct enc_session_funcs_common lsquic_enc_session_common_gquic_1 =
.esf_cipher = lsquic_enc_session_cipher,
.esf_keysize = lsquic_enc_session_keysize,
.esf_alg_keysize = lsquic_enc_session_alg_keysize,
.esf_get_sni = lsquic_enc_session_get_sni,
.esf_encrypt_packet = gquic_encrypt_packet,
.esf_decrypt_packet = gquic_decrypt_packet,
.esf_tag_len = GQUIC_PACKET_HASH_SZ,
Expand Down Expand Up @@ -4246,6 +4255,7 @@ const
/* Q050 and later */
struct enc_session_funcs_common lsquic_enc_session_common_gquic_2 =
{
.esf_get_sni = lsquic_enc_session_get_sni,
.esf_global_init = lsquic_handshake_init,
.esf_global_cleanup = lsquic_handshake_cleanup,
.esf_cipher = lsquic_enc_session_cipher,
Expand Down

0 comments on commit 38e8359

Please sign in to comment.