src/profeth: fix system IP address/netmask and gateway query

Replace the old p-net API for querying the interface address, netmask
and gateway with the new ones that use getifaddrs() and inspect the
kernel routing table.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2023-03-06 10:50:58 +01:00
parent e33f191d07
commit c0c087e719
2 changed files with 34 additions and 4 deletions
+16 -3
View File
@@ -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:
*/
+18 -1
View File
@@ -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);