diff --git a/include/net/net_app.h b/include/net/net_app.h index 1ce62198043a4..2c12d72740760 100644 --- a/include/net/net_app.h +++ b/include/net/net_app.h @@ -855,6 +855,19 @@ struct net_pkt *net_app_get_net_pkt(struct net_app_ctx *ctx, sa_family_t family, s32_t timeout); +/** + * @brief Create network packet based on dst address. + * + * @param ctx Network application context. + * @param dst Destination address to select net_context + * @param timeout How long to wait the send before giving up. + * + * @return valid net_pkt if ok, NULL if error. + */ +struct net_pkt *net_app_get_net_pkt_with_dst(struct net_app_ctx *ctx, + const struct sockaddr *dst, + s32_t timeout); + /** * @brief Create network buffer that will hold network data. * diff --git a/subsys/net/lib/app/net_app.c b/subsys/net/lib/app/net_app.c index 3fa0327a3ffcd..fdcb5ac228a19 100644 --- a/subsys/net/lib/app/net_app.c +++ b/subsys/net/lib/app/net_app.c @@ -562,27 +562,45 @@ struct net_context *select_client_ctx(struct net_app_ctx *ctx, #if defined(CONFIG_NET_APP_SERVER) #if defined(CONFIG_NET_TCP) -static struct net_context *get_server_ctx(struct net_app_ctx *ctx, - const struct sockaddr *dst) + +static struct net_context *get_server_ctx_without_dst(struct net_app_ctx *ctx) { int i; for (i = 0; i < CONFIG_NET_APP_SERVER_NUM_CONN; i++) { struct net_context *tmp = ctx->server.net_ctxs[i]; - u16_t port, rport; if (!tmp || !net_context_is_used(tmp)) { continue; } - if (!dst) { - if (tmp->net_app == ctx) { - NET_DBG("Selecting net_ctx %p iface %p for " - "NULL dst", - tmp, net_context_get_iface(tmp)); - return tmp; - } + if (tmp->net_app != ctx) { + continue; + } + NET_DBG("Selecting net_ctx %p iface %p for NULL dst", + tmp, net_context_get_iface(tmp)); + + return tmp; + } + + return NULL; +} + +static struct net_context *get_server_ctx(struct net_app_ctx *ctx, + const struct sockaddr *dst) +{ + int i; + + if (!dst) { + return get_server_ctx_without_dst(ctx); + } + + for (i = 0; i < CONFIG_NET_APP_SERVER_NUM_CONN; i++) { + struct net_context *tmp = ctx->server.net_ctxs[i]; + u16_t port, rport; + + if (!tmp || !net_context_is_used(tmp)) { continue; } @@ -606,16 +624,6 @@ static struct net_context *get_server_ctx(struct net_app_ctx *ctx, ntohs(rport)); return tmp; } - - if (tmp->net_app == ctx) { - NET_DBG("Selecting net_ctx %p iface %p" - " for %s port %d", tmp, - net_context_get_iface(tmp), - dst->sa_family == AF_UNSPEC ? - "AF_UNSPEC" : "AF_INET6", - ntohs(rport)); - return tmp; - } } if (IS_ENABLED(CONFIG_NET_IPV4) && @@ -637,20 +645,10 @@ static struct net_context *get_server_ctx(struct net_app_ctx *ctx, ntohs(port)); return tmp; } - - if (tmp->net_app == ctx) { - NET_DBG("Selecting net_ctx %p iface %p" - " for %s port %d", tmp, - net_context_get_iface(tmp), - dst->sa_family == AF_UNSPEC ? - "AF_UNSPEC" : "AF_INET", - ntohs(port)); - return tmp; - } } } - return NULL; + return get_server_ctx_without_dst(ctx); } #endif /* CONFIG_NET_TCP */ @@ -965,6 +963,28 @@ struct net_pkt *net_app_get_net_pkt(struct net_app_ctx *ctx, return net_pkt_get_tx(net_ctx, timeout); } +struct net_pkt *net_app_get_net_pkt_with_dst(struct net_app_ctx *ctx, + const struct sockaddr *dst, + s32_t timeout) +{ + struct net_context *net_ctx; + + if (!ctx || !dst) { + return NULL; + } + + if (!ctx->is_init) { + return NULL; + } + + net_ctx = _net_app_select_net_ctx(ctx, dst); + if (!net_ctx) { + return NULL; + } + + return net_pkt_get_tx(net_ctx, timeout); +} + struct net_buf *net_app_get_net_buf(struct net_app_ctx *ctx, struct net_pkt *pkt, s32_t timeout)