コード例 #1
0
static char *
ngx_http_ip2location_init_main_conf(ngx_conf_t *cf, void *data)
{
    ngx_http_ip2location_main_conf_t  *imcf = data;
    ngx_pool_cleanup_t                *cln;

    if (imcf->access_type == NGX_CONF_UNSET) {
        imcf->access_type = IP2LOCATION_SHARED_MEMORY;
    }

    if (imcf->enabled) {
        if (imcf->filename.len == 0) {
            ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
                          "ip2location enabled with no database specified in %s:%ui",
                          imcf->enable_file, imcf->enable_line);
            return NGX_CONF_ERROR;
        }

        // open database
        imcf->database = IP2Location_open((char *)imcf->filename.data);

        if (imcf->database == NULL) {
            ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
                          "can not open database file \"%V\" in %s:%ui",
                          &imcf->filename, imcf->database_file, imcf->database_line);
            return NGX_CONF_ERROR;
        }

        if (IP2Location_open_mem(imcf->database, imcf->access_type) == -1) {

            IP2Location_close(imcf->database);

            ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
                          "can not load database file %V using \"%V\" access type in %s:%ui",
                          &imcf->filename,
                          &imcf->access_type_name,
                          imcf->database_file,
                          imcf->database_line);

            return NGX_CONF_ERROR;
        }

        cln = ngx_pool_cleanup_add(cf->pool, 0);
        if (cln == NULL) {
            return NGX_CONF_ERROR;
        }

        cln->data = imcf->database;
        cln->handler = ngx_http_ip2location_cleanup;
    }

    return NGX_CONF_OK;
}
コード例 #2
0
int main () {
	FILE *f;
	char ipAddress[30];
	char expectedCountry[3];
	int failed = 0;
	int test_num = 1;

#ifdef WIN32
	IP2Location *IP2LocationObj = IP2Location_open("..\\data\\IP-COUNTRY.BIN");
#else
	IP2Location *IP2LocationObj = IP2Location_open("../data/IP-COUNTRY.BIN");
#endif
	IP2LocationRecord *record = NULL;
	
	printf("IP2Location API version: %s (%lu)\n", IP2Location_api_version_string(), IP2Location_api_version_num());

	if (IP2LocationObj == NULL)
	{
		printf("Please install the database in correct path.\n");
		return -1;
	}

	if(IP2Location_open_mem(IP2LocationObj, IP2LOCATION_SHARED_MEMORY) == -1)
	{
		fprintf(stderr, "IPv4: Call to IP2Location_open_mem failed\n");
	}
	
	f = fopen("country_test_ipv4_data.txt","r");

	while (fscanf(f, "%s", ipAddress) != EOF) {
		fscanf(f, "%s", expectedCountry);
		record = IP2Location_get_all(IP2LocationObj, ipAddress);
		if (record != NULL)	{
			if (strcmp(expectedCountry,record->country_short) != 0) {
				fprintf(stderr,"Test IP Address %s (Test %d) failed. We got %s but expected %s,\n",ipAddress,test_num,record->country_short,expectedCountry);
				failed++;
			}
			IP2Location_free_record(record);
			test_num++;
		} else {
			
		}
	}
	fclose(f);
	
	IP2Location_close(IP2LocationObj);
	/*Below call will delete the shared memory unless if any other process is attached it. 
	 *if any other process is attached to it, shared memory will be closed when last process
	 *attached to it closes the shared memory 
	 *If any process call IP2Location_delete_shm, next process which IP2Location_open_mem
	 *with shared memory option, will open the new shared memory.Deleted memory will not be available for
	 * any new process but will be accesible for the processes which are already using it. 
	 */
	IP2Location_delete_shm();
	if ((test_num > 1) && (failed == 0)) {
		fprintf(stdout, "IP2Location IPv4 Testing passed.\n");
	}
	
	/* --- IPv6 Testing --- */
#ifdef WIN32
	IP2LocationObj = IP2Location_open("..\\data\\IPV6-COUNTRY.BIN");
#else
	IP2LocationObj = IP2Location_open("../data/IPV6-COUNTRY.BIN");
#endif
	if ( IP2Location_open_mem(IP2LocationObj, IP2LOCATION_FILE_IO) == -1 )
	{
		fprintf(stderr, "IPv6: Call to IP2Location_open_mem failed\n");
	}	
	record = NULL;
	if (IP2LocationObj == NULL)
	{
		printf("Please install the database in correct path.\n");
		return -1;
	}

	f = fopen("country_test_ipv6_data.txt","r");
	
	while (fscanf(f, "%s", ipAddress) != EOF) {
		fscanf(f, "%s", expectedCountry);
		record = IP2Location_get_all(IP2LocationObj, ipAddress);
		if (strcmp(expectedCountry,record->country_short) != 0) {
			fprintf(stdout,"Test IP Address %s (Test %d) failed. We got %s but expected %s,\n",ipAddress,test_num,record->country_short,expectedCountry);
			failed++;
		}
		IP2Location_free_record(record);
		test_num++;
	}
	
	fclose(f);
	
	IP2Location_close(IP2LocationObj);
	
	if ((test_num > 1) && (failed == 0)) {
		fprintf(stdout, "IP2Location IPv6 Testing passed.\n");
	}
		
	return failed;
}