platform: rename variable "IS_IPv4" in platform code

The variable with this purpose is usually called "IS_IPv4".

It's upper case, because usually this is a const variable, and because
it reminds of the NM_IS_IPv4(addr_family) macro. That letter case
is unusual, but it makes sense to me for the special purpose that this
variable has.

Anyway. The naming of this variable is a different point. Let's
use the variable name that is consistent and widely used.

(cherry picked from commit 8085c0121f)
(cherry picked from commit eec32669a9)
(cherry picked from commit 99cd6ed25e)
This commit is contained in:
Thomas Haller 2022-02-03 17:57:14 +01:00 committed by Beniamino Galvani
parent 6aa3d40199
commit d64930f7eb

View file

@ -3246,7 +3246,7 @@ _new_from_nl_addr(struct nlmsghdr *nlh, gboolean id_only)
};
struct nlattr * tb[G_N_ELEMENTS(policy)];
const struct ifaddrmsg *ifa;
gboolean is_v4;
gboolean IS_IPv4;
nm_auto_nmpobj NMPObject *obj = NULL;
int addr_len;
guint32 lifetime, preferred, timestamp;
@ -3256,29 +3256,31 @@ _new_from_nl_addr(struct nlmsghdr *nlh, gboolean id_only)
ifa = nlmsg_data(nlh);
if (!NM_IN_SET(ifa->ifa_family, AF_INET, AF_INET6))
if (ifa->ifa_family == AF_INET)
IS_IPv4 = TRUE;
else if (ifa->ifa_family == AF_INET6)
IS_IPv4 = FALSE;
else
return NULL;
is_v4 = ifa->ifa_family == AF_INET;
if (nlmsg_parse_arr(nlh, sizeof(*ifa), tb, policy) < 0)
return NULL;
addr_len = is_v4 ? sizeof(in_addr_t) : sizeof(struct in6_addr);
addr_len = IS_IPv4 ? sizeof(in_addr_t) : sizeof(struct in6_addr);
if (ifa->ifa_prefixlen > (is_v4 ? 32 : 128))
if (ifa->ifa_prefixlen > (IS_IPv4 ? 32 : 128))
return NULL;
/*****************************************************************/
obj = nmp_object_new(is_v4 ? NMP_OBJECT_TYPE_IP4_ADDRESS : NMP_OBJECT_TYPE_IP6_ADDRESS, NULL);
obj = nmp_object_new(IS_IPv4 ? NMP_OBJECT_TYPE_IP4_ADDRESS : NMP_OBJECT_TYPE_IP6_ADDRESS, NULL);
obj->ip_address.ifindex = ifa->ifa_index;
obj->ip_address.plen = ifa->ifa_prefixlen;
_check_addr_or_return_null(tb, IFA_ADDRESS, addr_len);
_check_addr_or_return_null(tb, IFA_LOCAL, addr_len);
if (is_v4) {
if (IS_IPv4) {
/* For IPv4, kernel omits IFA_LOCAL/IFA_ADDRESS if (and only if) they
* are effectively 0.0.0.0 (all-zero). */
if (tb[IFA_LOCAL])
@ -3314,7 +3316,7 @@ _new_from_nl_addr(struct nlmsghdr *nlh, gboolean id_only)
obj->ip_address.n_ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifa->ifa_flags;
if (is_v4) {
if (IS_IPv4) {
if (tb[IFA_LABEL]) {
char label[IFNAMSIZ];
@ -3365,7 +3367,7 @@ _new_from_nl_route(struct nlmsghdr *nlh, gboolean id_only)
};
const struct rtmsg *rtm;
struct nlattr * tb[G_N_ELEMENTS(policy)];
gboolean is_v4;
gboolean IS_IPv4;
nm_auto_nmpobj NMPObject *obj = NULL;
int addr_len;
struct {
@ -3392,7 +3394,11 @@ _new_from_nl_route(struct nlmsghdr *nlh, gboolean id_only)
* only handle ~supported~ routes.
*****************************************************************/
if (!NM_IN_SET(rtm->rtm_family, AF_INET, AF_INET6))
if (rtm->rtm_family == AF_INET)
IS_IPv4 = TRUE;
else if (rtm->rtm_family == AF_INET6)
IS_IPv4 = FALSE;
else
return NULL;
if (!NM_IN_SET(rtm->rtm_type, RTN_UNICAST, RTN_LOCAL))
@ -3403,10 +3409,9 @@ _new_from_nl_route(struct nlmsghdr *nlh, gboolean id_only)
/*****************************************************************/
is_v4 = rtm->rtm_family == AF_INET;
addr_len = is_v4 ? sizeof(in_addr_t) : sizeof(struct in6_addr);
addr_len = IS_IPv4 ? sizeof(in_addr_t) : sizeof(struct in6_addr);
if (rtm->rtm_dst_len > (is_v4 ? 32 : 128))
if (rtm->rtm_dst_len > (IS_IPv4 ? 32 : 128))
return NULL;
/*****************************************************************
@ -3517,7 +3522,7 @@ rta_multipath_done:;
/*****************************************************************/
obj = nmp_object_new(is_v4 ? NMP_OBJECT_TYPE_IP4_ROUTE : NMP_OBJECT_TYPE_IP6_ROUTE, NULL);
obj = nmp_object_new(IS_IPv4 ? NMP_OBJECT_TYPE_IP4_ROUTE : NMP_OBJECT_TYPE_IP6_ROUTE, NULL);
obj->ip_route.is_external = TRUE;
obj->ip_route.type_coerced = nm_platform_route_type_coerce(rtm->rtm_type);
@ -3534,22 +3539,22 @@ rta_multipath_done:;
if (tb[RTA_PRIORITY])
obj->ip_route.metric = nla_get_u32(tb[RTA_PRIORITY]);
if (is_v4)
if (IS_IPv4)
obj->ip4_route.gateway = nh.gateway.addr4;
else
obj->ip6_route.gateway = nh.gateway.addr6;
if (is_v4)
if (IS_IPv4)
obj->ip4_route.scope_inv = nm_platform_route_scope_inv(rtm->rtm_scope);
if (_check_addr_or_return_null(tb, RTA_PREFSRC, addr_len)) {
if (is_v4)
if (IS_IPv4)
memcpy(&obj->ip4_route.pref_src, nla_data(tb[RTA_PREFSRC]), addr_len);
else
memcpy(&obj->ip6_route.pref_src, nla_data(tb[RTA_PREFSRC]), addr_len);
}
if (is_v4)
if (IS_IPv4)
obj->ip4_route.tos = rtm->rtm_tos;
else {
if (tb[RTA_SRC]) {
@ -3571,14 +3576,7 @@ rta_multipath_done:;
obj->ip_route.lock_initrwnd = NM_FLAGS_HAS(lock, 1 << RTAX_INITRWND);
obj->ip_route.lock_mtu = NM_FLAGS_HAS(lock, 1 << RTAX_MTU);
if (!is_v4) {
if (!_nm_platform_kernel_support_detected(NM_PLATFORM_KERNEL_SUPPORT_TYPE_RTA_PREF)) {
/* Detect support for RTA_PREF by inspecting the netlink message.
* RTA_PREF was added in kernel 4.1, dated 21 June, 2015. */
_nm_platform_kernel_support_init(NM_PLATFORM_KERNEL_SUPPORT_TYPE_RTA_PREF,
tb[RTA_PREF] ? 1 : -1);
}
if (!IS_IPv4) {
if (tb[RTA_PREF])
obj->ip6_route.rt_pref = nla_get_u8(tb[RTA_PREF]);
}
@ -4676,23 +4674,23 @@ ip_route_get_lock_flag(const NMPlatformIPRoute *route)
static struct nl_msg *
_nl_msg_new_route(int nlmsg_type, guint16 nlmsgflags, const NMPObject *obj)
{
nm_auto_nlmsg struct nl_msg *msg = NULL;
const NMPClass * klass = NMP_OBJECT_GET_CLASS(obj);
gboolean is_v4 = klass->addr_family == AF_INET;
const guint32 lock = ip_route_get_lock_flag(NMP_OBJECT_CAST_IP_ROUTE(obj));
nm_auto_nlmsg struct nl_msg *msg = NULL;
const NMPClass * klass = NMP_OBJECT_GET_CLASS(obj);
const gboolean IS_IPv4 = NM_IS_IPv4(klass->addr_family);
const guint32 lock = ip_route_get_lock_flag(NMP_OBJECT_CAST_IP_ROUTE(obj));
const guint32 table =
nm_platform_route_table_uncoerce(NMP_OBJECT_CAST_IP_ROUTE(obj)->table_coerced, TRUE);
const struct rtmsg rtmsg = {
.rtm_family = klass->addr_family,
.rtm_tos = is_v4 ? obj->ip4_route.tos : 0,
.rtm_tos = IS_IPv4 ? obj->ip4_route.tos : 0,
.rtm_table = table <= 0xFF ? table : RT_TABLE_UNSPEC,
.rtm_protocol = nmp_utils_ip_config_source_coerce_to_rtprot(obj->ip_route.rt_source),
.rtm_scope =
is_v4 ? nm_platform_route_scope_inv(obj->ip4_route.scope_inv) : RT_SCOPE_NOWHERE,
IS_IPv4 ? nm_platform_route_scope_inv(obj->ip4_route.scope_inv) : RT_SCOPE_NOWHERE,
.rtm_type = nm_platform_route_type_uncoerce(NMP_OBJECT_CAST_IP_ROUTE(obj)->type_coerced),
.rtm_flags = obj->ip_route.r_rtm_flags & ((unsigned) (RTNH_F_ONLINK)),
.rtm_dst_len = obj->ip_route.plen,
.rtm_src_len = is_v4 ? 0 : NMP_OBJECT_CAST_IP6_ROUTE(obj)->src_plen,
.rtm_src_len = IS_IPv4 ? 0 : NMP_OBJECT_CAST_IP6_ROUTE(obj)->src_plen,
};
gsize addr_len;
@ -4706,28 +4704,28 @@ _nl_msg_new_route(int nlmsg_type, guint16 nlmsgflags, const NMPObject *obj)
if (nlmsg_append_struct(msg, &rtmsg) < 0)
goto nla_put_failure;
addr_len = is_v4 ? sizeof(in_addr_t) : sizeof(struct in6_addr);
addr_len = IS_IPv4 ? sizeof(in_addr_t) : sizeof(struct in6_addr);
NLA_PUT(msg,
RTA_DST,
addr_len,
is_v4 ? (gconstpointer) &obj->ip4_route.network
: (gconstpointer) &obj->ip6_route.network);
IS_IPv4 ? (gconstpointer) &obj->ip4_route.network
: (gconstpointer) &obj->ip6_route.network);
if (!is_v4) {
if (!IS_IPv4) {
if (!IN6_IS_ADDR_UNSPECIFIED(&NMP_OBJECT_CAST_IP6_ROUTE(obj)->src))
NLA_PUT(msg, RTA_SRC, addr_len, &obj->ip6_route.src);
}
NLA_PUT_U32(msg,
RTA_PRIORITY,
is_v4 ? nm_platform_ip4_route_get_effective_metric(&obj->ip4_route)
: nm_platform_ip6_route_get_effective_metric(&obj->ip6_route));
IS_IPv4 ? nm_platform_ip4_route_get_effective_metric(&obj->ip4_route)
: nm_platform_ip6_route_get_effective_metric(&obj->ip6_route));
if (table > 0xFF)
NLA_PUT_U32(msg, RTA_TABLE, table);
if (is_v4) {
if (IS_IPv4) {
if (NMP_OBJECT_CAST_IP4_ROUTE(obj)->pref_src)
NLA_PUT(msg, RTA_PREFSRC, addr_len, &obj->ip4_route.pref_src);
} else {
@ -4762,7 +4760,7 @@ _nl_msg_new_route(int nlmsg_type, guint16 nlmsgflags, const NMPObject *obj)
}
/* We currently don't have need for multi-hop routes... */
if (is_v4) {
if (IS_IPv4) {
NLA_PUT(msg, RTA_GATEWAY, addr_len, &obj->ip4_route.gateway);
} else {
if (!IN6_IS_ADDR_UNSPECIFIED(&obj->ip6_route.gateway))
@ -4770,7 +4768,7 @@ _nl_msg_new_route(int nlmsg_type, guint16 nlmsgflags, const NMPObject *obj)
}
NLA_PUT_U32(msg, RTA_OIF, obj->ip_route.ifindex);
if (!is_v4 && obj->ip6_route.rt_pref != NM_ICMPV6_ROUTER_PREF_MEDIUM)
if (!IS_IPv4 && obj->ip6_route.rt_pref != NM_ICMPV6_ROUTER_PREF_MEDIUM)
NLA_PUT_U8(msg, RTA_PREF, obj->ip6_route.rt_pref);
return g_steal_pointer(&msg);
@ -8707,8 +8705,8 @@ ip_route_get(NMPlatform * platform,
int oif_ifindex,
NMPObject ** out_route)
{
const gboolean is_v4 = (addr_family == AF_INET);
const int addr_len = is_v4 ? 4 : 16;
const gboolean IS_IPv4 = NM_IS_IPv4(addr_family);
const int addr_len = IS_IPv4 ? 4 : 16;
int try_count = 0;
WaitForNlResponseResult seq_result;
int nle;
@ -8729,7 +8727,7 @@ ip_route_get(NMPlatform * platform,
.n.nlmsg_type = RTM_GETROUTE,
.r.rtm_family = addr_family,
.r.rtm_tos = 0,
.r.rtm_dst_len = is_v4 ? 32 : 128,
.r.rtm_dst_len = IS_IPv4 ? 32 : 128,
.r.rtm_flags = 0x1000 /* RTM_F_LOOKUP_TABLE */,
};