diff --git a/states/haproxy/mods/haproxy.c b/states/haproxy/mods/haproxy.c index 0b7d540..90178d7 100644 --- a/states/haproxy/mods/haproxy.c +++ b/states/haproxy/mods/haproxy.c @@ -19,13 +19,13 @@ done #include #include -static MMDB_s mmdb; - -const int STR_MAX_LEN = 255; +static MMDB_s* mmdb; static int load_geoip(lua_State *L) { const char *filename = luaL_checkstring(L, 1); - int status = MMDB_open(filename, MMDB_MODE_MMAP, &mmdb); + int status; + mmdb = malloc(sizeof (struct MMDB_s)); + status = MMDB_open(filename, MMDB_MODE_MMAP, mmdb); if (status != 0) { printf("Error loading geoip database\n"); exit(1); @@ -37,7 +37,7 @@ static int load_geoip(lua_State *L) { int get_geoip_info(const char* ip_address, MMDB_lookup_result_s* result) { int gai_error, mmdb_error; - (*result) = MMDB_lookup_string(&mmdb, ip_address, &gai_error, &mmdb_error); + (*result) = MMDB_lookup_string(mmdb, ip_address, &gai_error, &mmdb_error); if (0 != gai_error) { fprintf(stderr, "\n Error from getaddrinfo for %s - %s\n\n", ip_address, gai_strerror(gai_error)); @@ -51,25 +51,22 @@ int get_geoip_info(const char* ip_address, MMDB_lookup_result_s* result) { return 0; } -int filter_value(MMDB_lookup_result_s* result, - MMDB_entry_data_list_s** entry_data_list, +int filter_value( + bool found_entry, + MMDB_entry_s* entry, + MMDB_entry_data_list_s* entry_data_list, MMDB_entry_data_s* entry_data, const char* query) { - int status = MMDB_get_entry_data_list(&result->entry, entry_data_list); + int status; + MMDB_get_entry_data_list(entry, &entry_data_list); + MMDB_free_entry_data_list(entry_data_list); - if (result->found_entry) { + if (found_entry) { if (strcmp(query, "country") == 0) { - status = MMDB_get_value(&result->entry, entry_data, - "country", - "iso_code", - NULL); + status = MMDB_get_value(entry, entry_data, "country", "iso_code", NULL); return status; } else if (strcmp(query, "city") == 0){ - status = MMDB_get_value(&result->entry, entry_data, - "city", - "names", - "en", - NULL); + status = MMDB_get_value(entry, entry_data, "city", "names", "en", NULL); return status; } } @@ -78,18 +75,21 @@ int filter_value(MMDB_lookup_result_s* result, static int get_country(lua_State *L) { const char *ip_address = luaL_checkstring(L, 1); - char country[STR_MAX_LEN]; + char* country = malloc(4); + int status; - MMDB_entry_data_list_s *entry_data_list = NULL; - MMDB_entry_data_s entry_data; - MMDB_lookup_result_s result; + MMDB_entry_data_list_s *entry_data_list = malloc(sizeof (struct MMDB_entry_data_list_s)); + MMDB_entry_data_s* entry_data = malloc(sizeof (struct MMDB_entry_data_s)); + MMDB_lookup_result_s* result = malloc(sizeof (struct MMDB_lookup_result_s)); - get_geoip_info(ip_address, &result); + status = get_geoip_info(ip_address, result); - int status = filter_value(&result, &entry_data_list, &entry_data, "country"); + status = filter_value(result->found_entry, &result->entry, entry_data_list, entry_data, "country"); + free(result); + result = NULL; if (status == 0) { - sprintf(country, "%.*s", entry_data.data_size,entry_data.utf8_string); + sprintf(country, "%.*s", entry_data->data_size, entry_data->utf8_string); } else { strcpy(country, "not found"); } @@ -100,23 +100,33 @@ static int get_country(lua_State *L) { static int get_city(lua_State *L) { const char *ip_address = luaL_checkstring(L, 1); - char city[STR_MAX_LEN]; + char* city = malloc(50); + int status; - MMDB_entry_data_list_s *entry_data_list = NULL; - MMDB_entry_data_s entry_data; - MMDB_lookup_result_s result; + MMDB_entry_data_list_s *entry_data_list = malloc(sizeof (struct MMDB_entry_data_list_s)); + MMDB_entry_data_s* entry_data = malloc(sizeof (struct MMDB_entry_data_s)); + MMDB_lookup_result_s* result = malloc(sizeof (struct MMDB_lookup_result_s)); - get_geoip_info(ip_address, &result); + status = get_geoip_info(ip_address, result); - int status = filter_value(&result, &entry_data_list, &entry_data, "city"); + status = filter_value(result->found_entry, &result->entry, entry_data_list, entry_data, "city"); + free(result); + result = NULL; if (status == 0) { - sprintf(city, "%.*s", entry_data.data_size, entry_data.utf8_string); + sprintf(city, "%.*s", entry_data->data_size, entry_data->utf8_string); } else { strcpy(city, "not found"); } + free(entry_data); + entry_data = NULL; + lua_pushstring(L, city); + + free(city); + city = NULL; + return 1; }