From e98f5d37f55aa9b8767886342c9fd678ad20bae7 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Tue, 17 Sep 2019 09:51:26 +0300 Subject: [PATCH] net: sockets: Store socket private data into its own variable Do not try to re-use net_context.user_data field as in many places (like in accept) it is expected to contain pointer to net_context. Storing the socket flags will corrupt the value. To simplify and make things less error prone, use socket specific field in net_context to store the socket flags. Fixes #19191 Signed-off-by: Jukka Rissanen --- include/net/net_context.h | 3 +++ subsys/net/lib/sockets/sockets.c | 3 +++ subsys/net/lib/sockets/sockets_internal.h | 6 +++--- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/net/net_context.h b/include/net/net_context.h index 39c46e8a445..9d90fddc599 100644 --- a/include/net/net_context.h +++ b/include/net/net_context.h @@ -257,6 +257,9 @@ struct net_context { #endif /* CONFIG_NET_CONTEXT_SYNC_RECV */ #if defined(CONFIG_NET_SOCKETS) + /** BSD socket private data */ + void *socket_data; + /** Per-socket packet or connection queues */ union { struct k_fifo recv_q; diff --git a/subsys/net/lib/sockets/sockets.c b/subsys/net/lib/sockets/sockets.c index b57f19c0900..b808cbc1cab 100644 --- a/subsys/net/lib/sockets/sockets.c +++ b/subsys/net/lib/sockets/sockets.c @@ -111,6 +111,9 @@ int zsock_socket_internal(int family, int type, int proto) /* Initialize user_data, all other calls will preserve it */ ctx->user_data = NULL; + /* The socket flags are stored here */ + ctx->socket_data = NULL; + /* recv_q and accept_q are in union */ k_fifo_init(&ctx->recv_q); diff --git a/subsys/net/lib/sockets/sockets_internal.h b/subsys/net/lib/sockets/sockets_internal.h index 356ab7c4827..22c525732de 100644 --- a/subsys/net/lib/sockets/sockets_internal.h +++ b/subsys/net/lib/sockets/sockets_internal.h @@ -15,15 +15,15 @@ static inline void sock_set_flag(struct net_context *ctx, uintptr_t mask, uintptr_t flag) { - uintptr_t val = POINTER_TO_UINT(ctx->user_data); + uintptr_t val = POINTER_TO_UINT(ctx->socket_data); val = (val & ~mask) | flag; - (ctx)->user_data = UINT_TO_POINTER(val); + (ctx)->socket_data = UINT_TO_POINTER(val); } static inline uintptr_t sock_get_flag(struct net_context *ctx, uintptr_t mask) { - return POINTER_TO_UINT(ctx->user_data) & mask; + return POINTER_TO_UINT(ctx->socket_data) & mask; } #define sock_is_eof(ctx) sock_get_flag(ctx, SOCK_EOF)