From 17e342f26826121b87576a032e88d1b49d80afe0 Mon Sep 17 00:00:00 2001 From: homueicon Date: Tue, 29 Sep 2026 21:00:18 +0100 Subject: [PATCH] added hosts file support --- Makefile | 13 +- README.md | 1 + hosts.list | 29 ++++ include/nx/nx.h | 1 + include/nx/nx_hosts.h | 38 +++++ md/FAQ.md | 18 ++ md/docs.md | 42 +++++ src/net/dns.c | 26 +++ src/net/hosts.c | 387 ++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 553 insertions(+), 2 deletions(-) create mode 100644 hosts.list create mode 100644 include/nx/nx_hosts.h create mode 100644 src/net/hosts.c diff --git a/Makefile b/Makefile index 6f9dc4e..b4e468b 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ BACKEND := src/reactor/reactor_poll.c endif SRCS := src/core/util.c src/core/sockaddr.c src/core/buf.c \ - src/net/socket.c \ + src/net/socket.c src/net/hosts.c \ src/reactor/reactor.c $(BACKEND) \ src/net/dns.c src/net/signal.c \ src/proto/url.c src/proto/http.c @@ -37,7 +37,7 @@ SHARED := libnx.so TEST_BINS := tests/test_buf tests/test_sockaddr tests/test_url \ tests/test_reactor tests/test_socket tests/test_http -.PHONY: all clean test install +.PHONY: all clean test install install-hosts all: $(STATIC) $(SHARED) @@ -65,3 +65,12 @@ install: all install -m 644 include/nx/*.h $(DESTDIR)$(PREFIX)/include/nx/ install -m 644 $(STATIC) $(DESTDIR)$(PREFIX)/lib/ install -m 755 $(SHARED) $(DESTDIR)$(PREFIX)/lib/ + +install-hosts: + install -d $(DESTDIR)/var/lib/libnx + @if [ ! -e $(DESTDIR)/var/lib/libnx/hosts.txt ] && [ ! -e $(DESTDIR)/var/lib/libnx/hosts.list ]; then \ + install -m 644 hosts.list $(DESTDIR)/var/lib/libnx/hosts.txt; \ + echo "installed $(DESTDIR)/var/lib/libnx/hosts.txt"; \ + else \ + echo "hosts file already present, leaving it alone"; \ + fi diff --git a/README.md b/README.md index 54a8078..4a3261e 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Nothing beyond base UNIX TCP sockets, event loops and async name resolution. - HTTP/1.1 client - URL parsing +- Optional hosts and alias overrides (`/var/lib/libnx/hosts.txt`) ## Unsupported diff --git a/hosts.list b/hosts.list new file mode 100644 index 0000000..040728f --- /dev/null +++ b/hosts.list @@ -0,0 +1,29 @@ +# libnx hosts file +# +# Optional. If this file is absent, libnx resolves names normally through the +# system resolver. Load it with nx_hosts_load_default(), which reads +# /var/lib/libnx/hosts.txt, or /var/lib/libnx/hosts.list if the first is missing. +# A missing file is not an error. +# +# One entry per line. Blank lines and lines starting with '#' are ignored. +# A '#' also starts an inline comment. +# +# [...] +# alias +# +# An alias makes resolve to whatever resolves to. The +# target must also be defined in this file; if it is not, the lookup fails. + +# Direct addresses (domain first, then one or more IPv4/IPv6 addresses): +# +# example.com 93.184.216.34 +# example.com 2606:2800:220:1:248:1893:25c8:1946 +# local.test 127.0.0.1 +# local.test ::1 +# api.internal 10.0.0.5 10.0.0.6 + +# Aliases (alias ): +# +# alias example.net example.com +# alias example.org example.com +# alias www.local local.test diff --git a/include/nx/nx.h b/include/nx/nx.h index eb97100..4551d1d 100644 --- a/include/nx/nx.h +++ b/include/nx/nx.h @@ -15,6 +15,7 @@ #include "nx_buf.h" #include "nx_socket.h" #include "nx_reactor.h" +#include "nx_hosts.h" #include "nx_dns.h" #include "nx_signal.h" #include "nx_url.h" diff --git a/include/nx/nx_hosts.h b/include/nx/nx_hosts.h new file mode 100644 index 0000000..096ecbb --- /dev/null +++ b/include/nx/nx_hosts.h @@ -0,0 +1,38 @@ +#ifndef NX_HOSTS_H +#define NX_HOSTS_H + +#include "nx_types.h" +#include "nx_sockaddr.h" + +#if defined(__cplusplus) +extern "C" { +#endif + +#define NX_HOSTS_DEFAULT_DIR "/var/lib/libnx" +#define NX_HOSTS_DEFAULT_FILE NX_HOSTS_DEFAULT_DIR "/hosts.txt" +#define NX_HOSTS_DEFAULT_ALT NX_HOSTS_DEFAULT_DIR "/hosts.list" + +typedef struct nx_hosts nx_hosts; + +int nx_hosts_load(nx_hosts **out, const char *path); + +void nx_hosts_free(nx_hosts *h); + +size_t nx_hosts_count(const nx_hosts *h); + +int nx_hosts_lookup(const nx_hosts *h, const char *name, int family, int port, + struct sockaddr_storage *out, socklen_t *outlen, int max); + +nx_hosts *nx_hosts_global(void); + +void nx_hosts_set_global(nx_hosts *h); + +void nx_hosts_clear_global(void); + +int nx_hosts_load_default(void); + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/md/FAQ.md b/md/FAQ.md index d975c81..514fbcf 100644 --- a/md/FAQ.md +++ b/md/FAQ.md @@ -62,6 +62,24 @@ on a worker thread and delivers the result on the reactor thread through the reactor's wakeup pipe. An in-flight async lookup cannot be cancelled, because `getaddrinfo` cannot be interrupted. +## Can I override DNS for specific names? + +Yes. Load a hosts file with `nx_hosts_load_default()` (or +`nx_hosts_set_global()`); `nx_resolve` then checks it before `getaddrinfo`. +Names found in the file are authoritative, and names that are not found fall +through to the system resolver. See [docs.md](docs.md#hosts-and-aliases). + +## What if the hosts file is missing? + +It is optional. A missing `/var/lib/libnx/hosts.txt` (or `hosts.list`) leaves the +table empty and resolution proceeds normally. Only a malformed file is an error. + +## What does an `alias` entry do? + +`alias a.example b.example` makes `a.example` resolve to the addresses of +`b.example`, which must itself be defined in the same hosts file. If the target +is missing, the lookup fails instead of falling back to DNS. + ## How do I do non-blocking I/O? Create the socket, put it in non-blocking mode with `nx_set_nonblock`, and drive diff --git a/md/docs.md b/md/docs.md index 40125f6..4d8582c 100644 --- a/md/docs.md +++ b/md/docs.md @@ -89,6 +89,7 @@ its own; every public header is self-contained. | `nx_buf.h` | growable byte buffer and fixed-capacity ring buffer | | `nx_socket.h` | socket helpers and connections | | `nx_reactor.h` | event loop over epoll / kqueue / poll | +| `nx_hosts.h` | optional hosts and alias overrides | | `nx_dns.h` | blocking and async name resolution | | `nx_signal.h` | self-pipe and signal handlers safe for a reactor | | `nx_url.h` | URL parsing for HTTP request targets | @@ -233,6 +234,47 @@ requested, because it hides working IPv4 names on hosts with no global IPv6 route. A resolution in flight cannot be cancelled: `getaddrinfo` offers no way to interrupt it. +## Hosts and aliases + +A hosts file can override resolution for selected names. It is optional: if no +file is present, resolution goes straight to the system resolver. The default +location is `/var/lib/libnx/hosts.txt`, falling back to +`/var/lib/libnx/hosts.list`. + +```c +if (nx_hosts_load_default() != 0) { + /* malformed file; a missing file is not an error */ +} +``` + +With a table loaded, `nx_resolve` and `nx_http_perform` consult it before +`getaddrinfo`. A name present in the table is authoritative: it either produces +its addresses or fails the lookup. Names that are not in the table fall through +to DNS as usual. + +Manage the process-global table with `nx_hosts_global()`, +`nx_hosts_set_global()` and `nx_hosts_clear_global()`, or load an explicit path +with `nx_hosts_load()` (a directory is searched for `hosts.txt`, then +`hosts.list`). `nx_hosts_lookup()` queries a table directly. + +File format, one entry per line: + +``` +# comment + [...] +alias +``` + +``` +example.com 93.184.216.34 +example.com 2606:2800:220:1:248:1893:25c8:1946 +alias example.net example.com +``` + +An alias resolves to whatever its target resolves to. The target must also be +defined in the same file; if it is not, the lookup fails rather than falling back +to DNS. Alias chains are followed up to 16 links. + ## Platform support - Linux: epoll backend, `accept4`, `pipe2`. diff --git a/src/net/dns.c b/src/net/dns.c index c7997bb..f9d297a 100644 --- a/src/net/dns.c +++ b/src/net/dns.c @@ -15,12 +15,26 @@ const char *nx_gai_strerror(int err) return s ? s : "unknown name resolution error"; } +static int service_to_port(const char *service) +{ + char *end; + long v; + + if (service == NULL || *service == '\0') return 0; + errno = 0; + v = strtol(service, &end, 10); + if (errno != 0 || end == service || *end != '\0') return 0; + if (v < 0 || v > 65535) return 0; + return (int)v; +} + int nx_resolve(const char *host, const char *service, int family, int socktype, struct sockaddr_storage *out, socklen_t *outlen, int max, int *gai_err) { struct addrinfo hints; struct addrinfo *res = NULL, *ai; + const nx_hosts *hosts; int n = 0; int rc; @@ -30,6 +44,18 @@ int nx_resolve(const char *host, const char *service, int family, return -1; } + hosts = nx_hosts_global(); + if (hosts != NULL) { + n = nx_hosts_lookup(hosts, host, family, service_to_port(service), out, + outlen, max); + if (n > 0) return n; + if (n < 0) { + if (gai_err) *gai_err = EAI_FAIL; + return -1; + } + n = 0; + } + memset(&hints, 0, sizeof(hints)); hints.ai_family = family; hints.ai_socktype = socktype; diff --git a/src/net/hosts.c b/src/net/hosts.c new file mode 100644 index 0000000..d2150f4 --- /dev/null +++ b/src/net/hosts.c @@ -0,0 +1,387 @@ +#include "nx_internal.h" + +#include +#include +#include +#include +#include + +#define NX_HOSTS_MAX_ALIAS_DEPTH 16 +#define NX_HOSTS_PATH_MAX 4096 + +struct nx_host_addr { + struct sockaddr_storage ss; + socklen_t len; +}; + +struct nx_host_entry { + char *name; + char *alias; + struct nx_host_addr *addrs; + size_t naddrs; + size_t addrs_cap; +}; + +struct nx_hosts { + struct nx_host_entry *entries; + size_t nentries; + size_t entries_cap; +}; + +static struct nx_host_entry *entry_find(const nx_hosts *h, const char *name) +{ + size_t i; + + for (i = 0; i < h->nentries; i++) { + if (nx_strcasecmp_ascii(h->entries[i].name, name) == 0) { + return &h->entries[i]; + } + } + return NULL; +} + +static struct nx_host_entry *entry_add(nx_hosts *h, const char *name) +{ + struct nx_host_entry *e; + struct nx_host_entry *grown; + size_t cap; + + if (h->nentries == h->entries_cap) { + cap = h->entries_cap != 0 ? h->entries_cap * 2 : 16; + grown = realloc(h->entries, cap * sizeof(*grown)); + if (grown == NULL) return NULL; + h->entries = grown; + h->entries_cap = cap; + } + + e = &h->entries[h->nentries]; + memset(e, 0, sizeof(*e)); + e->name = strdup(name); + if (e->name == NULL) return NULL; + h->nentries++; + return e; +} + +static int addr_add(struct nx_host_entry *e, const struct sockaddr_storage *ss, + socklen_t len) +{ + struct nx_host_addr *grown; + size_t cap; + + if (e->naddrs == e->addrs_cap) { + cap = e->addrs_cap != 0 ? e->addrs_cap * 2 : 4; + grown = realloc(e->addrs, cap * sizeof(*grown)); + if (grown == NULL) return -1; + e->addrs = grown; + e->addrs_cap = cap; + } + + e->addrs[e->naddrs].ss = *ss; + e->addrs[e->naddrs].len = len; + e->naddrs++; + return 0; +} + +static char *next_token(char **cursor) +{ + char *s = *cursor; + char *start; + + while (*s == ' ' || *s == '\t') s++; + if (*s == '\0' || *s == '#') { + *cursor = s; + return NULL; + } + + start = s; + while (*s != '\0' && *s != ' ' && *s != '\t') s++; + if (*s != '\0') { + *s = '\0'; + s++; + } + *cursor = s; + return start; +} + +static void chomp(char *s) +{ + size_t n = strlen(s); + + while (n > 0) { + char c = s[n - 1]; + if (c != ' ' && c != '\t' && c != '\r' && c != '\n') break; + s[--n] = '\0'; + } +} + +static int parse_line(nx_hosts *h, char *line) +{ + struct nx_host_entry *e; + char *cursor = line; + char *name; + char *target; + char *tok; + int added = 0; + + chomp(line); + while (*cursor == ' ' || *cursor == '\t') cursor++; + if (*cursor == '\0' || *cursor == '#') return 0; + + name = next_token(&cursor); + if (name == NULL) return 0; + + if (nx_strcasecmp_ascii(name, "alias") == 0) { + char *alias_name = next_token(&cursor); + target = next_token(&cursor); + + if (alias_name == NULL || target == NULL) { + errno = EINVAL; + return -1; + } + + e = entry_find(h, alias_name); + if (e == NULL) e = entry_add(h, alias_name); + if (e == NULL) { + errno = ENOMEM; + return -1; + } + + free(e->alias); + e->alias = strdup(target); + if (e->alias == NULL) { + errno = ENOMEM; + return -1; + } + return 0; + } + + e = entry_find(h, name); + if (e == NULL) e = entry_add(h, name); + if (e == NULL) { + errno = ENOMEM; + return -1; + } + + while ((tok = next_token(&cursor)) != NULL) { + struct sockaddr_storage ss; + + if (nx_addr_parse_ip(tok, 0, &ss) != 0) { + errno = EINVAL; + return -1; + } + if (addr_add(e, &ss, nx_sockaddr_len((struct sockaddr *)(void *)&ss)) != + 0) { + errno = ENOMEM; + return -1; + } + added = 1; + } + + if (!added) { + errno = EINVAL; + return -1; + } + return 0; +} + +static int parse_file(nx_hosts *h, const char *path) +{ + FILE *fp; + char *line = NULL; + size_t cap = 0; + ssize_t n; + int rc = 0; + + fp = fopen(path, "re"); + if (fp == NULL) return -1; + + while ((n = getline(&line, &cap, fp)) != -1) { + (void)n; + if (parse_line(h, line) != 0) { + rc = -1; + break; + } + } + + free(line); + fclose(fp); + return rc; +} + +static int is_file(const char *path) +{ + struct stat st; + + return stat(path, &st) == 0 && S_ISREG(st.st_mode); +} + +static int is_dir(const char *path) +{ + struct stat st; + + return stat(path, &st) == 0 && S_ISDIR(st.st_mode); +} + +static int join_path(char *buf, size_t bufsz, const char *dir, const char *name) +{ + int n = snprintf(buf, bufsz, "%s/%s", dir, name); + + if (n < 0 || (size_t)n >= bufsz) { + errno = ENAMETOOLONG; + return -1; + } + return 0; +} + +static int load_path(nx_hosts *h, const char *path) +{ + char buf[NX_HOSTS_PATH_MAX]; + + if (path == NULL) { + if (is_file(NX_HOSTS_DEFAULT_FILE)) { + return parse_file(h, NX_HOSTS_DEFAULT_FILE); + } + if (is_file(NX_HOSTS_DEFAULT_ALT)) { + return parse_file(h, NX_HOSTS_DEFAULT_ALT); + } + errno = ENOENT; + return 0; + } + + if (is_dir(path)) { + if (join_path(buf, sizeof(buf), path, "hosts.txt") != 0) return -1; + if (is_file(buf)) return parse_file(h, buf); + if (join_path(buf, sizeof(buf), path, "hosts.list") != 0) return -1; + if (is_file(buf)) return parse_file(h, buf); + errno = ENOENT; + return 0; + } + + if (!is_file(path)) { + errno = ENOENT; + return 0; + } + return parse_file(h, path); +} + +int nx_hosts_load(nx_hosts **out, const char *path) +{ + nx_hosts *h; + int saved; + + if (out == NULL) { + errno = EINVAL; + return -1; + } + *out = NULL; + + h = calloc(1, sizeof(*h)); + if (h == NULL) { + errno = ENOMEM; + return -1; + } + + if (load_path(h, path) != 0) { + saved = errno != 0 ? errno : EINVAL; + nx_hosts_free(h); + errno = saved; + return -1; + } + + *out = h; + return 0; +} + +void nx_hosts_free(nx_hosts *h) +{ + size_t i; + + if (h == NULL) return; + + for (i = 0; i < h->nentries; i++) { + free(h->entries[i].name); + free(h->entries[i].alias); + free(h->entries[i].addrs); + } + free(h->entries); + free(h); +} + +size_t nx_hosts_count(const nx_hosts *h) +{ + return h != NULL ? h->nentries : 0; +} + +int nx_hosts_lookup(const nx_hosts *h, const char *name, int family, int port, + struct sockaddr_storage *out, socklen_t *outlen, int max) +{ + const struct nx_host_entry *e; + int depth = 0; + int n = 0; + size_t i; + + if (h == NULL || name == NULL || out == NULL || outlen == NULL || max <= 0) { + errno = EINVAL; + return -1; + } + + e = entry_find(h, name); + if (e == NULL) return 0; + + while (e->naddrs == 0 && e->alias != NULL) { + if (++depth > NX_HOSTS_MAX_ALIAS_DEPTH) { + errno = ELOOP; + return -1; + } + e = entry_find(h, e->alias); + if (e == NULL) { + errno = ENOENT; + return -1; + } + } + + for (i = 0; i < e->naddrs && n < max; i++) { + if (family != AF_UNSPEC && e->addrs[i].ss.ss_family != family) continue; + out[n] = e->addrs[i].ss; + outlen[n] = e->addrs[i].len; + nx_addr_set_port((struct sockaddr *)(void *)&out[n], port); + n++; + } + + return n; +} + +static nx_hosts *nx_hosts_active; + +nx_hosts *nx_hosts_global(void) +{ + return nx_hosts_active; +} + +void nx_hosts_set_global(nx_hosts *h) +{ + if (nx_hosts_active == h) return; + nx_hosts_free(nx_hosts_active); + nx_hosts_active = h; +} + +void nx_hosts_clear_global(void) +{ + nx_hosts_set_global(NULL); +} + +int nx_hosts_load_default(void) +{ + nx_hosts *h = NULL; + + if (nx_hosts_load(&h, NULL) != 0) return -1; + + if (nx_hosts_count(h) == 0) { + nx_hosts_free(h); + nx_hosts_clear_global(); + return 0; + } + + nx_hosts_set_global(h); + return 0; +}