diff --git a/include/coap/coap_dtls.h b/include/coap/coap_dtls.h index 22150a3d..6d388667 100644 --- a/include/coap/coap_dtls.h +++ b/include/coap/coap_dtls.h @@ -27,8 +27,6 @@ */ #define COAP_DTLS_MAX_PACKET_SIZE COAP_MAX_PDU_SIZE -struct coap_dtls_context_t; - /** Returns 1 if support for DTLS is enabled, or 0 otherwise. */ int coap_dtls_is_supported(void); @@ -45,11 +43,11 @@ int coap_dtls_get_log_level(void); * @param coap_context The CoAP context where the DTLS object shall be used. * @return A DTLS context object or NULL on error; */ -struct coap_dtls_context_t * +void * coap_dtls_new_context( coap_context_t *coap_context ); /** Releases the storage allocated for @p dtls_context. */ -void coap_dtls_free_context( struct coap_dtls_context_t *dtls_context ); +void coap_dtls_free_context( void *dtls_context ); /** * Create a new client-side session. This should send a HELLO to the server. diff --git a/include/coap/net.h b/include/coap/net.h index 0faadf62..e463d72c 100644 --- a/include/coap/net.h +++ b/include/coap/net.h @@ -135,7 +135,7 @@ typedef struct coap_context_t { unsigned (*get_server_psk)( const coap_session_t *session, const char *identity, uint8_t *psk, unsigned max_psk_len ); int (*get_server_hint)( const coap_session_t *session, char *hint, unsigned max_hint_len ); - struct coap_dtls_context_t *dtls_context; + void *dtls_context; char *psk_hint; uint8_t *psk_key; unsigned psk_key_len; diff --git a/include/coap/session.h b/include/coap/session.h index 002fc405..b577dd1e 100644 --- a/include/coap/session.h +++ b/include/coap/session.h @@ -207,4 +207,6 @@ coap_session_t *coap_endpoint_get_session( coap_endpoint_t *endpoint, const coap coap_session_t *coap_endpoint_new_dtls_session( coap_endpoint_t *endpoint, const coap_packet_t *packet ); +coap_session_t *coap_session_get_by_peer( coap_context_t *ctx, const coap_address_t *remote_addr, int ifindex ); + #endif /* _SESSION_H */ diff --git a/src/coap_openssl.c b/src/coap_openssl.c index 739ac6cf..d3fa2bd2 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -297,8 +297,8 @@ static void coap_dtls_info_callback( const SSL *ssl, int where, int ret ) { static int ssl_library_loaded = 0; -struct coap_dtls_context_t *coap_dtls_new_context( struct coap_context_t *coap_context ) { - struct coap_dtls_context_t *context; +void *coap_dtls_new_context( struct coap_context_t *coap_context ) { + coap_dtls_context_t *context; if ( !ssl_library_loaded ) { SSL_load_error_strings(); @@ -306,11 +306,11 @@ struct coap_dtls_context_t *coap_dtls_new_context( struct coap_context_t *coap_c ssl_library_loaded = 1; } - context = ( struct coap_dtls_context_t * )coap_malloc( sizeof( struct coap_dtls_context_t ) ); + context = (coap_dtls_context_t *)coap_malloc( sizeof(coap_dtls_context_t ) ); if ( context ) { BIO *bio; uint8_t cookie_secret[32]; - memset( context, 0, sizeof( struct coap_dtls_context_t ) ); + memset( context, 0, sizeof(coap_dtls_context_t) ); context->ctx = SSL_CTX_new( DTLSv1_2_method() ); if ( !context->ctx ) goto error; @@ -362,7 +362,8 @@ error: return NULL; } -void coap_dtls_free_context( struct coap_dtls_context_t *context ) { +void coap_dtls_free_context( void *handle ) { + coap_dtls_context_t *context = (coap_dtls_context_t *)handle; if ( context->ssl ) SSL_free( context->ssl ); if ( context->ctx ) @@ -380,7 +381,7 @@ void * coap_dtls_new_server_session( coap_session_t *session ) { BIO *nbio = NULL; SSL *nssl = NULL, *ssl = NULL; coap_ssl_data *data; - coap_dtls_context_t *dtls = session->context->dtls_context; + coap_dtls_context_t *dtls = (coap_dtls_context_t *)session->context->dtls_context; int r; nssl = SSL_new( dtls->ctx ); @@ -431,7 +432,7 @@ void *coap_dtls_new_client_session( coap_session_t *session ) { SSL *ssl = NULL; coap_ssl_data *data; int r; - coap_dtls_context_t *dtls = session->context->dtls_context; + coap_dtls_context_t *dtls = (coap_dtls_context_t *)session->context->dtls_context; ssl = SSL_new( dtls->ctx ); if ( !ssl ) @@ -505,7 +506,7 @@ int coap_dtls_send( coap_session_t *session, int coap_dtls_hello( coap_session_t *session, const uint8_t *data, size_t data_len ) { - struct coap_dtls_context_t *ctx = session->context->dtls_context; + coap_dtls_context_t *ctx = (coap_dtls_context_t *)session->context->dtls_context; coap_ssl_data *ssl_data; int r; diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index ad83215d..4b5745fc 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -15,7 +15,6 @@ #include "debug.h" #include "mem.h" #include "coap_dtls.h" -#include "utlist.h" #include #include @@ -25,32 +24,6 @@ void dtls_set_log_level(int); int dtls_get_log_level(void); -/* Data item in the DTLS send queue. */ -struct queue_t { - struct queue_t *next; - coap_tid_t id; - size_t data_length; - unsigned char data[]; -}; - -/* This structure takes a tinydtls peer object to represent a session - * with a remote peer. Note that session_t objects in tinydtls are - * less useful to pass around because in the end, you will always need - * to find the corresponding dtls_peer_t object. dtls_session must be - * * the first component in this structure. */ -typedef struct coap_dtls_session_t { - session_t dtls_session; - struct coap_dtls_session_t *next; - struct queue_t *sendqueue; -} coap_dtls_session_t; - -/* This structure encapsulates the dtls_context_t object from tinydtls - * which must always be the first component. */ -typedef struct coap_dtls_context_t { - dtls_context_t *dtls_context; - coap_dtls_session_t *sessions; -} coap_dtls_context_t; - int coap_dtls_is_supported(void) { return 1; @@ -66,95 +39,31 @@ coap_dtls_get_log_level(void) { return dtls_get_log_level(); } -static int -push_data_item(struct coap_dtls_session_t *session, coap_tid_t id, - const unsigned char *data, size_t data_length) { - struct queue_t *item; -#define ITEM_SIZE (sizeof(struct queue_t) + data_length) - - /* Only add if we do not already have that item. */ - LL_SEARCH_SCALAR(session->sendqueue, item, id, id); - if (!item) { /* Not found, add new item */ - if ((item = (struct queue_t *)coap_malloc(ITEM_SIZE)) != NULL) { - debug("*** add %p to sendqueue of session %p\n", item, session); - item->id = id; - item->data_length = data_length; - memcpy(item->data, data, data_length); - LL_APPEND(session->sendqueue, item); - } - } - - return item != NULL; -} - -static int -flush_data(struct coap_dtls_context_t *context, - struct coap_dtls_session_t *session) { - struct queue_t *item, *tmp; - int ok = 0; - int res; - - LL_FOREACH_SAFE(session->sendqueue, item, tmp) { - res = dtls_write(context->dtls_context, &session->dtls_session, - (uint8_t *)item->data, item->data_length); - - if (res <= 0) { - debug("data not written\n"); - return ok; - } else { - if ((size_t)res < item->data_length) { - debug("data truncated by dtls_write()\n"); - } else { - ok = 1; - } - LL_DELETE(session->sendqueue, item); - coap_free(item); - } - } - - return ok; -} - static int dtls_send_to_peer(struct dtls_context_t *dtls_context, - session_t *session, uint8 *data, size_t len) { - coap_context_t *coap_context = dtls_get_app_data(dtls_context); - coap_endpoint_t *local_interface; - - assert(coap_context); - - LL_SEARCH_SCALAR(coap_context->endpoint, local_interface, - handle.fd, session->ifindex); - - if (!local_interface) { - coap_log(LOG_WARNING, "dtls_send_to_peer: cannot find local interface\n"); + session_t *dtls_session, uint8 *data, size_t len) { + coap_context_t *coap_context = (coap_context_t *)dtls_get_app_data( dtls_context ); + coap_session_t *coap_session = + coap_session_get_by_peer( coap_context, (const coap_address_t*)dtls_session, dtls_session->ifindex ); + if ( !coap_session ) { + coap_log( LOG_WARNING, "dtls_send_to_peer: cannot find local interface\n" ); return -3; } - - return coap_network_send(coap_context, local_interface, - (coap_address_t *)session, data, len); + return (int)coap_session_send( coap_session, data, len ); } static int dtls_application_data(struct dtls_context_t *dtls_context, - session_t *session, uint8 *data, size_t len) { - coap_context_t *coap_context; - coap_endpoint_t *local_interface; - - coap_context = (coap_context_t *)dtls_get_app_data(dtls_context); - assert(coap_context && coap_context->dtls_context); - - LL_SEARCH_SCALAR(coap_context->endpoint, local_interface, - handle.fd, session->ifindex); - - if (!local_interface) { - debug("dropped message that was received on invalid interface\n"); + session_t *dtls_session, uint8 *data, size_t len) { + coap_context_t *coap_context = (coap_context_t *)dtls_get_app_data( dtls_context ); + coap_session_t *coap_session = + coap_session_get_by_peer( coap_context, (const coap_address_t*)dtls_session, dtls_session->ifindex ); + if ( !coap_session ) { + debug( "dropped message that was received on invalid interface\n" ); return -1; } - return coap_handle_message(coap_context, local_interface, - (coap_address_t *)session, - (unsigned char *)data, len); + return coap_handle_message(coap_context, coap_session, data, len); } static int @@ -162,37 +71,16 @@ dtls_event(struct dtls_context_t *dtls_context, session_t *dtls_session, dtls_alert_level_t level, unsigned short code) { - coap_context_t *coap_context; - coap_dtls_session_t *session; + coap_context_t *coap_context = (coap_context_t *)dtls_get_app_data( dtls_context ); + coap_session_t *coap_session = + coap_session_get_by_peer( coap_context, (const coap_address_t*)dtls_session, dtls_session->ifindex ); int event = (level == DTLS_ALERT_LEVEL_FATAL) ? COAP_EVENT_DTLS_ERROR : -1; - coap_context = (coap_context_t *)dtls_get_app_data(dtls_context); - assert(coap_context && coap_context->dtls_context); - - LL_FOREACH(coap_context->dtls_context->sessions, session) { - if ((session->dtls_session.ifindex == dtls_session->ifindex) && - coap_address_equals((coap_address_t *)&session->dtls_session, - (coap_address_t *)dtls_session)) { - break; - } - } - - if (!session) { + if (!coap_session ) { coap_log(LOG_CRIT, "cannot handle event: session not found\n"); return -1; } - /* Stop all transactions that are affected from a fatal error - * condition. */ - if (level == DTLS_ALERT_LEVEL_FATAL) { - struct queue_t *item; - LL_FOREACH(session->sendqueue, item) { - coap_queue_t *node = NULL; - coap_remove_from_queue(&coap_context->sendqueue, item->id, &node); - coap_delete_node(node); - } - } - /* handle DTLS events */ switch (code) { case DTLS_ALERT_CLOSE_NOTIFY: { @@ -200,7 +88,6 @@ dtls_event(struct dtls_context_t *dtls_context, break; } case DTLS_EVENT_CONNECTED: { - flush_data(coap_context->dtls_context, session); event = COAP_EVENT_DTLS_CONNECTED; break; } @@ -213,13 +100,14 @@ dtls_event(struct dtls_context_t *dtls_context, } if (event != -1) { - coap_handle_event(coap_context, event, session); - } - - if (level == DTLS_ALERT_LEVEL_FATAL) { - coap_dtls_free_session(coap_context->dtls_context, session); + coap_handle_event(coap_context, event, coap_session); } + if ( event == COAP_EVENT_DTLS_CONNECTED ) + coap_session_connected( coap_session ); + else if ( event == DTLS_ALERT_CLOSE_NOTIFY ) + coap_session_disconnected( coap_session ); + return 0; } @@ -228,65 +116,90 @@ dtls_event(struct dtls_context_t *dtls_context, * session. */ static int get_psk_info(struct dtls_context_t *dtls_context, - const session_t *session, + const session_t *dtls_session, dtls_credentials_type_t type, const unsigned char *id, size_t id_len, unsigned char *result, size_t result_length) { - coap_context_t *coap_context = dtls_get_app_data(dtls_context); - coap_keystore_item_t *psk; - ssize_t length; + coap_context_t *coap_context; + coap_session_t *coap_session; int fatal_error = DTLS_ALERT_INTERNAL_ERROR; + char buf[128]; + size_t length; + static int client = 0; + static uint8_t psk[128]; + static unsigned psk_len = 0; - if(!coap_context || !coap_context->keystore) { - goto error; + if ( type == DTLS_PSK_KEY && client ) { + if ( psk_len > result_length ) { + coap_log( LOG_WARNING, "cannot set psk -- buffer too small\n" ); + goto error; + } + memcpy( result, psk, psk_len ); + client = 0; + return (int)psk_len; } + client = 0; + coap_context = (coap_context_t *)dtls_get_app_data( dtls_context ); + coap_session = + coap_session_get_by_peer( coap_context, (const coap_address_t*)dtls_session, dtls_session->ifindex ); + switch (type) { case DTLS_PSK_IDENTITY: + if (id_len) { coap_log(LOG_DEBUG, "got psk_identity_hint: '%.*s'\n", id_len, id); } - psk = coap_keystore_find_psk(coap_context->keystore, id, id_len, - NULL, 0, (coap_address_t *)session); - if (!psk) { - coap_log(LOG_WARNING, "no PSK identity for given realm\n"); + if ( !coap_context || !coap_context->get_client_psk ) + goto error; + + if ( id_len ) { + if ( id_len + 1 < sizeof( buf ) ) { + memcpy( buf, id, id_len ); + buf[id_len] = 0; + } else { + coap_log( LOG_WARNING, "cannot set psk -- hint too big\n" ); + goto error; + } + } + result[0] = 0; + psk_len = coap_context->get_client_psk( coap_session, buf, (char*)result, (unsigned)result_length, psk, (unsigned)sizeof( psk ) ); + if ( !psk_len ) { + coap_log( LOG_WARNING, "no PSK identity for given realm\n" ); fatal_error = DTLS_ALERT_CLOSE_NOTIFY; goto error; } + length = strlen( result ); + client = 1; + return (int)length; - length = coap_psk_set_identity(psk, result, result_length); - if (length < 0) { - coap_log(LOG_WARNING, "cannot set psk_identity -- buffer too small\n"); - goto error; - } - - return length; case DTLS_PSK_KEY: - psk = coap_keystore_find_psk(coap_context->keystore, NULL, 0, - id, id_len, (coap_address_t *)session); - if (!psk) { - coap_log(LOG_WARNING, "PSK for unknown id requested, exiting\n"); - fatal_error = DTLS_ALERT_HANDSHAKE_FAILURE; - goto error; + if ( coap_context->get_server_psk ) { + if ( id_len + 1 > sizeof( buf ) ) { + coap_log( LOG_WARNING, "cannot set psk -- identity too big\n" ); + goto error; + } + memcpy( buf, id, id_len ); + buf[id_len] = 0; + return (int)coap_context->get_server_psk( coap_session, buf, result, (unsigned)result_length ) } - - length = coap_psk_set_key(psk, result, result_length); - if (length < 0) { - coap_log(LOG_WARNING, "cannot set psk -- buffer too small\n"); - goto error; - } - - return length; case DTLS_PSK_HINT: - /* There is no point in sending a psk_identity_hint hence it is - * set to zero length. */ - return 0; + client = 0; + if ( coap_context->get_server_hint ) { + result[0] = 0; + if ( coap_context->get_server_hint( coap_session, result, result_length ) ) + length = strlen( result ); + } else { + length = 0; + } + return length; default: coap_log(LOG_WARNING, "unsupported request type: %d\n", type); } error: + client = 0; return dtls_alert_fatal_create(fatal_error); } @@ -302,197 +215,111 @@ static dtls_handler_t cb = { }; struct coap_dtls_context_t * -coap_dtls_new_context(struct coap_context_t *coap_context) { - struct coap_dtls_context_t *context; -#define CONTEXT_SIZE (sizeof(struct coap_dtls_context_t)) - - context = (struct coap_dtls_context_t *)coap_malloc(CONTEXT_SIZE); - if (context) { - memset(context, 0, CONTEXT_SIZE); - - context->dtls_context = dtls_new_context(coap_context); - if (!context->dtls_context) { - goto error; - } - - dtls_set_handler(context->dtls_context, &cb); - } - - return context; +coap_dtls_new_context( coap_context_t *coap_context ) { + struct dtls_context_t *dtls_context = dtls_new_context( coap_context ); + if (!dtls_context) + goto error; + dtls_set_handler( dtls_context, &cb ); + return dtls_context; error: - - coap_dtls_free_context(context); + coap_dtls_free_context( dtls_context ); return NULL; } void -coap_dtls_free_context(struct coap_dtls_context_t *dtls_context) { - while(dtls_context->sessions) { - coap_dtls_free_session(dtls_context, dtls_context->sessions); +coap_dtls_free_context( void *handle ) { + if ( handle ) { + struct dtls_context_t *dtls_context = (struct dtls_context_t *)handle; + dtls_free_context( dtls_context ); } - dtls_free_context(dtls_context->dtls_context); - coap_free(dtls_context); } -struct coap_dtls_session_t * -coap_dtls_new_session(coap_dtls_context_t *dtls_context, - const coap_endpoint_t *local_interface, - const coap_address_t *remote) { - struct coap_dtls_session_t *session; - const size_t need = sizeof(struct coap_dtls_session_t); +static session_t * +coap_dtls_new_session( coap_session_t *session ) { + session_t *dtls_session = coap_malloc_type( COAP_DTLS_SESSION, sizeof( session_t ) ); - session = coap_malloc_type(COAP_DTLS_SESSION, need); - - if (session) { + if ( dtls_session ) { /* create tinydtls session object from remote address and local - * endpoint handle */ - memset(session, 0, need); - dtls_session_init(&session->dtls_session); - COAP_COPY_ADDRESS(&session->dtls_session, remote); - session->dtls_session.ifindex = local_interface->handle.fd; - - LL_PREPEND(dtls_context->sessions, session); - debug("*** new session %p\n", session); + * endpoint handle */ + dtls_session_init( &dtls_session ); + coap_copy_address( (coap_address_t*)&dtls_session, &session->remote_addr ); + dtls_session.ifindex = session->ifindex; + debug( "*** new session %p\n", dtls_session ); } - return session; + return dtls_session; +} + +void *coap_dtls_new_server_session( coap_session_t *session ) { + return coap_dtls_new_session( session ); +} + +void *coap_dtls_new_client_session( coap_session_t *session ) { + dtls_peer_t *peer; + session_t *dtls_session = coap_dtls_new_session( session ); + if ( !dtls_session ) + return NULL; + peer = + dtls_get_peer( (struct dtls_context_t *)session->context->dtls_context, + &dtls_session ); + + if ( !peer ) { + /* The peer connection does not yet exist. */ + /* dtls_connect() returns a value greater than zero if a new + * connection attempt is made, 0 for session reuse. */ + if ( dtls_connect( (struct dtls_context_t *)session->context->dtls_context, + &dtls_session ) >= 0 ) { + peer = + dtls_get_peer( (struct dtls_context_t *)coap_context->dtls_context, + &dtls_session ); + } + } + + if ( !peer ) { + /* delete existing session because the peer object has been invalidated */ + coap_free_type( COAP_DTLS_SESSION, dtls_session ); + dtls_session = NULL; + } + + return dtls_session; } void -coap_dtls_free_session(coap_dtls_context_t *dtls_context, - coap_dtls_session_t *session) { - if (session) { - struct queue_t *item, *tmp; - - LL_DELETE(dtls_context->sessions, session); - LL_FOREACH_SAFE(session->sendqueue, item, tmp) { - coap_free(item); - } - debug("*** removed session %p\n", session); - coap_free_type(COAP_DTLS_SESSION, session); +coap_dtls_free_session( coap_session_t *coap_session ) { + if ( coap_session->tls ) { + debug("*** removed session %p\n", coap_session->tls); + coap_free_type(COAP_DTLS_SESSION, coap_session->tls); } } -static coap_dtls_session_t * -coap_dtls_find_session(coap_dtls_context_t *dtls_context, - const coap_endpoint_t *local_interface, - const coap_address_t *dst) { - struct coap_dtls_session_t *session; - - LL_FOREACH(dtls_context->sessions, session) { - if ((session->dtls_session.ifindex == local_interface->handle.fd) && - coap_address_equals((coap_address_t *)&session->dtls_session, dst)) { - return session; - } - } - return session; -} - -struct coap_dtls_session_t * -coap_dtls_get_session(struct coap_context_t *coap_context, - const coap_endpoint_t *local_interface, - const coap_address_t *dst) { - dtls_peer_t *peer; - coap_dtls_session_t *session; - - assert(coap_context && coap_context->dtls_context); - - /* reuse existing session if available, otherwise create new session */ - session = coap_dtls_find_session(coap_context->dtls_context, - local_interface, dst); - - if (!session && - ((session = coap_dtls_new_session(coap_context->dtls_context, - local_interface, dst)) == NULL)) { - coap_log(LOG_WARNING, "cannot create session object\n"); - return NULL; - } - - peer = dtls_get_peer(coap_context->dtls_context->dtls_context, - &session->dtls_session); - - if (!peer) { - /* The peer connection does not yet exist. */ - /* dtls_connect() returns a value greater than zero if a new - * connection attempt is made, 0 for session reuse. */ - if (dtls_connect(coap_context->dtls_context->dtls_context, - &session->dtls_session) >= 0) { - - peer = dtls_get_peer(coap_context->dtls_context->dtls_context, - &session->dtls_session); - } - } - - if (!peer) { - /* delete existing session because the peer object has been invalidated */ - coap_dtls_free_session(coap_context->dtls_context, session); - session = NULL; - } - - return session; -} - int -coap_dtls_send(struct coap_context_t *coap_context, - struct coap_dtls_session_t *session, - const coap_pdu_t *pdu) { - int res = -2; +coap_dtls_send( coap_session_t *session, + const uint8_t *data, + size_t data_len +) { + int res; - assert(coap_context && coap_context->dtls_context); coap_log(LOG_DEBUG, "call dtls_write\n"); - res = dtls_write(coap_context->dtls_context->dtls_context, - &session->dtls_session, - (uint8 *)pdu->hdr, pdu->length); + res = dtls_write((struct dtls_context_t *)session->context->dtls_context, + (session_t)session->tls, (uint8 *)data, data_len); - if (res < 0) { + if (res < 0) coap_log(LOG_WARNING, "coap_dtls_send: cannot send PDU\n"); - } else if (res == 0) { - coap_tid_t id; - coap_transaction_id((coap_address_t *)&session->dtls_session, pdu, &id); - - if (!push_data_item(session, id, (uint8 *)pdu->hdr, pdu->length)) { - coap_log(LOG_DEBUG, "cannot store %u bytes for deferred transmission\n", - pdu->length); - res = -2; - } - } return res; } int -coap_dtls_handle_message(struct coap_context_t *coap_context, - const coap_endpoint_t *local_interface, - const coap_address_t *dst, - const unsigned char *data, size_t data_len) { - coap_dtls_session_t *session; - int new_session = 0; - - session = coap_dtls_find_session(coap_context->dtls_context, - local_interface, dst); - - if (!session) { - if ((session = coap_dtls_new_session(coap_context->dtls_context, - local_interface, dst)) != NULL) { - new_session = 1; - } - } - - if (!session) { - coap_log(LOG_WARNING, "cannot allocate session, drop packet\n"); - return -1; - } - - int res = - dtls_handle_message(coap_context->dtls_context->dtls_context, - &session->dtls_session, (uint8 *)data, data_len); - - if ((res < 0) && new_session) { - coap_dtls_free_session(coap_context->dtls_context, session); - } - - return -1; +coap_dtls_receive( coap_session_t *session, + const uint8_t *data, + size_t data_len +) { + session_t *dtls_session = (session_t *)session->tls; + int res = dtls_handle_message( + (struct dtls_context_t *)session->context->dtls_context, + dtls_session, (uint8 *)data, (int)data_len); + return res; } #endif /* HAVE_LIBTINYDTLS */ diff --git a/src/session.c b/src/session.c index 85b32751..65a5ca1b 100644 --- a/src/session.c +++ b/src/session.c @@ -398,6 +398,28 @@ coap_free_endpoint( coap_endpoint_t *ep ) { } } +coap_session_t * +coap_session_get_by_peer( coap_context_t *ctx, + const coap_address_t *remote_addr, + int ifindex ) +{ + coap_session_t *s; + coap_endpoint_t *ep; + LL_FOREACH( ctx->sessions, s ) { + if ( s->ifindex == ifindex && coap_address_equals( &s->remote_addr, remote_addr ) ) + return s; + } + LL_FOREACH( ctx->endpoint, ep ) { + if ( ep->hello.ifindex == ifindex && coap_address_equals( &ep->hello.remote_addr, remote_addr ) ) + return &ep->hello; + LL_FOREACH( ep->sessions, s ) { + if ( s->ifindex == ifindex && coap_address_equals( &s->remote_addr, remote_addr ) ) + return s; + } + } + return NULL; +} + const char *coap_session_str( const coap_session_t *session ) { static char szSession[256]; char *p = szSession, *end = szSession + sizeof( szSession );