mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2025-12-25 03:40:07 +01:00
core: add nm_utils_sysctl_ip_conf_is_path() util
This commit is contained in:
parent
32b3eb1181
commit
cd271d5cb1
3 changed files with 80 additions and 1 deletions
|
|
@ -2547,7 +2547,7 @@ nm_utils_sysctl_ip_conf_path (int addr_family, char *buf, const char *ifname, co
|
||||||
nm_assert (buf);
|
nm_assert (buf);
|
||||||
nm_assert_addr_family (addr_family);
|
nm_assert_addr_family (addr_family);
|
||||||
|
|
||||||
ifname = NM_ASSERT_VALID_PATH_COMPONENT (ifname);
|
g_assert (nm_utils_is_valid_iface_name (ifname, NULL));
|
||||||
property = NM_ASSERT_VALID_PATH_COMPONENT (property);
|
property = NM_ASSERT_VALID_PATH_COMPONENT (property);
|
||||||
|
|
||||||
len = g_snprintf (buf,
|
len = g_snprintf (buf,
|
||||||
|
|
@ -2560,6 +2560,55 @@ nm_utils_sysctl_ip_conf_path (int addr_family, char *buf, const char *ifname, co
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gboolean
|
||||||
|
nm_utils_sysctl_ip_conf_is_path (int addr_family, const char *path, const char *ifname, const char *property)
|
||||||
|
{
|
||||||
|
g_return_val_if_fail (path, FALSE);
|
||||||
|
NM_ASSERT_VALID_PATH_COMPONENT (property);
|
||||||
|
g_assert (!ifname || nm_utils_is_valid_iface_name (ifname, NULL));
|
||||||
|
|
||||||
|
if (addr_family == AF_INET) {
|
||||||
|
if (!g_str_has_prefix (path, IPV4_PROPERTY_DIR))
|
||||||
|
return FALSE;
|
||||||
|
path += NM_STRLEN (IPV4_PROPERTY_DIR);
|
||||||
|
} else if (addr_family == AF_INET6) {
|
||||||
|
if (!g_str_has_prefix (path, IPV6_PROPERTY_DIR))
|
||||||
|
return FALSE;
|
||||||
|
path += NM_STRLEN (IPV6_PROPERTY_DIR);
|
||||||
|
} else
|
||||||
|
g_return_val_if_reached (FALSE);
|
||||||
|
|
||||||
|
if (ifname) {
|
||||||
|
if (!g_str_has_prefix (path, ifname))
|
||||||
|
return FALSE;
|
||||||
|
path += strlen (ifname);
|
||||||
|
if (path[0] != '/')
|
||||||
|
return FALSE;
|
||||||
|
path++;
|
||||||
|
} else {
|
||||||
|
const char *slash;
|
||||||
|
char buf[IFNAMSIZ];
|
||||||
|
gsize l;
|
||||||
|
|
||||||
|
slash = strchr (path, '/');
|
||||||
|
if (!slash)
|
||||||
|
return FALSE;
|
||||||
|
l = slash - path;
|
||||||
|
if (l >= IFNAMSIZ)
|
||||||
|
return FALSE;
|
||||||
|
memcpy (buf, path, l);
|
||||||
|
buf[l] = '\0';
|
||||||
|
if (!nm_utils_is_valid_iface_name (buf, NULL))
|
||||||
|
return FALSE;
|
||||||
|
path = slash + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!nm_streq (path, property))
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
nm_utils_is_valid_path_component (const char *name)
|
nm_utils_is_valid_path_component (const char *name)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -253,6 +253,8 @@ const char *NM_ASSERT_VALID_PATH_COMPONENT (const char *name);
|
||||||
|
|
||||||
const char *nm_utils_sysctl_ip_conf_path (int addr_family, char *buf, const char *ifname, const char *property);
|
const char *nm_utils_sysctl_ip_conf_path (int addr_family, char *buf, const char *ifname, const char *property);
|
||||||
|
|
||||||
|
gboolean nm_utils_sysctl_ip_conf_is_path (int addr_family, const char *path, const char *ifname, const char *property);
|
||||||
|
|
||||||
gboolean nm_utils_is_specific_hostname (const char *name);
|
gboolean nm_utils_is_specific_hostname (const char *name);
|
||||||
|
|
||||||
int nm_utils_fd_get_contents (int fd,
|
int nm_utils_fd_get_contents (int fd,
|
||||||
|
|
|
||||||
|
|
@ -272,6 +272,32 @@ test_nm_utils_log_connection_diff (void)
|
||||||
|
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
|
|
||||||
|
static void
|
||||||
|
do_test_sysctl_ip_conf (int addr_family,
|
||||||
|
const char *iface,
|
||||||
|
const char *property)
|
||||||
|
{
|
||||||
|
char path[NM_UTILS_SYSCTL_IP_CONF_PATH_BUFSIZE];
|
||||||
|
const char *pp;
|
||||||
|
|
||||||
|
pp = nm_utils_sysctl_ip_conf_path (addr_family, path, iface, property);
|
||||||
|
g_assert (pp == path);
|
||||||
|
g_assert (path[0] == '/');
|
||||||
|
|
||||||
|
g_assert (nm_utils_sysctl_ip_conf_is_path (addr_family, path, iface, property));
|
||||||
|
g_assert (nm_utils_sysctl_ip_conf_is_path (addr_family, path, NULL, property));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_nm_utils_sysctl_ip_conf_path (void)
|
||||||
|
{
|
||||||
|
do_test_sysctl_ip_conf (AF_INET6, "a", "mtu");
|
||||||
|
do_test_sysctl_ip_conf (AF_INET6, "eth0", "mtu");
|
||||||
|
do_test_sysctl_ip_conf (AF_INET6, "e23456789012345", "mtu");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*****************************************************************************/
|
||||||
|
|
||||||
static NMConnection *
|
static NMConnection *
|
||||||
_match_connection_new (void)
|
_match_connection_new (void)
|
||||||
{
|
{
|
||||||
|
|
@ -1716,6 +1742,8 @@ main (int argc, char **argv)
|
||||||
g_test_add_func ("/general/nm_utils_ip6_address_same_prefix", test_nm_utils_ip6_address_same_prefix);
|
g_test_add_func ("/general/nm_utils_ip6_address_same_prefix", test_nm_utils_ip6_address_same_prefix);
|
||||||
g_test_add_func ("/general/nm_utils_log_connection_diff", test_nm_utils_log_connection_diff);
|
g_test_add_func ("/general/nm_utils_log_connection_diff", test_nm_utils_log_connection_diff);
|
||||||
|
|
||||||
|
g_test_add_func ("/general/nm_utils_sysctl_ip_conf_path", test_nm_utils_sysctl_ip_conf_path);
|
||||||
|
|
||||||
g_test_add_func ("/general/exp10", test_nm_utils_exp10);
|
g_test_add_func ("/general/exp10", test_nm_utils_exp10);
|
||||||
|
|
||||||
g_test_add_func ("/general/connection-match/basic", test_connection_match_basic);
|
g_test_add_func ("/general/connection-match/basic", test_connection_match_basic);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue