First Commit
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
.zig-cache
|
||||
zig-out
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
# The No Vibe Coders Open Source License
|
||||
|
||||
```
|
||||
Copyright (C) 2026 William Welna (wwelna@occultusterra.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
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following condition.
|
||||
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
In addition, the following restrictions apply:
|
||||
|
||||
* The software, either in source or compiled binary form, with or without any
|
||||
modification, may not be used with or incorporated into any other software
|
||||
that used an Artificial Intelligence (AI) model and/or Large Language Model
|
||||
(LLM) to generate any portion of that other software's source code, binaries,
|
||||
or artwork.
|
||||
* The usage of this software is not authorized in the state of California nor are
|
||||
residents of California authorized to be in possession of this software, either
|
||||
in source or compiled binary form.
|
||||
* The usage of this software is not authorized in the state of Colorado nor are
|
||||
residents of Colorado authorized to be in possession of this software, either
|
||||
in source or compiled binary form.
|
||||
* The usage and/or possession of this software, in either source or compiled
|
||||
binary form, is not authorized in any legal jurisdiction where there is a
|
||||
requirement of any age verification mechanism and/or identity verification
|
||||
mechanism as a requirement to use this software under threat of criminal
|
||||
and/or civil penalties.
|
||||
* If the unauthorized usage of this software results in financial penalties
|
||||
towards the developer, William Welna, the responsible individual, corporation,
|
||||
or other legal entity agrees to fully cover said financial penalties.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
```
|
||||
@@ -0,0 +1,10 @@
|
||||
# cidr-rdns
|
||||
|
||||
```
|
||||
CIDR Reverse DNS Lookup (https://occultusterra.com/WWelna/cidr-rdns-zig)
|
||||
Copyright (C) 2026 William Welna (wwelna@occultusterra.com)
|
||||
|
||||
Usage: cidr-rdns <ip/cidr> <optional output file>
|
||||
Example: cidr-rdns 127.0.0.0/24 localrdns.json
|
||||
Example: cidr-rdns 127.0.0.0/24
|
||||
```
|
||||
@@ -0,0 +1,41 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.Build) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "cidr-rdns",
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
.imports = &.{},
|
||||
.link_libc = true,
|
||||
}),
|
||||
});
|
||||
|
||||
exe.use_llvm = true;
|
||||
exe.use_lld = true;
|
||||
|
||||
exe.root_module.linkSystemLibrary("cares", .{});
|
||||
|
||||
b.installArtifact(exe);
|
||||
|
||||
const run_step = b.step("run", "Run the app");
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| {
|
||||
run_cmd.addArgs(args);
|
||||
}
|
||||
|
||||
const exe_tests = b.addTest(.{
|
||||
.root_module = exe.root_module,
|
||||
});
|
||||
const run_exe_tests = b.addRunArtifact(exe_tests);
|
||||
|
||||
const test_step = b.step("test", "Run tests");
|
||||
test_step.dependOn(&run_exe_tests.step);
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
.{
|
||||
// This is the default name used by packages depending on this one. For
|
||||
// example, when a user runs `zig fetch --save <url>`, this field is used
|
||||
// as the key in the `dependencies` table. Although the user can choose a
|
||||
// different name, most users will stick with this provided value.
|
||||
//
|
||||
// It is redundant to include "zig" in this name because it is already
|
||||
// within the Zig package namespace.
|
||||
.name = .cidr_rdns_zig,
|
||||
// This is a [Semantic Version](https://semver.org/).
|
||||
// In a future version of Zig it will be used for package deduplication.
|
||||
.version = "0.0.0",
|
||||
// Together with name, this represents a globally unique package
|
||||
// identifier. This field is generated by the Zig toolchain when the
|
||||
// package is first created, and then *never changes*. This allows
|
||||
// unambiguous detection of one package being an updated version of
|
||||
// another.
|
||||
//
|
||||
// When forking a Zig project, this id should be regenerated (delete the
|
||||
// field and run `zig build`) if the upstream project is still maintained.
|
||||
// Otherwise, the fork is *hostile*, attempting to take control over the
|
||||
// original project's identity. Thus it is recommended to leave the comment
|
||||
// on the following line intact, so that it shows up in code reviews that
|
||||
// modify the field.
|
||||
.fingerprint = 0x60d447abe05dca5, // Changing this has security and trust implications.
|
||||
// Tracks the earliest Zig version that the package considers to be a
|
||||
// supported use case.
|
||||
.minimum_zig_version = "0.16.0",
|
||||
// This field is optional.
|
||||
// Each dependency must either provide a `url` and `hash`, or a `path`.
|
||||
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
|
||||
// Once all dependencies are fetched, `zig build` no longer requires
|
||||
// internet connectivity.
|
||||
.dependencies = .{
|
||||
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
|
||||
//.example = .{
|
||||
// // When updating this field to a new URL, be sure to delete the corresponding
|
||||
// // `hash`, otherwise you are communicating that you expect to find the old hash at
|
||||
// // the new URL. If the contents of a URL change this will result in a hash mismatch
|
||||
// // which will prevent zig from using it.
|
||||
// .url = "https://example.com/foo.tar.gz",
|
||||
//
|
||||
// // This is computed from the file contents of the directory of files that is
|
||||
// // obtained after fetching `url` and applying the inclusion rules given by
|
||||
// // `paths`.
|
||||
// //
|
||||
// // This field is the source of truth; packages do not come from a `url`; they
|
||||
// // come from a `hash`. `url` is just one of many possible mirrors for how to
|
||||
// // obtain a package matching this `hash`.
|
||||
// //
|
||||
// // Uses the [multihash](https://multiformats.io/multihash/) format.
|
||||
// .hash = "...",
|
||||
//
|
||||
// // When this is provided, the package is found in a directory relative to the
|
||||
// // build root. In this case the package's hash is irrelevant and therefore not
|
||||
// // computed. This field and `url` are mutually exclusive.
|
||||
// .path = "foo",
|
||||
//
|
||||
// // When this is set to `true`, a package is declared to be lazily
|
||||
// // fetched. This makes the dependency only get fetched if it is
|
||||
// // actually used.
|
||||
// .lazy = false,
|
||||
//},
|
||||
},
|
||||
// Specifies the set of files and directories that are included in this package.
|
||||
// Only files and directories listed here are included in the `hash` that
|
||||
// is computed for this package. Only files listed here will remain on disk
|
||||
// when using the zig package manager. As a rule of thumb, one should list
|
||||
// files required for compilation plus any license(s).
|
||||
// Paths are relative to the build root. Use the empty string (`""`) to refer to
|
||||
// the build root itself.
|
||||
// A directory listed here means that all files within, recursively, are included.
|
||||
.paths = .{
|
||||
"build.zig",
|
||||
"build.zig.zon",
|
||||
"src",
|
||||
// For example...
|
||||
//"LICENSE",
|
||||
//"README.md",
|
||||
},
|
||||
}
|
||||
+254
@@ -0,0 +1,254 @@
|
||||
// Copyright (C) 2026 William Welna (wwelna@occultusterra.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
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following condition.
|
||||
|
||||
// * The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
|
||||
// In addition, the following restrictions apply:
|
||||
|
||||
// * The software, either in source or compiled binary form, with or without any
|
||||
// modification, may not be used with or incorporated into any other software
|
||||
// that used an Artificial Intelligence (AI) model and/or Large Language Model
|
||||
// (LLM) to generate any portion of that other software's source code, binaries,
|
||||
// or artwork.
|
||||
// * The usage of this software is not authorized in the state of California nor are
|
||||
// residents of California authorized to be in possession of this software, either
|
||||
// in source or compiled binary form.
|
||||
// * The usage of this software is not authorized in the state of Colorado nor are
|
||||
// residents of Colorado authorized to be in possession of this software, either
|
||||
// in source or compiled binary form.
|
||||
// * The usage and/or possession of this software, in either source or compiled
|
||||
// binary form, is not authorized in any legal jurisdiction where there is a
|
||||
// requirement of any age verification mechanism and/or identity verification
|
||||
// mechanism as a requirement to use this software under threat of criminal
|
||||
// and/or civil penalties.
|
||||
// * If the unauthorized usage of this software results in financial penalties
|
||||
// towards the developer, William Welna, the responsible individual, corporation,
|
||||
// or other legal entity agrees to fully cover said financial penalties.
|
||||
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
|
||||
const c = @cImport({
|
||||
@cInclude("ares.h");
|
||||
@cInclude("netdb.h");
|
||||
@cInclude("string.h");
|
||||
@cInclude("sys/select.h");
|
||||
@cInclude("sys/time.h");
|
||||
});
|
||||
|
||||
const Entry = struct {
|
||||
ip: u32,
|
||||
ip_str: []u8,
|
||||
hostname: []u8,
|
||||
};
|
||||
|
||||
var global_entries: std.ArrayList(Entry) = undefined;
|
||||
var global_counter: usize = undefined;
|
||||
var global_ares_chan: c.ares_channel = undefined;
|
||||
var allocator: std.mem.Allocator = undefined;
|
||||
|
||||
inline fn ip2String(ip_num: u32, buf: *[16]u8) ![]u8 {
|
||||
const w = (ip_num >> 24) & 0xFF;
|
||||
const x = (ip_num >> 16) & 0xFF;
|
||||
const y = (ip_num >> 8) & 0xFF;
|
||||
const z = ip_num & 0xFF;
|
||||
|
||||
const ret = try std.fmt.bufPrint(buf[0..15], "{d}.{d}.{d}.{d}", .{ w, x, y, z });
|
||||
return try allocator.dupe(u8, ret);
|
||||
}
|
||||
|
||||
inline fn String2ip(ip_str: []const u8) !u32 {
|
||||
var ret: u32 = 0;
|
||||
var parts = std.mem.splitScalar(u8, ip_str, '.');
|
||||
|
||||
var bytes: [4]u8 = undefined;
|
||||
bytes[3] = try std.fmt.parseInt(u8, parts.next().?, 10);
|
||||
bytes[2] = try std.fmt.parseInt(u8, parts.next().?, 10);
|
||||
bytes[1] = try std.fmt.parseInt(u8, parts.next().?, 10);
|
||||
bytes[0] = try std.fmt.parseInt(u8, parts.next().?, 10);
|
||||
ret = @as(u32, @bitCast(bytes));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
fn calculateSubnet(cidr: []const u8) !struct {
|
||||
start: u32,
|
||||
end: u32,
|
||||
total: u32,
|
||||
prefix: u8,
|
||||
ip_s: []u8,
|
||||
ip_e: []u8,
|
||||
} {
|
||||
var buf: [16]u8 = undefined;
|
||||
const slash = std.mem.findScalar(u8, cidr, '/') orelse 0;
|
||||
const ip_str = if (slash > 0) cidr[0..slash] else cidr;
|
||||
const prefix_str = if (slash > 0) cidr[slash + 1 ..] else "";
|
||||
|
||||
const prefix = if (slash > 0) try std.fmt.parseInt(u8, prefix_str, 10) else 0;
|
||||
if (prefix > 32) return error.InvalidPrefix;
|
||||
const mask: u32 = if (prefix == 0) 0 else (~@as(u32, 0)) << @as(u5, @intCast(32 - prefix));
|
||||
const ip = try String2ip(ip_str);
|
||||
|
||||
var start: u32 = undefined;
|
||||
var end: u32 = undefined;
|
||||
var ip_s: []u8 = undefined;
|
||||
var ip_e: []u8 = undefined;
|
||||
|
||||
if (prefix > 0) {
|
||||
start = ip & mask;
|
||||
end = start | (~mask & (~@as(u32, 0)));
|
||||
ip_s = try ip2String(start, &buf);
|
||||
ip_e = try ip2String(end, &buf);
|
||||
} else {
|
||||
start = ip;
|
||||
end = ip;
|
||||
ip_s = try allocator.dupe(u8, ip_str);
|
||||
ip_e = ip_s;
|
||||
}
|
||||
|
||||
const total = if (end > start) end - start + 1 else 1;
|
||||
|
||||
return .{
|
||||
.start = start,
|
||||
.end = end,
|
||||
.total = total,
|
||||
.prefix = prefix,
|
||||
.ip_s = ip_s,
|
||||
.ip_e = ip_e,
|
||||
};
|
||||
}
|
||||
|
||||
fn rdns_Callback(arg: ?*anyopaque, status: c_int, timeouts: c_int, host: ?*c.hostent) callconv(.c) void {
|
||||
const entry = @as(*Entry, @ptrCast(@alignCast(arg orelse return)));
|
||||
_ = timeouts;
|
||||
|
||||
if (status == c.ARES_SUCCESS and host != null) {
|
||||
const h_name = host.?.h_name;
|
||||
entry.hostname = allocator.dupe(u8, std.mem.span(h_name)) catch &[_]u8{};
|
||||
}
|
||||
|
||||
global_counter += 1;
|
||||
|
||||
if (global_counter < global_entries.items.len) {
|
||||
c.ares_gethostbyaddr(global_ares_chan, &global_entries.items[global_counter].ip, 4, c.AF_INET, rdns_Callback, &global_entries.items[global_counter]);
|
||||
}
|
||||
}
|
||||
|
||||
fn toFile(io: std.Io, fileName: []const u8) !void {
|
||||
var file = try std.Io.Dir.cwd().createFile(io, fileName, .{});
|
||||
defer file.close(io);
|
||||
|
||||
var buffer: [4096]u8 = undefined;
|
||||
var file_writer: std.Io.File.Writer = file.writer(io, &buffer);
|
||||
const writer = &file_writer.interface;
|
||||
|
||||
std.log.info("Opened {s} for writing.", .{fileName});
|
||||
|
||||
var stringify: std.json.Stringify = .{
|
||||
.writer = writer,
|
||||
};
|
||||
|
||||
for (global_entries.items) |entry| {
|
||||
try stringify.write(.{ .ip_str = entry.ip_str, .hostname = entry.hostname });
|
||||
try writer.writeByte('\n');
|
||||
}
|
||||
try writer.flush();
|
||||
std.log.info("Finished writting to file.", .{});
|
||||
}
|
||||
|
||||
pub fn main(init: std.process.Init) !void {
|
||||
allocator = init.arena.allocator();
|
||||
std.debug.print("CIDR Reverse DNS Lookup (https://occultusterra.com/WWelna/cidr-rdns-zig)\n", .{});
|
||||
std.debug.print("Copyright (C) 2026 William Welna (wwelna@occultusterra.com)\n\n", .{});
|
||||
|
||||
const args = try init.minimal.args.toSlice(allocator);
|
||||
const exe_name = std.fs.path.basename(args[0]);
|
||||
|
||||
if (args.len > 3 or args.len == 1) {
|
||||
std.debug.print("Usage: {s} <ip/cidr> <optional output file>\n", .{exe_name});
|
||||
std.debug.print("\tExample: {s} 127.0.0.0/24 localrdns.json\n", .{exe_name});
|
||||
std.debug.print("\tExample: {s} 127.0.0.0/24\n\n", .{exe_name});
|
||||
return;
|
||||
}
|
||||
|
||||
const cidr = args[1];
|
||||
|
||||
if (c.ares_library_init(c.ARES_LIB_INIT_ALL) != c.ARES_SUCCESS) {
|
||||
std.log.err("Error: ares_library_init() failed\n", .{});
|
||||
return;
|
||||
}
|
||||
defer c.ares_library_cleanup();
|
||||
|
||||
if (c.ares_init(&global_ares_chan) != c.ARES_SUCCESS) {
|
||||
std.log.err("Error: ares_init() failed\n", .{});
|
||||
return;
|
||||
}
|
||||
defer c.ares_destroy(global_ares_chan);
|
||||
|
||||
const subnet = try calculateSubnet(cidr);
|
||||
std.log.info("Resolving {s} → {s} ({d} IPs)", .{ subnet.ip_s, subnet.ip_e, subnet.total });
|
||||
|
||||
global_entries = try std.ArrayList(Entry).initCapacity(allocator, subnet.total);
|
||||
|
||||
var buf: [16]u8 = undefined;
|
||||
for (subnet.start..subnet.end + 1) |x| {
|
||||
try global_entries.append(allocator, .{ .ip = @intCast(x), .ip_str = try ip2String(@intCast(x), &buf), .hostname = "" });
|
||||
}
|
||||
|
||||
global_counter = @min(global_entries.items.len, 60);
|
||||
for (0..@min(global_entries.items.len, 60)) |x| {
|
||||
c.ares_gethostbyaddr(global_ares_chan, @ptrCast(&global_entries.items[x].ip), 4, c.AF_INET, rdns_Callback, @ptrCast(&global_entries.items[x]));
|
||||
}
|
||||
|
||||
// Resolve loop
|
||||
var readfds: c.fd_set = undefined;
|
||||
var writefds: c.fd_set = undefined;
|
||||
var tv: c.timeval = .{
|
||||
.tv_sec = 0,
|
||||
.tv_usec = 100_000,
|
||||
};
|
||||
|
||||
while (true) {
|
||||
@memset(std.mem.asBytes(&readfds), 0);
|
||||
@memset(std.mem.asBytes(&writefds), 0);
|
||||
|
||||
const nfds = c.ares_fds(global_ares_chan, &readfds, &writefds);
|
||||
if (nfds == 0) break;
|
||||
|
||||
const tvp: ?*c.timeval = c.ares_timeout(global_ares_chan, null, &tv);
|
||||
_ = c.select(nfds, &readfds, &writefds, null, tvp);
|
||||
|
||||
_ = c.ares_process(global_ares_chan, &readfds, &writefds);
|
||||
}
|
||||
|
||||
std.log.info("Finished Resolving {d} IPs.", .{subnet.total});
|
||||
|
||||
if (args.len == 2) {
|
||||
std.debug.print("\n", .{});
|
||||
var stdout_buffer: [1024]u8 = undefined;
|
||||
var stdout_file_writer: std.Io.File.Writer = .init(.stdout(), init.io, &stdout_buffer);
|
||||
const stdout_writer = &stdout_file_writer.interface;
|
||||
|
||||
for (global_entries.items) |entry| {
|
||||
try stdout_writer.print("{s} -> {s}\n", .{ entry.ip_str, entry.hostname });
|
||||
}
|
||||
try stdout_writer.flush();
|
||||
} else if (args.len == 3) {
|
||||
try toFile(init.io, args[2]);
|
||||
} else {}
|
||||
}
|
||||
Reference in New Issue
Block a user