From f630857e69d89bcce6292d64a3e66bab64d08e3f Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Tue, 5 Sep 2017 21:03:49 -0700 Subject: [PATCH] net: lwm2m: fix message release for separate reply In the case of a proxy server translating HTTP -> COAP (known in the code as "separate reply"), we were leaking lwm2m_message structures. This was due to pending objects being cleared out during the first ACK, and no other way of finding a matching message when the follow up packet was received. Let's add a second match for reply to make sure we can find our matching message resources. NOTE: This change renames find_msg_from_pending() to find_msg() and makes it a static function as it's only used by the lwm2m_engine. Signed-off-by: Michael Scott --- subsys/net/lib/lwm2m/lwm2m_engine.c | 17 +++++++++++++---- subsys/net/lib/lwm2m/lwm2m_engine.h | 1 - 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index bde50bde953..3e6d1e741d0 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -720,11 +720,12 @@ static void zoap_options_to_path(struct zoap_option *opt, int options_count, } } -struct lwm2m_message *find_msg_from_pending(struct zoap_pending *pending) +static struct lwm2m_message *find_msg(struct zoap_pending *pending, + struct zoap_reply *reply) { size_t i; - if (!pending) { + if (!pending && !reply) { return NULL; } @@ -732,6 +733,10 @@ struct lwm2m_message *find_msg_from_pending(struct zoap_pending *pending) if (messages[i].ctx && messages[i].pending == pending) { return &messages[i]; } + + if (messages[i].ctx && messages[i].reply == reply) { + return &messages[i]; + } } return NULL; @@ -2594,7 +2599,7 @@ void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, struct net_pkt *pkt, * is != NULL. */ if (pending) { - msg = find_msg_from_pending(pending); + msg = find_msg(pending, NULL); if (msg) { msg->pending = NULL; } @@ -2621,6 +2626,10 @@ void lwm2m_udp_receive(struct lwm2m_ctx *client_ctx, struct net_pkt *pkt, SYS_LOG_DBG("separated response, not removing reply"); goto cleanup; } + + if (!msg) { + msg = find_msg(pending, reply); + } } if (reply || pending) { @@ -2704,7 +2713,7 @@ static void retransmit_request(struct k_work *work) return; } - msg = find_msg_from_pending(pending); + msg = find_msg(pending, NULL); if (!msg) { SYS_LOG_ERR("pending has no valid LwM2M message!"); return; diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.h b/subsys/net/lib/lwm2m/lwm2m_engine.h index bf1116afb40..4d1d047add7 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.h +++ b/subsys/net/lib/lwm2m/lwm2m_engine.h @@ -86,7 +86,6 @@ int lwm2m_get_or_create_engine_obj(struct lwm2m_engine_context *context, u8_t *created); /* LwM2M message functions */ -struct lwm2m_message *find_msg_from_pending(struct zoap_pending *pending); struct lwm2m_message *lwm2m_get_message(struct lwm2m_ctx *client_ctx); void lwm2m_release_message(struct lwm2m_message *msg); int lwm2m_init_message(struct lwm2m_message *msg);