authorgravatar for git@l4.pmLuna <git@l4.pm> 2019-11-08 21:44:17-03:00
committergravatar for git@l4.pmLuna <git@l4.pm> 2019-11-08 21:44:17-03:00
logc2325053a86f4350e20ea3b476c7efeb942d8438
treed20d4742a6d5c6f92525374ab8f01a1e37be5159
parent9458620e18cb89b71cd0ad2755b9a7ae4f63e846

add Address.parseUnix and Address.format support for AF_UNIX


1 files changed, 16 insertions(+), 1 deletions(-)

lib/std/net.zig+16-1
......@@ -14,7 +14,7 @@ pub const Address = extern union {
1414 any: os.sockaddr,
1515 in: os.sockaddr_in,
1616 in6: os.sockaddr_in6,
17 unix: os.sockaddr_un,
17 un: os.sockaddr_un,
1818
1919 // TODO this crashed the compiler
2020 //pub const localhost = initIp4(parseIp4("127.0.0.1") catch unreachable, 0);
......@@ -40,6 +40,18 @@ pub const Address = extern union {
4040 return error.InvalidIPAddressFormat;
4141 }
4242
43 pub fn parseUnix(path: []const u8) !Address {
44 var sock_addr = os.sockaddr_un{
45 .family = os.AF_UNIX,
46 .path = undefined,
47 };
48
49 if (path.len > sock_addr.path.len) return error.NameTooLong;
50 mem.copy(u8, &sock_addr.path, path);
51
52 return Address{ .un = sock_addr };
53 }
54
4355 pub fn parseExpectingFamily(name: []const u8, family: os.sa_family_t, port: u16) !Address {
4456 switch (family) {
4557 os.AF_INET => return parseIp4(name, port),
......@@ -315,6 +327,9 @@ pub const Address = extern union {
315327 }
316328 try std.fmt.format(context, Errors, output, "]:{}", port);
317329 },
330 os.AF_UNIX => {
331 try std.fmt.format(context, Errors, output, "{}", self.un.path);
332 },
318333 else => unreachable,
319334 }
320335 }