mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-08-09 19:30:29 +00:00
Add optional network mask to redbean FormatIp
This commit is contained in:
parent
d6a039821f
commit
6c062ac79c
2 changed files with 14 additions and 5 deletions
|
@ -539,9 +539,10 @@ FUNCTIONS
|
||||||
Converts UNIX timestamp to an RFC1123 string that looks like this:
|
Converts UNIX timestamp to an RFC1123 string that looks like this:
|
||||||
Mon, 29 Mar 2021 15:37:13 GMT. See formathttpdatetime.c.
|
Mon, 29 Mar 2021 15:37:13 GMT. See formathttpdatetime.c.
|
||||||
|
|
||||||
FormatIp(uint32) → str
|
FormatIp(uint32[,mask:int]) → str
|
||||||
Turns integer like 0x01020304 into a string like 1.2.3.4. See also
|
Turns integer like 0x01020304 into a string like 1.2.3.4. mask is
|
||||||
ParseIp for the inverse operation.
|
an optional width (in bits) of the network prefix applied before
|
||||||
|
conversion. See also ParseIp for the inverse operation.
|
||||||
|
|
||||||
GetAssetComment(path:str) → str
|
GetAssetComment(path:str) → str
|
||||||
Returns comment text associated with asset in the ZIP central
|
Returns comment text associated with asset in the ZIP central
|
||||||
|
|
|
@ -3950,10 +3950,18 @@ static int LuaGetRemoteAddr(lua_State *L) {
|
||||||
return LuaGetAddr(L, GetRemoteAddr);
|
return LuaGetAddr(L, GetRemoteAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int MAX_CIDR = TYPE_BIT(uint32_t);
|
||||||
static int LuaFormatIp(lua_State *L) {
|
static int LuaFormatIp(lua_State *L) {
|
||||||
char b[16];
|
char b[16];
|
||||||
uint32_t ip;
|
uint32_t ip, mask;
|
||||||
ip = htonl(luaL_checkinteger(L, 1));
|
int cidr;
|
||||||
|
ip = luaL_checkinteger(L, 1);
|
||||||
|
cidr = luaL_optinteger(L, 2, MAX_CIDR);
|
||||||
|
if (cidr > MAX_CIDR || cidr <= 0) {
|
||||||
|
return luaL_argerror(L, 2,
|
||||||
|
gc(xasprintf("network mask not in range 1..%d", MAX_CIDR)));
|
||||||
|
}
|
||||||
|
ip = htonl(ip & (~0UL << (MAX_CIDR-cidr)));
|
||||||
inet_ntop(AF_INET, &ip, b, sizeof(b));
|
inet_ntop(AF_INET, &ip, b, sizeof(b));
|
||||||
lua_pushstring(L, b);
|
lua_pushstring(L, b);
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue