1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
/*
* This file is part of RTRlib.
*
* This file is subject to the terms and conditions of the MIT license.
* See the file LICENSE in the top level directory for more details.
*
* Website: http://rtrlib.realmv6.org/
*/
#include "ip_private.h"
#include "rtrlib/rtrlib_export_private.h"
#include <stdbool.h>
#include <string.h>
bool lrtr_ip_addr_is_zero(const struct lrtr_ip_addr prefix)
{
if (prefix.ver == LRTR_IPV6) {
if (prefix.u.addr6.addr[0] == 0 && prefix.u.addr6.addr[1] == 0 && prefix.u.addr6.addr[2] == 0 &&
prefix.u.addr6.addr[3] == 0) {
return true;
}
} else if (prefix.u.addr4.addr == 0) {
return true;
}
return false;
}
struct lrtr_ip_addr lrtr_ip_addr_get_bits(const struct lrtr_ip_addr *val, const uint8_t from, const uint8_t number)
{
struct lrtr_ip_addr result;
if (val->ver == LRTR_IPV6) {
result.ver = LRTR_IPV6;
result.u.addr6 = lrtr_ipv6_get_bits(&(val->u.addr6), from, number);
} else {
result.ver = LRTR_IPV4;
result.u.addr4 = lrtr_ipv4_get_bits(&(val->u.addr4), from, number);
}
return result;
}
RTRLIB_EXPORT bool lrtr_ip_addr_equal(const struct lrtr_ip_addr a, const struct lrtr_ip_addr b)
{
if (a.ver != b.ver)
return false;
if (a.ver == LRTR_IPV6)
return lrtr_ipv6_addr_equal(&(a.u.addr6), &(b.u.addr6));
return lrtr_ipv4_addr_equal(&(a.u.addr4), &(b.u.addr4));
}
RTRLIB_EXPORT int lrtr_ip_addr_to_str(const struct lrtr_ip_addr *ip, char *str, const unsigned int len)
{
if (ip->ver == LRTR_IPV6)
return lrtr_ipv6_addr_to_str(&(ip->u.addr6), str, len);
return lrtr_ipv4_addr_to_str(&(ip->u.addr4), str, len);
}
RTRLIB_EXPORT int lrtr_ip_str_to_addr(const char *str, struct lrtr_ip_addr *ip)
{
if (!strchr(str, ':')) {
ip->ver = LRTR_IPV4;
return lrtr_ipv4_str_to_addr(str, &(ip->u.addr4));
}
ip->ver = LRTR_IPV6;
return lrtr_ipv6_str_to_addr(str, &(ip->u.addr6));
}
// cppcheck-suppress unusedFunction
RTRLIB_EXPORT bool lrtr_ip_str_cmp(const struct lrtr_ip_addr *addr1, const char *addr2)
{
struct lrtr_ip_addr tmp;
if (lrtr_ip_str_to_addr(addr2, &tmp) == -1)
return false;
return lrtr_ip_addr_equal(*addr1, tmp);
}
|