net: lwm2m: Clean up context on stop

When lwm2m_rd_client_stop() was called and immediately
followed by lwm2m_rd_client_start() it leaked the file
handle for existing socket.

Problem can be fixed when rd_client_stop() does not
move state machine to IDLE, but instead DEREGISTER
and then allow state machine to move forward.

I added a blocking wait for rd_client_stop() because
it needs to wait for proper clean up.

I also move couple of lwm2m_engine_context_close() to
set_sm_state() event handler or similarly in lwm2m_engine.c
there was couple of places where context was not properly
cleaned.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
This commit is contained in:
Seppo Takalo 2022-03-30 15:24:37 +03:00 committed by Carles Cufí
commit 43c988d43e
2 changed files with 38 additions and 18 deletions

View file

@ -5591,8 +5591,9 @@ int lwm2m_socket_start(struct lwm2m_ctx *client_ctx)
{
socklen_t addr_len;
int flags;
#if defined(CONFIG_LWM2M_DTLS_SUPPORT)
int ret;
#if defined(CONFIG_LWM2M_DTLS_SUPPORT)
uint8_t tmp;
if (client_ctx->load_credentials) {
@ -5636,10 +5637,9 @@ int lwm2m_socket_start(struct lwm2m_ctx *client_ctx)
ret = setsockopt(client_ctx->sock_fd, SOL_TLS, TLS_SEC_TAG_LIST,
tls_tag_list, sizeof(tls_tag_list));
if (ret < 0) {
LOG_ERR("Failed to set TLS_SEC_TAG_LIST option: %d",
errno);
lwm2m_engine_context_close(client_ctx);
return -errno;
ret = -errno;
LOG_ERR("Failed to set TLS_SEC_TAG_LIST option: %d", ret);
goto error;
}
if (client_ctx->hostname_verify && (client_ctx->desthostname != NULL)) {
@ -5656,9 +5656,9 @@ int lwm2m_socket_start(struct lwm2m_ctx *client_ctx)
/** restore character */
client_ctx->desthostname[client_ctx->desthostnamelen] = tmp;
if (ret < 0) {
LOG_ERR("Failed to set TLS_HOSTNAME option: %d", errno);
lwm2m_engine_context_close(client_ctx);
return -errno;
ret = -errno;
LOG_ERR("Failed to set TLS_HOSTNAME option: %d", ret);
goto error;
}
}
}
@ -5674,18 +5674,31 @@ int lwm2m_socket_start(struct lwm2m_ctx *client_ctx)
if (connect(client_ctx->sock_fd, &client_ctx->remote_addr,
addr_len) < 0) {
LOG_ERR("Cannot connect UDP (-%d)", errno);
lwm2m_engine_context_close(client_ctx);
return -errno;
ret = -errno;
LOG_ERR("Cannot connect UDP (%d)", ret);
goto error;
}
flags = fcntl(client_ctx->sock_fd, F_GETFL, 0);
if (flags == -1) {
return -errno;
ret = -errno;
LOG_ERR("fcntl(F_GETFL) failed (%d)", ret);
goto error;
}
fcntl(client_ctx->sock_fd, F_SETFL, flags | O_NONBLOCK);
ret = fcntl(client_ctx->sock_fd, F_SETFL, flags | O_NONBLOCK);
if (ret == -1) {
ret = -errno;
LOG_ERR("fcntl(F_SETFL) failed (%d)", ret);
goto error;
}
LOG_INF("Connected, sock id %d", client_ctx->sock_fd);
return lwm2m_socket_add(client_ctx);
error:
lwm2m_engine_context_close(client_ctx);
return ret;
}
int lwm2m_parse_peerinfo(char *url, struct lwm2m_ctx *client_ctx, bool is_firmware_uri)