예제 #1
0
static void
tf_geoip_maxminddb_call(LogTemplateFunction *self, gpointer s, const LogTemplateInvokeArgs *args, GString *result)
{
  GString **argv = (GString **) args->bufs->pdata;
  TFMaxMindDBState *state = (TFMaxMindDBState *) s;

  int _gai_error, mmdb_error;
  MMDB_lookup_result_s mmdb_result =
    MMDB_lookup_string(state->database, argv[0]->str, &_gai_error, &mmdb_error);

  if (!mmdb_result.found_entry)
    {
      mmdb_problem_to_error(_gai_error, mmdb_error, "tflookup");
      return;
    }

  MMDB_entry_data_s entry_data;
  mmdb_error = MMDB_aget_value(&mmdb_result.entry, &entry_data, (const char *const* const)state->entry_path);
  if (mmdb_error != MMDB_SUCCESS)
    {
      mmdb_problem_to_error(0, mmdb_error, "tfget_value");
      return;
    }

  if (entry_data.has_data)
    append_mmdb_entry_data_to_gstring(result, &entry_data);

  return;
}
예제 #2
0
int geoip2_update_pv(str *tomatch, str *name)
{
	sr_geoip2_record_t *gr = NULL;
	int gai_error, mmdb_error;

	if(tomatch->len>255)
	{
		LM_DBG("target too long (max 255): %s\n", tomatch->s);
		return -3;
	}

	gr = sr_geoip2_get_record(name);
	if(gr==NULL)
	{
		LM_DBG("container not found: %s\n", tomatch->s);
		return - 4;
	}

	strncpy(gr->tomatch, tomatch->s, tomatch->len);
	tomatch->s[tomatch->len] = '\0';
	gr->record = MMDB_lookup_string(&_handle_GeoIP,
			(const char*)gr->tomatch,
			&gai_error, &mmdb_error
			);
	LM_DBG("attempt to match: %s\n", gr->tomatch);
	if (gai_error || MMDB_SUCCESS != mmdb_error || !gr->record.found_entry)
	{
		LM_DBG("no match for: %s\n", gr->tomatch);
		return -2;
	}
	LM_DBG("geoip2 PV updated for: %s\n", gr->tomatch);

	return 1;
}
예제 #3
0
파일: geoip2.c 프로젝트: OpenSIPS/opensips
lookup_res_t geoip2_lookup_ip(char *ip, int *status)
{
	int gai_error, mmdb_error;

	MMDB_lookup_result_s result = MMDB_lookup_string(&mmdb, ip, &gai_error,
		&mmdb_error);

	if (gai_error) {
		LM_ERR("getaddrinfo() error\n");
		goto error;
	}
	if (mmdb_error != MMDB_SUCCESS) {
		LM_ERR("libmaxminddb error: %s\n", MMDB_strerror(mmdb_error));
		goto error;
	}

	if (!result.found_entry) {
		LM_DBG("IP: %s not found\n", ip);
		goto error;
	}

	*status = 0;
	return result;

error:
	*status = -1;
	return result;
}
예제 #4
0
static std::vector <double> mmdb_getdouble(MMDB_s *data, const std::vector< std::string > &ip_addresses, ...){
  int input_size = ip_addresses.size();
  std::vector < double > output(input_size);
  
  int run_status;
  int lookup_error;
  int gai_error;
  MMDB_lookup_result_s result;
  MMDB_entry_data_s entry_data;
  va_list path;
  
  
  for(int i = 0; i < input_size; i++){
    if((i % 10000) == 0){
      Rcpp::checkUserInterrupt();
    }
    
    result = MMDB_lookup_string(data, ip_addresses[i].c_str(), &lookup_error, &gai_error);
    if((lookup_error != MMDB_SUCCESS) | (gai_error != MMDB_SUCCESS)){
      output[i] = NA_REAL;
    } else {
      va_start(path, ip_addresses);
      run_status = MMDB_vget_value(&result.entry, &entry_data, path);
      va_end(path);
      if((run_status != MMDB_SUCCESS) | (!entry_data.has_data)){
        output[i] = NA_REAL;
      } else {
        output[i] = entry_data.double_value;
      }
    }
  }
  
  return output;
}
예제 #5
0
CAMLprim value mmdb_ml_dump_per_ip(value ip, value mmdb)
{
  CAMLparam2(ip, mmdb);
  CAMLlocal1(pulled_string);

  unsigned int len = caml_string_length(ip);
  char *as_string = caml_strdup(String_val(ip));

  if (strlen(as_string) != (size_t)len) {
    caml_failwith("Could not copy IP address properly");
  }

  MMDB_s *as_mmdb = (MMDB_s*)Data_custom_val(mmdb);
  int gai_error = 0, mmdb_error = 0;

  MMDB_lookup_result_s *result = caml_stat_alloc(sizeof(*result));
  *result = MMDB_lookup_string(as_mmdb, as_string, &gai_error, &mmdb_error);
  MMDB_entry_data_list_s *entry_data_list = NULL;
  int status = MMDB_get_entry_data_list(&result->entry, &entry_data_list);
  check_status(status);
  char *pulled_from_db = data_from_dump(entry_data_list);
  pulled_string = caml_copy_string(pulled_from_db);
  caml_stat_free(result);
  caml_stat_free(as_string);
  caml_stat_free(pulled_from_db);
  free(entry_data_list);
  as_mmdb = NULL;
  CAMLreturn(pulled_string);
}
예제 #6
0
MMDB_lookup_result_s lookup_string_ok(MMDB_s *mmdb, const char *ip,
                                      const char *file, const char *mode_desc)
{
    int gai_error, mmdb_error;
    MMDB_lookup_result_s result =
        MMDB_lookup_string(mmdb, ip, &gai_error, &mmdb_error);

    test_lookup_errors(gai_error, mmdb_error, "MMDB_lookup_string", ip, file,
                       mode_desc);

    return result;
}
// lookup an ip address using the maxmind db and return the value
// lookup_path described in this doc: http://maxmind.github.io/MaxMind-DB/
const char *
geo_lookup(MMDB_s *const mmdb_handle, const char *ipstr, const char **lookup_path)
{
    char *data = NULL;
    // Lookup IP in the DB
    int gai_error, mmdb_error;
    MMDB_lookup_result_s result =
        MMDB_lookup_string(mmdb_handle, ipstr, &gai_error, &mmdb_error);

    if (0 != gai_error) {
#if DEBUG
        fprintf(stderr,
            "[INFO] Error from getaddrinfo for %s - %s\n\n",
            ipstr, gai_strerror(gai_error));
#endif
        return NULL;
    }

    if (MMDB_SUCCESS != mmdb_error) {
#if DEBUG
        fprintf(stderr,
            "[ERROR] Got an error from libmaxminddb: %s\n\n",
            MMDB_strerror(mmdb_error));
#endif
        return NULL;
    }

    // Parse results
    MMDB_entry_data_s entry_data;
    int exit_code = 0;

    if (result.found_entry) {
        int status = MMDB_aget_value(&result.entry, &entry_data, lookup_path);

        if (MMDB_SUCCESS != status) {
#if DEBUG
            fprintf(
                stderr,
                    "[WARN] Got an error looking up the entry data. Make sure \
 the lookup_path is correct. %s\n",
                    MMDB_strerror(status));
#endif
            exit_code = 4;
            return NULL;
        }
예제 #8
0
static VALUE geoip2_compat_lookup(VALUE self, VALUE ip)
{
  int status, gai_error, mmdb_error;
  Check_Type(ip, T_STRING);

  char* ip_addr = StringValuePtr(ip);
  MMDB_s *mmdb;
  Data_Get_Struct(self, MMDB_s, mmdb);

  MMDB_lookup_result_s result =
    MMDB_lookup_string(mmdb, ip_addr, &gai_error, &mmdb_error);

  if (mmdb_error != MMDB_SUCCESS) {
    rb_raise(egeoip2_compat_Exception,
      "geoip2_compat - lookup failed: %s", MMDB_strerror(mmdb_error)
    );
  }

  if (gai_error != 0) {
    rb_raise(egeoip2_compat_Exception,
      "geoip2_compat - getaddrinfo failed: %s", gai_strerror(gai_error)
    );
  }

  if (!result.found_entry) return Qnil;

  VALUE hash = rb_hash_new();

  geoip2_compat_lookup_internal(&result, hash, geoip2_compat_COUNTRY_CODE, "country", "iso_code", NULL);
  geoip2_compat_lookup_internal(&result, hash, geoip2_compat_COUNTRY_NAME, "country", "names", "en", NULL);
  geoip2_compat_lookup_internal(&result, hash, geoip2_compat_REGION, "subdivisions", "0", "iso_code", NULL);
  geoip2_compat_lookup_internal(&result, hash, geoip2_compat_REGION_NAME, "subdivisions", "0", "names", "en", NULL);
  geoip2_compat_lookup_internal(&result, hash, geoip2_compat_CITY, "city", "names", "en", NULL);
  geoip2_compat_lookup_internal(&result, hash, geoip2_compat_POSTAL_CODE, "postal", "code", NULL);
  geoip2_compat_lookup_internal(&result, hash, geoip2_compat_LATITUDE, "location", "latitude", NULL);
  geoip2_compat_lookup_internal(&result, hash, geoip2_compat_LONGITUDE, "location", "longitude", NULL);

  return hash;
}
예제 #9
0
static gboolean
_mmdb_load_entry_data_list(GeoIPParser *self, const gchar *input, MMDB_entry_data_list_s **entry_data_list)
{
  int _gai_error, mmdb_error;
  MMDB_lookup_result_s result =
    MMDB_lookup_string(self->database, input, &_gai_error, &mmdb_error);

  if (!result.found_entry)
    {
      mmdb_problem_to_error(_gai_error, mmdb_error, "lookup");
      return FALSE;
    }

  mmdb_error = MMDB_get_entry_data_list(&result.entry, entry_data_list);
  if (MMDB_SUCCESS != mmdb_error)
    {
      msg_debug("GeoIP2: MMDB_get_entry_data_list",
                evt_tag_str("error", MMDB_strerror(mmdb_error)));
      return FALSE;
    }
  return TRUE;
}
예제 #10
0
int main(void)
{
	int gai_error, mmdb_error;
	MMDB_s mmdb;
	char *ip = "8.8.8.8";
	int status = MMDB_open("/home/lannister/Documentos/GeoLite2-City.mmdb", MMDB_MODE_MMAP, &mmdb);

	if (MMDB_SUCCESS != status) { 
		printf("Fail WTF"); 
	}

	MMDB_lookup_result_s result = MMDB_lookup_string(&mmdb, ip, &gai_error, &mmdb_error);

	if (MMDB_SUCCESS != mmdb_error) { 
		printf("Fail WTF2"); 
	}

	int exit_code = 0;
	char *country = NULL;

	MMDB_entry_data_s entry_data;

	status = MMDB_get_value(&result.entry, &entry_data,"city","names","es", NULL);
	if (entry_data.has_data) { 
		country =  strndup(entry_data.utf8_string, entry_data.data_size);
		printf("%s\n", country);
	}else{
		printf("No city");
	}

	status = MMDB_get_value(&result.entry, &entry_data,"country","names","es", NULL);
	if (entry_data.has_data) { 
		country =  strndup(entry_data.utf8_string, entry_data.data_size);
		printf("%s\n", country);
	}

	MMDB_close(&mmdb);
 return 0;
}
예제 #11
0
	void Worker( GeoIPData *data ) {
		int error = -1, gai_error = -1;
		MMDB_lookup_result_s result =  MMDB_lookup_string( handle, data->getIp().c_str(), &gai_error, &error );
		if ( error != MMDB_SUCCESS || gai_error != 0 ) {
			std::string *str = data->getData();
			*str = MMDB_strerror( error );
			data->setStatus( 0 ); // error
			return;
		}
		if ( result.found_entry ) {
			MMDB_entry_s entry = result.entry;
			MMDB_entry_data_s entry_data;
			if ( (error = MMDB_get_value( &entry, &entry_data, "names", "en", NULL )) != MMDB_SUCCESS ) {
				std::string *str = data->getData();
				*str = MMDB_strerror( error );
				data->setStatus( 0 ); // error
				return;
			}
			if ( entry_data.has_data ) {
				std::string *str = data->getData();
				*str = entry_data.utf8_string;
				data->setStatus( 1 );
				return;
			}
			else {
				*data->getData() = "Unknown";
				data->setStatus( 1 );
				return;
			}
		}
		else {
			*data->getData() = "Unknown";
			data->setStatus( 1 );
			return;
		}
	}
예제 #12
0
void OnGeoipCommand()
{
	const auto cmd = CMD_ARGV(1);

	if (!strcmp(cmd, "version"))
	{
		if (!HandleDB.filename)
		{
			MF_PrintSrvConsole("\n  Database is not loaded.\n");
			return;
		}

		const auto meta_dump = "\n"
			"  Database metadata\n"
			"    Node count:    %i\n"
			"    Record size:   %i bits\n"
			"    IP version:    IPv%i\n"
			"    Binary format: %i.%i\n"
			"    Build epoch:   %llu (%s)\n"
			"    Type:          %s\n"
			"    Languages:     ";

		char date[40];
		strftime(date, sizeof date, "%Y-%m-%d %H:%M:%S UTC", gmtime(reinterpret_cast<const time_t *>(&HandleDB.metadata.build_epoch)));

		fprintf(stdout, meta_dump,
				HandleDB.metadata.node_count,
				HandleDB.metadata.record_size,
				HandleDB.metadata.ip_version,
				HandleDB.metadata.binary_format_major_version,
				HandleDB.metadata.binary_format_minor_version,
				HandleDB.metadata.build_epoch,
				date,
				HandleDB.metadata.database_type);

		for (size_t i = 0; i < HandleDB.metadata.languages.count; ++i)
		{
			fprintf(stdout, "%s", HandleDB.metadata.languages.names[i]);

			if (i < HandleDB.metadata.languages.count - 1)
			{
				fprintf(stdout, " ");
			}
		}

		fprintf(stdout, "\n");
		fprintf(stdout, "    Description:\n");

		for (size_t i = 0; i < HandleDB.metadata.description.count; ++i)
		{
			fprintf(stdout, "      %s:   %s\n",
					HandleDB.metadata.description.descriptions[i]->language,
					HandleDB.metadata.description.descriptions[i]->description);
		}
		fprintf(stdout, "\n");
	}
	else if (!strcmp(cmd, "dump"))
	{
		if (!HandleDB.filename)
		{
			MF_PrintSrvConsole("\n  Database is not loaded.\n\n");
			return;
		}

		const auto num_args = CMD_ARGC();

		if (num_args < 3)
		{
			MF_PrintSrvConsole("\n  An IP address must be provided.\n\n");
			return;
		}

		const auto ip = stripPort(const_cast<char *>(CMD_ARGV(2)));

		auto gai_error = 0;
		auto mmdb_error = 0;

		auto result = MMDB_lookup_string(&HandleDB, ip, &gai_error, &mmdb_error);

		if (gai_error != 0 || mmdb_error != MMDB_SUCCESS || !result.found_entry)
		{
			MF_PrintSrvConsole("\n  Either look up failed or no found result.\n\n");
			return;
		}

		MMDB_entry_data_list_s *entry_data_list = nullptr;
		int status;

		if ((status = MMDB_get_entry_data_list(&result.entry, &entry_data_list)) != MMDB_SUCCESS || entry_data_list == nullptr)
		{
			MF_PrintSrvConsole("\n  Could not retrieve data list - %s.\n\n", MMDB_strerror(status));
			return;
		}

		const char *file = nullptr;
		FILE *fp = nullptr;

		if (num_args > 3)
		{
			file = CMD_ARGV(3);
			fp = fopen(MF_BuildPathname("%s", file), "w");
		}

		if (!fp)
		{
			file = nullptr;
			fp = stdout;
		}

		fprintf(fp, "\n");
		MMDB_dump_entry_data_list(fp, entry_data_list, 2);
		fprintf(fp, "\n");

		if (file)
		{
			fclose(fp);
		}

		MMDB_free_entry_data_list(entry_data_list);
	}
	else if (!strcmp(cmd, "reload"))
	{
		const auto isDatabaseLoaded = HandleDB.filename != nullptr;

		if (isDatabaseLoaded)
		{
			MMDB_close(&HandleDB);
		}

		if (loadDatabase() && !NativesRegistered)
		{
			MF_AddNatives(GeoipNatives);

			NativesRegistered = true;
		}
	}
	else
	{
		MF_PrintSrvConsole("\n");
		MF_PrintSrvConsole("  Usage: geoip <command> [argument]\n");
		MF_PrintSrvConsole("  Commands:\n");
		MF_PrintSrvConsole("     version                 - display geoip database metadata\n");
		MF_PrintSrvConsole("     reload                  - reload geoip database\n");
		MF_PrintSrvConsole("     dump <ip> [output file] - dump all data from an IP address formatted in a JSON-ish fashion.\n");
		MF_PrintSrvConsole("                               An output file is mod-based and if not provided, it will print in the console.\n");
		MF_PrintSrvConsole("\n");
	}
}
예제 #13
0
CAMLprim value mmdb_ml_lookup_path(value ip, value query_list, value mmdb)
{
  CAMLparam3(ip, query_list, mmdb);
  CAMLlocal3(iter_count, caml_clean_result, query_r);

  int total_len = 0, copy_count = 0, gai_error = 0, mmdb_error = 0;
  char *clean_result;
  long int int_result;

  iter_count = query_list;

  unsigned int len = caml_string_length(ip);
  char *as_string = caml_strdup(String_val(ip));
  if (strlen(as_string) != (size_t)len) {
    caml_failwith("Could not copy IP address properly");
  }

  MMDB_s *as_mmdb = (MMDB_s*)Data_custom_val(mmdb);
  MMDB_lookup_result_s *result = caml_stat_alloc(sizeof(*result));
  *result = MMDB_lookup_string(as_mmdb, as_string, &gai_error, &mmdb_error);
  check_error(gai_error, mmdb_error);
  caml_stat_free(as_string);

  while (iter_count != Val_emptylist) {
    total_len++;
    iter_count = Field(iter_count, 1);
  }

  char **query = caml_stat_alloc(sizeof(char *) * (total_len + 1));

  while (query_list != Val_emptylist) {
    query[copy_count] = caml_strdup(String_val(Field(query_list, 0)));
    copy_count++;
    query_list = Field(query_list, 1);
  }
  query[total_len] = NULL;
  MMDB_entry_data_s entry_data;

  int status = MMDB_aget_value(&result->entry,
			       &entry_data,
			       (const char *const *const)query);
  check_status(status);
  check_data(entry_data);
  caml_stat_free(result);
  for (int i = 0; i < copy_count; caml_stat_free(query[i]), i++);
  caml_stat_free(query);
  query_r = caml_alloc(2, 0);
  as_mmdb = NULL;
  switch (entry_data.type) {
  case MMDB_DATA_TYPE_BYTES:
    clean_result = caml_stat_alloc(entry_data.data_size + 1);
    memcpy(clean_result, entry_data.bytes, entry_data.data_size);
    caml_clean_result = caml_copy_string(clean_result);
    caml_stat_free(clean_result);
    goto string_finish;

  case MMDB_DATA_TYPE_UTF8_STRING:
    clean_result = strndup(entry_data.utf8_string, entry_data.data_size);
    caml_clean_result = caml_copy_string(clean_result);
    free(clean_result);
    goto string_finish;

  case MMDB_DATA_TYPE_FLOAT:
    Store_field(query_r, 0, polymorphic_variants.poly_float);
    Store_field(query_r, 1, caml_copy_double(entry_data.float_value));
    goto finish;

  case MMDB_DATA_TYPE_BOOLEAN:
    Store_field(query_r, 0, polymorphic_variants.poly_bool);
    Store_field(query_r, 1, Val_true ? entry_data.boolean : Val_false);
    goto finish;

  case MMDB_DATA_TYPE_DOUBLE:
    Store_field(query_r, 0, polymorphic_variants.poly_float);
    Store_field(query_r, 1, caml_copy_double(entry_data.double_value));
    goto finish;

  case MMDB_DATA_TYPE_UINT16:
    Store_field(query_r, 0, polymorphic_variants.poly_int);
    int_result = Val_long(entry_data.uint16);
    goto int_finish;

  case MMDB_DATA_TYPE_UINT32:
    Store_field(query_r, 0, polymorphic_variants.poly_int);
    int_result = Val_long(entry_data.uint32);
    goto int_finish;

  case MMDB_DATA_TYPE_UINT64:
    Store_field(query_r, 0, polymorphic_variants.poly_int);
    int_result = Val_long(entry_data.uint32);
    goto int_finish;

    // look at /usr/bin/sed -n 1380,1430p src/maxminddb.c
  case MMDB_DATA_TYPE_ARRAY:
  case MMDB_DATA_TYPE_MAP:
    caml_failwith("Can't return a Map or Array yet");
  }

 string_finish:
  Store_field(query_r, 0, polymorphic_variants.poly_string);
  Store_field(query_r, 1, caml_clean_result);
  CAMLreturn(query_r);

 int_finish:
  Store_field(query_r, 1, int_result);
  CAMLreturn(query_r);

 finish:
  CAMLreturn(query_r);
}
예제 #14
0
static PyObject *Reader_get(PyObject *self, PyObject *args)
{
    char *ip_address = NULL;

    Reader_obj *mmdb_obj = (Reader_obj *)self;
    if (!PyArg_ParseTuple(args, "s", &ip_address)) {
        return NULL;
    }

    MMDB_s *mmdb = mmdb_obj->mmdb;

    if (NULL == mmdb) {
        PyErr_SetString(PyExc_ValueError,
                        "Attempt to read from a closed MaxMind DB.");
        return NULL;
    }

    int gai_error = 0;
    int mmdb_error = MMDB_SUCCESS;
    MMDB_lookup_result_s result =
        MMDB_lookup_string(mmdb, ip_address, &gai_error,
                           &mmdb_error);

    if (0 != gai_error) {
        PyErr_Format(PyExc_ValueError,
                     "'%s' does not appear to be an IPv4 or IPv6 address.",
                     ip_address);
        return NULL;
    }

    if (MMDB_SUCCESS != mmdb_error) {
        PyObject *exception;
        if (MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR == mmdb_error) {
            exception = PyExc_ValueError;
        } else {
            exception = MaxMindDB_error;
        }
        PyErr_Format(exception, "Error looking up %s. %s",
                     ip_address, MMDB_strerror(mmdb_error));
        return NULL;
    }

    if (!result.found_entry) {
        Py_RETURN_NONE;
    }

    MMDB_entry_data_list_s *entry_data_list = NULL;
    int status = MMDB_get_entry_data_list(&result.entry, &entry_data_list);
    if (MMDB_SUCCESS != status) {
        PyErr_Format(MaxMindDB_error,
                     "Error while looking up data for %s. %s",
                     ip_address, MMDB_strerror(status));
        MMDB_free_entry_data_list(entry_data_list);
        return NULL;
    }

    MMDB_entry_data_list_s *original_entry_data_list = entry_data_list;
    PyObject *py_obj = from_entry_data_list(&entry_data_list);
    MMDB_free_entry_data_list(original_entry_data_list);
    return py_obj;
}