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

* Re: Problem using "WireGuardSetConfiguration" for Wireguard-NT with heap allocated configuration
  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
  0 siblings, 0 replies; 2+ messages in thread
From: Jason A. Donenfeld @ 2021-08-13 22:19 UTC (permalink / raw)
  To: Lukas Lüdke; +Cc: WireGuard mailing list

Hi Lukas,

This appears to be a C pointer snafu, with this line in the code you sent:

> if (!WireGuardSetConfiguration(Adapter, (WIREGUARD_INTERFACE*)&(buffer), sizeof(struct t))) {

The issue is that `buffer` is already a pointer to the malloc'd
memory, so passing &buffer is actually the address of the pointer that
points to the memory you want. The reason why the example.c code
passes "&thing" and not "thing" is because thing is on the stack. But
in your case, `buffer` is on the heap, so it's already a pointer. The
second thing that stands out is `sizeof(struct t)` might not be what
you want. Instead try providing as length the same length that you
passed to the malloc call -- sizeof(iface)+sizeof(peer).

Hope that helps.

Jason

^ 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

Development discussion of WireGuard

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://inbox.vuxu.org/wireguard/0 wireguard/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 wireguard wireguard/ https://inbox.vuxu.org/wireguard \
		wireguard@lists.zx2c4.com
	public-inbox-index wireguard

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.wireguard


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git