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 {...@@ -14,7 +14,7 @@ pub const Address = extern union {
14 any: os.sockaddr,14 any: os.sockaddr,
15 in: os.sockaddr_in,15 in: os.sockaddr_in,
16 in6: os.sockaddr_in6,16 in6: os.sockaddr_in6,
17 unix: os.sockaddr_un,17 un: os.sockaddr_un,
1818
19 // TODO this crashed the compiler19 // TODO this crashed the compiler
20 //pub const localhost = initIp4(parseIp4("127.0.0.1") catch unreachable, 0);20 //pub const localhost = initIp4(parseIp4("127.0.0.1") catch unreachable, 0);
...@@ -40,6 +40,18 @@ pub const Address = extern union {...@@ -40,6 +40,18 @@ pub const Address = extern union {
40 return error.InvalidIPAddressFormat;40 return error.InvalidIPAddressFormat;
41 }41 }
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
43 pub fn parseExpectingFamily(name: []const u8, family: os.sa_family_t, port: u16) !Address {55 pub fn parseExpectingFamily(name: []const u8, family: os.sa_family_t, port: u16) !Address {
44 switch (family) {56 switch (family) {
45 os.AF_INET => return parseIp4(name, port),57 os.AF_INET => return parseIp4(name, port),
...@@ -315,6 +327,9 @@ pub const Address = extern union {...@@ -315,6 +327,9 @@ pub const Address = extern union {
315 }327 }
316 try std.fmt.format(context, Errors, output, "]:{}", port);328 try std.fmt.format(context, Errors, output, "]:{}", port);
317 },329 },
330 os.AF_UNIX => {
331 try std.fmt.format(context, Errors, output, "{}", self.un.path);
332 },
318 else => unreachable,333 else => unreachable,
319 }334 }
320 }335 }