@@ -21,6 +21,9 @@ local ffi_cast = ffi.cast
21
21
local ffi_gc = ffi .gc
22
22
local C = ffi .C
23
23
24
+ local tab_isarray = require (' table.isarray' )
25
+ local tab_nkeys = require (' table.nkeys' )
26
+
24
27
local _M = {}
25
28
_M ._VERSION = ' 1.3.3'
26
29
local mt = { __index = _M }
@@ -312,7 +315,7 @@ local function _dump_entry_data_list(entry_data_list,status)
312
315
return entry_data_list ,status ,result
313
316
end
314
317
315
- function _M .lookup (ip )
318
+ function _M .lookup (ip , lookup_path )
316
319
317
320
if not initted then
318
321
return nil , " not initialized"
@@ -337,8 +340,30 @@ function _M.lookup(ip)
337
340
end
338
341
339
342
local entry_data_list = ffi_cast (' MMDB_entry_data_list_s **const' ,ffi_new (" MMDB_entry_data_list_s" ))
343
+ local status
344
+
345
+ if lookup_path then
346
+ if not tab_isarray (lookup_path ) then
347
+ return nil , ' lookup path expected array'
348
+ end
340
349
341
- local status = maxm .MMDB_get_entry_data_list (result .entry ,entry_data_list )
350
+ local lookup_path_len = tab_nkeys (lookup_path )
351
+ local entry_data = ffi_cast (' MMDB_entry_data_s *const' , ffi_new (" MMDB_entry_data_s" ))
352
+ local path = ffi_new (' const char * [?]' , lookup_path_len + 1 , lookup_path ) -- +1 for null termination
353
+ path [lookup_path_len ] = ffi .NULL
354
+
355
+ status = maxm .MMDB_aget_value (result .entry , entry_data , path )
356
+ if status == MMDB_SUCCESS then
357
+ if entry_data .offset == 0 then
358
+ return nil , ' no data found at the lookup path'
359
+ end
360
+ local entry = ffi_new (" MMDB_entry_s" , {mmdb = mmdb , offset = entry_data .offset })
361
+ status = maxm .MMDB_get_entry_data_list (entry , entry_data_list )
362
+ end
363
+
364
+ else
365
+ status = maxm .MMDB_get_entry_data_list (result .entry ,entry_data_list )
366
+ end
342
367
343
368
if status ~= MMDB_SUCCESS then
344
369
return nil ,' get entry data failed: ' .. mmdb_strerror (status )
0 commit comments