如何把ngx.var.binary_remote_addr转化成一个整形数字? 2014-02-26 coderesty --
http://blog.163.com/lhmwzy@126/blog/static/64215736201331110561068/这个如何? 2014-02-26 16:16 GMT+08:00 coderesty <code...@gmail.com>: 如何把ngx.var.binary_remote_addr转化成一个整形数字? 2014-02-26 coderesty -- --
以前回我的邮件里 agentzh 给过 ffi 的 ip2long 和 long2ipLance On Wed, Feb 26, 2014 at 4:30 PM, lhmwzy <lh...@gmail.com> wrote: http://blog.163.com/lhmwzy@126/blog/static/64215736201331110561068/这个如何? 2014-02-26 16:16 GMT+08:00 coderesty <code...@gmail.com>: 如何把ngx.var.binary_remote_addr转化成一个整形数字? 2014-02-26 coderesty -- -- --
function ip2long(ip_addr) local blocks = check_ip(ip_addr)[2] or error("Invalid IP-Address") return bor(bor(lshift(blocks[1],24),lshift(blocks[2],16),lshift(blocks[3],8)),blocks[4]) end在 2014年2月26日 下午4:34,Lance <lance...@gmail.com>写道: 以前回我的邮件里 agentzh 给过 ffi 的 ip2long 和 long2ipLance On Wed, Feb 26, 2014 at 4:30 PM, lhmwzy <lh...@gmail.com> wrote: http://blog.163.com/lhmwzy@126/blog/static/64215736201331110561068/这个如何? 2014-02-26 16:16 GMT+08:00 coderesty <code...@gmail.com>: 如何把ngx.var.binary_remote_addr转化成一个整形数字? 2014-02-26 coderesty -- -- -- --
ffi这个看起来应该更快吧local ffi = require "ffi"module(...) 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 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 nilendfunction 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))endLance 2014-02-26 16:40 GMT+08:00 lhmwzy <lh...@gmail.com>: function ip2long(ip_addr) local blocks = check_ip(ip_addr)[2] or error("Invalid IP-Address") return bor(bor(lshift(blocks[1],24),lshift(blocks[2],16),lshift(blocks[3],8)),blocks[4]) end在 2014年2月26日 下午4:34,Lance <lance...@gmail.com>写道: 以前回我的邮件里 agentzh 给过 ffi 的 ip2long 和 long2ipLance On Wed, Feb 26, 2014 at 4:30 PM, lhmwzy <lh...@gmail.com> wrote: http://blog.163.com/lhmwzy@126/blog/static/64215736201331110561068/这个如何? 2014-02-26 16:16 GMT+08:00 coderesty <code...@gmail.com>: 如何把ngx.var.binary_remote_addr转化成一个整形数字? 2014-02-26 coderesty -- -- -- -- --
ffi这个看起来应该更快吧 local ffi = require "ffi"module(...)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 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 nilendfunction 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 Lance 2014-02-26 16:40 GMT+08:00 lhmwzy <lh...@gmail.com>: function ip2long(ip_addr)local blocks = check_ip(ip_addr)[2] or error("Invalid IP-Address")return bor(bor(lshift(blocks[1],24),lshift(blocks[2],16),lshift(blocks[3],8)),blocks[4])end 在 2014年2月26日 下午4:34,Lance <lance...@gmail.com>写道: 以前回我的邮件里 agentzh 给过 ffi 的 ip2long 和 long2ip Lance On Wed, Feb 26, 2014 at 4:30 PM, lhmwzy <lh...@gmail.com> wrote: http://blog.163.com/lhmwzy@126/blog/static/64215736201331110561068/ 这个如何? 2014-02-26 16:16 GMT+08:00 coderesty <code...@gmail.com>: 如何把ngx.var.binary_remote_addr转化成一个整形数字? 2014-02-26 coderesty -- -- -- -- --
function ip2long(ip_addr)local blocks = check_ip(ip_addr)[2] or error("Invalid IP-Address")return bor(bor(lshift(blocks[1],24),lshift(blocks[2],16),lshift(blocks[3],8)),blocks[4])end 在 2014年2月26日 下午4:34,Lance <lance...@gmail.com>写道: 以前回我的邮件里 agentzh 给过 ffi 的 ip2long 和 long2ip Lance On Wed, Feb 26, 2014 at 4:30 PM, lhmwzy <lh...@gmail.com> wrote: http://blog.163.com/lhmwzy@126/blog/static/64215736201331110561068/ 这个如何? 2014-02-26 16:16 GMT+08:00 coderesty <code...@gmail.com>: 如何把ngx.var.binary_remote_addr转化成一个整形数字? 2014-02-26 coderesty -- -- -- --
以前回我的邮件里 agentzh 给过 ffi 的 ip2long 和 long2ip Lance On Wed, Feb 26, 2014 at 4:30 PM, lhmwzy <lh...@gmail.com> wrote: http://blog.163.com/lhmwzy@126/blog/static/64215736201331110561068/ 这个如何? 2014-02-26 16:16 GMT+08:00 coderesty <code...@gmail.com>: 如何把ngx.var.binary_remote_addr转化成一个整形数字? 2014-02-26 coderesty -- -- --
http://blog.163.com/lhmwzy@126/blog/static/64215736201331110561068/ 这个如何? 2014-02-26 16:16 GMT+08:00 coderesty <code...@gmail.com>: 如何把ngx.var.binary_remote_addr转化成一个整形数字? 2014-02-26 coderesty -- --