Beispiel #1
0
/** Initialize GeoIP global states.
 */
void geoip_init(void)
{
#ifdef USE_GEOIP
  if (gi4 != NULL)
    GeoIP_delete(gi4);
  if (gi6 != NULL)
    GeoIP_delete(gi6);
  gi4 = NULL;
  gi6 = NULL;
#endif /* USE_GEOIP */

  if (!feature_bool(FEAT_GEOIP_ENABLE))
    return;

#ifdef USE_GEOIP
  /* Load IPv4 GeoIP database */
  if (feature_str(FEAT_GEOIP_FILE)) {
    gi4 = GeoIP_open(feature_str(FEAT_GEOIP_FILE), GEOIP_STANDARD);
  }

  /* Try to load GeoIP.dat from lib/ if FEAT_GEOIP_FILE was not loaded. */
  if (gi4 == NULL)
    gi4 = GeoIP_open_type(GEOIP_COUNTRY_EDITION, GEOIP_STANDARD);

  /* Load IPv6 GeoIP database */
  if (feature_str(FEAT_GEOIP_IPV6_FILE)) {
    gi6 = GeoIP_open(feature_str(FEAT_GEOIP_IPV6_FILE), GEOIP_STANDARD);
  }

  /* Try to load GeoIPv6.dat from lib/ if FEAT_GEOIP_IPV6_FILE was not loaded. */
  if (gi6 == NULL)
    gi6 = GeoIP_open_type(GEOIP_COUNTRY_EDITION_V6, GEOIP_STANDARD);
#endif /* USE_GEOIP */
}
Beispiel #2
0
void
set_geoip_info(struct userNode *user)
{
    if(IsLocal(user))
        return;
/* Need the libs and the headers if this is going to compile properly */
#if defined(HAVE_LIBGEOIP)&&defined(HAVE_GEOIP_H)&&defined(HAVE_GEOIPCITY_H)
    GeoIPRecord * gir;
    const char *geoip_data_file = NULL;
    const char *geoip_city_file = NULL;

    geoip_data_file = conf_get_data("services/opserv/geoip_data_file", RECDB_QSTRING);
    geoip_city_file = conf_get_data("services/opserv/geoip_city_data_file", RECDB_QSTRING);

    if ((!geoip_data_file && !geoip_city_file))
        return; /* Admin doesnt want to use geoip functions */

    if (geoip_data_file && !gi)
        gi  = GeoIP_open(geoip_data_file, GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE);

    if (geoip_city_file && !cgi)
        cgi = GeoIP_open(geoip_city_file, GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE);

    if (cgi) {
        gir = GeoIP_record_by_name(cgi, user->hostname);
        if (gir) {
            user->country_name = strdup(gir->country_name ? gir->country_name : "");
            user->country_code = strdup(gir->country_code ? gir->country_code : "");
            user->city         = strdup(gir->city ? gir->city : "");
            user->region       = strdup(gir->region ? gir->region : "");
            user->postal_code  = strdup(gir->postal_code ? gir->postal_code : "");

            user->latitude  = gir->latitude ? gir->latitude : 0;
            user->longitude = gir->longitude ? gir->longitude : 0;
            user->dma_code  = gir->dma_code ? gir->dma_code : 0;
            user->area_code = gir->area_code ? gir->area_code : 0;

            GeoIPRecord_delete(gir);
        }

        return;
    } else if (gi) {
        const char *country = GeoIP_country_name_by_name(gi, user->hostname);
        user->country_name = strdup(country ? country : "");
        return;
    }

    return;
#endif
}
Beispiel #3
0
int
init_function(struct vmod_priv *pp, const struct VCL_conf *conf)
{
    pp->priv = malloc(sizeof(struct GeoIP_databases));
    if (!pp->priv)
        return 1;
    struct GeoIP_databases* db = (struct GeoIP_databases*)pp->priv;

    db->country = GeoIP_new(GEOIP_MMAP_CACHE);
    db->org = GeoIP_open(GeoIPDBFileName[GEOIP_ORG_EDITION], GEOIP_MMAP_CACHE);
    db->region = GeoIP_open(GeoIPDBFileName[GEOIP_REGION_EDITION_REV1], GEOIP_MMAP_CACHE);

    pp->free = &free_databases;
    return (0);
}
Beispiel #4
0
static PyObject *GeoIP_open_Py(PyObject * self, PyObject * args)
{
    GeoIP_GeoIPObject *GeoIP;
    char *filename;
    int flags;

    if (!PyArg_ParseTuple(args, "si", &filename, &flags)) {
        return NULL;
    }

    GeoIP = PyObject_New(GeoIP_GeoIPObject, &GeoIP_GeoIPType);

    if (!GeoIP)
        return NULL;

    GeoIP->gi = GeoIP_open(filename, flags);

    if (!GeoIP->gi) {
        PyErr_SetString(PyGeoIPError, "Can't create GeoIP->gi object");
        Py_DECREF(GeoIP);
        return NULL;
    }

    return (PyObject *) GeoIP;
}
Beispiel #5
0
int
esgf_geolookup (char *hostname, struct geo_output_struct *geo_output)
{
  char *db_info;
  GeoIP *gi;
  int i, ret_code;
  char *custom_directory = NULL;
  char *custom_file = NULL;
  char geoipdat[1024] = { '\0' };

  sprintf (geoipdat, "%s/share/GeoIP/%s", GEOIP_DATA_PATH, GEOIPIDATABASE);

  pmesg(LOG_DEBUG,__FILE__,__LINE__,"Loading GeoLiteCity from %s...\n",geoipdat);
  if (gi = GeoIP_open (geoipdat, GEOIP_STANDARD))
    {
      i = GeoIP_database_edition (gi);
      pmesg(LOG_DEBUG,__FILE__,__LINE__," GeoIP database found [Ok]\n");
      if (ret_code = geoiplookup (gi, hostname, i, geo_output))
	{
	GeoIP_delete (gi);
	return ret_code;
	}
    }
  else
    {
      pmesg(LOG_ERROR,__FILE__,__LINE__,"%s not available, skipping.Please check ! [Exit]\n",geoipdat);
      GeoIP_delete (gi);
      return (-4);
    }

  GeoIP_delete (gi);
  pmesg(LOG_DEBUG,__FILE__,__LINE__,"Geoiplookup ok [code=%d]\n", ret_code);
  return 0;
}
Beispiel #6
0
/* Generic initializer */
static VALUE rb_geoip_database_new(VALUE mGeoIP_Database_Class, int argc, VALUE *argv, VALUE self)
{
  GeoIP *gi;
  VALUE database = Qnil;
  VALUE filename, load_option = Qnil, check_cache = Qnil;
  int flag;

  rb_scan_args(argc, argv, "12", &filename, &load_option, &check_cache);
  if(NIL_P(load_option))
    load_option = rb_geoip_memory;
  if(NIL_P(check_cache))
    check_cache = Qfalse;
  Check_Type(load_option, T_SYMBOL);

  if(flag = check_load_option(load_option)) flag;

  if(RTEST(check_cache)) flag |= GEOIP_CHECK_CACHE;

  if(gi = GeoIP_open(StringValuePtr(filename), flag)) {
    database = Data_Wrap_Struct(mGeoIP_Database_Class, 0, GeoIP_delete, gi);
    rb_obj_call_init(database, 0, 0);
  } else {
    rb_sys_fail("Problem opening database");
  }
  return database;
}
int main(int argc, char *argv[])
{
    FILE *f;
    GeoIP *gi;
    char *org;
    char host[50];

    gi = GeoIP_open("../data/GeoIPISP.dat", GEOIP_STANDARD);

    if (gi == NULL) {
        fprintf(stderr, "Error opening database\n");
        exit(1);
    }

    f = fopen("isp_test.txt", "r");

    if (f == NULL) {
        fprintf(stderr, "Error opening isp_test.txt\n");
        exit(1);
    }

    while (fscanf(f, "%s", host) != EOF) {
        org = GeoIP_org_by_name(gi, (const char *)host);

        if (org != NULL) {
            printf("%s\t%s\n", host, _mk_NA(org));
            free(org);
        }
    }

    fclose(f);
    GeoIP_delete(gi);

    return 0;
}
static void loadGeoIP(void) {
  int i;
  struct stat statbuf;

  /* Initialize GeoIP databases */
  for(i=0; myGlobals.configFileDirs[i] != NULL; i++) {
    char path[256];
    
    safe_snprintf(__FILE__, __LINE__, path, sizeof(path),
		  "%s%c%s",
		  myGlobals.configFileDirs[i], 
		  CONST_PATH_SEP, GEO_IP_FILE);
    revertSlashIfWIN32(path, 0);

    if(stat(path, &statbuf) == 0) {
      if((myGlobals.geo_ip_db = GeoIP_open(path, GEOIP_CHECK_CACHE)) != NULL) {
	traceEvent(CONST_TRACE_INFO, "GeoIP: loaded config file %s", path);
	break;
      }
    }
  }
  
  if(myGlobals.geo_ip_db == NULL)
    traceEvent(CONST_TRACE_ERROR, "GeoIP: unable to load file %s", GEO_IP_FILE);
  
  /* *************************** */

  for(i=0; myGlobals.configFileDirs[i] != NULL; i++) {
    char path[256];
    
    safe_snprintf(__FILE__, __LINE__, path, sizeof(path),
		  "%s%c%s",
		  myGlobals.configFileDirs[i], 
		  CONST_PATH_SEP, GEO_IP_ASN_FILE);
    revertSlashIfWIN32(path, 0);

    if(stat(path, &statbuf) == 0) {
      if((myGlobals.geo_ip_asn_db = GeoIP_open(path, GEOIP_CHECK_CACHE)) != NULL) {
	traceEvent(CONST_TRACE_INFO, "GeoIP: loaded ASN config file %s", path);
	break;
      }
    }
  }
  
  if(myGlobals.geo_ip_asn_db == NULL)
    traceEvent(CONST_TRACE_ERROR, "GeoIP: unable to load ASN file %s", GEO_IP_ASN_FILE);  
}
static char *
ngx_stream_geoip_org(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    ngx_stream_geoip_conf_t  *gcf = conf;

    ngx_str_t  *value;

    if (gcf->org) {
        return "is duplicate";
    }

    value = cf->args->elts;

    gcf->org = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE);

    if (gcf->org == NULL) {
        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                           "GeoIP_open(\"%V\") failed", &value[1]);

        return NGX_CONF_ERROR;
    }

    if (cf->args->nelts == 3) {
        if (ngx_strcmp(value[2].data, "utf8") == 0) {
            GeoIP_set_charset(gcf->org, GEOIP_CHARSET_UTF8);

        } else {
            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                               "invalid parameter \"%V\"", &value[2]);
            return NGX_CONF_ERROR;
        }
    }

    switch (gcf->org->databaseType) {

    case GEOIP_ISP_EDITION:
    case GEOIP_ORG_EDITION:
    case GEOIP_DOMAIN_EDITION:
    case GEOIP_ASNUM_EDITION:

        return NGX_CONF_OK;

#if (NGX_HAVE_GEOIP_V6)
    case GEOIP_ISP_EDITION_V6:
    case GEOIP_ORG_EDITION_V6:
    case GEOIP_DOMAIN_EDITION_V6:
    case GEOIP_ASNUM_EDITION_V6:

        gcf->org_v6 = 1;
        return NGX_CONF_OK;
#endif

    default:
        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                           "invalid GeoIP database \"%V\" type:%d",
                           &value[1], gcf->org->databaseType);
        return NGX_CONF_ERROR;
    }
}
Beispiel #10
0
static ib_status_t geoip_database_file_dir_param1(ib_cfgparser_t *cp,
        const char *name,
        const char *p1,
        void *cbdata)
{
    IB_FTRACE_INIT();

    assert(cp!=NULL);
    assert(name!=NULL);
    assert(p1!=NULL);

    ib_status_t rc;
    size_t p1_len = strlen(p1);
    size_t p1_unescaped_len;
    char *p1_unescaped = malloc(p1_len+1);

    if ( p1_unescaped == NULL ) {
        IB_FTRACE_RET_STATUS(IB_EALLOC);
    }

    rc = ib_util_unescape_string(p1_unescaped,
                                 &p1_unescaped_len,
                                 p1,
                                 p1_len,
                                 IB_UTIL_UNESCAPE_NULTERMINATE |
                                 IB_UTIL_UNESCAPE_NONULL);

    if (rc != IB_OK ) {
        const char *msg = ( rc == IB_EBADVAL )?
                          "GeoIP Database File \"%s\" contains nulls." :
                          "GeoIP Database File \"%s\" is an invalid string.";

        ib_log_debug(cp->ib, msg, p1);
        free(p1_unescaped);
        IB_FTRACE_RET_STATUS(rc);
    }

    if (geoip_db != NULL)
    {
        GeoIP_delete(geoip_db);
        geoip_db = NULL;
    }

    IB_FTRACE_MSG("Initializing custom GeoIP database...");
    IB_FTRACE_MSG(p1_unescaped);

    geoip_db = GeoIP_open(p1_unescaped, GEOIP_MMAP_CACHE);

    free(p1_unescaped);

    if (geoip_db == NULL)
    {
        IB_FTRACE_MSG("Failed to initialize GeoIP database.");
        IB_FTRACE_RET_STATUS(IB_EUNKNOWN);
    }

    IB_FTRACE_RET_STATUS(IB_OK);
}
Beispiel #11
0
int main(int argc, char **argv) {
	const char *geoipfilename = DEFAULT_GEOIPFILE;
	GeoIP *gip = NULL;
	GeoIPRecord *gir = NULL;
	char *name;
	const char *region_name;
	int edition, i;

	if ((argc != 2) && (argc != 4)) {
		printf("Usage: geoiplookup_city [-f custom_file] <hostname_or_ip>\n");
		return 1;
	}

	i = 1;
	while (i < argc) {
                if (strcmp(argv[i],"-f") == 0) {
                        if ((i+1) < argc){
                                i++;
                                geoipfilename = argv[i];
                        }
                } else {
			name = argv[i];
		}
                i++;
        }

	gip = GeoIP_open(geoipfilename, GEOIP_STANDARD);
	edition = GeoIP_database_edition(gip);

	if (edition == GEOIP_COUNTRY_EDITION) {
		short int country_id = GeoIP_country_id_by_name(gip, name);
		region_name = GeoIP_country_continent[country_id];
		printf("%s\n", region_name);
        } else if ((edition == GEOIP_CITY_EDITION_REV0) || 
		   (edition == GEOIP_CITY_EDITION_REV1)) {
                gir = GeoIP_record_by_name(gip, name);
                if (NULL == gir) {
                        printf("%s: IP Address not found\n", GeoIPDBDescription[edition]);
			return 1;
                } else {
                        printf("Continent: %s\n"
			       "Country:   %s\n"
			       "Region id: %s\n"
			       "Region:    %s\n"
			       "City:      %s\n"
			       "Latitude:  %f\n"
			       "Longitude: %f\n", gir->continent_code, 
			       			  gir->country_code, 
						  gir->region, 
						  GeoIP_region_name_by_code(gir->country_code, gir->region),
						  gir->city, 
						  gir->latitude, 
						  gir->longitude);
		}
	}
 
	return 0;
}
Beispiel #12
0
/* Use the GeoIP API to locate an IP address
 */
char *GeoIPLookup(char *ip)
{
	GeoIP	*gi;
	GeoIPRecord	*gir;
	char buffer[OS_SIZE_1024 +1];
        unsigned long longip;

	/* Dumb way to detect an IPv6 address */
	if (strchr(ip, ':')) {
		/* Use the IPv6 DB */
		gi = GeoIP_open(Config.geoip_db_path, GEOIP_INDEX_CACHE);
		if (gi == NULL) {
			merror(INVALID_GEOIP_DB, ARGV0, Config.geoip6_db_path);
			return("Unknown");
		}
		gir = GeoIP_record_by_name_v6(gi, (const char *)ip);
	}
	else {
		/* Use the IPv4 DB */
                /* If we have a RFC1918 IP, do not perform a DB lookup (performance) */
                longip = StrIP2Int(ip);
                if (longip == 0 ) return("Unknown");
                if ((longip & NETMASK_8)  == RFC1918_10 ||
                    (longip & NETMASK_12) == RFC1918_172 ||
                    (longip & NETMASK_16) == RFC1918_192) return("");

		gi = GeoIP_open(Config.geoip_db_path, GEOIP_INDEX_CACHE);
		if (gi == NULL) {
			merror(INVALID_GEOIP_DB, ARGV0, Config.geoip_db_path);
			return("Unknown");
		}
		gir = GeoIP_record_by_name(gi, (const char *)ip);
	}
	if (gir != NULL) {
		sprintf(buffer,"%s,%s,%s",
				_mk_NA(gir->country_code),
				_mk_NA(GeoIP_region_name_by_code(gir->country_code, gir->region)),
				_mk_NA(gir->city)
		);
		GeoIP_delete(gi);
		return(buffer);
	}
	GeoIP_delete(gi);
	return("Unknown");
}
Beispiel #13
0
/** Возвращает объект GeoIP для определения города по ip.

    При первом вызове пытается открыть файл с базой данных, указанный в
    параметре ``filename``. Если ``filename`` не задан
 */
GeoIP *GeoCity(const char *filename)
{
    static GeoIP *geo = 0;
    static bool open_failed = false;

    if (open_failed)
        return 0;
    if (geo)
        return geo;

    if (filename)
        geo = GeoIP_open(filename, GEOIP_MEMORY_CACHE);
    else
        geo = GeoIP_open("/usr/share/GeoIP/GeoLiteCity.dat",
                         GEOIP_MEMORY_CACHE);
    open_failed = geo? false : true;
    return geo;
}
Beispiel #14
0
static int _tag_attr(char *tagname, char *attrname, char *attrvalue, XML_CONFIG *config) {
	struct rdb_options_s *options = ((RDB *) config->backptr)->options;

	if (strcasecmp(config->xpath, "/nnodes/options/listen") == 0) {
		if (strcasecmp(attrname, "port") == 0 && attrvalue) {
			curr_port = atoi(attrvalue);
		}
		else if (strcasecmp(attrname, "addr") == 0 && attrvalue) {
			curr_addr = strdup(attrvalue);	
		}
	} else if (strcasecmp(config->xpath, "/nnodes/options/user") == 0) {
		if (strcasecmp(attrname, "uid") == 0 && attrvalue) {
			options->uid = atoi(attrvalue);
		} else if (strcasecmp(attrname, "gid") == 0 && attrvalue) {
			options->gid = atoi(attrvalue);
		}
	} else if (strcasecmp(config->xpath, "/nnodes/options/pid") == 0) {
		if (strcasecmp(attrname, "file") == 0 && attrvalue) {
			options->pid = strdup(attrvalue);
		}
	} else if (strcasecmp(config->xpath, "/nnodes/options/threads") == 0) {
		if (strcasecmp(attrname, "value") == 0 && attrvalue) {
			options->nthreads = atoi(attrvalue);
		}
	} else if (strcasecmp(config->xpath, "/nnodes/options/log") == 0) {
		if (strcasecmp(attrname, "file") == 0 && attrvalue) {
			options->log_file = strdup(attrvalue);
		}
		else if (strcasecmp(attrname, "level") == 0 && attrvalue) {
			options->log_level = atoi(attrvalue);
		}
		else if (strcasecmp(attrname, "maxfile") == 0 && attrvalue) {
			options->log_maxfile = atoi(attrvalue);
		}
	} else if (strcasecmp(config->xpath, "/nnodes/options/websrv") == 0) {
		if (strcasecmp(attrname, "xslt") == 0 && attrvalue) {
			options->websrv_xslt = strdup(attrvalue);
		}
		else if (strcasecmp(attrname, "port") == 0 && attrvalue) {
			options->websrv_port = atoi(attrvalue);
		}
	} else if (strcasecmp(config->xpath, "/nnodes/options/geoip") == 0) {
#ifdef HAVE_GEOIP
		if (strcasecmp(attrname, "database") == 0 && attrvalue) {
			if ((options->gi = GeoIP_open(attrvalue, GEOIP_MEMORY_CACHE)) == NULL) {
				LOG_ERR("error opening database %s", attrvalue);
				return -1;
			}
		}
#else
		LOG_WARN ("%s\n", "not compiled with geoip");
#endif

	}

	return 0;
}
Beispiel #15
0
db_t *ipdata_dbinit()
{
  db_t *dbp = MALLOCORDIE(sizeof(db_t), "ipdata_dbinit()");

  if(opts.flags & NOAS) /* AS db not requested. */
    dbp->asgp = NULL;
  else /* AS db requested. */
  {
    if(!opts.asdb) /* No db name specified. */
    {
      if(opts.family == AF_INET6) opts.asdb = ASDB_V6_DEFAULT;
      else opts.asdb = ASDB_V4_DEFAULT;
    }

    dbp->asgp = GeoIP_open(opts.asdb, GEOIP_INDEX_CACHE);
    if(!dbp->asgp)
    {
      fputs("ASNum DB requested but not found! "
            "Proceeding without it.\n", stderr);
      opts.flags |= NOAS;
    }
  }

  if(opts.flags & NOCITY) /* City db not requested. */
    dbp->citygp = NULL;
  else /* City db requested. */
  {
    if(!opts.citydb) /* No db name specified. */
    {
      if(opts.family == AF_INET6) opts.citydb = CITYDB_V6_DEFAULT;
      else opts.citydb = CITYDB_V4_DEFAULT;
    }

    dbp->citygp = GeoIP_open(opts.citydb, GEOIP_INDEX_CACHE);
    if(!dbp->citygp)
    {
      fputs("City DB requested but not found! "
            "Proceeding without it.\n", stderr);
      opts.flags |= NOCITY;
    }
  }

  return dbp;
}
Beispiel #16
0
wxString LoadDatabase(const wxString& path, GeoIPDBTypes type, GeoIPOptions options)
{
    if (::wxFileExists(path) && (g_geoIP = GeoIP_open(U2C(path), options)))
    {
        CSL_LOG_DEBUG("loaded GeoIP database: %s\n", U2C(path));
        g_geoIP->charset = GEOIP_CHARSET_UTF8;
    }

    return g_geoIP ? path : wxT("");
}
Beispiel #17
0
/* Geolocation data */
GeoIP *
geoip_open_db (const char *db)
{
  GeoIP *geoip;
  geoip = GeoIP_open (db, GEOIP_INDEX_CACHE);
  if (geoip == NULL)
    FATAL ("Unable to open GeoIP City database: %s\n", db);
  LOG_DEBUG (("Opened GeoIP City database: %s\n", db));

  return geoip;
}
Beispiel #18
0
int geoip_init_pv(char *path)
{
	_handle_GeoIP = GeoIP_open(path, GEOIP_MMAP_CACHE);

	if(_handle_GeoIP==NULL)
	{
		LM_ERR("cannot open GeoIP database file at: %s\n", path);
		return -1;
	}
	return 0;
}
Beispiel #19
0
static GeoIP *NET_GeoIP_LoadData (int db)
{
	GeoIP *data = GeoIP_open_type (db, GEOIP_MEMORY_CACHE);

	if (data == nullptr)
	{
		if (db == GEOIP_COUNTRY_EDITION)
		{
			std::string dbPath = FS::Path::Build(FS::GetLibPath(), "GeoIP.dat");
			data = GeoIP_open (dbPath.c_str(), GEOIP_MEMORY_CACHE);
		}
		else if (db == GEOIP_COUNTRY_EDITION_V6)
		{
			std::string dbPath = FS::Path::Build(FS::GetLibPath(), "GeoIPv6.dat");
			data = GeoIP_open (dbPath.c_str(), GEOIP_MEMORY_CACHE);
		}
	}

	return data;
}
Beispiel #20
0
int main(int argc, char *argv[])
{
    FILE *f;
    GeoIP *gi;
    GeoIPRecord *gir;
    int generate = 0;
    char host[50];
    const char *time_zone = NULL;
    char **ret;
    if (argc == 2) {
        if (!strcmp(argv[1], "gen")) {
            generate = 1;
        }
    }

    gi = GeoIP_open("../data/GeoIPCity.dat", GEOIP_INDEX_CACHE);

    if (gi == NULL) {
        fprintf(stderr, "Error opening database\n");
        exit(1);
    }

    f = fopen("city_test.txt", "r");

    if (f == NULL) {
        fprintf(stderr, "Error opening city_test.txt\n");
        exit(1);
    }

    while (fscanf(f, "%s", host) != EOF) {
        gir = GeoIP_record_by_name(gi, (const char *)host);

        if (gir != NULL) {
            ret = GeoIP_range_by_ip(gi, (const char *)host);
            time_zone =
                GeoIP_time_zone_by_country_and_region(gir->country_code,
                                                      gir->region);
            printf("%s\t%s\t%s\t%s\t%s\t%s\t%f\t%f\t%d\t%d\t%s\t%s\t%s\n", host,
                   _mk_NA(gir->country_code), _mk_NA(gir->region),
                   _mk_NA(GeoIP_region_name_by_code
                              (gir->country_code,
                              gir->region)), _mk_NA(gir->city),
                   _mk_NA(gir->postal_code), gir->latitude, gir->longitude,
                   gir->metro_code, gir->area_code, _mk_NA(time_zone), ret[0],
                   ret[1]);
            GeoIP_range_by_ip_delete(ret);
            GeoIPRecord_delete(gir);
        }
    }
    GeoIP_delete(gi);
    fclose(f);
    return 0;
}
Beispiel #21
0
static ib_status_t geoip_database_file_dir_param1(ib_cfgparser_t *cp,
                                                  const char *name,
                                                  const char *p1,
                                                  void *cbdata)
{
    assert(cp!=NULL);
    assert(name!=NULL);
    assert(p1!=NULL);

    ib_status_t rc;
    size_t p1_len = strlen(p1);
    size_t p1_unescaped_len;
    char *p1_unescaped = malloc(p1_len+1);

    if ( p1_unescaped == NULL ) {
        return IB_EALLOC;
    }

    rc = ib_util_unescape_string(p1_unescaped,
                                 &p1_unescaped_len,
                                 p1,
                                 p1_len,
                                 IB_UTIL_UNESCAPE_NULTERMINATE |
                                 IB_UTIL_UNESCAPE_NONULL);

    if (rc != IB_OK ) {
        const char *msg = ( rc == IB_EBADVAL )?
                        "GeoIP Database File \"%s\" contains nulls." :
                        "GeoIP Database File \"%s\" is an invalid string.";

        ib_log_debug(cp->ib, msg, p1);
        free(p1_unescaped);
        return rc;
    }

    if (geoip_db != NULL)
    {
        GeoIP_delete(geoip_db);
        geoip_db = NULL;
    }

            geoip_db = GeoIP_open(p1_unescaped, GEOIP_MMAP_CACHE);

    free(p1_unescaped);

    if (geoip_db == NULL)
    {
                return IB_EUNKNOWN;
    }

    return IB_OK;
}
Beispiel #22
0
/* Open the given GeoLocation database and set its charset.
 *
 * On error, it aborts.
 * On success, a new geolocation structure is returned. */
GeoIP *
geoip_open_db (const char *db)
{
  GeoIP *geoip;
  geoip = GeoIP_open (db, GEOIP_MEMORY_CACHE);

  if (geoip == NULL)
    FATAL ("Unable to open GeoIP database: %s\n", db);

  GeoIP_set_charset (geoip, GEOIP_CHARSET_UTF8);
  LOG_DEBUG (("Opened GeoIP City database: %s\n", db));

  return geoip;
}
Beispiel #23
0
int main(void)
{
      const char *ipAddress = "193.140.74.29";
      char expectedCountry[3];
      const char *returnedCountry;
      GeoIP *gi;
      int i;
 
      if (0 == i) {
	  /* Read from filesystem, check for updated file */
	  gi = GeoIP_open("/usr/share/GeoIP/GeoIP.dat",  GEOIP_STANDARD | GEOIP_CHECK_CACHE);
      } else {
	  /* Read from memory, faster but takes up more memory */
	  gi = GeoIP_open("/usr/share/GeoIP/GeoIP.dat", GEOIP_MEMORY_CACHE);
      }

      if (gi == NULL) {
	fprintf(stderr, "Error opening database\n");
	exit(1);
      }
  
      returnedCountry = GeoIP_country_code_by_addr(gi, NULL);
      if (returnedCountry != NULL) {
	  fprintf(stderr, "Invalid Query test failed, got non NULL, expected NULL\n");
	  exit(EXIT_FAILURE);  
      }
  
      returnedCountry = GeoIP_country_code_by_name(gi, NULL);
      if (returnedCountry != NULL) {
	fprintf(stderr,"Invalid Query test failed, got non NULL, expected NULL\n");
      }
  
      returnedCountry = GeoIP_country_code3_by_addr(gi, ipAddress);
      printf("%s => %s\n", ipAddress, returnedCountry);
      
      return 0;
}
Beispiel #24
0
void CIP2Country::Enable()
{
	Disable();

	if (m_CountryDataMap.empty()) {
		LoadFlags();
	}

	if (!CPath::FileExists(m_DataBasePath)) {
		Update();
		return;
	}

	m_geoip = GeoIP_open(unicode2char(m_DataBasePath), GEOIP_STANDARD);
}
static char *
ngx_http_geoip_country(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    ngx_http_geoip_conf_t  *gcf = conf;

    ngx_str_t  *value;

    if (gcf->country) {
        return "is duplicate";
    }

    value = cf->args->elts;

    gcf->country = GeoIP_open((char *) value[1].data, GEOIP_MEMORY_CACHE);

    if (gcf->country == NULL) {
        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                           "GeoIP_open(\"%V\") failed", &value[1]);

        return NGX_CONF_ERROR;
    }

    if (cf->args->nelts == 3) {
        if (ngx_strcmp(value[2].data, "utf8") == 0) {
            GeoIP_set_charset (gcf->country, GEOIP_CHARSET_UTF8);

        } else {
            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                               "invalid parameter \"%V\"", &value[2]);
            return NGX_CONF_ERROR;
        }
    }

    switch (gcf->country->databaseType) {

    case GEOIP_COUNTRY_EDITION:
    case GEOIP_PROXY_EDITION:
    case GEOIP_NETSPEED_EDITION:

        return NGX_CONF_OK;

    default:
        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                           "invalid GeoIP database \"%V\" type:%d",
                           &value[1], gcf->country->databaseType);
        return NGX_CONF_ERROR;
    }
}
Beispiel #26
0
GeoIP* GeoIP_open_type (int type, int flags) {
	GeoIP * gi;
	const char * filePath;
	if (type < 0 || type >= NUM_DB_TYPES) {
		printf("Invalid database type %d\n", type);
		return NULL;
	}
	_GeoIP_setup_dbfilename();
	filePath = GeoIPDBFileName[type];
	if (filePath == NULL) {
		printf("Invalid database type %d\n", type);
		return NULL;
	}
	gi = GeoIP_open (filePath, flags);
	return gi;
}
Beispiel #27
0
/* call-seq:
 *   Net::GeoIP.new(database)
 *
 * Initializes a new GeoIP object.  The argument database is the path to the
 * database to open and use with this GeoIP instance.
 */
VALUE
geoip_initialize(VALUE self, VALUE database) {
	GeoIPContainer *container;

	Check_Type(database, T_STRING);

	Data_Get_Struct(self, GeoIPContainer, container);

	container->geoip = GeoIP_open(RSTRING(database)->ptr, GEOIP_INDEX_CACHE);

	if (container->geoip == NULL) {
		rb_raise(cGeoIPError,  "Invalid Database");
	}

	return self;
}
Beispiel #28
0
int geo_acl_check(struct sockaddr_in *sin, char *allowed_countries, char *peername)
{
	GeoIP * gi;
        const char * returnedCountry;
        gi = GeoIP_open("/opt/GeoIP/share/GeoIP/GeoIP.dat", GEOIP_STANDARD | GEOIP_CHECK_CACHE);
	int retValue = 0;
	int j, i=0; // used to iterate through array
	char lookup[255];
    	char *token[86];
        if (gi == NULL) {
                ast_log(LOG_WARNING, "Failed to open GeoIP Lookup database - ignoring GeoACL");
        } else {
		if((allowed_countries == NULL) || (strlen(allowed_countries) == 0) ) {
			ast_log(LOG_NOTICE, "GeoACL - Peer %s does not have allowed/denied countries defined, ignoring check\n", peername);
		} else {
                	ast_log(LOG_DEBUG, "Using %s for allowed geo acl list\n", allowed_countries);
			returnedCountry = GeoIP_country_code_by_addr(gi,ast_inet_ntoa(sin->sin_addr));
	                if (returnedCountry == NULL) {
        	                ast_log(LOG_WARNING, "GeoACL Lookup failed for '%s' - ignoring GeoACL\n", ast_inet_ntoa(sin->sin_addr));
             	   	} else {
				// Now we default to deny since we know what they want and where the IP is from 
				strcpy(lookup, allowed_countries);
				retValue = 1;			
				//ast_log(LOG_NOTICE, "GeoACL - value of allowed_countries is %s", allowed_countries);						
				token[0] = strtok (lookup,",");
  				while(token[i]!= NULL) {   //ensure a pointer was found
        				i++;
        				token[i] = strtok(NULL, ","); //continue to tokenize the string
    				}
  				for(j = 0; j <= i-1; j++) {
			//		ast_log(LOG_DEBUG, "Checking token %s", token[j]);
					if (strncmp(returnedCountry, token[j], 2) == 0) {
						retValue = 0;
					}
				}
				if (retValue == 1)  {
					ast_log(LOG_NOTICE, "GeoACL - peer '%s' failed ACL check.  Allowed from %s but connected from %s\n", peername, allowed_countries, returnedCountry);
				} else {
                	       		 ast_log(LOG_NOTICE, "GeoACL passed for peer '%s' - connected from '%s'\n", peername, returnedCountry);
                		}
			}
		}
        }
        // Free resources
        GeoIP_delete(gi);
	return retValue;
}
static void geoip_child_init(apr_pool_t * p, server_rec * s)
{
    geoip_server_config_rec *cfg;
    int i, flags;

    cfg = (geoip_server_config_rec *)
        ap_get_module_config(s->module_config, &geoip_module);

    if (cfg->gips) {
        if (cfg->GeoIPFilenames != NULL) {
            for (i = 0; i < cfg->numGeoIPFiles; i++) {
                flags =
                    (cfg->GeoIPFlags2[i] ==
                     GEOIP_UNKNOWN) ? cfg->GeoIPFlags : cfg->GeoIPFlags2[i];
                if (flags & (GEOIP_MEMORY_CACHE | GEOIP_MMAP_CACHE))
                    continue;
                if (cfg->gips[i]) {
                    GeoIP_delete(cfg->gips[i]);
                }
                cfg->gips[i] = GeoIP_open(cfg->GeoIPFilenames[i], flags);

                if (cfg->gips[i]) {
                    if (cfg->GeoIPEnableUTF8) {
                        GeoIP_set_charset(cfg->gips[i], GEOIP_CHARSET_UTF8);
                    }
                } else {
                    ap_log_error(APLOG_MARK, APLOG_ERR, 0,
                                 s,
                                 "[mod_geoip]: Error while opening data file %s",
                                 cfg->GeoIPFilenames[i]);
                    continue;
                }
            }
        } else {
            if (cfg->gips[0])
                GeoIP_delete(cfg->gips[0]);
            cfg->gips[0] = GeoIP_new(GEOIP_STANDARD);
            if (!cfg->gips[0]) {
                ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
                             "[mod_geoip]: Error while opening data file");
            }
            cfg->numGeoIPFiles = 1;
        }
    }
}
Beispiel #30
0
void geoip_init(void) {
	const char* geoipdat = getenv("xqf_GEOIPDAT");

	if (gi) return; // already initialized

	if (geoipdat)
		gi = GeoIP_open(geoipdat, GEOIP_STANDARD);

	if (!gi)
		gi = GeoIP_new(GEOIP_STANDARD);

	if (gi && geoip_num_countries())
		flags = g_malloc0((MaxCountries+1) *sizeof(struct pixmap)); /*+1-> flag for LAN server*/
	else {
		geoip_done();
		xqf_error("GeoIP initialization failed");
	}
}