From: Michael Tremer Date: Thu, 22 Oct 2020 12:24:34 +0000 (+0000) Subject: network: Allow adding single IP addresses and automatically add the prefix X-Git-Tag: 0.9.5~103 X-Git-Url: http://git.ipfire.org/?p=people%2Fms%2Flibloc.git;a=commitdiff_plain;h=26ab419b68d166f932db1f97c38cb9d793d04187 network: Allow adding single IP addresses and automatically add the prefix Signed-off-by: Michael Tremer --- diff --git a/src/network.c b/src/network.c index be88d75..d7b1645 100644 --- a/src/network.c +++ b/src/network.c @@ -161,6 +161,7 @@ LOC_EXPORT int loc_network_new_from_string(struct loc_ctx* ctx, struct loc_netwo const char* address_string) { struct in6_addr first_address; char* prefix_string; + unsigned int prefix = 128; int r = -EINVAL; DEBUG(ctx, "Attempting to parse network %s\n", address_string); @@ -174,21 +175,6 @@ LOC_EXPORT int loc_network_new_from_string(struct loc_ctx* ctx, struct loc_netwo DEBUG(ctx, " Split into address = %s, prefix = %s\n", address_string, prefix_string); - // We need to have a prefix - if (!prefix_string) { - DEBUG(ctx, "No prefix set\n"); - goto FAIL; - } - - // Convert prefix to integer - unsigned int prefix = strtol(prefix_string, NULL, 10); - - // End if the prefix was invalid - if (!prefix) { - DEBUG(ctx, "The prefix is zero or not a number\n"); - goto FAIL; - } - // Parse the address r = loc_parse_address(ctx, address_string, &first_address); if (r) { @@ -196,9 +182,20 @@ LOC_EXPORT int loc_network_new_from_string(struct loc_ctx* ctx, struct loc_netwo goto FAIL; } - // Map the prefix to IPv6 if needed - if (IN6_IS_ADDR_V4MAPPED(&first_address)) - prefix += 96; + // If a prefix was given, we will try to parse it + if (prefix_string) { + // Convert prefix to integer + prefix = strtol(prefix_string, NULL, 10); + + if (!prefix) { + DEBUG(ctx, "The prefix was not parsable: %s\n", prefix_string); + goto FAIL; + } + + // Map the prefix to IPv6 if needed + if (IN6_IS_ADDR_V4MAPPED(&first_address)) + prefix += 96; + } FAIL: // Free temporary buffer diff --git a/src/test-network.c b/src/test-network.c index 8c7e898..b6776b4 100644 --- a/src/test-network.c +++ b/src/test-network.c @@ -170,8 +170,8 @@ int main(int argc, char** argv) { // Try adding a single address err = loc_writer_add_network(writer, &network, "2001:db8::"); - if (err != -EINVAL) { - fprintf(stderr, "It was possible to add an invalid network (err = %d)\n", err); + if (err) { + fprintf(stderr, "It was impossible to add an single IP address (err = %d)\n", err); exit(EXIT_FAILURE); }