Skip to content

Commit

Permalink
net/gcoap: use common names to simplify use of DTLS at tranport layer
Browse files Browse the repository at this point in the history
  • Loading branch information
kb2ma authored and Aiman Ismail committed Aug 28, 2019
1 parent 96e3a68 commit 6c8f9cd
Showing 1 changed file with 45 additions and 63 deletions.
108 changes: 45 additions & 63 deletions sys/net/application_layer/gcoap/gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
#include "random.h"
#include "thread.h"

#ifdef MODULE_SOCK_DTLS
#include "net/sock/dtls.h"
#endif

#define ENABLE_DEBUG (0)
#include "debug.h"

Expand All @@ -44,11 +40,7 @@

/* Internal functions */
static void *_event_loop(void *arg);
#ifdef MODULE_SOCK_DTLS
static void _listen(sock_dtls_t *sock);
#else
static void _listen(sock_udp_t *sock);
#endif
static void _listen(coap_tl_sock_t *sock);
static ssize_t _well_known_core_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len,
sock_udp_ep_t *remote);
Expand All @@ -64,9 +56,9 @@ static void _find_obs_memo_resource(gcoap_observe_memo_t **memo,
const coap_resource_t *resource);

#ifdef MODULE_SOCK_DTLS
static ssize_t _dtls_send(sock_dtls_t *sock, const void *data, size_t len,
static ssize_t _tl_send(sock_dtls_t *sock, const void *data, size_t len,
const sock_udp_ep_t *remote);
static ssize_t _dtls_recv(sock_dtls_t *sock, void *data, size_t max_len,
static ssize_t _tl_recv(sock_dtls_t *sock, void *data, size_t max_len,
uint32_t timeout, sock_udp_ep_t *remote);
#endif

Expand Down Expand Up @@ -109,10 +101,15 @@ static gcoap_state_t _coap_state = {
static kernel_pid_t _pid = KERNEL_PID_UNDEF;
static char _msg_stack[GCOAP_STACK_SIZE];
static msg_t _msg_queue[GCOAP_MSG_QUEUE_SIZE];
static sock_udp_t _sock;
static sock_dtls_t _dtls_sock;

static ssize_t _dtls_send(sock_dtls_t *sock, const void *data, size_t len,
/* Use _tl_sock as the common name for the top-level sock object for either
* DTLS or non-DTLS context. _udp_sock is just a synonym for it in non-DTLS case.
* Use _tl_send() and _tl_recv() as the common names for send/recv functions. */
#ifdef MODULE_SOCK_DTLS
static sock_udp_t _udp_sock;
static sock_dtls_t _tl_sock;

static ssize_t _tl_send(sock_dtls_t *sock, const void *data, size_t len,
const sock_udp_ep_t *remote)
{
sock_dtls_session_t session;
Expand All @@ -129,7 +126,7 @@ static ssize_t _dtls_send(sock_dtls_t *sock, const void *data, size_t len,
return res;
}

static ssize_t _dtls_recv(sock_dtls_t *sock, void *data, size_t max_len,
static ssize_t _tl_recv(sock_dtls_t *sock, void *data, size_t max_len,
uint32_t timeout, sock_udp_ep_t *remote)
{
sock_dtls_session_t rcvd_session;
Expand All @@ -141,6 +138,23 @@ static ssize_t _dtls_recv(sock_dtls_t *sock, void *data, size_t max_len,
memcpy(remote, &rcvd_session.remote_ep, sizeof(sock_udp_ep_t));
return res;
}
#else
/* provide synonyms for structs and functions when no DTLS */
static sock_udp_t _tl_sock;
#define _udp_sock (_tl_sock)

static inline ssize_t _tl_send(sock_udp_t *sock, const void *data, size_t len,
const sock_udp_ep_t *remote)
{
return sock_udp_send(sock, data, len, remote);
}

static inline ssize_t _tl_recv(sock_udp_t *sock, void *data, size_t len,
uint32_t timeout, sock_udp_ep_t *remote)
{
return sock_udp_recv(sock, data, len, timeout, remote);
}
#endif

/* Event/Message loop for gcoap _pid thread. */
static void *_event_loop(void *arg)
Expand All @@ -156,7 +170,7 @@ static void *_event_loop(void *arg)
local.netif = SOCK_ADDR_ANY_NETIF;
local.port = GCOAP_PORT;

int res = sock_udp_create(&_sock, &local, NULL, 0);
int res = sock_udp_create(&_udp_sock, &local, NULL, 0);
if (res < 0) {
DEBUG("gcoap: cannot create sock: %d\n", res);
return 0;
Expand All @@ -169,12 +183,12 @@ static void *_event_loop(void *arg)
(void)queue_array;
(void)rcv_session;

res = sock_dtls_create(&_dtls_sock, &_sock, 0);
res = sock_dtls_create(&_tl_sock, &_udp_sock, 0);
if (res < 0) {
DEBUG("gcoap: cannot create dtls sock: %d\n", res);
return 0;
}
sock_dtls_init_server(&_dtls_sock, &queue, queue_array, 5);
sock_dtls_init_server(&_tl_sock, &queue, queue_array, 5);
#endif

while(1) {
Expand Down Expand Up @@ -204,16 +218,10 @@ static void *_event_loop(void *arg)
timeout = random_uint32_range(timeout, timeout + variance);
#endif

#ifdef MODULE_SOCK_DTLS
ssize_t bytes = _dtls_send(&_dtls_sock,
memo->msg.data.pdu_buf,
memo->msg.data.pdu_len,
&memo->remote_ep);
#else
ssize_t bytes = sock_udp_send(&_sock, memo->msg.data.pdu_buf,
memo->msg.data.pdu_len,
&memo->remote_ep);
#endif
ssize_t bytes = _tl_send(&_tl_sock,
memo->msg.data.pdu_buf,
memo->msg.data.pdu_len,
&memo->remote_ep);
if (bytes > 0) {
xtimer_set_msg(&memo->response_timer, timeout,
&memo->timeout_msg, _pid);
Expand All @@ -230,22 +238,14 @@ static void *_event_loop(void *arg)
}
}

#ifdef MODULE_SOCK_DTLS
_listen(&_dtls_sock);
#else
_listen(&_sock);
#endif
_listen(&_tl_sock);
}

return 0;
}

/* Listen for an incoming CoAP message. */
#ifdef MODULE_SOCK_DTLS
static void _listen(sock_dtls_t *sock)
#else
static void _listen(sock_udp_t *sock)
#endif
static void _listen(coap_tl_sock_t *sock)
{
coap_pkt_t pdu;
uint8_t buf[GCOAP_PDU_BUF_SIZE];
Expand All @@ -258,15 +258,9 @@ static void _listen(sock_udp_t *sock)
* request is outstanding, sock_udp_recv() is called here with limited
* waiting so the request's timeout can be handled in a timely manner in
* _event_loop(). */
#ifdef MODULE_SOCK_DTLS
ssize_t res = _dtls_recv(sock, buf, sizeof(buf),
open_reqs > 0 ? GCOAP_RECV_TIMEOUT : SOCK_NO_TIMEOUT,
&remote);
#else
ssize_t res = sock_udp_recv(sock, buf, sizeof(buf),
open_reqs > 0 ? GCOAP_RECV_TIMEOUT : SOCK_NO_TIMEOUT,
&remote);
#endif
ssize_t res = _tl_recv(sock, buf, sizeof(buf),
open_reqs > 0 ? GCOAP_RECV_TIMEOUT : SOCK_NO_TIMEOUT,
&remote);
if (res <= 0) {
#if ENABLE_DEBUG
if (res < 0 && res != -ETIMEDOUT) {
Expand Down Expand Up @@ -296,11 +290,7 @@ static void _listen(sock_udp_t *sock)
|| coap_get_type(&pdu) == COAP_TYPE_CON) {
size_t pdu_len = _handle_req(&pdu, buf, sizeof(buf), &remote);
if (pdu_len > 0) {
#ifdef MODULE_SOCK_DTLS
ssize_t bytes = _dtls_send(sock, buf, pdu_len, &remote);
#else
ssize_t bytes = sock_udp_send(sock, buf, pdu_len, &remote);
#endif
ssize_t bytes = _tl_send(sock, buf, pdu_len, &remote);
if (bytes <= 0) {
DEBUG("gcoap: send response failed: %d\n", (int)bytes);
}
Expand Down Expand Up @@ -879,11 +869,7 @@ size_t gcoap_req_send(const uint8_t *buf, size_t len,
}

/* Memos complete; send msg and start timer */
#ifdef MODULE_SOCK_DTLS
ssize_t res = _dtls_send(&_dtls_sock, buf, len, remote);
#else
ssize_t res = sock_udp_send(&_sock, buf, len, remote);
#endif
ssize_t res = _tl_send(&_tl_sock, buf, len, remote);

/* timeout may be zero for non-confirmable */
if ((memo != NULL) && (res > 0) && (timeout > 0)) {
Expand All @@ -898,7 +884,7 @@ size_t gcoap_req_send(const uint8_t *buf, size_t len,
msg_t mbox_msg;
mbox_msg.type = GCOAP_MSG_TYPE_INTR;
mbox_msg.content.value = 0;
if (mbox_try_put(&_sock.reg.mbox, &mbox_msg)) {
if (mbox_try_put(&_udp_sock.reg.mbox, &mbox_msg)) {
/* start response wait timer on the gcoap thread */
memo->timeout_msg.type = GCOAP_MSG_TYPE_TIMEOUT;
memo->timeout_msg.content.ptr = (char *)memo;
Expand Down Expand Up @@ -983,11 +969,7 @@ size_t gcoap_obs_send(const uint8_t *buf, size_t len,
_find_obs_memo_resource(&memo, resource);

if (memo) {
#ifdef MODULE_SOCK_DTLS
ssize_t bytes = _dtls_send(&_dtls_sock, buf, len, memo->observer);
#else
ssize_t bytes = sock_udp_send(&_sock, buf, len, memo->observer);
#endif
ssize_t bytes = _tl_send(&_tl_sock, buf, len, memo->observer);
return (size_t)((bytes > 0) ? bytes : 0);
}
else {
Expand Down

0 comments on commit 6c8f9cd

Please sign in to comment.