Skip to content

Commit c0fc269

Browse files
jukkarAnas Nashif
authored andcommitted
net: app: Do not allow local unspecified address in client
Do not allow :: or ANY address in client when sending data. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 59161ce commit c0fc269

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

subsys/net/lib/app/client.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,54 @@ static int connect_dtls(struct net_app_ctx *ctx, struct net_context *orig,
595595
}
596596
#endif /* CONFIG_NET_APP_DTLS */
597597

598+
static void check_local_address(struct net_app_ctx *ctx,
599+
struct net_context *net_ctx)
600+
{
601+
#if defined(CONFIG_NET_IPV6)
602+
if (net_context_get_family(net_ctx) == AF_INET6) {
603+
const struct in6_addr *laddr;
604+
struct in6_addr *raddr;
605+
606+
laddr = &net_sin6(&ctx->ipv6.local)->sin6_addr;
607+
if (!net_is_ipv6_addr_unspecified(laddr)) {
608+
return;
609+
}
610+
611+
raddr = &net_sin6(&ctx->ipv6.remote)->sin6_addr;
612+
613+
laddr = net_if_ipv6_select_src_addr(NULL, raddr);
614+
if (laddr && laddr != net_ipv6_unspecified_address()) {
615+
net_ipaddr_copy(&net_sin6(&ctx->ipv6.local)->sin6_addr,
616+
laddr);
617+
} else {
618+
NET_WARN("Source address is unspecified!");
619+
}
620+
}
621+
#endif
622+
623+
#if defined(CONFIG_NET_IPV4)
624+
if (net_context_get_family(net_ctx) == AF_INET) {
625+
struct in_addr *laddr;
626+
struct net_if *iface;
627+
628+
laddr = &net_sin(&ctx->ipv4.local)->sin_addr;
629+
if (!net_is_ipv4_addr_unspecified(laddr)) {
630+
return;
631+
}
632+
633+
/* Just take the first IPv4 address of an interface */
634+
iface = net_context_get_iface(net_ctx);
635+
if (iface) {
636+
laddr = &iface->ipv4.unicast[0].address.in_addr;
637+
net_ipaddr_copy(&net_sin(&ctx->ipv4.local)->sin_addr,
638+
laddr);
639+
} else {
640+
NET_WARN("Source address is unspecified!");
641+
}
642+
}
643+
#endif
644+
}
645+
598646
int net_app_connect(struct net_app_ctx *ctx, s32_t timeout)
599647
{
600648
struct net_context *net_ctx;
@@ -639,6 +687,11 @@ int net_app_connect(struct net_app_ctx *ctx, s32_t timeout)
639687
ctx->is_enabled = true;
640688

641689
_net_app_print_info(ctx);
690+
} else {
691+
/* We cannot bind to local unspecified address when sending.
692+
* Select proper address depending on remote one in this case.
693+
*/
694+
check_local_address(ctx, net_ctx);
642695
}
643696

644697
#if defined(CONFIG_NET_APP_TLS) || defined(CONFIG_NET_APP_DTLS)

0 commit comments

Comments
 (0)