From a9d77362f9a116b39ae3cf7a6374fb8a5481a529 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Thu, 23 Jun 2022 17:59:35 -0700 Subject: [PATCH] Release redbean 2.0.7 --- tool/net/help.txt | 14 +++++++++--- tool/net/lfuncs.c | 2 +- tool/net/redbean.c | 53 +++++++++++++++++++++++++--------------------- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/tool/net/help.txt b/tool/net/help.txt index 739118cc6..bb9de07aa 100644 --- a/tool/net/help.txt +++ b/tool/net/help.txt @@ -1038,9 +1038,17 @@ FUNCTIONS 0x01020304, or returns -1 for invalid inputs. See also FormatIp for the inverse operation. - ProgramAddr(str) - Configures the address on which to listen. Can be used multiple - times to set more than one address. + ProgramAddr(ip:int) + ProgramAddr(host:str) + Configures the address on which to listen. This can be called + multiple times to set more than one address. If an integer is + provided then it should be a word-encoded IPv4 address, such + as the ones returned by ResolveIp(). If a string is provided, + it will first be passed to ParseIp() to see if it's an IPv4 + address. If it isn't, then a HOSTS.TXT lookup is performed, + with fallback to the system-configured DNS resolution service. + Please note that in MODE=tiny the HOSTS.TXT and DNS resolution + isn't included, and therefore an IP must be provided. ProgramBrand(str) Changes HTTP Server header, as well as the

title on the / diff --git a/tool/net/lfuncs.c b/tool/net/lfuncs.c index 8b3bff173..2b4df020e 100644 --- a/tool/net/lfuncs.c +++ b/tool/net/lfuncs.c @@ -390,7 +390,7 @@ int LuaResolveIp(lua_State *L) { struct addrinfo hint = {AI_NUMERICSERV, AF_INET, SOCK_STREAM, IPPROTO_TCP}; host = luaL_checkstring(L, 1); if ((ip = ParseIp(host, -1)) != -1) { - lua_pushinteger(L, ntohl(ai->ai_addr4->sin_addr.s_addr)); + lua_pushinteger(L, ip); return 1; } else if ((rc = getaddrinfo(host, "0", &hint, &ai)) == EAI_SUCCESS) { lua_pushinteger(L, ntohl(ai->ai_addr4->sin_addr.s_addr)); diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 9e553807f..ec6904f02 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -143,7 +143,7 @@ STATIC_YOINK("ShowCrashReportsEarly"); #define REDBEAN "redbean" #endif -#define VERSION 0x020006 +#define VERSION 0x020007 #define HEARTBEAT 5000 /*ms*/ #define HASH_LOAD_FACTOR /* 1. / */ 4 #define MONITOR_MICROS 150000 @@ -764,25 +764,22 @@ static void ProgramSslTicketLifetime(long x) { sslticketlifetime = x; } -static uint32_t ResolveIp(const char *addr) { - ssize_t rc; - uint32_t ip; - struct addrinfo *ai = NULL; - struct addrinfo hint = {AI_NUMERICSERV, AF_INET, SOCK_STREAM, IPPROTO_TCP}; - if ((rc = getaddrinfo(addr, "0", &hint, &ai)) != EAI_SUCCESS) { - DIEF("(cfg) error: bad addr: %s (EAI_%s)", addr, gai_strerror(rc)); - } - ip = ntohl(ai->ai_addr4->sin_addr.s_addr); - freeaddrinfo(ai); - return ip; -} - static void ProgramAddr(const char *addr) { - uint32_t ip; - if (IsTiny()) { - ip = ParseIp(addr, -1); - } else { - ip = ResolveIp(addr); + ssize_t rc; + int64_t ip; + if ((ip = ParseIp(addr, -1)) == -1) { + if (!IsTiny()) { + struct addrinfo *ai = NULL; + struct addrinfo hint = {AI_NUMERICSERV, AF_INET, SOCK_STREAM, + IPPROTO_TCP}; + if ((rc = getaddrinfo(addr, "0", &hint, &ai)) != EAI_SUCCESS) { + DIEF("(cfg) error: bad addr: %s (EAI_%s)", addr, gai_strerror(rc)); + } + ip = ntohl(ai->ai_addr4->sin_addr.s_addr); + freeaddrinfo(ai); + } else { + DIEF("(cfg) error: ProgramAddr() needs an IP in MODE=tiny: %s", addr); + } } ips.p = realloc(ips.p, ++ips.n * sizeof(*ips.p)); ips.p[ips.n - 1] = ip; @@ -4647,14 +4644,22 @@ static int LuaProgramUniprocess(lua_State *L) { return 1; } -static dontinline int LuaProgramString(lua_State *L, void P(const char *)) { - P(luaL_checkstring(L, 1)); +static int LuaProgramAddr(lua_State *L) { + uint32_t ip; + OnlyCallFromInitLua(L, "ProgramAddr"); + if (lua_isinteger(L, 1)) { + ip = luaL_checkinteger(L, 1); + ips.p = realloc(ips.p, ++ips.n * sizeof(*ips.p)); + ips.p[ips.n - 1] = ip; + } else { + ProgramAddr(luaL_checkstring(L, 1)); + } return 0; } -static int LuaProgramAddr(lua_State *L) { - OnlyCallFromInitLua(L, "ProgramAddr"); - return LuaProgramString(L, ProgramAddr); +static dontinline int LuaProgramString(lua_State *L, void P(const char *)) { + P(luaL_checkstring(L, 1)); + return 0; } static int LuaProgramBrand(lua_State *L) {