Skip to content

Commit 9acc940

Browse files
committed
refactor/dtls: Use message oriented send/receive for DTLS.
The protocol here needs to know and respect message boundaries.
1 parent fcd1adb commit 9acc940

File tree

9 files changed

+223
-74
lines changed

9 files changed

+223
-74
lines changed

src/sp/transport/dtls/dtls.c

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,6 @@ static void
215215
dtls_bio_recv_done(dtls_pipe *p)
216216
{
217217
nng_aio *aio;
218-
uint8_t *ptr;
219-
size_t resid;
220218
nni_msg *msg;
221219

222220
while ((!nni_lmq_empty(&p->rx_mq)) &&
@@ -225,22 +223,7 @@ dtls_bio_recv_done(dtls_pipe *p)
225223
nni_aio_list_remove(aio);
226224
nni_lmq_get(&p->rx_mq, &msg);
227225

228-
// assumption we only have a body, because we don't bother to
229-
// fill in the header for raw UDP.
230-
231-
resid = nni_msg_len(msg);
232-
ptr = nni_msg_body(msg);
233-
234-
for (unsigned i = 0; i < aio->a_nio && resid > 0; i++) {
235-
size_t num = resid > aio->a_iov[i].iov_len
236-
? aio->a_iov[i].iov_len
237-
: resid;
238-
memcpy(aio->a_iov[i].iov_buf, ptr, num);
239-
ptr += num;
240-
resid -= num;
241-
}
242-
nni_aio_finish(aio, NNG_OK, nni_msg_len(msg));
243-
nni_msg_free(msg);
226+
nni_aio_finish_msg(aio, msg);
244227
}
245228
}
246229

@@ -264,11 +247,19 @@ static void
264247
dtls_bio_send(void *arg, nng_aio *aio)
265248
{
266249
dtls_pipe *p = arg;
250+
nni_iov iov;
251+
nni_msg *msg;
267252

268253
nni_mtx_lock(&p->lower_mtx);
269254
if (!p->closed) {
270255
nni_aio_set_input(aio, 0, &p->peer_addr);
256+
msg = nni_aio_get_msg(aio);
257+
iov.iov_buf = nni_msg_body(msg);
258+
iov.iov_len = nni_msg_len(msg);
259+
nng_aio_set_iov(aio, 1, &iov);
271260
nng_udp_send(p->ep->udp, aio);
261+
} else {
262+
nni_aio_finish_error(aio, NNG_ECLOSED);
272263
}
273264
nni_mtx_unlock(&p->lower_mtx);
274265
}
@@ -766,7 +757,7 @@ dtls_pipe_alloc(dtls_ep *ep, dtls_pipe **pp, const nng_sockaddr *sa)
766757
p->recv_max = ep->rcvmax;
767758
*pp = p;
768759

769-
if (((rv = nni_tls_init(&p->tls, ep->tlscfg)) != NNG_OK) ||
760+
if (((rv = nni_tls_init(&p->tls, ep->tlscfg, true)) != NNG_OK) ||
770761
((rv = nni_tls_start(&p->tls, &dtls_bio_ops, p, sa)) != NNG_OK) ||
771762
((rv = dtls_add_pipe(ep, p)) != NNG_OK)) {
772763
nni_pipe_close(p->npipe);
@@ -919,6 +910,7 @@ dtls_add_pipe(dtls_ep *ep, dtls_pipe *p)
919910
id = 1;
920911
}
921912
}
913+
p->id = id;
922914
return (nni_id_set(&ep->pipes, id, p));
923915
}
924916

@@ -979,11 +971,12 @@ dtls_rx_cb(void *arg)
979971
}
980972
NNI_ASSERT(p != NULL);
981973

982-
if (nni_msg_alloc(&msg, nni_aio_count(aio)) != NNG_OK) {
974+
size_t len = nni_aio_count(aio);
975+
if (nni_msg_alloc(&msg, len) != NNG_OK) {
983976
// TODO BUMP A NO RECV ALLOC STAT
984977
goto fail;
985978
}
986-
memcpy(nni_msg_body(msg), ep->rx_buf, nni_aio_count(aio));
979+
memcpy(nni_msg_body(msg), ep->rx_buf, len);
987980
dtls_start_rx(ep);
988981
nni_pipe_hold(p->npipe);
989982
nni_mtx_unlock(&ep->mtx);
@@ -1405,13 +1398,18 @@ dtls_resolv_cb(void *arg)
14051398
ep->self_sa.s_family = ep->peer_sa.s_family;
14061399
}
14071400

1408-
if (ep->udp == NULL) {
1409-
if ((rv = nng_udp_open(&ep->udp, &ep->self_sa)) != NNG_OK) {
1410-
nni_aio_list_remove(aio);
1411-
nni_aio_finish_error(aio, rv);
1412-
nni_mtx_unlock(&ep->mtx);
1413-
return;
1414-
}
1401+
// Close the socket if it was open, because we need to
1402+
// start with a fresh port.
1403+
if (ep->udp != NULL) {
1404+
nng_udp_close(ep->udp);
1405+
ep->udp = NULL;
1406+
}
1407+
1408+
if ((rv = nng_udp_open(&ep->udp, &ep->self_sa)) != NNG_OK) {
1409+
nni_aio_list_remove(aio);
1410+
nni_aio_finish_error(aio, rv);
1411+
nni_mtx_unlock(&ep->mtx);
1412+
return;
14151413
}
14161414

14171415
if ((rv = dtls_pipe_alloc(ep, &p, &ep->peer_sa)) != NNG_OK) {

src/sp/transport/dtls/dtls_tran_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ test_dtls_reqrep_multi(void)
449449
nng_tls_config_free(c1);
450450
}
451451

452-
#define NCLIENT 3
452+
#define NCLIENT 10
453453
void
454454
test_dtls_pub_multi(void)
455455
{
@@ -493,7 +493,7 @@ test_dtls_pub_multi(void)
493493

494494
// send a bunch of messages - we're hoping that by serializing we won't
495495
// overwhelm the network.
496-
for (int i = 0; i < 100; i++) {
496+
for (int i = 0; i < 1000; i++) {
497497
size_t len = nng_random() % (sizeof(msg) - 1);
498498
memset(msg, 'a' + i % 26, sizeof(buf));
499499
msg[len] = 0;

src/supplemental/tls/mbedtls/mbedtls.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ tls_mk_err(int err)
211211
static int
212212
net_send(void *tls, const unsigned char *buf, size_t len)
213213
{
214-
size_t sz = len;
215-
int rv;
214+
size_t sz = len;
215+
nng_err rv;
216216

217217
rv = nng_tls_engine_send(tls, buf, &sz);
218218
switch (rv) {
@@ -228,8 +228,8 @@ net_send(void *tls, const unsigned char *buf, size_t len)
228228
static int
229229
net_recv(void *tls, unsigned char *buf, size_t len)
230230
{
231-
size_t sz = len;
232-
int rv;
231+
size_t sz = len;
232+
nng_err rv;
233233

234234
rv = nng_tls_engine_recv(tls, buf, &sz);
235235
switch (rv) {
@@ -993,7 +993,7 @@ tls_engine_init(void)
993993
#endif
994994
// Uncomment the following to have noisy debug from mbedTLS.
995995
// This may be useful when trying to debug failures.
996-
// mbedtls_debug_set_threshold(9);
996+
mbedtls_debug_set_threshold(1);
997997

998998
mbedtls_ssl_cookie_init(&mbed_ssl_cookie_ctx);
999999
rv = mbedtls_ssl_cookie_setup(&mbed_ssl_cookie_ctx, tls_random, NULL);

src/supplemental/tls/openssl/openssl.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "../../../core/list.h"
2626
#include "../../../core/strs.h"
2727
#include "../tls_engine.h"
28+
#include "nng/nng.h"
2829

2930
// library code for openssl
3031
static int ossl_libcode;
@@ -116,8 +117,8 @@ tls_log_err(const char *msgid, const char *context, int errnum)
116117
static int
117118
ossl_net_send(BIO *bio, const char *buf, size_t len, size_t *lenp)
118119
{
119-
void *ctx = BIO_get_data(bio);
120-
int rv;
120+
void *ctx = BIO_get_data(bio);
121+
nng_err rv;
121122

122123
switch (rv = nng_tls_engine_send(ctx, (const uint8_t *) buf, &len)) {
123124
case NNG_OK:
@@ -135,8 +136,8 @@ ossl_net_send(BIO *bio, const char *buf, size_t len, size_t *lenp)
135136
static int
136137
ossl_net_recv(BIO *bio, char *buf, size_t len, size_t *lenp)
137138
{
138-
void *ctx = BIO_get_data(bio);
139-
int rv;
139+
void *ctx = BIO_get_data(bio);
140+
nng_err rv;
140141

141142
switch (rv = nng_tls_engine_recv(ctx, (uint8_t *) buf, &len)) {
142143
case NNG_OK:
@@ -348,7 +349,8 @@ ossl_conn_handshake(nng_tls_engine_conn *ec)
348349

349350
rv = SSL_do_handshake(ec->ssl);
350351
if (rv == 1) {
351-
nng_log_debug("NNG-TLS-HS", "TLS handshake complete");
352+
nng_log_debug("NNG-TLS-HS", "TLS handshake complete %s",
353+
ec->mode == NNG_TLS_MODE_CLIENT ? "client" : "server");
352354
return (NNG_OK);
353355
}
354356
rv = SSL_get_error(ec->ssl, rv);

0 commit comments

Comments
 (0)