From 43fe5956adfe803249877309facd693306018c85 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Thu, 28 Dec 2023 22:58:17 -0800 Subject: [PATCH] Use DNS implementation from Musl Libc Now that our socket system call polyfills are good enough to support Musl's DNS library we should be using that rather than the barebones domain name system implementation we rolled on our own. There's many benefits to making this change. So many, that I myself wouldn't feel qualified to enumerate them all. The Musl DNS code had to be changed in order to support Windows of course, which looks very solid so far --- Makefile | 10 +- dsp/mpeg/buffer.h | 4 +- dsp/mpeg/mpeg.h | 7 +- dsp/tty/tty.h | 1 - examples/BUILD.mk | 1 - examples/nc.c | 24 +- examples/whois.c | 4 +- libc/BUILD.mk | 1 - libc/calls/{h_errno.c => gethoststxtpath.c} | 16 +- .../getprotocolstxtpath.c} | 21 +- .../getservicestxtpath.c} | 25 +- libc/{dns => calls}/getsystemdirectorypath.c | 11 +- libc/calls/sysdir.internal.h | 11 + libc/dns/BUILD.mk | 59 - libc/dns/comparednsnames.c | 77 - libc/dns/consts.h | 19 - libc/dns/dns.h | 79 - libc/dns/dnsheader.c | 57 - libc/dns/dnsheader.h | 19 - libc/dns/dnsquestion.h | 14 - libc/dns/ent.h | 73 - libc/dns/gai_strerror.c | 67 - libc/dns/getaddrinfo.c | 116 - libc/dns/gethostbyaddr.c | 58 - libc/dns/gethostbyname.c | 70 - libc/dns/gethoststxt.c | 94 - libc/dns/getnameinfo.c | 115 - libc/dns/getntnameservers.c | 93 - libc/dns/getntprotocolstxtpath.c | 37 - libc/dns/getntservicestxtpath.c | 36 - libc/dns/getprotobyname.c | 46 - libc/dns/getprotobynumber.c | 47 - libc/dns/getresolvconf.c | 83 - libc/dns/getservbyname.c | 55 - libc/dns/getservbyport.c | 56 - libc/dns/herror.c | 36 - libc/dns/hostent.c | 37 - libc/dns/hoststxt.h | 38 - libc/dns/lookupprotobyname.c | 101 - libc/dns/lookupprotobynumber.c | 90 - libc/dns/lookupservicesbyname.c | 107 - libc/dns/lookupservicesbyport.c | 107 - libc/dns/netent.c | 45 - libc/dns/newaddrinfo.c | 39 - libc/dns/parsehoststxt.c | 72 - libc/dns/parseresolvconf.c | 68 - libc/dns/pascalifydnsname.c | 58 - libc/dns/protoent.c | 37 - libc/dns/prototxt.h | 17 - libc/dns/resolvconf.h | 22 - libc/dns/resolvedns.c | 113 - libc/dns/resolvednsreverse.c | 128 - libc/dns/resolvehostsreverse.c | 52 - libc/dns/resolvehoststxt.c | 62 - libc/dns/rfc0226.txt | 59 - libc/dns/rfc0247.txt | 222 -- libc/dns/rfc1035.txt | 3077 ----------------- libc/dns/serializednsquestion.c | 39 - libc/dns/servent.c | 37 - libc/dns/servicestxt.h | 17 - libc/isystem/netdb.h | 3 +- libc/log/log.h | 2 +- libc/stdio/printargs.c | 1 - libc/sysv/consts/inaddr.h | 5 + libc/x/xgetline.h | 2 +- net/http/isacceptablehost.c | 3 +- test/libc/BUILD.mk | 1 - test/libc/dns/BUILD.mk | 60 - test/libc/dns/comparednsnames_test.c | 110 - test/libc/dns/dnsheader_test.c | 47 - test/libc/dns/dnsquestion_test.c | 46 - test/libc/dns/parsehoststxt_test.c | 80 - test/libc/dns/parseresolvconf_test.c | 70 - test/libc/dns/pascalifydnsname_test.c | 79 - test/libc/dns/prototxt_test.c | 78 - test/libc/dns/resolvehostsreverse_test.c | 79 - test/libc/dns/resolvehoststxt_test.c | 78 - test/libc/dns/servicestxt_test.c | 184 - third_party/bash/BUILD.mk | 1 - third_party/finger/BUILD.mk | 1 - third_party/finger/net.c | 3 +- third_party/hiredis/BUILD.mk | 4 +- third_party/hiredis/net.c | 3 +- third_party/lua/lunix.c | 3 +- third_party/mbedtls/BUILD.mk | 2 +- third_party/mbedtls/net_sockets.c | 3 +- third_party/mbedtls/test/BUILD.mk | 4 +- third_party/musl/BUILD.mk | 6 + third_party/musl/dn_comp.c | 23 +- third_party/musl/dn_expand.c | 11 +- third_party/musl/dn_skipname.c | 5 - {libc/dns => third_party/musl}/dns.png | Bin third_party/musl/dns_parse.c | 61 + third_party/musl/ent.c | 49 + third_party/musl/fgetspent.c | 29 +- third_party/musl/freeaddrinfo.c | 47 + third_party/musl/gai_strerror.c | 56 + third_party/musl/getaddrinfo.c | 171 + third_party/musl/gethostbyaddr.c | 50 + third_party/musl/gethostbyaddr_r.c | 103 + third_party/musl/gethostbyname.c | 34 + third_party/musl/gethostbyname2.c | 49 + third_party/musl/gethostbyname2_r.c | 110 + third_party/musl/gethostbyname_r.c | 36 + third_party/musl/getnameinfo.c | 254 ++ third_party/musl/getservbyname.c | 38 + third_party/musl/getservbyname_r.c | 85 + third_party/musl/getservbyport.c | 38 + third_party/musl/getservbyport_r.c | 92 + third_party/musl/getspnam_r.c | 32 + third_party/musl/h_errno.c | 36 + third_party/musl/herror.c | 34 + third_party/musl/hstrerror.c | 43 + third_party/musl/insque.c | 1 - third_party/musl/lookup.internal.h | 6 +- third_party/musl/lookup_ipliteral.c | 1 + third_party/musl/lookup_name.c | 486 +++ third_party/musl/lookup_serv.c | 154 + third_party/musl/netdb.h | 138 + third_party/musl/netname.c | 38 + third_party/musl/passwd.h | 11 +- third_party/musl/proto.c | 116 + third_party/musl/pwd.c | 2 +- third_party/musl/res_mkquery.c | 8 +- third_party/musl/res_msend.c | 30 +- third_party/musl/res_query.c | 2 +- third_party/musl/resolvconf.c | 3 +- third_party/musl/serv.c | 41 + third_party/python/BUILD.mk | 1 - third_party/python/Modules/socketmodule.c | 3 +- third_party/stb/stb_image.h | 11 +- tool/build/BUILD.mk | 1 - tool/build/runit.c | 14 +- tool/curl/BUILD.mk | 4 +- tool/curl/curl.c | 4 +- tool/decode/lib/bitabuilder.h | 4 +- tool/decode/lib/socknames.c | 4 +- tool/hello/BUILD.mk | 2 +- tool/net/BUILD.mk | 1 - tool/net/dig.c | 4 +- tool/net/fetch.inc | 2 +- tool/net/lfuncs.c | 9 +- tool/net/redbean.c | 102 +- tool/net/winbench.c | 1 - tool/viz/BUILD.mk | 1 - tool/viz/printvideo.c | 1 - 146 files changed, 2646 insertions(+), 7190 deletions(-) rename libc/calls/{h_errno.c => gethoststxtpath.c} (85%) rename libc/{dns/freeaddrinfo.c => calls/getprotocolstxtpath.c} (83%) rename libc/{dns/hstrerror.c => calls/getservicestxtpath.c} (81%) rename libc/{dns => calls}/getsystemdirectorypath.c (88%) create mode 100644 libc/calls/sysdir.internal.h delete mode 100644 libc/dns/BUILD.mk delete mode 100644 libc/dns/comparednsnames.c delete mode 100644 libc/dns/consts.h delete mode 100644 libc/dns/dns.h delete mode 100644 libc/dns/dnsheader.c delete mode 100644 libc/dns/dnsheader.h delete mode 100644 libc/dns/dnsquestion.h delete mode 100644 libc/dns/ent.h delete mode 100644 libc/dns/gai_strerror.c delete mode 100644 libc/dns/getaddrinfo.c delete mode 100644 libc/dns/gethostbyaddr.c delete mode 100644 libc/dns/gethostbyname.c delete mode 100644 libc/dns/gethoststxt.c delete mode 100644 libc/dns/getnameinfo.c delete mode 100644 libc/dns/getntnameservers.c delete mode 100644 libc/dns/getntprotocolstxtpath.c delete mode 100644 libc/dns/getntservicestxtpath.c delete mode 100644 libc/dns/getprotobyname.c delete mode 100644 libc/dns/getprotobynumber.c delete mode 100644 libc/dns/getresolvconf.c delete mode 100644 libc/dns/getservbyname.c delete mode 100644 libc/dns/getservbyport.c delete mode 100644 libc/dns/herror.c delete mode 100644 libc/dns/hostent.c delete mode 100644 libc/dns/hoststxt.h delete mode 100644 libc/dns/lookupprotobyname.c delete mode 100644 libc/dns/lookupprotobynumber.c delete mode 100644 libc/dns/lookupservicesbyname.c delete mode 100644 libc/dns/lookupservicesbyport.c delete mode 100644 libc/dns/netent.c delete mode 100644 libc/dns/newaddrinfo.c delete mode 100644 libc/dns/parsehoststxt.c delete mode 100644 libc/dns/parseresolvconf.c delete mode 100644 libc/dns/pascalifydnsname.c delete mode 100644 libc/dns/protoent.c delete mode 100644 libc/dns/prototxt.h delete mode 100644 libc/dns/resolvconf.h delete mode 100644 libc/dns/resolvedns.c delete mode 100644 libc/dns/resolvednsreverse.c delete mode 100644 libc/dns/resolvehostsreverse.c delete mode 100644 libc/dns/resolvehoststxt.c delete mode 100644 libc/dns/rfc0226.txt delete mode 100644 libc/dns/rfc0247.txt delete mode 100644 libc/dns/rfc1035.txt delete mode 100644 libc/dns/serializednsquestion.c delete mode 100644 libc/dns/servent.c delete mode 100644 libc/dns/servicestxt.h delete mode 100644 test/libc/dns/BUILD.mk delete mode 100644 test/libc/dns/comparednsnames_test.c delete mode 100644 test/libc/dns/dnsheader_test.c delete mode 100644 test/libc/dns/dnsquestion_test.c delete mode 100644 test/libc/dns/parsehoststxt_test.c delete mode 100644 test/libc/dns/parseresolvconf_test.c delete mode 100644 test/libc/dns/pascalifydnsname_test.c delete mode 100644 test/libc/dns/prototxt_test.c delete mode 100644 test/libc/dns/resolvehostsreverse_test.c delete mode 100644 test/libc/dns/resolvehoststxt_test.c delete mode 100644 test/libc/dns/servicestxt_test.c rename {libc/dns => third_party/musl}/dns.png (100%) create mode 100644 third_party/musl/dns_parse.c create mode 100644 third_party/musl/ent.c create mode 100644 third_party/musl/freeaddrinfo.c create mode 100644 third_party/musl/gai_strerror.c create mode 100644 third_party/musl/getaddrinfo.c create mode 100644 third_party/musl/gethostbyaddr.c create mode 100644 third_party/musl/gethostbyaddr_r.c create mode 100644 third_party/musl/gethostbyname.c create mode 100644 third_party/musl/gethostbyname2.c create mode 100644 third_party/musl/gethostbyname2_r.c create mode 100644 third_party/musl/gethostbyname_r.c create mode 100644 third_party/musl/getnameinfo.c create mode 100644 third_party/musl/getservbyname.c create mode 100644 third_party/musl/getservbyname_r.c create mode 100644 third_party/musl/getservbyport.c create mode 100644 third_party/musl/getservbyport_r.c create mode 100644 third_party/musl/h_errno.c create mode 100644 third_party/musl/herror.c create mode 100644 third_party/musl/hstrerror.c create mode 100644 third_party/musl/lookup_name.c create mode 100644 third_party/musl/lookup_serv.c create mode 100644 third_party/musl/netdb.h create mode 100644 third_party/musl/netname.c create mode 100644 third_party/musl/proto.c create mode 100644 third_party/musl/serv.c diff --git a/Makefile b/Makefile index cf679ec7e..2ac81ac7e 100644 --- a/Makefile +++ b/Makefile @@ -198,9 +198,9 @@ include third_party/nsync/mem/BUILD.mk # │ You can now use stdio include libc/proc/BUILD.mk # │ You can now use threads include libc/dlopen/BUILD.mk # │ You can now use processes include libc/thread/BUILD.mk # │ You can finally call malloc() -include tool/hello/BUILD.mk # │ include third_party/zlib/BUILD.mk # │ include libc/stdio/BUILD.mk # │ +include tool/hello/BUILD.mk # │ include libc/time/BUILD.mk # │ include net/BUILD.mk # │ include third_party/vqsort/BUILD.mk # │ @@ -213,9 +213,8 @@ include third_party/intel/BUILD.mk # │ include third_party/aarch64/BUILD.mk # │ include libc/BUILD.mk #─┘ include libc/sock/BUILD.mk #─┐ -include libc/dns/BUILD.mk # ├──ONLINE RUNTIME -include net/http/BUILD.mk # │ You can communicate with the network -include third_party/musl/BUILD.mk # │ +include net/http/BUILD.mk # ├──ONLINE RUNTIME +include third_party/musl/BUILD.mk # │ You can communicate with the network include libc/x/BUILD.mk # │ include dsp/scale/BUILD.mk # │ include dsp/mpeg/BUILD.mk # │ @@ -297,7 +296,6 @@ include test/libc/calls/BUILD.mk include test/libc/x/BUILD.mk include test/libc/xed/BUILD.mk include test/libc/fmt/BUILD.mk -include test/libc/dns/BUILD.mk include test/libc/time/BUILD.mk include test/libc/proc/BUILD.mk include test/libc/stdio/BUILD.mk @@ -373,7 +371,6 @@ loc: o/$(MODE)/tool/build/summy.com COSMOPOLITAN_OBJECTS = \ TOOL_ARGS \ NET_HTTP \ - LIBC_DNS \ LIBC_SOCK \ LIBC_NT_WS2_32 \ LIBC_NT_IPHLPAPI \ @@ -424,7 +421,6 @@ COSMOPOLITAN_H_PKGS = \ APE \ LIBC \ LIBC_CALLS \ - LIBC_DNS \ LIBC_ELF \ LIBC_FMT \ LIBC_DLOPEN \ diff --git a/dsp/mpeg/buffer.h b/dsp/mpeg/buffer.h index 5fefc72b5..5ab5faaa6 100644 --- a/dsp/mpeg/buffer.h +++ b/dsp/mpeg/buffer.h @@ -3,8 +3,6 @@ #include "dsp/mpeg/mpeg.h" COSMOPOLITAN_C_START_ -struct FILE; - enum plm_buffer_mode { PLM_BUFFER_MODE_FILE, PLM_BUFFER_MODE_FIXED_MEM, @@ -17,7 +15,7 @@ typedef struct plm_buffer_t { unsigned length; int free_when_done; int close_when_done; - struct FILE *fh; + FILE *fh; plm_buffer_load_callback load_callback; void *load_callback_user_data; unsigned char *bytes; diff --git a/dsp/mpeg/mpeg.h b/dsp/mpeg/mpeg.h index b96c622b9..f49ed953b 100644 --- a/dsp/mpeg/mpeg.h +++ b/dsp/mpeg/mpeg.h @@ -1,9 +1,8 @@ #ifndef COSMOPOLITAN_DSP_MPEG_MPEG_H_ #define COSMOPOLITAN_DSP_MPEG_MPEG_H_ +#include "libc/stdio/stdio.h" COSMOPOLITAN_C_START_ -struct FILE; - typedef struct plm_t plm_t; typedef struct plm_buffer_t plm_buffer_t; typedef struct plm_demux_t plm_demux_t; @@ -112,7 +111,7 @@ plm_t *plm_create_with_filename(const char *filename); * to let plmpeg call fclose() on the handle when plm_destroy() is * called. */ -plm_t *plm_create_with_file(struct FILE *fh, int close_when_done); +plm_t *plm_create_with_file(FILE *fh, int close_when_done); /** * Create a plmpeg instance with pointer to memory as source. This assumes the @@ -257,7 +256,7 @@ plm_buffer_t *plm_buffer_create_with_filename(const char *filename); * to let plmpeg call fclose() on the handle when plm_destroy() is * called. */ -plm_buffer_t *plm_buffer_create_with_file(struct FILE *fh, int close_when_done); +plm_buffer_t *plm_buffer_create_with_file(FILE *fh, int close_when_done); /** * Create a buffer instance with a pointer to memory as source. This assumes diff --git a/dsp/tty/tty.h b/dsp/tty/tty.h index 99ca8f00e..2b4e8f46e 100644 --- a/dsp/tty/tty.h +++ b/dsp/tty/tty.h @@ -5,7 +5,6 @@ COSMOPOLITAN_C_START_ -struct FILE; struct termios; struct TtyIdent { diff --git a/examples/BUILD.mk b/examples/BUILD.mk index 8f372d849..4b839fdaf 100644 --- a/examples/BUILD.mk +++ b/examples/BUILD.mk @@ -44,7 +44,6 @@ EXAMPLES_DIRECTDEPS = \ DSP_TTY \ LIBC_CALLS \ LIBC_DLOPEN \ - LIBC_DNS \ LIBC_FMT \ LIBC_INTRIN \ LIBC_IRQ \ diff --git a/examples/nc.c b/examples/nc.c index f484013bb..3e29a51d2 100644 --- a/examples/nc.c +++ b/examples/nc.c @@ -8,7 +8,6 @@ ╚─────────────────────────────────────────────────────────────────*/ #endif #include "libc/calls/calls.h" -#include "libc/dns/dns.h" #include "libc/fmt/conv.h" #include "libc/log/log.h" #include "libc/macros.internal.h" @@ -26,6 +25,7 @@ #include "libc/sysv/consts/sock.h" #include "libc/sysv/consts/sol.h" #include "third_party/getopt/getopt.internal.h" +#include "third_party/musl/netdb.h" /** * @fileoverview netcat clone @@ -60,9 +60,7 @@ int main(int argc, char *argv[]) { halfclose = false; break; case 'h': - fputs("Usage: ", stdout); - fputs(argv[0], stdout); - fputs(" [-hH] IP PORT\n", stdout); + tinyprint(1, "Usage: ", argv[0], " [-hH] IP PORT\n", NULL); exit(0); default: fprintf(stderr, "bad option %d\n", opt); @@ -76,17 +74,9 @@ int main(int argc, char *argv[]) { host = argv[optind + 0]; port = argv[optind + 1]; - switch ((rc = getaddrinfo(host, port, &hint, &ai))) { - case EAI_SUCCESS: - break; - case EAI_SYSTEM: - perror("getaddrinfo"); - exit(1); - default: - fputs("EAI_", stderr); - fputs(gai_strerror(rc), stderr); - fputs("\n", stderr); - exit(1); + if ((rc = getaddrinfo(host, port, &hint, &ai))) { + tinyprint(2, host, ": ", gai_strerror(rc), "\n", NULL); + exit(1); } if ((sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) == -1) { @@ -95,12 +85,12 @@ int main(int argc, char *argv[]) { } if (setsockopt(sock, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger)) == -1) { - perror("setsockopt(SO_LINGER)"); + perror("SO_LINGER"); exit(1); } if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) { - perror("connect"); + perror(host); exit(1); } diff --git a/examples/whois.c b/examples/whois.c index 9483cb960..bae2bc359 100644 --- a/examples/whois.c +++ b/examples/whois.c @@ -30,12 +30,13 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" #include "libc/calls/weirdtypes.h" -#include "libc/dns/dns.h" #include "libc/errno.h" #include "libc/log/bsd.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" +#include "libc/sock/sock.h" #include "libc/sock/struct/pollfd.h" +#include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/sysv/consts/af.h" #include "libc/sysv/consts/ex.h" @@ -44,6 +45,7 @@ #include "libc/sysv/consts/poll.h" #include "libc/sysv/consts/sock.h" #include "third_party/getopt/getopt.internal.h" +#include "third_party/musl/netdb.h" // clang-format off asm(".ident\t\"\\n\\n\ diff --git a/libc/BUILD.mk b/libc/BUILD.mk index 2a60ab0bd..11e8eb3c2 100644 --- a/libc/BUILD.mk +++ b/libc/BUILD.mk @@ -276,7 +276,6 @@ LIBC_FILES := $(wildcard libc/*) o/$(MODE)/libc: o/$(MODE)/libc/calls \ o/$(MODE)/libc/crt \ o/$(MODE)/libc/dlopen \ - o/$(MODE)/libc/dns \ o/$(MODE)/libc/elf \ o/$(MODE)/libc/fmt \ o/$(MODE)/libc/intrin \ diff --git a/libc/calls/h_errno.c b/libc/calls/gethoststxtpath.c similarity index 85% rename from libc/calls/h_errno.c rename to libc/calls/gethoststxtpath.c index 401326c36..b80f28a2d 100644 --- a/libc/calls/h_errno.c +++ b/libc/calls/gethoststxtpath.c @@ -1,7 +1,7 @@ /*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ ╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2022 Justine Alexandra Roberts Tunney │ +│ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ @@ -16,11 +16,13 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/ent.h" +#include "libc/calls/sysdir.internal.h" +#include "libc/dce.h" -// error number global for gethostbyname*(), gethostbyaddr*(), etc. -static _Thread_local int __h_errno; - -errno_t *__h_errno_location(void) { - return &__h_errno; +const char *GetHostsTxtPath(char *path, size_t size) { + if (!IsWindows()) { + return "/etc/hosts"; + } else { + return GetSystemDirectoryPath(path, "drivers\\etc\\hosts", size); + } } diff --git a/libc/dns/freeaddrinfo.c b/libc/calls/getprotocolstxtpath.c similarity index 83% rename from libc/dns/freeaddrinfo.c rename to libc/calls/getprotocolstxtpath.c index f9fe85e10..782ba5633 100644 --- a/libc/dns/freeaddrinfo.c +++ b/libc/calls/getprotocolstxtpath.c @@ -1,7 +1,7 @@ /*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ ╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 Justine Alexandra Roberts Tunney │ +│ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ @@ -16,18 +16,13 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/dns.h" -#include "libc/mem/mem.h" +#include "libc/calls/sysdir.internal.h" +#include "libc/dce.h" -/** - * Frees addresses returned by getaddrinfo(). - */ -void freeaddrinfo(struct addrinfo *ai) { - struct addrinfo *next; - while (ai) { - /* we assume ai_addr and ai_canonname are shoehorned */ - next = ai->ai_next; - free(ai); - ai = next; +const char *GetProtocolsTxtPath(char *buf, size_t size) { + if (!IsWindows()) { + return "/etc/protocols"; + } else { + return GetSystemDirectoryPath(buf, "drivers\\etc\\protocol", size); } } diff --git a/libc/dns/hstrerror.c b/libc/calls/getservicestxtpath.c similarity index 81% rename from libc/dns/hstrerror.c rename to libc/calls/getservicestxtpath.c index 4344ed6b7..dae496cc2 100644 --- a/libc/dns/hstrerror.c +++ b/libc/calls/getservicestxtpath.c @@ -1,7 +1,7 @@ /*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ ╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2022 Justine Alexandra Roberts Tunney │ +│ Copyright 2023 Justine Alexandra Roberts Tunney │ │ │ │ Permission to use, copy, modify, and/or distribute this software for │ │ any purpose with or without fee is hereby granted, provided that the │ @@ -16,22 +16,13 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/ent.h" +#include "libc/calls/sysdir.internal.h" +#include "libc/dce.h" -/** - * Turns `h_errno` value into string. - */ -const char *hstrerror(int err) { - switch (err) { - case HOST_NOT_FOUND: - return "HOST_NOT_FOUND"; - case TRY_AGAIN: - return "TRY_AGAIN"; - case NO_RECOVERY: - return "NO_RECOVERY"; - case NO_DATA: - return "NO_DATA"; - default: - return "UNKNOWN"; +const char *GetServicesTxtPath(char *path, size_t size) { + if (!IsWindows()) { + return "/etc/services"; + } else { + return GetSystemDirectoryPath(path, "drivers\\etc\\services", size); } } diff --git a/libc/dns/getsystemdirectorypath.c b/libc/calls/getsystemdirectorypath.c similarity index 88% rename from libc/dns/getsystemdirectorypath.c rename to libc/calls/getsystemdirectorypath.c index f382e2686..4030e3913 100644 --- a/libc/dns/getsystemdirectorypath.c +++ b/libc/calls/getsystemdirectorypath.c @@ -16,19 +16,16 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/dns.h" #include "libc/nt/systeminfo.h" #include "libc/str/str.h" -// e.g. GetSystemDirectoryPath(buf, size, "FOO") → "C:\WINDOWS\SYSTEM32\FOO" -textwindows char *GetSystemDirectoryPath(char *buf, size_t size, - const char *path) { +// e.g. GetSystemDirectoryPath(buf, "FOO", size) → "C:\WINDOWS\SYSTEM32\FOO" +textwindows char *GetSystemDirectoryPath(char *buf, const char *path, + size_t size) { uint32_t syslen = GetSystemDirectoryA(buf, size); size_t pathlen = strlen(path); if (syslen && syslen + pathlen + 1 < size) { - if (buf[syslen] == '\\') { - --syslen; - } + if (buf[syslen] == '\\') --syslen; memcpy(buf + syslen, path, pathlen + 1); return buf; } else { diff --git a/libc/calls/sysdir.internal.h b/libc/calls/sysdir.internal.h new file mode 100644 index 000000000..be2ab710c --- /dev/null +++ b/libc/calls/sysdir.internal.h @@ -0,0 +1,11 @@ +#ifndef COSMOPOLITAN_LIBC_CALLS_SYSDIR_H_ +#define COSMOPOLITAN_LIBC_CALLS_SYSDIR_H_ +COSMOPOLITAN_C_START_ + +const char *GetHostsTxtPath(char *, size_t); +const char *GetServicesTxtPath(char *, size_t); +const char *GetProtocolsTxtPath(char *, size_t); +char *GetSystemDirectoryPath(char *, const char *, size_t); + +COSMOPOLITAN_C_END_ +#endif /* COSMOPOLITAN_LIBC_CALLS_SYSDIR_H_ */ diff --git a/libc/dns/BUILD.mk b/libc/dns/BUILD.mk deleted file mode 100644 index 57fcdf6ba..000000000 --- a/libc/dns/BUILD.mk +++ /dev/null @@ -1,59 +0,0 @@ -#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐ -#── vi: set noet ft=make ts=8 sw=8 fenc=utf-8 :vi ────────────────────┘ - -PKGS += LIBC_DNS - -LIBC_DNS_ARTIFACTS += LIBC_DNS_A -LIBC_DNS = $(LIBC_DNS_A_DEPS) $(LIBC_DNS_A) -LIBC_DNS_A = o/$(MODE)/libc/dns/dns.a -LIBC_DNS_A_FILES := $(wildcard libc/dns/*) -LIBC_DNS_A_HDRS = $(filter %.h,$(LIBC_DNS_A_FILES)) -LIBC_DNS_A_SRCS_C = $(filter %.c,$(LIBC_DNS_A_FILES)) -LIBC_DNS_A_SRCS_S = $(filter %.S,$(LIBC_DNS_A_FILES)) - -LIBC_DNS_A_SRCS = \ - $(LIBC_DNS_A_SRCS_S) \ - $(LIBC_DNS_A_SRCS_C) - -LIBC_DNS_A_OBJS = \ - $(LIBC_DNS_A_SRCS_S:%.S=o/$(MODE)/%.o) \ - $(LIBC_DNS_A_SRCS_C:%.c=o/$(MODE)/%.o) - -LIBC_DNS_A_CHECKS = \ - $(LIBC_DNS_A).pkg \ - $(LIBC_DNS_A_HDRS:%=o/$(MODE)/%.ok) - -LIBC_DNS_A_DIRECTDEPS = \ - LIBC_CALLS \ - LIBC_FMT \ - LIBC_INTRIN \ - LIBC_MEM \ - LIBC_NEXGEN32E \ - LIBC_NT_ADVAPI32 \ - LIBC_NT_KERNEL32 \ - LIBC_RUNTIME \ - LIBC_SOCK \ - LIBC_STDIO \ - LIBC_STR \ - LIBC_SYSV - -LIBC_DNS_A_DEPS := \ - $(call uniq,$(foreach x,$(LIBC_DNS_A_DIRECTDEPS),$($(x)))) - -$(LIBC_DNS_A): libc/dns/ \ - $(LIBC_DNS_A).pkg \ - $(LIBC_DNS_A_OBJS) - -$(LIBC_DNS_A).pkg: \ - $(LIBC_DNS_A_OBJS) \ - $(foreach x,$(LIBC_DNS_A_DIRECTDEPS),$($(x)_A).pkg) - -LIBC_DNS_LIBS = $(foreach x,$(LIBC_DNS_ARTIFACTS),$($(x))) -LIBC_DNS_SRCS = $(foreach x,$(LIBC_DNS_ARTIFACTS),$($(x)_SRCS)) -LIBC_DNS_HDRS = $(foreach x,$(LIBC_DNS_ARTIFACTS),$($(x)_HDRS)) -LIBC_DNS_CHECKS = $(foreach x,$(LIBC_DNS_ARTIFACTS),$($(x)_CHECKS)) -LIBC_DNS_OBJS = $(foreach x,$(LIBC_DNS_ARTIFACTS),$($(x)_OBJS)) -$(LIBC_DNS_OBJS): $(BUILD_FILES) libc/dns/BUILD.mk - -.PHONY: o/$(MODE)/libc/dns -o/$(MODE)/libc/dns: $(LIBC_DNS_CHECKS) diff --git a/libc/dns/comparednsnames.c b/libc/dns/comparednsnames.c deleted file mode 100644 index bc8a56a1c..000000000 --- a/libc/dns/comparednsnames.c +++ /dev/null @@ -1,77 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 Justine Alexandra Roberts Tunney │ -│ │ -│ Permission to use, copy, modify, and/or distribute this software for │ -│ any purpose with or without fee is hereby granted, provided that the │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/dns.h" -#include "libc/macros.internal.h" -#include "libc/str/str.h" - -forceinline void FindDnsLabel(const char *A, size_t *i, size_t *n) { - while (*i) { - if (A[*i - 1] == '.') { - if (*i == *n) { - --(*n); - } else { - break; - } - } - --(*i); - } -} - -/** - * Compares DNS hostnames in reverse lexicographical asciibetical order. - * @return <0, 0, or >0 - * @see test/libc/dns/comparednsnames_test.c (the code that matters) - */ -int CompareDnsNames(const char *A, const char *B) { - int res; - bool first; - size_t n, m, i, j; - if (A == B) return 0; - n = strlen(A); - m = strlen(B); - if (!n || !m || ((A[n - 1] == '.') ^ (B[m - 1] == '.'))) { - if (n && m && A[n - 1] == '.' && strchr(B, '.')) { - --m; - } else if (n && m && B[m - 1] == '.' && strchr(A, '.')) { - --n; - } else { - return A[n ? n - 1 : 0] - B[m ? m - 1 : 0]; - } - } - i = n; - j = m; - first = true; - for (;;) { - FindDnsLabel(A, &i, &n); - FindDnsLabel(B, &j, &m); - if (first) { - first = false; - if (!i && j) return 1; - if (!j && i) return -1; - } - if ((res = strncasecmp(&A[i], &B[j], MIN(n - i + 1, m - j + 1)))) { - return res; - } - if (!i || !j) { - return i - j; - } - n = i; - m = j; - } -} diff --git a/libc/dns/consts.h b/libc/dns/consts.h deleted file mode 100644 index 03136ac79..000000000 --- a/libc/dns/consts.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef COSMOPOLITAN_LIBC_DNS_CONSTS_H_ -#define COSMOPOLITAN_LIBC_DNS_CONSTS_H_ -#include "libc/sock/sock.h" - -#define DNS_TYPE_A 1 -#define DNS_TYPE_NS 2 -#define DNS_TYPE_CNAME 5 -#define DNS_TYPE_SOA 6 -#define DNS_TYPE_PTR 12 -#define DNS_TYPE_MX 15 -#define DNS_TYPE_TXT 16 - -#define DNS_CLASS_IN 1 - - -#define kMinSockaddr4Size \ - (offsetof(struct sockaddr_in, sin_addr) + sizeof(struct in_addr)) - -#endif /* COSMOPOLITAN_LIBC_DNS_CONSTS_H_ */ diff --git a/libc/dns/dns.h b/libc/dns/dns.h deleted file mode 100644 index 0332b3077..000000000 --- a/libc/dns/dns.h +++ /dev/null @@ -1,79 +0,0 @@ -#ifndef COSMOPOLITAN_LIBC_DNS_DNS_H_ -#define COSMOPOLITAN_LIBC_DNS_DNS_H_ -#include "libc/dns/resolvconf.h" -#include "libc/sock/struct/sockaddr.h" - -#define DNS_PORT 53 -#define DNS_NAME_MAX 253 -#define DNS_LABEL_MAX 63 - -#define EAI_SUCCESS 0 -#define EAI_BADFLAGS -1 -#define EAI_NONAME -2 -#define EAI_AGAIN -3 -#define EAI_FAIL -4 -#define EAI_NODATA -5 -#define EAI_FAMILY -6 -#define EAI_SOCKTYPE -7 -#define EAI_SERVICE -8 -#define EAI_ADDRFAMILY -9 -#define EAI_MEMORY -10 -#define EAI_OVERFLOW -12 -#define EAI_SYSTEM -11 -#define EAI_ALLDONE -103 -#define EAI_CANCELED -101 -#define EAI_IDN_ENCODE -105 -#define EAI_INPROGRESS -100 -#define EAI_INTR -104 -#define EAI_NOTCANCELED -102 - -/* AI_* conforms to NT ABI */ -#define AI_PASSIVE 1 -#define AI_CANONNAME 2 -#define AI_NUMERICHOST 4 -#define AI_NUMERICSERV 8 -#define AI_ALL 0x0100 -#define AI_ADDRCONFIG 0x0400 -#define AI_V4MAPPED 0x0800 - -#define NI_NUMERICSCOPE 0 -#define NI_NUMERICHOST 1 -#define NI_NUMERICSERV 2 -#define NI_NOFQDN 4 -#define NI_NAMEREQD 8 -#define NI_DGRAM 16 -#define NI_MAXSERV 32 -#define NI_MAXHOST 1025 - -COSMOPOLITAN_C_START_ - -struct addrinfo { - int32_t ai_flags; /* AI_XXX */ - int32_t ai_family; /* AF_INET */ - int32_t ai_socktype; /* SOCK_XXX */ - int32_t ai_protocol; /* IPPROTO_XXX */ - uint32_t ai_addrlen; - union { - struct sockaddr *ai_addr; - struct sockaddr_in *ai_addr4; - }; - char *ai_canonname /*[DNS_NAME_MAX + 1]*/; - struct addrinfo *ai_next; -}; - -int getaddrinfo(const char *, const char *, const struct addrinfo *, - struct addrinfo **); -void freeaddrinfo(struct addrinfo *); -int getnameinfo(const struct sockaddr *, uint32_t, char *, uint32_t, char *, - uint32_t, int); -const char *gai_strerror(int); -int CompareDnsNames(const char *, const char *) paramsnonnull(); -int PascalifyDnsName(uint8_t *, size_t, const char *) paramsnonnull(); -int ResolveDns(const struct ResolvConf *, int, const char *, struct sockaddr *, - uint32_t) paramsnonnull(); -int ResolveDnsReverse(const struct ResolvConf *, int, const char *, char *, - size_t) paramsnonnull(); -struct addrinfo *newaddrinfo(uint16_t); - -COSMOPOLITAN_C_END_ -#endif /* COSMOPOLITAN_LIBC_DNS_DNS_H_ */ diff --git a/libc/dns/dnsheader.c b/libc/dns/dnsheader.c deleted file mode 100644 index 906517504..000000000 --- a/libc/dns/dnsheader.c +++ /dev/null @@ -1,57 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 Justine Alexandra Roberts Tunney │ -│ │ -│ Permission to use, copy, modify, and/or distribute this software for │ -│ any purpose with or without fee is hereby granted, provided that the │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/dnsheader.h" -#include "libc/serialize.h" -#include "libc/sysv/errfuns.h" - -/** - * Serializes DNS message h to wire. - * - * @return number of bytes written (always 12) or -1 w/ errno - * @see pascalifydnsname() - */ -void SerializeDnsHeader(uint8_t p[restrict 12], const struct DnsHeader *h) { - p[0x0] = h->id >> 8; - p[0x1] = h->id; - p[0x2] = h->bf1; - p[0x3] = h->bf2; - p[0x4] = h->qdcount >> 8; - p[0x5] = h->qdcount; - p[0x6] = h->ancount >> 8; - p[0x7] = h->ancount; - p[0x8] = h->nscount >> 8; - p[0x9] = h->nscount; - p[0xa] = h->arcount >> 8; - p[0xb] = h->arcount; -} - -/** - * Serializes DNS message h to wire. - * - * @return number of bytes read (always 12) or -1 w/ errno - */ -void DeserializeDnsHeader(struct DnsHeader *h, const uint8_t p[restrict 12]) { - h->id = READ16BE(p); - h->bf1 = p[2]; - h->bf2 = p[3]; - h->qdcount = READ16BE(p + 4); - h->ancount = READ16BE(p + 6); - h->nscount = READ16BE(p + 8); - h->arcount = READ16BE(p + 10); -} diff --git a/libc/dns/dnsheader.h b/libc/dns/dnsheader.h deleted file mode 100644 index 15318b9b9..000000000 --- a/libc/dns/dnsheader.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef COSMOPOLITAN_LIBC_DNS_DNSHEADER_H_ -#define COSMOPOLITAN_LIBC_DNS_DNSHEADER_H_ -COSMOPOLITAN_C_START_ - -struct DnsHeader { - uint16_t id; /* transaction id */ - uint8_t bf1; /* bit field 1 */ - uint8_t bf2; /* bit field 2 */ - uint16_t qdcount; /* question count */ - uint16_t ancount; /* answer count */ - uint16_t nscount; /* nameserver count */ - uint16_t arcount; /* additional record count */ -}; - -void SerializeDnsHeader(uint8_t[restrict 12], const struct DnsHeader *); -void DeserializeDnsHeader(struct DnsHeader *, const uint8_t[restrict 12]); - -COSMOPOLITAN_C_END_ -#endif /* COSMOPOLITAN_LIBC_DNS_DNSHEADER_H_ */ diff --git a/libc/dns/dnsquestion.h b/libc/dns/dnsquestion.h deleted file mode 100644 index 0047747cb..000000000 --- a/libc/dns/dnsquestion.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef COSMOPOLITAN_LIBC_DNS_DNSQUESTION_H_ -#define COSMOPOLITAN_LIBC_DNS_DNSQUESTION_H_ -COSMOPOLITAN_C_START_ - -struct DnsQuestion { - const char *qname; - uint16_t qtype; - uint16_t qclass; -}; - -int SerializeDnsQuestion(uint8_t *, size_t, const struct DnsQuestion *); - -COSMOPOLITAN_C_END_ -#endif /* COSMOPOLITAN_LIBC_DNS_DNSQUESTION_H_ */ diff --git a/libc/dns/ent.h b/libc/dns/ent.h deleted file mode 100644 index 4902038ce..000000000 --- a/libc/dns/ent.h +++ /dev/null @@ -1,73 +0,0 @@ -#ifndef COSMOPOLITAN_LIBC_DNS_ENT_H_ -#define COSMOPOLITAN_LIBC_DNS_ENT_H_ -#include "libc/dns/dns.h" - -#define HOST_NOT_FOUND 1 -#define TRY_AGAIN 2 -#define NO_RECOVERY 3 -#define NO_DATA 4 -#define NO_ADDRESS NO_DATA - -COSMOPOLITAN_C_START_ - -struct netent { - char *n_name; /* official network name */ - char **n_aliases; /* alias list */ - int n_addrtype; /* net address type */ - uint32_t n_net; /* network number */ -}; - -struct protoent { - char *p_name; /* official protocol name */ - char **p_aliases; /* alias list */ - int p_proto; /* protocol number */ -}; - -struct hostent { - char *h_name; /* official name of host */ - char **h_aliases; /* alias list */ - int h_addrtype; /* host address type */ - int h_length; /* length of address */ - char **h_addr_list; /* list of addresses */ -}; -#define h_addr h_addr_list[0] - -struct servent { - char *s_name; /* official service name */ - char **s_aliases; /* alias list */ - int s_port; /* port number (in network byte order) */ - char *s_proto; /* protocol to use */ -}; - -#define h_errno (*__h_errno_location()) -errno_t *__h_errno_location(void) dontthrow pureconst; - -void herror(const char *); -const char *hstrerror(int); - -struct netent *getnetent(void); -struct netent *getnetbyname(const char *); -struct netent *getnetbyaddr(uint32_t, int); -void setnetent(int); -void endnetent(void); - -struct protoent *getprotoent(void); -struct protoent *getprotobyname(const char *); -struct protoent *getprotobynumber(int); -void setprotoent(int); -void endprotoent(void); - -struct hostent *gethostent(void); -struct hostent *gethostbyname(const char *); -struct hostent *gethostbyaddr(const void *, uint32_t, int); -void sethostent(int); -void endhostent(void); - -struct servent *getservent(void); -struct servent *getservbyname(const char *, const char *); -struct servent *getservbyport(int, const char *); -void setservent(int); -void endservent(void); - -COSMOPOLITAN_C_END_ -#endif /* COSMOPOLITAN_LIBC_DNS_ENT_H_ */ diff --git a/libc/dns/gai_strerror.c b/libc/dns/gai_strerror.c deleted file mode 100644 index ef1ced002..000000000 --- a/libc/dns/gai_strerror.c +++ /dev/null @@ -1,67 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 Justine Alexandra Roberts Tunney │ -│ │ -│ Permission to use, copy, modify, and/or distribute this software for │ -│ any purpose with or without fee is hereby granted, provided that the │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/dns.h" - -/** - * Turns getaddrinfo() return code into string. - */ -const char *gai_strerror(int code) { - switch (code) { - case EAI_ADDRFAMILY: - return "ADDRFAMILY"; - case EAI_AGAIN: - return "AGAIN"; - case EAI_ALLDONE: - return "ALLDONE"; - case EAI_BADFLAGS: - return "BADFLAGS"; - case EAI_CANCELED: - return "CANCELED"; - case EAI_FAIL: - return "FAIL"; - case EAI_FAMILY: - return "FAMILY"; - case EAI_IDN_ENCODE: - return "ENCODE"; - case EAI_INPROGRESS: - return "INPROGRESS"; - case EAI_INTR: - return "INTR"; - case EAI_MEMORY: - return "MEMORY"; - case EAI_NODATA: - return "NODATA"; - case EAI_NONAME: - return "NONAME"; - case EAI_NOTCANCELED: - return "NOTCANCELED"; - case EAI_OVERFLOW: - return "OVERFLOW"; - case EAI_SERVICE: - return "SERVICE"; - case EAI_SOCKTYPE: - return "SOCKTYPE"; - case EAI_SUCCESS: - return "SUCCESS"; - case EAI_SYSTEM: - return "SYSTEM"; - default: - return "???"; - } -} diff --git a/libc/dns/getaddrinfo.c b/libc/dns/getaddrinfo.c deleted file mode 100644 index c59c1ce51..000000000 --- a/libc/dns/getaddrinfo.c +++ /dev/null @@ -1,116 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 Justine Alexandra Roberts Tunney │ -│ │ -│ Permission to use, copy, modify, and/or distribute this software for │ -│ any purpose with or without fee is hereby granted, provided that the │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/calls/calls.h" -#include "libc/dns/dns.h" -#include "libc/dns/hoststxt.h" -#include "libc/dns/resolvconf.h" -#include "libc/dns/servicestxt.h" -#include "libc/fmt/conv.h" -#include "libc/macros.internal.h" -#include "libc/mem/gc.h" -#include "libc/mem/mem.h" -#include "libc/sock/sock.h" -#include "libc/str/str.h" -#include "libc/sysv/consts/af.h" -#include "libc/sysv/consts/inaddr.h" -#include "libc/sysv/consts/sock.h" -#include "libc/sysv/errfuns.h" - -/** - * Resolves address for internet name. - * - * @param name is either an ip string or a utf-8 hostname - * @param service is the port number as a string - * @param hints may be passed to specialize behavior (optional) - * @param res receives a pointer that must be freed with freeaddrinfo(), - * and won't be modified if non-zero is returned - * @return 0 on success or EAI_xxx value - */ -int getaddrinfo(const char *name, const char *service, - const struct addrinfo *hints, struct addrinfo **res) { - char *eptr; - int rc, port; - char proto[32]; - const char *canon; - struct addrinfo *ai; - port = 0; - if (!name && !service) { - return EAI_NONAME; - } - if (!name && hints && (hints->ai_flags & AI_CANONNAME)) { - return EAI_BADFLAGS; - } - if (service && ((port = strtol(service, &eptr, 10)), *eptr)) { - if (hints && hints->ai_socktype == SOCK_STREAM) { - strcpy(proto, "tcp"); - } else if (hints && hints->ai_socktype == SOCK_DGRAM) { - strcpy(proto, "udp"); - } else { // ai_socktype == 0 - strcpy(proto, ""); - } - if ((port = LookupServicesByName(service, proto, sizeof(proto), NULL, 0, - NULL)) == -1) { - return EAI_NONAME; - } - } - if (!(ai = newaddrinfo(port))) { - return EAI_MEMORY; - } - if (service) { - // if service isn't specified, port is left uninitialized - ai->ai_addr4->sin_port = htons(port); - } - if (hints) { - ai->ai_socktype = hints->ai_socktype; - ai->ai_protocol = hints->ai_protocol; - } - if (!name) { - ai->ai_addr4->sin_addr.s_addr = - (hints && (hints->ai_flags & AI_PASSIVE) == AI_PASSIVE) - ? htonl(INADDR_ANY) - : htonl(INADDR_LOOPBACK); - *res = ai; - return 0; - } - if (inet_pton(AF_INET, name, &ai->ai_addr4->sin_addr.s_addr) == 1) { - *res = ai; - return 0; - } else if (hints && (hints->ai_flags & AI_NUMERICHOST) == AI_NUMERICHOST) { - freeaddrinfo(ai); - return EAI_NONAME; - } else if (ResolveHostsTxt(GetHostsTxt(), AF_INET, name, ai->ai_addr, - sizeof(ai->ai_addr4), &canon) > 0) { - strlcpy(ai->ai_canonname, canon, DNS_NAME_MAX + 1); - *res = ai; - return 0; - } else { - rc = ResolveDns(GetResolvConf(), AF_INET, name, ai->ai_addr, - sizeof(ai->ai_addr4)); - if (rc > 0) { - *res = ai; - return 0; - } - freeaddrinfo(ai); - if (rc == 0) { - return EAI_NONAME; - } else { - return EAI_SYSTEM; - } - } -} diff --git a/libc/dns/gethostbyaddr.c b/libc/dns/gethostbyaddr.c deleted file mode 100644 index f250de7ba..000000000 --- a/libc/dns/gethostbyaddr.c +++ /dev/null @@ -1,58 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/ent.h" -#include "libc/mem/mem.h" -#include "libc/sysv/consts/af.h" - -struct hostent *gethostbyaddr(const void *s_addr, uint32_t len, int type) { - static struct hostent *ptr1, he1; - static char h_name[DNS_NAME_MAX + 1]; - static char *h_aliases[1]; - static char *h_addr_list[2]; - static char h_addr_list0[4]; - struct sockaddr_in addr; - if (!ptr1) { - he1.h_name = h_name; - he1.h_aliases = h_aliases; - he1.h_aliases[0] = NULL; - he1.h_addrtype = AF_INET; - he1.h_length = 4; - he1.h_addr_list = h_addr_list; - he1.h_addr_list[0] = h_addr_list0; - he1.h_addr_list[1] = NULL; - ptr1 = &he1; - } - if (type != AF_INET || len != sizeof(uint32_t)) return NULL; - addr.sin_family = AF_INET; - addr.sin_port = 0; - addr.sin_addr.s_addr = *(uint32_t *)(s_addr); - if (getnameinfo((struct sockaddr *)&addr, sizeof(addr), ptr1->h_name, - DNS_NAME_MAX, NULL, 0, 0)) - return NULL; - *((uint32_t *)ptr1->h_addr_list[0]) = (addr.sin_addr.s_addr); - return ptr1; -} diff --git a/libc/dns/gethostbyname.c b/libc/dns/gethostbyname.c deleted file mode 100644 index f30d87af9..000000000 --- a/libc/dns/gethostbyname.c +++ /dev/null @@ -1,70 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/ent.h" -#include "libc/mem/mem.h" -#include "libc/str/str.h" -#include "libc/sysv/consts/af.h" - -struct hostent *gethostbyname(const char *name) { - static struct hostent *ptr0, he0; - static char h_name[DNS_NAME_MAX + 1]; - static char *h_aliases[1]; - static char *h_addr_list[2]; - static char h_addr_list0[4]; - struct addrinfo *result = NULL; - - if (!ptr0) { - he0.h_name = h_name; - - he0.h_aliases = h_aliases; - he0.h_aliases[0] = NULL; - - he0.h_addrtype = AF_INET; - he0.h_length = 4; - he0.h_addr_list = h_addr_list; - - he0.h_addr_list[0] = h_addr_list0; - he0.h_addr_list[1] = NULL; - - ptr0 = &he0; - } - - if (getaddrinfo(name, NULL, NULL, &result) || result == NULL) { - return NULL; - } - - /* if getaddrinfo is successful, result->ai_canonname is non-NULL, - * (see newaddrinfo) but the string can still be empty */ - strlcpy(ptr0->h_name, *result->ai_canonname ? result->ai_canonname : name, - sizeof(h_name)); - - *((uint32_t *)ptr0->h_addr_list[0]) = result->ai_addr4->sin_addr.s_addr; - /* TODO: if result has ai_next, fit multiple entries for h_addr_list */ - - freeaddrinfo(result); - return ptr0; -} diff --git a/libc/dns/gethoststxt.c b/libc/dns/gethoststxt.c deleted file mode 100644 index eeca919b1..000000000 --- a/libc/dns/gethoststxt.c +++ /dev/null @@ -1,94 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 Justine Alexandra Roberts Tunney │ -│ │ -│ Permission to use, copy, modify, and/or distribute this software for │ -│ any purpose with or without fee is hereby granted, provided that the │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/calls/calls.h" -#include "libc/cxxabi.h" -#include "libc/dce.h" -#include "libc/dns/hoststxt.h" -#include "libc/dns/servicestxt.h" -#include "libc/intrin/pushpop.internal.h" -#include "libc/intrin/safemacros.internal.h" -#include "libc/macros.internal.h" -#include "libc/mem/mem.h" -#include "libc/runtime/runtime.h" -#include "libc/stdio/stdio.h" -#include "libc/str/str.h" -#include "libc/thread/thread.h" - -static struct HostsTxt *g_hoststxt; -static struct HostsTxtInitialStaticMemory { - struct HostsTxt ht; - pthread_mutex_t lock; - struct HostsTxtEntry entries[8]; - char strings[64]; -} g_hoststxt_init; - -static const char *GetHostsTxtPath(char *path, size_t size) { - if (!IsWindows()) { - return "/etc/hosts"; - } else { - return GetSystemDirectoryPath(path, size, "drivers\\etc\\hosts"); - } -} - -/** - * Returns hosts.txt map. - * - * @note yoinking realloc() ensures there's no size limits - */ -const struct HostsTxt *GetHostsTxt(void) { - FILE *f; - char pathbuf[256]; - struct HostsTxtInitialStaticMemory *init; - init = &g_hoststxt_init; - pthread_mutex_lock(&init->lock); - if (!g_hoststxt) { - g_hoststxt = &init->ht; - init->ht.entries.n = pushpop(ARRAYLEN(init->entries)); - init->ht.entries.p = init->entries; - init->ht.strings.n = pushpop(ARRAYLEN(init->strings)); - init->ht.strings.p = init->strings; - __cxa_atexit((void *)FreeHostsTxt, &g_hoststxt, NULL); - if ((f = fopen(GetHostsTxtPath(pathbuf, sizeof(pathbuf)), "r"))) { - if (ParseHostsTxt(g_hoststxt, f) == -1) { - /* TODO(jart): Elevate robustness. */ - } - fclose(f); - } - } - pthread_mutex_unlock(&init->lock); - return g_hoststxt; -} - -/** - * Frees HOSTS.TXT data structure populated by ParseHostsTxt(). - */ -void FreeHostsTxt(struct HostsTxt **ht) { - if (*ht) { - if ((*ht)->entries.p != g_hoststxt_init.entries) { - free((*ht)->entries.p); - } - if ((*ht)->strings.p != g_hoststxt_init.strings) { - free((*ht)->strings.p); - } - if (*ht != &g_hoststxt_init.ht) { - free(*ht); - } - *ht = 0; - } -} diff --git a/libc/dns/getnameinfo.c b/libc/dns/getnameinfo.c deleted file mode 100644 index 7b8324efc..000000000 --- a/libc/dns/getnameinfo.c +++ /dev/null @@ -1,115 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/calls/calls.h" -#include "libc/dns/consts.h" -#include "libc/dns/dns.h" -#include "libc/dns/hoststxt.h" -#include "libc/dns/resolvconf.h" -#include "libc/dns/servicestxt.h" -#include "libc/fmt/conv.h" -#include "libc/fmt/itoa.h" -#include "libc/intrin/safemacros.internal.h" -#include "libc/mem/mem.h" -#include "libc/str/str.h" -#include "libc/sysv/consts/af.h" -#include "libc/sysv/consts/inaddr.h" -#include "libc/sysv/errfuns.h" - -/** - * Resolves name/service for socket address. - * - * @param addr - * @param addrlen - * @param name - * @param namelen - * @param service - * @param servicelen - * @param flags - * - * @return 0 on success or EAI_xxx value - */ -int getnameinfo(const struct sockaddr *addr, uint32_t addrlen, char *name, - uint32_t namelen, char *service, uint32_t servicelen, - int flags) { - char *p, rdomain[1 + sizeof "255.255.255.255.in-addr.arpa"]; - char info[NI_MAXHOST + 1]; - int port; - uint8_t *ip; - unsigned int valid_flags; - - valid_flags = - (NI_NAMEREQD | NI_NUMERICHOST | NI_NUMERICSERV | NI_NOFQDN | NI_DGRAM); - - if (flags & ~(valid_flags)) return EAI_BADFLAGS; - if (!name && !service) return EAI_NONAME; - if (addr->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in)) - return EAI_FAMILY; - - ip = (uint8_t *)&(((struct sockaddr_in *)addr)->sin_addr); - p = rdomain; - p = FormatUint32(p, ip[3]), *p++ = '.'; - p = FormatUint32(p, ip[2]), *p++ = '.'; - p = FormatUint32(p, ip[1]), *p++ = '.'; - p = FormatUint32(p, ip[0]), stpcpy(p, ".in-addr.arpa"); - info[0] = '\0'; - if (name != NULL && namelen != 0) { - if ((flags & NI_NUMERICHOST) && (flags & NI_NAMEREQD)) return EAI_NONAME; - - if ((flags & NI_NUMERICHOST) && - inet_ntop(AF_INET, ip, info, sizeof(info)) == NULL) - return EAI_SYSTEM; - else if (!info[0] && ResolveHostsReverse(GetHostsTxt(), AF_INET, ip, info, - sizeof(info)) < 0) - return EAI_SYSTEM; - else if (!info[0] && ResolveDnsReverse(GetResolvConf(), AF_INET, rdomain, - info, sizeof(info)) < 0) - return EAI_SYSTEM; - else if (!info[0] && (flags & NI_NAMEREQD)) - return EAI_NONAME; - else if (!info[0] && inet_ntop(AF_INET, ip, info, sizeof(info)) == NULL) - return EAI_SYSTEM; - - if (strlen(info) + 1 > namelen) return EAI_OVERFLOW; - strcpy(name, info); - } - - port = ntohs(((struct sockaddr_in *)addr)->sin_port); - info[0] = '\0'; - if (service != NULL && servicelen != 0) { - if ((flags & NI_NUMERICSERV) || - LookupServicesByPort(port, ((flags & NI_DGRAM) ? "udp" : "tcp"), 4, - info, sizeof(info), NULL) == -1) { - FormatInt32(info, port); - } - if (strlen(info) + 1 > servicelen) { - return EAI_OVERFLOW; - } - strcpy(service, info); - } - - return 0; -} diff --git a/libc/dns/getntnameservers.c b/libc/dns/getntnameservers.c deleted file mode 100644 index ebf3827c6..000000000 --- a/libc/dns/getntnameservers.c +++ /dev/null @@ -1,93 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 Justine Alexandra Roberts Tunney │ -│ │ -│ Permission to use, copy, modify, and/or distribute this software for │ -│ any purpose with or without fee is hereby granted, provided that the │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/calls/calls.h" -#include "libc/calls/syscall_support-nt.internal.h" -#include "libc/dns/dns.h" -#include "libc/dns/resolvconf.h" -#include "libc/mem/arraylist.internal.h" -#include "libc/nt/enum/keyaccess.h" -#include "libc/nt/enum/reggetvalueflags.h" -#include "libc/nt/registry.h" -#include "libc/nt/runtime.h" -#include "libc/runtime/runtime.h" -#include "libc/sock/sock.h" -#include "libc/str/str.h" -#include "libc/sysv/consts/af.h" - -/** - * Extracts DNS nameserver IPs from Windows Registry. - * - * @param resolv points to a ResolvConf object, which should be zero - * initialized by the caller; or if it already contains items, - * this function will append - * @return number of nameservers appended, or -1 w/ errno - */ -textwindows int GetNtNameServers(struct ResolvConf *resolv) { - int rc; - char value8[128]; - int64_t hkInterfaces; - char *state, *addr, *tmp; - struct sockaddr_in nameserver; - char16_t value[128], uuid[64]; - uint32_t i, keycount, valuebytes, uuidlen; - keycount = 0; - hkInterfaces = kNtInvalidHandleValue; - if (!RegOpenKeyEx( - kNtHkeyLocalMachine, - u"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces", - 0, kNtKeyRead, &hkInterfaces) && - !RegQueryInfoKey(hkInterfaces, 0, 0, 0, &keycount, 0, 0, 0, 0, 0, 0, 0)) { - nameserver.sin_family = AF_INET; - nameserver.sin_port = htons(DNS_PORT); - rc = 0; - for (i = 0; i < keycount; ++i) { - uuidlen = sizeof(uuid); - if (!RegEnumKeyEx(hkInterfaces, i, uuid, &uuidlen, 0, 0, 0, 0) && - ((!RegGetValue(hkInterfaces, uuid, u"DhcpIpAddress", - kNtRrfRtRegSz | kNtRrfRtRegMultiSz, NULL, value, - ((valuebytes = sizeof(value)), &valuebytes)) && - valuebytes > 2 * sizeof(char16_t)) || - (!RegGetValue(hkInterfaces, uuid, u"IpAddress", - kNtRrfRtRegSz | kNtRrfRtRegMultiSz, NULL, value, - ((valuebytes = sizeof(value)), &valuebytes)) && - valuebytes > 2 * sizeof(char16_t))) && - ((!RegGetValue(hkInterfaces, uuid, u"DhcpNameServer", - kNtRrfRtRegSz | kNtRrfRtRegMultiSz, NULL, value, - ((valuebytes = sizeof(value)), &valuebytes)) && - valuebytes > 2 * sizeof(char16_t)) || - (!RegGetValue(hkInterfaces, uuid, u"NameServer", - kNtRrfRtRegSz | kNtRrfRtRegMultiSz, NULL, value, - ((valuebytes = sizeof(value)), &valuebytes)) && - valuebytes > 2 * sizeof(char16_t)))) { - tprecode16to8(value8, sizeof(value8), value); - tmp = value8; - while ((addr = strtok_r(tmp, ", ", &state))) { - if (inet_pton(AF_INET, addr, &nameserver.sin_addr.s_addr) == 1) { - if (append(&resolv->nameservers, &nameserver) != -1) ++rc; - } - tmp = NULL; - } - } - } - } else { - rc = __winerr(); - } - RegCloseKey(hkInterfaces); - return rc; -} diff --git a/libc/dns/getntprotocolstxtpath.c b/libc/dns/getntprotocolstxtpath.c deleted file mode 100644 index 880e4b258..000000000 --- a/libc/dns/getntprotocolstxtpath.c +++ /dev/null @@ -1,37 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dce.h" -#include "libc/dns/prototxt.h" -#include "libc/dns/servicestxt.h" - -const char *GetProtocolsTxtPath(char *buf, size_t size) { - if (!IsWindows()) { - return "/etc/protocols"; - } else { - return GetSystemDirectoryPath(buf, size, "drivers\\etc\\protocol"); - } -} diff --git a/libc/dns/getntservicestxtpath.c b/libc/dns/getntservicestxtpath.c deleted file mode 100644 index f952dfa0d..000000000 --- a/libc/dns/getntservicestxtpath.c +++ /dev/null @@ -1,36 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dce.h" -#include "libc/dns/servicestxt.h" - -const char *GetServicesTxtPath(char *path, size_t size) { - if (!IsWindows()) { - return "/etc/services"; - } else { - return GetSystemDirectoryPath(path, size, "drivers\\etc\\services"); - } -} diff --git a/libc/dns/getprotobyname.c b/libc/dns/getprotobyname.c deleted file mode 100644 index 2735cf2b6..000000000 --- a/libc/dns/getprotobyname.c +++ /dev/null @@ -1,46 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/ent.h" -#include "libc/dns/prototxt.h" -#include "libc/mem/mem.h" - -struct protoent *getprotobyname(const char *name) { - static struct protoent *ptr0, pe0; - static char p_name[DNS_NAME_MAX + 1]; - - if (!ptr0) { - pe0.p_name = p_name; - if (!(pe0.p_aliases = calloc(1, sizeof(char *)))) return NULL; - pe0.p_proto = -1; - ptr0 = &pe0; - } - - ptr0->p_proto = LookupProtoByName(name, ptr0->p_name, DNS_NAME_MAX, NULL); - if (ptr0->p_proto == -1) return NULL; - - return ptr0; -} diff --git a/libc/dns/getprotobynumber.c b/libc/dns/getprotobynumber.c deleted file mode 100644 index 23f2ea00b..000000000 --- a/libc/dns/getprotobynumber.c +++ /dev/null @@ -1,47 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/ent.h" -#include "libc/dns/prototxt.h" -#include "libc/mem/mem.h" - -struct protoent *getprotobynumber(int proto) { - static struct protoent *ptr1, pe1; - static char p_name[DNS_NAME_MAX + 1]; - - if (!ptr1) { - pe1.p_name = p_name; - if (!(pe1.p_aliases = calloc(1, sizeof(char *)))) return NULL; - pe1.p_proto = -1; - ptr1 = &pe1; - } - - if (LookupProtoByNumber(proto, ptr1->p_name, DNS_NAME_MAX, NULL) == -1) - return NULL; - - ptr1->p_proto = proto; - return ptr1; -} diff --git a/libc/dns/getresolvconf.c b/libc/dns/getresolvconf.c deleted file mode 100644 index a2d75a856..000000000 --- a/libc/dns/getresolvconf.c +++ /dev/null @@ -1,83 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 Justine Alexandra Roberts Tunney │ -│ │ -│ Permission to use, copy, modify, and/or distribute this software for │ -│ any purpose with or without fee is hereby granted, provided that the │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/cxxabi.h" -#include "libc/dce.h" -#include "libc/dns/resolvconf.h" -#include "libc/intrin/pushpop.internal.h" -#include "libc/macros.internal.h" -#include "libc/mem/mem.h" -#include "libc/runtime/runtime.h" -#include "libc/sock/sock.h" -#include "libc/sock/struct/sockaddr.h" -#include "libc/stdio/stdio.h" -#include "libc/thread/thread.h" - -static struct ResolvConf *g_resolvconf; -static struct ResolvConfInitialStaticMemory { - struct ResolvConf rv; - pthread_mutex_t lock; - struct sockaddr_in nameservers[3]; -} g_resolvconf_init; - -/** - * Returns singleton with DNS server address. - */ -const struct ResolvConf *GetResolvConf(void) { - int rc; - FILE *f; - struct ResolvConfInitialStaticMemory *init; - init = &g_resolvconf_init; - pthread_mutex_lock(&init->lock); - if (!g_resolvconf) { - g_resolvconf = &init->rv; - pushmov(&init->rv.nameservers.n, ARRAYLEN(init->nameservers)); - init->rv.nameservers.p = init->nameservers; - __cxa_atexit((void *)FreeResolvConf, &g_resolvconf, NULL); - if (!IsWindows()) { - if ((f = fopen("/etc/resolv.conf", "r"))) { - rc = ParseResolvConf(g_resolvconf, f); - } else { - rc = -1; - } - fclose(f); - } else { - rc = GetNtNameServers(g_resolvconf); - } - if (rc == -1 && !IsTiny()) { - /* TODO(jart): Elevate robustness. */ - } - } - pthread_mutex_unlock(&init->lock); - return g_resolvconf; -} - -/** - * Frees resolv.conf data structure populated by ParseResolvConf(). - */ -void FreeResolvConf(struct ResolvConf **rvp) { - if (*rvp) { - if ((*rvp)->nameservers.p != g_resolvconf_init.nameservers) { - free((*rvp)->nameservers.p); - } - if (*rvp != &g_resolvconf_init.rv) { - free(*rvp); - } - *rvp = 0; - } -} diff --git a/libc/dns/getservbyname.c b/libc/dns/getservbyname.c deleted file mode 100644 index 02a5cf9fd..000000000 --- a/libc/dns/getservbyname.c +++ /dev/null @@ -1,55 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/ent.h" -#include "libc/dns/servicestxt.h" -#include "libc/mem/mem.h" -#include "libc/sock/sock.h" -#include "libc/str/str.h" - -struct servent *getservbyname(const char *name, const char *proto) { - static struct servent *ptr0, se0; - static char s_name[DNS_NAME_MAX + 1]; - static char localproto[DNS_NAME_MAX + 1]; - int p; - if (!ptr0) { - se0.s_name = s_name; - if (!(se0.s_aliases = calloc(1, sizeof(char *)))) return NULL; - se0.s_port = 0; - se0.s_proto = localproto; - ptr0 = &se0; - } - if (proto) { - strlcpy(localproto, proto, sizeof(localproto)); - } else { - *localproto = 0; - } - p = LookupServicesByName(name, ptr0->s_proto, DNS_NAME_MAX, ptr0->s_name, - DNS_NAME_MAX, NULL); - if (p == -1) return NULL; - ptr0->s_port = htons(p); - return ptr0; -} diff --git a/libc/dns/getservbyport.c b/libc/dns/getservbyport.c deleted file mode 100644 index ce0189060..000000000 --- a/libc/dns/getservbyport.c +++ /dev/null @@ -1,56 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/ent.h" -#include "libc/dns/servicestxt.h" -#include "libc/mem/mem.h" -#include "libc/str/str.h" - -struct servent *getservbyport(int port, const char *proto) { - static struct servent *ptr1, se1; - static char s_name[DNS_NAME_MAX + 1]; - static char localproto[DNS_NAME_MAX + 1]; - if (!ptr1) { - se1.s_name = s_name; - if (!(se1.s_aliases = calloc(1, sizeof(char *)))) return NULL; - se1.s_port = 0; - se1.s_proto = localproto; - ptr1 = &se1; - } - if (proto) { - if (!memccpy(localproto, proto, '\0', DNS_NAME_MAX)) { - return NULL; - } - } else { - *localproto = 0; - } - if (LookupServicesByPort(ntohs(port), ptr1->s_proto, DNS_NAME_MAX, - ptr1->s_name, DNS_NAME_MAX, NULL) == -1) { - return NULL; - } - ptr1->s_port = port; - return ptr1; -} diff --git a/libc/dns/herror.c b/libc/dns/herror.c deleted file mode 100644 index 5977b4fe2..000000000 --- a/libc/dns/herror.c +++ /dev/null @@ -1,36 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/ent.h" -#include "libc/stdio/stdio.h" - -/** - * Prints `h_errno` description to stderr. - * @see perror() - */ -void herror(const char *s) { - fprintf(stderr, "%s%s%s\n", s ? s : "", s ? ": " : "", hstrerror(h_errno)); -} diff --git a/libc/dns/hostent.c b/libc/dns/hostent.c deleted file mode 100644 index 54b86d0a4..000000000 --- a/libc/dns/hostent.c +++ /dev/null @@ -1,37 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/ent.h" - -struct hostent *gethostent(void) { - return NULL; -} - -void sethostent(int stayopen) { -} - -void endhostent(void) { -} diff --git a/libc/dns/hoststxt.h b/libc/dns/hoststxt.h deleted file mode 100644 index f413fea51..000000000 --- a/libc/dns/hoststxt.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef COSMOPOLITAN_LIBC_DNS_HOSTSTXT_H_ -#define COSMOPOLITAN_LIBC_DNS_HOSTSTXT_H_ -#include "libc/sock/struct/sockaddr.h" -#include "libc/stdio/stdio.h" -COSMOPOLITAN_C_START_ - -struct HostsTxtEntry { - uint8_t ip[4]; /* inet_ntop(AF_INET, he->ip, buf, size) */ - uint32_t name; /* &ht->strings.p[he->name] */ - uint32_t canon; /* &ht->strings.p[he->canon] */ -}; - -struct HostsTxtEntries { - size_t i, n; - struct HostsTxtEntry *p; -}; - -struct HostsTxtStrings { - size_t i, n; - char *p; -}; - -struct HostsTxt { - struct HostsTxtEntries entries; - struct HostsTxtStrings strings; -}; - -const struct HostsTxt *GetHostsTxt(void) returnsnonnull; -void FreeHostsTxt(struct HostsTxt **) paramsnonnull(); -int ParseHostsTxt(struct HostsTxt *, FILE *) paramsnonnull(); -int ResolveHostsTxt(const struct HostsTxt *, int, const char *, - struct sockaddr *, uint32_t, const char **) - paramsnonnull((1, 3)); -int ResolveHostsReverse(const struct HostsTxt *, int, const uint8_t *, char *, - size_t) paramsnonnull((1, 3)); - -COSMOPOLITAN_C_END_ -#endif /* COSMOPOLITAN_LIBC_DNS_HOSTSTXT_H_ */ diff --git a/libc/dns/lookupprotobyname.c b/libc/dns/lookupprotobyname.c deleted file mode 100644 index 281b46004..000000000 --- a/libc/dns/lookupprotobyname.c +++ /dev/null @@ -1,101 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dce.h" -#include "libc/dns/prototxt.h" -#include "libc/errno.h" -#include "libc/fmt/conv.h" -#include "libc/intrin/safemacros.internal.h" -#include "libc/macros.internal.h" -#include "libc/mem/mem.h" -#include "libc/str/str.h" - -/** - * Opens and searches /etc/protocols to find number for a given name. - * - * @param protoname is a NULL-terminated string - * @param buf is a buffer to store the official name of the protocol - * @param bufsize is the size of buf - * @param filepath is the location of protocols file - * (if NULL, uses /etc/protocols) - * @return -1 on error, or - * positive protocol number - * - * @note aliases are read from file for comparison, but not returned. - * @see LookupProtoByNumber - */ -int LookupProtoByName(const char *protoname, char *buf, size_t bufsize, - const char *filepath) { - FILE *f; - char *line; - size_t linesize; - const char *path; - int found, result; - char pathbuf[256]; - char *name, *number, *alias, *comment, *tok; - if (!(path = filepath)) { - path = - !IsWindows() - ? "/etc/protocols" - : firstnonnull(GetProtocolsTxtPath(pathbuf, sizeof(pathbuf)), path); - } - if (bufsize == 0 || !(f = fopen(path, "r"))) { - return -1; - } - line = NULL; - linesize = 0; - found = 0; - result = -1; - while (found == 0 && getline(&line, &linesize, f) != -1) { - if ((comment = strchr(line, '#'))) *comment = '\0'; - name = strtok_r(line, " \t\r\n\v", &tok); - number = strtok_r(NULL, "/ \t\r\n\v", &tok); - if (name && number) { - alias = name; - while (alias && strcasecmp(alias, protoname)) { - alias = strtok_r(NULL, " \t\r\n\v", &tok); - } - if (alias) { /* alias matched with protoname */ - if (!memccpy(buf, name, '\0', bufsize)) { - if (bufsize) { - *buf = 0; - } - break; - } - result = atoi(number); - found = 1; - } - } - } - free(line); - if (ferror(f)) { - errno = ferror(f); - return -1; - } - fclose(f); - if (!found) return -1; - return result; -} diff --git a/libc/dns/lookupprotobynumber.c b/libc/dns/lookupprotobynumber.c deleted file mode 100644 index 7aac82ab3..000000000 --- a/libc/dns/lookupprotobynumber.c +++ /dev/null @@ -1,90 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dce.h" -#include "libc/dns/prototxt.h" -#include "libc/errno.h" -#include "libc/fmt/conv.h" -#include "libc/macros.internal.h" -#include "libc/mem/mem.h" -#include "libc/str/str.h" - -/** - * Opens and searches /etc/protocols to find name for a given number. - * - * The format of /etc/protocols is like this: - * - * # comment - * # NAME PROTOCOL ALIASES - * ip 0 IP - * icmp 1 ICMP - * - * @param protonum is the protocol number - * @param buf is a buffer to store the official name of the protocol - * @param bufsize is the size of buf - * @param path is the location of the protocols file, which may be NULL - * to use the system-wide default - * @return 0 on success, -1 on error - * @note aliases are not read from the file. - */ -int LookupProtoByNumber(const int protonum, char *buf, size_t bufsize, - const char *path) { - FILE *f; - int found; - char *line; - size_t linesize; - char pathbuf[256]; - char *name, *number, *comment, *tok; - if (!bufsize || - !(f = fopen(path ? path : GetProtocolsTxtPath(pathbuf, sizeof(pathbuf)), - "r"))) { - return -1; - } - line = NULL; - linesize = 0; - found = 0; - while (found == 0 && (getline(&line, &linesize, f)) != -1) { - if ((comment = strchr(line, '#'))) *comment = '\0'; - name = strtok_r(line, " \t\r\n\v", &tok); - number = strtok_r(NULL, " \t\r\n\v", &tok); - if (name && number && protonum == atoi(number)) { - if (!memccpy(buf, name, '\0', bufsize)) { - strcpy(buf, ""); - break; - } - found = 1; - } - } - free(line); - if (ferror(f)) { - errno = ferror(f); - fclose(f); - return -1; - } - fclose(f); - if (!found) return -1; - return 0; -} diff --git a/libc/dns/lookupservicesbyname.c b/libc/dns/lookupservicesbyname.c deleted file mode 100644 index d41ea3fcd..000000000 --- a/libc/dns/lookupservicesbyname.c +++ /dev/null @@ -1,107 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dce.h" -#include "libc/dns/servicestxt.h" -#include "libc/errno.h" -#include "libc/fmt/conv.h" -#include "libc/intrin/safemacros.internal.h" -#include "libc/macros.internal.h" -#include "libc/mem/mem.h" -#include "libc/str/str.h" - -/** - * Opens and searches /etc/services to find port for a given name. - * - * @param servname is a NULL-terminated string - * @param servproto is a NULL-terminated string (eg "tcp", "udp") - * (if servproto is an empty string, - * if is filled with the first matching - * protocol) - * @param servprotolen the size of servproto - * @param buf is a buffer to store the official name of the service - * (if NULL, the official name is not stored) - * @param bufsize is the size of buf - * @param path is the location of services file - * (if NULL, uses /etc/services) - * @return -1 on error, or positive port number - * @note aliases are read from file for comparison, but not returned. - * @see LookupServicesByPort - */ -int LookupServicesByName(const char *servname, char *servproto, - size_t servprotolen, char *buf, size_t bufsize, - const char *path) { - FILE *f; - char *line; - size_t linesize; - char pathbuf[256]; - int found, result; - char *name, *port, *proto, *alias, *comment, *tok; - if (servprotolen == 0 || - !(f = fopen(path ? path : GetServicesTxtPath(pathbuf, sizeof(pathbuf)), - "r"))) { - return -1; - } - line = NULL; - linesize = 0; - found = 0; - result = -1; - if (bufsize) strcpy(buf, ""); - while (found == 0 && (getline(&line, &linesize, f)) != -1) { - if ((comment = strchr(line, '#'))) *comment = '\0'; - name = strtok_r(line, " \t\r\n\v", &tok); - port = strtok_r(NULL, "/ \t\r\n\v", &tok); - proto = strtok_r(NULL, " \t\r\n\v", &tok); - if (name && port && proto) { - alias = name; - while (alias && strcasecmp(alias, servname) != 0) - alias = strtok_r(NULL, " \t\r\n\v", &tok); - if (alias) /* alias matched with servname */ - { - if (!servproto[0] || strncasecmp(proto, servproto, servprotolen) == 0) { - if (!servproto[0] && !memccpy(servproto, proto, '\0', servprotolen)) { - strcpy(servproto, ""); - break; - } - if (bufsize && !memccpy(buf, name, '\0', bufsize)) { - *buf = 0; - break; - } - result = atoi(port); - found = 1; - } - } - } - } - free(line); - if (ferror(f)) { - errno = ferror(f); - return -1; - } - fclose(f); - if (!found) return -1; - return result; -} diff --git a/libc/dns/lookupservicesbyport.c b/libc/dns/lookupservicesbyport.c deleted file mode 100644 index 42af3aefc..000000000 --- a/libc/dns/lookupservicesbyport.c +++ /dev/null @@ -1,107 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dce.h" -#include "libc/dns/servicestxt.h" -#include "libc/errno.h" -#include "libc/fmt/conv.h" -#include "libc/intrin/safemacros.internal.h" -#include "libc/macros.internal.h" -#include "libc/mem/mem.h" -#include "libc/str/str.h" - -/** - * Opens and searches /etc/services to find name for a given port. - * - * The format of /etc/services is like this: - * - * # comment - * # NAME PORT/PROTOCOL ALIASES - * ftp 21/tcp - * fsp 21/udp fspd - * ssh 22/tcp - * - * @param servport is the port number - * @param servproto is a NULL-terminated string (eg "tcp", "udp") - * (if servproto is an empty string, - * if is filled with the first matching - * protocol) - * @param servprotolen the size of servproto - * @param buf is a buffer to store the official name of the service - * @param bufsize is the size of buf - * @param path is the location of the services file, which may be NULL - * to use the system-wide default - * @return 0 on success, -1 on error - * @note aliases are not read from the file. - */ -int LookupServicesByPort(const int servport, char *servproto, - size_t servprotolen, char *buf, size_t bufsize, - const char *path) { - FILE *f; - int found; - char *line; - size_t linesize; - char pathbuf[256]; - char *name, *port, *proto, *comment, *tok; - if (!servprotolen || - !(f = fopen(path ? path : GetServicesTxtPath(pathbuf, sizeof(pathbuf)), - "r"))) { - return -1; - } - found = 0; - line = NULL; - linesize = 0; - if (bufsize) *buf = 0; - while (!found && (getline(&line, &linesize, f)) != -1) { - if ((comment = strchr(line, '#'))) *comment = '\0'; - name = strtok_r(line, " \t\r\n\v", &tok); - port = strtok_r(NULL, "/ \t\r\n\v", &tok); - proto = strtok_r(NULL, " \t\r\n\v", &tok); - if (name && port && proto && servport == atoi(port)) { - if (!servproto[0] || strncasecmp(proto, servproto, servprotolen) == 0) { - if (!servproto[0] && !memccpy(servproto, proto, '\0', servprotolen)) { - strcpy(servproto, ""); - break; - } - if (!memccpy(buf, name, '\0', bufsize)) { - if (bufsize) { - *buf = 0; - } - break; - } - found = 1; - } - } - } - free(line); - if (ferror(f)) { - errno = ferror(f); - return -1; - } - fclose(f); - if (!found) return -1; - return 0; -} diff --git a/libc/dns/netent.c b/libc/dns/netent.c deleted file mode 100644 index 91dbca0b8..000000000 --- a/libc/dns/netent.c +++ /dev/null @@ -1,45 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/ent.h" - -struct netent *getnetent(void) { - return NULL; -} - -struct netent *getnetbyname(const char *name) { - return NULL; -} - -struct netent *getnetbyaddr(uint32_t net, int type) { - return NULL; -} - -void setnetent(int stayopen) { -} - -void endnetent(void) { -} diff --git a/libc/dns/newaddrinfo.c b/libc/dns/newaddrinfo.c deleted file mode 100644 index 76f918b34..000000000 --- a/libc/dns/newaddrinfo.c +++ /dev/null @@ -1,39 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 Justine Alexandra Roberts Tunney │ -│ │ -│ Permission to use, copy, modify, and/or distribute this software for │ -│ any purpose with or without fee is hereby granted, provided that the │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/dns.h" -#include "libc/macros.internal.h" -#include "libc/mem/mem.h" -#include "libc/sock/sock.h" -#include "libc/sysv/consts/af.h" - -#define SIZE sizeof(struct addrinfo) -#define ADDRLEN sizeof(struct sockaddr_in) - -struct addrinfo *newaddrinfo(uint16_t port) { - struct addrinfo *ai; - if ((ai = calloc(1, SIZE + ADDRLEN + DNS_NAME_MAX + 1))) { - ai->ai_family = AF_INET; - ai->ai_addrlen = ADDRLEN; - ai->ai_addr4 = (struct sockaddr_in *)((char *)ai + SIZE); - ai->ai_addr4->sin_family = AF_INET; - ai->ai_addr4->sin_port = htons(port); - ai->ai_canonname = (char *)ai + SIZE + ADDRLEN; - } - return ai; -} diff --git a/libc/dns/parsehoststxt.c b/libc/dns/parsehoststxt.c deleted file mode 100644 index 331722175..000000000 --- a/libc/dns/parsehoststxt.c +++ /dev/null @@ -1,72 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 Justine Alexandra Roberts Tunney │ -│ │ -│ Permission to use, copy, modify, and/or distribute this software for │ -│ any purpose with or without fee is hereby granted, provided that the │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/dns.h" -#include "libc/dns/hoststxt.h" -#include "libc/errno.h" -#include "libc/mem/arraylist.internal.h" -#include "libc/mem/mem.h" -#include "libc/runtime/runtime.h" -#include "libc/sock/sock.h" -#include "libc/stdio/stdio.h" -#include "libc/str/str.h" -#include "libc/sysv/consts/af.h" - -/** - * Parses HOSTS.TXT contents. - * - * Hostnames were invented by Peggy Karp; her format looks like this: - * - * # this is a comment - * # IP CANON [ALT...] - * 203.0.113.1 lol.example. lol - * 203.0.113.2 cat.example. cat - * - * @param htp points to a HostsTxt object, which should be zero - * initialized by the caller; or if it already contains items, - * this function will append - * @param f is the file content; see fopen() and fmemopen() - * @return 0 on success, or -1 w/ errno - * @see hoststxtsort() which is the logical next step - */ -int ParseHostsTxt(struct HostsTxt *ht, FILE *f) { - char *line; - size_t linesize; - struct HostsTxtEntry entry; - char *addr, *name, *tok, *comment; - line = NULL; - linesize = 0; - while ((getline(&line, &linesize, f)) != -1) { - if ((comment = strchr(line, '#'))) *comment = '\0'; - if ((addr = strtok_r(line, " \t\r\n\v", &tok)) && - inet_pton(AF_INET, addr, entry.ip) == 1) { - entry.canon = ht->strings.i; - while ((name = strtok_r(NULL, " \t\r\n\v", &tok))) { - entry.name = ht->strings.i; - concat(&ht->strings, name, strnlen(name, DNS_NAME_MAX) + 1); - append(&ht->entries, &entry); - } - } - } - free(line); - if (ferror(f)) { - errno = ferror(f); - return -1; - } - return 0; -} diff --git a/libc/dns/parseresolvconf.c b/libc/dns/parseresolvconf.c deleted file mode 100644 index f9a5cf05d..000000000 --- a/libc/dns/parseresolvconf.c +++ /dev/null @@ -1,68 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 Justine Alexandra Roberts Tunney │ -│ │ -│ Permission to use, copy, modify, and/or distribute this software for │ -│ any purpose with or without fee is hereby granted, provided that the │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/dns.h" -#include "libc/dns/resolvconf.h" -#include "libc/mem/arraylist.internal.h" -#include "libc/mem/mem.h" -#include "libc/runtime/runtime.h" -#include "libc/sock/sock.h" -#include "libc/stdio/stdio.h" -#include "libc/str/str.h" -#include "libc/sysv/consts/af.h" -#include "libc/sysv/consts/inaddr.h" - -/** - * Parses /etc/resolv.conf file. - * - * The content of the file usually looks like this: - * - * nameserver 8.8.8.8 - * nameserver 8.8.4.4 - * - * @param resolv points to a ResolvConf object, which should be zero - * initialized by the caller; or if it already contains items, - * this function will append - * @param f is an open stream with file content - * @return number of nameservers appended, or -1 w/ errno - */ -int ParseResolvConf(struct ResolvConf *resolv, struct FILE *f) { - /* TODO(jart): options ndots:5 */ - int rc; - char *line; - size_t linesize; - struct sockaddr_in nameserver; - char *directive, *value, *tok, *comment; - rc = 0; - line = 0; - linesize = 0; - nameserver.sin_family = AF_INET; - nameserver.sin_port = htons(DNS_PORT); - while (getline(&line, &linesize, f) != -1) { - if ((comment = strchr(line, '#'))) *comment = '\0'; - if ((directive = strtok_r(line, " \t\r\n\v", &tok)) && - (value = strtok_r(NULL, " \t\r\n\v", &tok))) { - if ((strcmp(directive, "nameserver") == 0 && - inet_pton(AF_INET, value, &nameserver.sin_addr.s_addr) == 1)) { - if (append(&resolv->nameservers, &nameserver) != -1) ++rc; - } - } - } - free(line); - return rc | ferror(f); -} diff --git a/libc/dns/pascalifydnsname.c b/libc/dns/pascalifydnsname.c deleted file mode 100644 index 966f3dc6a..000000000 --- a/libc/dns/pascalifydnsname.c +++ /dev/null @@ -1,58 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 Justine Alexandra Roberts Tunney │ -│ │ -│ Permission to use, copy, modify, and/or distribute this software for │ -│ any purpose with or without fee is hereby granted, provided that the │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/dns.h" -#include "libc/str/str.h" -#include "libc/sysv/errfuns.h" - -/** - * Writes dotted hostname to DNS message wire. - * - * The wire format is basically a sequence of Pascal strings, for each - * label in the name. We only do enough validation to maintain protocol - * invariants. - * - * @param name is a dotted NUL-terminated hostname string - * @return bytes written (excluding NUL) or -1 w/ errno - */ -int PascalifyDnsName(uint8_t *buf, size_t size, const char *name) { - size_t i, j, k, namelen; - if ((namelen = strlen(name)) > DNS_NAME_MAX) return enametoolong(); - i = 0; - if (size || namelen) { - if (namelen + 1 > size) return enospc(); - buf[0] = '\0'; - j = 0; - for (;;) { - for (k = 0; name[j + k] && name[j + k] != '.'; ++k) { - buf[i + k + 1] = name[j + k]; - } - if (k) { - if (k > DNS_LABEL_MAX) return enametoolong(); - buf[i] = k; - i += k + 1; - } - j += k + 1; - if (!name[j - 1]) { - break; - } - } - buf[i] = '\0'; - } - return i; -} diff --git a/libc/dns/protoent.c b/libc/dns/protoent.c deleted file mode 100644 index 772bcecc6..000000000 --- a/libc/dns/protoent.c +++ /dev/null @@ -1,37 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/ent.h" - -struct protoent *getprotoent(void) { - return NULL; -} - -void setprotoent(int stayopen) { -} - -void endprotoent(void) { -} diff --git a/libc/dns/prototxt.h b/libc/dns/prototxt.h deleted file mode 100644 index 8746f044d..000000000 --- a/libc/dns/prototxt.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef COSMOPOLITAN_LIBC_DNS_PROTOTXT_H_ -#define COSMOPOLITAN_LIBC_DNS_PROTOTXT_H_ -#include "libc/sock/sock.h" -#include "libc/stdio/stdio.h" -COSMOPOLITAN_C_START_ - -const char *GetProtocolsTxtPath(char *, size_t); - -int LookupProtoByNumber(const int, char *, size_t, const char *) - paramsnonnull((2)); -int LookupProtoByName(const char *, char *, size_t, const char *) - paramsnonnull((1, 2)); - -/* TODO: implement like struct HostsTxt? */ - -COSMOPOLITAN_C_END_ -#endif /* COSMOPOLITAN_LIBC_DNS_PROTOTXT_H_ */ diff --git a/libc/dns/resolvconf.h b/libc/dns/resolvconf.h deleted file mode 100644 index 46a24064f..000000000 --- a/libc/dns/resolvconf.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef COSMOPOLITAN_LIBC_DNS_RESOLVCONF_H_ -#define COSMOPOLITAN_LIBC_DNS_RESOLVCONF_H_ -#include "libc/sock/sock.h" -#include "libc/stdio/stdio.h" -COSMOPOLITAN_C_START_ - -struct Nameservers { - size_t i, n; - struct sockaddr_in *p; -}; - -struct ResolvConf { - struct Nameservers nameservers; -}; - -const struct ResolvConf *GetResolvConf(void) returnsnonnull; -int ParseResolvConf(struct ResolvConf *, struct FILE *) paramsnonnull(); -void FreeResolvConf(struct ResolvConf **) paramsnonnull(); -int GetNtNameServers(struct ResolvConf *) paramsnonnull(); - -COSMOPOLITAN_C_END_ -#endif /* COSMOPOLITAN_LIBC_DNS_RESOLVCONF_H_ */ diff --git a/libc/dns/resolvedns.c b/libc/dns/resolvedns.c deleted file mode 100644 index 83c6c4ac7..000000000 --- a/libc/dns/resolvedns.c +++ /dev/null @@ -1,113 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 Justine Alexandra Roberts Tunney │ -│ │ -│ Permission to use, copy, modify, and/or distribute this software for │ -│ any purpose with or without fee is hereby granted, provided that the │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/calls/calls.h" -#include "libc/dns/consts.h" -#include "libc/dns/dns.h" -#include "libc/dns/dnsheader.h" -#include "libc/dns/dnsquestion.h" -#include "libc/dns/resolvconf.h" -#include "libc/serialize.h" -#include "libc/mem/mem.h" -#include "libc/runtime/runtime.h" -#include "libc/sock/internal.h" -#include "libc/sock/sock.h" -#include "libc/sock/struct/sockaddr.h" -#include "libc/stdio/rand.h" -#include "libc/stdio/stdio.h" -#include "libc/str/str.h" -#include "libc/sysv/consts/af.h" -#include "libc/sysv/consts/ipproto.h" -#include "libc/sysv/consts/sock.h" -#include "libc/sysv/errfuns.h" - -#define kMsgMax 512 - -/** - * Queries Domain Name System for address associated with name. - * - * @param resolvconf can be GetResolvConf() - * @param af can be AF_INET, AF_UNSPEC - * @param name can be a local or fully-qualified hostname - * @param addr should point to a struct sockaddr_in; if this function - * succeeds, its sin_family and sin_addr fields will be modified - * @param addrsize is the byte size of addr - * @return number of matches found, or -1 w/ errno - * @error EAFNOSUPPORT. ENETDOWN, ENAMETOOLONG, EBADMSG - */ -int ResolveDns(const struct ResolvConf *resolvconf, int af, const char *name, - struct sockaddr *addr, uint32_t addrsize) { - int rc, fd, n; - struct DnsQuestion q; - struct DnsHeader h, h2; - struct sockaddr_in *a4; - uint8_t *p, *pe, msg[512]; - uint16_t rtype, rclass, rdlength; - if (addrsize < kMinSockaddr4Size) return einval(); - if (af != AF_INET && af != AF_UNSPEC) return eafnosupport(); - if (!resolvconf->nameservers.i) return 0; - bzero(&h, sizeof(h)); - rc = ebadmsg(); - h.id = _rand64(); - h.bf1 = 1; /* recursion desired */ - h.qdcount = 1; - q.qname = name; - q.qtype = DNS_TYPE_A; - q.qclass = DNS_CLASS_IN; - bzero(msg, sizeof(msg)); - SerializeDnsHeader(msg, &h); - if ((n = SerializeDnsQuestion(msg + 12, 500, &q)) == -1) return -1; - if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) return -1; - if (sendto(fd, msg, 12 + n, 0, (struct sockaddr *)resolvconf->nameservers.p, - sizeof(*resolvconf->nameservers.p)) == 12 + n && - (n = read(fd, msg, 512)) >= 12) { - DeserializeDnsHeader(&h2, msg); - if (h2.id == h.id) { - rc = 0; - p = msg + 12; - pe = msg + n; - while (p < pe && h2.qdcount--) { - p += strnlen((char *)p, pe - p) + 1 + 4; - } - while (p < pe && h2.ancount--) { - if ((p[0] & 0xc0) == 0xc0) { /* name pointer */ - p += 2; - } else { - p += strnlen((char *)p, pe - p) + 1; - } - if (p + 10 <= pe) { - rtype = READ16BE(p); - rclass = READ16BE(p + 2); - // ttl = READ32BE(p + 4); - rdlength = READ16BE(p + 8); - if (p + 10 + rdlength <= pe && rdlength == 4 && - rclass == DNS_CLASS_IN && rtype == DNS_TYPE_A) { - rc = 1; - a4 = (struct sockaddr_in *)addr; - a4->sin_family = AF_INET; - memcpy(&a4->sin_addr.s_addr, p + 10, 4); - break; - } - p += 10 + rdlength; - } - } - } - } - close(fd); - return rc; -} diff --git a/libc/dns/resolvednsreverse.c b/libc/dns/resolvednsreverse.c deleted file mode 100644 index e6c4871df..000000000 --- a/libc/dns/resolvednsreverse.c +++ /dev/null @@ -1,128 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ This is free and unencumbered software released into the public domain. │ -│ │ -│ Anyone is free to copy, modify, publish, use, compile, sell, or │ -│ distribute this software, either in source code form or as a compiled │ -│ binary, for any purpose, commercial or non-commercial, and by any │ -│ means. │ -│ │ -│ In jurisdictions that recognize copyright laws, the author or authors │ -│ of this software dedicate any and all copyright interest in the │ -│ software to the public domain. We make this dedication for the benefit │ -│ of the public at large and to the detriment of our heirs and │ -│ successors. We intend this dedication to be an overt act of │ -│ relinquishment in perpetuity of all present and future rights to this │ -│ software under copyright law. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ -│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ -│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ -│ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR │ -│ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ -│ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR │ -│ OTHER DEALINGS IN THE SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/calls/calls.h" -#include "libc/dns/consts.h" -#include "libc/dns/dns.h" -#include "libc/dns/dnsheader.h" -#include "libc/dns/dnsquestion.h" -#include "libc/dns/resolvconf.h" -#include "libc/serialize.h" -#include "libc/mem/mem.h" -#include "libc/runtime/runtime.h" -#include "libc/sock/sock.h" -#include "libc/sock/struct/sockaddr.h" -#include "libc/stdio/rand.h" -#include "libc/str/str.h" -#include "libc/sysv/consts/af.h" -#include "libc/sysv/consts/ipproto.h" -#include "libc/sysv/consts/sock.h" -#include "libc/sysv/errfuns.h" - -#define kMsgMax 512 - -/** - * Performs reverse DNS lookup with IP address. - * - * @param resolvconf can be GetResolvConf() - * @param af can be AF_INET, AF_UNSPEC - * @param name is a reversed IP address string ending with .in-addr.arpa - * @param buf to store the obtained hostname if any - * @param bufsize is size of buf - * @return 0 on success, or -1 w/ errno - * @error EAFNOSUPPORT, ENETDOWN, ENAMETOOLONG, EBADMSG - */ -int ResolveDnsReverse(const struct ResolvConf *resolvconf, int af, - const char *name, char *buf, size_t bufsize) { - int rc, fd, n; - struct DnsQuestion q; - struct DnsHeader h, h2; - uint8_t *p, *pe, msg[512]; - uint16_t rtype, rclass, rdlength; - if (af != AF_INET && af != AF_UNSPEC) return eafnosupport(); - if (!resolvconf->nameservers.i) return 0; - bzero(&h, sizeof(h)); - rc = ebadmsg(); - h.id = _rand64(); - h.bf1 = 1; /* recursion desired */ - h.qdcount = 1; - q.qname = name; - q.qtype = DNS_TYPE_PTR; - q.qclass = DNS_CLASS_IN; - bzero(msg, sizeof(msg)); - SerializeDnsHeader(msg, &h); - if ((n = SerializeDnsQuestion(msg + 12, 500, &q)) == -1) return -1; - if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) return -1; - if (sendto(fd, msg, 12 + n, 0, (struct sockaddr *)resolvconf->nameservers.p, - sizeof(*resolvconf->nameservers.p)) == 12 + n && - (n = read(fd, msg, 512)) >= 12) { - DeserializeDnsHeader(&h2, msg); - if (h2.id == h.id) { - rc = 0; - if (h2.ancount) { - p = msg + 12; - pe = msg + n; - while (p < pe && h2.qdcount) { - p += strnlen((char *)p, pe - p) + 1 + 4; - h2.qdcount--; - } - if (p + 1 < pe) { - if ((p[0] & 0b11000000) == 0b11000000) { /* name pointer */ - p += 2; - } else { - p += strnlen((char *)p, pe - p) + 1; - } - if (p + 2 + 2 + 4 + 2 < pe) { - rtype = READ16BE(p), p += 2; - rclass = READ16BE(p), p += 2; - /* ttl */ p += 4; - rdlength = READ16BE(p), p += 2; - if (p + rdlength <= pe && rtype == DNS_TYPE_PTR && - rclass == DNS_CLASS_IN) { - if (strnlen((char *)p, pe - p) + 1 > bufsize) - rc = -1; - else { - /* domain name starts with a letter */ - for (; !isalnum((char)(*p)) && p < pe; p++) rdlength--; - for (char *tmp = (char *)p; rdlength > 0 && *tmp != '\0'; - tmp++) { - /* each label is alphanumeric or hyphen - * any other character is assumed separator */ - if (!isalnum(*tmp) && *tmp != '-') *tmp = '.'; - rdlength--; - } - strcpy(buf, (char *)p); - } - } else - rc = -1; - } - } - } - } - } - close(fd); - return rc; -} diff --git a/libc/dns/resolvehostsreverse.c b/libc/dns/resolvehostsreverse.c deleted file mode 100644 index db21822c0..000000000 --- a/libc/dns/resolvehostsreverse.c +++ /dev/null @@ -1,52 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2021 Justine Alexandra Roberts Tunney │ -│ │ -│ Permission to use, copy, modify, and/or distribute this software for │ -│ any purpose with or without fee is hereby granted, provided that the │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/consts.h" -#include "libc/dns/dns.h" -#include "libc/dns/hoststxt.h" -#include "libc/serialize.h" -#include "libc/mem/alg.h" -#include "libc/sock/sock.h" -#include "libc/str/str.h" -#include "libc/sysv/consts/af.h" -#include "libc/sysv/errfuns.h" - -/** - * Finds name associated with address in HOSTS.TXT table. - * - * @param ht can be GetHostsTxt() - * @param af can be AF_INET - * @param ip is IP address in binary (sin_addr) - * @param buf is buffer to store the name - * @param bufsize is length of buf - * @return 1 if found, 0 if not found, or -1 w/ errno - * @error EAFNOSUPPORT - */ -int ResolveHostsReverse(const struct HostsTxt *ht, int af, const uint8_t *ip, - char *buf, size_t bufsize) { - size_t i; - if (af != AF_INET && af != AF_UNSPEC) return eafnosupport(); - for (i = 0; i < ht->entries.i; ++i) { - if (READ32LE(ip) == READ32LE(ht->entries.p[i].ip)) { - if (memccpy(buf, ht->strings.p + ht->entries.p[i].name, 0, bufsize)) { - return 1; - } - } - } - return 0; -} diff --git a/libc/dns/resolvehoststxt.c b/libc/dns/resolvehoststxt.c deleted file mode 100644 index 96125e26f..000000000 --- a/libc/dns/resolvehoststxt.c +++ /dev/null @@ -1,62 +0,0 @@ -/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ -│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 Justine Alexandra Roberts Tunney │ -│ │ -│ Permission to use, copy, modify, and/or distribute this software for │ -│ any purpose with or without fee is hereby granted, provided that the │ -│ above copyright notice and this permission notice appear in all copies. │ -│ │ -│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ -│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ -│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ -│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ -│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ -│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ -│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ -│ PERFORMANCE OF THIS SOFTWARE. │ -╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/dns/consts.h" -#include "libc/dns/dns.h" -#include "libc/dns/hoststxt.h" -#include "libc/mem/alg.h" -#include "libc/sock/sock.h" -#include "libc/str/str.h" -#include "libc/sysv/consts/af.h" -#include "libc/sysv/errfuns.h" - -/** - * Finds address associated with name in HOSTS.TXT table. - * - * @param ht can be GetHostsTxt() - * @param af can be AF_INET, AF_UNSPEC - * @param name can be a local or fully-qualified hostname - * @param addr should point to a struct sockaddr_in; if this function - * succeeds, its sin_family and sin_addr fields will be modified - * @param addrsize is the byte size of addr - * @param canon be used to return a pointer to the canonical name - * @return number of matches found, or -1 w/ errno - * @error EAFNOSUPPORT - */ -int ResolveHostsTxt(const struct HostsTxt *ht, int af, const char *name, - struct sockaddr *addr, uint32_t addrsize, - const char **canon) { - size_t i; - struct sockaddr_in *addr4; - if (af != AF_INET && af != AF_UNSPEC) return eafnosupport(); - for (i = 0; i < ht->entries.i; ++i) { - if (!CompareDnsNames(name, ht->strings.p + ht->entries.p[i].name)) { - if (addr) { - if (addrsize < kMinSockaddr4Size) return einval(); - addr4 = (struct sockaddr_in *)addr; - addr4->sin_family = AF_INET; - memcpy(&addr4->sin_addr.s_addr, &ht->entries.p[i].ip[0], 4); - } - if (canon) { - *canon = ht->strings.p + ht->entries.p[i].canon; - } - return 1; - } - } - return 0; -} diff --git a/libc/dns/rfc0226.txt b/libc/dns/rfc0226.txt deleted file mode 100644 index 961828314..000000000 --- a/libc/dns/rfc0226.txt +++ /dev/null @@ -1,59 +0,0 @@ - - - - - - -NETWORK WORKING GROUP PEGGY KARP -REQUEST FOR COMMENTS #226 MITRE -NIC #7625 20 SEPT 71 -CATEGORIES: D.3 -UPDATES: NONE -OBSOLETES: NONE - - STANDARDIZATION OF HOST MNEUMONICS - -IN EACH TELNET IMPLEMENTATION, A LIST OF HOST NMEUMONICS IS PROVIDED -FOR THE USER TO INDICATE THE SERVING HOST DESIRED. CURRENTLY, EACH -SITE EMPLOYS THEIR OWN SPECIAL LIST. RATHER THAN REQUIRE THE USER TO -BE COGNIZANT OF THE IDIOSYNCRASIES OF EACH LIST, ESPECIALLY WHEN -CHAINED THROUGH SEVERAL HOSTS VIA TELNET, IT HAS BEEN RECOMMENDED THAT -STANDARD HOST DESIGNATORS BE ADOPTED. - -THE FOLLOWING LIST OF SIX CHARACTER DESIGNATORS IS PROPOSED AS THE -STANDARD LIST. REGISTER ANY OBJECTIONS BY 3 OCTOBER TO P. KARP (703) -893-3500 X2391, X2318. AN OFFICIAL POLICY AND IMPLEMENTATION DATE WILL -BE SET ON 8 OCTOBER. - -THE LIST: - HOST # DESIGNATOR - 1 UCLA - 65 UCLA36 - 2 SRIARC - 66 SRIAI - 3 UCSB - 4 UTAH - 6 MULTCS - 70 MITDM - 7 RAND - 8 SDC - 9 HARV - 10 LNCTX2 - 74 LNC360 - 11 STAN - 12 ILL - 69 BBN - 133 BBNB - 144 AMES - 145 MITRE - 158 TIP - - [ This RFC was put into machine readable form for entry ] - [ into the online RFC archives by BBN Corp. under the ] - [ direction of Alex McKenzie. 12/96 ] - - - - - [Page 1] - diff --git a/libc/dns/rfc0247.txt b/libc/dns/rfc0247.txt deleted file mode 100644 index 57e320915..000000000 --- a/libc/dns/rfc0247.txt +++ /dev/null @@ -1,222 +0,0 @@ - - - - - - -Network Working Group Peggy Karp -Request for Comments: #247 MITRE -NIC 7688 12 October 1971 -Categories: Policy, Telnet -Related: #226, 236, 239, 233, 237 -Obsoletes: #226 - - Proferred Set of Standard Host Names - - In RFC #226, BBN's TENEX list of Host names was set up as a strawman - set of standard Host names. Comments received since then (an RFC - actually generated comments!!!) have influenced me to propose the - following general rules for forming Host names. - - The Host names will be 8 characters in length. The general form is - - '-' - - will be at most 4 characters, formed as follows: - - (a) Use the keyword in the site name, if not more than - four characters, e.g., NASA Ames, Case Western - Reserve. ---- ---- - - (b) Use the standard acronym, if not more than four - characters, e.g., UCLA, RADC, NBS. - - (c) If a standard abbreviation exists, use it, e.g., Ill. - - (d) If none of the above apply, use the first four letters - in the site name, e.g., Burr, Mitr, Harv. - - (e) If none of the above is acceptable to the site, the - technical liaison should select the site mnemonic. - - will be at most 4 characters of the form - . - Examples of mfg. # are: - - IBM 360 2 digit model number - IBM 370 3 digit model number - PDP 1 - 2 digit model number - Burroughs 4 digits - CDC 4 digits - etc. - - - - - - - - [Page 1] - -RFC #247 - - - will be used when more than one machine of the same - type is located at a site (e.g., 2 PDP-10s at MIT, at SRI, and - at BBN). - - Limiting to 4 characters does not permit distinctions - to be made between machines with 4 digit mfg. #s. I expect - the situation will be handled in an ad hoc manner by the NIC if - it arises. - - TIPs are identified as 'TIP' rather than by '316'. If a Host - is not to be permanently addressable, the machine is identified - as 'TEST'. - - A list of Host names, formed according to these rules, is - attached. Alternate Host names should be provided, as - suggested by Jon Postel (RFC #236). RFC's 206, 233, and - 236 present lists with 4-character alternate names. The - Technical Liaison should select the alternate name for his - site and communicate the selection to the NIC. - - - The preceding rules and the attached list of Host names are - subject to the approval of the NWG. Hereafter, the list will - be generated and maintained by the NIC in cooperation with - the Technical Liaison at each site, as suggested in RFC #237. - Comments should be addressed to Dick Watson. - - - - - - - - [ This RFC was put into machine readable form for entry ] - - [ into the online RFC archives by BBN Corp. under the ] - - [ direction of Alex McKenzie. 12/96 ] - - - - - - - - - - - - [Page 2] - -RFC #247 -Attachment 1 - - NETWORK ADDRESS STANDARD NAME - --------------- ------------- - 1 UCLA-7 - 65 UCLA-91 - 2 SRI-10NI - 66 SRI-10AI - 3 UCSB-75 - 4 UTAH-10 - 5 BBN-516 - 69 BBN-10A - 133 BBN-10B - 6 MIT-645 - 70 MIT-10DM - 134 MIT-10AI - 7 RAND-65 - 71 RAND-10 - 8 SDC-75 - 9 HARV-10 - 73 HARV-1 - 137 HARV-11 - 10 LL-67 - 74 LL-TX2 - 138 LL-TSP - 11 SAIL-10 - 12 ILL-11 - 76 ILL-6500 - 13 CASE-10 - 14 CMU-10 - 15 BURR-6500 - 79 BURR-TEST - 16 AMES-67 - 144 AMES-TIP - 145 MITR-TIP - 18 RADC-645 - 146 RADC-TIP - 19 NBS-11 - 147 NBS-TIP - 148 ETAC-TIP - 21 TINK-418 - 22 MCCL-418 - 23 USC-44 - 151 USC-TIP - 152 GWC-TIP - 25 NCAR-7600 - 153 NCAR-TIP - 158 BBNX-TEST - - - - [Page 3] - -RFC #247 -Attachment 2 - - An Implementation Scheme - -If the standard Host names are formed according to the proposed -rules, the following implementation scheme, suggested by Steve -Crocker, can be used. - - Map into an 8-bit number, S and - map into an 8-bit number, M, - where - S + M = Network Address. - - S and M can be selected such that specification of - alone could cause a default to the "primary" Host at - the site. Note that this scheme depends on a unique - designator for each IMP. - -Some examples: - -If the "primary" Host at UCLA is the 91, let - UCLA -> S = X'41' - 7 -> M = X'40' - 91 -> M = X'00' -then for - UCLA-7, S + M = X'01' = 1 base 10 - UCLA-91,S + M = X'41' = 65 base 10 - -and - UCLA alone = X'41' = 65 base 10 - -If the primary Host at BBN is TENEX System A, let - BBN -> S = X'45' - 516 -> M = X'40' - 10A -> M = X'00' - 10B -> M = X'C0' -then for - BBN-516, S + M = X'05' = 5 base 10 - BBN-10A, S + M = X'45' = 69 base 10 - BBN-10B, S + M = X'85' = 133 base 10 - -and - BBN alone = X'45' = 69 base 10 - -The primary Host for each IMP would be designated by the -site and such information disseminated by the NIC. - - - - - - [Page 4] - diff --git a/libc/dns/rfc1035.txt b/libc/dns/rfc1035.txt deleted file mode 100644 index b1a9bf5a9..000000000 --- a/libc/dns/rfc1035.txt +++ /dev/null @@ -1,3077 +0,0 @@ -Network Working Group P. Mockapetris -Request for Comments: 1035 ISI - November 1987 -Obsoletes: RFCs 882, 883, 973 - - DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION - - -1. STATUS OF THIS MEMO - -This RFC describes the details of the domain system and protocol, and -assumes that the reader is familiar with the concepts discussed in a -companion RFC, "Domain Names - Concepts and Facilities" [RFC-1034]. - -The domain system is a mixture of functions and data types which are an -official protocol and functions and data types which are still -experimental. Since the domain system is intentionally extensible, new -data types and experimental behavior should always be expected in parts -of the system beyond the official protocol. The official protocol parts -include standard queries, responses and the Internet class RR data -formats (e.g., host addresses). Since the previous RFC set, several -definitions have changed, so some previous definitions are obsolete. - -Experimental or obsolete features are clearly marked in these RFCs, and -such information should be used with caution. - -The reader is especially cautioned not to depend on the values which -appear in examples to be current or complete, since their purpose is -primarily pedagogical. Distribution of this memo is unlimited. - - Table of Contents - - 1. STATUS OF THIS MEMO 1 - 2. INTRODUCTION 3 - 2.1. Overview 3 - 2.2. Common configurations 4 - 2.3. Conventions 7 - 2.3.1. Preferred name syntax 7 - 2.3.2. Data Transmission Order 8 - 2.3.3. Character Case 9 - 2.3.4. Size limits 10 - 3. DOMAIN NAME SPACE AND RR DEFINITIONS 10 - 3.1. Name space definitions 10 - 3.2. RR definitions 11 - 3.2.1. Format 11 - 3.2.2. TYPE values 12 - 3.2.3. QTYPE values 12 - 3.2.4. CLASS values 13 - - - -Mockapetris [Page 1] - -RFC 1035 Domain Implementation and Specification November 1987 - - - 3.2.5. QCLASS values 13 - 3.3. Standard RRs 13 - 3.3.1. CNAME RDATA format 14 - 3.3.2. HINFO RDATA format 14 - 3.3.3. MB RDATA format (EXPERIMENTAL) 14 - 3.3.4. MD RDATA format (Obsolete) 15 - 3.3.5. MF RDATA format (Obsolete) 15 - 3.3.6. MG RDATA format (EXPERIMENTAL) 16 - 3.3.7. MINFO RDATA format (EXPERIMENTAL) 16 - 3.3.8. MR RDATA format (EXPERIMENTAL) 17 - 3.3.9. MX RDATA format 17 - 3.3.10. NULL RDATA format (EXPERIMENTAL) 17 - 3.3.11. NS RDATA format 18 - 3.3.12. PTR RDATA format 18 - 3.3.13. SOA RDATA format 19 - 3.3.14. TXT RDATA format 20 - 3.4. ARPA Internet specific RRs 20 - 3.4.1. A RDATA format 20 - 3.4.2. WKS RDATA format 21 - 3.5. IN-ADDR.ARPA domain 22 - 3.6. Defining new types, classes, and special namespaces 24 - 4. MESSAGES 25 - 4.1. Format 25 - 4.1.1. Header section format 26 - 4.1.2. Question section format 28 - 4.1.3. Resource record format 29 - 4.1.4. Message compression 30 - 4.2. Transport 32 - 4.2.1. UDP usage 32 - 4.2.2. TCP usage 32 - 5. MASTER FILES 33 - 5.1. Format 33 - 5.2. Use of master files to define zones 35 - 5.3. Master file example 36 - 6. NAME SERVER IMPLEMENTATION 37 - 6.1. Architecture 37 - 6.1.1. Control 37 - 6.1.2. Database 37 - 6.1.3. Time 39 - 6.2. Standard query processing 39 - 6.3. Zone refresh and reload processing 39 - 6.4. Inverse queries (Optional) 40 - 6.4.1. The contents of inverse queries and responses 40 - 6.4.2. Inverse query and response example 41 - 6.4.3. Inverse query processing 42 - - - - - - -Mockapetris [Page 2] - -RFC 1035 Domain Implementation and Specification November 1987 - - - 6.5. Completion queries and responses 42 - 7. RESOLVER IMPLEMENTATION 43 - 7.1. Transforming a user request into a query 43 - 7.2. Sending the queries 44 - 7.3. Processing responses 46 - 7.4. Using the cache 47 - 8. MAIL SUPPORT 47 - 8.1. Mail exchange binding 48 - 8.2. Mailbox binding (Experimental) 48 - 9. REFERENCES and BIBLIOGRAPHY 50 - Index 54 - -2. INTRODUCTION - -2.1. Overview - -The goal of domain names is to provide a mechanism for naming resources -in such a way that the names are usable in different hosts, networks, -protocol families, internets, and administrative organizations. - -From the user's point of view, domain names are useful as arguments to a -local agent, called a resolver, which retrieves information associated -with the domain name. Thus a user might ask for the host address or -mail information associated with a particular domain name. To enable -the user to request a particular type of information, an appropriate -query type is passed to the resolver with the domain name. To the user, -the domain tree is a single information space; the resolver is -responsible for hiding the distribution of data among name servers from -the user. - -From the resolver's point of view, the database that makes up the domain -space is distributed among various name servers. Different parts of the -domain space are stored in different name servers, although a particular -data item will be stored redundantly in two or more name servers. The -resolver starts with knowledge of at least one name server. When the -resolver processes a user query it asks a known name server for the -information; in return, the resolver either receives the desired -information or a referral to another name server. Using these -referrals, resolvers learn the identities and contents of other name -servers. Resolvers are responsible for dealing with the distribution of -the domain space and dealing with the effects of name server failure by -consulting redundant databases in other servers. - -Name servers manage two kinds of data. The first kind of data held in -sets called zones; each zone is the complete database for a particular -"pruned" subtree of the domain space. This data is called -authoritative. A name server periodically checks to make sure that its -zones are up to date, and if not, obtains a new copy of updated zones - - - -Mockapetris [Page 3] - -RFC 1035 Domain Implementation and Specification November 1987 - - -from master files stored locally or in another name server. The second -kind of data is cached data which was acquired by a local resolver. -This data may be incomplete, but improves the performance of the -retrieval process when non-local data is repeatedly accessed. Cached -data is eventually discarded by a timeout mechanism. - -This functional structure isolates the problems of user interface, -failure recovery, and distribution in the resolvers and isolates the -database update and refresh problems in the name servers. - -2.2. Common configurations - -A host can participate in the domain name system in a number of ways, -depending on whether the host runs programs that retrieve information -from the domain system, name servers that answer queries from other -hosts, or various combinations of both functions. The simplest, and -perhaps most typical, configuration is shown below: - - Local Host | Foreign - | - +---------+ +----------+ | +--------+ - | | user queries | |queries | | | - | User |-------------->| |---------|->|Foreign | - | Program | | Resolver | | | Name | - | |<--------------| |<--------|--| Server | - | | user responses| |responses| | | - +---------+ +----------+ | +--------+ - | A | - cache additions | | references | - V | | - +----------+ | - | cache | | - +----------+ | - -User programs interact with the domain name space through resolvers; the -format of user queries and user responses is specific to the host and -its operating system. User queries will typically be operating system -calls, and the resolver and its cache will be part of the host operating -system. Less capable hosts may choose to implement the resolver as a -subroutine to be linked in with every program that needs its services. -Resolvers answer user queries with information they acquire via queries -to foreign name servers and the local cache. - -Note that the resolver may have to make several queries to several -different foreign name servers to answer a particular user query, and -hence the resolution of a user query may involve several network -accesses and an arbitrary amount of time. The queries to foreign name -servers and the corresponding responses have a standard format described - - - -Mockapetris [Page 4] - -RFC 1035 Domain Implementation and Specification November 1987 - - -in this memo, and may be datagrams. - -Depending on its capabilities, a name server could be a stand alone -program on a dedicated machine or a process or processes on a large -timeshared host. A simple configuration might be: - - Local Host | Foreign - | - +---------+ | - / /| | - +---------+ | +----------+ | +--------+ - | | | | |responses| | | - | | | | Name |---------|->|Foreign | - | Master |-------------->| Server | | |Resolver| - | files | | | |<--------|--| | - | |/ | | queries | +--------+ - +---------+ +----------+ | - -Here a primary name server acquires information about one or more zones -by reading master files from its local file system, and answers queries -about those zones that arrive from foreign resolvers. - -The DNS requires that all zones be redundantly supported by more than -one name server. Designated secondary servers can acquire zones and -check for updates from the primary server using the zone transfer -protocol of the DNS. This configuration is shown below: - - Local Host | Foreign - | - +---------+ | - / /| | - +---------+ | +----------+ | +--------+ - | | | | |responses| | | - | | | | Name |---------|->|Foreign | - | Master |-------------->| Server | | |Resolver| - | files | | | |<--------|--| | - | |/ | | queries | +--------+ - +---------+ +----------+ | - A |maintenance | +--------+ - | +------------|->| | - | queries | |Foreign | - | | | Name | - +------------------|--| Server | - maintenance responses | +--------+ - -In this configuration, the name server periodically establishes a -virtual circuit to a foreign name server to acquire a copy of a zone or -to check that an existing copy has not changed. The messages sent for - - - -Mockapetris [Page 5] - -RFC 1035 Domain Implementation and Specification November 1987 - - -these maintenance activities follow the same form as queries and -responses, but the message sequences are somewhat different. - -The information flow in a host that supports all aspects of the domain -name system is shown below: - - Local Host | Foreign - | - +---------+ +----------+ | +--------+ - | | user queries | |queries | | | - | User |-------------->| |---------|->|Foreign | - | Program | | Resolver | | | Name | - | |<--------------| |<--------|--| Server | - | | user responses| |responses| | | - +---------+ +----------+ | +--------+ - | A | - cache additions | | references | - V | | - +----------+ | - | Shared | | - | database | | - +----------+ | - A | | - +---------+ refreshes | | references | - / /| | V | - +---------+ | +----------+ | +--------+ - | | | | |responses| | | - | | | | Name |---------|->|Foreign | - | Master |-------------->| Server | | |Resolver| - | files | | | |<--------|--| | - | |/ | | queries | +--------+ - +---------+ +----------+ | - A |maintenance | +--------+ - | +------------|->| | - | queries | |Foreign | - | | | Name | - +------------------|--| Server | - maintenance responses | +--------+ - -The shared database holds domain space data for the local name server -and resolver. The contents of the shared database will typically be a -mixture of authoritative data maintained by the periodic refresh -operations of the name server and cached data from previous resolver -requests. The structure of the domain data and the necessity for -synchronization between name servers and resolvers imply the general -characteristics of this database, but the actual format is up to the -local implementor. - - - - -Mockapetris [Page 6] - -RFC 1035 Domain Implementation and Specification November 1987 - - -Information flow can also be tailored so that a group of hosts act -together to optimize activities. Sometimes this is done to offload less -capable hosts so that they do not have to implement a full resolver. -This can be appropriate for PCs or hosts which want to minimize the -amount of new network code which is required. This scheme can also -allow a group of hosts can share a small number of caches rather than -maintaining a large number of separate caches, on the premise that the -centralized caches will have a higher hit ratio. In either case, -resolvers are replaced with stub resolvers which act as front ends to -resolvers located in a recursive server in one or more name servers -known to perform that service: - - Local Hosts | Foreign - | - +---------+ | - | | responses | - | Stub |<--------------------+ | - | Resolver| | | - | |----------------+ | | - +---------+ recursive | | | - queries | | | - V | | - +---------+ recursive +----------+ | +--------+ - | | queries | |queries | | | - | Stub |-------------->| Recursive|---------|->|Foreign | - | Resolver| | Server | | | Name | - | |<--------------| |<--------|--| Server | - +---------+ responses | |responses| | | - +----------+ | +--------+ - | Central | | - | cache | | - +----------+ | - -In any case, note that domain components are always replicated for -reliability whenever possible. - -2.3. Conventions - -The domain system has several conventions dealing with low-level, but -fundamental, issues. While the implementor is free to violate these -conventions WITHIN HIS OWN SYSTEM, he must observe these conventions in -ALL behavior observed from other hosts. - -2.3.1. Preferred name syntax - -The DNS specifications attempt to be as general as possible in the rules -for constructing domain names. The idea is that the name of any -existing object can be expressed as a domain name with minimal changes. - - - -Mockapetris [Page 7] - -RFC 1035 Domain Implementation and Specification November 1987 - - -However, when assigning a domain name for an object, the prudent user -will select a name which satisfies both the rules of the domain system -and any existing rules for the object, whether these rules are published -or implied by existing programs. - -For example, when naming a mail domain, the user should satisfy both the -rules of this memo and those in RFC-822. When creating a new host name, -the old rules for HOSTS.TXT should be followed. This avoids problems -when old software is converted to use domain names. - -The following syntax will result in fewer problems with many - -applications that use domain names (e.g., mail, TELNET). - - ::= | " " - - ::=