mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-02-19 13:30:36 +01:00
glib-aux: move nm_utils_ip[46]_address_*() helpers
This commit is contained in:
parent
ec22551ce9
commit
57735e1e8e
3 changed files with 171 additions and 167 deletions
|
|
@ -98,73 +98,6 @@ void _nm_singleton_instance_register_destruction(GObject *instance);
|
|||
gboolean nm_ether_addr_is_valid(const NMEtherAddr *addr);
|
||||
gboolean nm_ether_addr_is_valid_str(const char *str);
|
||||
|
||||
gconstpointer
|
||||
nm_utils_ipx_address_clear_host_address(int family, gpointer dst, gconstpointer src, guint8 plen);
|
||||
in_addr_t nm_utils_ip4_address_clear_host_address(in_addr_t addr, guint8 plen);
|
||||
const struct in6_addr *nm_utils_ip6_address_clear_host_address(struct in6_addr * dst,
|
||||
const struct in6_addr *src,
|
||||
guint8 plen);
|
||||
|
||||
static inline int
|
||||
nm_utils_ip4_address_same_prefix_cmp(in_addr_t addr_a, in_addr_t addr_b, guint8 plen)
|
||||
{
|
||||
NM_CMP_DIRECT(htonl(nm_utils_ip4_address_clear_host_address(addr_a, plen)),
|
||||
htonl(nm_utils_ip4_address_clear_host_address(addr_b, plen)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int nm_utils_ip6_address_same_prefix_cmp(const struct in6_addr *addr_a,
|
||||
const struct in6_addr *addr_b,
|
||||
guint8 plen);
|
||||
|
||||
static inline int
|
||||
nm_utils_ip_address_same_prefix_cmp(int addr_family,
|
||||
gconstpointer addr_a,
|
||||
gconstpointer addr_b,
|
||||
guint8 plen)
|
||||
{
|
||||
nm_assert_addr_family(addr_family);
|
||||
|
||||
NM_CMP_SELF(addr_a, addr_b);
|
||||
|
||||
if (NM_IS_IPv4(addr_family)) {
|
||||
return nm_utils_ip4_address_same_prefix_cmp(*((const in_addr_t *) addr_a),
|
||||
*((const in_addr_t *) addr_b),
|
||||
plen);
|
||||
}
|
||||
|
||||
return nm_utils_ip6_address_same_prefix_cmp(addr_a, addr_b, plen);
|
||||
}
|
||||
|
||||
static inline gboolean
|
||||
nm_utils_ip4_address_same_prefix(in_addr_t addr_a, in_addr_t addr_b, guint8 plen)
|
||||
{
|
||||
return nm_utils_ip4_address_same_prefix_cmp(addr_a, addr_b, plen) == 0;
|
||||
}
|
||||
|
||||
static inline gboolean
|
||||
nm_utils_ip6_address_same_prefix(const struct in6_addr *addr_a,
|
||||
const struct in6_addr *addr_b,
|
||||
guint8 plen)
|
||||
{
|
||||
return nm_utils_ip6_address_same_prefix_cmp(addr_a, addr_b, plen) == 0;
|
||||
}
|
||||
|
||||
static inline gboolean
|
||||
nm_utils_ip_address_same_prefix(int addr_family,
|
||||
gconstpointer addr_a,
|
||||
gconstpointer addr_b,
|
||||
guint8 plen)
|
||||
{
|
||||
return nm_utils_ip_address_same_prefix_cmp(addr_family, addr_a, addr_b, plen) == 0;
|
||||
}
|
||||
|
||||
#define NM_CMP_DIRECT_IN4ADDR_SAME_PREFIX(a, b, plen) \
|
||||
NM_CMP_RETURN(nm_utils_ip4_address_same_prefix_cmp((a), (b), (plen)))
|
||||
|
||||
#define NM_CMP_DIRECT_IN6ADDR_SAME_PREFIX(a, b, plen) \
|
||||
NM_CMP_RETURN(nm_utils_ip6_address_same_prefix_cmp((a), (b), (plen)))
|
||||
|
||||
static inline void
|
||||
nm_hash_update_in6addr(NMHashState *h, const struct in6_addr *addr)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -792,106 +792,6 @@ _nm_utils_ip4_prefix_to_netmask(guint32 prefix)
|
|||
return prefix < 32 ? ~htonl(0xFFFFFFFFu >> prefix) : 0xFFFFFFFFu;
|
||||
}
|
||||
|
||||
gconstpointer
|
||||
nm_utils_ipx_address_clear_host_address(int family, gpointer dst, gconstpointer src, guint8 plen)
|
||||
{
|
||||
g_return_val_if_fail(dst, NULL);
|
||||
|
||||
switch (family) {
|
||||
case AF_INET:
|
||||
g_return_val_if_fail(plen <= 32, NULL);
|
||||
|
||||
if (!src) {
|
||||
/* allow "self-assignment", by specifying %NULL as source. */
|
||||
src = dst;
|
||||
}
|
||||
|
||||
*((guint32 *) dst) = nm_utils_ip4_address_clear_host_address(*((guint32 *) src), plen);
|
||||
break;
|
||||
case AF_INET6:
|
||||
nm_utils_ip6_address_clear_host_address(dst, src, plen);
|
||||
break;
|
||||
default:
|
||||
g_return_val_if_reached(NULL);
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
/* nm_utils_ip4_address_clear_host_address:
|
||||
* @addr: source ip6 address
|
||||
* @plen: prefix length of network
|
||||
*
|
||||
* returns: the input address, with the host address set to 0.
|
||||
*/
|
||||
in_addr_t
|
||||
nm_utils_ip4_address_clear_host_address(in_addr_t addr, guint8 plen)
|
||||
{
|
||||
return addr & _nm_utils_ip4_prefix_to_netmask(plen);
|
||||
}
|
||||
|
||||
/* nm_utils_ip6_address_clear_host_address:
|
||||
* @dst: destination output buffer, will contain the network part of the @src address
|
||||
* @src: source ip6 address
|
||||
* @plen: prefix length of network
|
||||
*
|
||||
* Note: this function is self assignment safe, to update @src inplace, set both
|
||||
* @dst and @src to the same destination or set @src NULL.
|
||||
*/
|
||||
const struct in6_addr *
|
||||
nm_utils_ip6_address_clear_host_address(struct in6_addr * dst,
|
||||
const struct in6_addr *src,
|
||||
guint8 plen)
|
||||
{
|
||||
g_return_val_if_fail(plen <= 128, NULL);
|
||||
g_return_val_if_fail(dst, NULL);
|
||||
|
||||
if (!src)
|
||||
src = dst;
|
||||
|
||||
if (plen < 128) {
|
||||
guint nbytes = plen / 8;
|
||||
guint nbits = plen % 8;
|
||||
|
||||
if (nbytes && dst != src)
|
||||
memcpy(dst, src, nbytes);
|
||||
if (nbits) {
|
||||
dst->s6_addr[nbytes] = (src->s6_addr[nbytes] & (0xFF << (8 - nbits)));
|
||||
nbytes++;
|
||||
}
|
||||
if (nbytes <= 15)
|
||||
memset(&dst->s6_addr[nbytes], 0, 16 - nbytes);
|
||||
} else if (src != dst)
|
||||
*dst = *src;
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
int
|
||||
nm_utils_ip6_address_same_prefix_cmp(const struct in6_addr *addr_a,
|
||||
const struct in6_addr *addr_b,
|
||||
guint8 plen)
|
||||
{
|
||||
int nbytes;
|
||||
guint8 va, vb, m;
|
||||
|
||||
if (plen >= 128)
|
||||
NM_CMP_DIRECT_MEMCMP(addr_a, addr_b, sizeof(struct in6_addr));
|
||||
else {
|
||||
nbytes = plen / 8;
|
||||
if (nbytes)
|
||||
NM_CMP_DIRECT_MEMCMP(addr_a, addr_b, nbytes);
|
||||
|
||||
plen = plen % 8;
|
||||
if (plen != 0) {
|
||||
m = ~((1 << (8 - plen)) - 1);
|
||||
va = ((((const guint8 *) addr_a))[nbytes]) & m;
|
||||
vb = ((((const guint8 *) addr_b))[nbytes]) & m;
|
||||
NM_CMP_DIRECT(va, vb);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
guint32
|
||||
|
|
@ -5906,3 +5806,105 @@ _nm_utils_ssid_to_string_gbytes(GBytes *ssid)
|
|||
p = g_bytes_get_data(ssid, &l);
|
||||
return _nm_utils_ssid_to_string_arr(p, l);
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
gconstpointer
|
||||
nm_utils_ipx_address_clear_host_address(int family, gpointer dst, gconstpointer src, guint8 plen)
|
||||
{
|
||||
g_return_val_if_fail(dst, NULL);
|
||||
|
||||
switch (family) {
|
||||
case AF_INET:
|
||||
g_return_val_if_fail(plen <= 32, NULL);
|
||||
|
||||
if (!src) {
|
||||
/* allow "self-assignment", by specifying %NULL as source. */
|
||||
src = dst;
|
||||
}
|
||||
|
||||
*((guint32 *) dst) = nm_utils_ip4_address_clear_host_address(*((guint32 *) src), plen);
|
||||
break;
|
||||
case AF_INET6:
|
||||
nm_utils_ip6_address_clear_host_address(dst, src, plen);
|
||||
break;
|
||||
default:
|
||||
g_return_val_if_reached(NULL);
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
/* nm_utils_ip4_address_clear_host_address:
|
||||
* @addr: source ip6 address
|
||||
* @plen: prefix length of network
|
||||
*
|
||||
* returns: the input address, with the host address set to 0.
|
||||
*/
|
||||
in_addr_t
|
||||
nm_utils_ip4_address_clear_host_address(in_addr_t addr, guint8 plen)
|
||||
{
|
||||
return addr & _nm_utils_ip4_prefix_to_netmask(plen);
|
||||
}
|
||||
|
||||
/* nm_utils_ip6_address_clear_host_address:
|
||||
* @dst: destination output buffer, will contain the network part of the @src address
|
||||
* @src: source ip6 address
|
||||
* @plen: prefix length of network
|
||||
*
|
||||
* Note: this function is self assignment safe, to update @src inplace, set both
|
||||
* @dst and @src to the same destination or set @src NULL.
|
||||
*/
|
||||
const struct in6_addr *
|
||||
nm_utils_ip6_address_clear_host_address(struct in6_addr * dst,
|
||||
const struct in6_addr *src,
|
||||
guint8 plen)
|
||||
{
|
||||
g_return_val_if_fail(plen <= 128, NULL);
|
||||
g_return_val_if_fail(dst, NULL);
|
||||
|
||||
if (!src)
|
||||
src = dst;
|
||||
|
||||
if (plen < 128) {
|
||||
guint nbytes = plen / 8;
|
||||
guint nbits = plen % 8;
|
||||
|
||||
if (nbytes && dst != src)
|
||||
memcpy(dst, src, nbytes);
|
||||
if (nbits) {
|
||||
dst->s6_addr[nbytes] = (src->s6_addr[nbytes] & (0xFF << (8 - nbits)));
|
||||
nbytes++;
|
||||
}
|
||||
if (nbytes <= 15)
|
||||
memset(&dst->s6_addr[nbytes], 0, 16 - nbytes);
|
||||
} else if (src != dst)
|
||||
*dst = *src;
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
int
|
||||
nm_utils_ip6_address_same_prefix_cmp(const struct in6_addr *addr_a,
|
||||
const struct in6_addr *addr_b,
|
||||
guint8 plen)
|
||||
{
|
||||
int nbytes;
|
||||
guint8 va, vb, m;
|
||||
|
||||
if (plen >= 128)
|
||||
NM_CMP_DIRECT_MEMCMP(addr_a, addr_b, sizeof(struct in6_addr));
|
||||
else {
|
||||
nbytes = plen / 8;
|
||||
if (nbytes)
|
||||
NM_CMP_DIRECT_MEMCMP(addr_a, addr_b, nbytes);
|
||||
|
||||
plen = plen % 8;
|
||||
if (plen != 0) {
|
||||
m = ~((1 << (8 - plen)) - 1);
|
||||
va = ((((const guint8 *) addr_a))[nbytes]) & m;
|
||||
vb = ((((const guint8 *) addr_b))[nbytes]) & m;
|
||||
NM_CMP_DIRECT(va, vb);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -353,6 +353,75 @@ gboolean nm_utils_get_ipv6_interface_identifier(NMLinkType link_type,
|
|||
|
||||
/*****************************************************************************/
|
||||
|
||||
gconstpointer
|
||||
nm_utils_ipx_address_clear_host_address(int family, gpointer dst, gconstpointer src, guint8 plen);
|
||||
in_addr_t nm_utils_ip4_address_clear_host_address(in_addr_t addr, guint8 plen);
|
||||
const struct in6_addr *nm_utils_ip6_address_clear_host_address(struct in6_addr * dst,
|
||||
const struct in6_addr *src,
|
||||
guint8 plen);
|
||||
|
||||
static inline int
|
||||
nm_utils_ip4_address_same_prefix_cmp(in_addr_t addr_a, in_addr_t addr_b, guint8 plen)
|
||||
{
|
||||
NM_CMP_DIRECT(htonl(nm_utils_ip4_address_clear_host_address(addr_a, plen)),
|
||||
htonl(nm_utils_ip4_address_clear_host_address(addr_b, plen)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int nm_utils_ip6_address_same_prefix_cmp(const struct in6_addr *addr_a,
|
||||
const struct in6_addr *addr_b,
|
||||
guint8 plen);
|
||||
|
||||
static inline int
|
||||
nm_utils_ip_address_same_prefix_cmp(int addr_family,
|
||||
gconstpointer addr_a,
|
||||
gconstpointer addr_b,
|
||||
guint8 plen)
|
||||
{
|
||||
nm_assert_addr_family(addr_family);
|
||||
|
||||
NM_CMP_SELF(addr_a, addr_b);
|
||||
|
||||
if (NM_IS_IPv4(addr_family)) {
|
||||
return nm_utils_ip4_address_same_prefix_cmp(*((const in_addr_t *) addr_a),
|
||||
*((const in_addr_t *) addr_b),
|
||||
plen);
|
||||
}
|
||||
|
||||
return nm_utils_ip6_address_same_prefix_cmp(addr_a, addr_b, plen);
|
||||
}
|
||||
|
||||
static inline gboolean
|
||||
nm_utils_ip4_address_same_prefix(in_addr_t addr_a, in_addr_t addr_b, guint8 plen)
|
||||
{
|
||||
return nm_utils_ip4_address_same_prefix_cmp(addr_a, addr_b, plen) == 0;
|
||||
}
|
||||
|
||||
static inline gboolean
|
||||
nm_utils_ip6_address_same_prefix(const struct in6_addr *addr_a,
|
||||
const struct in6_addr *addr_b,
|
||||
guint8 plen)
|
||||
{
|
||||
return nm_utils_ip6_address_same_prefix_cmp(addr_a, addr_b, plen) == 0;
|
||||
}
|
||||
|
||||
static inline gboolean
|
||||
nm_utils_ip_address_same_prefix(int addr_family,
|
||||
gconstpointer addr_a,
|
||||
gconstpointer addr_b,
|
||||
guint8 plen)
|
||||
{
|
||||
return nm_utils_ip_address_same_prefix_cmp(addr_family, addr_a, addr_b, plen) == 0;
|
||||
}
|
||||
|
||||
#define NM_CMP_DIRECT_IN4ADDR_SAME_PREFIX(a, b, plen) \
|
||||
NM_CMP_RETURN(nm_utils_ip4_address_same_prefix_cmp((a), (b), (plen)))
|
||||
|
||||
#define NM_CMP_DIRECT_IN6ADDR_SAME_PREFIX(a, b, plen) \
|
||||
NM_CMP_RETURN(nm_utils_ip6_address_same_prefix_cmp((a), (b), (plen)))
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
#define NM_UTILS_INET_ADDRSTRLEN INET6_ADDRSTRLEN
|
||||
|
||||
static inline const char *
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue