208 lines
6.5 KiB
C
208 lines
6.5 KiB
C
/*
|
|||
|
|
* Header-only C11 helpers for validating DNS resolver presets and custom nameservers.
|
||
|
|
*/
|
||
|
|
#ifndef ANODE_DNS_VALID_H
|
||
|
|
#define ANODE_DNS_VALID_H
|
||
|
|
|
||
|
|
#include <arpa/inet.h>
|
||
|
|
#include <netinet/in.h>
|
||
|
|
#include <stdbool.h>
|
||
|
|
#include <stdint.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
#define ANODE_DNS_DEFAULT 0
|
||
|
|
#define ANODE_DNS_CLOUDFLARE 1
|
||
|
|
#define ANODE_DNS_GOOGLE 2
|
||
|
|
#define ANODE_DNS_RADICAL 3
|
||
|
|
#define ANODE_DNS_QUAD9 4
|
||
|
|
#define ANODE_DNS_CUSTOM 5
|
||
|
|
#define ANODE_DNS_COUNT 6
|
||
|
|
|
||
|
|
#define ANODE_DNS_MAX_SERVERS 6
|
||
|
|
|
||
|
|
typedef struct {
|
||
|
|
const char *mode;
|
||
|
|
const char *label;
|
||
|
|
const char *v4_csv;
|
||
|
|
const char *v6_csv;
|
||
|
|
} anode_dns_preset_t;
|
||
|
|
|
||
|
|
static const anode_dns_preset_t ANODE_DNS_PRESETS[ANODE_DNS_COUNT] = {
|
||
|
|
{"default", "Default (Router / DHCP)", "", ""},
|
||
|
|
{"cloudflare", "Cloudflare DNS", "1.1.1.1,1.0.0.1", "2606:4700:4700::1111"},
|
||
|
|
{"google", "Google Public DNS", "8.8.8.8,8.8.4.4", "2001:4860:4860::8888"},
|
||
|
|
{"radicaldns", "RadicalDNS", "88.198.92.222,192.71.166.92", "2a01:4f8:1c0c:82c0::1"},
|
||
|
|
{"quad9", "Quad9 (Filtered)", "9.9.9.9,149.112.112.112", "2620:fe::fe"},
|
||
|
|
{"custom", "Custom Nameservers", "", ""},
|
||
|
|
};
|
||
|
|
|
||
|
|
static inline int anode_dns_mode_index(const char *mode) {
|
||
|
|
if (!mode || !mode[0]) return -1;
|
||
|
|
for (int i = 0; i < ANODE_DNS_COUNT; i++) {
|
||
|
|
if (strcmp(mode, ANODE_DNS_PRESETS[i].mode) == 0) return i;
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
static inline bool anode_dns_mode_valid(const char *mode) {
|
||
|
|
return anode_dns_mode_index(mode) >= 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static inline bool anode_dns_token_chars_ok(const char *tok) {
|
||
|
|
if (!tok || !tok[0]) return false;
|
||
|
|
for (const unsigned char *p = (const unsigned char *)tok; *p; p++) {
|
||
|
|
bool ok = (*p >= '0' && *p <= '9') ||
|
||
|
|
(*p >= 'a' && *p <= 'f') ||
|
||
|
|
(*p >= 'A' && *p <= 'F') ||
|
||
|
|
*p == '.' || *p == ':';
|
||
|
|
if (!ok) return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
static inline bool anode_dns_v4_valid(const struct in_addr *a) {
|
||
|
|
uint32_t ip = ntohl(a->s_addr);
|
||
|
|
if (ip == 0x00000000U || ip == 0xffffffffU) return false;
|
||
|
|
if ((ip >> 24) == 0U) return false;
|
||
|
|
if ((ip & 0xf0000000U) == 0xe0000000U) return false;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
static inline bool anode_dns_v6_valid(const struct in6_addr *a) {
|
||
|
|
static const uint8_t z[16] = {0};
|
||
|
|
if (memcmp(a->s6_addr, z, 16) == 0) return false;
|
||
|
|
if (a->s6_addr[0] == 0xff) return false;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
static inline int anode_parse_dns_servers(const char *raw,
|
||
|
|
char v4[][16], int *n_v4, int max_v4,
|
||
|
|
char v6[][46], int *n_v6, int max_v6,
|
||
|
|
char *norm_csv, size_t norm_sz) {
|
||
|
|
int c4 = 0, c6 = 0;
|
||
|
|
char loc_v4[ANODE_DNS_MAX_SERVERS][16];
|
||
|
|
char loc_v6[ANODE_DNS_MAX_SERVERS][46];
|
||
|
|
|
||
|
|
if (n_v4) *n_v4 = 0;
|
||
|
|
if (n_v6) *n_v6 = 0;
|
||
|
|
if (norm_csv && norm_sz > 0) norm_csv[0] = '\0';
|
||
|
|
if (!raw || !raw[0]) return -1;
|
||
|
|
|
||
|
|
const char *p = raw;
|
||
|
|
while (*p) {
|
||
|
|
while (*p == ',' || *p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') p++;
|
||
|
|
if (!*p) break;
|
||
|
|
const char *start = p;
|
||
|
|
while (*p && *p != ',' && *p != ' ' && *p != '\t' && *p != '\r' && *p != '\n') p++;
|
||
|
|
size_t len = (size_t)(p - start);
|
||
|
|
if (len == 0 || len >= 46) return -1;
|
||
|
|
|
||
|
|
char tok[48];
|
||
|
|
memcpy(tok, start, len);
|
||
|
|
tok[len] = '\0';
|
||
|
|
if (!anode_dns_token_chars_ok(tok)) return -1;
|
||
|
|
|
||
|
|
struct in_addr a4;
|
||
|
|
struct in6_addr a6;
|
||
|
|
if (inet_pton(AF_INET, tok, &a4) == 1) {
|
||
|
|
if (!anode_dns_v4_valid(&a4)) return -1;
|
||
|
|
char canon[16];
|
||
|
|
if (!inet_ntop(AF_INET, &a4, canon, sizeof(canon))) return -1;
|
||
|
|
bool dup = false;
|
||
|
|
for (int i = 0; i < c4; i++) {
|
||
|
|
if (strcmp(loc_v4[i], canon) == 0) {
|
||
|
|
dup = true;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (!dup) {
|
||
|
|
if (c4 + c6 >= ANODE_DNS_MAX_SERVERS) return -1;
|
||
|
|
snprintf(loc_v4[c4], sizeof(loc_v4[c4]), "%s", canon);
|
||
|
|
if (v4 && c4 < max_v4) snprintf(v4[c4], 16, "%s", canon);
|
||
|
|
c4++;
|
||
|
|
}
|
||
|
|
} else if (inet_pton(AF_INET6, tok, &a6) == 1) {
|
||
|
|
if (!anode_dns_v6_valid(&a6)) return -1;
|
||
|
|
char canon[46];
|
||
|
|
if (!inet_ntop(AF_INET6, &a6, canon, sizeof(canon))) return -1;
|
||
|
|
bool dup = false;
|
||
|
|
for (int i = 0; i < c6; i++) {
|
||
|
|
if (strcmp(loc_v6[i], canon) == 0) {
|
||
|
|
dup = true;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (!dup) {
|
||
|
|
if (c4 + c6 >= ANODE_DNS_MAX_SERVERS) return -1;
|
||
|
|
snprintf(loc_v6[c6], sizeof(loc_v6[c6]), "%s", canon);
|
||
|
|
if (v6 && c6 < max_v6) snprintf(v6[c6], 46, "%s", canon);
|
||
|
|
c6++;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
int total = c4 + c6;
|
||
|
|
if (total <= 0) return -1;
|
||
|
|
if (n_v4) *n_v4 = c4;
|
||
|
|
if (n_v6) *n_v6 = c6;
|
||
|
|
|
||
|
|
if (norm_csv && norm_sz > 0) {
|
||
|
|
size_t pos = 0;
|
||
|
|
for (int i = 0; i < c4; i++) {
|
||
|
|
int w = snprintf(norm_csv + pos, norm_sz - pos, "%s%s", pos ? "," : "", loc_v4[i]);
|
||
|
|
if (w < 0 || (size_t)w >= norm_sz - pos) return -1;
|
||
|
|
pos += (size_t)w;
|
||
|
|
}
|
||
|
|
for (int i = 0; i < c6; i++) {
|
||
|
|
int w = snprintf(norm_csv + pos, norm_sz - pos, "%s%s", pos ? "," : "", loc_v6[i]);
|
||
|
|
if (w < 0 || (size_t)w >= norm_sz - pos) return -1;
|
||
|
|
pos += (size_t)w;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return total;
|
||
|
|
}
|
||
|
|
|
||
|
|
static inline int anode_parse_resolv_conf(const char *text, char *summary, size_t summary_sz) {
|
||
|
|
if (summary && summary_sz > 0) summary[0] = '\0';
|
||
|
|
if (!text || !text[0]) return 0;
|
||
|
|
|
||
|
|
int count = 0;
|
||
|
|
size_t pos = 0;
|
||
|
|
const char *cur = text;
|
||
|
|
while (*cur && count < 3) {
|
||
|
|
const char *nl = strchr(cur, '\n');
|
||
|
|
size_t raw_len = nl ? (size_t)(nl - cur) : strlen(cur);
|
||
|
|
char ln[256];
|
||
|
|
size_t cp = raw_len < sizeof(ln) - 1 ? raw_len : sizeof(ln) - 1;
|
||
|
|
memcpy(ln, cur, cp);
|
||
|
|
ln[cp] = '\0';
|
||
|
|
cur = nl ? nl + 1 : cur + raw_len;
|
||
|
|
|
||
|
|
char *p = ln;
|
||
|
|
while (*p == ' ' || *p == '\t') p++;
|
||
|
|
if (strncmp(p, "nameserver", 10) != 0) continue;
|
||
|
|
p += 10;
|
||
|
|
if (*p != ' ' && *p != '\t') continue;
|
||
|
|
while (*p == ' ' || *p == '\t') p++;
|
||
|
|
char *end = p;
|
||
|
|
while (*end && *end != ' ' && *end != '\t' && *end != '\r' && *end != '#') end++;
|
||
|
|
*end = '\0';
|
||
|
|
if (!anode_dns_token_chars_ok(p)) continue;
|
||
|
|
|
||
|
|
struct in_addr a4;
|
||
|
|
struct in6_addr a6;
|
||
|
|
if (inet_pton(AF_INET, p, &a4) != 1 && inet_pton(AF_INET6, p, &a6) != 1) continue;
|
||
|
|
|
||
|
|
if (summary && pos + 2 < summary_sz) {
|
||
|
|
int w = snprintf(summary + pos, summary_sz - pos, "%s%s", count ? ", " : "", p);
|
||
|
|
if (w > 0 && (size_t)w < summary_sz - pos) pos += (size_t)w;
|
||
|
|
}
|
||
|
|
count++;
|
||
|
|
}
|
||
|
|
return count;
|
||
|
|
}
|
||
|
|
|
||
|
|
#endif /* ANODE_DNS_VALID_H */
|