Skip to content

Commit

Permalink
Added supports for DB25.
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed May 20, 2021
1 parent 8c0fd77 commit 7a3198a
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 52 deletions.
2 changes: 1 addition & 1 deletion COPYING
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 IP2Location.com
Copyright (c) 2021 IP2Location.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion Developers_Guide.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ When IP2Location_clear_memory() function is called, and if any other process(es)
After calling IP2Location_clear_memory(), the next call to IP2Location_set_lookup_mode() with IP2LOCATION_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.


Version 8.3.1 09/11/2020
Version 8.4.0 20/05/2021
2 changes: 1 addition & 1 deletion LICENSE.TXT
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 IP2Location.com
Copyright (c) 2021 IP2Location.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

IP2Location is a C library that enables the user to find the country, region, city, latitude, longitude,
zip code, time zone, ISP, domain name, connection type, area code, weather, mobile network, elevation,
usage type by IP address or hostname originates from. The library reads the geo location information
usage type, address type and category by IP address or hostname originates from. The library reads the geo location information
from **IP2Location BIN data** file.

Supported IPv4 and IPv6 address.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

IP2Location is a C library that enables the user to find the country, region, city, latitude, longitude,
zip code, time zone, ISP, domain name, connection type, area code, weather, mobile network, elevation,
usage type by IP address or hostname originates from. The library reads the geo location information
usage type, address type and category by IP address or hostname originates from. The library reads the geo location information
from **IP2Location BIN data** file.

Supported IPv4 and IPv6 address.
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AC_PREREQ(2.59)
AC_INIT([IP2Loc], [8.3.1], [[email protected]])
AC_INIT([IP2Loc], [8.4.0], [[email protected]])
AM_INIT_AUTOMAKE([ subdir-objects ])

AM_CONFIG_HEADER([config.h])
Expand Down
5 changes: 4 additions & 1 deletion contrib/IP2Location.spec
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%define version 8.3.1
%define version 8.4.0

Name: IP2Location
Summary: C library for mapping IP address to geolocation information
Expand Down Expand Up @@ -122,6 +122,9 @@ install -p data/IPV6-COUNTRY.BIN %{buildroot}%{_datadir}/%{name}/IPV6-COUNTRY.SA


%changelog
* Thu May 20 2021 IP2Location <[email protected]> - 8.4.0
- added supports for DB25 to display address type and category fields

* Mon Nov 09 2020 IP2Location <[email protected]> - 8.3.1-1
- fixed version number

Expand Down
36 changes: 24 additions & 12 deletions contrib/IP2Location.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ BuildRequires: perl(Math::BigInt)
%description
IP2Location C library enables the user to get the country, region, city,
coordinates, ZIP code, time zone, ISP, domain name, connection type,
area code, weather info, mobile carrier, elevation and usage type from any IP
address or hostname. This library has been optimized for speed and memory
utilization. The library contains API to query all IP2Location LITE and
commercial binary databases.
area code, weather info, mobile carrier, elevation, usage type, address type
and category from any IP address or hostname. This library has been optimized
for speed and memory utilization. The library contains API to query all IP2Location
LITE and commercial binary databases.

Users can download the latest LITE database from IP2Location web site using e.g.
the included downloader.
Expand All @@ -31,10 +31,10 @@ Provides: %{name}-devel = %{version}-%{release}
%description devel
IP2Location C library enables the user to get the country, region, city,
coordinates, ZIP code, time zone, ISP, domain name, connection type,
area code, weather info, mobile carrier, elevation and usage type from any IP
address or hostname. This library has been optimized for speed and memory
utilization. The library contains API to query all IP2Location LITE and
commercial binary databases.
area code, weather info, mobile carrier, elevation, usage type, address type
and category from any IP address or hostname. This library has been optimized
for speed and memory utilization. The library contains API to query all IP2Location
LITE and commercial binary databases.

This package contains the development files for the IP2Location library.

Expand All @@ -46,10 +46,10 @@ Requires: %{name} = %{version}-%{release}
%description data-sample
IP2Location C library enables the user to get the country, region, city,
coordinates, ZIP code, time zone, ISP, domain name, connection type,
area code, weather info, mobile carrier, elevation and usage type from any IP
address or hostname. This library has been optimized for speed and memory
utilization. The library contains API to query all IP2Location LITE and
commercial binary databases.
area code, weather info, mobile carrier, elevation, usage type, address type
and category from any IP address or hostname. This library has been optimized
for speed and memory utilization. The library contains API to query all IP2Location
LITE and commercial binary databases.

This package contains the sample data files for testing the library.

Expand Down Expand Up @@ -122,6 +122,18 @@ install -p data/IPV6-COUNTRY.BIN %{buildroot}%{_datadir}/%{name}/IPV6-COUNTRY.SA


%changelog
* Thu May 20 2021 IP2Location <[email protected]> - 8.4.0
- added supports for DB25 to display address type and category fields

* Mon Nov 09 2020 IP2Location <[email protected]> - 8.3.1-1
- fixed version number

* Tue Nov 03 2020 IP2Location <[email protected]> - 8.3.1
- included ip2location lookup

* Fri Oct 30 2020 IP2Location <[email protected]> - 8.3.0
- added new interface "IP2Location_bin_version" to display BIN database version

* Thu Oct 01 2020 IP2Location <[email protected]> - 8.2.0
- reverted changes based on 8.0.9

Expand Down
4 changes: 2 additions & 2 deletions debian/copyright
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Upstream-Contact: IP2Location <[email protected]>
Source: https://github.com/chrislim2888/IP2Location-C-Library.git

Files: *
Copyright: 2020 IP2Location <[email protected]>
Copyright: 2021 IP2Location <[email protected]>
License: MIT

Files: debian/*
Copyright: 2020 IP2Location <[email protected]>
Copyright: 2021 IP2Location <[email protected]>
License: MIT

License: MIT
Expand Down
4 changes: 3 additions & 1 deletion ip2location.1
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Query an IP address and display the country_short and city result
\- mobile_brand
\- elevation
\- usage_type
\- address_type
\- category

\-n, \-\no-heading
Suppress the heading display.
Expand All @@ -91,6 +93,6 @@ Query an IP address and display the country_short and city result
This tool was created by IP2Location (http://www.ip2location.com).

.SH [COPYRIGHT AND LICENSE]
Copyright 2001\-2020 IP2Location.com
Copyright 2001\-2021 IP2Location.com

This tool is licensed under MIT.
12 changes: 11 additions & 1 deletion ip2location.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ static void print_usage(const char *argv0)
" usage_type\n"
" Usage type classification of ISP or company.\n"
"\n"
" address_type\n"
" IP address types as defined in Internet Protocol version 4 (IPv4) and Internet Protocol version 6 (IPv6).\n"
"\n"
" category\n"
" The domain category is based on IAB Tech Lab Content Taxonomy.\n"
"\n"
" -f, --format\n"
" Output format. Supported format:\n"
" CSV (default)\n"
Expand Down Expand Up @@ -165,6 +171,8 @@ static void print_header(FILE *fout, const char *field, const char *format)
WRITE_HEADER("mobile_brand");
WRITE_HEADER("elevation");
WRITE_HEADER("usage_type");
WRITE_HEADER("address_type");
WRITE_HEADER("category");

if (*end == ',') {
start = end + 1;
Expand Down Expand Up @@ -253,6 +261,8 @@ static void print_record(FILE *fout, const char *field, IP2LocationRecord *recor
WRITE_FIELD("mobile_brand", record->mobilebrand);
WRITE_FIELDF("elevation", record->elevation);
WRITE_FIELD("usage_type", record->usagetype);
WRITE_FIELD("address_type", record->address_type);
WRITE_FIELD("category", record->category);

if (*end == ',') {
start = end + 1;
Expand Down Expand Up @@ -283,7 +293,7 @@ int main(int argc, char *argv[])
IP2LocationRecord *record = NULL;
FILE *fout = stdout;

field = "ip,country_code,country_name,region_name,city_name,isp,latitude,longitude,domain,zip_code,time_zone,net_speed,idd_code,area_code,weather_station_code,weather_station_name,mcc,mnc,mobile_brand,elevation,usage_type";
field = "ip,country_code,country_name,region_name,city_name,isp,latitude,longitude,domain,zip_code,time_zone,net_speed,idd_code,area_code,weather_station_code,weather_station_name,mcc,mnc,mobile_brand,elevation,usage_type,address_type,category";

for (i = 1; i < argc; i++) {
const char *argvi = argv[i];
Expand Down
93 changes: 70 additions & 23 deletions libIP2Location/IP2Location.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* IP2Location C library is distributed under MIT license
* Copyright (c) 2013-2020 IP2Location.com. support at ip2location dot com
* Copyright (c) 2013-2021 IP2Location.com. support at ip2location dot com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the MIT license
Expand Down Expand Up @@ -44,25 +44,27 @@ typedef struct ip_container {
struct in6_addr ipv6;
} ip_container;

uint8_t COUNTRY_POSITION[25] = {0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
uint8_t REGION_POSITION[25] = {0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
uint8_t CITY_POSITION[25] = {0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4};
uint8_t ISP_POSITION[25] = {0, 0, 3, 0, 5, 0, 7, 5, 7, 0, 8, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 0, 9, 7, 9};
uint8_t LATITUDE_POSITION[25] = {0, 0, 0, 0, 0, 5, 5, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5};
uint8_t LONGITUDE_POSITION[25] = {0, 0, 0, 0, 0, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6};
uint8_t DOMAIN_POSITION[25] = {0, 0, 0, 0, 0, 0, 0, 6, 8, 0, 9, 0, 10, 0, 10, 0, 10, 0, 10, 8, 10, 0, 10, 8, 10};
uint8_t ZIPCODE_POSITION[25] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 7, 7, 7, 0, 7, 0, 7, 7, 7, 0, 7};
uint8_t TIMEZONE_POSITION[25] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 7, 8, 8, 8, 7, 8, 0, 8, 8, 8, 0, 8};
uint8_t NETSPEED_POSITION[25] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 11, 0, 11, 8, 11, 0, 11, 0, 11, 0, 11};
uint8_t IDDCODE_POSITION[25] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 12, 0, 12, 0, 12, 9, 12, 0, 12};
uint8_t AREACODE_POSITION[25] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 13, 0, 13, 0, 13, 10, 13, 0, 13};
uint8_t WEATHERSTATIONCODE_POSITION[25] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 14, 0, 14, 0, 14, 0, 14};
uint8_t WEATHERSTATIONNAME_POSITION[25] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 15, 0, 15, 0, 15, 0, 15};
uint8_t MCC_POSITION[25] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 16, 0, 16, 9, 16};
uint8_t MNC_POSITION[25] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 17, 0, 17, 10, 17};
uint8_t MOBILEBRAND_POSITION[25] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 18, 0, 18, 11, 18};
uint8_t ELEVATION_POSITION[25] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 19, 0, 19};
uint8_t USAGETYPE_POSITION[25] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 20};
uint8_t COUNTRY_POSITION[26] = {0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
uint8_t REGION_POSITION[26] = {0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
uint8_t CITY_POSITION[26] = {0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4};
uint8_t LATITUDE_POSITION[26] = {0, 0, 0, 0, 0, 5, 5, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5};
uint8_t LONGITUDE_POSITION[26] = {0, 0, 0, 0, 0, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6};
uint8_t ZIPCODE_POSITION[26] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 7, 7, 7, 0, 7, 0, 7, 7, 7, 0, 7, 7};
uint8_t TIMEZONE_POSITION[26] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 7, 8, 8, 8, 7, 8, 0, 8, 8, 8, 0, 8, 8};
uint8_t ISP_POSITION[26] = {0, 0, 3, 0, 5, 0, 7, 5, 7, 0, 8, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 0, 9, 7, 9, 9};
uint8_t DOMAIN_POSITION[26] = {0, 0, 0, 0, 0, 0, 0, 6, 8, 0, 9, 0, 10, 0, 10, 0, 10, 0, 10, 8, 10, 0, 10, 8, 10, 10};
uint8_t NETSPEED_POSITION[26] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 11, 0, 11, 8, 11, 0, 11, 0, 11, 0, 11, 11};
uint8_t IDDCODE_POSITION[26] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 12, 0, 12, 0, 12, 9, 12, 0, 12, 12};
uint8_t AREACODE_POSITION[26] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 13, 0, 13, 0, 13, 10, 13, 0, 13, 13};
uint8_t WEATHERSTATIONCODE_POSITION[26] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 14, 0, 14, 0, 14, 0, 14, 14};
uint8_t WEATHERSTATIONNAME_POSITION[26] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 15, 0, 15, 0, 15, 0, 15, 15};
uint8_t MCC_POSITION[26] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 16, 0, 16, 9, 16, 16};
uint8_t MNC_POSITION[26] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 17, 0, 17, 10, 17, 17};
uint8_t MOBILEBRAND_POSITION[26] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 18, 0, 18, 11, 18, 18};
uint8_t ELEVATION_POSITION[26] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 19, 0, 19, 19};
uint8_t USAGETYPE_POSITION[26] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 20, 20};
uint8_t ADDRESSTYPE_POSITION[26] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21};
uint8_t CATEGORY_POSITION[26] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22};

// Static variables
static int32_t is_in_memory = 0;
Expand Down Expand Up @@ -103,6 +105,15 @@ IP2Location *IP2Location_open(char *bin)

IP2Location_initialize(handler);

if (handler->product_code == 1) {
} else {
if (handler->database_year <= 20 && handler->product_code == 0) {
} else {
printf(INVALID_BIN_DATABASE);
return NULL;
}
}

return handler;
}

Expand All @@ -114,18 +125,18 @@ static int IP2Location_initialize(IP2Location *handler)
handler->database_year = IP2Location_read8(handler->file, 3);
handler->database_month = IP2Location_read8(handler->file, 4);
handler->database_day = IP2Location_read8(handler->file, 5);

handler->database_count = IP2Location_read32(handler->file, 6);
handler->database_address = IP2Location_read32(handler->file, 10);
handler->ip_version = IP2Location_read32(handler->file, 14);

handler->ipv4_database_count = IP2Location_read32(handler->file, 6);
handler->ipv4_database_address = IP2Location_read32(handler->file, 10);
handler->ipv6_database_count = IP2Location_read32(handler->file, 14);
handler->ipv6_database_address = IP2Location_read32(handler->file, 18);

handler->ipv4_index_base_address = IP2Location_read32(handler->file, 22);
handler->ipv6_index_base_address = IP2Location_read32(handler->file, 26);
handler->product_code = IP2Location_read8(handler->file, 30);
handler->license_code = IP2Location_read8(handler->file, 31);
handler->database_size = IP2Location_read32(handler->file, 32);

return 0;
}
Expand Down Expand Up @@ -381,6 +392,18 @@ IP2LocationRecord *IP2Location_get_usagetype(IP2Location *handler, char *ip)
return IP2Location_get_record(handler, ip, USAGETYPE);
}

// Get address type
IP2LocationRecord *IP2Location_get_addresstype(IP2Location *handler, char *ip)
{
return IP2Location_get_record(handler, ip, ADDRESSTYPE);
}

// Get category
IP2LocationRecord *IP2Location_get_category(IP2Location *handler, char *ip)
{
return IP2Location_get_record(handler, ip, CATEGORY);
}

// Get all records of an IP address
IP2LocationRecord *IP2Location_get_all(IP2Location *handler, char *ip)
{
Expand Down Expand Up @@ -422,6 +445,8 @@ static IP2LocationRecord *IP2Location_bad_record(const char *message)
record->weather_station_name = record->weatherstationname;
record->mobile_brand = record->mobilebrand;
record->usage_type = record->usagetype;
record->address_type = strdup(message);
record->category = strdup(message);

return record;
}
Expand Down Expand Up @@ -621,6 +646,26 @@ static IP2LocationRecord *IP2Location_read_record(IP2Location *handler, uint32_t
}
}

if ((mode & ADDRESSTYPE) && (ADDRESSTYPE_POSITION[database_type] != 0)) {
if (!record->address_type) {
record->address_type = IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (ADDRESSTYPE_POSITION[database_type] - 1)));
}
} else {
if (!record->address_type) {
record->address_type = strdup(NOT_SUPPORTED);
}
}

if ((mode & CATEGORY) && (CATEGORY_POSITION[database_type] != 0)) {
if (!record->category) {
record->category = IP2Location_readStr(handle, IP2Location_read32(handle, rowaddr + 4 * (CATEGORY_POSITION[database_type] - 1)));
}
} else {
if (!record->category) {
record->category = strdup(NOT_SUPPORTED);
}
}

// Create alias for the new variables
record->zip_code = record->zipcode;
record->time_zone = record->timezone;
Expand Down Expand Up @@ -778,6 +823,8 @@ void IP2Location_free_record(IP2LocationRecord *record) {
free(record->mnc);
free(record->mobilebrand);
free(record->usagetype);
free(record->address_type);
free(record->category);

free(record);
}
Expand Down
Loading

0 comments on commit 7a3198a

Please sign in to comment.