mirror of
https://github.com/amnezia-vpn/amneziawg-tools.git
synced 2025-09-06 09:02:50 +02:00
fix: support linux kernel
Some checks failed
Some checks failed
This commit is contained in:
parent
c5b17c2c9b
commit
a8ca46755f
3 changed files with 62 additions and 44 deletions
|
@ -9,6 +9,7 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
|
@ -167,4 +168,12 @@ static inline void free_wgdevice(struct wgdevice *dev)
|
||||||
free(dev);
|
free(dev);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline int contains_hyphen(const char *str)
|
||||||
|
{
|
||||||
|
if (strchr(str, '-') != NULL)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
@ -25,6 +26,35 @@
|
||||||
|
|
||||||
#define IPC_SUPPORTS_KERNEL_INTERFACE
|
#define IPC_SUPPORTS_KERNEL_INTERFACE
|
||||||
|
|
||||||
|
void put_magic_header_attr(struct nlmsghdr *nlh, int attr_name, char *header_field)
|
||||||
|
{
|
||||||
|
if (contains_hyphen(header_field)) {
|
||||||
|
mnl_attr_put_strz(nlh, attr_name, header_field);
|
||||||
|
} else {
|
||||||
|
uint32_t magic_header = strtoul(header_field, NULL, 10);
|
||||||
|
mnl_attr_put_u32(nlh, attr_name, magic_header);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GET_MAGIC_HEADER(attr, magic_header, attr_name)\
|
||||||
|
if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) {\
|
||||||
|
magic_header = strdup(mnl_attr_get_str(attr));\
|
||||||
|
if (!magic_header) {\
|
||||||
|
perror("strdup");\
|
||||||
|
return MNL_CB_ERROR;\
|
||||||
|
}\
|
||||||
|
device->flags |= attr_name;\
|
||||||
|
} else if (!mnl_attr_validate(attr, MNL_TYPE_U32)) {\
|
||||||
|
uint32_t numeric_value = mnl_attr_get_u32(attr);\
|
||||||
|
magic_header = malloc(12);\
|
||||||
|
if (!magic_header) {\
|
||||||
|
perror("malloc");\
|
||||||
|
return MNL_CB_ERROR;\
|
||||||
|
}\
|
||||||
|
snprintf(magic_header, 12, "%u", numeric_value);\
|
||||||
|
device->flags |= attr_name;\
|
||||||
|
}
|
||||||
|
|
||||||
#define SOCKET_BUFFER_SIZE (mnl_ideal_socket_buffer_size())
|
#define SOCKET_BUFFER_SIZE (mnl_ideal_socket_buffer_size())
|
||||||
|
|
||||||
struct interface {
|
struct interface {
|
||||||
|
@ -178,13 +208,13 @@ again:
|
||||||
if (dev->flags & WGDEVICE_HAS_S4)
|
if (dev->flags & WGDEVICE_HAS_S4)
|
||||||
mnl_attr_put_u16(nlh, WGDEVICE_A_S4, dev->transport_packet_junk_size);
|
mnl_attr_put_u16(nlh, WGDEVICE_A_S4, dev->transport_packet_junk_size);
|
||||||
if (dev->flags & WGDEVICE_HAS_H1)
|
if (dev->flags & WGDEVICE_HAS_H1)
|
||||||
mnl_attr_put_strz(nlh, WGDEVICE_A_H1, dev->init_packet_magic_header);
|
put_magic_header_attr(nlh, WGDEVICE_A_H1, dev->init_packet_magic_header);
|
||||||
if (dev->flags & WGDEVICE_HAS_H2)
|
if (dev->flags & WGDEVICE_HAS_H2)
|
||||||
mnl_attr_put_strz(nlh, WGDEVICE_A_H2, dev->response_packet_magic_header);
|
put_magic_header_attr(nlh, WGDEVICE_A_H2, dev->response_packet_magic_header);
|
||||||
if (dev->flags & WGDEVICE_HAS_H3)
|
if (dev->flags & WGDEVICE_HAS_H3)
|
||||||
mnl_attr_put_strz(nlh, WGDEVICE_A_H3, dev->underload_packet_magic_header);
|
put_magic_header_attr(nlh, WGDEVICE_A_H3, dev->underload_packet_magic_header);
|
||||||
if (dev->flags & WGDEVICE_HAS_H4)
|
if (dev->flags & WGDEVICE_HAS_H4)
|
||||||
mnl_attr_put_strz(nlh, WGDEVICE_A_H4, dev->transport_packet_magic_header);
|
put_magic_header_attr(nlh, WGDEVICE_A_H4, dev->transport_packet_magic_header);
|
||||||
if (dev->flags & WGDEVICE_HAS_I1)
|
if (dev->flags & WGDEVICE_HAS_I1)
|
||||||
mnl_attr_put_strz(nlh, WGDEVICE_A_I1, dev->i1);
|
mnl_attr_put_strz(nlh, WGDEVICE_A_I1, dev->i1);
|
||||||
if (dev->flags & WGDEVICE_HAS_I2)
|
if (dev->flags & WGDEVICE_HAS_I2)
|
||||||
|
@ -548,48 +578,16 @@ static int parse_device(const struct nlattr *attr, void *data)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case WGDEVICE_A_H1:
|
case WGDEVICE_A_H1:
|
||||||
if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) {
|
GET_MAGIC_HEADER(attr, device->init_packet_magic_header, WGDEVICE_HAS_H1);
|
||||||
device->init_packet_magic_header = strdup(mnl_attr_get_str(attr));
|
|
||||||
if (!device->init_packet_magic_header) {
|
|
||||||
perror("strdup");
|
|
||||||
return MNL_CB_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
device->flags |= WGDEVICE_HAS_H1;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case WGDEVICE_A_H2:
|
case WGDEVICE_A_H2:
|
||||||
if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) {
|
GET_MAGIC_HEADER(attr, device->response_packet_magic_header, WGDEVICE_HAS_H2);
|
||||||
device->response_packet_magic_header = strdup(mnl_attr_get_str(attr));
|
|
||||||
if (!device->response_packet_magic_header) {
|
|
||||||
perror("strdup");
|
|
||||||
return MNL_CB_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
device->flags |= WGDEVICE_HAS_H2;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case WGDEVICE_A_H3:
|
case WGDEVICE_A_H3:
|
||||||
if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) {
|
GET_MAGIC_HEADER(attr, device->underload_packet_magic_header, WGDEVICE_HAS_H3);
|
||||||
device->underload_packet_magic_header = strdup(mnl_attr_get_str(attr));
|
|
||||||
if (!device->underload_packet_magic_header) {
|
|
||||||
perror("strdup");
|
|
||||||
return MNL_CB_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
device->flags |= WGDEVICE_HAS_H3;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case WGDEVICE_A_H4:
|
case WGDEVICE_A_H4:
|
||||||
if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) {
|
GET_MAGIC_HEADER(attr, device->transport_packet_magic_header, WGDEVICE_HAS_H4);
|
||||||
device->transport_packet_magic_header = strdup(mnl_attr_get_str(attr));
|
|
||||||
if (!device->transport_packet_magic_header) {
|
|
||||||
perror("strdup");
|
|
||||||
return MNL_CB_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
device->flags |= WGDEVICE_HAS_H4;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case WGDEVICE_A_I1:
|
case WGDEVICE_A_I1:
|
||||||
if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) {
|
if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) {
|
||||||
|
|
|
@ -25,6 +25,17 @@
|
||||||
#include "ipc-uapi-unix.h"
|
#include "ipc-uapi-unix.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
static void set_magic_header(FILE *f, const char* prefix, const char *header_field)
|
||||||
|
{
|
||||||
|
if (contains_hyphen(header_field))
|
||||||
|
fprintf(f, "%s=%s\n", prefix, header_field);
|
||||||
|
else {
|
||||||
|
uint32_t magic_header = strtoul(header_field, NULL, 10);
|
||||||
|
fprintf(f, "%s=%u\n", prefix, magic_header);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int userspace_set_device(struct wgdevice *dev)
|
static int userspace_set_device(struct wgdevice *dev)
|
||||||
{
|
{
|
||||||
char hex[WG_KEY_LEN_HEX], ip[INET6_ADDRSTRLEN], host[4096 + 1], service[512 + 1];
|
char hex[WG_KEY_LEN_HEX], ip[INET6_ADDRSTRLEN], host[4096 + 1], service[512 + 1];
|
||||||
|
@ -66,13 +77,13 @@ static int userspace_set_device(struct wgdevice *dev)
|
||||||
if (dev->flags & WGDEVICE_HAS_S4)
|
if (dev->flags & WGDEVICE_HAS_S4)
|
||||||
fprintf(f, "s4=%u\n", dev->transport_packet_junk_size);
|
fprintf(f, "s4=%u\n", dev->transport_packet_junk_size);
|
||||||
if (dev->flags & WGDEVICE_HAS_H1)
|
if (dev->flags & WGDEVICE_HAS_H1)
|
||||||
fprintf(f, "h1=%s\n", dev->init_packet_magic_header);
|
set_magic_header(f, "h1", dev->init_packet_magic_header);
|
||||||
if (dev->flags & WGDEVICE_HAS_H2)
|
if (dev->flags & WGDEVICE_HAS_H2)
|
||||||
fprintf(f, "h2=%s\n", dev->response_packet_magic_header);
|
set_magic_header(f, "h2", dev->response_packet_magic_header);
|
||||||
if (dev->flags & WGDEVICE_HAS_H3)
|
if (dev->flags & WGDEVICE_HAS_H3)
|
||||||
fprintf(f, "h3=%s\n", dev->underload_packet_magic_header);
|
set_magic_header(f, "h3", dev->underload_packet_magic_header);
|
||||||
if (dev->flags & WGDEVICE_HAS_H4)
|
if (dev->flags & WGDEVICE_HAS_H4)
|
||||||
fprintf(f, "h4=%s\n", dev->transport_packet_magic_header);
|
set_magic_header(f, "h4", dev->transport_packet_magic_header);
|
||||||
|
|
||||||
if (dev->flags & WGDEVICE_HAS_I1)
|
if (dev->flags & WGDEVICE_HAS_I1)
|
||||||
fprintf(f, "i1=%s\n", dev->i1);
|
fprintf(f, "i1=%s\n", dev->i1);
|
||||||
|
|
Loading…
Add table
Reference in a new issue