authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-04-10 02:00:17 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-04-10 02:00:17 -07:00
log56c4bc345e8df284d13756250f687a974290eb9e
tree655a00c35be4826eadcf92f676f5b1910367247a
parent88b2ea1d0ca84bf0645bdae099e5b550bf6987cc
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

add Address.fromUrl()


3 files changed, 26 insertions(+), 0 deletions(-)

licenses.txt+3
......@@ -3,6 +3,8 @@ MPL-2.0:
33- This
44- git https://github.com/nektro/zig-nfs
55- git https://github.com/nektro/zig-nio
6- git https://github.com/nektro/zig-unicode-idna
7- git https://github.com/nektro/zig-whatwg-url
68
79MIT:
810= https://spdx.org/licenses/MIT
......@@ -10,6 +12,7 @@ MIT:
1012- git https://github.com/nektro/zig-extras
1113- git https://github.com/nektro/zig-sys-linux
1214- git https://github.com/nektro/zig-time
15- git https://github.com/nektro/zig-unicode-ucd
1316
1417Unspecified:
1518- system_lib c
net.zig+22
......@@ -3,6 +3,7 @@ const builtin = @import("builtin");
33const nio = @import("nio");
44const nfs = @import("nfs");
55const time = @import("time");
6const url = @import("url");
67const sys_linux = @import("sys-linux");
78
89const os = builtin.target.os.tag;
......@@ -27,6 +28,27 @@ pub const Address = extern union {
2728 return .{ .in6 = Ip6Address.init(addr, hport) };
2829 }
2930
31 pub fn fromUrl(u: *const url.URL, allocator: std.mem.Allocator) !Address {
32 return switch (u.hostFancy()) {
33 .unset => unreachable,
34 .ipv4 => |int| .initIp4(@bitCast(int), u.portFancy().?),
35 .ipv6 => |int| .initIp6(@bitCast(int), u.portFancy().?),
36 .name => |hostname| blk: {
37 const hostnamez = try allocator.dupeZ(u8, hostname);
38 defer allocator.free(hostnamez);
39 const portz = try std.fmt.allocPrintZ(allocator, "{d}", .{u.portFancy().?});
40 defer allocator.free(portz);
41 const gai = try getaddrinfo(hostnamez, portz, null);
42 defer freeaddrinfo(gai);
43 break :blk switch (gai.addr.?.family) {
44 .INET => .{ .in = .{ .sa = @as(*Ip4Address.SockAddr, @ptrCast(@alignCast(gai.addr.?))).* } },
45 .INET6 => .{ .in6 = .{ .sa = @as(*Ip6Address.SockAddr, @ptrCast(@alignCast(gai.addr.?))).* } },
46 else => @panic("TODO"),
47 };
48 },
49 };
50 }
51
3052 pub fn size(adr: Address) sys.socklen_t {
3153 return switch (adr.any.family) {
3254 .INET => @sizeOf(sys.struct_sockaddr_in),
zigmod.yml+1
......@@ -8,5 +8,6 @@ dependencies:
88 - src: git https://github.com/nektro/zig-sys-linux
99 - src: git https://github.com/nektro/zig-nfs
1010 - src: git https://github.com/nektro/zig-time
11 - src: git https://github.com/nektro/zig-whatwg-url
1112root_dependencies:
1213 - src: git https://github.com/nektro/zig-expect