Skip to content

Commit

Permalink
fixup! tcp: pair connection with the tls peer
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed May 31, 2024
1 parent e0b00f6 commit e63a90d
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 157 deletions.
13 changes: 10 additions & 3 deletions api/oc_client_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -961,16 +961,23 @@ oc_do_ip_discovery_at_endpoint(const char *rt, oc_discovery_handler_t handler,
void
oc_close_session(const oc_endpoint_t *endpoint)
{
if (endpoint->flags & SECURED) {
#ifdef OC_SECURITY
if ((endpoint->flags & SECURED) != 0) {
oc_tls_close_connection(endpoint);
return;
}
#endif /* OC_SECURITY */
} else if (endpoint->flags & TCP) {
#ifdef OC_TCP
if ((endpoint->flags & TCP) != 0) {
oc_endpoint_t session_endpoint;
while (oc_connectivity_end_session_v1(endpoint, false, &session_endpoint)) {
oc_handle_session(&session_endpoint, OC_SESSION_DISCONNECTED);
}
#endif /* OC_TCP */
return;
}
#endif /* OC_TCP */

#if !defined(OC_TCP) && !defined(OC_SECURITY)
(void)endpoint;
#endif /* !OC_TCP && !OC_SECURITY */
}
13 changes: 2 additions & 11 deletions api/oc_endpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ oc_endpoint_compare_session_ids(const oc_endpoint_t *ep1,
}
return ep1->session_id == ep2->session_id ? 0 : -1;
}
#endif
#endif /* OC_TCP */

int
oc_endpoint_compare(const oc_endpoint_t *ep1, const oc_endpoint_t *ep2)
Expand All @@ -711,7 +711,7 @@ oc_endpoint_compare(const oc_endpoint_t *ep1, const oc_endpoint_t *ep2)
return -1;
}
#ifdef OC_IPV4
else if (ep1->flags & IPV4) {
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
Expand All @@ -724,15 +724,6 @@ oc_endpoint_compare(const oc_endpoint_t *ep1, const oc_endpoint_t *ep2)
}
#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
6 changes: 6 additions & 0 deletions api/oc_session_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ oc_session_start_event(const oc_endpoint_t *endpoint)
return;
}

// only a specific session should be connected
assert(endpoint->session_id != 0);

oc_endpoint_t *ep = oc_new_endpoint();
memcpy(ep, endpoint, sizeof(oc_endpoint_t));
ep->next = NULL;
Expand All @@ -152,6 +155,9 @@ oc_session_end_event(const oc_endpoint_t *endpoint)
return;
}

// only a specific session should be disconnected
assert(endpoint->session_id != 0);

oc_endpoint_t *ep = oc_new_endpoint();
memcpy(ep, endpoint, sizeof(oc_endpoint_t));
ep->next = NULL;
Expand Down
63 changes: 36 additions & 27 deletions api/oc_session_events_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,22 @@
#include "oc_endpoint.h"
#include "oc_session_events.h"
#include "util/oc_process.h"
#include "util/oc_features.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Invoke all session handlers associated with given endpoint
*
* @param endpoint endpoint of the session event (cannot be NULLL)
* @param state new session state
*/
void oc_handle_session(const oc_endpoint_t *endpoint, oc_session_state_t state);

#ifdef OC_SESSION_EVENTS

#define OC_SESSION_EVENT_API_V0 (0)
#define OC_SESSION_EVENT_API_V1 (1)

Expand Down Expand Up @@ -61,6 +72,30 @@ typedef struct oc_session_event_cb
void *user_data;
} oc_session_event_cb_t;

/**
* @brief Find first session event callback matching the input parameters.
*
* @param cb handler to match
* @param user_data match user data (only valid for v1 handlers)
* @param ignore_user_data ignore user data for match (only valid for v1
* handlers)
* @return oc_session_event_cb_t* first matched session event callback
* @return NULL if no match is found
*/
oc_session_event_cb_t *oc_session_event_callback_find(
session_event_versioned_handler_t cb, const void *user_data,
bool ignore_user_data);

/**
* @brief Remove all previously registered session event notifications
* callbacks.
*/
void oc_session_events_remove_all_callbacks(void);

#endif /* OC_SESSION_EVENTS */

#ifdef OC_TCP

OC_PROCESS_NAME(oc_session_events);

/**
Expand All @@ -77,14 +112,6 @@ void oc_session_start_event(const oc_endpoint_t *endpoint);
*/
void oc_session_end_event(const oc_endpoint_t *endpoint);

/**
* @brief Invoke all session handlers associated with given endpoint
*
* @param endpoint endpoint of the session event (cannot be NULLL)
* @param state new session state
*/
void oc_handle_session(const oc_endpoint_t *endpoint, oc_session_state_t state);

/**
* @brief Check if session events are currently in the process of being
* disconnected.
Expand All @@ -95,25 +122,7 @@ void oc_handle_session(const oc_endpoint_t *endpoint, oc_session_state_t state);
*/
bool oc_session_events_disconnect_is_ongoing(void);

/**
* @brief Find first session event callback matching the input parameters.
*
* @param cb handler to match
* @param user_data match user data (only valid for v1 handlers)
* @param ignore_user_data ignore user data for match (only valid for v1
* handlers)
* @return oc_session_event_cb_t* first matched session event callback
* @return NULL if no match is found
*/
oc_session_event_cb_t *oc_session_event_callback_find(
session_event_versioned_handler_t cb, const void *user_data,
bool ignore_user_data);

/**
* @brief Remove all previously registered session event notifications
* callbacks.
*/
void oc_session_events_remove_all_callbacks(void);
#endif /* OC_TCP */

#ifdef __cplusplus
}
Expand Down
18 changes: 9 additions & 9 deletions api/oc_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@
#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 Expand Up @@ -96,6 +87,15 @@ oc_tcp_on_connect_event_free(oc_tcp_on_connect_event_t *event)

#endif /* OC_HAS_FEATURE_TCP_ASYNC_CONNECT */

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;
}

bool
oc_tcp_is_valid_header(const uint8_t *data, size_t data_size, bool is_tls)
{
Expand Down
2 changes: 1 addition & 1 deletion api/oc_tcp_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@

#ifdef OC_TCP

#include <stdint.h>
#include "messaging/coap/constants.h"
#include "port/oc_connectivity.h"
#include "oc_endpoint.h"
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
Expand Down
17 changes: 11 additions & 6 deletions port/android/tcpadapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ free_tcp_session(tcp_session_t *session, bool notify_session_end)

static int
add_new_session(int sock, ip_context_t *dev, oc_endpoint_t *endpoint,
tcp_csm_state_t state)
uint32_t session_id, tcp_csm_state_t state)
{
long if_index = get_interface_index(sock);
if (if_index == -1) {
Expand All @@ -235,8 +235,10 @@ 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) {
if (session_id == 0) {
session->endpoint.session_id = oc_tcp_get_new_session_id();
} else {
session->endpoint.session_id = session_id;
}

oc_list_add(session_list, session);
Expand Down Expand Up @@ -280,7 +282,8 @@ accept_new_session(ip_context_t *dev, int fd, fd_set *setfds,

FD_CLR(fd, setfds);

if (add_new_session(new_socket, dev, endpoint, CSM_NONE) < 0) {
if (add_new_session(new_socket, dev, endpoint, /*session_id*/ 0, CSM_NONE) <
0) {
OC_ERR("could not record new TCP session");
close(new_socket);
return -1;
Expand Down Expand Up @@ -495,6 +498,7 @@ get_session_socket(const oc_endpoint_t *endpoint)

static int
initiate_new_session(ip_context_t *dev, oc_endpoint_t *endpoint,
uint32_t session_id,
const struct sockaddr_storage *receiver)
{
int sock = -1;
Expand All @@ -517,7 +521,7 @@ initiate_new_session(ip_context_t *dev, oc_endpoint_t *endpoint,

OC_DBG("successfully initiated TCP connection");

if (add_new_session(sock, dev, endpoint, CSM_SENT) < 0) {
if (add_new_session(sock, dev, endpoint, session_id, CSM_SENT) < 0) {
OC_ERR("could not record new TCP session");
close(sock);
return -1;
Expand Down Expand Up @@ -549,8 +553,9 @@ oc_tcp_send_buffer(ip_context_t *dev, oc_message_t *message,
OC_ERR("connection was closed");
goto oc_tcp_send_buffer_done;
}
if ((send_sock = initiate_new_session(dev, &message->endpoint, receiver)) <
0) {
if ((send_sock =
initiate_new_session(dev, &message->endpoint,
message->endpoint.session_id, receiver)) < 0) {
OC_ERR("could not initiate new TCP session");
goto oc_tcp_send_buffer_done;
}
Expand Down
23 changes: 14 additions & 9 deletions port/esp32/adapter/src/tcpadapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@
#include "port/oc_tcp_socket_internal.h"
#include "tcpadapter.h"
#include "util/oc_memb.h"
#include "vfs_pipe.h"

#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <esp_netif.h>
#include <fcntl.h>
// #include <ifaddrs.h>
#include "esp_netif.h"
#include "vfs_pipe.h"
#include <net/if.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/socket.h>
#include <unistd.h>

Expand Down Expand Up @@ -184,7 +184,7 @@ free_tcp_session(tcp_session_t *session, bool notify_session_end)

static int
add_new_session(int sock, ip_context_t *dev, oc_endpoint_t *endpoint,
tcp_csm_state_t state)
uint32_t session_id, tcp_csm_state_t state)
{
int if_index = get_interface_index(sock);
if (if_index < 0) {
Expand All @@ -205,8 +205,10 @@ 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) {
if (session_id == 0) {
session->endpoint.session_id = oc_tcp_get_new_session_id();
} else {
session->endpoint.session_id = session_id;
}

oc_list_add(session_list, session);
Expand Down Expand Up @@ -251,7 +253,8 @@ accept_new_session(ip_context_t *dev, int fd, fd_set *setfds,

FD_CLR(fd, setfds);

if (add_new_session(new_socket, dev, endpoint, CSM_NONE) < 0) {
if (add_new_session(new_socket, dev, endpoint, /*session_id*/ 0, CSM_NONE) <
0) {
OC_ERR("could not record new TCP session");
close(new_socket);
return -1;
Expand Down Expand Up @@ -468,6 +471,7 @@ get_session_socket(const oc_endpoint_t *endpoint)

static int
initiate_new_session(ip_context_t *dev, oc_endpoint_t *endpoint,
uint32_t session_id,
const struct sockaddr_storage *receiver)
{
int sock = -1;
Expand All @@ -490,7 +494,7 @@ initiate_new_session(ip_context_t *dev, oc_endpoint_t *endpoint,

OC_DBG("successfully initiated TCP connection");

if (add_new_session(sock, dev, endpoint, CSM_SENT) < 0) {
if (add_new_session(sock, dev, endpoint, session_id, CSM_SENT) < 0) {
OC_ERR("could not record new TCP session");
close(sock);
return -1;
Expand Down Expand Up @@ -522,8 +526,9 @@ oc_tcp_send_buffer(ip_context_t *dev, oc_message_t *message,
OC_ERR("connection was closed");
goto oc_tcp_send_buffer_done;
}
if ((send_sock = initiate_new_session(dev, &message->endpoint, receiver)) <
0) {
if ((send_sock =
initiate_new_session(dev, &message->endpoint,
message->endpoint.session_id, receiver)) < 0) {
OC_ERR("could not initiate new TCP session");
goto oc_tcp_send_buffer_done;
}
Expand Down
Loading

0 comments on commit e63a90d

Please sign in to comment.