Skip to content

Commit e89a4c0

Browse files
committed
Added supports for provider field and error handling for invalid BIN file.
1 parent 436bd40 commit e89a4c0

File tree

6 files changed

+62
-20
lines changed

6 files changed

+62
-20
lines changed

Developers_Guide.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,4 @@ Call to IP2Proxy_close will not deleted the shared memory, it will only detach t
6969

7070
When IP2Proxy_delete_shared_memory() function is called, and if any other process(es) is attached to shared memory, it will only delete the name of the shared memory but the other process(es) will continue to use memory and memory will be freed only after last attached process is detached from it.
7171

72-
After calling IP2Proxy_delete_shared_memory(), the next call to IP2Proxy_set_lookup_mode() with IP2PROXY_SHARED_MEMORY option will result in a new shared memory and will not reuse the old one if one exists and used by any other process. Please refer shm_open and shm_unlink man pages for more info.
73-
74-
75-
Version 3.1.0 28/09/2020
72+
After calling IP2Proxy_delete_shared_memory(), the next call to IP2Proxy_set_lookup_mode() with IP2PROXY_SHARED_MEMORY option will result in a new shared memory and will not reuse the old one if one exists and used by any other process. Please refer shm_open and shm_unlink man pages for more info.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Below are the methods supported in this library.
6363
| IP2Proxy_get_as | Return autonomous system (AS) name. |
6464
| IP2Proxy_get_last_seen | Return proxy last seen in days. |
6565
| IP2Proxy_get_threat | Return security threat reported. |
66+
| IP2Proxy_get_provider | Return name of VPN provider if available. |
6667

6768

6869

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
AC_PREREQ(2.59)
22

3-
AC_INIT([IP2Proxy], [3.1.0])
3+
AC_INIT([IP2Proxy], [4.0.0])
44
AC_CONFIG_SRCDIR([libIP2Proxy/IP2Proxy.c])
55
AM_INIT_AUTOMAKE
66

libIP2Proxy/IP2Proxy.c

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,18 @@ typedef struct ip_container {
4343
struct in6_addr ipv6;
4444
} ip_container;
4545

46-
uint8_t IP2PROXY_COUNTRY_POSITION[11] = {0, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3};
47-
uint8_t IP2PROXY_REGION_POSITION[11] = {0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4};
48-
uint8_t IP2PROXY_CITY_POSITION[11] = {0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5};
49-
uint8_t IP2PROXY_ISP_POSITION[11] = {0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6};
50-
uint8_t IP2PROXY_PROXY_TYPE_POSITION[11] = {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2};
51-
uint8_t IP2PROXY_DOMAIN_POSITION[11] = {0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7};
52-
uint8_t IP2PROXY_USAGE_TYPE_POSITION[11] = {0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8};
53-
uint8_t IP2PROXY_ASN_POSITION[11] = {0, 0, 0, 0, 0, 0, 0, 9, 9, 9, 9};
54-
uint8_t IP2PROXY_AS_POSITION[11] = {0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10};
55-
uint8_t IP2PROXY_LAST_SEEN_POSITION[11] = {0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11};
56-
uint8_t IP2PROXY_THREAT_POSITION[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12};
46+
uint8_t IP2PROXY_PROXY_TYPE_POSITION[12] = {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
47+
uint8_t IP2PROXY_COUNTRY_POSITION[12] = {0, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
48+
uint8_t IP2PROXY_REGION_POSITION[12] = {0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4};
49+
uint8_t IP2PROXY_CITY_POSITION[12] = {0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5};
50+
uint8_t IP2PROXY_ISP_POSITION[12] = {0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 6};
51+
uint8_t IP2PROXY_DOMAIN_POSITION[12] = {0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7};
52+
uint8_t IP2PROXY_USAGE_TYPE_POSITION[12] = {0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8};
53+
uint8_t IP2PROXY_ASN_POSITION[12] = {0, 0, 0, 0, 0, 0, 0, 9, 9, 9, 9, 9};
54+
uint8_t IP2PROXY_AS_POSITION[12] = {0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10};
55+
uint8_t IP2PROXY_LAST_SEEN_POSITION[12] = {0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11, 11};
56+
uint8_t IP2PROXY_THREAT_POSITION[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 12};
57+
uint8_t IP2PROXY_PROVIDER_POSITION[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13};
5758

5859
// Static variables
5960
static int32_t is_in_memory = 0;
@@ -94,6 +95,15 @@ IP2Proxy *IP2Proxy_open(char *bin)
9495

9596
IP2Proxy_initialize(handler);
9697

98+
if (handler->product_code == 2) {
99+
} else {
100+
if (handler->database_year <= 20 && handler->product_code == 0) {
101+
} else {
102+
printf(INVALID_BIN_DATABASE);
103+
return NULL;
104+
}
105+
}
106+
97107
return handler;
98108
}
99109

@@ -223,6 +233,9 @@ static int IP2Proxy_initialize(IP2Proxy *handler)
223233
handler->ipv6_database_address = IP2Proxy_read32(handler->file, 18);
224234
handler->ipv4_index_base_address = IP2Proxy_read32(handler->file, 22);
225235
handler->ipv6_index_base_address = IP2Proxy_read32(handler->file, 26);
236+
handler->product_code = IP2Proxy_read8(handler->file, 30);
237+
handler->license_code = IP2Proxy_read8(handler->file, 31);
238+
handler->database_size = IP2Proxy_read32(handler->file, 32);
226239

227240
return 0;
228241
}
@@ -366,6 +379,12 @@ IP2ProxyRecord *IP2Proxy_get_threat(IP2Proxy *handler, char *ip)
366379
return IP2Proxy_get_record(handler, ip, THREAT);
367380
}
368381

382+
// Get Provider
383+
IP2ProxyRecord *IP2Proxy_get_provider(IP2Proxy *handler, char *ip)
384+
{
385+
return IP2Proxy_get_record(handler, ip, PROVIDER);
386+
}
387+
369388
// Get all records of an IP address
370389
IP2ProxyRecord *IP2Proxy_get_all(IP2Proxy *handler, char *ip)
371390
{
@@ -389,6 +408,7 @@ static IP2ProxyRecord *IP2Proxy_bad_record(const char *message)
389408
record->as_ = strdup(message);
390409
record->last_seen = strdup(message);
391410
record->threat = strdup(message);
411+
record->provider = strdup(message);
392412

393413
return record;
394414
}
@@ -546,6 +566,16 @@ static IP2ProxyRecord *IP2Proxy_read_record(IP2Proxy *handler, uint32_t rowaddr,
546566
}
547567
}
548568

569+
if ((mode & PROVIDER) && (IP2PROXY_PROVIDER_POSITION[dbtype] != 0)) {
570+
if (!record->provider) {
571+
record->provider = IP2Proxy_read_string(handle, IP2Proxy_read32(handle, rowaddr + 4 * (IP2PROXY_PROVIDER_POSITION[dbtype]-1)));
572+
}
573+
} else {
574+
if (!record->provider) {
575+
record->provider = strdup(NOT_SUPPORTED);
576+
}
577+
}
578+
549579
return record;
550580
}
551581

@@ -600,6 +630,7 @@ static IP2ProxyRecord *IP2Proxy_get_ipv4_record(IP2Proxy *handler, uint32_t mode
600630
record->as_ = NOT_SUPPORTED;
601631
record->last_seen = NOT_SUPPORTED;
602632
record->threat = NOT_SUPPORTED;
633+
record->provider = NOT_SUPPORTED;
603634

604635
while (fgets(line, 2048, handle) != NULL) {
605636
IP2Proxy_replace(line, "\n", "");
@@ -634,6 +665,7 @@ static IP2ProxyRecord *IP2Proxy_get_ipv4_record(IP2Proxy *handler, uint32_t mode
634665
record->as_ = "-";
635666
record->last_seen = "-";
636667
record->threat = "-";
668+
record->provider = "-";
637669

638670
return record;
639671
}
@@ -879,6 +911,10 @@ void IP2Proxy_free_record(IP2ProxyRecord *record)
879911
free(record->threat);
880912
}
881913

914+
if (record->threat != NULL) {
915+
free(record->provider);
916+
}
917+
882918
free(record);
883919
}
884920

libIP2Proxy/IP2Proxy.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* IP2Proxy C library is distributed under MIT license
3-
* Copyright (c) 2013-2020 IP2Location.com. support at ip2location dot com
3+
* Copyright (c) 2013-2021 IP2Location.com. support at ip2location dot com
44
*
55
* This library is free software; you can redistribute it and/or
66
* modify it under the terms of the MIT license
@@ -54,9 +54,9 @@ extern "C" {
5454
#endif
5555
#endif
5656

57-
#define API_VERSION 3.2.0
58-
#define API_VERSION_MAJOR 3
59-
#define API_VERSION_MINOR 2
57+
#define API_VERSION 4.0.0
58+
#define API_VERSION_MAJOR 4
59+
#define API_VERSION_MINOR 0
6060
#define API_VERSION_RELEASE 0
6161
#define API_VERSION_NUMERIC (((API_VERSION_MAJOR * 100) + API_VERSION_MINOR) * 100 + API_VERSION_RELEASE)
6262

@@ -78,12 +78,14 @@ extern "C" {
7878
#define AS 0x00400
7979
#define LASTSEEN 0x00800
8080
#define THREAT 0x01000
81+
#define PROVIDER 0x01200
8182

8283
#define ALL COUNTRYSHORT | COUNTRYLONG | REGION | CITY | ISP | ISPROXY | PROXYTYPE | DOMAINNAME | USAGETYPE | ASN | AS | LASTSEEN | THREAT
8384

8485
#define INVALID_IP_ADDRESS "INVALID IP ADDRESS"
8586
#define IPV6_ADDRESS_MISSING_IN_IPV4_BIN "IPV6 ADDRESS MISSING IN IPV4 BIN"
8687
#define NOT_SUPPORTED "NOT SUPPORTED"
88+
#define INVALID_BIN_DATABASE "Incorrect IP2Proxy BIN file format. Please make sure that you are using the latest IP2Proxy BIN file."
8789
#define IP2PROXY_SHM "/IP2Proxy_Shm"
8890
#define MAP_ADDR 4194500608
8991

@@ -101,12 +103,15 @@ typedef struct {
101103
uint8_t database_day;
102104
uint8_t database_month;
103105
uint8_t database_year;
106+
uint8_t product_code;
107+
uint8_t license_code;
104108
uint32_t ipv4_database_count;
105109
uint32_t ipv4_database_address;
106110
uint32_t ipv4_index_base_address;
107111
uint32_t ipv6_database_count;
108112
uint32_t ipv6_database_address;
109113
uint32_t ipv6_index_base_address;
114+
uint32_t database_size;
110115
} IP2Proxy;
111116

112117
typedef struct {
@@ -123,6 +128,7 @@ typedef struct {
123128
char *as_;
124129
char *last_seen;
125130
char *threat;
131+
char *provider;
126132
} IP2ProxyRecord;
127133

128134
/* Public functions */
@@ -153,6 +159,7 @@ IP2ProxyRecord *IP2Proxy_get_region(IP2Proxy *handler, char *ip);
153159
IP2ProxyRecord *IP2Proxy_get_threat(IP2Proxy *handler, char *ip);
154160
IP2ProxyRecord *IP2Proxy_get_usage_type(IP2Proxy *handler, char *ip);
155161
IP2ProxyRecord *IP2Proxy_is_proxy(IP2Proxy *handler, char *ip);
162+
IP2ProxyRecord *IP2Proxy_get_provider(IP2Proxy *handler, char *ip);
156163

157164
uint32_t IP2Proxy_close(IP2Proxy *handler);
158165
void IP2Proxy_free_record(IP2ProxyRecord *record);

test/test-IP2Proxy.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ int main ()
5353
fprintf(stdout, "AS: %s\n", record->as_);
5454
fprintf(stdout, "Last Seen: %s\n", record->last_seen);
5555
fprintf(stdout, "Threat: %s\n", record->threat);
56+
fprintf(stdout, "Provider: %s\n", record->provider);
5657

5758
IP2Proxy_close(IP2ProxyObj);
5859
IP2Proxy_free_record(record);

0 commit comments

Comments
 (0)