30 lines
668 B
Lua
30 lines
668 B
Lua
local ffi = assert( require 'ffi' )
|
|
local ipString = {}
|
|
local string = assert( string )
|
|
ffi.cdef[[
|
|
typedef struct {
|
|
uint8_t a;
|
|
uint8_t b;
|
|
uint8_t c;
|
|
uint8_t d;
|
|
} ipAddress;
|
|
]]
|
|
|
|
local ipAddress = ffi.typeof( ffi.new( "ipAddress" ) )
|
|
ffi.metatype( ipAddress, { __tostring = function( ip ) return ip.a.."."..ip.b.."."..ip.c.."."..ip.d end } )
|
|
|
|
function ipString.new( t )
|
|
local ip = ffi.new( ipAddress )
|
|
ip.a, ip.b, ip.c, ip.d = t[1], t[2], t[3], t[4]
|
|
return ip
|
|
end
|
|
|
|
function ipString.fromString( s )
|
|
local t = {}
|
|
for octet in s:gmatch( '%d+' ) do
|
|
table.insert( t, tonumber( octet ) )
|
|
end
|
|
return ipString.new( t )
|
|
end
|
|
|
|
return ipString |