add Address.parseUnix and Address.format support for AF_UNIX

master
Luna 2019-11-08 21:44:17 -03:00
parent 9458620e18
commit c2325053a8
1 changed files with 16 additions and 1 deletions

View File

@ -14,7 +14,7 @@ pub const Address = extern union {
any: os.sockaddr, any: os.sockaddr,
in: os.sockaddr_in, in: os.sockaddr_in,
in6: os.sockaddr_in6, in6: os.sockaddr_in6,
unix: os.sockaddr_un, un: os.sockaddr_un,
// TODO this crashed the compiler // TODO this crashed the compiler
//pub const localhost = initIp4(parseIp4("127.0.0.1") catch unreachable, 0); //pub const localhost = initIp4(parseIp4("127.0.0.1") catch unreachable, 0);
@ -40,6 +40,18 @@ pub const Address = extern union {
return error.InvalidIPAddressFormat; 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 { pub fn parseExpectingFamily(name: []const u8, family: os.sa_family_t, port: u16) !Address {
switch (family) { switch (family) {
os.AF_INET => return parseIp4(name, port), os.AF_INET => return parseIp4(name, port),
@ -315,6 +327,9 @@ pub const Address = extern union {
} }
try std.fmt.format(context, Errors, output, "]:{}", port); try std.fmt.format(context, Errors, output, "]:{}", port);
}, },
os.AF_UNIX => {
try std.fmt.format(context, Errors, output, "{}", self.un.path);
},
else => unreachable, else => unreachable,
} }
} }