Merge pull request #107 from BarbarossaTM/bugfix/seperate-netmask
Always convert netmasks to CIDR when configuring interfaces
This commit is contained in:
commit
24bfcc1737
6 changed files with 62 additions and 52 deletions
|
@ -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
|
void
|
||||||
print_interface_property(struct lif_interface *iface, const char *property)
|
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)
|
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];
|
char addr_buf[512];
|
||||||
|
if (!lif_address_format_cidr(iface, entry, addr_buf, sizeof(addr_buf)))
|
||||||
if (!lif_address_unparse(addr, addr_buf, sizeof addr_buf, true))
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
addr->netmask = orig_netmask;
|
|
||||||
|
|
||||||
printf("%s\n", addr_buf);
|
printf("%s\n", addr_buf);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -86,7 +86,7 @@ lif_dict_add_once(struct lif_dict *dict, const char *key, void *data,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct lif_dict_entry *
|
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;
|
struct lif_node *iter;
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ lif_dict_find(struct lif_dict *dict, const char *key)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct lif_list *
|
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_list *entries = calloc(1, sizeof *entries);
|
||||||
struct lif_node *iter;
|
struct lif_node *iter;
|
||||||
|
|
|
@ -44,8 +44,8 @@ extern void lif_dict_init(struct lif_dict *dict);
|
||||||
extern void lif_dict_fini(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(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_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_dict_entry *lif_dict_find(const 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_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(struct lif_dict *dict, const char *key);
|
||||||
extern void lif_dict_delete_entry(struct lif_dict *dict, struct lif_dict_entry *entry);
|
extern void lif_dict_delete_entry(struct lif_dict *dict, struct lif_dict_entry *entry);
|
||||||
|
|
||||||
|
|
|
@ -58,6 +58,57 @@ lif_address_unparse(const struct lif_address *address, char *buf, size_t buflen,
|
||||||
return true;
|
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
|
void
|
||||||
lif_interface_init(struct lif_interface *interface, const char *ifname)
|
lif_interface_init(struct lif_interface *interface, const char *ifname)
|
||||||
{
|
{
|
||||||
|
|
|
@ -32,9 +32,6 @@ struct lif_address {
|
||||||
int domain;
|
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
|
* Interfaces are contained in a dictionary, with the interfaces mapped by
|
||||||
* interface name to their `struct lif_interface`.
|
* interface name to their `struct lif_interface`.
|
||||||
|
@ -68,6 +65,10 @@ struct lif_interface {
|
||||||
#define LIF_INTERFACE_COLLECTION_FOREACH_SAFE(iter, iter_next, collection) \
|
#define LIF_INTERFACE_COLLECTION_FOREACH_SAFE(iter, iter_next, collection) \
|
||||||
LIF_DICT_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 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 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);
|
extern void lif_interface_address_delete(struct lif_interface *interface, const char *address);
|
||||||
|
|
|
@ -174,14 +174,13 @@ build_environment(char **envp[], const struct lif_execute_opts *opts, const stru
|
||||||
|
|
||||||
LIF_DICT_FOREACH(iter, &iface->vars)
|
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"))
|
if (!strcmp(entry->key, "address"))
|
||||||
{
|
{
|
||||||
struct lif_address *addr = entry->data;
|
|
||||||
char addrbuf[4096];
|
char addrbuf[4096];
|
||||||
|
|
||||||
if (!lif_address_unparse(addr, addrbuf, sizeof addrbuf, true))
|
if (!lif_address_format_cidr(iface, entry, addrbuf, sizeof(addrbuf)))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* Append address to buffer */
|
/* Append address to buffer */
|
||||||
|
|
Loading…
Reference in a new issue