}
// Check if the given IP address is inside the network
- r = loc_network_match_address(*network, address);
- if (r) {
+ if (!loc_network_match_address(*network, address)) {
DEBUG(db->ctx, "Searched address is not part of the network\n");
loc_network_unref(*network);
LOC_EXPORT int loc_network_match_address(struct loc_network* network, const struct in6_addr* address) {
// Address must be larger than the start address
if (in6_addr_cmp(&network->first_address, address) > 0)
- return 1;
+ return 0;
// Address must be smaller than the last address
if (in6_addr_cmp(&network->last_address, address) < 0)
- return 1;
+ return 0;
// The address is inside this network
- return 0;
+ return 1;
}
LOC_EXPORT const char* loc_network_get_country_code(struct loc_network* network) {
}
LOC_EXPORT int loc_network_overlaps(struct loc_network* self, struct loc_network* other) {
- if (loc_network_match_address(self, &other->first_address) == 0)
+ // Either of the start addresses must be in the other subnet
+ if (loc_network_match_address(self, &other->first_address))
return 1;
- if (loc_network_match_address(self, &other->last_address) == 0)
+ if (loc_network_match_address(other, &self->first_address))
return 1;
- if (loc_network_match_address(other, &self->first_address) == 0)
+ // Or either of the end addresses is in the other subnet
+ if (loc_network_match_address(self, &other->last_address))
return 1;
- if (loc_network_match_address(other, &self->last_address) == 0)
+ if (loc_network_match_address(other, &self->last_address))
return 1;
return 0;
GNU General Public License for more details.
*/
+#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <stddef.h>
}
#endif
+ struct in6_addr address;
+ err = inet_pton(AF_INET6, "2001:db8::1", &address);
+ if (err != 1) {
+ fprintf(stderr, "Could not parse IP address\n");
+ exit(EXIT_FAILURE);
+ }
+
// Create a network
struct loc_network* network1;
err = loc_network_new_from_string(ctx, &network1, "2001:db8::1/32");
exit(EXIT_FAILURE);
}
+ err = loc_network_match_address(network1, &address);
+ if (!err) {
+ fprintf(stderr, "Network1 does not match address\n");
+ exit(EXIT_FAILURE);
+ }
+
struct loc_network* network2;
err = loc_network_new_from_string(ctx, &network2, "2001:db8:ffff::/48");
if (err) {