diff options
| author | Luna <git@l4.pm> | 2019-11-08 21:44:17 -0300 |
|---|---|---|
| committer | Luna <git@l4.pm> | 2019-11-08 21:44:17 -0300 |
| commit | c2325053a86f4350e20ea3b476c7efeb942d8438 (patch) | |
| tree | d20d4742a6d5c6f92525374ab8f01a1e37be5159 /lib/std/net.zig | |
| parent | 9458620e18cb89b71cd0ad2755b9a7ae4f63e846 (diff) | |
| download | zig-c2325053a86f4350e20ea3b476c7efeb942d8438.tar.gz zig-c2325053a86f4350e20ea3b476c7efeb942d8438.zip | |
add Address.parseUnix and Address.format support for AF_UNIX
Diffstat (limited to 'lib/std/net.zig')
| -rw-r--r-- | lib/std/net.zig | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/lib/std/net.zig b/lib/std/net.zig index 215c0ed6fa..af3ac5475d 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, } } |
