Skip to content

Commit

Permalink
tcp: pair connection with the tls peer
Browse files Browse the repository at this point in the history
  • Loading branch information
jkralik committed May 21, 2024
1 parent 5c520c4 commit 446fdaf
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 5 deletions.
30 changes: 30 additions & 0 deletions api/oc_endpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,18 @@ oc_endpoint_compare_address(const oc_endpoint_t *ep1, const oc_endpoint_t *ep2)
return -1;
}

#ifdef OC_TCP
static int
oc_endpoint_compare_session_ids(const oc_endpoint_t *ep1,
const oc_endpoint_t *ep2)
{
if (ep1->session_id == 0 || ep2->session_id == 0) {
return 0; // session_id == 0 means any
}
return ep1->session_id == ep2->session_id ? 0 : -1;
}
#endif

int
oc_endpoint_compare(const oc_endpoint_t *ep1, const oc_endpoint_t *ep2)
{
Expand All @@ -690,19 +702,37 @@ oc_endpoint_compare(const oc_endpoint_t *ep1, const oc_endpoint_t *ep2)
if (ep1->flags & IPV6) {
if (memcmp(ep1->addr.ipv6.address, ep2->addr.ipv6.address, 16) == 0 &&
ep1->addr.ipv6.port == ep2->addr.ipv6.port) {
#ifdef OC_TCP
return oc_endpoint_compare_session_ids(ep1, ep2);
#else /* OC_TCP */
return 0;
#endif /* !OC_TCP */
}
return -1;
}
#ifdef OC_IPV4
else if (ep1->flags & IPV4) {
if (memcmp(ep1->addr.ipv4.address, ep2->addr.ipv4.address, 4) == 0 &&
ep1->addr.ipv4.port == ep2->addr.ipv4.port) {
#ifdef OC_TCP
return oc_endpoint_compare_session_ids(ep1, ep2);
#else /* OC_TCP */
return 0;
#endif /* !OC_TCP */
}
return -1;
}
#endif /* OC_IPV4 */

#ifdef OC_TCP
else if (ep1->flags & TCP) {
if (memcmp(ep1->addr.ipv6.address, ep2->addr.ipv6.address, 16) == 0 &&
ep1->addr.ipv6.port == ep2->addr.ipv6.port) {
return 0;
}
return -1;
}
#endif
// TODO: Add support for other endpoint types
return -1;
}
Expand Down
10 changes: 10 additions & 0 deletions api/oc_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,23 @@
#include "oc_endpoint.h"
#include "port/oc_connectivity.h"
#include "oc_tcp_internal.h"
#include "util/oc_atomic.h"
#ifdef OC_SECURITY
#include <mbedtls/ssl.h>
#ifdef OC_OSCORE
#include "messaging/coap/oscore_internal.h"
#endif /* OC_OSCORE */
#endif /* OC_SECURITY */

static OC_ATOMIC_UINT32_T g_tcp_session_id = 0;

uint32_t
oc_tcp_get_new_session_id(void)
{
uint32_t v = OC_ATOMIC_INCREMENT32(g_tcp_session_id);
return (v == 0) ? OC_ATOMIC_INCREMENT32(g_tcp_session_id) : v;
}

#ifdef OC_HAS_FEATURE_TCP_ASYNC_CONNECT

#include "port/oc_allocator_internal.h"
Expand Down
4 changes: 4 additions & 0 deletions api/oc_tcp_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#ifdef OC_TCP

#include <stdint.h>
#include "messaging/coap/constants.h"
#include "port/oc_connectivity.h"
#include "oc_endpoint.h"
Expand All @@ -34,6 +35,9 @@ extern "C" {
#define OC_TCP_DEFAULT_RECEIVE_SIZE \
(COAP_TCP_DEFAULT_HEADER_LEN + COAP_TCP_MAX_EXTENDED_LENGTH_LEN)

/** @brief Get new tcp session ID */
uint32_t oc_tcp_get_new_session_id(void);

#ifdef OC_HAS_FEATURE_TCP_ASYNC_CONNECT

typedef struct oc_tcp_on_connect_event_s
Expand Down
6 changes: 5 additions & 1 deletion include/oc_endpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,18 @@ typedef struct oc_endpoint_t
oc_ipv4_addr_t ipv4; ///< ipv4 address
oc_le_addr_t bt; ///< blue tooth address
} addr, addr_local;
unsigned interface_index; ///< interface index (valid intefaces are >0, 0
unsigned interface_index; ///< interface index (valid interfaces are >0, 0
///< means no index or error)
uint8_t priority; ///< priority
ocf_version_t version; ///< ocf version
#ifdef OC_OSCORE
uint8_t piv[OSCORE_PIV_LEN];
uint8_t piv_len;
#endif /* OC_OSCORE */
#ifdef OC_TCP
uint32_t session_id; ///< session id for pairing tls peer with tcp session - 0
///< means any
#endif
} oc_endpoint_t;

#define oc_make_ipv4_endpoint(__name__, __flags__, __port__, ...) \
Expand Down
3 changes: 3 additions & 0 deletions port/android/tcpadapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ add_new_session(int sock, ip_context_t *dev, oc_endpoint_t *endpoint,
session->endpoint.next = NULL;
session->sock = sock;
session->csm_state = state;
if (session->endpoint.session_id == 0) {
session->endpoint.session_id = oc_tcp_get_new_session_id();
}

oc_list_add(session_list, session);

Expand Down
3 changes: 3 additions & 0 deletions port/esp32/adapter/src/tcpadapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ add_new_session(int sock, ip_context_t *dev, oc_endpoint_t *endpoint,
session->endpoint.next = NULL;
session->sock = sock;
session->csm_state = state;
if (session->endpoint.session_id == 0) {
session->endpoint.session_id = oc_tcp_get_new_session_id();
}

oc_list_add(session_list, session);

Expand Down
27 changes: 23 additions & 4 deletions port/linux/tcpsession.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,24 @@ log_new_session(oc_endpoint_t *endpoint, int sock, bool is_connected)
addr = oc_string(ep);
}
OC_DBG("new TCP session endpoint: %s, endpoint interface: %d, sock: %d, "
"connected: %d",
addr, endpoint->interface_index, sock, (int)is_connected);
"connected: %d, session_id: %u",
addr, endpoint->interface_index, sock, (int)is_connected,
(unsigned)endpoint->session_id);
}

static void
log_free_session(oc_endpoint_t *endpoint, int sock)
{
oc_string64_t ep;
const char *addr = "";
if (oc_endpoint_to_string64(endpoint, &ep)) {
addr = oc_string(ep);
}
OC_DBG("free TCP session endpoint: %s, endpoint interface: %d, sock: %d, "
"session_id: %u",
addr, endpoint->interface_index, sock, (unsigned)endpoint->session_id);
}

#endif /* OC_DBG_IS_ENABLED */

static tcp_session_t *
Expand All @@ -240,6 +255,9 @@ add_new_session_locked(int sock, ip_context_t *dev, oc_endpoint_t *endpoint,
session->sock = sock;
session->csm_state = state;
session->notify_session_end = true;
if (session->endpoint.session_id == 0) {
session->endpoint.session_id = oc_tcp_get_new_session_id();
}

oc_list_add(g_session_list, session);

Expand Down Expand Up @@ -311,8 +329,9 @@ free_session_locked(tcp_session_t *session, bool signal)
signal_network_thread(&session->dev->tcp);
}
close(session->sock);

OC_DBG("free TCP session(%p, fd=%d)", (void *)session, session->sock);
#if OC_DBG_IS_ENABLED
log_free_session(&session->endpoint, session->sock);
#endif /* OC_DBG_IS_ENABLED */
oc_memb_free(&g_tcp_session_s, session);
}

Expand Down
3 changes: 3 additions & 0 deletions port/windows/tcpadapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ add_new_session_locked(SOCKET sock, ip_context_t *dev, oc_endpoint_t *endpoint,
session->csm_state = state;
session->sock_event = sock_event;
session->notify_session_end = true;
if (session->endpoint.session_id == 0) {
session->endpoint.session_id = oc_tcp_get_new_session_id();
}

oc_list_add(session_list, session);

Expand Down
6 changes: 6 additions & 0 deletions security/oc_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -2684,6 +2684,12 @@ oc_tls_init_connection(oc_message_t *message)
return;
}

#ifdef OC_TCP
if ((peer->endpoint.flags & TCP) != 0 && peer->endpoint.session_id == 0) {
peer->endpoint.session_id = oc_tcp_get_new_session_id();
}
#endif

#ifdef OC_HAS_FEATURE_TCP_ASYNC_CONNECT
if ((peer->endpoint.flags & TCP) != 0) {
int state = oc_tcp_connect(&peer->endpoint, oc_tls_on_tcp_connect, NULL);
Expand Down

0 comments on commit 446fdaf

Please sign in to comment.