Sindbad~EG File Manager
local ffi = require "ffi"
local type = type
local byte = string.byte
local _M = {}
-- FFI stuff from : https://groups.google.com/g/openresty/c/nl3A-Z6ZmWA
-- Also see : https://github.com/agentzh/lua-php-utils
ffi.cdef[[
struct in_addr {
uint32_t s_addr;
};
int inet_aton(const char *cp, struct in_addr *inp);
uint32_t ntohl(uint32_t netlong);
char *inet_ntoa(struct in_addr in);
uint32_t htonl(uint32_t hostlong);
]]
function _M.ip2long(ip)
local inp = ffi.new("struct in_addr[1]")
if ffi.C.inet_aton(ip, inp) ~= 0 then
return ffi.C.ntohl(inp[0].s_addr)
end
return nil
end
function _M.long2ip(long)
if type(long) ~= "number" then
return nil
end
local addr = ffi.new("struct in_addr")
addr.s_addr = ffi.C.htonl(long)
return ffi.string(ffi.C.inet_ntoa(addr))
end
-- Convert a binary nginx IP to a long representation
-- @binaryIp the IP binary representation from ngx.var.binary_remote_addr
function _M.binary2longIp(binaryIp)
return 16777216 * byte(binaryIp, 1)
+ 65536 * byte(binaryIp, 2)
+ 256 * byte(binaryIp, 3)
+ byte(binaryIp, 4)
end
-- Convert a binary nginx IP to the normal string reprensentation of an IP address
-- @binaryIp the IP binary representation from ngx.var.binary_remote_addr
-- @return string|nil
function _M.binaryToStringIp(binaryIp)
local longIp = _M.binary2longIp(binaryIp)
if(type(longIp) ~= 'number') then
return nil
end
return _M.long2ip(longIp)
end
return _M
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists