layers: Add in protocol layers support

Current stack:-

Application
CoAP
CoAP-Session
DTLS
Netif
Sockets
Network Stack

Depending on the CoAP protocol, indexing functions through coap_layers_coap[]
(in src/coap_io.c) will invoke the correct layer function when used at the
CoAP and DTLS layers.

It is then easy to add in another protocol layer by updating coap_layers_coap[].
This commit is contained in:
Jon Shallow authored and Jon Shallow committed 2023-03-20 10:49:02 +00:00
1 parent d06d701038
commit adb8a3f5e2
14 files changed
+308 -280

No files matched your search

+10 -12
View File
@@ -19,7 +19,6 @@
void
coap_dtls_establish(coap_session_t *session) {
assert(session->proto == COAP_PROTO_DTLS);
#if COAP_CLIENT_SUPPORT
if (session->type == COAP_SESSION_TYPE_CLIENT)
session->tls = coap_dtls_new_client_session(session);
@@ -36,15 +35,15 @@ coap_dtls_establish(coap_session_t *session) {
return;
}
coap_ticks(&session->last_rx_tx);
return;
}
void
coap_dtls_close(coap_session_t *session) {
if (session->tls)
if (session->tls) {
coap_dtls_free_session(session);
if (coap_netif_available(session))
coap_netif_close(session);
session->tls = NULL;
}
session->sock.lfunc[COAP_LAYER_TLS].close(session);
}
#if !COAP_DISABLE_TCP
@@ -52,7 +51,6 @@ void
coap_tls_establish(coap_session_t *session) {
int connected = 0;
assert(session->proto == COAP_PROTO_TLS);
#if COAP_CLIENT_SUPPORT
if (session->type == COAP_SESSION_TYPE_CLIENT)
session->tls = coap_tls_new_client_session(session, &connected);
@@ -66,21 +64,21 @@ coap_tls_establish(coap_session_t *session) {
session->state = COAP_SESSION_STATE_HANDSHAKE;
if (connected) {
coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session);
coap_session_establish(session);
session->sock.lfunc[COAP_LAYER_TLS].establish(session);
}
} else {
coap_session_disconnected(session, COAP_NACK_TLS_LAYER_FAILED);
return;
}
coap_ticks(&session->last_rx_tx);
return;
}
void
coap_tls_close(coap_session_t *session) {
if (session->tls)
if (session->tls) {
coap_tls_free_session(session);
if (coap_netif_available(session))
coap_netif_close(session);
session->tls = NULL;
}
session->sock.lfunc[COAP_LAYER_TLS].close(session);
}
#endif /* COAP_DISABLE_TCP */
#endif /* !COAP_DISABLE_TCP */
+8 -7
View File
@@ -1995,7 +1995,8 @@ coap_dgram_write(gnutls_transport_ptr_t context, const void *send_buffer,
errno = ECONNRESET;
return -1;
}
result = (int)coap_netif_dgrm_write(c_session, send_buffer, send_buffer_length);
result = c_session->sock.lfunc[COAP_LAYER_TLS].write(c_session,
send_buffer, send_buffer_length);
if (result != (int)send_buffer_length) {
coap_log_warn("coap_netif_dgrm_write failed (%zd != %zu)\n",
result, send_buffer_length);
@@ -2478,7 +2479,7 @@ coap_dtls_receive(coap_session_t *c_session, const uint8_t *data,
coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED,
c_session);
gnutls_transport_set_ptr(g_env->g_session, c_session);
coap_session_establish(c_session);
c_session->sock.lfunc[COAP_LAYER_TLS].establish(c_session);
}
ret = gnutls_record_recv(g_env->g_session, pdu, (int)sizeof(pdu));
if (ret > 0) {
@@ -2652,7 +2653,7 @@ coap_sock_read(gnutls_transport_ptr_t context, void *out, size_t outl) {
coap_session_t *c_session = (coap_session_t *)context;
if (out != NULL) {
ret = (int)coap_netif_strm_read(c_session, out, outl);
ret = (int)c_session->sock.lfunc[COAP_LAYER_TLS].read(c_session, out, outl);
/* Translate layer returns into what GnuTLS expects */
if (ret == 0) {
errno = EAGAIN;
@@ -2673,7 +2674,7 @@ coap_sock_write(gnutls_transport_ptr_t context, const void *in, size_t inl) {
int ret = 0;
coap_session_t *c_session = (coap_session_t *)context;
ret = (int)coap_netif_strm_write(c_session, in, inl);
ret = (int)c_session->sock.lfunc[COAP_LAYER_TLS].write(c_session, in, inl);
/* Translate layer what returns into what GnuTLS expects */
if (ret < 0) {
if ((c_session->state == COAP_SESSION_STATE_CSM ||
@@ -2742,7 +2743,7 @@ coap_tls_new_client_session(coap_session_t *c_session, int *connected) {
if (ret == 1) {
*connected = 1;
coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session);
coap_session_establish(c_session);
c_session->sock.lfunc[COAP_LAYER_TLS].establish(c_session);
}
return g_env;
@@ -2854,7 +2855,7 @@ coap_tls_write(coap_session_t *c_session, const uint8_t *data,
if (ret == 1) {
coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED,
c_session);
coap_session_establish(c_session);
c_session->sock.lfunc[COAP_LAYER_TLS].establish(c_session);
} else {
ret = -1;
}
@@ -2899,7 +2900,7 @@ coap_tls_read(coap_session_t *c_session, uint8_t *data, size_t data_len) {
if (ret == 1) {
coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED,
c_session);
coap_session_establish(c_session);
c_session->sock.lfunc[COAP_LAYER_TLS].establish(c_session);
}
}
if (c_session->state != COAP_SESSION_STATE_NONE && g_env->established) {
+55
View File
@@ -74,6 +74,61 @@
#endif /* IPV6_RECVPKTINFO */
#endif /* !(WITH_CONTIKI || RIOT_VERSION) */
/*
* Layer index table. A whole protocol chunk gets copied into coap_socket_t.
* Each layer invokes the function defined at its layer to get the next layer
* (which could be above or below) to complete.
*
* The stack layers are (* managed by libcoap)
* Application
* CoAP *
* CoAP-Session *
* DTLS *
* Netif *
* Sockets *
* Network Stack
*
* dgrm read currently handled separately.
* strm read works down the layers.
* write works down the layers.
* establish is done after netif accept/connect completes by invoking SESSION
* and then works up the layers.
* close works down the layers
*/
coap_layer_func_t coap_layers_coap[COAP_PROTO_LAST][COAP_LAYER_LAST] = {
{ /* COAP_PROTO_NONE */
{ NULL, NULL, NULL, NULL }, /* SESSION */
{ NULL, NULL, NULL, NULL } /* TLS */
},
{ /* COAP_PROTO_UDP */
{ NULL, coap_netif_dgrm_write, coap_session_establish, coap_netif_close }, /* SESSION */
{ NULL, NULL, NULL, NULL } /* TLS */
},
{ /* COAP_PROTO_DTLS */
{ NULL, coap_dtls_send, coap_dtls_establish, coap_dtls_close }, /* SESSION */
{ NULL, coap_netif_dgrm_write, coap_session_establish, coap_netif_close } /* TLS */
},
#if !COAP_DISABLE_TCP
{ /* COAP_PROTO_TCP */
{ coap_netif_strm_read, coap_netif_strm_write, coap_session_establish, coap_netif_close }, /* SESSION */
{ NULL, NULL, NULL, NULL } /* TLS */
},
{ /* COAP_PROTO_TLS */
{ coap_tls_read, coap_tls_write, coap_tls_establish, coap_tls_close }, /* SESSION */
{ coap_netif_strm_read, coap_netif_strm_write, coap_session_establish, coap_netif_close } /* TLS */
},
#else /* COAP_DISABLE_TCP */
{ /* COAP_PROTO_TCP */
{ NULL, NULL, NULL, NULL }, /* SESSION */
{ NULL, NULL, NULL, NULL } /* TLS */
},
{ /* COAP_PROTO_TLS */
{ NULL, NULL, NULL, NULL }, /* SESSION */
{ NULL, NULL, NULL, NULL } /* TLS */
},
#endif /* COAP_DISABLE_TCP */
};
#if COAP_SERVER_SUPPORT
coap_endpoint_t *
coap_malloc_endpoint(void) {
+11 -10
View File
@@ -229,8 +229,8 @@ coap_dgram_write(void *ctx, const unsigned char *send_buffer,
errno = ECONNRESET;
return -1;
}
result = (int)coap_netif_dgrm_write(c_session,
send_buffer, send_buffer_length);
result = (int)c_session->sock.lfunc[COAP_LAYER_TLS].write(c_session,
send_buffer, send_buffer_length);
if (result != (ssize_t)send_buffer_length) {
coap_log_warn("coap_netif_dgrm_write failed (%zd != %zu)\n",
result, send_buffer_length);
@@ -1411,7 +1411,7 @@ coap_sock_read(void *ctx, unsigned char *out, size_t outl) {
coap_session_t *c_session = (coap_session_t *)ctx;
if (out != NULL) {
ret = (int)coap_netif_strm_read(c_session, out, outl);
ret = (int)c_session->sock.lfunc[COAP_LAYER_TLS].read(c_session, out, outl);
/* Translate layer returns into what MbedTLS expects */
if (ret == -1) {
if (errno == ECONNRESET) {
@@ -1439,7 +1439,9 @@ coap_sock_write(void *context, const unsigned char *in, size_t inl) {
int ret = 0;
coap_session_t *c_session = (coap_session_t *)context;
ret = (int)coap_netif_strm_write(c_session, (const uint8_t *)in, inl);
ret = c_session->sock.lfunc[COAP_LAYER_TLS].write(c_session,
(const uint8_t *)in,
inl);
/* Translate layer what returns into what MbedTLS expects */
if (ret < 0) {
if ((c_session->state == COAP_SESSION_STATE_CSM ||
@@ -2062,7 +2064,7 @@ coap_dtls_receive(coap_session_t *c_session,
if (c_session->state == COAP_SESSION_STATE_HANDSHAKE) {
coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED,
c_session);
coap_session_establish(c_session);
c_session->sock.lfunc[COAP_LAYER_TLS].establish(c_session);
}
ret = mbedtls_ssl_read(&m_env->ssl, pdu, sizeof(pdu));
@@ -2255,7 +2257,7 @@ void *coap_tls_new_client_session(coap_session_t *c_session,
if (ret == 1) {
*connected = 1;
coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, c_session);
coap_session_establish(c_session);
c_session->sock.lfunc[COAP_LAYER_TLS].establish(c_session);
}
return m_env;
#endif /* MBEDTLS_SSL_CLI_C */
@@ -2357,9 +2359,8 @@ coap_tls_write(coap_session_t *c_session, const uint8_t *data,
if (ret == 1) {
coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED,
c_session);
coap_session_establish(c_session);
}
else {
c_session->sock.lfunc[COAP_LAYER_TLS].establish(c_session);
} else {
ret = -1;
}
}
@@ -2405,7 +2406,7 @@ coap_tls_read(coap_session_t *c_session, uint8_t *data, size_t data_len)
if (ret == 1) {
coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED,
c_session);
coap_session_establish(c_session);
c_session->sock.lfunc[COAP_LAYER_TLS].establish(c_session);
}
}
+4 -4
View File
@@ -219,8 +219,8 @@ coap_netif_strm_read(coap_session_t *session, uint8_t *data, size_t datalen) {
coap_log_debug("* %s: netif: received %zd bytes\n",
coap_session_str(session), bytes_read);
} else if (bytes_read == -1 && errno != EAGAIN) {
coap_log_debug( "* %s: netif: failed to receive any bytes (%s)\n",
coap_session_str(session), coap_socket_strerror());
coap_log_debug( "* %s: netif: failed to receive any bytes (%s) state %d\n",
coap_session_str(session), coap_socket_strerror(), session->state);
errno = keep_errno;
}
return bytes_read;
@@ -257,7 +257,8 @@ coap_netif_strm_write(coap_session_t *session, const uint8_t *data,
void
coap_netif_close(coap_session_t *session) {
coap_socket_close(&session->sock);
if (coap_netif_available(session))
coap_socket_close(&session->sock);
}
#if COAP_SERVER_SUPPORT
@@ -265,5 +266,4 @@ void
coap_netif_close_ep(coap_endpoint_t *endpoint) {
coap_socket_close(&endpoint->sock);
}
#endif /* COAP_SERVER_SUPPORT */
+11 -8
View File
@@ -367,7 +367,7 @@ coap_dgram_write(BIO *a, const char *in, int inl) {
errno = ECONNRESET;
return -1;
}
ret = (int)coap_netif_dgrm_write(data->session,
ret = (int)data->session->sock.lfunc[COAP_LAYER_TLS].write(data->session,
(const uint8_t *)in,
inl);
BIO_clear_retry_flags(a);
@@ -718,7 +718,8 @@ coap_sock_read(BIO *a, char *out, int outl) {
coap_session_t *session = (coap_session_t *)BIO_get_data(a);
if (out != NULL) {
ret = (int)coap_netif_strm_read(session, (u_char *)out, outl);
ret =(int)session->sock.lfunc[COAP_LAYER_TLS].read(session, (u_char *)out,
outl);
/* Translate layer returns into what OpenSSL expects */
if (ret == 0) {
BIO_set_retry_read(a);
@@ -741,7 +742,9 @@ coap_sock_write(BIO *a, const char *in, int inl) {
int ret = 0;
coap_session_t *session = (coap_session_t *)BIO_get_data(a);
ret = (int)coap_netif_strm_write(session, (const uint8_t *)in, inl);
ret = (int)session->sock.lfunc[COAP_LAYER_TLS].write(session,
(const uint8_t *)in,
inl);
/* Translate layer what returns into what OpenSSL expects */
BIO_clear_retry_flags(a);
if (ret == 0) {
@@ -3215,7 +3218,7 @@ coap_dtls_receive(coap_session_t *session, const uint8_t *data, size_t data_len)
coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n",
coap_session_str(session), SSL_get_cipher_name(ssl));
coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session);
coap_session_establish(session);
session->sock.lfunc[COAP_LAYER_TLS].establish(session);
}
r = 0;
} else {
@@ -3470,7 +3473,7 @@ coap_tls_write(coap_session_t *session, const uint8_t *data, size_t data_len) {
coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n",
coap_session_str(session), SSL_get_cipher_name(ssl));
coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session);
coap_session_establish(session);
session->sock.lfunc[COAP_LAYER_TLS].establish(session);
}
if (err == SSL_ERROR_WANT_READ)
session->sock.flags |= COAP_SOCKET_WANT_READ;
@@ -3498,7 +3501,7 @@ coap_tls_write(coap_session_t *session, const uint8_t *data, size_t data_len) {
coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n",
coap_session_str(session), SSL_get_cipher_name(ssl));
coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session);
coap_session_establish(session);
session->sock.lfunc[COAP_LAYER_TLS].establish(session);
}
if (session->dtls_event >= 0) {
@@ -3544,7 +3547,7 @@ coap_tls_read(coap_session_t *session, uint8_t *data, size_t data_len) {
coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n",
coap_session_str(session), SSL_get_cipher_name(ssl));
coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session);
coap_session_establish(session);
session->sock.lfunc[COAP_LAYER_TLS].establish(session);
}
if (err == SSL_ERROR_WANT_READ)
session->sock.flags |= COAP_SOCKET_WANT_READ;
@@ -3570,7 +3573,7 @@ coap_tls_read(coap_session_t *session, uint8_t *data, size_t data_len) {
coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n",
coap_session_str(session), SSL_get_cipher_name(ssl));
coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session);
coap_session_establish(session);
session->sock.lfunc[COAP_LAYER_TLS].establish(session);
}
if (session->dtls_event >= 0) {
+64 -100
View File
@@ -257,14 +257,9 @@ coap_session_mfree(coap_session_t *session) {
if (session->partial_pdu)
coap_delete_pdu(session->partial_pdu);
if (session->proto == COAP_PROTO_DTLS)
coap_dtls_close(session);
#if !COAP_DISABLE_TCP
else if (session->proto == COAP_PROTO_TLS)
coap_tls_close(session);
#endif /* !COAP_DISABLE_TCP */
else if (coap_netif_available(session))
coap_netif_close(session);
session->sock.lfunc[COAP_LAYER_SESSION].close(session);
if (session->psk_identity)
coap_delete_bin_const(session->psk_identity);
if (session->psk_key)
@@ -513,7 +508,8 @@ coap_mid_t coap_session_send_ping(coap_session_t *session) {
return coap_send_internal(session, ping);
}
void coap_session_connected(coap_session_t *session) {
void
coap_session_connected(coap_session_t *session) {
if (session->state != COAP_SESSION_STATE_ESTABLISHED) {
coap_log_debug("***%s: session connected\n",
coap_session_str(session));
@@ -709,17 +705,7 @@ coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason) {
session->doing_first = 0;
}
#endif /* !COAP_DISABLE_TCP */
if (session->tls) {
if (session->proto == COAP_PROTO_DTLS)
coap_dtls_close(session);
#if !COAP_DISABLE_TCP
else if (session->proto == COAP_PROTO_TLS)
coap_tls_close(session);
#endif /* !COAP_DISABLE_TCP */
session->tls = NULL;
} else if (coap_netif_available(session)) {
coap_netif_close(session);
}
session->sock.lfunc[COAP_LAYER_SESSION].close(session);
}
#if COAP_SERVER_SUPPORT
@@ -855,6 +841,8 @@ coap_endpoint_get_session(coap_endpoint_t *endpoint,
packet->ifindex, endpoint->context, endpoint);
if (session) {
session->last_rx_tx = now;
memcpy(session->sock.lfunc, endpoint->sock.lfunc,
sizeof(session->sock.lfunc));
if (endpoint->proto == COAP_PROTO_UDP)
session->state = COAP_SESSION_STATE_ESTABLISHED;
else if (endpoint->proto == COAP_PROTO_DTLS) {
@@ -917,6 +905,7 @@ coap_session_create_client(coap_context_t *ctx,
default_port = COAPS_DEFAULT_PORT;
break;
case COAP_PROTO_NONE:
case COAP_PROTO_LAST:
default:
assert(0);
return NULL;
@@ -928,6 +917,8 @@ coap_session_create_client(coap_context_t *ctx,
coap_session_reference(session);
session->sock.session = session;
memcpy(&session->sock.lfunc, coap_layers_coap[proto],
sizeof(session->sock.lfunc));
if (COAP_PROTO_NOT_RELIABLE(proto)) {
coap_session_t *s, *rtmp;
@@ -987,33 +978,27 @@ error:
return NULL;
}
static coap_session_t *
coap_session_connect(coap_session_t *session) {
if (session->proto == COAP_PROTO_UDP) {
session->state = COAP_SESSION_STATE_ESTABLISHED;
} else if (session->proto == COAP_PROTO_DTLS) {
coap_dtls_establish(session);
#if !COAP_DISABLE_TCP
} else {
if (session->proto == COAP_PROTO_TCP || session->proto == COAP_PROTO_TLS) {
if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) {
session->state = COAP_SESSION_STATE_CONNECTING;
if (session->state != COAP_SESSION_STATE_ESTABLISHED &&
session->state != COAP_SESSION_STATE_NONE &&
COAP_PROTO_RELIABLE(session->proto) &&
session->type == COAP_SESSION_TYPE_CLIENT) {
session->doing_first = 1;
}
} else if (session->proto == COAP_PROTO_TLS) {
coap_tls_establish(session);
} else {
coap_session_establish(session);
}
}
#endif /* !COAP_DISABLE_TCP */
static void
coap_session_check_connect(coap_session_t *session) {
if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
session->sock.lfunc[COAP_LAYER_SESSION].establish(session);
}
#if !COAP_DISABLE_TCP
if (COAP_PROTO_RELIABLE(session->proto)) {
if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) {
session->state = COAP_SESSION_STATE_CONNECTING;
if (session->state != COAP_SESSION_STATE_ESTABLISHED &&
session->state != COAP_SESSION_STATE_NONE &&
session->type == COAP_SESSION_TYPE_CLIENT) {
session->doing_first = 1;
}
} else {
/* Initial connect worked immediately */
session->sock.lfunc[COAP_LAYER_SESSION].establish(session);
}
}
#endif /* !COAP_DISABLE_TCP */
coap_ticks(&session->last_rx_tx);
return session;
}
#endif /* COAP_CLIENT_SUPPORT */
@@ -1027,50 +1012,28 @@ coap_session_establish(coap_session_t *session) {
#endif /* !COAP_DISABLE_TCP */
}
#if COAP_SERVER_SUPPORT
#if !COAP_DISABLE_TCP
static coap_session_t *
coap_session_accept(coap_session_t *session) {
if (session->proto == COAP_PROTO_TCP || session->proto == COAP_PROTO_TLS)
coap_handle_event(session->context, COAP_EVENT_TCP_CONNECTED, session);
if (session->proto == COAP_PROTO_TCP) {
coap_session_establish(session);
} else if (session->proto == COAP_PROTO_TLS) {
coap_tls_establish(session);
}
return session;
}
#endif /* !COAP_DISABLE_TCP */
#endif /* COAP_SERVER_SUPPORT */
#if COAP_CLIENT_SUPPORT
coap_session_t *coap_new_client_session(
coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto
) {
coap_session_t *
coap_new_client_session(coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto) {
coap_session_t *session = coap_session_create_client(ctx, local_if, server,
proto);
if (session) {
coap_log_debug("***%s: session %p: created outgoing session\n",
coap_session_str(session), (void *)session);
session = coap_session_connect(session);
if (session)
session->context = ctx;
coap_session_check_connect(session);
}
return session;
}
coap_session_t *coap_new_client_session_psk(
coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
const char *identity,
const uint8_t *key,
unsigned key_len
) {
coap_session_t *
coap_new_client_session_psk(coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto, const char *identity,
const uint8_t *key, unsigned key_len) {
coap_dtls_cpsk_t setup_data;
memset (&setup_data, 0, sizeof(setup_data));
@@ -1090,13 +1053,12 @@ coap_session_t *coap_new_client_session_psk(
proto, &setup_data);
}
coap_session_t *coap_new_client_session_psk2(
coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
coap_dtls_cpsk_t *setup_data
) {
coap_session_t *
coap_new_client_session_psk2(coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
coap_dtls_cpsk_t *setup_data) {
coap_session_t *session = coap_session_create_client(ctx, local_if,
server, proto);
@@ -1143,7 +1105,8 @@ coap_session_t *coap_new_client_session_psk2(
}
coap_log_debug("***%s: new outgoing session\n",
coap_session_str(session));
return coap_session_connect(session);
coap_session_check_connect(session);
return session;
}
#endif /* COAP_CLIENT_SUPPORT */
@@ -1265,13 +1228,11 @@ coap_session_get_psk_key(const coap_session_t *session) {
}
#if COAP_CLIENT_SUPPORT
coap_session_t *coap_new_client_session_pki(
coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
coap_dtls_pki_t* setup_data
) {
coap_session_t *coap_new_client_session_pki(coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
coap_dtls_pki_t* setup_data) {
coap_session_t *session;
if (coap_dtls_is_supported() || coap_tls_is_supported()) {
@@ -1301,7 +1262,8 @@ coap_session_t *coap_new_client_session_pki(
}
coap_log_debug("***%s: new outgoing session\n",
coap_session_str(session));
return coap_session_connect(session);
coap_session_check_connect(session);
return session;
}
#endif /* ! COAP_CLIENT_SUPPORT */
@@ -1315,6 +1277,7 @@ coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep) {
if (!session)
goto error;
memcpy(session->sock.lfunc, ep->sock.lfunc, sizeof(session->sock.lfunc));
if (!coap_netif_strm_accept(ep, session))
goto error;
@@ -1330,11 +1293,10 @@ coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep) {
if (session) {
coap_log_debug("***%s: session %p: new incoming session\n",
coap_session_str(session), (void *)session);
/* Returned session may already have been released and is now NULL */
session = coap_session_accept(session);
if(session) {
coap_handle_event(session->context, COAP_EVENT_SERVER_SESSION_NEW, session);
}
coap_handle_event(session->context, COAP_EVENT_TCP_CONNECTED, session);
coap_handle_event(session->context, COAP_EVENT_SERVER_SESSION_NEW, session);
session->state = COAP_SESSION_STATE_CONNECTING;
session->sock.lfunc[COAP_LAYER_SESSION].establish(session);
}
return session;
@@ -1451,6 +1413,7 @@ coap_proto_name(coap_proto_t proto) {
case COAP_PROTO_TCP: return "TCP ";
case COAP_PROTO_TLS: return "TLS ";
case COAP_PROTO_NONE:
case COAP_PROTO_LAST:
default: return "????" ; break;
}
return NULL;
@@ -1499,6 +1462,7 @@ coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, co
ep->context = context;
ep->proto = proto;
ep->sock.endpoint = ep;
memcpy(&ep->sock.lfunc, coap_layers_coap[proto], sizeof(ep->sock.lfunc));
if (COAP_PROTO_NOT_RELIABLE(proto)) {
if (!coap_netif_dgrm_listen(ep, listen_addr))
+2 -1
View File
@@ -260,7 +260,8 @@ dtls_send_to_peer(struct dtls_context_t *dtls_context,
coap_log_warn("dtls_send_to_peer: cannot find local interface\n");
return -3;
}
return (int)coap_netif_dgrm_write(coap_session, data, len);
return (int)coap_session->sock.lfunc[COAP_LAYER_TLS].write(coap_session,
data, len);
}
static int
+15 -128
View File
@@ -761,31 +761,9 @@ coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu) {
ssize_t bytes_written = -1;
assert(pdu->hdr_size > 0);
switch(session->proto) {
case COAP_PROTO_UDP:
bytes_written = coap_netif_dgrm_write(session, pdu->token - pdu->hdr_size,
pdu->used_size + pdu->hdr_size);
break;
case COAP_PROTO_DTLS:
bytes_written = coap_dtls_send(session, pdu->token - pdu->hdr_size,
pdu->used_size + pdu->hdr_size);
break;
case COAP_PROTO_TCP:
#if !COAP_DISABLE_TCP
bytes_written = coap_netif_strm_write(session, pdu->token - pdu->hdr_size,
pdu->used_size + pdu->hdr_size);
#endif /* !COAP_DISABLE_TCP */
break;
case COAP_PROTO_TLS:
#if !COAP_DISABLE_TCP
bytes_written = coap_tls_write(session, pdu->token - pdu->hdr_size,
pdu->used_size + pdu->hdr_size);
#endif /* !COAP_DISABLE_TCP */
break;
case COAP_PROTO_NONE:
default:
break;
}
bytes_written = session->sock.lfunc[COAP_LAYER_SESSION].write(session,
pdu->token - pdu->hdr_size,
pdu->used_size + pdu->hdr_size);
coap_show_pdu(COAP_LOG_DEBUG, pdu);
return bytes_written;
}
@@ -798,54 +776,8 @@ coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node) {
#if ! COAP_CLIENT_SUPPORT
return -1;
#else /* COAP_CLIENT_SUPPORT */
if (session->proto == COAP_PROTO_DTLS && !session->tls) {
session->tls = coap_dtls_new_client_session(session);
if (session->tls) {
session->state = COAP_SESSION_STATE_HANDSHAKE;
return coap_session_delay_pdu(session, pdu, node);
}
coap_handle_event(session->context, COAP_EVENT_DTLS_ERROR, session);
if (session->type != COAP_SESSION_TYPE_CLIENT)
return -1;
#if !COAP_DISABLE_TCP
} else if(COAP_PROTO_RELIABLE(session->proto)) {
if (!coap_netif_strm_connect1(session, &session->addr_info.local,
&session->addr_info.remote,
session->proto == COAP_PROTO_TLS ?
COAPS_DEFAULT_PORT :
COAP_DEFAULT_PORT)) {
coap_handle_event(session->context, COAP_EVENT_TCP_FAILED, session);
return -1;
}
session->last_ping = 0;
session->last_pong = 0;
session->csm_tx = 0;
coap_ticks( &session->last_rx_tx );
if ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) != 0) {
session->state = COAP_SESSION_STATE_CONNECTING;
return coap_session_delay_pdu(session, pdu, node);
}
coap_handle_event(session->context, COAP_EVENT_TCP_CONNECTED, session);
if (session->proto == COAP_PROTO_TLS) {
int connected = 0;
session->state = COAP_SESSION_STATE_HANDSHAKE;
session->tls = coap_tls_new_client_session(session, &connected);
if (session->tls) {
if (connected) {
coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session);
coap_session_establish(session);
}
return coap_session_delay_pdu(session, pdu, node);
}
coap_handle_event(session->context, COAP_EVENT_DTLS_ERROR, session);
coap_session_disconnected(session, COAP_NACK_TLS_FAILED);
return -1;
} else {
coap_session_establish(session);
}
#endif /* !COAP_DISABLE_TCP */
} else {
return -1;
}
#endif /* COAP_CLIENT_SUPPORT */
}
@@ -1624,34 +1556,16 @@ coap_handle_dgram_for_proto(coap_context_t *ctx, coap_session_t *session, coap_p
#if COAP_CLIENT_SUPPORT
static void
coap_connect_session(coap_context_t *ctx,
coap_session_t *session,
coap_tick_t now) {
(void)ctx;
coap_connect_session(coap_session_t *session, coap_tick_t now) {
#if COAP_DISABLE_TCP
(void)session;
(void)now;
session->sock.flags &= ~(COAP_SOCKET_WANT_CONNECT | COAP_SOCKET_CAN_CONNECT);
#else /* !COAP_DISABLE_TCP */
if (coap_netif_strm_connect2(session)) {
session->last_rx_tx = now;
coap_handle_event(session->context, COAP_EVENT_TCP_CONNECTED, session);
if (session->proto == COAP_PROTO_TCP) {
coap_session_establish(session);
} else if (session->proto == COAP_PROTO_TLS) {
int connected = 0;
session->state = COAP_SESSION_STATE_HANDSHAKE;
session->tls = coap_tls_new_client_session(session, &connected);
if (session->tls) {
if (connected) {
coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED,
session);
coap_session_establish(session);
}
} else {
coap_handle_event(session->context, COAP_EVENT_DTLS_ERROR, session);
coap_session_disconnected(session, COAP_NACK_TLS_FAILED);
}
}
session->sock.lfunc[COAP_LAYER_SESSION].establish(session);
} else {
coap_handle_event(session->context, COAP_EVENT_TCP_FAILED, session);
coap_session_disconnected(session, COAP_NACK_NOT_DELIVERABLE);
@@ -1671,32 +1585,9 @@ coap_write_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now
coap_log_debug("** %s: mid=0x%x: transmitted after delay\n",
coap_session_str(session), (int)q->pdu->mid);
assert(session->partial_write < q->pdu->used_size + q->pdu->hdr_size);
switch (session->proto) {
case COAP_PROTO_TCP:
#if !COAP_DISABLE_TCP
bytes_written = coap_netif_strm_write(
session,
bytes_written = session->sock.lfunc[COAP_LAYER_SESSION].write(session,
q->pdu->token - q->pdu->hdr_size + session->partial_write,
q->pdu->used_size + q->pdu->hdr_size - session->partial_write
);
#endif /* !COAP_DISABLE_TCP */
break;
case COAP_PROTO_TLS:
#if !COAP_DISABLE_TCP
bytes_written = coap_tls_write(
session,
q->pdu->token - q->pdu->hdr_size - session->partial_write,
q->pdu->used_size + q->pdu->hdr_size - session->partial_write
);
#endif /* !COAP_DISABLE_TCP */
break;
case COAP_PROTO_NONE:
case COAP_PROTO_UDP:
case COAP_PROTO_DTLS:
default:
bytes_written = -1;
break;
}
q->pdu->used_size + q->pdu->hdr_size - session->partial_write);
if (bytes_written > 0)
session->last_rx_tx = now;
if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size - session->partial_write) {
@@ -1753,14 +1644,10 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now)
int retry;
do {
if (session->proto == COAP_PROTO_TCP)
bytes_read = coap_netif_strm_read(session, packet->payload,
packet->length);
else if (session->proto == COAP_PROTO_TLS)
bytes_read = coap_tls_read(session, packet->payload, packet->length);
bytes_read = session->sock.lfunc[COAP_LAYER_SESSION].read(session,
packet->payload,
packet->length);
if (bytes_read > 0) {
coap_log_debug("* %s: received %zd bytes\n",
coap_session_str(session), bytes_read);
session->last_rx_tx = now;
}
p = packet->payload;
@@ -1975,7 +1862,7 @@ coap_io_do_io(coap_context_t *ctx, coap_tick_t now) {
/* Make sure the session object is not deleted in one of the callbacks */
coap_session_reference(s);
if ((s->sock.flags & COAP_SOCKET_CAN_CONNECT) != 0) {
coap_connect_session(ctx, s, now);
coap_connect_session(s, now);
}
if ((s->sock.flags & COAP_SOCKET_CAN_READ) != 0 && s->ref > 1) {
coap_read_session(ctx, s, now);
@@ -2050,7 +1937,7 @@ coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events, size_t nevents
if ((sock->flags & COAP_SOCKET_WANT_CONNECT) &&
(events[j].events & (EPOLLOUT|EPOLLERR|EPOLLHUP|EPOLLRDHUP))) {
sock->flags |= COAP_SOCKET_CAN_CONNECT;
coap_connect_session(session->context, session, now);
coap_connect_session(session, now);
if (coap_netif_available(session) &&
!(sock->flags & COAP_SOCKET_WANT_WRITE)) {
coap_epoll_ctl_mod(sock, EPOLLIN, __func__);
+2 -2
View File
@@ -1341,7 +1341,7 @@ coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto) {
"coap_add_token: Token size too large. PDU ignored\n");
return 0;
}
if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) {
if (COAP_PROTO_NOT_RELIABLE(proto)) {
assert(pdu->max_hdr_size >= 4);
if (pdu->max_hdr_size < 4) {
coap_log_warn(
@@ -1355,7 +1355,7 @@ coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto) {
pdu->token[-2] = (uint8_t)(pdu->mid >> 8);
pdu->token[-1] = (uint8_t)(pdu->mid);
pdu->hdr_size = 4;
} else if (proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) {
} else if (COAP_PROTO_RELIABLE(proto)) {
size_t len;
assert(pdu->used_size >= pdu->e_token_length);
if (pdu->used_size < pdu->e_token_length) {