diff --git a/licenses.txt b/licenses.txt index 0b78eaa5543a705ac8380756874d1f7e7634a8d0..b5c0749538285657740beb932eb0b6041ea1d401 100644 --- a/licenses.txt +++ b/licenses.txt @@ -3,6 +3,8 @@ MPL-2.0: - This - git https://github.com/nektro/zig-nfs - git https://github.com/nektro/zig-nio +- git https://github.com/nektro/zig-unicode-idna +- git https://github.com/nektro/zig-whatwg-url MIT: = https://spdx.org/licenses/MIT @@ -10,6 +12,7 @@ MIT: - git https://github.com/nektro/zig-extras - git https://github.com/nektro/zig-sys-linux - git https://github.com/nektro/zig-time +- git https://github.com/nektro/zig-unicode-ucd Unspecified: - system_lib c diff --git a/net.zig b/net.zig index 326077278cba3e5eaa4346b251dc18f17f50518e..09bd81caffc4db78d019ac9e15587735a099770f 100644 --- a/net.zig +++ b/net.zig @@ -3,6 +3,7 @@ const builtin = @import("builtin"); const nio = @import("nio"); const nfs = @import("nfs"); const time = @import("time"); +const url = @import("url"); const sys_linux = @import("sys-linux"); const os = builtin.target.os.tag; @@ -27,6 +28,27 @@ pub const Address = extern union { return .{ .in6 = Ip6Address.init(addr, hport) }; } + pub fn fromUrl(u: *const url.URL, allocator: std.mem.Allocator) !Address { + return switch (u.hostFancy()) { + .unset => unreachable, + .ipv4 => |int| .initIp4(@bitCast(int), u.portFancy().?), + .ipv6 => |int| .initIp6(@bitCast(int), u.portFancy().?), + .name => |hostname| blk: { + const hostnamez = try allocator.dupeZ(u8, hostname); + defer allocator.free(hostnamez); + const portz = try std.fmt.allocPrintZ(allocator, "{d}", .{u.portFancy().?}); + defer allocator.free(portz); + const gai = try getaddrinfo(hostnamez, portz, null); + defer freeaddrinfo(gai); + break :blk switch (gai.addr.?.family) { + .INET => .{ .in = .{ .sa = @as(*Ip4Address.SockAddr, @ptrCast(@alignCast(gai.addr.?))).* } }, + .INET6 => .{ .in6 = .{ .sa = @as(*Ip6Address.SockAddr, @ptrCast(@alignCast(gai.addr.?))).* } }, + else => @panic("TODO"), + }; + }, + }; + } + pub fn size(adr: Address) sys.socklen_t { return switch (adr.any.family) { .INET => @sizeOf(sys.struct_sockaddr_in), diff --git a/zigmod.yml b/zigmod.yml index 749b06d5fbf34e3d3d99bab31e9e30238f16a370..1a87a86268e26e966c434277b9dee0966025bf81 100644 --- a/zigmod.yml +++ b/zigmod.yml @@ -8,5 +8,6 @@ dependencies: - src: git https://github.com/nektro/zig-sys-linux - src: git https://github.com/nektro/zig-nfs - src: git https://github.com/nektro/zig-time + - src: git https://github.com/nektro/zig-whatwg-url root_dependencies: - src: git https://github.com/nektro/zig-expect