Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions include/net/net_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
82 changes: 51 additions & 31 deletions subsys/net/lib/app/net_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) &&
Expand All @@ -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 */

Expand Down Expand Up @@ -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)
Expand Down