]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/network.c
importer: Drop EDROP as it has been merged into DROP
[people/ms/libloc.git] / src / network.c
index 11fe670e235b074f9a4aab7a9e16cee4687b3355..69306a91f4b18ed0be8f4280373b34bbb7496c81 100644 (file)
@@ -51,6 +51,8 @@ struct loc_network {
 
 LOC_EXPORT int loc_network_new(struct loc_ctx* ctx, struct loc_network** network,
                struct in6_addr* address, unsigned int prefix) {
+       struct loc_network* n = NULL;
+
        // Validate the prefix
        if (!loc_address_valid_prefix(address, prefix)) {
                ERROR(ctx, "Invalid prefix in %s: %u\n", loc_address_str(address), prefix);
@@ -58,7 +60,8 @@ LOC_EXPORT int loc_network_new(struct loc_ctx* ctx, struct loc_network** network
                return 1;
        }
 
-       struct loc_network* n = calloc(1, sizeof(*n));
+       // Allocate a new network
+       n = calloc(1, sizeof(*n));
        if (!n)
                return 1;
 
@@ -72,7 +75,7 @@ LOC_EXPORT int loc_network_new(struct loc_ctx* ctx, struct loc_network** network
                n->prefix = prefix;
 
        // Convert the prefix into a bitmask
-       struct in6_addr bitmask = loc_prefix_to_bitmask(n->prefix);
+       const struct in6_addr bitmask = loc_prefix_to_bitmask(n->prefix);
 
        // Store the first and last address in the network
        n->first_address = loc_address_and(address, &bitmask);
@@ -605,11 +608,15 @@ int loc_network_merge(struct loc_network** n,
        const unsigned int prefix = loc_network_prefix(n1);
 
        // How many bits do we need to represent this address?
-       const size_t bitlength = loc_address_bit_length(&n1->first_address) - 1;
+       const size_t bitlength = loc_address_bit_length(&n1->first_address);
 
        // We cannot shorten this any more
-       if (bitlength < prefix)
+       if (bitlength >= prefix) {
+               DEBUG(n1->ctx, "Cannot shorten this any further because we need at least %jd bits,"
+                       " but only have %d\n", bitlength, prefix);
+
                return 0;
+       }
 
        // Increment the last address of the first network
        address = n1->last_address;
@@ -693,3 +700,121 @@ int loc_network_new_from_database_v1(struct loc_ctx* ctx, struct loc_network** n
 
        return 0;
 }
+
+static char* loc_network_reverse_pointer6(struct loc_network* network, const char* suffix) {
+       char* buffer = NULL;
+       int r;
+
+       unsigned int prefix = loc_network_prefix(network);
+
+       // Must border on a nibble
+       if (prefix % 4) {
+               errno = ENOTSUP;
+               return NULL;
+       }
+
+       if (!suffix)
+               suffix = "ip6.arpa.";
+
+       // Initialize the buffer
+       r = asprintf(&buffer, "%s", suffix);
+       if (r < 0)
+               goto ERROR;
+
+       for (unsigned int i = 0; i < (prefix / 4); i++) {
+               r = asprintf(&buffer, "%x.%s", loc_address_get_nibble(&network->first_address, i), buffer);
+               if (r < 0)
+                       goto ERROR;
+       }
+
+       // Add the asterisk
+       if (prefix < 128) {
+               r = asprintf(&buffer, "*.%s", buffer);
+               if (r < 0)
+                       goto ERROR;
+       }
+
+       return buffer;
+
+ERROR:
+       if (buffer)
+               free(buffer);
+
+       return NULL;
+}
+
+static char* loc_network_reverse_pointer4(struct loc_network* network, const char* suffix) {
+       char* buffer = NULL;
+       int r;
+
+       unsigned int prefix = loc_network_prefix(network);
+
+       // Must border on an octet
+       if (prefix % 8) {
+               errno = ENOTSUP;
+               return NULL;
+       }
+
+       if (!suffix)
+               suffix = "in-addr.arpa.";
+
+       switch (prefix) {
+               case 32:
+                       r = asprintf(&buffer, "%d.%d.%d.%d.%s",
+                               loc_address_get_octet(&network->first_address, 3),
+                               loc_address_get_octet(&network->first_address, 2),
+                               loc_address_get_octet(&network->first_address, 1),
+                               loc_address_get_octet(&network->first_address, 0),
+                               suffix);
+                       break;
+
+               case 24:
+                       r = asprintf(&buffer, "*.%d.%d.%d.%s",
+                               loc_address_get_octet(&network->first_address, 2),
+                               loc_address_get_octet(&network->first_address, 1),
+                               loc_address_get_octet(&network->first_address, 0),
+                               suffix);
+                       break;
+
+               case 16:
+                       r = asprintf(&buffer, "*.%d.%d.%s",
+                               loc_address_get_octet(&network->first_address, 1),
+                               loc_address_get_octet(&network->first_address, 0),
+                               suffix);
+                       break;
+
+               case 8:
+                       r = asprintf(&buffer, "*.%d.%s",
+                               loc_address_get_octet(&network->first_address, 0),
+                               suffix);
+                       break;
+
+               case 0:
+                       r = asprintf(&buffer, "*.%s", suffix);
+                       break;
+
+               // To make the compiler happy
+               default:
+                       return NULL;
+       }
+
+       if (r < 0)
+               return NULL;
+
+       return buffer;
+}
+
+LOC_EXPORT char* loc_network_reverse_pointer(struct loc_network* network, const char* suffix) {
+       switch (network->family) {
+               case AF_INET6:
+                       return loc_network_reverse_pointer6(network, suffix);
+
+               case AF_INET:
+                       return loc_network_reverse_pointer4(network, suffix);
+
+               default:
+                       break;
+       }
+
+       return NULL;
+}