int IpToGeo::lookUp(uint32_t addr, GeoInfo *pInfo)
{
    addr = htonl(addr);
    if (m_pLocation)
    {
        switch (m_locDbType)
        {
        case GEOIP_COUNTRY_EDITION:
            pInfo->m_countryId = GeoIP_id_by_ipnum(m_pLocation, addr);
            break;

        case GEOIP_REGION_EDITION_REV0:
        case GEOIP_REGION_EDITION_REV1:
            pInfo->m_pRegion = GeoIP_region_by_ipnum(m_pLocation, addr);
            break;

        case GEOIP_CITY_EDITION_REV0:
        case GEOIP_CITY_EDITION_REV1:
            pInfo->m_pCity = GeoIP_record_by_ipnum(m_pLocation, addr);
            break;
        }

    }
    if (m_pOrg)
        pInfo->m_pOrg = GeoIP_name_by_ipnum(m_pOrg, addr);
    if (m_pIsp)
        pInfo->m_pIsp = GeoIP_name_by_ipnum(m_pIsp, addr);
    if (m_pNetspeed)
        pInfo->m_netspeed = GeoIP_id_by_ipnum(m_pNetspeed, addr);
    return 0;
}
Example #2
0
int GeoIPLocIP(ftval *ip, enum ftype itype, float *latitude, float *longitude, char **country_code)
{
    GeoIPRecord *gir;
    geoipv6_t gv6;
    const char *ccode;

    gir = NULL;
    ccode = NULL;
    switch (itype) {
    case FT_IPv4:
        if (gi == NULL)
            return -1;
        pthread_mutex_lock(&atom);
        if (city) {
            gir = GeoIP_record_by_ipnum(gi, ntohl(ip->uint32));
            if (gir != NULL) {
                ccode = gir->country_code;
            }
        }
        else {
            ccode = GeoIP_country_code_by_ipnum(gi, ntohl(ip->uint32));
        }
        pthread_mutex_unlock(&atom);
        break;
        
    case FT_IPv6:
        if (giv6 == NULL)
            return -1;
        memcpy(gv6.s6_addr, ip->ipv6, 16);
        pthread_mutex_lock(&atom6);
        if (cityv6) {
            gir = GeoIP_record_by_ipnum_v6(giv6, gv6);
            if (gir != NULL)
                ccode = gir->country_code;
        }
        else {
            ccode = GeoIP_country_code_by_ipnum_v6(giv6, gv6);
        }
        pthread_mutex_unlock(&atom6);
        break;

    default:
        LogPrintf(LV_ERROR, "GeoIP IP type error");
    }

    if (gir != NULL) {
        *latitude = gir->latitude;
        *longitude = gir->longitude;
        if (country_code != NULL)
            *country_code = (char *)ccode;
        GeoIPRecord_delete(gir);
        return 0;
    }
    if (country_code != NULL && ccode != NULL) {
        *country_code = (char *)ccode;
        return 0;
    }

    return -1;
}
Example #3
0
wxString CslGeoIP::GetCountryNameByIPnum(const unsigned long ipnum)
{
    CslGeoIP& self = GetInstance();

    if (!g_geoIP)
        return wxEmptyString;

    GeoIPRecord *r = NULL;
    const char *country = NULL;

    switch (self.m_type)
    {
        case GEOIP_COUNTRY:
            country = GeoIP_country_name_by_ipnum(g_geoIP, wxUINT32_SWAP_ON_LE(ipnum));
            break;
        case GEOIP_CITY:
            if ((r = GeoIP_record_by_ipnum(g_geoIP, wxUINT32_SWAP_ON_LE(ipnum))))
            {
                country = r->country_name;
                GeoIPRecord_delete(r);
            }
            break;
    }

    return country ? C2U(country) : wxString(wxEmptyString);
}
Example #4
0
const char* CslGeoIP::GetCountryCodeByIPnum(const unsigned long ipnum)
{
    CslGeoIP& self = GetInstance();

    if (!g_geoIP)
        return NULL;

    GeoIPRecord *r = NULL;
    const char *code = NULL;

    switch (self.m_type)
    {
        case GEOIP_COUNTRY:
            code = GeoIP_country_code_by_ipnum(g_geoIP, wxUINT32_SWAP_ON_LE(ipnum));
            break;
        case GEOIP_CITY:
            if ((r = GeoIP_record_by_ipnum(g_geoIP, wxUINT32_SWAP_ON_LE(ipnum))))
            {
                code = r->country_code;
                GeoIPRecord_delete(r);
            }
            break;
    }

    return code;
}
Example #5
0
void Geolocation::getInfo(IpAddress *addr, char **country_code, char **city, float *latitude, float *longitude) {
#ifdef HAVE_GEOIP
  GeoIPRecord *geo = NULL;
  struct ipAddress *ip = addr->getIP();
  
  switch(ip->ipVersion) {
  case 4:
    if(geo_ip_city_db != NULL)
      geo = GeoIP_record_by_ipnum(geo_ip_city_db, ntohl(ip->ipType.ipv4));
    break;
    
  case 6:
    if(geo_ip_city_db_v6 != NULL) {
      struct in6_addr *ipv6 = (struct in6_addr*)&ip->ipType.ipv6;
      
      geo = GeoIP_record_by_ipnum_v6(geo_ip_city_db_v6, *ipv6);
    }
    break;
  }

  if(geo != NULL) {
    *country_code = geo->country_code ? strdup(geo->country_code) : NULL;
    *city = geo->city ? strdup(geo->city) : NULL;
    *latitude = geo->latitude, *longitude = geo->longitude;
    GeoIPRecord_delete(geo);
  } else
    *country_code = NULL, *city = NULL, *latitude = *longitude = 0;
#endif
}
Example #6
0
/*
 * GeoIPRecord lookups are performed if the previous lookup was
 * from a different IP address than the current, or was for a search
 * outside the City database.
 */
static GeoIPRecord *
city_lookup(GeoIP *db, dns_geoip_subtype_t subtype,
	    unsigned int family, isc_uint32_t ipnum, const geoipv6_t *ipnum6)
{
	GeoIPRecord *record = NULL;
	geoip_state_t *prev_state = NULL;

	REQUIRE(db != NULL);

#ifndef HAVE_GEOIP_V6
	/* no IPv6 support? give up now */
	if (family == AF_INET6)
		return (NULL);
#endif

	prev_state = get_state_for(family, ipnum, ipnum6);
	if (prev_state != NULL && is_city(prev_state->subtype))
		record = prev_state->record;

	if (record == NULL) {
		if (family == AF_INET)
			record = GeoIP_record_by_ipnum(db, ipnum);
#ifdef HAVE_GEOIP_V6
		else
			record = GeoIP_record_by_ipnum_v6(db, *ipnum6);
#endif
		if (record == NULL)
			return (NULL);

		set_state(family, ipnum, ipnum6, subtype,
			  record, NULL, NULL, NULL, 0);
	}

	return (record);
}
static GeoIPRecord *
ngx_http_geoip_get_city_record(ngx_http_request_t *r)
{
    ngx_http_geoip_conf_t  *gcf;

    gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);

    if (gcf->city) {
#if (NGX_HAVE_GEOIP_V6)
        return gcf->city_v6
                   ? GeoIP_record_by_ipnum_v6(gcf->city,
                                              ngx_http_geoip_addr_v6(r, gcf))
                   : GeoIP_record_by_ipnum(gcf->city,
                                           ngx_http_geoip_addr(r, gcf));
#else
        return GeoIP_record_by_ipnum(gcf->city, ngx_http_geoip_addr(r, gcf));
#endif
    }

    return NULL;
}
static GeoIPRecord *
ngx_stream_geoip_get_city_record(ngx_stream_session_t *s)
{
    ngx_stream_geoip_conf_t  *gcf;

    gcf = ngx_stream_get_module_main_conf(s, ngx_stream_geoip_module);

    if (gcf->city) {
#if (NGX_HAVE_GEOIP_V6)
        return gcf->city_v6
               ? GeoIP_record_by_ipnum_v6(gcf->city,
                                          ngx_stream_geoip_addr_v6(s, gcf))
               : GeoIP_record_by_ipnum(gcf->city,
                                       ngx_stream_geoip_addr(s, gcf));
#else
        return GeoIP_record_by_ipnum(gcf->city, ngx_stream_geoip_addr(s, gcf));
#endif
    }

    return NULL;
}
static GeoIPRecord *
ngx_http_geoip_get_city_record(ngx_http_request_t *r)
{
    ngx_http_geoip_conf_t  *gcf;

    gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);

    if (gcf->city) {
        return GeoIP_record_by_ipnum(gcf->city, ngx_http_geoip_addr(r));
    }

    return NULL;
}
Example #10
0
void
fprintip(FILE *stream, uint32_t ip, ncc_t *ncc)
{
    uint8_t addr[4];
#if (HAVE_GEOIP)
    GeoIPRecord *gir;
#endif

#if (HAVE_GEOIP)
        if (((ncc->flags) & NFEX_GEOIP) && ncc->gi)
        {
            gir = GeoIP_record_by_ipnum(ncc->gi, ntohl(ip));
            if (gir == NULL)
            {
                /** fall back on printing the IP address */
                goto print_simple;
            }
            if (gir->city && gir->country_code)
            {
                fprintf(stream, "%s, %s", gir->city, gir->country_code);
                return;
            }
            if (gir->city && gir->country_code == NULL)
            {
                fprintf(stream, "%s, ?", gir->city);
                return;
            }
            if (gir->city == NULL && gir->country_code)
            {
                fprintf(stream, "?, %s", gir->country_code);
                return;
            }
            if (gir->city == NULL && gir->country_code == NULL)
            {
                goto print_simple;
            }
        }
#else
        goto print_simple;
#endif /** HAVE_GEOIP */

print_simple:
    memcpy(addr, &ip, 4);
    fprintf(stream, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]);

    return;
}
Example #11
0
wxString CslGeoIP::GetCityNameByIPnum(const unsigned long ipnum)
{
    CslGeoIP& self = GetInstance();

    wxString city;

    if (g_geoIP && self.m_type==GEOIP_CITY)
    {
        GeoIPRecord *r = GeoIP_record_by_ipnum(g_geoIP, wxUINT32_SWAP_ON_LE(ipnum));

        if (r)
        {
            city = C2U(r->city);
            GeoIPRecord_delete(r);
        }
    }

    return city;
}
static GeoIPRecord *
ngx_http_geoip_get_city_record(ngx_http_request_t *r)
{
    u_long                  addr;
    struct sockaddr_in     *sin;
    ngx_http_geoip_conf_t  *gcf;

    gcf = ngx_http_get_module_main_conf(r, ngx_http_geoip_module);

    if (gcf->city && r->connection->sockaddr->sa_family == AF_INET) {

        sin = (struct sockaddr_in *) r->connection->sockaddr;
        addr = ntohl(sin->sin_addr.s_addr);

        return GeoIP_record_by_ipnum(gcf->city, addr);
    }

    return NULL;
}
Example #13
0
int
geoiplookup (GeoIP * gi, char *hostname, int i,
	     struct geo_output_struct *geo_output)
{
  GeoIPRecord *gir;
  uint32_t ipnum;

  ipnum = _GeoIP_lookupaddress (hostname);
  if (ipnum == 0)
    {
      pmesg(LOG_ERROR,__FILE__,__LINE__, "%s: can't resolve hostname ( %s ) [keep this code for debug -1]\n", GeoIPDBDescription[i], hostname);
      return -1;

    }
  if (GEOIP_CITY_EDITION_REV1 == i)	// it should be always true
    {
      gir = GeoIP_record_by_ipnum (gi, ipnum);
      if (NULL == gir)
	{
	  pmesg(LOG_ERROR,__FILE__,__LINE__,"%s: IP Address not found [keep this code for debug -3]\n",GeoIPDBDescription[i]);
	  return -3;
	}
      else
	{
	    sprintf (geo_output->country_code, "%s", gir->country_code);
	    sprintf (geo_output->region, "%s",_mk_NA (gir->region));
	    sprintf (geo_output->city,"%s",_mk_NA (gir->city));
	    sprintf (geo_output->postal_code,"%s", _mk_NA (gir->postal_code));
	    geo_output->latitude=gir->latitude;
	    geo_output->longitude=gir->longitude;
	    geo_output->metro_code=gir->metro_code;
	    geo_output->area_code=gir->area_code;
	    //_say_range_by_ip (gi, ipnum);
	  GeoIPRecord_delete (gir);
	}
    }
  else
    {
      pmesg(LOG_ERROR,__FILE__,__LINE__,"Some problem with the current release [keep this code for debug -2 ver:%d]\n",i);
      return -2;
    }
  return 0;
}
Example #14
0
void geoiplookup(GeoIP * gi, char *hostname, int i)
{
    const char *country_code;
    const char *country_name;
    const char *domain_name;
    const char *asnum_name;
    int netspeed;
    int country_id;
    GeoIPRegion *region;
    GeoIPRecord *gir;
    const char *org;
    uint32_t ipnum;

    ipnum = _GeoIP_lookupaddress(hostname);
    if (ipnum == 0) {
        printf("%s: can't resolve hostname ( %s )\n", GeoIPDBDescription[i],
               hostname);
    }else {
        if (GEOIP_DOMAIN_EDITION == i) {
            domain_name = GeoIP_name_by_ipnum(gi, ipnum);
            if (domain_name == NULL) {
                printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
            }else {
                printf("%s: %s\n", GeoIPDBDescription[i], domain_name);
                _say_range_by_ip(gi, ipnum);
                free((void *)domain_name);
            }
        }else if (GEOIP_LOCATIONA_EDITION == i ||
                  GEOIP_ACCURACYRADIUS_EDITION == i
                  || GEOIP_ASNUM_EDITION == i || GEOIP_USERTYPE_EDITION == i
                  || GEOIP_REGISTRAR_EDITION == i ||
                  GEOIP_NETSPEED_EDITION_REV1 == i
                  || GEOIP_COUNTRYCONF_EDITION == i ||
                  GEOIP_CITYCONF_EDITION == i
                  || GEOIP_REGIONCONF_EDITION == i ||
                  GEOIP_POSTALCONF_EDITION == i) {
            asnum_name = GeoIP_name_by_ipnum(gi, ipnum);
            if (asnum_name == NULL) {
                printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
            }else {
                printf("%s: %s\n", GeoIPDBDescription[i], asnum_name);
                _say_range_by_ip(gi, ipnum);
                free((void *)asnum_name);
            }
        }else if (GEOIP_COUNTRY_EDITION == i) {
            country_id = GeoIP_id_by_ipnum(gi, ipnum);
            if (country_id < 0 || country_id >= (int)GeoIP_num_countries()) {
                printf("%s: Invalid database\n", GeoIPDBDescription[i]);
                return;
            }
            country_code = GeoIP_country_code[country_id];
            country_name = GeoIP_country_name[country_id];
            if (country_id == 0) {
                printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
            }else {
                printf("%s: %s, %s\n", GeoIPDBDescription[i], country_code,
                       country_name);
                _say_range_by_ip(gi, ipnum);
            }
        }else if (GEOIP_REGION_EDITION_REV0 == i ||
                  GEOIP_REGION_EDITION_REV1 == i) {
            region = GeoIP_region_by_ipnum(gi, ipnum);
            if (NULL == region || region->country_code[0] == '\0') {
                printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
            }else {
                printf("%s: %s, %s\n", GeoIPDBDescription[i],
                       region->country_code,
                       region->region);
                _say_range_by_ip(gi, ipnum);
            }
            if (region) {
                GeoIPRegion_delete(region);
            }
        }else if (GEOIP_CITY_EDITION_REV0 == i) {
            gir = GeoIP_record_by_ipnum(gi, ipnum);
            if (NULL == gir) {
                printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
            }else {
                printf("%s: %s, %s, %s, %s, %s, %f, %f\n",
                       GeoIPDBDescription[i], 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);
                _say_range_by_ip(gi, ipnum);
                GeoIPRecord_delete(gir);
            }
        }else if (GEOIP_CITY_EDITION_REV1 == i) {
            gir = GeoIP_record_by_ipnum(gi, ipnum);
            if (NULL == gir) {
                printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
            }else {
                printf("%s: %s, %s, %s, %s, %s, %f, %f, %d, %d\n",
                       GeoIPDBDescription[i], 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);
                _say_range_by_ip(gi, ipnum);
                GeoIPRecord_delete(gir);
            }
        }else if (GEOIP_ORG_EDITION == i || GEOIP_ISP_EDITION == i) {
            org = GeoIP_org_by_ipnum(gi, ipnum);
            if (org == NULL) {
                printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
            }else {
                printf("%s: %s\n", GeoIPDBDescription[i], org);
                _say_range_by_ip(gi, ipnum);
                free((void *)org);
            }
        }else if (GEOIP_NETSPEED_EDITION == i) {
            netspeed = GeoIP_id_by_ipnum(gi, ipnum);
            if (netspeed == GEOIP_UNKNOWN_SPEED) {
                printf("%s: Unknown\n", GeoIPDBDescription[i]);
            }else if (netspeed == GEOIP_DIALUP_SPEED) {
                printf("%s: Dialup\n", GeoIPDBDescription[i]);
            }else if (netspeed == GEOIP_CABLEDSL_SPEED) {
                printf("%s: Cable/DSL\n", GeoIPDBDescription[i]);
            }else if (netspeed == GEOIP_CORPORATE_SPEED) {
                printf("%s: Corporate\n", GeoIPDBDescription[i]);
            }
            _say_range_by_ip(gi, ipnum);
        }
    }
}
Example #15
0
void
geoiplookup(GeoIP * gi, char *hostname, int i)
{
	const char     *country_code;
	const char     *country_name;
	const char     *domain_name;
	int             netspeed;
	int             country_id;
	GeoIPRegion    *region;
	GeoIPRecord    *gir;
	const char     *org;
	uint32_t        ipnum;
	
	ipnum = _GeoIP_lookupaddress(hostname);
	if (ipnum == 0) {
		printf("%s: can't resolve hostname ( %s )\n", GeoIPDBDescription[i], hostname);

	}
	else {

		if (GEOIP_DOMAIN_EDITION == i) {
			domain_name = GeoIP_name_by_ipnum(gi, ipnum);
			if (domain_name == NULL) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
				printf("%s: %s\n", GeoIPDBDescription[i], domain_name);
			}
		}
		else if (GEOIP_COUNTRY_EDITION == i) {
                        country_id = GeoIP_id_by_ipnum(gi, ipnum);
			country_code = GeoIP_country_code[country_id];
			country_name = GeoIP_country_name[country_id];
			if (country_id == 0) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
				printf("%s: %s, %s\n", GeoIPDBDescription[i], country_code, country_name);
			}
		}
		else if (GEOIP_REGION_EDITION_REV0 == i || GEOIP_REGION_EDITION_REV1 == i) {
			region = GeoIP_region_by_ipnum(gi, ipnum);
			if (NULL == region) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
				printf("%s: %s, %s\n", GeoIPDBDescription[i], region->country_code, region->region);
				GeoIPRegion_delete(region);
			}
		}
		else if (GEOIP_CITY_EDITION_REV0 == i) {
			gir = GeoIP_record_by_ipnum(gi, ipnum);
			if (NULL == gir) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
				printf("%s: %s, %s, %s, %s, %f, %f\n", GeoIPDBDescription[i], gir->country_code, _mk_NA(gir->region),
				       _mk_NA(gir->city), _mk_NA(gir->postal_code), gir->latitude, gir->longitude);
			}
		}
		else if (GEOIP_CITY_EDITION_REV1 == i) {
			gir = GeoIP_record_by_ipnum(gi, ipnum);
			if (NULL == gir) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
				printf("%s: %s, %s, %s, %s, %f, %f, %d, %d\n", GeoIPDBDescription[i], gir->country_code, _mk_NA(gir->region), _mk_NA(gir->city), _mk_NA(gir->postal_code),
				       gir->latitude, gir->longitude, gir->metro_code, gir->area_code);
			}
		}
		else if (GEOIP_ORG_EDITION == i || GEOIP_ISP_EDITION == i) {
			org = GeoIP_org_by_ipnum(gi, ipnum);
			if (org == NULL) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
				printf("%s: %s\n", GeoIPDBDescription[i], org);
			}
		}
		else if (GEOIP_NETSPEED_EDITION == i) {
			netspeed = GeoIP_id_by_ipnum(gi, ipnum);
			if (netspeed == GEOIP_UNKNOWN_SPEED) {
				printf("%s: Unknown\n", GeoIPDBDescription[i]);
			}
			else if (netspeed == GEOIP_DIALUP_SPEED) {
				printf("%s: Dialup\n", GeoIPDBDescription[i]);
			}
			else if (netspeed == GEOIP_CABLEDSL_SPEED) {
				printf("%s: Cable/DSL\n", GeoIPDBDescription[i]);
			}
			else if (netspeed == GEOIP_CORPORATE_SPEED) {
				printf("%s: Corporate\n", GeoIPDBDescription[i]);
			}
		}
		else {

		/*
		 * Silent ignore IPv6 databases. Otherwise we get annoying 
		 * messages whenever we have a mixed environment IPv4 and
		 *  IPv6
		 */
		
		/*
		 * printf("Can not handle database type -- try geoiplookup6\n");
		 */
		;
		}
	}
}
Example #16
0
void
geoiplookup(GeoIP * gi, char *hostname, int i)
{
	const char     *country_code;
	const char     *country_name;
	const char     *domain_name;
        const char     *asnum_name;
	int             netspeed;
	int             country_id;
	GeoIPRegion    *region;
	GeoIPRecord    *gir;
	const char     *org;
	uint32_t        ipnum;
	
	ipnum = _GeoIP_lookupaddress(hostname);
	if (ipnum == 0) {
		printf("%s: can't resolve hostname ( %s )\n", GeoIPDBDescription[i], hostname);

	}
	else {

		if (GEOIP_DOMAIN_EDITION == i) {
			domain_name = GeoIP_name_by_ipnum(gi, ipnum);
			if (domain_name == NULL) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
				printf("%s: %s\n", GeoIPDBDescription[i], domain_name);
                                _say_range_by_ip(gi, ipnum);
			}
		}
		else if (GEOIP_LOCATIONA_EDITION == i || GEOIP_ACCURACYRADIUS_EDITION == i || GEOIP_ASNUM_EDITION == i || GEOIP_USERTYPE_EDITION == i || GEOIP_REGISTRAR_EDITION == i || GEOIP_NETSPEED_EDITION_REV1 == i ) {
			asnum_name = GeoIP_name_by_ipnum(gi, ipnum);
			if (asnum_name == NULL) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
				printf("%s: %s\n", GeoIPDBDescription[i], asnum_name);
                                _say_range_by_ip(gi, ipnum);
			}
		}
		else if (GEOIP_COUNTRY_EDITION == i) {
                        country_id = GeoIP_id_by_ipnum(gi, ipnum);
			country_code = GeoIP_country_code[country_id];
			country_name = GeoIP_country_name[country_id];
			if (country_id == 0) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
				printf("%s: %s, %s\n", GeoIPDBDescription[i], country_code, country_name);
                                _say_range_by_ip(gi, ipnum);
			}
		}
		else if (GEOIP_REGION_EDITION_REV0 == i || GEOIP_REGION_EDITION_REV1 == i) {
			region = GeoIP_region_by_ipnum(gi, ipnum);
			if (NULL == region || region->country_code[0] == '\0' ) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
				printf("%s: %s, %s\n", GeoIPDBDescription[i], region->country_code, region->region);
                                _say_range_by_ip(gi, ipnum);
				GeoIPRegion_delete(region);
			}
		}
		else if (GEOIP_CITY_EDITION_REV0 == i) {
			gir = GeoIP_record_by_ipnum(gi, ipnum);
			if (NULL == gir) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
				printf("%s: %s, %s, %s, %s, %f, %f\n", GeoIPDBDescription[i], gir->country_code, _mk_NA(gir->region),
				       _mk_NA(gir->city), _mk_NA(gir->postal_code), gir->latitude, gir->longitude);
                                _say_range_by_ip(gi, ipnum);
                                GeoIPRecord_delete(gir);
			}
		}
		else if (GEOIP_CITY_EDITION_REV1 == i) {
			gir = GeoIP_record_by_ipnum(gi, ipnum);
			if (NULL == gir) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
				printf("%s: %s, %s, %s, %s, %f, %f, %d, %d\n", GeoIPDBDescription[i], gir->country_code, _mk_NA(gir->region), _mk_NA(gir->city), _mk_NA(gir->postal_code),
				       gir->latitude, gir->longitude, gir->metro_code, gir->area_code);
                                _say_range_by_ip(gi, ipnum);
                                GeoIPRecord_delete(gir);
			}
		}
		else if (GEOIP_CITYCONFIDENCE_EDITION == i) {
			gir = GeoIP_record_by_ipnum(gi, ipnum);
			if (NULL == gir) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
                                char country_str[5], region_str[5], city_str[5], postal_str[5];
                                _mk_conf_str(gir->country_conf, country_str, 5);
                                _mk_conf_str(gir->region_conf,  region_str,  5);
                                _mk_conf_str(gir->city_conf,    city_str,    5);
                                _mk_conf_str(gir->postal_conf,  postal_str,  5);

				printf("%s: %s, %s, %s, %s, %f, %f, %d, %d, %s, %s, %s, %s\n", GeoIPDBDescription[i], gir->country_code, _mk_NA(gir->region), _mk_NA(gir->city), _mk_NA(gir->postal_code),
				       gir->latitude, gir->longitude, gir->metro_code, gir->area_code,
                                       country_str, region_str, city_str, postal_str                       
                                     );
                                _say_range_by_ip(gi, ipnum);
                                GeoIPRecord_delete(gir);                                
			}
		}
		else if (GEOIP_CITYCONFIDENCEDIST_EDITION == i) {
			gir = GeoIP_record_by_ipnum(gi, ipnum);
			if (NULL == gir) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
                                char country_str[5], region_str[5], city_str[5], postal_str[5], accuracy_radius_str[5];
                                _mk_conf_str(gir->country_conf, country_str, 5);
                                _mk_conf_str(gir->region_conf,  region_str,  5);
                                _mk_conf_str(gir->city_conf,    city_str,    5);
                                _mk_conf_str(gir->postal_conf,  postal_str,  5);
                                if (gir->accuracy_radius != 1023){
                                  sprintf(accuracy_radius_str, "%d", gir->accuracy_radius );
} else {
                                strcpy(accuracy_radius_str,"N/A");}
  
				printf("%s: %s, %s, %s, %s, %f, %f, %d, %d, %s, %s, %s, %s, %s\n", GeoIPDBDescription[i], gir->country_code, _mk_NA(gir->region), _mk_NA(gir->city), _mk_NA(gir->postal_code),
				       gir->latitude, gir->longitude, gir->metro_code, gir->area_code,
                                       country_str, region_str, city_str, postal_str, accuracy_radius_str
                                     );
                                _say_range_by_ip(gi, ipnum);
                                GeoIPRecord_delete(gir);                                
			}
		}
		else if (GEOIP_ORG_EDITION == i || GEOIP_ISP_EDITION == i) {
			org = GeoIP_org_by_ipnum(gi, ipnum);
			if (org == NULL) {
				printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
			}
			else {
				printf("%s: %s\n", GeoIPDBDescription[i], org);
                                _say_range_by_ip(gi, ipnum);
			}
		}
		else if (GEOIP_NETSPEED_EDITION == i) {
			netspeed = GeoIP_id_by_ipnum(gi, ipnum);
			if (netspeed == GEOIP_UNKNOWN_SPEED) {
				printf("%s: Unknown\n", GeoIPDBDescription[i]);
			}
			else if (netspeed == GEOIP_DIALUP_SPEED) {
				printf("%s: Dialup\n", GeoIPDBDescription[i]);
			}
			else if (netspeed == GEOIP_CABLEDSL_SPEED) {
				printf("%s: Cable/DSL\n", GeoIPDBDescription[i]);
			}
			else if (netspeed == GEOIP_CORPORATE_SPEED) {
				printf("%s: Corporate\n", GeoIPDBDescription[i]);
			}
                        _say_range_by_ip(gi, ipnum);
		}
		else {

		/*
		 * Silent ignore IPv6 databases. Otherwise we get annoying 
		 * messages whenever we have a mixed environment IPv4 and
		 *  IPv6
		 */
		
		/*
		 * printf("Can not handle database type -- try geoiplookup6\n");
		 */
		;
		}
	}
}
Example #17
0
iplocation utils::get_iplocation( wxString ip )
{
	iplocation a;
	//Check if dat file already opened or not
	//static bool db_opened = false;
//#if defined(__UNIX__)
	GeoIP *gi;

    GeoIPRecord*  gir;

	wxString dat_path = wxFileName(wxStandardPaths::Get().GetExecutablePath()).GetPath() + wxT("/GeoLiteCity.dat");

	uint32_t ipnum;
	char *_ip = strdup( ip.ToAscii().data() );

	//Ip number first
	ipnum = _GeoIP_lookupaddress(_ip);

    //Open location data file if it exists
    if( ::wxFileExists(dat_path) )
    {
        char *path = strdup ( dat_path.ToAscii().data());
        gi = GeoIP_open(path , GEOIP_STANDARD);
        //db_opened = true;
        free(path);
    }

    //If dat file doesnt exist return
    else
    {
        return a;
    }

	if (gi == NULL)
	{
		//printf("%s not available, skipping...\n" , path);
	}
	else
	{
		gir = GeoIP_record_by_ipnum(gi , ipnum);

		GeoIP_delete(gi);

		if (gir == NULL)
		{
			//printf("%s: IP Address not found\n", GeoIPDBDescription[i]);
		}
		else
		{
			//printf("%s, %s, %s, %s, %f, %f\n",  gir->country_code, _mk_NA(gir->region) , _mk_NA(gir->city), _mk_NA(gir->postal_code), gir->latitude, gir->longitude);

			a.country_code = wxString(gir->country_code , wxConvUTF8);
			a.city = wxString(gir->city , wxConvUTF8);

			a.latitude = gir->latitude;
			a.longitude = gir->longitude;

			GeoIPRecord_delete(gir);
		}
	}

//#endif
	return a;
}
Example #18
0
/* new_request_stat()
 *
 * Register statistics about client requests. Returns the newly generated
 * and unique id.
 * */
int
new_request_stat			(unsigned long ip_num, const char * type, char * user_agent)
{
    int i, auto_id, client_id_len, child_id;
    char * query, * server_id, * client_id, * tmp_id;
    unsigned char * uchar_sign;
    GeoIPRecord * record;
    MYSQL_RES * result;
    MYSQL_ROW row;
    char * add_info;
	
    /* TODO: browser and opsys detection. */
    const char browser [] = UNKNOWN_FIELD;
    const char opsys [] = UNKNOWN_FIELD;
	
    /* Check if statistic data gathering is enabled. */
    if (stats_enabled != 1)
    {
	return (EXIT_SUCCESS);
    }

    child_id = get_child_pos();
    server_id = (char *)get_server_name();
	
    /* Calculate unique digest. */
    uchar_sign = malloc(SHA_DIGEST_LENGTH);
    client_id = malloc(SIGN_LEN + 1);
    client_id_len = asprintf(&tmp_id, "%lu", ip_num);
    SHA1((unsigned char *)tmp_id, client_id_len, uchar_sign);
    free(tmp_id);
	
    for (i = 0; i < SHA_DIGEST_LENGTH; i++)
    {
	sprintf(client_id + i * 2, "%02x", uchar_sign[i]);
    }

    free(uchar_sign);
	
    if ((record = GeoIP_record_by_ipnum(gi_db, ip_num)) != NULL)
    {
	asprintf(&query, "INSERT INTO requests (%s) VALUES ('%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', CURRENT_TIMESTAMP())",
		 REQUEST_FIELDS, client_id, server_id, child_id, type, browser, opsys, record->city, record->country_name);
	GeoIPRecord_delete(record);
    }
    else
    {
	asprintf(&query, "INSERT INTO requests (%s) VALUES ('%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', CURRENT_TIMESTAMP())",
		 REQUEST_FIELDS, client_id, server_id, child_id, type, browser, opsys, UNKNOWN_FIELD, UNKNOWN_FIELD);
    }
	
    /* MySQL threaded interaction.
     * Multithread clients sharing the same connection *must* enclose
     * mysql_query() and mysql_store_result() in a lock to prevent access
     * from other threads.
     * */
	
    /* Insert data and get the last autoincremented value. */
    pthread_mutex_lock(&stat_lock);
    if (mysql_query(db_conn, query) != 0)
    {
	pthread_mutex_unlock(&stat_lock);
	log_message(ERROR, EMSG_INSERT, query);
	free(client_id);
	free(server_id);
	free(query);
	return (ECOD_INSERT);
    }

    pthread_mutex_unlock(&stat_lock);
    free(client_id);
    free(query);
    asprintf(&query, "SELECT MAX(id) FROM requests WHERE server_id = '%s' AND child_id = %d", server_id, child_id);
    free(server_id);
	
    pthread_mutex_lock(&stat_lock);
    if (mysql_query(db_conn, query) != 0)
    {
	pthread_mutex_unlock(&stat_lock);
	log_message(ERROR, EMSG_SELECT, query);
	free(query);
	return (ECOD_SELECT);
    }
	
    if ((result = mysql_store_result(db_conn)) != NULL)
    {
	pthread_mutex_unlock(&stat_lock);
	free(query);
	row = mysql_fetch_row(result);
	auto_id = atoi(row[0]);
	mysql_free_result(result);
	asprintf(&add_info, "Request ID: %d\n", auto_id);
	log_message(MESSAGE, IMSG_NEWREQSTAT, add_info);
	free(add_info);
    }
    else
    {
	pthread_mutex_unlock(&stat_lock);
	log_message(ERROR, EMSG_SELECT, query);
	free(query);
	return (ECOD_SELECT);
    }

    return (auto_id);
}