Created
January 18, 2017 15:21
-
-
Save eugeneia/083d5422cff7432173ae3d9f08aa8541 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local ffi = require("ffi") | |
local ctable = require("lib.ctable") | |
local ipv4 = require("lib.protocol.ipv4") | |
local ip_t = ffi.typeof('uint8_t[4]') | |
local mac_sessionid_t = ffi.typeof([[ | |
struct { | |
uint8_t mac[6]; | |
uint16_t sessionid; | |
} __attribute__((packed)) | |
]]) | |
function init_ip_hash(max_sessions) | |
local max_occupy = 0.8 | |
local params = { | |
key_type = ip_t, | |
value_type = mac_sessionid_t, | |
hash_fn = ctable.hashv_32, | |
initial_size = math.ceil(max_sessions / max_occupy), | |
max_occupancy_rate = max_occupy, | |
} | |
return ctable.new(params) | |
end | |
local ip_hash = init_ip_hash(10000) | |
local ip1 = ipv4:pton("10.0.0.5") | |
local mac_sessionid = ffi.new(mac_sessionid_t) | |
mac_sessionid.sessionid = 42 | |
print("add", ip_hash:add(ip1, mac_sessionid)) | |
print("lookup", ip_hash:lookup_ptr(ip1)) | |
print("key", ipv4:ntop(ip_hash:lookup_ptr(ip1).key)) | |
print("value.sessionid", ip_hash:lookup_ptr(ip1).value.sessionid) | |
print("remove", ip_hash:remove(ip1)) | |
print("lookup", ip_hash:lookup_ptr(ip1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment