ipapi-zig
is a Zig library for querying 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 integrate this library into your own Zig project, you can use git
and the build.zig
system.
- Zig version 0.10 or later.
Clone the repository into your project directory:
git clone https://github.com/rezwanahmedsami/ipapi-zig.git
In your project’s build.zig
, add this library as a dependency. For example:
const std = @import("std");
const ipapi = b.addExecutable("ipapi-example", "src/main.zig");
ipapi.linkLibC();
ipapi.addPackagePath("ipapi", "./path/to/ipapi-zig");
const ipapi_zig = ipapi.addPackage("ipapi", "./path/to/ipapi-zig/src");
After adding the library, you can build your project as usual with:
zig build
To query information about a specific IP address, you can use the queryIp
function. Here's an example of how to use it:
const std = @import("std");
const ipapi = @import("ipapi.zig");
pub fn main() void {
const allocator = std.heap.page_allocator;
// Create the response buffer to store the result from cURL
var response_buffer = std.ArrayList(u8).init(allocator);
// Free the response buffer after usage (!!!!Important!!!!)
defer response_buffer.deinit();
// Query a specific IP address
const result = ipapi.queryIp("8.8.8.8", allocator, &response_buffer);
std.debug.print("{}\n", .{result});
}
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: "94043",
latitude: 37.436,
longitude: -122.0938,
timezone: "America/Los_Angeles",
localtime: "2024-12-11T18:26:48"
},
risk: {
is_mobile: false,
is_vpn: false,
is_tor: false,
is_proxy: false,
is_datacenter: true,
risk_score: 0
}
}
To query multiple IP addresses at once, use the queryBulk
function:
const std = @import("std");
const ipapi = @import("ipapi.zig");
pub fn main() void {
const allocator = std.heap.page_allocator;
// Create the response buffer to store the result from cURL
var response_buffer = std.ArrayList(u8).init(allocator);
// Free the response buffer after usage (!!!!Important!!!!)
defer response_buffer.deinit();
// Define multiple IPs to query
const ips = &[_][]const u8{ "8.8.8.8", "1.1.1.1" };
// Perform bulk query
const results = ipapi.queryBulk(ips, allocator, &response_buffer);
// Print the results for each IP
for (results) |ip_info| {
std.debug.print("Queried IP: {s}\n", .{ip_info.ip});
}
}
Queried IP: 8.8.8.8
Queried IP: 1.1.1.1
Use the queryOwnIp
function to fetch your own public IP address:
const std = @import("std");
const ipapi = @import("ipapi.zig");
pub fn main() void {
const allocator = std.heap.page_allocator;
// Get the public IP address of the current machine
const own_ip = ipapi.queryOwnIp();
std.debug.print("Own IP: {s}\n", .{own_ip});
}
Own IP: 203.0.113.45
fn queryIp(ip: []const u8, allocator: std.mem.Allocator, response_buffer: *std.ArrayList(u8)) !IPInfo;
Fetches detailed information about a specific IP address, including its ISP, location, and risk information.
ip
: The IP address to query (as a[]const u8
).allocator
: The allocator to use for memory allocation.response_buffer
: A buffer to store the response from cURL.
- An
IPInfo
struct containing details about the queried IP address. - An error if the query fails.
fn queryBulk(ips: [][]const u8, allocator: std.mem.Allocator, response_buffer: *std.ArrayList(u8)) ![]IPInfo;
Fetches information for multiple IP addresses in a single request.
ips
: A list of IP addresses to query (as a[][]const u8
).allocator
: The allocator to use for memory allocation.response_buffer
: A buffer to store the response from cURL.
- A slice of
IPInfo
structs containing the information for each IP address. - An error if the query fails.
fn queryOwnIp() ![]const u8;
Fetches the public IP address of the current machine.
- A
[]const u8
containing the public IP address. - An error if the query fails.
To run the library tests (if any), use the following command:
zig build test
This will execute any tests defined within the project and display the results in the terminal.
Contributions are welcome! If you find any bugs or have improvements, feel free to submit an issue or pull request to the GitHub repository.