vision/src/shared/ipstring.lua

23 lines
649 B
Lua

--CData structure that can hold the longest string representation of an IPv6 address
--we do this because LuaSocket expects a Lua string, and because we can't guarantee v4-only addresses
local ffi = assert( require 'ffi' )
local ipString = {}
local string = assert( string )
ffi.cdef[[
typedef struct {
char ip[40];
} ipAddress;
]]
local ipAddress = ffi.typeof( ffi.new( "ipAddress" ) )
ffi.metatype( ipAddress, { __tostring = function( ip ) return ffi.string( ip.ip, ffi.sizeof( ip.ip ) ) end } )
function ipString.new( t )
local ip = ffi.new( ipAddress )
ip.ip = t
return ip
end
ipString.fromString = ipString.new
return ipString