From c2325053a86f4350e20ea3b476c7efeb942d8438 Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 8 Nov 2019 21:44:17 -0300 Subject: [PATCH] add Address.parseUnix and Address.format support for AF_UNIX --- lib/std/net.zig | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/std/net.zig b/lib/std/net.zig index 215c0ed6f..af3ac5475 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -14,7 +14,7 @@ pub const Address = extern union { any: os.sockaddr, in: os.sockaddr_in, in6: os.sockaddr_in6, - unix: os.sockaddr_un, + un: os.sockaddr_un, // TODO this crashed the compiler //pub const localhost = initIp4(parseIp4("127.0.0.1") catch unreachable, 0); @@ -40,6 +40,18 @@ pub const Address = extern union { return error.InvalidIPAddressFormat; } + pub fn parseUnix(path: []const u8) !Address { + var sock_addr = os.sockaddr_un{ + .family = os.AF_UNIX, + .path = undefined, + }; + + if (path.len > sock_addr.path.len) return error.NameTooLong; + mem.copy(u8, &sock_addr.path, path); + + return Address{ .un = sock_addr }; + } + pub fn parseExpectingFamily(name: []const u8, family: os.sa_family_t, port: u16) !Address { switch (family) { os.AF_INET => return parseIp4(name, port), @@ -315,6 +327,9 @@ pub const Address = extern union { } try std.fmt.format(context, Errors, output, "]:{}", port); }, + os.AF_UNIX => { + try std.fmt.format(context, Errors, output, "{}", self.un.path); + }, else => unreachable, } }