embeddable-wg-library: use newer string_list

This ports 1d2d6200b8.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-02-03 16:35:01 +01:00
parent 1ad6b17c35
commit db5cb4f15c
2 changed files with 42 additions and 58 deletions

View file

@ -27,8 +27,11 @@ void list_devices(void)
perror("Unable to get device"); perror("Unable to get device");
continue; continue;
} }
if (device->flags & WGDEVICE_HAS_PUBLIC_KEY) {
wg_key_to_base64(key, device->public_key); wg_key_to_base64(key, device->public_key);
printf("%s has public key %s\n", device_name, key); printf("%s has public key %s\n", device_name, key);
} else
printf("%s has no public key\n", device_name);
wg_for_each_peer(device, peer) { wg_for_each_peer(device, peer) {
wg_key_to_base64(key, peer->public_key); wg_key_to_base64(key, peer->public_key);
printf(" - peer %s\n", key); printf(" - peer %s\n", key);

View file

@ -856,85 +856,73 @@ static void mnlg_socket_close(struct mnlg_socket *nlg)
/* wireguard-specific parts: */ /* wireguard-specific parts: */
struct inflatable_buffer { struct string_list {
char *buffer; char *buffer;
char *next;
bool good;
size_t len; size_t len;
size_t pos; size_t cap;
}; };
#define max(a, b) ((a) > (b) ? (a) : (b)) static int string_list_add(struct string_list *list, const char *str)
static int add_next_to_inflatable_buffer(struct inflatable_buffer *buffer)
{ {
size_t len, expand_to; size_t len = strlen(str) + 1;
if (len == 1)
return 0;
if (len >= list->cap - list->len) {
char *new_buffer; char *new_buffer;
size_t new_cap = list->cap * 2;
if (!buffer->good || !buffer->next) { if (new_cap < list->len +len + 1)
free(buffer->next); new_cap = list->len + len + 1;
buffer->good = false; new_buffer = realloc(list->buffer, new_cap);
return 0; if (!new_buffer)
}
len = strlen(buffer->next) + 1;
if (len == 1) {
free(buffer->next);
buffer->good = false;
return 0;
}
if (buffer->len - buffer->pos <= len) {
expand_to = max(buffer->len * 2, buffer->len + len + 1);
new_buffer = realloc(buffer->buffer, expand_to);
if (!new_buffer) {
free(buffer->next);
buffer->good = false;
return -errno; return -errno;
list->buffer = new_buffer;
list->cap = new_cap;
} }
memset(&new_buffer[buffer->len], 0, expand_to - buffer->len); memcpy(list->buffer + list->len, str, len);
buffer->buffer = new_buffer; list->len += len;
buffer->len = expand_to; list->buffer[list->len] = '\0';
}
memcpy(&buffer->buffer[buffer->pos], buffer->next, len);
free(buffer->next);
buffer->good = false;
buffer->pos += len;
return 0; return 0;
} }
struct interface {
const char *name;
bool is_wireguard;
};
static int parse_linkinfo(const struct nlattr *attr, void *data) static int parse_linkinfo(const struct nlattr *attr, void *data)
{ {
struct inflatable_buffer *buffer = data; struct interface *interface = data;
if (mnl_attr_get_type(attr) == IFLA_INFO_KIND && !strcmp(WG_GENL_NAME, mnl_attr_get_str(attr))) if (mnl_attr_get_type(attr) == IFLA_INFO_KIND && !strcmp(WG_GENL_NAME, mnl_attr_get_str(attr)))
buffer->good = true; interface->is_wireguard = true;
return MNL_CB_OK; return MNL_CB_OK;
} }
static int parse_infomsg(const struct nlattr *attr, void *data) static int parse_infomsg(const struct nlattr *attr, void *data)
{ {
struct inflatable_buffer *buffer = data; struct interface *interface = data;
if (mnl_attr_get_type(attr) == IFLA_LINKINFO) if (mnl_attr_get_type(attr) == IFLA_LINKINFO)
return mnl_attr_parse_nested(attr, parse_linkinfo, data); return mnl_attr_parse_nested(attr, parse_linkinfo, data);
else if (mnl_attr_get_type(attr) == IFLA_IFNAME) else if (mnl_attr_get_type(attr) == IFLA_IFNAME)
buffer->next = strdup(mnl_attr_get_str(attr)); interface->name = mnl_attr_get_str(attr);
return MNL_CB_OK; return MNL_CB_OK;
} }
static int read_devices_cb(const struct nlmsghdr *nlh, void *data) static int read_devices_cb(const struct nlmsghdr *nlh, void *data)
{ {
struct inflatable_buffer *buffer = data; struct string_list *list = data;
struct interface interface = { 0 };
int ret; int ret;
buffer->good = false; ret = mnl_attr_parse(nlh, sizeof(struct ifinfomsg), parse_infomsg, &interface);
buffer->next = NULL;
ret = mnl_attr_parse(nlh, sizeof(struct ifinfomsg), parse_infomsg, data);
if (ret != MNL_CB_OK) if (ret != MNL_CB_OK)
return ret; return ret;
ret = add_next_to_inflatable_buffer(buffer); if (interface.name && interface.is_wireguard)
ret = string_list_add(list, interface.name);
if (ret < 0) if (ret < 0)
return ret; return ret;
if (nlh->nlmsg_type != NLMSG_DONE) if (nlh->nlmsg_type != NLMSG_DONE)
@ -942,7 +930,7 @@ static int read_devices_cb(const struct nlmsghdr *nlh, void *data)
return MNL_CB_OK; return MNL_CB_OK;
} }
static int fetch_device_names(struct inflatable_buffer *buffer) static int fetch_device_names(struct string_list *list)
{ {
struct mnl_socket *nl = NULL; struct mnl_socket *nl = NULL;
char *rtnl_buffer = NULL; char *rtnl_buffer = NULL;
@ -989,7 +977,7 @@ another:
ret = -errno; ret = -errno;
goto cleanup; goto cleanup;
} }
if ((len = mnl_cb_run(rtnl_buffer, len, seq, portid, read_devices_cb, buffer)) < 0) { if ((len = mnl_cb_run(rtnl_buffer, len, seq, portid, read_devices_cb, list)) < 0) {
/* Netlink returns NLM_F_DUMP_INTR if the set of all tunnels changed /* Netlink returns NLM_F_DUMP_INTR if the set of all tunnels changed
* during the dump. That's unfortunate, but is pretty common on busy * during the dump. That's unfortunate, but is pretty common on busy
* systems that are adding and removing tunnels all the time. Rather * systems that are adding and removing tunnels all the time. Rather
@ -1463,22 +1451,15 @@ out:
/* first\0second\0third\0forth\0last\0\0 */ /* first\0second\0third\0forth\0last\0\0 */
char *wg_list_device_names(void) char *wg_list_device_names(void)
{ {
struct inflatable_buffer buffer = { .len = MNL_SOCKET_BUFFER_SIZE }; struct string_list list = { 0 };
int ret; int ret = fetch_device_names(&list);
ret = -ENOMEM;
buffer.buffer = calloc(1, buffer.len);
if (!buffer.buffer)
goto err;
ret = fetch_device_names(&buffer);
err:
errno = -ret; errno = -ret;
if (errno) { if (errno) {
free(buffer.buffer); free(list.buffer);
return NULL; return NULL;
} }
return buffer.buffer; return list.buffer ?: strdup("\0");
} }
int wg_add_device(const char *device_name) int wg_add_device(const char *device_name)