mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-23 11:00:31 +00:00
Make improvements
This commit is contained in:
parent
3e4fd4b0ad
commit
e44a0cf6f8
256 changed files with 23100 additions and 2294 deletions
|
@ -41,21 +41,24 @@
|
|||
*/
|
||||
textwindows int getntnameservers(struct ResolvConf *resolv) {
|
||||
int rc;
|
||||
int64_t hkInterfaces = kNtInvalidHandleValue;
|
||||
uint32_t keycount = 0;
|
||||
char value8[128];
|
||||
int64_t hkInterfaces;
|
||||
struct sockaddr_in nameserver;
|
||||
char16_t value[128], ifaceuuid[64];
|
||||
uint32_t i, keycount, valuebytes, ifaceuuidlen;
|
||||
keycount = 0;
|
||||
hkInterfaces = kNtInvalidHandleValue;
|
||||
if (!RegOpenKeyEx(
|
||||
kNtHkeyLocalMachine,
|
||||
u"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces",
|
||||
0, kNtKeyRead, &hkInterfaces) &&
|
||||
!RegQueryInfoKey(hkInterfaces, NULL, NULL, NULL, &keycount, NULL, NULL,
|
||||
NULL, NULL, NULL, NULL, NULL)) {
|
||||
struct sockaddr_in nameserver;
|
||||
nameserver.sin_family = AF_INET;
|
||||
nameserver.sin_port = htons(DNS_PORT);
|
||||
rc = 0;
|
||||
for (uint32_t i = 0; i < keycount; ++i) {
|
||||
char16_t value[128], ifaceuuid[64];
|
||||
uint32_t valuebytes, ifaceuuidlen = sizeof(ifaceuuid);
|
||||
for (i = 0; i < keycount; ++i) {
|
||||
ifaceuuidlen = sizeof(ifaceuuid);
|
||||
if (!RegEnumKeyEx(hkInterfaces, i, ifaceuuid, &ifaceuuidlen, NULL, NULL,
|
||||
NULL, NULL) &&
|
||||
((!RegGetValue(hkInterfaces, ifaceuuid, u"DhcpIpAddress",
|
||||
|
@ -74,7 +77,6 @@ textwindows int getntnameservers(struct ResolvConf *resolv) {
|
|||
kNtRrfRtRegSz | kNtRrfRtRegMultiSz, NULL, value,
|
||||
((valuebytes = sizeof(value)), &valuebytes)) &&
|
||||
valuebytes > 2 * sizeof(char16_t)))) {
|
||||
char value8[128];
|
||||
tprecode16to8(value8, sizeof(value8), value);
|
||||
if (inet_pton(AF_INET, value8, &nameserver.sin_addr.s_addr) == 1) {
|
||||
if (append(&resolv->nameservers, &nameserver) != -1) ++rc;
|
||||
|
|
|
@ -38,7 +38,8 @@ static struct ResolvConfInitialStaticMemory {
|
|||
const struct ResolvConf *getresolvconf(void) {
|
||||
int rc;
|
||||
FILE *f;
|
||||
struct ResolvConfInitialStaticMemory *init = &g_resolvconf_init;
|
||||
struct ResolvConfInitialStaticMemory *init;
|
||||
init = &g_resolvconf_init;
|
||||
if (!g_resolvconf) {
|
||||
g_resolvconf = &init->rv;
|
||||
pushmov(&init->rv.nameservers.n, ARRAYLEN(init->nameservers));
|
||||
|
|
|
@ -23,19 +23,18 @@
|
|||
#include "libc/sock/sock.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
|
||||
#define SIZE ROUNDUP(sizeof(struct addrinfo), sizeof(void *))
|
||||
#define ADDRLEN sizeof(struct sockaddr_in)
|
||||
|
||||
struct addrinfo *newaddrinfo(uint16_t port) {
|
||||
void *mem;
|
||||
struct addrinfo *ai = NULL;
|
||||
/* shoehorning is ok since this'll never be realloc()'d */
|
||||
uint32_t size = ROUNDUP(sizeof(struct addrinfo), sizeof(void *));
|
||||
uint32_t addrlen = sizeof(struct sockaddr_in);
|
||||
if ((ai = mem = calloc(1, size + addrlen + DNS_NAME_MAX + 1))) {
|
||||
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 *)mem + size);
|
||||
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 *)mem + size + addrlen;
|
||||
ai->ai_canonname = (char *)ai + SIZE + ADDRLEN;
|
||||
}
|
||||
return ai;
|
||||
}
|
||||
|
|
|
@ -44,9 +44,10 @@
|
|||
* @see hoststxtsort() which is the logical next step
|
||||
*/
|
||||
int parsehoststxt(struct HostsTxt *ht, FILE *f) {
|
||||
int rc = 0;
|
||||
int rc;
|
||||
char *line;
|
||||
size_t linesize;
|
||||
rc = 0;
|
||||
line = NULL;
|
||||
linesize = 0;
|
||||
while ((getline(&line, &linesize, f)) != -1) {
|
||||
|
|
|
@ -44,16 +44,17 @@
|
|||
*/
|
||||
int parseresolvconf(struct ResolvConf *resolv, struct FILE *f) {
|
||||
/* TODO(jart): options ndots:5 */
|
||||
int rc = 0;
|
||||
int rc;
|
||||
char *line;
|
||||
size_t linesize;
|
||||
struct sockaddr_in nameserver;
|
||||
char *directive, *value, *tok, *comment;
|
||||
rc = 0;
|
||||
line = NULL;
|
||||
linesize = 0;
|
||||
nameserver.sin_family = AF_INET;
|
||||
nameserver.sin_port = htons(DNS_PORT);
|
||||
while (getline(&line, &linesize, f) != -1) {
|
||||
char *directive, *value, *tok, *comment;
|
||||
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))) {
|
||||
|
|
|
@ -32,15 +32,14 @@
|
|||
* @return bytes written (excluding NUL) or -1 w/ errno
|
||||
*/
|
||||
int pascalifydnsname(uint8_t *buf, size_t size, const char *name) {
|
||||
size_t i = 0;
|
||||
size_t namelen = strlen(name);
|
||||
if (namelen > DNS_NAME_MAX) return enametoolong();
|
||||
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';
|
||||
size_t j = 0;
|
||||
j = 0;
|
||||
for (;;) {
|
||||
size_t k;
|
||||
for (k = 0; name[j + k] && name[j + k] != '.'; ++k) {
|
||||
buf[i + k + 1] = name[j + k];
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ struct ResolvConf {
|
|||
const struct ResolvConf *getresolvconf(void) returnsnonnull;
|
||||
int parseresolvconf(struct ResolvConf *, struct FILE *) paramsnonnull();
|
||||
void freeresolvconf(struct ResolvConf **) paramsnonnull();
|
||||
int getntnameservers(struct ResolvConf *resolv) paramsnonnull();
|
||||
int getntnameservers(struct ResolvConf *) paramsnonnull();
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
|
|
|
@ -34,6 +34,8 @@
|
|||
#include "libc/sysv/consts/sock.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
#define kMsgMax 512
|
||||
|
||||
/**
|
||||
* Queries Domain Name System for address associated with name.
|
||||
*
|
||||
|
@ -48,10 +50,15 @@
|
|||
*/
|
||||
int resolvedns(const struct ResolvConf *resolvconf, int af, const char *name,
|
||||
struct sockaddr *addr, uint32_t addrsize) {
|
||||
size_t msgsize;
|
||||
int res, fd, rc, rc2;
|
||||
struct sockaddr_in *addr4;
|
||||
struct DnsQuestion question;
|
||||
uint16_t rtype, rclass, rdlength;
|
||||
uint8_t *p, *pe, *outmsg, *inmsg;
|
||||
struct DnsHeader header, response;
|
||||
if (af != AF_INET && af != AF_UNSPEC) return eafnosupport();
|
||||
if (!resolvconf->nameservers.i) return 0;
|
||||
struct DnsHeader header;
|
||||
struct DnsQuestion question;
|
||||
memset(&header, 0, sizeof(header));
|
||||
header.id = rand32();
|
||||
header.bf1 = 1; /* recursion desired */
|
||||
|
@ -59,28 +66,21 @@ int resolvedns(const struct ResolvConf *resolvconf, int af, const char *name,
|
|||
question.qname = name;
|
||||
question.qtype = DNS_TYPE_A;
|
||||
question.qclass = DNS_CLASS_IN;
|
||||
const size_t kMsgMax = 512;
|
||||
uint8_t *outmsg = NULL;
|
||||
uint8_t *inmsg = NULL;
|
||||
size_t msgsize;
|
||||
int res = -1;
|
||||
int rc, rc2;
|
||||
res = -1;
|
||||
if ((outmsg = malloc(kMsgMax)) && (inmsg = malloc(kMsgMax)) &&
|
||||
(rc = serializednsheader(outmsg, kMsgMax, header)) != -1 &&
|
||||
(rc2 = serializednsquestion(outmsg + rc, kMsgMax - rc, question)) != -1) {
|
||||
msgsize = rc + rc2;
|
||||
int fd;
|
||||
if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) != -1 &&
|
||||
sendto(fd, outmsg, msgsize, 0, (void *)&resolvconf->nameservers.p[0],
|
||||
sizeof(resolvconf->nameservers.p[0])) == msgsize) {
|
||||
struct DnsHeader response;
|
||||
if ((rc = recv(fd, inmsg, kMsgMax, 0)) != -1 &&
|
||||
(rc2 = deserializednsheader(&response, inmsg, rc)) != -1 &&
|
||||
response.id == header.id) {
|
||||
res = 0;
|
||||
if (response.ancount) {
|
||||
uint8_t *p = inmsg + rc2;
|
||||
uint8_t *pe = inmsg + rc;
|
||||
p = inmsg + rc2;
|
||||
pe = inmsg + rc;
|
||||
while (p < pe && response.qdcount) {
|
||||
p += strnlen((char *)p, pe - p) + 1 + 4;
|
||||
response.qdcount--;
|
||||
|
@ -92,7 +92,6 @@ int resolvedns(const struct ResolvConf *resolvconf, int af, const char *name,
|
|||
p += strnlen((char *)p, pe - p) + 1;
|
||||
}
|
||||
if (p + 2 + 2 + 4 + 2 < pe) {
|
||||
uint16_t rtype, rclass, rdlength;
|
||||
rtype = READ16BE(p), p += 2;
|
||||
rclass = READ16BE(p), p += 2;
|
||||
/* ttl */ p += 4;
|
||||
|
@ -102,7 +101,7 @@ int resolvedns(const struct ResolvConf *resolvconf, int af, const char *name,
|
|||
res = 1;
|
||||
if (addrsize) {
|
||||
if (addrsize >= kMinSockaddr4Size) {
|
||||
struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
|
||||
addr4 = (struct sockaddr_in *)addr;
|
||||
addr4->sin_family = AF_INET;
|
||||
memcpy(&addr4->sin_addr.s_addr, p, 4);
|
||||
} else {
|
||||
|
|
|
@ -23,8 +23,8 @@
|
|||
#include "libc/dns/hoststxt.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
static int hoststxtgetcmp(const char *node, const struct HostsTxtEntry *entry,
|
||||
const char *strings) {
|
||||
|
@ -50,14 +50,15 @@ static int hoststxtgetcmp(const char *node, const struct HostsTxtEntry *entry,
|
|||
int resolvehoststxt(const struct HostsTxt *ht, int af, const char *name,
|
||||
struct sockaddr *addr, uint32_t addrsize,
|
||||
const char **canon) {
|
||||
struct sockaddr_in *addr4;
|
||||
struct HostsTxtEntry *entry;
|
||||
if (af != AF_INET && af != AF_UNSPEC) return eafnosupport();
|
||||
struct HostsTxtEntry *entry = bsearch_r(
|
||||
name, ht->entries.p, ht->entries.i, sizeof(struct HostsTxtEntry),
|
||||
(void *)hoststxtgetcmp, ht->strings.p);
|
||||
if (entry) {
|
||||
if ((entry = bsearch_r(name, ht->entries.p, ht->entries.i,
|
||||
sizeof(struct HostsTxtEntry), (void *)hoststxtgetcmp,
|
||||
ht->strings.p))) {
|
||||
if (addr) {
|
||||
if (addrsize < kMinSockaddr4Size) return einval();
|
||||
struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
|
||||
addr4 = (struct sockaddr_in *)addr;
|
||||
addr4->sin_family = AF_INET;
|
||||
memcpy(&addr4->sin_addr.s_addr, &entry->ip[0], 4);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue