net.c: moved DTLS-specific code to coap_send_impl()

coap_send_impl() now decides if outbound data is passed to coap_dtls_send()
or to the registered network_send callback function.

For coap_send_confirmed() this implies that retransmitted PDUs are
passed to coap_dtls_send() as well, causing retransmissions on the
DTLS layer when the session is established. As long as the DTLS handshake
is not complete, coap_dtls_send() caches the outbound application data
for this particular PDU hence avoiding duplicate messages.
This commit is contained in:
Olaf Bergmann committed 2016-03-29 15:34:34 +02:00
1 parent 42aa0d7833
commit 2633479bd7
1 file changed
+29 -31
+29 -31
View File
@@ -569,6 +569,11 @@ coap_send_ack(coap_context_t *context,
return result;
}
static int
is_dtls(const coap_endpoint_t *endpoint) {
return (endpoint->flags & COAP_ENDPOINT_DTLS) != 0;
}
#if defined(WITH_POSIX) || defined(WITH_CONTIKI)
static coap_tid_t
coap_send_impl(coap_context_t *context,
@@ -588,8 +593,29 @@ coap_send_impl(coap_context_t *context,
return COAP_DROPPED_RESPONSE;
}
bytes_written = context->network_send(context, local_interface, dst,
(unsigned char *)pdu->hdr, pdu->length);
/* Pass to DTLS module for sending if local_interface is for secure
* communication. */
if (is_dtls(local_interface)) {
struct coap_dtls_session_t *session;
session = coap_dtls_get_session(context, local_interface, dst);
if (session == NULL) {
coap_log(LOG_WARNING, "coap_send: no DTLS session available\n");
goto finish;
} else {
bytes_written = coap_dtls_send(context, session, pdu);
/* If coap_dtls_send() has returned a negative value we need to
* output a custom error message. Otherwise, we end this branch
* to create a valid transaction id. */
if (bytes_written < 0) {
coap_log(LOG_WARNING, "coap_send: error sending data over DTLS\n");
goto finish;
}
}
} else {
bytes_written = context->network_send(context, local_interface, dst,
(unsigned char *)pdu->hdr, pdu->length);
}
if (bytes_written >= 0) {
coap_transaction_id(dst, pdu, &id);
@@ -597,6 +623,7 @@ coap_send_impl(coap_context_t *context,
coap_log(LOG_CRIT, "coap_send_impl: %s\n", strerror(errno));
}
finish:
return id;
}
#endif /* WITH_POSIX */
@@ -632,41 +659,12 @@ coap_send_impl(coap_context_t *context,
}
#endif /* WITH_LWIP */
static int
is_dtls(const coap_endpoint_t *endpoint) {
return (endpoint->flags & COAP_ENDPOINT_DTLS) != 0;
}
coap_tid_t
coap_send(coap_context_t *context,
const coap_endpoint_t *local_interface,
const coap_address_t *dst,
coap_pdu_t *pdu) {
/* FIXME: the following DTLS-specific case should probably go into
* coap_send_impl() with proper treatment of CON messages. */
if (is_dtls(local_interface)) {
struct coap_dtls_session_t *session;
coap_tid_t id = COAP_INVALID_TID;
session = coap_dtls_get_session(context, local_interface, dst);
if (session == NULL) {
coap_log(LOG_WARNING, "coap_send: no DTLS session available\n");
return COAP_INVALID_TID;
} else {
int res = coap_dtls_send(context, session, pdu);
if (res < 0) {
coap_log(LOG_WARNING, "coap_send: error sending data over DTLS\n");
goto finish;
} else {
coap_transaction_id(dst, pdu, &id);
/* goto finish */
}
}
finish:
return id;
}
return coap_send_impl(context, local_interface, dst, pdu);
}