diff --git a/src/profeth/src/app_utils.c b/src/profeth/src/app_utils.c index 99381f56..3664e1f5 100644 --- a/src/profeth/src/app_utils.c +++ b/src/profeth/src/app_utils.c @@ -564,9 +564,15 @@ int app_utils_pnet_cfg_init_netifs ( } /* Read IP, netmask, gateway from operating system */ - ip = pnal_get_ip_address (if_cfg->main_netif_name); - netmask = pnal_get_netmask (if_cfg->main_netif_name); - gateway = pnal_get_gateway (if_cfg->main_netif_name); + if (pnal_get_ipmask(if_cfg->main_netif_name, &ip, &netmask)) + { + APP_LOG_INFO ("No valid interface address on %s yet.", if_cfg->main_netif_name); + } + gateway = pnal_get_gateway(if_cfg->main_netif_name); + if (gateway == PNAL_IPADDR_INVALID) + { + APP_LOG_INFO ("System has no default gateway set.\n"); + } app_utils_copy_ip_to_struct (&if_cfg->ip_cfg.ip_addr, ip); app_utils_copy_ip_to_struct (&if_cfg->ip_cfg.ip_gateway, gateway); @@ -863,3 +869,10 @@ void app_utils_cyclic_data_poll (app_api_t * p_api) } } } + +/** + * Local Variables: + * indent-tabs-mode: nil + * c-file-style: "ellemtel" + * End: + */ diff --git a/src/profeth/src/pnal.h b/src/profeth/src/pnal.h index f6371cbf..ae1e62b0 100644 --- a/src/profeth/src/pnal.h +++ b/src/profeth/src/pnal.h @@ -487,13 +487,30 @@ pnal_ipaddr_t pnal_get_ip_address (const char * interface_name); */ pnal_ipaddr_t pnal_get_netmask (const char * interface_name); +/** + * Read interface IP address and netmask. For IPv4. + * + * Replacement for pnal_get_ip_address() and pnal_get_netmask(), uses + * getifaddrs() instead of ioctl() to find the best IP address on the + * given interface. + * + * @param interface_name In: Name of network interface + * @param p_ip Out: IP address + * @param p_netmask out: IP netmask + * @return 0 on success and + * -1 if an error occurred. + */ +int pnal_get_ipmask(const char *interface_name, pnal_ipaddr_t *p_ip, pnal_ipaddr_t *p_netmask); + /** * Read the default gateway address as an integer. For IPv4. * * Assumes the default gateway is found on .1 on same subnet as the IP address. + * On Linux systems /proc/net/route is parsed instead, meaning this function + * may fail and return ::PNAL_IPADDR_INVALID. * * @param interface_name In: Name of network interface - * @return netmask + * @return Gateway IP address. */ pnal_ipaddr_t pnal_get_gateway (const char * interface_name);