2018-09-20 01:49:47 +02:00
|
|
|
// SPDX-License-Identifier: LGPL-2.1+
|
|
|
|
/*
|
2020-01-02 19:52:25 +01:00
|
|
|
* Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2018-02-15 13:50:12 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "wireguard.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2018-02-21 02:53:06 +01:00
|
|
|
void list_devices(void)
|
2018-02-15 13:50:12 +01:00
|
|
|
{
|
|
|
|
char *device_names, *device_name;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
device_names = wg_list_device_names();
|
|
|
|
if (!device_names) {
|
|
|
|
perror("Unable to get device names");
|
2018-02-21 02:53:06 +01:00
|
|
|
exit(1);
|
2018-02-15 13:50:12 +01:00
|
|
|
}
|
|
|
|
wg_for_each_device_name(device_names, device_name, len) {
|
|
|
|
wg_device *device;
|
|
|
|
wg_peer *peer;
|
|
|
|
wg_key_b64_string key;
|
|
|
|
|
|
|
|
if (wg_get_device(&device, device_name) < 0) {
|
|
|
|
perror("Unable to get device");
|
|
|
|
continue;
|
|
|
|
}
|
2020-02-03 16:35:01 +01:00
|
|
|
if (device->flags & WGDEVICE_HAS_PUBLIC_KEY) {
|
|
|
|
wg_key_to_base64(key, device->public_key);
|
|
|
|
printf("%s has public key %s\n", device_name, key);
|
|
|
|
} else
|
|
|
|
printf("%s has no public key\n", device_name);
|
2018-02-15 13:50:12 +01:00
|
|
|
wg_for_each_peer(device, peer) {
|
|
|
|
wg_key_to_base64(key, peer->public_key);
|
|
|
|
printf(" - peer %s\n", key);
|
|
|
|
}
|
|
|
|
wg_free_device(device);
|
|
|
|
}
|
|
|
|
free(device_names);
|
2018-02-21 02:53:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
wg_peer new_peer = {
|
|
|
|
.flags = WGPEER_HAS_PUBLIC_KEY | WGPEER_REPLACE_ALLOWEDIPS
|
|
|
|
};
|
|
|
|
wg_device new_device = {
|
|
|
|
.name = "wgtest0",
|
|
|
|
.listen_port = 1234,
|
|
|
|
.flags = WGDEVICE_HAS_PRIVATE_KEY | WGDEVICE_HAS_LISTEN_PORT,
|
|
|
|
.first_peer = &new_peer,
|
|
|
|
.last_peer = &new_peer
|
|
|
|
};
|
2018-02-21 19:19:50 +01:00
|
|
|
wg_key temp_private_key;
|
2018-02-21 02:53:06 +01:00
|
|
|
|
2018-02-21 19:19:50 +01:00
|
|
|
wg_generate_private_key(temp_private_key);
|
|
|
|
wg_generate_public_key(new_peer.public_key, temp_private_key);
|
|
|
|
wg_generate_private_key(new_device.private_key);
|
2018-02-21 02:53:06 +01:00
|
|
|
|
|
|
|
if (wg_add_device(new_device.name) < 0) {
|
|
|
|
perror("Unable to add device");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wg_set_device(&new_device) < 0) {
|
|
|
|
perror("Unable to set device");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
list_devices();
|
|
|
|
|
|
|
|
if (wg_del_device(new_device.name) < 0) {
|
|
|
|
perror("Unable to delete device");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2018-02-15 13:50:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|