Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dtls: add single connection mode #643

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/re_tls.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ const struct sa *dtls_peer(const struct tls_conn *tc);
void dtls_set_peer(struct tls_conn *tc, const struct sa *peer);
void dtls_recv_packet(struct dtls_sock *sock, const struct sa *src,
struct mbuf *mb);
void dtls_set_single(struct dtls_sock *sock, bool single);


struct x509_st;
Expand Down
43 changes: 43 additions & 0 deletions src/tls/openssl/tls_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct dtls_sock {
dtls_conn_h *connh;
void *arg;
size_t mtu;
bool single_conn; /* If enabled, only one DTLS connection */
};


Expand Down Expand Up @@ -90,6 +91,21 @@ static const char *content_type_str(enum content_type content_type)
#endif


static bool first_handler(struct le *le, void *arg)
{
(void)le;
(void)arg;

return true; /* stop on the first element */
}


static struct le *hash_get_first(const struct hash *ht)
{
return hash_apply(ht, first_handler, NULL);
}


static int bio_create(BIO *b)
{
BIO_set_init(b, 1);
Expand Down Expand Up @@ -439,6 +455,13 @@ static int conn_alloc(struct tls_conn **ptc, struct tls *tls,
struct tls_conn *tc;
int err = 0;

if (sock->single_conn) {
if (hash_get_first(sock->ht)) {
DEBUG_WARNING("single: only one connection allowed\n");
return EMFILE;
}
}

tc = mem_zalloc(sizeof(*tc), conn_destructor);
if (!tc)
return ENOMEM;
Expand Down Expand Up @@ -708,6 +731,11 @@ static bool cmp_handler(struct le *le, void *arg)
static struct tls_conn *conn_lookup(struct dtls_sock *sock,
const struct sa *peer)
{
if (sock->single_conn) {
struct le *le = hash_get_first(sock->ht);
return list_ledata(le);
}

return list_ledata(hash_lookup(sock->ht, sa_hash(peer, SA_ALL),
cmp_handler, (void *)peer));
}
Expand Down Expand Up @@ -846,3 +874,18 @@ void dtls_recv_packet(struct dtls_sock *sock, const struct sa *src,

recv_handler(&addr, mb, sock);
}


/**
* Set single connection mode. If enabled, only one DTLS connection is allowed.
*
* @param sock DTLS Socket
* @param single True to enable, False to disable
*/
void dtls_set_single(struct dtls_sock *sock, bool single)
{
if (!sock)
return;

sock->single_conn = single;
}