@@ -595,6 +595,54 @@ static int connect_dtls(struct net_app_ctx *ctx, struct net_context *orig,
595
595
}
596
596
#endif /* CONFIG_NET_APP_DTLS */
597
597
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
+
598
646
int net_app_connect (struct net_app_ctx * ctx , s32_t timeout )
599
647
{
600
648
struct net_context * net_ctx ;
@@ -639,6 +687,11 @@ int net_app_connect(struct net_app_ctx *ctx, s32_t timeout)
639
687
ctx -> is_enabled = true;
640
688
641
689
_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 );
642
695
}
643
696
644
697
#if defined(CONFIG_NET_APP_TLS ) || defined(CONFIG_NET_APP_DTLS )
0 commit comments