Merge pull request #107 from BarbarossaTM/bugfix/seperate-netmask

Always convert netmasks to CIDR when configuring interfaces
This commit is contained in:
Ariadne Conill 2020-10-13 18:41:51 -06:00 committed by GitHub
commit 24bfcc1737
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 62 additions and 52 deletions

View file

@ -92,31 +92,6 @@ print_interface_dot(struct lif_dict *collection, struct lif_interface *iface, st
}
}
static inline size_t
count_set_bits(const char *netmask)
{
/* netmask set to CIDR length */
if (strchr(netmask, '.') == NULL)
return strtol(netmask, NULL, 10);
size_t r = 0;
struct in_addr in;
if (inet_pton(AF_INET, netmask, &in) == 0)
return r;
/* take the IP, put it in host endian order, and
* flip it so that all the set bits are set to the right.
* then we can simply count down from 32 and right-shift
* until the bit field is all zero.
*/
unsigned int bits = htonl(in.s_addr);
for (bits = ~bits, r = 32; bits; bits >>= 1, r--)
;
return r;
}
void
print_interface_property(struct lif_interface *iface, const char *property)
{
@ -132,26 +107,10 @@ print_interface_property(struct lif_interface *iface, const char *property)
if (printing_address)
{
struct lif_address *addr = entry->data;
size_t orig_netmask = addr->netmask;
if (!addr->netmask)
{
/* if fallback netmask is not set, default to 255.255.255.0 */
addr->netmask = 24;
struct lif_dict_entry *entry = lif_dict_find(&iface->vars, "netmask");
if (entry != NULL)
addr->netmask = count_set_bits(entry->data);
}
char addr_buf[512];
if (!lif_address_unparse(addr, addr_buf, sizeof addr_buf, true))
if (!lif_address_format_cidr(iface, entry, addr_buf, sizeof(addr_buf)))
continue;
addr->netmask = orig_netmask;
printf("%s\n", addr_buf);
}
else

View file

@ -86,7 +86,7 @@ lif_dict_add_once(struct lif_dict *dict, const char *key, void *data,
}
struct lif_dict_entry *
lif_dict_find(struct lif_dict *dict, const char *key)
lif_dict_find(const struct lif_dict *dict, const char *key)
{
struct lif_node *iter;
@ -102,7 +102,7 @@ lif_dict_find(struct lif_dict *dict, const char *key)
}
struct lif_list *
lif_dict_find_all(struct lif_dict *dict, const char *key)
lif_dict_find_all(const struct lif_dict *dict, const char *key)
{
struct lif_list *entries = calloc(1, sizeof *entries);
struct lif_node *iter;

View file

@ -44,8 +44,8 @@ extern void lif_dict_init(struct lif_dict *dict);
extern void lif_dict_fini(struct lif_dict *dict);
extern struct lif_dict_entry *lif_dict_add(struct lif_dict *dict, const char *key, void *data);
extern struct lif_dict_entry *lif_dict_add_once(struct lif_dict *dict, const char *key, void *data, lif_dict_cmp_t compar);
extern struct lif_dict_entry *lif_dict_find(struct lif_dict *dict, const char *key);
extern struct lif_list *lif_dict_find_all(struct lif_dict *dict, const char *key);
extern struct lif_dict_entry *lif_dict_find(const struct lif_dict *dict, const char *key);
extern struct lif_list *lif_dict_find_all(const struct lif_dict *dict, const char *key);
extern void lif_dict_delete(struct lif_dict *dict, const char *key);
extern void lif_dict_delete_entry(struct lif_dict *dict, struct lif_dict_entry *entry);

View file

@ -58,6 +58,57 @@ lif_address_unparse(const struct lif_address *address, char *buf, size_t buflen,
return true;
}
static inline size_t
count_set_bits(const char *netmask)
{
/* netmask set to CIDR length */
if (strchr(netmask, '.') == NULL)
return strtol(netmask, NULL, 10);
size_t r = 0;
struct in_addr in;
if (inet_pton(AF_INET, netmask, &in) == 0)
return r;
/* take the IP, put it in host endian order, and
* flip it so that all the set bits are set to the right.
* then we can simply count down from 32 and right-shift
* until the bit field is all zero.
*/
unsigned int bits = htonl(in.s_addr);
for (bits = ~bits, r = 32; bits; bits >>= 1, r--)
;
return r;
}
bool
lif_address_format_cidr(const struct lif_interface *iface, struct lif_dict_entry *entry, char *buf, size_t buflen)
{
struct lif_address *addr = entry->data;
size_t orig_netmask = addr->netmask;
if (!addr->netmask)
{
/* if netmask is not set, default to 255.255.255.0, ifupdown does so too */
addr->netmask = 24;
struct lif_dict_entry *entry = lif_dict_find(&iface->vars, "netmask");
if (entry != NULL)
addr->netmask = count_set_bits(entry->data);
}
if (!lif_address_unparse(addr, buf, buflen, true))
{
addr->netmask = orig_netmask;
return false;
}
addr->netmask = orig_netmask;
return true;
}
void
lif_interface_init(struct lif_interface *interface, const char *ifname)
{

View file

@ -32,9 +32,6 @@ struct lif_address {
int domain;
};
extern bool lif_address_parse(struct lif_address *address, const char *presentation);
extern bool lif_address_unparse(const struct lif_address *address, char *buf, size_t buflen, bool with_netmask);
/*
* Interfaces are contained in a dictionary, with the interfaces mapped by
* interface name to their `struct lif_interface`.
@ -68,6 +65,10 @@ struct lif_interface {
#define LIF_INTERFACE_COLLECTION_FOREACH_SAFE(iter, iter_next, collection) \
LIF_DICT_FOREACH_SAFE((iter), (iter_next), (collection))
extern bool lif_address_parse(struct lif_address *address, const char *presentation);
extern bool lif_address_unparse(const struct lif_address *address, char *buf, size_t buflen, bool with_netmask);
extern bool lif_address_format_cidr(const struct lif_interface *iface, struct lif_dict_entry *entry, char *buf, size_t buflen);
extern void lif_interface_init(struct lif_interface *interface, const char *ifname);
extern bool lif_interface_address_add(struct lif_interface *interface, const char *address);
extern void lif_interface_address_delete(struct lif_interface *interface, const char *address);

View file

@ -174,14 +174,13 @@ build_environment(char **envp[], const struct lif_execute_opts *opts, const stru
LIF_DICT_FOREACH(iter, &iface->vars)
{
const struct lif_dict_entry *entry = iter->data;
struct lif_dict_entry *entry = iter->data;
if (!strcmp(entry->key, "address"))
{
struct lif_address *addr = entry->data;
char addrbuf[4096];
if (!lif_address_unparse(addr, addrbuf, sizeof addrbuf, true))
if (!lif_address_format_cidr(iface, entry, addrbuf, sizeof(addrbuf)))
continue;
/* Append address to buffer */