X-Git-Url: http://git.ipfire.org/?a=blobdiff_plain;f=src%2Fnetwork.c;h=0c2b260f31487989180e19fe52d4128c2494ec5f;hb=04cdff09f64c940fe3ca057d25872458f2a25852;hp=592ec733ff47890d2f1d6fc45d0d398150f201c8;hpb=8fb874e928582d6bf4a3743684a1160dd2b99b39;p=people%2Fms%2Flibloc.git diff --git a/src/network.c b/src/network.c index 592ec73..0c2b260 100644 --- a/src/network.c +++ b/src/network.c @@ -59,7 +59,7 @@ static struct in6_addr prefix_to_bitmask(unsigned int prefix) { for (unsigned int i = 0; i < 16; i++) bitmask.s6_addr[i] = 0; - for (unsigned int i = prefix, j = 0; i > 0; i -= 8, j++) { + for (int i = prefix, j = 0; i > 0; i -= 8, j++) { if (i >= 8) bitmask.s6_addr[j] = 0xff; else @@ -276,7 +276,7 @@ LOC_EXPORT int loc_network_match_address(struct loc_network* network, const stru struct in6_addr last_address = make_last_address(&network->start_address, network->prefix); // Address must be smaller than the last address - if (in6_addr_cmp(&last_address, address) > 0) + if (in6_addr_cmp(address, &last_address) > 0) return 1; // The address is inside this network @@ -288,6 +288,12 @@ LOC_EXPORT const char* loc_network_get_country_code(struct loc_network* network) } LOC_EXPORT int loc_network_set_country_code(struct loc_network* network, const char* country_code) { + // Set empty country code + if (!country_code || !*country_code) { + *network->country_code = '\0'; + return 0; + } + // Country codes must be two characters if (strlen(country_code) != 2) return -EINVAL; @@ -310,11 +316,9 @@ LOC_EXPORT int loc_network_set_asn(struct loc_network* network, uint32_t asn) { } LOC_EXPORT int loc_network_to_database_v0(struct loc_network* network, struct loc_database_network_v0* dbobj) { - dbobj->prefix = network->prefix; - // Add country code for (unsigned int i = 0; i < 2; i++) { - dbobj->country_code[i] = network->country_code ? network->country_code[i] : '\0'; + dbobj->country_code[i] = network->country_code[i] ? network->country_code[i] : '\0'; } // Add ASN @@ -324,8 +328,8 @@ LOC_EXPORT int loc_network_to_database_v0(struct loc_network* network, struct lo } LOC_EXPORT int loc_network_new_from_database_v0(struct loc_ctx* ctx, struct loc_network** network, - struct in6_addr* address, const struct loc_database_network_v0* dbobj) { - int r = loc_network_new(ctx, network, address, dbobj->prefix); + struct in6_addr* address, unsigned int prefix, const struct loc_database_network_v0* dbobj) { + int r = loc_network_new(ctx, network, address, prefix); if (r) return r; @@ -407,10 +411,10 @@ static struct loc_network_tree_node* loc_network_tree_get_node(struct loc_networ return *n; } -static struct loc_network_tree_node* loc_network_tree_get_path(struct loc_network_tree* tree, const struct in6_addr* address) { +static struct loc_network_tree_node* loc_network_tree_get_path(struct loc_network_tree* tree, const struct in6_addr* address, unsigned int prefix) { struct loc_network_tree_node* node = tree->root; - for (unsigned int i = 0; i < 128; i++) { + for (unsigned int i = 0; i < prefix; i++) { // Check if the ith bit is one or zero node = loc_network_tree_get_node(node, in6_addr_get_bit(address, i)); } @@ -502,7 +506,8 @@ LOC_EXPORT int loc_network_tree_dump(struct loc_network_tree* tree) { LOC_EXPORT int loc_network_tree_add_network(struct loc_network_tree* tree, struct loc_network* network) { DEBUG(tree->ctx, "Adding network %p to tree %p\n", network, tree); - struct loc_network_tree_node* node = loc_network_tree_get_path(tree, &network->start_address); + struct loc_network_tree_node* node = loc_network_tree_get_path(tree, + &network->start_address, network->prefix); if (!node) { ERROR(tree->ctx, "Could not find a node\n"); return -ENOMEM; @@ -511,7 +516,7 @@ LOC_EXPORT int loc_network_tree_add_network(struct loc_network_tree* tree, struc // Check if node has not been set before if (node->network) { DEBUG(tree->ctx, "There is already a network at this path\n"); - return 1; + return -EBUSY; } // Point node to the network