]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
network: Adjust return codes of loc_network_match_address and add test
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 24 Nov 2020 16:50:17 +0000 (16:50 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 24 Nov 2020 16:50:47 +0000 (16:50 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/database.c
src/libloc.sym
src/network.c
src/test-network.c

index 914ed3eb6e55abb6c19864c7881899e56e102b70..1871b740f7bbbd47d3e7b6b8a6f5e4c59c47f8ff 100644 (file)
@@ -776,8 +776,7 @@ static int __loc_database_lookup_handle_leaf(struct loc_database* db, const stru
        }
 
        // 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);
index cb5e8efb562b0340c14889bf3c0679dbda8f6c51..ee333f18f86c41cbb7b00693b6fa81c5ee9768d4 100644 (file)
@@ -117,6 +117,7 @@ global:
        loc_network_get_last_address;
        loc_network_has_flag;
        loc_network_is_subnet;
+       loc_network_match_address;
        loc_network_match_asn;
        loc_network_match_country_code;
        loc_network_match_flag;
index ac478d5de03869b6d5cae7cd76f6bbd164bd56e1..febab957862e4981d1f99a67a4cdcd49ef741574 100644 (file)
@@ -374,14 +374,14 @@ LOC_EXPORT char* loc_network_format_last_address(struct loc_network* 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) {
@@ -464,16 +464,18 @@ LOC_EXPORT int loc_network_cmp(struct loc_network* self, struct loc_network* oth
 }
 
 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;
index f4cf97b5bb6786b5e58ce48ace9fb4ccbf82f33c..339743db13321fec44d38c061b3d89709c6e2988 100644 (file)
@@ -14,6 +14,7 @@
        GNU General Public License for more details.
 */
 
+#include <arpa/inet.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stddef.h>
@@ -46,6 +47,13 @@ int main(int argc, char** argv) {
        }
 #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");
@@ -92,6 +100,12 @@ int main(int argc, char** argv) {
                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) {