1 // SPDX-License-Identifier: LGPL-2.1+
2 /*
3 * Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4 */
5
6 #include "wireguard.h"
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10
list_devices(void)11 void list_devices(void)
12 {
13 char *device_names, *device_name;
14 size_t len;
15
16 device_names = wg_list_device_names();
17 if (!device_names) {
18 perror("Unable to get device names");
19 exit(1);
20 }
21 wg_for_each_device_name(device_names, device_name, len) {
22 wg_device *device;
23 wg_peer *peer;
24 wg_key_b64_string key;
25
26 if (wg_get_device(&device, device_name) < 0) {
27 perror("Unable to get device");
28 continue;
29 }
30 if (device->flags & WGDEVICE_HAS_PUBLIC_KEY) {
31 wg_key_to_base64(key, device->public_key);
32 printf("%s has public key %s\n", device_name, key);
33 } else
34 printf("%s has no public key\n", device_name);
35 wg_for_each_peer(device, peer) {
36 wg_key_to_base64(key, peer->public_key);
37 printf(" - peer %s\n", key);
38 }
39 wg_free_device(device);
40 }
41 free(device_names);
42 }
43
main(int argc,char * argv[])44 int main(int argc, char *argv[])
45 {
46 wg_peer new_peer = {
47 .flags = WGPEER_HAS_PUBLIC_KEY | WGPEER_REPLACE_ALLOWEDIPS
48 };
49 wg_device new_device = {
50 .name = "wgtest0",
51 .listen_port = 1234,
52 .flags = WGDEVICE_HAS_PRIVATE_KEY | WGDEVICE_HAS_LISTEN_PORT,
53 .first_peer = &new_peer,
54 .last_peer = &new_peer
55 };
56 wg_key temp_private_key;
57
58 wg_generate_private_key(temp_private_key);
59 wg_generate_public_key(new_peer.public_key, temp_private_key);
60 wg_generate_private_key(new_device.private_key);
61
62 if (wg_add_device(new_device.name) < 0) {
63 perror("Unable to add device");
64 exit(1);
65 }
66
67 if (wg_set_device(&new_device) < 0) {
68 perror("Unable to set device");
69 exit(1);
70 }
71
72 list_devices();
73
74 if (wg_del_device(new_device.name) < 0) {
75 perror("Unable to delete device");
76 exit(1);
77 }
78
79 return 0;
80 }
81