Skip to content

Commit

Permalink
interfaces-plugin: remove duplicate netmask to prefix length function
Browse files Browse the repository at this point in the history
  • Loading branch information
andrej committed Oct 17, 2022
1 parent 62f2ad2 commit 6b1a067
Showing 1 changed file with 1 addition and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,34 +81,6 @@ int interfaces_interface_ipv4_address_element_set_ip(interfaces_interface_ipv4_a
return 0;
}

int interfaces_interface_netmask_to_prefix_length(const char *netmask)
{
int error = 0;

struct sockaddr_in sa = { 0 };
struct sockaddr_in6 sa6 = { 0 };

// IPv6 if a ':' is found
if (strchr(netmask, ':')) {
SRPC_SAFE_CALL(inet_pton(AF_INET6, netmask, &(sa6.sin6_addr)), error_out);

// s6_addr is a uint8_t array of length 16, all the byte popcounts need to be summarized
// avoid branching, use popcountll's 64 bits minimum
uint64_t *s6_addr64 = (uint64_t *) sa6.sin6_addr.s6_addr;

return __builtin_popcountll(s6_addr64[0]) + __builtin_popcountll(s6_addr64[1]);

}

// IPv4 otherwise
SRPC_SAFE_CALL(inet_pton(AF_INET, netmask, &(sa.sin_addr)), error_out);

return __builtin_popcountl(sa.sin_addr.s_addr);

error_out:
return -1;
}

int interfaces_interface_ipv4_address_element_set_subnet(interfaces_interface_ipv4_address_element_t** el, char *netmask, enum interfaces_interface_ipv4_address_subnet subtype)
{
int error = 0;
Expand Down Expand Up @@ -191,4 +163,4 @@ int interfaces_interface_ipv4_address_netmask2prefix(const char* netmask, uint8_

out:
return error;
}
}

0 comments on commit 6b1a067

Please sign in to comment.