Skip to content

Commit 43fe595

Browse files
committed
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
1 parent 1a28e35 commit 43fe595

File tree

146 files changed

+2644
-7188
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

146 files changed

+2644
-7188
lines changed

Makefile

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ include third_party/nsync/mem/BUILD.mk # │ You can now use stdio
198198
include libc/proc/BUILD.mk # │ You can now use threads
199199
include libc/dlopen/BUILD.mk # │ You can now use processes
200200
include libc/thread/BUILD.mk # │ You can finally call malloc()
201-
include tool/hello/BUILD.mk #
202201
include third_party/zlib/BUILD.mk #
203202
include libc/stdio/BUILD.mk #
203+
include tool/hello/BUILD.mk #
204204
include libc/time/BUILD.mk #
205205
include net/BUILD.mk #
206206
include third_party/vqsort/BUILD.mk #
@@ -213,9 +213,8 @@ include third_party/intel/BUILD.mk # │
213213
include third_party/aarch64/BUILD.mk #
214214
include libc/BUILD.mk #─┘
215215
include libc/sock/BUILD.mk #─┐
216-
include libc/dns/BUILD.mk # ├──ONLINE RUNTIME
217-
include net/http/BUILD.mk # │ You can communicate with the network
218-
include third_party/musl/BUILD.mk #
216+
include net/http/BUILD.mk # ├──ONLINE RUNTIME
217+
include third_party/musl/BUILD.mk # │ You can communicate with the network
219218
include libc/x/BUILD.mk #
220219
include dsp/scale/BUILD.mk #
221220
include dsp/mpeg/BUILD.mk #
@@ -297,7 +296,6 @@ include test/libc/calls/BUILD.mk
297296
include test/libc/x/BUILD.mk
298297
include test/libc/xed/BUILD.mk
299298
include test/libc/fmt/BUILD.mk
300-
include test/libc/dns/BUILD.mk
301299
include test/libc/time/BUILD.mk
302300
include test/libc/proc/BUILD.mk
303301
include test/libc/stdio/BUILD.mk
@@ -373,7 +371,6 @@ loc: o/$(MODE)/tool/build/summy.com
373371
COSMOPOLITAN_OBJECTS = \
374372
TOOL_ARGS \
375373
NET_HTTP \
376-
LIBC_DNS \
377374
LIBC_SOCK \
378375
LIBC_NT_WS2_32 \
379376
LIBC_NT_IPHLPAPI \
@@ -424,7 +421,6 @@ COSMOPOLITAN_H_PKGS = \
424421
APE \
425422
LIBC \
426423
LIBC_CALLS \
427-
LIBC_DNS \
428424
LIBC_ELF \
429425
LIBC_FMT \
430426
LIBC_DLOPEN \

dsp/mpeg/buffer.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include "dsp/mpeg/mpeg.h"
44
COSMOPOLITAN_C_START_
55

6-
struct FILE;
7-
86
enum plm_buffer_mode {
97
PLM_BUFFER_MODE_FILE,
108
PLM_BUFFER_MODE_FIXED_MEM,
@@ -17,7 +15,7 @@ typedef struct plm_buffer_t {
1715
unsigned length;
1816
int free_when_done;
1917
int close_when_done;
20-
struct FILE *fh;
18+
FILE *fh;
2119
plm_buffer_load_callback load_callback;
2220
void *load_callback_user_data;
2321
unsigned char *bytes;

dsp/mpeg/mpeg.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#ifndef COSMOPOLITAN_DSP_MPEG_MPEG_H_
22
#define COSMOPOLITAN_DSP_MPEG_MPEG_H_
3+
#include "libc/stdio/stdio.h"
34
COSMOPOLITAN_C_START_
45

5-
struct FILE;
6-
76
typedef struct plm_t plm_t;
87
typedef struct plm_buffer_t plm_buffer_t;
98
typedef struct plm_demux_t plm_demux_t;
@@ -112,7 +111,7 @@ plm_t *plm_create_with_filename(const char *filename);
112111
* to let plmpeg call fclose() on the handle when plm_destroy() is
113112
* called.
114113
*/
115-
plm_t *plm_create_with_file(struct FILE *fh, int close_when_done);
114+
plm_t *plm_create_with_file(FILE *fh, int close_when_done);
116115

117116
/**
118117
* 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);
257256
* to let plmpeg call fclose() on the handle when plm_destroy() is
258257
* called.
259258
*/
260-
plm_buffer_t *plm_buffer_create_with_file(struct FILE *fh, int close_when_done);
259+
plm_buffer_t *plm_buffer_create_with_file(FILE *fh, int close_when_done);
261260

262261
/**
263262
* Create a buffer instance with a pointer to memory as source. This assumes

dsp/tty/tty.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
COSMOPOLITAN_C_START_
77

8-
struct FILE;
98
struct termios;
109

1110
struct TtyIdent {

examples/BUILD.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ EXAMPLES_DIRECTDEPS = \
4444
DSP_TTY \
4545
LIBC_CALLS \
4646
LIBC_DLOPEN \
47-
LIBC_DNS \
4847
LIBC_FMT \
4948
LIBC_INTRIN \
5049
LIBC_IRQ \

examples/nc.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
╚─────────────────────────────────────────────────────────────────*/
99
#endif
1010
#include "libc/calls/calls.h"
11-
#include "libc/dns/dns.h"
1211
#include "libc/fmt/conv.h"
1312
#include "libc/log/log.h"
1413
#include "libc/macros.internal.h"
@@ -26,6 +25,7 @@
2625
#include "libc/sysv/consts/sock.h"
2726
#include "libc/sysv/consts/sol.h"
2827
#include "third_party/getopt/getopt.internal.h"
28+
#include "third_party/musl/netdb.h"
2929

3030
/**
3131
* @fileoverview netcat clone
@@ -60,9 +60,7 @@ int main(int argc, char *argv[]) {
6060
halfclose = false;
6161
break;
6262
case 'h':
63-
fputs("Usage: ", stdout);
64-
fputs(argv[0], stdout);
65-
fputs(" [-hH] IP PORT\n", stdout);
63+
tinyprint(1, "Usage: ", argv[0], " [-hH] IP PORT\n", NULL);
6664
exit(0);
6765
default:
6866
fprintf(stderr, "bad option %d\n", opt);
@@ -76,17 +74,9 @@ int main(int argc, char *argv[]) {
7674
host = argv[optind + 0];
7775
port = argv[optind + 1];
7876

79-
switch ((rc = getaddrinfo(host, port, &hint, &ai))) {
80-
case EAI_SUCCESS:
81-
break;
82-
case EAI_SYSTEM:
83-
perror("getaddrinfo");
84-
exit(1);
85-
default:
86-
fputs("EAI_", stderr);
87-
fputs(gai_strerror(rc), stderr);
88-
fputs("\n", stderr);
89-
exit(1);
77+
if ((rc = getaddrinfo(host, port, &hint, &ai))) {
78+
tinyprint(2, host, ": ", gai_strerror(rc), "\n", NULL);
79+
exit(1);
9080
}
9181

9282
if ((sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) == -1) {
@@ -95,12 +85,12 @@ int main(int argc, char *argv[]) {
9585
}
9686

9787
if (setsockopt(sock, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger)) == -1) {
98-
perror("setsockopt(SO_LINGER)");
88+
perror("SO_LINGER");
9989
exit(1);
10090
}
10191

10292
if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
103-
perror("connect");
93+
perror(host);
10494
exit(1);
10595
}
10696

examples/whois.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@
3030
╚─────────────────────────────────────────────────────────────────────────────*/
3131
#include "libc/calls/calls.h"
3232
#include "libc/calls/weirdtypes.h"
33-
#include "libc/dns/dns.h"
3433
#include "libc/errno.h"
3534
#include "libc/log/bsd.h"
3635
#include "libc/mem/mem.h"
3736
#include "libc/runtime/runtime.h"
37+
#include "libc/sock/sock.h"
3838
#include "libc/sock/struct/pollfd.h"
39+
#include "libc/stdio/stdio.h"
3940
#include "libc/str/str.h"
4041
#include "libc/sysv/consts/af.h"
4142
#include "libc/sysv/consts/ex.h"
@@ -44,6 +45,7 @@
4445
#include "libc/sysv/consts/poll.h"
4546
#include "libc/sysv/consts/sock.h"
4647
#include "third_party/getopt/getopt.internal.h"
48+
#include "third_party/musl/netdb.h"
4749
// clang-format off
4850

4951
asm(".ident\t\"\\n\\n\

libc/BUILD.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ LIBC_FILES := $(wildcard libc/*)
276276
o/$(MODE)/libc: o/$(MODE)/libc/calls \
277277
o/$(MODE)/libc/crt \
278278
o/$(MODE)/libc/dlopen \
279-
o/$(MODE)/libc/dns \
280279
o/$(MODE)/libc/elf \
281280
o/$(MODE)/libc/fmt \
282281
o/$(MODE)/libc/intrin \
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
22
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
33
╞══════════════════════════════════════════════════════════════════════════════╡
4-
│ Copyright 2022 Justine Alexandra Roberts Tunney │
4+
│ Copyright 2023 Justine Alexandra Roberts Tunney │
55
│ │
66
│ Permission to use, copy, modify, and/or distribute this software for │
77
│ any purpose with or without fee is hereby granted, provided that the │
@@ -16,11 +16,13 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19-
#include "libc/dns/ent.h"
19+
#include "libc/calls/sysdir.internal.h"
20+
#include "libc/dce.h"
2021

21-
// error number global for gethostbyname*(), gethostbyaddr*(), etc.
22-
static _Thread_local int __h_errno;
23-
24-
errno_t *__h_errno_location(void) {
25-
return &__h_errno;
22+
const char *GetHostsTxtPath(char *path, size_t size) {
23+
if (!IsWindows()) {
24+
return "/etc/hosts";
25+
} else {
26+
return GetSystemDirectoryPath(path, "drivers\\etc\\hosts", size);
27+
}
2628
}
Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
22
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
33
╞══════════════════════════════════════════════════════════════════════════════╡
4-
│ Copyright 2020 Justine Alexandra Roberts Tunney │
4+
│ Copyright 2023 Justine Alexandra Roberts Tunney │
55
│ │
66
│ Permission to use, copy, modify, and/or distribute this software for │
77
│ any purpose with or without fee is hereby granted, provided that the │
@@ -16,18 +16,13 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19-
#include "libc/dns/dns.h"
20-
#include "libc/mem/mem.h"
19+
#include "libc/calls/sysdir.internal.h"
20+
#include "libc/dce.h"
2121

22-
/**
23-
* Frees addresses returned by getaddrinfo().
24-
*/
25-
void freeaddrinfo(struct addrinfo *ai) {
26-
struct addrinfo *next;
27-
while (ai) {
28-
/* we assume ai_addr and ai_canonname are shoehorned */
29-
next = ai->ai_next;
30-
free(ai);
31-
ai = next;
22+
const char *GetProtocolsTxtPath(char *buf, size_t size) {
23+
if (!IsWindows()) {
24+
return "/etc/protocols";
25+
} else {
26+
return GetSystemDirectoryPath(buf, "drivers\\etc\\protocol", size);
3227
}
3328
}

0 commit comments

Comments
 (0)