void
ngx_http_ip2location_cleanup(void *data)
{
    IP2Location *loc = data;
    IP2Location_close(loc);
    IP2Location_DB_del_shm();
}
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;
}
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;
}
Example #4
0
int main(int argc, char *argv[])
{
    int i;
    const char *datafile = NULL;
    const char *inputfile = NULL;
    const char *ip = NULL;
    const char *outputfile = NULL;
    const char *format = "CSV";
    const char *field = NULL;
    int noheading = 0;
    IP2Location *dbobj = NULL;
    IP2LocationRecord *record = NULL;
    FILE *fout = stdout;

    field = "ip,countryshort,countrylong,region,city,isp,latitude,longitude,domain,zipcode,timezone,netspeed,iddcode,areacode,weatherstationcode,weatherstationname,mcc,mnc,mobilebrand,elevation,usagetype";

    for (i = 1; i < argc; i++) {
        const char *argvi = argv[i];

        if (strcmp(argvi, "-d") == 0 || strcmp(argvi, "--datafile") == 0) {
            if (i + 1 < argc) {
                datafile = argv[++i];
            }
        } else if (strcmp(argvi, "-i") == 0 || strcmp(argvi, "--inputfile") == 0) {
            if (i + 1 < argc) {
                inputfile = argv[++i];
            }
        } else if (strcmp(argvi, "-p") == 0 || strcmp(argvi, "--ip") == 0) {
            if (i + 1 < argc) {
                ip = argv[++i];
            }
        } else if (strcmp(argvi, "-o") == 0 || strcmp(argvi, "--outputfile") == 0) {
            if (i + 1 < argc) {
                outputfile = argv[++i];
            }
        } else if (strcmp(argvi, "-f") == 0 || strcmp(argvi, "--format") == 0) {
            if (i + 1 < argc) {
                format = argv[++i];
            }
        } else if (strcmp(argvi, "-h") == 0 || strcmp(argvi, "-?") == 0 || strcmp(argvi, "--help") == 0) {
            print_usage(argv[0]);
            return 0;
        } else if (strcmp(argvi, "-v") == 0 || strcmp(argvi, "--version") == 0) {
            print_version();
            return 0;
        } else if (strcmp(argvi, "-e") == 0 || strcmp(argvi, "--field") == 0) {
            if (i + 1 < argc) {
                field = argv[++i];
            }
        } else if (strcmp(argvi, "-n") == 0 || strcmp(argvi, "--noheading") == 0) {
            noheading = 1;
        }
    }

    if (strcmp(format, "CSV") != 0 && strcmp(format, "XML") != 0 && strcmp(format, "TAB") != 0) {
        fprintf(stderr, "Invalid format %s, supported formats: CSV, XML, TAB\n", format);
        exit(-1);
    }

    if (datafile == NULL) {
        fprintf(stderr, "Datafile is absent\n");
        exit(-1);
    }

	if (ip == NULL) {
        fprintf(stderr, "IP address is absent\n");
        exit(-1);
    }
	
    dbobj = IP2Location_open((char *)datafile);
    if (dbobj == NULL) {
        fprintf(stderr, "Failed to open datafile %s\n", datafile);
        exit(-1);
    }

    if (outputfile != NULL) {
        fout = fopen(outputfile, "w");
        if (fout == NULL) {
            fprintf(stderr, "Failed to open outputfile %s\n", outputfile);
            exit(-1);
        }
    }

    if (!noheading) {
        print_header(fout, field, format);
    }

    if (ip != NULL) {
        record = IP2Location_get_all(dbobj, (char *)ip);
        print_record(fout, field, record, format, ip);
        IP2Location_free_record(record);
    }
    if (inputfile != NULL) {
        char *line = NULL;
        size_t n;
        ssize_t len;
        FILE *fin = fopen(inputfile, "r");

        if (fin == NULL) {
            fprintf(stderr, "Failed to open inputfile %s\n", inputfile);
            exit(-1);
        }

        while ((len = getline(&line, &n, fin)) != -1) {
            if (line[len - 1] == '\n') {
                line[--len] = '\0';
            }
            if (line[len - 1] == '\r') {
                line[--len] = '\0';
            }
            record = IP2Location_get_all(dbobj, line);
            print_record(fout, field, record, format, line);
            IP2Location_free_record(record);
        }

        fclose(fin);
    }
    print_footer(fout, field, format);
    IP2Location_close(dbobj);
    // OS will clean-up the rest
    return 0;
}