A C++ library to query IP addresses using the ipquery.io API.
- Query details for a specific IP address
- Bulk query multiple IP addresses
- Fetch your own public IP address
To use this library, include the ipapi.h and link the necessary dependencies for HTTP requests (e.g., libcurl).
- A C++17 or later compiler
libcurlfor making HTTP requests
Install libcurl:
# Ubuntu
sudo apt-get install libcurl4-openssl-dev
# macOS (using Homebrew)
brew install curlInclude the following files in your project:
ipapi.hipapi.cpp
The query_ip function retrieves information about a specific IP address, including its ISP, location, and risk data.
#include <iostream>
#include "ipapi.h"
int main() {
auto ip_info = ipapi::query_ip("8.8.8.8");
if (ip_info) {
std::cout << *ip_info << std::endl;
} else {
std::cerr << "Failed to fetch IP information." << std::endl;
}
return 0;
}IPInfo {
ip: "8.8.8.8",
isp: { asn: "AS15169", org: "Google LLC", isp: "Google LLC" },
location: {
country: "United States",
country_code: "US",
city: "Mountain View",
state: "California",
zipcode: "94035",
latitude: 37.386,
longitude: -122.0838,
timezone: "America/Los_Angeles",
localtime: "2024-11-09T12:45:32"
},
risk: {
is_mobile: false,
is_vpn: false,
is_tor: false,
is_proxy: false,
is_datacenter: true,
risk_score: 0
}
}
The query_bulk function allows you to query information for multiple IP addresses at once.
#include <iostream>
#include "ipapi.h"
int main() {
std::vector<std::string> ips = {"8.8.8.8", "1.1.1.1"};
auto ip_infos = ipapi::query_bulk(ips);
for (const auto& info : ip_infos) {
std::cout << info << std::endl;
}
return 0;
}IPInfo {
ip: "8.8.8.8",
...
}
IPInfo {
ip: "1.1.1.1",
...
}
The query_own_ip function retrieves the public IP address of the current machine.
#include <iostream>
#include "ipapi.h"
int main() {
auto ip = ipapi::query_own_ip();
if (ip) {
std::cout << "Your IP Address: " << *ip << std::endl;
} else {
std::cerr << "Failed to fetch public IP address." << std::endl;
}
return 0;
}Your IP Address: 203.0.113.45
std::optional<IPInfo> query_ip(const std::string& ip);Fetches detailed information about a specific IP address, including its ISP, location, and risk information.
ip: Astd::stringrepresenting the IP address to query.
std::optional<IPInfo>containing details about the IP address on success.std::nulloptif the network request fails.
std::vector<IPInfo> query_bulk(const std::vector<std::string>& ips);Fetches information for multiple IP addresses at once. Useful for batch processing.
ips: Astd::vector<std::string>containing the list of IP addresses to query.
- A
std::vector<IPInfo>containing details for each IP address.
std::optional<std::string> query_own_ip();Fetches the public IP address of the current machine.
std::optional<std::string>containing the public IP address on success.std::nulloptif the network request fails.
To run tests for this library, include and execute the tests.cpp file:
# initialize the cmake build first
cmake -S . -B build
# Build and test
cmake --build build
cd build && ctestContributions are welcome! Feel free to open issues or submit pull requests on the GitHub repository.
This project is licensed under the MIT License. See the LICENSE file for details.