예제 #1
0
void _say_range_by_ip(GeoIP * gi, uint32_t ipnum )
{
    unsigned long last_nm, mask, low, hi;
    char ipaddr[16];
    char tmp[16];
    char ** range;

    if (info_flag == 0) {
        return; /* noop unless extra information is requested */

    }
    range = GeoIP_range_by_ip( gi, __num_to_addr_r( ipnum, ipaddr ) );
    if (range == NULL) {
        return;
    }

    printf( "  ipaddr: %s\n", ipaddr );

    printf( "  range_by_ip:  %s - %s\n", range[0], range[1] );
    last_nm = GeoIP_last_netmask(gi);
    mask = 0xffffffff << ( 32 - last_nm );
    low = ipnum & mask;
    hi = low + ( 0xffffffff & ~mask );
    printf( "  network:      %s - %s ::%ld\n",
            __num_to_addr_r( low, ipaddr ),
            __num_to_addr_r( hi, tmp ),
            last_nm
            );
    printf( "  ipnum: %u\n", ipnum );
    printf( "  range_by_num: %lu - %lu\n", __addr_to_num(
                range[0]), __addr_to_num(range[1]) );
    printf( "  network num:  %lu - %lu ::%lu\n", low, hi, last_nm );

    GeoIP_range_by_ip_delete(range);
}
예제 #2
0
static PyObject * GeoIP_range_by_ip_Py(PyObject *self, PyObject *args) {
  char * name;
  char ** start_stop_ptr;
  PyObject * retval;

  GeoIP_GeoIPObject* GeoIP = (GeoIP_GeoIPObject*)self;
  if (!PyArg_ParseTuple(args, "s", &name)) {
    return NULL;
  }
  start_stop_ptr = GeoIP_range_by_ip(GeoIP->gi, name);
  if ( !start_stop_ptr ) {
    return NULL;
  }

  retval = Py_BuildValue("ss", start_stop_ptr[0], start_stop_ptr[1]);
  
  /* relplace this code with GeoIP_range_by_ip_delete in the next version 
   * otherwise the users need 1.4.5 instead of 1.4.4 */
  if ( retval ) {
    if ( start_stop_ptr[0] )
      free(start_stop_ptr[0]);
    if ( start_stop_ptr[1] )
      free(start_stop_ptr[1]);
    free(start_stop_ptr);
  }
  return retval;
}
예제 #3
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;
}
예제 #4
0
int main(int argc, char *argv[]) {
    FILE *f;
    GeoIP *gi;
    char *domain;
    char host[50];
    char **ret;

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

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

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

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

    printf("IP\tdomain\tnetmask\tbeginIp\tendIp\n");
    while (fscanf(f, "%s", host) != EOF) {
        domain = GeoIP_name_by_name(gi, (const char *)host);

        if (domain != NULL) {
            ret = GeoIP_range_by_ip(gi, (const char *)host);

            printf("%s\t%s\t%d\t%s\t%s\n",
                   host,
                   _mk_NA(domain),
                   GeoIP_last_netmask(gi),
                   ret[0],
                   ret[1]);
            GeoIP_range_by_ip_delete(ret);
            free(domain);
        }
    }

    fclose(f);
    GeoIP_delete(gi);
    return 0;
}
예제 #5
0
int pv_get_geoip(struct sip_msg *msg, pv_param_t *param,
		pv_value_t *res)
{
	geoip_pv_t *gpv;

	if(msg==NULL || param==NULL)
		return -1;

	gpv = (geoip_pv_t*)param->pvn.u.dname;
	if(gpv==NULL)
		return -1;
	if(gpv->item==NULL)
		return pv_get_null(msg, param, res);

	switch(gpv->type)
	{
		case 1: /* tz */
			if(gpv->item->r.time_zone==NULL)
			{
				if(gpv->item->r.flags&1)
					return pv_get_null(msg, param, res);
				if(gpv->item->r.record==NULL)
					return pv_get_null(msg, param, res);
				gpv->item->r.time_zone
					= (char*)GeoIP_time_zone_by_country_and_region(
						gpv->item->r.record->country_code,
						gpv->item->r.record->region);
				gpv->item->r.flags |= 1;
			}
			return pv_geoip_get_strzval(msg, param, res,
					gpv->item->r.time_zone);
		case 2: /* zip */
			if(gpv->item->r.record==NULL)
				return pv_get_null(msg, param, res);
			return pv_geoip_get_strzval(msg, param, res,
					gpv->item->r.record->postal_code);
		case 3: /* lat */
			if((gpv->item->r.flags&2)==0)
			{
				if(gpv->item->r.record==NULL)
					return pv_get_null(msg, param, res);
				snprintf(gpv->item->r.latitude, 15, "%f",
						gpv->item->r.record->latitude);
				gpv->item->r.flags |= 2;
			}
			return pv_geoip_get_strzval(msg, param, res,
					gpv->item->r.latitude);
		case 4: /* lon */
			if((gpv->item->r.flags&4)==0)
			{
				if(gpv->item->r.record==NULL)
					return pv_get_null(msg, param, res);
				snprintf(gpv->item->r.longitude, 15, "%f",
						gpv->item->r.record->longitude);
				gpv->item->r.flags |= 4;
			}
			return pv_geoip_get_strzval(msg, param, res,
					gpv->item->r.longitude);
		case 5: /* dma */
			if(gpv->item->r.record==NULL)
				return pv_get_null(msg, param, res);
			return pv_get_sintval(msg, param, res,
					gpv->item->r.record->dma_code);
		case 6: /* ips */
		case 7: /* ipe */
			if((gpv->item->r.flags&8)==0)
			{
				gpv->item->r.range = GeoIP_range_by_ip(_handle_GeoIP,
					gpv->item->r.tomatch);
				gpv->item->r.flags |= 8;
			}
			if(gpv->item->r.range==NULL)
				return pv_get_null(msg, param, res);
			if(gpv->type==6)
				return pv_geoip_get_strzval(msg, param, res,
						gpv->item->r.range[0]);
			return pv_geoip_get_strzval(msg, param, res,
					gpv->item->r.range[1]);
		case 8: /* city */
			return pv_geoip_get_strzval(msg, param, res,
					gpv->item->r.record->city);
		case 9: /* area */
			if(gpv->item->r.record==NULL)
				return pv_get_null(msg, param, res);
			return pv_get_sintval(msg, param, res,
					gpv->item->r.record->area_code);
		case 10: /* regc */
			if(gpv->item->r.record==NULL)
				return pv_get_null(msg, param, res);
			return pv_geoip_get_strzval(msg, param, res,
					gpv->item->r.record->region);
		case 11: /* regn */
			if((gpv->item->r.flags&16)==0)
			{
				if(gpv->item->r.record==NULL)
					return pv_get_null(msg, param, res);
				gpv->item->r.region_name
						= (char*)GeoIP_region_name_by_code(
							gpv->item->r.record->country_code,
							gpv->item->r.record->region);
				gpv->item->r.flags |= 16;
			}
			return pv_geoip_get_strzval(msg, param, res,
					gpv->item->r.region_name);
		case 12: /* metro */
			if(gpv->item->r.record==NULL)
				return pv_get_null(msg, param, res);
			return pv_get_sintval(msg, param, res,
					gpv->item->r.record->metro_code);
		case 13: /* contc */
			if(gpv->item->r.record==NULL)
				return pv_get_null(msg, param, res);
			return pv_geoip_get_strzval(msg, param, res,
					gpv->item->r.record->continent_code);
		default: /* cc */
			if(gpv->item->r.record==NULL)
				return pv_get_null(msg, param, res);
			return pv_geoip_get_strzval(msg, param, res,
					gpv->item->r.record->country_code);
	}
}