Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/tools/net/ynl/tests/wireguard.c
170959 views
1
// SPDX-License-Identifier: GPL-2.0
2
#include <arpa/inet.h>
3
#include <string.h>
4
#include <stdio.h>
5
#include <errno.h>
6
#include <ynl.h>
7
8
#include "wireguard-user.h"
9
10
static void print_allowed_ip(const struct wireguard_wgallowedip *aip)
11
{
12
char addr_out[INET6_ADDRSTRLEN];
13
14
if (!inet_ntop(aip->family, aip->ipaddr, addr_out, sizeof(addr_out))) {
15
addr_out[0] = '?';
16
addr_out[1] = '\0';
17
}
18
printf("\t\t\t%s/%u\n", addr_out, aip->cidr_mask);
19
}
20
21
/* Only printing public key in this demo. For better key formatting,
22
* use the constant-time implementation as found in wireguard-tools.
23
*/
24
static void print_peer_header(const struct wireguard_wgpeer *peer)
25
{
26
unsigned int len = peer->_len.public_key;
27
uint8_t *key = peer->public_key;
28
unsigned int i;
29
30
if (len != 32)
31
return;
32
printf("\tPeer ");
33
for (i = 0; i < len; i++)
34
printf("%02x", key[i]);
35
printf(":\n");
36
}
37
38
static void print_peer(const struct wireguard_wgpeer *peer)
39
{
40
unsigned int i;
41
42
print_peer_header(peer);
43
printf("\t\tData: rx: %llu / tx: %llu bytes\n",
44
peer->rx_bytes, peer->tx_bytes);
45
printf("\t\tAllowed IPs:\n");
46
for (i = 0; i < peer->_count.allowedips; i++)
47
print_allowed_ip(&peer->allowedips[i]);
48
}
49
50
static void build_request(struct wireguard_get_device_req *req, char *arg)
51
{
52
char *endptr;
53
int ifindex;
54
55
ifindex = strtol(arg, &endptr, 0);
56
if (endptr != arg + strlen(arg) || errno != 0)
57
ifindex = 0;
58
if (ifindex > 0)
59
wireguard_get_device_req_set_ifindex(req, ifindex);
60
else
61
wireguard_get_device_req_set_ifname(req, arg);
62
}
63
64
int main(int argc, char **argv)
65
{
66
struct wireguard_get_device_list *devs;
67
struct wireguard_get_device_req *req;
68
struct ynl_error yerr;
69
struct ynl_sock *ys;
70
71
if (argc < 2) {
72
fprintf(stderr, "usage: %s <ifindex|ifname>\n", argv[0]);
73
return 1;
74
}
75
76
ys = ynl_sock_create(&ynl_wireguard_family, &yerr);
77
if (!ys) {
78
fprintf(stderr, "YNL: %s\n", yerr.msg);
79
return 2;
80
}
81
82
req = wireguard_get_device_req_alloc();
83
build_request(req, argv[1]);
84
85
devs = wireguard_get_device_dump(ys, req);
86
if (!devs) {
87
fprintf(stderr, "YNL (%d): %s\n", ys->err.code, ys->err.msg);
88
wireguard_get_device_req_free(req);
89
ynl_sock_destroy(ys);
90
return 3;
91
}
92
93
ynl_dump_foreach(devs, d) {
94
unsigned int i;
95
96
printf("Interface %d: %s\n", d->ifindex, d->ifname);
97
for (i = 0; i < d->_count.peers; i++)
98
print_peer(&d->peers[i]);
99
}
100
101
wireguard_get_device_list_free(devs);
102
wireguard_get_device_req_free(req);
103
ynl_sock_destroy(ys);
104
105
return 0;
106
}
107
108