Skip to content

Commit

Permalink
Merge pull request #164 from Syndica/ink/dns-improvement
Browse files Browse the repository at this point in the history
fix(cmd): DNS improvements
  • Loading branch information
InKryption authored Jun 10, 2024
2 parents 12d9939 + 1a5116d commit b10437c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 37 deletions.
5 changes: 0 additions & 5 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ pub fn build(b: *Build) void {
const httpz_dep = b.dependency("httpz", dep_opts);
const httpz_mod = httpz_dep.module("httpz");

const zigdig_dep = b.dependency("zigdig", dep_opts);
const zigdig_mod = zigdig_dep.module("dns");

const zstd_dep = b.dependency("zstd", dep_opts);
const zstd_mod = zstd_dep.module("zstd");

Expand All @@ -45,7 +42,6 @@ pub fn build(b: *Build) void {
sig_mod.addImport("base58-zig", base58_module);
sig_mod.addImport("zig-cli", zig_cli_module);
sig_mod.addImport("httpz", httpz_mod);
sig_mod.addImport("zigdig", zigdig_mod);
sig_mod.addImport("zstd", zstd_mod);
sig_mod.addImport("curl", curl_mod);

Expand All @@ -62,7 +58,6 @@ pub fn build(b: *Build) void {
sig_exe.root_module.addImport("httpz", httpz_mod);
sig_exe.root_module.addImport("zig-cli", zig_cli_module);
sig_exe.root_module.addImport("zig-network", zig_network_module);
sig_exe.root_module.addImport("zigdig", zigdig_mod);
sig_exe.root_module.addImport("zstd", zstd_mod);

const main_exe_run = b.addRunArtifact(sig_exe);
Expand Down
4 changes: 0 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
.url = "https://github.com/karlseguin/http.zig/archive/d05b9889382d240c826c7654a999e4da515bd20a.tar.gz",
.hash = "1220833d40f1767fa46b0efc4ae1af9bd5880a8e6fe7a66ee0215be916263f149e40",
},
.zigdig = .{
.url = "https://github.com/lun-4/zigdig/archive/6d872bb061a85e416304b379ce3f3b868d6e835c.tar.gz",
.hash = "1220fe113318a795d366cd63d2d3f1abacfaa6ff0739bc3240eaf7b68b06d21e4917",
},
.zstd = .{
.url = "https://github.com/Syndica/zstd.zig/archive/a052e839a3dfc44feb00c2eb425815baa3c76e0d.tar.gz",
.hash = "122001d56e43ef94e31243739ae83d7508abf0b8102795aff1ac91446e7ff450d875",
Expand Down
58 changes: 30 additions & 28 deletions src/cmd/cmd.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const std = @import("std");
const base58 = @import("base58-zig");
const cli = @import("zig-cli");
const dns = @import("zigdig");
const network = @import("zig-network");
const helpers = @import("helpers.zig");
const sig = @import("../lib.zig");
Expand Down Expand Up @@ -585,54 +584,57 @@ fn getMyDataFromIpEcho(

fn getEntrypoints(logger: Logger) !std.ArrayList(SocketAddr) {
var entrypoints = std.ArrayList(SocketAddr).init(gpa_allocator);
errdefer entrypoints.deinit();

const EntrypointSet = std.AutoArrayHashMap(SocketAddr, void);
var entrypoint_set = EntrypointSet.init(gpa_allocator);
defer entrypoint_set.deinit();

try entrypoint_set.ensureTotalCapacity(config.current.gossip.entrypoints.len);
try entrypoints.ensureTotalCapacityPrecise(config.current.gossip.entrypoints.len);

for (config.current.gossip.entrypoints) |entrypoint| {
const socket_addr = SocketAddr.parse(entrypoint) catch brk: {
// if we couldn't parse as IpV4, we attempt to resolve DNS and get IP
var domain_and_port = std.mem.splitScalar(u8, entrypoint, ':');
const domain_str = domain_and_port.next() orelse {
logger.field("entrypoint", entrypoint).err("entrypoint domain missing");
return error.EntrypointDomainMissing;
};
const port_str = domain_and_port.next() orelse {
const domain_port_sep = std.mem.indexOfScalar(u8, entrypoint, ':') orelse {
logger.field("entrypoint", entrypoint).err("entrypoint port missing");
return error.EntrypointPortMissing;
};
const domain_str = entrypoint[0..domain_port_sep];
if (domain_str.len == 0) {
logger.errf("'{s}': entrypoint domain not valid", .{entrypoint});
return error.EntrypointDomainNotValid;
}
// parse port from string
const port = std.fmt.parseInt(u16, entrypoint[domain_port_sep + 1 ..], 10) catch {
logger.errf("'{s}': entrypoint port not valid", .{entrypoint});
return error.EntrypointPortNotValid;
};

// get dns address lists
var addr_list = try dns.helpers.getAddressList(domain_str, gpa_allocator);
const addr_list = try std.net.getAddressList(gpa_allocator, domain_str, port);
defer addr_list.deinit();

if (addr_list.addrs.len == 0) {
logger.field("entrypoint", entrypoint).err("entrypoint resolve dns failed (no records found)");
logger.errf("'{s}': entrypoint resolve dns failed (no records found)", .{entrypoint});
return error.EntrypointDnsResolutionFailure;
}

// use first A record address
const ipv4_addr = addr_list.addrs[0];

// parse port from string
const port = std.fmt.parseInt(u16, port_str, 10) catch {
logger.field("entrypoint", entrypoint).err("entrypoint port not valid");
return error.EntrypointPortNotValid;
};

var socket_addr = SocketAddr.fromIpV4Address(ipv4_addr);
socket_addr.setPort(port);
const socket_addr = SocketAddr.fromIpV4Address(ipv4_addr);
std.debug.assert(socket_addr.port() == port);
break :brk socket_addr;
};

try entrypoints.append(socket_addr);
const gop = entrypoint_set.getOrPutAssumeCapacity(socket_addr);
if (!gop.found_existing) {
entrypoints.appendAssumeCapacity(socket_addr);
}
}

// log entrypoints
var entrypoint_string = try gpa_allocator.alloc(u8, 53 * entrypoints.items.len);
defer gpa_allocator.free(entrypoint_string);
var stream = std.io.fixedBufferStream(entrypoint_string);
var writer = stream.writer();
for (0.., entrypoints.items) |i, entrypoint| {
try entrypoint.toAddress().format("", .{}, writer);
if (i != entrypoints.items.len - 1) try writer.writeAll(", ");
}
logger.infof("entrypoints: {s}", .{entrypoint_string[0..stream.pos]});
logger.infof("entrypoints: {any}", .{entrypoints.items});

return entrypoints;
}
Expand Down

0 comments on commit b10437c

Please sign in to comment.