]> git.ipfire.org Git - location/libloc.git/commitdiff
Move all address convenience functions into their own header
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 7 Mar 2022 11:49:21 +0000 (11:49 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 7 Mar 2022 11:49:21 +0000 (11:49 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/database.c
src/libloc/address.h [new file with mode: 0644]
src/libloc/network.h
src/libloc/private.h
src/network-list.c
src/network.c
src/test-network.c

index 4da2ec58a3b0136c4ff06d0891fdf5c3bbdaf130..e5197a56cacbe8d37ded38cb597795fdf51198fd 100644 (file)
@@ -91,6 +91,7 @@ EXTRA_DIST += \
 
 pkginclude_HEADERS = \
        src/libloc/libloc.h \
+       src/libloc/address.h \
        src/libloc/as.h \
        src/libloc/as-list.h \
        src/libloc/compat.h \
index de4b56ef94c29bae32e6bf5855ce1da722a4ec30..feb0970f6eef7d9c5b38bc1a2385a79833072d2d 100644 (file)
@@ -37,6 +37,7 @@
 #include <openssl/pem.h>
 
 #include <libloc/libloc.h>
+#include <libloc/address.h>
 #include <libloc/as.h>
 #include <libloc/as-list.h>
 #include <libloc/compat.h>
diff --git a/src/libloc/address.h b/src/libloc/address.h
new file mode 100644 (file)
index 0000000..ea4a87c
--- /dev/null
@@ -0,0 +1,303 @@
+/*
+       libloc - A library to determine the location of someone on the Internet
+
+       Copyright (C) 2022 IPFire Development Team <info@ipfire.org>
+
+       This library is free software; you can redistribute it and/or
+       modify it under the terms of the GNU Lesser General Public
+       License as published by the Free Software Foundation; either
+       version 2.1 of the License, or (at your option) any later version.
+
+       This library is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+       Lesser General Public License for more details.
+*/
+
+#ifndef LIBLOC_ADDRESS_H
+#define LIBLOC_ADDRESS_H
+
+#ifdef LIBLOC_PRIVATE
+
+#include <errno.h>
+#include <netinet/in.h>
+
+/*
+       All of these functions are private and for internal use only
+*/
+
+static inline int loc_address_family(const struct in6_addr* address) {
+       if (IN6_IS_ADDR_V4MAPPED(address))
+               return AF_INET;
+       else
+               return AF_INET6;
+}
+
+static inline int in6_addr_cmp(const struct in6_addr* a1, const struct in6_addr* a2) {
+       for (unsigned int i = 0; i < 16; i++) {
+               if (a1->s6_addr[i] > a2->s6_addr[i])
+                       return 1;
+
+               else if (a1->s6_addr[i] < a2->s6_addr[i])
+                       return -1;
+       }
+
+       return 0;
+}
+
+static inline int in6_addr_get_bit(const struct in6_addr* address, unsigned int i) {
+       return ((address->s6_addr[i / 8] >> (7 - (i % 8))) & 1);
+}
+
+static inline void in6_addr_set_bit(struct in6_addr* address, unsigned int i, unsigned int val) {
+       address->s6_addr[i / 8] ^= (-val ^ address->s6_addr[i / 8]) & (1 << (7 - (i % 8)));
+}
+
+static inline struct in6_addr loc_prefix_to_bitmask(const unsigned int prefix) {
+       struct in6_addr bitmask;
+
+       for (unsigned int i = 0; i < 16; i++)
+               bitmask.s6_addr[i] = 0;
+
+       for (int i = prefix, j = 0; i > 0; i -= 8, j++) {
+               if (i >= 8)
+                       bitmask.s6_addr[j] = 0xff;
+               else
+                       bitmask.s6_addr[j] = 0xff << (8 - i);
+       }
+
+       return bitmask;
+}
+
+static inline unsigned int __loc_address6_bit_length(const struct in6_addr* address) {
+       unsigned int length = 128;
+
+       for (int octet = 0; octet <= 15; octet++) {
+               if (address->s6_addr[octet]) {
+                       length -= __builtin_clz(address->s6_addr[octet]) - 24;
+                       break;
+               } else
+                       length -= 8;
+       }
+
+       return length;
+}
+
+static inline unsigned int __loc_address4_bit_length(const struct in6_addr* address) {
+       unsigned int length = 32;
+
+       for (int octet = 12; octet <= 15; octet++) {
+               if (address->s6_addr[octet]) {
+                       length -= __builtin_clz(address->s6_addr[octet]) - 24;
+                       break;
+               } else
+                       length -= 8;
+       }
+
+       return length;
+}
+
+static inline unsigned int loc_address_bit_length(const struct in6_addr* address) {
+       if (IN6_IS_ADDR_V4MAPPED(address))
+               return __loc_address4_bit_length(address);
+       else
+               return __loc_address6_bit_length(address);
+}
+
+static inline int loc_address_reset(struct in6_addr* address, int family) {
+       switch (family) {
+               case AF_INET6:
+                       address->s6_addr32[0] = 0x0000;
+                       address->s6_addr32[1] = 0x0000;
+                       address->s6_addr32[2] = 0x0000;
+                       address->s6_addr32[3] = 0x0000;
+                       return 0;
+
+               case AF_INET:
+                       address->s6_addr32[0] = 0x0000;
+                       address->s6_addr32[1] = 0x0000;
+                       address->s6_addr32[2] = htonl(0xffff);
+                       address->s6_addr32[3] = 0x0000;
+                       return 0;
+       }
+
+       return -1;
+}
+
+static inline int loc_address_reset_last(struct in6_addr* address, int family) {
+       switch (family) {
+               case AF_INET6:
+                       address->s6_addr32[0] = 0xffff;
+                       address->s6_addr32[1] = 0xffff;
+                       address->s6_addr32[2] = 0xffff;
+                       address->s6_addr32[3] = 0xffff;
+                       return 0;
+
+               case AF_INET:
+                       address->s6_addr32[0] = 0x0000;
+                       address->s6_addr32[1] = 0x0000;
+                       address->s6_addr32[2] = htonl(0xffff);
+                       address->s6_addr32[3] = 0xffff;
+                       return 0;
+       }
+
+       return -1;
+}
+
+static inline struct in6_addr loc_address_and(
+               const struct in6_addr* address, const struct in6_addr* bitmask) {
+       struct in6_addr a;
+
+       // Perform bitwise AND
+       for (unsigned int i = 0; i < 4; i++)
+               a.s6_addr32[i] = address->s6_addr32[i] & bitmask->s6_addr32[i];
+
+       return a;
+}
+
+static inline struct in6_addr loc_address_or(
+               const struct in6_addr* address, const struct in6_addr* bitmask) {
+       struct in6_addr a;
+
+       // Perform bitwise OR
+       for (unsigned int i = 0; i < 4; i++)
+               a.s6_addr32[i] = address->s6_addr32[i] | ~bitmask->s6_addr32[i];
+
+       return a;
+}
+
+static inline int __loc_address6_sub(struct in6_addr* result,
+               const struct in6_addr* address1, const struct in6_addr* address2) {
+       int remainder = 0;
+
+       for (int octet = 15; octet >= 0; octet--) {
+               int x = address1->s6_addr[octet] - address2->s6_addr[octet] + remainder;
+
+               // Store remainder for the next iteration
+               remainder = (x >> 8);
+
+               result->s6_addr[octet] = x & 0xff;
+       }
+
+       return 0;
+}
+
+static inline int __loc_address4_sub(struct in6_addr* result,
+               const struct in6_addr* address1, const struct in6_addr* address2) {
+       int remainder = 0;
+
+       for (int octet = 15; octet >= 12; octet--) {
+               int x = address1->s6_addr[octet] - address2->s6_addr[octet] + remainder;
+
+               // Store remainder for the next iteration
+               remainder = (x >> 8);
+
+               result->s6_addr[octet] = x & 0xff;
+       }
+
+       return 0;
+}
+
+static inline int loc_address_sub(struct in6_addr* result,
+               const struct in6_addr* address1, const struct in6_addr* address2) {
+       // XXX should be using loc_address_family
+       int family1 = IN6_IS_ADDR_V4MAPPED(address1) ? AF_INET : AF_INET6;
+       int family2 = IN6_IS_ADDR_V4MAPPED(address2) ? AF_INET : AF_INET6;
+
+       // Address family must match
+       if (family1 != family2) {
+               errno = EINVAL;
+               return 1;
+       }
+
+       // Clear result
+       int r = loc_address_reset(result, family1);
+       if (r)
+               return r;
+
+       switch (family1) {
+               case AF_INET6:
+                       return __loc_address6_sub(result, address1, address2);
+
+               case AF_INET:
+                       return __loc_address4_sub(result, address1, address2);
+
+               default:
+                       errno = ENOTSUP;
+                       return 1;
+       }
+}
+
+static inline struct in6_addr address_increment(const struct in6_addr* address) {
+       struct in6_addr a = *address;
+
+       for (int octet = 15; octet >= 0; octet--) {
+               if (a.s6_addr[octet] < 255) {
+                       a.s6_addr[octet]++;
+                       break;
+               } else {
+                       a.s6_addr[octet] = 0;
+               }
+       }
+
+       return a;
+}
+
+static inline struct in6_addr address_decrement(const struct in6_addr* address) {
+       struct in6_addr a = *address;
+
+       for (int octet = 15; octet >= 0; octet--) {
+               if (a.s6_addr[octet] > 0) {
+                       a.s6_addr[octet]--;
+                       break;
+               }
+       }
+
+       return a;
+}
+
+static inline int loc_address_family_bit_length(const int family) {
+       switch (family) {
+               case AF_INET6:
+                       return 128;
+
+               case AF_INET:
+                       return 32;
+
+               default:
+                       return -1;
+       }
+}
+
+static inline int loc_address_all_zeroes(const struct in6_addr* address) {
+       struct in6_addr all_zeroes = IN6ADDR_ANY_INIT;
+
+       const int family = loc_address_family(address);
+
+       int r = loc_address_reset(&all_zeroes, family);
+       if (r)
+               return r;
+
+       if (in6_addr_cmp(address, &all_zeroes) == 0)
+               return 1;
+
+       return 0;
+}
+
+static inline int loc_address_count_trailing_zero_bits(const struct in6_addr* address) {
+       int zeroes = 0;
+
+       for (int octet = 15; octet >= 0; octet--) {
+               if (address->s6_addr[octet]) {
+                       zeroes += __builtin_ctz(address->s6_addr[octet]);
+                       break;
+               } else
+                       zeroes += 8;
+       }
+
+       return zeroes;
+}
+
+#endif /* LIBLOC_PRIVATE */
+
+#endif /* LIBLOC_ADDRESS_H */
index c937b745472c88759bb46dc445be10ec3cf4e917..8053ebe699f223b88f8fcfa01dd08cee43c34832 100644 (file)
@@ -68,83 +68,6 @@ struct loc_network_list* loc_network_exclude_list(
 
 #ifdef LIBLOC_PRIVATE
 
-static inline struct in6_addr address_increment(const struct in6_addr* address) {
-       struct in6_addr a = *address;
-
-       for (int octet = 15; octet >= 0; octet--) {
-               if (a.s6_addr[octet] < 255) {
-                       a.s6_addr[octet]++;
-                       break;
-               } else {
-                       a.s6_addr[octet] = 0;
-               }
-       }
-
-       return a;
-}
-
-static inline struct in6_addr address_decrement(const struct in6_addr* address) {
-       struct in6_addr a = *address;
-
-       for (int octet = 15; octet >= 0; octet--) {
-               if (a.s6_addr[octet] > 0) {
-                       a.s6_addr[octet]--;
-                       break;
-               }
-       }
-
-       return a;
-}
-
-static inline int loc_address_family(const struct in6_addr* address) {
-       if (IN6_IS_ADDR_V4MAPPED(address))
-               return AF_INET;
-       else
-               return AF_INET6;
-}
-
-static inline int loc_address_family_bit_length(const int family) {
-       switch (family) {
-               case AF_INET6:
-                       return 128;
-
-               case AF_INET:
-                       return 32;
-
-               default:
-                       return -1;
-       }
-}
-
-static inline int loc_address_all_zeroes(const struct in6_addr* address) {
-       struct in6_addr all_zeroes = IN6ADDR_ANY_INIT;
-
-       const int family = loc_address_family(address);
-
-       int r = loc_address_reset(&all_zeroes, family);
-       if (r)
-               return r;
-
-       if (in6_addr_cmp(address, &all_zeroes) == 0)
-               return 1;
-
-       return 0;
-}
-
-static inline int loc_address_count_trailing_zero_bits(const struct in6_addr* address) {
-       int zeroes = 0;
-
-       for (int octet = 15; octet >= 0; octet--) {
-               if (address->s6_addr[octet]) {
-                       zeroes += __builtin_ctz(address->s6_addr[octet]);
-                       break;
-               } else
-                       zeroes += 8;
-       }
-
-       return zeroes;
-}
-
 int loc_network_to_database_v1(struct loc_network* network, struct loc_database_network_v1* dbobj);
 int loc_network_new_from_database_v1(struct loc_ctx* ctx, struct loc_network** network,
                struct in6_addr* address, unsigned int prefix, const struct loc_database_network_v1* dbobj);
index 41a89299f480214057a7a670ee6dcd26517ffcf4..2ee97d1c4142ab32c26f041fa3cd75fa37e09a64 100644 (file)
@@ -19,9 +19,6 @@
 
 #ifdef LIBLOC_PRIVATE
 
-#include <errno.h>
-#include <netinet/in.h>
-#include <stdbool.h>
 #include <stdio.h>
 #include <syslog.h>
 
@@ -59,200 +56,6 @@ void loc_log(struct loc_ctx *ctx,
        int priority, const char *file, int line, const char *fn,
        const char *format, ...) __attribute__((format(printf, 6, 7)));
 
-static inline int in6_addr_cmp(const struct in6_addr* a1, const struct in6_addr* a2) {
-       for (unsigned int i = 0; i < 16; i++) {
-               if (a1->s6_addr[i] > a2->s6_addr[i])
-                       return 1;
-
-               else if (a1->s6_addr[i] < a2->s6_addr[i])
-                       return -1;
-       }
-
-       return 0;
-}
-
-static inline int in6_addr_get_bit(const struct in6_addr* address, unsigned int i) {
-       return ((address->s6_addr[i / 8] >> (7 - (i % 8))) & 1);
-}
-
-static inline void in6_addr_set_bit(struct in6_addr* address, unsigned int i, unsigned int val) {
-       address->s6_addr[i / 8] ^= (-val ^ address->s6_addr[i / 8]) & (1 << (7 - (i % 8)));
-}
-
-static inline struct in6_addr loc_prefix_to_bitmask(const unsigned int prefix) {
-       struct in6_addr bitmask;
-
-       for (unsigned int i = 0; i < 16; i++)
-               bitmask.s6_addr[i] = 0;
-
-       for (int i = prefix, j = 0; i > 0; i -= 8, j++) {
-               if (i >= 8)
-                       bitmask.s6_addr[j] = 0xff;
-               else
-                       bitmask.s6_addr[j] = 0xff << (8 - i);
-       }
-
-       return bitmask;
-}
-
-static inline unsigned int __loc_address6_bit_length(const struct in6_addr* address) {
-       unsigned int length = 128;
-
-       for (int octet = 0; octet <= 15; octet++) {
-               if (address->s6_addr[octet]) {
-                       length -= __builtin_clz(address->s6_addr[octet]) - 24;
-                       break;
-               } else
-                       length -= 8;
-       }
-
-       return length;
-}
-
-static inline unsigned int __loc_address4_bit_length(const struct in6_addr* address) {
-       unsigned int length = 32;
-
-       for (int octet = 12; octet <= 15; octet++) {
-               if (address->s6_addr[octet]) {
-                       length -= __builtin_clz(address->s6_addr[octet]) - 24;
-                       break;
-               } else
-                       length -= 8;
-       }
-
-       return length;
-}
-
-static inline unsigned int loc_address_bit_length(const struct in6_addr* address) {
-       if (IN6_IS_ADDR_V4MAPPED(address))
-               return __loc_address4_bit_length(address);
-       else
-               return __loc_address6_bit_length(address);
-}
-
-static inline int loc_address_reset(struct in6_addr* address, int family) {
-       switch (family) {
-               case AF_INET6:
-                       address->s6_addr32[0] = 0x0000;
-                       address->s6_addr32[1] = 0x0000;
-                       address->s6_addr32[2] = 0x0000;
-                       address->s6_addr32[3] = 0x0000;
-                       return 0;
-
-               case AF_INET:
-                       address->s6_addr32[0] = 0x0000;
-                       address->s6_addr32[1] = 0x0000;
-                       address->s6_addr32[2] = htonl(0xffff);
-                       address->s6_addr32[3] = 0x0000;
-                       return 0;
-       }
-
-       return -1;
-}
-
-static inline int loc_address_reset_last(struct in6_addr* address, int family) {
-       switch (family) {
-               case AF_INET6:
-                       address->s6_addr32[0] = 0xffff;
-                       address->s6_addr32[1] = 0xffff;
-                       address->s6_addr32[2] = 0xffff;
-                       address->s6_addr32[3] = 0xffff;
-                       return 0;
-
-               case AF_INET:
-                       address->s6_addr32[0] = 0x0000;
-                       address->s6_addr32[1] = 0x0000;
-                       address->s6_addr32[2] = htonl(0xffff);
-                       address->s6_addr32[3] = 0xffff;
-                       return 0;
-       }
-
-       return -1;
-}
-
-static inline struct in6_addr loc_address_and(
-               const struct in6_addr* address, const struct in6_addr* bitmask) {
-       struct in6_addr a;
-
-       // Perform bitwise AND
-       for (unsigned int i = 0; i < 4; i++)
-               a.s6_addr32[i] = address->s6_addr32[i] & bitmask->s6_addr32[i];
-
-       return a;
-}
-
-static inline struct in6_addr loc_address_or(
-               const struct in6_addr* address, const struct in6_addr* bitmask) {
-       struct in6_addr a;
-
-       // Perform bitwise OR
-       for (unsigned int i = 0; i < 4; i++)
-               a.s6_addr32[i] = address->s6_addr32[i] | ~bitmask->s6_addr32[i];
-
-       return a;
-}
-
-static inline int __loc_address6_sub(struct in6_addr* result,
-               const struct in6_addr* address1, const struct in6_addr* address2) {
-       int remainder = 0;
-
-       for (int octet = 15; octet >= 0; octet--) {
-               int x = address1->s6_addr[octet] - address2->s6_addr[octet] + remainder;
-
-               // Store remainder for the next iteration
-               remainder = (x >> 8);
-
-               result->s6_addr[octet] = x & 0xff;
-       }
-
-       return 0;
-}
-
-static inline int __loc_address4_sub(struct in6_addr* result,
-               const struct in6_addr* address1, const struct in6_addr* address2) {
-       int remainder = 0;
-
-       for (int octet = 15; octet >= 12; octet--) {
-               int x = address1->s6_addr[octet] - address2->s6_addr[octet] + remainder;
-
-               // Store remainder for the next iteration
-               remainder = (x >> 8);
-
-               result->s6_addr[octet] = x & 0xff;
-       }
-
-       return 0;
-}
-
-static inline int loc_address_sub(struct in6_addr* result,
-               const struct in6_addr* address1, const struct in6_addr* address2) {
-       // XXX should be using loc_address_family
-       int family1 = IN6_IS_ADDR_V4MAPPED(address1) ? AF_INET : AF_INET6;
-       int family2 = IN6_IS_ADDR_V4MAPPED(address2) ? AF_INET : AF_INET6;
-
-       // Address family must match
-       if (family1 != family2) {
-               errno = EINVAL;
-               return 1;
-       }
-
-       // Clear result
-       int r = loc_address_reset(result, family1);
-       if (r)
-               return r;
-
-       switch (family1) {
-               case AF_INET6:
-                       return __loc_address6_sub(result, address1, address2);
-
-               case AF_INET:
-                       return __loc_address4_sub(result, address1, address2);
-
-               default:
-                       errno = ENOTSUP;
-                       return 1;
-       }
-}
 
 static inline void hexdump(struct loc_ctx* ctx, const void* addr, size_t len) {
        char buffer_hex[16 * 3 + 6];
index 0157d44a4fdcbd231ebbc7765c800dbe8246c720..200d94be541d13d871827b474dfb6c16e2421354 100644 (file)
@@ -18,6 +18,7 @@
 #include <stdlib.h>
 #include <time.h>
 
+#include <libloc/address.h>
 #include <libloc/libloc.h>
 #include <libloc/network.h>
 #include <libloc/private.h>
index 0266ebf3edc0fd63e159b0faa9a785e87a2654f6..67d21362122de74abdaf3578f45d446c27edd2bc 100644 (file)
@@ -26,6 +26,7 @@
 #endif
 
 #include <libloc/libloc.h>
+#include <libloc/address.h>
 #include <libloc/compat.h>
 #include <libloc/country.h>
 #include <libloc/network.h>
index 7001071fbb2ade794367c5cad6c2f191c7a765ba..0d00a6e6287ff1574d97352452b094b1fa8c4c04 100644 (file)
@@ -23,6 +23,7 @@
 #include <syslog.h>
 
 #include <libloc/libloc.h>
+#include <libloc/address.h>
 #include <libloc/database.h>
 #include <libloc/network.h>
 #include <libloc/private.h>