2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017-2021 IPFire Development Team <info@ipfire.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
17 #ifndef LIBLOC_NETWORK_H
18 #define LIBLOC_NETWORK_H
20 #include <netinet/in.h>
22 #include <libloc/libloc.h>
23 #include <libloc/format.h>
24 #include <libloc/network-list.h>
26 enum loc_network_flags
{
27 LOC_NETWORK_FLAG_ANONYMOUS_PROXY
= (1 << 0), // A1
28 LOC_NETWORK_FLAG_SATELLITE_PROVIDER
= (1 << 1), // A2
29 LOC_NETWORK_FLAG_ANYCAST
= (1 << 2), // A3
30 LOC_NETWORK_FLAG_DROP
= (1 << 3), // XD
34 int loc_network_new(struct loc_ctx
* ctx
, struct loc_network
** network
,
35 struct in6_addr
* first_address
, unsigned int prefix
);
36 int loc_network_new_from_string(struct loc_ctx
* ctx
, struct loc_network
** network
,
37 const char* address_string
);
38 struct loc_network
* loc_network_ref(struct loc_network
* network
);
39 struct loc_network
* loc_network_unref(struct loc_network
* network
);
40 char* loc_network_str(struct loc_network
* network
);
41 int loc_network_address_family(struct loc_network
* network
);
42 unsigned int loc_network_prefix(struct loc_network
* network
);
44 const struct in6_addr
* loc_network_get_first_address(struct loc_network
* network
);
45 char* loc_network_format_first_address(struct loc_network
* network
);
46 const struct in6_addr
* loc_network_get_last_address(struct loc_network
* network
);
47 char* loc_network_format_last_address(struct loc_network
* network
);
48 int loc_network_match_address(struct loc_network
* network
, const struct in6_addr
* address
);
50 const char* loc_network_get_country_code(struct loc_network
* network
);
51 int loc_network_set_country_code(struct loc_network
* network
, const char* country_code
);
52 int loc_network_match_country_code(struct loc_network
* network
, const char* country_code
);
54 uint32_t loc_network_get_asn(struct loc_network
* network
);
55 int loc_network_set_asn(struct loc_network
* network
, uint32_t asn
);
56 int loc_network_match_asn(struct loc_network
* network
, uint32_t asn
);
58 int loc_network_has_flag(struct loc_network
* network
, uint32_t flag
);
59 int loc_network_set_flag(struct loc_network
* network
, uint32_t flag
);
60 int loc_network_match_flag(struct loc_network
* network
, uint32_t flag
);
62 int loc_network_cmp(struct loc_network
* self
, struct loc_network
* other
);
63 int loc_network_overlaps(struct loc_network
* self
, struct loc_network
* other
);
64 int loc_network_is_subnet(struct loc_network
* self
, struct loc_network
* other
);
65 int loc_network_subnets(struct loc_network
* network
, struct loc_network
** subnet1
, struct loc_network
** subnet2
);
66 struct loc_network_list
* loc_network_exclude(
67 struct loc_network
* self
, struct loc_network
* other
);
68 struct loc_network_list
* loc_network_exclude_list(
69 struct loc_network
* network
, struct loc_network_list
* list
);
74 static inline struct in6_addr
address_increment(const struct in6_addr
* address
) {
75 struct in6_addr a
= *address
;
77 for (int octet
= 15; octet
>= 0; octet
--) {
78 if (a
.s6_addr
[octet
] < 255) {
89 static inline struct in6_addr
address_decrement(const struct in6_addr
* address
) {
90 struct in6_addr a
= *address
;
92 for (int octet
= 15; octet
>= 0; octet
--) {
93 if (a
.s6_addr
[octet
] > 0) {
102 static inline int loc_address_family(const struct in6_addr
* address
) {
103 if (IN6_IS_ADDR_V4MAPPED(address
))
109 static inline int loc_address_count_trailing_zero_bits(const struct in6_addr
* address
) {
112 for (int octet
= 15; octet
>= 0; octet
--) {
113 if (address
->s6_addr
[octet
]) {
114 zeroes
+= __builtin_ctz(address
->s6_addr
[octet
]);
123 int loc_network_to_database_v1(struct loc_network
* network
, struct loc_database_network_v1
* dbobj
);
124 int loc_network_new_from_database_v1(struct loc_ctx
* ctx
, struct loc_network
** network
,
125 struct in6_addr
* address
, unsigned int prefix
, const struct loc_database_network_v1
* dbobj
);
127 struct loc_network_tree
;
128 int loc_network_tree_new(struct loc_ctx
* ctx
, struct loc_network_tree
** tree
);
129 struct loc_network_tree
* loc_network_tree_unref(struct loc_network_tree
* tree
);
130 struct loc_network_tree_node
* loc_network_tree_get_root(struct loc_network_tree
* tree
);
131 int loc_network_tree_walk(struct loc_network_tree
* tree
,
132 int(*filter_callback
)(struct loc_network
* network
, void* data
),
133 int(*callback
)(struct loc_network
* network
, void* data
), void* data
);
134 int loc_network_tree_dump(struct loc_network_tree
* tree
);
135 int loc_network_tree_add_network(struct loc_network_tree
* tree
, struct loc_network
* network
);
136 size_t loc_network_tree_count_networks(struct loc_network_tree
* tree
);
137 size_t loc_network_tree_count_nodes(struct loc_network_tree
* tree
);
139 struct loc_network_tree_node
;
140 int loc_network_tree_node_new(struct loc_ctx
* ctx
, struct loc_network_tree_node
** node
);
141 struct loc_network_tree_node
* loc_network_tree_node_ref(struct loc_network_tree_node
* node
);
142 struct loc_network_tree_node
* loc_network_tree_node_unref(struct loc_network_tree_node
* node
);
143 struct loc_network_tree_node
* loc_network_tree_node_get(struct loc_network_tree_node
* node
, unsigned int index
);
145 int loc_network_tree_node_is_leaf(struct loc_network_tree_node
* node
);
146 struct loc_network
* loc_network_tree_node_get_network(struct loc_network_tree_node
* node
);