@@ -117,41 +117,54 @@ void wifi_fpm_set_sleep_type (sleep_type_t type)
117117uint32_t global_ipv4_netfmt = 0 ; // global binding
118118
119119netif netif0;
120+ uint32_t global_source_address = INADDR_ANY;
120121
121122bool wifi_get_ip_info (uint8 if_index, struct ip_info *info)
122123{
124+ // emulate wifi_get_ip_info()
125+ // ignore if_index
126+ // use global option -i (host_interface) to select bound interface/address
127+
123128 struct ifaddrs * ifAddrStruct = NULL , * ifa = NULL ;
124129 uint32_t ipv4 = lwip_htonl (0x7f000001 );
125130 uint32_t mask = lwip_htonl (0xff000000 );
131+ global_source_address = INADDR_ANY; // =0
126132
127133 if (getifaddrs (&ifAddrStruct) != 0 )
128134 {
129135 perror (" getifaddrs" );
130136 exit (EXIT_FAILURE);
131137 }
138+ if (host_interface)
139+ mockverbose (" host: looking for interface '%s':\n " , host_interface);
140+ else
141+ mockverbose (" host: looking the first for non-local IPv4 interface:\n " );
132142 for (ifa = ifAddrStruct; ifa != NULL ; ifa = ifa->ifa_next )
133143 {
144+ mockverbose (" host: interface: %s" , ifa->ifa_name );
134145 if ( ifa->ifa_addr
135146 && ifa->ifa_addr ->sa_family == AF_INET // ip_info is IPv4 only
136147 )
137148 {
138- if (lwip_ntohl (*(uint32_t *)&((struct sockaddr_in *) ifa->ifa_netmask )->sin_addr ) != 0xff000000 )
149+ auto test_ipv4 = lwip_ntohl (*(uint32_t *)&((struct sockaddr_in *)ifa->ifa_addr )->sin_addr );
150+ mockverbose (" IPV4 (0x%08lx)" , test_ipv4);
151+ if ((test_ipv4 & 0xff000000 ) == 0x7f000000 )
152+ // 127./8
153+ mockverbose (" (local, ignored)" );
154+ else
139155 {
140- if (ipv4 == lwip_htonl (0x7f000001 ))
141- {
142- // take the first by default
143- ipv4 = *(uint32_t *)&((struct sockaddr_in *)ifa->ifa_addr )->sin_addr ;
144- mask = *(uint32_t *)&((struct sockaddr_in *)ifa->ifa_netmask )->sin_addr ;
145- }
146- if (host_interface && strcmp (ifa->ifa_name , host_interface) == 0 )
156+ if (!host_interface || (host_interface && strcmp (ifa->ifa_name , host_interface) == 0 ))
147157 {
148- // .. or the one specified by user on cmdline
158+ // use the first non-local interface, or, if specified, the one selected by user on cmdline
149159 ipv4 = *(uint32_t *)&((struct sockaddr_in *)ifa->ifa_addr )->sin_addr ;
150160 mask = *(uint32_t *)&((struct sockaddr_in *)ifa->ifa_netmask )->sin_addr ;
161+ mockverbose (" (selected)\n " );
162+ global_source_address = ntohl (ipv4);
151163 break ;
152164 }
153165 }
154166 }
167+ mockverbose (" \n " );
155168 }
156169 if (ifAddrStruct != NULL )
157170 freeifaddrs (ifAddrStruct);
0 commit comments