mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-28 15:28:30 +00:00
Add SSL to redbean
Your redbean can now interoperate with clients that require TLS crypto. This is accomplished using a protocol polyglot that lets us distinguish between HTTP and HTTPS regardless of the port number. Certificates will be generated automatically, if none are supplied by the user. Footprint increases by only a few hundred kb so redbean in MODY=tiny is now 1.0mb - Add lseek() polyfills for ZIP executable - Automatically polyfill /tmp/FOO paths on NT - Fix readdir() / ftw() / nftw() bugs on Windows - Introduce -B flag for slower SSL that's stronger - Remove mbedtls features Cosmopolitan doesn't need - Have base64 decoder support the uri-safe alternative - Remove Truncated HMAC because it's forbidden by the IETF - Add all the mbedtls test suites and make them go 3x faster - Support opendir() / readdir() / closedir() on ZIP executable - Use Everest for ECDHE-ECDSA because it's so good it's so good - Add tinier implementation of sha1 since it's not worth the rom - Add chi-square monte-carlo mean correlation tests for getrandom() - Source entropy on Windows from the proper interface everyone uses We're continuing to outperform NGINX and other servers on raw message throughput. Using SSL means that instead of 1,000,000 qps you can get around 300,000 qps. However redbean isn't as fast as NGINX yet at SSL handshakes, since redbean can do 2,627 per second and NGINX does 4.3k Right now, the SSL UX story works best if you give your redbean a key signing key since that can be easily generated by openssl using a one liner then redbean will do all the things that are impossibly hard to do like signing ecdsa and rsa certificates that'll work in chrome. We should integrate the let's encrypt acme protocol in the future. Live Demo: https://redbean.justine.lol/ Root Cert: https://redbean.justine.lol/redbean1.crt
This commit is contained in:
parent
1beeb7a829
commit
cc1920749e
1032 changed files with 152673 additions and 69310 deletions
122
libc/sock/gethostips.c
Normal file
122
libc/sock/gethostips.c
Normal file
|
@ -0,0 +1,122 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net 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/bits/bits.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nt/errors.h"
|
||||
#include "libc/nt/iphlpapi.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/sysv/consts/ipproto.h"
|
||||
#include "libc/sysv/consts/sio.h"
|
||||
#include "libc/sysv/consts/sock.h"
|
||||
|
||||
/* TODO(jart): DELETE */
|
||||
|
||||
static uint32_t *GetUnixIps(void) {
|
||||
int fd, n;
|
||||
uint64_t z;
|
||||
uint32_t *a;
|
||||
char *b, *p, *e, c[16];
|
||||
if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == -1) return 0;
|
||||
a = 0;
|
||||
n = 0;
|
||||
z = 15000;
|
||||
b = malloc(z);
|
||||
memcpy(c, &z, 8);
|
||||
memcpy(c + (IsXnu() ? 4 : 8), &b, 8);
|
||||
if (sys_ioctl(fd, SIOCGIFCONF, &c) != -1) {
|
||||
for (p = b, e = p + MIN(z, READ32LE(c)); p + 16 + 16 <= e;
|
||||
p += IsBsd() ? 16 + MAX(16, p[16] & 255) : 40) {
|
||||
if ((p[IsBsd() ? 17 : 16] & 255) != AF_INET) continue;
|
||||
a = realloc(a, ++n * sizeof(*a));
|
||||
a[n - 1] = READ32BE(p + 20);
|
||||
}
|
||||
a = realloc(a, ++n * sizeof(*a));
|
||||
a[n - 1] = 0;
|
||||
}
|
||||
close(fd);
|
||||
free(b);
|
||||
return a;
|
||||
}
|
||||
|
||||
static textwindows uint32_t *GetWindowsIps(void) {
|
||||
uint32_t i, z, n, rc, *a;
|
||||
NtIpAdapterUnicastAddress *u;
|
||||
NtIpAdapterAddresses *p, *ifaces;
|
||||
i = 0;
|
||||
z = 15000;
|
||||
do {
|
||||
if (!(ifaces = malloc(z))) return 0;
|
||||
rc = GetAdaptersAddresses(AF_INET,
|
||||
kNtGaaFlagSkipAnycast | kNtGaaFlagSkipMulticast |
|
||||
kNtGaaFlagSkipDnsServer |
|
||||
kNtGaaFlagSkipFriendlyName,
|
||||
0, ifaces, &z);
|
||||
if (rc != kNtErrorBufferOverflow) break;
|
||||
free(ifaces);
|
||||
ifaces = 0;
|
||||
} while (++i < 3);
|
||||
if (rc == kNtErrorNoData) {
|
||||
a = calloc(1, sizeof(*a));
|
||||
} else if (rc == kNtNoError) {
|
||||
for (a = 0, n = 0, p = ifaces; p; p = p->Next) {
|
||||
if (p->OperStatus != kNtIfOperStatusUp) continue;
|
||||
for (u = p->FirstUnicastAddress; u; u = u->Next) {
|
||||
if (u->Address.lpSockaddr->sa_family != AF_INET) continue;
|
||||
a = realloc(a, ++n * sizeof(*a));
|
||||
a[n - 1] = ntohl(
|
||||
((struct sockaddr_in *)u->Address.lpSockaddr)->sin_addr.s_addr);
|
||||
}
|
||||
}
|
||||
a = realloc(a, ++n * sizeof(*a));
|
||||
a[n - 1] = 0;
|
||||
} else {
|
||||
__winerr();
|
||||
a = 0;
|
||||
}
|
||||
free(ifaces);
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns IP addresses of system.
|
||||
*
|
||||
* Normally return values will look like `{0x7f000001, 0x0a0a0a7c, 0}`
|
||||
* which means the same thing as `{"127.0.0.1", "10.10.10.124", 0}`.
|
||||
* Returned IPs will IPv4 anycast addresses bound to network interfaces
|
||||
* which come in a NULL-terminated array with no particular ordering.
|
||||
*
|
||||
* uint32_t *ip, *ips = GetIps();
|
||||
* for (ip = ips; *ip; ++ip) {
|
||||
* printf("%hhu.%hhu.%hhu.%hhu\n", *ip >> 24, *ip >> 16, *ip >> 8, *ip);
|
||||
* }
|
||||
*
|
||||
* This function supports Windows, Linux, XNU, FreeBSD, NetBSD, OpenBSD.
|
||||
*
|
||||
* @return null-terminated ip array on success, or null w/ errno
|
||||
*/
|
||||
uint32_t *GetHostIps(void) {
|
||||
if (!IsWindows()) {
|
||||
return GetUnixIps();
|
||||
} else {
|
||||
return GetWindowsIps();
|
||||
}
|
||||
}
|
|
@ -24,11 +24,11 @@
|
|||
#include "libc/sysv/errfuns.h"
|
||||
|
||||
textwindows int sys_getsockopt_nt(struct Fd *fd, int level, int optname,
|
||||
void *out_opt_optval, uint32_t *out_optlen) {
|
||||
void *out_opt_optval, uint32_t *out_optlen) {
|
||||
/* TODO(jart): Use WSAIoctl? */
|
||||
assert(fd->kind == kFdSocket);
|
||||
if (__sys_getsockopt_nt(fd->handle, level, optname, out_opt_optval, out_optlen) !=
|
||||
-1) {
|
||||
if (__sys_getsockopt_nt(fd->handle, level, optname, out_opt_optval,
|
||||
out_optlen) != -1) {
|
||||
return 0;
|
||||
} else {
|
||||
return __winsockerr();
|
||||
|
|
|
@ -23,27 +23,70 @@
|
|||
/**
|
||||
* Formats internet address to string.
|
||||
*
|
||||
* @param af can be AF_INET
|
||||
* @param af can be AF_INET or AF_INET6
|
||||
* @param src is the binary-encoded address, e.g. &addr->sin_addr
|
||||
* @param dst is the output string buffer
|
||||
* @param size is bytes in dst, which needs 16+ for IPv4
|
||||
* @param size needs to be 16+ for IPv4 and 72+ for IPv6
|
||||
* @return dst on success or NULL w/ errno
|
||||
*/
|
||||
const char *inet_ntop(int af, const void *src, char *dst, uint32_t size) {
|
||||
char *p;
|
||||
unsigned char *ip4;
|
||||
if (src) {
|
||||
unsigned char *ip;
|
||||
int i, t, a, b, c, d;
|
||||
p = dst;
|
||||
if ((ip = src)) {
|
||||
if (af == AF_INET) {
|
||||
if (size >= 16) {
|
||||
p = dst;
|
||||
ip4 = src;
|
||||
p += uint64toarray_radix10(ip4[0], p);
|
||||
p += uint64toarray_radix10(ip[0], p);
|
||||
*p++ = '.';
|
||||
p += uint64toarray_radix10(ip4[1], p);
|
||||
p += uint64toarray_radix10(ip[1], p);
|
||||
*p++ = '.';
|
||||
p += uint64toarray_radix10(ip4[2], p);
|
||||
p += uint64toarray_radix10(ip[2], p);
|
||||
*p++ = '.';
|
||||
p += uint64toarray_radix10(ip4[3], p);
|
||||
p += uint64toarray_radix10(ip[3], p);
|
||||
*p = '\0';
|
||||
return dst;
|
||||
} else {
|
||||
enospc();
|
||||
}
|
||||
} else if (af == AF_INET6) {
|
||||
if (size >= 16 * 4 + 8) {
|
||||
t = 0;
|
||||
i = 0;
|
||||
for (i = 0; i < 16; i += 2) {
|
||||
switch (t) {
|
||||
case 0:
|
||||
if (!ip[i] && !ip[i + 1]) {
|
||||
t = 1;
|
||||
*p++ = ':';
|
||||
*p++ = ':';
|
||||
continue;
|
||||
} else if (i) {
|
||||
*p++ = ':';
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if (!ip[i] && !ip[i + 1]) {
|
||||
continue;
|
||||
} else {
|
||||
t = 2;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
*p++ = ':';
|
||||
break;
|
||||
default:
|
||||
unreachable;
|
||||
}
|
||||
a = (ip[i + 0] & 0xF0) >> 4;
|
||||
b = (ip[i + 0] & 0x0F) >> 0;
|
||||
c = (ip[i + 1] & 0xF0) >> 4;
|
||||
d = (ip[i + 1] & 0x0F) >> 0;
|
||||
if (a) *p++ = "0123456789abcdef"[a];
|
||||
if (a || b) *p++ = "0123456789abcdef"[b];
|
||||
if (a || b || c) *p++ = "0123456789abcdef"[c];
|
||||
*p++ = "0123456789abcdef"[d];
|
||||
}
|
||||
*p = '\0';
|
||||
return dst;
|
||||
} else {
|
||||
|
|
|
@ -181,7 +181,7 @@ int sys_close_epoll(int) hidden;
|
|||
* Converts sockaddr (Linux/Windows) → sockaddr_bsd (XNU/BSD).
|
||||
*/
|
||||
forceinline void sockaddr2bsd(void *saddr) {
|
||||
uint8_t *p;
|
||||
char *p;
|
||||
uint16_t fam;
|
||||
if (saddr) {
|
||||
p = saddr;
|
||||
|
@ -195,7 +195,7 @@ forceinline void sockaddr2bsd(void *saddr) {
|
|||
* Converts sockaddr_in_bsd (XNU/BSD) → sockaddr (Linux/Windows).
|
||||
*/
|
||||
forceinline void sockaddr2linux(void *saddr) {
|
||||
uint8_t *p, fam;
|
||||
char *p, fam;
|
||||
if (saddr) {
|
||||
p = saddr;
|
||||
fam = p[1];
|
||||
|
|
|
@ -81,7 +81,6 @@ struct msghdr { /* Linux+NT ABI */
|
|||
uint32_t msg_flags; /* MSG_XXX */
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Structure used in SIOCGIFCONF request.
|
||||
* Used to retrieve interface configuration
|
||||
|
@ -89,50 +88,47 @@ struct msghdr { /* Linux+NT ABI */
|
|||
* must know all networks accessible).
|
||||
*/
|
||||
struct ifconf {
|
||||
uint64_t ifc_len; /* size of buffer */
|
||||
uint64_t ifc_len; /* size of buffer */
|
||||
union {
|
||||
char *ifcu_buf;
|
||||
struct ifreq *ifcu_req;
|
||||
} ifc_ifcu;
|
||||
};
|
||||
|
||||
|
||||
/* Shortcuts to the ifconf buffer or ifreq array */
|
||||
#define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */
|
||||
#define ifc_req ifc_ifcu.ifcu_req /* array of structures */
|
||||
#define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */
|
||||
#define ifc_req ifc_ifcu.ifcu_req /* array of structures */
|
||||
|
||||
#define IFHWADDRLEN 6
|
||||
#define IF_NAMESIZE 16
|
||||
#define IFNAMSIZ IF_NAMESIZE
|
||||
#define IFHWADDRLEN 6
|
||||
#define IF_NAMESIZE 16
|
||||
#define IFNAMSIZ IF_NAMESIZE
|
||||
struct ifreq {
|
||||
union {
|
||||
char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "en0". */
|
||||
char ifrn_name[IFNAMSIZ]; /* Interface name, e.g. "en0". */
|
||||
} ifr_ifrn;
|
||||
|
||||
union {
|
||||
struct sockaddr ifru_addr; /* SIOCGIFADDR */
|
||||
struct sockaddr ifru_dstaddr; /* SIOCGIFDSTADDR */
|
||||
struct sockaddr ifru_netmask; /* SIOCGIFNETMASK */
|
||||
struct sockaddr ifru_broadaddr; /* SIOCGIFBRDADDR */
|
||||
short ifru_flags; /* SIOCGIFFLAGS */
|
||||
char ifru_pad[24]; /* ifru_map is the largest, just pad */
|
||||
struct sockaddr ifru_addr; /* SIOCGIFADDR */
|
||||
struct sockaddr ifru_dstaddr; /* SIOCGIFDSTADDR */
|
||||
struct sockaddr ifru_netmask; /* SIOCGIFNETMASK */
|
||||
struct sockaddr ifru_broadaddr; /* SIOCGIFBRDADDR */
|
||||
short ifru_flags; /* SIOCGIFFLAGS */
|
||||
char ifru_pad[24]; /* ifru_map is the largest, just pad */
|
||||
} ifr_ifru;
|
||||
};
|
||||
|
||||
#define ifr_name ifr_ifrn.ifrn_name /* interface name */
|
||||
#define ifr_addr ifr_ifru.ifru_addr /* address */
|
||||
#define ifr_netmask ifr_ifru.ifru_netmask /* netmask */
|
||||
#define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
|
||||
#define ifr_dstaddr ifr_ifru.ifru_dstaddr /* destination address */
|
||||
#define ifr_flags ifr_ifru.ifru_flags /* flags */
|
||||
|
||||
#define _IOT_ifreq _IOT(_IOTS(char),IFNAMSIZ,_IOTS(char),16,0,0)
|
||||
#define _IOT_ifreq_short _IOT(_IOTS(char),IFNAMSIZ,_IOTS(short),1,0,0)
|
||||
#define _IOT_ifreq_int _IOT(_IOTS(char),IFNAMSIZ,_IOTS(int),1,0,0)
|
||||
|
||||
#define IFF_UP (1<<0)
|
||||
#define ifr_name ifr_ifrn.ifrn_name /* interface name */
|
||||
#define ifr_addr ifr_ifru.ifru_addr /* address */
|
||||
#define ifr_netmask ifr_ifru.ifru_netmask /* netmask */
|
||||
#define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */
|
||||
#define ifr_dstaddr ifr_ifru.ifru_dstaddr /* destination address */
|
||||
#define ifr_flags ifr_ifru.ifru_flags /* flags */
|
||||
|
||||
#define _IOT_ifreq _IOT(_IOTS(char), IFNAMSIZ, _IOTS(char), 16, 0, 0)
|
||||
#define _IOT_ifreq_short _IOT(_IOTS(char), IFNAMSIZ, _IOTS(short), 1, 0, 0)
|
||||
#define _IOT_ifreq_int _IOT(_IOTS(char), IFNAMSIZ, _IOTS(int), 1, 0, 0)
|
||||
|
||||
#define IFF_UP (1 << 0)
|
||||
|
||||
const char *inet_ntop(int, const void *, char *, uint32_t);
|
||||
int inet_aton(const char *, struct in_addr *);
|
||||
|
@ -140,6 +136,7 @@ int inet_pton(int, const char *, void *);
|
|||
uint32_t inet_addr(const char *);
|
||||
char *inet_ntoa(struct in_addr);
|
||||
int parseport(const char *);
|
||||
uint32_t *GetHostIps(void);
|
||||
|
||||
int socket(int, int, int) nodiscard;
|
||||
int accept(int, void *, uint32_t *) nodiscard;
|
||||
|
|
|
@ -24,11 +24,9 @@ LIBC_SOCK_A_DIRECTDEPS = \
|
|||
LIBC_FMT \
|
||||
LIBC_INTRIN \
|
||||
LIBC_MEM \
|
||||
LIBC_TIME \
|
||||
LIBC_STR \
|
||||
LIBC_UNICODE \
|
||||
LIBC_NEXGEN32E \
|
||||
LIBC_NT_ADVAPI32 \
|
||||
LIBC_NT_IPHLPAPI \
|
||||
LIBC_NT_KERNEL32 \
|
||||
LIBC_NT_MSWSOCK \
|
||||
LIBC_NT_NTDLL \
|
||||
|
@ -37,8 +35,11 @@ LIBC_SOCK_A_DIRECTDEPS = \
|
|||
LIBC_RUNTIME \
|
||||
LIBC_STDIO \
|
||||
LIBC_STR \
|
||||
LIBC_STR \
|
||||
LIBC_STUBS \
|
||||
LIBC_SYSV_CALLS \
|
||||
LIBC_TIME \
|
||||
LIBC_UNICODE \
|
||||
LIBC_SYSV
|
||||
|
||||
LIBC_SOCK_A_DEPS := \
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue