Add cross-platform BLE transport and Bluetooth settings

Integrate shared C transport with desktop and Android frontends, add radio probes and detailed Windows diagnostics, and fix GUI build dependencies and early configuration startup errors.
This commit is contained in:
evgeny committed 2026-10-29 10:51:19 +01:00
1 parent 25cea8ca4c
commit f5bac950d1
82 files changed
+6608 -146

No files matched your search

+108 -97
View File
@@ -53,7 +53,7 @@ typedef enum {
static int assign_string(char *dest, size_t dest_size, const char *src) {
if (!dest || !src || strlen(src) >= dest_size) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "configuration string exceeds capacity=%zu or has a missing argument", dest_size);
CONFIG_ERROR("configuration string exceeds capacity=%zu or has a missing argument", dest_size);
return -1;
}
strcpy(dest, src);
@@ -67,7 +67,7 @@ static int parse_unsigned(const char* text, int base, uint64_t maximum, uint64_t
unsigned long long value = strtoull(text, &end, base);
if ((base == 16 ? config_ini_nibble((unsigned char)*text) < 0 : !isdigit((unsigned char)*text)) ||
end == text || *end || errno || value > maximum) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "invalid unsigned base-%d value '%s', maximum=%llu", base, text, (unsigned long long)maximum);
CONFIG_ERROR("invalid unsigned base-%d value '%s', maximum=%llu", base, text, (unsigned long long)maximum);
return -1;
}
*out = value; return 0;
@@ -79,7 +79,7 @@ static int parse_integer(const char* text, int minimum, int maximum, int* out) {
errno = 0;
long value = strtol(text, &end, 10);
if ((!isdigit((unsigned char)*text) && *text != '-') || end == text || *end || errno || value < minimum || value > maximum) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "invalid integer '%s', expected %d..%d", text, minimum, maximum); return -1;
CONFIG_ERROR("invalid integer '%s', expected %d..%d", text, minimum, maximum); return -1;
}
*out = (int)value; return 0;
}
@@ -88,7 +88,7 @@ static int parse_integer(const char* text, int minimum, int maximum, int* out) {
static int parse_boolean(const char* text) {
if (!strcmp(text, "1") || !strcasecmp(text, "yes") || !strcasecmp(text, "true")) return 1;
if (!strcmp(text, "0") || !strcasecmp(text, "no") || !strcasecmp(text, "false")) return 0;
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "invalid boolean '%s', expected yes/no, true/false or 1/0", text); return -1;
CONFIG_ERROR("invalid boolean '%s', expected yes/no, true/false or 1/0", text); return -1;
}
/* Версия содержит ровно три десятичных октета, без хвоста; выход применяется целиком. */
@@ -100,7 +100,7 @@ static int parse_version(const char* value, uint8_t out[3]) {
for (unsigned i = 0; i < 3; i++) {
char* dot = strchr(cursor, '.');
if ((i < 2 && !dot) || (i == 2 && dot)) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "invalid version format, expected x.y.z"); return -1;
CONFIG_ERROR("invalid version format, expected x.y.z"); return -1;
}
if (dot) *dot = '\0';
uint64_t number;
@@ -121,7 +121,7 @@ static int parse_ip_with_netmask(const char *str, struct IP *ip, uint8_t *netmas
if (inet_pton(AF_INET, ip_str, &parsed.addr.v4) == 1) { parsed.family = AF_INET; maximum = 32; }
else if (inet_pton(AF_INET6, ip_str, &parsed.addr.v6) == 1) { parsed.family = AF_INET6; maximum = 128; }
else {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "invalid IP address: %s", str); return -1;
CONFIG_ERROR("invalid IP address: %s", str); return -1;
}
uint64_t prefix = maximum;
if (slash && parse_unsigned(slash, 10, maximum, &prefix)) return -1;
@@ -141,7 +141,7 @@ static int parse_sockaddr(const char *addr_str, const char *port_str, struct soc
if (addr_str[0] == '[') {
size_t len = strlen(addr_str);
if (len > 2 && addr_str[len - 1] == ']') {
if (len - 2 >= sizeof(clean_addr)) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "bracketed address is too long"); return -1; }
if (len - 2 >= sizeof(clean_addr)) { CONFIG_ERROR("bracketed address is too long"); return -1; }
memcpy(clean_addr, addr_str + 1, len - 2);
clean_addr[len - 2] = '\0';
a = clean_addr;
@@ -151,11 +151,16 @@ static int parse_sockaddr(const char *addr_str, const char *port_str, struct soc
struct addrinfo *result;
int rc = getaddrinfo(a, port_str, &hints, &result);
if (rc != 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "cannot resolve configured endpoint host=%s port=%s error=%d", a, port_str, rc);
#ifdef _WIN32
const char *reason = rc == WSANOTINITIALISED ? "WinSock is not initialized; call WSAStartup first" : gai_strerrorA(rc);
#else
const char *reason = gai_strerror(rc);
#endif
CONFIG_ERROR("cannot resolve configured endpoint host=%s port=%s error=%d (%s)", a, port_str, rc, reason);
return -1;
}
if (!result || result->ai_addrlen > sizeof(*sockaddr)) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "resolved endpoint has an invalid address size"); freeaddrinfo(result); return -1;
CONFIG_ERROR("resolved endpoint has an invalid address size"); freeaddrinfo(result); return -1;
}
memset(sockaddr, 0, sizeof(*sockaddr));
memcpy(sockaddr, result->ai_addr, result->ai_addrlen);
@@ -168,7 +173,7 @@ static int parse_address_and_port(const char *str, struct sockaddr_storage *sock
if (assign_string(addr_copy, sizeof(addr_copy), str)) return -1;
char *port_str = strrchr(addr_copy, ':');
if (!port_str) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "endpoint requires host:port: %s", str); return -1; }
if (!port_str) { CONFIG_ERROR("endpoint requires host:port: %s", str); return -1; }
*port_str = '\0';
port_str++;
@@ -190,7 +195,7 @@ static uint32_t get_netif_index(const char *ifname) {
static struct CFG_CLIENT_LINK* create_client_link(struct CFG_SERVER* local_srv, const char *remote_addr) {
struct CFG_CLIENT_LINK *link = u_calloc(1, sizeof(struct CFG_CLIENT_LINK));
if (!link) {
DEBUG_ERROR(DEBUG_CATEGORY_SYS, "create_client_link: failed to allocate memory for client link");
CONFIG_ERROR("create_client_link: failed to allocate memory for client link");
return NULL;
}
@@ -217,7 +222,7 @@ static void free_cfg_client_links(struct CFG_CLIENT_LINK *links) {
static struct CFG_ROUTE_ENTRY* create_route_entry(const char *subnet_str) {
struct CFG_ROUTE_ENTRY *entry = u_calloc(1, sizeof(struct CFG_ROUTE_ENTRY));
if (!entry) {
DEBUG_ERROR(DEBUG_CATEGORY_SYS, "create_route_entry: failed to allocate memory for route entry");
CONFIG_ERROR("create_route_entry: failed to allocate memory for route entry");
return NULL;
}
@@ -250,11 +255,11 @@ static int add_route_entry(struct CFG_ROUTE_ENTRY **list, const char *subnet_str
/* Разрешение добавляется только после проверки всей строки; ошибочный порт не становится wildcard. */
static int parse_firewall_rule(const char *rule_str, struct global_config *global) {
if (!rule_str || !global) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Firewall: missing rule/config"); return -1; }
if (!rule_str || !global) { CONFIG_ERROR("Firewall: missing rule/config"); return -1; }
char rule_copy[64];
if (strlen(rule_str) >= sizeof(rule_copy)) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Firewall: rule is too long: %s", rule_str);
CONFIG_ERROR("Firewall: rule is too long: %s", rule_str);
return -1;
}
strcpy(rule_copy, rule_str);
@@ -275,7 +280,7 @@ static int parse_firewall_rule(const char *rule_str, struct global_config *globa
errno = 0;
unsigned long parsed = strtoul(port_str, &end, 10);
if (!isdigit((unsigned char)*port_str) || errno || *end || parsed > UINT16_MAX) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Firewall: invalid port in rule: %s", rule_str);
CONFIG_ERROR("Firewall: invalid port in rule: %s", rule_str);
return -1;
}
port = (uint16_t)parsed;
@@ -284,7 +289,7 @@ static int parse_firewall_rule(const char *rule_str, struct global_config *globa
// Parse IP to uint32_t (host order)
struct in_addr addr;
if (inet_pton(AF_INET, rule_copy, &addr) != 1) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "parse_firewall_rule: invalid IP: %s", rule_str);
CONFIG_ERROR("parse_firewall_rule: invalid IP: %s", rule_str);
return -1;
}
@@ -293,14 +298,14 @@ static int parse_firewall_rule(const char *rule_str, struct global_config *globa
// Reallocate array if needed
if ((global->firewall_rule_count % INITIAL_ARRAY_CAPACITY) == 0) {
if ((uint32_t)global->firewall_rule_count > UINT32_MAX / sizeof(struct CFG_FIREWALL_RULE) - INITIAL_ARRAY_CAPACITY) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Firewall: too many rules");
CONFIG_ERROR("Firewall: too many rules");
return -1;
}
int new_cap = global->firewall_rule_count + INITIAL_ARRAY_CAPACITY;
struct CFG_FIREWALL_RULE *new_rules = u_realloc(global->firewall_rules,
new_cap * sizeof(struct CFG_FIREWALL_RULE));
if (!new_rules) {
DEBUG_ERROR(DEBUG_CATEGORY_SYS, "Failed to realloc firewall rules");
CONFIG_ERROR("Failed to realloc firewall rules");
return -1;
}
global->firewall_rules = new_rules;
@@ -319,7 +324,7 @@ static int parse_control_allow(const char *rule_str, struct global_config *globa
struct IP address;
uint8_t cidr;
if (parse_ip_with_netmask(rule_str, &address, &cidr) || address.family != AF_INET) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "control allow requires IPv4[/0..32]: %s", rule_str);
CONFIG_ERROR("control allow requires IPv4[/0..32]: %s", rule_str);
return -1;
}
uint32_t ip = ntohl(address.addr.v4.s_addr);
@@ -327,13 +332,13 @@ static int parse_control_allow(const char *rule_str, struct global_config *globa
if ((global->control_allow_count % INITIAL_ARRAY_CAPACITY) == 0) {
if ((uint32_t)global->control_allow_count > UINT32_MAX / sizeof(struct CFG_CONTROL_ALLOW) - INITIAL_ARRAY_CAPACITY) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "too many control allow rules"); return -1;
CONFIG_ERROR("too many control allow rules"); return -1;
}
int new_cap = global->control_allow_count + INITIAL_ARRAY_CAPACITY;
struct CFG_CONTROL_ALLOW *new_allows = u_realloc(global->control_allows,
new_cap * sizeof(struct CFG_CONTROL_ALLOW));
if (!new_allows) {
DEBUG_ERROR(DEBUG_CATEGORY_SYS, "Failed to realloc control allows");
CONFIG_ERROR("Failed to realloc control allows");
return -1;
}
global->control_allows = new_allows;
@@ -361,7 +366,7 @@ static struct CFG_SERVER* find_server_by_name(struct CFG_SERVER *servers, const
static int parse_global(const char *key, const char *value, struct global_config *global, const char *filename, int line_num) {
if ((!strcmp(key, "my_private_key") || !strcmp(key, "my_public_key")) && *value && config_ini_hex(value, NULL, SC_PRIVKEY_SIZE)) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: %s requires 64 HEX characters", filename, line_num, key); return -1;
CONFIG_ERROR("%s:%d: %s requires 64 HEX characters", filename, line_num, key); return -1;
}
if (!strcmp(key, "my_node_name")) {
if (strlen(value) >= sizeof(global->name))
@@ -428,10 +433,10 @@ static int parse_global(const char *key, const char *value, struct global_config
if (!strcasecmp(value, "tcp")) global->auto_socket_protocols = CFG_AUTO_SOCKET_TCP;
else if (!strcasecmp(value, "udp")) global->auto_socket_protocols = CFG_AUTO_SOCKET_UDP;
else if (!strcasecmp(value, "tcp+udp")) global->auto_socket_protocols = CFG_AUTO_SOCKET_BOTH;
else { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: expected auto_socket_protocols=tcp, udp or tcp+udp", filename, line_num); return -1; }
else { CONFIG_ERROR("%s:%d: expected auto_socket_protocols=tcp, udp or tcp+udp", filename, line_num); return -1; }
return 0;
}
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: unknown global option '%s'", filename, line_num, key); return -1;
CONFIG_ERROR("%s:%d: unknown global option '%s'", filename, line_num, key); return -1;
}
static int parse_control(const char *key, const char *value, struct global_config *global, const char *filename, int line_num) {
@@ -453,7 +458,7 @@ static int parse_control(const char *key, const char *value, struct global_confi
if (strcmp(key, "allow") == 0 || strcmp(key, "control_allow") == 0) {
return parse_control_allow(value, global);
}
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Unknown control option '%s'. Valid: ip/control_ip, port/control_port, allow/control_allow", filename, line_num, key);
CONFIG_ERROR("%s:%d: Unknown control option '%s'. Valid: ip/control_ip, port/control_port, allow/control_allow", filename, line_num, key);
return -1;
}
@@ -490,7 +495,7 @@ static int parse_tcp_proxy_client(const char *key, const char *value, struct glo
}
if (strcmp(key, "forward") == 0) {
if (global->tcp_proxy_client_mapping_count >= MAX_TCP_PROXY_CLIENT_MAPPINGS) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Too many tcp_proxy_client forward rules (max %d)", MAX_TCP_PROXY_CLIENT_MAPPINGS);
CONFIG_ERROR("Too many tcp_proxy_client forward rules (max %d)", MAX_TCP_PROXY_CLIENT_MAPPINGS);
return -1;
}
char buf[256];
@@ -501,24 +506,24 @@ static int parse_tcp_proxy_client(const char *key, const char *value, struct glo
char* arrow = strtok(NULL, " ");
char* remote_str = strtok(NULL, "");
if (!local_port_str || !arrow || !remote_str || strcmp(arrow, "->") != 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Invalid tcp_proxy_client forward format: %s (expected: port -> ip:port)", value);
CONFIG_ERROR("Invalid tcp_proxy_client forward format: %s (expected: port -> ip:port)", value);
return -1;
}
uint64_t local_port;
if (parse_unsigned(local_port_str, 10, UINT16_MAX, &local_port) || !local_port) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "invalid tcp_proxy_client forward local port"); return -1;
CONFIG_ERROR("invalid tcp_proxy_client forward local port"); return -1;
}
char* remote_ip_str = strtok(remote_str, ":");
char* remote_port_str = strtok(NULL, "");
if (!remote_ip_str || !remote_port_str) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Invalid tcp_proxy_client forward remote: %s (expected ip:port)", remote_str);
CONFIG_ERROR("Invalid tcp_proxy_client forward remote: %s (expected ip:port)", remote_str);
return -1;
}
uint64_t remote_port;
struct in_addr address;
if (parse_unsigned(remote_port_str, 10, UINT16_MAX, &remote_port) || !remote_port || inet_pton(AF_INET, remote_ip_str, &address) != 1) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "invalid tcp_proxy_client forward remote IPv4/port"); return -1;
CONFIG_ERROR("invalid tcp_proxy_client forward remote IPv4/port"); return -1;
}
int idx = global->tcp_proxy_client_mapping_count;
@@ -528,14 +533,14 @@ static int parse_tcp_proxy_client(const char *key, const char *value, struct glo
global->tcp_proxy_client_mapping_count++;
return 0;
}
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Unknown tcp_proxy_client option '%s'. Valid: enabled, tun_name, tun_ip, mtu, via_node, forward, socks_enabled, socks_addr, http_proxy_enabled, http_proxy_addr", filename, line_num, key);
CONFIG_ERROR("%s:%d: Unknown tcp_proxy_client option '%s'. Valid: enabled, tun_name, tun_ip, mtu, via_node, forward, socks_enabled, socks_addr, http_proxy_enabled, http_proxy_addr", filename, line_num, key);
return -1;
}
static int parse_nat(const char *key, const char *value, struct global_config *global, const char *filename, int line_num) {
if (strcmp(key, "tun_ifname") == 0) {
if (assign_string(global->nat_tun_ifname, sizeof(global->nat_tun_ifname), value) != 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: NAT interface name too long: %s", filename, line_num, value);
CONFIG_ERROR("%s:%d: NAT interface name too long: %s", filename, line_num, value);
return -1;
}
return 0;
@@ -543,7 +548,7 @@ static int parse_nat(const char *key, const char *value, struct global_config *g
if (strcmp(key, "tun_ip") == 0) {
uint8_t netmask;
if (parse_ip_with_netmask(value, &global->nat_tun_ip, &netmask) != 0 || global->nat_tun_ip.family != AF_INET) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: NAT requires an IPv4 tun_ip: %s", filename, line_num, value);
CONFIG_ERROR("%s:%d: NAT requires an IPv4 tun_ip: %s", filename, line_num, value);
return -1;
}
return 0;
@@ -553,7 +558,7 @@ static int parse_nat(const char *key, const char *value, struct global_config *g
errno = 0;
uint64_t node_id = strtoull(value, &end, 16);
if (!*value || *value == '-' || *value == '+' || *end || errno == ERANGE) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Invalid NAT provider ID: %s", filename, line_num, value);
CONFIG_ERROR("%s:%d: Invalid NAT provider ID: %s", filename, line_num, value);
return -1;
}
global->nat_via_node_id = node_id;
@@ -564,7 +569,7 @@ static int parse_nat(const char *key, const char *value, struct global_config *g
errno = 0;
unsigned long port = strtoul(value, &end, 10);
if (!isdigit((unsigned char)*value) || *end || errno == ERANGE || port == 0 || port > 65535) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Invalid NAT %s: %s (expected 1..65535)", filename, line_num, key, value);
CONFIG_ERROR("%s:%d: Invalid NAT %s: %s (expected 1..65535)", filename, line_num, key, value);
return -1;
}
if (strcmp(key, "port_start") == 0) global->nat_port_start = (uint16_t)port;
@@ -573,13 +578,13 @@ static int parse_nat(const char *key, const char *value, struct global_config *g
}
if (strcmp(key, "forward") == 0) {
if (global->nat_forward_count >= MAX_NAT_FORWARDS) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Too many NAT forward rules (max %d)", MAX_NAT_FORWARDS);
CONFIG_ERROR("Too many NAT forward rules (max %d)", MAX_NAT_FORWARDS);
return -1;
}
// Format: proto:node_id_hex:internal_ip:internal_port:external_port.
char buf[128];
if (strlen(value) >= sizeof(buf)) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: NAT forward value too long", filename, line_num);
CONFIG_ERROR("%s:%d: NAT forward value too long", filename, line_num);
return -1;
}
strcpy(buf, value);
@@ -589,7 +594,7 @@ static int parse_nat(const char *key, const char *value, struct global_config *g
fields[i] = cursor;
char* colon = strchr(cursor, ':');
if ((i < 4 && !colon) || (i == 4 && colon) || !*cursor || colon == cursor) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Invalid NAT forward: %s (proto:node_id:ip:internal_port:external_port)",
CONFIG_ERROR("%s:%d: Invalid NAT forward: %s (proto:node_id:ip:internal_port:external_port)",
filename, line_num, value);
return -1;
}
@@ -601,7 +606,7 @@ static int parse_nat(const char *key, const char *value, struct global_config *g
errno = 0;
uint64_t node_id = strtoull(fields[1], &end, 16);
if (!isxdigit((unsigned char)*fields[1]) || !node_id || *end || errno == ERANGE) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Invalid NAT forward node ID: %s", filename, line_num, fields[1]);
CONFIG_ERROR("%s:%d: Invalid NAT forward node ID: %s", filename, line_num, fields[1]);
return -1;
}
uint16_t ports[2];
@@ -609,7 +614,7 @@ static int parse_nat(const char *key, const char *value, struct global_config *g
errno = 0;
unsigned long port = strtoul(fields[i + 3], &end, 10);
if (!isdigit((unsigned char)*fields[i + 3]) || *end || errno == ERANGE || port == 0 || port > 65535) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Invalid NAT forward port: %s", filename, line_num, fields[i + 3]);
CONFIG_ERROR("%s:%d: Invalid NAT forward port: %s", filename, line_num, fields[i + 3]);
return -1;
}
ports[i] = (uint16_t)port;
@@ -619,13 +624,13 @@ static int parse_nat(const char *key, const char *value, struct global_config *g
if (strcasecmp(proto_str, "tcp") == 0) proto = IPPROTO_TCP;
else if (strcasecmp(proto_str, "udp") == 0) proto = IPPROTO_UDP;
else {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Invalid NAT forward proto: %s (tcp/udp)", proto_str);
CONFIG_ERROR("Invalid NAT forward proto: %s (tcp/udp)", proto_str);
return -1;
}
struct in_addr addr;
if (inet_pton(AF_INET, ip_str, &addr) != 1) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "Invalid NAT forward IP: %s", ip_str);
CONFIG_ERROR("Invalid NAT forward IP: %s", ip_str);
return -1;
}
@@ -640,7 +645,7 @@ static int parse_nat(const char *key, const char *value, struct global_config *g
ports[1], (unsigned long long)node_id, proto_str, ip_str, ports[0]);
return 0;
}
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Unknown NAT option '%s'. Valid: tun_ifname, tun_ip, nat_via, port_start, port_end, forward", filename, line_num, key);
CONFIG_ERROR("%s:%d: Unknown NAT option '%s'. Valid: tun_ifname, tun_ip, nat_via, port_start, port_end, forward", filename, line_num, key);
return -1;
}
@@ -680,7 +685,7 @@ static int parse_server(const char *key, const char *value, struct CFG_SERVER *s
} else if (strcmp(value, "local") == 0) {
srv->type = CFG_SERVER_TYPE_LOCAL;
} else {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: unknown server type '%s'", filename, line_num, value); return -1;
CONFIG_ERROR("%s:%d: unknown server type '%s'", filename, line_num, value); return -1;
}
return 0;
}
@@ -694,7 +699,7 @@ static int parse_server(const char *key, const char *value, struct CFG_SERVER *s
if (strcmp(key, "transport") == 0) {
if (strcmp(value, "tcp") == 0) srv->transport = 1;
else if (strcmp(value, "udp") == 0) srv->transport = 0;
else { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: unknown server transport '%s'", filename, line_num, value); return -1; }
else { CONFIG_ERROR("%s:%d: unknown server transport '%s'", filename, line_num, value); return -1; }
return 0;
}
if (strcmp(key, "reality") == 0) {
@@ -705,7 +710,7 @@ static int parse_server(const char *key, const char *value, struct CFG_SERVER *s
int enabled = parse_boolean(value); if (enabled < 0) return -1;
srv->socks_enabled = (uint8_t)enabled; return 0;
}
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Unknown server option '%s'. Valid: addr, so_mark, fib, netif, type, mtu, only_local, transport, reality, socks", filename, line_num, key);
CONFIG_ERROR("%s:%d: Unknown server option '%s'. Valid: addr, so_mark, fib, netif, type, mtu, only_local, transport, reality, socks", filename, line_num, key);
return -1;
}
@@ -739,7 +744,7 @@ static int parse_client(const char *key, const char *value, struct CFG_CLIENT *c
if (strcmp(key, "peer_public_key") == 0) {
if (*value && config_ini_hex(value, NULL, SC_PUBKEY_SIZE)) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: peer_public_key requires 64 HEX characters", filename, line_num); return -1;
CONFIG_ERROR("%s:%d: peer_public_key requires 64 HEX characters", filename, line_num); return -1;
}
return assign_string(cli->peer_public_key_hex, MAX_KEY_LEN, value);
}
@@ -755,14 +760,14 @@ static int parse_client(const char *key, const char *value, struct CFG_CLIENT *c
if (strcmp(key, "server_name") == 0) { return assign_string(cli->reality.server_name, sizeof(cli->reality.server_name), value); }
if (strcmp(key, "short_id") == 0) {
if (config_ini_hex(value, cli->reality.short_id, REALITY_SHORT_ID_SIZE) < 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: client reality short_id must be %d hex chars", filename, line_num, REALITY_SHORT_ID_SIZE * 2);
CONFIG_ERROR("%s:%d: client reality short_id must be %d hex chars", filename, line_num, REALITY_SHORT_ID_SIZE * 2);
return -1;
}
return 0;
}
if (strcmp(key, "public_key") == 0) {
if (config_ini_hex(value, cli->reality.server_static_pubkey, REALITY_AUTH_KEY_SIZE) < 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: client reality public_key must be %d hex chars", filename, line_num, REALITY_AUTH_KEY_SIZE * 2);
CONFIG_ERROR("%s:%d: client reality public_key must be %d hex chars", filename, line_num, REALITY_AUTH_KEY_SIZE * 2);
return -1;
}
return 0;
@@ -772,11 +777,11 @@ static int parse_client(const char *key, const char *value, struct CFG_CLIENT *c
}
if (strcmp(key, "fingerprint") == 0) {
if (strcmp(value, "openssl") == 0) { cli->reality.fingerprint = REALITY_FP_OPENSSL; return 0; }
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: unknown client reality fingerprint '%s' (valid: openssl)", filename, line_num, value);
CONFIG_ERROR("%s:%d: unknown client reality fingerprint '%s' (valid: openssl)", filename, line_num, value);
return -1;
}
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Unknown client option '%s'. Valid: link, peer_public_key, keepalive, reality, server_name, short_id, public_key, version, fingerprint", filename, line_num, key);
CONFIG_ERROR("%s:%d: Unknown client option '%s'. Valid: link, peer_public_key, keepalive, reality, server_name, short_id, public_key, version, fingerprint", filename, line_num, key);
return -1;
}
@@ -799,7 +804,7 @@ static int parse_chatserver(const char *key, const char *value, struct global_co
if (config_ini_hex(value, global->chatserver_add_key, sizeof(global->chatserver_add_key))) return -1;
int nonzero = 0;
for (size_t i = 0; i < sizeof(global->chatserver_add_key); i++) nonzero |= global->chatserver_add_key[i];
if (!nonzero) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "chatserver add_key must be nonzero"); return -1; }
if (!nonzero) { CONFIG_ERROR("chatserver add_key must be nonzero"); return -1; }
global->chatserver_has_add_key = 1;
return 0;
}
@@ -809,7 +814,7 @@ static int parse_chatserver(const char *key, const char *value, struct global_co
if (strcmp(key, "storage_total_size") == 0) {
uint64_t sz;
if (parse_size_with_suffix(value, &sz) < 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Invalid chatserver storage_total_size '%s' (expected number with M/G suffix)", filename, line_num, value);
CONFIG_ERROR("%s:%d: Invalid chatserver storage_total_size '%s' (expected number with M/G suffix)", filename, line_num, value);
return -1;
}
global->chatserver_storage_total_size = sz;
@@ -818,7 +823,7 @@ static int parse_chatserver(const char *key, const char *value, struct global_co
if (strcmp(key, "storage_unit_size") == 0) {
uint64_t sz;
if (parse_size_with_suffix(value, &sz) < 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Invalid chatserver storage_unit_size '%s' (expected number with M/G suffix)", filename, line_num, value);
CONFIG_ERROR("%s:%d: Invalid chatserver storage_unit_size '%s' (expected number with M/G suffix)", filename, line_num, value);
return -1;
}
global->chatserver_storage_unit_size = sz;
@@ -841,14 +846,14 @@ static int parse_network(const char *key, const char *value, struct CFG_NETWORK
return parse_unsigned(value, 16, UINT64_C(0x00ffffffffffffff), &net->id);
}
if (strcmp(key, "pubkey") == 0) {
if (config_ini_hex(value, NULL, SC_PUBKEY_SIZE)) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "network pubkey requires 64 HEX characters"); return -1; }
if (config_ini_hex(value, NULL, SC_PUBKEY_SIZE)) { CONFIG_ERROR("network pubkey requires 64 HEX characters"); return -1; }
return assign_string(net->pubkey_hex, sizeof(net->pubkey_hex), value);
}
if (strcmp(key, "signing_key") == 0) {
if (config_ini_hex(value, NULL, SC_PRIVKEY_SIZE)) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "network signing key requires 64 HEX characters"); return -1; }
if (config_ini_hex(value, NULL, SC_PRIVKEY_SIZE)) { CONFIG_ERROR("network signing key requires 64 HEX characters"); return -1; }
return assign_string(net->signing_key_hex, sizeof(net->signing_key_hex), value);
}
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Unknown network option '%s'. Valid: id, pubkey, signing_key", filename, line_num, key);
CONFIG_ERROR("%s:%d: Unknown network option '%s'. Valid: id, pubkey, signing_key", filename, line_num, key);
return -1;
}
@@ -861,7 +866,7 @@ static int parse_reality(const char *key, const char *value, struct reality_conf
if (strcmp(key, "dest") == 0) { return assign_string(rc->dest, sizeof(rc->dest), value); }
if (strcmp(key, "short_id") == 0) {
if (config_ini_hex(value, rc->short_id, REALITY_SHORT_ID_SIZE) < 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: reality short_id must be %d hex chars", filename, line_num, REALITY_SHORT_ID_SIZE * 2);
CONFIG_ERROR("%s:%d: reality short_id must be %d hex chars", filename, line_num, REALITY_SHORT_ID_SIZE * 2);
return -1;
}
return 0;
@@ -869,7 +874,7 @@ static int parse_reality(const char *key, const char *value, struct reality_conf
if (strcmp(key, "short_ids") == 0) {
if (config_ini_hex_list(value, &rc->short_ids[0][0], REALITY_SHORT_ID_SIZE,
REALITY_MAX_SHORT_IDS, &rc->short_id_count)) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: invalid reality short_ids list", filename, line_num);
CONFIG_ERROR("%s:%d: invalid reality short_ids list", filename, line_num);
return -1;
}
return 0;
@@ -877,7 +882,7 @@ static int parse_reality(const char *key, const char *value, struct reality_conf
if (strcmp(key, "public_key") == 0) {
rc->has_public_key = 0;
if (config_ini_hex(value, rc->public_key, REALITY_AUTH_KEY_SIZE) < 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: reality public_key must be %d hex chars", filename, line_num, REALITY_AUTH_KEY_SIZE * 2);
CONFIG_ERROR("%s:%d: reality public_key must be %d hex chars", filename, line_num, REALITY_AUTH_KEY_SIZE * 2);
return -1;
}
rc->has_public_key = 1;
@@ -886,7 +891,7 @@ static int parse_reality(const char *key, const char *value, struct reality_conf
if (strcmp(key, "private_key") == 0) {
rc->has_private_key = 0;
if (config_ini_hex(value, rc->private_key, REALITY_AUTH_KEY_SIZE) < 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: reality private_key must be %d hex chars", filename, line_num, REALITY_AUTH_KEY_SIZE * 2);
CONFIG_ERROR("%s:%d: reality private_key must be %d hex chars", filename, line_num, REALITY_AUTH_KEY_SIZE * 2);
return -1;
}
rc->has_private_key = 1;
@@ -899,10 +904,10 @@ static int parse_reality(const char *key, const char *value, struct reality_conf
if (strcmp(key, "relay_idle_timeout") == 0) return parse_integer(value, 0, INT_MAX / 10000, &rc->relay_idle_timeout_sec);
if (strcmp(key, "fingerprint") == 0) {
if (strcmp(value, "openssl") == 0) { rc->fingerprint = REALITY_FP_OPENSSL; return 0; }
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: unknown reality fingerprint '%s' (valid: openssl)", filename, line_num, value);
CONFIG_ERROR("%s:%d: unknown reality fingerprint '%s' (valid: openssl)", filename, line_num, value);
return -1;
}
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Unknown reality option '%s'. Valid: enabled, server_name, dest, short_id, short_ids, public_key, private_key, version, time_window, relay_idle_timeout, fingerprint", filename, line_num, key);
CONFIG_ERROR("%s:%d: Unknown reality option '%s'. Valid: enabled, server_name, dest, short_id, short_ids, public_key, private_key, version, time_window, relay_idle_timeout, fingerprint", filename, line_num, key);
return -1;
}
@@ -914,14 +919,14 @@ static int parse_socks(const char *key, const char *value, struct global_config
}
if (strcmp(key, "addr") == 0) {
const char *colon = strrchr(value, ':');
if (!colon) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: socks addr must be 'host:port'", filename, line_num); return -1; }
if (!colon) { CONFIG_ERROR("%s:%d: socks addr must be 'host:port'", filename, line_num); return -1; }
uint64_t port;
if (parse_unsigned(colon + 1, 10, UINT16_MAX, &port) || !port) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: bad socks port", filename, line_num); return -1;
CONFIG_ERROR("%s:%d: bad socks port", filename, line_num); return -1;
}
char host[64];
size_t hlen = (size_t)(colon - value);
if (!hlen || hlen >= sizeof(host)) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: invalid socks host length", filename, line_num); return -1; }
if (!hlen || hlen >= sizeof(host)) { CONFIG_ERROR("%s:%d: invalid socks host length", filename, line_num); return -1; }
memcpy(host, value, hlen); host[hlen] = '\0';
if (hlen >= 2 && host[0] == '[' && host[hlen - 1] == ']') { host[hlen - 1] = '\0'; memmove(host, host + 1, strlen(host)); }
strncpy(global->socks_host, host, sizeof(global->socks_host) - 1);
@@ -931,7 +936,7 @@ static int parse_socks(const char *key, const char *value, struct global_config
}
if (strcmp(key, "username") == 0) return assign_string(global->socks_username, sizeof(global->socks_username), value);
if (strcmp(key, "password") == 0) return assign_string(global->socks_password, sizeof(global->socks_password), value);
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Unknown socks option '%s'. Valid: enabled, addr, username, password", filename, line_num, key);
CONFIG_ERROR("%s:%d: Unknown socks option '%s'. Valid: enabled, addr, username, password", filename, line_num, key);
return -1;
}
@@ -940,11 +945,11 @@ static int parse_stun(const char* key, const char* value, struct global_config*
if (strcmp(key, "enabled") == 0) {
if (!strcasecmp(value, "yes") || !strcasecmp(value, "true") || !strcmp(value, "1")) global->stun_enabled = 1;
else if (!strcasecmp(value, "no") || !strcasecmp(value, "false") || !strcmp(value, "0")) global->stun_enabled = 0;
else { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: invalid stun enabled='%s'", filename, line_num, value); return -1; }
else { CONFIG_ERROR("%s:%d: invalid stun enabled='%s'", filename, line_num, value); return -1; }
return 0;
}
if (strcmp(key, "server") != 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: unknown stun option '%s'; valid: enabled, server", filename, line_num, key);
CONFIG_ERROR("%s:%d: unknown stun option '%s'; valid: enabled, server", filename, line_num, key);
return -1;
}
const char* colon = strrchr(value, ':');
@@ -953,24 +958,24 @@ static int parse_stun(const char* key, const char* value, struct global_config*
long port = colon ? strtol(colon + 1, &end, 10) : 3478;
if (!host_len || host_len >= sizeof(((struct CFG_STUN_SERVER*)0)->host) ||
(colon && (colon[1] == '\0' || *end || port < 1 || port > 65535))) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: invalid STUN server '%s'; expected host[:port]", filename, line_num, value);
CONFIG_ERROR("%s:%d: invalid STUN server '%s'; expected host[:port]", filename, line_num, value);
return -1;
}
for (size_t i = 0; i < host_len; i++) {
unsigned char c = (unsigned char)value[i];
if (!(isalnum(c) || c == '-' || c == '.' || c == '_')) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: invalid IPv4 STUN hostname '%s'", filename, line_num, value);
CONFIG_ERROR("%s:%d: invalid IPv4 STUN hostname '%s'", filename, line_num, value);
return -1;
}
}
for (struct CFG_STUN_SERVER* s = global->stun_servers; s; s = s->next) {
if (strlen(s->host) == host_len && !strncasecmp(s->host, value, host_len) && s->port == port) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: duplicate STUN server '%s'", filename, line_num, value);
CONFIG_ERROR("%s:%d: duplicate STUN server '%s'", filename, line_num, value);
return -1;
}
}
struct CFG_STUN_SERVER* server = u_calloc(1, sizeof(*server));
if (!server) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: cannot allocate STUN server", filename, line_num); return -1; }
if (!server) { CONFIG_ERROR("%s:%d: cannot allocate STUN server", filename, line_num); return -1; }
memcpy(server->host, value, host_len);
server->port = (uint16_t)port;
struct CFG_STUN_SERVER** tail = &global->stun_servers;
@@ -1034,10 +1039,11 @@ static int blr_get_line(blr_t *blr, char *line, size_t maxlen) {
}
struct utun_config* parse_config_from_buf(const char *buf, size_t len, const char *filename) {
config_error_clear();
if (!filename) filename = "<buffer>";
if (config_ini_validate(buf, len, filename)) return NULL;
struct utun_config *cfg = u_calloc(1, sizeof(struct utun_config));
if (!cfg) { DEBUG_ERROR(DEBUG_CATEGORY_SYS, "failed to allocate memory for config structure"); return NULL; }
if (!cfg) { CONFIG_ERROR("failed to allocate memory for config structure"); return NULL; }
cfg->global.name[0] = '\0';
cfg->global.mtu = 1280;
cfg->global.keepalive_timeout = 2000;
@@ -1098,14 +1104,14 @@ struct utun_config* parse_config_from_buf(const char *buf, size_t len, const cha
}
char key[MAX_LINE_LEN], value[MAX_LINE_LEN];
if (config_ini_key_value(trimmed, key, sizeof(key), value, sizeof(value)) < 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Invalid key=value format", filename, line_num);
CONFIG_ERROR("%s:%d: Invalid key=value format", filename, line_num);
goto error;
}
int option_result = 0;
switch (cur_section) {
case SECTION_GLOBAL:
if (parse_global(key, value, &cfg->global, filename, line_num) < 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: invalid global option %s", filename, line_num, key);
CONFIG_ERROR("%s:%d: invalid global option %s", filename, line_num, key);
goto error;
}
break;
@@ -1114,7 +1120,7 @@ struct utun_config* parse_config_from_buf(const char *buf, size_t len, const cha
case SECTION_ROUTING:
if (strcmp(key, "route_subnet") == 0) option_result = add_route_entry(&cfg->route_subnets, value);
else if (strcmp(key, "my_subnet") == 0) option_result = add_route_entry(&cfg->my_subnets, value);
else { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: unknown routing option '%s'", filename, line_num, key); goto error; }
else { CONFIG_ERROR("%s:%d: unknown routing option '%s'", filename, line_num, key); goto error; }
break;
case SECTION_DEBUG:
if (cfg->global.debug_levels.count < DEBUG_CATEGORY_COUNT) {
@@ -1125,11 +1131,11 @@ struct utun_config* parse_config_from_buf(const char *buf, size_t len, const cha
break;
case SECTION_FIREWALL:
if (strcmp(key, "allow") != 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Unknown firewall option '%s'", filename, line_num, key);
CONFIG_ERROR("%s:%d: Unknown firewall option '%s'", filename, line_num, key);
goto error;
}
if (parse_firewall_rule(value, &cfg->global) < 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Invalid firewall rule allow=%s", filename, line_num, value);
CONFIG_ERROR("%s:%d: Invalid firewall rule allow=%s", filename, line_num, value);
goto error;
}
break;
@@ -1144,7 +1150,7 @@ struct utun_config* parse_config_from_buf(const char *buf, size_t len, const cha
if (!nk) goto error;
if (config_ini_hex(value, nk->key_bin, SC_PUBKEY_SIZE)) { u_free(nk); goto error; }
nk->next = cfg->global.allowed_keys; cfg->global.allowed_keys = nk; cfg->global.allowed_keys_count++;
} else { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: unknown allowed_keys option '%s'", filename, line_num, key); goto error; }
} else { CONFIG_ERROR("%s:%d: unknown allowed_keys option '%s'", filename, line_num, key); goto error; }
break;
case SECTION_NAT:
cfg->global.nat_enabled = 1;
@@ -1165,7 +1171,7 @@ struct utun_config* parse_config_from_buf(const char *buf, size_t len, const cha
} else if (strcmp(key, "connect_timeout") == 0) {
option_result = parse_integer(value, 0, INT_MAX / 10, &cfg->global.tcp_proxy_server_connect_timeout_ms);
} else {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: Unknown tcp_proxy_server option '%s'. Valid: tcp_recv_buf, fib, so_mark, netif, bind_ip, connect_timeout", filename, line_num, key);
CONFIG_ERROR("%s:%d: Unknown tcp_proxy_server option '%s'. Valid: tcp_recv_buf, fib, so_mark, netif, bind_ip, connect_timeout", filename, line_num, key);
goto error;
}
break;
@@ -1183,12 +1189,15 @@ struct utun_config* parse_config_from_buf(const char *buf, size_t len, const cha
} else if (strcmp(key, "interval") == 0) {
option_result = parse_integer(value, 0, INT_MAX / 10000, &cfg->global.ntp_resync_interval);
if (cfg->global.ntp_resync_interval < 60) cfg->global.ntp_resync_interval = 60;
} else { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: unknown ntp option '%s'", filename, line_num, key); goto error; }
} else { CONFIG_ERROR("%s:%d: unknown ntp option '%s'", filename, line_num, key); goto error; }
break;
case SECTION_CHATSERVER:
if (strcmp(key, "db_path") == 0) option_result = assign_string(cfg->global.db_path, sizeof(cfg->global.db_path), value);
else if (parse_chatserver(key, value, &cfg->global, filename, line_num) < 0 &&
chat_setting_set_state(&cfg->global.chat_settings, key, value) < 0) goto error;
chat_setting_set_state(&cfg->global.chat_settings, key, value) < 0) {
CONFIG_ERROR("%s:%d: invalid or unknown chatserver option '%s'", filename, line_num, key);
goto error;
}
break;
case SECTION_GUI:
if (!strcasecmp(key, "utun_enabled")) {
@@ -1196,8 +1205,9 @@ struct utun_config* parse_config_from_buf(const char *buf, size_t len, const cha
cfg->global.gui_utun_enabled = 1;
else if (!strcasecmp(value, "no") || !strcasecmp(value, "false") || !strcmp(value, "0"))
cfg->global.gui_utun_enabled = 0;
else { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: invalid gui utun_enabled '%s'", filename, line_num, value); goto error; }
}
else { CONFIG_ERROR("%s:%d: invalid gui utun_enabled '%s'", filename, line_num, value); goto error; }
} else if (!strcasecmp(key, "bluetooth_flags"))
option_result = parse_integer(value, 0, 7, &cfg->global.gui_bluetooth_flags);
break;
case SECTION_REALITY: option_result = parse_reality(key, value, &cfg->global.reality, filename, line_num); break;
case SECTION_SOCKS: option_result = parse_socks(key, value, &cfg->global, filename, line_num); break;
@@ -1205,16 +1215,16 @@ struct utun_config* parse_config_from_buf(const char *buf, size_t len, const cha
case SECTION_LOG_UDP:
if (strcmp(key, "ip") == 0) option_result = assign_string(cfg->global.log_udp_ip, sizeof(cfg->global.log_udp_ip), value);
else if (strcmp(key, "port") == 0) option_result = parse_integer(value, 0, UINT16_MAX, &cfg->global.log_udp_port);
else { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: unknown log_udp option '%s'", filename, line_num, key); goto error; }
else { CONFIG_ERROR("%s:%d: unknown log_udp option '%s'", filename, line_num, key); goto error; }
break;
default: DEBUG_WARN(DEBUG_CATEGORY_CONFIG, "%s:%d: Key outside section: %s", filename, line_num, key); break;
}
if (option_result < 0) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: rejected option '%s'", filename, line_num, key); goto error;
CONFIG_ERROR("%s:%d: rejected option '%s'", filename, line_num, key); goto error;
}
}
if (cfg->global.stun_enabled && cfg->global.stun_server_count < 2) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s: enabled STUN requires at least two servers", filename);
CONFIG_ERROR("%s: enabled STUN requires at least two servers", filename);
goto error;
}
if (cur_section == SECTION_SERVER && cur_server) { cur_server->next = cfg->servers; cfg->servers = cur_server; cur_server = NULL; }
@@ -1222,7 +1232,7 @@ struct utun_config* parse_config_from_buf(const char *buf, size_t len, const cha
if (cur_section == SECTION_NETWORK && cur_network) { cur_network->next = cfg->networks; cfg->networks = cur_network; cur_network = NULL; }
for (struct CFG_NETWORK* net = cfg->networks; net; net = net->next) {
if (config_ini_hex(net->pubkey_hex, NULL, SC_PUBKEY_SIZE) || config_ini_hex(net->signing_key_hex, NULL, SC_PRIVKEY_SIZE)) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s: network '%s' requires both 32-byte HEX keys", filename, net->name); goto error;
CONFIG_ERROR("%s: network '%s' requires both 32-byte HEX keys", filename, net->name); goto error;
}
}
if (!cfg->global.tun_enabled && cfg->global.tun_ifname[0] == '\0' && cfg->global.tun_ip.family == AF_UNSPEC) {}
@@ -1230,7 +1240,7 @@ struct utun_config* parse_config_from_buf(const char *buf, size_t len, const cha
else { cfg->global.tun_enabled = 1; if (cfg->global.tun_ifname[0] == '\0') { snprintf(cfg->global.tun_ifname, sizeof(cfg->global.tun_ifname), "tun0"); } }
return cfg;
error:
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "%s:%d: configuration rejected; releasing partial model", filename, line_num);
CONFIG_ERROR("%s:%d: configuration rejected; releasing partial model", filename, line_num);
if (cur_server) u_free(cur_server);
if (cur_client) { free_cfg_client_links(cur_client->links); u_free(cur_client); }
if (cur_network) u_free(cur_network);
@@ -1240,13 +1250,14 @@ error:
/* Файл и буфер проходят один разбор; ошибки чтения/закрытия не дают частичный конфиг. */
struct utun_config* parse_config(const char *filename) {
if (!filename || !*filename) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "empty config filename"); return NULL; }
config_error_clear();
if (!filename || !*filename) { CONFIG_ERROR("empty config filename"); return NULL; }
FILE *file = fopen(filename, "rb");
if (!file) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "cannot open config %s: %s", filename, strerror(errno)); return NULL; }
if (!file) { CONFIG_ERROR("cannot open config %s: %s", filename, strerror(errno)); return NULL; }
char *text = NULL;
size_t length = 0;
int rc = config_ini_read(file, filename, &text, &length);
if (fclose(file)) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "cannot close config %s: %s", filename, strerror(errno)); rc = -1; }
if (fclose(file)) { CONFIG_ERROR("cannot close config %s: %s", filename, strerror(errno)); rc = -1; }
struct utun_config *config = rc ? NULL : parse_config_from_buf(text, length, filename);
u_free(text);
return config;
@@ -1423,13 +1434,13 @@ void print_config(const struct utun_config *cfg) {
int update_config_keys(const char *filename, const char *priv_key, const char *pub_key) {
if (!filename || !*filename || config_ini_hex(priv_key, NULL, SC_PRIVKEY_SIZE) || config_ini_hex(pub_key, NULL, SC_PUBKEY_SIZE)) {
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "update_config_keys requires a path and two 32-byte HEX keys"); return -1;
CONFIG_ERROR("update_config_keys requires a path and two 32-byte HEX keys"); return -1;
}
FILE *fp = fopen(filename, "a");
if (!fp) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "cannot append keys to %s: %s", filename, strerror(errno)); return -1; }
if (!fp) { CONFIG_ERROR("cannot append keys to %s: %s", filename, strerror(errno)); return -1; }
int failed = fprintf(fp, "\n[global]\nmy_private_key=%s\nmy_public_key=%s\n", priv_key, pub_key) < 0;
if (failed) DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "cannot write appended keys to %s: %s", filename, strerror(errno));
if (fclose(fp)) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "cannot close appended key file %s: %s", filename, strerror(errno)); failed = 1; }
if (failed) CONFIG_ERROR("cannot write appended keys to %s: %s", filename, strerror(errno));
if (fclose(fp)) { CONFIG_ERROR("cannot close appended key file %s: %s", filename, strerror(errno)); failed = 1; }
if (!failed) DEBUG_INFO(DEBUG_CATEGORY_CONFIG, "appended key configuration to %s", filename);
return failed ? -1 : 0;
}