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 {...@@ -14,7 +14,10 @@ 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 un: os.sockaddr_un,17 un: switch (builtin.os) {
18 .linux, .macosx, .freebsd, .netbsd => os.sockaddr_un,
19 else => void,
20 },
1821
19 // TODO this crashed the compiler22 // TODO this crashed the compiler
20 //pub const localhost = initIp4(parseIp4("127.0.0.1") catch unreachable, 0);23 //pub const localhost = initIp4(parseIp4("127.0.0.1") catch unreachable, 0);
...@@ -216,6 +219,11 @@ pub const Address = extern union {...@@ -216,6 +219,11 @@ pub const Address = extern union {
216 }219 }
217220
218 pub fn initUnix(path: []const u8) !Address {221 pub fn initUnix(path: []const u8) !Address {
222 switch (builtin.os) {
223 .linux, .macosx, .freebsd, .netbsd => {},
224 else => return error.UnsupportedOS,
225 }
226
219 var sock_addr = os.sockaddr_un{227 var sock_addr = os.sockaddr_un{
220 .family = os.AF_UNIX,228 .family = os.AF_UNIX,
221 .path = undefined,229 .path = undefined,
...@@ -331,6 +339,10 @@ pub const Address = extern union {...@@ -331,6 +339,10 @@ pub const Address = extern union {
331 try std.fmt.format(context, Errors, output, "]:{}", port);339 try std.fmt.format(context, Errors, output, "]:{}", port);
332 },340 },
333 os.AF_UNIX => {341 os.AF_UNIX => {
342 if (@typeOf(self.un) == void) {
343 @panic("unsupported OS");
344 }
345
334 try std.fmt.format(context, Errors, output, "{}", self.un.path);346 try std.fmt.format(context, Errors, output, "{}", self.un.path);
335 },347 },
336 else => unreachable,348 else => unreachable,
...@@ -348,6 +360,10 @@ pub const Address = extern union {...@@ -348,6 +360,10 @@ pub const Address = extern union {
348 os.AF_INET => return @sizeOf(os.sockaddr_in),360 os.AF_INET => return @sizeOf(os.sockaddr_in),
349 os.AF_INET6 => return @sizeOf(os.sockaddr_in6),361 os.AF_INET6 => return @sizeOf(os.sockaddr_in6),
350 os.AF_UNIX => {362 os.AF_UNIX => {
363 if (@typeOf(self.un) == void) {
364 @panic("unsupported OS");
365 }
366
351 const path_len = std.mem.len(u8, &self.un.path);367 const path_len = std.mem.len(u8, &self.un.path);
352 return @intCast(os.socklen_t, @sizeOf(os.sockaddr_un) - self.un.path.len + path_len);368 return @intCast(os.socklen_t, @sizeOf(os.sockaddr_un) - self.un.path.len + path_len);
353 },369 },
...@@ -1304,6 +1320,13 @@ pub const StreamServer = struct {...@@ -1304,6 +1320,13 @@ pub const StreamServer = struct {
1304 const nonblock = if (std.io.is_async) os.SOCK_NONBLOCK else 0;1320 const nonblock = if (std.io.is_async) os.SOCK_NONBLOCK else 0;
1305 const sock_flags = os.SOCK_STREAM | os.SOCK_CLOEXEC | nonblock;1321 const sock_flags = os.SOCK_STREAM | os.SOCK_CLOEXEC | nonblock;
1306 const proto = if (address.any.family == os.AF_UNIX) @as(u32, 0) else os.IPPROTO_TCP;1322 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
1307 const sockfd = try os.socket(address.any.family, sock_flags, proto);1330 const sockfd = try os.socket(address.any.family, sock_flags, proto);
1308 self.sockfd = sockfd;1331 self.sockfd = sockfd;
1309 errdefer {1332 errdefer {