| ... | ... | @@ -14,7 +14,10 @@ pub const Address = extern union { |
| 14 | 14 | any: os.sockaddr, |
| 15 | 15 | in: os.sockaddr_in, |
| 16 | 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 | }, |
| 18 | 21 | |
| 19 | 22 | // TODO this crashed the compiler |
| 20 | 23 | //pub const localhost = initIp4(parseIp4("127.0.0.1") catch unreachable, 0); |
| ... | ... | @@ -216,6 +219,11 @@ pub const Address = extern union { |
| 216 | 219 | } |
| 217 | 220 | |
| 218 | 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 | 227 | var sock_addr = os.sockaddr_un{ |
| 220 | 228 | .family = os.AF_UNIX, |
| 221 | 229 | .path = undefined, |
| ... | ... | @@ -331,6 +339,10 @@ pub const Address = extern union { |
| 331 | 339 | try std.fmt.format(context, Errors, output, "]:{}", port); |
| 332 | 340 | }, |
| 333 | 341 | os.AF_UNIX => { |
| 342 | if (@typeOf(self.un) == void) { |
| 343 | @panic("unsupported OS"); |
| 344 | } |
| 345 | |
| 334 | 346 | try std.fmt.format(context, Errors, output, "{}", self.un.path); |
| 335 | 347 | }, |
| 336 | 348 | else => unreachable, |
| ... | ... | @@ -348,6 +360,10 @@ pub const Address = extern union { |
| 348 | 360 | os.AF_INET => return @sizeOf(os.sockaddr_in), |
| 349 | 361 | os.AF_INET6 => return @sizeOf(os.sockaddr_in6), |
| 350 | 362 | os.AF_UNIX => { |
| 363 | if (@typeOf(self.un) == void) { |
| 364 | @panic("unsupported OS"); |
| 365 | } |
| 366 | |
| 351 | 367 | const path_len = std.mem.len(u8, &self.un.path); |
| 352 | 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 | 1320 | const nonblock = if (std.io.is_async) os.SOCK_NONBLOCK else 0; |
| 1305 | 1321 | const sock_flags = os.SOCK_STREAM | os.SOCK_CLOEXEC | nonblock; |
| 1306 | 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 | 1330 | const sockfd = try os.socket(address.any.family, sock_flags, proto); |
| 1308 | 1331 | self.sockfd = sockfd; |
| 1309 | 1332 | errdefer { |