This source file includes following definitions.
- in_addr_is_null
- in_addr_is_link_local
- in_addr_equal
- in_addr_prefix_intersect
- in_addr_prefix_next
- in_addr_to_string
- in_addr_from_string
- in_addr_from_string_auto
- in_addr_netmask_to_prefixlen
- in_addr_default_prefixlen
- in_addr_default_subnet_mask
#include <arpa/inet.h>
#include "in-addr-util.h"
int in_addr_is_null(int family, const union in_addr_union *u) {
assert(u);
if (family == AF_INET)
return u->in.s_addr == 0;
if (family == AF_INET6)
return
u->in6.s6_addr32[0] == 0 &&
u->in6.s6_addr32[1] == 0 &&
u->in6.s6_addr32[2] == 0 &&
u->in6.s6_addr32[3] == 0;
return -EAFNOSUPPORT;
}
int in_addr_is_link_local(int family, const union in_addr_union *u) {
assert(u);
if (family == AF_INET)
return (be32toh(u->in.s_addr) & 0xFFFF0000) == (169U << 24 | 254U << 16);
if (family == AF_INET6)
return IN6_IS_ADDR_LINKLOCAL(&u->in6);
return -EAFNOSUPPORT;
}
int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b) {
assert(a);
assert(b);
if (family == AF_INET)
return a->in.s_addr == b->in.s_addr;
if (family == AF_INET6)
return
a->in6.s6_addr32[0] == b->in6.s6_addr32[0] &&
a->in6.s6_addr32[1] == b->in6.s6_addr32[1] &&
a->in6.s6_addr32[2] == b->in6.s6_addr32[2] &&
a->in6.s6_addr32[3] == b->in6.s6_addr32[3];
return -EAFNOSUPPORT;
}
int in_addr_prefix_intersect(
int family,
const union in_addr_union *a,
unsigned aprefixlen,
const union in_addr_union *b,
unsigned bprefixlen) {
unsigned m;
assert(a);
assert(b);
m = MIN(aprefixlen, bprefixlen);
if (family == AF_INET) {
uint32_t x, nm;
x = be32toh(a->in.s_addr ^ b->in.s_addr);
nm = (m == 0) ? 0 : 0xFFFFFFFFUL << (32 - m);
return (x & nm) == 0;
}
if (family == AF_INET6) {
unsigned i;
if (m > 128)
m = 128;
for (i = 0; i < 16; i++) {
uint8_t x, nm;
x = a->in6.s6_addr[i] ^ b->in6.s6_addr[i];
if (m < 8)
nm = 0xFF << (8 - m);
else
nm = 0xFF;
if ((x & nm) != 0)
return 0;
if (m > 8)
m -= 8;
else
m = 0;
}
return 1;
}
return -EAFNOSUPPORT;
}
int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen) {
assert(u);
if (prefixlen <= 0)
return 0;
if (family == AF_INET) {
uint32_t c, n;
if (prefixlen > 32)
prefixlen = 32;
c = be32toh(u->in.s_addr);
n = c + (1UL << (32 - prefixlen));
if (n < c)
return 0;
n &= 0xFFFFFFFFUL << (32 - prefixlen);
u->in.s_addr = htobe32(n);
return 1;
}
if (family == AF_INET6) {
struct in6_addr add = {}, result;
uint8_t overflow = 0;
unsigned i;
if (prefixlen > 128)
prefixlen = 128;
add.s6_addr[(prefixlen-1) / 8] = 1 << (7 - (prefixlen-1) % 8);
for (i = 16; i > 0; i--) {
unsigned j = i - 1;
result.s6_addr[j] = u->in6.s6_addr[j] + add.s6_addr[j] + overflow;
overflow = (result.s6_addr[j] < u->in6.s6_addr[j]);
}
if (overflow)
return 0;
u->in6 = result;
return 1;
}
return -EAFNOSUPPORT;
}
int in_addr_to_string(int family, const union in_addr_union *u, char **ret) {
char *x;
size_t l;
assert(u);
assert(ret);
if (family == AF_INET)
l = INET_ADDRSTRLEN;
else if (family == AF_INET6)
l = INET6_ADDRSTRLEN;
else
return -EAFNOSUPPORT;
x = new(char, l);
if (!x)
return -ENOMEM;
errno = 0;
if (!inet_ntop(family, u, x, l)) {
free(x);
return errno ? -errno : -EINVAL;
}
*ret = x;
return 0;
}
int in_addr_from_string(int family, const char *s, union in_addr_union *ret) {
assert(s);
assert(ret);
if (!IN_SET(family, AF_INET, AF_INET6))
return -EAFNOSUPPORT;
errno = 0;
if (inet_pton(family, s, ret) <= 0)
return errno ? -errno : -EINVAL;
return 0;
}
int in_addr_from_string_auto(const char *s, int *family, union in_addr_union *ret) {
int r;
assert(s);
assert(family);
assert(ret);
r = in_addr_from_string(AF_INET, s, ret);
if (r >= 0) {
*family = AF_INET;
return 0;
}
r = in_addr_from_string(AF_INET6, s, ret);
if (r >= 0) {
*family = AF_INET6;
return 0;
}
return -EINVAL;
}
unsigned in_addr_netmask_to_prefixlen(const struct in_addr *addr) {
assert(addr);
return 32 - u32ctz(be32toh(addr->s_addr));
}
int in_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen) {
uint32_t address;
assert(addr);
assert(addr->s_addr != INADDR_ANY);
assert(prefixlen);
address = be32toh(addr->s_addr);
if ((address >> 31) == 0x0)
*prefixlen = 8;
else if ((address >> 30) == 0x2)
*prefixlen = 16;
else if ((address >> 29) == 0x6)
*prefixlen = 24;
else
return -ERANGE;
return 0;
}
int in_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask) {
unsigned char prefixlen;
int r;
assert(addr);
assert(mask);
r = in_addr_default_prefixlen(addr, &prefixlen);
if (r < 0)
return r;
assert(prefixlen > 0 && prefixlen < 32);
mask->s_addr = htobe32((0xffffffff << (32 - prefixlen)) & 0xffffffff);
return 0;
}