authorgravatar for git@l4.pmLuna <git@l4.pm> 2019-11-10 10:50:22-03:00
committergravatar for git@l4.pmLuna <git@l4.pm> 2019-11-10 10:50:22-03:00
log25423eb453e1c0cece457f4012106dbcd97f29f3
treef9d03365ed480e52f5e07de7545a16258df9d6f6
parente4704f68f850021543ceab86c253806fdded7733

add errors/panics for unsupported OSes


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

lib/std/net.zig+24-1
......@@ -14,7 +14,10 @@ pub const Address = extern union {
1414 any: os.sockaddr,
1515 in: os.sockaddr_in,
1616 in6: os.sockaddr_in6,
17 un: os.sockaddr_un,
17 un: switch (builtin.os) {
18 .linux, .macosx, .freebsd, .netbsd => os.sockaddr_un,
19 else => void,
20 },
1821
1922 // TODO this crashed the compiler
2023 //pub const localhost = initIp4(parseIp4("127.0.0.1") catch unreachable, 0);
......@@ -216,6 +219,11 @@ pub const Address = extern union {
216219 }
217220
218221 pub fn initUnix(path: []const u8) !Address {
222 switch (builtin.os) {
223 .linux, .macosx, .freebsd, .netbsd => {},
224 else => return error.UnsupportedOS,
225 }
226
219227 var sock_addr = os.sockaddr_un{
220228 .family = os.AF_UNIX,
221229 .path = undefined,
......@@ -331,6 +339,10 @@ pub const Address = extern union {
331339 try std.fmt.format(context, Errors, output, "]:{}", port);
332340 },
333341 os.AF_UNIX => {
342 if (@typeOf(self.un) == void) {
343 @panic("unsupported OS");
344 }
345
334346 try std.fmt.format(context, Errors, output, "{}", self.un.path);
335347 },
336348 else => unreachable,
......@@ -348,6 +360,10 @@ pub const Address = extern union {
348360 os.AF_INET => return @sizeOf(os.sockaddr_in),
349361 os.AF_INET6 => return @sizeOf(os.sockaddr_in6),
350362 os.AF_UNIX => {
363 if (@typeOf(self.un) == void) {
364 @panic("unsupported OS");
365 }
366
351367 const path_len = std.mem.len(u8, &self.un.path);
352368 return @intCast(os.socklen_t, @sizeOf(os.sockaddr_un) - self.un.path.len + path_len);
353369 },
......@@ -1304,6 +1320,13 @@ pub const StreamServer = struct {
13041320 const nonblock = if (std.io.is_async) os.SOCK_NONBLOCK else 0;
13051321 const sock_flags = os.SOCK_STREAM | os.SOCK_CLOEXEC | nonblock;
13061322 const proto = if (address.any.family == os.AF_UNIX) @as(u32, 0) else os.IPPROTO_TCP;
1323 if (address.any.family == os.AF_UNIX and @typeOf(address.un) == void) {
1324 return error{
1325 /// When the OS does not have support for AF_UNIX sockets.
1326 UnsupportedOS,
1327 }.UnsupportedOS;
1328 }
1329
13071330 const sockfd = try os.socket(address.any.family, sock_flags, proto);
13081331 self.sockfd = sockfd;
13091332 errdefer {