Development discussion of WireGuard
 help / color / mirror / Atom feed
* Problem using "WireGuardSetConfiguration" for Wireguard-NT with heap allocated configuration
@ 2021-08-13 22:02 Lukas Lüdke
  2021-08-13 22:19 ` Jason A. Donenfeld
  0 siblings, 1 reply; 2+ messages in thread
From: Lukas Lüdke @ 2021-08-13 22:02 UTC (permalink / raw)
  To: wireguard

Hello,

I am trying to implement a custom wireguard client using the provided
wireguard.dll/wireguard.h in c++.
I tried to modify the example from here:
https://git.zx2c4.com/wireguard-nt/tree/example/example.c
It works, when I specify a struct with one interface
(WIREGUARD_INTERFACE) and one or more peers + allowed ips
(WIREGUARD_PEER, WIREGUARD_ALLOWED_IP) and then use
"WireGuardSetConfiguration" to the the configuration on the adapter,
like it the example.
But when I try to use a buffer allocated on the heap, containing the
interface and the peers + allowed_ips, I always get error 0x57 from
"WireGuardSetConfiguration".
It seems to work, when I only create the buffer and memcpy a
WIREGUARD_INTERFACE object into the buffer, but if I create a larger
buffer and also memcpy the WIREGUARD_PEER object into it, set the Flags
and set the PeersCount, I get the error.
like in the following example it does not work:

unsigned char* buffer = (unsigned char
*)malloc(sizeof(WIREGUARD_INTERFACE) + sizeof(WIREGUARD_PEER));
/*new_interface is the interface from the example.c file*/
new_interface.PeersCount = 1;
memcpy(buffer, &new_interface, sizeof(WIREGUARD_INTERFACE));
unsigned char* buffer2 = (buffer + sizeof(WIREGUARD_INTERFACE));
/*peer is a created peer, which does work, if I use it in a struct*/
memcpy(buffer2, &peer, sizeof(WIREGUARD_PEER));
if (!WireGuardSetConfiguration(Adapter, (WIREGUARD_INTERFACE
*)&(buffer), sizeof(struct t))) {
     LastError = LogError(L"Failed to set configuration", GetLastError());
     return cleanupAdapter(Adapter, WireGuard, LastError);
}

Does anyone has an idea, how I can implement this correctly, so that I
can dynamically add/remove the peers and I am not forced to create a
struct with fixed peer count?

Best Regards,
Lukas

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-08-13 22:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-13 22:02 Problem using "WireGuardSetConfiguration" for Wireguard-NT with heap allocated configuration Lukas Lüdke
2021-08-13 22:19 ` Jason A. Donenfeld

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).