diff --git a/binding/c/ip2region.c b/binding/c/ip2region.c index 87e0ed0..35cc2d7 100644 --- a/binding/c/ip2region.c +++ b/binding/c/ip2region.c @@ -15,7 +15,7 @@ * * @param dbFile path */ -IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, char *dbFile) +IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, const char *dbFile) { memset(ip2rObj, 0x00, sizeof(ip2region_entry)); ip2rObj->headerLen = 0; @@ -149,7 +149,7 @@ IP2R_API uint_t ip2region_memory_search(ip2region_t ip2rObj, uint_t ip, databloc return 1; } -IP2R_API uint_t ip2region_memory_search_string(ip2region_t ip2rObj, char *ip, datablock_t datablock) +IP2R_API uint_t ip2region_memory_search_string(ip2region_t ip2rObj, const char *ip, datablock_t datablock) { return ip2region_memory_search(ip2rObj, ip2long(ip), datablock); } @@ -226,7 +226,7 @@ IP2R_API uint_t ip2region_binary_search(ip2region_t ip2rObj, uint_t ip, databloc return 1; } -IP2R_API uint_t ip2region_binary_search_string(ip2region_t ip2rObj, char *ip, datablock_t datablock) +IP2R_API uint_t ip2region_binary_search_string(ip2region_t ip2rObj, const char *ip, datablock_t datablock) { return ip2region_binary_search(ip2rObj, ip2long(ip), datablock); } @@ -355,7 +355,7 @@ IP2R_API uint_t ip2region_btree_search(ip2region_t ip2rObj, uint_t ip, datablock return 1; } -IP2R_API uint_t ip2region_btree_search_string(ip2region_t ip2rObj, char *ip, datablock_t datablock) +IP2R_API uint_t ip2region_btree_search_string(ip2region_t ip2rObj, const char *ip, datablock_t datablock) { return ip2region_btree_search(ip2rObj, ip2long(ip), datablock); } @@ -367,7 +367,7 @@ IP2R_API uint_t ip2region_btree_search_string(ip2region_t ip2rObj, char *ip, dat * @param offset * @return uint_t */ -IP2R_API uint_t getUnsignedInt(char *buffer, int offset) +IP2R_API uint_t getUnsignedInt(const char *buffer, int offset) { return ( ((buffer[offset ]) & 0x000000FF) | @@ -383,10 +383,11 @@ IP2R_API uint_t getUnsignedInt(char *buffer, int offset) * @param ip * @return uint_t */ -IP2R_API uint_t ip2long(char *ip) +IP2R_API uint_t ip2long(const char *ip) { int i = 0, p = 24; - char buffer[4], *cs = ip; + char buffer[4]; + const char *cs = ip; uint_t ipval = 0; while ( *cs != '\0' ) { diff --git a/binding/c/ip2region.h b/binding/c/ip2region.h index 5c807b3..25eee71 100644 --- a/binding/c/ip2region.h +++ b/binding/c/ip2region.h @@ -80,7 +80,7 @@ typedef datablock_entry * datablock_t; * @param ip2rObj * @param dbFile path */ -IP2R_API uint_t ip2region_create(ip2region_t, char *); +IP2R_API uint_t ip2region_create(ip2region_t, const char *); /** * destroy the specified ip2region object @@ -98,7 +98,7 @@ IP2R_API uint_t ip2region_destroy(ip2region_t); * @date 2016/06/30 */ IP2R_API uint_t ip2region_memory_search(ip2region_t, uint_t, datablock_t); -IP2R_API uint_t ip2region_memory_search_string(ip2region_t, char *, datablock_t); +IP2R_API uint_t ip2region_memory_search_string(ip2region_t, const char *, datablock_t); /** @@ -110,7 +110,7 @@ IP2R_API uint_t ip2region_memory_search_string(ip2region_t, char *, datablock_t) * @return uint_t */ IP2R_API uint_t ip2region_binary_search(ip2region_t, uint_t, datablock_t); -IP2R_API uint_t ip2region_binary_search_string(ip2region_t, char *, datablock_t); +IP2R_API uint_t ip2region_binary_search_string(ip2region_t, const char *, datablock_t); /** * get the region associated with the specified ip address with b-tree algorithm @@ -121,7 +121,7 @@ IP2R_API uint_t ip2region_binary_search_string(ip2region_t, char *, datablock_t) * @return uint_t */ IP2R_API uint_t ip2region_btree_search(ip2region_t, uint_t, datablock_t); -IP2R_API uint_t ip2region_btree_search_string(ip2region_t, char *, datablock_t); +IP2R_API uint_t ip2region_btree_search_string(ip2region_t, const char *, datablock_t); /** * get a unsinged long(4bytes) from a specified buffer start from the specified offset @@ -130,7 +130,7 @@ IP2R_API uint_t ip2region_btree_search_string(ip2region_t, char *, datablock_t); * @param offset * @return uint_t */ -IP2R_API uint_t getUnsignedInt(char *, int); +IP2R_API uint_t getUnsignedInt(const char *, int); /** * string ip to long @@ -138,7 +138,7 @@ IP2R_API uint_t getUnsignedInt(char *, int); * @param ip * @return uint_t */ -IP2R_API uint_t ip2long(char *); +IP2R_API uint_t ip2long(const char *); /** * long to string ip diff --git a/binding/lua_c/Makefile b/binding/lua_c/Makefile new file mode 100644 index 0000000..cd5f207 --- /dev/null +++ b/binding/lua_c/Makefile @@ -0,0 +1,32 @@ +# ip2region luc c module Make file +# @author chenxin +# @date 2018/10/05 + +# @Note +# Please modify the LIBS and the LIB_DIR to fit you system +# + +VER = 5.2 +CC = gcc +LIBS = -I ../c/ -I /usr/include/lua$(VER)/ +FFLAGS = -O2 -Wall -fPIC +SO_FILE = Ip2region.so +LIB_DIR = /usr/local/share/lua/$(VER) + +all: ../c/ip2region.c ../c/ip2region.h lua_ip2region.c + $(CC) $(FFLAGS) $(LIBS) ../c/ip2region.c lua_ip2region.c -fPIC -shared -o $(SO_FILE) + +install: + @if [ -d $(LIB_DIR) ];\ + then\ + sudo cp $(SO_FILE) $(LIB_DIR);\ + echo "install Ip2region successfully.";\ + else\ + echo "Sorry, $(LIB_DIR) seems not not exits.";\ + fi + +clean: + find . -name \*.so | xargs rm -f + find . -name \*.o | xargs rm -f + +.PHONY: clean diff --git a/binding/lua_c/Ip2region.c b/binding/lua_c/lua_ip2region.c similarity index 87% rename from binding/lua_c/Ip2region.c rename to binding/lua_c/lua_ip2region.c index 75a8e4a..53b4422 100644 --- a/binding/lua_c/Ip2region.c +++ b/binding/lua_c/lua_ip2region.c @@ -8,17 +8,17 @@ #include #include #include -#include +#include #include "../c/ip2region.h" -#define L_METATABLE_NAME "ip2region" +#define L_METATABLE_NAME "Ip2region" /** create a new ip2region object with a specified dbFile */ static int lua_ip2region_new(lua_State *L) { ip2region_entry *self; - char *dbFile; + const char *dbFile; /* Check the arguments are valid */ dbFile = luaL_checkstring(L, 1); @@ -45,7 +45,6 @@ static int lua_ip2region_destroy(lua_State *L) ip2region_entry *self; self = (ip2region_entry *) luaL_checkudata(L, 1, L_METATABLE_NAME); ip2region_destroy(self); - &self = NULL; return 0; } @@ -56,22 +55,22 @@ static int lua_ip2region_destroy(lua_State *L) do { \ entry = (ip2region_entry *) luaL_checkudata(L, 1, L_METATABLE_NAME); \ ip = luaL_checkstring(L, 2); \ -while (0); +} while (0); \ #define set_search_result(data, rptr) \ do { \ rptr = (datablock_entry *) lua_newuserdata(L, sizeof(datablock_entry)); \ rptr->city_id = data.city_id; \ - memcpy(rptr->data.region, data.region, strlen(data.region)); \ -while (0); \ + memcpy(rptr->region, data.region, strlen(data.region)); \ +} while (0); \ /** ip2region_memory_search wrapper */ static int lua_ip2region_memory_search(lua_State *L) { ip2region_entry *self; - char *addr; + const char *addr; datablock_entry data, *rptr; // self = (ip2region_entry *) luaL_checkudata(L, 1, L_METATABLE_NAME); @@ -97,8 +96,8 @@ static int lua_ip2region_memory_search(lua_State *L) /** ip2region_binary_search wrapper */ static int lua_ip2region_binary_search(lua_State *L) { - ip2region_destroy *self; - char *addr; + ip2region_entry *self; + const char *addr; datablock_entry data, *rptr; /* Check and get the search parameters */ @@ -119,8 +118,8 @@ static int lua_ip2region_binary_search(lua_State *L) /** ip2region_btree_search wrapper */ static int lua_ip2region_btree_search(lua_State *L) { - ip2region_destroy *self; - char *addr; + ip2region_entry *self; + const char *addr; datablock_entry data, *rptr; /* Check and get the search parameters */ @@ -142,10 +141,17 @@ static int lua_ip2region_btree_search(lua_State *L) /** ip2long wrapper */ static int lua_ip2long(lua_State *L) { - char *addr; + int argc; + const char *addr; uint_t ipval; - addr = luaL_checkstring(L, 1); + argc = lua_gettop(L); + if ( argc == 1 ) { + addr = luaL_checkstring(L, 1); + } else { + luaL_checkudata(L, 1, L_METATABLE_NAME); + addr = luaL_checkstring(L, 2); + } if ( (ipval = ip2long(addr)) == 0 ) { lua_pushnil(L); @@ -164,7 +170,7 @@ static int lua_ip2region_tostring(lua_State *L) /* Push the string to return to lua */ lua_pushfstring(L, - "dbFile=%s, headerLen=%d, fristIndexPtr=%d, lastIndexPtr=%d, totalBlocks=%d" + "dbFile=%s, headerLen=%d, fristIndexPtr=%d, lastIndexPtr=%d, totalBlocks=%d", self->dbFile, self->headerLen, self->firstIndexPtr, self->lastIndexPtr, self->totalBlocks ); @@ -179,9 +185,9 @@ static const struct luaL_Reg ip2region_methods[] = { { "ip2long", lua_ip2long }, { "memorySearch", lua_ip2region_memory_search }, { "binarySearch", lua_ip2region_binary_search }, - { "btreeSerach", lua_ip2region_binary_search }, + { "btreeSearch", lua_ip2region_btree_search }, { "__gc", lua_ip2region_destroy }, - { "__tostring", ip2region_tostring }, + { "__tostring", lua_ip2region_tostring }, { NULL, NULL }, }; @@ -193,7 +199,7 @@ static const struct luaL_Reg ip2region_functions[] = { /** module open function interface */ -int luaopen_ip2region(lua_State *L) +int luaopen_Ip2region(lua_State *L) { /* Create a metatable and push it onto the stack */ luaL_newmetatable(L, L_METATABLE_NAME); @@ -215,4 +221,6 @@ int luaopen_ip2region(lua_State *L) /* Finally register the object.func functions * into the table witch at the top of the stack */ luaL_newlib(L, ip2region_functions); + + return 1; } diff --git a/binding/lua_c/testSearcher.lua b/binding/lua_c/testSearcher.lua new file mode 100644 index 0000000..9a4f3ec --- /dev/null +++ b/binding/lua_c/testSearcher.lua @@ -0,0 +1,85 @@ +--[[ +ip2region lua client test script + +@author chenxin +]]-- + +-- check the command line arguments +if ( not arg[1] ) then + print([[ +Usage: lua testSearcher.lua [ip2region db file] [algorithm] ++-Optional Algorithm: binary, b-tree, memory]]); + os.exit(); +end + +local Ip2region = require "Ip2region"; +-- local cjson = require "cjson"; +-- local socket = require "socket"; + + +-- check and parse the dbFile and the method algorithm +-- Create a new ip2region object by the new interface +local ip2region = Ip2region.new(arg[1]); + +-- reset the dbFile by the follow two ways: +-- ip2region.dbFile = arg[1]; +-- ip2region:setDbFile(arg[1]); + + +local algorithm = "btree"; +if ( arg[2] ~= nil ) then + local arg_2 = string.lower(arg[2]); + if ( arg_2 ~= "binary" and arg_2 ~= "memory" ) then + algorithm = "binary"; + elseif ( arg_2 == "memory" ) then + algorithm = "memory"; + end +end + + + +-- local data = searcher:memorySearch("120.79.17.142"); +-- local data = searcher:binarySearch("120.79.17.142"); +-- local data = searcher:btreeSearch("120.79.17.142"); +print("initializing " .. algorithm ..[[ ++----------------------------------+ +| ip2region test script | +| Author: chenxin619315@gmail.com | +| Type 'quit' to exit program | ++----------------------------------+]] +); + +while ( true ) do + io.write("ip2region>> "); + io.input(io.stdin); + local line = io.read(); + if ( line == nil ) then + -- do nothing + break; + elseif ( line == "quit" ) then + break; + elseif ( ip2region.ip2long(line) == nil ) then + print("Invalid ip address=", line); + else + local data; + local s_time = os.clock(); + if ( algorithm == "btree" ) then + data = ip2region:btreeSearch(line); + elseif ( algorithm == "binary" ) then + data = ip2region:binarySearch(line); + elseif ( algorithm == "memory" ) then + data = ip2region:memorySearch(line); + end + + local cost_time = (os.clock() - s_time) * 1000; -- to millseconds + if ( data == nil ) then + io.write("Failed for ip=", line, " is it a valid ip address ?"); + else + io.write(string.format("%u|%s in %5f millseconds\n", data.city_id, data.region, cost_time)); + end + end +end + +-- close the object +-- print(ip2region); +ip2region:close();