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
/*
 * GeoIPRegion lookups are performed if the previous lookup was
 * from a different IP address than the current, or was for a search
 * outside the Region database.
 */
static GeoIPRegion *
region_lookup(GeoIP *db, dns_geoip_subtype_t subtype, isc_uint32_t ipnum) {
	GeoIPRegion *region = NULL;
	geoip_state_t *prev_state = NULL;

	REQUIRE(db != NULL);

	prev_state = get_state_for(AF_INET, ipnum, NULL);
	if (prev_state != NULL && is_region(prev_state->subtype))
		region = prev_state->region;

	if (region == NULL) {
		region = GeoIP_region_by_ipnum(db, ipnum);
		if (region == NULL)
			return (NULL);

		set_state(AF_INET, ipnum, NULL,
			  subtype, NULL, region, NULL, NULL, 0);
	}

	return (region);
}
Example #3
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 #4
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 #5
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");
		 */
		;
		}
	}
}