| ... | ... | @@ -3,6 +3,7 @@ const builtin = @import("builtin"); |
| 3 | 3 | const nio = @import("nio"); |
| 4 | 4 | const nfs = @import("nfs"); |
| 5 | 5 | const time = @import("time"); |
| 6 | const url = @import("url"); |
| 6 | 7 | const sys_linux = @import("sys-linux"); |
| 7 | 8 | |
| 8 | 9 | const os = builtin.target.os.tag; |
| ... | ... | @@ -27,6 +28,27 @@ pub const Address = extern union { |
| 27 | 28 | return .{ .in6 = Ip6Address.init(addr, hport) }; |
| 28 | 29 | } |
| 29 | 30 | |
| 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 | |
| 30 | 52 | pub fn size(adr: Address) sys.socklen_t { |
| 31 | 53 | return switch (adr.any.family) { |
| 32 | 54 | .INET => @sizeOf(sys.struct_sockaddr_in), |