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; }
static ngx_http_ip2location_ctx_t * ngx_http_ip2location_create_ctx(ngx_http_request_t *r) { ngx_array_t *xfwd; ngx_http_ip2location_ctx_t *ctx; ngx_pool_cleanup_t *cln; ngx_http_ip2location_main_conf_t *imcf; ngx_addr_t addr; u_char address[NGX_INET6_ADDRSTRLEN + 1]; size_t size; ctx = ngx_http_get_module_ctx(r, ngx_http_ip2location_module); if (ctx) { return ctx; } ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_ip2location_ctx_t)); if (ctx == NULL) { return NULL; } ngx_http_set_ctx(r, ctx, ngx_http_ip2location_module); imcf = ngx_http_get_module_main_conf(r, ngx_http_ip2location_module); addr.sockaddr = r->connection->sockaddr; addr.socklen = r->connection->socklen; xfwd = &r->headers_in.x_forwarded_for; if (xfwd->nelts > 0 && imcf->proxies != NULL) { (void) ngx_http_get_forwarded_addr(r, &addr, xfwd, NULL, imcf->proxies, imcf->proxy_recursive); } #if defined(nginx_version) && (nginx_version) >= 1005003 size = ngx_sock_ntop(addr.sockaddr, addr.socklen, address, NGX_INET6_ADDRSTRLEN, 0); #else size = ngx_sock_ntop(addr.sockaddr, address, NGX_INET6_ADDRSTRLEN, 0); #endif address[size] = '\0'; ctx->record = IP2Location_get_all(imcf->database, (char *)address); if (ctx->record == NULL) { ctx->not_found = 1; return ctx; } cln = ngx_pool_cleanup_add(r->pool, 0); if (cln == NULL) { ngx_http_set_ctx(r, NULL, ngx_http_ip2location_module); IP2Location_free_record(ctx->record); return NULL; } ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "http ip2location record (%s):\n" " country short: %s\n" " country long: %s\n" " region: %s\n" " city: %s\n" " isp: %s\n" " latitude: %f\n" " longitude: %f\n" " domain: %s\n" " zipcode: %s\n" " timezone: %s\n" " netspeed: %s\n" " iddcode: %s\n" " areacode: %s\n" " weatherstationcode: %s\n" " weatherstationname: %s\n" " mcc: %s\n" " mnc: %s\n" " mobilebrand: %s\n" " elevation: %f\n" " usagetype: %s\n", address, ctx->record->country_short, ctx->record->country_long, ctx->record->region, ctx->record->city, ctx->record->isp, ctx->record->latitude, ctx->record->longitude, ctx->record->domain, ctx->record->zipcode, ctx->record->timezone, ctx->record->netspeed, ctx->record->iddcode, ctx->record->areacode, ctx->record->weatherstationcode, ctx->record->weatherstationname, ctx->record->mcc, ctx->record->mnc, ctx->record->mobilebrand, ctx->record->elevation, ctx->record->usagetype); cln->data = ctx->record; cln->handler = (ngx_pool_cleanup_pt) IP2Location_free_record; return ctx; }
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; }